5 * Copyright (C) 2007-2010 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 load_state(struct connman_technology *technology)
299 GError *error = NULL;
300 connman_bool_t enable;
302 DBG("technology %p", technology);
304 keyfile = __connman_storage_load_global();
305 /* Fallback on disabling technology if file not found. */
306 if (keyfile == NULL) {
307 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
308 /* We enable ethernet by default */
309 technology->enable_persistent = TRUE;
311 technology->enable_persistent = FALSE;
315 identifier = g_strdup_printf("%s", get_name(technology->type));
316 if (identifier == NULL)
319 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
321 technology->enable_persistent = enable;
323 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
324 technology->enable_persistent = TRUE;
326 technology->enable_persistent = FALSE;
327 g_clear_error(&error);
332 g_key_file_free(keyfile);
337 static void save_state(struct connman_technology *technology)
342 DBG("technology %p", technology);
344 keyfile = __connman_storage_load_global();
346 keyfile = g_key_file_new();
348 identifier = g_strdup_printf("%s", get_name(technology->type));
349 if (identifier == NULL)
352 g_key_file_set_boolean(keyfile, identifier, "Enable",
353 technology->enable_persistent);
358 __connman_storage_save_global(keyfile);
360 g_key_file_free(keyfile);
365 connman_bool_t __connman_technology_get_offlinemode(void)
367 return global_offlinemode;
370 static void connman_technology_save_offlinemode()
374 keyfile = __connman_storage_load_global();
376 keyfile = g_key_file_new();
378 g_key_file_set_boolean(keyfile, "global",
379 "OfflineMode", global_offlinemode);
381 __connman_storage_save_global(keyfile);
383 g_key_file_free(keyfile);
388 static connman_bool_t connman_technology_load_offlinemode()
391 GError *error = NULL;
392 connman_bool_t offlinemode;
394 /* If there is a error, we enable offlinemode */
395 keyfile = __connman_storage_load_global();
399 offlinemode = g_key_file_get_boolean(keyfile, "global",
400 "OfflineMode", &error);
403 g_clear_error(&error);
406 g_key_file_free(keyfile);
411 static void append_properties(DBusMessageIter *iter,
412 struct connman_technology *technology)
414 DBusMessageIter dict;
416 connman_bool_t powered;
418 connman_dbus_dict_open(iter, &dict);
420 str = get_name(technology->type);
422 connman_dbus_dict_append_basic(&dict, "Name",
423 DBUS_TYPE_STRING, &str);
425 str = __connman_service_type2string(technology->type);
427 connman_dbus_dict_append_basic(&dict, "Type",
428 DBUS_TYPE_STRING, &str);
430 __sync_synchronize();
431 if (technology->enabled > 0)
435 connman_dbus_dict_append_basic(&dict, "Powered",
436 DBUS_TYPE_BOOLEAN, &powered);
438 connman_dbus_dict_append_basic(&dict, "Connected",
440 &technology->connected);
442 connman_dbus_dict_append_basic(&dict, "Tethering",
444 &technology->tethering);
446 if (technology->tethering_ident != NULL)
447 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
449 &technology->tethering_ident);
451 if (technology->tethering_passphrase != NULL)
452 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
454 &technology->tethering_passphrase);
456 connman_dbus_dict_close(iter, &dict);
459 static void technology_added_signal(struct connman_technology *technology)
462 DBusMessageIter iter;
464 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
465 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
469 dbus_message_iter_init_append(signal, &iter);
470 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
472 append_properties(&iter, technology);
474 dbus_connection_send(connection, signal, NULL);
475 dbus_message_unref(signal);
478 static void technology_removed_signal(struct connman_technology *technology)
480 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
481 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
482 DBUS_TYPE_OBJECT_PATH, &technology->path,
486 static DBusMessage *get_properties(DBusConnection *conn,
487 DBusMessage *message, void *user_data)
489 struct connman_technology *technology = user_data;
491 DBusMessageIter iter;
493 reply = dbus_message_new_method_return(message);
497 dbus_message_iter_init_append(reply, &iter);
498 append_properties(&iter, technology);
503 void __connman_technology_list_struct(DBusMessageIter *array)
506 DBusMessageIter entry;
508 for (list = technology_list; list; list = list->next) {
509 struct connman_technology *technology = list->data;
511 if (technology->path == NULL)
514 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
516 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
518 append_properties(&entry, technology);
519 dbus_message_iter_close_container(array, &entry);
523 static gboolean technology_pending_reply(gpointer user_data)
525 struct connman_technology *technology = user_data;
528 /* Power request timedout, send ETIMEDOUT. */
529 if (technology->pending_reply != NULL) {
530 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
532 g_dbus_send_message(connection, reply);
534 dbus_message_unref(technology->pending_reply);
535 technology->pending_reply = NULL;
536 technology->pending_timeout = 0;
542 static int technology_enable(struct connman_technology *technology,
550 DBG("technology %p enable", technology);
552 __sync_synchronize();
553 if (technology->enabled > 0) {
558 if (technology->pending_reply != NULL) {
565 * This is a bit of a trick. When msg is not NULL it means
566 * thats technology_enable was invoked from the manager API.
567 * Hence we save the state here.
569 technology->enable_persistent = TRUE;
570 save_state(technology);
573 __connman_rfkill_block(technology->type, FALSE);
576 * An empty device list means that devices in the technology
577 * were rfkill blocked. The unblock above will enable the devs.
579 if (technology->device_list == NULL) {
584 for (list = technology->device_list; list; list = list->next) {
585 struct connman_device *device = list->data;
587 err = __connman_device_enable(device);
589 * err = 0 : Device was enabled right away.
590 * If atleast one device gets enabled, we consider
591 * the technology to be enabled.
600 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
605 if (err == -EINPROGRESS) {
606 technology->pending_reply = dbus_message_ref(msg);
607 technology->pending_timeout = g_timeout_add_seconds(10,
608 technology_pending_reply, technology);
610 reply = __connman_error_failed(msg, -err);
612 g_dbus_send_message(connection, reply);
619 static int technology_disable(struct connman_technology *technology,
627 DBG("technology %p disable", technology);
629 __sync_synchronize();
630 if (technology->enabled == 0) {
635 if (technology->pending_reply != NULL) {
640 if (technology->tethering == TRUE)
641 set_tethering(technology, FALSE);
644 technology->enable_persistent = FALSE;
645 save_state(technology);
648 __connman_rfkill_block(technology->type, TRUE);
650 for (list = technology->device_list; list; list = list->next) {
651 struct connman_device *device = list->data;
653 err = __connman_device_disable(device);
661 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
666 if (err == -EINPROGRESS) {
667 technology->pending_reply = dbus_message_ref(msg);
668 technology->pending_timeout = g_timeout_add_seconds(10,
669 technology_pending_reply, technology);
671 reply = __connman_error_failed(msg, -err);
673 g_dbus_send_message(connection, reply);
680 static DBusMessage *set_property(DBusConnection *conn,
681 DBusMessage *msg, void *data)
683 struct connman_technology *technology = data;
684 DBusMessageIter iter, value;
688 DBG("conn %p", conn);
690 if (dbus_message_iter_init(msg, &iter) == FALSE)
691 return __connman_error_invalid_arguments(msg);
693 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
694 return __connman_error_invalid_arguments(msg);
696 dbus_message_iter_get_basic(&iter, &name);
697 dbus_message_iter_next(&iter);
699 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
700 return __connman_error_invalid_arguments(msg);
702 dbus_message_iter_recurse(&iter, &value);
704 type = dbus_message_iter_get_arg_type(&value);
706 DBG("property %s", name);
708 if (g_str_equal(name, "Tethering") == TRUE) {
710 connman_bool_t tethering;
712 if (type != DBUS_TYPE_BOOLEAN)
713 return __connman_error_invalid_arguments(msg);
715 dbus_message_iter_get_basic(&value, &tethering);
717 if (technology->tethering == tethering)
718 return __connman_error_in_progress(msg);
720 err = set_tethering(technology, tethering);
722 return __connman_error_failed(msg, -err);
724 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
727 dbus_message_iter_get_basic(&value, &str);
729 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
730 return __connman_error_not_supported(msg);
732 technology->tethering_ident = g_strdup(str);
733 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
736 dbus_message_iter_get_basic(&value, &str);
738 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
739 return __connman_error_not_supported(msg);
742 return __connman_error_invalid_arguments(msg);
744 technology->tethering_passphrase = g_strdup(str);
745 } else if (g_str_equal(name, "Powered") == TRUE) {
746 connman_bool_t enable;
748 if (type != DBUS_TYPE_BOOLEAN)
749 return __connman_error_invalid_arguments(msg);
751 dbus_message_iter_get_basic(&value, &enable);
753 technology_enable(technology, msg);
755 technology_disable(technology, msg);
758 return __connman_error_invalid_property(msg);
760 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
763 static struct connman_technology *technology_find(enum connman_service_type type)
767 DBG("type %d", type);
769 for (list = technology_list; list; list = list->next) {
770 struct connman_technology *technology = list->data;
772 if (technology->type == type)
779 static void reply_scan_pending(struct connman_technology *technology, int err)
783 DBG("technology %p err %d", technology, err);
785 while (technology->scan_pending != NULL) {
786 DBusMessage *msg = technology->scan_pending->data;
788 DBG("reply to %s", dbus_message_get_sender(msg));
791 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
793 reply = __connman_error_failed(msg, -err);
794 g_dbus_send_message(connection, reply);
795 dbus_message_unref(msg);
797 technology->scan_pending =
798 g_slist_delete_link(technology->scan_pending,
799 technology->scan_pending);
803 void __connman_technology_scan_started(struct connman_device *device)
805 DBG("device %p", device);
808 void __connman_technology_scan_stopped(struct connman_device *device)
811 struct connman_technology *technology;
812 enum connman_service_type type;
815 type = __connman_device_get_service_type(device);
816 technology = technology_find(type);
818 DBG("technology %p device %p", technology, device);
820 if (technology == NULL)
823 for (list = technology->device_list; list != NULL; list = list->next) {
824 struct connman_device *other_device = list->data;
826 if (device == other_device)
829 if (__connman_device_get_service_type(other_device) != type)
832 if (__connman_device_scanning(other_device))
837 reply_scan_pending(technology, 0);
840 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
842 struct connman_technology *technology = data;
845 DBG ("technology %p request from %s", technology,
846 dbus_message_get_sender(msg));
848 dbus_message_ref(msg);
849 technology->scan_pending =
850 g_slist_prepend(technology->scan_pending, msg);
852 err = __connman_device_request_scan(technology->type);
854 reply_scan_pending(technology, err);
859 static GDBusMethodTable technology_methods[] = {
860 { "GetProperties", "", "a{sv}", get_properties },
861 { "SetProperty", "sv", "", set_property },
862 { "Scan", "", "", scan,
863 G_DBUS_METHOD_FLAG_ASYNC },
867 static GDBusSignalTable technology_signals[] = {
868 { "PropertyChanged", "sv" },
872 static struct connman_technology *technology_get(enum connman_service_type type)
874 struct connman_technology *technology;
875 struct connman_technology_driver *driver = NULL;
880 DBG("type %d", type);
882 str = __connman_service_type2string(type);
886 technology = technology_find(type);
887 if (technology != NULL) {
888 __sync_fetch_and_add(&technology->refcount, 1);
892 /* First check if we have a driver for this technology type */
893 for (list = driver_list; list; list = list->next) {
896 if (driver->type == type)
902 if (driver == NULL) {
903 DBG("No matching driver found for %s.",
904 __connman_service_type2string(type));
908 technology = g_try_new0(struct connman_technology, 1);
909 if (technology == NULL)
912 technology->refcount = 1;
914 technology->type = type;
915 technology->path = g_strdup_printf("%s/technology/%s",
918 technology->device_list = NULL;
920 technology->pending_reply = NULL;
922 load_state(technology);
924 if (g_dbus_register_interface(connection, technology->path,
925 CONNMAN_TECHNOLOGY_INTERFACE,
926 technology_methods, technology_signals,
927 NULL, technology, NULL) == FALSE) {
928 connman_error("Failed to register %s", technology->path);
933 technology_list = g_slist_append(technology_list, technology);
935 technology_added_signal(technology);
937 technology->driver = driver;
938 err = driver->probe(technology);
940 DBG("Driver probe failed for technology %p", technology);
942 DBG("technology %p", technology);
947 static void technology_put(struct connman_technology *technology)
949 DBG("technology %p", technology);
951 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
954 reply_scan_pending(technology, -EINTR);
956 if (technology->driver) {
957 technology->driver->remove(technology);
958 technology->driver = NULL;
961 technology_list = g_slist_remove(technology_list, technology);
963 technology_removed_signal(technology);
965 g_dbus_unregister_interface(connection, technology->path,
966 CONNMAN_TECHNOLOGY_INTERFACE);
968 g_slist_free(technology->device_list);
970 g_free(technology->path);
971 g_free(technology->regdom);
975 void __connman_technology_add_interface(enum connman_service_type type,
976 int index, const char *name, const char *ident)
978 struct connman_technology *technology;
981 case CONNMAN_SERVICE_TYPE_UNKNOWN:
982 case CONNMAN_SERVICE_TYPE_SYSTEM:
984 case CONNMAN_SERVICE_TYPE_ETHERNET:
985 case CONNMAN_SERVICE_TYPE_WIFI:
986 case CONNMAN_SERVICE_TYPE_WIMAX:
987 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
988 case CONNMAN_SERVICE_TYPE_CELLULAR:
989 case CONNMAN_SERVICE_TYPE_GPS:
990 case CONNMAN_SERVICE_TYPE_VPN:
991 case CONNMAN_SERVICE_TYPE_GADGET:
995 connman_info("Adding interface %s [ %s ]", name,
996 __connman_service_type2string(type));
998 technology = technology_find(type);
1000 if (technology == NULL || technology->driver == NULL
1001 || technology->driver->add_interface == NULL)
1004 technology->driver->add_interface(technology,
1005 index, name, ident);
1008 void __connman_technology_remove_interface(enum connman_service_type type,
1009 int index, const char *name, const char *ident)
1011 struct connman_technology *technology;
1014 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1015 case CONNMAN_SERVICE_TYPE_SYSTEM:
1017 case CONNMAN_SERVICE_TYPE_ETHERNET:
1018 case CONNMAN_SERVICE_TYPE_WIFI:
1019 case CONNMAN_SERVICE_TYPE_WIMAX:
1020 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1021 case CONNMAN_SERVICE_TYPE_CELLULAR:
1022 case CONNMAN_SERVICE_TYPE_GPS:
1023 case CONNMAN_SERVICE_TYPE_VPN:
1024 case CONNMAN_SERVICE_TYPE_GADGET:
1028 connman_info("Remove interface %s [ %s ]", name,
1029 __connman_service_type2string(type));
1031 technology = technology_find(type);
1033 if (technology == NULL || technology->driver == NULL)
1036 if (technology->driver->remove_interface)
1037 technology->driver->remove_interface(technology, index);
1040 int __connman_technology_add_device(struct connman_device *device)
1042 struct connman_technology *technology;
1043 enum connman_service_type type;
1045 DBG("device %p", device);
1047 type = __connman_device_get_service_type(device);
1049 technology = technology_get(type);
1050 if (technology == NULL) {
1052 * Since no driver can be found for this device at the moment we
1053 * add it to the techless device list.
1055 techless_device_list = g_slist_prepend(techless_device_list,
1061 if (technology->enable_persistent && !global_offlinemode) {
1062 int err = __connman_device_enable(device);
1064 * connman_technology_add_device() calls __connman_device_enable()
1065 * but since the device is already enabled, the calls does not
1066 * propagate through to connman_technology_enabled via
1067 * connman_device_set_powered.
1069 if (err == -EALREADY)
1070 __connman_technology_enabled(type);
1072 /* if technology persistent state is offline */
1073 if (!technology->enable_persistent)
1074 __connman_device_disable(device);
1076 technology->device_list = g_slist_append(technology->device_list,
1082 int __connman_technology_remove_device(struct connman_device *device)
1084 struct connman_technology *technology;
1085 enum connman_service_type type;
1087 DBG("device %p", device);
1089 type = __connman_device_get_service_type(device);
1091 technology = technology_find(type);
1092 if (technology == NULL) {
1093 techless_device_list = g_slist_remove(techless_device_list,
1098 if (__connman_device_scanning(device))
1099 __connman_technology_scan_stopped(device);
1101 technology->device_list = g_slist_remove(technology->device_list,
1103 technology_put(technology);
1108 static void powered_changed(struct connman_technology *technology)
1110 connman_bool_t powered;
1112 __sync_synchronize();
1113 if (technology->enabled >0)
1118 connman_dbus_property_changed_basic(technology->path,
1119 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1120 DBUS_TYPE_BOOLEAN, &powered);
1123 int __connman_technology_enabled(enum connman_service_type type)
1125 struct connman_technology *technology;
1127 technology = technology_find(type);
1128 if (technology == NULL)
1131 if (__sync_fetch_and_add(&technology->enabled, 1) == 0)
1132 powered_changed(technology);
1134 if (technology->pending_reply != NULL) {
1135 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1136 dbus_message_unref(technology->pending_reply);
1137 g_source_remove(technology->pending_timeout);
1138 technology->pending_reply = NULL;
1139 technology->pending_timeout = 0;
1145 int __connman_technology_disabled(enum connman_service_type type)
1147 struct connman_technology *technology;
1149 technology = technology_find(type);
1150 if (technology == NULL)
1153 if (technology->pending_reply != NULL) {
1154 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1155 dbus_message_unref(technology->pending_reply);
1156 g_source_remove(technology->pending_timeout);
1157 technology->pending_reply = NULL;
1158 technology->pending_timeout = 0;
1161 if (__sync_fetch_and_sub(&technology->enabled, 1) == 1)
1162 powered_changed(technology);
1167 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1172 if (global_offlinemode == offlinemode)
1175 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1178 * This is a bit tricky. When you set offlinemode, there is no
1179 * way to differentiate between attempting offline mode and
1180 * resuming offlinemode from last saved profile. We need that
1181 * information in rfkill_update, otherwise it falls back on the
1182 * technology's persistent state. Hence we set the offline mode here
1183 * but save it & call the notifier only if its successful.
1186 global_offlinemode = offlinemode;
1188 /* Traverse technology list, enable/disable each technology. */
1189 for (list = technology_list; list; list = list->next) {
1190 struct connman_technology *technology = list->data;
1193 err = technology_disable(technology, NULL);
1195 if (!offlinemode && technology->enable_persistent)
1196 err = technology_enable(technology, NULL);
1199 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1200 connman_technology_save_offlinemode();
1201 __connman_notifier_offlinemode(offlinemode);
1203 global_offlinemode = connman_technology_load_offlinemode();
1208 void __connman_technology_set_connected(enum connman_service_type type,
1209 connman_bool_t connected)
1211 struct connman_technology *technology;
1213 technology = technology_find(type);
1214 if (technology == NULL)
1217 DBG("technology %p connected %d", technology, connected);
1219 technology->connected = connected;
1221 connman_dbus_property_changed_basic(technology->path,
1222 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1223 DBUS_TYPE_BOOLEAN, &connected);
1226 int __connman_technology_add_rfkill(unsigned int index,
1227 enum connman_service_type type,
1228 connman_bool_t softblock,
1229 connman_bool_t hardblock)
1231 struct connman_technology *technology;
1232 struct connman_rfkill *rfkill;
1234 DBG("index %u type %d soft %u hard %u", index, type,
1235 softblock, hardblock);
1237 rfkill = g_hash_table_lookup(rfkill_list, &index);
1241 rfkill = g_try_new0(struct connman_rfkill, 1);
1245 rfkill->index = index;
1246 rfkill->type = type;
1247 rfkill->softblock = softblock;
1248 rfkill->hardblock = hardblock;
1250 g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1253 technology = technology_get(type);
1254 /* If there is no driver for this type, ignore it. */
1255 if (technology == NULL)
1259 DBG("%s is switched off.", get_name(type));
1264 * If Offline mode is on, we softblock the device if it isnt already.
1265 * If Offline mode is off, we rely on the persistent state of tech.
1267 if (global_offlinemode) {
1269 return __connman_rfkill_block(type, TRUE);
1271 if (technology->enable_persistent && softblock)
1272 return __connman_rfkill_block(type, FALSE);
1273 /* if technology persistent state is offline */
1274 if (!technology->enable_persistent && !softblock)
1275 return __connman_rfkill_block(type, TRUE);
1281 int __connman_technology_update_rfkill(unsigned int index,
1282 enum connman_service_type type,
1283 connman_bool_t softblock,
1284 connman_bool_t hardblock)
1286 struct connman_technology *technology;
1287 struct connman_rfkill *rfkill;
1289 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1291 rfkill = g_hash_table_lookup(rfkill_list, &index);
1295 if (rfkill->softblock == softblock &&
1296 rfkill->hardblock == hardblock)
1299 rfkill->softblock = softblock;
1300 rfkill->hardblock = hardblock;
1303 DBG("%s is switched off.", get_name(type));
1307 technology = technology_find(type);
1308 /* If there is no driver for this type, ignore it. */
1309 if (technology == NULL)
1312 if (!global_offlinemode) {
1313 if (technology->enable_persistent && softblock)
1314 return __connman_rfkill_block(type, FALSE);
1315 if (!technology->enable_persistent && !softblock)
1316 return __connman_rfkill_block(type, TRUE);
1322 int __connman_technology_remove_rfkill(unsigned int index,
1323 enum connman_service_type type)
1325 struct connman_technology *technology;
1326 struct connman_rfkill *rfkill;
1328 DBG("index %u", index);
1330 rfkill = g_hash_table_lookup(rfkill_list, &index);
1334 g_hash_table_remove(rfkill_list, &index);
1336 technology = technology_find(type);
1337 if (technology == NULL)
1340 technology_put(technology);
1345 int __connman_technology_init(void)
1349 connection = connman_dbus_get_connection();
1351 rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1354 global_offlinemode = connman_technology_load_offlinemode();
1359 void __connman_technology_cleanup(void)
1363 g_hash_table_destroy(rfkill_list);
1365 dbus_connection_unref(connection);