blob: 25386740649a150e21981bb96428ba33f9ec0bfd [file] [log] [blame]
Vitaly Buka7ce499f2015-06-09 08:04:11 -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
Vitaly Bukaddb2e382015-07-31 00:33:31 -07005#ifndef BUFFET_SHILL_CLIENT_H_
6#define BUFFET_SHILL_CLIENT_H_
Vitaly Buka7ce499f2015-06-09 08:04:11 -07007
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>
19
20#include "shill/dbus-proxies.h"
Vitaly Buka387b4f42015-07-30 23:55:13 -070021#include "weave/network.h"
Vitaly Buka7ce499f2015-06-09 08:04:11 -070022
Vitaly Bukaddb2e382015-07-31 00:33:31 -070023namespace buffet {
Vitaly Buka7ce499f2015-06-09 08:04:11 -070024
Vitaly Bukab6c8a3e2015-07-31 01:12:07 -070025class ApManagerClient;
26
Vitaly Bukaddb2e382015-07-31 00:33:31 -070027class ShillClient final : public weave::Network {
Vitaly Buka7ce499f2015-06-09 08:04:11 -070028 public:
Vitaly Buka7ce499f2015-06-09 08:04:11 -070029 ShillClient(const scoped_refptr<dbus::Bus>& bus,
30 const std::set<std::string>& device_whitelist);
Vitaly Bukab6c8a3e2015-07-31 01:12:07 -070031 ~ShillClient();
Vitaly Buka7ce499f2015-06-09 08:04:11 -070032
Vitaly Buka7ce499f2015-06-09 08:04:11 -070033 void Init();
Vitaly Buka387b4f42015-07-30 23:55:13 -070034
35 // Network implementation.
36 void AddOnConnectionChangedCallback(
37 const OnConnectionChangedCallback& listener) override;
Vitaly Buka7ce499f2015-06-09 08:04:11 -070038 bool ConnectToService(const std::string& ssid,
39 const std::string& passphrase,
40 const base::Closure& on_success,
Vitaly Buka387b4f42015-07-30 23:55:13 -070041 chromeos::ErrorPtr* error) override;
Vitaly Bukaddb2e382015-07-31 00:33:31 -070042 weave::NetworkState GetConnectionState() const override;
Vitaly Bukab6c8a3e2015-07-31 01:12:07 -070043 void EnableAccessPoint(const std::string& ssid) override;
44 void DisableAccessPoint() override;
Vitaly Buka7ce499f2015-06-09 08:04:11 -070045
46 private:
47 struct DeviceState {
48 std::unique_ptr<org::chromium::flimflam::DeviceProxy> device;
49 // ServiceProxy objects are shared because the connecting service will
50 // also be the selected service for a device, but is not always the selected
51 // service (for instance, in the period between configuring a WiFi service
52 // with credentials, and when Connect() is called.)
53 std::shared_ptr<org::chromium::flimflam::ServiceProxy> selected_service;
Vitaly Bukaddb2e382015-07-31 00:33:31 -070054 weave::NetworkState service_state{weave::NetworkState::kOffline};
Vitaly Buka7ce499f2015-06-09 08:04:11 -070055 };
56
57 bool IsMonitoredDevice(org::chromium::flimflam::DeviceProxy* device);
58 void OnShillServiceOwnerChange(const std::string& old_owner,
59 const std::string& new_owner);
60 void OnManagerPropertyChangeRegistration(const std::string& interface,
61 const std::string& signal_name,
62 bool success);
63 void OnManagerPropertyChange(const std::string& property_name,
64 const chromeos::Any& property_value);
65 void OnDevicePropertyChangeRegistration(const dbus::ObjectPath& device_path,
66 const std::string& interface,
67 const std::string& signal_name,
68 bool success);
69 void OnDevicePropertyChange(const dbus::ObjectPath& device_path,
70 const std::string& property_name,
71 const chromeos::Any& property_value);
72 void OnServicePropertyChangeRegistration(const dbus::ObjectPath& path,
73 const std::string& interface,
74 const std::string& signal_name,
75 bool success);
76 void OnServicePropertyChange(const dbus::ObjectPath& service_path,
77 const std::string& property_name,
78 const chromeos::Any& property_value);
79
80 void OnStateChangeForConnectingService(const dbus::ObjectPath& service_path,
81 const std::string& state);
82 void OnStrengthChangeForConnectingService(
83 const dbus::ObjectPath& service_path,
84 uint8_t signal_strength);
85 void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path,
86 const std::string& state);
87 void UpdateConnectivityState();
88 void NotifyConnectivityListeners(bool am_online);
89 // Clean up state related to a connecting service. If
90 // |check_for_reset_pending| is set, then we'll check to see if we've called
91 // ConnectToService() in the time since a task to call this function was
92 // posted.
93 void CleanupConnectingService(bool check_for_reset_pending);
94
95 const scoped_refptr<dbus::Bus> bus_;
96 org::chromium::flimflam::ManagerProxy manager_proxy_;
97 // There is logic that assumes we will never change this device list
98 // in OnManagerPropertyChange. Do not be tempted to remove this const.
99 const std::set<std::string> device_whitelist_;
Vitaly Buka387b4f42015-07-30 23:55:13 -0700100 std::vector<OnConnectionChangedCallback> connectivity_listeners_;
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700101
102 // State for tracking where we are in our attempts to connect to a service.
103 bool connecting_service_reset_pending_{false};
104 bool have_called_connect_{false};
105 std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_;
106 base::CancelableClosure on_connect_success_;
107
108 // State for tracking our online connectivity.
109 std::map<dbus::ObjectPath, DeviceState> devices_;
Vitaly Bukaddb2e382015-07-31 00:33:31 -0700110 weave::NetworkState connectivity_state_{weave::NetworkState::kOffline};
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700111
Vitaly Bukab6c8a3e2015-07-31 01:12:07 -0700112 std::unique_ptr<ApManagerClient> ap_manager_client_;
113
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700114 base::WeakPtrFactory<ShillClient> weak_factory_{this};
115
116 DISALLOW_COPY_AND_ASSIGN(ShillClient);
117};
118
Vitaly Bukaddb2e382015-07-31 00:33:31 -0700119} // namespace buffet
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700120
Vitaly Bukaddb2e382015-07-31 00:33:31 -0700121#endif // BUFFET_SHILL_CLIENT_H_