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
32 struct connman_network {
33 struct connman_element element;
34 enum connman_network_type type;
35 enum connman_network_protocol protocol;
36 connman_bool_t connected;
37 connman_bool_t remember;
38 connman_uint8_t strength;
43 struct connman_network_driver *driver;
46 connman_bool_t registered;
48 struct connman_device *device;
59 static const char *type2string(enum connman_network_type type)
62 case CONNMAN_NETWORK_TYPE_UNKNOWN:
63 case CONNMAN_NETWORK_TYPE_VENDOR:
65 case CONNMAN_NETWORK_TYPE_WIFI:
67 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
68 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
70 case CONNMAN_NETWORK_TYPE_HSO:
77 static DBusMessage *get_properties(DBusConnection *conn,
78 DBusMessage *msg, void *data)
80 struct connman_network *network = data;
82 DBusMessageIter array, dict;
86 reply = dbus_message_new_method_return(msg);
90 dbus_message_iter_init_append(reply, &array);
92 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
93 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
94 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
95 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
97 if (network->device) {
98 const char *path = connman_device_get_path(network->device);
100 connman_dbus_dict_append_variant(&dict, "Device",
101 DBUS_TYPE_OBJECT_PATH, &path);
104 if (network->name != NULL)
105 connman_dbus_dict_append_variant(&dict, "Name",
106 DBUS_TYPE_STRING, &network->name);
108 connman_dbus_dict_append_variant(&dict, "Connected",
109 DBUS_TYPE_BOOLEAN, &network->connected);
111 connman_dbus_dict_append_variant(&dict, "Remember",
112 DBUS_TYPE_BOOLEAN, &network->remember);
114 if (network->strength > 0)
115 connman_dbus_dict_append_variant(&dict, "Strength",
116 DBUS_TYPE_BYTE, &network->strength);
118 if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
119 connman_dbus_dict_append_array(&dict, "WiFi.SSID",
120 DBUS_TYPE_BYTE, &network->wifi.ssid,
121 network->wifi.ssid_len);
123 if (network->wifi.mode != NULL)
124 connman_dbus_dict_append_variant(&dict, "WiFi.Mode",
125 DBUS_TYPE_STRING, &network->wifi.mode);
127 if (network->wifi.security != NULL)
128 connman_dbus_dict_append_variant(&dict, "WiFi.Security",
129 DBUS_TYPE_STRING, &network->wifi.security);
131 if (network->wifi.passphrase != NULL)
132 connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase",
133 DBUS_TYPE_STRING, &network->wifi.passphrase);
135 dbus_message_iter_close_container(&array, &dict);
140 static DBusMessage *set_property(DBusConnection *conn,
141 DBusMessage *msg, void *data)
143 struct connman_network *network = data;
144 DBusMessageIter iter, value;
147 DBG("conn %p", conn);
149 if (dbus_message_iter_init(msg, &iter) == FALSE)
150 return __connman_error_invalid_arguments(msg);
152 dbus_message_iter_get_basic(&iter, &name);
153 dbus_message_iter_next(&iter);
154 dbus_message_iter_recurse(&iter, &value);
156 if (__connman_security_check_privileges(msg) < 0)
157 return __connman_error_permission_denied(msg);
159 if (g_str_equal(name, "Remember") == TRUE) {
160 connman_bool_t remember;
162 dbus_message_iter_get_basic(&value, &remember);
164 if (network->remember == remember)
165 return __connman_error_invalid_arguments(msg);
166 } else if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
167 const char *passphrase;
169 dbus_message_iter_get_basic(&value, &passphrase);
171 g_free(network->wifi.passphrase);
172 network->wifi.passphrase = g_strdup(passphrase);
175 __connman_storage_save_network(network);
177 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
180 static DBusMessage *do_connect(DBusConnection *conn,
181 DBusMessage *msg, void *data)
183 struct connman_network *network = data;
186 DBG("conn %p", conn);
188 if (network->connected == TRUE)
189 return __connman_error_failed(msg);
191 if (network->driver && network->driver->connect) {
192 err = network->driver->connect(network);
193 if (err < 0 && err != -EINPROGRESS)
194 return __connman_error_failed(msg);
196 network->connected = TRUE;
198 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
201 static DBusMessage *do_disconnect(DBusConnection *conn,
202 DBusMessage *msg, void *data)
204 struct connman_network *network = data;
207 DBG("conn %p", conn);
209 if (network->connected == FALSE)
210 return __connman_error_failed(msg);
212 connman_element_unregister_children(&network->element);
214 if (network->driver && network->driver->disconnect) {
215 err = network->driver->disconnect(network);
216 if (err < 0 && err != -EINPROGRESS)
217 return __connman_error_failed(msg);
219 network->connected = FALSE;
221 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
224 static GDBusMethodTable network_methods[] = {
225 { "GetProperties", "", "a{sv}", get_properties },
226 { "SetProperty", "sv", "", set_property },
227 { "Connect", "", "", do_connect },
228 { "Disconnect", "", "", do_disconnect },
232 static GDBusSignalTable network_signals[] = {
233 { "PropertyChanged", "sv" },
237 static DBusConnection *connection;
239 static void emit_networks_signal(void)
243 static int register_interface(struct connman_element *element)
245 struct connman_network *network = element->network;
247 DBG("element %p name %s", element, element->name);
249 if (g_dbus_register_interface(connection, element->path,
250 CONNMAN_NETWORK_INTERFACE,
251 network_methods, network_signals,
252 NULL, network, NULL) == FALSE) {
253 connman_error("Failed to register %s network", element->path);
257 network->registered = TRUE;
259 emit_networks_signal();
264 static void unregister_interface(struct connman_element *element)
266 struct connman_network * network = element->network;
268 DBG("element %p name %s", element, element->name);
270 network->registered = FALSE;
272 emit_networks_signal();
274 g_dbus_unregister_interface(connection, element->path,
275 CONNMAN_NETWORK_INTERFACE);
278 connman_bool_t __connman_network_has_driver(struct connman_network *network)
280 if (network == NULL || network->driver == NULL)
283 return network->registered;
286 static GSList *driver_list = NULL;
288 static gint compare_priority(gconstpointer a, gconstpointer b)
290 const struct connman_network_driver *driver1 = a;
291 const struct connman_network_driver *driver2 = b;
293 return driver2->priority - driver1->priority;
297 * connman_network_driver_register:
298 * @driver: network driver definition
300 * Register a new network driver
302 * Returns: %0 on success
304 int connman_network_driver_register(struct connman_network_driver *driver)
306 DBG("driver %p name %s", driver, driver->name);
308 driver_list = g_slist_insert_sorted(driver_list, driver,
315 * connman_network_driver_unregister:
316 * @driver: network driver definition
318 * Remove a previously registered network driver
320 void connman_network_driver_unregister(struct connman_network_driver *driver)
322 DBG("driver %p name %s", driver, driver->name);
324 driver_list = g_slist_remove(driver_list, driver);
327 static void network_destruct(struct connman_element *element)
329 struct connman_network *network = element->network;
331 DBG("element %p name %s", element, element->name);
333 g_free(network->wifi.ssid);
334 g_free(network->wifi.mode);
335 g_free(network->wifi.security);
336 g_free(network->wifi.passphrase);
338 g_free(network->node);
339 g_free(network->name);
340 g_free(network->identifier);
344 * connman_network_create:
345 * @identifier: network identifier (for example an unqiue name)
347 * Allocate a new network and assign the #identifier to it.
349 * Returns: a newly-allocated #connman_network structure
351 struct connman_network *connman_network_create(const char *identifier,
352 enum connman_network_type type)
354 struct connman_network *network;
357 DBG("identifier %s type %d", identifier, type);
359 network = g_try_new0(struct connman_network, 1);
363 DBG("network %p", network);
365 network->element.refcount = 1;
367 network->element.name = g_strdup(identifier);
368 network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
369 network->element.index = -1;
371 network->element.network = network;
372 network->element.destruct = network_destruct;
374 str = type2string(type);
376 connman_element_add_static_property(&network->element,
377 "Type", DBUS_TYPE_STRING, &str);
379 network->type = type;
380 network->identifier = g_strdup(identifier);
386 * connman_network_ref:
387 * @network: network structure
389 * Increase reference counter of network
391 struct connman_network *connman_network_ref(struct connman_network *network)
393 if (connman_element_ref(&network->element) == NULL)
400 * connman_network_unref:
401 * @network: network structure
403 * Decrease reference counter of network
405 void connman_network_unref(struct connman_network *network)
407 connman_element_unref(&network->element);
411 * connman_network_get_identifier:
412 * @network: network structure
414 * Get identifier of network
416 const char *connman_network_get_identifier(struct connman_network *network)
418 return network->identifier;
422 * connman_network_get_path:
423 * @network: network structure
425 * Get path name of network
427 const char *connman_network_get_path(struct connman_network *network)
429 return network->element.path;
433 * connman_network_set_index:
434 * @network: network structure
435 * @index: index number
437 * Set index number of network
439 void connman_network_set_index(struct connman_network *network, int index)
441 network->element.index = index;
445 * connman_network_get_index:
446 * @network: network structure
448 * Get index number of network
450 int connman_network_get_index(struct connman_network *network)
452 return network->element.index;
456 * connman_network_set_protocol:
457 * @network: network structure
458 * @protocol: network protocol
460 * Change protocol of network
462 void connman_network_set_protocol(struct connman_network *network,
463 enum connman_network_protocol protocol)
465 network->protocol = protocol;
469 * connman_network_set_connected:
470 * @network: network structure
471 * @connected: connected state
473 * Change connected state of network
475 int connman_network_set_connected(struct connman_network *network,
476 connman_bool_t connected)
478 DBG("network %p connected %d", network, connected);
480 if (network->connected == connected)
483 network->connected = connected;
485 if (connected == TRUE) {
486 struct connman_element *element;
487 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
489 switch (network->protocol) {
490 case CONNMAN_NETWORK_PROTOCOL_UNKNOWN:
492 case CONNMAN_NETWORK_PROTOCOL_IP:
493 type = CONNMAN_ELEMENT_TYPE_DHCP;
495 case CONNMAN_NETWORK_PROTOCOL_PPP:
496 type = CONNMAN_ELEMENT_TYPE_PPP;
500 __connman_device_increase_connections(network->device);
502 element = connman_element_create(NULL);
503 if (element != NULL) {
504 element->type = type;
505 element->index = network->element.index;
507 if (connman_element_register(element,
508 &network->element) < 0)
509 connman_element_unref(element);
512 connman_element_unregister_children(&network->element);
514 __connman_device_decrease_connections(network->device);
521 * connman_network_get_remember:
522 * @network: network structure
524 * Get network remember setting
526 connman_bool_t connman_network_get_remember(struct connman_network *network)
528 return network->remember;
532 * connman_network_connect:
533 * @network: network structure
537 int connman_network_connect(struct connman_network *network)
539 if (network->connected == TRUE)
542 if (network->driver && network->driver->connect)
543 return network->driver->connect(network);
545 network->connected = TRUE;
551 * connman_network_set_string:
552 * @network: network structure
553 * @key: unique identifier
554 * @value: string value
556 * Set string value for specific key
558 int connman_network_set_string(struct connman_network *network,
559 const char *key, const char *value)
561 DBG("network %p key %s value %s", network, key, value);
563 if (g_str_equal(key, "Name") == TRUE) {
564 g_free(network->name);
565 network->name = g_strdup(value);
566 } else if (g_str_equal(key, "Node") == TRUE) {
567 g_free(network->node);
568 network->node = g_strdup(value);
569 } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
570 g_free(network->wifi.mode);
571 network->wifi.mode = g_strdup(value);
572 } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
573 g_free(network->wifi.security);
574 network->wifi.security = g_strdup(value);
575 } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
576 g_free(network->wifi.passphrase);
577 network->wifi.passphrase = g_strdup(value);
584 * connman_network_get_string:
585 * @network: network structure
586 * @key: unique identifier
588 * Get string value for specific key
590 const char *connman_network_get_string(struct connman_network *network,
593 DBG("network %p key %s", network, key);
595 if (g_str_equal(key, "Name") == TRUE)
596 return network->name;
597 else if (g_str_equal(key, "Node") == TRUE)
598 return network->node;
599 else if (g_str_equal(key, "WiFi.Mode") == TRUE)
600 return network->wifi.mode;
601 else if (g_str_equal(key, "WiFi.Security") == TRUE)
602 return network->wifi.security;
603 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
604 return network->wifi.passphrase;
610 * connman_network_set_uint8:
611 * @network: network structure
612 * @key: unique identifier
613 * @value: integer value
615 * Set integer value for specific key
617 int connman_network_set_uint8(struct connman_network *network,
618 const char *key, connman_uint8_t value)
620 DBG("network %p key %s value %d", network, key, value);
622 if (g_str_equal(key, "Strength") == TRUE)
623 network->strength = value;
629 * connman_network_set_blob:
630 * @network: network structure
631 * @key: unique identifier
635 * Set binary blob value for specific key
637 int connman_network_set_blob(struct connman_network *network,
638 const char *key, const void *data, unsigned int size)
640 DBG("network %p key %s size %d", network, key, size);
642 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
643 g_free(network->wifi.ssid);
644 network->wifi.ssid = g_try_malloc(size);
645 if (network->wifi.ssid != NULL) {
646 memcpy(network->wifi.ssid, data, size);
647 network->wifi.ssid_len = size;
649 network->wifi.ssid_len = 0;
656 * connman_network_get_blob:
657 * @network: network structure
658 * @key: unique identifier
659 * @size: pointer to blob size
661 * Get binary blob value for specific key
663 const void *connman_network_get_blob(struct connman_network *network,
664 const char *key, unsigned int *size)
666 DBG("network %p key %s", network, key);
668 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
670 *size = network->wifi.ssid_len;
671 return network->wifi.ssid;
677 void __connman_network_set_device(struct connman_network *network,
678 struct connman_device *device)
680 network->device = device;
684 * connman_network_get_device:
685 * @network: network structure
687 * Get parent device of network
689 struct connman_device *connman_network_get_device(struct connman_network *network)
691 return network->device;
695 * connman_network_get_data:
696 * @network: network structure
698 * Get private network data pointer
700 void *connman_network_get_data(struct connman_network *network)
702 return network->driver_data;
706 * connman_network_set_data:
707 * @network: network structure
708 * @data: data pointer
710 * Set private network data pointer
712 void connman_network_set_data(struct connman_network *network, void *data)
714 network->driver_data = data;
717 static gboolean match_driver(struct connman_network *network,
718 struct connman_network_driver *driver)
720 if (network->type == driver->type ||
721 driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
727 static int network_probe(struct connman_element *element)
729 struct connman_network *network = element->network;
733 DBG("element %p name %s", element, element->name);
738 for (list = driver_list; list; list = list->next) {
739 struct connman_network_driver *driver = list->data;
741 if (match_driver(network, driver) == FALSE)
744 DBG("driver %p name %s", driver, driver->name);
746 if (driver->probe(network) == 0) {
747 network->driver = driver;
752 if (network->driver == NULL)
755 err = register_interface(element);
757 if (network->driver->remove)
758 network->driver->remove(network);
765 static void network_remove(struct connman_element *element)
767 struct connman_network *network = element->network;
769 DBG("element %p name %s", element, element->name);
774 if (network->driver == NULL)
777 unregister_interface(element);
779 if (network->driver->remove)
780 network->driver->remove(network);
783 static struct connman_driver network_driver = {
785 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
786 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
787 .probe = network_probe,
788 .remove = network_remove,
791 static int network_load(struct connman_network *network)
794 gchar *pathname, *data = NULL;
798 DBG("network %p", network);
800 name = connman_device_get_name(network->device);
804 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, name);
805 if (pathname == NULL)
808 keyfile = g_key_file_new();
810 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
817 if (g_key_file_load_from_data(keyfile, data, length,
825 network->remember = g_key_file_get_boolean(keyfile,
826 network->identifier, "Remember", NULL);
828 g_free(network->wifi.security);
829 network->wifi.security = g_key_file_get_string(keyfile,
830 network->identifier, "WiFi.Security", NULL);
832 g_free(network->wifi.passphrase);
833 network->wifi.passphrase = g_key_file_get_string(keyfile,
834 network->identifier, "WiFi.Passphrase", NULL);
836 g_key_file_free(keyfile);
841 static int network_save(struct connman_network *network)
844 gchar *pathname, *data = NULL;
848 DBG("network %p", network);
850 name = connman_device_get_name(network->device);
854 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, name);
855 if (pathname == NULL)
858 keyfile = g_key_file_new();
860 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
864 if (g_key_file_load_from_data(keyfile, data, length,
872 g_key_file_set_boolean(keyfile, network->identifier,
873 "Remember", network->remember);
875 if (network->wifi.security != NULL)
876 g_key_file_set_string(keyfile, network->identifier,
877 "WiFi.Security", network->wifi.security);
879 if (network->wifi.passphrase != NULL)
880 g_key_file_set_string(keyfile, network->identifier,
881 "WiFi.Passphrase", network->wifi.passphrase);
883 data = g_key_file_to_data(keyfile, &length, NULL);
885 g_file_set_contents(pathname, data, length, NULL);
890 g_key_file_free(keyfile);
897 static struct connman_storage network_storage = {
899 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
900 .network_load = network_load,
901 .network_save = network_save,
904 int __connman_network_init(void)
908 connection = connman_dbus_get_connection();
910 if (connman_storage_register(&network_storage) < 0)
911 connman_error("Failed to register network storage");
913 return connman_driver_register(&network_driver);
916 void __connman_network_cleanup(void)
920 connman_driver_unregister(&network_driver);
922 connman_storage_unregister(&network_storage);
924 dbus_connection_unref(connection);