blob: b1522cc42078ec4f22e0316a01d12064f4ef5573 [file] [log] [blame]
mukesh agrawalb54601c2011-06-07 17:39:22 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wifi_service.h"
6
7#include <string>
8
9#include <base/logging.h>
Chris Masone34af2182011-08-22 11:59:36 -070010#include <base/stringprintf.h>
11#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080012#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070013#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <dbus/dbus.h>
mukesh agrawald835b202011-10-07 15:26:47 -070016#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
18#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Thieu Lef7709452011-11-15 01:13:19 +000023#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070024#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070025#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070026#include "shill/wifi_endpoint.h"
27#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070028
29using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070030using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070031
32namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070033
Paul Stewartd08f4432011-11-04 07:48:20 -070034const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
35
mukesh agrawalb54601c2011-06-07 17:39:22 -070036WiFiService::WiFiService(ControlInterface *control_interface,
37 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070038 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070039 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080040 const vector<uint8_t> &ssid,
41 const string &mode,
42 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080043 bool hidden_ssid)
Gaurav Shah435de2c2011-11-17 19:01:07 -080044 : Service(control_interface, dispatcher, manager, Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070045 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070046 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070047 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080048 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070049 task_factory_(this),
50 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070051 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070052 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070053 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Thieu Lef7709452011-11-15 01:13:19 +000054 HelpRegisterDerivedString(store,
55 flimflam::kPassphraseProperty,
56 NULL,
57 &WiFiService::SetPassphrase);
Paul Stewartac4ac002011-08-26 12:04:26 -070058 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
59 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
60 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061
Paul Stewartac4ac002011-08-26 12:04:26 -070062 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
63 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
64 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
65 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070066
mukesh agrawald835b202011-10-07 15:26:47 -070067 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
68 string ssid_string(
69 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
70 if (SanitizeSSID(&ssid_string)) {
71 // WifiHexSsid property should only be present if Name property
72 // has been munged.
73 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
74 }
75 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070076
mukesh agrawal6e277772011-09-29 15:04:23 -070077 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
78 // a service that is not 802.1x.
79 if (security_ == flimflam::kSecurity8021x) {
80 NOTIMPLEMENTED();
81 // XXX needs_passpharse_ = false ?
82 } else if (security_ == flimflam::kSecurityPsk) {
83 SetEAPKeyManagement("WPA-PSK");
84 need_passphrase_ = true;
85 } else if (security_ == flimflam::kSecurityRsn) {
86 SetEAPKeyManagement("WPA-PSK");
87 need_passphrase_ = true;
88 } else if (security_ == flimflam::kSecurityWpa) {
89 SetEAPKeyManagement("WPA-PSK");
90 need_passphrase_ = true;
91 } else if (security_ == flimflam::kSecurityWep) {
92 SetEAPKeyManagement("NONE");
93 need_passphrase_ = true;
94 } else if (security_ == flimflam::kSecurityNone) {
95 SetEAPKeyManagement("NONE");
96 need_passphrase_ = false;
97 } else {
98 LOG(ERROR) << "unsupported security method " << security_;
99 }
100
Paul Stewartd08f4432011-11-04 07:48:20 -0700101 // Until we know better (at Profile load time), use the generic name.
102 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000103 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700104}
105
106WiFiService::~WiFiService() {
107 LOG(INFO) << __func__;
108}
109
mukesh agrawal1830fa12011-09-26 14:31:40 -0700110void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700111 LOG(INFO) << __func__;
112
113 // NB(quiche) defer handling, since dbus-c++ does not permit us to
114 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700115 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700116 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700117}
118
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700119void WiFiService::Disconnect() {
120 // TODO(quiche) RemoveNetwork from supplicant
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700121}
122
Paul Stewart22aa71b2011-09-16 12:15:11 -0700123bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
124 return wifi_->TechnologyIs(type);
125}
126
Chris Masone6515aab2011-10-12 16:19:09 -0700127string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700128 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700129}
Chris Masone092df3e2011-08-22 09:41:39 -0700130const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700131 return mode_;
132}
133
Chris Masone092df3e2011-08-22 09:41:39 -0700134const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700135 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700136}
137
Paul Stewarta41e38d2011-11-11 07:47:29 -0800138const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700139 return ssid_;
140}
141
mukesh agrawal1a056262011-10-05 14:36:54 -0700142void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
143 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000144 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700145 } else if (security_ == flimflam::kSecurityPsk ||
146 security_ == flimflam::kSecurityWpa ||
147 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000148 ValidateWPAPassphrase(passphrase, error);
149 } else {
150 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700151 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000152
153 if (error->IsSuccess())
154 passphrase_ = passphrase;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000155
156 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700157}
158
Paul Stewartd08f4432011-11-04 07:48:20 -0700159bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
160 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
161 storage->ContainsGroup(GetSpecificStorageIdentifier());
162}
163
Paul Stewarta41e38d2011-11-11 07:47:29 -0800164bool WiFiService::IsVisible() const {
165 // TODO(quiche): Write a function that returns whether (or which)
166 // endpoints are associated with this service. crosbug.com/22948
167 const bool is_visible_in_scan = true;
168
169 // WiFi Services should be displayed only if they are in range (have
170 // endpoints that have shown up in a scan) or if the service is actively
171 // being connected.
172 return is_visible_in_scan || IsConnected() || IsConnecting();
173}
174
Paul Stewartd08f4432011-11-04 07:48:20 -0700175bool WiFiService::Load(StoreInterface *storage) {
176 // First find out which storage identifier is available in priority order
177 // of specific, generic.
178 string id = GetSpecificStorageIdentifier();
179 if (!storage->ContainsGroup(id)) {
180 id = GetGenericStorageIdentifier();
181 if (!storage->ContainsGroup(id)) {
182 LOG(WARNING) << "Service is not available in the persistent store: "
183 << id;
184 return false;
185 }
186 }
187
188 // Set our storage identifier to match the storage name in the Profile.
189 storage_identifier_ = id;
190
191 // Load properties common to all Services.
192 if (!Service::Load(storage)) {
193 return false;
194 }
195
196 // Load properties specific to WiFi services.
197 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000198
199 // TODO(quiche): Load Passphrase property, ensure that UpdateConnectable
200 // is called (maybe via SetPassphrase). (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700201 return true;
202}
203
204bool WiFiService::Save(StoreInterface *storage) {
205 // Save properties common to all Services.
206 if (!Service::Save(storage)) {
207 return false;
208 }
209
210 // Save properties specific to WiFi services.
211 const string id = GetStorageIdentifier();
212 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000213
214 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700215 return true;
216}
217
Paul Stewart6ab23a92011-11-09 17:17:47 -0800218bool WiFiService::IsSecurityMatch(const string &security) const {
219 return GetSecurityClass(security) == GetSecurityClass(security_);
220}
221
mukesh agrawal32399322011-09-01 10:53:43 -0700222// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000223void WiFiService::HelpRegisterDerivedString(
224 PropertyStore *store,
225 const std::string &name,
226 std::string(WiFiService::*get)(Error *),
227 void(WiFiService::*set)(const std::string&, Error *)) {
228 store->RegisterDerivedString(
229 name,
230 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
231}
232
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700233void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700234 std::map<string, DBus::Variant> params;
235 DBus::MessageIter writer;
236
237 params[wpa_supplicant::kNetworkPropertyMode].writer().
238 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
239
240 if (security_ == flimflam::kSecurity8021x) {
241 NOTIMPLEMENTED();
242 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800243 const string psk_proto = StringPrintf("%s %s",
244 wpa_supplicant::kSecurityModeWPA,
245 wpa_supplicant::kSecurityModeRSN);
246 params[wpa_supplicant::kPropertySecurityProtocol].writer().
247 append_string(psk_proto.c_str());
248 params[wpa_supplicant::kPropertyPreSharedKey].writer().
249 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700250 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700251 params[wpa_supplicant::kPropertySecurityProtocol].writer().
252 append_string(wpa_supplicant::kSecurityModeRSN);
253 params[wpa_supplicant::kPropertyPreSharedKey].writer().
254 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700255 } else if (security_ == flimflam::kSecurityWpa) {
256 params[wpa_supplicant::kPropertySecurityProtocol].writer().
257 append_string(wpa_supplicant::kSecurityModeWPA);
258 params[wpa_supplicant::kPropertyPreSharedKey].writer().
259 append_string(passphrase_.c_str());
260 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000261 params[wpa_supplicant::kPropertyAuthAlg].writer().
262 append_string(wpa_supplicant::kSecurityAuthAlg);
263 Error error;
264 int key_index;
265 std::vector<uint8> password_bytes;
266 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
267 writer = params[wpa_supplicant::kPropertyWEPKey +
268 base::IntToString(key_index)].writer();
269 writer << password_bytes;
270 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
271 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700272 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800273 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700274 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800275 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700276 }
277
278 params[wpa_supplicant::kPropertyKeyManagement].writer().
279 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800280
281 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700282 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
283 writer << ssid_;
284
285 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700286}
287
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800288string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700289 return wifi_->GetRpcIdentifier();
290}
291
mukesh agrawal29c13a12011-11-24 00:09:19 +0000292void WiFiService::UpdateConnectable() {
293 if (security_ == flimflam::kSecurityNone) {
294 DCHECK(passphrase_.empty());
295 set_connectable(true);
296 } else if (security_ == flimflam::kSecurityWep ||
297 security_ == flimflam::kSecurityWpa ||
298 security_ == flimflam::kSecurityPsk ||
299 security_ == flimflam::kSecurityRsn) {
300 set_connectable(!passphrase_.empty());
301 } else {
302 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
303 set_connectable(false);
304 }
305}
306
mukesh agrawal1a056262011-10-05 14:36:54 -0700307// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000308void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
309 Error *error) {
310 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700311}
312
313// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000314void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
315 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700316 unsigned int length = passphrase.length();
317 vector<uint8> passphrase_bytes;
318
319 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
320 if (length != IEEE_80211::kWPAHexLen &&
321 (length < IEEE_80211::kWPAAsciiMinLen ||
322 length > IEEE_80211::kWPAAsciiMaxLen)) {
323 error->Populate(Error::kInvalidPassphrase);
324 }
325 } else {
326 if (length < IEEE_80211::kWPAAsciiMinLen ||
327 length > IEEE_80211::kWPAAsciiMaxLen) {
328 error->Populate(Error::kInvalidPassphrase);
329 }
330 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000331}
mukesh agrawal1a056262011-10-05 14:36:54 -0700332
Thieu Lef4cbda92011-11-10 23:41:24 +0000333// static
334void WiFiService::ParseWEPPassphrase(const string &passphrase,
335 int *key_index,
336 std::vector<uint8> *password_bytes,
337 Error *error) {
338 unsigned int length = passphrase.length();
339 int key_index_local;
340 std::string password_text;
341 bool is_hex = false;
342
343 switch (length) {
344 case IEEE_80211::kWEP40AsciiLen:
345 case IEEE_80211::kWEP104AsciiLen:
346 key_index_local = 0;
347 password_text = passphrase;
348 break;
349 case IEEE_80211::kWEP40AsciiLen + 2:
350 case IEEE_80211::kWEP104AsciiLen + 2:
351 if (CheckWEPKeyIndex(passphrase, error)) {
352 base::StringToInt(passphrase.substr(0,1), &key_index_local);
353 password_text = passphrase.substr(2);
354 }
355 break;
356 case IEEE_80211::kWEP40HexLen:
357 case IEEE_80211::kWEP104HexLen:
358 if (CheckWEPIsHex(passphrase, error)) {
359 key_index_local = 0;
360 password_text = passphrase;
361 is_hex = true;
362 }
363 break;
364 case IEEE_80211::kWEP40HexLen + 2:
365 case IEEE_80211::kWEP104HexLen + 2:
366 if(CheckWEPKeyIndex(passphrase, error) &&
367 CheckWEPIsHex(passphrase.substr(2), error)) {
368 base::StringToInt(passphrase.substr(0,1), &key_index_local);
369 password_text = passphrase.substr(2);
370 is_hex = true;
371 } else if (CheckWEPPrefix(passphrase, error) &&
372 CheckWEPIsHex(passphrase.substr(2), error)) {
373 key_index_local = 0;
374 password_text = passphrase.substr(2);
375 is_hex = true;
376 }
377 break;
378 case IEEE_80211::kWEP40HexLen + 4:
379 case IEEE_80211::kWEP104HexLen + 4:
380 if (CheckWEPKeyIndex(passphrase, error) &&
381 CheckWEPPrefix(passphrase.substr(2), error) &&
382 CheckWEPIsHex(passphrase.substr(4), error)) {
383 base::StringToInt(passphrase.substr(0,1), &key_index_local);
384 password_text = passphrase.substr(4);
385 is_hex = true;
386 }
387 break;
388 default:
389 error->Populate(Error::kInvalidPassphrase);
390 break;
391 }
392
mukesh agrawal1a056262011-10-05 14:36:54 -0700393 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000394 if (key_index)
395 *key_index = key_index_local;
396 if (password_bytes) {
397 if (is_hex)
398 base::HexStringToBytes(password_text, password_bytes);
399 else
400 password_bytes->insert(password_bytes->end(),
401 password_text.begin(),
402 password_text.end());
403 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700404 }
405}
406
407// static
408bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
409 vector<uint8> passphrase_bytes;
410 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
411 return true;
412 } else {
413 error->Populate(Error::kInvalidPassphrase);
414 return false;
415 }
416}
417
418// static
419bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
420 if (StartsWithASCII(passphrase, "0:", false) ||
421 StartsWithASCII(passphrase, "1:", false) ||
422 StartsWithASCII(passphrase, "2:", false) ||
423 StartsWithASCII(passphrase, "3:", false)) {
424 return true;
425 } else {
426 error->Populate(Error::kInvalidPassphrase);
427 return false;
428 }
429}
430
431// static
432bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
433 if (StartsWithASCII(passphrase, "0x", false)) {
434 return true;
435 } else {
436 error->Populate(Error::kInvalidPassphrase);
437 return false;
438 }
439}
440
Paul Stewart6ab23a92011-11-09 17:17:47 -0800441// static
mukesh agrawald835b202011-10-07 15:26:47 -0700442bool WiFiService::SanitizeSSID(string *ssid) {
443 CHECK(ssid);
444
445 size_t ssid_len = ssid->length();
446 size_t i;
447 bool changed = false;
448
449 for (i=0; i < ssid_len; ++i) {
450 if (!g_ascii_isprint((*ssid)[i])) {
451 (*ssid)[i] = '?';
452 changed = true;
453 }
454 }
455
456 return changed;
457}
458
Paul Stewart6ab23a92011-11-09 17:17:47 -0800459// static
460string WiFiService::GetSecurityClass(const string &security) {
461 if (security == flimflam::kSecurityRsn ||
462 security == flimflam::kSecurityWpa) {
463 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700464 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800465 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700466 }
467}
468
Paul Stewarta41e38d2011-11-11 07:47:29 -0800469// static
470bool WiFiService::ParseStorageIdentifier(const string &storage_name,
471 string *address,
472 string *mode,
473 string *security) {
474 vector<string> wifi_parts;
475 base::SplitString(storage_name, '_', &wifi_parts);
476 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
477 return false;
478 }
479 *address = wifi_parts[1];
480 *mode = wifi_parts[3];
481 *security = wifi_parts[4];
482 return true;
483}
484
Paul Stewart6ab23a92011-11-09 17:17:47 -0800485string WiFiService::GetGenericStorageIdentifier() const {
486 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
487}
488
Paul Stewartd08f4432011-11-04 07:48:20 -0700489string WiFiService::GetSpecificStorageIdentifier() const {
490 return GetStorageIdentifierForSecurity(security_);
491}
492
493string WiFiService::GetStorageIdentifierForSecurity(
494 const string &security) const {
495 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
496 flimflam::kTypeWifi,
497 wifi_->address().c_str(),
498 hex_ssid_.c_str(),
499 mode_.c_str(),
500 security.c_str()));
501}
502
mukesh agrawalb54601c2011-06-07 17:39:22 -0700503} // namespace shill