5 * Copyright (C) 2007-2012 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 static DBusConnection *connection;
35 static GSList *technology_list = NULL;
38 * List of devices with no technology associated with them either because of
39 * no compiled in support or the driver is not yet loaded.
41 static GSList *techless_device_list = NULL;
42 static GHashTable *rfkill_list;
44 static connman_bool_t global_offlinemode;
46 struct connman_rfkill {
48 enum connman_service_type type;
49 connman_bool_t softblock;
50 connman_bool_t hardblock;
53 struct connman_technology {
55 enum connman_service_type type;
60 connman_bool_t connected;
62 connman_bool_t tethering;
63 char *tethering_ident;
64 char *tethering_passphrase;
66 connman_bool_t enable_persistent; /* Save the tech state */
68 struct connman_technology_driver *driver;
71 DBusMessage *pending_reply;
72 guint pending_timeout;
77 static GSList *driver_list = NULL;
79 static gint compare_priority(gconstpointer a, gconstpointer b)
81 const struct connman_technology_driver *driver1 = a;
82 const struct connman_technology_driver *driver2 = b;
84 return driver2->priority - driver1->priority;
87 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
89 struct connman_rfkill *rfkill = value;
90 enum connman_service_type type = GPOINTER_TO_INT(user_data);
92 /* Calling _technology_rfkill_add will update the tech. */
93 if (rfkill->type == type)
94 __connman_technology_add_rfkill(rfkill->index, type,
95 rfkill->softblock, rfkill->hardblock);
99 * connman_technology_driver_register:
100 * @driver: Technology driver definition
102 * Register a new technology driver
104 * Returns: %0 on success
106 int connman_technology_driver_register(struct connman_technology_driver *driver)
109 struct connman_device *device;
110 enum connman_service_type type;
112 DBG("Registering %s driver", driver->name);
114 driver_list = g_slist_insert_sorted(driver_list, driver,
117 if (techless_device_list == NULL)
121 * Check for technology less devices if this driver
122 * can service any of them.
124 for (list = techless_device_list; list; list = list->next) {
127 type = __connman_device_get_service_type(device);
128 if (type != driver->type)
131 techless_device_list = g_slist_remove(techless_device_list,
134 __connman_technology_add_device(device);
138 /* Check for orphaned rfkill switches. */
139 g_hash_table_foreach(rfkill_list, rfkill_check,
140 GINT_TO_POINTER(driver->type));
146 * connman_technology_driver_unregister:
147 * @driver: Technology driver definition
149 * Remove a previously registered technology driver
151 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
154 struct connman_technology *technology;
156 DBG("Unregistering driver %p name %s", driver, driver->name);
158 for (list = technology_list; list; list = list->next) {
159 technology = list->data;
161 if (technology->driver == NULL)
164 if (technology->type == driver->type) {
165 technology->driver->remove(technology);
166 technology->driver = NULL;
170 driver_list = g_slist_remove(driver_list, driver);
173 static void tethering_changed(struct connman_technology *technology)
175 connman_bool_t tethering = technology->tethering;
177 connman_dbus_property_changed_basic(technology->path,
178 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
179 DBUS_TYPE_BOOLEAN, &tethering);
182 void connman_technology_tethering_notify(struct connman_technology *technology,
183 connman_bool_t enabled)
187 DBG("technology %p enabled %u", technology, enabled);
189 if (technology->tethering == enabled)
192 technology->tethering = enabled;
194 tethering_changed(technology);
197 __connman_tethering_set_enabled();
199 for (list = technology_list; list; list = list->next) {
200 struct connman_technology *other_tech = list->data;
201 if (other_tech->tethering == TRUE)
205 __connman_tethering_set_disabled();
209 static int set_tethering(struct connman_technology *technology,
210 connman_bool_t enabled)
212 const char *ident, *passphrase, *bridge;
214 ident = technology->tethering_ident;
215 passphrase = technology->tethering_passphrase;
217 if (technology->driver == NULL ||
218 technology->driver->set_tethering == NULL)
221 bridge = __connman_tethering_get_bridge();
225 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
226 (ident == NULL || passphrase == NULL))
229 return technology->driver->set_tethering(technology, ident, passphrase,
233 void connman_technology_regdom_notify(struct connman_technology *technology,
239 connman_error("Failed to set regulatory domain");
241 DBG("Regulatory domain set to %s", alpha2);
243 g_free(technology->regdom);
244 technology->regdom = g_strdup(alpha2);
247 static int set_regdom_by_device(struct connman_technology *technology,
252 for (list = technology->device_list; list; list = list->next) {
253 struct connman_device *device = list->data;
255 if (connman_device_set_regdom(device, alpha2) != 0)
262 int connman_technology_set_regdom(const char *alpha2)
266 for (list = technology_list; list; list = list->next) {
267 struct connman_technology *technology = list->data;
269 if (set_regdom_by_device(technology, alpha2) != 0) {
270 if (technology->driver == NULL)
273 if (technology->driver->set_regdom != NULL)
274 technology->driver->set_regdom(technology,
282 static void free_rfkill(gpointer data)
284 struct connman_rfkill *rfkill = data;
289 static const char *get_name(enum connman_service_type type)
292 case CONNMAN_SERVICE_TYPE_UNKNOWN:
293 case CONNMAN_SERVICE_TYPE_SYSTEM:
294 case CONNMAN_SERVICE_TYPE_GPS:
295 case CONNMAN_SERVICE_TYPE_VPN:
296 case CONNMAN_SERVICE_TYPE_GADGET:
298 case CONNMAN_SERVICE_TYPE_ETHERNET:
300 case CONNMAN_SERVICE_TYPE_WIFI:
302 case CONNMAN_SERVICE_TYPE_WIMAX:
304 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
306 case CONNMAN_SERVICE_TYPE_CELLULAR:
313 static void technology_save(struct connman_technology *technology)
318 DBG("technology %p", technology);
320 keyfile = __connman_storage_load_global();
322 keyfile = g_key_file_new();
324 identifier = g_strdup_printf("%s", get_name(technology->type));
325 if (identifier == NULL)
328 g_key_file_set_boolean(keyfile, identifier, "Enable",
329 technology->enable_persistent);
334 __connman_storage_save_global(keyfile);
336 g_key_file_free(keyfile);
341 static void technology_load(struct connman_technology *technology)
345 GError *error = NULL;
346 connman_bool_t enable;
348 DBG("technology %p", technology);
350 keyfile = __connman_storage_load_global();
351 /* Fallback on disabling technology if file not found. */
352 if (keyfile == NULL) {
353 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
354 /* We enable ethernet by default */
355 technology->enable_persistent = TRUE;
357 technology->enable_persistent = FALSE;
361 identifier = g_strdup_printf("%s", get_name(technology->type));
362 if (identifier == NULL)
365 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
367 technology->enable_persistent = enable;
369 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
370 technology->enable_persistent = TRUE;
372 technology->enable_persistent = FALSE;
374 technology_save(technology);
375 g_clear_error(&error);
380 g_key_file_free(keyfile);
385 connman_bool_t __connman_technology_get_offlinemode(void)
387 return global_offlinemode;
390 static void connman_technology_save_offlinemode()
394 keyfile = __connman_storage_load_global();
396 keyfile = g_key_file_new();
398 g_key_file_set_boolean(keyfile, "global",
399 "OfflineMode", global_offlinemode);
401 __connman_storage_save_global(keyfile);
403 g_key_file_free(keyfile);
408 static connman_bool_t connman_technology_load_offlinemode()
411 GError *error = NULL;
412 connman_bool_t offlinemode;
414 /* If there is a error, we enable offlinemode */
415 keyfile = __connman_storage_load_global();
419 offlinemode = g_key_file_get_boolean(keyfile, "global",
420 "OfflineMode", &error);
423 g_clear_error(&error);
426 g_key_file_free(keyfile);
431 static void append_properties(DBusMessageIter *iter,
432 struct connman_technology *technology)
434 DBusMessageIter dict;
436 connman_bool_t powered;
438 connman_dbus_dict_open(iter, &dict);
440 str = get_name(technology->type);
442 connman_dbus_dict_append_basic(&dict, "Name",
443 DBUS_TYPE_STRING, &str);
445 str = __connman_service_type2string(technology->type);
447 connman_dbus_dict_append_basic(&dict, "Type",
448 DBUS_TYPE_STRING, &str);
450 __sync_synchronize();
451 if (technology->enabled > 0)
455 connman_dbus_dict_append_basic(&dict, "Powered",
456 DBUS_TYPE_BOOLEAN, &powered);
458 connman_dbus_dict_append_basic(&dict, "Connected",
460 &technology->connected);
462 connman_dbus_dict_append_basic(&dict, "Tethering",
464 &technology->tethering);
466 if (technology->tethering_ident != NULL)
467 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
469 &technology->tethering_ident);
471 if (technology->tethering_passphrase != NULL)
472 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
474 &technology->tethering_passphrase);
476 connman_dbus_dict_close(iter, &dict);
479 static void technology_added_signal(struct connman_technology *technology)
482 DBusMessageIter iter;
484 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
485 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
489 dbus_message_iter_init_append(signal, &iter);
490 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
492 append_properties(&iter, technology);
494 dbus_connection_send(connection, signal, NULL);
495 dbus_message_unref(signal);
498 static void technology_removed_signal(struct connman_technology *technology)
500 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
501 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
502 DBUS_TYPE_OBJECT_PATH, &technology->path,
506 static DBusMessage *get_properties(DBusConnection *conn,
507 DBusMessage *message, void *user_data)
509 struct connman_technology *technology = user_data;
511 DBusMessageIter iter;
513 reply = dbus_message_new_method_return(message);
517 dbus_message_iter_init_append(reply, &iter);
518 append_properties(&iter, technology);
523 void __connman_technology_list_struct(DBusMessageIter *array)
526 DBusMessageIter entry;
528 for (list = technology_list; list; list = list->next) {
529 struct connman_technology *technology = list->data;
531 if (technology->path == NULL)
534 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
536 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
538 append_properties(&entry, technology);
539 dbus_message_iter_close_container(array, &entry);
543 static gboolean technology_pending_reply(gpointer user_data)
545 struct connman_technology *technology = user_data;
548 /* Power request timedout, send ETIMEDOUT. */
549 if (technology->pending_reply != NULL) {
550 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
552 g_dbus_send_message(connection, reply);
554 dbus_message_unref(technology->pending_reply);
555 technology->pending_reply = NULL;
556 technology->pending_timeout = 0;
562 static int technology_enable(struct connman_technology *technology,
570 DBG("technology %p enable", technology);
572 __sync_synchronize();
573 if (technology->enabled > 0) {
578 if (technology->pending_reply != NULL) {
585 * This is a bit of a trick. When msg is not NULL it means
586 * thats technology_enable was invoked from the manager API.
587 * Hence we save the state here.
589 technology->enable_persistent = TRUE;
590 technology_save(technology);
593 __connman_rfkill_block(technology->type, FALSE);
596 * An empty device list means that devices in the technology
597 * were rfkill blocked. The unblock above will enable the devs.
599 if (technology->device_list == NULL) {
604 for (list = technology->device_list; list; list = list->next) {
605 struct connman_device *device = list->data;
607 err = __connman_device_enable(device);
609 * err = 0 : Device was enabled right away.
610 * If atleast one device gets enabled, we consider
611 * the technology to be enabled.
620 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
625 if (err == -EINPROGRESS) {
626 technology->pending_reply = dbus_message_ref(msg);
627 technology->pending_timeout = g_timeout_add_seconds(10,
628 technology_pending_reply, technology);
630 if (err == -EALREADY)
631 reply = __connman_error_already_enabled(msg);
633 reply = __connman_error_failed(msg, -err);
635 g_dbus_send_message(connection, reply);
642 static int technology_disable(struct connman_technology *technology,
650 DBG("technology %p disable", technology);
652 __sync_synchronize();
653 if (technology->enabled == 0) {
658 if (technology->pending_reply != NULL) {
663 if (technology->tethering == TRUE)
664 set_tethering(technology, FALSE);
667 technology->enable_persistent = FALSE;
668 technology_save(technology);
671 __connman_rfkill_block(technology->type, TRUE);
673 for (list = technology->device_list; list; list = list->next) {
674 struct connman_device *device = list->data;
676 err = __connman_device_disable(device);
684 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
689 if (err == -EINPROGRESS) {
690 technology->pending_reply = dbus_message_ref(msg);
691 technology->pending_timeout = g_timeout_add_seconds(10,
692 technology_pending_reply, technology);
694 if (err == -EALREADY)
695 reply = __connman_error_already_disabled(msg);
697 reply = __connman_error_failed(msg, -err);
699 g_dbus_send_message(connection, reply);
706 static DBusMessage *set_property(DBusConnection *conn,
707 DBusMessage *msg, void *data)
709 struct connman_technology *technology = data;
710 DBusMessageIter iter, value;
714 DBG("conn %p", conn);
716 if (dbus_message_iter_init(msg, &iter) == FALSE)
717 return __connman_error_invalid_arguments(msg);
719 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
720 return __connman_error_invalid_arguments(msg);
722 dbus_message_iter_get_basic(&iter, &name);
723 dbus_message_iter_next(&iter);
725 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
726 return __connman_error_invalid_arguments(msg);
728 dbus_message_iter_recurse(&iter, &value);
730 type = dbus_message_iter_get_arg_type(&value);
732 DBG("property %s", name);
734 if (g_str_equal(name, "Tethering") == TRUE) {
736 connman_bool_t tethering;
738 if (type != DBUS_TYPE_BOOLEAN)
739 return __connman_error_invalid_arguments(msg);
741 dbus_message_iter_get_basic(&value, &tethering);
743 if (technology->tethering == tethering)
744 return __connman_error_already_enabled(msg);
746 err = set_tethering(technology, tethering);
748 return __connman_error_failed(msg, -err);
750 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
753 dbus_message_iter_get_basic(&value, &str);
755 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
756 return __connman_error_not_supported(msg);
758 if (strlen(str) < 1 || strlen(str) > 32)
759 return __connman_error_invalid_arguments(msg);
761 technology->tethering_ident = g_strdup(str);
762 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
765 dbus_message_iter_get_basic(&value, &str);
767 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
768 return __connman_error_not_supported(msg);
770 if (strlen(str) < 8 || strlen(str) > 63)
771 return __connman_error_passphrase_required(msg);
773 technology->tethering_passphrase = g_strdup(str);
774 } else if (g_str_equal(name, "Powered") == TRUE) {
775 connman_bool_t enable;
777 if (type != DBUS_TYPE_BOOLEAN)
778 return __connman_error_invalid_arguments(msg);
780 dbus_message_iter_get_basic(&value, &enable);
782 technology_enable(technology, msg);
784 technology_disable(technology, msg);
787 return __connman_error_invalid_property(msg);
789 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
792 static struct connman_technology *technology_find(enum connman_service_type type)
796 DBG("type %d", type);
798 for (list = technology_list; list; list = list->next) {
799 struct connman_technology *technology = list->data;
801 if (technology->type == type)
808 static void reply_scan_pending(struct connman_technology *technology, int err)
812 DBG("technology %p err %d", technology, err);
814 while (technology->scan_pending != NULL) {
815 DBusMessage *msg = technology->scan_pending->data;
817 DBG("reply to %s", dbus_message_get_sender(msg));
820 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
822 reply = __connman_error_failed(msg, -err);
823 g_dbus_send_message(connection, reply);
824 dbus_message_unref(msg);
826 technology->scan_pending =
827 g_slist_delete_link(technology->scan_pending,
828 technology->scan_pending);
832 void __connman_technology_scan_started(struct connman_device *device)
834 DBG("device %p", device);
837 void __connman_technology_scan_stopped(struct connman_device *device)
840 struct connman_technology *technology;
841 enum connman_service_type type;
844 type = __connman_device_get_service_type(device);
845 technology = technology_find(type);
847 DBG("technology %p device %p", technology, device);
849 if (technology == NULL)
852 for (list = technology->device_list; list != NULL; list = list->next) {
853 struct connman_device *other_device = list->data;
855 if (device == other_device)
858 if (__connman_device_get_service_type(other_device) != type)
861 if (connman_device_get_scanning(other_device) == TRUE)
866 reply_scan_pending(technology, 0);
869 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
870 int result, const char *alpha2)
872 struct connman_technology *technology;
873 enum connman_service_type type;
875 type = __connman_device_get_service_type(device);
876 technology = technology_find(type);
878 if (technology == NULL)
882 if (technology->driver != NULL &&
883 technology->driver->set_regdom != NULL) {
884 technology->driver->set_regdom(technology, alpha2);
891 connman_technology_regdom_notify(technology, alpha2);
894 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
896 struct connman_technology *technology = data;
899 DBG ("technology %p request from %s", technology,
900 dbus_message_get_sender(msg));
902 dbus_message_ref(msg);
903 technology->scan_pending =
904 g_slist_prepend(technology->scan_pending, msg);
906 err = __connman_device_request_scan(technology->type);
908 reply_scan_pending(technology, err);
913 static const GDBusMethodTable technology_methods[] = {
914 { GDBUS_DEPRECATED_METHOD("GetProperties",
915 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
917 { GDBUS_METHOD("SetProperty",
918 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
919 NULL, set_property) },
920 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
924 static const GDBusSignalTable technology_signals[] = {
925 { GDBUS_SIGNAL("PropertyChanged",
926 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
930 static struct connman_technology *technology_get(enum connman_service_type type)
932 struct connman_technology *technology;
933 struct connman_technology_driver *driver = NULL;
938 DBG("type %d", type);
940 str = __connman_service_type2string(type);
944 technology = technology_find(type);
945 if (technology != NULL) {
946 __sync_fetch_and_add(&technology->refcount, 1);
950 /* First check if we have a driver for this technology type */
951 for (list = driver_list; list; list = list->next) {
954 if (driver->type == type)
960 if (driver == NULL) {
961 DBG("No matching driver found for %s.",
962 __connman_service_type2string(type));
966 technology = g_try_new0(struct connman_technology, 1);
967 if (technology == NULL)
970 technology->refcount = 1;
972 technology->type = type;
973 technology->path = g_strdup_printf("%s/technology/%s",
976 technology->device_list = NULL;
978 technology->pending_reply = NULL;
980 technology_load(technology);
982 if (g_dbus_register_interface(connection, technology->path,
983 CONNMAN_TECHNOLOGY_INTERFACE,
984 technology_methods, technology_signals,
985 NULL, technology, NULL) == FALSE) {
986 connman_error("Failed to register %s", technology->path);
991 technology_list = g_slist_append(technology_list, technology);
993 technology_added_signal(technology);
995 technology->driver = driver;
996 err = driver->probe(technology);
998 DBG("Driver probe failed for technology %p", technology);
1000 DBG("technology %p", technology);
1005 static void technology_put(struct connman_technology *technology)
1007 DBG("technology %p", technology);
1009 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1012 reply_scan_pending(technology, -EINTR);
1014 if (technology->driver) {
1015 technology->driver->remove(technology);
1016 technology->driver = NULL;
1019 technology_list = g_slist_remove(technology_list, technology);
1021 technology_removed_signal(technology);
1023 g_dbus_unregister_interface(connection, technology->path,
1024 CONNMAN_TECHNOLOGY_INTERFACE);
1026 g_slist_free(technology->device_list);
1028 g_free(technology->path);
1029 g_free(technology->regdom);
1033 void __connman_technology_add_interface(enum connman_service_type type,
1034 int index, const char *name, const char *ident)
1036 struct connman_technology *technology;
1039 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1040 case CONNMAN_SERVICE_TYPE_SYSTEM:
1042 case CONNMAN_SERVICE_TYPE_ETHERNET:
1043 case CONNMAN_SERVICE_TYPE_WIFI:
1044 case CONNMAN_SERVICE_TYPE_WIMAX:
1045 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1046 case CONNMAN_SERVICE_TYPE_CELLULAR:
1047 case CONNMAN_SERVICE_TYPE_GPS:
1048 case CONNMAN_SERVICE_TYPE_VPN:
1049 case CONNMAN_SERVICE_TYPE_GADGET:
1053 connman_info("Adding interface %s [ %s ]", name,
1054 __connman_service_type2string(type));
1056 technology = technology_find(type);
1058 if (technology == NULL || technology->driver == NULL
1059 || technology->driver->add_interface == NULL)
1062 technology->driver->add_interface(technology,
1063 index, name, ident);
1066 void __connman_technology_remove_interface(enum connman_service_type type,
1067 int index, const char *name, const char *ident)
1069 struct connman_technology *technology;
1072 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1073 case CONNMAN_SERVICE_TYPE_SYSTEM:
1075 case CONNMAN_SERVICE_TYPE_ETHERNET:
1076 case CONNMAN_SERVICE_TYPE_WIFI:
1077 case CONNMAN_SERVICE_TYPE_WIMAX:
1078 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1079 case CONNMAN_SERVICE_TYPE_CELLULAR:
1080 case CONNMAN_SERVICE_TYPE_GPS:
1081 case CONNMAN_SERVICE_TYPE_VPN:
1082 case CONNMAN_SERVICE_TYPE_GADGET:
1086 connman_info("Remove interface %s [ %s ]", name,
1087 __connman_service_type2string(type));
1089 technology = technology_find(type);
1091 if (technology == NULL || technology->driver == NULL)
1094 if (technology->driver->remove_interface)
1095 technology->driver->remove_interface(technology, index);
1098 int __connman_technology_add_device(struct connman_device *device)
1100 struct connman_technology *technology;
1101 enum connman_service_type type;
1103 DBG("device %p", device);
1105 type = __connman_device_get_service_type(device);
1107 technology = technology_get(type);
1108 if (technology == NULL) {
1110 * Since no driver can be found for this device at the moment we
1111 * add it to the techless device list.
1113 techless_device_list = g_slist_prepend(techless_device_list,
1119 if (technology->enable_persistent && !global_offlinemode) {
1120 int err = __connman_device_enable(device);
1122 * connman_technology_add_device() calls __connman_device_enable()
1123 * but since the device is already enabled, the calls does not
1124 * propagate through to connman_technology_enabled via
1125 * connman_device_set_powered.
1127 if (err == -EALREADY)
1128 __connman_technology_enabled(type);
1130 /* if technology persistent state is offline */
1131 if (!technology->enable_persistent)
1132 __connman_device_disable(device);
1134 technology->device_list = g_slist_append(technology->device_list,
1140 int __connman_technology_remove_device(struct connman_device *device)
1142 struct connman_technology *technology;
1143 enum connman_service_type type;
1145 DBG("device %p", device);
1147 type = __connman_device_get_service_type(device);
1149 technology = technology_find(type);
1150 if (technology == NULL) {
1151 techless_device_list = g_slist_remove(techless_device_list,
1156 technology->device_list = g_slist_remove(technology->device_list,
1158 technology_put(technology);
1163 static void powered_changed(struct connman_technology *technology)
1165 connman_bool_t powered;
1167 __sync_synchronize();
1168 if (technology->enabled >0)
1173 connman_dbus_property_changed_basic(technology->path,
1174 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1175 DBUS_TYPE_BOOLEAN, &powered);
1178 int __connman_technology_enabled(enum connman_service_type type)
1180 struct connman_technology *technology;
1182 technology = technology_find(type);
1183 if (technology == NULL)
1186 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1189 powered_changed(technology);
1191 if (technology->pending_reply != NULL) {
1192 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1193 dbus_message_unref(technology->pending_reply);
1194 g_source_remove(technology->pending_timeout);
1195 technology->pending_reply = NULL;
1196 technology->pending_timeout = 0;
1202 int __connman_technology_disabled(enum connman_service_type type)
1204 struct connman_technology *technology;
1206 technology = technology_find(type);
1207 if (technology == NULL)
1210 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1211 return -EINPROGRESS;
1213 if (technology->pending_reply != NULL) {
1214 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1215 dbus_message_unref(technology->pending_reply);
1216 g_source_remove(technology->pending_timeout);
1217 technology->pending_reply = NULL;
1218 technology->pending_timeout = 0;
1221 powered_changed(technology);
1226 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1231 if (global_offlinemode == offlinemode)
1234 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1237 * This is a bit tricky. When you set offlinemode, there is no
1238 * way to differentiate between attempting offline mode and
1239 * resuming offlinemode from last saved profile. We need that
1240 * information in rfkill_update, otherwise it falls back on the
1241 * technology's persistent state. Hence we set the offline mode here
1242 * but save it & call the notifier only if its successful.
1245 global_offlinemode = offlinemode;
1247 /* Traverse technology list, enable/disable each technology. */
1248 for (list = technology_list; list; list = list->next) {
1249 struct connman_technology *technology = list->data;
1252 err = technology_disable(technology, NULL);
1254 if (!offlinemode && technology->enable_persistent)
1255 err = technology_enable(technology, NULL);
1258 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1259 connman_technology_save_offlinemode();
1260 __connman_notifier_offlinemode(offlinemode);
1262 global_offlinemode = connman_technology_load_offlinemode();
1267 void __connman_technology_set_connected(enum connman_service_type type,
1268 connman_bool_t connected)
1270 struct connman_technology *technology;
1272 technology = technology_find(type);
1273 if (technology == NULL)
1276 DBG("technology %p connected %d", technology, connected);
1278 technology->connected = connected;
1280 connman_dbus_property_changed_basic(technology->path,
1281 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1282 DBUS_TYPE_BOOLEAN, &connected);
1285 int __connman_technology_add_rfkill(unsigned int index,
1286 enum connman_service_type type,
1287 connman_bool_t softblock,
1288 connman_bool_t hardblock)
1290 struct connman_technology *technology;
1291 struct connman_rfkill *rfkill;
1293 DBG("index %u type %d soft %u hard %u", index, type,
1294 softblock, hardblock);
1296 rfkill = g_hash_table_lookup(rfkill_list, &index);
1300 rfkill = g_try_new0(struct connman_rfkill, 1);
1304 rfkill->index = index;
1305 rfkill->type = type;
1306 rfkill->softblock = softblock;
1307 rfkill->hardblock = hardblock;
1309 g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1312 technology = technology_get(type);
1313 /* If there is no driver for this type, ignore it. */
1314 if (technology == NULL)
1318 DBG("%s is switched off.", get_name(type));
1323 * If Offline mode is on, we softblock the device if it isnt already.
1324 * If Offline mode is off, we rely on the persistent state of tech.
1326 if (global_offlinemode) {
1328 return __connman_rfkill_block(type, TRUE);
1330 if (technology->enable_persistent && softblock)
1331 return __connman_rfkill_block(type, FALSE);
1332 /* if technology persistent state is offline */
1333 if (!technology->enable_persistent && !softblock)
1334 return __connman_rfkill_block(type, TRUE);
1340 int __connman_technology_update_rfkill(unsigned int index,
1341 enum connman_service_type type,
1342 connman_bool_t softblock,
1343 connman_bool_t hardblock)
1345 struct connman_technology *technology;
1346 struct connman_rfkill *rfkill;
1348 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1350 rfkill = g_hash_table_lookup(rfkill_list, &index);
1354 if (rfkill->softblock == softblock &&
1355 rfkill->hardblock == hardblock)
1358 rfkill->softblock = softblock;
1359 rfkill->hardblock = hardblock;
1362 DBG("%s is switched off.", get_name(type));
1366 technology = technology_find(type);
1367 /* If there is no driver for this type, ignore it. */
1368 if (technology == NULL)
1371 if (!global_offlinemode) {
1372 if (technology->enable_persistent && softblock)
1373 return __connman_rfkill_block(type, FALSE);
1374 if (!technology->enable_persistent && !softblock)
1375 return __connman_rfkill_block(type, TRUE);
1381 int __connman_technology_remove_rfkill(unsigned int index,
1382 enum connman_service_type type)
1384 struct connman_technology *technology;
1385 struct connman_rfkill *rfkill;
1387 DBG("index %u", index);
1389 rfkill = g_hash_table_lookup(rfkill_list, &index);
1393 g_hash_table_remove(rfkill_list, &index);
1395 technology = technology_find(type);
1396 if (technology == NULL)
1399 technology_put(technology);
1404 int __connman_technology_init(void)
1408 connection = connman_dbus_get_connection();
1410 rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1413 global_offlinemode = connman_technology_load_offlinemode();
1415 /* This will create settings file if it is missing */
1416 connman_technology_save_offlinemode();
1421 void __connman_technology_cleanup(void)
1425 g_hash_table_destroy(rfkill_list);
1427 dbus_connection_unref(connection);