blob: 90b61a2c7bae99c823b68c7ed20fca9531018338 [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 agrawal5c05b292012-03-07 10:12:52 -08008#include <time.h>
9
mukesh agrawalab87ea42011-05-18 11:44:49 -070010#include <map>
Chris Masone46eaaf52011-05-24 13:08:30 -070011#include <string>
mukesh agrawalab87ea42011-05-18 11:44:49 -070012#include <vector>
Chris Masone46eaaf52011-05-24 13:08:30 -070013
mukesh agrawalaf571952011-07-14 14:31:12 -070014#include <dbus-c++/dbus.h>
Paul Stewart6ab23a92011-11-09 17:17:47 -080015#include <gtest/gtest_prod.h> // for FRIEND_TEST
mukesh agrawalaf571952011-07-14 14:31:12 -070016
Paul Stewartb50f0b92011-05-16 16:31:42 -070017#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070018#include "shill/event_dispatcher.h"
Gary Morainac1bdb42012-02-16 17:42:29 -080019#include "shill/power_manager.h"
Chris Masone2b105542011-06-22 10:58:09 -070020#include "shill/refptr_types.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070021
22namespace shill {
23
mukesh agrawal7a4e4002011-09-06 11:26:05 -070024class Error;
25class KeyValueStore;
Darin Petkovab565bb2011-10-06 02:55:51 -070026class ProxyFactory;
mukesh agrawalaf571952011-07-14 14:31:12 -070027class SupplicantInterfaceProxyInterface;
28class SupplicantProcessProxyInterface;
mukesh agrawal445e72c2011-06-22 11:13:50 -070029class WiFiService;
mukesh agrawalb54601c2011-06-07 17:39:22 -070030
mukesh agrawalab87ea42011-05-18 11:44:49 -070031// WiFi class. Specialization of Device for WiFi.
Paul Stewartb50f0b92011-05-16 16:31:42 -070032class WiFi : public Device {
33 public:
Paul Stewartf1ce5d22011-05-19 13:10:20 -070034 WiFi(ControlInterface *control_interface,
35 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080036 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070037 Manager *manager,
Chris Masone626719f2011-08-18 16:58:48 -070038 const std::string &link,
39 const std::string &address,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070040 int interface_index);
mukesh agrawalab87ea42011-05-18 11:44:49 -070041 virtual ~WiFi();
Darin Petkovc0865312011-09-16 15:31:20 -070042
mukesh agrawalab87ea42011-05-18 11:44:49 -070043 virtual void Start();
44 virtual void Stop();
Paul Stewarta41e38d2011-11-11 07:47:29 -080045 virtual bool Load(StoreInterface *storage);
Darin Petkovc0865312011-09-16 15:31:20 -070046 virtual void Scan(Error *error);
Paul Stewartfdd16072011-09-16 12:41:35 -070047 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +000048 virtual bool IsConnectingTo(const WiFiService &service) const;
mukesh agrawalab87ea42011-05-18 11:44:49 -070049
mukesh agrawal15908392011-11-16 18:29:25 +000050 // Called by SupplicantInterfaceProxy, in response to events from
mukesh agrawalab87ea42011-05-18 11:44:49 -070051 // wpa_supplicant.
52 void BSSAdded(const ::DBus::Path &BSS,
Chris Masonea82b7112011-05-25 15:16:29 -070053 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawal261daca2011-12-02 18:56:56 +000054 void BSSRemoved(const ::DBus::Path &BSS);
mukesh agrawal7ec71312011-11-10 02:08:26 +000055 void PropertiesChanged(
56 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawalab87ea42011-05-18 11:44:49 -070057 void ScanDone();
58
mukesh agrawal15908392011-11-16 18:29:25 +000059 // Called by WiFiService.
mukesh agrawal6e277772011-09-29 15:04:23 -070060 virtual void ConnectTo(
61 WiFiService *service,
mukesh agrawal64896322011-12-01 01:13:10 +000062 std::map<std::string, ::DBus::Variant> service_params);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000063 virtual void DisconnectFrom(WiFiService *service);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000064 virtual bool IsIdle() const;
Paul Stewart66c86002012-01-30 18:00:52 -080065 virtual void ClearCachedCredentials();
mukesh agrawalb54601c2011-06-07 17:39:22 -070066
mukesh agrawalb20776f2012-02-10 16:00:36 -080067 // Called by WiFiEndpoint.
68 virtual void NotifyEndpointChanged(const WiFiEndpoint &endpoint);
69
mukesh agrawal15908392011-11-16 18:29:25 +000070 // Called by Manager.
mukesh agrawal7a4e4002011-09-06 11:26:05 -070071 virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error);
72
mukesh agrawal16bc1b82012-02-09 18:38:26 -080073 // Utility, used by WiFiService and WiFiEndpoint.
74 // Replace non-ASCII characters with '?'. Return true if one or more
75 // characters were changed.
76 static bool SanitizeSSID(std::string *ssid);
77
Paul Stewartb50f0b92011-05-16 16:31:42 -070078 private:
mukesh agrawal7ec71312011-11-10 02:08:26 +000079 friend class WiFiMainTest; // access to supplicant_*_proxy_, link_up_
mukesh agrawal7ec71312011-11-10 02:08:26 +000080 FRIEND_TEST(WiFiMainTest, InitialSupplicantState); // kInterfaceStateUnknown
Thieu Lee41a72d2012-02-06 20:46:51 +000081 FRIEND_TEST(WiFiMainTest, ScanResults); // EndpointMap
mukesh agrawal165e6142011-11-22 02:22:56 +000082 FRIEND_TEST(WiFiMainTest, ScanResultsWithUpdates); // EndpointMap
mukesh agrawal5c05b292012-03-07 10:12:52 -080083 FRIEND_TEST(WiFiMainTest, FlushBSSOnResume); // kMaxBSSResumeAgeSeconds
mukesh agrawal8abd2f62012-01-30 14:56:14 -080084 FRIEND_TEST(WiFiPropertyTest, ClearDerivedProperty); // bgscan_method_
Paul Stewart6ab23a92011-11-09 17:17:47 -080085
mukesh agrawalb54601c2011-06-07 17:39:22 -070086 typedef std::map<const std::string, WiFiEndpointRefPtr> EndpointMap;
mukesh agrawal15908392011-11-16 18:29:25 +000087 typedef std::map<WiFiService *, std::string> ReverseServiceMap;
mukesh agrawalb54601c2011-06-07 17:39:22 -070088
mukesh agrawal4d0401c2012-01-06 16:05:31 -080089 static const char *kDefaultBgscanMethod;
90 static const uint16 kDefaultBgscanShortIntervalSeconds;
91 static const int32 kDefaultBgscanSignalThresholdDbm;
92 static const uint16 kDefaultScanIntervalSeconds;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070093 static const char kManagerErrorPassphraseRequired[];
94 static const char kManagerErrorSSIDTooLong[];
95 static const char kManagerErrorSSIDTooShort[];
96 static const char kManagerErrorSSIDRequired[];
mukesh agrawal7a4e4002011-09-06 11:26:05 -070097 static const char kManagerErrorUnsupportedSecurityMode[];
mukesh agrawal7a4e4002011-09-06 11:26:05 -070098 static const char kManagerErrorUnsupportedServiceMode[];
mukesh agrawal5c05b292012-03-07 10:12:52 -080099 static const time_t kMaxBSSResumeAgeSeconds;
mukesh agrawal7ec71312011-11-10 02:08:26 +0000100 static const char kInterfaceStateUnknown[];
mukesh agrawalab87ea42011-05-18 11:44:49 -0700101
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800102 std::string CreateBgscanConfigString();
103 std::string GetBgscanMethod(Error */* error */) { return bgscan_method_; }
104 uint16 GetBgscanShortInterval(Error */* error */) {
105 return bgscan_short_interval_seconds_;
106 }
107 int32 GetBgscanSignalThreshold(Error */* error */) {
108 return bgscan_signal_threshold_dbm_;
109 }
110 uint16 GetScanInterval(Error */* error */) { return scan_interval_seconds_; }
111 void SetBgscanMethod(const std::string &method, Error *error);
112 void SetBgscanShortInterval(const uint16 &seconds, Error *error);
113 void SetBgscanSignalThreshold(const int32 &dbm, Error *error);
114 void SetScanInterval(const uint16 &seconds, Error *error);
115
mukesh agrawal15908392011-11-16 18:29:25 +0000116 WiFiServiceRefPtr CreateServiceForEndpoint(
117 const WiFiEndpoint &endpoint, bool hidden_ssid);
118 void CurrentBSSChanged(const ::DBus::Path &new_bss);
Paul Stewart6ab23a92011-11-09 17:17:47 -0800119 WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid,
120 const std::string &mode,
121 const std::string &security) const;
mukesh agrawal165e6142011-11-22 02:22:56 +0000122 WiFiServiceRefPtr FindServiceForEndpoint(const WiFiEndpoint &endpoint);
Paul Stewartced6a0b2011-11-08 15:32:04 -0800123 ByteArrays GetHiddenSSIDList();
mukesh agrawal15908392011-11-16 18:29:25 +0000124 void HandleDisconnect();
125 void HandleRoam(const ::DBus::Path &new_bssid);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800126 // Create services for hidden networks stored in |storage|. Returns true
127 // if any were found, otherwise returns false.
128 bool LoadHiddenServices(StoreInterface *storage);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000129 void BSSAddedTask(const ::DBus::Path &BSS,
130 const std::map<std::string, ::DBus::Variant> &properties);
131 void BSSRemovedTask(const ::DBus::Path &BSS);
Paul Stewart66c86002012-01-30 18:00:52 -0800132 void ClearCachedCredentialsTask();
mukesh agrawal15908392011-11-16 18:29:25 +0000133 void PropertiesChangedTask(
134 const std::map<std::string, ::DBus::Variant> &properties);
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700135 void ScanDoneTask();
mukesh agrawal32399322011-09-01 10:53:43 -0700136 void ScanTask();
mukesh agrawal7ec71312011-11-10 02:08:26 +0000137 void StateChanged(const std::string &new_state);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700138
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800139 void HelpRegisterDerivedInt32(
140 PropertyStore *store,
141 const std::string &name,
142 int32(WiFi::*get)(Error *error),
143 void(WiFi::*set)(const int32 &value, Error *error));
144 void HelpRegisterDerivedString(
145 PropertyStore *store,
146 const std::string &name,
147 std::string(WiFi::*get)(Error *error),
148 void(WiFi::*set)(const std::string &value, Error *error));
149 void HelpRegisterDerivedUint16(
150 PropertyStore *store,
151 const std::string &name,
152 uint16(WiFi::*get)(Error *error),
153 void(WiFi::*set)(const uint16 &value, Error *error));
154
Gary Morainac1bdb42012-02-16 17:42:29 -0800155 // If this WiFi device is idle and |new_state| indicates a resume event, a
156 // scan is initiated.
157 void HandlePowerStateChange(PowerManager::SuspendState new_state);
158
Darin Petkovab565bb2011-10-06 02:55:51 -0700159 // Store cached copies of singletons for speed/ease of testing.
160 ProxyFactory *proxy_factory_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800161 Time *time_;
Darin Petkovab565bb2011-10-06 02:55:51 -0700162
mukesh agrawalb54601c2011-06-07 17:39:22 -0700163 ScopedRunnableMethodFactory<WiFi> task_factory_;
mukesh agrawalaf571952011-07-14 14:31:12 -0700164 scoped_ptr<SupplicantProcessProxyInterface> supplicant_process_proxy_;
165 scoped_ptr<SupplicantInterfaceProxyInterface> supplicant_interface_proxy_;
mukesh agrawal15908392011-11-16 18:29:25 +0000166 // The rpcid used as the key is wpa_supplicant's D-Bus path for the
167 // Endpoint (BSS, in supplicant parlance).
168 EndpointMap endpoint_by_rpcid_;
mukesh agrawal15908392011-11-16 18:29:25 +0000169 // Map from Services to the D-Bus path for the corresponding wpa_supplicant
170 // Network.
171 ReverseServiceMap rpcid_by_service_;
mukesh agrawal15908392011-11-16 18:29:25 +0000172 std::vector<WiFiServiceRefPtr> services_;
173 // The Service we are presently connected to. May be NULL is we're not
174 // not connected to any Service.
175 WiFiServiceRefPtr current_service_;
176 // The Service we're attempting to connect to. May be NULL if we're
177 // not attempting to connect to a new Service. If non-NULL, should
178 // be distinct from |current_service_|. (A service should not
179 // simultaneously be both pending, and current.)
180 WiFiServiceRefPtr pending_service_;
181 std::string supplicant_state_;
182 std::string supplicant_bss_;
Paul Stewart66c86002012-01-30 18:00:52 -0800183 // Signifies that ClearCachedCredentialsTask() is pending.
184 bool clear_cached_credentials_pending_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800185 // Indicates that we should flush supplicant's BSS cache after the
186 // next scan completes.
187 bool need_bss_flush_;
188 struct timeval resumed_at_;
mukesh agrawalab87ea42011-05-18 11:44:49 -0700189
Chris Masone853b81b2011-06-24 14:11:41 -0700190 // Properties
191 std::string bgscan_method_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800192 uint16 bgscan_short_interval_seconds_;
193 int32 bgscan_signal_threshold_dbm_;
Chris Masone853b81b2011-06-24 14:11:41 -0700194 bool scan_pending_;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800195 uint16 scan_interval_seconds_;
Chris Masone853b81b2011-06-24 14:11:41 -0700196
Paul Stewartb50f0b92011-05-16 16:31:42 -0700197 DISALLOW_COPY_AND_ASSIGN(WiFi);
198};
199
200} // namespace shill
201
202#endif // SHILL_WIFI_