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 int connman_technology_set_regdom(const char *alpha2)
251 for (list = technology_list; list; list = list->next) {
252 struct connman_technology *technology = list->data;
254 if (technology->driver == NULL)
257 if (technology->driver->set_regdom)
258 technology->driver->set_regdom(technology, alpha2);
264 static void free_rfkill(gpointer data)
266 struct connman_rfkill *rfkill = data;
271 static const char *get_name(enum connman_service_type type)
274 case CONNMAN_SERVICE_TYPE_UNKNOWN:
275 case CONNMAN_SERVICE_TYPE_SYSTEM:
276 case CONNMAN_SERVICE_TYPE_GPS:
277 case CONNMAN_SERVICE_TYPE_VPN:
278 case CONNMAN_SERVICE_TYPE_GADGET:
280 case CONNMAN_SERVICE_TYPE_ETHERNET:
282 case CONNMAN_SERVICE_TYPE_WIFI:
284 case CONNMAN_SERVICE_TYPE_WIMAX:
286 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
288 case CONNMAN_SERVICE_TYPE_CELLULAR:
295 static void save_state(struct connman_technology *technology)
300 DBG("technology %p", technology);
302 keyfile = __connman_storage_load_global();
304 keyfile = g_key_file_new();
306 identifier = g_strdup_printf("%s", get_name(technology->type));
307 if (identifier == NULL)
310 g_key_file_set_boolean(keyfile, identifier, "Enable",
311 technology->enable_persistent);
316 __connman_storage_save_global(keyfile);
318 g_key_file_free(keyfile);
323 static void load_state(struct connman_technology *technology)
327 GError *error = NULL;
328 connman_bool_t enable;
330 DBG("technology %p", technology);
332 keyfile = __connman_storage_load_global();
333 /* Fallback on disabling technology if file not found. */
334 if (keyfile == NULL) {
335 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
336 /* We enable ethernet by default */
337 technology->enable_persistent = TRUE;
339 technology->enable_persistent = FALSE;
343 identifier = g_strdup_printf("%s", get_name(technology->type));
344 if (identifier == NULL)
347 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
349 technology->enable_persistent = enable;
351 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
352 technology->enable_persistent = TRUE;
354 technology->enable_persistent = FALSE;
356 save_state(technology);
357 g_clear_error(&error);
362 g_key_file_free(keyfile);
367 connman_bool_t __connman_technology_get_offlinemode(void)
369 return global_offlinemode;
372 static void connman_technology_save_offlinemode()
376 keyfile = __connman_storage_load_global();
378 keyfile = g_key_file_new();
380 g_key_file_set_boolean(keyfile, "global",
381 "OfflineMode", global_offlinemode);
383 __connman_storage_save_global(keyfile);
385 g_key_file_free(keyfile);
390 static connman_bool_t connman_technology_load_offlinemode()
393 GError *error = NULL;
394 connman_bool_t offlinemode;
396 /* If there is a error, we enable offlinemode */
397 keyfile = __connman_storage_load_global();
401 offlinemode = g_key_file_get_boolean(keyfile, "global",
402 "OfflineMode", &error);
405 g_clear_error(&error);
408 g_key_file_free(keyfile);
413 static void append_properties(DBusMessageIter *iter,
414 struct connman_technology *technology)
416 DBusMessageIter dict;
418 connman_bool_t powered;
420 connman_dbus_dict_open(iter, &dict);
422 str = get_name(technology->type);
424 connman_dbus_dict_append_basic(&dict, "Name",
425 DBUS_TYPE_STRING, &str);
427 str = __connman_service_type2string(technology->type);
429 connman_dbus_dict_append_basic(&dict, "Type",
430 DBUS_TYPE_STRING, &str);
432 __sync_synchronize();
433 if (technology->enabled > 0)
437 connman_dbus_dict_append_basic(&dict, "Powered",
438 DBUS_TYPE_BOOLEAN, &powered);
440 connman_dbus_dict_append_basic(&dict, "Connected",
442 &technology->connected);
444 connman_dbus_dict_append_basic(&dict, "Tethering",
446 &technology->tethering);
448 if (technology->tethering_ident != NULL)
449 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
451 &technology->tethering_ident);
453 if (technology->tethering_passphrase != NULL)
454 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
456 &technology->tethering_passphrase);
458 connman_dbus_dict_close(iter, &dict);
461 static void technology_added_signal(struct connman_technology *technology)
464 DBusMessageIter iter;
466 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
467 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
471 dbus_message_iter_init_append(signal, &iter);
472 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
474 append_properties(&iter, technology);
476 dbus_connection_send(connection, signal, NULL);
477 dbus_message_unref(signal);
480 static void technology_removed_signal(struct connman_technology *technology)
482 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
483 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
484 DBUS_TYPE_OBJECT_PATH, &technology->path,
488 static DBusMessage *get_properties(DBusConnection *conn,
489 DBusMessage *message, void *user_data)
491 struct connman_technology *technology = user_data;
493 DBusMessageIter iter;
495 reply = dbus_message_new_method_return(message);
499 dbus_message_iter_init_append(reply, &iter);
500 append_properties(&iter, technology);
505 void __connman_technology_list_struct(DBusMessageIter *array)
508 DBusMessageIter entry;
510 for (list = technology_list; list; list = list->next) {
511 struct connman_technology *technology = list->data;
513 if (technology->path == NULL)
516 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
518 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
520 append_properties(&entry, technology);
521 dbus_message_iter_close_container(array, &entry);
525 static gboolean technology_pending_reply(gpointer user_data)
527 struct connman_technology *technology = user_data;
530 /* Power request timedout, send ETIMEDOUT. */
531 if (technology->pending_reply != NULL) {
532 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
534 g_dbus_send_message(connection, reply);
536 dbus_message_unref(technology->pending_reply);
537 technology->pending_reply = NULL;
538 technology->pending_timeout = 0;
544 static int technology_enable(struct connman_technology *technology,
552 DBG("technology %p enable", technology);
554 __sync_synchronize();
555 if (technology->enabled > 0) {
560 if (technology->pending_reply != NULL) {
567 * This is a bit of a trick. When msg is not NULL it means
568 * thats technology_enable was invoked from the manager API.
569 * Hence we save the state here.
571 technology->enable_persistent = TRUE;
572 save_state(technology);
575 __connman_rfkill_block(technology->type, FALSE);
578 * An empty device list means that devices in the technology
579 * were rfkill blocked. The unblock above will enable the devs.
581 if (technology->device_list == NULL) {
586 for (list = technology->device_list; list; list = list->next) {
587 struct connman_device *device = list->data;
589 err = __connman_device_enable(device);
591 * err = 0 : Device was enabled right away.
592 * If atleast one device gets enabled, we consider
593 * the technology to be enabled.
602 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
607 if (err == -EINPROGRESS) {
608 technology->pending_reply = dbus_message_ref(msg);
609 technology->pending_timeout = g_timeout_add_seconds(10,
610 technology_pending_reply, technology);
612 if (err == -EALREADY)
613 reply = __connman_error_already_enabled(msg);
615 reply = __connman_error_failed(msg, -err);
617 g_dbus_send_message(connection, reply);
624 static int technology_disable(struct connman_technology *technology,
632 DBG("technology %p disable", technology);
634 __sync_synchronize();
635 if (technology->enabled == 0) {
640 if (technology->pending_reply != NULL) {
645 if (technology->tethering == TRUE)
646 set_tethering(technology, FALSE);
649 technology->enable_persistent = FALSE;
650 save_state(technology);
653 __connman_rfkill_block(technology->type, TRUE);
655 for (list = technology->device_list; list; list = list->next) {
656 struct connman_device *device = list->data;
658 err = __connman_device_disable(device);
666 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
671 if (err == -EINPROGRESS) {
672 technology->pending_reply = dbus_message_ref(msg);
673 technology->pending_timeout = g_timeout_add_seconds(10,
674 technology_pending_reply, technology);
676 if (err == -EALREADY)
677 reply = __connman_error_already_disabled(msg);
679 reply = __connman_error_failed(msg, -err);
681 g_dbus_send_message(connection, reply);
688 static DBusMessage *set_property(DBusConnection *conn,
689 DBusMessage *msg, void *data)
691 struct connman_technology *technology = data;
692 DBusMessageIter iter, value;
696 DBG("conn %p", conn);
698 if (dbus_message_iter_init(msg, &iter) == FALSE)
699 return __connman_error_invalid_arguments(msg);
701 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
702 return __connman_error_invalid_arguments(msg);
704 dbus_message_iter_get_basic(&iter, &name);
705 dbus_message_iter_next(&iter);
707 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
708 return __connman_error_invalid_arguments(msg);
710 dbus_message_iter_recurse(&iter, &value);
712 type = dbus_message_iter_get_arg_type(&value);
714 DBG("property %s", name);
716 if (g_str_equal(name, "Tethering") == TRUE) {
718 connman_bool_t tethering;
720 if (type != DBUS_TYPE_BOOLEAN)
721 return __connman_error_invalid_arguments(msg);
723 dbus_message_iter_get_basic(&value, &tethering);
725 if (technology->tethering == tethering)
726 return __connman_error_in_progress(msg);
728 err = set_tethering(technology, tethering);
730 return __connman_error_failed(msg, -err);
732 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
735 dbus_message_iter_get_basic(&value, &str);
737 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
738 return __connman_error_not_supported(msg);
740 technology->tethering_ident = g_strdup(str);
741 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
744 dbus_message_iter_get_basic(&value, &str);
746 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
747 return __connman_error_not_supported(msg);
750 return __connman_error_invalid_arguments(msg);
752 technology->tethering_passphrase = g_strdup(str);
753 } else if (g_str_equal(name, "Powered") == TRUE) {
754 connman_bool_t enable;
756 if (type != DBUS_TYPE_BOOLEAN)
757 return __connman_error_invalid_arguments(msg);
759 dbus_message_iter_get_basic(&value, &enable);
761 technology_enable(technology, msg);
763 technology_disable(technology, msg);
766 return __connman_error_invalid_property(msg);
768 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
771 static struct connman_technology *technology_find(enum connman_service_type type)
775 DBG("type %d", type);
777 for (list = technology_list; list; list = list->next) {
778 struct connman_technology *technology = list->data;
780 if (technology->type == type)
787 static void reply_scan_pending(struct connman_technology *technology, int err)
791 DBG("technology %p err %d", technology, err);
793 while (technology->scan_pending != NULL) {
794 DBusMessage *msg = technology->scan_pending->data;
796 DBG("reply to %s", dbus_message_get_sender(msg));
799 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
801 reply = __connman_error_failed(msg, -err);
802 g_dbus_send_message(connection, reply);
803 dbus_message_unref(msg);
805 technology->scan_pending =
806 g_slist_delete_link(technology->scan_pending,
807 technology->scan_pending);
811 void __connman_technology_scan_started(struct connman_device *device)
813 DBG("device %p", device);
816 void __connman_technology_scan_stopped(struct connman_device *device)
819 struct connman_technology *technology;
820 enum connman_service_type type;
823 type = __connman_device_get_service_type(device);
824 technology = technology_find(type);
826 DBG("technology %p device %p", technology, device);
828 if (technology == NULL)
831 for (list = technology->device_list; list != NULL; list = list->next) {
832 struct connman_device *other_device = list->data;
834 if (device == other_device)
837 if (__connman_device_get_service_type(other_device) != type)
840 if (connman_device_get_scanning(other_device) == TRUE)
845 reply_scan_pending(technology, 0);
848 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
849 int result, const char *alpha2)
851 struct connman_technology *technology;
852 enum connman_service_type type;
854 type = __connman_device_get_service_type(device);
855 technology = technology_find(type);
857 if (technology == NULL)
860 connman_technology_regdom_notify(technology, alpha2);
863 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
865 struct connman_technology *technology = data;
868 DBG ("technology %p request from %s", technology,
869 dbus_message_get_sender(msg));
871 dbus_message_ref(msg);
872 technology->scan_pending =
873 g_slist_prepend(technology->scan_pending, msg);
875 err = __connman_device_request_scan(technology->type);
877 reply_scan_pending(technology, err);
882 static const GDBusMethodTable technology_methods[] = {
883 { GDBUS_DEPRECATED_METHOD("GetProperties",
884 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
886 { GDBUS_METHOD("SetProperty",
887 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
888 NULL, set_property) },
889 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
893 static const GDBusSignalTable technology_signals[] = {
894 { GDBUS_SIGNAL("PropertyChanged",
895 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
899 static struct connman_technology *technology_get(enum connman_service_type type)
901 struct connman_technology *technology;
902 struct connman_technology_driver *driver = NULL;
907 DBG("type %d", type);
909 str = __connman_service_type2string(type);
913 technology = technology_find(type);
914 if (technology != NULL) {
915 __sync_fetch_and_add(&technology->refcount, 1);
919 /* First check if we have a driver for this technology type */
920 for (list = driver_list; list; list = list->next) {
923 if (driver->type == type)
929 if (driver == NULL) {
930 DBG("No matching driver found for %s.",
931 __connman_service_type2string(type));
935 technology = g_try_new0(struct connman_technology, 1);
936 if (technology == NULL)
939 technology->refcount = 1;
941 technology->type = type;
942 technology->path = g_strdup_printf("%s/technology/%s",
945 technology->device_list = NULL;
947 technology->pending_reply = NULL;
949 load_state(technology);
951 if (g_dbus_register_interface(connection, technology->path,
952 CONNMAN_TECHNOLOGY_INTERFACE,
953 technology_methods, technology_signals,
954 NULL, technology, NULL) == FALSE) {
955 connman_error("Failed to register %s", technology->path);
960 technology_list = g_slist_append(technology_list, technology);
962 technology_added_signal(technology);
964 technology->driver = driver;
965 err = driver->probe(technology);
967 DBG("Driver probe failed for technology %p", technology);
969 DBG("technology %p", technology);
974 static void technology_put(struct connman_technology *technology)
976 DBG("technology %p", technology);
978 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
981 reply_scan_pending(technology, -EINTR);
983 if (technology->driver) {
984 technology->driver->remove(technology);
985 technology->driver = NULL;
988 technology_list = g_slist_remove(technology_list, technology);
990 technology_removed_signal(technology);
992 g_dbus_unregister_interface(connection, technology->path,
993 CONNMAN_TECHNOLOGY_INTERFACE);
995 g_slist_free(technology->device_list);
997 g_free(technology->path);
998 g_free(technology->regdom);
1002 void __connman_technology_add_interface(enum connman_service_type type,
1003 int index, const char *name, const char *ident)
1005 struct connman_technology *technology;
1008 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1009 case CONNMAN_SERVICE_TYPE_SYSTEM:
1011 case CONNMAN_SERVICE_TYPE_ETHERNET:
1012 case CONNMAN_SERVICE_TYPE_WIFI:
1013 case CONNMAN_SERVICE_TYPE_WIMAX:
1014 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1015 case CONNMAN_SERVICE_TYPE_CELLULAR:
1016 case CONNMAN_SERVICE_TYPE_GPS:
1017 case CONNMAN_SERVICE_TYPE_VPN:
1018 case CONNMAN_SERVICE_TYPE_GADGET:
1022 connman_info("Adding interface %s [ %s ]", name,
1023 __connman_service_type2string(type));
1025 technology = technology_find(type);
1027 if (technology == NULL || technology->driver == NULL
1028 || technology->driver->add_interface == NULL)
1031 technology->driver->add_interface(technology,
1032 index, name, ident);
1035 void __connman_technology_remove_interface(enum connman_service_type type,
1036 int index, const char *name, const char *ident)
1038 struct connman_technology *technology;
1041 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1042 case CONNMAN_SERVICE_TYPE_SYSTEM:
1044 case CONNMAN_SERVICE_TYPE_ETHERNET:
1045 case CONNMAN_SERVICE_TYPE_WIFI:
1046 case CONNMAN_SERVICE_TYPE_WIMAX:
1047 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1048 case CONNMAN_SERVICE_TYPE_CELLULAR:
1049 case CONNMAN_SERVICE_TYPE_GPS:
1050 case CONNMAN_SERVICE_TYPE_VPN:
1051 case CONNMAN_SERVICE_TYPE_GADGET:
1055 connman_info("Remove interface %s [ %s ]", name,
1056 __connman_service_type2string(type));
1058 technology = technology_find(type);
1060 if (technology == NULL || technology->driver == NULL)
1063 if (technology->driver->remove_interface)
1064 technology->driver->remove_interface(technology, index);
1067 int __connman_technology_add_device(struct connman_device *device)
1069 struct connman_technology *technology;
1070 enum connman_service_type type;
1072 DBG("device %p", device);
1074 type = __connman_device_get_service_type(device);
1076 technology = technology_get(type);
1077 if (technology == NULL) {
1079 * Since no driver can be found for this device at the moment we
1080 * add it to the techless device list.
1082 techless_device_list = g_slist_prepend(techless_device_list,
1088 if (technology->enable_persistent && !global_offlinemode) {
1089 int err = __connman_device_enable(device);
1091 * connman_technology_add_device() calls __connman_device_enable()
1092 * but since the device is already enabled, the calls does not
1093 * propagate through to connman_technology_enabled via
1094 * connman_device_set_powered.
1096 if (err == -EALREADY)
1097 __connman_technology_enabled(type);
1099 /* if technology persistent state is offline */
1100 if (!technology->enable_persistent)
1101 __connman_device_disable(device);
1103 technology->device_list = g_slist_append(technology->device_list,
1109 int __connman_technology_remove_device(struct connman_device *device)
1111 struct connman_technology *technology;
1112 enum connman_service_type type;
1114 DBG("device %p", device);
1116 type = __connman_device_get_service_type(device);
1118 technology = technology_find(type);
1119 if (technology == NULL) {
1120 techless_device_list = g_slist_remove(techless_device_list,
1125 technology->device_list = g_slist_remove(technology->device_list,
1127 technology_put(technology);
1132 static void powered_changed(struct connman_technology *technology)
1134 connman_bool_t powered;
1136 __sync_synchronize();
1137 if (technology->enabled >0)
1142 connman_dbus_property_changed_basic(technology->path,
1143 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1144 DBUS_TYPE_BOOLEAN, &powered);
1147 int __connman_technology_enabled(enum connman_service_type type)
1149 struct connman_technology *technology;
1151 technology = technology_find(type);
1152 if (technology == NULL)
1155 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1158 powered_changed(technology);
1160 if (technology->pending_reply != NULL) {
1161 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1162 dbus_message_unref(technology->pending_reply);
1163 g_source_remove(technology->pending_timeout);
1164 technology->pending_reply = NULL;
1165 technology->pending_timeout = 0;
1171 int __connman_technology_disabled(enum connman_service_type type)
1173 struct connman_technology *technology;
1175 technology = technology_find(type);
1176 if (technology == NULL)
1179 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1180 return -EINPROGRESS;
1182 if (technology->pending_reply != NULL) {
1183 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1184 dbus_message_unref(technology->pending_reply);
1185 g_source_remove(technology->pending_timeout);
1186 technology->pending_reply = NULL;
1187 technology->pending_timeout = 0;
1190 powered_changed(technology);
1195 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1200 if (global_offlinemode == offlinemode)
1203 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1206 * This is a bit tricky. When you set offlinemode, there is no
1207 * way to differentiate between attempting offline mode and
1208 * resuming offlinemode from last saved profile. We need that
1209 * information in rfkill_update, otherwise it falls back on the
1210 * technology's persistent state. Hence we set the offline mode here
1211 * but save it & call the notifier only if its successful.
1214 global_offlinemode = offlinemode;
1216 /* Traverse technology list, enable/disable each technology. */
1217 for (list = technology_list; list; list = list->next) {
1218 struct connman_technology *technology = list->data;
1221 err = technology_disable(technology, NULL);
1223 if (!offlinemode && technology->enable_persistent)
1224 err = technology_enable(technology, NULL);
1227 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1228 connman_technology_save_offlinemode();
1229 __connman_notifier_offlinemode(offlinemode);
1231 global_offlinemode = connman_technology_load_offlinemode();
1236 void __connman_technology_set_connected(enum connman_service_type type,
1237 connman_bool_t connected)
1239 struct connman_technology *technology;
1241 technology = technology_find(type);
1242 if (technology == NULL)
1245 DBG("technology %p connected %d", technology, connected);
1247 technology->connected = connected;
1249 connman_dbus_property_changed_basic(technology->path,
1250 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1251 DBUS_TYPE_BOOLEAN, &connected);
1254 int __connman_technology_add_rfkill(unsigned int index,
1255 enum connman_service_type type,
1256 connman_bool_t softblock,
1257 connman_bool_t hardblock)
1259 struct connman_technology *technology;
1260 struct connman_rfkill *rfkill;
1262 DBG("index %u type %d soft %u hard %u", index, type,
1263 softblock, hardblock);
1265 rfkill = g_hash_table_lookup(rfkill_list, &index);
1269 rfkill = g_try_new0(struct connman_rfkill, 1);
1273 rfkill->index = index;
1274 rfkill->type = type;
1275 rfkill->softblock = softblock;
1276 rfkill->hardblock = hardblock;
1278 g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1281 technology = technology_get(type);
1282 /* If there is no driver for this type, ignore it. */
1283 if (technology == NULL)
1287 DBG("%s is switched off.", get_name(type));
1292 * If Offline mode is on, we softblock the device if it isnt already.
1293 * If Offline mode is off, we rely on the persistent state of tech.
1295 if (global_offlinemode) {
1297 return __connman_rfkill_block(type, TRUE);
1299 if (technology->enable_persistent && softblock)
1300 return __connman_rfkill_block(type, FALSE);
1301 /* if technology persistent state is offline */
1302 if (!technology->enable_persistent && !softblock)
1303 return __connman_rfkill_block(type, TRUE);
1309 int __connman_technology_update_rfkill(unsigned int index,
1310 enum connman_service_type type,
1311 connman_bool_t softblock,
1312 connman_bool_t hardblock)
1314 struct connman_technology *technology;
1315 struct connman_rfkill *rfkill;
1317 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1319 rfkill = g_hash_table_lookup(rfkill_list, &index);
1323 if (rfkill->softblock == softblock &&
1324 rfkill->hardblock == hardblock)
1327 rfkill->softblock = softblock;
1328 rfkill->hardblock = hardblock;
1331 DBG("%s is switched off.", get_name(type));
1335 technology = technology_find(type);
1336 /* If there is no driver for this type, ignore it. */
1337 if (technology == NULL)
1340 if (!global_offlinemode) {
1341 if (technology->enable_persistent && softblock)
1342 return __connman_rfkill_block(type, FALSE);
1343 if (!technology->enable_persistent && !softblock)
1344 return __connman_rfkill_block(type, TRUE);
1350 int __connman_technology_remove_rfkill(unsigned int index,
1351 enum connman_service_type type)
1353 struct connman_technology *technology;
1354 struct connman_rfkill *rfkill;
1356 DBG("index %u", index);
1358 rfkill = g_hash_table_lookup(rfkill_list, &index);
1362 g_hash_table_remove(rfkill_list, &index);
1364 technology = technology_find(type);
1365 if (technology == NULL)
1368 technology_put(technology);
1373 int __connman_technology_init(void)
1377 connection = connman_dbus_get_connection();
1379 rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1382 global_offlinemode = connman_technology_load_offlinemode();
1384 /* This will create settings file if it is missing */
1385 connman_technology_save_offlinemode();
1390 void __connman_technology_cleanup(void)
1394 g_hash_table_destroy(rfkill_list);
1396 dbus_connection_unref(connection);