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 int technology_enabled(struct connman_technology *technology);
89 static int technology_disabled(struct connman_technology *technology);
91 static gint compare_priority(gconstpointer a, gconstpointer b)
93 const struct connman_technology_driver *driver1 = a;
94 const struct connman_technology_driver *driver2 = b;
96 return driver2->priority - driver1->priority;
99 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
101 struct connman_rfkill *rfkill = value;
102 enum connman_service_type type = GPOINTER_TO_INT(user_data);
104 /* Calling _technology_rfkill_add will update the tech. */
105 if (rfkill->type == type)
106 __connman_technology_add_rfkill(rfkill->index, type,
107 rfkill->softblock, rfkill->hardblock);
111 connman_technology_is_tethering_allowed(enum connman_service_type type)
113 static char *allowed_default[] = { "wifi", "bluetooth", "gadget",
115 const char *type_str = __connman_service_type2string(type);
122 allowed = connman_setting_get_string_list("TetheringTechnologies");
124 allowed = allowed_default;
126 for (i = 0; allowed[i]; i++) {
127 if (g_strcmp0(allowed[i], type_str) == 0)
134 static const char *get_name(enum connman_service_type type)
137 case CONNMAN_SERVICE_TYPE_UNKNOWN:
138 case CONNMAN_SERVICE_TYPE_SYSTEM:
139 case CONNMAN_SERVICE_TYPE_GPS:
140 case CONNMAN_SERVICE_TYPE_VPN:
142 case CONNMAN_SERVICE_TYPE_GADGET:
144 case CONNMAN_SERVICE_TYPE_ETHERNET:
146 case CONNMAN_SERVICE_TYPE_WIFI:
148 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
150 case CONNMAN_SERVICE_TYPE_CELLULAR:
152 case CONNMAN_SERVICE_TYPE_P2P:
159 static void technology_save(struct connman_technology *technology)
163 const char *name = get_name(technology->type);
165 DBG("technology %p type %d name %s", technology, technology->type,
170 keyfile = __connman_storage_load_global();
172 keyfile = g_key_file_new();
174 identifier = g_strdup_printf("%s", name);
178 g_key_file_set_boolean(keyfile, identifier, "Enable",
179 technology->enable_persistent);
181 g_key_file_set_boolean(keyfile, identifier, "Tethering",
182 technology->tethering_persistent);
184 g_key_file_set_boolean(keyfile, identifier, "Hidden",
185 technology->tethering_hidden);
187 if (technology->tethering_ident)
188 g_key_file_set_string(keyfile, identifier,
189 "Tethering.Identifier",
190 technology->tethering_ident);
192 if (technology->tethering_passphrase)
193 g_key_file_set_string(keyfile, identifier,
194 "Tethering.Passphrase",
195 technology->tethering_passphrase);
200 __connman_storage_save_global(keyfile);
202 g_key_file_free(keyfile);
207 static void tethering_changed(struct connman_technology *technology)
209 dbus_bool_t tethering = technology->tethering;
211 connman_dbus_property_changed_basic(technology->path,
212 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
213 DBUS_TYPE_BOOLEAN, &tethering);
215 technology_save(technology);
218 void connman_technology_tethering_notify(struct connman_technology *technology,
221 DBG("technology %p enabled %u", technology, enabled);
223 if (technology->tethering == enabled)
226 technology->tethering = enabled;
228 tethering_changed(technology);
231 __connman_tethering_set_enabled();
233 __connman_tethering_set_disabled();
236 static int set_tethering(struct connman_technology *technology,
239 int result = -EOPNOTSUPP;
241 const char *ident, *passphrase, *bridge;
242 GSList *tech_drivers;
245 ident = technology->tethering_ident;
246 passphrase = technology->tethering_passphrase;
247 hidden = technology->tethering_hidden;
249 __sync_synchronize();
250 if (!technology->enabled)
253 bridge = __connman_tethering_get_bridge();
257 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && (!ident))
260 for (tech_drivers = technology->driver_list; tech_drivers;
261 tech_drivers = g_slist_next(tech_drivers)) {
262 struct connman_technology_driver *driver = tech_drivers->data;
264 if (!driver || !driver->set_tethering)
267 err = driver->set_tethering(technology, ident, passphrase,
268 bridge, enabled, hidden);
270 if (result == -EINPROGRESS)
273 if (err == -EINPROGRESS || err == 0) {
282 void connman_technology_regdom_notify(struct connman_technology *technology,
288 connman_error("Failed to set regulatory domain");
290 DBG("Regulatory domain set to %s", alpha2);
292 g_free(technology->regdom);
293 technology->regdom = g_strdup(alpha2);
296 static int set_regdom_by_device(struct connman_technology *technology,
301 for (list = technology->device_list; list; list = list->next) {
302 struct connman_device *device = list->data;
304 if (connman_device_set_regdom(device, alpha2) != 0)
311 int connman_technology_set_regdom(const char *alpha2)
313 GSList *list, *tech_drivers;
315 for (list = technology_list; list; list = list->next) {
316 struct connman_technology *technology = list->data;
318 if (set_regdom_by_device(technology, alpha2) != 0) {
320 for (tech_drivers = technology->driver_list;
322 tech_drivers = g_slist_next(tech_drivers)) {
324 struct connman_technology_driver *driver =
327 if (driver->set_regdom)
328 driver->set_regdom(technology, alpha2);
336 static struct connman_technology *technology_find(enum connman_service_type type)
340 DBG("type %d", type);
342 for (list = technology_list; list; list = list->next) {
343 struct connman_technology *technology = list->data;
345 if (technology->type == type)
352 bool connman_technology_get_wifi_tethering(const char **ssid,
355 struct connman_technology *technology;
362 technology = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
366 if (!technology->tethering)
369 *ssid = technology->tethering_ident;
370 *psk = technology->tethering_passphrase;
375 static void free_rfkill(gpointer data)
377 struct connman_rfkill *rfkill = data;
382 static void technology_load(struct connman_technology *technology)
386 GError *error = NULL;
387 bool enable, need_saving = false;
389 DBG("technology %p", technology);
391 keyfile = __connman_storage_load_global();
392 /* Fallback on disabling technology if file not found. */
394 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
395 /* We enable ethernet by default */
396 technology->enable_persistent = true;
398 technology->enable_persistent = false;
402 identifier = g_strdup_printf("%s", get_name(technology->type));
406 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
408 technology->enable_persistent = enable;
410 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
411 technology->enable_persistent = true;
413 technology->enable_persistent = false;
416 g_clear_error(&error);
419 enable = g_key_file_get_boolean(keyfile, identifier,
420 "Tethering", &error);
422 technology->tethering_persistent = enable;
425 g_clear_error(&error);
429 technology_save(technology);
431 technology->tethering_ident = g_key_file_get_string(keyfile,
432 identifier, "Tethering.Identifier", NULL);
434 technology->tethering_passphrase = g_key_file_get_string(keyfile,
435 identifier, "Tethering.Passphrase", NULL);
439 g_key_file_free(keyfile);
444 bool __connman_technology_get_offlinemode(void)
446 return global_offlinemode;
449 static void connman_technology_save_offlinemode(void)
453 keyfile = __connman_storage_load_global();
455 keyfile = g_key_file_new();
457 g_key_file_set_boolean(keyfile, "global",
458 "OfflineMode", global_offlinemode);
460 __connman_storage_save_global(keyfile);
462 g_key_file_free(keyfile);
467 static bool connman_technology_load_offlinemode(void)
470 GError *error = NULL;
473 /* If there is a error, we enable offlinemode */
474 keyfile = __connman_storage_load_global();
478 offlinemode = g_key_file_get_boolean(keyfile, "global",
479 "OfflineMode", &error);
482 g_clear_error(&error);
485 g_key_file_free(keyfile);
490 static void append_properties(DBusMessageIter *iter,
491 struct connman_technology *technology)
493 DBusMessageIter dict;
497 connman_dbus_dict_open(iter, &dict);
499 str = get_name(technology->type);
501 connman_dbus_dict_append_basic(&dict, "Name",
502 DBUS_TYPE_STRING, &str);
504 str = __connman_service_type2string(technology->type);
506 connman_dbus_dict_append_basic(&dict, "Type",
507 DBUS_TYPE_STRING, &str);
509 __sync_synchronize();
510 val = technology->enabled;
511 connman_dbus_dict_append_basic(&dict, "Powered",
515 val = technology->connected;
516 connman_dbus_dict_append_basic(&dict, "Connected",
520 val = technology->tethering;
521 connman_dbus_dict_append_basic(&dict, "Tethering",
525 if (technology->tethering_ident)
526 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
528 &technology->tethering_ident);
530 if (technology->tethering_passphrase)
531 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
533 &technology->tethering_passphrase);
535 val = technology->tethering_hidden;
536 connman_dbus_dict_append_basic(&dict, "Hidden",
540 connman_dbus_dict_close(iter, &dict);
543 static void technology_added_signal(struct connman_technology *technology)
546 DBusMessageIter iter;
548 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
549 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
553 dbus_message_iter_init_append(signal, &iter);
554 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
556 append_properties(&iter, technology);
558 dbus_connection_send(connection, signal, NULL);
559 dbus_message_unref(signal);
562 static void technology_removed_signal(struct connman_technology *technology)
564 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
565 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
566 DBUS_TYPE_OBJECT_PATH, &technology->path,
570 static DBusMessage *get_properties(DBusConnection *conn,
571 DBusMessage *message, void *user_data)
573 struct connman_technology *technology = user_data;
575 DBusMessageIter iter;
577 reply = dbus_message_new_method_return(message);
581 dbus_message_iter_init_append(reply, &iter);
582 append_properties(&iter, technology);
587 void __connman_technology_list_struct(DBusMessageIter *array)
590 DBusMessageIter entry;
592 for (list = technology_list; list; list = list->next) {
593 struct connman_technology *technology = list->data;
595 if (!technology->path ||
596 (technology->rfkill_driven &&
597 technology->hardblocked))
600 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
602 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
604 append_properties(&entry, technology);
605 dbus_message_iter_close_container(array, &entry);
609 static gboolean technology_pending_reply(gpointer user_data)
611 struct connman_technology *technology = user_data;
614 /* Power request timedout, send ETIMEDOUT. */
615 if (technology->pending_reply) {
616 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
618 g_dbus_send_message(connection, reply);
620 dbus_message_unref(technology->pending_reply);
621 technology->pending_reply = NULL;
622 technology->pending_timeout = 0;
628 static int technology_affect_devices(struct connman_technology *technology,
631 int err = 0, err_dev;
634 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
636 __connman_technology_enabled(technology->type);
638 __connman_technology_disabled(technology->type);
642 for (list = technology->device_list; list; list = list->next) {
643 struct connman_device *device = list->data;
646 err_dev = __connman_device_enable(device);
648 err_dev = __connman_device_disable(device);
650 if (err_dev < 0 && err_dev != -EALREADY)
657 static void powered_changed(struct connman_technology *technology)
661 if (!technology->dbus_registered)
664 if (technology->pending_reply) {
665 g_dbus_send_reply(connection,
666 technology->pending_reply, DBUS_TYPE_INVALID);
667 dbus_message_unref(technology->pending_reply);
668 technology->pending_reply = NULL;
670 g_source_remove(technology->pending_timeout);
671 technology->pending_timeout = 0;
674 __sync_synchronize();
675 enabled = technology->enabled;
676 connman_dbus_property_changed_basic(technology->path,
677 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
678 DBUS_TYPE_BOOLEAN, &enabled);
681 static void enable_tethering(struct connman_technology *technology)
685 if (!connman_setting_get_bool("PersistentTetheringMode"))
688 ret = set_tethering(technology, true);
689 if (ret < 0 && ret != -EALREADY)
690 DBG("Cannot enable tethering yet for %s (%d/%s)",
691 get_name(technology->type),
692 -ret, strerror(-ret));
695 static int technology_enabled(struct connman_technology *technology)
697 __sync_synchronize();
698 if (technology->enabled)
701 technology->enabled = true;
703 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
704 struct connman_technology *p2p;
706 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
707 if (p2p && !p2p->enabled && p2p->enable_persistent)
708 technology_enabled(p2p);
711 if (technology->tethering_persistent)
712 enable_tethering(technology);
714 powered_changed(technology);
719 static int technology_enable(struct connman_technology *technology)
724 DBG("technology %p enable", technology);
726 __sync_synchronize();
728 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
729 struct connman_technology *wifi;
731 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
732 if (wifi && wifi->enabled)
733 return technology_enabled(technology);
737 if (technology->enabled)
740 if (technology->pending_reply)
743 if (connman_setting_get_bool("PersistentTetheringMode") &&
744 technology->tethering)
745 set_tethering(technology, true);
747 if (technology->rfkill_driven)
748 err = __connman_rfkill_block(technology->type, false);
750 err_dev = technology_affect_devices(technology, true);
752 if (!technology->rfkill_driven)
758 static int technology_disabled(struct connman_technology *technology)
760 __sync_synchronize();
761 if (!technology->enabled)
764 technology->enabled = false;
766 powered_changed(technology);
771 static int technology_disable(struct connman_technology *technology)
775 DBG("technology %p disable", technology);
777 __sync_synchronize();
779 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
780 technology->enable_persistent = false;
781 return technology_disabled(technology);
782 } else if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
783 struct connman_technology *p2p;
785 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
786 if (p2p && p2p->enabled) {
787 p2p->enable_persistent = true;
788 technology_disabled(p2p);
792 if (!technology->enabled)
795 if (technology->pending_reply)
798 if (technology->tethering)
799 set_tethering(technology, false);
801 err = technology_affect_devices(technology, false);
803 if (technology->rfkill_driven)
804 err = __connman_rfkill_block(technology->type, true);
809 static DBusMessage *set_powered(struct connman_technology *technology,
810 DBusMessage *msg, bool powered)
812 DBusMessage *reply = NULL;
815 if (technology->rfkill_driven && technology->hardblocked) {
821 err = technology_enable(technology);
823 err = technology_disable(technology);
826 technology->enable_persistent = powered;
827 technology_save(technology);
831 if (err == -EINPROGRESS) {
832 technology->pending_reply = dbus_message_ref(msg);
833 technology->pending_timeout = g_timeout_add_seconds(10,
834 technology_pending_reply, technology);
835 } else if (err == -EALREADY) {
837 reply = __connman_error_already_enabled(msg);
839 reply = __connman_error_already_disabled(msg);
841 reply = __connman_error_failed(msg, -err);
843 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
848 static DBusMessage *set_property(DBusConnection *conn,
849 DBusMessage *msg, void *data)
851 struct connman_technology *technology = data;
852 DBusMessageIter iter, value;
856 DBG("conn %p", conn);
858 if (!dbus_message_iter_init(msg, &iter))
859 return __connman_error_invalid_arguments(msg);
861 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
862 return __connman_error_invalid_arguments(msg);
864 dbus_message_iter_get_basic(&iter, &name);
865 dbus_message_iter_next(&iter);
867 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
868 return __connman_error_invalid_arguments(msg);
870 dbus_message_iter_recurse(&iter, &value);
872 type = dbus_message_iter_get_arg_type(&value);
874 DBG("property %s", name);
876 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && technology->connected) {
878 if (connman_dbus_get_connection_unix_user_sync(conn,
879 dbus_message_get_sender(msg),
881 DBG("Can not get unix user id!");
882 return __connman_error_permission_denied(msg);
885 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
886 DBG("Not allow this user to operate wifi technology now!");
887 return __connman_error_permission_denied(msg);
891 if (g_str_equal(name, "Tethering")) {
892 dbus_bool_t tethering;
895 if (type != DBUS_TYPE_BOOLEAN)
896 return __connman_error_invalid_arguments(msg);
898 if (!connman_technology_is_tethering_allowed(technology->type)) {
899 DBG("%s tethering not allowed by config file",
900 __connman_service_type2string(technology->type));
901 return __connman_error_not_supported(msg);
904 dbus_message_iter_get_basic(&value, &tethering);
906 if (technology->tethering == tethering) {
908 return __connman_error_already_disabled(msg);
910 return __connman_error_already_enabled(msg);
913 err = set_tethering(technology, tethering);
915 return __connman_error_failed(msg, -err);
917 technology->tethering_persistent = tethering;
919 technology_save(technology);
921 } else if (g_str_equal(name, "TetheringIdentifier")) {
924 dbus_message_iter_get_basic(&value, &str);
926 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
927 return __connman_error_not_supported(msg);
929 if (strlen(str) < 1 || strlen(str) > 32)
930 return __connman_error_invalid_arguments(msg);
932 if (g_strcmp0(technology->tethering_ident, str) != 0) {
933 g_free(technology->tethering_ident);
934 technology->tethering_ident = g_strdup(str);
935 technology_save(technology);
937 connman_dbus_property_changed_basic(technology->path,
938 CONNMAN_TECHNOLOGY_INTERFACE,
939 "TetheringIdentifier",
941 &technology->tethering_ident);
943 } else if (g_str_equal(name, "TetheringPassphrase")) {
946 dbus_message_iter_get_basic(&value, &str);
948 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
949 return __connman_error_not_supported(msg);
951 if (strlen(str) < 8 || strlen(str) > 63) {
952 if (g_str_equal(str, "")) {
953 technology->tethering_passphrase = NULL;
955 connman_dbus_property_changed_basic(technology->path,
956 CONNMAN_TECHNOLOGY_INTERFACE,
957 "TetheringPassphrase",
962 return __connman_error_passphrase_required(msg);
964 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
965 g_free(technology->tethering_passphrase);
966 technology->tethering_passphrase = g_strdup(str);
967 technology_save(technology);
969 connman_dbus_property_changed_basic(technology->path,
970 CONNMAN_TECHNOLOGY_INTERFACE,
971 "TetheringPassphrase",
973 &technology->tethering_passphrase);
976 } else if (g_str_equal(name, "Hidden")) {
979 if (type != DBUS_TYPE_BOOLEAN)
980 return __connman_error_invalid_arguments(msg);
982 dbus_message_iter_get_basic(&value, &hidden);
984 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
985 return __connman_error_not_supported(msg);
987 technology->tethering_hidden = hidden;
988 technology_save(technology);
990 connman_dbus_property_changed_basic(technology->path,
991 CONNMAN_TECHNOLOGY_INTERFACE,
995 } else if (g_str_equal(name, "Powered")) {
998 if (type != DBUS_TYPE_BOOLEAN)
999 return __connman_error_invalid_arguments(msg);
1001 dbus_message_iter_get_basic(&value, &enable);
1003 return set_powered(technology, msg, enable);
1005 return __connman_error_invalid_property(msg);
1007 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1010 static void reply_scan_pending(struct connman_technology *technology, int err)
1014 DBG("technology %p err %d", technology, err);
1016 while (technology->scan_pending) {
1017 DBusMessage *msg = technology->scan_pending->data;
1019 DBG("reply to %s", dbus_message_get_sender(msg));
1022 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1024 reply = __connman_error_failed(msg, -err);
1025 g_dbus_send_message(connection, reply);
1026 dbus_message_unref(msg);
1028 technology->scan_pending =
1029 g_slist_delete_link(technology->scan_pending,
1030 technology->scan_pending);
1034 void __connman_technology_scan_started(struct connman_device *device)
1036 DBG("device %p", device);
1037 #if defined TIZEN_EXT
1038 DBusMessage *signal;
1040 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1041 CONNMAN_MANAGER_INTERFACE, "ScanStarted");
1045 dbus_connection_send(connection, signal, NULL);
1046 dbus_message_unref(signal);
1050 void __connman_technology_scan_stopped(struct connman_device *device,
1051 enum connman_service_type type)
1054 struct connman_technology *technology;
1057 technology = technology_find(type);
1059 DBG("technology %p device %p", technology, device);
1064 for (list = technology->device_list; list; list = list->next) {
1065 struct connman_device *other_device = list->data;
1067 if (device == other_device)
1070 if (__connman_device_get_service_type(other_device) != type)
1073 if (connman_device_get_scanning(other_device))
1077 #if defined TIZEN_EXT
1079 DBusMessage *signal;
1081 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1082 CONNMAN_MANAGER_INTERFACE, "ScanDone");
1086 dbus_connection_send(connection, signal, NULL);
1087 dbus_message_unref(signal);
1088 reply_scan_pending(technology, 0);
1092 reply_scan_pending(technology, 0);
1096 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
1097 int result, const char *alpha2)
1099 bool regdom_set = false;
1100 struct connman_technology *technology;
1101 enum connman_service_type type;
1102 GSList *tech_drivers;
1104 type = __connman_device_get_service_type(device);
1105 technology = technology_find(type);
1112 for (tech_drivers = technology->driver_list;
1114 tech_drivers = g_slist_next(tech_drivers)) {
1115 struct connman_technology_driver *driver =
1118 if (driver->set_regdom) {
1119 driver->set_regdom(technology, alpha2);
1129 connman_technology_regdom_notify(technology, alpha2);
1132 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
1134 struct connman_technology *technology = data;
1137 DBG("technology %p request from %s", technology,
1138 dbus_message_get_sender(msg));
1140 dbus_message_ref(msg);
1141 technology->scan_pending =
1142 g_slist_prepend(technology->scan_pending, msg);
1144 err = __connman_device_request_scan(technology->type);
1146 reply_scan_pending(technology, err);
1151 static const GDBusMethodTable technology_methods[] = {
1152 { GDBUS_DEPRECATED_METHOD("GetProperties",
1153 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
1155 { GDBUS_ASYNC_METHOD("SetProperty",
1156 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1157 NULL, set_property) },
1158 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
1162 static const GDBusSignalTable technology_signals[] = {
1163 { GDBUS_SIGNAL("PropertyChanged",
1164 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
1165 { GDBUS_SIGNAL("DhcpConnected",
1166 GDBUS_ARGS({ "aptype", "s" },
1169 { "hostname", "s" })) },
1170 { GDBUS_SIGNAL("DhcpLeaseDeleted",
1171 GDBUS_ARGS({ "aptype", "s" },
1174 { "hostname", "s" })) },
1178 static bool technology_dbus_register(struct connman_technology *technology)
1180 if (technology->dbus_registered ||
1181 (technology->rfkill_driven &&
1182 technology->hardblocked))
1185 if (!g_dbus_register_interface(connection, technology->path,
1186 CONNMAN_TECHNOLOGY_INTERFACE,
1187 technology_methods, technology_signals,
1188 NULL, technology, NULL)) {
1189 connman_error("Failed to register %s", technology->path);
1193 technology_added_signal(technology);
1194 technology->dbus_registered = true;
1199 static void technology_dbus_unregister(struct connman_technology *technology)
1201 if (!technology->dbus_registered)
1204 technology_removed_signal(technology);
1205 g_dbus_unregister_interface(connection, technology->path,
1206 CONNMAN_TECHNOLOGY_INTERFACE);
1208 technology->dbus_registered = false;
1211 static void technology_put(struct connman_technology *technology)
1213 DBG("technology %p", technology);
1215 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1218 reply_scan_pending(technology, -EINTR);
1220 while (technology->driver_list) {
1221 struct connman_technology_driver *driver;
1223 driver = technology->driver_list->data;
1226 driver->remove(technology);
1228 technology->driver_list =
1229 g_slist_delete_link(technology->driver_list,
1230 technology->driver_list);
1233 technology_list = g_slist_remove(technology_list, technology);
1235 technology_dbus_unregister(technology);
1237 g_slist_free(technology->device_list);
1239 g_free(technology->path);
1240 g_free(technology->regdom);
1241 g_free(technology->tethering_ident);
1242 g_free(technology->tethering_passphrase);
1246 static struct connman_technology *technology_get(enum connman_service_type type)
1248 GSList *tech_drivers = NULL;
1249 struct connman_technology_driver *driver;
1250 struct connman_technology *technology;
1254 DBG("type %d", type);
1256 str = __connman_service_type2string(type);
1260 technology = technology_find(type);
1262 if (type != CONNMAN_SERVICE_TYPE_P2P)
1263 __sync_fetch_and_add(&technology->refcount, 1);
1267 /* First check if we have a driver for this technology type */
1268 for (list = driver_list; list; list = list->next) {
1269 driver = list->data;
1271 if (driver->type == type) {
1272 DBG("technology %p driver %p", technology, driver);
1273 tech_drivers = g_slist_append(tech_drivers, driver);
1277 if (!tech_drivers) {
1278 DBG("No matching drivers found for %s.",
1279 __connman_service_type2string(type));
1283 technology = g_try_new0(struct connman_technology, 1);
1287 technology->refcount = 1;
1288 technology->type = type;
1289 technology->tethering_hidden = FALSE;
1290 technology->path = g_strdup_printf("%s/technology/%s",
1293 technology_load(technology);
1294 technology_list = g_slist_prepend(technology_list, technology);
1295 technology->driver_list = tech_drivers;
1297 for (list = tech_drivers; list; list = list->next) {
1298 driver = list->data;
1300 if (driver->probe && driver->probe(technology) < 0)
1301 DBG("Driver probe failed for technology %p",
1305 if (!technology_dbus_register(technology)) {
1306 technology_put(technology);
1310 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1311 struct connman_technology *wifi;
1314 enable = technology->enable_persistent;
1316 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
1318 enable = wifi->enabled;
1320 technology_affect_devices(technology, enable);
1323 DBG("technology %p %s", technology, get_name(technology->type));
1328 int connman_technology_driver_register(struct connman_technology_driver *driver)
1331 struct connman_device *device;
1332 enum connman_service_type type;
1334 for (list = driver_list; list; list = list->next) {
1335 if (list->data == driver)
1339 DBG("Registering %s driver", driver->name);
1341 driver_list = g_slist_insert_sorted(driver_list, driver,
1345 * Check for technology less devices if this driver
1346 * can service any of them.
1348 for (list = techless_device_list; list; list = list->next) {
1349 device = list->data;
1351 type = __connman_device_get_service_type(device);
1352 if (type != driver->type)
1355 techless_device_list = g_slist_remove(techless_device_list,
1358 __connman_technology_add_device(device);
1361 /* Check for orphaned rfkill switches. */
1362 g_hash_table_foreach(rfkill_list, rfkill_check,
1363 GINT_TO_POINTER(driver->type));
1366 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1367 if (!technology_get(CONNMAN_SERVICE_TYPE_P2P))
1374 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
1376 GSList *list, *tech_drivers;
1377 struct connman_technology *technology;
1378 struct connman_technology_driver *current;
1380 DBG("Unregistering driver %p name %s", driver, driver->name);
1382 for (list = technology_list; list; list = list->next) {
1383 technology = list->data;
1385 for (tech_drivers = technology->driver_list; tech_drivers;
1386 tech_drivers = g_slist_next(tech_drivers)) {
1387 current = tech_drivers->data;
1388 if (driver != current)
1392 driver->remove(technology);
1394 technology->driver_list =
1395 g_slist_remove(technology->driver_list,
1401 driver_list = g_slist_remove(driver_list, driver);
1403 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1404 technology = technology_find(CONNMAN_SERVICE_TYPE_P2P);
1406 technology_put(technology);
1410 void __connman_technology_add_interface(enum connman_service_type type,
1411 int index, const char *ident)
1413 struct connman_technology *technology;
1414 GSList *tech_drivers;
1415 struct connman_technology_driver *driver;
1419 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1420 case CONNMAN_SERVICE_TYPE_SYSTEM:
1422 case CONNMAN_SERVICE_TYPE_ETHERNET:
1423 case CONNMAN_SERVICE_TYPE_WIFI:
1424 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1425 case CONNMAN_SERVICE_TYPE_CELLULAR:
1426 case CONNMAN_SERVICE_TYPE_GPS:
1427 case CONNMAN_SERVICE_TYPE_VPN:
1428 case CONNMAN_SERVICE_TYPE_GADGET:
1429 case CONNMAN_SERVICE_TYPE_P2P:
1433 name = connman_inet_ifname(index);
1434 connman_info("Adding interface %s [ %s ]", name,
1435 __connman_service_type2string(type));
1437 technology = technology_find(type);
1442 for (tech_drivers = technology->driver_list; tech_drivers;
1443 tech_drivers = g_slist_next(tech_drivers)) {
1444 driver = tech_drivers->data;
1446 if (driver->add_interface)
1447 driver->add_interface(technology, index, name, ident);
1451 * At this point we can try to enable tethering automatically as
1452 * now the interfaces are set properly.
1454 if (technology->tethering_persistent)
1455 enable_tethering(technology);
1461 void __connman_technology_remove_interface(enum connman_service_type type,
1462 int index, const char *ident)
1464 struct connman_technology *technology;
1465 GSList *tech_drivers;
1466 struct connman_technology_driver *driver;
1470 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1471 case CONNMAN_SERVICE_TYPE_SYSTEM:
1473 case CONNMAN_SERVICE_TYPE_ETHERNET:
1474 case CONNMAN_SERVICE_TYPE_WIFI:
1475 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1476 case CONNMAN_SERVICE_TYPE_CELLULAR:
1477 case CONNMAN_SERVICE_TYPE_GPS:
1478 case CONNMAN_SERVICE_TYPE_VPN:
1479 case CONNMAN_SERVICE_TYPE_GADGET:
1480 case CONNMAN_SERVICE_TYPE_P2P:
1484 name = connman_inet_ifname(index);
1485 connman_info("Remove interface %s [ %s ]", name,
1486 __connman_service_type2string(type));
1489 technology = technology_find(type);
1494 for (tech_drivers = technology->driver_list; tech_drivers;
1495 tech_drivers = g_slist_next(tech_drivers)) {
1496 driver = tech_drivers->data;
1498 if (driver->remove_interface)
1499 driver->remove_interface(technology, index);
1503 int __connman_technology_add_device(struct connman_device *device)
1505 struct connman_technology *technology;
1506 enum connman_service_type type;
1508 type = __connman_device_get_service_type(device);
1510 DBG("device %p type %s", device, get_name(type));
1512 technology = technology_get(type);
1515 * Since no driver can be found for this device at the moment we
1516 * add it to the techless device list.
1518 techless_device_list = g_slist_prepend(techless_device_list,
1524 __sync_synchronize();
1525 if (technology->rfkill_driven) {
1526 if (technology->enabled)
1527 __connman_device_enable(device);
1529 __connman_device_disable(device);
1534 if (technology->enable_persistent &&
1535 !global_offlinemode) {
1536 int err = __connman_device_enable(device);
1538 * connman_technology_add_device() calls __connman_device_enable()
1539 * but since the device is already enabled, the calls does not
1540 * propagate through to connman_technology_enabled via
1541 * connman_device_set_powered.
1543 if (err == -EALREADY)
1544 __connman_technology_enabled(type);
1546 /* if technology persistent state is offline */
1547 if (!technology->enable_persistent)
1548 __connman_device_disable(device);
1551 technology->device_list = g_slist_prepend(technology->device_list,
1557 int __connman_technology_remove_device(struct connman_device *device)
1559 struct connman_technology *technology;
1560 enum connman_service_type type;
1562 DBG("device %p", device);
1564 type = __connman_device_get_service_type(device);
1566 technology = technology_find(type);
1568 techless_device_list = g_slist_remove(techless_device_list,
1573 technology->device_list = g_slist_remove(technology->device_list,
1576 if (technology->tethering)
1577 set_tethering(technology, false);
1579 technology_put(technology);
1584 int __connman_technology_enabled(enum connman_service_type type)
1586 struct connman_technology *technology;
1588 technology = technology_find(type);
1592 DBG("technology %p type %s rfkill %d enabled %d", technology,
1593 get_name(type), technology->rfkill_driven,
1594 technology->enabled);
1595 #if !defined TIZEN_EXT
1596 if (technology->rfkill_driven) {
1597 if (technology->tethering_persistent)
1598 enable_tethering(technology);
1603 return technology_enabled(technology);
1606 int __connman_technology_disabled(enum connman_service_type type)
1608 struct connman_technology *technology;
1611 technology = technology_find(type);
1614 #if !defined TIZEN_EXT
1615 if (technology->rfkill_driven)
1618 for (list = technology->device_list; list; list = list->next) {
1619 struct connman_device *device = list->data;
1621 if (connman_device_get_powered(device))
1625 return technology_disabled(technology);
1628 int __connman_technology_set_offlinemode(bool offlinemode)
1631 int err = -EINVAL, enabled_tech_count = 0;
1633 if (global_offlinemode == offlinemode)
1636 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1639 * This is a bit tricky. When you set offlinemode, there is no
1640 * way to differentiate between attempting offline mode and
1641 * resuming offlinemode from last saved profile. We need that
1642 * information in rfkill_update, otherwise it falls back on the
1643 * technology's persistent state. Hence we set the offline mode here
1644 * but save it & call the notifier only if its successful.
1647 global_offlinemode = offlinemode;
1649 /* Traverse technology list, enable/disable each technology. */
1650 for (list = technology_list; list; list = list->next) {
1651 struct connman_technology *technology = list->data;
1654 err = technology_disable(technology);
1656 if (technology->hardblocked)
1659 if (technology->enable_persistent) {
1660 err = technology_enable(technology);
1661 enabled_tech_count++;
1666 if (err == 0 || err == -EINPROGRESS || err == -EALREADY ||
1667 (err == -EINVAL && enabled_tech_count == 0)) {
1668 connman_technology_save_offlinemode();
1669 __connman_notifier_offlinemode(offlinemode);
1671 global_offlinemode = connman_technology_load_offlinemode();
1676 void __connman_technology_set_connected(enum connman_service_type type,
1679 struct connman_technology *technology;
1682 technology = technology_find(type);
1686 DBG("technology %p connected %d", technology, connected);
1688 technology->connected = connected;
1691 connman_dbus_property_changed_basic(technology->path,
1692 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1693 DBUS_TYPE_BOOLEAN, &val);
1696 static bool technology_apply_rfkill_change(struct connman_technology *technology,
1701 bool hardblock_changed = false;
1703 GList *start, *list;
1705 DBG("technology %p --> %d/%d vs %d/%d",
1706 technology, softblock, hardblock,
1707 technology->softblocked, technology->hardblocked);
1709 if (technology->hardblocked == hardblock)
1710 goto softblock_change;
1712 if (!(new_rfkill && !hardblock)) {
1713 start = g_hash_table_get_values(rfkill_list);
1715 for (list = start; list; list = list->next) {
1716 struct connman_rfkill *rfkill = list->data;
1718 if (rfkill->type != technology->type)
1721 if (rfkill->hardblock != hardblock)
1729 goto softblock_change;
1731 technology->hardblocked = hardblock;
1732 hardblock_changed = true;
1735 if (!apply && technology->softblocked != softblock)
1739 return technology->hardblocked;
1741 technology->softblocked = softblock;
1743 if (technology->hardblocked ||
1744 technology->softblocked) {
1745 if (technology_disabled(technology) != -EALREADY)
1746 technology_affect_devices(technology, false);
1747 } else if (!technology->hardblocked &&
1748 !technology->softblocked) {
1749 if (technology_enabled(technology) != -EALREADY)
1750 technology_affect_devices(technology, true);
1753 if (hardblock_changed) {
1754 if (technology->hardblocked) {
1755 DBG("%s is switched off.", get_name(technology->type));
1756 technology_dbus_unregister(technology);
1758 DBG("%s is switched on.", get_name(technology->type));
1759 technology_dbus_register(technology);
1761 if (global_offlinemode)
1762 __connman_rfkill_block(technology->type, true);
1766 return technology->hardblocked;
1769 int __connman_technology_add_rfkill(unsigned int index,
1770 enum connman_service_type type,
1774 struct connman_technology *technology;
1775 struct connman_rfkill *rfkill;
1777 DBG("index %u type %d soft %u hard %u", index, type,
1778 softblock, hardblock);
1780 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1784 rfkill = g_try_new0(struct connman_rfkill, 1);
1788 rfkill->index = index;
1789 rfkill->type = type;
1790 rfkill->softblock = softblock;
1791 rfkill->hardblock = hardblock;
1793 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1796 technology = technology_get(type);
1797 /* If there is no driver for this type, ignore it. */
1801 technology->rfkill_driven = true;
1803 #if !defined TIZEN_EXT
1804 /* If hardblocked, there is no need to handle softblocked state */
1805 if (technology_apply_rfkill_change(technology,
1806 softblock, hardblock, true))
1809 if (global_offlinemode)
1813 * Depending on softblocked state we unblock/block according to
1814 * offlinemode and persistente state.
1816 if (technology->softblocked &&
1817 technology->enable_persistent)
1818 return __connman_rfkill_block(type, false);
1819 else if (!technology->softblocked &&
1820 !technology->enable_persistent)
1821 return __connman_rfkill_block(type, true);
1826 int __connman_technology_update_rfkill(unsigned int index,
1827 enum connman_service_type type,
1831 struct connman_technology *technology;
1832 struct connman_rfkill *rfkill;
1834 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1836 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1840 if (rfkill->softblock == softblock &&
1841 rfkill->hardblock == hardblock)
1844 rfkill->softblock = softblock;
1845 rfkill->hardblock = hardblock;
1847 technology = technology_find(type);
1848 /* If there is no driver for this type, ignore it. */
1852 technology_apply_rfkill_change(technology, softblock, hardblock,
1855 if (technology->hardblocked)
1856 DBG("%s hardblocked", get_name(technology->type));
1858 DBG("%s is%s softblocked", get_name(technology->type),
1859 technology->softblocked ? "" : " not");
1864 int __connman_technology_remove_rfkill(unsigned int index,
1865 enum connman_service_type type)
1867 struct connman_technology *technology;
1868 struct connman_rfkill *rfkill;
1870 DBG("index %u", index);
1872 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1876 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1878 technology = technology_find(type);
1882 technology_apply_rfkill_change(technology,
1883 technology->softblocked, !technology->hardblocked, false);
1885 technology_put(technology);
1890 int __connman_technology_init(void)
1894 connection = connman_dbus_get_connection();
1896 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1899 global_offlinemode = connman_technology_load_offlinemode();
1901 /* This will create settings file if it is missing */
1902 connman_technology_save_offlinemode();
1907 void __connman_technology_cleanup(void)
1911 while (technology_list) {
1912 struct connman_technology *technology = technology_list->data;
1913 technology_list = g_slist_remove(technology_list, technology);
1914 technology_put(technology);
1917 g_hash_table_destroy(rfkill_list);
1919 dbus_connection_unref(connection);