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;
58 connman_bool_t enabled;
60 connman_bool_t connected;
62 connman_bool_t tethering;
63 connman_bool_t tethering_persistent; /* Tells the save status, needed
64 * as offline mode might set
67 char *tethering_ident;
68 char *tethering_passphrase;
70 connman_bool_t enable_persistent; /* Save the tech state */
74 DBusMessage *pending_reply;
75 guint pending_timeout;
79 connman_bool_t rfkill_driven;
80 connman_bool_t softblocked;
81 connman_bool_t hardblocked;
82 connman_bool_t dbus_registered;
85 static GSList *driver_list = NULL;
87 static gint compare_priority(gconstpointer a, gconstpointer b)
89 const struct connman_technology_driver *driver1 = a;
90 const struct connman_technology_driver *driver2 = b;
92 return driver2->priority - driver1->priority;
95 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
97 struct connman_rfkill *rfkill = value;
98 enum connman_service_type type = GPOINTER_TO_INT(user_data);
100 /* Calling _technology_rfkill_add will update the tech. */
101 if (rfkill->type == type)
102 __connman_technology_add_rfkill(rfkill->index, type,
103 rfkill->softblock, rfkill->hardblock);
107 connman_technology_is_tethering_allowed(enum connman_service_type type)
109 static char *allowed_default[] = { "wifi", "bluetooth", "gadget",
111 const char *type_str = __connman_service_type2string(type);
115 if (type_str == NULL)
118 allowed = connman_setting_get_string_list("TetheringTechnologies");
120 allowed = allowed_default;
122 for (i = 0; allowed[i] != NULL; i++) {
123 if (g_strcmp0(allowed[i], type_str) == 0)
131 * connman_technology_driver_register:
132 * @driver: Technology driver definition
134 * Register a new technology driver
136 * Returns: %0 on success
138 int connman_technology_driver_register(struct connman_technology_driver *driver)
141 struct connman_device *device;
142 enum connman_service_type type;
144 DBG("Registering %s driver", driver->name);
146 driver_list = g_slist_insert_sorted(driver_list, driver,
150 * Check for technology less devices if this driver
151 * can service any of them.
153 for (list = techless_device_list; list != NULL; list = list->next) {
156 type = __connman_device_get_service_type(device);
157 if (type != driver->type)
160 techless_device_list = g_slist_remove(techless_device_list,
163 __connman_technology_add_device(device);
166 /* Check for orphaned rfkill switches. */
167 g_hash_table_foreach(rfkill_list, rfkill_check,
168 GINT_TO_POINTER(driver->type));
174 * connman_technology_driver_unregister:
175 * @driver: Technology driver definition
177 * Remove a previously registered technology driver
179 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
181 GSList *list, *tech_drivers;
182 struct connman_technology *technology;
183 struct connman_technology_driver *current;
185 DBG("Unregistering driver %p name %s", driver, driver->name);
187 for (list = technology_list; list; list = list->next) {
188 technology = list->data;
190 for (tech_drivers = technology->driver_list;
191 tech_drivers != NULL;
192 tech_drivers = g_slist_next(tech_drivers)) {
194 current = tech_drivers->data;
195 if (driver != current)
198 if (driver->remove != NULL)
199 driver->remove(technology);
201 technology->driver_list =
202 g_slist_remove(technology->driver_list, driver);
208 driver_list = g_slist_remove(driver_list, driver);
211 static const char *get_name(enum connman_service_type type)
214 case CONNMAN_SERVICE_TYPE_UNKNOWN:
215 case CONNMAN_SERVICE_TYPE_SYSTEM:
216 case CONNMAN_SERVICE_TYPE_GPS:
217 case CONNMAN_SERVICE_TYPE_VPN:
218 case CONNMAN_SERVICE_TYPE_GADGET:
220 case CONNMAN_SERVICE_TYPE_ETHERNET:
222 case CONNMAN_SERVICE_TYPE_WIFI:
224 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
226 case CONNMAN_SERVICE_TYPE_CELLULAR:
233 static void technology_save(struct connman_technology *technology)
238 DBG("technology %p", technology);
240 keyfile = __connman_storage_load_global();
242 keyfile = g_key_file_new();
244 identifier = g_strdup_printf("%s", get_name(technology->type));
245 if (identifier == NULL)
248 g_key_file_set_boolean(keyfile, identifier, "Enable",
249 technology->enable_persistent);
251 g_key_file_set_boolean(keyfile, identifier, "Tethering",
252 technology->tethering_persistent);
254 if (technology->tethering_ident != NULL)
255 g_key_file_set_string(keyfile, identifier,
256 "Tethering.Identifier",
257 technology->tethering_ident);
259 if (technology->tethering_passphrase != NULL)
260 g_key_file_set_string(keyfile, identifier,
261 "Tethering.Passphrase",
262 technology->tethering_passphrase);
267 __connman_storage_save_global(keyfile);
269 g_key_file_free(keyfile);
274 static void tethering_changed(struct connman_technology *technology)
276 connman_bool_t tethering = technology->tethering;
278 connman_dbus_property_changed_basic(technology->path,
279 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
280 DBUS_TYPE_BOOLEAN, &tethering);
282 technology_save(technology);
285 void connman_technology_tethering_notify(struct connman_technology *technology,
286 connman_bool_t enabled)
290 DBG("technology %p enabled %u", technology, enabled);
292 if (technology->tethering == enabled)
295 technology->tethering = enabled;
297 tethering_changed(technology);
300 __connman_tethering_set_enabled();
302 for (list = technology_list; list; list = list->next) {
303 struct connman_technology *other_tech = list->data;
304 if (other_tech->tethering == TRUE)
308 __connman_tethering_set_disabled();
312 static int set_tethering(struct connman_technology *technology,
313 connman_bool_t enabled)
315 int result = -EOPNOTSUPP;
317 const char *ident, *passphrase, *bridge;
318 GSList *tech_drivers;
320 ident = technology->tethering_ident;
321 passphrase = technology->tethering_passphrase;
323 __sync_synchronize();
324 if (technology->enabled == FALSE)
327 bridge = __connman_tethering_get_bridge();
331 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
332 (ident == NULL || passphrase == NULL))
335 for (tech_drivers = technology->driver_list; tech_drivers != NULL;
336 tech_drivers = g_slist_next(tech_drivers)) {
337 struct connman_technology_driver *driver = tech_drivers->data;
339 if (driver == NULL || driver->set_tethering == NULL)
342 err = driver->set_tethering(technology, ident, passphrase,
345 if (result == -EINPROGRESS)
348 if (err == -EINPROGRESS || err == 0) {
357 void connman_technology_regdom_notify(struct connman_technology *technology,
363 connman_error("Failed to set regulatory domain");
365 DBG("Regulatory domain set to %s", alpha2);
367 g_free(technology->regdom);
368 technology->regdom = g_strdup(alpha2);
371 static int set_regdom_by_device(struct connman_technology *technology,
376 for (list = technology->device_list; list; list = list->next) {
377 struct connman_device *device = list->data;
379 if (connman_device_set_regdom(device, alpha2) != 0)
386 int connman_technology_set_regdom(const char *alpha2)
388 GSList *list, *tech_drivers;
390 for (list = technology_list; list; list = list->next) {
391 struct connman_technology *technology = list->data;
393 if (set_regdom_by_device(technology, alpha2) != 0) {
395 for (tech_drivers = technology->driver_list;
396 tech_drivers != NULL;
397 tech_drivers = g_slist_next(tech_drivers)) {
399 struct connman_technology_driver *driver =
402 if (driver->set_regdom != NULL)
403 driver->set_regdom(technology, alpha2);
411 static struct connman_technology *technology_find(enum connman_service_type type)
415 DBG("type %d", type);
417 for (list = technology_list; list; list = list->next) {
418 struct connman_technology *technology = list->data;
420 if (technology->type == type)
427 connman_bool_t connman_technology_get_wifi_tethering(const char **ssid,
430 struct connman_technology *technology;
432 if (ssid == NULL || psk == NULL)
437 technology = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
438 if (technology == NULL)
441 if (technology->tethering == FALSE)
444 *ssid = technology->tethering_ident;
445 *psk = technology->tethering_passphrase;
450 static void free_rfkill(gpointer data)
452 struct connman_rfkill *rfkill = data;
457 static void technology_load(struct connman_technology *technology)
461 GError *error = NULL;
462 connman_bool_t enable, need_saving = FALSE;
464 DBG("technology %p", technology);
466 keyfile = __connman_storage_load_global();
467 /* Fallback on disabling technology if file not found. */
468 if (keyfile == NULL) {
469 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
470 /* We enable ethernet by default */
471 technology->enable_persistent = TRUE;
473 technology->enable_persistent = FALSE;
477 identifier = g_strdup_printf("%s", get_name(technology->type));
478 if (identifier == NULL)
481 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
483 technology->enable_persistent = enable;
485 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
486 technology->enable_persistent = TRUE;
488 technology->enable_persistent = FALSE;
491 g_clear_error(&error);
494 enable = g_key_file_get_boolean(keyfile, identifier,
495 "Tethering", &error);
497 technology->tethering_persistent = enable;
500 g_clear_error(&error);
503 if (need_saving == TRUE)
504 technology_save(technology);
506 technology->tethering_ident = g_key_file_get_string(keyfile,
507 identifier, "Tethering.Identifier", NULL);
509 technology->tethering_passphrase = g_key_file_get_string(keyfile,
510 identifier, "Tethering.Passphrase", NULL);
514 g_key_file_free(keyfile);
519 connman_bool_t __connman_technology_get_offlinemode(void)
521 return global_offlinemode;
524 static void connman_technology_save_offlinemode()
528 keyfile = __connman_storage_load_global();
530 keyfile = g_key_file_new();
532 g_key_file_set_boolean(keyfile, "global",
533 "OfflineMode", global_offlinemode);
535 __connman_storage_save_global(keyfile);
537 g_key_file_free(keyfile);
542 static connman_bool_t connman_technology_load_offlinemode()
545 GError *error = NULL;
546 connman_bool_t offlinemode;
548 /* If there is a error, we enable offlinemode */
549 keyfile = __connman_storage_load_global();
553 offlinemode = g_key_file_get_boolean(keyfile, "global",
554 "OfflineMode", &error);
557 g_clear_error(&error);
560 g_key_file_free(keyfile);
565 static void append_properties(DBusMessageIter *iter,
566 struct connman_technology *technology)
568 DBusMessageIter dict;
571 connman_dbus_dict_open(iter, &dict);
573 str = get_name(technology->type);
575 connman_dbus_dict_append_basic(&dict, "Name",
576 DBUS_TYPE_STRING, &str);
578 str = __connman_service_type2string(technology->type);
580 connman_dbus_dict_append_basic(&dict, "Type",
581 DBUS_TYPE_STRING, &str);
583 __sync_synchronize();
584 connman_dbus_dict_append_basic(&dict, "Powered",
586 &technology->enabled);
588 connman_dbus_dict_append_basic(&dict, "Connected",
590 &technology->connected);
592 connman_dbus_dict_append_basic(&dict, "Tethering",
594 &technology->tethering);
596 if (technology->tethering_ident != NULL)
597 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
599 &technology->tethering_ident);
601 if (technology->tethering_passphrase != NULL)
602 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
604 &technology->tethering_passphrase);
606 connman_dbus_dict_close(iter, &dict);
609 static void technology_added_signal(struct connman_technology *technology)
612 DBusMessageIter iter;
614 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
615 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
619 dbus_message_iter_init_append(signal, &iter);
620 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
622 append_properties(&iter, technology);
624 dbus_connection_send(connection, signal, NULL);
625 dbus_message_unref(signal);
628 static void technology_removed_signal(struct connman_technology *technology)
630 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
631 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
632 DBUS_TYPE_OBJECT_PATH, &technology->path,
636 static DBusMessage *get_properties(DBusConnection *conn,
637 DBusMessage *message, void *user_data)
639 struct connman_technology *technology = user_data;
641 DBusMessageIter iter;
643 reply = dbus_message_new_method_return(message);
647 dbus_message_iter_init_append(reply, &iter);
648 append_properties(&iter, technology);
653 void __connman_technology_list_struct(DBusMessageIter *array)
656 DBusMessageIter entry;
658 for (list = technology_list; list; list = list->next) {
659 struct connman_technology *technology = list->data;
661 if (technology->path == NULL ||
662 (technology->rfkill_driven == TRUE &&
663 technology->hardblocked == TRUE))
666 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
668 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
670 append_properties(&entry, technology);
671 dbus_message_iter_close_container(array, &entry);
675 static gboolean technology_pending_reply(gpointer user_data)
677 struct connman_technology *technology = user_data;
680 /* Power request timedout, send ETIMEDOUT. */
681 if (technology->pending_reply != NULL) {
682 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
684 g_dbus_send_message(connection, reply);
686 dbus_message_unref(technology->pending_reply);
687 technology->pending_reply = NULL;
688 technology->pending_timeout = 0;
694 static int technology_affect_devices(struct connman_technology *technology,
695 connman_bool_t enable_device)
700 for (list = technology->device_list; list; list = list->next) {
701 struct connman_device *device = list->data;
703 if (enable_device == TRUE)
704 err = __connman_device_enable(device);
706 err = __connman_device_disable(device);
712 static int technology_enable(struct connman_technology *technology)
717 DBG("technology %p enable", technology);
719 __sync_synchronize();
720 if (technology->enabled == TRUE)
723 if (technology->pending_reply != NULL)
726 if (connman_setting_get_bool("PersistentTetheringMode") == TRUE &&
727 technology->tethering == TRUE)
728 set_tethering(technology, TRUE);
730 if (technology->rfkill_driven == TRUE)
731 err = __connman_rfkill_block(technology->type, FALSE);
733 err_dev = technology_affect_devices(technology, TRUE);
735 if (technology->rfkill_driven == FALSE)
741 static int technology_disable(struct connman_technology *technology)
745 DBG("technology %p disable", technology);
747 __sync_synchronize();
748 if (technology->enabled == FALSE)
751 if (technology->pending_reply != NULL)
754 if (technology->tethering == TRUE)
755 set_tethering(technology, FALSE);
757 err = technology_affect_devices(technology, FALSE);
759 if (technology->rfkill_driven == TRUE)
760 err = __connman_rfkill_block(technology->type, TRUE);
765 static DBusMessage *set_powered(struct connman_technology *technology,
766 DBusMessage *msg, connman_bool_t powered)
768 DBusMessage *reply = NULL;
771 if (technology->rfkill_driven && technology->hardblocked == TRUE) {
777 err = technology_enable(technology);
779 err = technology_disable(technology);
782 technology->enable_persistent = powered;
783 technology_save(technology);
787 if (err == -EINPROGRESS) {
788 technology->pending_reply = dbus_message_ref(msg);
789 technology->pending_timeout = g_timeout_add_seconds(10,
790 technology_pending_reply, technology);
791 } else if (err == -EALREADY) {
793 reply = __connman_error_already_enabled(msg);
795 reply = __connman_error_already_disabled(msg);
797 reply = __connman_error_failed(msg, -err);
799 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
804 static DBusMessage *set_property(DBusConnection *conn,
805 DBusMessage *msg, void *data)
807 struct connman_technology *technology = data;
808 DBusMessageIter iter, value;
812 DBG("conn %p", conn);
814 if (dbus_message_iter_init(msg, &iter) == FALSE)
815 return __connman_error_invalid_arguments(msg);
817 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
818 return __connman_error_invalid_arguments(msg);
820 dbus_message_iter_get_basic(&iter, &name);
821 dbus_message_iter_next(&iter);
823 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
824 return __connman_error_invalid_arguments(msg);
826 dbus_message_iter_recurse(&iter, &value);
828 type = dbus_message_iter_get_arg_type(&value);
830 DBG("property %s", name);
832 if (g_str_equal(name, "Tethering") == TRUE) {
834 connman_bool_t tethering;
836 if (type != DBUS_TYPE_BOOLEAN)
837 return __connman_error_invalid_arguments(msg);
839 if (connman_technology_is_tethering_allowed(technology->type)
841 DBG("%s tethering not allowed by config file",
842 __connman_service_type2string(technology->type));
843 return __connman_error_not_supported(msg);
846 dbus_message_iter_get_basic(&value, &tethering);
848 if (technology->tethering == tethering) {
849 if (tethering == FALSE)
850 return __connman_error_already_disabled(msg);
852 return __connman_error_already_enabled(msg);
855 err = set_tethering(technology, tethering);
857 return __connman_error_failed(msg, -err);
859 technology->tethering_persistent = tethering;
861 technology_save(technology);
863 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
866 dbus_message_iter_get_basic(&value, &str);
868 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
869 return __connman_error_not_supported(msg);
871 if (strlen(str) < 1 || strlen(str) > 32)
872 return __connman_error_invalid_arguments(msg);
874 if (g_strcmp0(technology->tethering_ident, str) != 0) {
875 g_free(technology->tethering_ident);
876 technology->tethering_ident = g_strdup(str);
877 technology_save(technology);
879 connman_dbus_property_changed_basic(technology->path,
880 CONNMAN_TECHNOLOGY_INTERFACE,
881 "TetheringIdentifier",
883 &technology->tethering_ident);
885 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
888 dbus_message_iter_get_basic(&value, &str);
890 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
891 return __connman_error_not_supported(msg);
893 if (strlen(str) < 8 || strlen(str) > 63)
894 return __connman_error_passphrase_required(msg);
896 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
897 g_free(technology->tethering_passphrase);
898 technology->tethering_passphrase = g_strdup(str);
899 technology_save(technology);
901 connman_dbus_property_changed_basic(technology->path,
902 CONNMAN_TECHNOLOGY_INTERFACE,
903 "TetheringPassphrase",
905 &technology->tethering_passphrase);
907 } else if (g_str_equal(name, "Powered") == TRUE) {
908 connman_bool_t enable;
910 if (type != DBUS_TYPE_BOOLEAN)
911 return __connman_error_invalid_arguments(msg);
913 dbus_message_iter_get_basic(&value, &enable);
915 return set_powered(technology, msg, enable);
917 return __connman_error_invalid_property(msg);
919 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
922 static void reply_scan_pending(struct connman_technology *technology, int err)
926 DBG("technology %p err %d", technology, err);
928 while (technology->scan_pending != NULL) {
929 DBusMessage *msg = technology->scan_pending->data;
931 DBG("reply to %s", dbus_message_get_sender(msg));
934 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
936 reply = __connman_error_failed(msg, -err);
937 g_dbus_send_message(connection, reply);
938 dbus_message_unref(msg);
940 technology->scan_pending =
941 g_slist_delete_link(technology->scan_pending,
942 technology->scan_pending);
946 void __connman_technology_scan_started(struct connman_device *device)
948 DBG("device %p", device);
951 void __connman_technology_scan_stopped(struct connman_device *device)
954 struct connman_technology *technology;
955 enum connman_service_type type;
958 type = __connman_device_get_service_type(device);
959 technology = technology_find(type);
961 DBG("technology %p device %p", technology, device);
963 if (technology == NULL)
966 for (list = technology->device_list; list != NULL; list = list->next) {
967 struct connman_device *other_device = list->data;
969 if (device == other_device)
972 if (__connman_device_get_service_type(other_device) != type)
975 if (connman_device_get_scanning(other_device) == TRUE)
980 reply_scan_pending(technology, 0);
983 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
984 int result, const char *alpha2)
986 connman_bool_t regdom_set = FALSE;
987 struct connman_technology *technology;
988 enum connman_service_type type;
989 GSList *tech_drivers;
991 type = __connman_device_get_service_type(device);
992 technology = technology_find(type);
994 if (technology == NULL)
999 for (tech_drivers = technology->driver_list;
1000 tech_drivers != NULL;
1001 tech_drivers = g_slist_next(tech_drivers)) {
1002 struct connman_technology_driver *driver =
1005 if (driver->set_regdom != NULL) {
1006 driver->set_regdom(technology, alpha2);
1012 if (regdom_set == FALSE)
1016 connman_technology_regdom_notify(technology, alpha2);
1019 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
1021 struct connman_technology *technology = data;
1024 DBG ("technology %p request from %s", technology,
1025 dbus_message_get_sender(msg));
1027 dbus_message_ref(msg);
1028 technology->scan_pending =
1029 g_slist_prepend(technology->scan_pending, msg);
1031 err = __connman_device_request_scan(technology->type);
1033 reply_scan_pending(technology, err);
1038 static const GDBusMethodTable technology_methods[] = {
1039 { GDBUS_DEPRECATED_METHOD("GetProperties",
1040 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
1042 { GDBUS_ASYNC_METHOD("SetProperty",
1043 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1044 NULL, set_property) },
1045 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
1049 static const GDBusSignalTable technology_signals[] = {
1050 { GDBUS_SIGNAL("PropertyChanged",
1051 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
1055 static gboolean technology_dbus_register(struct connman_technology *technology)
1057 if (technology->dbus_registered == TRUE ||
1058 (technology->rfkill_driven == TRUE &&
1059 technology->hardblocked == TRUE))
1062 if (g_dbus_register_interface(connection, technology->path,
1063 CONNMAN_TECHNOLOGY_INTERFACE,
1064 technology_methods, technology_signals,
1065 NULL, technology, NULL) == FALSE) {
1066 connman_error("Failed to register %s", technology->path);
1070 technology_added_signal(technology);
1071 technology->dbus_registered = TRUE;
1076 static struct connman_technology *technology_get(enum connman_service_type type)
1078 GSList *tech_drivers = NULL;
1079 struct connman_technology_driver *driver;
1080 struct connman_technology *technology;
1084 DBG("type %d", type);
1086 str = __connman_service_type2string(type);
1090 technology = technology_find(type);
1091 if (technology != NULL) {
1092 __sync_fetch_and_add(&technology->refcount, 1);
1096 /* First check if we have a driver for this technology type */
1097 for (list = driver_list; list; list = list->next) {
1098 driver = list->data;
1100 if (driver->type == type) {
1101 DBG("technology %p driver %p", technology, driver);
1102 tech_drivers = g_slist_append(tech_drivers, driver);
1106 if (tech_drivers == NULL) {
1107 DBG("No matching drivers found for %s.",
1108 __connman_service_type2string(type));
1112 technology = g_try_new0(struct connman_technology, 1);
1113 if (technology == NULL)
1116 technology->refcount = 1;
1118 technology->rfkill_driven = FALSE;
1119 technology->softblocked = FALSE;
1120 technology->hardblocked = FALSE;
1122 technology->type = type;
1123 technology->path = g_strdup_printf("%s/technology/%s",
1126 technology->device_list = NULL;
1128 technology->pending_reply = NULL;
1130 technology_load(technology);
1132 if (technology_dbus_register(technology) == FALSE) {
1137 technology_list = g_slist_prepend(technology_list, technology);
1139 technology->driver_list = tech_drivers;
1141 for (list = tech_drivers; list != NULL; list = g_slist_next(list)) {
1142 driver = list->data;
1144 if (driver->probe != NULL && driver->probe(technology) < 0)
1145 DBG("Driver probe failed for technology %p",
1149 DBG("technology %p", technology);
1154 static void technology_dbus_unregister(struct connman_technology *technology)
1156 if (technology->dbus_registered == FALSE)
1159 technology_removed_signal(technology);
1160 g_dbus_unregister_interface(connection, technology->path,
1161 CONNMAN_TECHNOLOGY_INTERFACE);
1163 technology->dbus_registered = FALSE;
1166 static void technology_put(struct connman_technology *technology)
1168 DBG("technology %p", technology);
1170 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1173 reply_scan_pending(technology, -EINTR);
1175 while (technology->driver_list != NULL) {
1176 struct connman_technology_driver *driver;
1178 driver = technology->driver_list->data;
1180 if (driver->remove != NULL)
1181 driver->remove(technology);
1183 technology->driver_list =
1184 g_slist_delete_link(technology->driver_list,
1185 technology->driver_list);
1188 technology_list = g_slist_remove(technology_list, technology);
1190 technology_dbus_unregister(technology);
1192 g_slist_free(technology->device_list);
1194 g_free(technology->path);
1195 g_free(technology->regdom);
1196 g_free(technology->tethering_ident);
1197 g_free(technology->tethering_passphrase);
1201 static void enable_tethering(struct connman_technology *technology)
1205 if (connman_setting_get_bool("PersistentTetheringMode") == FALSE)
1208 ret = set_tethering(technology, TRUE);
1209 if (ret < 0 && ret != -EALREADY)
1210 DBG("Cannot enable tethering yet for %s (%d/%s)",
1211 get_name(technology->type),
1212 -ret, strerror(-ret));
1215 void __connman_technology_add_interface(enum connman_service_type type,
1216 int index, const char *name, const char *ident)
1218 struct connman_technology *technology;
1219 GSList *tech_drivers;
1220 struct connman_technology_driver *driver;
1223 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1224 case CONNMAN_SERVICE_TYPE_SYSTEM:
1226 case CONNMAN_SERVICE_TYPE_ETHERNET:
1227 case CONNMAN_SERVICE_TYPE_WIFI:
1228 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1229 case CONNMAN_SERVICE_TYPE_CELLULAR:
1230 case CONNMAN_SERVICE_TYPE_GPS:
1231 case CONNMAN_SERVICE_TYPE_VPN:
1232 case CONNMAN_SERVICE_TYPE_GADGET:
1236 connman_info("Adding interface %s [ %s ]", name,
1237 __connman_service_type2string(type));
1239 technology = technology_find(type);
1241 if (technology == NULL)
1244 for (tech_drivers = technology->driver_list; tech_drivers != NULL;
1245 tech_drivers = g_slist_next(tech_drivers)) {
1246 driver = tech_drivers->data;
1248 if(driver->add_interface != NULL)
1249 driver->add_interface(technology, index, name, ident);
1253 * At this point we can try to enable tethering automatically as
1254 * now the interfaces are set properly.
1256 if (technology->tethering_persistent == TRUE)
1257 enable_tethering(technology);
1260 void __connman_technology_remove_interface(enum connman_service_type type,
1261 int index, const char *name, const char *ident)
1263 struct connman_technology *technology;
1264 GSList *tech_drivers;
1265 struct connman_technology_driver *driver;
1268 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1269 case CONNMAN_SERVICE_TYPE_SYSTEM:
1271 case CONNMAN_SERVICE_TYPE_ETHERNET:
1272 case CONNMAN_SERVICE_TYPE_WIFI:
1273 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1274 case CONNMAN_SERVICE_TYPE_CELLULAR:
1275 case CONNMAN_SERVICE_TYPE_GPS:
1276 case CONNMAN_SERVICE_TYPE_VPN:
1277 case CONNMAN_SERVICE_TYPE_GADGET:
1281 connman_info("Remove interface %s [ %s ]", name,
1282 __connman_service_type2string(type));
1284 technology = technology_find(type);
1286 if (technology == NULL)
1289 for (tech_drivers = technology->driver_list; tech_drivers != NULL;
1290 tech_drivers = g_slist_next(tech_drivers)) {
1291 driver = tech_drivers->data;
1293 if(driver->remove_interface != NULL)
1294 driver->remove_interface(technology, index);
1298 int __connman_technology_add_device(struct connman_device *device)
1300 struct connman_technology *technology;
1301 enum connman_service_type type;
1303 type = __connman_device_get_service_type(device);
1305 DBG("device %p type %s", device, get_name(type));
1307 technology = technology_get(type);
1308 if (technology == NULL) {
1310 * Since no driver can be found for this device at the moment we
1311 * add it to the techless device list.
1313 techless_device_list = g_slist_prepend(techless_device_list,
1319 __sync_synchronize();
1320 if (technology->rfkill_driven == TRUE) {
1321 if (technology->enabled == TRUE)
1322 __connman_device_enable(device);
1324 __connman_device_disable(device);
1329 if (technology->enable_persistent == TRUE &&
1330 global_offlinemode == FALSE) {
1331 int err = __connman_device_enable(device);
1333 * connman_technology_add_device() calls __connman_device_enable()
1334 * but since the device is already enabled, the calls does not
1335 * propagate through to connman_technology_enabled via
1336 * connman_device_set_powered.
1338 if (err == -EALREADY)
1339 __connman_technology_enabled(type);
1341 /* if technology persistent state is offline */
1342 if (technology->enable_persistent == FALSE)
1343 __connman_device_disable(device);
1346 technology->device_list = g_slist_prepend(technology->device_list,
1352 int __connman_technology_remove_device(struct connman_device *device)
1354 struct connman_technology *technology;
1355 enum connman_service_type type;
1357 DBG("device %p", device);
1359 type = __connman_device_get_service_type(device);
1361 technology = technology_find(type);
1362 if (technology == NULL) {
1363 techless_device_list = g_slist_remove(techless_device_list,
1368 technology->device_list = g_slist_remove(technology->device_list,
1371 if (technology->tethering == TRUE)
1372 set_tethering(technology, FALSE);
1374 technology_put(technology);
1379 static void powered_changed(struct connman_technology *technology)
1381 if (technology->dbus_registered == FALSE)
1384 if (technology->pending_reply != NULL) {
1385 g_dbus_send_reply(connection,
1386 technology->pending_reply, DBUS_TYPE_INVALID);
1387 dbus_message_unref(technology->pending_reply);
1388 technology->pending_reply = NULL;
1390 g_source_remove(technology->pending_timeout);
1391 technology->pending_timeout = 0;
1394 __sync_synchronize();
1395 connman_dbus_property_changed_basic(technology->path,
1396 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1397 DBUS_TYPE_BOOLEAN, &technology->enabled);
1400 static int technology_enabled(struct connman_technology *technology)
1402 __sync_synchronize();
1403 if (technology->enabled == TRUE)
1406 technology->enabled = TRUE;
1408 if (technology->tethering_persistent == TRUE)
1409 enable_tethering(technology);
1411 powered_changed(technology);
1416 int __connman_technology_enabled(enum connman_service_type type)
1418 struct connman_technology *technology;
1420 technology = technology_find(type);
1421 if (technology == NULL)
1424 DBG("technology %p type %s rfkill %d enabled %d", technology,
1425 get_name(type), technology->rfkill_driven,
1426 technology->enabled);
1428 if (technology->rfkill_driven == TRUE) {
1429 if (technology->tethering_persistent == TRUE)
1430 enable_tethering(technology);
1434 return technology_enabled(technology);
1437 static int technology_disabled(struct connman_technology *technology)
1439 __sync_synchronize();
1440 if (technology->enabled == FALSE)
1443 technology->enabled = FALSE;
1445 powered_changed(technology);
1450 int __connman_technology_disabled(enum connman_service_type type)
1452 struct connman_technology *technology;
1455 technology = technology_find(type);
1456 if (technology == NULL)
1459 if (technology->rfkill_driven == TRUE)
1462 for (list = technology->device_list; list != NULL; list = list->next) {
1463 struct connman_device *device = list->data;
1465 if (connman_device_get_powered(device) == TRUE)
1469 return technology_disabled(technology);
1472 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1477 if (global_offlinemode == offlinemode)
1480 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1483 * This is a bit tricky. When you set offlinemode, there is no
1484 * way to differentiate between attempting offline mode and
1485 * resuming offlinemode from last saved profile. We need that
1486 * information in rfkill_update, otherwise it falls back on the
1487 * technology's persistent state. Hence we set the offline mode here
1488 * but save it & call the notifier only if its successful.
1491 global_offlinemode = offlinemode;
1493 /* Traverse technology list, enable/disable each technology. */
1494 for (list = technology_list; list; list = list->next) {
1495 struct connman_technology *technology = list->data;
1498 err = technology_disable(technology);
1500 if (!offlinemode && technology->enable_persistent)
1501 err = technology_enable(technology);
1504 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1505 connman_technology_save_offlinemode();
1506 __connman_notifier_offlinemode(offlinemode);
1508 global_offlinemode = connman_technology_load_offlinemode();
1513 void __connman_technology_set_connected(enum connman_service_type type,
1514 connman_bool_t connected)
1516 struct connman_technology *technology;
1518 technology = technology_find(type);
1519 if (technology == NULL)
1522 DBG("technology %p connected %d", technology, connected);
1524 technology->connected = connected;
1526 connman_dbus_property_changed_basic(technology->path,
1527 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1528 DBUS_TYPE_BOOLEAN, &connected);
1531 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1532 connman_bool_t softblock,
1533 connman_bool_t hardblock,
1534 connman_bool_t new_rfkill)
1536 gboolean hardblock_changed = FALSE;
1537 gboolean apply = TRUE;
1538 GList *start, *list;
1540 DBG("technology %p --> %d/%d vs %d/%d",
1541 technology, softblock, hardblock,
1542 technology->softblocked, technology->hardblocked);
1544 if (technology->hardblocked == hardblock)
1545 goto softblock_change;
1547 if (!(new_rfkill == TRUE && hardblock == FALSE)) {
1548 start = g_hash_table_get_values(rfkill_list);
1550 for (list = start; list != NULL; list = list->next) {
1551 struct connman_rfkill *rfkill = list->data;
1553 if (rfkill->type != technology->type)
1556 if (rfkill->hardblock != hardblock)
1564 goto softblock_change;
1566 technology->hardblocked = hardblock;
1567 hardblock_changed = TRUE;
1570 if (apply == FALSE && technology->softblocked != softblock)
1574 return technology->hardblocked;
1576 technology->softblocked = softblock;
1578 if (technology->hardblocked == TRUE ||
1579 technology->softblocked == TRUE) {
1580 if (technology_disabled(technology) != -EALREADY)
1581 technology_affect_devices(technology, FALSE);
1582 } else if (technology->hardblocked == FALSE &&
1583 technology->softblocked == FALSE) {
1584 if (technology_enabled(technology) != -EALREADY)
1585 technology_affect_devices(technology, TRUE);
1588 if (hardblock_changed == TRUE) {
1589 if (technology->hardblocked == TRUE) {
1590 DBG("%s is switched off.", get_name(technology->type));
1591 technology_dbus_unregister(technology);
1593 technology_dbus_register(technology);
1596 return technology->hardblocked;
1599 int __connman_technology_add_rfkill(unsigned int index,
1600 enum connman_service_type type,
1601 connman_bool_t softblock,
1602 connman_bool_t hardblock)
1604 struct connman_technology *technology;
1605 struct connman_rfkill *rfkill;
1607 DBG("index %u type %d soft %u hard %u", index, type,
1608 softblock, hardblock);
1610 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1614 rfkill = g_try_new0(struct connman_rfkill, 1);
1618 rfkill->index = index;
1619 rfkill->type = type;
1620 rfkill->softblock = softblock;
1621 rfkill->hardblock = hardblock;
1623 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1626 technology = technology_get(type);
1627 /* If there is no driver for this type, ignore it. */
1628 if (technology == NULL)
1631 technology->rfkill_driven = TRUE;
1633 /* If hardblocked, there is no need to handle softblocked state */
1634 if (technology_apply_rfkill_change(technology,
1635 softblock, hardblock, TRUE) == TRUE)
1639 * Depending on softblocked state we unblock/block according to
1640 * offlinemode and persistente state.
1642 if (technology->softblocked == TRUE &&
1643 global_offlinemode == FALSE &&
1644 technology->enable_persistent == TRUE)
1645 return __connman_rfkill_block(type, FALSE);
1646 else if (technology->softblocked == FALSE &&
1647 (global_offlinemode == TRUE ||
1648 technology->enable_persistent == FALSE))
1649 return __connman_rfkill_block(type, TRUE);
1654 int __connman_technology_update_rfkill(unsigned int index,
1655 enum connman_service_type type,
1656 connman_bool_t softblock,
1657 connman_bool_t hardblock)
1659 struct connman_technology *technology;
1660 struct connman_rfkill *rfkill;
1662 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1664 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1668 if (rfkill->softblock == softblock &&
1669 rfkill->hardblock == hardblock)
1672 rfkill->softblock = softblock;
1673 rfkill->hardblock = hardblock;
1675 technology = technology_find(type);
1676 /* If there is no driver for this type, ignore it. */
1677 if (technology == NULL)
1680 /* If hardblocked, there is no need to handle softblocked state */
1681 if (technology_apply_rfkill_change(technology,
1682 softblock, hardblock, FALSE) == TRUE)
1685 if (global_offlinemode == TRUE)
1689 * Depending on softblocked state we unblock/block according to
1692 if (technology->softblocked == TRUE &&
1693 technology->enable_persistent == TRUE)
1694 return __connman_rfkill_block(type, FALSE);
1695 else if (technology->softblocked == FALSE &&
1696 technology->enable_persistent == FALSE)
1697 return __connman_rfkill_block(type, TRUE);
1702 int __connman_technology_remove_rfkill(unsigned int index,
1703 enum connman_service_type type)
1705 struct connman_technology *technology;
1706 struct connman_rfkill *rfkill;
1708 DBG("index %u", index);
1710 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1714 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1716 technology = technology_find(type);
1717 if (technology == NULL)
1720 technology_apply_rfkill_change(technology,
1721 technology->softblocked, !technology->hardblocked, FALSE);
1723 technology_put(technology);
1728 int __connman_technology_init(void)
1732 connection = connman_dbus_get_connection();
1734 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1737 global_offlinemode = connman_technology_load_offlinemode();
1739 /* This will create settings file if it is missing */
1740 connman_technology_save_offlinemode();
1745 void __connman_technology_cleanup(void)
1749 g_hash_table_destroy(rfkill_list);
1751 dbus_connection_unref(connection);