blob: 0845d36d028cef3278f9ddd067fe066a6e158fef [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
Chris Masone2b105542011-06-22 10:58:09 -07005#include "shill/wifi.h"
6
Paul Stewartb50f0b92011-05-16 16:31:42 -07007#include <stdio.h>
mukesh agrawalc7426a42011-06-03 13:04:28 -07008#include <string.h>
mukesh agrawalf2f68a52011-09-01 12:15:48 -07009#include <netinet/ether.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070010#include <linux/if.h> // Needs definitions from netinet/ether.h
Paul Stewartb50f0b92011-05-16 16:31:42 -070011
mukesh agrawal8a3188d2011-12-01 20:56:44 +000012#include <algorithm>
mukesh agrawalab87ea42011-05-18 11:44:49 -070013#include <map>
Paul Stewartced6a0b2011-11-08 15:32:04 -080014#include <set>
Paul Stewartb50f0b92011-05-16 16:31:42 -070015#include <string>
mukesh agrawalab87ea42011-05-18 11:44:49 -070016#include <vector>
Paul Stewartb50f0b92011-05-16 16:31:42 -070017
Eric Shienbrood3e20a232012-02-16 11:35:56 -050018#include <base/bind.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070019#include <base/logging.h>
mukesh agrawal15908392011-11-16 18:29:25 +000020#include <base/stringprintf.h>
mukesh agrawal7a4e4002011-09-06 11:26:05 -070021#include <base/string_number_conversions.h>
22#include <base/string_util.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070023#include <chromeos/dbus/service_constants.h>
mukesh agrawal16bc1b82012-02-09 18:38:26 -080024#include <glib.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070025
26#include "shill/control_interface.h"
Paul Stewartced6a0b2011-11-08 15:32:04 -080027#include "shill/dbus_adaptor.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070028#include "shill/device.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070029#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070030#include "shill/event_dispatcher.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070031#include "shill/key_value_store.h"
32#include "shill/ieee80211.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070033#include "shill/manager.h"
Thieu Le67370f62012-02-14 23:01:42 +000034#include "shill/metrics.h"
Gary Morainac1bdb42012-02-16 17:42:29 -080035#include "shill/power_manager.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070036#include "shill/profile.h"
mukesh agrawal4d0401c2012-01-06 16:05:31 -080037#include "shill/property_accessor.h"
Darin Petkovd1967262011-07-18 14:55:18 -070038#include "shill/proxy_factory.h"
Eric Shienbrood9a245532012-03-07 14:20:39 -050039#include "shill/rtnl_handler.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070040#include "shill/scope_logger.h"
mukesh agrawal5c05b292012-03-07 10:12:52 -080041#include "shill/shill_time.h"
Paul Stewarta41e38d2011-11-11 07:47:29 -080042#include "shill/store_interface.h"
mukesh agrawalaf571952011-07-14 14:31:12 -070043#include "shill/supplicant_interface_proxy_interface.h"
44#include "shill/supplicant_process_proxy_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080045#include "shill/technology.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070046#include "shill/wifi_endpoint.h"
47#include "shill/wifi_service.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070048#include "shill/wpa_supplicant.h"
Paul Stewartb50f0b92011-05-16 16:31:42 -070049
Eric Shienbrood3e20a232012-02-16 11:35:56 -050050using base::Bind;
mukesh agrawal15908392011-11-16 18:29:25 +000051using base::StringPrintf;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070052using std::map;
Paul Stewartced6a0b2011-11-08 15:32:04 -080053using std::set;
mukesh agrawalab87ea42011-05-18 11:44:49 -070054using std::string;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070055using std::vector;
mukesh agrawalab87ea42011-05-18 11:44:49 -070056
Paul Stewartb50f0b92011-05-16 16:31:42 -070057namespace shill {
mukesh agrawal7a4e4002011-09-06 11:26:05 -070058
59// statics
mukesh agrawal4d0401c2012-01-06 16:05:31 -080060const char *WiFi::kDefaultBgscanMethod =
61 wpa_supplicant::kNetworkBgscanMethodSimple;
62const uint16 WiFi::kDefaultBgscanShortIntervalSeconds = 30;
63const int32 WiFi::kDefaultBgscanSignalThresholdDbm = -50;
64const uint16 WiFi::kDefaultScanIntervalSeconds = 180;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070065// Note that WiFi generates some manager-level errors, because it implements
66// the Manager.GetWiFiService flimflam API. The API is implemented here,
67// rather than in manager, to keep WiFi-specific logic in the right place.
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068const char WiFi::kManagerErrorSSIDRequired[] = "must specify SSID";
69const char WiFi::kManagerErrorSSIDTooLong[] = "SSID is too long";
70const char WiFi::kManagerErrorSSIDTooShort[] = "SSID is too short";
mukesh agrawal7a4e4002011-09-06 11:26:05 -070071const char WiFi::kManagerErrorUnsupportedSecurityMode[] =
72 "security mode is unsupported";
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073const char WiFi::kManagerErrorUnsupportedServiceMode[] =
74 "service mode is unsupported";
mukesh agrawal5c05b292012-03-07 10:12:52 -080075// Age (in seconds) beyond which a BSS cache entry will not be preserved,
76// across a suspend/resume.
77const time_t WiFi::kMaxBSSResumeAgeSeconds = 10;
mukesh agrawal7ec71312011-11-10 02:08:26 +000078const char WiFi::kInterfaceStateUnknown[] = "shill-unknown";
mukesh agrawalf2028172012-03-13 14:20:22 -070079const time_t WiFi::kRescanIntervalSeconds = 1;
mukesh agrawalb54601c2011-06-07 17:39:22 -070080
Paul Stewartb50f0b92011-05-16 16:31:42 -070081WiFi::WiFi(ControlInterface *control_interface,
82 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080083 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070084 Manager *manager,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070085 const string& link,
Paul Stewarta41e38d2011-11-11 07:47:29 -080086 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070087 int interface_index)
Chris Masonea82b7112011-05-25 15:16:29 -070088 : Device(control_interface,
89 dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080090 metrics,
Chris Masonea82b7112011-05-25 15:16:29 -070091 manager,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070092 link,
Chris Masone626719f2011-08-18 16:58:48 -070093 address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080094 interface_index,
95 Technology::kWifi),
Eric Shienbrood9a245532012-03-07 14:20:39 -050096 weak_ptr_factory_(this),
Darin Petkovab565bb2011-10-06 02:55:51 -070097 proxy_factory_(ProxyFactory::GetInstance()),
mukesh agrawal5c05b292012-03-07 10:12:52 -080098 time_(Time::GetInstance()),
mukesh agrawal15908392011-11-16 18:29:25 +000099 supplicant_state_(kInterfaceStateUnknown),
100 supplicant_bss_("(unknown)"),
Paul Stewart66c86002012-01-30 18:00:52 -0800101 clear_cached_credentials_pending_(false),
mukesh agrawal5c05b292012-03-07 10:12:52 -0800102 need_bss_flush_(false),
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800103 bgscan_method_(kDefaultBgscanMethod),
104 bgscan_short_interval_seconds_(kDefaultBgscanShortIntervalSeconds),
105 bgscan_signal_threshold_dbm_(kDefaultBgscanSignalThresholdDbm),
Chris Masone853b81b2011-06-24 14:11:41 -0700106 scan_pending_(false),
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800107 scan_interval_seconds_(kDefaultScanIntervalSeconds) {
mukesh agrawalde29fa82011-09-16 16:16:36 -0700108 PropertyStore *store = this->mutable_store();
mukesh agrawal5c05b292012-03-07 10:12:52 -0800109 resumed_at_.tv_sec = 0;
110 resumed_at_.tv_usec = 0;
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800111 HelpRegisterDerivedString(store,
112 flimflam::kBgscanMethodProperty,
113 &WiFi::GetBgscanMethod,
114 &WiFi::SetBgscanMethod);
115 HelpRegisterDerivedUint16(store,
116 flimflam::kBgscanShortIntervalProperty,
117 &WiFi::GetBgscanShortInterval,
118 &WiFi::SetBgscanShortInterval);
119 HelpRegisterDerivedInt32(store,
120 flimflam::kBgscanSignalThresholdProperty,
121 &WiFi::GetBgscanSignalThreshold,
122 &WiFi::SetBgscanSignalThreshold);
Chris Masone853b81b2011-06-24 14:11:41 -0700123
Chris Masoneb925cc82011-06-22 15:39:57 -0700124 // TODO(quiche): Decide if scan_pending_ is close enough to
125 // "currently scanning" that we don't care, or if we want to track
126 // scan pending/currently scanning/no scan scheduled as a tri-state
127 // kind of thing.
Paul Stewartac4ac002011-08-26 12:04:26 -0700128 store->RegisterConstBool(flimflam::kScanningProperty, &scan_pending_);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800129 HelpRegisterDerivedUint16(store,
130 flimflam::kScanIntervalProperty,
131 &WiFi::GetScanInterval,
132 &WiFi::SetScanInterval);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700133 SLOG(WiFi, 2) << "WiFi device " << link_name() << " initialized.";
Paul Stewartb50f0b92011-05-16 16:31:42 -0700134}
135
mukesh agrawalaf571952011-07-14 14:31:12 -0700136WiFi::~WiFi() {}
Paul Stewartb50f0b92011-05-16 16:31:42 -0700137
Eric Shienbrood9a245532012-03-07 14:20:39 -0500138void WiFi::Start(Error *error, const EnabledStateChangedCallback &callback) {
mukesh agrawalab87ea42011-05-18 11:44:49 -0700139 ::DBus::Path interface_path;
140
mukesh agrawalaf571952011-07-14 14:31:12 -0700141 supplicant_process_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -0700142 proxy_factory_->CreateSupplicantProcessProxy(
mukesh agrawal6e277772011-09-29 15:04:23 -0700143 wpa_supplicant::kDBusPath, wpa_supplicant::kDBusAddr));
mukesh agrawalc7426a42011-06-03 13:04:28 -0700144 try {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800145 map<string, DBus::Variant> create_interface_args;
Paul Stewart20550982012-04-16 12:16:11 -0700146 create_interface_args[wpa_supplicant::kInterfacePropertyName].writer().
Paul Stewartac4ac002011-08-26 12:04:26 -0700147 append_string(link_name().c_str());
Paul Stewart20550982012-04-16 12:16:11 -0700148 create_interface_args[wpa_supplicant::kInterfacePropertyDriver].writer().
mukesh agrawal6e277772011-09-29 15:04:23 -0700149 append_string(wpa_supplicant::kDriverNL80211);
Paul Stewart20550982012-04-16 12:16:11 -0700150 create_interface_args[
151 wpa_supplicant::kInterfacePropertyConfigFile].writer().
152 append_string(SCRIPTDIR "/wpa_supplicant.conf");
mukesh agrawalc7426a42011-06-03 13:04:28 -0700153 interface_path =
154 supplicant_process_proxy_->CreateInterface(create_interface_args);
Ben Chan80326f32012-05-04 17:51:32 -0700155 } catch (const DBus::Error &e) { // NOLINT
mukesh agrawal6e277772011-09-29 15:04:23 -0700156 if (!strcmp(e.name(), wpa_supplicant::kErrorInterfaceExists)) {
mukesh agrawalc7426a42011-06-03 13:04:28 -0700157 interface_path =
Paul Stewartac4ac002011-08-26 12:04:26 -0700158 supplicant_process_proxy_->GetInterface(link_name());
mukesh agrawal7ec71312011-11-10 02:08:26 +0000159 // TODO(quiche): Is it okay to crash here, if device is missing?
mukesh agrawalc7426a42011-06-03 13:04:28 -0700160 } else {
mukesh agrawal7ec71312011-11-10 02:08:26 +0000161 // TODO(quiche): Log error.
mukesh agrawalc7426a42011-06-03 13:04:28 -0700162 }
163 }
164
mukesh agrawalab87ea42011-05-18 11:44:49 -0700165 supplicant_interface_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -0700166 proxy_factory_->CreateSupplicantInterfaceProxy(
mukesh agrawal6e277772011-09-29 15:04:23 -0700167 this, interface_path, wpa_supplicant::kDBusAddr));
mukesh agrawalc7426a42011-06-03 13:04:28 -0700168
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169 RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index(), IFF_UP,
170 IFF_UP);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000171 // TODO(quiche) Set ApScan=1 and BSSExpireAge=190, like flimflam does?
mukesh agrawalc7426a42011-06-03 13:04:28 -0700172
mukesh agrawal7ec71312011-11-10 02:08:26 +0000173 // Clear out any networks that might previously have been configured
mukesh agrawalc7426a42011-06-03 13:04:28 -0700174 // for this interface.
175 supplicant_interface_proxy_->RemoveAllNetworks();
176
mukesh agrawal7ec71312011-11-10 02:08:26 +0000177 // Flush interface's BSS cache, so that we get BSSAdded signals for
mukesh agrawalc7426a42011-06-03 13:04:28 -0700178 // all BSSes (not just new ones since the last scan).
179 supplicant_interface_proxy_->FlushBSS(0);
180
mukesh agrawal23e9f282012-02-17 15:36:36 -0800181 try {
182 // TODO(pstew): Disable fast_reauth until supplicant can properly deal
183 // with RADIUS servers that respond strangely to such requests.
184 // crosbug.com/25630
185 supplicant_interface_proxy_->SetFastReauth(false);
Ben Chan80326f32012-05-04 17:51:32 -0700186 } catch (const DBus::Error &e) { // NOLINT
mukesh agrawalf2028172012-03-13 14:20:22 -0700187 LOG(INFO) << "Failed to disable fast_reauth."
188 << "May be running an older version of wpa_supplicant.";
189 }
190
191 try {
192 // Helps with passing WiFiRomaing.001SSIDSwitchBack.
193 supplicant_interface_proxy_->SetScanInterval(kRescanIntervalSeconds);
Ben Chan80326f32012-05-04 17:51:32 -0700194 } catch (const DBus::Error &e) { // NOLINT
mukesh agrawalf2028172012-03-13 14:20:22 -0700195 LOG(INFO) << "Failed to set scan_interval. "
mukesh agrawal23e9f282012-02-17 15:36:36 -0800196 << "May be running an older version of wpa_supplicant.";
197 }
Paul Stewart2987dcf2012-01-30 15:47:42 -0800198
Gary Morainac1bdb42012-02-16 17:42:29 -0800199 // Register for power state changes. HandlePowerStateChange() will be called
200 // when the power state changes.
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500201 manager()->power_manager()->AddStateChangeCallback(
202 UniqueName(),
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 Bind(&WiFi::HandlePowerStateChange, weak_ptr_factory_.GetWeakPtr()));
Gary Morainac1bdb42012-02-16 17:42:29 -0800204
Darin Petkovc0865312011-09-16 15:31:20 -0700205 Scan(NULL);
mukesh agrawalb66c6462012-05-07 11:45:25 -0700206 StartScanTimer();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500207 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
208 if (error)
209 error->Reset(); // indicate immediate completion
mukesh agrawalab87ea42011-05-18 11:44:49 -0700210}
211
Eric Shienbrood9a245532012-03-07 14:20:39 -0500212void WiFi::Stop(Error *error, const EnabledStateChangedCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700213 SLOG(WiFi, 2) << "WiFi " << link_name() << " stopping.";
mukesh agrawal7ec71312011-11-10 02:08:26 +0000214 // TODO(quiche): Remove interface from supplicant.
mukesh agrawalb66c6462012-05-07 11:45:25 -0700215 StopScanTimer();
mukesh agrawal31950242011-07-14 11:53:38 -0700216 supplicant_interface_proxy_.reset(); // breaks a reference cycle
217 supplicant_process_proxy_.reset();
mukesh agrawal15908392011-11-16 18:29:25 +0000218 endpoint_by_rpcid_.clear();
mukesh agrawal15908392011-11-16 18:29:25 +0000219 rpcid_by_service_.clear();
Gary Morain91001122012-02-29 16:22:26 -0800220 manager()->power_manager()->RemoveStateChangeCallback(UniqueName());
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700221
Paul Stewartced6a0b2011-11-08 15:32:04 -0800222 for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin();
223 it != services_.end();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700224 ++it) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700225 SLOG(WiFi, 3) << "WiFi " << link_name() << " deregistering service "
226 << (*it)->friendly_name();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700227 manager()->DeregisterService(*it);
228 }
Paul Stewartced6a0b2011-11-08 15:32:04 -0800229 services_.clear(); // breaks reference cycles
mukesh agrawalb20776f2012-02-10 16:00:36 -0800230 current_service_ = NULL; // breaks a reference cycle
mukesh agrawal7ec71312011-11-10 02:08:26 +0000231 pending_service_ = NULL; // breaks a reference cycle
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700232
Eric Shienbrood9a245532012-03-07 14:20:39 -0500233 OnEnabledStateChanged(EnabledStateChangedCallback(), Error());
234 if (error)
235 error->Reset(); // indicate immediate completion
mukesh agrawal7ec71312011-11-10 02:08:26 +0000236 // TODO(quiche): Anything else to do?
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700237
Ben Chanfad4a0b2012-04-18 15:49:59 -0700238 SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_process_proxy_ "
239 << (supplicant_process_proxy_.get() ?
240 "is set." : "is not set.");
241 SLOG(WiFi, 3) << "WiFi " << link_name() << " supplicant_interface_proxy_ "
242 << (supplicant_interface_proxy_.get() ?
243 "is set." : "is not set.");
244 SLOG(WiFi, 3) << "WiFi " << link_name() << " pending_service_ "
245 << (pending_service_.get() ? "is set." : "is not set.");
246 SLOG(WiFi, 3) << "WiFi " << link_name() << " has "
247 << endpoint_by_rpcid_.size() << " EndpointMap entries.";
248 SLOG(WiFi, 3) << "WiFi " << link_name() << " has " << services_.size()
249 << " Services.";
mukesh agrawalab87ea42011-05-18 11:44:49 -0700250}
251
Paul Stewarta41e38d2011-11-11 07:47:29 -0800252bool WiFi::Load(StoreInterface *storage) {
253 LoadHiddenServices(storage);
254 return Device::Load(storage);
255}
256
mukesh agrawal1830fa12011-09-26 14:31:40 -0700257void WiFi::Scan(Error */*error*/) {
mukesh agrawal32399322011-09-01 10:53:43 -0700258 LOG(INFO) << __func__;
259
mukesh agrawal7ec71312011-11-10 02:08:26 +0000260 // Needs to send a D-Bus message, but may be called from D-Bus
261 // signal handler context (via Manager::RequestScan). So defer work
mukesh agrawal32399322011-09-01 10:53:43 -0700262 // to event loop.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500263 dispatcher()->PostTask(Bind(&WiFi::ScanTask, weak_ptr_factory_.GetWeakPtr()));
mukesh agrawal32399322011-09-01 10:53:43 -0700264}
265
Paul Stewartfdd16072011-09-16 12:41:35 -0700266bool WiFi::TechnologyIs(const Technology::Identifier type) const {
267 return type == Technology::kWifi;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700268}
269
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000270bool WiFi::IsConnectingTo(const WiFiService &service) const {
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800271 return pending_service_ == &service ||
272 (current_service_ == &service &&
273 service.state() != Service::kStateConnected);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000274}
275
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000276void WiFi::BSSAdded(const ::DBus::Path &path,
277 const map<string, ::DBus::Variant> &properties) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500278 // Called from a D-Bus signal handler, and may need to send a D-Bus
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000279 // message. So defer work to event loop.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500280 dispatcher()->PostTask(Bind(&WiFi::BSSAddedTask,
281 weak_ptr_factory_.GetWeakPtr(),
282 path, properties));
mukesh agrawal261daca2011-12-02 18:56:56 +0000283}
284
285void WiFi::BSSRemoved(const ::DBus::Path &path) {
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500286 // Called from a D-Bus signal handler, and may need to send a D-Bus
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000287 // message. So defer work to event loop.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500288 dispatcher()->PostTask(Bind(&WiFi::BSSRemovedTask,
289 weak_ptr_factory_.GetWeakPtr(), path));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700290}
291
mukesh agrawal7ec71312011-11-10 02:08:26 +0000292void WiFi::PropertiesChanged(const map<string, ::DBus::Variant> &properties) {
mukesh agrawal15908392011-11-16 18:29:25 +0000293 LOG(INFO) << "In " << __func__ << "(): called";
294 // Called from D-Bus signal handler, but may need to send a D-Bus
295 // message. So defer work to event loop.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500296 dispatcher()->PostTask(Bind(&WiFi::PropertiesChangedTask,
297 weak_ptr_factory_.GetWeakPtr(), properties));
mukesh agrawal7ec71312011-11-10 02:08:26 +0000298}
299
mukesh agrawalb54601c2011-06-07 17:39:22 -0700300void WiFi::ScanDone() {
301 LOG(INFO) << __func__;
302
mukesh agrawal7ec71312011-11-10 02:08:26 +0000303 // Defer handling of scan result processing, because that processing
304 // may require the the registration of new D-Bus objects. And such
mukesh agrawalb54601c2011-06-07 17:39:22 -0700305 // registration can't be done in the context of a D-Bus signal
306 // handler.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500307 dispatcher()->PostTask(Bind(&WiFi::ScanDoneTask,
308 weak_ptr_factory_.GetWeakPtr()));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700309}
310
mukesh agrawal6e277772011-09-29 15:04:23 -0700311void WiFi::ConnectTo(WiFiService *service,
mukesh agrawal64896322011-12-01 01:13:10 +0000312 map<string, DBus::Variant> service_params) {
mukesh agrawale9adda12012-02-09 18:33:48 -0800313 CHECK(service) << "Can't connect to NULL service.";
mukesh agrawal445e72c2011-06-22 11:13:50 -0700314 DBus::Path network_path;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700315
mukesh agrawal7ec71312011-11-10 02:08:26 +0000316 // TODO(quiche): Handle cases where already connected.
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000317 if (pending_service_ && pending_service_ == service) {
318 // TODO(quiche): Return an error to the caller. crosbug.com/23832
319 LOG(INFO) << "WiFi " << link_name() << " ignoring ConnectTo "
320 << service->friendly_name()
321 << ", which is already pending.";
322 return;
323 }
324
325 if (pending_service_ && pending_service_ != service) {
326 DisconnectFrom(pending_service_);
327 }
mukesh agrawal32399322011-09-01 10:53:43 -0700328
mukesh agrawal6e277772011-09-29 15:04:23 -0700329 try {
mukesh agrawal15908392011-11-16 18:29:25 +0000330 // TODO(quiche): Set a timeout here. In the normal case, we expect
331 // that, if wpa_supplicant fails to connect, it will eventually send
332 // a signal that its CurrentBSS has changed. But there may be cases
333 // where the signal is not sent. (crosbug.com/23206)
mukesh agrawal64896322011-12-01 01:13:10 +0000334 const uint32_t scan_ssid = 1; // "True": Use directed probe.
335 service_params[wpa_supplicant::kNetworkPropertyScanSSID].writer().
336 append_uint32(scan_ssid);
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800337 service_params[wpa_supplicant::kNetworkPropertyBgscan].writer().
338 append_string(CreateBgscanConfigString().c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700339 network_path =
340 supplicant_interface_proxy_->AddNetwork(service_params);
mukesh agrawal15908392011-11-16 18:29:25 +0000341 rpcid_by_service_[service] = network_path;
Ben Chan80326f32012-05-04 17:51:32 -0700342 } catch (const DBus::Error &e) { // NOLINT
mukesh agrawal6e277772011-09-29 15:04:23 -0700343 LOG(ERROR) << "exception while adding network: " << e.what();
344 return;
345 }
mukesh agrawal445e72c2011-06-22 11:13:50 -0700346
mukesh agrawal445e72c2011-06-22 11:13:50 -0700347 supplicant_interface_proxy_->SelectNetwork(network_path);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000348 pending_service_ = service;
349 CHECK(current_service_.get() != pending_service_.get());
350
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700351 // SelectService here (instead of in LinkEvent, like Ethernet), so
352 // that, if we fail to bring up L2, we can attribute failure correctly.
353 //
mukesh agrawal7ec71312011-11-10 02:08:26 +0000354 // TODO(quiche): When we add code for dealing with connection failures,
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700355 // reconsider if this is the right place to change the selected service.
356 // see discussion in crosbug.com/20191.
357 SelectService(service);
mukesh agrawal15908392011-11-16 18:29:25 +0000358}
359
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000360void WiFi::DisconnectFrom(WiFiService *service) {
361 if (service != current_service_ && service != pending_service_) {
362 // TODO(quiche): Once we have asynchronous reply support, we should
363 // generate a D-Bus error here. (crosbug.com/23832)
364 LOG(WARNING) << "In " << __func__ << "(): "
365 << " ignoring request to disconnect from service "
366 << service->friendly_name()
367 << " which is neither current nor pending";
368 return;
369 }
370
371 if (pending_service_ && service != pending_service_) {
372 // TODO(quiche): Once we have asynchronous reply support, we should
373 // generate a D-Bus error here. (crosbug.com/23832)
374 LOG(WARNING) << "In " << __func__ << "(): "
375 << " ignoring request to disconnect from service "
376 << service->friendly_name()
377 << " which is not the pending service.";
378 return;
379 }
380
381 if (!pending_service_ && service != current_service_) {
382 // TODO(quiche): Once we have asynchronous reply support, we should
383 // generate a D-Bus error here. (crosbug.com/23832)
384 LOG(WARNING) << "In " << __func__ << "(): "
385 << " ignoring request to disconnect from service "
386 << service->friendly_name()
387 << " which is not the current service.";
388 return;
389 }
390
391 pending_service_ = NULL;
392 try {
393 supplicant_interface_proxy_->Disconnect();
394 // We'll call RemoveNetwork and reset |current_service_| after
395 // supplicant notifies us that the CurrentBSS has changed.
Ben Chan80326f32012-05-04 17:51:32 -0700396 } catch (const DBus::Error &e) { // NOLINT
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000397 // Can't depend on getting a notification of CurrentBSS change.
398 // So effect changes immediately.
399 ReverseServiceMap::const_iterator rpcid_it =
400 rpcid_by_service_.find(service);
401 DCHECK(rpcid_it != rpcid_by_service_.end());
402 if (rpcid_it == rpcid_by_service_.end()) {
403 LOG(WARNING) << "WiFi " << link_name() << " can not disconnect from "
404 << service->friendly_name() << ": "
405 << "could not find supplicant network to disable.";
406 } else {
407 supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second);
408 }
409 current_service_ = NULL;
410 }
411
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800412 CHECK(current_service_ == NULL ||
413 current_service_.get() != pending_service_.get());
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000414}
415
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000416bool WiFi::IsIdle() const {
Paul Stewart3d9bcf52011-12-12 15:02:22 -0800417 return !current_service_ && !pending_service_;
418}
419
Paul Stewart66c86002012-01-30 18:00:52 -0800420void WiFi::ClearCachedCredentials() {
421 LOG(INFO) << __func__;
422
423 // Needs to send a D-Bus message, but may be called from D-Bus
424 // caller context (via Manager::PopProfile). So defer work
425 // to event loop.
426 if (!clear_cached_credentials_pending_) {
427 clear_cached_credentials_pending_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500428 dispatcher()->PostTask(Bind(&WiFi::ClearCachedCredentialsTask,
429 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart66c86002012-01-30 18:00:52 -0800430 }
431}
432
mukesh agrawalb20776f2012-02-10 16:00:36 -0800433void WiFi::NotifyEndpointChanged(const WiFiEndpoint &endpoint) {
434 WiFiService *service = FindServiceForEndpoint(endpoint);
435 DCHECK(service);
436 if (service) {
437 service->NotifyEndpointUpdated(endpoint);
438 return;
439 }
440}
441
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800442string WiFi::CreateBgscanConfigString() {
443 return StringPrintf("%s:%d:%d:%d",
444 bgscan_method_.c_str(),
445 bgscan_short_interval_seconds_,
446 bgscan_signal_threshold_dbm_,
447 scan_interval_seconds_);
448}
449
450void WiFi::SetBgscanMethod(const string &method, Error *error) {
451 if (method != wpa_supplicant::kNetworkBgscanMethodSimple &&
452 method != wpa_supplicant::kNetworkBgscanMethodLearn) {
453 const string error_message =
454 StringPrintf("Unrecognized bgscan method %s", method.c_str());
455 LOG(WARNING) << error_message;
456 error->Populate(Error::kInvalidArguments, error_message);
457 return;
458 }
459
460 bgscan_method_ = method;
461 // We do not update kNetworkPropertyBgscan for |pending_service_| or
462 // |current_service_|, because supplicant does not allow for
463 // reconfiguration without disconnect and reconnect.
464}
465
466void WiFi::SetBgscanShortInterval(const uint16 &seconds, Error */*error*/) {
467 bgscan_short_interval_seconds_ = seconds;
468 // We do not update kNetworkPropertyBgscan for |pending_service_| or
469 // |current_service_|, because supplicant does not allow for
470 // reconfiguration without disconnect and reconnect.
471}
472
473void WiFi::SetBgscanSignalThreshold(const int32 &dbm, Error */*error*/) {
474 bgscan_signal_threshold_dbm_ = dbm;
475 // We do not update kNetworkPropertyBgscan for |pending_service_| or
476 // |current_service_|, because supplicant does not allow for
477 // reconfiguration without disconnect and reconnect.
478}
479
480void WiFi::SetScanInterval(const uint16 &seconds, Error */*error*/) {
481 scan_interval_seconds_ = seconds;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700482 if (running()) {
483 StartScanTimer();
484 }
485 // The scan interval affects both foreground scans (handled by
486 // |scan_timer_callback_|), and background scans (handled by
487 // supplicant). However, we do not update |pending_service_| or
488 // |current_service_|, because supplicant does not allow for
489 // reconfiguration without disconnect and reconnect.
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800490}
491
mukesh agrawal165e6142011-11-22 02:22:56 +0000492// To avoid creating duplicate services, call FindServiceForEndpoint
493// before calling this method.
mukesh agrawal15908392011-11-16 18:29:25 +0000494WiFiServiceRefPtr WiFi::CreateServiceForEndpoint(const WiFiEndpoint &endpoint,
495 bool hidden_ssid) {
496 WiFiServiceRefPtr service =
497 new WiFiService(control_interface(),
498 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800499 metrics(),
mukesh agrawal15908392011-11-16 18:29:25 +0000500 manager(),
501 this,
502 endpoint.ssid(),
503 endpoint.network_mode(),
504 endpoint.security_mode(),
505 hidden_ssid);
mukesh agrawal165e6142011-11-22 02:22:56 +0000506 services_.push_back(service);
mukesh agrawal15908392011-11-16 18:29:25 +0000507 return service;
508}
509
510void WiFi::CurrentBSSChanged(const ::DBus::Path &new_bss) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700511 SLOG(WiFi, 3) << "WiFi " << link_name() << " CurrentBSS "
512 << supplicant_bss_ << " -> " << new_bss;
mukesh agrawal15908392011-11-16 18:29:25 +0000513 supplicant_bss_ = new_bss;
514 if (new_bss == wpa_supplicant::kCurrentBSSNull) {
515 HandleDisconnect();
mukesh agrawalb66c6462012-05-07 11:45:25 -0700516 if (!GetHiddenSSIDList().empty()) {
517 // Before disconnecting, wpa_supplicant probably scanned for
518 // APs. So, in the normal case, we defer to the timer for the next scan.
519 //
520 // However, in the case of hidden SSIDs, supplicant knows about
521 // at most one of them. (That would be the hidden SSID we were
522 // connected to, if applicable.)
523 //
524 // So, in this case, we initiate an immediate scan. This scan
525 // will include the hidden SSIDs we know about (up to the limit of
526 // kScanMAxSSIDsPerScan).
527 //
528 // We may want to reconsider this immediate scan, if/when shill
529 // takes greater responsibility for scanning (vs. letting
530 // supplicant handle most of it).
531 Scan(NULL);
532 }
mukesh agrawal15908392011-11-16 18:29:25 +0000533 } else {
534 HandleRoam(new_bss);
535 }
536
537 SelectService(current_service_);
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000538 // Invariant check: a Service can either be current, or pending, but
539 // not both.
mukesh agrawal15908392011-11-16 18:29:25 +0000540 CHECK(current_service_.get() != pending_service_.get() ||
541 current_service_.get() == NULL);
542
543 // TODO(quiche): Update BSSID property on the Service
544 // (crosbug.com/22377).
545}
546
547void WiFi::HandleDisconnect() {
548 // Identify the affected service. We expect to get a disconnect
549 // event when we fall off a Service that we were connected
550 // to. However, we also allow for the case where we get a disconnect
551 // event while attempting to connect from a disconnected state.
552 WiFiService *affected_service =
553 current_service_.get() ? current_service_.get() : pending_service_.get();
554
555 current_service_ = NULL;
556 if (!affected_service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700557 SLOG(WiFi, 2) << "WiFi " << link_name()
558 << " disconnected while not connected or connecting";
mukesh agrawal15908392011-11-16 18:29:25 +0000559 return;
560 }
561
562 ReverseServiceMap::const_iterator rpcid_it =
563 rpcid_by_service_.find(affected_service);
564 if (rpcid_it == rpcid_by_service_.end()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700565 SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from "
566 << " (or failed to connect to) "
567 << affected_service->friendly_name() << ", "
568 << "but could not find supplicant network to disable.";
mukesh agrawal15908392011-11-16 18:29:25 +0000569 return;
570 }
571
Ben Chanfad4a0b2012-04-18 15:49:59 -0700572 SLOG(WiFi, 2) << "WiFi " << link_name() << " disconnected from "
573 << " (or failed to connect to) "
574 << affected_service->friendly_name();
mukesh agrawal15908392011-11-16 18:29:25 +0000575 // TODO(quiche): Reconsider giving up immediately. Maybe give
576 // wpa_supplicant some time to retry, first.
577 supplicant_interface_proxy_->RemoveNetwork(rpcid_it->second);
mukesh agrawal568b5c62012-02-28 14:44:47 -0800578 // TOOD(quiche): In the event that the disconnect was deliberate, we
579 // might want to go to SetState(kStateIdle), rather than reporting a
580 // failure. crosbug.com/24700.
581 // TODO(quiche): In the event that we suspect a password failure,
582 // we should not be silent. crosbug.com/23211.
583 affected_service->SetFailureSilent(Service::kFailureUnknown);
mukesh agrawale1d90e92012-02-15 17:36:08 -0800584 affected_service->NotifyCurrentEndpoint(NULL);
Thieu Le67370f62012-02-14 23:01:42 +0000585 metrics()->NotifyServiceDisconnect(affected_service);
mukesh agrawal15908392011-11-16 18:29:25 +0000586
587 if (affected_service == pending_service_.get()) {
588 // The attempt to connect to |pending_service_| failed. Clear
589 // |pending_service_|, to indicate we're no longer in the middle
590 // of a connect request.
591 pending_service_ = NULL;
592 } else if (pending_service_.get()) {
593 // We've attributed the disconnection to what was the
594 // |current_service_|, rather than the |pending_service_|.
595 //
596 // If we're wrong about that (i.e. supplicant reported this
597 // CurrentBSS change after attempting to connect to
598 // |pending_service_|), we're depending on supplicant to retry
599 // connecting to |pending_service_|, and delivering another
600 // CurrentBSS change signal in the future.
601 //
602 // Log this fact, to help us debug (in case our assumptions are
603 // wrong).
Ben Chanfad4a0b2012-04-18 15:49:59 -0700604 SLOG(WiFi, 2) << "WiFi " << link_name() << " pending connection to "
605 << pending_service_->friendly_name()
606 << " after disconnect";
mukesh agrawal15908392011-11-16 18:29:25 +0000607 }
608}
609
610// We use the term "Roam" loosely. In particular, we include the case
611// where we "Roam" to a BSS from the disconnected state.
612void WiFi::HandleRoam(const ::DBus::Path &new_bss) {
613 EndpointMap::iterator endpoint_it = endpoint_by_rpcid_.find(new_bss);
614 if (endpoint_it == endpoint_by_rpcid_.end()) {
615 LOG(WARNING) << "WiFi " << link_name() << " connected to unknown BSS "
616 << new_bss;
617 return;
618 }
619
620 const WiFiEndpoint &endpoint(*endpoint_it->second);
mukesh agrawal165e6142011-11-22 02:22:56 +0000621 WiFiServiceRefPtr service = FindServiceForEndpoint(endpoint);
mukesh agrawal15908392011-11-16 18:29:25 +0000622 if (!service.get()) {
623 LOG(WARNING) << "WiFi " << link_name()
624 << " could not find Service for Endpoint "
625 << endpoint.bssid_string()
626 << " (service will be unchanged)";
627 return;
628 }
629
Ben Chanfad4a0b2012-04-18 15:49:59 -0700630 SLOG(WiFi, 2) << "WiFi " << link_name()
631 << " roamed to Endpoint " << endpoint.bssid_string()
632 << " (SSID " << endpoint.ssid_string() << ")";
mukesh agrawal15908392011-11-16 18:29:25 +0000633
mukesh agrawale1d90e92012-02-15 17:36:08 -0800634 service->NotifyCurrentEndpoint(&endpoint);
Thieu Lee41a72d2012-02-06 20:46:51 +0000635
mukesh agrawal15908392011-11-16 18:29:25 +0000636 if (pending_service_.get() &&
637 service.get() != pending_service_.get()) {
638 // The Service we've roamed on to is not the one we asked for.
639 // We assume that this is transient, and that wpa_supplicant
640 // is trying / will try to connect to |pending_service_|.
641 //
642 // If it succeeds, we'll end up back here, but with |service|
643 // pointing at the same service as |pending_service_|.
644 //
645 // If it fails, we'll process things in HandleDisconnect.
646 //
647 // So we leave |pending_service_| untouched.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700648 SLOG(WiFi, 2) << "WiFi " << link_name()
649 << " new current Endpoint "
650 << endpoint.bssid_string()
651 << " is not part of pending service "
652 << pending_service_->friendly_name();
mukesh agrawal15908392011-11-16 18:29:25 +0000653
654 // Sanity check: if we didn't roam onto |pending_service_|, we
655 // should still be on |current_service_|.
656 if (service.get() != current_service_.get()) {
657 LOG(WARNING) << "WiFi " << link_name()
658 << " new current Endpoint "
659 << endpoint.bssid_string()
660 << " is neither part of pending service "
661 << pending_service_->friendly_name()
662 << " nor part of current service "
663 << (current_service_.get() ?
664 current_service_->friendly_name() :
665 "(NULL)");
666 // Although we didn't expect to get here, we should keep
667 // |current_service_| in sync with what supplicant has done.
668 current_service_ = service;
669 }
670 return;
671 }
672
673 if (pending_service_.get()) {
674 // We assume service.get() == pending_service_.get() here, because
675 // of the return in the previous if clause.
676 //
677 // Boring case: we've connected to the service we asked
678 // for. Simply update |current_service_| and |pending_service_|.
679 current_service_ = service;
680 pending_service_ = NULL;
681 return;
682 }
683
684 // |pending_service_| was NULL, so we weren't attempting to connect
685 // to a new Service. Sanity check that we're still on
686 // |current_service_|.
687 if (service.get() != current_service_.get()) {
688 LOG(WARNING)
689 << "WiFi " << link_name()
690 << " new current Endpoint "
691 << endpoint.bssid_string()
692 << (current_service_.get() ?
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000693 StringPrintf(" is not part of current service %s",
mukesh agrawal15908392011-11-16 18:29:25 +0000694 current_service_->friendly_name().c_str()) :
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000695 " with no current service");
mukesh agrawal15908392011-11-16 18:29:25 +0000696 // We didn't expect to be here, but let's cope as well as we
697 // can. Update |current_service_| to keep it in sync with
698 // supplicant.
699 current_service_ = service;
700 return;
701 }
702
703 // At this point, we know that |pending_service_| was NULL, and that
704 // we're still on |current_service_|. This is the most boring case
705 // of all, because there's no state to update here.
706 return;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700707}
708
Paul Stewarta41e38d2011-11-11 07:47:29 -0800709WiFiServiceRefPtr WiFi::FindService(const vector<uint8_t> &ssid,
710 const string &mode,
711 const string &security) const {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800712 for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin();
713 it != services_.end();
714 ++it) {
715 if ((*it)->ssid() == ssid && (*it)->mode() == mode &&
716 (*it)->IsSecurityMatch(security)) {
717 return *it;
718 }
719 }
720 return NULL;
721}
722
mukesh agrawal165e6142011-11-22 02:22:56 +0000723WiFiServiceRefPtr WiFi::FindServiceForEndpoint(const WiFiEndpoint &endpoint) {
724 return FindService(endpoint.ssid(),
725 endpoint.network_mode(),
726 endpoint.security_mode());
727}
728
Paul Stewartced6a0b2011-11-08 15:32:04 -0800729ByteArrays WiFi::GetHiddenSSIDList() {
730 // Create a unique set of hidden SSIDs.
731 set<ByteArray> hidden_ssids_set;
732 for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin();
733 it != services_.end();
734 ++it) {
Paul Stewart10ccbb32012-04-26 15:59:30 -0700735 if ((*it)->hidden_ssid() && (*it)->IsRemembered()) {
Paul Stewartced6a0b2011-11-08 15:32:04 -0800736 hidden_ssids_set.insert((*it)->ssid());
737 }
738 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700739 SLOG(WiFi, 2) << "Found " << hidden_ssids_set.size() << " hidden services";
Paul Stewartced6a0b2011-11-08 15:32:04 -0800740 ByteArrays hidden_ssids(hidden_ssids_set.begin(), hidden_ssids_set.end());
741 if (!hidden_ssids.empty()) {
742 // TODO(pstew): Devise a better method for time-sharing with SSIDs that do
743 // not fit in.
744 if (hidden_ssids.size() >= wpa_supplicant::kScanMaxSSIDsPerScan) {
745 hidden_ssids.erase(
746 hidden_ssids.begin() + wpa_supplicant::kScanMaxSSIDsPerScan - 1,
747 hidden_ssids.end());
748 }
749 // Add Broadcast SSID, signified by an empty ByteArray. If we specify
750 // SSIDs to wpa_supplicant, we need to explicitly specify the default
751 // behavior of doing a broadcast probe.
752 hidden_ssids.push_back(ByteArray());
753 }
754 return hidden_ssids;
755}
756
Paul Stewarta41e38d2011-11-11 07:47:29 -0800757bool WiFi::LoadHiddenServices(StoreInterface *storage) {
758 bool created_hidden_service = false;
759 set<string> groups = storage->GetGroupsWithKey(flimflam::kWifiHiddenSsid);
760 for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) {
761 bool is_hidden = false;
762 if (!storage->GetBool(*it, flimflam::kWifiHiddenSsid, &is_hidden)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700763 SLOG(WiFi, 2) << "Storage group " << *it << " returned by "
764 << "GetGroupsWithKey failed for GetBool("
765 << flimflam::kWifiHiddenSsid
766 << ") -- possible non-bool key";
Paul Stewarta41e38d2011-11-11 07:47:29 -0800767 continue;
768 }
769 if (!is_hidden) {
770 continue;
771 }
772 string ssid_hex;
773 vector<uint8_t> ssid_bytes;
774 if (!storage->GetString(*it, flimflam::kSSIDProperty, &ssid_hex) ||
775 !base::HexStringToBytes(ssid_hex, &ssid_bytes)) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700776 SLOG(WiFi, 2) << "Hidden network is missing/invalid \""
777 << flimflam::kSSIDProperty << "\" property";
Paul Stewarta41e38d2011-11-11 07:47:29 -0800778 continue;
779 }
780 string device_address;
781 string network_mode;
782 string security;
783 // It is gross that we have to do this, but the only place we can
784 // get information about the service is from its storage name.
785 if (!WiFiService::ParseStorageIdentifier(*it, &device_address,
786 &network_mode, &security) ||
787 device_address != address()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700788 SLOG(WiFi, 2) << "Hidden network has unparsable storage identifier \""
789 << *it << "\"";
Paul Stewarta41e38d2011-11-11 07:47:29 -0800790 continue;
791 }
792 if (FindService(ssid_bytes, network_mode, security).get()) {
793 // If service already exists, we have nothing to do, since the
794 // service has already loaded its configuration from storage.
795 // This is guaranteed to happen in both cases where Load() is
796 // called on a device (via a ConfigureDevice() call on the
797 // profile):
798 // - In RegisterDevice() the Device hasn't been started yet,
799 // so it has no services registered, except for those
800 // created by previous iterations of LoadHiddenService().
801 // The latter can happen if another profile in the Manager's
802 // stack defines the same ssid/mode/security. Even this
803 // case is okay, since even if the profiles differ
804 // materially on configuration and credentials, the "right"
805 // one will be configured in the course of the
806 // RegisterService() call below.
807 // - In PushProfile(), all registered services have been
808 // introduced to the profile via ConfigureService() prior
809 // to calling ConfigureDevice() on the profile.
810 continue;
811 }
812 WiFiServiceRefPtr service(new WiFiService(control_interface(),
813 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -0800814 metrics(),
Paul Stewarta41e38d2011-11-11 07:47:29 -0800815 manager(),
816 this,
817 ssid_bytes,
818 network_mode,
819 security,
820 true));
821 services_.push_back(service);
822
823 // By registering the service, the rest of the configuration
824 // will be loaded from the profile into the service via ConfigureService().
825 manager()->RegisterService(service);
826
827 created_hidden_service = true;
828 }
829
830 // If we are idle and we created a service as a result of opening the
831 // profile, we should initiate a scan for our new hidden SSID.
832 if (running() && created_hidden_service &&
833 supplicant_state_ == wpa_supplicant::kInterfaceStateInactive) {
834 Scan(NULL);
835 }
836
837 return created_hidden_service;
838}
839
Paul Stewart66c86002012-01-30 18:00:52 -0800840void WiFi::ClearCachedCredentialsTask() {
Paul Stewart57f3d9d2012-04-11 18:21:41 -0700841 try {
842 supplicant_interface_proxy_->ClearCachedCredentials();
Ben Chan80326f32012-05-04 17:51:32 -0700843 } catch (const DBus::Error &e) { // NOLINT
Paul Stewart57f3d9d2012-04-11 18:21:41 -0700844 LOG(WARNING) << "Clear of cached credentials failed.";
845 }
Paul Stewart66c86002012-01-30 18:00:52 -0800846 clear_cached_credentials_pending_ = false;
847}
848
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000849void WiFi::BSSAddedTask(
850 const ::DBus::Path &path,
851 const map<string, ::DBus::Variant> &properties) {
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000852 // Note: we assume that BSSIDs are unique across endpoints. This
853 // means that if an AP reuses the same BSSID for multiple SSIDs, we
854 // lose.
mukesh agrawalb20776f2012-02-10 16:00:36 -0800855 WiFiEndpointRefPtr endpoint(
856 new WiFiEndpoint(proxy_factory_, this, path, properties));
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000857 LOG(INFO) << "Found endpoint. "
mukesh agrawale9adda12012-02-09 18:33:48 -0800858 << "RPC path: " << path << ", "
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000859 << "ssid: " << endpoint->ssid_string() << ", "
860 << "bssid: " << endpoint->bssid_string() << ", "
861 << "signal: " << endpoint->signal_strength() << ", "
Thieu Lee41a72d2012-02-06 20:46:51 +0000862 << "security: " << endpoint->security_mode() << ", "
863 << "frequency: " << endpoint->frequency();
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000864
865 if (endpoint->ssid_string().empty()) {
866 // Don't bother trying to find or create a Service for an Endpoint
867 // without an SSID. We wouldn't be able to connect to it anyway.
868 return;
869 }
870
mukesh agrawalb3857612012-01-18 16:23:29 -0800871 if (endpoint->ssid()[0] == 0) {
872 // Assume that an SSID starting with NULL is bogus/misconfigured,
873 // and filter it out.
874 return;
875 }
876
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000877 WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint);
878 if (service) {
879 LOG(INFO) << "Assigned endpoint " << endpoint->bssid_string()
880 << " to service " << service->friendly_name() << ".";
881 service->AddEndpoint(endpoint);
882
883 if (manager()->HasService(service)) {
884 manager()->UpdateService(service);
885 } else {
886 DCHECK_EQ(1, service->NumEndpoints()); // Expect registered by now if >1.
887 manager()->RegisterService(service);
888 }
mukesh agrawale9adda12012-02-09 18:33:48 -0800889 } else {
890 const bool hidden_ssid = false;
891 service = CreateServiceForEndpoint(*endpoint, hidden_ssid);
892 LOG(INFO) << "New service " << service->GetRpcIdentifier()
893 << " (" << service->friendly_name() << ")";
894 service->AddEndpoint(endpoint);
895 manager()->RegisterService(service);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000896 }
897
mukesh agrawale9adda12012-02-09 18:33:48 -0800898 // Do this last, to maintain the invariant that any Endpoint we
899 // know about has a corresponding Service.
mukesh agrawalb20776f2012-02-10 16:00:36 -0800900 //
901 // TODO(quiche): Write test to verify correct behavior in the case
902 // where we get multiple BSSAdded events for a single endpoint.
903 // (Old Endpoint's refcount should fall to zero, and old Endpoint
904 // should be destroyed.)
mukesh agrawale9adda12012-02-09 18:33:48 -0800905 endpoint_by_rpcid_[path] = endpoint;
mukesh agrawalb20776f2012-02-10 16:00:36 -0800906 endpoint->Start();
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000907}
908
909void WiFi::BSSRemovedTask(const ::DBus::Path &path) {
910 EndpointMap::iterator i = endpoint_by_rpcid_.find(path);
911 if (i == endpoint_by_rpcid_.end()) {
912 LOG(WARNING) << "WiFi " << link_name()
913 << " could not find BSS " << path
914 << " to remove.";
915 return;
916 }
917
918 WiFiEndpointRefPtr endpoint = i->second;
919 CHECK(endpoint);
920 endpoint_by_rpcid_.erase(i);
921
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000922 WiFiServiceRefPtr service = FindServiceForEndpoint(*endpoint);
mukesh agrawale9adda12012-02-09 18:33:48 -0800923 CHECK(service) << "Can't find Service for Endpoint "
924 << path << " "
925 << "(with BSSID " << endpoint->bssid_string() << ").";
Ben Chanfad4a0b2012-04-18 15:49:59 -0700926 SLOG(WiFi, 2) << "Removing Endpoint " << endpoint->bssid_string()
927 << " from Service " << service->friendly_name();
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000928 service->RemoveEndpoint(endpoint);
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000929
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000930 bool disconnect_service = !service->HasEndpoints() &&
931 (service->IsConnecting() || service->IsConnected());
932 bool forget_service =
933 // Forget Services without Endpoints, except that we always keep
934 // hidden services around. (We need them around to populate the
935 // hidden SSIDs list.)
936 !service->HasEndpoints() && !service->hidden_ssid();
937 bool deregister_service =
938 // Only deregister a Service if we're forgetting it. Otherwise,
939 // Manager can't keep our configuration up-to-date (as Profiles
940 // change).
941 forget_service;
942
943 if (disconnect_service) {
944 DisconnectFrom(service);
945 }
946
947 if (deregister_service) {
948 manager()->DeregisterService(service);
949 } else {
950 manager()->UpdateService(service);
951 }
952
953 if (forget_service) {
954 vector<WiFiServiceRefPtr>::iterator it;
955 it = std::find(services_.begin(), services_.end(), service);
956 if (it != services_.end()) {
957 services_.erase(it);
958 }
959 }
mukesh agrawalb4bc57d2011-12-07 01:07:47 +0000960}
961
mukesh agrawal15908392011-11-16 18:29:25 +0000962void WiFi::PropertiesChangedTask(
963 const map<string, ::DBus::Variant> &properties) {
964 // TODO(quiche): Handle changes in other properties (e.g. signal
965 // strength).
966
967 // Note that order matters here. In particular, we want to process
968 // changes in the current BSS before changes in state. This is so
969 // that we update the state of the correct Endpoint/Service.
970
971 map<string, ::DBus::Variant>::const_iterator properties_it =
972 properties.find(wpa_supplicant::kInterfacePropertyCurrentBSS);
973 if (properties_it != properties.end()) {
974 CurrentBSSChanged(properties_it->second.reader().get_path());
975 }
976
977 properties_it = properties.find(wpa_supplicant::kInterfacePropertyState);
978 if (properties_it != properties.end()) {
979 StateChanged(properties_it->second.reader().get_string());
980 }
981}
982
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700983void WiFi::ScanDoneTask() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700984 SLOG(WiFi, 2) << __func__ << " need_bss_flush_ " << need_bss_flush_;
mukesh agrawal5c05b292012-03-07 10:12:52 -0800985 if (need_bss_flush_) {
986 CHECK(supplicant_interface_proxy_ != NULL);
987 // Compute |max_age| relative to |resumed_at_|, to account for the
988 // time taken to scan.
989 struct timeval now;
990 uint32_t max_age;
991 time_->GetTimeMonotonic(&now);
992 max_age = kMaxBSSResumeAgeSeconds + (now.tv_sec - resumed_at_.tv_sec);
993 supplicant_interface_proxy_->FlushBSS(max_age);
994 need_bss_flush_ = false;
995 }
mukesh agrawalb54601c2011-06-07 17:39:22 -0700996 scan_pending_ = false;
mukesh agrawalb66c6462012-05-07 11:45:25 -0700997 StartScanTimer();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700998}
999
mukesh agrawal32399322011-09-01 10:53:43 -07001000void WiFi::ScanTask() {
Ben Chanfad4a0b2012-04-18 15:49:59 -07001001 SLOG(WiFi, 2) << "WiFi " << link_name() << " scan requested.";
Paul Stewarta41e38d2011-11-11 07:47:29 -08001002 map<string, DBus::Variant> scan_args;
mukesh agrawal6e277772011-09-29 15:04:23 -07001003 scan_args[wpa_supplicant::kPropertyScanType].writer().
1004 append_string(wpa_supplicant::kScanTypeActive);
Paul Stewartced6a0b2011-11-08 15:32:04 -08001005
1006 ByteArrays hidden_ssids = GetHiddenSSIDList();
1007 if (!hidden_ssids.empty()) {
1008 scan_args[wpa_supplicant::kPropertyScanSSIDs] =
1009 DBusAdaptor::ByteArraysToVariant(hidden_ssids);
1010 }
1011
Gaurav Shahf8721ee2011-11-07 09:12:46 -08001012 // TODO(quiche): Indicate scanning in UI. crosbug.com/14887
Gaurav Shah10109f22011-11-11 20:16:22 -08001013 // FIXME(gauravsh): A scan can fail if, for example, wpa_supplicant
1014 // was restarted. This is done by a few of the 802.1x tests.
1015 // crosbug.com/25657
1016 try {
1017 supplicant_interface_proxy_->Scan(scan_args);
1018 scan_pending_ = true;
Ben Chan80326f32012-05-04 17:51:32 -07001019 } catch (const DBus::Error &e) { // NOLINT
Gaurav Shah10109f22011-11-11 20:16:22 -08001020 LOG(WARNING) << "Scan failed. Attempting to re-connect to the supplicant.";
Eric Shienbrood9a245532012-03-07 14:20:39 -05001021 EnabledStateChangedCallback null_callback;
1022 Stop(NULL, null_callback);
1023 Start(NULL, null_callback);
Gaurav Shah10109f22011-11-11 20:16:22 -08001024 }
mukesh agrawal32399322011-09-01 10:53:43 -07001025}
1026
mukesh agrawal15908392011-11-16 18:29:25 +00001027void WiFi::StateChanged(const string &new_state) {
1028 const string old_state = supplicant_state_;
mukesh agrawal7ec71312011-11-10 02:08:26 +00001029 supplicant_state_ = new_state;
mukesh agrawal15908392011-11-16 18:29:25 +00001030 LOG(INFO) << "WiFi " << link_name() << " " << __func__ << " "
1031 << old_state << " -> " << new_state;
1032
1033 WiFiService *affected_service;
1034 // Identify the service to which the state change applies. If
1035 // |pending_service_| is non-NULL, then the state change applies to
1036 // |pending_service_|. Otherwise, it applies to |current_service_|.
1037 //
1038 // This policy is driven by the fact that the |pending_service_|
1039 // doesn't become the |current_service_| until wpa_supplicant
1040 // reports a CurrentBSS change to the |pending_service_|. And the
mukesh agrawalc01f3982012-01-24 13:48:39 -08001041 // CurrentBSS change won't be reported until the |pending_service_|
mukesh agrawal15908392011-11-16 18:29:25 +00001042 // reaches the wpa_supplicant::kInterfaceStateCompleted state.
1043 affected_service =
1044 pending_service_.get() ? pending_service_.get() : current_service_.get();
1045 if (!affected_service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -07001046 SLOG(WiFi, 2) << "WiFi " << link_name() << " " << __func__
1047 << " with no service";
mukesh agrawal15908392011-11-16 18:29:25 +00001048 return;
1049 }
1050
mukesh agrawalc01f3982012-01-24 13:48:39 -08001051 if (new_state == wpa_supplicant::kInterfaceStateCompleted &&
1052 !affected_service->IsConnected()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -07001053 if (AcquireIPConfigWithLeaseName(
1054 affected_service->GetStorageIdentifier())) {
1055 LOG(INFO) << link_name() << " is up; started L3 configuration.";
mukesh agrawalc01f3982012-01-24 13:48:39 -08001056 affected_service->SetState(Service::kStateConfiguring);
1057 } else {
1058 LOG(ERROR) << "Unable to acquire DHCP config.";
1059 }
mukesh agrawal15908392011-11-16 18:29:25 +00001060 } else if (new_state == wpa_supplicant::kInterfaceStateAssociated) {
1061 affected_service->SetState(Service::kStateAssociating);
1062 } else if (new_state == wpa_supplicant::kInterfaceStateAuthenticating ||
1063 new_state == wpa_supplicant::kInterfaceStateAssociating ||
1064 new_state == wpa_supplicant::kInterfaceState4WayHandshake ||
1065 new_state == wpa_supplicant::kInterfaceStateGroupHandshake) {
1066 // Ignore transitions into these states from Completed, to avoid
1067 // bothering the user when roaming, or re-keying.
1068 if (old_state != wpa_supplicant::kInterfaceStateCompleted)
1069 affected_service->SetState(Service::kStateAssociating);
1070 // TOOD(quiche): On backwards transitions, we should probably set
1071 // a timeout for getting back into the completed state. At present,
1072 // we depend on wpa_supplicant eventually reporting that CurrentBSS
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001073 // has changed. But there may be cases where that signal is not sent.
mukesh agrawal15908392011-11-16 18:29:25 +00001074 // (crosbug.com/23207)
1075 } else {
1076 // Other transitions do not affect Service state.
1077 //
1078 // Note in particular that we ignore a State change into
1079 // kInterfaceStateDisconnected, in favor of observing the corresponding
1080 // change in CurrentBSS.
1081 }
mukesh agrawal7ec71312011-11-10 02:08:26 +00001082}
1083
1084// Used by Manager.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001085WiFiServiceRefPtr WiFi::GetService(const KeyValueStore &args, Error *error) {
Darin Petkovb65c2452012-02-23 15:17:06 +01001086 CHECK_EQ(args.GetString(flimflam::kTypeProperty), flimflam::kTypeWifi);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001087
1088 if (args.ContainsString(flimflam::kModeProperty) &&
1089 args.GetString(flimflam::kModeProperty) !=
1090 flimflam::kModeManaged) {
mukesh agrawal06175d72012-04-23 16:46:01 -07001091 Error::PopulateAndLog(error, Error::kNotSupported,
1092 kManagerErrorUnsupportedServiceMode);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001093 return NULL;
1094 }
1095
1096 if (!args.ContainsString(flimflam::kSSIDProperty)) {
mukesh agrawal06175d72012-04-23 16:46:01 -07001097 Error::PopulateAndLog(error, Error::kInvalidArguments,
1098 kManagerErrorSSIDRequired);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001099 return NULL;
1100 }
1101
1102 string ssid = args.GetString(flimflam::kSSIDProperty);
1103 if (ssid.length() < 1) {
mukesh agrawal06175d72012-04-23 16:46:01 -07001104 Error::PopulateAndLog(error, Error::kInvalidNetworkName,
1105 kManagerErrorSSIDTooShort);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001106 return NULL;
1107 }
1108
1109 if (ssid.length() > IEEE_80211::kMaxSSIDLen) {
mukesh agrawal06175d72012-04-23 16:46:01 -07001110 Error::PopulateAndLog(error, Error::kInvalidNetworkName,
1111 kManagerErrorSSIDTooLong);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001112 return NULL;
1113 }
1114
1115 string security_method;
1116 if (args.ContainsString(flimflam::kSecurityProperty)) {
1117 security_method = args.GetString(flimflam::kSecurityProperty);
1118 } else {
1119 security_method = flimflam::kSecurityNone;
1120 }
1121
1122 if (security_method != flimflam::kSecurityNone &&
1123 security_method != flimflam::kSecurityWep &&
1124 security_method != flimflam::kSecurityPsk &&
1125 security_method != flimflam::kSecurityWpa &&
1126 security_method != flimflam::kSecurityRsn &&
1127 security_method != flimflam::kSecurity8021x) {
mukesh agrawal06175d72012-04-23 16:46:01 -07001128 Error::PopulateAndLog(error, Error::kNotSupported,
1129 kManagerErrorUnsupportedSecurityMode);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001130 return NULL;
1131 }
1132
Paul Stewart6ab23a92011-11-09 17:17:47 -08001133 bool hidden_ssid;
Paul Stewartced6a0b2011-11-08 15:32:04 -08001134 if (args.ContainsBool(flimflam::kWifiHiddenSsid)) {
1135 hidden_ssid = args.GetBool(flimflam::kWifiHiddenSsid);
Paul Stewart6ab23a92011-11-09 17:17:47 -08001136 } else {
1137 // If the service is not found, and the caller hasn't specified otherwise,
1138 // we assume this is is a hidden network.
1139 hidden_ssid = true;
Paul Stewartced6a0b2011-11-08 15:32:04 -08001140 }
1141
Paul Stewart6ab23a92011-11-09 17:17:47 -08001142 vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
1143 WiFiServiceRefPtr service(FindService(ssid_bytes, flimflam::kModeManaged,
1144 security_method));
1145 if (!service.get()) {
mukesh agrawal1a056262011-10-05 14:36:54 -07001146 service = new WiFiService(control_interface(),
1147 dispatcher(),
Thieu Le3426c8f2012-01-11 17:35:11 -08001148 metrics(),
mukesh agrawal1a056262011-10-05 14:36:54 -07001149 manager(),
1150 this,
Paul Stewart6ab23a92011-11-09 17:17:47 -08001151 ssid_bytes,
mukesh agrawal1a056262011-10-05 14:36:54 -07001152 flimflam::kModeManaged,
Paul Stewartced6a0b2011-11-08 15:32:04 -08001153 security_method,
1154 hidden_ssid);
1155 services_.push_back(service);
mukesh agrawal261daca2011-12-02 18:56:56 +00001156 // NB: We do not register the newly created Service with the Manager.
1157 // The Service will be registered if/when we find Endpoints for it.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001158 }
1159
mukesh agrawalb172e4a2012-04-25 18:15:30 -07001160 if ((security_method == flimflam::kSecurityWep ||
1161 security_method == flimflam::kSecurityPsk ||
1162 security_method == flimflam::kSecurityWpa ||
1163 security_method == flimflam::kSecurityRsn) &&
1164 args.ContainsString(flimflam::kPassphraseProperty)) {
mukesh agrawal1a056262011-10-05 14:36:54 -07001165 service->SetPassphrase(args.GetString(flimflam::kPassphraseProperty),
1166 error);
1167 if (error->IsFailure()) {
1168 return NULL;
1169 }
1170 }
1171
Paul Stewartcb59fed2012-03-21 21:14:46 -07001172 service->Configure(args, error);
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001173
Paul Stewart7f61e522012-03-22 11:13:45 -07001174 // TODO(pstew): Schedule a task to forget up all non-hidden services that
1175 // have no endpoints like the one we may have just created. crosbug.com/28224
1176
mukesh agrawal7a4e4002011-09-06 11:26:05 -07001177 return service;
1178}
1179
mukesh agrawal16bc1b82012-02-09 18:38:26 -08001180// static
1181bool WiFi::SanitizeSSID(string *ssid) {
1182 CHECK(ssid);
1183
1184 size_t ssid_len = ssid->length();
1185 size_t i;
1186 bool changed = false;
1187
Gary Morainac1bdb42012-02-16 17:42:29 -08001188 for (i = 0; i < ssid_len; ++i) {
mukesh agrawal16bc1b82012-02-09 18:38:26 -08001189 if (!g_ascii_isprint((*ssid)[i])) {
1190 (*ssid)[i] = '?';
1191 changed = true;
1192 }
1193 }
1194
1195 return changed;
1196}
1197
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001198void WiFi::HelpRegisterDerivedInt32(
1199 PropertyStore *store,
1200 const string &name,
1201 int32(WiFi::*get)(Error *error),
1202 void(WiFi::*set)(const int32 &value, Error *error)) {
1203 store->RegisterDerivedInt32(
1204 name,
1205 Int32Accessor(new CustomAccessor<WiFi, int32>(this, get, set)));
1206}
1207
1208void WiFi::HelpRegisterDerivedString(
1209 PropertyStore *store,
1210 const string &name,
1211 string(WiFi::*get)(Error *error),
1212 void(WiFi::*set)(const string &value, Error *error)) {
1213 store->RegisterDerivedString(
1214 name,
1215 StringAccessor(new CustomAccessor<WiFi, string>(this, get, set)));
1216}
1217
1218void WiFi::HelpRegisterDerivedUint16(
1219 PropertyStore *store,
1220 const string &name,
1221 uint16(WiFi::*get)(Error *error),
1222 void(WiFi::*set)(const uint16 &value, Error *error)) {
1223 store->RegisterDerivedUint16(
1224 name,
1225 Uint16Accessor(new CustomAccessor<WiFi, uint16>(this, get, set)));
1226}
1227
Gary Morainac1bdb42012-02-16 17:42:29 -08001228void WiFi::HandlePowerStateChange(PowerManager::SuspendState new_state) {
mukesh agrawal5c05b292012-03-07 10:12:52 -08001229 LOG(INFO) << __func__;
1230 if (new_state == PowerManagerProxyDelegate::kOn) {
1231 // We want to flush the BSS cache, but we don't want to conflict
1232 // with a running scan or an active connection attempt. So record
1233 // the need to flush, and take care of flushing when the next scan
1234 // completes.
1235 //
1236 // Note that supplicant will automatically expire old cache
1237 // entries (after, e.g., a BSS is not found in two consecutive
1238 // scans). However, our explicit flush accelerates re-association
1239 // in cases where a BSS disappeared while we were asleep. (See,
1240 // e.g. WiFiRoaming.005SuspendRoam.)
1241 time_->GetTimeMonotonic(&resumed_at_);
1242 need_bss_flush_ = true;
1243
1244 if (!scan_pending_ && IsIdle()) {
1245 // Not scanning/connecting/connected, so let's get things rolling.
1246 Scan(NULL);
1247 }
Gary Morainac1bdb42012-02-16 17:42:29 -08001248 }
1249}
1250
mukesh agrawalb66c6462012-05-07 11:45:25 -07001251void WiFi::StartScanTimer() {
1252 if (scan_interval_seconds_ == 0) {
1253 StopScanTimer();
1254 return;
1255 }
1256 scan_timer_callback_.Reset(
1257 Bind(&WiFi::ScanTimerHandler, weak_ptr_factory_.GetWeakPtr()));
1258 dispatcher()->PostDelayedTask(
1259 scan_timer_callback_.callback(), scan_interval_seconds_ * 1000);
1260}
1261
1262void WiFi::StopScanTimer() {
1263 scan_timer_callback_.Cancel();
1264}
1265
1266void WiFi::ScanTimerHandler() {
1267 SLOG(WiFi, 2) << "WiFi Device " << link_name() << ": " << __func__;
1268 if (IsIdle() && !scan_pending_) {
1269 Scan(NULL);
1270 }
1271 StartScanTimer();
1272}
1273
Paul Stewartb50f0b92011-05-16 16:31:42 -07001274} // namespace shill