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 save_state(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 load_state(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 save_state(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 save_state(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 save_state(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_in_progress(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 technology->tethering_ident = g_strdup(str);
759 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
762 dbus_message_iter_get_basic(&value, &str);
764 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
765 return __connman_error_not_supported(msg);
768 return __connman_error_invalid_arguments(msg);
770 technology->tethering_passphrase = g_strdup(str);
771 } else if (g_str_equal(name, "Powered") == TRUE) {
772 connman_bool_t enable;
774 if (type != DBUS_TYPE_BOOLEAN)
775 return __connman_error_invalid_arguments(msg);
777 dbus_message_iter_get_basic(&value, &enable);
779 technology_enable(technology, msg);
781 technology_disable(technology, msg);
784 return __connman_error_invalid_property(msg);
786 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
789 static struct connman_technology *technology_find(enum connman_service_type type)
793 DBG("type %d", type);
795 for (list = technology_list; list; list = list->next) {
796 struct connman_technology *technology = list->data;
798 if (technology->type == type)
805 static void reply_scan_pending(struct connman_technology *technology, int err)
809 DBG("technology %p err %d", technology, err);
811 while (technology->scan_pending != NULL) {
812 DBusMessage *msg = technology->scan_pending->data;
814 DBG("reply to %s", dbus_message_get_sender(msg));
817 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
819 reply = __connman_error_failed(msg, -err);
820 g_dbus_send_message(connection, reply);
821 dbus_message_unref(msg);
823 technology->scan_pending =
824 g_slist_delete_link(technology->scan_pending,
825 technology->scan_pending);
829 void __connman_technology_scan_started(struct connman_device *device)
831 DBG("device %p", device);
834 void __connman_technology_scan_stopped(struct connman_device *device)
837 struct connman_technology *technology;
838 enum connman_service_type type;
841 type = __connman_device_get_service_type(device);
842 technology = technology_find(type);
844 DBG("technology %p device %p", technology, device);
846 if (technology == NULL)
849 for (list = technology->device_list; list != NULL; list = list->next) {
850 struct connman_device *other_device = list->data;
852 if (device == other_device)
855 if (__connman_device_get_service_type(other_device) != type)
858 if (connman_device_get_scanning(other_device) == TRUE)
863 reply_scan_pending(technology, 0);
866 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
867 int result, const char *alpha2)
869 struct connman_technology *technology;
870 enum connman_service_type type;
872 type = __connman_device_get_service_type(device);
873 technology = technology_find(type);
875 if (technology == NULL)
879 if (technology->driver != NULL &&
880 technology->driver->set_regdom != NULL) {
881 technology->driver->set_regdom(technology, alpha2);
888 connman_technology_regdom_notify(technology, alpha2);
891 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
893 struct connman_technology *technology = data;
896 DBG ("technology %p request from %s", technology,
897 dbus_message_get_sender(msg));
899 dbus_message_ref(msg);
900 technology->scan_pending =
901 g_slist_prepend(technology->scan_pending, msg);
903 err = __connman_device_request_scan(technology->type);
905 reply_scan_pending(technology, err);
910 static const GDBusMethodTable technology_methods[] = {
911 { GDBUS_DEPRECATED_METHOD("GetProperties",
912 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
914 { GDBUS_METHOD("SetProperty",
915 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
916 NULL, set_property) },
917 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
921 static const GDBusSignalTable technology_signals[] = {
922 { GDBUS_SIGNAL("PropertyChanged",
923 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
927 static struct connman_technology *technology_get(enum connman_service_type type)
929 struct connman_technology *technology;
930 struct connman_technology_driver *driver = NULL;
935 DBG("type %d", type);
937 str = __connman_service_type2string(type);
941 technology = technology_find(type);
942 if (technology != NULL) {
943 __sync_fetch_and_add(&technology->refcount, 1);
947 /* First check if we have a driver for this technology type */
948 for (list = driver_list; list; list = list->next) {
951 if (driver->type == type)
957 if (driver == NULL) {
958 DBG("No matching driver found for %s.",
959 __connman_service_type2string(type));
963 technology = g_try_new0(struct connman_technology, 1);
964 if (technology == NULL)
967 technology->refcount = 1;
969 technology->type = type;
970 technology->path = g_strdup_printf("%s/technology/%s",
973 technology->device_list = NULL;
975 technology->pending_reply = NULL;
977 load_state(technology);
979 if (g_dbus_register_interface(connection, technology->path,
980 CONNMAN_TECHNOLOGY_INTERFACE,
981 technology_methods, technology_signals,
982 NULL, technology, NULL) == FALSE) {
983 connman_error("Failed to register %s", technology->path);
988 technology_list = g_slist_append(technology_list, technology);
990 technology_added_signal(technology);
992 technology->driver = driver;
993 err = driver->probe(technology);
995 DBG("Driver probe failed for technology %p", technology);
997 DBG("technology %p", technology);
1002 static void technology_put(struct connman_technology *technology)
1004 DBG("technology %p", technology);
1006 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1009 reply_scan_pending(technology, -EINTR);
1011 if (technology->driver) {
1012 technology->driver->remove(technology);
1013 technology->driver = NULL;
1016 technology_list = g_slist_remove(technology_list, technology);
1018 technology_removed_signal(technology);
1020 g_dbus_unregister_interface(connection, technology->path,
1021 CONNMAN_TECHNOLOGY_INTERFACE);
1023 g_slist_free(technology->device_list);
1025 g_free(technology->path);
1026 g_free(technology->regdom);
1030 void __connman_technology_add_interface(enum connman_service_type type,
1031 int index, const char *name, const char *ident)
1033 struct connman_technology *technology;
1036 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1037 case CONNMAN_SERVICE_TYPE_SYSTEM:
1039 case CONNMAN_SERVICE_TYPE_ETHERNET:
1040 case CONNMAN_SERVICE_TYPE_WIFI:
1041 case CONNMAN_SERVICE_TYPE_WIMAX:
1042 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1043 case CONNMAN_SERVICE_TYPE_CELLULAR:
1044 case CONNMAN_SERVICE_TYPE_GPS:
1045 case CONNMAN_SERVICE_TYPE_VPN:
1046 case CONNMAN_SERVICE_TYPE_GADGET:
1050 connman_info("Adding interface %s [ %s ]", name,
1051 __connman_service_type2string(type));
1053 technology = technology_find(type);
1055 if (technology == NULL || technology->driver == NULL
1056 || technology->driver->add_interface == NULL)
1059 technology->driver->add_interface(technology,
1060 index, name, ident);
1063 void __connman_technology_remove_interface(enum connman_service_type type,
1064 int index, const char *name, const char *ident)
1066 struct connman_technology *technology;
1069 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1070 case CONNMAN_SERVICE_TYPE_SYSTEM:
1072 case CONNMAN_SERVICE_TYPE_ETHERNET:
1073 case CONNMAN_SERVICE_TYPE_WIFI:
1074 case CONNMAN_SERVICE_TYPE_WIMAX:
1075 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1076 case CONNMAN_SERVICE_TYPE_CELLULAR:
1077 case CONNMAN_SERVICE_TYPE_GPS:
1078 case CONNMAN_SERVICE_TYPE_VPN:
1079 case CONNMAN_SERVICE_TYPE_GADGET:
1083 connman_info("Remove interface %s [ %s ]", name,
1084 __connman_service_type2string(type));
1086 technology = technology_find(type);
1088 if (technology == NULL || technology->driver == NULL)
1091 if (technology->driver->remove_interface)
1092 technology->driver->remove_interface(technology, index);
1095 int __connman_technology_add_device(struct connman_device *device)
1097 struct connman_technology *technology;
1098 enum connman_service_type type;
1100 DBG("device %p", device);
1102 type = __connman_device_get_service_type(device);
1104 technology = technology_get(type);
1105 if (technology == NULL) {
1107 * Since no driver can be found for this device at the moment we
1108 * add it to the techless device list.
1110 techless_device_list = g_slist_prepend(techless_device_list,
1116 if (technology->enable_persistent && !global_offlinemode) {
1117 int err = __connman_device_enable(device);
1119 * connman_technology_add_device() calls __connman_device_enable()
1120 * but since the device is already enabled, the calls does not
1121 * propagate through to connman_technology_enabled via
1122 * connman_device_set_powered.
1124 if (err == -EALREADY)
1125 __connman_technology_enabled(type);
1127 /* if technology persistent state is offline */
1128 if (!technology->enable_persistent)
1129 __connman_device_disable(device);
1131 technology->device_list = g_slist_append(technology->device_list,
1137 int __connman_technology_remove_device(struct connman_device *device)
1139 struct connman_technology *technology;
1140 enum connman_service_type type;
1142 DBG("device %p", device);
1144 type = __connman_device_get_service_type(device);
1146 technology = technology_find(type);
1147 if (technology == NULL) {
1148 techless_device_list = g_slist_remove(techless_device_list,
1153 technology->device_list = g_slist_remove(technology->device_list,
1155 technology_put(technology);
1160 static void powered_changed(struct connman_technology *technology)
1162 connman_bool_t powered;
1164 __sync_synchronize();
1165 if (technology->enabled >0)
1170 connman_dbus_property_changed_basic(technology->path,
1171 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1172 DBUS_TYPE_BOOLEAN, &powered);
1175 int __connman_technology_enabled(enum connman_service_type type)
1177 struct connman_technology *technology;
1179 technology = technology_find(type);
1180 if (technology == NULL)
1183 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1186 powered_changed(technology);
1188 if (technology->pending_reply != NULL) {
1189 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1190 dbus_message_unref(technology->pending_reply);
1191 g_source_remove(technology->pending_timeout);
1192 technology->pending_reply = NULL;
1193 technology->pending_timeout = 0;
1199 int __connman_technology_disabled(enum connman_service_type type)
1201 struct connman_technology *technology;
1203 technology = technology_find(type);
1204 if (technology == NULL)
1207 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1208 return -EINPROGRESS;
1210 if (technology->pending_reply != NULL) {
1211 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1212 dbus_message_unref(technology->pending_reply);
1213 g_source_remove(technology->pending_timeout);
1214 technology->pending_reply = NULL;
1215 technology->pending_timeout = 0;
1218 powered_changed(technology);
1223 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1228 if (global_offlinemode == offlinemode)
1231 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1234 * This is a bit tricky. When you set offlinemode, there is no
1235 * way to differentiate between attempting offline mode and
1236 * resuming offlinemode from last saved profile. We need that
1237 * information in rfkill_update, otherwise it falls back on the
1238 * technology's persistent state. Hence we set the offline mode here
1239 * but save it & call the notifier only if its successful.
1242 global_offlinemode = offlinemode;
1244 /* Traverse technology list, enable/disable each technology. */
1245 for (list = technology_list; list; list = list->next) {
1246 struct connman_technology *technology = list->data;
1249 err = technology_disable(technology, NULL);
1251 if (!offlinemode && technology->enable_persistent)
1252 err = technology_enable(technology, NULL);
1255 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1256 connman_technology_save_offlinemode();
1257 __connman_notifier_offlinemode(offlinemode);
1259 global_offlinemode = connman_technology_load_offlinemode();
1264 void __connman_technology_set_connected(enum connman_service_type type,
1265 connman_bool_t connected)
1267 struct connman_technology *technology;
1269 technology = technology_find(type);
1270 if (technology == NULL)
1273 DBG("technology %p connected %d", technology, connected);
1275 technology->connected = connected;
1277 connman_dbus_property_changed_basic(technology->path,
1278 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1279 DBUS_TYPE_BOOLEAN, &connected);
1282 int __connman_technology_add_rfkill(unsigned int index,
1283 enum connman_service_type type,
1284 connman_bool_t softblock,
1285 connman_bool_t hardblock)
1287 struct connman_technology *technology;
1288 struct connman_rfkill *rfkill;
1290 DBG("index %u type %d soft %u hard %u", index, type,
1291 softblock, hardblock);
1293 rfkill = g_hash_table_lookup(rfkill_list, &index);
1297 rfkill = g_try_new0(struct connman_rfkill, 1);
1301 rfkill->index = index;
1302 rfkill->type = type;
1303 rfkill->softblock = softblock;
1304 rfkill->hardblock = hardblock;
1306 g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1309 technology = technology_get(type);
1310 /* If there is no driver for this type, ignore it. */
1311 if (technology == NULL)
1315 DBG("%s is switched off.", get_name(type));
1320 * If Offline mode is on, we softblock the device if it isnt already.
1321 * If Offline mode is off, we rely on the persistent state of tech.
1323 if (global_offlinemode) {
1325 return __connman_rfkill_block(type, TRUE);
1327 if (technology->enable_persistent && softblock)
1328 return __connman_rfkill_block(type, FALSE);
1329 /* if technology persistent state is offline */
1330 if (!technology->enable_persistent && !softblock)
1331 return __connman_rfkill_block(type, TRUE);
1337 int __connman_technology_update_rfkill(unsigned int index,
1338 enum connman_service_type type,
1339 connman_bool_t softblock,
1340 connman_bool_t hardblock)
1342 struct connman_technology *technology;
1343 struct connman_rfkill *rfkill;
1345 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1347 rfkill = g_hash_table_lookup(rfkill_list, &index);
1351 if (rfkill->softblock == softblock &&
1352 rfkill->hardblock == hardblock)
1355 rfkill->softblock = softblock;
1356 rfkill->hardblock = hardblock;
1359 DBG("%s is switched off.", get_name(type));
1363 technology = technology_find(type);
1364 /* If there is no driver for this type, ignore it. */
1365 if (technology == NULL)
1368 if (!global_offlinemode) {
1369 if (technology->enable_persistent && softblock)
1370 return __connman_rfkill_block(type, FALSE);
1371 if (!technology->enable_persistent && !softblock)
1372 return __connman_rfkill_block(type, TRUE);
1378 int __connman_technology_remove_rfkill(unsigned int index,
1379 enum connman_service_type type)
1381 struct connman_technology *technology;
1382 struct connman_rfkill *rfkill;
1384 DBG("index %u", index);
1386 rfkill = g_hash_table_lookup(rfkill_list, &index);
1390 g_hash_table_remove(rfkill_list, &index);
1392 technology = technology_find(type);
1393 if (technology == NULL)
1396 technology_put(technology);
1401 int __connman_technology_init(void)
1405 connection = connman_dbus_get_connection();
1407 rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1410 global_offlinemode = connman_technology_load_offlinemode();
1412 /* This will create settings file if it is missing */
1413 connman_technology_save_offlinemode();
1418 void __connman_technology_cleanup(void)
1422 g_hash_table_destroy(rfkill_list);
1424 dbus_connection_unref(connection);