5 * Copyright (C) 2007-2009 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 secondary;
40 connman_bool_t available;
41 connman_bool_t connected;
42 connman_bool_t roaming;
43 connman_bool_t hidden;
44 connman_uint8_t strength;
45 connman_uint16_t frequency;
51 struct connman_ipconfig *ipconfig;
53 struct connman_network_driver *driver;
56 connman_bool_t registered;
57 connman_bool_t connecting;
58 connman_bool_t associating;
60 struct connman_device *device;
66 unsigned short channel;
72 static const char *type2string(enum connman_network_type type)
75 case CONNMAN_NETWORK_TYPE_UNKNOWN:
76 case CONNMAN_NETWORK_TYPE_VENDOR:
78 case CONNMAN_NETWORK_TYPE_CABLE:
80 case CONNMAN_NETWORK_TYPE_WIFI:
82 case CONNMAN_NETWORK_TYPE_WIMAX:
84 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
85 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
87 case CONNMAN_NETWORK_TYPE_CELLULAR:
88 case CONNMAN_NETWORK_TYPE_MBM:
89 case CONNMAN_NETWORK_TYPE_HSO:
96 static DBusMessage *get_properties(DBusConnection *conn,
97 DBusMessage *msg, void *data)
99 struct connman_network *network = data;
101 DBusMessageIter array, dict;
103 DBG("conn %p", conn);
105 if (__connman_security_check_privilege(msg,
106 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
107 return __connman_error_permission_denied(msg);
109 reply = dbus_message_new_method_return(msg);
113 dbus_message_iter_init_append(reply, &array);
115 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
116 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
117 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
118 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
120 if (network->device) {
121 const char *path = connman_device_get_path(network->device);
123 connman_dbus_dict_append_variant(&dict, "Device",
124 DBUS_TYPE_OBJECT_PATH, &path);
127 if (network->address != NULL)
128 connman_dbus_dict_append_variant(&dict, "Address",
129 DBUS_TYPE_STRING, &network->address);
131 if (network->name != NULL)
132 connman_dbus_dict_append_variant(&dict, "Name",
133 DBUS_TYPE_STRING, &network->name);
135 connman_dbus_dict_append_variant(&dict, "Connected",
136 DBUS_TYPE_BOOLEAN, &network->connected);
138 if (network->strength > 0)
139 connman_dbus_dict_append_variant(&dict, "Strength",
140 DBUS_TYPE_BYTE, &network->strength);
142 if (network->frequency > 0)
143 connman_dbus_dict_append_variant(&dict, "Frequency",
144 DBUS_TYPE_UINT16, &network->frequency);
146 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
147 connman_dbus_dict_append_array(&dict, "WiFi.SSID",
148 DBUS_TYPE_BYTE, &network->wifi.ssid,
149 network->wifi.ssid_len);
151 if (network->wifi.mode != NULL)
152 connman_dbus_dict_append_variant(&dict, "WiFi.Mode",
153 DBUS_TYPE_STRING, &network->wifi.mode);
155 if (network->wifi.channel > 0)
156 connman_dbus_dict_append_variant(&dict, "WiFi.Channel",
157 DBUS_TYPE_UINT16, &network->wifi.channel);
159 if (network->wifi.security != NULL)
160 connman_dbus_dict_append_variant(&dict, "WiFi.Security",
161 DBUS_TYPE_STRING, &network->wifi.security);
163 if (network->wifi.passphrase != NULL &&
164 __connman_security_check_privilege(msg,
165 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
166 connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase",
167 DBUS_TYPE_STRING, &network->wifi.passphrase);
169 dbus_message_iter_close_container(&array, &dict);
174 static DBusMessage *set_property(DBusConnection *conn,
175 DBusMessage *msg, void *data)
177 struct connman_network *network = data;
178 DBusMessageIter iter, value;
182 DBG("conn %p", conn);
184 if (dbus_message_iter_init(msg, &iter) == FALSE)
185 return __connman_error_invalid_arguments(msg);
187 dbus_message_iter_get_basic(&iter, &name);
188 dbus_message_iter_next(&iter);
189 dbus_message_iter_recurse(&iter, &value);
191 if (__connman_security_check_privilege(msg,
192 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
193 return __connman_error_permission_denied(msg);
195 type = dbus_message_iter_get_arg_type(&value);
197 if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
198 const char *passphrase;
200 if (type != DBUS_TYPE_STRING)
201 return __connman_error_invalid_arguments(msg);
203 if (__connman_security_check_privilege(msg,
204 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
205 return __connman_error_permission_denied(msg);
207 dbus_message_iter_get_basic(&value, &passphrase);
209 g_free(network->wifi.passphrase);
210 network->wifi.passphrase = g_strdup(passphrase);
211 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
214 if (network->ipconfig == NULL)
215 return __connman_error_invalid_property(msg);
217 err = __connman_ipconfig_set_ipv4(network->ipconfig,
220 return __connman_error_failed(msg, -err);
222 return __connman_error_invalid_property(msg);
224 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
227 static GDBusMethodTable network_methods[] = {
228 { "GetProperties", "", "a{sv}", get_properties },
229 { "SetProperty", "sv", "", set_property },
233 static GDBusSignalTable network_signals[] = {
234 { "PropertyChanged", "sv" },
238 static DBusConnection *connection;
240 static void append_networks(struct connman_device *device,
241 DBusMessageIter *entry)
243 DBusMessageIter value, iter;
244 const char *key = "Networks";
246 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
248 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
249 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
252 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
253 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
254 __connman_element_list((struct connman_element *) device,
255 CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
256 dbus_message_iter_close_container(&value, &iter);
258 dbus_message_iter_close_container(entry, &value);
261 static void emit_networks_signal(struct connman_device *device)
263 const char *path = connman_device_get_path(device);
265 DBusMessageIter entry;
267 signal = dbus_message_new_signal(path,
268 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
272 dbus_message_iter_init_append(signal, &entry);
274 append_networks(device, &entry);
276 g_dbus_send_message(connection, signal);
279 static int register_interface(struct connman_element *element)
281 struct connman_network *network = element->network;
283 DBG("element %p name %s", element, element->name);
285 if (g_dbus_register_interface(connection, element->path,
286 CONNMAN_NETWORK_INTERFACE,
287 network_methods, network_signals,
288 NULL, network, NULL) == FALSE) {
289 connman_error("Failed to register %s network", element->path);
293 network->registered = TRUE;
295 emit_networks_signal(network->device);
300 static void unregister_interface(struct connman_element *element)
302 struct connman_network * network = element->network;
304 DBG("element %p name %s", element, element->name);
306 network->registered = FALSE;
308 emit_networks_signal(network->device);
310 g_dbus_unregister_interface(connection, element->path,
311 CONNMAN_NETWORK_INTERFACE);
314 connman_bool_t __connman_network_has_driver(struct connman_network *network)
316 if (network == NULL || network->driver == NULL)
319 return network->registered;
322 static GSList *driver_list = NULL;
324 static gint compare_priority(gconstpointer a, gconstpointer b)
326 const struct connman_network_driver *driver1 = a;
327 const struct connman_network_driver *driver2 = b;
329 return driver2->priority - driver1->priority;
333 * connman_network_driver_register:
334 * @driver: network driver definition
336 * Register a new network driver
338 * Returns: %0 on success
340 int connman_network_driver_register(struct connman_network_driver *driver)
342 DBG("driver %p name %s", driver, driver->name);
344 driver_list = g_slist_insert_sorted(driver_list, driver,
351 * connman_network_driver_unregister:
352 * @driver: network driver definition
354 * Remove a previously registered network driver
356 void connman_network_driver_unregister(struct connman_network_driver *driver)
358 DBG("driver %p name %s", driver, driver->name);
360 driver_list = g_slist_remove(driver_list, driver);
363 static void network_destruct(struct connman_element *element)
365 struct connman_network *network = element->network;
367 DBG("element %p name %s", element, element->name);
369 g_free(network->wifi.ssid);
370 g_free(network->wifi.mode);
371 g_free(network->wifi.security);
372 g_free(network->wifi.passphrase);
374 g_free(network->group);
375 g_free(network->node);
376 g_free(network->name);
377 g_free(network->address);
378 g_free(network->identifier);
380 if (network->ipconfig) {
381 connman_ipconfig_unref(network->ipconfig);
382 network->ipconfig = NULL;
387 * connman_network_create:
388 * @identifier: network identifier (for example an unqiue name)
390 * Allocate a new network and assign the #identifier to it.
392 * Returns: a newly-allocated #connman_network structure
394 struct connman_network *connman_network_create(const char *identifier,
395 enum connman_network_type type)
397 struct connman_network *network;
398 connman_uint8_t strength = 0;
402 DBG("identifier %s type %d", identifier, type);
404 network = g_try_new0(struct connman_network, 1);
408 DBG("network %p", network);
410 __connman_element_initialize(&network->element);
412 //temp = connman_dbus_encode_string(identifier);
413 if (identifier == NULL) {
414 temp = g_strdup_printf("hidden_%d", hidden_counter++);
415 network->hidden = TRUE;
417 temp = g_strdup(identifier);
424 network->element.name = temp;
425 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
427 network->element.network = network;
428 network->element.destruct = network_destruct;
430 str = type2string(type);
432 connman_element_set_string(&network->element, "Type", str);
434 connman_element_set_uint8(&network->element, "Strength", strength);
436 network->type = type;
437 network->secondary = FALSE;
438 network->identifier = g_strdup(temp);
444 * connman_network_ref:
445 * @network: network structure
447 * Increase reference counter of network
449 struct connman_network *connman_network_ref(struct connman_network *network)
451 if (connman_element_ref(&network->element) == NULL)
458 * connman_network_unref:
459 * @network: network structure
461 * Decrease reference counter of network
463 void connman_network_unref(struct connman_network *network)
465 connman_element_unref(&network->element);
468 const char *__connman_network_get_type(struct connman_network *network)
470 return type2string(network->type);
474 * connman_network_get_type:
475 * @network: network structure
477 * Get type of network
479 enum connman_network_type connman_network_get_type(struct connman_network *network)
481 return network->type;
485 * connman_network_get_identifier:
486 * @network: network structure
488 * Get identifier of network
490 const char *connman_network_get_identifier(struct connman_network *network)
492 return network->identifier;
496 * connman_network_get_path:
497 * @network: network structure
499 * Get path name of network
501 const char *connman_network_get_path(struct connman_network *network)
503 return network->element.path;
507 * connman_network_set_index:
508 * @network: network structure
509 * @index: index number
511 * Set index number of network
513 void connman_network_set_index(struct connman_network *network, int index)
515 network->element.index = index;
519 * connman_network_get_index:
520 * @network: network structure
522 * Get index number of network
524 int connman_network_get_index(struct connman_network *network)
526 return network->element.index;
530 * connman_network_set_protocol:
531 * @network: network structure
532 * @protocol: network protocol
534 * Change protocol of network
536 void connman_network_set_protocol(struct connman_network *network,
537 enum connman_network_protocol protocol)
539 network->protocol = protocol;
543 * connman_network_set_group:
544 * @network: network structure
547 * Set group name for automatic clustering
549 void connman_network_set_group(struct connman_network *network,
552 if (network->secondary == TRUE)
555 switch (network->type) {
556 case CONNMAN_NETWORK_TYPE_UNKNOWN:
557 case CONNMAN_NETWORK_TYPE_CABLE:
558 case CONNMAN_NETWORK_TYPE_VENDOR:
560 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
561 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
562 case CONNMAN_NETWORK_TYPE_CELLULAR:
563 case CONNMAN_NETWORK_TYPE_MBM:
564 case CONNMAN_NETWORK_TYPE_HSO:
565 case CONNMAN_NETWORK_TYPE_WIFI:
566 case CONNMAN_NETWORK_TYPE_WIMAX:
570 if (g_strcmp0(network->group, group) == 0) {
572 __connman_profile_update_network(network);
576 if (network->group != NULL) {
577 __connman_profile_remove_network(network);
579 g_free(network->group);
582 network->group = g_strdup(group);
584 if (network->group != NULL)
585 __connman_profile_add_network(network);
589 * connman_network_get_group:
590 * @network: network structure
592 * Get group name for automatic clustering
594 const char *connman_network_get_group(struct connman_network *network)
596 return network->group;
599 const char *__connman_network_get_ident(struct connman_network *network)
601 if (network->device == NULL)
604 return __connman_device_get_ident(network->device);
607 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
609 if (network->secondary == TRUE)
612 switch (network->type) {
613 case CONNMAN_NETWORK_TYPE_UNKNOWN:
614 case CONNMAN_NETWORK_TYPE_VENDOR:
615 case CONNMAN_NETWORK_TYPE_CABLE:
616 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
617 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
618 case CONNMAN_NETWORK_TYPE_CELLULAR:
619 case CONNMAN_NETWORK_TYPE_MBM:
620 case CONNMAN_NETWORK_TYPE_HSO:
621 case CONNMAN_NETWORK_TYPE_WIMAX:
623 case CONNMAN_NETWORK_TYPE_WIFI:
624 if (network->strength > 0 && network->strength < 20)
633 * connman_network_set_available:
634 * @network: network structure
635 * @available: availability state
637 * Change availability state of network (in range)
639 int connman_network_set_available(struct connman_network *network,
640 connman_bool_t available)
642 DBG("network %p available %d", network, available);
644 if (network->available == available)
647 network->available = available;
653 * connman_network_get_available:
654 * @network: network structure
656 * Get network available setting
658 connman_bool_t connman_network_get_available(struct connman_network *network)
660 if (network->hidden == TRUE)
663 return network->available;
667 * connman_network_set_associating:
668 * @network: network structure
669 * @associating: associating state
671 * Change associating state of network
673 int connman_network_set_associating(struct connman_network *network,
674 connman_bool_t associating)
676 DBG("network %p associating %d", network, associating);
678 if (network->associating == associating)
681 network->associating = associating;
683 if (associating == TRUE) {
684 struct connman_service *service;
686 service = __connman_service_lookup_from_network(network);
687 __connman_service_indicate_state(service,
688 CONNMAN_SERVICE_STATE_ASSOCIATION);
694 static void set_associate_error(struct connman_network *network)
696 struct connman_service *service;
698 if (network->associating == FALSE)
701 network->associating = FALSE;
703 service = __connman_service_lookup_from_network(network);
705 __connman_service_indicate_state(service,
706 CONNMAN_SERVICE_STATE_FAILURE);
709 void connman_network_set_error(struct connman_network *network,
710 enum connman_network_error error)
712 DBG("nework %p, error %d", network, error);
715 case CONNMAN_NETWORK_ERROR_UNKNOWN:
717 case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
718 set_associate_error(network);
723 static gboolean set_connected(gpointer user_data)
725 struct connman_network *network = user_data;
726 struct connman_service *service;
728 service = __connman_service_lookup_from_network(network);
730 if (network->connected == TRUE) {
731 struct connman_element *element;
732 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
734 switch (network->protocol) {
735 case CONNMAN_NETWORK_PROTOCOL_UNKNOWN:
737 case CONNMAN_NETWORK_PROTOCOL_IP:
738 type = CONNMAN_ELEMENT_TYPE_DHCP;
740 case CONNMAN_NETWORK_PROTOCOL_PPP:
741 type = CONNMAN_ELEMENT_TYPE_PPP;
745 __connman_device_increase_connections(network->device);
747 __connman_device_set_network(network->device, network);
749 connman_device_set_disconnected(network->device, FALSE);
751 element = connman_element_create(NULL);
752 if (element != NULL) {
753 element->type = type;
754 element->index = network->element.index;
756 if (connman_element_register(element,
757 &network->element) < 0)
758 connman_element_unref(element);
760 __connman_service_indicate_state(service,
761 CONNMAN_SERVICE_STATE_CONFIGURATION);
764 connman_element_unregister_children(&network->element);
766 __connman_device_set_network(network->device, NULL);
767 network->hidden = FALSE;
769 __connman_device_decrease_connections(network->device);
771 __connman_service_indicate_state(service,
772 CONNMAN_SERVICE_STATE_IDLE);
775 network->connecting = FALSE;
777 connman_network_set_associating(network, FALSE);
783 * connman_network_set_connected:
784 * @network: network structure
785 * @connected: connected state
787 * Change connected state of network
789 int connman_network_set_connected(struct connman_network *network,
790 connman_bool_t connected)
793 DBusMessageIter entry, value;
794 const char *key = "Connected";
796 DBG("network %p connected %d", network, connected);
798 if ((network->connecting == TRUE || network->associating == TRUE) &&
799 connected == FALSE) {
800 connman_element_set_error(&network->element,
801 CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
802 __connman_network_disconnect(network);
805 if (network->connected == connected)
808 network->connected = connected;
810 if (network->registered == FALSE) {
811 g_idle_add(set_connected, network);
815 signal = dbus_message_new_signal(network->element.path,
816 CONNMAN_NETWORK_INTERFACE, "PropertyChanged");
820 dbus_message_iter_init_append(signal, &entry);
822 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
824 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
825 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
826 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &connected);
827 dbus_message_iter_close_container(&entry, &value);
829 g_dbus_send_message(connection, signal);
831 set_connected(network);
837 * connman_network_get_connected:
838 * @network: network structure
840 * Get network connection status
842 connman_bool_t connman_network_get_connected(struct connman_network *network)
844 return network->connected;
848 * connman_network_get_associating:
849 * @network: network structure
851 * Get network associating status
853 connman_bool_t connman_network_get_associating(struct connman_network *network)
855 return network->associating;
859 * __connman_network_connect:
860 * @network: network structure
864 int __connman_network_connect(struct connman_network *network)
868 DBG("network %p", network);
870 if (network->connected == TRUE)
873 if (network->connecting == TRUE || network->associating == TRUE)
876 if (network->driver == NULL)
879 if (network->driver->connect == NULL)
882 __connman_device_disconnect(network->device);
884 network->connecting = TRUE;
886 err = network->driver->connect(network);
888 if (err == -EINPROGRESS)
889 connman_network_set_associating(network, TRUE);
891 network->hidden = FALSE;
896 network->connected = TRUE;
897 set_connected(network);
903 * __connman_network_disconnect:
904 * @network: network structure
908 int __connman_network_disconnect(struct connman_network *network)
912 DBG("network %p", network);
914 if (network->connected == FALSE && network->connecting == FALSE &&
915 network->associating == FALSE)
918 if (network->driver == NULL)
921 if (network->driver->disconnect == NULL)
924 network->connecting = FALSE;
926 err = network->driver->disconnect(network);
928 network->connected = FALSE;
929 set_connected(network);
936 * connman_network_set_address:
937 * @network: network structure
938 * @address: binary address value
939 * @size: binary address length
941 * Set unique address value for network
943 int connman_network_set_address(struct connman_network *network,
944 const void *address, unsigned int size)
946 const unsigned char *addr_octet = address;
949 DBG("network %p size %d", network, size);
954 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
955 addr_octet[0], addr_octet[1], addr_octet[2],
956 addr_octet[3], addr_octet[4], addr_octet[5]);
960 g_free(network->address);
961 network->address = str;
963 return connman_element_set_string(&network->element,
964 "Address", network->address);
968 * connman_network_set_name:
969 * @network: network structure
972 * Set display name value for network
974 int connman_network_set_name(struct connman_network *network,
977 DBG("network %p name %s", network, name);
979 g_free(network->name);
980 network->name = g_strdup(name);
982 return connman_element_set_string(&network->element, "Name", name);
986 * connman_network_set_strength:
987 * @network: network structure
988 * @strength: strength value
990 * Set signal strength value for network
992 int connman_network_set_strength(struct connman_network *network,
993 connman_uint8_t strength)
995 DBG("network %p strengh %d", network, strength);
997 network->strength = strength;
999 return connman_element_set_uint8(&network->element,
1000 "Strength", strength);
1004 * connman_network_set_roaming:
1005 * @network: network structure
1006 * @roaming: roaming state
1008 * Set roaming state for network
1010 int connman_network_set_roaming(struct connman_network *network,
1011 connman_bool_t roaming)
1013 DBG("network %p roaming %d", network, roaming);
1015 network->roaming = roaming;
1017 return connman_element_set_bool(&network->element,
1018 "Roaming", roaming);
1022 * connman_network_set_string:
1023 * @network: network structure
1024 * @key: unique identifier
1025 * @value: string value
1027 * Set string value for specific key
1029 int connman_network_set_string(struct connman_network *network,
1030 const char *key, const char *value)
1032 DBG("network %p key %s value %s", network, key, value);
1034 if (g_strcmp0(key, "Name") == 0)
1035 return connman_network_set_name(network, value);
1037 if (g_str_equal(key, "Address") == TRUE) {
1038 g_free(network->address);
1039 network->address = g_strdup(value);
1040 } else if (g_str_equal(key, "Node") == TRUE) {
1041 g_free(network->node);
1042 network->node = g_strdup(value);
1043 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1044 g_free(network->wifi.mode);
1045 network->wifi.mode = g_strdup(value);
1046 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1047 g_free(network->wifi.security);
1048 network->wifi.security = g_strdup(value);
1049 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1050 g_free(network->wifi.passphrase);
1051 network->wifi.passphrase = g_strdup(value);
1054 return connman_element_set_string(&network->element, key, value);
1058 * connman_network_get_string:
1059 * @network: network structure
1060 * @key: unique identifier
1062 * Get string value for specific key
1064 const char *connman_network_get_string(struct connman_network *network,
1067 DBG("network %p key %s", network, key);
1069 if (g_str_equal(key, "Address") == TRUE)
1070 return network->address;
1071 else if (g_str_equal(key, "Name") == TRUE)
1072 return network->name;
1073 else if (g_str_equal(key, "Node") == TRUE)
1074 return network->node;
1075 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1076 return network->wifi.mode;
1077 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1078 return network->wifi.security;
1079 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1080 return network->wifi.passphrase;
1082 return connman_element_get_string(&network->element, key);
1086 * connman_network_set_bool:
1087 * @network: network structure
1088 * @key: unique identifier
1089 * @value: boolean value
1091 * Set boolean value for specific key
1093 int connman_network_set_bool(struct connman_network *network,
1094 const char *key, connman_bool_t value)
1096 DBG("network %p key %s value %d", network, key, value);
1098 if (g_strcmp0(key, "Roaming") == 0)
1099 return connman_network_set_roaming(network, value);
1101 return connman_element_set_bool(&network->element, key, value);
1105 * connman_network_get_bool:
1106 * @network: network structure
1107 * @key: unique identifier
1109 * Get boolean value for specific key
1111 connman_bool_t connman_network_get_bool(struct connman_network *network,
1114 DBG("network %p key %s", network, key);
1116 if (g_str_equal(key, "Roaming") == TRUE)
1117 return network->roaming;
1119 return connman_element_get_bool(&network->element, key);
1123 * connman_network_set_uint8:
1124 * @network: network structure
1125 * @key: unique identifier
1126 * @value: integer value
1128 * Set integer value for specific key
1130 int connman_network_set_uint8(struct connman_network *network,
1131 const char *key, connman_uint8_t value)
1133 DBG("network %p key %s value %d", network, key, value);
1135 if (g_strcmp0(key, "Strength") == 0)
1136 return connman_network_set_strength(network, value);
1138 return connman_element_set_uint8(&network->element, key, value);
1142 * connman_network_get_uint8:
1143 * @network: network structure
1144 * @key: unique identifier
1146 * Get integer value for specific key
1148 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1151 DBG("network %p key %s", network, key);
1153 if (g_str_equal(key, "Strength") == TRUE)
1154 return network->strength;
1156 return connman_element_get_uint8(&network->element, key);
1160 * connman_network_set_uint16:
1161 * @network: network structure
1162 * @key: unique identifier
1163 * @value: integer value
1165 * Set integer value for specific key
1167 int connman_network_set_uint16(struct connman_network *network,
1168 const char *key, connman_uint16_t value)
1170 DBG("network %p key %s value %d", network, key, value);
1172 if (g_str_equal(key, "Frequency") == TRUE)
1173 network->frequency = value;
1174 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1175 network->wifi.channel = value;
1181 * connman_network_get_uint16:
1182 * @network: network structure
1183 * @key: unique identifier
1185 * Get integer value for specific key
1187 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1190 DBG("network %p key %s", network, key);
1192 if (g_str_equal(key, "Frequency") == TRUE)
1193 return network->frequency;
1194 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1195 return network->wifi.channel;
1201 * connman_network_set_blob:
1202 * @network: network structure
1203 * @key: unique identifier
1207 * Set binary blob value for specific key
1209 int connman_network_set_blob(struct connman_network *network,
1210 const char *key, const void *data, unsigned int size)
1212 DBG("network %p key %s size %d", network, key, size);
1214 if (g_strcmp0(key, "Address") == 0)
1215 return connman_network_set_address(network, data, size);
1217 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1218 g_free(network->wifi.ssid);
1219 network->wifi.ssid = g_try_malloc(size);
1220 if (network->wifi.ssid != NULL) {
1221 memcpy(network->wifi.ssid, data, size);
1222 network->wifi.ssid_len = size;
1224 network->wifi.ssid_len = 0;
1227 return connman_element_set_blob(&network->element, key, data, size);
1231 * connman_network_get_blob:
1232 * @network: network structure
1233 * @key: unique identifier
1234 * @size: pointer to blob size
1236 * Get binary blob value for specific key
1238 const void *connman_network_get_blob(struct connman_network *network,
1239 const char *key, unsigned int *size)
1241 DBG("network %p key %s", network, key);
1243 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1245 *size = network->wifi.ssid_len;
1246 return network->wifi.ssid;
1249 return connman_element_get_blob(&network->element, key, size);
1252 void __connman_network_set_device(struct connman_network *network,
1253 struct connman_device *device)
1255 network->device = device;
1259 * connman_network_get_device:
1260 * @network: network structure
1262 * Get parent device of network
1264 struct connman_device *connman_network_get_device(struct connman_network *network)
1266 return network->device;
1270 * connman_network_get_data:
1271 * @network: network structure
1273 * Get private network data pointer
1275 void *connman_network_get_data(struct connman_network *network)
1277 return network->driver_data;
1281 * connman_network_set_data:
1282 * @network: network structure
1283 * @data: data pointer
1285 * Set private network data pointer
1287 void connman_network_set_data(struct connman_network *network, void *data)
1289 network->driver_data = data;
1292 static gboolean match_driver(struct connman_network *network,
1293 struct connman_network_driver *driver)
1295 if (network->type == driver->type ||
1296 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1302 static int network_probe(struct connman_element *element)
1304 struct connman_network *network = element->network;
1308 DBG("element %p name %s", element, element->name);
1310 if (network == NULL)
1313 for (list = driver_list; list; list = list->next) {
1314 struct connman_network_driver *driver = list->data;
1316 if (match_driver(network, driver) == FALSE)
1319 DBG("driver %p name %s", driver, driver->name);
1321 if (driver->probe(network) == 0) {
1322 network->driver = driver;
1327 if (network->driver == NULL)
1330 err = register_interface(element);
1332 if (network->driver->remove)
1333 network->driver->remove(network);
1337 network->secondary = connman_device_get_secondary(network->device);
1339 switch (network->type) {
1340 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1341 case CONNMAN_NETWORK_TYPE_VENDOR:
1343 case CONNMAN_NETWORK_TYPE_CABLE:
1344 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1345 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1346 case CONNMAN_NETWORK_TYPE_CELLULAR:
1347 case CONNMAN_NETWORK_TYPE_MBM:
1348 case CONNMAN_NETWORK_TYPE_HSO:
1349 case CONNMAN_NETWORK_TYPE_WIFI:
1350 case CONNMAN_NETWORK_TYPE_WIMAX:
1351 if (network->group != NULL && network->secondary == FALSE)
1352 __connman_profile_add_network(network);
1359 static void network_remove(struct connman_element *element)
1361 struct connman_network *network = element->network;
1363 DBG("element %p name %s", element, element->name);
1365 if (network == NULL)
1368 if (network->driver == NULL)
1371 switch (network->type) {
1372 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1373 case CONNMAN_NETWORK_TYPE_VENDOR:
1375 case CONNMAN_NETWORK_TYPE_CABLE:
1376 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1377 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1378 case CONNMAN_NETWORK_TYPE_CELLULAR:
1379 case CONNMAN_NETWORK_TYPE_MBM:
1380 case CONNMAN_NETWORK_TYPE_HSO:
1381 case CONNMAN_NETWORK_TYPE_WIFI:
1382 case CONNMAN_NETWORK_TYPE_WIMAX:
1383 if (network->group != NULL && network->secondary == FALSE) {
1384 __connman_profile_remove_network(network);
1386 g_free(network->group);
1387 network->group = NULL;
1392 unregister_interface(element);
1394 if (network->driver->remove)
1395 network->driver->remove(network);
1398 static void network_change(struct connman_element *element)
1400 struct connman_network *network = element->network;
1402 DBG("element %p name %s", element, element->name);
1404 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1407 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1410 if (network->connected == FALSE)
1413 connman_element_unregister_children(element);
1415 connman_device_set_disconnected(network->device, TRUE);
1417 if (network->driver && network->driver->disconnect) {
1418 network->driver->disconnect(network);
1422 network->connected = FALSE;
1425 static struct connman_driver network_driver = {
1427 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1428 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1429 .probe = network_probe,
1430 .remove = network_remove,
1431 .change = network_change,
1434 int __connman_network_init(void)
1438 connection = connman_dbus_get_connection();
1440 return connman_driver_register(&network_driver);
1443 void __connman_network_cleanup(void)
1447 connman_driver_unregister(&network_driver);
1449 dbus_connection_unref(connection);