blob: bca5f34837f61ee37b7dc4002d9b35135763e201 [file] [log] [blame]
Vitaly Buka58a288b2015-07-31 00:33:31 -07001// 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 Vakulenkoe32375b2015-09-28 08:55:40 -070019#include <shill/dbus-proxies.h>
20#include <weave/provider/network.h>
21#include <weave/provider/wifi.h>
Vitaly Buka58a288b2015-07-31 00:33:31 -070022
23namespace buffet {
24
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070025class ApManagerClient;
26
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070027class ShillClient final : public weave::provider::Network,
28 public weave::provider::Wifi {
Vitaly Buka58a288b2015-07-31 00:33:31 -070029 public:
30 ShillClient(const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko0022b752015-10-02 11:09:59 -070031 const std::set<std::string>& device_whitelist,
32 bool disable_xmpp);
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070033 ~ShillClient();
Vitaly Buka58a288b2015-07-31 00:33:31 -070034
35 void Init();
36
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070037 // 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 Vakulenko94f8eba2015-10-14 08:52:45 -070043 const OpenSslSocketCallback& callback) override;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070044
45 // WifiProvider implementation.
46 void Connect(const std::string& ssid,
47 const std::string& passphrase,
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070048 const weave::DoneCallback& callback) override;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070049 void StartAccessPoint(const std::string& ssid) override;
50 void StopAccessPoint() override;
Vitaly Buka58a288b2015-07-31 00:33:31 -070051
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 Vakulenkoe32375b2015-09-28 08:55:40 -070060 State service_state{State::kOffline};
Vitaly Buka58a288b2015-07-31 00:33:31 -070061 };
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 Vakulenko41705852015-10-13 10:12:06 -070070 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070071 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 Vakulenko41705852015-10-13 10:12:06 -070077 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070078 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 Vakulenko41705852015-10-13 10:12:06 -070084 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070085
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070086 void OnStateChangeForConnectingService(const std::string& state);
87 void OnErrorChangeForConnectingService(const std::string& error);
88 void OnStrengthChangeForConnectingService(uint8_t signal_strength);
Vitaly Buka58a288b2015-07-31 00:33:31 -070089 void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path,
90 const std::string& state);
91 void UpdateConnectivityState();
92 void NotifyConnectivityListeners(bool am_online);
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070093 // Clean up state related to a connecting service.
94 void CleanupConnectingService();
Vitaly Buka58a288b2015-07-31 00:33:31 -070095
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070096 void ConnectToServiceError(
97 std::shared_ptr<org::chromium::flimflam::ServiceProxy>
98 connecting_service);
Vitaly Buka4f771532015-08-14 14:58:39 -070099
Vitaly Buka58a288b2015-07-31 00:33:31 -0700100 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 Vakulenko0022b752015-10-02 11:09:59 -0700105 bool disable_xmpp_{false};
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700106 std::vector<ConnectionChangedCallback> connectivity_listeners_;
Vitaly Buka58a288b2015-07-31 00:33:31 -0700107
108 // State for tracking where we are in our attempts to connect to a service.
Vitaly Buka58a288b2015-07-31 00:33:31 -0700109 bool have_called_connect_{false};
110 std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700111 std::string connecting_service_error_;
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700112 weave::DoneCallback connect_done_callback_;
Vitaly Buka58a288b2015-07-31 00:33:31 -0700113
114 // State for tracking our online connectivity.
115 std::map<dbus::ObjectPath, DeviceState> devices_;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700116 State connectivity_state_{State::kOffline};
Vitaly Buka58a288b2015-07-31 00:33:31 -0700117
Vitaly Buka3ef4fff2015-07-31 01:12:07 -0700118 std::unique_ptr<ApManagerClient> ap_manager_client_;
119
Vitaly Buka58a288b2015-07-31 00:33:31 -0700120 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_