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_MBM:
81 case CONNMAN_NETWORK_TYPE_HSO:
88 static DBusMessage *get_properties(DBusConnection *conn,
89 DBusMessage *msg, void *data)
91 struct connman_network *network = data;
93 DBusMessageIter array, dict;
97 if (__connman_security_check_privilege(msg,
98 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
99 return __connman_error_permission_denied(msg);
101 reply = dbus_message_new_method_return(msg);
105 dbus_message_iter_init_append(reply, &array);
107 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
108 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
109 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
110 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
112 if (network->device) {
113 const char *path = connman_device_get_path(network->device);
115 connman_dbus_dict_append_variant(&dict, "Device",
116 DBUS_TYPE_OBJECT_PATH, &path);
119 if (network->address != NULL)
120 connman_dbus_dict_append_variant(&dict, "Address",
121 DBUS_TYPE_STRING, &network->address);
123 if (network->name != NULL)
124 connman_dbus_dict_append_variant(&dict, "Name",
125 DBUS_TYPE_STRING, &network->name);
127 connman_dbus_dict_append_variant(&dict, "Connected",
128 DBUS_TYPE_BOOLEAN, &network->connected);
130 if (network->strength > 0)
131 connman_dbus_dict_append_variant(&dict, "Strength",
132 DBUS_TYPE_BYTE, &network->strength);
134 if (network->frequency > 0)
135 connman_dbus_dict_append_variant(&dict, "Frequency",
136 DBUS_TYPE_UINT16, &network->frequency);
138 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
139 connman_dbus_dict_append_array(&dict, "WiFi.SSID",
140 DBUS_TYPE_BYTE, &network->wifi.ssid,
141 network->wifi.ssid_len);
143 if (network->wifi.mode != NULL)
144 connman_dbus_dict_append_variant(&dict, "WiFi.Mode",
145 DBUS_TYPE_STRING, &network->wifi.mode);
147 if (network->wifi.channel > 0)
148 connman_dbus_dict_append_variant(&dict, "WiFi.Channel",
149 DBUS_TYPE_UINT16, &network->wifi.channel);
151 if (network->wifi.security != NULL)
152 connman_dbus_dict_append_variant(&dict, "WiFi.Security",
153 DBUS_TYPE_STRING, &network->wifi.security);
155 if (network->wifi.passphrase != NULL &&
156 __connman_security_check_privilege(msg,
157 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
158 connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase",
159 DBUS_TYPE_STRING, &network->wifi.passphrase);
161 dbus_message_iter_close_container(&array, &dict);
166 static DBusMessage *set_property(DBusConnection *conn,
167 DBusMessage *msg, void *data)
169 struct connman_network *network = data;
170 DBusMessageIter iter, value;
174 DBG("conn %p", conn);
176 if (dbus_message_iter_init(msg, &iter) == FALSE)
177 return __connman_error_invalid_arguments(msg);
179 dbus_message_iter_get_basic(&iter, &name);
180 dbus_message_iter_next(&iter);
181 dbus_message_iter_recurse(&iter, &value);
183 if (__connman_security_check_privilege(msg,
184 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
185 return __connman_error_permission_denied(msg);
187 type = dbus_message_iter_get_arg_type(&value);
189 if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
190 const char *passphrase;
192 if (type != DBUS_TYPE_STRING)
193 return __connman_error_invalid_arguments(msg);
195 if (__connman_security_check_privilege(msg,
196 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
197 return __connman_error_permission_denied(msg);
199 dbus_message_iter_get_basic(&value, &passphrase);
201 g_free(network->wifi.passphrase);
202 network->wifi.passphrase = g_strdup(passphrase);
203 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
206 err = __connman_ipconfig_set_ipv4(network->ipconfig,
209 return __connman_error_failed(msg, -err);
211 return __connman_error_invalid_property(msg);
213 __connman_storage_save_network(network);
215 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
218 static DBusMessage *do_connect(DBusConnection *conn,
219 DBusMessage *msg, void *data)
221 struct connman_network *network = data;
224 DBG("conn %p", conn);
226 if (__connman_security_check_privilege(msg,
227 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
228 return __connman_error_permission_denied(msg);
230 if (network->connected == TRUE)
231 return __connman_error_failed(msg, EALREADY);
233 if (network->driver && network->driver->connect) {
234 enum connman_device_mode mode;
236 mode = connman_device_get_mode(network->device);
237 if (mode == CONNMAN_DEVICE_MODE_NETWORK_SINGLE)
238 __connman_device_disconnect(network->device);
240 err = network->driver->connect(network);
241 if (err < 0 && err != -EINPROGRESS)
242 return __connman_error_failed(msg, -err);
244 network->connected = TRUE;
246 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
249 static DBusMessage *do_disconnect(DBusConnection *conn,
250 DBusMessage *msg, void *data)
252 struct connman_network *network = data;
255 DBG("conn %p", conn);
257 if (__connman_security_check_privilege(msg,
258 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
259 return __connman_error_permission_denied(msg);
261 if (network->connected == FALSE)
262 return __connman_error_failed(msg, EINVAL);
264 connman_element_unregister_children(&network->element);
266 connman_device_set_disconnected(network->device, TRUE);
268 if (network->driver && network->driver->disconnect) {
269 err = network->driver->disconnect(network);
270 if (err < 0 && err != -EINPROGRESS)
271 return __connman_error_failed(msg, -err);
273 network->connected = FALSE;
275 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
278 static GDBusMethodTable network_methods[] = {
279 { "GetProperties", "", "a{sv}", get_properties },
280 { "SetProperty", "sv", "", set_property },
281 { "Connect", "", "", do_connect },
282 { "Disconnect", "", "", do_disconnect },
286 static GDBusSignalTable network_signals[] = {
287 { "PropertyChanged", "sv" },
291 static DBusConnection *connection;
293 static void append_networks(struct connman_device *device,
294 DBusMessageIter *entry)
296 DBusMessageIter value, iter;
297 const char *key = "Networks";
299 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
301 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
302 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
305 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
306 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
307 __connman_element_list((struct connman_element *) device,
308 CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
309 dbus_message_iter_close_container(&value, &iter);
311 dbus_message_iter_close_container(entry, &value);
314 static void emit_networks_signal(struct connman_device *device)
316 const char *path = connman_device_get_path(device);
318 DBusMessageIter entry;
320 signal = dbus_message_new_signal(path,
321 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
325 dbus_message_iter_init_append(signal, &entry);
327 append_networks(device, &entry);
329 g_dbus_send_message(connection, signal);
332 static int register_interface(struct connman_element *element)
334 struct connman_network *network = element->network;
336 DBG("element %p name %s", element, element->name);
338 if (g_dbus_register_interface(connection, element->path,
339 CONNMAN_NETWORK_INTERFACE,
340 network_methods, network_signals,
341 NULL, network, NULL) == FALSE) {
342 connman_error("Failed to register %s network", element->path);
346 network->registered = TRUE;
348 emit_networks_signal(network->device);
353 static void unregister_interface(struct connman_element *element)
355 struct connman_network * network = element->network;
357 DBG("element %p name %s", element, element->name);
359 network->registered = FALSE;
361 emit_networks_signal(network->device);
363 g_dbus_unregister_interface(connection, element->path,
364 CONNMAN_NETWORK_INTERFACE);
367 connman_bool_t __connman_network_has_driver(struct connman_network *network)
369 if (network == NULL || network->driver == NULL)
372 return network->registered;
375 static GSList *driver_list = NULL;
377 static gint compare_priority(gconstpointer a, gconstpointer b)
379 const struct connman_network_driver *driver1 = a;
380 const struct connman_network_driver *driver2 = b;
382 return driver2->priority - driver1->priority;
386 * connman_network_driver_register:
387 * @driver: network driver definition
389 * Register a new network driver
391 * Returns: %0 on success
393 int connman_network_driver_register(struct connman_network_driver *driver)
395 DBG("driver %p name %s", driver, driver->name);
397 driver_list = g_slist_insert_sorted(driver_list, driver,
404 * connman_network_driver_unregister:
405 * @driver: network driver definition
407 * Remove a previously registered network driver
409 void connman_network_driver_unregister(struct connman_network_driver *driver)
411 DBG("driver %p name %s", driver, driver->name);
413 driver_list = g_slist_remove(driver_list, driver);
416 static void network_destruct(struct connman_element *element)
418 struct connman_network *network = element->network;
420 DBG("element %p name %s", element, element->name);
422 g_free(network->wifi.ssid);
423 g_free(network->wifi.mode);
424 g_free(network->wifi.security);
425 g_free(network->wifi.passphrase);
427 g_free(network->group);
428 g_free(network->node);
429 g_free(network->name);
430 g_free(network->address);
431 g_free(network->identifier);
433 connman_ipconfig_unref(network->ipconfig);
437 * connman_network_create:
438 * @identifier: network identifier (for example an unqiue name)
440 * Allocate a new network and assign the #identifier to it.
442 * Returns: a newly-allocated #connman_network structure
444 struct connman_network *connman_network_create(const char *identifier,
445 enum connman_network_type type)
447 struct connman_network *network;
448 connman_uint8_t strength = 0;
452 DBG("identifier %s type %d", identifier, type);
454 network = g_try_new0(struct connman_network, 1);
458 DBG("network %p", network);
460 __connman_element_initialize(&network->element);
462 //temp = connman_dbus_encode_string(identifier);
463 temp = g_strdup(identifier);
469 network->element.name = temp;
470 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
472 network->element.network = network;
473 network->element.destruct = network_destruct;
475 str = type2string(type);
477 connman_element_set_string(&network->element, "Type", str);
479 connman_element_set_uint8(&network->element, "Strength", strength);
481 network->type = type;
482 network->secondary = FALSE;
483 network->identifier = g_strdup(identifier);
485 network->ipconfig = connman_ipconfig_create();
486 if (network->ipconfig == NULL) {
487 connman_network_unref(network);
495 * connman_network_ref:
496 * @network: network structure
498 * Increase reference counter of network
500 struct connman_network *connman_network_ref(struct connman_network *network)
502 if (connman_element_ref(&network->element) == NULL)
509 * connman_network_unref:
510 * @network: network structure
512 * Decrease reference counter of network
514 void connman_network_unref(struct connman_network *network)
516 connman_element_unref(&network->element);
519 const char *__connman_network_get_type(struct connman_network *network)
521 return type2string(network->type);
525 * connman_network_get_type:
526 * @network: network structure
528 * Get type of network
530 enum connman_network_type connman_network_get_type(struct connman_network *network)
532 return network->type;
536 * connman_network_get_identifier:
537 * @network: network structure
539 * Get identifier of network
541 const char *connman_network_get_identifier(struct connman_network *network)
543 return network->identifier;
547 * connman_network_get_path:
548 * @network: network structure
550 * Get path name of network
552 const char *connman_network_get_path(struct connman_network *network)
554 return network->element.path;
558 * connman_network_set_index:
559 * @network: network structure
560 * @index: index number
562 * Set index number of network
564 void connman_network_set_index(struct connman_network *network, int index)
566 network->element.index = index;
570 * connman_network_get_index:
571 * @network: network structure
573 * Get index number of network
575 int connman_network_get_index(struct connman_network *network)
577 return network->element.index;
581 * connman_network_set_protocol:
582 * @network: network structure
583 * @protocol: network protocol
585 * Change protocol of network
587 void connman_network_set_protocol(struct connman_network *network,
588 enum connman_network_protocol protocol)
590 network->protocol = protocol;
594 * connman_network_set_group:
595 * @network: network structure
598 * Set group name for automatic clustering
600 void connman_network_set_group(struct connman_network *network,
603 if (network->secondary == TRUE)
606 if (g_strcmp0(network->group, group) == 0)
609 switch (network->type) {
610 case CONNMAN_NETWORK_TYPE_UNKNOWN:
611 case CONNMAN_NETWORK_TYPE_VENDOR:
612 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
613 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
615 case CONNMAN_NETWORK_TYPE_MBM:
616 case CONNMAN_NETWORK_TYPE_HSO:
617 case CONNMAN_NETWORK_TYPE_WIFI:
618 case CONNMAN_NETWORK_TYPE_WIMAX:
622 if (network->group != NULL) {
623 __connman_profile_remove_network(network);
625 g_free(network->group);
628 network->group = g_strdup(group);
630 if (network->group != NULL)
631 __connman_profile_add_network(network);
635 * connman_network_get_group:
636 * @network: network structure
638 * Get group name for automatic clustering
640 const char *connman_network_get_group(struct connman_network *network)
642 return network->group;
645 const char *__connman_network_get_ident(struct connman_network *network)
647 if (network->device == NULL)
650 return __connman_device_get_ident(network->device);
654 * connman_network_set_available:
655 * @network: network structure
656 * @available: availability state
658 * Change availability state of network (in range)
660 int connman_network_set_available(struct connman_network *network,
661 connman_bool_t available)
663 DBG("network %p available %d", network, available);
665 if (network->available == available)
668 network->available = available;
674 * connman_network_get_available:
675 * @network: network structure
677 * Get network available setting
679 connman_bool_t connman_network_get_available(struct connman_network *network)
681 return network->available;
685 * connman_network_set_associating:
686 * @network: network structure
687 * @associating: associating state
689 * Change associating state of network
691 int connman_network_set_associating(struct connman_network *network,
692 connman_bool_t associating)
694 DBG("network %p associating %d", network, associating);
696 if (network->associating == associating)
699 network->associating = associating;
701 if (associating == TRUE) {
702 struct connman_service *service;
704 service = __connman_service_lookup_from_network(network);
705 __connman_service_indicate_state(service,
706 CONNMAN_SERVICE_STATE_ASSOCIATION);
712 static gboolean set_connected(gpointer user_data)
714 struct connman_network *network = user_data;
715 struct connman_service *service;
717 service = __connman_service_lookup_from_network(network);
719 if (network->connected == TRUE) {
720 struct connman_element *element;
721 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
723 switch (network->protocol) {
724 case CONNMAN_NETWORK_PROTOCOL_UNKNOWN:
726 case CONNMAN_NETWORK_PROTOCOL_IP:
727 type = CONNMAN_ELEMENT_TYPE_DHCP;
729 case CONNMAN_NETWORK_PROTOCOL_PPP:
730 type = CONNMAN_ELEMENT_TYPE_PPP;
734 __connman_device_increase_connections(network->device);
736 __connman_device_set_network(network->device, network);
738 connman_device_set_disconnected(network->device, FALSE);
740 element = connman_element_create(NULL);
741 if (element != NULL) {
742 element->type = type;
743 element->index = network->element.index;
745 if (connman_element_register(element,
746 &network->element) < 0)
747 connman_element_unref(element);
749 __connman_service_indicate_state(service,
750 CONNMAN_SERVICE_STATE_CONFIGURATION);
753 connman_element_unregister_children(&network->element);
755 __connman_device_set_network(network->device, NULL);
757 __connman_device_decrease_connections(network->device);
759 __connman_service_indicate_state(service,
760 CONNMAN_SERVICE_STATE_IDLE);
767 * connman_network_set_connected:
768 * @network: network structure
769 * @connected: connected state
771 * Change connected state of network
773 int connman_network_set_connected(struct connman_network *network,
774 connman_bool_t connected)
777 DBusMessageIter entry, value;
778 const char *key = "Connected";
780 DBG("network %p connected %d", network, connected);
782 if (network->connected == connected)
785 network->connected = connected;
787 if (connected == TRUE)
788 network->associating = FALSE;
790 if (network->registered == FALSE) {
791 g_idle_add(set_connected, network);
795 signal = dbus_message_new_signal(network->element.path,
796 CONNMAN_NETWORK_INTERFACE, "PropertyChanged");
800 dbus_message_iter_init_append(signal, &entry);
802 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
804 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
805 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
806 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &connected);
807 dbus_message_iter_close_container(&entry, &value);
809 g_dbus_send_message(connection, signal);
811 set_connected(network);
817 * connman_network_get_connected:
818 * @network: network structure
820 * Get network connection status
822 connman_bool_t connman_network_get_connected(struct connman_network *network)
824 return network->connected;
828 * __connman_network_connect:
829 * @network: network structure
833 int __connman_network_connect(struct connman_network *network)
837 DBG("network %p", network);
839 if (network->connected == TRUE)
842 if (network->driver == NULL)
845 if (network->driver->connect == NULL)
848 __connman_device_disconnect(network->device);
850 err = network->driver->connect(network);
852 network->connected = TRUE;
853 set_connected(network);
860 * __connman_network_disconnect:
861 * @network: network structure
865 int __connman_network_disconnect(struct connman_network *network)
869 DBG("network %p", network);
871 if (network->connected == FALSE)
874 if (network->driver == NULL)
877 if (network->driver->disconnect == NULL)
880 err = network->driver->disconnect(network);
882 network->connected = FALSE;
883 set_connected(network);
890 * connman_network_set_address:
891 * @network: network structure
892 * @address: binary address value
893 * @size: binary address length
895 * Set unique address value for network
897 int connman_network_set_address(struct connman_network *network,
898 const void *address, unsigned int size)
900 const unsigned char *addr_octet = address;
903 DBG("network %p size %d", network, size);
908 str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
909 addr_octet[0], addr_octet[1], addr_octet[2],
910 addr_octet[3], addr_octet[4], addr_octet[5]);
914 g_free(network->address);
915 network->address = str;
917 return connman_element_set_string(&network->element,
918 "Address", network->address);
922 * connman_network_set_name:
923 * @network: network structure
926 * Set display name value for network
928 int connman_network_set_name(struct connman_network *network,
931 DBG("network %p name %s", network, name);
933 g_free(network->name);
934 network->name = g_strdup(name);
936 return connman_element_set_string(&network->element, "Name", name);
940 * connman_network_set_strength:
941 * @network: network structure
942 * @strength: strength value
944 * Set signal strength value for network
946 int connman_network_set_strength(struct connman_network *network,
947 connman_uint8_t strength)
949 DBG("network %p strengh %d", network, strength);
951 network->strength = strength;
953 return connman_element_set_uint8(&network->element,
954 "Strength", strength);
958 * connman_network_set_string:
959 * @network: network structure
960 * @key: unique identifier
961 * @value: string value
963 * Set string value for specific key
965 int connman_network_set_string(struct connman_network *network,
966 const char *key, const char *value)
968 DBG("network %p key %s value %s", network, key, value);
970 if (g_strcmp0(key, "Name") == 0)
971 return connman_network_set_name(network, value);
973 if (g_str_equal(key, "Address") == TRUE) {
974 g_free(network->address);
975 network->address = g_strdup(value);
976 } else if (g_str_equal(key, "Node") == TRUE) {
977 g_free(network->node);
978 network->node = g_strdup(value);
979 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
980 g_free(network->wifi.mode);
981 network->wifi.mode = g_strdup(value);
982 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
983 g_free(network->wifi.security);
984 network->wifi.security = g_strdup(value);
985 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
986 g_free(network->wifi.passphrase);
987 network->wifi.passphrase = g_strdup(value);
990 return connman_element_set_string(&network->element, key, value);
994 * connman_network_get_string:
995 * @network: network structure
996 * @key: unique identifier
998 * Get string value for specific key
1000 const char *connman_network_get_string(struct connman_network *network,
1003 DBG("network %p key %s", network, key);
1005 if (g_str_equal(key, "Address") == TRUE)
1006 return network->address;
1007 else if (g_str_equal(key, "Name") == TRUE)
1008 return network->name;
1009 else if (g_str_equal(key, "Node") == TRUE)
1010 return network->node;
1011 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1012 return network->wifi.mode;
1013 else if (g_str_equal(key, "WiFi.Security") == TRUE)
1014 return network->wifi.security;
1015 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1016 return network->wifi.passphrase;
1018 return connman_element_get_string(&network->element, key);
1022 * connman_network_set_uint8:
1023 * @network: network structure
1024 * @key: unique identifier
1025 * @value: integer value
1027 * Set integer value for specific key
1029 int connman_network_set_uint8(struct connman_network *network,
1030 const char *key, connman_uint8_t value)
1032 DBG("network %p key %s value %d", network, key, value);
1034 if (g_strcmp0(key, "Strength") == 0)
1035 return connman_network_set_strength(network, value);
1037 return connman_element_set_uint8(&network->element, key, value);
1041 * connman_network_get_uint8:
1042 * @network: network structure
1043 * @key: unique identifier
1045 * Get integer value for specific key
1047 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1050 DBG("network %p key %s", network, key);
1052 if (g_str_equal(key, "Strength") == TRUE)
1053 return network->strength;
1055 return connman_element_get_uint8(&network->element, key);
1059 * connman_network_set_uint16:
1060 * @network: network structure
1061 * @key: unique identifier
1062 * @value: integer value
1064 * Set integer value for specific key
1066 int connman_network_set_uint16(struct connman_network *network,
1067 const char *key, connman_uint16_t value)
1069 DBG("network %p key %s value %d", network, key, value);
1071 if (g_str_equal(key, "Frequency") == TRUE)
1072 network->frequency = value;
1073 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1074 network->wifi.channel = value;
1080 * connman_network_get_uint16:
1081 * @network: network structure
1082 * @key: unique identifier
1084 * Get integer value for specific key
1086 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1089 DBG("network %p key %s", network, key);
1091 if (g_str_equal(key, "Frequency") == TRUE)
1092 return network->frequency;
1093 else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1094 return network->wifi.channel;
1100 * connman_network_set_blob:
1101 * @network: network structure
1102 * @key: unique identifier
1106 * Set binary blob value for specific key
1108 int connman_network_set_blob(struct connman_network *network,
1109 const char *key, const void *data, unsigned int size)
1111 DBG("network %p key %s size %d", network, key, size);
1113 if (g_strcmp0(key, "Address") == 0)
1114 return connman_network_set_address(network, data, size);
1116 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1117 g_free(network->wifi.ssid);
1118 network->wifi.ssid = g_try_malloc(size);
1119 if (network->wifi.ssid != NULL) {
1120 memcpy(network->wifi.ssid, data, size);
1121 network->wifi.ssid_len = size;
1123 network->wifi.ssid_len = 0;
1126 return connman_element_set_blob(&network->element, key, data, size);
1130 * connman_network_get_blob:
1131 * @network: network structure
1132 * @key: unique identifier
1133 * @size: pointer to blob size
1135 * Get binary blob value for specific key
1137 const void *connman_network_get_blob(struct connman_network *network,
1138 const char *key, unsigned int *size)
1140 DBG("network %p key %s", network, key);
1142 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1144 *size = network->wifi.ssid_len;
1145 return network->wifi.ssid;
1148 return connman_element_get_blob(&network->element, key, size);
1151 void __connman_network_set_device(struct connman_network *network,
1152 struct connman_device *device)
1154 network->device = device;
1158 * connman_network_get_device:
1159 * @network: network structure
1161 * Get parent device of network
1163 struct connman_device *connman_network_get_device(struct connman_network *network)
1165 return network->device;
1169 * connman_network_get_data:
1170 * @network: network structure
1172 * Get private network data pointer
1174 void *connman_network_get_data(struct connman_network *network)
1176 return network->driver_data;
1180 * connman_network_set_data:
1181 * @network: network structure
1182 * @data: data pointer
1184 * Set private network data pointer
1186 void connman_network_set_data(struct connman_network *network, void *data)
1188 network->driver_data = data;
1191 static gboolean match_driver(struct connman_network *network,
1192 struct connman_network_driver *driver)
1194 if (network->type == driver->type ||
1195 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1201 static int network_probe(struct connman_element *element)
1203 struct connman_network *network = element->network;
1207 DBG("element %p name %s", element, element->name);
1209 if (network == NULL)
1212 for (list = driver_list; list; list = list->next) {
1213 struct connman_network_driver *driver = list->data;
1215 if (match_driver(network, driver) == FALSE)
1218 DBG("driver %p name %s", driver, driver->name);
1220 if (driver->probe(network) == 0) {
1221 network->driver = driver;
1226 if (network->driver == NULL)
1229 err = register_interface(element);
1231 if (network->driver->remove)
1232 network->driver->remove(network);
1236 network->secondary = connman_device_get_secondary(network->device);
1238 switch (network->type) {
1239 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1240 case CONNMAN_NETWORK_TYPE_VENDOR:
1241 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1242 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1244 case CONNMAN_NETWORK_TYPE_MBM:
1245 case CONNMAN_NETWORK_TYPE_HSO:
1246 case CONNMAN_NETWORK_TYPE_WIFI:
1247 case CONNMAN_NETWORK_TYPE_WIMAX:
1248 if (network->group != NULL && network->secondary == FALSE)
1249 __connman_profile_add_network(network);
1256 static void network_remove(struct connman_element *element)
1258 struct connman_network *network = element->network;
1260 DBG("element %p name %s", element, element->name);
1262 if (network == NULL)
1265 if (network->driver == NULL)
1268 switch (network->type) {
1269 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1270 case CONNMAN_NETWORK_TYPE_VENDOR:
1271 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1272 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1274 case CONNMAN_NETWORK_TYPE_MBM:
1275 case CONNMAN_NETWORK_TYPE_HSO:
1276 case CONNMAN_NETWORK_TYPE_WIFI:
1277 case CONNMAN_NETWORK_TYPE_WIMAX:
1278 if (network->group != NULL && network->secondary == FALSE) {
1279 __connman_profile_remove_network(network);
1281 g_free(network->group);
1282 network->group = NULL;
1287 unregister_interface(element);
1289 if (network->driver->remove)
1290 network->driver->remove(network);
1293 static void network_change(struct connman_element *element)
1295 struct connman_network *network = element->network;
1297 DBG("element %p name %s", element, element->name);
1299 if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1302 if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1305 if (network->connected == FALSE)
1308 connman_element_unregister_children(element);
1310 connman_device_set_disconnected(network->device, TRUE);
1312 if (network->driver && network->driver->disconnect) {
1313 network->driver->disconnect(network);
1317 network->connected = FALSE;
1320 static struct connman_driver network_driver = {
1322 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
1323 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1324 .probe = network_probe,
1325 .remove = network_remove,
1326 .change = network_change,
1329 int __connman_network_init(void)
1333 connection = connman_dbus_get_connection();
1335 return connman_driver_register(&network_driver);
1338 void __connman_network_cleanup(void)
1342 connman_driver_unregister(&network_driver);
1344 dbus_connection_unref(connection);