Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: chromeos_network.cc

Issue 652187: Added ConnectToNetwork (Closed)
Patch Set: merge in new changes Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos_network.h ('k') | load.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos_network.h" // NOLINT 5 #include "chromeos_network.h" // NOLINT
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 #include <cstring> 9 #include <cstring>
10 10
11 #include "marshal.h" // NOLINT 11 #include "marshal.h" // NOLINT
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "chromeos/dbus/dbus.h" // NOLINT 13 #include "chromeos/dbus/dbus.h" // NOLINT
14 #include "chromeos/glib/object.h" // NOLINT 14 #include "chromeos/glib/object.h" // NOLINT
15 #include "chromeos/string.h" 15 #include "chromeos/string.h"
16 16
17 // TODO(rtc): Unittest this code as soon as the tree is refactored. 17 // TODO(rtc): Unittest this code as soon as the tree is refactored.
18 namespace chromeos { // NOLINT 18 namespace chromeos { // NOLINT
19 19
20 namespace { // NOLINT 20 namespace { // NOLINT
21 21
22 // Connman D-Bus service identifiers. 22 // Connman D-Bus service identifiers.
23 const char* kConnmanManagerInterface = "org.moblin.connman.Manager"; 23 static const char* kConnmanManagerInterface = "org.moblin.connman.Manager";
24 const char* kConnmanServiceInterface = "org.moblin.connman.Service"; 24 static const char* kConnmanServiceInterface = "org.moblin.connman.Service";
25 const char* kConnmanServiceName = "org.moblin.connman"; 25 static const char* kConnmanServiceName = "org.moblin.connman";
26 const char* kConnmanIPConfigInterface = "org.moblin.connman.IPConfig"; 26 static const char* kConnmanIPConfigInterface = "org.moblin.connman.IPConfig";
27 const char* kConnmanDeviceInterface = "org.moblin.connman.Device"; 27 static const char* kConnmanDeviceInterface = "org.moblin.connman.Device";
28 28
29 // Connman function names. 29 // Connman function names.
30 const char* kGetPropertiesFunction = "GetProperties"; 30 static const char* kGetPropertiesFunction = "GetProperties";
31 const char* kSetPropertyFunction = "SetProperty"; 31 static const char* kSetPropertyFunction = "SetProperty";
32 const char* kConnectServiceFunction = "ConnectService"; 32 static const char* kConnectFunction = "Connect";
33 const char* kEnableTechnologyFunction = "EnableTechnology"; 33 static const char* kEnableTechnologyFunction = "EnableTechnology";
34 const char* kDisableTechnologyFunction = "DisableTechnology"; 34 static const char* kDisableTechnologyFunction = "DisableTechnology";
35 const char* kAddIPConfigFunction = "AddIPConfig"; 35 static const char* kAddIPConfigFunction = "AddIPConfig";
36 const char* kRemoveConfigFunction = "Remove"; 36 static const char* kRemoveConfigFunction = "Remove";
37 37
38 // Connman property names. 38 // Connman property names.
39 const char* kEncryptionProperty = "Security"; 39 static const char* kSecurityProperty = "Security";
40 const char* kPassphraseRequiredProperty = "PassphraseRequired"; 40 static const char* kPassphraseProperty = "Passphrase";
41 const char* kServicesProperty = "Services"; 41 static const char* kPassphraseRequiredProperty = "PassphraseRequired";
42 const char* kEnabledTechnologiesProperty = "EnabledTechnologies"; 42 static const char* kServicesProperty = "Services";
43 const char* kOfflineModeProperty = "OfflineMode"; 43 static const char* kEnabledTechnologiesProperty = "EnabledTechnologies";
44 const char* kSignalStrengthProperty = "Strength"; 44 static const char* kOfflineModeProperty = "OfflineMode";
45 const char* kSsidProperty = "Name"; 45 static const char* kSignalStrengthProperty = "Strength";
46 const char* kStateProperty = "State"; 46 static const char* kNameProperty = "Name";
47 const char* kTypeProperty = "Type"; 47 static const char* kStateProperty = "State";
48 const char* kUnknownString = "UNKNOWN"; 48 static const char* kTypeProperty = "Type";
49 const char* kIPConfigsProperty = "IPConfigs"; 49 static const char* kUnknownString = "UNKNOWN";
50 const char* kDeviceProperty = "Device"; 50 static const char* kIPConfigsProperty = "IPConfigs";
51 static const char* kDeviceProperty = "Device";
52 static const char* kFavoriteProperty = "Favorite";
53 static const char* kAutoConnectProperty = "AutoConnect";
54 static const char* kModeProperty = "Mode";
55 static const char* kErrorProperty = "Error";
51 56
52 // Connman type options. 57 // Connman type options.
53 const char* kTypeEthernet = "ethernet"; 58 static const char* kTypeEthernet = "ethernet";
54 const char* kTypeWifi = "wifi"; 59 static const char* kTypeWifi = "wifi";
55 const char* kTypeWimax = "wimax"; 60 static const char* kTypeWimax = "wimax";
56 const char* kTypeBluetooth = "bluetooth"; 61 static const char* kTypeBluetooth = "bluetooth";
57 const char* kTypeCellular = "cellular"; 62 static const char* kTypeCellular = "cellular";
63
64 // Connman mode options.
65 static const char* kModeManaged = "managed";
66 static const char* kModeAdhoc = "adhoc";
67
68 // Connman security options.
69 static const char* kSecurityWpa = "wpa";
70 static const char* kSecurityWep = "wep";
71 static const char* kSecurityRsn = "rsn";
72 static const char* kSecurityNone = "none";
58 73
59 // Connman state options. 74 // Connman state options.
60 const char* kStateIdle = "idle"; 75 static const char* kStateIdle = "idle";
61 const char* kStateCarrier = "carrier"; 76 static const char* kStateCarrier = "carrier";
62 const char* kStateAssociation = "association"; 77 static const char* kStateAssociation = "association";
63 const char* kStateConfiguration = "configuration"; 78 static const char* kStateConfiguration = "configuration";
64 const char* kStateReady = "ready"; 79 static const char* kStateReady = "ready";
65 const char* kStateDisconnect = "disconnect"; 80 static const char* kStateDisconnect = "disconnect";
66 const char* kStateFailure = "failure"; 81 static const char* kStateFailure = "failure";
67 82
68 // Connman encryption options. 83 // Connman error options.
69 const char* kWpaEnabled = "wpa"; 84 static const char* kErrorOutOfRange = "out-of-range";
70 const char* kWepEnabled = "wep"; 85 static const char* kErrorPinMissing = "pin-missing";
71 const char* kRsnEnabled = "rsn"; 86 static const char* kErrorDhcpFailed = "dhcp-failed";
87 static const char* kErrorConnectFailed = "connect-failed";
72 88
73 // IPConfig property names. 89 // IPConfig property names.
74 const char* kMethodProperty = "Method"; 90 static const char* kMethodProperty = "Method";
75 const char* kAddressProperty = "Address"; 91 static const char* kAddressProperty = "Address";
76 const char* kMtuProperty = "Mtu"; 92 static const char* kMtuProperty = "Mtu";
77 const char* kPrefixlenProperty = "Prefixlen"; 93 static const char* kPrefixlenProperty = "Prefixlen";
78 const char* kBroadcastProperty = "Broadcast"; 94 static const char* kBroadcastProperty = "Broadcast";
79 const char* kPeerAddressProperty = "PeerAddress"; 95 static const char* kPeerAddressProperty = "PeerAddress";
80 const char* kGatewayProperty = "Gateway"; 96 static const char* kGatewayProperty = "Gateway";
81 const char* kDomainNameProperty = "DomainName"; 97 static const char* kDomainNameProperty = "DomainName";
82 const char* kNameServersProperty = "NameServers"; 98 static const char* kNameServersProperty = "NameServers";
83 99
84 // IPConfig type options. 100 // IPConfig type options.
85 const char* kTypeIPv4 = "ipv4"; 101 static const char* kTypeIPv4 = "ipv4";
86 const char* kTypeIPv6 = "ipv6"; 102 static const char* kTypeIPv6 = "ipv6";
87 const char* kTypeDHCP = "dhcp"; 103 static const char* kTypeDHCP = "dhcp";
88 const char* kTypeBOOTP = "bootp"; 104 static const char* kTypeBOOTP = "bootp";
89 const char* kTypeZeroConf = "zeroconf"; 105 static const char* kTypeZeroConf = "zeroconf";
90 const char* kTypeDHCP6 = "dhcp6"; 106 static const char* kTypeDHCP6 = "dhcp6";
91 const char* kTypePPP = "ppp"; 107 static const char* kTypePPP = "ppp";
92 108
93 // Invokes the given method on the proxy and stores the result 109 static ConnectionType ParseType(const std::string& type) {
94 // in the ScopedHashTable. The hash table will map strings to glib values.
95 bool GetProperties(const dbus::Proxy& proxy, glib::ScopedHashTable* result) {
96 glib::ScopedError error;
97 if (!::dbus_g_proxy_call(proxy.gproxy(),
98 kGetPropertiesFunction,
99 &Resetter(&error).lvalue(),
100 G_TYPE_INVALID,
101 ::dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
102 G_TYPE_VALUE),
103 &Resetter(result).lvalue(), G_TYPE_INVALID)) {
104 LOG(WARNING) << "GetProperties failed: "
105 << (error->message ? error->message : "Unknown Error.");
106 return false;
107 }
108 return true;
109 }
110
111 ConnectionType ParseType(const std::string& type) {
112 if (type == kTypeEthernet) 110 if (type == kTypeEthernet)
113 return TYPE_ETHERNET; 111 return TYPE_ETHERNET;
114 if (type == kTypeWifi) 112 if (type == kTypeWifi)
115 return TYPE_WIFI; 113 return TYPE_WIFI;
116 if (type == kTypeWimax) 114 if (type == kTypeWimax)
117 return TYPE_WIMAX; 115 return TYPE_WIMAX;
118 if (type == kTypeBluetooth) 116 if (type == kTypeBluetooth)
119 return TYPE_BLUETOOTH; 117 return TYPE_BLUETOOTH;
120 if (type == kTypeCellular) 118 if (type == kTypeCellular)
121 return TYPE_CELLULAR; 119 return TYPE_CELLULAR;
122 return TYPE_UNKNOWN; 120 return TYPE_UNKNOWN;
123 } 121 }
124 122
125 ConnectionState ParseState(const std::string& state) { 123 static const char* TypeToString(ConnectionType type) {
124 switch (type) {
125 case TYPE_UNKNOWN:
126 break;
127 case TYPE_ETHERNET:
128 return kTypeEthernet;
129 case TYPE_WIFI:
130 return kTypeWifi;
131 case TYPE_WIMAX:
132 return kTypeWimax;
133 case TYPE_BLUETOOTH:
134 return kTypeBluetooth;
135 case TYPE_CELLULAR:
136 return kTypeCellular;
137 }
138 return kUnknownString;
139 }
140
141 static ConnectionMode ParseMode(const std::string& mode) {
142 if (mode == kModeManaged)
143 return MODE_MANAGED;
144 if (mode == kModeAdhoc)
145 return MODE_ADHOC;
146 return MODE_UNKNOWN;
147 }
148 /*
149 static const char* ModeToString(ConnectionMode mode) {
150 switch (mode) {
151 case MODE_UNKNOWN:
152 break;
153 case MODE_MANAGED:
154 return kModeManaged;
155 case MODE_ADHOC:
156 return kModeAdhoc;
157 }
158 return kUnknownString;
159 }
160 */
161 static ConnectionSecurity ParseSecurity(const std::string& security) {
162 if (security == kSecurityRsn)
163 return SECURITY_RSN;
164 if (security == kSecurityWpa)
165 return SECURITY_WPA;
166 if (security == kSecurityWep)
167 return SECURITY_WEP;
168 if (security == kSecurityNone)
169 return SECURITY_NONE;
170 return SECURITY_UNKNOWN;
171 }
172 /*
173 static const char* SecurityToString(ConnectionSecurity security) {
174 switch (security) {
175 case SECURITY_UNKNOWN:
176 break;
177 case SECURITY_RSN:
178 return kSecurityRsn;
179 case SECURITY_WPA:
180 return kSecurityWpa;
181 case SECURITY_WEP:
182 return kSecurityWep;
183 case SECURITY_NONE:
184 return kSecurityNone;
185 }
186 return kUnknownString;
187 }
188 */
189 static ConnectionState ParseState(const std::string& state) {
126 if (state == kStateIdle) 190 if (state == kStateIdle)
127 return STATE_IDLE; 191 return STATE_IDLE;
128 if (state == kStateCarrier) 192 if (state == kStateCarrier)
129 return STATE_CARRIER; 193 return STATE_CARRIER;
130 if (state == kStateAssociation) 194 if (state == kStateAssociation)
131 return STATE_ASSOCIATION; 195 return STATE_ASSOCIATION;
132 if (state == kStateConfiguration) 196 if (state == kStateConfiguration)
133 return STATE_CONFIGURATION; 197 return STATE_CONFIGURATION;
134 if (state == kStateReady) 198 if (state == kStateReady)
135 return STATE_READY; 199 return STATE_READY;
136 if (state == kStateDisconnect) 200 if (state == kStateDisconnect)
137 return STATE_DISCONNECT; 201 return STATE_DISCONNECT;
138 if (state == kStateFailure) 202 if (state == kStateFailure)
139 return STATE_FAILURE; 203 return STATE_FAILURE;
140 return STATE_UNKNOWN; 204 return STATE_UNKNOWN;
141 } 205 }
142 206
143 EncryptionType ParseEncryptionType(const std::string& encryption) { 207 static ConnectionError ParseError(const std::string& error) {
144 if (encryption == kRsnEnabled) 208 if (error == kErrorOutOfRange)
145 return RSN; 209 return ERROR_OUT_OF_RANGE;
146 if (encryption == kWpaEnabled) 210 if (error == kErrorPinMissing)
147 return WPA; 211 return ERROR_PIN_MISSING;
148 if (encryption == kWepEnabled) 212 if (error == kErrorDhcpFailed)
149 return WEP; 213 return ERROR_DHCP_FAILED;
150 return NONE; 214 if (error == kErrorConnectFailed)
215 return ERROR_CONNECT_FAILED;
216 return ERROR_UNKNOWN;
217 }
218
219 // Invokes the given method on the proxy and stores the result
220 // in the ScopedHashTable. The hash table will map strings to glib values.
221 bool GetProperties(const dbus::Proxy& proxy, glib::ScopedHashTable* result) {
222 glib::ScopedError error;
223 if (!::dbus_g_proxy_call(proxy.gproxy(),
224 kGetPropertiesFunction,
225 &Resetter(&error).lvalue(),
226 G_TYPE_INVALID,
227 ::dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
228 G_TYPE_VALUE),
229 &Resetter(result).lvalue(), G_TYPE_INVALID)) {
230 LOG(WARNING) << "GetProperties failed: "
231 << (error->message ? error->message : "Unknown Error.");
232 return false;
233 }
234 return true;
151 } 235 }
152 236
153 // Populates an instance of a ServiceInfo with the properties 237 // Populates an instance of a ServiceInfo with the properties
154 // from a Connman service. 238 // from a Connman service.
155 void ParseServiceProperties(const glib::ScopedHashTable& properties, 239 void ParseServiceProperties(const glib::ScopedHashTable& properties,
156 ServiceInfo* info) { 240 ServiceInfo* info) {
241 // Name
157 const char* default_string = kUnknownString; 242 const char* default_string = kUnknownString;
243 properties.Retrieve(kNameProperty, &default_string);
244 info->name = NewStringCopy(default_string);
245
246 // Type
247 default_string = kUnknownString;
158 properties.Retrieve(kTypeProperty, &default_string); 248 properties.Retrieve(kTypeProperty, &default_string);
159 info->type = ParseType(default_string); 249 info->type = ParseType(default_string);
160 250
251 // Mode
161 default_string = kUnknownString; 252 default_string = kUnknownString;
162 properties.Retrieve(kSsidProperty, &default_string); 253 properties.Retrieve(kModeProperty, &default_string);
163 info->ssid = NewStringCopy(default_string); 254 info->mode = ParseMode(default_string);
164 255
256 // Security
257 default_string = kSecurityNone;
258 properties.Retrieve(kSecurityProperty, &default_string);
259 info->security = ParseSecurity(default_string);
260
261 // State
262 default_string = kUnknownString;
263 properties.Retrieve(kStateProperty, &default_string);
264 info->state = ParseState(default_string);
265
266 // Error
267 default_string = kUnknownString;
268 properties.Retrieve(kErrorProperty, &default_string);
269 info->error = ParseError(default_string);
270
271 // PassphraseRequired
272 bool default_bool = false;
273 properties.Retrieve(kPassphraseRequiredProperty, &default_bool);
274 info->passphrase_required = default_bool;
275
276 // Passphrase
277 default_string = "";
278 properties.Retrieve(kPassphraseProperty, &default_string);
279 info->passphrase = NewStringCopy(default_string);
280
281 // Strength
282 uint8 default_uint8 = 0;
283 properties.Retrieve(kSignalStrengthProperty, &default_uint8);
284 info->strength = default_uint8;
285
286 // Favorite
287 default_bool = false;
288 properties.Retrieve(kFavoriteProperty, &default_bool);
289 info->favorite = default_bool;
290
291 // AutoConnect
292 default_bool = false;
293 properties.Retrieve(kAutoConnectProperty, &default_bool);
294 info->auto_connect = default_bool;
295
296 // Device
165 glib::Value val; 297 glib::Value val;
166 if (properties.Retrieve(kDeviceProperty, &val)) { 298 if (properties.Retrieve(kDeviceProperty, &val)) {
167 const gchar* path = static_cast<const gchar*>(g_value_get_boxed (&val)); 299 const gchar* path = static_cast<const gchar*>(g_value_get_boxed (&val));
168 info->device_path = NewStringCopy(path); 300 info->device_path = NewStringCopy(path);
169 } else { 301 } else {
170 info->device_path = NULL; 302 info->device_path = NewStringCopy(kUnknownString);
171 } 303 }
172
173 default_string = kUnknownString;
174 properties.Retrieve(kStateProperty, &default_string);
175 info->state = ParseState(default_string);
176
177 default_string = kUnknownString;
178 properties.Retrieve(kEncryptionProperty, &default_string);
179 info->encryption = ParseEncryptionType(default_string);
180
181 bool default_bool = false;
182 properties.Retrieve(kPassphraseRequiredProperty, &default_bool);
183 info->needs_passphrase = default_bool;
184
185 uint8 default_uint8 = 0;
186 properties.Retrieve(kSignalStrengthProperty, &default_uint8);
187 info->signal_strength = default_uint8;
188 } 304 }
189 305
190 // Returns a ServiceInfo object populated with data from a 306 // Returns a ServiceInfo object populated with data from a
191 // given DBus object path. 307 // given DBus object path.
192 // 308 //
193 // returns true on success. 309 // returns true on success.
194 bool ParseServiceInfo(const char* path, ServiceInfo *info) { 310 bool ParseServiceInfo(const char* path, ServiceInfo *info) {
195 dbus::Proxy service_proxy(dbus::GetSystemBusConnection(), 311 dbus::Proxy service_proxy(dbus::GetSystemBusConnection(),
196 kConnmanServiceName, 312 kConnmanServiceName,
197 path, 313 path,
198 kConnmanServiceInterface); 314 kConnmanServiceInterface);
199 glib::ScopedHashTable service_properties; 315 glib::ScopedHashTable service_properties;
200 if (!GetProperties(service_proxy, &service_properties)) 316 if (!GetProperties(service_proxy, &service_properties))
201 return false; 317 return false;
318 info->service_path = NewStringCopy(path);
202 ParseServiceProperties(service_properties, info); 319 ParseServiceProperties(service_properties, info);
203 return true; 320 return true;
204 } 321 }
205 322
206 // Creates a new ServiceStatus instance populated with the contents from 323 // Creates a new ServiceStatus instance populated with the contents from
207 // a vector of ServiceInfo. 324 // a vector of ServiceInfo.
208 ServiceStatus* CopyFromVector(const std::vector<ServiceInfo>& services) { 325 ServiceStatus* CopyFromVector(const std::vector<ServiceInfo>& services) {
209 ServiceStatus* result = new ServiceStatus(); 326 ServiceStatus* result = new ServiceStatus();
210 if (services.size() == 0) { 327 if (services.size() == 0) {
211 result->services = NULL; 328 result->services = NULL;
212 } else { 329 } else {
213 result->services = new ServiceInfo[services.size()]; 330 result->services = new ServiceInfo[services.size()];
214 } 331 }
215 result->size = services.size(); 332 result->size = services.size();
216 std::copy(services.begin(), services.end(), result->services); 333 std::copy(services.begin(), services.end(), result->services);
217 return result; 334 return result;
218 } 335 }
219 336
220 // Deletes all of the heap allocated members of a given ServiceInfo instance. 337 // Deletes all of the heap allocated members of a given ServiceInfo instance.
221 void DeleteServiceInfoProperties(ServiceInfo info) { 338 void DeleteServiceInfoProperties(ServiceInfo info) {
222 delete info.ssid; 339 if (info.service_path)
223 if (info.device_path) { 340 delete info.service_path;
341 if (info.name)
342 delete info.name;
343 if (info.passphrase)
344 delete info.passphrase;
345 if (info.device_path)
224 delete info.device_path; 346 delete info.device_path;
225 } 347
348 info.service_path = NULL;
349 info.name = NULL;
350 info.passphrase = NULL;
351 info.device_path = NULL;
226 } 352 }
227 353
228 ServiceStatus* GetServiceStatus(const GPtrArray* array) { 354 ServiceStatus* GetServiceStatus(const GPtrArray* array) {
229 std::vector<ServiceInfo> buffer; 355 std::vector<ServiceInfo> buffer;
230 // TODO(rtc/seanparent): Think about using std::transform instead of this 356 // TODO(rtc/seanparent): Think about using std::transform instead of this
231 // loop. For now I think the loop will be generally more readable than 357 // loop. For now I think the loop will be generally more readable than
232 // std::transform(). 358 // std::transform().
233 const char* path = NULL; 359 const char* path = NULL;
234 for (size_t i = 0; i < array->len; i++) { 360 for (size_t i = 0; i < array->len; i++) {
235 path = static_cast<const char*>(g_ptr_array_index(array, i)); 361 path = static_cast<const char*>(g_ptr_array_index(array, i));
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 DBUS_TYPE_G_PROXY, 618 DBUS_TYPE_G_PROXY,
493 &obj, 619 &obj,
494 G_TYPE_INVALID)) { 620 G_TYPE_INVALID)) {
495 LOG(WARNING) <<"Add IPConfig failed: " 621 LOG(WARNING) <<"Add IPConfig failed: "
496 << (error->message ? error->message : "Unknown Error."); 622 << (error->message ? error->message : "Unknown Error.");
497 return false; 623 return false;
498 } 624 }
499 return true; 625 return true;
500 } 626 }
501 627
502 // DEPRECATED: need to remove.
503 extern "C"
504 bool ChromeOSSetIPConfigProperty(IPConfig* config,
505 const char* key,
506 const char* value) {
507 return true;
508 }
509
510 // DEPRECATED: need to remove.
511 extern "C"
512 bool ChromeOSGetIPConfigProperty(IPConfig* config,
513 const char* key,
514 char* val,
515 size_t valsz) {
516 return true;
517 }
518
519 extern "C" 628 extern "C"
520 bool ChromeOSSaveIPConfig(IPConfig* config) { 629 bool ChromeOSSaveIPConfig(IPConfig* config) {
521 dbus::BusConnection bus = dbus::GetSystemBusConnection(); 630 dbus::BusConnection bus = dbus::GetSystemBusConnection();
522 dbus::Proxy device_proxy(bus, 631 dbus::Proxy device_proxy(bus,
523 kConnmanServiceName, 632 kConnmanServiceName,
524 config->path, 633 config->path,
525 kConnmanIPConfigInterface); 634 kConnmanIPConfigInterface);
526 /* 635 /*
527 TODO(chocobo): Save all the values 636 TODO(chocobo): Save all the values
528 637
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 return result; 723 return result;
615 } 724 }
616 725
617 extern "C" 726 extern "C"
618 void ChromeOSDisconnectNetworkStatus(NetworkStatusConnection connection) { 727 void ChromeOSDisconnectNetworkStatus(NetworkStatusConnection connection) {
619 dbus::Disconnect(connection->connection()); 728 dbus::Disconnect(connection->connection());
620 delete connection; 729 delete connection;
621 } 730 }
622 731
623 extern "C" 732 extern "C"
624 bool ChromeOSConnectToWifiNetwork(const char* ssid, 733 bool ChromeOSConnectToNetwork(const char* service_path,
625 const char* passphrase, 734 const char* passphrase) {
626 const char* encryption) { 735 dbus::Proxy service_proxy(dbus::GetSystemBusConnection(),
627 if (ssid == NULL) 736 kConnmanServiceName,
628 return false; 737 service_path,
738 kConnmanServiceInterface);
629 739
630 dbus::BusConnection bus = dbus::GetSystemBusConnection(); 740 // Set passphrase if non-null.
631 dbus::Proxy manager_proxy(bus, 741 if (passphrase) {
632 kConnmanServiceName, 742 glib::Value value_passphrase(passphrase);
633 "/", 743 glib::ScopedError error;
634 kConnmanManagerInterface); 744 if (!::dbus_g_proxy_call(service_proxy.gproxy(),
635 745 kSetPropertyFunction,
636 glib::ScopedHashTable scoped_properties = 746 &Resetter(&error).lvalue(),
637 glib::ScopedHashTable( 747 G_TYPE_STRING,
638 ::g_hash_table_new_full(::g_str_hash, 748 kPassphraseProperty,
639 ::g_str_equal, 749 G_TYPE_VALUE,
640 ::g_free, 750 &value_passphrase,
641 NULL)); 751 G_TYPE_INVALID,
642 752 G_TYPE_INVALID)) {
643 glib::Value value_mode("managed"); 753 LOG(WARNING) << "ConnectToNetwork failed on set passphrase: "
644 glib::Value value_type("wifi"); 754 << (error->message ? error->message : "Unknown Error.");
645 glib::Value value_ssid(ssid); 755 return false;
646 glib::Value value_security(encryption == NULL ? "rsn" : encryption); 756 }
647 glib::Value value_passphrase(passphrase == NULL ? "" : passphrase);
648
649 ::GHashTable* properties = scoped_properties.get();
650 ::g_hash_table_insert(properties, ::g_strdup("Mode"), &value_mode);
651 ::g_hash_table_insert(properties, ::g_strdup("Type"), &value_type);
652 ::g_hash_table_insert(properties, ::g_strdup("SSID"), &value_ssid);
653 ::g_hash_table_insert(properties, ::g_strdup("Security"), &value_security);
654
655 // Connman will overwrite any passphrase that it remembers with the value
656 // sent via d-bus. So Passphrase needs to be omitted when reconnecting.
657 if (passphrase != NULL) {
658 ::g_hash_table_insert(properties, ::g_strdup("Passphrase"),
659 &value_passphrase);
660 } 757 }
661 758
759 // Now try connecting.
662 glib::ScopedError error; 760 glib::ScopedError error;
663 ::DBusGProxy obj; 761 if (!::dbus_g_proxy_call(service_proxy.gproxy(),
664 if (!::dbus_g_proxy_call(manager_proxy.gproxy(), 762 kConnectFunction,
665 kConnectServiceFunction,
666 &Resetter(&error).lvalue(), 763 &Resetter(&error).lvalue(),
667 ::dbus_g_type_get_map("GHashTable",
668 G_TYPE_STRING,
669 G_TYPE_VALUE),
670 properties,
671 G_TYPE_INVALID, 764 G_TYPE_INVALID,
672 DBUS_TYPE_G_PROXY,
673 &obj,
674 G_TYPE_INVALID)) { 765 G_TYPE_INVALID)) {
675 LOG(WARNING) << "ConnectService failed: " 766 LOG(WARNING) << "ConnectToNetwork failed: "
676 << (error->message ? error->message : "Unknown Error."); 767 << (error->message ? error->message : "Unknown Error.");
677 return false; 768 return false;
678 } 769 }
679 return true; 770 return true;
680 } 771 }
681 772
682 extern "C" 773 extern "C"
683 ServiceStatus* ChromeOSGetAvailableNetworks() { 774 ServiceStatus* ChromeOSGetAvailableNetworks() {
684 dbus::BusConnection bus = dbus::GetSystemBusConnection(); 775 dbus::BusConnection bus = dbus::GetSystemBusConnection();
685 dbus::Proxy manager_proxy(bus, 776 dbus::Proxy manager_proxy(bus,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 return -1; 816 return -1;
726 817
727 GHashTable* table = properties.get(); 818 GHashTable* table = properties.get();
728 gpointer ptr = g_hash_table_lookup(table, kEnabledTechnologiesProperty); 819 gpointer ptr = g_hash_table_lookup(table, kEnabledTechnologiesProperty);
729 if (ptr == NULL) 820 if (ptr == NULL)
730 return devices; 821 return devices;
731 822
732 gchar** value = 823 gchar** value =
733 static_cast<gchar**>(g_value_get_boxed(static_cast<GValue*>(ptr))); 824 static_cast<gchar**>(g_value_get_boxed(static_cast<GValue*>(ptr)));
734 while (*value) { 825 while (*value) {
735 devices |= ParseType(*value); 826 // Bitwise OR with a bit left-shifted by the enum ConnectionType value.
827 devices |= 1 << ParseType(*value);
736 value++; 828 value++;
737 } 829 }
738 return devices; 830 return devices;
739 } 831 }
740 832
741 extern "C" 833 extern "C"
742 bool ChromeOSEnableNetworkDevice(ConnectionType type, bool enable) { 834 bool ChromeOSEnableNetworkDevice(ConnectionType type, bool enable) {
743 dbus::BusConnection bus = dbus::GetSystemBusConnection(); 835 dbus::BusConnection bus = dbus::GetSystemBusConnection();
744 dbus::Proxy manager_proxy(bus, 836 dbus::Proxy manager_proxy(bus,
745 kConnmanServiceName, 837 kConnmanServiceName,
746 "/", 838 "/",
747 kConnmanManagerInterface); 839 kConnmanManagerInterface);
748 840 if (type == TYPE_UNKNOWN) {
749 gchar* device; 841 LOG(WARNING) << "EnableNetworkDevice called with an unknown type: " << type;
750 switch (type) { 842 return false;
751 case TYPE_ETHERNET:
752 device = ::g_strdup(kTypeEthernet);
753 break;
754 case TYPE_WIFI:
755 device = ::g_strdup(kTypeWifi);
756 break;
757 case TYPE_WIMAX:
758 device = ::g_strdup(kTypeWimax);
759 break;
760 case TYPE_BLUETOOTH:
761 device = ::g_strdup(kTypeBluetooth);
762 break;
763 case TYPE_CELLULAR:
764 device = ::g_strdup(kTypeCellular);
765 break;
766 case TYPE_UNKNOWN:
767 default:
768 LOG(WARNING) << "EnableNetworkDevice called with an unknown type: "
769 << type;
770 return false;
771 } 843 }
772 844
845 gchar* device = ::g_strdup(TypeToString(type));
773 glib::ScopedError error; 846 glib::ScopedError error;
774 if (!::dbus_g_proxy_call(manager_proxy.gproxy(), 847 if (!::dbus_g_proxy_call(manager_proxy.gproxy(),
775 enable ? kEnableTechnologyFunction : 848 enable ? kEnableTechnologyFunction :
776 kDisableTechnologyFunction, 849 kDisableTechnologyFunction,
777 &Resetter(&error).lvalue(), 850 &Resetter(&error).lvalue(),
778 G_TYPE_STRING, 851 G_TYPE_STRING,
779 device, 852 device,
780 G_TYPE_INVALID, 853 G_TYPE_INVALID,
781 G_TYPE_INVALID)) { 854 G_TYPE_INVALID)) {
782 LOG(WARNING) << "EnableNetworkDevice failed: " 855 LOG(WARNING) << "EnableNetworkDevice failed: "
(...skipping 28 matching lines...) Expand all
811 G_TYPE_INVALID)) { 884 G_TYPE_INVALID)) {
812 LOG(WARNING) << "SetOfflineMode failed: " 885 LOG(WARNING) << "SetOfflineMode failed: "
813 << (error->message ? error->message : "Unknown Error."); 886 << (error->message ? error->message : "Unknown Error.");
814 return false; 887 return false;
815 } 888 }
816 889
817 return true; 890 return true;
818 } 891 }
819 892
820 } // namespace chromeos 893 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos_network.h ('k') | load.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698