Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BUFFET_SHILL_CLIENT_H_ |
| 6 | #define BUFFET_SHILL_CLIENT_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/callback.h> |
| 14 | #include <base/cancelable_callback.h> |
| 15 | #include <base/macros.h> |
| 16 | #include <base/memory/ref_counted.h> |
| 17 | #include <base/memory/weak_ptr.h> |
| 18 | #include <dbus/bus.h> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 19 | #include <shill/dbus-proxies.h> |
| 20 | #include <weave/provider/network.h> |
| 21 | #include <weave/provider/wifi.h> |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 22 | |
| 23 | namespace buffet { |
| 24 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 25 | class ApManagerClient; |
| 26 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 27 | class ShillClient final : public weave::provider::Network, |
| 28 | public weave::provider::Wifi { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 29 | public: |
| 30 | ShillClient(const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 31 | const std::set<std::string>& device_whitelist, |
| 32 | bool disable_xmpp); |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 33 | ~ShillClient(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 34 | |
| 35 | void Init(); |
| 36 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 37 | // NetworkProvider implementation. |
| 38 | void AddConnectionChangedCallback( |
| 39 | const ConnectionChangedCallback& listener) override; |
| 40 | State GetConnectionState() const override; |
| 41 | void OpenSslSocket(const std::string& host, |
| 42 | uint16_t port, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 43 | const OpenSslSocketCallback& callback) override; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 44 | |
| 45 | // WifiProvider implementation. |
| 46 | void Connect(const std::string& ssid, |
| 47 | const std::string& passphrase, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 48 | const weave::DoneCallback& callback) override; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 49 | void StartAccessPoint(const std::string& ssid) override; |
| 50 | void StopAccessPoint() override; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | struct DeviceState { |
| 54 | std::unique_ptr<org::chromium::flimflam::DeviceProxy> device; |
| 55 | // ServiceProxy objects are shared because the connecting service will |
| 56 | // also be the selected service for a device, but is not always the selected |
| 57 | // service (for instance, in the period between configuring a WiFi service |
| 58 | // with credentials, and when Connect() is called.) |
| 59 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> selected_service; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 60 | State service_state{State::kOffline}; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | bool IsMonitoredDevice(org::chromium::flimflam::DeviceProxy* device); |
| 64 | void OnShillServiceOwnerChange(const std::string& old_owner, |
| 65 | const std::string& new_owner); |
| 66 | void OnManagerPropertyChangeRegistration(const std::string& interface, |
| 67 | const std::string& signal_name, |
| 68 | bool success); |
| 69 | void OnManagerPropertyChange(const std::string& property_name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 70 | const brillo::Any& property_value); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 71 | void OnDevicePropertyChangeRegistration(const dbus::ObjectPath& device_path, |
| 72 | const std::string& interface, |
| 73 | const std::string& signal_name, |
| 74 | bool success); |
| 75 | void OnDevicePropertyChange(const dbus::ObjectPath& device_path, |
| 76 | const std::string& property_name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 77 | const brillo::Any& property_value); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 78 | void OnServicePropertyChangeRegistration(const dbus::ObjectPath& path, |
| 79 | const std::string& interface, |
| 80 | const std::string& signal_name, |
| 81 | bool success); |
| 82 | void OnServicePropertyChange(const dbus::ObjectPath& service_path, |
| 83 | 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 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 86 | void OnStateChangeForConnectingService(const std::string& state); |
| 87 | void OnErrorChangeForConnectingService(const std::string& error); |
| 88 | void OnStrengthChangeForConnectingService(uint8_t signal_strength); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 89 | void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path, |
| 90 | const std::string& state); |
| 91 | void UpdateConnectivityState(); |
| 92 | void NotifyConnectivityListeners(bool am_online); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 93 | // Clean up state related to a connecting service. |
| 94 | void CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 95 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 96 | void ConnectToServiceError( |
| 97 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> |
| 98 | connecting_service); |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 99 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 100 | const scoped_refptr<dbus::Bus> bus_; |
| 101 | org::chromium::flimflam::ManagerProxy manager_proxy_; |
| 102 | // There is logic that assumes we will never change this device list |
| 103 | // in OnManagerPropertyChange. Do not be tempted to remove this const. |
| 104 | const std::set<std::string> device_whitelist_; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 105 | bool disable_xmpp_{false}; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 106 | std::vector<ConnectionChangedCallback> connectivity_listeners_; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 107 | |
| 108 | // 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] | 109 | bool have_called_connect_{false}; |
| 110 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 111 | std::string connecting_service_error_; |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 112 | weave::DoneCallback connect_done_callback_; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 113 | |
| 114 | // State for tracking our online connectivity. |
| 115 | std::map<dbus::ObjectPath, DeviceState> devices_; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 116 | State connectivity_state_{State::kOffline}; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 117 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 118 | std::unique_ptr<ApManagerClient> ap_manager_client_; |
| 119 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 120 | base::WeakPtrFactory<ShillClient> weak_factory_{this}; |
| 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(ShillClient); |
| 123 | }; |
| 124 | |
| 125 | } // namespace buffet |
| 126 | |
| 127 | #endif // BUFFET_SHILL_CLIENT_H_ |