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:
100 static DBusMessage *get_properties(DBusConnection *conn,
101 DBusMessage *msg, void *data)
103 struct connman_network *network = data;
105 DBusMessageIter array, dict;
107 DBG("conn %p", conn);
109 reply = dbus_message_new_method_return(msg);
113 dbus_message_iter_init_append(reply, &array);
115 connman_dbus_dict_open(&array, &dict);
117 if (network->device) {
118 const char *path = connman_device_get_path(network->device);
120 connman_dbus_dict_append_basic(&dict, "Device",
121 DBUS_TYPE_OBJECT_PATH, &path);
124 if (network->address != NULL)
125 connman_dbus_dict_append_basic(&dict, "Address",
126 DBUS_TYPE_STRING, &network->address);
128 if (network->name != NULL)
129 connman_dbus_dict_append_basic(&dict, "Name",
130 DBUS_TYPE_STRING, &network->name);
132 connman_dbus_dict_append_basic(&dict, "Connected",
133 DBUS_TYPE_BOOLEAN, &network->connected);
135 if (network->strength > 0)
136 connman_dbus_dict_append_basic(&dict, "Strength",
137 DBUS_TYPE_BYTE, &network->strength);
139 if (network->frequency > 0)
140 connman_dbus_dict_append_basic(&dict, "Frequency",
141 DBUS_TYPE_UINT16, &network->frequency);
143 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
144 connman_dbus_dict_append_fixed_array(&dict, "WiFi.SSID",
145 DBUS_TYPE_BYTE, &network->wifi.ssid,
146 network->wifi.ssid_len);
148 if (network->wifi.mode != NULL)
149 connman_dbus_dict_append_basic(&dict, "WiFi.Mode",
150 DBUS_TYPE_STRING, &network->wifi.mode);
152 if (network->wifi.channel > 0)
153 connman_dbus_dict_append_basic(&dict, "WiFi.Channel",
154 DBUS_TYPE_UINT16, &network->wifi.channel);
156 if (network->wifi.security != NULL) {
157 connman_dbus_dict_append_basic(&dict, "WiFi.Security",
158 DBUS_TYPE_STRING, &network->wifi.security);
160 if (g_strcmp0(network->wifi.security, "ieee8021x") == 0 &&
161 network->wifi.eap != NULL)
162 connman_dbus_dict_append_basic(&dict, "WiFi.EAP",
163 DBUS_TYPE_STRING, &network->wifi.eap);
167 if (network->wifi.passphrase != NULL)
168 connman_dbus_dict_append_basic(&dict, "WiFi.Passphrase",
169 DBUS_TYPE_STRING, &network->wifi.passphrase);
171 connman_dbus_dict_close(&array, &dict);
176 static GDBusMethodTable network_methods[] = {
177 { "GetProperties", "", "a{sv}", get_properties },
181 static GDBusSignalTable network_signals[] = {
182 { "PropertyChanged", "sv" },
186 static DBusConnection *connection;
188 static void append_networks(DBusMessageIter *iter, void *user_data)
190 struct connman_device *device = user_data;
192 __connman_element_list((struct connman_element *) device,
193 CONNMAN_ELEMENT_TYPE_NETWORK, iter);
196 static void emit_networks_signal(struct connman_device *device)
198 const char *path = connman_device_get_path(device);
200 connman_dbus_property_changed_array(path,
201 CONNMAN_DEVICE_INTERFACE, "Networks",
202 DBUS_TYPE_OBJECT_PATH, append_networks, device);
205 static int register_interface(struct connman_element *element)
207 struct connman_network *network = element->network;
209 DBG("element %p name %s", element, element->name);
211 if (g_dbus_register_interface(connection, element->path,
212 CONNMAN_NETWORK_INTERFACE,
213 network_methods, network_signals,
214 NULL, network, NULL) == FALSE) {
215 connman_error("Failed to register %s network", element->path);
219 network->registered = TRUE;
221 emit_networks_signal(network->device);
226 static void unregister_interface(struct connman_element *element)
228 struct connman_network * network = element->network;
230 DBG("element %p name %s", element, element->name);
232 network->registered = FALSE;
234 if (network->device != NULL)
235 emit_networks_signal(network->device);
237 g_dbus_unregister_interface(connection, element->path,
238 CONNMAN_NETWORK_INTERFACE);
241 connman_bool_t __connman_network_has_driver(struct connman_network *network)
243 if (network == NULL || network->driver == NULL)
246 return network->registered;
249 static GSList *driver_list = NULL;
251 static gint compare_priority(gconstpointer a, gconstpointer b)
253 const struct connman_network_driver *driver1 = a;
254 const struct connman_network_driver *driver2 = b;
256 return driver2->priority - driver1->priority;
260 * connman_network_driver_register:
261 * @driver: network driver definition
263 * Register a new network driver
265 * Returns: %0 on success
267 int connman_network_driver_register(struct connman_network_driver *driver)
269 DBG("driver %p name %s", driver, driver->name);
271 driver_list = g_slist_insert_sorted(driver_list, driver,
278 * connman_network_driver_unregister:
279 * @driver: network driver definition
281 * Remove a previously registered network driver
283 void connman_network_driver_unregister(struct connman_network_driver *driver)
285 DBG("driver %p name %s", driver, driver->name);
287 driver_list = g_slist_remove(driver_list, driver);
290 static void network_destruct(struct connman_element *element)
292 struct connman_network *network = element->network;
294 DBG("element %p name %s", element, element->name);
296 g_free(network->wifi.ssid);
297 g_free(network->wifi.mode);
298 g_free(network->wifi.security);
299 g_free(network->wifi.passphrase);
301 g_free(network->group);
302 g_free(network->node);
303 g_free(network->name);
304 g_free(network->address);
305 g_free(network->identifier);
307 if (network->ipconfig) {
308 connman_ipconfig_unref(network->ipconfig);
309 network->ipconfig = NULL;
312 network->device = NULL;
316 * connman_network_create:
317 * @identifier: network identifier (for example an unqiue name)
319 * Allocate a new network and assign the #identifier to it.
321 * Returns: a newly-allocated #connman_network structure
323 struct connman_network *connman_network_create(const char *identifier,
324 enum connman_network_type type)
326 struct connman_network *network;
327 connman_uint8_t strength = 0;
331 DBG("identifier %s type %d", identifier, type);
333 network = g_try_new0(struct connman_network, 1);
337 DBG("network %p", network);
339 __connman_element_initialize(&network->element);
341 //temp = connman_dbus_encode_string(identifier);
342 if (identifier == NULL) {
343 temp = g_strdup_printf("hidden_%d", hidden_counter++);
344 network->hidden = TRUE;
346 temp = g_strdup(identifier);
353 network->element.name = temp;
354 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
356 network->element.network = network;
357 network->element.destruct = network_destruct;
359 str = type2string(type);
361 connman_element_set_string(&network->element, "Type", str);
363 connman_element_set_uint8(&network->element, "Strength", strength);
365 network->type = type;
366 network->identifier = g_strdup(temp);
372 * connman_network_ref:
373 * @network: network structure
375 * Increase reference counter of network
377 struct connman_network *connman_network_ref(struct connman_network *network)
379 if (connman_element_ref(&network->element) == NULL)
386 * connman_network_unref:
387 * @network: network structure
389 * Decrease reference counter of network
391 void connman_network_unref(struct connman_network *network)
393 connman_element_unref(&network->element);
396 const char *__connman_network_get_type(struct connman_network *network)
398 return type2string(network->type);
402 * connman_network_get_type:
403 * @network: network structure
405 * Get type of network
407 enum connman_network_type connman_network_get_type(struct connman_network *network)
409 return network->type;
413 * connman_network_get_identifier:
414 * @network: network structure
416 * Get identifier of network
418 const char *connman_network_get_identifier(struct connman_network *network)
420 return network->identifier;
424 * connman_network_get_path:
425 * @network: network structure
427 * Get path name of network
429 const char *connman_network_get_path(struct connman_network *network)
431 return network->element.path;
435 * connman_network_set_index:
436 * @network: network structure
437 * @index: index number
439 * Set index number of network
441 void connman_network_set_index(struct connman_network *network, int index)
443 struct connman_service *service;
444 struct connman_ipconfig *ipconfig;
446 service = __connman_service_lookup_from_network(network);
450 ipconfig = __connman_service_get_ipconfig(service);
452 if (network->element.index < 0 && ipconfig == NULL)
454 * This is needed for plugins that havent set their ipconfig
455 * layer yet, due to not being able to get a network index
456 * prior to creating a service.
458 __connman_service_create_ipconfig(service, index);
460 /* If index changed, the index of ipconfig must be reset. */
461 if (ipconfig == NULL)
464 __connman_ipconfig_set_index(ipconfig, index);
468 network->element.index = index;
472 * connman_network_get_index:
473 * @network: network structure
475 * Get index number of network
477 int connman_network_get_index(struct connman_network *network)
479 return network->element.index;
483 * connman_network_get_element:
484 * @network: network structure
486 * Get connman_element of network
488 struct connman_element *connman_network_get_element(
489 struct connman_network *network)
491 return &network->element;
495 * connman_network_set_protocol:
496 * @network: network structure
497 * @protocol: network protocol
499 * Change protocol of network
501 void connman_network_set_protocol(struct connman_network *network,
502 enum connman_network_protocol protocol)
504 network->protocol = protocol;
508 * connman_network_set_group:
509 * @network: network structure
512 * Set group name for automatic clustering
514 void connman_network_set_group(struct connman_network *network,
517 switch (network->type) {
518 case CONNMAN_NETWORK_TYPE_UNKNOWN:
519 case CONNMAN_NETWORK_TYPE_VENDOR:
521 case CONNMAN_NETWORK_TYPE_ETHERNET:
522 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
523 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
524 case CONNMAN_NETWORK_TYPE_CELLULAR:
525 case CONNMAN_NETWORK_TYPE_WIFI:
526 case CONNMAN_NETWORK_TYPE_WIMAX:
530 if (g_strcmp0(network->group, group) == 0) {
531 if (group != NULL && network->registered)
532 __connman_profile_update_network(network);
536 if (network->group != NULL) {
537 if (network->registered)
538 __connman_profile_remove_network(network);
540 g_free(network->group);
543 network->group = g_strdup(group);
545 if (network->group != NULL) {
546 if (network->registered)
547 __connman_profile_add_network(network);
552 * connman_network_get_group:
553 * @network: network structure
555 * Get group name for automatic clustering
557 const char *connman_network_get_group(struct connman_network *network)
559 return network->group;
562 const char *__connman_network_get_ident(struct connman_network *network)
564 if (network->device == NULL)
567 return connman_device_get_ident(network->device);
570 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
572 switch (network->type) {
573 case CONNMAN_NETWORK_TYPE_UNKNOWN:
574 case CONNMAN_NETWORK_TYPE_VENDOR:
575 case CONNMAN_NETWORK_TYPE_ETHERNET:
576 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
577 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
578 case CONNMAN_NETWORK_TYPE_CELLULAR:
579 case CONNMAN_NETWORK_TYPE_WIMAX:
581 case CONNMAN_NETWORK_TYPE_WIFI:
582 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
584 if (network->strength > 0 && network->strength < 20)
592 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
594 return network->connecting;
598 * connman_network_set_available:
599 * @network: network structure
600 * @available: availability state
602 * Change availability state of network (in range)
604 int connman_network_set_available(struct connman_network *network,
605 connman_bool_t available)
607 DBG("network %p available %d", network, available);
609 if (network->available == available)
612 network->available = available;
618 * connman_network_get_available:
619 * @network: network structure
621 * Get network available setting
623 connman_bool_t connman_network_get_available(struct connman_network *network)
625 if (network->hidden == TRUE)
628 return network->available;
632 * connman_network_set_associating:
633 * @network: network structure
634 * @associating: associating state
636 * Change associating state of network
638 int connman_network_set_associating(struct connman_network *network,
639 connman_bool_t associating)
641 DBG("network %p associating %d", network, associating);
643 if (network->associating == associating)
646 network->associating = associating;
648 if (associating == TRUE) {
649 struct connman_service *service;
651 service = __connman_service_lookup_from_network(network);
652 __connman_service_indicate_state(service,
653 CONNMAN_SERVICE_STATE_ASSOCIATION);
659 static void set_associate_error(struct connman_network *network)
661 struct connman_service *service;
663 if (network->associating == FALSE)
666 network->associating = FALSE;
668 service = __connman_service_lookup_from_network(network);
670 __connman_service_indicate_state(service,
671 CONNMAN_SERVICE_STATE_FAILURE);
674 static void set_configure_error(struct connman_network *network)
676 struct connman_service *service;
678 network->connecting = FALSE;
680 service = __connman_service_lookup_from_network(network);
682 __connman_service_indicate_state(service,
683 CONNMAN_SERVICE_STATE_FAILURE);
686 void connman_network_set_method(struct connman_network *network,
687 enum connman_ipconfig_method method)
689 struct connman_service *service;
690 struct connman_ipconfig *ipconfig;
692 network->element.ipv4.method = method;
694 service = __connman_service_lookup_from_network(network);
698 ipconfig = __connman_service_get_ipconfig(service);
699 if (ipconfig == NULL)
702 connman_ipconfig_set_method(ipconfig, method);
705 void connman_network_set_error(struct connman_network *network,
706 enum connman_network_error error)
708 DBG("nework %p, error %d", network, error);
710 network->connecting = FALSE;
713 case CONNMAN_NETWORK_ERROR_UNKNOWN:
715 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
716 set_associate_error(network);
718 case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
719 set_configure_error(network);
724 static void set_configuration(struct connman_network *network)
726 struct connman_service *service;
728 DBG("network %p", network);
730 __connman_device_increase_connections(network->device);
732 __connman_device_set_network(network->device, network);
734 connman_device_set_disconnected(network->device, FALSE);
736 service = __connman_service_lookup_from_network(network);
737 __connman_service_indicate_state(service,
738 CONNMAN_SERVICE_STATE_CONFIGURATION);
741 static int set_connected_fixed(struct connman_network *network)
743 struct connman_service *service;
744 struct connman_element *parent, *element;
748 service = __connman_service_lookup_from_network(network);
750 parent = connman_network_get_element(network);
752 set_configuration(network);
754 if (parent->ipv4.address == NULL)
757 if (parent->ipv4.netmask == NULL)
760 element = connman_element_create(NULL);
761 if (element == NULL) {
762 connman_error("Can not create connman_element");
766 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
767 element->index = parent->index;
769 if (connman_element_register(element, parent) < 0) {
770 connman_error("Can not register connman_element");
774 network->connecting = FALSE;
776 connman_network_set_associating(network, FALSE);
781 static void set_connected_manual(struct connman_network *network)
783 struct connman_service *service;
784 struct connman_ipconfig *ipconfig;
785 const char *nameserver = NULL;
788 DBG("network %p", network);
790 service = __connman_service_lookup_from_network(network);
792 ipconfig = __connman_service_get_ipconfig(service);
794 set_configuration(network);
796 err = __connman_ipconfig_set_address(ipconfig);
798 connman_network_set_error(network,
799 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
803 connman_element_get_value(&network->element,
804 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
805 if (nameserver != NULL)
806 __connman_service_append_nameserver(service, nameserver);
808 __connman_ipconfig_set_gateway(ipconfig, &network->element);
810 network->connecting = FALSE;
812 connman_network_set_associating(network, FALSE);
814 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
817 static int set_connected_dhcp(struct connman_network *network)
819 struct connman_element *element;
822 DBG("network %p", network);
824 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
827 element = connman_element_create(NULL);
831 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
832 element->index = network->element.index;
834 error = connman_element_register(element, &network->element);
836 connman_element_unref(element);
840 set_configuration(network);
845 static int manual_ipv6_set(struct connman_network *network,
846 struct connman_ipconfig *ipconfig_ipv6)
848 struct connman_service *service;
851 service = __connman_service_lookup_from_network(network);
855 err = __connman_ipconfig_set_address(ipconfig_ipv6);
857 connman_network_set_error(network,
858 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
863 * READY state will be indicated by IPV4 setting
864 * gateway will be set by IPV4 setting
870 static gboolean set_connected(gpointer user_data)
872 struct connman_network *network = user_data;
873 struct connman_service *service;
874 struct connman_ipconfig *ipconfig;
875 enum connman_ipconfig_method method;
877 service = __connman_service_lookup_from_network(network);
879 ipconfig = __connman_service_get_ipconfig(service);
881 method = __connman_ipconfig_get_method(ipconfig);
883 DBG("method %d", method);
885 if (network->connected == TRUE) {
886 enum connman_ipconfig_method ipv6_method;
887 struct connman_ipconfig *ipv6config;
890 ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
891 ipv6_method = __connman_ipconfig_get_method(ipv6config);
892 switch (ipv6_method) {
893 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
894 case CONNMAN_IPCONFIG_METHOD_OFF:
896 case CONNMAN_IPCONFIG_METHOD_FIXED:
897 case CONNMAN_IPCONFIG_METHOD_MANUAL:
898 ret = manual_ipv6_set(network, ipv6config);
900 connman_network_set_error(network,
901 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
905 case CONNMAN_IPCONFIG_METHOD_DHCP:
910 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
911 case CONNMAN_IPCONFIG_METHOD_OFF:
913 case CONNMAN_IPCONFIG_METHOD_FIXED:
914 if (set_connected_fixed(network) < 0) {
915 connman_network_set_error(network,
916 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
920 case CONNMAN_IPCONFIG_METHOD_MANUAL:
921 set_connected_manual(network);
923 case CONNMAN_IPCONFIG_METHOD_DHCP:
924 if (set_connected_dhcp(network) < 0) {
925 connman_network_set_error(network,
926 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
932 struct connman_service *service;
934 connman_element_unregister_children(&network->element);
936 __connman_device_set_network(network->device, NULL);
937 network->hidden = FALSE;
939 __connman_device_decrease_connections(network->device);
941 service = __connman_service_lookup_from_network(network);
943 __connman_service_indicate_state(service,
944 CONNMAN_SERVICE_STATE_IDLE);
947 network->connecting = FALSE;
949 connman_network_set_associating(network, FALSE);
955 * connman_network_set_connected:
956 * @network: network structure
957 * @connected: connected state
959 * Change connected state of network
961 int connman_network_set_connected(struct connman_network *network,
962 connman_bool_t connected)
964 DBG("network %p connected %d", network, connected);
966 if ((network->connecting == TRUE || network->associating == TRUE) &&
967 connected == FALSE) {
968 connman_element_set_error(&network->element,
969 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
970 __connman_network_disconnect(network);
973 if (network->connected == connected)
976 network->connected = connected;
978 if (network->registered == FALSE) {
979 g_idle_add(set_connected, network);
983 connman_dbus_property_changed_basic(network->element.path,
984 CONNMAN_NETWORK_INTERFACE, "Connected",
985 DBUS_TYPE_BOOLEAN, &connected);
987 set_connected(network);
993 * connman_network_get_connected:
994 * @network: network structure
996 * Get network connection status
998 connman_bool_t connman_network_get_connected(struct connman_network *network)
1000 return network->connected;
1004 * connman_network_get_associating:
1005 * @network: network structure
1007 * Get network associating status
1009 connman_bool_t connman_network_get_associating(struct connman_network *network)
1011 return network->associating;
1015 * __connman_network_connect:
1016 * @network: network structure
1020 int __connman_network_connect(struct connman_network *network)
1022 struct connman_service *service;
1025 DBG("network %p", network);
1027 if (network->connected == TRUE)
1030 if (network->connecting == TRUE || network->associating == TRUE)
1033 if (network->driver == NULL)
1036 if (network->driver->connect == NULL)
1039 if (network->device == NULL)
1042 __connman_device_disconnect(network->device);
1044 network->connecting = TRUE;
1046 service = __connman_service_lookup_from_network(network);
1048 err = network->driver->connect(network);
1050 if (err == -EINPROGRESS)
1051 connman_network_set_associating(network, TRUE);
1053 network->connecting = FALSE;
1054 network->hidden = FALSE;
1060 network->connected = TRUE;
1061 set_connected(network);
1067 * __connman_network_disconnect:
1068 * @network: network structure
1070 * Disconnect network
1072 int __connman_network_disconnect(struct connman_network *network)
1076 DBG("network %p", network);
1078 if (network->connected == FALSE && network->connecting == FALSE &&
1079 network->associating == FALSE)
1082 if (network->driver == NULL)
1085 if (network->driver->disconnect == NULL)
1088 network->connecting = FALSE;
1090 err = network->driver->disconnect(network);
1092 connman_network_set_connected(network, FALSE);
1093 set_connected(network);
1099 static int dhcp_start(struct connman_network *network)
1101 struct connman_element *element;
1104 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1107 element = connman_element_create(NULL);
1108 if (element == NULL)
1111 element->type = CONNMAN_ELEMENT_TYPE_DHCP;
1112 element->index = network->element.index;
1114 error = connman_element_register(element, &network->element);
1116 connman_element_unref(element);
1123 static int dhcp_stop(struct connman_network *network)
1125 if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1128 connman_element_unregister_children_type(&network->element,
1129 CONNMAN_ELEMENT_TYPE_CONNECTION);
1130 connman_element_unregister_children_type(&network->element,
1131 CONNMAN_ELEMENT_TYPE_IPV4);
1132 connman_element_unregister_children_type(&network->element,
1133 CONNMAN_ELEMENT_TYPE_DHCP);
1138 static int manual_ipv4_set(struct connman_network *network,
1139 struct connman_ipconfig *ipconfig)
1141 struct connman_service *service;
1144 service = __connman_service_lookup_from_network(network);
1145 if (service == NULL)
1148 err = __connman_ipconfig_set_address(ipconfig);
1150 connman_network_set_error(network,
1151 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1155 __connman_ipconfig_set_gateway(ipconfig, &network->element);
1157 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
1162 int __connman_network_clear_ipconfig(struct connman_network *network,
1163 struct connman_ipconfig *ipconfig)
1165 struct connman_service *service;
1166 enum connman_ipconfig_method method;
1168 service = __connman_service_lookup_from_network(network);
1169 if (service == NULL)
1172 method = __connman_ipconfig_get_method(ipconfig);
1175 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1176 case CONNMAN_IPCONFIG_METHOD_OFF:
1177 case CONNMAN_IPCONFIG_METHOD_FIXED:
1179 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1180 connman_element_unregister_children_type(&network->element,
1181 CONNMAN_ELEMENT_TYPE_CONNECTION);
1182 __connman_ipconfig_clear_address(ipconfig);
1184 case CONNMAN_IPCONFIG_METHOD_DHCP:
1189 __connman_service_indicate_state(service,
1190 CONNMAN_SERVICE_STATE_CONFIGURATION);
1195 int __connman_network_set_ipconfig(struct connman_network *network,
1196 struct connman_ipconfig *ipconfig)
1198 struct connman_ipconfig *ipv6config;
1199 enum connman_ipconfig_method method;
1202 ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
1203 method = __connman_ipconfig_get_method(ipv6config);
1205 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1206 case CONNMAN_IPCONFIG_METHOD_OFF:
1208 case CONNMAN_IPCONFIG_METHOD_FIXED:
1209 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1210 ret = manual_ipv6_set(network, ipv6config);
1212 connman_network_set_error(network,
1213 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1217 case CONNMAN_IPCONFIG_METHOD_DHCP:
1221 method = __connman_ipconfig_get_method(ipconfig);
1224 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1225 case CONNMAN_IPCONFIG_METHOD_OFF:
1226 case CONNMAN_IPCONFIG_METHOD_FIXED:
1228 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1229 return manual_ipv4_set(network, ipconfig);
1230 case CONNMAN_IPCONFIG_METHOD_DHCP:
1231 return dhcp_start(network);
1238 * connman_network_set_address:
1239 * @network: network structure
1240 * @address: binary address value
1241 * @size: binary address length
1243 * Set unique address value for network
1245 int connman_network_set_address(struct connman_network *network,
1246 const void *address, unsigned int size)
1248 const unsigned char *addr_octet = address;
1251 DBG("network %p size %d", network, size);
1256 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1257 addr_octet[0], addr_octet[1], addr_octet[2],
1258 addr_octet[3], addr_octet[4], addr_octet[5]);
1262 g_free(network->address);
1263 network->address = str;
1265 return connman_element_set_string(&network->element,
1266 "Address", network->address);
1270 * connman_network_set_name:
1271 * @network: network structure
1274 * Set display name value for network
1276 int connman_network_set_name(struct connman_network *network,
1279 DBG("network %p name %s", network, name);
1281 g_free(network->name);
1282 network->name = g_strdup(name);
1284 return connman_element_set_string(&network->element, "Name", name);
1288 * connman_network_set_strength:
1289 * @network: network structure
1290 * @strength: strength value
1292 * Set signal strength value for network
1294 int connman_network_set_strength(struct connman_network *network,
1295 connman_uint8_t strength)
1297 DBG("network %p strengh %d", network, strength);
1299 network->strength = strength;
1301 return connman_element_set_uint8(&network->element,
1302 "Strength", strength);
1306 * connman_network_set_roaming:
1307 * @network: network structure
1308 * @roaming: roaming state
1310 * Set roaming state for network
1312 int connman_network_set_roaming(struct connman_network *network,
1313 connman_bool_t roaming)
1315 DBG("network %p roaming %d", network, roaming);
1317 network->roaming = roaming;
1319 return connman_element_set_bool(&network->element,
1320 "Roaming", roaming);
1324 * connman_network_set_string:
1325 * @network: network structure
1326 * @key: unique identifier
1327 * @value: string value
1329 * Set string value for specific key
1331 int connman_network_set_string(struct connman_network *network,
1332 const char *key, const char *value)
1336 DBG("network %p key %s value %s", network, key, value);
1338 if (g_strcmp0(key, "Name") == 0)
1339 return connman_network_set_name(network, value);
1341 if (g_str_equal(key, "Address") == TRUE) {
1342 g_free(network->address);
1343 network->address = g_strdup(value);
1344 } else if (g_str_equal(key, "Node") == TRUE) {
1345 g_free(network->node);
1346 network->node = g_strdup(value);
1347 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1348 g_free(network->wifi.mode);
1349 network->wifi.mode = g_strdup(value);
1350 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1351 g_free(network->wifi.security);
1352 network->wifi.security = g_strdup(value);
1353 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1354 g_free(network->wifi.passphrase);
1355 network->wifi.passphrase = g_strdup(value);
1356 } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1357 g_free(network->wifi.eap);
1358 network->wifi.eap = g_strdup(value);
1359 } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1360 g_free(network->wifi.identity);
1361 network->wifi.identity = g_strdup(value);
1362 } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1363 g_free(network->wifi.ca_cert_path);
1364 network->wifi.ca_cert_path = g_strdup(value);
1365 } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1366 g_free(network->wifi.client_cert_path);
1367 network->wifi.client_cert_path = g_strdup(value);
1368 } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1369 g_free(network->wifi.private_key_path);
1370 network->wifi.private_key_path = g_strdup(value);
1371 } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1372 g_free(network->wifi.private_key_passphrase);
1373 network->wifi.private_key_passphrase = g_strdup(value);
1374 } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1375 g_free(network->wifi.phase2_auth);
1376 network->wifi.phase2_auth = g_strdup(value);
1379 err = connman_element_set_string(&network->element, key, value);
1383 if (network->driver == NULL)
1386 if (network->driver->setup)
1387 return network->driver->setup(network, key);
1393 * connman_network_get_string:
1394 * @network: network structure
1395 * @key: unique identifier
1397 * Get string value for specific key
1399 const char *connman_network_get_string(struct connman_network *network,
1402 DBG("network %p key %s", network, key);
1404 if (g_str_equal(key, "Address") == TRUE)
1405 return network->address;
1406 else if (g_str_equal(key, "Name") == TRUE)
1407 return network->name;
1408 else if (g_str_equal(key, "Node") == TRUE)
1409 return network->node;
1410 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1411 return network->wifi.mode;
1412 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1413 return network->wifi.security;
1414 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1415 return network->wifi.passphrase;
1416 else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1417 return network->wifi.eap;
1418 else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1419 return network->wifi.identity;
1420 else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1421 return network->wifi.ca_cert_path;
1422 else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1423 return network->wifi.client_cert_path;
1424 else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1425 return network->wifi.private_key_path;
1426 else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1427 return network->wifi.private_key_passphrase;
1428 else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1429 return network->wifi.phase2_auth;
1431 return connman_element_get_string(&network->element, key);
1435 * connman_network_set_bool:
1436 * @network: network structure
1437 * @key: unique identifier
1438 * @value: boolean value
1440 * Set boolean value for specific key
1442 int connman_network_set_bool(struct connman_network *network,
1443 const char *key, connman_bool_t value)
1445 DBG("network %p key %s value %d", network, key, value);
1447 if (g_strcmp0(key, "Roaming") == 0)
1448 return connman_network_set_roaming(network, value);
1450 return connman_element_set_bool(&network->element, key, value);
1454 * connman_network_get_bool:
1455 * @network: network structure
1456 * @key: unique identifier
1458 * Get boolean value for specific key
1460 connman_bool_t connman_network_get_bool(struct connman_network *network,
1463 DBG("network %p key %s", network, key);
1465 if (g_str_equal(key, "Roaming") == TRUE)
1466 return network->roaming;
1468 return connman_element_get_bool(&network->element, key);
1472 * connman_network_set_uint8:
1473 * @network: network structure
1474 * @key: unique identifier
1475 * @value: integer value
1477 * Set integer value for specific key
1479 int connman_network_set_uint8(struct connman_network *network,
1480 const char *key, connman_uint8_t value)
1482 DBG("network %p key %s value %d", network, key, value);
1484 if (g_strcmp0(key, "Strength") == 0)
1485 return connman_network_set_strength(network, value);
1487 return connman_element_set_uint8(&network->element, key, value);
1491 * connman_network_get_uint8:
1492 * @network: network structure
1493 * @key: unique identifier
1495 * Get integer value for specific key
1497 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1500 DBG("network %p key %s", network, key);
1502 if (g_str_equal(key, "Strength") == TRUE)
1503 return network->strength;
1505 return connman_element_get_uint8(&network->element, key);
1509 * connman_network_set_uint16:
1510 * @network: network structure
1511 * @key: unique identifier
1512 * @value: integer value
1514 * Set integer value for specific key
1516 int connman_network_set_uint16(struct connman_network *network,
1517 const char *key, connman_uint16_t value)
1519 DBG("network %p key %s value %d", network, key, value);
1521 if (g_str_equal(key, "Frequency") == TRUE)
1522 network->frequency = value;
1523 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1524 network->wifi.channel = value;
1530 * connman_network_get_uint16:
1531 * @network: network structure
1532 * @key: unique identifier
1534 * Get integer value for specific key
1536 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1539 DBG("network %p key %s", network, key);
1541 if (g_str_equal(key, "Frequency") == TRUE)
1542 return network->frequency;
1543 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1544 return network->wifi.channel;
1550 * connman_network_set_blob:
1551 * @network: network structure
1552 * @key: unique identifier
1556 * Set binary blob value for specific key
1558 int connman_network_set_blob(struct connman_network *network,
1559 const char *key, const void *data, unsigned int size)
1561 DBG("network %p key %s size %d", network, key, size);
1563 if (g_strcmp0(key, "Address") == 0)
1564 return connman_network_set_address(network, data, size);
1566 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1567 g_free(network->wifi.ssid);
1568 network->wifi.ssid = g_try_malloc(size);
1569 if (network->wifi.ssid != NULL) {
1570 memcpy(network->wifi.ssid, data, size);
1571 network->wifi.ssid_len = size;
1573 network->wifi.ssid_len = 0;
1576 return connman_element_set_blob(&network->element, key, data, size);
1580 * connman_network_get_blob:
1581 * @network: network structure
1582 * @key: unique identifier
1583 * @size: pointer to blob size
1585 * Get binary blob value for specific key
1587 const void *connman_network_get_blob(struct connman_network *network,
1588 const char *key, unsigned int *size)
1590 DBG("network %p key %s", network, key);
1592 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1594 *size = network->wifi.ssid_len;
1595 return network->wifi.ssid;
1598 return connman_element_get_blob(&network->element, key, size);
1601 void __connman_network_set_device(struct connman_network *network,
1602 struct connman_device *device)
1604 network->device = device;
1608 * connman_network_get_device:
1609 * @network: network structure
1611 * Get parent device of network
1613 struct connman_device *connman_network_get_device(struct connman_network *network)
1615 return network->device;
1619 * connman_network_get_data:
1620 * @network: network structure
1622 * Get private network data pointer
1624 void *connman_network_get_data(struct connman_network *network)
1626 return network->driver_data;
1630 * connman_network_set_data:
1631 * @network: network structure
1632 * @data: data pointer
1634 * Set private network data pointer
1636 void connman_network_set_data(struct connman_network *network, void *data)
1638 network->driver_data = data;
1641 void connman_network_update(struct connman_network *network)
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:
1656 if (network->group != NULL)
1657 __connman_service_update_from_network(network);
1662 static gboolean match_driver(struct connman_network *network,
1663 struct connman_network_driver *driver)
1665 if (network->type == driver->type ||
1666 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1672 static int network_probe(struct connman_element *element)
1674 struct connman_network *network = element->network;
1678 DBG("element %p name %s", element, element->name);
1680 if (network == NULL)
1683 for (list = driver_list; list; list = list->next) {
1684 struct connman_network_driver *driver = list->data;
1686 if (match_driver(network, driver) == FALSE)
1689 DBG("driver %p name %s", driver, driver->name);
1691 if (driver->probe(network) == 0) {
1692 network->driver = driver;
1697 if (network->driver == NULL)
1700 err = register_interface(element);
1702 if (network->driver->remove)
1703 network->driver->remove(network);
1707 switch (network->type) {
1708 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1709 case CONNMAN_NETWORK_TYPE_VENDOR:
1711 case CONNMAN_NETWORK_TYPE_ETHERNET:
1712 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1713 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1714 case CONNMAN_NETWORK_TYPE_CELLULAR:
1715 case CONNMAN_NETWORK_TYPE_WIFI:
1716 case CONNMAN_NETWORK_TYPE_WIMAX:
1717 if (network->group != NULL)
1718 __connman_profile_add_network(network);
1725 static void network_remove(struct connman_element *element)
1727 struct connman_network *network = element->network;
1729 DBG("element %p name %s", element, element->name);
1731 if (network == NULL)
1734 if (network->driver == NULL)
1737 switch (network->type) {
1738 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1739 case CONNMAN_NETWORK_TYPE_VENDOR:
1741 case CONNMAN_NETWORK_TYPE_ETHERNET:
1742 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1743 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1744 case CONNMAN_NETWORK_TYPE_CELLULAR:
1745 case CONNMAN_NETWORK_TYPE_WIFI:
1746 case CONNMAN_NETWORK_TYPE_WIMAX:
1747 if (network->group != NULL) {
1748 if (network->registered)
1749 __connman_profile_remove_network(network);
1751 g_free(network->group);
1752 network->group = NULL;
1757 unregister_interface(element);
1759 if (network->driver->remove)
1760 network->driver->remove(network);
1763 static void network_change(struct connman_element *element)
1765 struct connman_network *network = element->network;
1767 DBG("element %p name %s", element, element->name);
1769 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1772 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1775 if (network->connected == FALSE)
1778 connman_element_unregister_children(element);
1780 connman_device_set_disconnected(network->device, TRUE);
1782 if (network->driver && network->driver->disconnect) {
1783 network->driver->disconnect(network);
1787 network->connected = FALSE;
1790 static struct connman_driver network_driver = {
1792 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1793 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1794 .probe = network_probe,
1795 .remove = network_remove,
1796 .change = network_change,
1799 int __connman_network_init(void)
1803 connection = connman_dbus_get_connection();
1805 return connman_driver_register(&network_driver);
1808 void __connman_network_cleanup(void)
1812 connman_driver_unregister(&network_driver);
1814 dbus_connection_unref(connection);