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;
41 enum connman_pending_type {
47 struct connman_device {
49 enum connman_device_type type;
50 enum connman_pending_type powered_pending; /* Indicates a pending
63 guint pending_timeout;
65 struct connman_device_driver *driver;
69 struct connman_network *network;
73 static void clear_pending_trigger(struct connman_device *device)
75 if (device->pending_timeout > 0) {
76 g_source_remove(device->pending_timeout);
77 device->pending_timeout = 0;
81 static const char *type2description(enum connman_device_type type)
84 case CONNMAN_DEVICE_TYPE_UNKNOWN:
85 case CONNMAN_DEVICE_TYPE_VENDOR:
87 case CONNMAN_DEVICE_TYPE_ETHERNET:
89 case CONNMAN_DEVICE_TYPE_WIFI:
91 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
93 case CONNMAN_DEVICE_TYPE_GPS:
95 case CONNMAN_DEVICE_TYPE_CELLULAR:
97 case CONNMAN_DEVICE_TYPE_GADGET:
105 static const char *type2string(enum connman_device_type type)
108 case CONNMAN_DEVICE_TYPE_UNKNOWN:
109 case CONNMAN_DEVICE_TYPE_VENDOR:
111 case CONNMAN_DEVICE_TYPE_ETHERNET:
113 case CONNMAN_DEVICE_TYPE_WIFI:
115 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
117 case CONNMAN_DEVICE_TYPE_GPS:
119 case CONNMAN_DEVICE_TYPE_CELLULAR:
121 case CONNMAN_DEVICE_TYPE_GADGET:
129 enum connman_service_type __connman_device_get_service_type(
130 struct connman_device *device)
132 enum connman_device_type type = connman_device_get_type(device);
135 case CONNMAN_DEVICE_TYPE_UNKNOWN:
136 case CONNMAN_DEVICE_TYPE_VENDOR:
137 case CONNMAN_DEVICE_TYPE_GPS:
139 case CONNMAN_DEVICE_TYPE_ETHERNET:
140 return CONNMAN_SERVICE_TYPE_ETHERNET;
141 case CONNMAN_DEVICE_TYPE_WIFI:
142 return CONNMAN_SERVICE_TYPE_WIFI;
143 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
144 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
145 case CONNMAN_DEVICE_TYPE_CELLULAR:
146 return CONNMAN_SERVICE_TYPE_CELLULAR;
147 case CONNMAN_DEVICE_TYPE_GADGET:
148 return CONNMAN_SERVICE_TYPE_GADGET;
152 return CONNMAN_SERVICE_TYPE_UNKNOWN;
155 static gboolean device_pending_reset(gpointer user_data)
157 struct connman_device *device = user_data;
159 DBG("device %p", device);
161 /* Power request timedout, reset power pending state. */
162 device->pending_timeout = 0;
163 device->powered_pending = PENDING_NONE;
168 int __connman_device_enable(struct connman_device *device)
172 DBG("device %p", device);
174 if (!device->driver || !device->driver->enable)
177 /* There is an ongoing power disable request. */
178 if (device->powered_pending == PENDING_DISABLE)
181 if (device->powered_pending == PENDING_ENABLE)
184 if (device->powered_pending == PENDING_NONE && device->powered)
187 if (device->index > 0) {
188 err = connman_inet_ifup(device->index);
189 if (err < 0 && err != -EALREADY)
193 device->powered_pending = PENDING_ENABLE;
195 err = device->driver->enable(device);
197 * device gets enabled right away.
198 * Invoke the callback
201 connman_device_set_powered(device, true);
205 if (err == -EALREADY) {
206 /* If device is already powered, but connman is not updated */
207 connman_device_set_powered(device, true);
211 * if err == -EINPROGRESS, then the DBus call to the respective daemon
212 * was successful. We set a 4 sec timeout so if the daemon never
213 * returns a reply, we would reset the pending request.
215 if (err == -EINPROGRESS)
216 device->pending_timeout = g_timeout_add_seconds(4,
217 device_pending_reset, device);
222 int __connman_device_disable(struct connman_device *device)
226 DBG("device %p", device);
228 /* Ongoing power enable request */
229 if (device->powered_pending == PENDING_ENABLE)
232 if (device->powered_pending == PENDING_DISABLE)
235 if (device->powered_pending == PENDING_NONE && !device->powered)
238 device->powered_pending = PENDING_DISABLE;
240 if (device->network) {
241 struct connman_service *service =
242 connman_service_lookup_from_network(device->network);
245 __connman_service_disconnect(service);
247 connman_network_set_connected(device->network, false);
250 if (!device->driver || !device->driver->disable)
253 err = device->driver->disable(device);
254 if (err == 0 || err == -EALREADY) {
255 connman_device_set_powered(device, false);
259 if (err == -EINPROGRESS)
260 device->pending_timeout = g_timeout_add_seconds(4,
261 device_pending_reset, device);
266 static void probe_driver(struct connman_device_driver *driver)
270 DBG("driver %p name %s", driver, driver->name);
272 for (list = device_list; list; list = list->next) {
273 struct connman_device *device = list->data;
278 if (driver->type != device->type)
281 if (driver->probe(device) < 0)
284 device->driver = driver;
286 __connman_technology_add_device(device);
290 static void remove_device(struct connman_device *device)
292 DBG("device %p", device);
294 __connman_device_disable(device);
296 __connman_technology_remove_device(device);
298 if (device->driver->remove)
299 device->driver->remove(device);
301 device->driver = NULL;
304 static void remove_driver(struct connman_device_driver *driver)
308 DBG("driver %p name %s", driver, driver->name);
310 for (list = device_list; list; list = list->next) {
311 struct connman_device *device = list->data;
313 if (device->driver == driver)
314 remove_device(device);
318 bool __connman_device_has_driver(struct connman_device *device)
320 if (!device || !device->driver)
326 static GSList *driver_list = NULL;
328 static gint compare_priority(gconstpointer a, gconstpointer b)
330 const struct connman_device_driver *driver1 = a;
331 const struct connman_device_driver *driver2 = b;
333 return driver2->priority - driver1->priority;
337 * connman_device_driver_register:
338 * @driver: device driver definition
340 * Register a new device driver
342 * Returns: %0 on success
344 int connman_device_driver_register(struct connman_device_driver *driver)
346 DBG("driver %p name %s", driver, driver->name);
348 driver_list = g_slist_insert_sorted(driver_list, driver,
350 probe_driver(driver);
356 * connman_device_driver_unregister:
357 * @driver: device driver definition
359 * Remove a previously registered device driver
361 void connman_device_driver_unregister(struct connman_device_driver *driver)
363 DBG("driver %p name %s", driver, driver->name);
365 driver_list = g_slist_remove(driver_list, driver);
367 remove_driver(driver);
370 static void free_network(gpointer data)
372 struct connman_network *network = data;
374 DBG("network %p", network);
376 __connman_network_set_device(network, NULL);
378 connman_network_unref(network);
381 static void device_destruct(struct connman_device *device)
383 DBG("device %p name %s", device, device->name);
385 clear_pending_trigger(device);
387 g_hash_table_destroy(device->networks);
388 device->networks = NULL;
390 g_free(device->ident);
391 g_free(device->node);
392 g_free(device->name);
393 g_free(device->address);
394 g_free(device->interface);
395 g_free(device->path);
397 g_free(device->last_network);
403 * connman_device_create:
404 * @node: device node name (for example an address)
407 * Allocate a new device of given #type and assign the #node name to it.
409 * Returns: a newly-allocated #connman_device structure
411 struct connman_device *connman_device_create(const char *node,
412 enum connman_device_type type)
414 struct connman_device *device;
416 DBG("node %s type %d", node, type);
418 device = g_try_new0(struct connman_device, 1);
422 DBG("device %p", device);
424 device->refcount = 1;
427 device->name = g_strdup(type2description(device->type));
429 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
430 g_free, free_network);
432 device_list = g_slist_prepend(device_list, device);
438 * connman_device_ref:
439 * @device: device structure
441 * Increase reference counter of device
443 struct connman_device *connman_device_ref_debug(struct connman_device *device,
444 const char *file, int line, const char *caller)
446 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
449 __sync_fetch_and_add(&device->refcount, 1);
455 * connman_device_unref:
456 * @device: device structure
458 * Decrease reference counter of device
460 void connman_device_unref_debug(struct connman_device *device,
461 const char *file, int line, const char *caller)
463 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
466 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
469 if (device->driver) {
470 device->driver->remove(device);
471 device->driver = NULL;
474 device_list = g_slist_remove(device_list, device);
476 device_destruct(device);
479 const char *__connman_device_get_type(struct connman_device *device)
481 return type2string(device->type);
485 * connman_device_get_type:
486 * @device: device structure
490 enum connman_device_type connman_device_get_type(struct connman_device *device)
496 * connman_device_set_index:
497 * @device: device structure
498 * @index: index number
500 * Set index number of device
502 void connman_device_set_index(struct connman_device *device, int index)
504 device->index = index;
508 * connman_device_get_index:
509 * @device: device structure
511 * Get index number of device
513 int connman_device_get_index(struct connman_device *device)
515 return device->index;
519 * connman_device_set_interface:
520 * @device: device structure
521 * @interface: interface name
523 * Set interface name of device
525 void connman_device_set_interface(struct connman_device *device,
526 const char *interface)
528 g_free(device->interface);
529 device->interface = g_strdup(interface);
532 const char *str = type2description(device->type);
533 if (str && device->interface)
534 device->name = g_strdup_printf("%s (%s)", str,
540 * connman_device_set_ident:
541 * @device: device structure
542 * @ident: unique identifier
544 * Set unique identifier of device
546 void connman_device_set_ident(struct connman_device *device,
549 g_free(device->ident);
550 device->ident = g_strdup(ident);
553 const char *connman_device_get_ident(struct connman_device *device)
555 return device->ident;
559 * connman_device_set_powered:
560 * @device: device structure
561 * @powered: powered state
563 * Change power state of device
565 int connman_device_set_powered(struct connman_device *device,
568 enum connman_service_type type;
570 DBG("device %p powered %d", device, powered);
572 if (device->powered == powered)
575 clear_pending_trigger(device);
577 device->powered_pending = PENDING_NONE;
579 device->powered = powered;
581 type = __connman_device_get_service_type(device);
583 if (!device->powered) {
584 __connman_technology_disabled(type);
588 __connman_technology_enabled(type);
590 device->scanning = false;
592 if (device->driver && device->driver->scan)
593 device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN, device,
594 NULL, 0, NULL, NULL, NULL, NULL);
599 bool connman_device_get_powered(struct connman_device *device)
601 return device->powered;
604 static int device_scan(enum connman_service_type type,
605 struct connman_device *device)
607 if (!device->driver || !device->driver->scan)
610 if (!device->powered)
613 return device->driver->scan(type, device, NULL, 0,
614 NULL, NULL, NULL, NULL);
617 int __connman_device_disconnect(struct connman_device *device)
622 DBG("device %p", device);
624 g_hash_table_iter_init(&iter, device->networks);
626 while (g_hash_table_iter_next(&iter, &key, &value)) {
627 struct connman_network *network = value;
629 if (connman_network_get_connecting(network)) {
631 * Skip network in the process of connecting.
632 * This is a workaround for WiFi networks serviced
633 * by the supplicant plugin that hold a reference
634 * to the network. If we disconnect the network
635 * here then the referenced object will not be
636 * registered and usage (like launching DHCP client)
637 * will fail. There is nothing to be gained by
638 * removing the network here anyway.
640 connman_warn("Skipping disconnect of %s, network is connecting.",
641 connman_network_get_identifier(network));
645 __connman_network_disconnect(network);
651 int connman_device_reconnect_service(struct connman_device *device)
653 DBG("device %p", device);
655 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
660 static void mark_network_available(gpointer key, gpointer value,
663 struct connman_network *network = value;
665 connman_network_set_available(network, true);
668 static void mark_network_unavailable(gpointer key, gpointer value,
671 struct connman_network *network = value;
673 if (connman_network_get_connected(network) ||
674 connman_network_get_connecting(network))
677 connman_network_set_available(network, false);
680 static gboolean remove_unavailable_network(gpointer key, gpointer value,
683 struct connman_network *network = value;
685 if (connman_network_get_connected(network))
688 if (connman_network_get_available(network))
694 void __connman_device_cleanup_networks(struct connman_device *device)
696 g_hash_table_foreach_remove(device->networks,
697 remove_unavailable_network, NULL);
700 bool connman_device_get_scanning(struct connman_device *device)
702 return device->scanning;
705 void connman_device_reset_scanning(struct connman_device *device)
707 g_hash_table_foreach(device->networks,
708 mark_network_available, NULL);
712 * connman_device_set_scanning:
713 * @device: device structure
714 * @scanning: scanning state
716 * Change scanning state of device
718 int connman_device_set_scanning(struct connman_device *device,
719 enum connman_service_type type, bool scanning)
721 DBG("device %p scanning %d", device, scanning);
723 if (!device->driver || !device->driver->scan)
726 if (device->scanning == scanning)
729 device->scanning = scanning;
732 __connman_technology_scan_started(device);
734 g_hash_table_foreach(device->networks,
735 mark_network_unavailable, NULL);
740 __connman_device_cleanup_networks(device);
742 __connman_technology_scan_stopped(device, type);
744 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
750 * connman_device_set_string:
751 * @device: device structure
752 * @key: unique identifier
753 * @value: string value
755 * Set string value for specific key
757 int connman_device_set_string(struct connman_device *device,
758 const char *key, const char *value)
760 DBG("device %p key %s value %s", device, key, value);
762 if (g_str_equal(key, "Address")) {
763 g_free(device->address);
764 device->address = g_strdup(value);
765 } else if (g_str_equal(key, "Name")) {
766 g_free(device->name);
767 device->name = g_strdup(value);
768 } else if (g_str_equal(key, "Node")) {
769 g_free(device->node);
770 device->node = g_strdup(value);
771 } else if (g_str_equal(key, "Path")) {
772 g_free(device->path);
773 device->path = g_strdup(value);
782 * connman_device_get_string:
783 * @device: device structure
784 * @key: unique identifier
786 * Get string value for specific key
788 const char *connman_device_get_string(struct connman_device *device,
791 DBG("device %p key %s", device, key);
793 if (g_str_equal(key, "Address"))
794 return device->address;
795 else if (g_str_equal(key, "Name"))
797 else if (g_str_equal(key, "Node"))
799 else if (g_str_equal(key, "Interface"))
800 return device->interface;
801 else if (g_str_equal(key, "Path"))
808 * connman_device_add_network:
809 * @device: device structure
810 * @network: network structure
812 * Add new network to the device
814 int connman_device_add_network(struct connman_device *device,
815 struct connman_network *network)
817 const char *identifier = connman_network_get_identifier(network);
819 DBG("device %p network %p", device, network);
824 connman_network_ref(network);
826 __connman_network_set_device(network, device);
828 g_hash_table_replace(device->networks, g_strdup(identifier),
835 * connman_device_get_network:
836 * @device: device structure
837 * @identifier: network identifier
839 * Get network for given identifier
841 struct connman_network *connman_device_get_network(struct connman_device *device,
842 const char *identifier)
844 DBG("device %p identifier %s", device, identifier);
846 return g_hash_table_lookup(device->networks, identifier);
850 * connman_device_remove_network:
851 * @device: device structure
852 * @identifier: network identifier
854 * Remove network for given identifier
856 int connman_device_remove_network(struct connman_device *device,
857 struct connman_network *network)
859 const char *identifier;
861 DBG("device %p network %p", device, network);
866 identifier = connman_network_get_identifier(network);
867 g_hash_table_remove(device->networks, identifier);
872 void __connman_device_set_network(struct connman_device *device,
873 struct connman_network *network)
880 if (device->network == network)
884 name = connman_network_get_string(network, "Name");
885 g_free(device->last_network);
886 device->last_network = g_strdup(name);
888 device->network = network;
890 g_free(device->last_network);
891 device->last_network = NULL;
893 device->network = NULL;
897 static bool match_driver(struct connman_device *device,
898 struct connman_device_driver *driver)
900 if (device->type == driver->type ||
901 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
908 * connman_device_register:
909 * @device: device structure
911 * Register device with the system
913 int connman_device_register(struct connman_device *device)
917 DBG("device %p name %s", device, device->name);
922 for (list = driver_list; list; list = list->next) {
923 struct connman_device_driver *driver = list->data;
925 if (!match_driver(device, driver))
928 DBG("driver %p name %s", driver, driver->name);
930 if (driver->probe(device) == 0) {
931 device->driver = driver;
939 return __connman_technology_add_device(device);
943 * connman_device_unregister:
944 * @device: device structure
946 * Unregister device with the system
948 void connman_device_unregister(struct connman_device *device)
950 DBG("device %p name %s", device, device->name);
955 remove_device(device);
959 * connman_device_get_data:
960 * @device: device structure
962 * Get private device data pointer
964 void *connman_device_get_data(struct connman_device *device)
966 return device->driver_data;
970 * connman_device_set_data:
971 * @device: device structure
972 * @data: data pointer
974 * Set private device data pointer
976 void connman_device_set_data(struct connman_device *device, void *data)
978 device->driver_data = data;
981 struct connman_device *__connman_device_find_device(
982 enum connman_service_type type)
986 for (list = device_list; list; list = list->next) {
987 struct connman_device *device = list->data;
988 enum connman_service_type service_type =
989 __connman_device_get_service_type(device);
991 if (service_type != type)
1000 struct connman_device *connman_device_find_by_index(int index)
1004 for (list = device_list; list; list = list->next) {
1005 struct connman_device *device = list->data;
1006 if (device->index == index)
1014 * connman_device_set_regdom
1015 * @device: device structure
1016 * @alpha2: string representing regulatory domain
1018 * Set regulatory domain on device basis
1020 int connman_device_set_regdom(struct connman_device *device,
1023 if (!device->driver || !device->driver->set_regdom)
1026 if (!device->powered)
1029 return device->driver->set_regdom(device, alpha2);
1033 * connman_device_regdom_notify
1034 * @device: device structure
1035 * @alpha2: string representing regulatory domain
1037 * Notify on setting regulatory domain on device basis
1039 void connman_device_regdom_notify(struct connman_device *device,
1040 int result, const char *alpha2)
1042 __connman_technology_notify_regdom_by_device(device, result, alpha2);
1045 #if defined TIZEN_EXT
1046 static int device_specific_scan(enum connman_service_type type,
1047 struct connman_device *device,
1048 int scan_type, GSList *specific_scan_list)
1050 if (!device->driver || !device->driver->specific_scan)
1053 if (!device->powered)
1056 return device->driver->specific_scan(type, device, scan_type,
1057 specific_scan_list, NULL);
1060 int __connman_device_request_specific_scan(enum connman_service_type type,
1061 int scan_type, GSList *specific_scan_list)
1063 bool success = false;
1064 int last_err = -ENOSYS;
1069 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1070 case CONNMAN_SERVICE_TYPE_SYSTEM:
1071 case CONNMAN_SERVICE_TYPE_ETHERNET:
1072 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1073 case CONNMAN_SERVICE_TYPE_CELLULAR:
1074 case CONNMAN_SERVICE_TYPE_GPS:
1075 case CONNMAN_SERVICE_TYPE_VPN:
1076 case CONNMAN_SERVICE_TYPE_GADGET:
1078 case CONNMAN_SERVICE_TYPE_WIFI:
1079 case CONNMAN_SERVICE_TYPE_P2P:
1083 for (list = device_list; list; list = list->next) {
1084 struct connman_device *device = list->data;
1085 enum connman_service_type service_type =
1086 __connman_device_get_service_type(device);
1088 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1089 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1090 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1092 } else if (service_type != type)
1096 err = device_specific_scan(type, device, scan_type, specific_scan_list);
1097 if (err == 0 || err == -EINPROGRESS) {
1101 DBG("device %p err %d", device, err);
1112 int __connman_device_request_scan(enum connman_service_type type)
1114 bool success = false;
1115 int last_err = -ENOSYS;
1120 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1121 case CONNMAN_SERVICE_TYPE_SYSTEM:
1122 case CONNMAN_SERVICE_TYPE_ETHERNET:
1123 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1124 case CONNMAN_SERVICE_TYPE_CELLULAR:
1125 case CONNMAN_SERVICE_TYPE_GPS:
1126 case CONNMAN_SERVICE_TYPE_VPN:
1127 case CONNMAN_SERVICE_TYPE_GADGET:
1129 case CONNMAN_SERVICE_TYPE_WIFI:
1130 case CONNMAN_SERVICE_TYPE_P2P:
1134 for (list = device_list; list; list = list->next) {
1135 struct connman_device *device = list->data;
1136 enum connman_service_type service_type =
1137 __connman_device_get_service_type(device);
1139 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1140 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1141 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1143 } else if (service_type != type)
1147 err = device_scan(type, device);
1148 #if defined TIZEN_EXT
1149 /* When Scan is already in progress then return Error so that
1150 * wifi-manager can block the scan-done signal to be sent to
1151 * application and start requested scan after scan already in progress
1152 * is completed then notify to application about the scan event */
1153 if (err == 0 || err == -EINPROGRESS) {
1155 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1160 DBG("device %p err %d", device, err);
1170 int __connman_device_request_hidden_scan(struct connman_device *device,
1171 const char *ssid, unsigned int ssid_len,
1172 const char *identity, const char *passphrase,
1173 const char *security, void *user_data)
1175 DBG("device %p", device);
1177 if (!device || !device->driver ||
1178 !device->driver->scan)
1181 return device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN,
1182 device, ssid, ssid_len, identity,
1183 passphrase, security, user_data);
1186 #if defined TIZEN_EXT
1187 char *index2ident(int index, const char *prefix)
1189 static char *index2ident(int index, const char *prefix)
1193 struct ether_addr eth;
1200 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1204 memset(&ifr, 0, sizeof(ifr));
1205 ifr.ifr_ifindex = index;
1207 err = ioctl(sk, SIOCGIFNAME, &ifr);
1210 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1217 len = prefix ? strlen(prefix) + 18 : 18;
1219 str = g_malloc(len);
1223 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1224 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1225 prefix ? prefix : "",
1226 eth.ether_addr_octet[0],
1227 eth.ether_addr_octet[1],
1228 eth.ether_addr_octet[2],
1229 eth.ether_addr_octet[3],
1230 eth.ether_addr_octet[4],
1231 eth.ether_addr_octet[5]);
1236 #if defined TIZEN_EXT
1237 char *index2addr(int index)
1239 static char *index2addr(int index)
1243 struct ether_addr eth;
1250 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1254 memset(&ifr, 0, sizeof(ifr));
1255 ifr.ifr_ifindex = index;
1257 err = ioctl(sk, SIOCGIFNAME, &ifr);
1260 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1271 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1272 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1273 eth.ether_addr_octet[0],
1274 eth.ether_addr_octet[1],
1275 eth.ether_addr_octet[2],
1276 eth.ether_addr_octet[3],
1277 eth.ether_addr_octet[4],
1278 eth.ether_addr_octet[5]);
1283 struct connman_device *connman_device_create_from_index(int index)
1285 enum connman_device_type type;
1286 struct connman_device *device;
1287 char *devname, *ident = NULL;
1288 char *addr = NULL, *name = NULL;
1293 devname = connman_inet_ifname(index);
1297 if (__connman_device_isfiltered(devname)) {
1298 connman_info("Ignoring interface %s (filtered)", devname);
1303 type = __connman_rtnl_get_device_type(index);
1306 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1307 connman_info("Ignoring interface %s (type unknown)", devname);
1310 case CONNMAN_DEVICE_TYPE_ETHERNET:
1311 case CONNMAN_DEVICE_TYPE_GADGET:
1312 case CONNMAN_DEVICE_TYPE_WIFI:
1313 name = index2ident(index, "");
1314 addr = index2addr(index);
1316 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1317 case CONNMAN_DEVICE_TYPE_CELLULAR:
1318 case CONNMAN_DEVICE_TYPE_GPS:
1319 case CONNMAN_DEVICE_TYPE_VENDOR:
1320 name = g_strdup(devname);
1324 device = connman_device_create(name, type);
1329 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1330 case CONNMAN_DEVICE_TYPE_VENDOR:
1331 case CONNMAN_DEVICE_TYPE_GPS:
1333 case CONNMAN_DEVICE_TYPE_ETHERNET:
1334 case CONNMAN_DEVICE_TYPE_GADGET:
1335 ident = index2ident(index, NULL);
1337 case CONNMAN_DEVICE_TYPE_WIFI:
1338 ident = index2ident(index, NULL);
1340 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1342 case CONNMAN_DEVICE_TYPE_CELLULAR:
1343 ident = index2ident(index, NULL);
1347 connman_device_set_index(device, index);
1348 connman_device_set_interface(device, devname);
1351 connman_device_set_ident(device, ident);
1355 connman_device_set_string(device, "Address", addr);
1365 bool __connman_device_isfiltered(const char *devname)
1368 char **blacklisted_interfaces;
1374 for (pattern = device_filter, match = false; *pattern; pattern++) {
1375 if (g_pattern_match_simple(*pattern, devname)) {
1382 DBG("ignoring device %s (match)", devname);
1387 if (g_pattern_match_simple("dummy*", devname)) {
1388 DBG("ignoring dummy networking devices");
1392 if (!nodevice_filter)
1395 for (pattern = nodevice_filter; *pattern; pattern++) {
1396 if (g_pattern_match_simple(*pattern, devname)) {
1397 DBG("ignoring device %s (no match)", devname);
1403 if (__connman_inet_isrootnfs_device(devname)) {
1404 DBG("ignoring device %s (rootnfs)", devname);
1408 blacklisted_interfaces =
1409 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1410 if (!blacklisted_interfaces)
1413 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1414 if (g_str_has_prefix(devname, *pattern)) {
1415 DBG("ignoring device %s (blacklist)", devname);
1423 static void cleanup_devices(void)
1426 * Check what interfaces are currently up and if connman is
1427 * suppose to handle the interface, then cleanup the mess
1428 * related to that interface. There might be weird routes etc
1429 * that are related to that interface and that might confuse
1430 * connmand. So in this case we just turn the interface down
1431 * so that kernel removes routes/addresses automatically and
1432 * then proceed the startup.
1434 * Note that this cleanup must be done before rtnl/detect code
1435 * has activated interface watches.
1441 interfaces = __connman_inet_get_running_interfaces();
1446 for (i = 0; interfaces[i]; i++) {
1449 struct sockaddr_in sin_addr, sin_mask;
1451 filtered = __connman_device_isfiltered(interfaces[i]);
1455 index = connman_inet_ifindex(interfaces[i]);
1459 if (!__connman_inet_get_address_netmask(index, &sin_addr,
1461 char *address = g_strdup(inet_ntoa(sin_addr.sin_addr));
1462 char *netmask = g_strdup(inet_ntoa(sin_mask.sin_addr));
1464 if (__connman_config_address_provisioned(address,
1466 DBG("Skip %s which is already provisioned "
1467 "with %s/%s", interfaces[i], address,
1478 DBG("cleaning up %s index %d", interfaces[i], index);
1480 #if defined TIZEN_EXT
1481 if (strcmp(interfaces[i], "wlan0") != 0)
1483 connman_inet_ifdown(index);
1486 * ConnMan will turn the interface UP automatically so
1487 * no need to do it here.
1491 g_strfreev(interfaces);
1494 int __connman_device_init(const char *device, const char *nodevice)
1499 device_filter = g_strsplit(device, ",", -1);
1502 nodevice_filter = g_strsplit(nodevice, ",", -1);
1509 void __connman_device_cleanup(void)
1513 g_strfreev(nodevice_filter);
1514 g_strfreev(device_filter);