Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 14 | |
| 15 | #ifndef BUFFET_SHILL_CLIENT_H_ |
| 16 | #define BUFFET_SHILL_CLIENT_H_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <set> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <base/callback.h> |
| 24 | #include <base/cancelable_callback.h> |
| 25 | #include <base/macros.h> |
| 26 | #include <base/memory/ref_counted.h> |
| 27 | #include <base/memory/weak_ptr.h> |
| 28 | #include <dbus/bus.h> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 29 | #include <shill/dbus-proxies.h> |
| 30 | #include <weave/provider/network.h> |
| 31 | #include <weave/provider/wifi.h> |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace buffet { |
| 34 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 35 | class ApManagerClient; |
| 36 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 37 | class ShillClient final : public weave::provider::Network, |
| 38 | public weave::provider::Wifi { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 39 | public: |
| 40 | ShillClient(const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 41 | const std::set<std::string>& device_whitelist, |
| 42 | bool disable_xmpp); |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 43 | ~ShillClient(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 44 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 45 | // NetworkProvider implementation. |
| 46 | void AddConnectionChangedCallback( |
| 47 | const ConnectionChangedCallback& listener) override; |
| 48 | State GetConnectionState() const override; |
| 49 | void OpenSslSocket(const std::string& host, |
| 50 | uint16_t port, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 51 | const OpenSslSocketCallback& callback) override; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 52 | |
| 53 | // WifiProvider implementation. |
| 54 | void Connect(const std::string& ssid, |
| 55 | const std::string& passphrase, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 56 | const weave::DoneCallback& callback) override; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 57 | void StartAccessPoint(const std::string& ssid) override; |
| 58 | void StopAccessPoint() override; |
Alex Vakulenko | d8f3699 | 2016-02-02 12:28:47 -0800 | [diff] [blame] | 59 | bool IsWifi24Supported() const override { return true; } |
| 60 | // TODO(avakulenko): See if we can get appropriate information from Shill |
| 61 | // regarding 5.0 GHz support. |
| 62 | bool IsWifi50Supported() const override { return false; } |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | struct DeviceState { |
| 66 | std::unique_ptr<org::chromium::flimflam::DeviceProxy> device; |
| 67 | // ServiceProxy objects are shared because the connecting service will |
| 68 | // also be the selected service for a device, but is not always the selected |
| 69 | // service (for instance, in the period between configuring a WiFi service |
| 70 | // with credentials, and when Connect() is called.) |
| 71 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> selected_service; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 72 | State service_state{State::kOffline}; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Vitaly Buka | 5d14dca | 2015-11-05 22:26:19 -0800 | [diff] [blame] | 75 | void Init(); |
| 76 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 77 | bool IsMonitoredDevice(org::chromium::flimflam::DeviceProxy* device); |
| 78 | void OnShillServiceOwnerChange(const std::string& old_owner, |
| 79 | const std::string& new_owner); |
| 80 | void OnManagerPropertyChangeRegistration(const std::string& interface, |
| 81 | const std::string& signal_name, |
| 82 | bool success); |
| 83 | void OnManagerPropertyChange(const std::string& property_name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 84 | const brillo::Any& property_value); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 85 | void OnDevicePropertyChangeRegistration(const dbus::ObjectPath& device_path, |
| 86 | const std::string& interface, |
| 87 | const std::string& signal_name, |
| 88 | bool success); |
| 89 | void OnDevicePropertyChange(const dbus::ObjectPath& device_path, |
| 90 | const std::string& property_name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 91 | const brillo::Any& property_value); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 92 | void OnServicePropertyChangeRegistration(const dbus::ObjectPath& path, |
| 93 | const std::string& interface, |
| 94 | const std::string& signal_name, |
| 95 | bool success); |
| 96 | void OnServicePropertyChange(const dbus::ObjectPath& service_path, |
| 97 | const std::string& property_name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 98 | const brillo::Any& property_value); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 99 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 100 | void OnStateChangeForConnectingService(const std::string& state); |
| 101 | void OnErrorChangeForConnectingService(const std::string& error); |
| 102 | void OnStrengthChangeForConnectingService(uint8_t signal_strength); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 103 | void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path, |
| 104 | const std::string& state); |
| 105 | void UpdateConnectivityState(); |
| 106 | void NotifyConnectivityListeners(bool am_online); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 107 | // Clean up state related to a connecting service. |
| 108 | void CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 109 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 110 | void ConnectToServiceError( |
| 111 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> |
| 112 | connecting_service); |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 113 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 114 | const scoped_refptr<dbus::Bus> bus_; |
| 115 | org::chromium::flimflam::ManagerProxy manager_proxy_; |
| 116 | // There is logic that assumes we will never change this device list |
| 117 | // in OnManagerPropertyChange. Do not be tempted to remove this const. |
| 118 | const std::set<std::string> device_whitelist_; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 119 | bool disable_xmpp_{false}; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 120 | std::vector<ConnectionChangedCallback> connectivity_listeners_; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 121 | |
| 122 | // State for tracking where we are in our attempts to connect to a service. |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 123 | bool have_called_connect_{false}; |
| 124 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 125 | std::string connecting_service_error_; |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 126 | weave::DoneCallback connect_done_callback_; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 127 | |
| 128 | // State for tracking our online connectivity. |
| 129 | std::map<dbus::ObjectPath, DeviceState> devices_; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 130 | State connectivity_state_{State::kOffline}; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 131 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 132 | std::unique_ptr<ApManagerClient> ap_manager_client_; |
| 133 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 134 | base::WeakPtrFactory<ShillClient> weak_factory_{this}; |
| 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(ShillClient); |
| 137 | }; |
| 138 | |
| 139 | } // namespace buffet |
| 140 | |
| 141 | #endif // BUFFET_SHILL_CLIENT_H_ |