5 * Copyright (C) 2007-2014 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
31 #include <sys/ioctl.h>
32 #include <net/ethernet.h>
37 static GSList *device_list = NULL;
38 static gchar **device_filter = NULL;
39 static gchar **nodevice_filter = NULL;
42 static DBusConnection *connection;
45 enum connman_pending_type {
51 struct connman_device {
53 enum connman_device_type type;
54 enum connman_pending_type powered_pending; /* Indicates a pending
59 bool scanning[MAX_CONNMAN_SERVICE_TYPES];
67 guint pending_timeout;
69 struct connman_device_driver *driver;
73 struct connman_network *network;
76 time_t last_user_selection_time;
77 char *last_user_selection_ident;
78 char *last_connected_ident;
79 DBusMessage *pending_reply;
81 bool is_5_0_ghz_supported;
85 static void clear_pending_trigger(struct connman_device *device)
88 if (device->pending_reply) {
89 dbus_message_unref(device->pending_reply);
90 device->pending_reply = NULL;
93 if (device->pending_timeout > 0) {
94 g_source_remove(device->pending_timeout);
95 device->pending_timeout = 0;
99 static const char *type2description(enum connman_device_type type)
102 case CONNMAN_DEVICE_TYPE_UNKNOWN:
103 case CONNMAN_DEVICE_TYPE_VENDOR:
105 case CONNMAN_DEVICE_TYPE_ETHERNET:
107 case CONNMAN_DEVICE_TYPE_WIFI:
109 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
111 case CONNMAN_DEVICE_TYPE_GPS:
113 case CONNMAN_DEVICE_TYPE_CELLULAR:
115 case CONNMAN_DEVICE_TYPE_GADGET:
123 static const char *type2string(enum connman_device_type type)
126 case CONNMAN_DEVICE_TYPE_UNKNOWN:
127 case CONNMAN_DEVICE_TYPE_VENDOR:
129 case CONNMAN_DEVICE_TYPE_ETHERNET:
131 case CONNMAN_DEVICE_TYPE_WIFI:
133 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
135 case CONNMAN_DEVICE_TYPE_GPS:
137 case CONNMAN_DEVICE_TYPE_CELLULAR:
139 case CONNMAN_DEVICE_TYPE_GADGET:
147 enum connman_service_type __connman_device_get_service_type(
148 struct connman_device *device)
150 enum connman_device_type type = connman_device_get_type(device);
153 case CONNMAN_DEVICE_TYPE_UNKNOWN:
154 case CONNMAN_DEVICE_TYPE_VENDOR:
155 case CONNMAN_DEVICE_TYPE_GPS:
157 case CONNMAN_DEVICE_TYPE_ETHERNET:
158 return CONNMAN_SERVICE_TYPE_ETHERNET;
159 case CONNMAN_DEVICE_TYPE_WIFI:
160 return CONNMAN_SERVICE_TYPE_WIFI;
161 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
162 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
163 case CONNMAN_DEVICE_TYPE_CELLULAR:
164 return CONNMAN_SERVICE_TYPE_CELLULAR;
165 case CONNMAN_DEVICE_TYPE_GADGET:
166 return CONNMAN_SERVICE_TYPE_GADGET;
170 return CONNMAN_SERVICE_TYPE_UNKNOWN;
173 static bool device_has_service_type(struct connman_device *device,
174 enum connman_service_type service_type)
176 enum connman_service_type device_service_type =
177 __connman_device_get_service_type(device);
180 * For devices whose device_service_type is unknown we should
181 * allow to decide whether they support specific service_type
184 if (device_service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
187 #if defined TIZEN_EXT_WIFI_MESH
188 if (device_service_type == CONNMAN_SERVICE_TYPE_MESH)
189 return service_type != CONNMAN_SERVICE_TYPE_MESH;
192 if (device_service_type == CONNMAN_SERVICE_TYPE_WIFI) {
193 return service_type == CONNMAN_SERVICE_TYPE_WIFI ||
194 service_type == CONNMAN_SERVICE_TYPE_P2P;
197 return service_type == device_service_type;
200 static gboolean device_pending_reset(gpointer user_data)
202 struct connman_device *device = user_data;
204 DBG("device %p", device);
206 #if defined TIZEN_EXT
209 /* Power request timed out, send ETIMEDOUT. */
210 if (device->pending_reply) {
211 reply = __connman_error_failed(device->pending_reply, ETIMEDOUT);
213 g_dbus_send_message(connection, reply);
215 dbus_message_unref(device->pending_reply);
216 device->pending_reply = NULL;
219 /* Power request timedout, reset power pending state. */
220 device->pending_timeout = 0;
221 device->powered_pending = PENDING_NONE;
226 int __connman_device_enable(struct connman_device *device)
230 DBG("device %p", device);
232 if (!device->driver || !device->driver->enable)
235 /* There is an ongoing power disable request. */
236 if (device->powered_pending == PENDING_DISABLE)
239 if (device->powered_pending == PENDING_ENABLE)
242 if (device->powered_pending == PENDING_NONE && device->powered)
245 if (device->index > 0) {
246 err = connman_inet_ifup(device->index);
247 if (err < 0 && err != -EALREADY)
251 device->powered_pending = PENDING_ENABLE;
253 err = device->driver->enable(device);
255 * device gets enabled right away.
256 * Invoke the callback
259 connman_device_set_powered(device, true);
263 if (err == -EALREADY) {
264 /* If device is already powered, but connman is not updated */
265 connman_device_set_powered(device, true);
269 * if err == -EINPROGRESS, then the DBus call to the respective daemon
270 * was successful. We set a 4 sec timeout so if the daemon never
271 * returns a reply, we would reset the pending request.
273 if (err == -EINPROGRESS)
274 device->pending_timeout = g_timeout_add_seconds(4,
275 device_pending_reset, device);
280 int __connman_device_disable(struct connman_device *device)
284 DBG("device %p", device);
286 /* Ongoing power enable request */
287 if (device->powered_pending == PENDING_ENABLE)
290 if (device->powered_pending == PENDING_DISABLE)
293 if (device->powered_pending == PENDING_NONE && !device->powered)
296 device->powered_pending = PENDING_DISABLE;
298 if (device->network) {
299 struct connman_service *service =
300 connman_service_lookup_from_network(device->network);
303 __connman_service_disconnect(service);
305 connman_network_set_connected(device->network, false);
308 if (!device->driver || !device->driver->disable)
311 err = device->driver->disable(device);
312 if (err == 0 || err == -EALREADY) {
313 connman_device_set_powered(device, false);
317 if (err == -EINPROGRESS)
318 device->pending_timeout = g_timeout_add_seconds(4,
319 device_pending_reset, device);
324 static void probe_driver(struct connman_device_driver *driver)
328 DBG("driver %p name %s", driver, driver->name);
330 for (list = device_list; list; list = list->next) {
331 struct connman_device *device = list->data;
336 if (driver->type != device->type)
339 if (driver->probe(device) < 0)
342 device->driver = driver;
344 __connman_technology_add_device(device);
348 static void remove_device(struct connman_device *device)
350 DBG("device %p", device);
352 __connman_device_disable(device);
354 __connman_technology_remove_device(device);
356 if (device->driver->remove)
357 device->driver->remove(device);
359 device->driver = NULL;
362 static void remove_driver(struct connman_device_driver *driver)
366 DBG("driver %p name %s", driver, driver->name);
368 for (list = device_list; list; list = list->next) {
369 struct connman_device *device = list->data;
371 if (device->driver == driver)
372 remove_device(device);
376 bool __connman_device_has_driver(struct connman_device *device)
378 if (!device || !device->driver)
384 static GSList *driver_list = NULL;
386 static gint compare_priority(gconstpointer a, gconstpointer b)
388 const struct connman_device_driver *driver1 = a;
389 const struct connman_device_driver *driver2 = b;
391 return driver2->priority - driver1->priority;
395 * connman_device_driver_register:
396 * @driver: device driver definition
398 * Register a new device driver
400 * Returns: %0 on success
402 int connman_device_driver_register(struct connman_device_driver *driver)
404 DBG("driver %p name %s", driver, driver->name);
406 driver_list = g_slist_insert_sorted(driver_list, driver,
408 probe_driver(driver);
414 * connman_device_driver_unregister:
415 * @driver: device driver definition
417 * Remove a previously registered device driver
419 void connman_device_driver_unregister(struct connman_device_driver *driver)
421 DBG("driver %p name %s", driver, driver->name);
423 driver_list = g_slist_remove(driver_list, driver);
425 remove_driver(driver);
428 static void free_network(gpointer data)
430 struct connman_network *network = data;
432 DBG("network %p", network);
434 __connman_network_set_device(network, NULL);
436 connman_network_unref(network);
439 static void device_destruct(struct connman_device *device)
441 DBG("device %p name %s", device, device->name);
443 clear_pending_trigger(device);
445 g_hash_table_destroy(device->networks);
446 device->networks = NULL;
448 g_free(device->ident);
449 g_free(device->node);
450 g_free(device->name);
451 g_free(device->address);
452 g_free(device->interface);
453 g_free(device->path);
455 g_free(device->last_network);
457 #if defined TIZEN_EXT
458 g_free(device->last_user_selection_ident);
459 g_free(device->last_connected_ident);
465 #if defined TIZEN_EXT
466 static void device_send_changed(const char *ifname, enum connman_service_type type,
467 const char *key, bool state)
470 DBusMessageIter iter, dict;
471 dbus_bool_t value = state;
472 const char *tech_path = connman_techonology_get_path(type);
474 if (!tech_path || !ifname)
477 DBG("%s %s %s", ifname, key, state ? "TRUE" : "FALSE");
479 signal = dbus_message_new_signal(tech_path,
480 CONNMAN_TECHNOLOGY_INTERFACE, "DeviceChanged");
484 dbus_message_iter_init_append(signal, &iter);
486 connman_dbus_dict_open(&iter, &dict);
487 connman_dbus_dict_append_basic(&dict, "Ifname",
490 connman_dbus_dict_append_basic(&dict, key,
493 connman_dbus_dict_close(&iter, &dict);
495 dbus_connection_send(connection, signal, NULL);
496 dbus_message_unref(signal);
499 static void device_send_reply(struct connman_device *device)
501 if (device->pending_reply) {
502 g_dbus_send_reply(connection,
503 device->pending_reply, DBUS_TYPE_INVALID);
504 dbus_message_unref(device->pending_reply);
505 device->pending_reply = NULL;
511 * connman_device_create:
512 * @node: device node name (for example an address)
515 * Allocate a new device of given #type and assign the #node name to it.
517 * Returns: a newly-allocated #connman_device structure
519 struct connman_device *connman_device_create(const char *node,
520 enum connman_device_type type)
522 struct connman_device *device;
524 DBG("node %s type %d", node, type);
526 device = g_try_new0(struct connman_device, 1);
530 DBG("device %p", device);
532 device->refcount = 1;
535 device->name = g_strdup(type2description(device->type));
537 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
538 g_free, free_network);
540 device_list = g_slist_prepend(device_list, device);
546 * connman_device_ref:
547 * @device: device structure
549 * Increase reference counter of device
551 struct connman_device *connman_device_ref_debug(struct connman_device *device,
552 const char *file, int line, const char *caller)
554 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
557 __sync_fetch_and_add(&device->refcount, 1);
563 * connman_device_unref:
564 * @device: device structure
566 * Decrease reference counter of device
568 void connman_device_unref_debug(struct connman_device *device,
569 const char *file, int line, const char *caller)
571 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
574 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
577 if (device->driver) {
578 device->driver->remove(device);
579 device->driver = NULL;
582 device_list = g_slist_remove(device_list, device);
584 device_destruct(device);
587 const char *__connman_device_get_type(struct connman_device *device)
589 return type2string(device->type);
593 * connman_device_get_type:
594 * @device: device structure
598 enum connman_device_type connman_device_get_type(struct connman_device *device)
604 * connman_device_set_index:
605 * @device: device structure
606 * @index: index number
608 * Set index number of device
610 void connman_device_set_index(struct connman_device *device, int index)
612 device->index = index;
616 * connman_device_get_index:
617 * @device: device structure
619 * Get index number of device
621 int connman_device_get_index(struct connman_device *device)
623 return device->index;
627 * connman_device_set_interface:
628 * @device: device structure
629 * @interface: interface name
631 * Set interface name of device
633 void connman_device_set_interface(struct connman_device *device,
634 const char *interface)
636 g_free(device->interface);
637 device->interface = g_strdup(interface);
640 const char *str = type2description(device->type);
641 if (str && device->interface)
642 device->name = g_strdup_printf("%s (%s)", str,
648 * connman_device_set_ident:
649 * @device: device structure
650 * @ident: unique identifier
652 * Set unique identifier of device
654 void connman_device_set_ident(struct connman_device *device,
657 g_free(device->ident);
658 device->ident = g_strdup(ident);
661 const char *connman_device_get_ident(struct connman_device *device)
663 return device->ident;
667 * connman_device_set_powered:
668 * @device: device structure
669 * @powered: powered state
671 * Change power state of device
673 int connman_device_set_powered(struct connman_device *device,
676 struct connman_device_scan_params params;
677 enum connman_service_type type;
680 DBG("device %p powered %d", device, powered);
682 if (device->powered == powered)
685 #if defined TIZEN_EXT
686 device_send_reply(device);
689 clear_pending_trigger(device);
691 device->powered_pending = PENDING_NONE;
693 device->powered = powered;
695 type = __connman_device_get_service_type(device);
697 #if defined TIZEN_EXT
698 device_send_changed(device->interface, type, "Powered", powered);
699 technology_save_device(device);
702 if (!device->powered) {
703 __connman_technology_disabled(type);
707 __connman_technology_enabled(type);
709 for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++)
710 device->scanning[i] = false;
712 if (device->driver && device->driver->scan) {
713 memset(¶ms, 0, sizeof(params));
714 params.type = CONNMAN_SERVICE_TYPE_UNKNOWN;
716 device->driver->scan(device, ¶ms);
722 bool connman_device_get_powered(struct connman_device *device)
724 return device->powered;
727 static int device_scan(enum connman_service_type type,
728 struct connman_device *device,
729 bool force_full_scan)
731 struct connman_device_scan_params params;
733 if (!device->driver || !device->driver->scan)
736 if (!device->powered)
739 memset(¶ms, 0, sizeof(params));
741 params.force_full_scan = force_full_scan;
743 return device->driver->scan(device, ¶ms);
746 int __connman_device_disconnect(struct connman_device *device)
751 DBG("device %p", device);
753 g_hash_table_iter_init(&iter, device->networks);
755 while (g_hash_table_iter_next(&iter, &key, &value)) {
756 struct connman_network *network = value;
758 if (connman_network_get_connecting(network)) {
760 * Skip network in the process of connecting.
761 * This is a workaround for WiFi networks serviced
762 * by the supplicant plugin that hold a reference
763 * to the network. If we disconnect the network
764 * here then the referenced object will not be
765 * registered and usage (like launching DHCP client)
766 * will fail. There is nothing to be gained by
767 * removing the network here anyway.
769 connman_warn("Skipping disconnect of %s, network is connecting.",
770 connman_network_get_identifier(network));
774 __connman_network_disconnect(network);
780 int connman_device_reconnect_service(struct connman_device *device)
782 DBG("device %p", device);
784 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
789 #if defined TIZEN_EXT
790 bool connman_device_set_last_user_selection_time(struct connman_device *device,
793 if (device->last_user_selection_time != time) {
794 device->last_user_selection_time = time;
801 time_t connman_device_get_last_user_selection_time(struct connman_device *device)
803 return device->last_user_selection_time;
806 bool connman_device_set_last_user_selection_ident(struct connman_device *device,
809 if (g_strcmp0(device->last_user_selection_ident, ident) != 0) {
810 g_free(device->last_user_selection_ident);
811 device->last_user_selection_ident = g_strdup(ident);
819 const char *connman_device_get_last_user_selection_ident(struct connman_device *device)
821 return device->last_user_selection_ident;
824 bool connman_device_set_last_connected_ident(struct connman_device *device,
827 if (g_strcmp0(device->last_connected_ident, ident) != 0) {
828 g_free(device->last_connected_ident);
829 device->last_connected_ident = g_strdup(ident);
837 const char *connman_device_get_last_connected_ident(struct connman_device *device)
839 return device->last_connected_ident;
842 void connman_device_save_last_user_selection(struct connman_device *device)
846 gchar *selection_str;
848 keyfile = __connman_storage_load_ins();
850 selection_str = g_strdup_printf("%s:%ld",
851 device->last_user_selection_ident, device->last_user_selection_time);
854 keyfile = g_key_file_new();
856 g_key_file_set_string(keyfile, device->interface, "LastUserSelection", selection_str);
857 DBG("%s", selection_str);
858 __connman_storage_save_ins(keyfile);
861 get_str = g_key_file_get_string(keyfile, device->interface, "LastUserSelection", NULL);
862 if (!get_str || g_strcmp0(get_str, selection_str) != 0) {
863 g_key_file_set_string(keyfile, device->interface, "LastUserSelection", selection_str);
864 DBG("%s -> %s", get_str, selection_str);
865 __connman_storage_save_ins(keyfile);
871 g_free(selection_str);
872 g_key_file_free(keyfile);
875 void connman_device_load_last_user_selection(struct connman_device *device)
879 char **selection_str;
881 keyfile = __connman_storage_load_ins();
885 get_str = g_key_file_get_string(keyfile, device->interface, "LastUserSelection", NULL);
887 selection_str = g_strsplit(get_str, ":", 0);
891 time_t last_user_selection_time;
893 /* Only events that occur within 8 hours are counted. */
894 ref_time = time(NULL);
895 timeinfo = localtime(&ref_time);
896 timeinfo->tm_hour -= 8;
897 ref_time = mktime(timeinfo);
899 last_user_selection_time = strtol(selection_str[1], NULL, 10);
901 if (last_user_selection_time > ref_time) {
902 if (g_strcmp0(selection_str[0], device->last_user_selection_ident) != 0) {
903 g_free(device->last_user_selection_ident);
904 device->last_user_selection_ident = g_strdup(selection_str[0]);
907 device->last_user_selection_time = last_user_selection_time;
909 DBG("%s %ld", device->last_user_selection_ident, device->last_user_selection_time);
912 g_strfreev(selection_str);
918 g_key_file_free(keyfile);
921 void connman_device_save_last_connected(struct connman_device *device)
926 if (!device->last_connected_ident)
929 keyfile = __connman_storage_load_ins();
932 keyfile = g_key_file_new();
934 g_key_file_set_string(keyfile, device->interface, "LastConnected", device->last_connected_ident);
935 DBG("%s", device->last_connected_ident);
936 __connman_storage_save_ins(keyfile);
939 get_str = g_key_file_get_string(keyfile, device->interface, "LastConnected", NULL);
940 if (!get_str || g_strcmp0(get_str, device->last_connected_ident) != 0) {
941 g_key_file_set_string(keyfile, device->interface, "LastConnected", device->last_connected_ident);
942 DBG("%s -> %s", get_str, device->last_connected_ident);
943 __connman_storage_save_ins(keyfile);
949 g_key_file_free(keyfile);
952 void connman_device_load_last_connected(struct connman_device *device)
957 keyfile = __connman_storage_load_ins();
961 get_str = g_key_file_get_string(keyfile, device->interface, "LastConnected", NULL);
963 if (g_strcmp0(get_str, device->last_connected_ident) != 0) {
964 g_free(device->last_connected_ident);
965 device->last_connected_ident = g_strdup(get_str);
968 DBG("%s", device->last_connected_ident);
973 g_key_file_free(keyfile);
977 static void mark_network_available(gpointer key, gpointer value,
980 struct connman_network *network = value;
982 connman_network_set_available(network, true);
985 static void mark_network_unavailable(gpointer key, gpointer value,
988 struct connman_network *network = value;
990 if (connman_network_get_connected(network) ||
991 connman_network_get_connecting(network))
994 connman_network_set_available(network, false);
997 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1000 struct connman_network *network = value;
1002 if (connman_network_get_connected(network) ||
1003 connman_network_get_connecting(network))
1006 if (connman_network_get_available(network))
1012 void __connman_device_cleanup_networks(struct connman_device *device)
1014 g_hash_table_foreach_remove(device->networks,
1015 remove_unavailable_network, NULL);
1018 bool connman_device_get_scanning(struct connman_device *device,
1019 enum connman_service_type type)
1023 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN)
1024 return device->scanning[type];
1026 for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++)
1027 if (device->scanning[i])
1033 void connman_device_reset_scanning(struct connman_device *device)
1035 g_hash_table_foreach(device->networks,
1036 mark_network_available, NULL);
1040 * connman_device_set_scanning:
1041 * @device: device structure
1042 * @scanning: scanning state
1044 * Change scanning state of device
1046 int connman_device_set_scanning(struct connman_device *device,
1047 enum connman_service_type type, bool scanning)
1049 DBG("device %p scanning %d", device, scanning);
1051 if (!device->driver || !device->driver->scan)
1054 if (type == CONNMAN_SERVICE_TYPE_UNKNOWN)
1057 if (device->scanning[type] == scanning)
1060 device->scanning[type] = scanning;
1063 __connman_technology_scan_started(device);
1065 g_hash_table_foreach(device->networks,
1066 mark_network_unavailable, NULL);
1071 __connman_device_cleanup_networks(device);
1073 __connman_technology_scan_stopped(device, type);
1075 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
1077 #if defined TIZEN_EXT_WIFI_MESH
1078 if (type == CONNMAN_SERVICE_TYPE_MESH)
1079 __connman_mesh_auto_connect();
1086 * connman_device_set_string:
1087 * @device: device structure
1088 * @key: unique identifier
1089 * @value: string value
1091 * Set string value for specific key
1093 int connman_device_set_string(struct connman_device *device,
1094 const char *key, const char *value)
1096 DBG("device %p key %s value %s", device, key, value);
1098 if (g_str_equal(key, "Address")) {
1099 g_free(device->address);
1100 device->address = g_strdup(value);
1101 } else if (g_str_equal(key, "Name")) {
1102 g_free(device->name);
1103 device->name = g_strdup(value);
1104 } else if (g_str_equal(key, "Node")) {
1105 g_free(device->node);
1106 device->node = g_strdup(value);
1107 } else if (g_str_equal(key, "Path")) {
1108 g_free(device->path);
1109 device->path = g_strdup(value);
1118 * connman_device_get_string:
1119 * @device: device structure
1120 * @key: unique identifier
1122 * Get string value for specific key
1124 const char *connman_device_get_string(struct connman_device *device,
1127 DBG("device %p key %s", device, key);
1129 if (g_str_equal(key, "Address"))
1130 return device->address;
1131 else if (g_str_equal(key, "Name"))
1132 return device->name;
1133 else if (g_str_equal(key, "Node"))
1134 return device->node;
1135 else if (g_str_equal(key, "Interface"))
1136 return device->interface;
1137 else if (g_str_equal(key, "Path"))
1138 return device->path;
1144 * connman_device_add_network:
1145 * @device: device structure
1146 * @network: network structure
1148 * Add new network to the device
1150 int connman_device_add_network(struct connman_device *device,
1151 struct connman_network *network)
1153 const char *identifier = connman_network_get_identifier(network);
1155 DBG("device %p network %p", device, network);
1160 connman_network_ref(network);
1162 __connman_network_set_device(network, device);
1164 g_hash_table_replace(device->networks, g_strdup(identifier),
1171 * connman_device_get_network:
1172 * @device: device structure
1173 * @identifier: network identifier
1175 * Get network for given identifier
1177 struct connman_network *connman_device_get_network(struct connman_device *device,
1178 const char *identifier)
1180 DBG("device %p identifier %s", device, identifier);
1182 return g_hash_table_lookup(device->networks, identifier);
1185 #if defined TIZEN_EXT
1186 struct connman_network *connman_device_get_default_network(
1187 struct connman_device *device)
1189 return device->network;
1192 void connman_device_set_pending_reply(struct connman_device *device,
1195 device->pending_reply = dbus_message_ref(msg);
1198 void connman_device_send_connected_signal(struct connman_device *device,
1201 enum connman_service_type type;
1206 type = __connman_device_get_service_type(device);
1207 device_send_changed(device->interface, type, "Connected", connected);
1210 void connman_device_set_max_scan_ssids(struct connman_device *device,
1213 device->max_scan_ssids = max_scan_ssids;
1216 int connman_device_get_max_scan_ssids(struct connman_device *device)
1218 return device->max_scan_ssids;
1221 void connman_device_set_wifi_5ghz_supported(struct connman_device *device,
1222 bool is_5_0_ghz_supported)
1224 device->is_5_0_ghz_supported = is_5_0_ghz_supported;
1227 bool connman_device_get_wifi_5ghz_supported(struct connman_device *device)
1229 return device->is_5_0_ghz_supported;
1234 * connman_device_remove_network:
1235 * @device: device structure
1236 * @identifier: network identifier
1238 * Remove network for given identifier
1240 int connman_device_remove_network(struct connman_device *device,
1241 struct connman_network *network)
1243 const char *identifier;
1245 DBG("device %p network %p", device, network);
1250 identifier = connman_network_get_identifier(network);
1251 g_hash_table_remove(device->networks, identifier);
1256 void __connman_device_set_network(struct connman_device *device,
1257 struct connman_network *network)
1264 if (device->network == network)
1268 name = connman_network_get_string(network, "Name");
1269 g_free(device->last_network);
1270 device->last_network = g_strdup(name);
1272 device->network = network;
1274 g_free(device->last_network);
1275 device->last_network = NULL;
1277 device->network = NULL;
1281 static bool match_driver(struct connman_device *device,
1282 struct connman_device_driver *driver)
1284 if (device->type == driver->type ||
1285 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1292 * connman_device_register:
1293 * @device: device structure
1295 * Register device with the system
1297 int connman_device_register(struct connman_device *device)
1301 DBG("device %p name %s", device, device->name);
1306 for (list = driver_list; list; list = list->next) {
1307 struct connman_device_driver *driver = list->data;
1309 if (!match_driver(device, driver))
1312 DBG("driver %p name %s", driver, driver->name);
1314 if (driver->probe(device) == 0) {
1315 device->driver = driver;
1320 if (!device->driver)
1323 return __connman_technology_add_device(device);
1327 * connman_device_unregister:
1328 * @device: device structure
1330 * Unregister device with the system
1332 void connman_device_unregister(struct connman_device *device)
1334 DBG("device %p name %s", device, device->name);
1336 if (!device->driver)
1339 remove_device(device);
1343 * connman_device_get_data:
1344 * @device: device structure
1346 * Get private device data pointer
1348 void *connman_device_get_data(struct connman_device *device)
1350 return device->driver_data;
1354 * connman_device_set_data:
1355 * @device: device structure
1356 * @data: data pointer
1358 * Set private device data pointer
1360 void connman_device_set_data(struct connman_device *device, void *data)
1362 device->driver_data = data;
1365 struct connman_device *__connman_device_find_device(
1366 enum connman_service_type type)
1370 for (list = device_list; list; list = list->next) {
1371 struct connman_device *device = list->data;
1372 enum connman_service_type service_type =
1373 __connman_device_get_service_type(device);
1375 if (service_type != type)
1384 struct connman_device *connman_device_find_by_index(int index)
1388 for (list = device_list; list; list = list->next) {
1389 struct connman_device *device = list->data;
1390 if (device->index == index)
1398 * connman_device_set_regdom
1399 * @device: device structure
1400 * @alpha2: string representing regulatory domain
1402 * Set regulatory domain on device basis
1404 int connman_device_set_regdom(struct connman_device *device,
1407 if (!device->driver || !device->driver->set_regdom)
1410 if (!device->powered)
1413 return device->driver->set_regdom(device, alpha2);
1417 * connman_device_regdom_notify
1418 * @device: device structure
1419 * @alpha2: string representing regulatory domain
1421 * Notify on setting regulatory domain on device basis
1423 void connman_device_regdom_notify(struct connman_device *device,
1424 int result, const char *alpha2)
1426 __connman_technology_notify_regdom_by_device(device, result, alpha2);
1429 #if defined TIZEN_EXT
1430 static int device_specific_scan(enum connman_service_type type,
1431 struct connman_device *device,
1432 int scan_type, GSList *specific_scan_list)
1434 if (!device->driver || !device->driver->specific_scan)
1437 if (!device->powered)
1440 return device->driver->specific_scan(type, device, scan_type,
1441 specific_scan_list, NULL);
1444 int __connman_device_request_specific_scan(enum connman_service_type type,
1445 const char *ifname, int scan_type, GSList *specific_scan_list)
1447 bool success = false;
1448 int last_err = -ENOSYS;
1453 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1454 case CONNMAN_SERVICE_TYPE_SYSTEM:
1455 case CONNMAN_SERVICE_TYPE_ETHERNET:
1456 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1457 case CONNMAN_SERVICE_TYPE_CELLULAR:
1458 case CONNMAN_SERVICE_TYPE_GPS:
1459 case CONNMAN_SERVICE_TYPE_VPN:
1460 case CONNMAN_SERVICE_TYPE_GADGET:
1462 case CONNMAN_SERVICE_TYPE_WIFI:
1463 case CONNMAN_SERVICE_TYPE_P2P:
1464 #if defined TIZEN_EXT_WIFI_MESH
1465 case CONNMAN_SERVICE_TYPE_MESH:
1470 for (list = device_list; list; list = list->next) {
1471 struct connman_device *device = list->data;
1472 enum connman_service_type service_type =
1473 __connman_device_get_service_type(device);
1475 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1476 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1477 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1479 } else if (service_type != type)
1483 if (ifname && g_strcmp0(device->interface, ifname) != 0)
1486 err = device_specific_scan(type, device, scan_type, specific_scan_list);
1487 if (err == 0 || err == -EINPROGRESS) {
1491 DBG("device %p err %d", device, err);
1501 int connman_device_request_device_scan(enum connman_service_type type,
1502 const char * ifname, bool force_full_scan)
1504 bool success = false;
1505 int last_err = -ENOSYS;
1510 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1511 case CONNMAN_SERVICE_TYPE_SYSTEM:
1512 case CONNMAN_SERVICE_TYPE_ETHERNET:
1513 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1514 case CONNMAN_SERVICE_TYPE_CELLULAR:
1515 case CONNMAN_SERVICE_TYPE_GPS:
1516 case CONNMAN_SERVICE_TYPE_VPN:
1517 case CONNMAN_SERVICE_TYPE_GADGET:
1519 case CONNMAN_SERVICE_TYPE_WIFI:
1520 case CONNMAN_SERVICE_TYPE_P2P:
1521 #if defined TIZEN_EXT_WIFI_MESH
1522 case CONNMAN_SERVICE_TYPE_MESH:
1527 for (list = device_list; list; list = list->next) {
1528 struct connman_device *device = list->data;
1530 if (!device_has_service_type(device, type))
1533 if (g_strcmp0(device->interface, ifname) != 0)
1536 err = device_scan(type, device, force_full_scan);
1538 if (err == 0 || err == -EINPROGRESS) {
1542 DBG("device %p err %d", device, err);
1553 #if defined TIZEN_EXT_WIFI_MESH
1554 static int device_abort_scan(enum connman_service_type type,
1555 struct connman_device *device)
1557 if (!device->driver || !device->driver->scan)
1560 if (!device->powered)
1563 return device->driver->abort_scan(type, device);
1566 int __connman_device_abort_scan(enum connman_service_type type)
1571 if (type != CONNMAN_SERVICE_TYPE_MESH)
1574 for (list = device_list; list; list = list->next) {
1575 struct connman_device *device = list->data;
1576 enum connman_service_type service_type =
1577 __connman_device_get_service_type(device);
1579 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1580 if (type == CONNMAN_SERVICE_TYPE_MESH)
1581 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1584 if (!device->scanning) {
1589 err = device_abort_scan(type, device);
1595 static int device_mesh_specific_scan(enum connman_service_type type,
1596 struct connman_device *device, const char *name,
1599 if (!device->driver || !device->driver->mesh_specific_scan)
1602 if (!device->powered)
1605 return device->driver->mesh_specific_scan(type, device, name, freq, NULL);
1608 int __connman_device_request_mesh_specific_scan(enum connman_service_type type,
1612 bool success = false;
1613 int last_err = -ENOSYS;
1617 if (type != CONNMAN_SERVICE_TYPE_MESH)
1620 for (list = device_list; list; list = list->next) {
1621 struct connman_device *device = list->data;
1622 enum connman_service_type service_type =
1623 __connman_device_get_service_type(device);
1625 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1626 if (type == CONNMAN_SERVICE_TYPE_MESH)
1627 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1631 err = device_mesh_specific_scan(type, device, name, freq);
1632 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1636 DBG("device %p err %d", device, err);
1645 #endif /* TIZEN_EXT_WIFI_MESH */
1648 static int connman_device_request_scan(enum connman_service_type type,
1649 bool force_full_scan)
1651 bool success = false;
1652 int last_err = -ENOSYS;
1657 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1658 case CONNMAN_SERVICE_TYPE_SYSTEM:
1659 case CONNMAN_SERVICE_TYPE_ETHERNET:
1660 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1661 case CONNMAN_SERVICE_TYPE_CELLULAR:
1662 case CONNMAN_SERVICE_TYPE_GPS:
1663 case CONNMAN_SERVICE_TYPE_VPN:
1664 case CONNMAN_SERVICE_TYPE_GADGET:
1666 case CONNMAN_SERVICE_TYPE_WIFI:
1667 case CONNMAN_SERVICE_TYPE_P2P:
1668 #if defined TIZEN_EXT_WIFI_MESH
1669 case CONNMAN_SERVICE_TYPE_MESH:
1674 for (list = device_list; list; list = list->next) {
1675 struct connman_device *device = list->data;
1677 if (!device_has_service_type(device, type))
1680 err = device_scan(type, device, force_full_scan);
1681 #if defined TIZEN_EXT
1682 /* When Scan is already in progress then return Error so that
1683 * wifi-manager can block the scan-done signal to be sent to
1684 * application and start requested scan after scan already in progress
1685 * is completed then notify to application about the scan event */
1686 if (err == 0 || err == -EINPROGRESS) {
1688 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1693 DBG("device %p err %d", device, err);
1703 int __connman_device_request_scan(enum connman_service_type type)
1705 return connman_device_request_scan(type, false);
1708 int __connman_device_request_scan_full(enum connman_service_type type)
1710 return connman_device_request_scan(type, true);
1713 int __connman_device_request_hidden_scan(struct connman_device *device,
1714 const char *ssid, unsigned int ssid_len,
1715 const char *identity, const char *passphrase,
1716 const char *security, void *user_data)
1718 struct connman_device_scan_params params;
1720 DBG("device %p", device);
1722 if (!device || !device->driver ||
1723 !device->driver->scan)
1726 params.type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1728 params.ssid_len = ssid_len;
1729 params.identity = identity;
1730 params.passphrase = passphrase;
1731 params.security = security;
1732 params.user_data = user_data;
1734 return device->driver->scan(device, ¶ms);
1737 void __connman_device_stop_scan(enum connman_service_type type)
1741 for (list = device_list; list; list = list->next) {
1742 struct connman_device *device = list->data;
1744 if (!device_has_service_type(device, type))
1747 if (device->driver && device->driver->stop_scan)
1748 device->driver->stop_scan(type, device);
1752 #if defined TIZEN_EXT
1753 char *index2ident(int index, const char *prefix)
1755 static char *index2ident(int index, const char *prefix)
1759 struct ether_addr eth;
1766 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1770 memset(&ifr, 0, sizeof(ifr));
1771 ifr.ifr_ifindex = index;
1773 err = ioctl(sk, SIOCGIFNAME, &ifr);
1776 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1783 len = prefix ? strlen(prefix) + 18 : 18;
1785 str = g_malloc(len);
1789 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1790 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1791 prefix ? prefix : "",
1792 eth.ether_addr_octet[0],
1793 eth.ether_addr_octet[1],
1794 eth.ether_addr_octet[2],
1795 eth.ether_addr_octet[3],
1796 eth.ether_addr_octet[4],
1797 eth.ether_addr_octet[5]);
1802 #if defined TIZEN_EXT
1803 char *index2addr(int index)
1805 static char *index2addr(int index)
1809 struct ether_addr eth;
1816 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1820 memset(&ifr, 0, sizeof(ifr));
1821 ifr.ifr_ifindex = index;
1823 err = ioctl(sk, SIOCGIFNAME, &ifr);
1826 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1837 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1838 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1839 eth.ether_addr_octet[0],
1840 eth.ether_addr_octet[1],
1841 eth.ether_addr_octet[2],
1842 eth.ether_addr_octet[3],
1843 eth.ether_addr_octet[4],
1844 eth.ether_addr_octet[5]);
1849 struct connman_device *connman_device_create_from_index(int index)
1851 enum connman_device_type type;
1852 struct connman_device *device;
1853 char *devname, *ident = NULL;
1854 char *addr = NULL, *name = NULL;
1859 devname = connman_inet_ifname(index);
1863 if (__connman_device_isfiltered(devname)) {
1864 connman_info("Ignoring interface %s (filtered)", devname);
1869 type = __connman_rtnl_get_device_type(index);
1872 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1873 connman_info("Ignoring interface %s (type unknown)", devname);
1876 case CONNMAN_DEVICE_TYPE_ETHERNET:
1877 case CONNMAN_DEVICE_TYPE_GADGET:
1878 case CONNMAN_DEVICE_TYPE_WIFI:
1879 name = index2ident(index, "");
1880 addr = index2addr(index);
1882 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1883 case CONNMAN_DEVICE_TYPE_CELLULAR:
1884 case CONNMAN_DEVICE_TYPE_GPS:
1885 case CONNMAN_DEVICE_TYPE_VENDOR:
1886 name = g_strdup(devname);
1890 device = connman_device_create(name, type);
1895 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1896 case CONNMAN_DEVICE_TYPE_VENDOR:
1897 case CONNMAN_DEVICE_TYPE_GPS:
1899 case CONNMAN_DEVICE_TYPE_ETHERNET:
1900 case CONNMAN_DEVICE_TYPE_GADGET:
1901 ident = index2ident(index, NULL);
1903 case CONNMAN_DEVICE_TYPE_WIFI:
1904 ident = index2ident(index, NULL);
1906 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1908 case CONNMAN_DEVICE_TYPE_CELLULAR:
1909 ident = index2ident(index, NULL);
1913 connman_device_set_index(device, index);
1914 connman_device_set_interface(device, devname);
1915 #if defined TIZEN_EXT
1916 connman_device_load_last_connected(device);
1917 connman_device_load_last_user_selection(device);
1921 connman_device_set_ident(device, ident);
1925 connman_device_set_string(device, "Address", addr);
1935 bool __connman_device_isfiltered(const char *devname)
1938 char **blacklisted_interfaces;
1944 for (pattern = device_filter, match = false; *pattern; pattern++) {
1945 if (g_pattern_match_simple(*pattern, devname)) {
1952 DBG("ignoring device %s (match)", devname);
1957 if (g_pattern_match_simple("dummy*", devname)) {
1958 DBG("ignoring dummy networking devices");
1962 if (!nodevice_filter)
1965 for (pattern = nodevice_filter; *pattern; pattern++) {
1966 if (g_pattern_match_simple(*pattern, devname)) {
1967 DBG("ignoring device %s (no match)", devname);
1973 if (__connman_inet_isrootnfs_device(devname)) {
1974 DBG("ignoring device %s (rootnfs)", devname);
1978 blacklisted_interfaces =
1979 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1980 if (!blacklisted_interfaces)
1983 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1984 if (g_str_has_prefix(devname, *pattern)) {
1985 DBG("ignoring device %s (blacklist)", devname);
1993 static void cleanup_devices(void)
1996 * Check what interfaces are currently up and if connman is
1997 * suppose to handle the interface, then cleanup the mess
1998 * related to that interface. There might be weird routes etc
1999 * that are related to that interface and that might confuse
2000 * connmand. So in this case we just turn the interface down
2001 * so that kernel removes routes/addresses automatically and
2002 * then proceed the startup.
2004 * Note that this cleanup must be done before rtnl/detect code
2005 * has activated interface watches.
2011 interfaces = __connman_inet_get_running_interfaces();
2016 for (i = 0; interfaces[i]; i++) {
2019 struct sockaddr_in sin_addr, sin_mask;
2021 filtered = __connman_device_isfiltered(interfaces[i]);
2025 index = connman_inet_ifindex(interfaces[i]);
2029 if (!__connman_inet_get_address_netmask(index, &sin_addr,
2031 char *address = g_strdup(inet_ntoa(sin_addr.sin_addr));
2032 char *netmask = g_strdup(inet_ntoa(sin_mask.sin_addr));
2034 if (__connman_config_address_provisioned(address,
2036 DBG("Skip %s which is already provisioned "
2037 "with %s/%s", interfaces[i], address,
2048 DBG("cleaning up %s index %d", interfaces[i], index);
2050 #if defined TIZEN_EXT
2051 if (strcmp(interfaces[i], "wlan0") != 0)
2053 connman_inet_ifdown(index);
2056 * ConnMan will turn the interface UP automatically so
2057 * no need to do it here.
2061 g_strfreev(interfaces);
2064 int __connman_device_init(const char *device, const char *nodevice)
2068 #if defined TIZEN_EXT
2069 connection = connman_dbus_get_connection();
2073 device_filter = g_strsplit(device, ",", -1);
2076 nodevice_filter = g_strsplit(nodevice, ",", -1);
2083 void __connman_device_cleanup(void)
2087 g_strfreev(nodevice_filter);
2088 g_strfreev(device_filter);
2090 #if defined TIZEN_EXT
2091 dbus_connection_unref(connection);