1 // Copyright 2013 The Chromium 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.
5 #include "components/wifi/wifi_service.h"
7 #include "base/json/json_reader.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "components/onc/onc_constants.h"
14 const char WiFiService::kErrorAssociateToNetwork
[] = "Error.AssociateToNetwork";
15 const char WiFiService::kErrorInvalidData
[] = "Error.InvalidData";
16 const char WiFiService::kErrorNotConfigured
[] ="Error.NotConfigured";
17 const char WiFiService::kErrorNotConnected
[] = "Error.NotConnected";
18 const char WiFiService::kErrorNotFound
[] = "Error.NotFound";
19 const char WiFiService::kErrorNotImplemented
[] = "Error.NotImplemented";
20 const char WiFiService::kErrorScanForNetworksWithName
[] =
21 "Error.ScanForNetworksWithName";
22 const char WiFiService::kErrorWiFiService
[] = "Error.WiFiService";
24 WiFiService::NetworkProperties::NetworkProperties()
25 : connection_state(onc::connection_state::kNotConnected
),
26 security(onc::wifi::kNone
),
29 frequency(WiFiService::kFrequencyUnknown
) {}
31 WiFiService::NetworkProperties::~NetworkProperties() {}
33 scoped_ptr
<base::DictionaryValue
> WiFiService::NetworkProperties::ToValue(
34 bool network_list
) const {
35 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
37 value
->SetString(onc::network_config::kGUID
, guid
);
38 value
->SetString(onc::network_config::kName
, name
);
39 value
->SetString(onc::network_config::kConnectionState
, connection_state
);
40 DCHECK(type
== onc::network_type::kWiFi
);
41 value
->SetString(onc::network_config::kType
, type
);
43 // For now, assume all WiFi services are connectable.
44 value
->SetBoolean(onc::network_config::kConnectable
, true);
46 scoped_ptr
<base::DictionaryValue
> wifi(new base::DictionaryValue());
47 wifi
->SetString(onc::wifi::kSecurity
, security
);
48 wifi
->SetInteger(onc::wifi::kSignalStrength
, signal_strength
);
50 // Network list expects subset of data.
52 if (frequency
!= WiFiService::kFrequencyUnknown
)
53 wifi
->SetInteger(onc::wifi::kFrequency
, frequency
);
54 scoped_ptr
<base::ListValue
> frequency_list(new base::ListValue());
55 for (FrequencySet::const_iterator it
= this->frequency_set
.begin();
56 it
!= this->frequency_set
.end();
58 frequency_list
->AppendInteger(*it
);
60 if (!frequency_list
->empty())
61 wifi
->Set(onc::wifi::kFrequencyList
, frequency_list
.release());
63 wifi
->SetString(onc::wifi::kBSSID
, bssid
);
64 wifi
->SetString(onc::wifi::kSSID
, ssid
);
66 value
->Set(onc::network_type::kWiFi
, wifi
.release());
68 if (!network_list
&& !json_extra
.empty()) {
69 base::Value
* value_extra
= base::JSONReader::Read(json_extra
);
71 base::DictionaryValue
* value_dictionary
;
72 if (value_extra
->GetAsDictionary(&value_dictionary
))
73 value
->MergeDictionary(value_dictionary
);
79 bool WiFiService::NetworkProperties::UpdateFromValue(
80 const base::DictionaryValue
& value
) {
81 const base::DictionaryValue
* wifi
= NULL
;
82 std::string network_type
;
83 // Get network type and make sure that it is WiFi (if specified).
84 if (value
.GetString(onc::network_config::kType
, &network_type
)) {
85 if (network_type
!= onc::network_type::kWiFi
)
89 if (value
.GetDictionary(onc::network_type::kWiFi
, &wifi
)) {
90 wifi
->GetString(onc::wifi::kSecurity
, &security
);
91 wifi
->GetString(onc::wifi::kSSID
, &ssid
);
92 wifi
->GetString(onc::wifi::kPassphrase
, &password
);
93 wifi
->GetBoolean(onc::wifi::kAutoConnect
, &auto_connect
);
99 std::string
WiFiService::NetworkProperties::MacAddressAsString(
100 const uint8 mac_as_int
[6]) {
101 // mac_as_int is big-endian. Write in byte chunks.
102 // Format is XX:XX:XX:XX:XX:XX.
103 static const char* const kMacFormatString
= "%02x:%02x:%02x:%02x:%02x:%02x";
104 return base::StringPrintf(kMacFormatString
,
113 bool WiFiService::NetworkProperties::OrderByType(const NetworkProperties
& l
,
114 const NetworkProperties
& r
) {
115 if (l
.connection_state
!= r
.connection_state
)
116 return l
.connection_state
< r
.connection_state
;
117 // This sorting order is needed only for browser_tests, which expect this
118 // network type sort order: ethernet < wifi < vpn < cellular.
119 if (l
.type
== r
.type
)
120 return l
.guid
< r
.guid
;
121 if (l
.type
== onc::network_type::kEthernet
)
123 if (r
.type
== onc::network_type::kEthernet
)
125 return l
.type
> r
.type
;