blob: 25386740649a150e21981bb96428ba33f9ec0bfd [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>
19
20#include "shill/dbus-proxies.h"
21#include "weave/network.h"
22
23namespace buffet {
24
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070025class ApManagerClient;
26
Vitaly Buka58a288b2015-07-31 00:33:31 -070027class ShillClient final : public weave::Network {
28 public:
29 ShillClient(const scoped_refptr<dbus::Bus>& bus,
30 const std::set<std::string>& device_whitelist);
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070031 ~ShillClient();
Vitaly Buka58a288b2015-07-31 00:33:31 -070032
33 void Init();
34
35 // Network implementation.
36 void AddOnConnectionChangedCallback(
37 const OnConnectionChangedCallback& listener) override;
38 bool ConnectToService(const std::string& ssid,
39 const std::string& passphrase,
40 const base::Closure& on_success,
41 chromeos::ErrorPtr* error) override;
42 weave::NetworkState GetConnectionState() const override;
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070043 void EnableAccessPoint(const std::string& ssid) override;
44 void DisableAccessPoint() override;
Vitaly Buka58a288b2015-07-31 00:33:31 -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;
54 weave::NetworkState service_state{weave::NetworkState::kOffline};
55 };
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_;
100 std::vector<OnConnectionChangedCallback> connectivity_listeners_;
101
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_;
110 weave::NetworkState connectivity_state_{weave::NetworkState::kOffline};
111
Vitaly Buka3ef4fff2015-07-31 01:12:07 -0700112 std::unique_ptr<ApManagerClient> ap_manager_client_;
113
Vitaly Buka58a288b2015-07-31 00:33:31 -0700114 base::WeakPtrFactory<ShillClient> weak_factory_{this};
115
116 DISALLOW_COPY_AND_ASSIGN(ShillClient);
117};
118
119} // namespace buffet
120
121#endif // BUFFET_SHILL_CLIENT_H_