5 * Copyright (C) 2007-2013 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 bool global_offlinemode;
46 struct connman_rfkill {
48 enum connman_service_type type;
53 struct connman_technology {
55 enum connman_service_type type;
63 bool tethering_persistent; /* Tells the save status, needed
64 * as offline mode might set
67 char *tethering_ident;
68 char *tethering_passphrase;
69 bool tethering_hidden;
71 bool enable_persistent; /* Save the tech state */
75 DBusMessage *pending_reply;
76 guint pending_timeout;
86 static GSList *driver_list = NULL;
88 static gint compare_priority(gconstpointer a, gconstpointer b)
90 const struct connman_technology_driver *driver1 = a;
91 const struct connman_technology_driver *driver2 = b;
93 return driver2->priority - driver1->priority;
96 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
98 struct connman_rfkill *rfkill = value;
99 enum connman_service_type type = GPOINTER_TO_INT(user_data);
101 /* Calling _technology_rfkill_add will update the tech. */
102 if (rfkill->type == type)
103 __connman_technology_add_rfkill(rfkill->index, type,
104 rfkill->softblock, rfkill->hardblock);
108 connman_technology_is_tethering_allowed(enum connman_service_type type)
110 static char *allowed_default[] = { "wifi", "bluetooth", "gadget",
112 const char *type_str = __connman_service_type2string(type);
119 allowed = connman_setting_get_string_list("TetheringTechnologies");
121 allowed = allowed_default;
123 for (i = 0; allowed[i]; i++) {
124 if (g_strcmp0(allowed[i], type_str) == 0)
131 static const char *get_name(enum connman_service_type type)
134 case CONNMAN_SERVICE_TYPE_UNKNOWN:
135 case CONNMAN_SERVICE_TYPE_SYSTEM:
136 case CONNMAN_SERVICE_TYPE_GPS:
137 case CONNMAN_SERVICE_TYPE_VPN:
139 case CONNMAN_SERVICE_TYPE_GADGET:
141 case CONNMAN_SERVICE_TYPE_ETHERNET:
143 case CONNMAN_SERVICE_TYPE_WIFI:
145 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
147 case CONNMAN_SERVICE_TYPE_CELLULAR:
149 case CONNMAN_SERVICE_TYPE_P2P:
156 static void technology_save(struct connman_technology *technology)
160 const char *name = get_name(technology->type);
162 DBG("technology %p type %d name %s", technology, technology->type,
167 keyfile = __connman_storage_load_global();
169 keyfile = g_key_file_new();
171 identifier = g_strdup_printf("%s", name);
175 g_key_file_set_boolean(keyfile, identifier, "Enable",
176 technology->enable_persistent);
178 g_key_file_set_boolean(keyfile, identifier, "Tethering",
179 technology->tethering_persistent);
181 g_key_file_set_boolean(keyfile, identifier, "Hidden",
182 technology->tethering_hidden);
184 if (technology->tethering_ident)
185 g_key_file_set_string(keyfile, identifier,
186 "Tethering.Identifier",
187 technology->tethering_ident);
189 if (technology->tethering_passphrase)
190 g_key_file_set_string(keyfile, identifier,
191 "Tethering.Passphrase",
192 technology->tethering_passphrase);
197 __connman_storage_save_global(keyfile);
199 g_key_file_free(keyfile);
204 static void tethering_changed(struct connman_technology *technology)
206 dbus_bool_t tethering = technology->tethering;
208 connman_dbus_property_changed_basic(technology->path,
209 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
210 DBUS_TYPE_BOOLEAN, &tethering);
212 technology_save(technology);
215 void connman_technology_tethering_notify(struct connman_technology *technology,
218 DBG("technology %p enabled %u", technology, enabled);
220 if (technology->tethering == enabled)
223 technology->tethering = enabled;
225 tethering_changed(technology);
228 __connman_tethering_set_enabled();
230 __connman_tethering_set_disabled();
233 static int set_tethering(struct connman_technology *technology,
236 int result = -EOPNOTSUPP;
238 const char *ident, *passphrase, *bridge;
239 GSList *tech_drivers;
242 ident = technology->tethering_ident;
243 passphrase = technology->tethering_passphrase;
244 hidden = technology->tethering_hidden;
246 __sync_synchronize();
247 if (!technology->enabled)
250 bridge = __connman_tethering_get_bridge();
254 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && (!ident))
257 for (tech_drivers = technology->driver_list; tech_drivers;
258 tech_drivers = g_slist_next(tech_drivers)) {
259 struct connman_technology_driver *driver = tech_drivers->data;
261 if (!driver || !driver->set_tethering)
264 err = driver->set_tethering(technology, ident, passphrase,
265 bridge, enabled, hidden);
267 if (result == -EINPROGRESS)
270 if (err == -EINPROGRESS || err == 0) {
279 void connman_technology_regdom_notify(struct connman_technology *technology,
285 connman_error("Failed to set regulatory domain");
287 DBG("Regulatory domain set to %s", alpha2);
289 g_free(technology->regdom);
290 technology->regdom = g_strdup(alpha2);
293 static int set_regdom_by_device(struct connman_technology *technology,
298 for (list = technology->device_list; list; list = list->next) {
299 struct connman_device *device = list->data;
301 if (connman_device_set_regdom(device, alpha2) != 0)
308 int connman_technology_set_regdom(const char *alpha2)
310 GSList *list, *tech_drivers;
312 for (list = technology_list; list; list = list->next) {
313 struct connman_technology *technology = list->data;
315 if (set_regdom_by_device(technology, alpha2) != 0) {
317 for (tech_drivers = technology->driver_list;
319 tech_drivers = g_slist_next(tech_drivers)) {
321 struct connman_technology_driver *driver =
324 if (driver->set_regdom)
325 driver->set_regdom(technology, alpha2);
333 static struct connman_technology *technology_find(enum connman_service_type type)
337 DBG("type %d", type);
339 for (list = technology_list; list; list = list->next) {
340 struct connman_technology *technology = list->data;
342 if (technology->type == type)
349 bool connman_technology_get_wifi_tethering(const char **ssid,
352 struct connman_technology *technology;
359 technology = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
363 if (!technology->tethering)
366 *ssid = technology->tethering_ident;
367 *psk = technology->tethering_passphrase;
372 static void free_rfkill(gpointer data)
374 struct connman_rfkill *rfkill = data;
379 static void technology_load(struct connman_technology *technology)
383 GError *error = NULL;
384 bool enable, need_saving = false;
386 DBG("technology %p", technology);
388 keyfile = __connman_storage_load_global();
389 /* Fallback on disabling technology if file not found. */
391 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
392 /* We enable ethernet by default */
393 technology->enable_persistent = true;
395 technology->enable_persistent = false;
399 identifier = g_strdup_printf("%s", get_name(technology->type));
403 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
405 technology->enable_persistent = enable;
407 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
408 technology->enable_persistent = true;
410 technology->enable_persistent = false;
413 g_clear_error(&error);
416 enable = g_key_file_get_boolean(keyfile, identifier,
417 "Tethering", &error);
419 technology->tethering_persistent = enable;
422 g_clear_error(&error);
426 technology_save(technology);
428 technology->tethering_ident = g_key_file_get_string(keyfile,
429 identifier, "Tethering.Identifier", NULL);
431 technology->tethering_passphrase = g_key_file_get_string(keyfile,
432 identifier, "Tethering.Passphrase", NULL);
436 g_key_file_free(keyfile);
441 bool __connman_technology_get_offlinemode(void)
443 return global_offlinemode;
446 static void connman_technology_save_offlinemode(void)
450 keyfile = __connman_storage_load_global();
452 keyfile = g_key_file_new();
454 g_key_file_set_boolean(keyfile, "global",
455 "OfflineMode", global_offlinemode);
457 __connman_storage_save_global(keyfile);
459 g_key_file_free(keyfile);
464 static bool connman_technology_load_offlinemode(void)
467 GError *error = NULL;
470 /* If there is a error, we enable offlinemode */
471 keyfile = __connman_storage_load_global();
475 offlinemode = g_key_file_get_boolean(keyfile, "global",
476 "OfflineMode", &error);
479 g_clear_error(&error);
482 g_key_file_free(keyfile);
487 static void append_properties(DBusMessageIter *iter,
488 struct connman_technology *technology)
490 DBusMessageIter dict;
494 connman_dbus_dict_open(iter, &dict);
496 str = get_name(technology->type);
498 connman_dbus_dict_append_basic(&dict, "Name",
499 DBUS_TYPE_STRING, &str);
501 str = __connman_service_type2string(technology->type);
503 connman_dbus_dict_append_basic(&dict, "Type",
504 DBUS_TYPE_STRING, &str);
506 __sync_synchronize();
507 val = technology->enabled;
508 connman_dbus_dict_append_basic(&dict, "Powered",
512 val = technology->connected;
513 connman_dbus_dict_append_basic(&dict, "Connected",
517 val = technology->tethering;
518 connman_dbus_dict_append_basic(&dict, "Tethering",
522 if (technology->tethering_ident)
523 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
525 &technology->tethering_ident);
527 if (technology->tethering_passphrase)
528 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
530 &technology->tethering_passphrase);
532 val = technology->tethering_hidden;
533 connman_dbus_dict_append_basic(&dict, "Hidden",
537 connman_dbus_dict_close(iter, &dict);
540 static void technology_added_signal(struct connman_technology *technology)
543 DBusMessageIter iter;
545 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
546 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
550 dbus_message_iter_init_append(signal, &iter);
551 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
553 append_properties(&iter, technology);
555 dbus_connection_send(connection, signal, NULL);
556 dbus_message_unref(signal);
559 static void technology_removed_signal(struct connman_technology *technology)
561 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
562 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
563 DBUS_TYPE_OBJECT_PATH, &technology->path,
567 static DBusMessage *get_properties(DBusConnection *conn,
568 DBusMessage *message, void *user_data)
570 struct connman_technology *technology = user_data;
572 DBusMessageIter iter;
574 reply = dbus_message_new_method_return(message);
578 dbus_message_iter_init_append(reply, &iter);
579 append_properties(&iter, technology);
584 void __connman_technology_list_struct(DBusMessageIter *array)
587 DBusMessageIter entry;
589 for (list = technology_list; list; list = list->next) {
590 struct connman_technology *technology = list->data;
592 if (!technology->path ||
593 (technology->rfkill_driven &&
594 technology->hardblocked))
597 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
599 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
601 append_properties(&entry, technology);
602 dbus_message_iter_close_container(array, &entry);
606 static gboolean technology_pending_reply(gpointer user_data)
608 struct connman_technology *technology = user_data;
611 /* Power request timedout, send ETIMEDOUT. */
612 if (technology->pending_reply) {
613 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
615 g_dbus_send_message(connection, reply);
617 dbus_message_unref(technology->pending_reply);
618 technology->pending_reply = NULL;
619 technology->pending_timeout = 0;
625 static int technology_affect_devices(struct connman_technology *technology,
631 if (technology->type == CONNMAN_SERVICE_TYPE_P2P)
634 for (list = technology->device_list; list; list = list->next) {
635 struct connman_device *device = list->data;
638 err = __connman_device_enable(device);
640 err = __connman_device_disable(device);
646 static void powered_changed(struct connman_technology *technology)
650 if (!technology->dbus_registered)
653 if (technology->pending_reply) {
654 g_dbus_send_reply(connection,
655 technology->pending_reply, DBUS_TYPE_INVALID);
656 dbus_message_unref(technology->pending_reply);
657 technology->pending_reply = NULL;
659 g_source_remove(technology->pending_timeout);
660 technology->pending_timeout = 0;
663 __sync_synchronize();
664 enabled = technology->enabled;
665 connman_dbus_property_changed_basic(technology->path,
666 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
667 DBUS_TYPE_BOOLEAN, &enabled);
670 static void enable_tethering(struct connman_technology *technology)
674 if (!connman_setting_get_bool("PersistentTetheringMode"))
677 ret = set_tethering(technology, true);
678 if (ret < 0 && ret != -EALREADY)
679 DBG("Cannot enable tethering yet for %s (%d/%s)",
680 get_name(technology->type),
681 -ret, strerror(-ret));
684 static int technology_enabled(struct connman_technology *technology)
686 __sync_synchronize();
687 if (technology->enabled)
690 technology->enabled = true;
692 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
693 struct connman_technology *p2p;
695 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
696 if (p2p && !p2p->enabled && p2p->enable_persistent)
697 technology_enabled(p2p);
700 if (technology->tethering_persistent)
701 enable_tethering(technology);
703 powered_changed(technology);
708 static int technology_enable(struct connman_technology *technology)
713 DBG("technology %p enable", technology);
715 __sync_synchronize();
717 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
718 struct connman_technology *wifi;
720 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
721 if (wifi && wifi->enabled)
722 return technology_enabled(technology);
726 if (technology->enabled)
729 if (technology->pending_reply)
732 if (connman_setting_get_bool("PersistentTetheringMode") &&
733 technology->tethering)
734 set_tethering(technology, true);
736 if (technology->rfkill_driven)
737 err = __connman_rfkill_block(technology->type, false);
739 err_dev = technology_affect_devices(technology, true);
741 if (!technology->rfkill_driven)
747 static int technology_disabled(struct connman_technology *technology)
749 __sync_synchronize();
750 if (!technology->enabled)
753 technology->enabled = false;
755 powered_changed(technology);
760 static int technology_disable(struct connman_technology *technology)
764 DBG("technology %p disable", technology);
766 __sync_synchronize();
768 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
769 technology->enable_persistent = false;
770 return technology_disabled(technology);
771 } else if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
772 struct connman_technology *p2p;
774 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
775 if (p2p && p2p->enabled) {
776 p2p->enable_persistent = true;
777 technology_disabled(p2p);
781 if (!technology->enabled)
784 if (technology->pending_reply)
787 if (technology->tethering)
788 set_tethering(technology, false);
790 err = technology_affect_devices(technology, false);
792 if (technology->rfkill_driven)
793 err = __connman_rfkill_block(technology->type, true);
798 static DBusMessage *set_powered(struct connman_technology *technology,
799 DBusMessage *msg, bool powered)
801 DBusMessage *reply = NULL;
804 if (technology->rfkill_driven && technology->hardblocked) {
810 err = technology_enable(technology);
812 err = technology_disable(technology);
815 technology->enable_persistent = powered;
816 technology_save(technology);
820 if (err == -EINPROGRESS) {
821 technology->pending_reply = dbus_message_ref(msg);
822 technology->pending_timeout = g_timeout_add_seconds(10,
823 technology_pending_reply, technology);
824 } else if (err == -EALREADY) {
826 reply = __connman_error_already_enabled(msg);
828 reply = __connman_error_already_disabled(msg);
830 reply = __connman_error_failed(msg, -err);
832 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
837 static DBusMessage *set_property(DBusConnection *conn,
838 DBusMessage *msg, void *data)
840 struct connman_technology *technology = data;
841 DBusMessageIter iter, value;
845 DBG("conn %p", conn);
847 if (!dbus_message_iter_init(msg, &iter))
848 return __connman_error_invalid_arguments(msg);
850 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
851 return __connman_error_invalid_arguments(msg);
853 dbus_message_iter_get_basic(&iter, &name);
854 dbus_message_iter_next(&iter);
856 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
857 return __connman_error_invalid_arguments(msg);
859 dbus_message_iter_recurse(&iter, &value);
861 type = dbus_message_iter_get_arg_type(&value);
863 DBG("property %s", name);
865 if (g_str_equal(name, "Tethering")) {
866 dbus_bool_t tethering;
869 if (type != DBUS_TYPE_BOOLEAN)
870 return __connman_error_invalid_arguments(msg);
872 if (!connman_technology_is_tethering_allowed(technology->type)) {
873 DBG("%s tethering not allowed by config file",
874 __connman_service_type2string(technology->type));
875 return __connman_error_not_supported(msg);
878 dbus_message_iter_get_basic(&value, &tethering);
880 if (technology->tethering == tethering) {
882 return __connman_error_already_disabled(msg);
884 return __connman_error_already_enabled(msg);
887 err = set_tethering(technology, tethering);
889 return __connman_error_failed(msg, -err);
891 technology->tethering_persistent = tethering;
893 technology_save(technology);
895 } else if (g_str_equal(name, "TetheringIdentifier")) {
898 dbus_message_iter_get_basic(&value, &str);
900 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
901 return __connman_error_not_supported(msg);
903 if (strlen(str) < 1 || strlen(str) > 32)
904 return __connman_error_invalid_arguments(msg);
906 if (g_strcmp0(technology->tethering_ident, str) != 0) {
907 g_free(technology->tethering_ident);
908 technology->tethering_ident = g_strdup(str);
909 technology_save(technology);
911 connman_dbus_property_changed_basic(technology->path,
912 CONNMAN_TECHNOLOGY_INTERFACE,
913 "TetheringIdentifier",
915 &technology->tethering_ident);
917 } else if (g_str_equal(name, "TetheringPassphrase")) {
920 dbus_message_iter_get_basic(&value, &str);
922 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
923 return __connman_error_not_supported(msg);
925 if (strlen(str) < 8 || strlen(str) > 63) {
926 if (g_str_equal(str, "")) {
927 technology->tethering_passphrase = NULL;
929 connman_dbus_property_changed_basic(technology->path,
930 CONNMAN_TECHNOLOGY_INTERFACE,
931 "TetheringPassphrase",
936 return __connman_error_passphrase_required(msg);
938 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
939 g_free(technology->tethering_passphrase);
940 technology->tethering_passphrase = g_strdup(str);
941 technology_save(technology);
943 connman_dbus_property_changed_basic(technology->path,
944 CONNMAN_TECHNOLOGY_INTERFACE,
945 "TetheringPassphrase",
947 &technology->tethering_passphrase);
950 } else if (g_str_equal(name, "Hidden")) {
953 if (type != DBUS_TYPE_BOOLEAN)
954 return __connman_error_invalid_arguments(msg);
956 dbus_message_iter_get_basic(&value, &hidden);
958 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
959 return __connman_error_not_supported(msg);
961 technology->tethering_hidden = hidden;
962 technology_save(technology);
964 connman_dbus_property_changed_basic(technology->path,
965 CONNMAN_TECHNOLOGY_INTERFACE,
969 } else if (g_str_equal(name, "Powered")) {
972 if (type != DBUS_TYPE_BOOLEAN)
973 return __connman_error_invalid_arguments(msg);
975 dbus_message_iter_get_basic(&value, &enable);
977 return set_powered(technology, msg, enable);
979 return __connman_error_invalid_property(msg);
981 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
984 static void reply_scan_pending(struct connman_technology *technology, int err)
988 DBG("technology %p err %d", technology, err);
990 while (technology->scan_pending) {
991 DBusMessage *msg = technology->scan_pending->data;
993 DBG("reply to %s", dbus_message_get_sender(msg));
996 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
998 reply = __connman_error_failed(msg, -err);
999 g_dbus_send_message(connection, reply);
1000 dbus_message_unref(msg);
1002 technology->scan_pending =
1003 g_slist_delete_link(technology->scan_pending,
1004 technology->scan_pending);
1008 void __connman_technology_scan_started(struct connman_device *device)
1010 DBG("device %p", device);
1013 void __connman_technology_scan_stopped(struct connman_device *device,
1014 enum connman_service_type type)
1017 struct connman_technology *technology;
1020 technology = technology_find(type);
1022 DBG("technology %p device %p", technology, device);
1027 for (list = technology->device_list; list; list = list->next) {
1028 struct connman_device *other_device = list->data;
1030 if (device == other_device)
1033 if (__connman_device_get_service_type(other_device) != type)
1036 if (connman_device_get_scanning(other_device))
1041 reply_scan_pending(technology, 0);
1044 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
1045 int result, const char *alpha2)
1047 bool regdom_set = false;
1048 struct connman_technology *technology;
1049 enum connman_service_type type;
1050 GSList *tech_drivers;
1052 type = __connman_device_get_service_type(device);
1053 technology = technology_find(type);
1060 for (tech_drivers = technology->driver_list;
1062 tech_drivers = g_slist_next(tech_drivers)) {
1063 struct connman_technology_driver *driver =
1066 if (driver->set_regdom) {
1067 driver->set_regdom(technology, alpha2);
1077 connman_technology_regdom_notify(technology, alpha2);
1080 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
1082 struct connman_technology *technology = data;
1085 DBG("technology %p request from %s", technology,
1086 dbus_message_get_sender(msg));
1088 dbus_message_ref(msg);
1089 technology->scan_pending =
1090 g_slist_prepend(technology->scan_pending, msg);
1092 err = __connman_device_request_scan(technology->type);
1094 reply_scan_pending(technology, err);
1099 static const GDBusMethodTable technology_methods[] = {
1100 { GDBUS_DEPRECATED_METHOD("GetProperties",
1101 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
1103 { GDBUS_ASYNC_METHOD("SetProperty",
1104 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1105 NULL, set_property) },
1106 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
1110 static const GDBusSignalTable technology_signals[] = {
1111 { GDBUS_SIGNAL("PropertyChanged",
1112 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
1113 { GDBUS_SIGNAL("DhcpConnected",
1114 GDBUS_ARGS({ "aptype", "s" },
1117 { "hostname", "s" })) },
1118 { GDBUS_SIGNAL("DhcpLeaseDeleted",
1119 GDBUS_ARGS({ "aptype", "s" },
1122 { "hostname", "s" })) },
1126 static bool technology_dbus_register(struct connman_technology *technology)
1128 if (technology->dbus_registered ||
1129 (technology->rfkill_driven &&
1130 technology->hardblocked))
1133 if (!g_dbus_register_interface(connection, technology->path,
1134 CONNMAN_TECHNOLOGY_INTERFACE,
1135 technology_methods, technology_signals,
1136 NULL, technology, NULL)) {
1137 connman_error("Failed to register %s", technology->path);
1141 technology_added_signal(technology);
1142 technology->dbus_registered = true;
1147 static void technology_dbus_unregister(struct connman_technology *technology)
1149 if (!technology->dbus_registered)
1152 technology_removed_signal(technology);
1153 g_dbus_unregister_interface(connection, technology->path,
1154 CONNMAN_TECHNOLOGY_INTERFACE);
1156 technology->dbus_registered = false;
1159 static void technology_put(struct connman_technology *technology)
1161 DBG("technology %p", technology);
1163 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1166 reply_scan_pending(technology, -EINTR);
1168 while (technology->driver_list) {
1169 struct connman_technology_driver *driver;
1171 driver = technology->driver_list->data;
1174 driver->remove(technology);
1176 technology->driver_list =
1177 g_slist_delete_link(technology->driver_list,
1178 technology->driver_list);
1181 technology_list = g_slist_remove(technology_list, technology);
1183 technology_dbus_unregister(technology);
1185 g_slist_free(technology->device_list);
1187 g_free(technology->path);
1188 g_free(technology->regdom);
1189 g_free(technology->tethering_ident);
1190 g_free(technology->tethering_passphrase);
1194 static struct connman_technology *technology_get(enum connman_service_type type)
1196 GSList *tech_drivers = NULL;
1197 struct connman_technology_driver *driver;
1198 struct connman_technology *technology;
1202 DBG("type %d", type);
1204 str = __connman_service_type2string(type);
1208 technology = technology_find(type);
1210 if (type != CONNMAN_SERVICE_TYPE_P2P)
1211 __sync_fetch_and_add(&technology->refcount, 1);
1215 /* First check if we have a driver for this technology type */
1216 for (list = driver_list; list; list = list->next) {
1217 driver = list->data;
1219 if (driver->type == type) {
1220 DBG("technology %p driver %p", technology, driver);
1221 tech_drivers = g_slist_append(tech_drivers, driver);
1225 if (!tech_drivers) {
1226 DBG("No matching drivers found for %s.",
1227 __connman_service_type2string(type));
1231 technology = g_try_new0(struct connman_technology, 1);
1235 technology->refcount = 1;
1236 technology->type = type;
1237 technology->tethering_hidden = FALSE;
1238 technology->path = g_strdup_printf("%s/technology/%s",
1240 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1241 struct connman_technology *wifi;
1243 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
1245 technology->enabled = wifi->enabled;
1248 technology_load(technology);
1249 technology_list = g_slist_prepend(technology_list, technology);
1250 technology->driver_list = tech_drivers;
1252 for (list = tech_drivers; list; list = list->next) {
1253 driver = list->data;
1255 if (driver->probe && driver->probe(technology) < 0)
1256 DBG("Driver probe failed for technology %p",
1260 if (!technology_dbus_register(technology)) {
1261 technology_put(technology);
1265 DBG("technology %p", technology);
1270 int connman_technology_driver_register(struct connman_technology_driver *driver)
1273 struct connman_device *device;
1274 enum connman_service_type type;
1276 for (list = driver_list; list; list = list->next) {
1277 if (list->data == driver)
1281 DBG("Registering %s driver", driver->name);
1283 driver_list = g_slist_insert_sorted(driver_list, driver,
1287 * Check for technology less devices if this driver
1288 * can service any of them.
1290 for (list = techless_device_list; list; list = list->next) {
1291 device = list->data;
1293 type = __connman_device_get_service_type(device);
1294 if (type != driver->type)
1297 techless_device_list = g_slist_remove(techless_device_list,
1300 __connman_technology_add_device(device);
1303 /* Check for orphaned rfkill switches. */
1304 g_hash_table_foreach(rfkill_list, rfkill_check,
1305 GINT_TO_POINTER(driver->type));
1308 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1309 if (!technology_get(CONNMAN_SERVICE_TYPE_P2P))
1316 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
1318 GSList *list, *tech_drivers;
1319 struct connman_technology *technology;
1320 struct connman_technology_driver *current;
1322 DBG("Unregistering driver %p name %s", driver, driver->name);
1324 for (list = technology_list; list; list = list->next) {
1325 technology = list->data;
1327 for (tech_drivers = technology->driver_list; tech_drivers;
1328 tech_drivers = g_slist_next(tech_drivers)) {
1329 current = tech_drivers->data;
1330 if (driver != current)
1334 driver->remove(technology);
1336 technology->driver_list =
1337 g_slist_remove(technology->driver_list,
1343 driver_list = g_slist_remove(driver_list, driver);
1345 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1346 technology = technology_find(CONNMAN_SERVICE_TYPE_P2P);
1348 technology_put(technology);
1352 void __connman_technology_add_interface(enum connman_service_type type,
1353 int index, const char *ident)
1355 struct connman_technology *technology;
1356 GSList *tech_drivers;
1357 struct connman_technology_driver *driver;
1361 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1362 case CONNMAN_SERVICE_TYPE_SYSTEM:
1364 case CONNMAN_SERVICE_TYPE_ETHERNET:
1365 case CONNMAN_SERVICE_TYPE_WIFI:
1366 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1367 case CONNMAN_SERVICE_TYPE_CELLULAR:
1368 case CONNMAN_SERVICE_TYPE_GPS:
1369 case CONNMAN_SERVICE_TYPE_VPN:
1370 case CONNMAN_SERVICE_TYPE_GADGET:
1371 case CONNMAN_SERVICE_TYPE_P2P:
1375 name = connman_inet_ifname(index);
1376 connman_info("Adding interface %s [ %s ]", name,
1377 __connman_service_type2string(type));
1379 technology = technology_find(type);
1384 for (tech_drivers = technology->driver_list; tech_drivers;
1385 tech_drivers = g_slist_next(tech_drivers)) {
1386 driver = tech_drivers->data;
1388 if (driver->add_interface)
1389 driver->add_interface(technology, index, name, ident);
1393 * At this point we can try to enable tethering automatically as
1394 * now the interfaces are set properly.
1396 if (technology->tethering_persistent)
1397 enable_tethering(technology);
1403 void __connman_technology_remove_interface(enum connman_service_type type,
1404 int index, const char *ident)
1406 struct connman_technology *technology;
1407 GSList *tech_drivers;
1408 struct connman_technology_driver *driver;
1412 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1413 case CONNMAN_SERVICE_TYPE_SYSTEM:
1415 case CONNMAN_SERVICE_TYPE_ETHERNET:
1416 case CONNMAN_SERVICE_TYPE_WIFI:
1417 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1418 case CONNMAN_SERVICE_TYPE_CELLULAR:
1419 case CONNMAN_SERVICE_TYPE_GPS:
1420 case CONNMAN_SERVICE_TYPE_VPN:
1421 case CONNMAN_SERVICE_TYPE_GADGET:
1422 case CONNMAN_SERVICE_TYPE_P2P:
1426 name = connman_inet_ifname(index);
1427 connman_info("Remove interface %s [ %s ]", name,
1428 __connman_service_type2string(type));
1431 technology = technology_find(type);
1436 for (tech_drivers = technology->driver_list; tech_drivers;
1437 tech_drivers = g_slist_next(tech_drivers)) {
1438 driver = tech_drivers->data;
1440 if (driver->remove_interface)
1441 driver->remove_interface(technology, index);
1445 int __connman_technology_add_device(struct connman_device *device)
1447 struct connman_technology *technology;
1448 enum connman_service_type type;
1450 type = __connman_device_get_service_type(device);
1452 DBG("device %p type %s", device, get_name(type));
1454 technology = technology_get(type);
1457 * Since no driver can be found for this device at the moment we
1458 * add it to the techless device list.
1460 techless_device_list = g_slist_prepend(techless_device_list,
1466 __sync_synchronize();
1467 if (technology->rfkill_driven) {
1468 if (technology->enabled)
1469 __connman_device_enable(device);
1471 __connman_device_disable(device);
1476 if (technology->enable_persistent &&
1477 !global_offlinemode) {
1478 int err = __connman_device_enable(device);
1480 * connman_technology_add_device() calls __connman_device_enable()
1481 * but since the device is already enabled, the calls does not
1482 * propagate through to connman_technology_enabled via
1483 * connman_device_set_powered.
1485 if (err == -EALREADY)
1486 __connman_technology_enabled(type);
1488 /* if technology persistent state is offline */
1489 if (!technology->enable_persistent)
1490 __connman_device_disable(device);
1493 technology->device_list = g_slist_prepend(technology->device_list,
1499 int __connman_technology_remove_device(struct connman_device *device)
1501 struct connman_technology *technology;
1502 enum connman_service_type type;
1504 DBG("device %p", device);
1506 type = __connman_device_get_service_type(device);
1508 technology = technology_find(type);
1510 techless_device_list = g_slist_remove(techless_device_list,
1515 technology->device_list = g_slist_remove(technology->device_list,
1518 if (technology->tethering)
1519 set_tethering(technology, false);
1521 technology_put(technology);
1526 int __connman_technology_enabled(enum connman_service_type type)
1528 struct connman_technology *technology;
1530 technology = technology_find(type);
1534 DBG("technology %p type %s rfkill %d enabled %d", technology,
1535 get_name(type), technology->rfkill_driven,
1536 technology->enabled);
1538 if (technology->rfkill_driven) {
1539 if (technology->tethering_persistent)
1540 enable_tethering(technology);
1544 return technology_enabled(technology);
1547 int __connman_technology_disabled(enum connman_service_type type)
1549 struct connman_technology *technology;
1552 technology = technology_find(type);
1556 if (technology->rfkill_driven)
1559 for (list = technology->device_list; list; list = list->next) {
1560 struct connman_device *device = list->data;
1562 if (connman_device_get_powered(device))
1566 return technology_disabled(technology);
1569 int __connman_technology_set_offlinemode(bool offlinemode)
1572 int err = -EINVAL, enabled_tech_count = 0;
1574 if (global_offlinemode == offlinemode)
1577 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1580 * This is a bit tricky. When you set offlinemode, there is no
1581 * way to differentiate between attempting offline mode and
1582 * resuming offlinemode from last saved profile. We need that
1583 * information in rfkill_update, otherwise it falls back on the
1584 * technology's persistent state. Hence we set the offline mode here
1585 * but save it & call the notifier only if its successful.
1588 global_offlinemode = offlinemode;
1590 /* Traverse technology list, enable/disable each technology. */
1591 for (list = technology_list; list; list = list->next) {
1592 struct connman_technology *technology = list->data;
1595 err = technology_disable(technology);
1597 if (technology->hardblocked)
1600 if (technology->enable_persistent) {
1601 err = technology_enable(technology);
1602 enabled_tech_count++;
1607 if (err == 0 || err == -EINPROGRESS || err == -EALREADY ||
1608 (err == -EINVAL && enabled_tech_count == 0)) {
1609 connman_technology_save_offlinemode();
1610 __connman_notifier_offlinemode(offlinemode);
1612 global_offlinemode = connman_technology_load_offlinemode();
1617 void __connman_technology_set_connected(enum connman_service_type type,
1620 struct connman_technology *technology;
1623 technology = technology_find(type);
1627 DBG("technology %p connected %d", technology, connected);
1629 technology->connected = connected;
1632 connman_dbus_property_changed_basic(technology->path,
1633 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1634 DBUS_TYPE_BOOLEAN, &val);
1637 static bool technology_apply_rfkill_change(struct connman_technology *technology,
1642 bool hardblock_changed = false;
1644 GList *start, *list;
1646 DBG("technology %p --> %d/%d vs %d/%d",
1647 technology, softblock, hardblock,
1648 technology->softblocked, technology->hardblocked);
1650 if (technology->hardblocked == hardblock)
1651 goto softblock_change;
1653 if (!(new_rfkill && !hardblock)) {
1654 start = g_hash_table_get_values(rfkill_list);
1656 for (list = start; list; list = list->next) {
1657 struct connman_rfkill *rfkill = list->data;
1659 if (rfkill->type != technology->type)
1662 if (rfkill->hardblock != hardblock)
1670 goto softblock_change;
1672 technology->hardblocked = hardblock;
1673 hardblock_changed = true;
1676 if (!apply && technology->softblocked != softblock)
1680 return technology->hardblocked;
1682 technology->softblocked = softblock;
1684 if (technology->hardblocked ||
1685 technology->softblocked) {
1686 if (technology_disabled(technology) != -EALREADY)
1687 technology_affect_devices(technology, false);
1688 } else if (!technology->hardblocked &&
1689 !technology->softblocked) {
1690 if (technology_enabled(technology) != -EALREADY)
1691 technology_affect_devices(technology, true);
1694 if (hardblock_changed) {
1695 if (technology->hardblocked) {
1696 DBG("%s is switched off.", get_name(technology->type));
1697 technology_dbus_unregister(technology);
1699 DBG("%s is switched on.", get_name(technology->type));
1700 technology_dbus_register(technology);
1702 if (global_offlinemode)
1703 __connman_rfkill_block(technology->type, true);
1707 return technology->hardblocked;
1710 int __connman_technology_add_rfkill(unsigned int index,
1711 enum connman_service_type type,
1715 struct connman_technology *technology;
1716 struct connman_rfkill *rfkill;
1718 DBG("index %u type %d soft %u hard %u", index, type,
1719 softblock, hardblock);
1721 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1725 rfkill = g_try_new0(struct connman_rfkill, 1);
1729 rfkill->index = index;
1730 rfkill->type = type;
1731 rfkill->softblock = softblock;
1732 rfkill->hardblock = hardblock;
1734 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1737 technology = technology_get(type);
1738 /* If there is no driver for this type, ignore it. */
1742 technology->rfkill_driven = true;
1744 /* If hardblocked, there is no need to handle softblocked state */
1745 if (technology_apply_rfkill_change(technology,
1746 softblock, hardblock, true))
1749 if (global_offlinemode)
1753 * Depending on softblocked state we unblock/block according to
1754 * offlinemode and persistente state.
1756 if (technology->softblocked &&
1757 technology->enable_persistent)
1758 return __connman_rfkill_block(type, false);
1759 else if (!technology->softblocked &&
1760 !technology->enable_persistent)
1761 return __connman_rfkill_block(type, true);
1766 int __connman_technology_update_rfkill(unsigned int index,
1767 enum connman_service_type type,
1771 struct connman_technology *technology;
1772 struct connman_rfkill *rfkill;
1774 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1776 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1780 if (rfkill->softblock == softblock &&
1781 rfkill->hardblock == hardblock)
1784 rfkill->softblock = softblock;
1785 rfkill->hardblock = hardblock;
1787 technology = technology_find(type);
1788 /* If there is no driver for this type, ignore it. */
1792 technology_apply_rfkill_change(technology, softblock, hardblock,
1795 if (technology->hardblocked)
1796 DBG("%s hardblocked", get_name(technology->type));
1798 DBG("%s is%s softblocked", get_name(technology->type),
1799 technology->softblocked ? "" : " not");
1804 int __connman_technology_remove_rfkill(unsigned int index,
1805 enum connman_service_type type)
1807 struct connman_technology *technology;
1808 struct connman_rfkill *rfkill;
1810 DBG("index %u", index);
1812 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1816 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1818 technology = technology_find(type);
1822 technology_apply_rfkill_change(technology,
1823 technology->softblocked, !technology->hardblocked, false);
1825 technology_put(technology);
1830 int __connman_technology_init(void)
1834 connection = connman_dbus_get_connection();
1836 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1839 global_offlinemode = connman_technology_load_offlinemode();
1841 /* This will create settings file if it is missing */
1842 connman_technology_save_offlinemode();
1847 void __connman_technology_cleanup(void)
1851 g_hash_table_destroy(rfkill_list);
1853 dbus_connection_unref(connection);