blob: 80c91acfe4e3f81a4a968815991e5ea614a30fc2 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartb50f0b92011-05-16 16:31:42 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_WIFI_
6#define SHILL_WIFI_
7
mukesh agrawalab87ea42011-05-18 11:44:49 -07008#include <map>
Chris Masone46eaaf52011-05-24 13:08:30 -07009#include <string>
mukesh agrawalab87ea42011-05-18 11:44:49 -070010#include <vector>
Chris Masone46eaaf52011-05-24 13:08:30 -070011
mukesh agrawalaf571952011-07-14 14:31:12 -070012#include <dbus-c++/dbus.h>
Paul Stewart6ab23a92011-11-09 17:17:47 -080013#include <gtest/gtest_prod.h> // for FRIEND_TEST
mukesh agrawalaf571952011-07-14 14:31:12 -070014
Paul Stewartb50f0b92011-05-16 16:31:42 -070015#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Chris Masone2b105542011-06-22 10:58:09 -070017#include "shill/refptr_types.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070018
19namespace shill {
20
mukesh agrawal7a4e4002011-09-06 11:26:05 -070021class Error;
22class KeyValueStore;
Darin Petkovab565bb2011-10-06 02:55:51 -070023class ProxyFactory;
mukesh agrawalaf571952011-07-14 14:31:12 -070024class SupplicantInterfaceProxyInterface;
25class SupplicantProcessProxyInterface;
mukesh agrawal445e72c2011-06-22 11:13:50 -070026class WiFiService;
mukesh agrawalb54601c2011-06-07 17:39:22 -070027
mukesh agrawalab87ea42011-05-18 11:44:49 -070028// WiFi class. Specialization of Device for WiFi.
Paul Stewartb50f0b92011-05-16 16:31:42 -070029class WiFi : public Device {
30 public:
Paul Stewartf1ce5d22011-05-19 13:10:20 -070031 WiFi(ControlInterface *control_interface,
32 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080033 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070034 Manager *manager,
Chris Masone626719f2011-08-18 16:58:48 -070035 const std::string &link,
36 const std::string &address,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070037 int interface_index);
mukesh agrawalab87ea42011-05-18 11:44:49 -070038 virtual ~WiFi();
Darin Petkovc0865312011-09-16 15:31:20 -070039
mukesh agrawalab87ea42011-05-18 11:44:49 -070040 virtual void Start();
41 virtual void Stop();
Paul Stewarta41e38d2011-11-11 07:47:29 -080042 virtual bool Load(StoreInterface *storage);
Darin Petkovc0865312011-09-16 15:31:20 -070043 virtual void Scan(Error *error);
Paul Stewartfdd16072011-09-16 12:41:35 -070044 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +000045 virtual bool IsConnectingTo(const WiFiService &service) const;
mukesh agrawalab87ea42011-05-18 11:44:49 -070046
mukesh agrawal15908392011-11-16 18:29:25 +000047 // Called by SupplicantInterfaceProxy, in response to events from
mukesh agrawalab87ea42011-05-18 11:44:49 -070048 // wpa_supplicant.
49 void BSSAdded(const ::DBus::Path &BSS,
Chris Masonea82b7112011-05-25 15:16:29 -070050 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawal261daca2011-12-02 18:56:56 +000051 void BSSRemoved(const ::DBus::Path &BSS);
mukesh agrawal7ec71312011-11-10 02:08:26 +000052 void PropertiesChanged(
53 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawalab87ea42011-05-18 11:44:49 -070054 void ScanDone();
55
mukesh agrawal15908392011-11-16 18:29:25 +000056 // Called by WiFiService.
mukesh agrawal6e277772011-09-29 15:04:23 -070057 virtual void ConnectTo(
58 WiFiService *service,
mukesh agrawal64896322011-12-01 01:13:10 +000059 std::map<std::string, ::DBus::Variant> service_params);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000060 virtual void DisconnectFrom(WiFiService *service);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000061 virtual bool IsIdle() const;
Paul Stewart66c86002012-01-30 18:00:52 -080062 virtual void ClearCachedCredentials();
mukesh agrawalb54601c2011-06-07 17:39:22 -070063
mukesh agrawalb20776f2012-02-10 16:00:36 -080064 // Called by WiFiEndpoint.
65 virtual void NotifyEndpointChanged(const WiFiEndpoint &endpoint);
66
mukesh agrawal15908392011-11-16 18:29:25 +000067 // Called by Manager.
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068 virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error);
69
mukesh agrawal16bc1b82012-02-09 18:38:26 -080070 // Utility, used by WiFiService and WiFiEndpoint.
71 // Replace non-ASCII characters with '?'. Return true if one or more
72 // characters were changed.
73 static bool SanitizeSSID(std::string *ssid);
74
Paul Stewartb50f0b92011-05-16 16:31:42 -070075 private:
mukesh agrawal7ec71312011-11-10 02:08:26 +000076 friend class WiFiMainTest; // access to supplicant_*_proxy_, link_up_
mukesh agrawal7ec71312011-11-10 02:08:26 +000077 FRIEND_TEST(WiFiMainTest, InitialSupplicantState); // kInterfaceStateUnknown
Thieu Lee41a72d2012-02-06 20:46:51 +000078 FRIEND_TEST(WiFiMainTest, ScanResults); // EndpointMap
mukesh agrawal165e6142011-11-22 02:22:56 +000079 FRIEND_TEST(WiFiMainTest, ScanResultsWithUpdates); // EndpointMap
mukesh agrawal8abd2f62012-01-30 14:56:14 -080080 FRIEND_TEST(WiFiPropertyTest, ClearDerivedProperty); // bgscan_method_
Paul Stewart6ab23a92011-11-09 17:17:47 -080081
mukesh agrawalb54601c2011-06-07 17:39:22 -070082 typedef std::map<const std::string, WiFiEndpointRefPtr> EndpointMap;
mukesh agrawal15908392011-11-16 18:29:25 +000083 typedef std::map<WiFiService *, std::string> ReverseServiceMap;
mukesh agrawalb54601c2011-06-07 17:39:22 -070084
mukesh agrawal4d0401c2012-01-06 16:05:31 -080085 static const char *kDefaultBgscanMethod;
86 static const uint16 kDefaultBgscanShortIntervalSeconds;
87 static const int32 kDefaultBgscanSignalThresholdDbm;
88 static const uint16 kDefaultScanIntervalSeconds;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070089 static const char kManagerErrorPassphraseRequired[];
90 static const char kManagerErrorSSIDTooLong[];
91 static const char kManagerErrorSSIDTooShort[];
92 static const char kManagerErrorSSIDRequired[];
93 static const char kManagerErrorTypeRequired[];
94 static const char kManagerErrorUnsupportedSecurityMode[];
95 static const char kManagerErrorUnsupportedServiceType[];
96 static const char kManagerErrorUnsupportedServiceMode[];
mukesh agrawal7ec71312011-11-10 02:08:26 +000097 static const char kInterfaceStateUnknown[];
mukesh agrawalab87ea42011-05-18 11:44:49 -070098
mukesh agrawal4d0401c2012-01-06 16:05:31 -080099 std::string CreateBgscanConfigString();
100 std::string GetBgscanMethod(Error */* error */) { return bgscan_method_; }
101 uint16 GetBgscanShortInterval(Error */* error */) {
102 return bgscan_short_interval_seconds_;
103 }
104 int32 GetBgscanSignalThreshold(Error */* error */) {
105 return bgscan_signal_threshold_dbm_;
106 }
107 uint16 GetScanInterval(Error */* error */) { return scan_interval_seconds_; }
108 void SetBgscanMethod(const std::string &method, Error *error);
109 void SetBgscanShortInterval(const uint16 &seconds, Error *error);
110 void SetBgscanSignalThreshold(const int32 &dbm, Error *error);
111 void SetScanInterval(const uint16 &seconds, Error *error);
112
mukesh agrawal15908392011-11-16 18:29:25 +0000113 WiFiServiceRefPtr CreateServiceForEndpoint(
114 const WiFiEndpoint &endpoint, bool hidden_ssid);
115 void CurrentBSSChanged(const ::DBus::Path &new_bss);
Paul Stewart6ab23a92011-11-09 17:17:47 -0800116 WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid,
117 const std::string &mode,
118 const std::string &security) const;
mukesh agrawal165e6142011-11-22 02:22:56 +0000119 WiFiServiceRefPtr FindServiceForEndpoint(const WiFiEndpoint &endpoint);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800120 ByteArrays GetHiddenSSIDList();
mukesh agrawal15908392011-11-16 18:29:25 +0000121 void HandleDisconnect();
122 void HandleRoam(const ::DBus::Path &new_bssid);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800123 // Create services for hidden networks stored in |storage|. Returns true
124 // if any were found, otherwise returns false.
125 bool LoadHiddenServices(StoreInterface *storage);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000126 void BSSAddedTask(const ::DBus::Path &BSS,
127 const std::map<std::string, ::DBus::Variant> &properties);
128 void BSSRemovedTask(const ::DBus::Path &BSS);
Paul Stewart66c86002012-01-30 18:00:52 -0800129 void ClearCachedCredentialsTask();
mukesh agrawal15908392011-11-16 18:29:25 +0000130 void PropertiesChangedTask(
131 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700132 void ScanDoneTask();
mukesh agrawal32399322011-09-01 10:53:43 -0700133 void ScanTask();
mukesh agrawal7ec71312011-11-10 02:08:26 +0000134 void StateChanged(const std::string &new_state);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700135
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800136 void HelpRegisterDerivedInt32(
137 PropertyStore *store,
138 const std::string &name,
139 int32(WiFi::*get)(Error *error),
140 void(WiFi::*set)(const int32 &value, Error *error));
141 void HelpRegisterDerivedString(
142 PropertyStore *store,
143 const std::string &name,
144 std::string(WiFi::*get)(Error *error),
145 void(WiFi::*set)(const std::string &value, Error *error));
146 void HelpRegisterDerivedUint16(
147 PropertyStore *store,
148 const std::string &name,
149 uint16(WiFi::*get)(Error *error),
150 void(WiFi::*set)(const uint16 &value, Error *error));
151
Darin Petkovab565bb2011-10-06 02:55:51 -0700152 // Store cached copies of singletons for speed/ease of testing.
153 ProxyFactory *proxy_factory_;
154
mukesh agrawalb54601c2011-06-07 17:39:22 -0700155 ScopedRunnableMethodFactory<WiFi> task_factory_;
mukesh agrawalaf571952011-07-14 14:31:12 -0700156 scoped_ptr<SupplicantProcessProxyInterface> supplicant_process_proxy_;
157 scoped_ptr<SupplicantInterfaceProxyInterface> supplicant_interface_proxy_;
mukesh agrawal15908392011-11-16 18:29:25 +0000158 // The rpcid used as the key is wpa_supplicant's D-Bus path for the
159 // Endpoint (BSS, in supplicant parlance).
160 EndpointMap endpoint_by_rpcid_;
mukesh agrawal15908392011-11-16 18:29:25 +0000161 // Map from Services to the D-Bus path for the corresponding wpa_supplicant
162 // Network.
163 ReverseServiceMap rpcid_by_service_;
mukesh agrawal15908392011-11-16 18:29:25 +0000164 std::vector<WiFiServiceRefPtr> services_;
165 // The Service we are presently connected to. May be NULL is we're not
166 // not connected to any Service.
167 WiFiServiceRefPtr current_service_;
168 // The Service we're attempting to connect to. May be NULL if we're
169 // not attempting to connect to a new Service. If non-NULL, should
170 // be distinct from |current_service_|. (A service should not
171 // simultaneously be both pending, and current.)
172 WiFiServiceRefPtr pending_service_;
173 std::string supplicant_state_;
174 std::string supplicant_bss_;
Paul Stewart66c86002012-01-30 18:00:52 -0800175 // Signifies that ClearCachedCredentialsTask() is pending.
176 bool clear_cached_credentials_pending_;
mukesh agrawalab87ea42011-05-18 11:44:49 -0700177
Chris Masone853b81b2011-06-24 14:11:41 -0700178 // Properties
179 std::string bgscan_method_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800180 uint16 bgscan_short_interval_seconds_;
181 int32 bgscan_signal_threshold_dbm_;
Chris Masone853b81b2011-06-24 14:11:41 -0700182 bool scan_pending_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800183 uint16 scan_interval_seconds_;
Chris Masone853b81b2011-06-24 14:11:41 -0700184
Paul Stewartb50f0b92011-05-16 16:31:42 -0700185 DISALLOW_COPY_AND_ASSIGN(WiFi);
186};
187
188} // namespace shill
189
190#endif // SHILL_WIFI_