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 technology->enable_persistent = FALSE;
311 identifier = g_strdup_printf("%s", get_name(technology->type));
312 if (identifier == NULL)
315 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
317 technology->enable_persistent = enable;
319 technology->enable_persistent = FALSE;
320 g_clear_error(&error);
325 g_key_file_free(keyfile);
330 static void save_state(struct connman_technology *technology)
335 DBG("technology %p", technology);
337 keyfile = __connman_storage_load_global();
339 keyfile = g_key_file_new();
341 identifier = g_strdup_printf("%s", get_name(technology->type));
342 if (identifier == NULL)
345 g_key_file_set_boolean(keyfile, identifier, "Enable",
346 technology->enable_persistent);
351 __connman_storage_save_global(keyfile);
353 g_key_file_free(keyfile);
358 connman_bool_t __connman_technology_get_offlinemode(void)
360 return global_offlinemode;
363 static void connman_technology_save_offlinemode()
367 keyfile = __connman_storage_load_global();
369 keyfile = g_key_file_new();
371 g_key_file_set_boolean(keyfile, "global",
372 "OfflineMode", global_offlinemode);
374 __connman_storage_save_global(keyfile);
376 g_key_file_free(keyfile);
381 static connman_bool_t connman_technology_load_offlinemode()
384 GError *error = NULL;
385 connman_bool_t offlinemode;
387 /* If there is a error, we enable offlinemode */
388 keyfile = __connman_storage_load_global();
392 offlinemode = g_key_file_get_boolean(keyfile, "global",
393 "OfflineMode", &error);
396 g_clear_error(&error);
399 g_key_file_free(keyfile);
404 static void append_properties(DBusMessageIter *iter,
405 struct connman_technology *technology)
407 DBusMessageIter dict;
409 connman_bool_t powered;
411 connman_dbus_dict_open(iter, &dict);
413 str = get_name(technology->type);
415 connman_dbus_dict_append_basic(&dict, "Name",
416 DBUS_TYPE_STRING, &str);
418 str = __connman_service_type2string(technology->type);
420 connman_dbus_dict_append_basic(&dict, "Type",
421 DBUS_TYPE_STRING, &str);
423 __sync_synchronize();
424 if (technology->enabled > 0)
428 connman_dbus_dict_append_basic(&dict, "Powered",
429 DBUS_TYPE_BOOLEAN, &powered);
431 connman_dbus_dict_append_basic(&dict, "Connected",
433 &technology->connected);
435 connman_dbus_dict_append_basic(&dict, "Tethering",
437 &technology->tethering);
439 if (technology->tethering_ident != NULL)
440 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
442 &technology->tethering_ident);
444 if (technology->tethering_passphrase != NULL)
445 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
447 &technology->tethering_passphrase);
449 connman_dbus_dict_close(iter, &dict);
452 static void technology_added_signal(struct connman_technology *technology)
455 DBusMessageIter iter;
457 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
458 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
462 dbus_message_iter_init_append(signal, &iter);
463 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
465 append_properties(&iter, technology);
467 dbus_connection_send(connection, signal, NULL);
468 dbus_message_unref(signal);
471 static void technology_removed_signal(struct connman_technology *technology)
473 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
474 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
475 DBUS_TYPE_OBJECT_PATH, &technology->path,
479 static DBusMessage *get_properties(DBusConnection *conn,
480 DBusMessage *message, void *user_data)
482 struct connman_technology *technology = user_data;
484 DBusMessageIter iter;
486 reply = dbus_message_new_method_return(message);
490 dbus_message_iter_init_append(reply, &iter);
491 append_properties(&iter, technology);
496 void __connman_technology_list_struct(DBusMessageIter *array)
499 DBusMessageIter entry;
501 for (list = technology_list; list; list = list->next) {
502 struct connman_technology *technology = list->data;
504 if (technology->path == NULL)
507 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
509 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
511 append_properties(&entry, technology);
512 dbus_message_iter_close_container(array, &entry);
516 static gboolean technology_pending_reply(gpointer user_data)
518 struct connman_technology *technology = user_data;
521 /* Power request timedout, send ETIMEDOUT. */
522 if (technology->pending_reply != NULL) {
523 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
525 g_dbus_send_message(connection, reply);
527 dbus_message_unref(technology->pending_reply);
528 technology->pending_reply = NULL;
529 technology->pending_timeout = 0;
535 static int technology_enable(struct connman_technology *technology,
543 DBG("technology %p enable", technology);
545 __sync_synchronize();
546 if (technology->enabled > 0) {
551 if (technology->pending_reply != NULL) {
558 * This is a bit of a trick. When msg is not NULL it means
559 * thats technology_enable was invoked from the manager API.
560 * Hence we save the state here.
562 technology->enable_persistent = TRUE;
563 save_state(technology);
566 __connman_rfkill_block(technology->type, FALSE);
569 * An empty device list means that devices in the technology
570 * were rfkill blocked. The unblock above will enable the devs.
572 if (technology->device_list == NULL) {
577 for (list = technology->device_list; list; list = list->next) {
578 struct connman_device *device = list->data;
580 err = __connman_device_enable(device);
582 * err = 0 : Device was enabled right away.
583 * If atleast one device gets enabled, we consider
584 * the technology to be enabled.
593 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
598 if (err == -EINPROGRESS) {
599 technology->pending_reply = dbus_message_ref(msg);
600 technology->pending_timeout = g_timeout_add_seconds(10,
601 technology_pending_reply, technology);
603 reply = __connman_error_failed(msg, -err);
605 g_dbus_send_message(connection, reply);
612 static int technology_disable(struct connman_technology *technology,
620 DBG("technology %p disable", technology);
622 __sync_synchronize();
623 if (technology->enabled == 0) {
628 if (technology->pending_reply != NULL) {
633 if (technology->tethering == TRUE)
634 set_tethering(technology, FALSE);
637 technology->enable_persistent = FALSE;
638 save_state(technology);
641 __connman_rfkill_block(technology->type, TRUE);
643 for (list = technology->device_list; list; list = list->next) {
644 struct connman_device *device = list->data;
646 err = __connman_device_disable(device);
654 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
659 if (err == -EINPROGRESS) {
660 technology->pending_reply = dbus_message_ref(msg);
661 technology->pending_timeout = g_timeout_add_seconds(10,
662 technology_pending_reply, technology);
664 reply = __connman_error_failed(msg, -err);
666 g_dbus_send_message(connection, reply);
673 static DBusMessage *set_property(DBusConnection *conn,
674 DBusMessage *msg, void *data)
676 struct connman_technology *technology = data;
677 DBusMessageIter iter, value;
681 DBG("conn %p", conn);
683 if (dbus_message_iter_init(msg, &iter) == FALSE)
684 return __connman_error_invalid_arguments(msg);
686 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
687 return __connman_error_invalid_arguments(msg);
689 dbus_message_iter_get_basic(&iter, &name);
690 dbus_message_iter_next(&iter);
692 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
693 return __connman_error_invalid_arguments(msg);
695 dbus_message_iter_recurse(&iter, &value);
697 type = dbus_message_iter_get_arg_type(&value);
699 DBG("property %s", name);
701 if (g_str_equal(name, "Tethering") == TRUE) {
703 connman_bool_t tethering;
705 if (type != DBUS_TYPE_BOOLEAN)
706 return __connman_error_invalid_arguments(msg);
708 dbus_message_iter_get_basic(&value, &tethering);
710 if (technology->tethering == tethering)
711 return __connman_error_in_progress(msg);
713 err = set_tethering(technology, tethering);
715 return __connman_error_failed(msg, -err);
717 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
720 dbus_message_iter_get_basic(&value, &str);
722 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
723 return __connman_error_not_supported(msg);
725 technology->tethering_ident = g_strdup(str);
726 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
729 dbus_message_iter_get_basic(&value, &str);
731 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
732 return __connman_error_not_supported(msg);
735 return __connman_error_invalid_arguments(msg);
737 technology->tethering_passphrase = g_strdup(str);
738 } else if (g_str_equal(name, "Powered") == TRUE) {
739 connman_bool_t enable;
741 if (type != DBUS_TYPE_BOOLEAN)
742 return __connman_error_invalid_arguments(msg);
744 dbus_message_iter_get_basic(&value, &enable);
746 technology_enable(technology, msg);
748 technology_disable(technology, msg);
751 return __connman_error_invalid_property(msg);
753 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
756 static struct connman_technology *technology_find(enum connman_service_type type)
760 DBG("type %d", type);
762 for (list = technology_list; list; list = list->next) {
763 struct connman_technology *technology = list->data;
765 if (technology->type == type)
772 static void reply_scan_pending(struct connman_technology *technology, int err)
776 DBG("technology %p err %d", technology, err);
778 while (technology->scan_pending != NULL) {
779 DBusMessage *msg = technology->scan_pending->data;
781 DBG("reply to %s", dbus_message_get_sender(msg));
784 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
786 reply = __connman_error_failed(msg, -err);
787 g_dbus_send_message(connection, reply);
788 dbus_message_unref(msg);
790 technology->scan_pending =
791 g_slist_delete_link(technology->scan_pending,
792 technology->scan_pending);
796 void __connman_technology_scan_started(struct connman_device *device)
798 DBG("device %p", device);
801 void __connman_technology_scan_stopped(struct connman_device *device)
804 struct connman_technology *technology;
805 enum connman_service_type type;
808 type = __connman_device_get_service_type(device);
809 technology = technology_find(type);
811 DBG("technology %p device %p", technology, device);
813 if (technology == NULL)
816 for (list = technology->device_list; list != NULL; list = list->next) {
817 struct connman_device *other_device = list->data;
819 if (device == other_device)
822 if (__connman_device_get_service_type(other_device) != type)
825 if (__connman_device_scanning(other_device))
830 reply_scan_pending(technology, 0);
833 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
835 struct connman_technology *technology = data;
838 DBG ("technology %p request from %s", technology,
839 dbus_message_get_sender(msg));
841 dbus_message_ref(msg);
842 technology->scan_pending =
843 g_slist_prepend(technology->scan_pending, msg);
845 err = __connman_device_request_scan(technology->type);
847 reply_scan_pending(technology, err);
852 static GDBusMethodTable technology_methods[] = {
853 { "GetProperties", "", "a{sv}", get_properties },
854 { "SetProperty", "sv", "", set_property },
855 { "Scan", "", "", scan,
856 G_DBUS_METHOD_FLAG_ASYNC },
860 static GDBusSignalTable technology_signals[] = {
861 { "PropertyChanged", "sv" },
865 static struct connman_technology *technology_get(enum connman_service_type type)
867 struct connman_technology *technology;
868 struct connman_technology_driver *driver = NULL;
873 DBG("type %d", type);
875 str = __connman_service_type2string(type);
879 technology = technology_find(type);
880 if (technology != NULL) {
881 __sync_fetch_and_add(&technology->refcount, 1);
885 /* First check if we have a driver for this technology type */
886 for (list = driver_list; list; list = list->next) {
889 if (driver->type == type)
895 if (driver == NULL) {
896 DBG("No matching driver found for %s.",
897 __connman_service_type2string(type));
901 technology = g_try_new0(struct connman_technology, 1);
902 if (technology == NULL)
905 technology->refcount = 1;
907 technology->type = type;
908 technology->path = g_strdup_printf("%s/technology/%s",
911 technology->device_list = NULL;
913 technology->pending_reply = NULL;
915 load_state(technology);
917 if (g_dbus_register_interface(connection, technology->path,
918 CONNMAN_TECHNOLOGY_INTERFACE,
919 technology_methods, technology_signals,
920 NULL, technology, NULL) == FALSE) {
921 connman_error("Failed to register %s", technology->path);
926 technology_list = g_slist_append(technology_list, technology);
928 technology_added_signal(technology);
930 technology->driver = driver;
931 err = driver->probe(technology);
933 DBG("Driver probe failed for technology %p", technology);
935 DBG("technology %p", technology);
940 static void technology_put(struct connman_technology *technology)
942 DBG("technology %p", technology);
944 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
947 reply_scan_pending(technology, -EINTR);
949 if (technology->driver) {
950 technology->driver->remove(technology);
951 technology->driver = NULL;
954 technology_list = g_slist_remove(technology_list, technology);
956 technology_removed_signal(technology);
958 g_dbus_unregister_interface(connection, technology->path,
959 CONNMAN_TECHNOLOGY_INTERFACE);
961 g_slist_free(technology->device_list);
963 g_free(technology->path);
964 g_free(technology->regdom);
968 void __connman_technology_add_interface(enum connman_service_type type,
969 int index, const char *name, const char *ident)
971 struct connman_technology *technology;
974 case CONNMAN_SERVICE_TYPE_UNKNOWN:
975 case CONNMAN_SERVICE_TYPE_SYSTEM:
977 case CONNMAN_SERVICE_TYPE_ETHERNET:
978 case CONNMAN_SERVICE_TYPE_WIFI:
979 case CONNMAN_SERVICE_TYPE_WIMAX:
980 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
981 case CONNMAN_SERVICE_TYPE_CELLULAR:
982 case CONNMAN_SERVICE_TYPE_GPS:
983 case CONNMAN_SERVICE_TYPE_VPN:
984 case CONNMAN_SERVICE_TYPE_GADGET:
988 connman_info("Adding interface %s [ %s ]", name,
989 __connman_service_type2string(type));
991 technology = technology_find(type);
993 if (technology == NULL || technology->driver == NULL
994 || technology->driver->add_interface == NULL)
997 technology->driver->add_interface(technology,
1001 void __connman_technology_remove_interface(enum connman_service_type type,
1002 int index, const char *name, const char *ident)
1004 struct connman_technology *technology;
1007 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1008 case CONNMAN_SERVICE_TYPE_SYSTEM:
1010 case CONNMAN_SERVICE_TYPE_ETHERNET:
1011 case CONNMAN_SERVICE_TYPE_WIFI:
1012 case CONNMAN_SERVICE_TYPE_WIMAX:
1013 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1014 case CONNMAN_SERVICE_TYPE_CELLULAR:
1015 case CONNMAN_SERVICE_TYPE_GPS:
1016 case CONNMAN_SERVICE_TYPE_VPN:
1017 case CONNMAN_SERVICE_TYPE_GADGET:
1021 connman_info("Remove interface %s [ %s ]", name,
1022 __connman_service_type2string(type));
1024 technology = technology_find(type);
1026 if (technology == NULL || technology->driver == NULL)
1029 if (technology->driver->remove_interface)
1030 technology->driver->remove_interface(technology, index);
1033 int __connman_technology_add_device(struct connman_device *device)
1035 struct connman_technology *technology;
1036 enum connman_service_type type;
1038 DBG("device %p", device);
1040 type = __connman_device_get_service_type(device);
1042 technology = technology_get(type);
1043 if (technology == NULL) {
1045 * Since no driver can be found for this device at the moment we
1046 * add it to the techless device list.
1048 techless_device_list = g_slist_prepend(techless_device_list,
1054 if (technology->enable_persistent && !global_offlinemode) {
1055 int err = __connman_device_enable(device);
1057 * connman_technology_add_device() calls __connman_device_enable()
1058 * but since the device is already enabled, the calls does not
1059 * propagate through to connman_technology_enabled via
1060 * connman_device_set_powered.
1062 if (err == -EALREADY)
1063 __connman_technology_enabled(type);
1065 /* if technology persistent state is offline */
1066 if (!technology->enable_persistent)
1067 __connman_device_disable(device);
1069 technology->device_list = g_slist_append(technology->device_list,
1075 int __connman_technology_remove_device(struct connman_device *device)
1077 struct connman_technology *technology;
1078 enum connman_service_type type;
1080 DBG("device %p", device);
1082 type = __connman_device_get_service_type(device);
1084 technology = technology_find(type);
1085 if (technology == NULL) {
1086 techless_device_list = g_slist_remove(techless_device_list,
1091 if (__connman_device_scanning(device))
1092 __connman_technology_scan_stopped(device);
1094 technology->device_list = g_slist_remove(technology->device_list,
1096 technology_put(technology);
1101 static void powered_changed(struct connman_technology *technology)
1103 connman_bool_t powered;
1105 __sync_synchronize();
1106 if (technology->enabled >0)
1111 connman_dbus_property_changed_basic(technology->path,
1112 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1113 DBUS_TYPE_BOOLEAN, &powered);
1116 int __connman_technology_enabled(enum connman_service_type type)
1118 struct connman_technology *technology;
1120 technology = technology_find(type);
1121 if (technology == NULL)
1124 if (__sync_fetch_and_add(&technology->enabled, 1) == 0)
1125 powered_changed(technology);
1127 if (technology->pending_reply != NULL) {
1128 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1129 dbus_message_unref(technology->pending_reply);
1130 g_source_remove(technology->pending_timeout);
1131 technology->pending_reply = NULL;
1132 technology->pending_timeout = 0;
1138 int __connman_technology_disabled(enum connman_service_type type)
1140 struct connman_technology *technology;
1142 technology = technology_find(type);
1143 if (technology == NULL)
1146 if (technology->pending_reply != NULL) {
1147 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1148 dbus_message_unref(technology->pending_reply);
1149 g_source_remove(technology->pending_timeout);
1150 technology->pending_reply = NULL;
1151 technology->pending_timeout = 0;
1154 if (__sync_fetch_and_sub(&technology->enabled, 1) == 1)
1155 powered_changed(technology);
1160 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1165 if (global_offlinemode == offlinemode)
1168 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1171 * This is a bit tricky. When you set offlinemode, there is no
1172 * way to differentiate between attempting offline mode and
1173 * resuming offlinemode from last saved profile. We need that
1174 * information in rfkill_update, otherwise it falls back on the
1175 * technology's persistent state. Hence we set the offline mode here
1176 * but save it & call the notifier only if its successful.
1179 global_offlinemode = offlinemode;
1181 /* Traverse technology list, enable/disable each technology. */
1182 for (list = technology_list; list; list = list->next) {
1183 struct connman_technology *technology = list->data;
1186 err = technology_disable(technology, NULL);
1188 if (!offlinemode && technology->enable_persistent)
1189 err = technology_enable(technology, NULL);
1192 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1193 connman_technology_save_offlinemode();
1194 __connman_notifier_offlinemode(offlinemode);
1196 global_offlinemode = connman_technology_load_offlinemode();
1201 void __connman_technology_set_connected(enum connman_service_type type,
1202 connman_bool_t connected)
1204 struct connman_technology *technology;
1206 technology = technology_find(type);
1207 if (technology == NULL)
1210 DBG("technology %p connected %d", technology, connected);
1212 technology->connected = connected;
1214 connman_dbus_property_changed_basic(technology->path,
1215 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1216 DBUS_TYPE_BOOLEAN, &connected);
1219 int __connman_technology_add_rfkill(unsigned int index,
1220 enum connman_service_type type,
1221 connman_bool_t softblock,
1222 connman_bool_t hardblock)
1224 struct connman_technology *technology;
1225 struct connman_rfkill *rfkill;
1227 DBG("index %u type %d soft %u hard %u", index, type,
1228 softblock, hardblock);
1230 rfkill = g_hash_table_lookup(rfkill_list, &index);
1234 rfkill = g_try_new0(struct connman_rfkill, 1);
1238 rfkill->index = index;
1239 rfkill->type = type;
1240 rfkill->softblock = softblock;
1241 rfkill->hardblock = hardblock;
1243 g_hash_table_insert(rfkill_list, &rfkill->index, rfkill);
1246 technology = technology_get(type);
1247 /* If there is no driver for this type, ignore it. */
1248 if (technology == NULL)
1252 DBG("%s is switched off.", get_name(type));
1257 * If Offline mode is on, we softblock the device if it isnt already.
1258 * If Offline mode is off, we rely on the persistent state of tech.
1260 if (global_offlinemode) {
1262 return __connman_rfkill_block(type, TRUE);
1264 if (technology->enable_persistent && softblock)
1265 return __connman_rfkill_block(type, FALSE);
1266 /* if technology persistent state is offline */
1267 if (!technology->enable_persistent && !softblock)
1268 return __connman_rfkill_block(type, TRUE);
1274 int __connman_technology_update_rfkill(unsigned int index,
1275 enum connman_service_type type,
1276 connman_bool_t softblock,
1277 connman_bool_t hardblock)
1279 struct connman_technology *technology;
1280 struct connman_rfkill *rfkill;
1282 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1284 rfkill = g_hash_table_lookup(rfkill_list, &index);
1288 if (rfkill->softblock == softblock &&
1289 rfkill->hardblock == hardblock)
1292 rfkill->softblock = softblock;
1293 rfkill->hardblock = hardblock;
1296 DBG("%s is switched off.", get_name(type));
1300 technology = technology_find(type);
1301 /* If there is no driver for this type, ignore it. */
1302 if (technology == NULL)
1305 if (!global_offlinemode) {
1306 if (technology->enable_persistent && softblock)
1307 return __connman_rfkill_block(type, FALSE);
1308 if (!technology->enable_persistent && !softblock)
1309 return __connman_rfkill_block(type, TRUE);
1315 int __connman_technology_remove_rfkill(unsigned int index,
1316 enum connman_service_type type)
1318 struct connman_technology *technology;
1319 struct connman_rfkill *rfkill;
1321 DBG("index %u", index);
1323 rfkill = g_hash_table_lookup(rfkill_list, &index);
1327 g_hash_table_remove(rfkill_list, &index);
1329 technology = technology_find(type);
1330 if (technology == NULL)
1333 technology_put(technology);
1338 int __connman_technology_init(void)
1342 connection = connman_dbus_get_connection();
1344 rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
1347 global_offlinemode = connman_technology_load_offlinemode();
1352 void __connman_technology_cleanup(void)
1356 g_hash_table_destroy(rfkill_list);
1358 dbus_connection_unref(connection);