5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static unsigned int hidden_counter = 0;
35 struct connman_network {
36 struct connman_element element;
37 enum connman_network_type type;
38 enum connman_network_protocol protocol;
39 connman_bool_t available;
40 connman_bool_t connected;
41 connman_bool_t roaming;
42 connman_bool_t hidden;
43 connman_uint8_t strength;
44 connman_uint16_t frequency;
50 struct connman_ipconfig *ipconfig;
52 struct connman_network_driver *driver;
55 connman_bool_t registered;
56 connman_bool_t connecting;
57 connman_bool_t associating;
59 struct connman_device *device;
65 unsigned short channel;
71 char *client_cert_path;
72 char *private_key_path;
73 char *private_key_passphrase;
78 static const char *type2string(enum connman_network_type type)
81 case CONNMAN_NETWORK_TYPE_UNKNOWN:
82 case CONNMAN_NETWORK_TYPE_VENDOR:
84 case CONNMAN_NETWORK_TYPE_ETHERNET:
86 case CONNMAN_NETWORK_TYPE_WIFI:
88 case CONNMAN_NETWORK_TYPE_WIMAX:
90 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
91 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
93 case CONNMAN_NETWORK_TYPE_CELLULAR:
94 case CONNMAN_NETWORK_TYPE_MBM:
95 case CONNMAN_NETWORK_TYPE_HSO:
102 static DBusMessage *get_properties(DBusConnection *conn,
103 DBusMessage *msg, void *data)
105 struct connman_network *network = data;
107 DBusMessageIter array, dict;
109 DBG("conn %p", conn);
111 if (__connman_security_check_privilege(msg,
112 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
113 return __connman_error_permission_denied(msg);
115 reply = dbus_message_new_method_return(msg);
119 dbus_message_iter_init_append(reply, &array);
121 connman_dbus_dict_open(&array, &dict);
123 if (network->device) {
124 const char *path = connman_device_get_path(network->device);
126 connman_dbus_dict_append_basic(&dict, "Device",
127 DBUS_TYPE_OBJECT_PATH, &path);
130 if (network->address != NULL)
131 connman_dbus_dict_append_basic(&dict, "Address",
132 DBUS_TYPE_STRING, &network->address);
134 if (network->name != NULL)
135 connman_dbus_dict_append_basic(&dict, "Name",
136 DBUS_TYPE_STRING, &network->name);
138 connman_dbus_dict_append_basic(&dict, "Connected",
139 DBUS_TYPE_BOOLEAN, &network->connected);
141 if (network->strength > 0)
142 connman_dbus_dict_append_basic(&dict, "Strength",
143 DBUS_TYPE_BYTE, &network->strength);
145 if (network->frequency > 0)
146 connman_dbus_dict_append_basic(&dict, "Frequency",
147 DBUS_TYPE_UINT16, &network->frequency);
149 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
150 connman_dbus_dict_append_fixed_array(&dict, "WiFi.SSID",
151 DBUS_TYPE_BYTE, &network->wifi.ssid,
152 network->wifi.ssid_len);
154 if (network->wifi.mode != NULL)
155 connman_dbus_dict_append_basic(&dict, "WiFi.Mode",
156 DBUS_TYPE_STRING, &network->wifi.mode);
158 if (network->wifi.channel > 0)
159 connman_dbus_dict_append_basic(&dict, "WiFi.Channel",
160 DBUS_TYPE_UINT16, &network->wifi.channel);
162 if (network->wifi.security != NULL) {
163 connman_dbus_dict_append_basic(&dict, "WiFi.Security",
164 DBUS_TYPE_STRING, &network->wifi.security);
166 if (g_strcmp0(network->wifi.security, "ieee8021x") == 0 &&
167 network->wifi.eap != NULL)
168 connman_dbus_dict_append_basic(&dict, "WiFi.EAP",
169 DBUS_TYPE_STRING, &network->wifi.eap);
173 if (network->wifi.passphrase != NULL &&
174 __connman_security_check_privilege(msg,
175 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
176 connman_dbus_dict_append_basic(&dict, "WiFi.Passphrase",
177 DBUS_TYPE_STRING, &network->wifi.passphrase);
179 connman_dbus_dict_close(&array, &dict);
184 static GDBusMethodTable network_methods[] = {
185 { "GetProperties", "", "a{sv}", get_properties },
189 static GDBusSignalTable network_signals[] = {
190 { "PropertyChanged", "sv" },
194 static DBusConnection *connection;
196 static void append_networks(DBusMessageIter *iter, void *user_data)
198 struct connman_device *device = user_data;
200 __connman_element_list((struct connman_element *) device,
201 CONNMAN_ELEMENT_TYPE_NETWORK, iter);
204 static void emit_networks_signal(struct connman_device *device)
206 const char *path = connman_device_get_path(device);
208 connman_dbus_property_changed_array(path,
209 CONNMAN_DEVICE_INTERFACE, "Networks",
210 DBUS_TYPE_OBJECT_PATH, append_networks, device);
213 static int register_interface(struct connman_element *element)
215 struct connman_network *network = element->network;
217 DBG("element %p name %s", element, element->name);
219 if (g_dbus_register_interface(connection, element->path,
220 CONNMAN_NETWORK_INTERFACE,
221 network_methods, network_signals,
222 NULL, network, NULL) == FALSE) {
223 connman_error("Failed to register %s network", element->path);
227 network->registered = TRUE;
229 emit_networks_signal(network->device);
234 static void unregister_interface(struct connman_element *element)
236 struct connman_network * network = element->network;
238 DBG("element %p name %s", element, element->name);
240 network->registered = FALSE;
242 emit_networks_signal(network->device);
244 g_dbus_unregister_interface(connection, element->path,
245 CONNMAN_NETWORK_INTERFACE);
248 connman_bool_t __connman_network_has_driver(struct connman_network *network)
250 if (network == NULL || network->driver == NULL)
253 return network->registered;
256 static GSList *driver_list = NULL;
258 static gint compare_priority(gconstpointer a, gconstpointer b)
260 const struct connman_network_driver *driver1 = a;
261 const struct connman_network_driver *driver2 = b;
263 return driver2->priority - driver1->priority;
267 * connman_network_driver_register:
268 * @driver: network driver definition
270 * Register a new network driver
272 * Returns: %0 on success
274 int connman_network_driver_register(struct connman_network_driver *driver)
276 DBG("driver %p name %s", driver, driver->name);
278 driver_list = g_slist_insert_sorted(driver_list, driver,
285 * connman_network_driver_unregister:
286 * @driver: network driver definition
288 * Remove a previously registered network driver
290 void connman_network_driver_unregister(struct connman_network_driver *driver)
292 DBG("driver %p name %s", driver, driver->name);
294 driver_list = g_slist_remove(driver_list, driver);
297 static void network_destruct(struct connman_element *element)
299 struct connman_network *network = element->network;
301 DBG("element %p name %s", element, element->name);
303 g_free(network->wifi.ssid);
304 g_free(network->wifi.mode);
305 g_free(network->wifi.security);
306 g_free(network->wifi.passphrase);
308 g_free(network->group);
309 g_free(network->node);
310 g_free(network->name);
311 g_free(network->address);
312 g_free(network->identifier);
314 if (network->ipconfig) {
315 connman_ipconfig_unref(network->ipconfig);
316 network->ipconfig = NULL;
321 * connman_network_create:
322 * @identifier: network identifier (for example an unqiue name)
324 * Allocate a new network and assign the #identifier to it.
326 * Returns: a newly-allocated #connman_network structure
328 struct connman_network *connman_network_create(const char *identifier,
329 enum connman_network_type type)
331 struct connman_network *network;
332 connman_uint8_t strength = 0;
336 DBG("identifier %s type %d", identifier, type);
338 network = g_try_new0(struct connman_network, 1);
342 DBG("network %p", network);
344 __connman_element_initialize(&network->element);
346 //temp = connman_dbus_encode_string(identifier);
347 if (identifier == NULL) {
348 temp = g_strdup_printf("hidden_%d", hidden_counter++);
349 network->hidden = TRUE;
351 temp = g_strdup(identifier);
358 network->element.name = temp;
359 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
361 network->element.network = network;
362 network->element.destruct = network_destruct;
364 str = type2string(type);
366 connman_element_set_string(&network->element, "Type", str);
368 connman_element_set_uint8(&network->element, "Strength", strength);
370 network->type = type;
371 network->identifier = g_strdup(temp);
377 * connman_network_ref:
378 * @network: network structure
380 * Increase reference counter of network
382 struct connman_network *connman_network_ref(struct connman_network *network)
384 if (connman_element_ref(&network->element) == NULL)
391 * connman_network_unref:
392 * @network: network structure
394 * Decrease reference counter of network
396 void connman_network_unref(struct connman_network *network)
398 connman_element_unref(&network->element);
401 const char *__connman_network_get_type(struct connman_network *network)
403 return type2string(network->type);
407 * connman_network_get_type:
408 * @network: network structure
410 * Get type of network
412 enum connman_network_type connman_network_get_type(struct connman_network *network)
414 return network->type;
418 * connman_network_get_identifier:
419 * @network: network structure
421 * Get identifier of network
423 const char *connman_network_get_identifier(struct connman_network *network)
425 return network->identifier;
429 * connman_network_get_path:
430 * @network: network structure
432 * Get path name of network
434 const char *connman_network_get_path(struct connman_network *network)
436 return network->element.path;
440 * connman_network_set_index:
441 * @network: network structure
442 * @index: index number
444 * Set index number of network
446 void connman_network_set_index(struct connman_network *network, int index)
448 if (network->element.index < 0) {
449 struct connman_service *service;
452 * This is needed for plugins that havent set their ipconfig
453 * layer yet, due to not being able to get a network index
454 * prior to creating a service.
456 service = __connman_service_lookup_from_network(network);
458 __connman_service_create_ipconfig(service, index);
461 network->element.index = index;
465 * connman_network_get_index:
466 * @network: network structure
468 * Get index number of network
470 int connman_network_get_index(struct connman_network *network)
472 return network->element.index;
476 * connman_network_get_element:
477 * @network: network structure
479 * Get connman_element of network
481 struct connman_element *connman_network_get_element(
482 struct connman_network *network)
484 return &network->element;
488 * connman_network_set_protocol:
489 * @network: network structure
490 * @protocol: network protocol
492 * Change protocol of network
494 void connman_network_set_protocol(struct connman_network *network,
495 enum connman_network_protocol protocol)
497 network->protocol = protocol;
501 * connman_network_set_group:
502 * @network: network structure
505 * Set group name for automatic clustering
507 void connman_network_set_group(struct connman_network *network,
510 switch (network->type) {
511 case CONNMAN_NETWORK_TYPE_UNKNOWN:
512 case CONNMAN_NETWORK_TYPE_VENDOR:
514 case CONNMAN_NETWORK_TYPE_ETHERNET:
515 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
516 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
517 case CONNMAN_NETWORK_TYPE_CELLULAR:
518 case CONNMAN_NETWORK_TYPE_MBM:
519 case CONNMAN_NETWORK_TYPE_HSO:
520 case CONNMAN_NETWORK_TYPE_WIFI:
521 case CONNMAN_NETWORK_TYPE_WIMAX:
525 if (g_strcmp0(network->group, group) == 0) {
527 __connman_profile_update_network(network);
531 if (network->group != NULL) {
532 __connman_profile_remove_network(network);
534 g_free(network->group);
537 network->group = g_strdup(group);
539 if (network->group != NULL)
540 __connman_profile_add_network(network);
544 * connman_network_get_group:
545 * @network: network structure
547 * Get group name for automatic clustering
549 const char *connman_network_get_group(struct connman_network *network)
551 return network->group;
554 const char *__connman_network_get_ident(struct connman_network *network)
556 if (network->device == NULL)
559 return __connman_device_get_ident(network->device);
562 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
564 switch (network->type) {
565 case CONNMAN_NETWORK_TYPE_UNKNOWN:
566 case CONNMAN_NETWORK_TYPE_VENDOR:
567 case CONNMAN_NETWORK_TYPE_ETHERNET:
568 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
569 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
570 case CONNMAN_NETWORK_TYPE_CELLULAR:
571 case CONNMAN_NETWORK_TYPE_MBM:
572 case CONNMAN_NETWORK_TYPE_HSO:
573 case CONNMAN_NETWORK_TYPE_WIMAX:
575 case CONNMAN_NETWORK_TYPE_WIFI:
576 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
578 if (network->strength > 0 && network->strength < 20)
586 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
588 return network->connecting;
592 * connman_network_set_available:
593 * @network: network structure
594 * @available: availability state
596 * Change availability state of network (in range)
598 int connman_network_set_available(struct connman_network *network,
599 connman_bool_t available)
601 DBG("network %p available %d", network, available);
603 if (network->available == available)
606 network->available = available;
612 * connman_network_get_available:
613 * @network: network structure
615 * Get network available setting
617 connman_bool_t connman_network_get_available(struct connman_network *network)
619 if (network->hidden == TRUE)
622 return network->available;
626 * connman_network_set_associating:
627 * @network: network structure
628 * @associating: associating state
630 * Change associating state of network
632 int connman_network_set_associating(struct connman_network *network,
633 connman_bool_t associating)
635 DBG("network %p associating %d", network, associating);
637 if (network->associating == associating)
640 network->associating = associating;
642 if (associating == TRUE) {
643 struct connman_service *service;
645 service = __connman_service_lookup_from_network(network);
646 __connman_service_indicate_state(service,
647 CONNMAN_SERVICE_STATE_ASSOCIATION);
653 static void set_associate_error(struct connman_network *network)
655 struct connman_service *service;
657 if (network->associating == FALSE)
660 network->associating = FALSE;
662 service = __connman_service_lookup_from_network(network);
664 __connman_service_indicate_state(service,
665 CONNMAN_SERVICE_STATE_FAILURE);
668 static void set_configure_error(struct connman_network *network)
670 struct connman_service *service;
672 network->connecting = FALSE;
674 service = __connman_service_lookup_from_network(network);
676 __connman_service_indicate_state(service,
677 CONNMAN_SERVICE_STATE_FAILURE);
680 void connman_network_set_method(struct connman_network *network,
681 enum connman_ipconfig_method method)
683 struct connman_service *service;
684 struct connman_ipconfig *ipconfig;
686 network->element.ipv4.method = method;
688 service = __connman_service_lookup_from_network(network);
692 ipconfig = __connman_service_get_ipconfig(service);
693 if (ipconfig == NULL)
696 connman_ipconfig_set_method(ipconfig, method);
699 void connman_network_set_error(struct connman_network *network,
700 enum connman_network_error error)
702 DBG("nework %p, error %d", network, error);
704 network->connecting = FALSE;
707 case CONNMAN_NETWORK_ERROR_UNKNOWN:
709 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
710 set_associate_error(network);
712 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
713 set_configure_error(network);
718 static void set_configuration(struct connman_network *network)
720 struct connman_service *service;
722 DBG("network %p", network);
724 __connman_device_increase_connections(network->device);
726 __connman_device_set_network(network->device, network);
728 connman_device_set_disconnected(network->device, FALSE);
730 service = __connman_service_lookup_from_network(network);
731 __connman_service_indicate_state(service,
732 CONNMAN_SERVICE_STATE_CONFIGURATION);
735 static void set_connected_manual(struct connman_network *network)
737 struct connman_service *service;
738 struct connman_ipconfig *ipconfig;
741 DBG("network %p", network);
743 service = __connman_service_lookup_from_network(network);
745 ipconfig = __connman_service_get_ipconfig(service);
747 set_configuration(network);
749 err = __connman_ipconfig_set_address(ipconfig);
751 connman_network_set_error(network,
752 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
756 network->connecting = FALSE;
758 connman_network_set_associating(network, FALSE);
760 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
763 static int set_connected_dhcp(struct connman_network *network)
765 struct connman_element *element;
768 DBG("network %p", network);
770 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
773 element = connman_element_create(NULL);
777 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
778 element->index = network->element.index;
780 error = connman_element_register(element, &network->element);
782 connman_element_unref(element);
786 set_configuration(network);
791 static gboolean set_connected(gpointer user_data)
793 struct connman_network *network = user_data;
794 struct connman_service *service;
795 struct connman_ipconfig *ipconfig;
796 enum connman_ipconfig_method method;
798 service = __connman_service_lookup_from_network(network);
800 ipconfig = __connman_service_get_ipconfig(service);
802 method = __connman_ipconfig_get_method(ipconfig);
804 DBG("method %d", method);
806 if (network->connected == TRUE) {
808 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
809 case CONNMAN_IPCONFIG_METHOD_OFF:
811 case CONNMAN_IPCONFIG_METHOD_MANUAL:
812 case CONNMAN_IPCONFIG_METHOD_FIXED:
813 set_connected_manual(network);
815 case CONNMAN_IPCONFIG_METHOD_DHCP:
816 if (set_connected_dhcp(network) < 0) {
817 connman_network_set_error(network,
818 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
824 struct connman_service *service;
826 connman_element_unregister_children(&network->element);
828 __connman_device_set_network(network->device, NULL);
829 network->hidden = FALSE;
831 __connman_device_decrease_connections(network->device);
833 service = __connman_service_lookup_from_network(network);
835 __connman_service_indicate_state(service,
836 CONNMAN_SERVICE_STATE_IDLE);
839 network->connecting = FALSE;
841 connman_network_set_associating(network, FALSE);
847 * connman_network_set_connected:
848 * @network: network structure
849 * @connected: connected state
851 * Change connected state of network
853 int connman_network_set_connected(struct connman_network *network,
854 connman_bool_t connected)
856 DBG("network %p connected %d", network, connected);
858 if ((network->connecting == TRUE || network->associating == TRUE) &&
859 connected == FALSE) {
860 connman_element_set_error(&network->element,
861 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
862 __connman_network_disconnect(network);
865 if (network->connected == connected)
868 network->connected = connected;
870 if (network->registered == FALSE) {
871 g_idle_add(set_connected, network);
875 connman_dbus_property_changed_basic(network->element.path,
876 CONNMAN_NETWORK_INTERFACE, "Connected",
877 DBUS_TYPE_BOOLEAN, &connected);
879 set_connected(network);
885 * connman_network_get_connected:
886 * @network: network structure
888 * Get network connection status
890 connman_bool_t connman_network_get_connected(struct connman_network *network)
892 return network->connected;
896 * connman_network_get_associating:
897 * @network: network structure
899 * Get network associating status
901 connman_bool_t connman_network_get_associating(struct connman_network *network)
903 return network->associating;
907 * __connman_network_connect:
908 * @network: network structure
912 int __connman_network_connect(struct connman_network *network)
916 DBG("network %p", network);
918 if (network->connected == TRUE)
921 if (network->connecting == TRUE || network->associating == TRUE)
924 if (network->driver == NULL)
927 if (network->driver->connect == NULL)
930 __connman_device_disconnect(network->device);
932 network->connecting = TRUE;
934 err = network->driver->connect(network);
936 if (err == -EINPROGRESS)
937 connman_network_set_associating(network, TRUE);
939 network->connecting = FALSE;
940 network->hidden = FALSE;
946 network->connected = TRUE;
947 set_connected(network);
953 * __connman_network_disconnect:
954 * @network: network structure
958 int __connman_network_disconnect(struct connman_network *network)
962 DBG("network %p", network);
964 if (network->connected == FALSE && network->connecting == FALSE &&
965 network->associating == FALSE)
968 if (network->driver == NULL)
971 if (network->driver->disconnect == NULL)
974 network->connecting = FALSE;
976 err = network->driver->disconnect(network);
978 connman_network_set_connected(network, FALSE);
979 set_connected(network);
986 * connman_network_set_address:
987 * @network: network structure
988 * @address: binary address value
989 * @size: binary address length
991 * Set unique address value for network
993 int connman_network_set_address(struct connman_network *network,
994 const void *address, unsigned int size)
996 const unsigned char *addr_octet = address;
999 DBG("network %p size %d", network, size);
1004 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1005 addr_octet[0], addr_octet[1], addr_octet[2],
1006 addr_octet[3], addr_octet[4], addr_octet[5]);
1010 g_free(network->address);
1011 network->address = str;
1013 return connman_element_set_string(&network->element,
1014 "Address", network->address);
1018 * connman_network_set_name:
1019 * @network: network structure
1022 * Set display name value for network
1024 int connman_network_set_name(struct connman_network *network,
1027 DBG("network %p name %s", network, name);
1029 g_free(network->name);
1030 network->name = g_strdup(name);
1032 return connman_element_set_string(&network->element, "Name", name);
1036 * connman_network_set_strength:
1037 * @network: network structure
1038 * @strength: strength value
1040 * Set signal strength value for network
1042 int connman_network_set_strength(struct connman_network *network,
1043 connman_uint8_t strength)
1045 DBG("network %p strengh %d", network, strength);
1047 network->strength = strength;
1049 return connman_element_set_uint8(&network->element,
1050 "Strength", strength);
1054 * connman_network_set_roaming:
1055 * @network: network structure
1056 * @roaming: roaming state
1058 * Set roaming state for network
1060 int connman_network_set_roaming(struct connman_network *network,
1061 connman_bool_t roaming)
1063 DBG("network %p roaming %d", network, roaming);
1065 network->roaming = roaming;
1067 return connman_element_set_bool(&network->element,
1068 "Roaming", roaming);
1072 * connman_network_set_string:
1073 * @network: network structure
1074 * @key: unique identifier
1075 * @value: string value
1077 * Set string value for specific key
1079 int connman_network_set_string(struct connman_network *network,
1080 const char *key, const char *value)
1084 DBG("network %p key %s value %s", network, key, value);
1086 if (g_strcmp0(key, "Name") == 0)
1087 return connman_network_set_name(network, value);
1089 if (g_str_equal(key, "Address") == TRUE) {
1090 g_free(network->address);
1091 network->address = g_strdup(value);
1092 } else if (g_str_equal(key, "Node") == TRUE) {
1093 g_free(network->node);
1094 network->node = g_strdup(value);
1095 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1096 g_free(network->wifi.mode);
1097 network->wifi.mode = g_strdup(value);
1098 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1099 g_free(network->wifi.security);
1100 network->wifi.security = g_strdup(value);
1101 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1102 g_free(network->wifi.passphrase);
1103 network->wifi.passphrase = g_strdup(value);
1104 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1105 g_free(network->wifi.eap);
1106 network->wifi.eap = g_strdup(value);
1107 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1108 g_free(network->wifi.identity);
1109 network->wifi.identity = g_strdup(value);
1110 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1111 g_free(network->wifi.ca_cert_path);
1112 network->wifi.ca_cert_path = g_strdup(value);
1113 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1114 g_free(network->wifi.client_cert_path);
1115 network->wifi.client_cert_path = g_strdup(value);
1116 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1117 g_free(network->wifi.private_key_path);
1118 network->wifi.private_key_path = g_strdup(value);
1119 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1120 g_free(network->wifi.private_key_passphrase);
1121 network->wifi.private_key_passphrase = g_strdup(value);
1122 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1123 g_free(network->wifi.phase2_auth);
1124 network->wifi.phase2_auth = g_strdup(value);
1127 err = connman_element_set_string(&network->element, key, value);
1131 if (network->driver == NULL)
1134 if (network->driver->setup)
1135 return network->driver->setup(network, key);
1141 * connman_network_get_string:
1142 * @network: network structure
1143 * @key: unique identifier
1145 * Get string value for specific key
1147 const char *connman_network_get_string(struct connman_network *network,
1150 DBG("network %p key %s", network, key);
1152 if (g_str_equal(key, "Address") == TRUE)
1153 return network->address;
1154 else if (g_str_equal(key, "Name") == TRUE)
1155 return network->name;
1156 else if (g_str_equal(key, "Node") == TRUE)
1157 return network->node;
1158 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1159 return network->wifi.mode;
1160 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1161 return network->wifi.security;
1162 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1163 return network->wifi.passphrase;
1164 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1165 return network->wifi.eap;
1166 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1167 return network->wifi.identity;
1168 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1169 return network->wifi.ca_cert_path;
1170 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1171 return network->wifi.client_cert_path;
1172 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1173 return network->wifi.private_key_path;
1174 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1175 return network->wifi.private_key_passphrase;
1176 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1177 return network->wifi.phase2_auth;
1179 return connman_element_get_string(&network->element, key);
1183 * connman_network_set_bool:
1184 * @network: network structure
1185 * @key: unique identifier
1186 * @value: boolean value
1188 * Set boolean value for specific key
1190 int connman_network_set_bool(struct connman_network *network,
1191 const char *key, connman_bool_t value)
1193 DBG("network %p key %s value %d", network, key, value);
1195 if (g_strcmp0(key, "Roaming") == 0)
1196 return connman_network_set_roaming(network, value);
1198 return connman_element_set_bool(&network->element, key, value);
1202 * connman_network_get_bool:
1203 * @network: network structure
1204 * @key: unique identifier
1206 * Get boolean value for specific key
1208 connman_bool_t connman_network_get_bool(struct connman_network *network,
1211 DBG("network %p key %s", network, key);
1213 if (g_str_equal(key, "Roaming") == TRUE)
1214 return network->roaming;
1216 return connman_element_get_bool(&network->element, key);
1220 * connman_network_set_uint8:
1221 * @network: network structure
1222 * @key: unique identifier
1223 * @value: integer value
1225 * Set integer value for specific key
1227 int connman_network_set_uint8(struct connman_network *network,
1228 const char *key, connman_uint8_t value)
1230 DBG("network %p key %s value %d", network, key, value);
1232 if (g_strcmp0(key, "Strength") == 0)
1233 return connman_network_set_strength(network, value);
1235 return connman_element_set_uint8(&network->element, key, value);
1239 * connman_network_get_uint8:
1240 * @network: network structure
1241 * @key: unique identifier
1243 * Get integer value for specific key
1245 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1248 DBG("network %p key %s", network, key);
1250 if (g_str_equal(key, "Strength") == TRUE)
1251 return network->strength;
1253 return connman_element_get_uint8(&network->element, key);
1257 * connman_network_set_uint16:
1258 * @network: network structure
1259 * @key: unique identifier
1260 * @value: integer value
1262 * Set integer value for specific key
1264 int connman_network_set_uint16(struct connman_network *network,
1265 const char *key, connman_uint16_t value)
1267 DBG("network %p key %s value %d", network, key, value);
1269 if (g_str_equal(key, "Frequency") == TRUE)
1270 network->frequency = value;
1271 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1272 network->wifi.channel = value;
1278 * connman_network_get_uint16:
1279 * @network: network structure
1280 * @key: unique identifier
1282 * Get integer value for specific key
1284 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1287 DBG("network %p key %s", network, key);
1289 if (g_str_equal(key, "Frequency") == TRUE)
1290 return network->frequency;
1291 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1292 return network->wifi.channel;
1298 * connman_network_set_blob:
1299 * @network: network structure
1300 * @key: unique identifier
1304 * Set binary blob value for specific key
1306 int connman_network_set_blob(struct connman_network *network,
1307 const char *key, const void *data, unsigned int size)
1309 DBG("network %p key %s size %d", network, key, size);
1311 if (g_strcmp0(key, "Address") == 0)
1312 return connman_network_set_address(network, data, size);
1314 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1315 g_free(network->wifi.ssid);
1316 network->wifi.ssid = g_try_malloc(size);
1317 if (network->wifi.ssid != NULL) {
1318 memcpy(network->wifi.ssid, data, size);
1319 network->wifi.ssid_len = size;
1321 network->wifi.ssid_len = 0;
1324 return connman_element_set_blob(&network->element, key, data, size);
1328 * connman_network_get_blob:
1329 * @network: network structure
1330 * @key: unique identifier
1331 * @size: pointer to blob size
1333 * Get binary blob value for specific key
1335 const void *connman_network_get_blob(struct connman_network *network,
1336 const char *key, unsigned int *size)
1338 DBG("network %p key %s", network, key);
1340 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1342 *size = network->wifi.ssid_len;
1343 return network->wifi.ssid;
1346 return connman_element_get_blob(&network->element, key, size);
1349 void __connman_network_set_device(struct connman_network *network,
1350 struct connman_device *device)
1352 network->device = device;
1356 * connman_network_get_device:
1357 * @network: network structure
1359 * Get parent device of network
1361 struct connman_device *connman_network_get_device(struct connman_network *network)
1363 return network->device;
1367 * connman_network_get_data:
1368 * @network: network structure
1370 * Get private network data pointer
1372 void *connman_network_get_data(struct connman_network *network)
1374 return network->driver_data;
1378 * connman_network_set_data:
1379 * @network: network structure
1380 * @data: data pointer
1382 * Set private network data pointer
1384 void connman_network_set_data(struct connman_network *network, void *data)
1386 network->driver_data = data;
1389 static gboolean match_driver(struct connman_network *network,
1390 struct connman_network_driver *driver)
1392 if (network->type == driver->type ||
1393 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1399 static int network_probe(struct connman_element *element)
1401 struct connman_network *network = element->network;
1405 DBG("element %p name %s", element, element->name);
1407 if (network == NULL)
1410 for (list = driver_list; list; list = list->next) {
1411 struct connman_network_driver *driver = list->data;
1413 if (match_driver(network, driver) == FALSE)
1416 DBG("driver %p name %s", driver, driver->name);
1418 if (driver->probe(network) == 0) {
1419 network->driver = driver;
1424 if (network->driver == NULL)
1427 err = register_interface(element);
1429 if (network->driver->remove)
1430 network->driver->remove(network);
1434 switch (network->type) {
1435 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1436 case CONNMAN_NETWORK_TYPE_VENDOR:
1438 case CONNMAN_NETWORK_TYPE_ETHERNET:
1439 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1440 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1441 case CONNMAN_NETWORK_TYPE_CELLULAR:
1442 case CONNMAN_NETWORK_TYPE_MBM:
1443 case CONNMAN_NETWORK_TYPE_HSO:
1444 case CONNMAN_NETWORK_TYPE_WIFI:
1445 case CONNMAN_NETWORK_TYPE_WIMAX:
1446 if (network->group != NULL)
1447 __connman_profile_add_network(network);
1454 static void network_remove(struct connman_element *element)
1456 struct connman_network *network = element->network;
1458 DBG("element %p name %s", element, element->name);
1460 if (network == NULL)
1463 if (network->driver == NULL)
1466 switch (network->type) {
1467 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1468 case CONNMAN_NETWORK_TYPE_VENDOR:
1470 case CONNMAN_NETWORK_TYPE_ETHERNET:
1471 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1472 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1473 case CONNMAN_NETWORK_TYPE_CELLULAR:
1474 case CONNMAN_NETWORK_TYPE_MBM:
1475 case CONNMAN_NETWORK_TYPE_HSO:
1476 case CONNMAN_NETWORK_TYPE_WIFI:
1477 case CONNMAN_NETWORK_TYPE_WIMAX:
1478 if (network->group != NULL) {
1479 __connman_profile_remove_network(network);
1481 g_free(network->group);
1482 network->group = NULL;
1487 unregister_interface(element);
1489 if (network->driver->remove)
1490 network->driver->remove(network);
1493 static void network_change(struct connman_element *element)
1495 struct connman_network *network = element->network;
1497 DBG("element %p name %s", element, element->name);
1499 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1502 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1505 if (network->connected == FALSE)
1508 connman_element_unregister_children(element);
1510 connman_device_set_disconnected(network->device, TRUE);
1512 if (network->driver && network->driver->disconnect) {
1513 network->driver->disconnect(network);
1517 network->connected = FALSE;
1520 static struct connman_driver network_driver = {
1522 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1523 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1524 .probe = network_probe,
1525 .remove = network_remove,
1526 .change = network_change,
1529 int __connman_network_init(void)
1533 connection = connman_dbus_get_connection();
1535 return connman_driver_register(&network_driver);
1538 void __connman_network_cleanup(void)
1542 connman_driver_unregister(&network_driver);
1544 dbus_connection_unref(connection);