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 struct connman_network {
34 struct connman_element element;
35 enum connman_network_type type;
36 enum connman_network_protocol protocol;
37 connman_bool_t associating;
38 connman_bool_t secondary;
39 connman_bool_t available;
40 connman_bool_t connected;
41 connman_uint8_t strength;
42 connman_uint16_t frequency;
48 struct connman_ipconfig *ipconfig;
50 struct connman_network_driver *driver;
53 connman_bool_t registered;
55 struct connman_device *device;
61 unsigned short channel;
67 static const char *type2string(enum connman_network_type type)
70 case CONNMAN_NETWORK_TYPE_UNKNOWN:
71 case CONNMAN_NETWORK_TYPE_VENDOR:
73 case CONNMAN_NETWORK_TYPE_WIFI:
75 case CONNMAN_NETWORK_TYPE_WIMAX:
77 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
78 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
80 case CONNMAN_NETWORK_TYPE_HSO:
87 static DBusMessage *get_properties(DBusConnection *conn,
88 DBusMessage *msg, void *data)
90 struct connman_network *network = data;
92 DBusMessageIter array, dict;
96 if (__connman_security_check_privilege(msg,
97 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
98 return __connman_error_permission_denied(msg);
100 reply = dbus_message_new_method_return(msg);
104 dbus_message_iter_init_append(reply, &array);
106 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
107 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
108 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
109 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
111 if (network->device) {
112 const char *path = connman_device_get_path(network->device);
114 connman_dbus_dict_append_variant(&dict, "Device",
115 DBUS_TYPE_OBJECT_PATH, &path);
118 if (network->address != NULL)
119 connman_dbus_dict_append_variant(&dict, "Address",
120 DBUS_TYPE_STRING, &network->address);
122 if (network->name != NULL)
123 connman_dbus_dict_append_variant(&dict, "Name",
124 DBUS_TYPE_STRING, &network->name);
126 connman_dbus_dict_append_variant(&dict, "Connected",
127 DBUS_TYPE_BOOLEAN, &network->connected);
129 if (network->strength > 0)
130 connman_dbus_dict_append_variant(&dict, "Strength",
131 DBUS_TYPE_BYTE, &network->strength);
133 if (network->frequency > 0)
134 connman_dbus_dict_append_variant(&dict, "Frequency",
135 DBUS_TYPE_UINT16, &network->frequency);
137 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
138 connman_dbus_dict_append_array(&dict, "WiFi.SSID",
139 DBUS_TYPE_BYTE, &network->wifi.ssid,
140 network->wifi.ssid_len);
142 if (network->wifi.mode != NULL)
143 connman_dbus_dict_append_variant(&dict, "WiFi.Mode",
144 DBUS_TYPE_STRING, &network->wifi.mode);
146 if (network->wifi.channel > 0)
147 connman_dbus_dict_append_variant(&dict, "WiFi.Channel",
148 DBUS_TYPE_UINT16, &network->wifi.channel);
150 if (network->wifi.security != NULL)
151 connman_dbus_dict_append_variant(&dict, "WiFi.Security",
152 DBUS_TYPE_STRING, &network->wifi.security);
154 if (network->wifi.passphrase != NULL &&
155 __connman_security_check_privilege(msg,
156 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
157 connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase",
158 DBUS_TYPE_STRING, &network->wifi.passphrase);
160 dbus_message_iter_close_container(&array, &dict);
165 static DBusMessage *set_property(DBusConnection *conn,
166 DBusMessage *msg, void *data)
168 struct connman_network *network = data;
169 DBusMessageIter iter, value;
173 DBG("conn %p", conn);
175 if (dbus_message_iter_init(msg, &iter) == FALSE)
176 return __connman_error_invalid_arguments(msg);
178 dbus_message_iter_get_basic(&iter, &name);
179 dbus_message_iter_next(&iter);
180 dbus_message_iter_recurse(&iter, &value);
182 if (__connman_security_check_privilege(msg,
183 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
184 return __connman_error_permission_denied(msg);
186 type = dbus_message_iter_get_arg_type(&value);
188 if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
189 const char *passphrase;
191 if (type != DBUS_TYPE_STRING)
192 return __connman_error_invalid_arguments(msg);
194 if (__connman_security_check_privilege(msg,
195 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
196 return __connman_error_permission_denied(msg);
198 dbus_message_iter_get_basic(&value, &passphrase);
200 g_free(network->wifi.passphrase);
201 network->wifi.passphrase = g_strdup(passphrase);
202 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
205 err = __connman_ipconfig_set_ipv4(network->ipconfig,
208 return __connman_error_failed(msg, -err);
210 return __connman_error_invalid_property(msg);
212 __connman_storage_save_network(network);
214 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
217 static DBusMessage *do_connect(DBusConnection *conn,
218 DBusMessage *msg, void *data)
220 struct connman_network *network = data;
223 DBG("conn %p", conn);
225 if (__connman_security_check_privilege(msg,
226 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
227 return __connman_error_permission_denied(msg);
229 if (network->connected == TRUE)
230 return __connman_error_failed(msg, EALREADY);
232 if (network->driver && network->driver->connect) {
233 enum connman_device_mode mode;
235 mode = connman_device_get_mode(network->device);
236 if (mode == CONNMAN_DEVICE_MODE_NETWORK_SINGLE)
237 __connman_device_disconnect(network->device);
239 err = network->driver->connect(network);
240 if (err < 0 && err != -EINPROGRESS)
241 return __connman_error_failed(msg, -err);
243 network->connected = TRUE;
245 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
248 static DBusMessage *do_disconnect(DBusConnection *conn,
249 DBusMessage *msg, void *data)
251 struct connman_network *network = data;
254 DBG("conn %p", conn);
256 if (__connman_security_check_privilege(msg,
257 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
258 return __connman_error_permission_denied(msg);
260 if (network->connected == FALSE)
261 return __connman_error_failed(msg, EINVAL);
263 connman_element_unregister_children(&network->element);
265 connman_device_set_disconnected(network->device, TRUE);
267 if (network->driver && network->driver->disconnect) {
268 err = network->driver->disconnect(network);
269 if (err < 0 && err != -EINPROGRESS)
270 return __connman_error_failed(msg, -err);
272 network->connected = FALSE;
274 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
277 static GDBusMethodTable network_methods[] = {
278 { "GetProperties", "", "a{sv}", get_properties },
279 { "SetProperty", "sv", "", set_property },
280 { "Connect", "", "", do_connect },
281 { "Disconnect", "", "", do_disconnect },
285 static GDBusSignalTable network_signals[] = {
286 { "PropertyChanged", "sv" },
290 static DBusConnection *connection;
292 static void append_networks(struct connman_device *device,
293 DBusMessageIter *entry)
295 DBusMessageIter value, iter;
296 const char *key = "Networks";
298 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
300 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
301 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
304 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
305 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
306 __connman_element_list((struct connman_element *) device,
307 CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
308 dbus_message_iter_close_container(&value, &iter);
310 dbus_message_iter_close_container(entry, &value);
313 static void emit_networks_signal(struct connman_device *device)
315 const char *path = connman_device_get_path(device);
317 DBusMessageIter entry;
319 signal = dbus_message_new_signal(path,
320 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
324 dbus_message_iter_init_append(signal, &entry);
326 append_networks(device, &entry);
328 g_dbus_send_message(connection, signal);
331 static int register_interface(struct connman_element *element)
333 struct connman_network *network = element->network;
335 DBG("element %p name %s", element, element->name);
337 if (g_dbus_register_interface(connection, element->path,
338 CONNMAN_NETWORK_INTERFACE,
339 network_methods, network_signals,
340 NULL, network, NULL) == FALSE) {
341 connman_error("Failed to register %s network", element->path);
345 network->registered = TRUE;
347 emit_networks_signal(network->device);
352 static void unregister_interface(struct connman_element *element)
354 struct connman_network * network = element->network;
356 DBG("element %p name %s", element, element->name);
358 network->registered = FALSE;
360 emit_networks_signal(network->device);
362 g_dbus_unregister_interface(connection, element->path,
363 CONNMAN_NETWORK_INTERFACE);
366 connman_bool_t __connman_network_has_driver(struct connman_network *network)
368 if (network == NULL || network->driver == NULL)
371 return network->registered;
374 static GSList *driver_list = NULL;
376 static gint compare_priority(gconstpointer a, gconstpointer b)
378 const struct connman_network_driver *driver1 = a;
379 const struct connman_network_driver *driver2 = b;
381 return driver2->priority - driver1->priority;
385 * connman_network_driver_register:
386 * @driver: network driver definition
388 * Register a new network driver
390 * Returns: %0 on success
392 int connman_network_driver_register(struct connman_network_driver *driver)
394 DBG("driver %p name %s", driver, driver->name);
396 driver_list = g_slist_insert_sorted(driver_list, driver,
403 * connman_network_driver_unregister:
404 * @driver: network driver definition
406 * Remove a previously registered network driver
408 void connman_network_driver_unregister(struct connman_network_driver *driver)
410 DBG("driver %p name %s", driver, driver->name);
412 driver_list = g_slist_remove(driver_list, driver);
415 static void network_destruct(struct connman_element *element)
417 struct connman_network *network = element->network;
419 DBG("element %p name %s", element, element->name);
421 g_free(network->wifi.ssid);
422 g_free(network->wifi.mode);
423 g_free(network->wifi.security);
424 g_free(network->wifi.passphrase);
426 g_free(network->group);
427 g_free(network->node);
428 g_free(network->name);
429 g_free(network->address);
430 g_free(network->identifier);
432 connman_ipconfig_unref(network->ipconfig);
436 * connman_network_create:
437 * @identifier: network identifier (for example an unqiue name)
439 * Allocate a new network and assign the #identifier to it.
441 * Returns: a newly-allocated #connman_network structure
443 struct connman_network *connman_network_create(const char *identifier,
444 enum connman_network_type type)
446 struct connman_network *network;
447 connman_uint8_t strength = 0;
451 DBG("identifier %s type %d", identifier, type);
453 network = g_try_new0(struct connman_network, 1);
457 DBG("network %p", network);
459 __connman_element_initialize(&network->element);
461 //temp = connman_dbus_encode_string(identifier);
462 temp = g_strdup(identifier);
468 network->element.name = temp;
469 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
471 network->element.network = network;
472 network->element.destruct = network_destruct;
474 str = type2string(type);
476 connman_element_set_string(&network->element, "Type", str);
478 connman_element_set_uint8(&network->element, "Strength", strength);
480 network->type = type;
481 network->secondary = FALSE;
482 network->identifier = g_strdup(identifier);
484 network->ipconfig = connman_ipconfig_create();
485 if (network->ipconfig == NULL) {
486 connman_network_unref(network);
494 * connman_network_ref:
495 * @network: network structure
497 * Increase reference counter of network
499 struct connman_network *connman_network_ref(struct connman_network *network)
501 if (connman_element_ref(&network->element) == NULL)
508 * connman_network_unref:
509 * @network: network structure
511 * Decrease reference counter of network
513 void connman_network_unref(struct connman_network *network)
515 connman_element_unref(&network->element);
518 const char *__connman_network_get_type(struct connman_network *network)
520 return type2string(network->type);
524 * connman_network_get_type:
525 * @network: network structure
527 * Get type of network
529 enum connman_network_type connman_network_get_type(struct connman_network *network)
531 return network->type;
535 * connman_network_get_identifier:
536 * @network: network structure
538 * Get identifier of network
540 const char *connman_network_get_identifier(struct connman_network *network)
542 return network->identifier;
546 * connman_network_get_path:
547 * @network: network structure
549 * Get path name of network
551 const char *connman_network_get_path(struct connman_network *network)
553 return network->element.path;
557 * connman_network_set_index:
558 * @network: network structure
559 * @index: index number
561 * Set index number of network
563 void connman_network_set_index(struct connman_network *network, int index)
565 network->element.index = index;
569 * connman_network_get_index:
570 * @network: network structure
572 * Get index number of network
574 int connman_network_get_index(struct connman_network *network)
576 return network->element.index;
580 * connman_network_set_protocol:
581 * @network: network structure
582 * @protocol: network protocol
584 * Change protocol of network
586 void connman_network_set_protocol(struct connman_network *network,
587 enum connman_network_protocol protocol)
589 network->protocol = protocol;
593 * connman_network_set_group:
594 * @network: network structure
597 * Set group name for automatic clustering
599 void connman_network_set_group(struct connman_network *network,
602 if (network->secondary == TRUE)
605 if (g_strcmp0(network->group, group) == 0)
608 switch (network->type) {
609 case CONNMAN_NETWORK_TYPE_UNKNOWN:
610 case CONNMAN_NETWORK_TYPE_VENDOR:
611 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
612 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
613 case CONNMAN_NETWORK_TYPE_HSO:
615 case CONNMAN_NETWORK_TYPE_WIFI:
616 case CONNMAN_NETWORK_TYPE_WIMAX:
620 if (network->group != NULL) {
621 __connman_profile_remove_network(network);
623 g_free(network->group);
626 network->group = g_strdup(group);
628 if (network->group != NULL)
629 __connman_profile_add_network(network);
632 const char *connman_network_get_group(struct connman_network *network)
634 return network->group;
637 const char *__connman_network_get_ident(struct connman_network *network)
639 if (network->device == NULL)
642 return __connman_device_get_ident(network->device);
646 * connman_network_set_available:
647 * @network: network structure
648 * @available: availability state
650 * Change availability state of network (in range)
652 int connman_network_set_available(struct connman_network *network,
653 connman_bool_t available)
655 DBG("network %p available %d", network, available);
657 if (network->available == available)
660 network->available = available;
666 * connman_network_get_available:
667 * @network: network structure
669 * Get network available setting
671 connman_bool_t connman_network_get_available(struct connman_network *network)
673 return network->available;
677 * connman_network_set_associating:
678 * @network: network structure
679 * @associating: associating state
681 * Change associating state of network
683 int connman_network_set_associating(struct connman_network *network,
684 connman_bool_t associating)
686 DBG("network %p associating %d", network, associating);
688 if (network->associating == associating)
691 network->associating = associating;
693 if (associating == TRUE) {
694 struct connman_service *service;
696 service = __connman_service_lookup_from_network(network);
697 __connman_service_indicate_state(service,
698 CONNMAN_SERVICE_STATE_ASSOCIATION);
704 static gboolean set_connected(gpointer user_data)
706 struct connman_network *network = user_data;
707 struct connman_service *service;
709 service = __connman_service_lookup_from_network(network);
711 if (network->connected == TRUE) {
712 struct connman_element *element;
713 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
715 switch (network->protocol) {
716 case CONNMAN_NETWORK_PROTOCOL_UNKNOWN:
718 case CONNMAN_NETWORK_PROTOCOL_IP:
719 type = CONNMAN_ELEMENT_TYPE_DHCP;
721 case CONNMAN_NETWORK_PROTOCOL_PPP:
722 type = CONNMAN_ELEMENT_TYPE_PPP;
726 __connman_device_increase_connections(network->device);
728 __connman_device_set_network(network->device, network);
730 connman_device_set_disconnected(network->device, FALSE);
732 element = connman_element_create(NULL);
733 if (element != NULL) {
734 element->type = type;
735 element->index = network->element.index;
737 if (connman_element_register(element,
738 &network->element) < 0)
739 connman_element_unref(element);
741 __connman_service_indicate_state(service,
742 CONNMAN_SERVICE_STATE_CONFIGURATION);
745 connman_element_unregister_children(&network->element);
747 __connman_device_set_network(network->device, NULL);
749 __connman_device_decrease_connections(network->device);
751 __connman_service_indicate_state(service,
752 CONNMAN_SERVICE_STATE_IDLE);
759 * connman_network_set_connected:
760 * @network: network structure
761 * @connected: connected state
763 * Change connected state of network
765 int connman_network_set_connected(struct connman_network *network,
766 connman_bool_t connected)
769 DBusMessageIter entry, value;
770 const char *key = "Connected";
772 DBG("network %p connected %d", network, connected);
774 if (network->connected == connected)
777 network->connected = connected;
779 if (connected == TRUE)
780 network->associating = FALSE;
782 if (network->registered == FALSE) {
783 g_idle_add(set_connected, network);
787 signal = dbus_message_new_signal(network->element.path,
788 CONNMAN_NETWORK_INTERFACE, "PropertyChanged");
792 dbus_message_iter_init_append(signal, &entry);
794 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
796 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
797 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
798 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &connected);
799 dbus_message_iter_close_container(&entry, &value);
801 g_dbus_send_message(connection, signal);
803 set_connected(network);
809 * connman_network_get_connected:
810 * @network: network structure
812 * Get network connection status
814 connman_bool_t connman_network_get_connected(struct connman_network *network)
816 return network->connected;
820 * __connman_network_connect:
821 * @network: network structure
825 int __connman_network_connect(struct connman_network *network)
829 DBG("network %p", network);
831 if (network->connected == TRUE)
834 if (network->driver == NULL)
837 if (network->driver->connect == NULL)
840 __connman_device_disconnect(network->device);
842 err = network->driver->connect(network);
844 network->connected = TRUE;
845 set_connected(network);
852 * __connman_network_disconnect:
853 * @network: network structure
857 int __connman_network_disconnect(struct connman_network *network)
861 DBG("network %p", network);
863 if (network->connected == FALSE)
866 if (network->driver == NULL)
869 if (network->driver->disconnect == NULL)
872 err = network->driver->disconnect(network);
874 network->connected = FALSE;
875 set_connected(network);
882 * connman_network_set_address:
883 * @network: network structure
884 * @address: binary address value
885 * @size: binary address length
887 * Set unique address value for network
889 int connman_network_set_address(struct connman_network *network,
890 const void *address, unsigned int size)
892 const unsigned char *addr_octet = address;
895 DBG("network %p size %d", network, size);
900 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
901 addr_octet[0], addr_octet[1], addr_octet[2],
902 addr_octet[3], addr_octet[4], addr_octet[5]);
906 g_free(network->address);
907 network->address = str;
909 return connman_element_set_string(&network->element,
910 "Address", network->address);
914 * connman_network_set_name:
915 * @network: network structure
918 * Set display name value for network
920 int connman_network_set_name(struct connman_network *network,
923 DBG("network %p name %s", network, name);
925 g_free(network->name);
926 network->name = g_strdup(name);
928 return connman_element_set_string(&network->element, "Name", name);
932 * connman_network_set_strength:
933 * @network: network structure
934 * @strength: strength value
936 * Set signal strength value for network
938 int connman_network_set_strength(struct connman_network *network,
939 connman_uint8_t strength)
941 DBG("network %p strengh %d", network, strength);
943 network->strength = strength;
945 return connman_element_set_uint8(&network->element,
946 "Strength", strength);
950 * connman_network_set_string:
951 * @network: network structure
952 * @key: unique identifier
953 * @value: string value
955 * Set string value for specific key
957 int connman_network_set_string(struct connman_network *network,
958 const char *key, const char *value)
960 DBG("network %p key %s value %s", network, key, value);
962 if (g_strcmp0(key, "Name") == 0)
963 return connman_network_set_name(network, value);
965 if (g_str_equal(key, "Address") == TRUE) {
966 g_free(network->address);
967 network->address = g_strdup(value);
968 } else if (g_str_equal(key, "Node") == TRUE) {
969 g_free(network->node);
970 network->node = g_strdup(value);
971 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
972 g_free(network->wifi.mode);
973 network->wifi.mode = g_strdup(value);
974 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
975 g_free(network->wifi.security);
976 network->wifi.security = g_strdup(value);
977 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
978 g_free(network->wifi.passphrase);
979 network->wifi.passphrase = g_strdup(value);
982 return connman_element_set_string(&network->element, key, value);
986 * connman_network_get_string:
987 * @network: network structure
988 * @key: unique identifier
990 * Get string value for specific key
992 const char *connman_network_get_string(struct connman_network *network,
995 DBG("network %p key %s", network, key);
997 if (g_str_equal(key, "Address") == TRUE)
998 return network->address;
999 else if (g_str_equal(key, "Name") == TRUE)
1000 return network->name;
1001 else if (g_str_equal(key, "Node") == TRUE)
1002 return network->node;
1003 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1004 return network->wifi.mode;
1005 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1006 return network->wifi.security;
1007 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1008 return network->wifi.passphrase;
1010 return connman_element_get_string(&network->element, key);
1014 * connman_network_set_uint8:
1015 * @network: network structure
1016 * @key: unique identifier
1017 * @value: integer value
1019 * Set integer value for specific key
1021 int connman_network_set_uint8(struct connman_network *network,
1022 const char *key, connman_uint8_t value)
1024 DBG("network %p key %s value %d", network, key, value);
1026 if (g_strcmp0(key, "Strength") == 0)
1027 return connman_network_set_strength(network, value);
1029 return connman_element_set_uint8(&network->element, key, value);
1033 * connman_network_get_uint8:
1034 * @network: network structure
1035 * @key: unique identifier
1037 * Get integer value for specific key
1039 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1042 DBG("network %p key %s", network, key);
1044 if (g_str_equal(key, "Strength") == TRUE)
1045 return network->strength;
1047 return connman_element_get_uint8(&network->element, key);
1051 * connman_network_set_uint16:
1052 * @network: network structure
1053 * @key: unique identifier
1054 * @value: integer value
1056 * Set integer value for specific key
1058 int connman_network_set_uint16(struct connman_network *network,
1059 const char *key, connman_uint16_t value)
1061 DBG("network %p key %s value %d", network, key, value);
1063 if (g_str_equal(key, "Frequency") == TRUE)
1064 network->frequency = value;
1065 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1066 network->wifi.channel = value;
1072 * connman_network_get_uint16:
1073 * @network: network structure
1074 * @key: unique identifier
1076 * Get integer value for specific key
1078 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1081 DBG("network %p key %s", network, key);
1083 if (g_str_equal(key, "Frequency") == TRUE)
1084 return network->frequency;
1085 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1086 return network->wifi.channel;
1092 * connman_network_set_blob:
1093 * @network: network structure
1094 * @key: unique identifier
1098 * Set binary blob value for specific key
1100 int connman_network_set_blob(struct connman_network *network,
1101 const char *key, const void *data, unsigned int size)
1103 DBG("network %p key %s size %d", network, key, size);
1105 if (g_strcmp0(key, "Address") == 0)
1106 return connman_network_set_address(network, data, size);
1108 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1109 g_free(network->wifi.ssid);
1110 network->wifi.ssid = g_try_malloc(size);
1111 if (network->wifi.ssid != NULL) {
1112 memcpy(network->wifi.ssid, data, size);
1113 network->wifi.ssid_len = size;
1115 network->wifi.ssid_len = 0;
1118 return connman_element_set_blob(&network->element, key, data, size);
1122 * connman_network_get_blob:
1123 * @network: network structure
1124 * @key: unique identifier
1125 * @size: pointer to blob size
1127 * Get binary blob value for specific key
1129 const void *connman_network_get_blob(struct connman_network *network,
1130 const char *key, unsigned int *size)
1132 DBG("network %p key %s", network, key);
1134 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1136 *size = network->wifi.ssid_len;
1137 return network->wifi.ssid;
1140 return connman_element_get_blob(&network->element, key, size);
1143 void __connman_network_set_device(struct connman_network *network,
1144 struct connman_device *device)
1146 network->device = device;
1150 * connman_network_get_device:
1151 * @network: network structure
1153 * Get parent device of network
1155 struct connman_device *connman_network_get_device(struct connman_network *network)
1157 return network->device;
1161 * connman_network_get_data:
1162 * @network: network structure
1164 * Get private network data pointer
1166 void *connman_network_get_data(struct connman_network *network)
1168 return network->driver_data;
1172 * connman_network_set_data:
1173 * @network: network structure
1174 * @data: data pointer
1176 * Set private network data pointer
1178 void connman_network_set_data(struct connman_network *network, void *data)
1180 network->driver_data = data;
1183 static gboolean match_driver(struct connman_network *network,
1184 struct connman_network_driver *driver)
1186 if (network->type == driver->type ||
1187 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1193 static int network_probe(struct connman_element *element)
1195 struct connman_network *network = element->network;
1199 DBG("element %p name %s", element, element->name);
1201 if (network == NULL)
1204 for (list = driver_list; list; list = list->next) {
1205 struct connman_network_driver *driver = list->data;
1207 if (match_driver(network, driver) == FALSE)
1210 DBG("driver %p name %s", driver, driver->name);
1212 if (driver->probe(network) == 0) {
1213 network->driver = driver;
1218 if (network->driver == NULL)
1221 err = register_interface(element);
1223 if (network->driver->remove)
1224 network->driver->remove(network);
1228 network->secondary = connman_device_get_secondary(network->device);
1230 switch (network->type) {
1231 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1232 case CONNMAN_NETWORK_TYPE_VENDOR:
1233 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1234 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1235 case CONNMAN_NETWORK_TYPE_HSO:
1237 case CONNMAN_NETWORK_TYPE_WIFI:
1238 case CONNMAN_NETWORK_TYPE_WIMAX:
1239 if (network->group != NULL && network->secondary == FALSE)
1240 __connman_profile_add_network(network);
1247 static void network_remove(struct connman_element *element)
1249 struct connman_network *network = element->network;
1251 DBG("element %p name %s", element, element->name);
1253 if (network == NULL)
1256 if (network->driver == NULL)
1259 switch (network->type) {
1260 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1261 case CONNMAN_NETWORK_TYPE_VENDOR:
1262 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1263 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1264 case CONNMAN_NETWORK_TYPE_HSO:
1266 case CONNMAN_NETWORK_TYPE_WIFI:
1267 case CONNMAN_NETWORK_TYPE_WIMAX:
1268 if (network->group != NULL && network->secondary == FALSE) {
1269 __connman_profile_remove_network(network);
1271 g_free(network->group);
1272 network->group = NULL;
1277 unregister_interface(element);
1279 if (network->driver->remove)
1280 network->driver->remove(network);
1283 static void network_change(struct connman_element *element)
1285 struct connman_network *network = element->network;
1287 DBG("element %p name %s", element, element->name);
1289 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1292 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1295 if (network->connected == FALSE)
1298 connman_element_unregister_children(element);
1300 connman_device_set_disconnected(network->device, TRUE);
1302 if (network->driver && network->driver->disconnect) {
1303 network->driver->disconnect(network);
1307 network->connected = FALSE;
1310 static struct connman_driver network_driver = {
1312 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1313 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1314 .probe = network_probe,
1315 .remove = network_remove,
1316 .change = network_change,
1319 int __connman_network_init(void)
1323 connection = connman_dbus_get_connection();
1325 return connman_driver_register(&network_driver);
1328 void __connman_network_cleanup(void)
1332 connman_driver_unregister(&network_driver);
1334 dbus_connection_unref(connection);