blob: 0499707e86d04a1b47054faba24c11a05d791bd8 [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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 Buka58a288b2015-07-31 00:33:31 -070014
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 Vakulenkoe32375b2015-09-28 08:55:40 -070029#include <shill/dbus-proxies.h>
30#include <weave/provider/network.h>
31#include <weave/provider/wifi.h>
Vitaly Buka58a288b2015-07-31 00:33:31 -070032
33namespace buffet {
34
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070035class ApManagerClient;
36
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070037class ShillClient final : public weave::provider::Network,
38 public weave::provider::Wifi {
Vitaly Buka58a288b2015-07-31 00:33:31 -070039 public:
40 ShillClient(const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko0022b752015-10-02 11:09:59 -070041 const std::set<std::string>& device_whitelist,
42 bool disable_xmpp);
Vitaly Buka3ef4fff2015-07-31 01:12:07 -070043 ~ShillClient();
Vitaly Buka58a288b2015-07-31 00:33:31 -070044
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070045 // 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 Vakulenko94f8eba2015-10-14 08:52:45 -070051 const OpenSslSocketCallback& callback) override;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070052
53 // WifiProvider implementation.
54 void Connect(const std::string& ssid,
55 const std::string& passphrase,
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070056 const weave::DoneCallback& callback) override;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070057 void StartAccessPoint(const std::string& ssid) override;
58 void StopAccessPoint() override;
Alex Vakulenkod8f36992016-02-02 12:28:47 -080059 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 Buka58a288b2015-07-31 00:33:31 -070063
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 Vakulenkoe32375b2015-09-28 08:55:40 -070072 State service_state{State::kOffline};
Vitaly Buka58a288b2015-07-31 00:33:31 -070073 };
74
Vitaly Buka5d14dca2015-11-05 22:26:19 -080075 void Init();
76
Vitaly Buka58a288b2015-07-31 00:33:31 -070077 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 Vakulenko41705852015-10-13 10:12:06 -070084 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070085 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 Vakulenko41705852015-10-13 10:12:06 -070091 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070092 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 Vakulenko41705852015-10-13 10:12:06 -070098 const brillo::Any& property_value);
Vitaly Buka58a288b2015-07-31 00:33:31 -070099
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700100 void OnStateChangeForConnectingService(const std::string& state);
101 void OnErrorChangeForConnectingService(const std::string& error);
102 void OnStrengthChangeForConnectingService(uint8_t signal_strength);
Vitaly Buka58a288b2015-07-31 00:33:31 -0700103 void OnStateChangeForSelectedService(const dbus::ObjectPath& service_path,
104 const std::string& state);
105 void UpdateConnectivityState();
106 void NotifyConnectivityListeners(bool am_online);
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700107 // Clean up state related to a connecting service.
108 void CleanupConnectingService();
Vitaly Buka58a288b2015-07-31 00:33:31 -0700109
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700110 void ConnectToServiceError(
111 std::shared_ptr<org::chromium::flimflam::ServiceProxy>
112 connecting_service);
Vitaly Buka4f771532015-08-14 14:58:39 -0700113
Vitaly Buka58a288b2015-07-31 00:33:31 -0700114 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 Vakulenko0022b752015-10-02 11:09:59 -0700119 bool disable_xmpp_{false};
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700120 std::vector<ConnectionChangedCallback> connectivity_listeners_;
Vitaly Buka58a288b2015-07-31 00:33:31 -0700121
122 // State for tracking where we are in our attempts to connect to a service.
Vitaly Buka58a288b2015-07-31 00:33:31 -0700123 bool have_called_connect_{false};
124 std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service_;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700125 std::string connecting_service_error_;
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700126 weave::DoneCallback connect_done_callback_;
Vitaly Buka58a288b2015-07-31 00:33:31 -0700127
128 // State for tracking our online connectivity.
129 std::map<dbus::ObjectPath, DeviceState> devices_;
Alex Vakulenkoe32375b2015-09-28 08:55:40 -0700130 State connectivity_state_{State::kOffline};
Vitaly Buka58a288b2015-07-31 00:33:31 -0700131
Vitaly Buka3ef4fff2015-07-31 01:12:07 -0700132 std::unique_ptr<ApManagerClient> ap_manager_client_;
133
Vitaly Buka58a288b2015-07-31 00:33:31 -0700134 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_