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
31 static unsigned int hidden_counter = 0;
33 struct connman_network {
34 struct connman_element element;
35 enum connman_network_type type;
36 connman_bool_t available;
37 connman_bool_t connected;
38 connman_bool_t roaming;
39 connman_bool_t hidden;
40 connman_uint8_t strength;
41 connman_uint16_t frequency;
48 struct connman_network_driver *driver;
51 connman_bool_t connecting;
52 connman_bool_t associating;
54 struct connman_device *device;
60 unsigned short channel;
66 char *client_cert_path;
67 char *private_key_path;
68 char *private_key_passphrase;
71 connman_bool_t use_wps;
76 static const char *type2string(enum connman_network_type type)
79 case CONNMAN_NETWORK_TYPE_UNKNOWN:
80 case CONNMAN_NETWORK_TYPE_VENDOR:
82 case CONNMAN_NETWORK_TYPE_ETHERNET:
84 case CONNMAN_NETWORK_TYPE_WIFI:
86 case CONNMAN_NETWORK_TYPE_WIMAX:
88 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
89 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
91 case CONNMAN_NETWORK_TYPE_CELLULAR:
98 connman_bool_t __connman_network_has_driver(struct connman_network *network)
100 if (network == NULL || network->driver == NULL)
106 static GSList *driver_list = NULL;
108 static gint compare_priority(gconstpointer a, gconstpointer b)
110 const struct connman_network_driver *driver1 = a;
111 const struct connman_network_driver *driver2 = b;
113 return driver2->priority - driver1->priority;
117 * connman_network_driver_register:
118 * @driver: network driver definition
120 * Register a new network driver
122 * Returns: %0 on success
124 int connman_network_driver_register(struct connman_network_driver *driver)
128 DBG("driver %p name %s", driver, driver->name);
130 for (list = driver_list; list; list = list->next) {
131 struct connman_network_driver *tmp = list->data;
133 if (tmp->type == driver->type)
138 driver_list = g_slist_insert_sorted(driver_list, driver,
145 * connman_network_driver_unregister:
146 * @driver: network driver definition
148 * Remove a previously registered network driver
150 void connman_network_driver_unregister(struct connman_network_driver *driver)
152 DBG("driver %p name %s", driver, driver->name);
154 driver_list = g_slist_remove(driver_list, driver);
157 static void network_destruct(struct connman_element *element)
159 struct connman_network *network = element->network;
161 DBG("element %p name %s", element, element->name);
163 g_free(network->wifi.ssid);
164 g_free(network->wifi.mode);
165 g_free(network->wifi.security);
166 g_free(network->wifi.passphrase);
167 g_free(network->wifi.eap);
168 g_free(network->wifi.identity);
169 g_free(network->wifi.ca_cert_path);
170 g_free(network->wifi.client_cert_path);
171 g_free(network->wifi.private_key_path);
172 g_free(network->wifi.private_key_passphrase);
173 g_free(network->wifi.phase2_auth);
174 g_free(network->wifi.pin_wps);
176 g_free(network->group);
177 g_free(network->node);
178 g_free(network->name);
179 g_free(network->address);
180 g_free(network->identifier);
182 network->device = NULL;
186 * connman_network_create:
187 * @identifier: network identifier (for example an unqiue name)
189 * Allocate a new network and assign the #identifier to it.
191 * Returns: a newly-allocated #connman_network structure
193 struct connman_network *connman_network_create(const char *identifier,
194 enum connman_network_type type)
196 struct connman_network *network;
197 connman_uint8_t strength = 0;
201 DBG("identifier %s type %d", identifier, type);
203 network = g_try_new0(struct connman_network, 1);
207 DBG("network %p", network);
209 __connman_element_initialize(&network->element);
211 if (identifier == NULL) {
212 temp = g_strdup_printf("hidden_%d", hidden_counter++);
213 network->hidden = TRUE;
215 temp = g_strdup(identifier);
222 network->element.name = temp;
223 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
225 network->element.network = network;
226 network->element.destruct = network_destruct;
228 str = type2string(type);
230 connman_element_set_string(&network->element, "Type", str);
232 connman_element_set_uint8(&network->element, "Strength", strength);
234 network->type = type;
235 network->identifier = g_strdup(temp);
241 * connman_network_ref:
242 * @network: network structure
244 * Increase reference counter of network
246 struct connman_network *connman_network_ref(struct connman_network *network)
248 if (connman_element_ref(&network->element) == NULL)
255 * connman_network_unref:
256 * @network: network structure
258 * Decrease reference counter of network
260 void connman_network_unref(struct connman_network *network)
262 connman_element_unref(&network->element);
265 const char *__connman_network_get_type(struct connman_network *network)
267 return type2string(network->type);
271 * connman_network_get_type:
272 * @network: network structure
274 * Get type of network
276 enum connman_network_type connman_network_get_type(struct connman_network *network)
278 return network->type;
282 * connman_network_get_identifier:
283 * @network: network structure
285 * Get identifier of network
287 const char *connman_network_get_identifier(struct connman_network *network)
289 return network->identifier;
293 * connman_network_set_index:
294 * @network: network structure
295 * @index: index number
297 * Set index number of network
299 void connman_network_set_index(struct connman_network *network, int index)
301 struct connman_service *service;
302 struct connman_ipconfig *ipconfig;
304 service = __connman_service_lookup_from_network(network);
308 ipconfig = __connman_service_get_ip4config(service);
310 DBG("index %d service %p ip4config %p", network->element.index,
313 if (network->element.index < 0 && ipconfig == NULL) {
315 ipconfig = __connman_service_get_ip4config(service);
316 if (ipconfig == NULL)
318 * This is needed for plugins that havent set their
319 * ipconfig layer yet, due to not being able to get
320 * a network index prior to creating a service.
322 __connman_service_create_ip4config(service, index);
324 __connman_ipconfig_set_index(ipconfig, index);
327 /* If index changed, the index of ipconfig must be reset. */
328 if (ipconfig == NULL)
331 __connman_ipconfig_set_index(ipconfig, index);
335 network->element.index = index;
339 * connman_network_get_index:
340 * @network: network structure
342 * Get index number of network
344 int connman_network_get_index(struct connman_network *network)
346 return network->element.index;
350 * connman_network_get_element:
351 * @network: network structure
353 * Get connman_element of network
355 struct connman_element *connman_network_get_element(
356 struct connman_network *network)
358 return &network->element;
362 * connman_network_set_group:
363 * @network: network structure
366 * Set group name for automatic clustering
368 void connman_network_set_group(struct connman_network *network,
371 switch (network->type) {
372 case CONNMAN_NETWORK_TYPE_UNKNOWN:
373 case CONNMAN_NETWORK_TYPE_VENDOR:
375 case CONNMAN_NETWORK_TYPE_ETHERNET:
376 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
377 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
378 case CONNMAN_NETWORK_TYPE_CELLULAR:
379 case CONNMAN_NETWORK_TYPE_WIFI:
380 case CONNMAN_NETWORK_TYPE_WIMAX:
384 if (g_strcmp0(network->group, group) == 0) {
386 __connman_profile_update_network(network);
390 if (network->group != NULL) {
391 __connman_profile_remove_network(network);
393 g_free(network->group);
396 network->group = g_strdup(group);
398 if (network->group != NULL)
399 __connman_profile_add_network(network);
403 * connman_network_get_group:
404 * @network: network structure
406 * Get group name for automatic clustering
408 const char *connman_network_get_group(struct connman_network *network)
410 return network->group;
413 const char *__connman_network_get_ident(struct connman_network *network)
415 if (network->device == NULL)
418 return connman_device_get_ident(network->device);
421 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
423 switch (network->type) {
424 case CONNMAN_NETWORK_TYPE_UNKNOWN:
425 case CONNMAN_NETWORK_TYPE_VENDOR:
426 case CONNMAN_NETWORK_TYPE_ETHERNET:
427 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
428 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
429 case CONNMAN_NETWORK_TYPE_CELLULAR:
430 case CONNMAN_NETWORK_TYPE_WIMAX:
432 case CONNMAN_NETWORK_TYPE_WIFI:
433 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
435 if (network->strength > 0 && network->strength < 20)
443 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
445 return network->connecting;
449 * connman_network_set_available:
450 * @network: network structure
451 * @available: availability state
453 * Change availability state of network (in range)
455 int connman_network_set_available(struct connman_network *network,
456 connman_bool_t available)
458 DBG("network %p available %d", network, available);
460 if (network->available == available)
463 network->available = available;
469 * connman_network_get_available:
470 * @network: network structure
472 * Get network available setting
474 connman_bool_t connman_network_get_available(struct connman_network *network)
476 if (network->hidden == TRUE)
479 return network->available;
483 * connman_network_set_associating:
484 * @network: network structure
485 * @associating: associating state
487 * Change associating state of network
489 int connman_network_set_associating(struct connman_network *network,
490 connman_bool_t associating)
492 DBG("network %p associating %d", network, associating);
494 if (network->associating == associating)
497 network->associating = associating;
499 if (associating == TRUE) {
500 struct connman_service *service;
502 service = __connman_service_lookup_from_network(network);
503 __connman_service_indicate_state(service,
504 CONNMAN_SERVICE_STATE_ASSOCIATION,
505 CONNMAN_IPCONFIG_TYPE_IPV4);
511 static void set_associate_error(struct connman_network *network)
513 struct connman_service *service;
515 if (network->associating == FALSE)
518 network->associating = FALSE;
520 service = __connman_service_lookup_from_network(network);
522 __connman_service_indicate_state(service,
523 CONNMAN_SERVICE_STATE_FAILURE,
524 CONNMAN_IPCONFIG_TYPE_IPV4);
527 static void set_configure_error(struct connman_network *network)
529 struct connman_service *service;
531 network->connecting = FALSE;
533 service = __connman_service_lookup_from_network(network);
535 __connman_service_indicate_state(service,
536 CONNMAN_SERVICE_STATE_FAILURE,
537 CONNMAN_IPCONFIG_TYPE_IPV4);
540 static void set_invalid_key_error(struct connman_network *network)
542 struct connman_service *service;
544 service = __connman_service_lookup_from_network(network);
546 __connman_service_indicate_error(service,
547 CONNMAN_SERVICE_ERROR_INVALID_KEY);
550 void connman_network_set_ipv4_method(struct connman_network *network,
551 enum connman_ipconfig_method method)
553 struct connman_service *service;
554 struct connman_ipconfig *ipconfig;
556 network->element.ipv4.method = method;
558 service = __connman_service_lookup_from_network(network);
562 ipconfig = __connman_service_get_ip4config(service);
563 if (ipconfig == NULL)
566 connman_ipconfig_set_method(ipconfig, method);
569 void connman_network_set_ipv6_method(struct connman_network *network,
570 enum connman_ipconfig_method method)
572 struct connman_service *service;
573 struct connman_ipconfig *ipconfig;
575 network->element.ipv6.method = method;
577 service = __connman_service_lookup_from_network(network);
581 ipconfig = __connman_service_get_ip6config(service);
582 if (ipconfig == NULL)
585 connman_ipconfig_set_method(ipconfig, method);
588 void connman_network_set_error(struct connman_network *network,
589 enum connman_network_error error)
591 DBG("nework %p, error %d", network, error);
593 network->connecting = FALSE;
596 case CONNMAN_NETWORK_ERROR_UNKNOWN:
598 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
599 set_associate_error(network);
601 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
602 set_configure_error(network);
604 case CONNMAN_NETWORK_ERROR_INVALID_KEY:
605 set_invalid_key_error(network);
610 void connman_network_clear_error(struct connman_network *network)
612 struct connman_service *service;
614 DBG("network %p", network);
619 if (network->connecting == TRUE || network->associating == TRUE)
622 service = __connman_service_lookup_from_network(network);
623 __connman_service_clear_error(service);
626 static void set_configuration(struct connman_network *network)
628 struct connman_service *service;
630 DBG("network %p", network);
632 __connman_device_increase_connections(network->device);
634 __connman_device_set_network(network->device, network);
636 connman_device_set_disconnected(network->device, FALSE);
638 service = __connman_service_lookup_from_network(network);
639 __connman_service_indicate_state(service,
640 CONNMAN_SERVICE_STATE_CONFIGURATION,
641 CONNMAN_IPCONFIG_TYPE_IPV4);
644 static int set_connected_fixed(struct connman_network *network)
646 struct connman_service *service;
647 struct connman_element *parent, *element;
651 service = __connman_service_lookup_from_network(network);
653 parent = connman_network_get_element(network);
655 set_configuration(network);
657 if (parent->ipv4.address == NULL)
660 if (parent->ipv4.netmask == NULL)
663 element = connman_element_create(NULL);
664 if (element == NULL) {
665 connman_error("Can not create connman_element");
669 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
670 element->index = parent->index;
672 if (connman_element_register(element, parent) < 0) {
673 connman_error("Can not register connman_element");
677 network->connecting = FALSE;
679 connman_network_set_associating(network, FALSE);
684 static void set_connected_manual(struct connman_network *network)
686 struct connman_service *service;
687 struct connman_ipconfig *ipconfig;
688 const char *nameserver = NULL;
691 DBG("network %p", network);
693 service = __connman_service_lookup_from_network(network);
695 ipconfig = __connman_service_get_ip4config(service);
697 set_configuration(network);
699 err = __connman_ipconfig_set_address(ipconfig);
701 connman_network_set_error(network,
702 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
706 connman_element_get_value(&network->element,
707 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
708 if (nameserver != NULL)
709 __connman_service_append_nameserver(service, nameserver);
711 __connman_ipconfig_set_gateway(ipconfig, &network->element);
713 network->connecting = FALSE;
715 connman_network_set_associating(network, FALSE);
717 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
718 CONNMAN_IPCONFIG_TYPE_IPV4);
721 static int set_connected_dhcp(struct connman_network *network)
723 struct connman_element *element;
726 DBG("network %p", network);
728 element = connman_element_create(NULL);
732 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
733 element->index = network->element.index;
735 error = connman_element_register(element, &network->element);
737 connman_element_unref(element);
741 set_configuration(network);
746 static int manual_ipv6_set(struct connman_network *network,
747 struct connman_ipconfig *ipconfig_ipv6)
749 struct connman_service *service;
752 service = __connman_service_lookup_from_network(network);
756 err = __connman_ipconfig_set_address(ipconfig_ipv6);
758 connman_network_set_error(network,
759 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
764 * READY state will be indicated by IPV4 setting
765 * gateway will be set by IPV4 setting
771 static gboolean set_connected(gpointer user_data)
773 struct connman_network *network = user_data;
774 struct connman_service *service;
775 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
776 enum connman_ipconfig_method ipv4_method, ipv6_method;
778 service = __connman_service_lookup_from_network(network);
780 ipconfig_ipv4 = __connman_service_get_ip4config(service);
781 ipconfig_ipv6 = __connman_service_get_ip6config(service);
783 DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
786 ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
787 ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
789 DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
790 DBG("network connected %d", network->connected);
792 if (network->connected == TRUE) {
795 switch (ipv6_method) {
796 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
797 case CONNMAN_IPCONFIG_METHOD_OFF:
798 case CONNMAN_IPCONFIG_METHOD_AUTO:
800 case CONNMAN_IPCONFIG_METHOD_FIXED:
801 case CONNMAN_IPCONFIG_METHOD_MANUAL:
802 ret = manual_ipv6_set(network, ipconfig_ipv6);
804 connman_network_set_error(network,
805 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
809 case CONNMAN_IPCONFIG_METHOD_DHCP:
813 switch (ipv4_method) {
814 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
815 case CONNMAN_IPCONFIG_METHOD_OFF:
816 case CONNMAN_IPCONFIG_METHOD_AUTO:
818 case CONNMAN_IPCONFIG_METHOD_FIXED:
819 if (set_connected_fixed(network) < 0) {
820 connman_network_set_error(network,
821 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
825 case CONNMAN_IPCONFIG_METHOD_MANUAL:
826 set_connected_manual(network);
828 case CONNMAN_IPCONFIG_METHOD_DHCP:
829 if (set_connected_dhcp(network) < 0) {
830 connman_network_set_error(network,
831 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
837 struct connman_service *service;
839 connman_element_unregister_children(&network->element);
841 __connman_device_set_network(network->device, NULL);
842 network->hidden = FALSE;
844 service = __connman_service_lookup_from_network(network);
846 __connman_service_indicate_state(service,
847 CONNMAN_SERVICE_STATE_IDLE,
848 CONNMAN_IPCONFIG_TYPE_IPV4);
851 network->connecting = FALSE;
853 connman_network_set_associating(network, FALSE);
859 * connman_network_set_connected:
860 * @network: network structure
861 * @connected: connected state
863 * Change connected state of network
865 int connman_network_set_connected(struct connman_network *network,
866 connman_bool_t connected)
868 DBG("network %p connected %d", network, connected);
870 if ((network->connecting == TRUE || network->associating == TRUE) &&
871 connected == FALSE) {
872 connman_element_set_error(&network->element,
873 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
874 __connman_network_disconnect(network);
877 if (network->connected == connected)
880 if (connected == FALSE)
881 __connman_device_decrease_connections(network->device);
883 network->connected = connected;
885 set_connected(network);
891 * connman_network_get_connected:
892 * @network: network structure
894 * Get network connection status
896 connman_bool_t connman_network_get_connected(struct connman_network *network)
898 return network->connected;
902 * connman_network_get_associating:
903 * @network: network structure
905 * Get network associating status
907 connman_bool_t connman_network_get_associating(struct connman_network *network)
909 return network->associating;
913 * __connman_network_connect:
914 * @network: network structure
918 int __connman_network_connect(struct connman_network *network)
920 struct connman_service *service;
923 DBG("network %p", network);
925 if (network->connected == TRUE)
928 if (network->connecting == TRUE || network->associating == TRUE)
931 if (network->driver == NULL)
934 if (network->driver->connect == NULL)
937 if (network->device == NULL)
940 network->connecting = TRUE;
942 __connman_device_disconnect(network->device);
944 service = __connman_service_lookup_from_network(network);
946 err = network->driver->connect(network);
948 if (err == -EINPROGRESS)
949 connman_network_set_associating(network, TRUE);
951 network->connecting = FALSE;
952 network->hidden = FALSE;
958 network->connected = TRUE;
959 set_connected(network);
965 * __connman_network_disconnect:
966 * @network: network structure
970 int __connman_network_disconnect(struct connman_network *network)
974 DBG("network %p", network);
976 if (network->connected == FALSE && network->connecting == FALSE &&
977 network->associating == FALSE)
980 if (network->driver == NULL)
983 if (network->driver->disconnect == NULL)
986 network->connecting = FALSE;
988 err = network->driver->disconnect(network);
990 connman_network_set_connected(network, FALSE);
991 set_connected(network);
997 static int dhcp_start(struct connman_network *network)
999 struct connman_element *element;
1002 element = connman_element_create(NULL);
1003 if (element == NULL)
1006 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
1007 element->index = network->element.index;
1009 error = connman_element_register(element, &network->element);
1011 connman_element_unref(element);
1018 static int dhcp_stop(struct connman_network *network)
1020 connman_element_unregister_children_type(&network->element,
1021 CONNMAN_ELEMENT_TYPE_CONNECTION);
1022 connman_element_unregister_children_type(&network->element,
1023 CONNMAN_ELEMENT_TYPE_IPV4);
1024 connman_element_unregister_children_type(&network->element,
1025 CONNMAN_ELEMENT_TYPE_DHCP);
1030 static int manual_ipv4_set(struct connman_network *network,
1031 struct connman_ipconfig *ipconfig)
1033 struct connman_service *service;
1036 service = __connman_service_lookup_from_network(network);
1037 if (service == NULL)
1040 err = __connman_ipconfig_set_address(ipconfig);
1042 connman_network_set_error(network,
1043 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1047 __connman_ipconfig_set_gateway(ipconfig, &network->element);
1049 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
1050 CONNMAN_IPCONFIG_TYPE_IPV4);
1055 int __connman_network_clear_ipconfig(struct connman_network *network,
1056 struct connman_ipconfig *ipconfig)
1058 struct connman_service *service;
1059 enum connman_ipconfig_method method;
1061 service = __connman_service_lookup_from_network(network);
1062 if (service == NULL)
1065 method = __connman_ipconfig_get_method(ipconfig);
1068 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1069 case CONNMAN_IPCONFIG_METHOD_OFF:
1070 case CONNMAN_IPCONFIG_METHOD_FIXED:
1071 case CONNMAN_IPCONFIG_METHOD_AUTO:
1073 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1074 connman_element_unregister_children_type(&network->element,
1075 CONNMAN_ELEMENT_TYPE_CONNECTION);
1076 __connman_ipconfig_clear_address(ipconfig);
1078 case CONNMAN_IPCONFIG_METHOD_DHCP:
1083 __connman_service_indicate_state(service,
1084 CONNMAN_SERVICE_STATE_CONFIGURATION,
1085 CONNMAN_IPCONFIG_TYPE_IPV4);
1090 int __connman_network_set_ipconfig(struct connman_network *network,
1091 struct connman_ipconfig *ipconfig_ipv4,
1092 struct connman_ipconfig *ipconfig_ipv6)
1094 enum connman_ipconfig_method method;
1097 if (ipconfig_ipv6) {
1098 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1101 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1102 case CONNMAN_IPCONFIG_METHOD_OFF:
1103 case CONNMAN_IPCONFIG_METHOD_AUTO:
1105 case CONNMAN_IPCONFIG_METHOD_FIXED:
1106 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1107 ret = manual_ipv6_set(network, ipconfig_ipv6);
1109 connman_network_set_error(network,
1110 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1114 case CONNMAN_IPCONFIG_METHOD_DHCP:
1119 if (ipconfig_ipv4) {
1120 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1123 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1124 case CONNMAN_IPCONFIG_METHOD_OFF:
1125 case CONNMAN_IPCONFIG_METHOD_FIXED:
1126 case CONNMAN_IPCONFIG_METHOD_AUTO:
1128 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1129 return manual_ipv4_set(network, ipconfig_ipv4);
1130 case CONNMAN_IPCONFIG_METHOD_DHCP:
1131 return dhcp_start(network);
1139 * connman_network_set_address:
1140 * @network: network structure
1141 * @address: binary address value
1142 * @size: binary address length
1144 * Set unique address value for network
1146 int connman_network_set_address(struct connman_network *network,
1147 const void *address, unsigned int size)
1149 const unsigned char *addr_octet = address;
1152 DBG("network %p size %d", network, size);
1157 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1158 addr_octet[0], addr_octet[1], addr_octet[2],
1159 addr_octet[3], addr_octet[4], addr_octet[5]);
1163 g_free(network->address);
1164 network->address = str;
1166 return connman_element_set_string(&network->element,
1167 "Address", network->address);
1171 * connman_network_set_name:
1172 * @network: network structure
1175 * Set display name value for network
1177 int connman_network_set_name(struct connman_network *network,
1180 DBG("network %p name %s", network, name);
1182 g_free(network->name);
1183 network->name = g_strdup(name);
1185 return connman_element_set_string(&network->element, "Name", name);
1189 * connman_network_set_strength:
1190 * @network: network structure
1191 * @strength: strength value
1193 * Set signal strength value for network
1195 int connman_network_set_strength(struct connman_network *network,
1196 connman_uint8_t strength)
1198 DBG("network %p strengh %d", network, strength);
1200 network->strength = strength;
1202 return connman_element_set_uint8(&network->element,
1203 "Strength", strength);
1207 * connman_network_set_roaming:
1208 * @network: network structure
1209 * @roaming: roaming state
1211 * Set roaming state for network
1213 int connman_network_set_roaming(struct connman_network *network,
1214 connman_bool_t roaming)
1216 DBG("network %p roaming %d", network, roaming);
1218 network->roaming = roaming;
1220 return connman_element_set_bool(&network->element,
1221 "Roaming", roaming);
1225 * connman_network_set_string:
1226 * @network: network structure
1227 * @key: unique identifier
1228 * @value: string value
1230 * Set string value for specific key
1232 int connman_network_set_string(struct connman_network *network,
1233 const char *key, const char *value)
1237 DBG("network %p key %s value %s", network, key, value);
1239 if (g_strcmp0(key, "Name") == 0)
1240 return connman_network_set_name(network, value);
1242 if (g_str_equal(key, "Address") == TRUE) {
1243 g_free(network->address);
1244 network->address = g_strdup(value);
1245 } else if (g_str_equal(key, "Node") == TRUE) {
1246 g_free(network->node);
1247 network->node = g_strdup(value);
1248 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1249 g_free(network->wifi.mode);
1250 network->wifi.mode = g_strdup(value);
1251 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1252 g_free(network->wifi.security);
1253 network->wifi.security = g_strdup(value);
1254 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1255 g_free(network->wifi.passphrase);
1256 network->wifi.passphrase = g_strdup(value);
1257 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1258 g_free(network->wifi.eap);
1259 network->wifi.eap = g_strdup(value);
1260 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1261 g_free(network->wifi.identity);
1262 network->wifi.identity = g_strdup(value);
1263 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1264 g_free(network->wifi.ca_cert_path);
1265 network->wifi.ca_cert_path = g_strdup(value);
1266 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1267 g_free(network->wifi.client_cert_path);
1268 network->wifi.client_cert_path = g_strdup(value);
1269 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1270 g_free(network->wifi.private_key_path);
1271 network->wifi.private_key_path = g_strdup(value);
1272 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1273 g_free(network->wifi.private_key_passphrase);
1274 network->wifi.private_key_passphrase = g_strdup(value);
1275 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1276 g_free(network->wifi.phase2_auth);
1277 network->wifi.phase2_auth = g_strdup(value);
1278 } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1279 g_free(network->wifi.pin_wps);
1280 network->wifi.pin_wps = g_strdup(value);
1283 err = connman_element_set_string(&network->element, key, value);
1287 if (network->driver == NULL)
1290 if (network->driver->setup)
1291 return network->driver->setup(network, key);
1297 * connman_network_get_string:
1298 * @network: network structure
1299 * @key: unique identifier
1301 * Get string value for specific key
1303 const char *connman_network_get_string(struct connman_network *network,
1306 DBG("network %p key %s", network, key);
1308 if (g_str_equal(key, "Address") == TRUE)
1309 return network->address;
1310 else if (g_str_equal(key, "Name") == TRUE)
1311 return network->name;
1312 else if (g_str_equal(key, "Node") == TRUE)
1313 return network->node;
1314 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1315 return network->wifi.mode;
1316 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1317 return network->wifi.security;
1318 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1319 return network->wifi.passphrase;
1320 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1321 return network->wifi.eap;
1322 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1323 return network->wifi.identity;
1324 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1325 return network->wifi.ca_cert_path;
1326 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1327 return network->wifi.client_cert_path;
1328 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1329 return network->wifi.private_key_path;
1330 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1331 return network->wifi.private_key_passphrase;
1332 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1333 return network->wifi.phase2_auth;
1334 else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1335 return network->wifi.pin_wps;
1337 return connman_element_get_string(&network->element, key);
1341 * connman_network_set_bool:
1342 * @network: network structure
1343 * @key: unique identifier
1344 * @value: boolean value
1346 * Set boolean value for specific key
1348 int connman_network_set_bool(struct connman_network *network,
1349 const char *key, connman_bool_t value)
1351 DBG("network %p key %s value %d", network, key, value);
1353 if (g_strcmp0(key, "Roaming") == 0)
1354 return connman_network_set_roaming(network, value);
1355 else if (g_strcmp0(key, "WiFi.WPS") == 0)
1356 network->wifi.wps = value;
1357 else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1358 network->wifi.use_wps = value;
1360 return connman_element_set_bool(&network->element, key, value);
1364 * connman_network_get_bool:
1365 * @network: network structure
1366 * @key: unique identifier
1368 * Get boolean value for specific key
1370 connman_bool_t connman_network_get_bool(struct connman_network *network,
1373 DBG("network %p key %s", network, key);
1375 if (g_str_equal(key, "Roaming") == TRUE)
1376 return network->roaming;
1377 else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1378 return network->wifi.wps;
1379 else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1380 return network->wifi.use_wps;
1382 return connman_element_get_bool(&network->element, key);
1386 * connman_network_set_uint8:
1387 * @network: network structure
1388 * @key: unique identifier
1389 * @value: integer value
1391 * Set integer value for specific key
1393 int connman_network_set_uint8(struct connman_network *network,
1394 const char *key, connman_uint8_t value)
1396 DBG("network %p key %s value %d", network, key, value);
1398 if (g_strcmp0(key, "Strength") == 0)
1399 return connman_network_set_strength(network, value);
1401 return connman_element_set_uint8(&network->element, key, value);
1405 * connman_network_get_uint8:
1406 * @network: network structure
1407 * @key: unique identifier
1409 * Get integer value for specific key
1411 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1414 DBG("network %p key %s", network, key);
1416 if (g_str_equal(key, "Strength") == TRUE)
1417 return network->strength;
1419 return connman_element_get_uint8(&network->element, key);
1423 * connman_network_set_uint16:
1424 * @network: network structure
1425 * @key: unique identifier
1426 * @value: integer value
1428 * Set integer value for specific key
1430 int connman_network_set_uint16(struct connman_network *network,
1431 const char *key, connman_uint16_t value)
1433 DBG("network %p key %s value %d", network, key, value);
1435 if (g_str_equal(key, "Frequency") == TRUE)
1436 network->frequency = value;
1437 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1438 network->wifi.channel = value;
1444 * connman_network_get_uint16:
1445 * @network: network structure
1446 * @key: unique identifier
1448 * Get integer value for specific key
1450 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1453 DBG("network %p key %s", network, key);
1455 if (g_str_equal(key, "Frequency") == TRUE)
1456 return network->frequency;
1457 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1458 return network->wifi.channel;
1464 * connman_network_set_blob:
1465 * @network: network structure
1466 * @key: unique identifier
1470 * Set binary blob value for specific key
1472 int connman_network_set_blob(struct connman_network *network,
1473 const char *key, const void *data, unsigned int size)
1475 DBG("network %p key %s size %d", network, key, size);
1477 if (g_strcmp0(key, "Address") == 0)
1478 return connman_network_set_address(network, data, size);
1480 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1481 g_free(network->wifi.ssid);
1482 network->wifi.ssid = g_try_malloc(size);
1483 if (network->wifi.ssid != NULL) {
1484 memcpy(network->wifi.ssid, data, size);
1485 network->wifi.ssid_len = size;
1487 network->wifi.ssid_len = 0;
1490 return connman_element_set_blob(&network->element, key, data, size);
1494 * connman_network_get_blob:
1495 * @network: network structure
1496 * @key: unique identifier
1497 * @size: pointer to blob size
1499 * Get binary blob value for specific key
1501 const void *connman_network_get_blob(struct connman_network *network,
1502 const char *key, unsigned int *size)
1504 DBG("network %p key %s", network, key);
1506 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1508 *size = network->wifi.ssid_len;
1509 return network->wifi.ssid;
1512 return connman_element_get_blob(&network->element, key, size);
1515 void __connman_network_set_device(struct connman_network *network,
1516 struct connman_device *device)
1518 network->device = device;
1522 * connman_network_get_device:
1523 * @network: network structure
1525 * Get parent device of network
1527 struct connman_device *connman_network_get_device(struct connman_network *network)
1529 return network->device;
1533 * connman_network_get_data:
1534 * @network: network structure
1536 * Get private network data pointer
1538 void *connman_network_get_data(struct connman_network *network)
1540 return network->driver_data;
1544 * connman_network_set_data:
1545 * @network: network structure
1546 * @data: data pointer
1548 * Set private network data pointer
1550 void connman_network_set_data(struct connman_network *network, void *data)
1552 network->driver_data = data;
1555 void connman_network_update(struct connman_network *network)
1557 switch (network->type) {
1558 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1559 case CONNMAN_NETWORK_TYPE_VENDOR:
1561 case CONNMAN_NETWORK_TYPE_ETHERNET:
1562 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1563 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1564 case CONNMAN_NETWORK_TYPE_CELLULAR:
1565 case CONNMAN_NETWORK_TYPE_WIFI:
1566 case CONNMAN_NETWORK_TYPE_WIMAX:
1570 if (network->group != NULL)
1571 __connman_service_update_from_network(network);
1576 static gboolean match_driver(struct connman_network *network,
1577 struct connman_network_driver *driver)
1579 if (network->type == driver->type ||
1580 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1586 static int network_probe(struct connman_element *element)
1588 struct connman_network *network = element->network;
1591 DBG("element %p name %s", element, element->name);
1593 if (network == NULL)
1596 for (list = driver_list; list; list = list->next) {
1597 struct connman_network_driver *driver = list->data;
1599 if (match_driver(network, driver) == FALSE)
1602 DBG("driver %p name %s", driver, driver->name);
1604 if (driver->probe(network) == 0) {
1605 network->driver = driver;
1610 if (network->driver == NULL)
1613 switch (network->type) {
1614 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1615 case CONNMAN_NETWORK_TYPE_VENDOR:
1617 case CONNMAN_NETWORK_TYPE_ETHERNET:
1618 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1619 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1620 case CONNMAN_NETWORK_TYPE_CELLULAR:
1621 case CONNMAN_NETWORK_TYPE_WIFI:
1622 case CONNMAN_NETWORK_TYPE_WIMAX:
1623 if (network->group != NULL)
1624 __connman_profile_add_network(network);
1631 static void network_remove(struct connman_element *element)
1633 struct connman_network *network = element->network;
1635 DBG("element %p name %s", element, element->name);
1637 if (network == NULL)
1640 if (network->driver == NULL)
1643 switch (network->type) {
1644 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1645 case CONNMAN_NETWORK_TYPE_VENDOR:
1647 case CONNMAN_NETWORK_TYPE_ETHERNET:
1648 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1649 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1650 case CONNMAN_NETWORK_TYPE_CELLULAR:
1651 case CONNMAN_NETWORK_TYPE_WIFI:
1652 case CONNMAN_NETWORK_TYPE_WIMAX:
1653 if (network->group != NULL) {
1654 __connman_profile_remove_network(network);
1656 g_free(network->group);
1657 network->group = NULL;
1662 if (network->driver->remove)
1663 network->driver->remove(network);
1666 static void network_change(struct connman_element *element)
1668 struct connman_network *network = element->network;
1670 DBG("element %p name %s", element, element->name);
1672 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1675 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1678 if (network->connected == FALSE)
1681 connman_element_unregister_children(element);
1683 connman_device_set_disconnected(network->device, TRUE);
1685 if (network->driver && network->driver->disconnect) {
1686 network->driver->disconnect(network);
1690 network->connected = FALSE;
1693 static struct connman_driver network_driver = {
1695 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1696 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1697 .probe = network_probe,
1698 .remove = network_remove,
1699 .change = network_change,
1702 int __connman_network_init(void)
1706 return connman_driver_register(&network_driver);
1709 void __connman_network_cleanup(void)
1713 connman_driver_unregister(&network_driver);