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 #if defined TIZEN_EXT
677 DBG("ConnMan, Powered : %s, %s",
678 enabled ? "TRUE" : "FALSE",technology->path);
680 connman_dbus_property_changed_basic(technology->path,
681 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
682 DBUS_TYPE_BOOLEAN, &enabled);
685 static void enable_tethering(struct connman_technology *technology)
689 if (!connman_setting_get_bool("PersistentTetheringMode"))
692 ret = set_tethering(technology, true);
693 if (ret < 0 && ret != -EALREADY)
694 DBG("Cannot enable tethering yet for %s (%d/%s)",
695 get_name(technology->type),
696 -ret, strerror(-ret));
699 static int technology_enabled(struct connman_technology *technology)
701 __sync_synchronize();
702 if (technology->enabled)
705 technology->enabled = true;
707 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
708 struct connman_technology *p2p;
710 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
711 if (p2p && !p2p->enabled && p2p->enable_persistent)
712 technology_enabled(p2p);
715 if (technology->tethering_persistent)
716 enable_tethering(technology);
718 powered_changed(technology);
723 static int technology_enable(struct connman_technology *technology)
728 DBG("technology %p enable", technology);
730 __sync_synchronize();
732 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
733 struct connman_technology *wifi;
735 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
736 if (wifi && wifi->enabled)
737 return technology_enabled(technology);
741 if (technology->enabled)
744 if (technology->pending_reply)
747 if (connman_setting_get_bool("PersistentTetheringMode") &&
748 technology->tethering)
749 set_tethering(technology, true);
751 if (technology->rfkill_driven)
752 err = __connman_rfkill_block(technology->type, false);
754 err_dev = technology_affect_devices(technology, true);
756 if (!technology->rfkill_driven)
762 static int technology_disabled(struct connman_technology *technology)
764 __sync_synchronize();
765 if (!technology->enabled)
768 technology->enabled = false;
770 powered_changed(technology);
775 static int technology_disable(struct connman_technology *technology)
779 DBG("technology %p disable", technology);
781 __sync_synchronize();
783 if (technology->type == CONNMAN_SERVICE_TYPE_P2P) {
784 technology->enable_persistent = false;
785 return technology_disabled(technology);
786 } else if (technology->type == CONNMAN_SERVICE_TYPE_WIFI) {
787 struct connman_technology *p2p;
789 p2p = technology_find(CONNMAN_SERVICE_TYPE_P2P);
790 if (p2p && p2p->enabled) {
791 p2p->enable_persistent = true;
792 technology_disabled(p2p);
796 if (!technology->enabled)
799 if (technology->pending_reply)
802 if (technology->tethering)
803 set_tethering(technology, false);
805 err = technology_affect_devices(technology, false);
807 if (technology->rfkill_driven)
808 err = __connman_rfkill_block(technology->type, true);
813 static DBusMessage *set_powered(struct connman_technology *technology,
814 DBusMessage *msg, bool powered)
816 DBusMessage *reply = NULL;
819 if (technology->rfkill_driven && technology->hardblocked) {
825 err = technology_enable(technology);
827 err = technology_disable(technology);
830 technology->enable_persistent = powered;
831 technology_save(technology);
835 if (err == -EINPROGRESS) {
836 technology->pending_reply = dbus_message_ref(msg);
837 technology->pending_timeout = g_timeout_add_seconds(10,
838 technology_pending_reply, technology);
839 } else if (err == -EALREADY) {
841 reply = __connman_error_already_enabled(msg);
843 reply = __connman_error_already_disabled(msg);
845 reply = __connman_error_failed(msg, -err);
847 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
852 static DBusMessage *set_property(DBusConnection *conn,
853 DBusMessage *msg, void *data)
855 struct connman_technology *technology = data;
856 DBusMessageIter iter, value;
860 DBG("conn %p", conn);
862 if (!dbus_message_iter_init(msg, &iter))
863 return __connman_error_invalid_arguments(msg);
865 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
866 return __connman_error_invalid_arguments(msg);
868 dbus_message_iter_get_basic(&iter, &name);
869 dbus_message_iter_next(&iter);
871 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
872 return __connman_error_invalid_arguments(msg);
874 dbus_message_iter_recurse(&iter, &value);
876 type = dbus_message_iter_get_arg_type(&value);
878 DBG("property %s", name);
880 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI && technology->connected) {
882 if (connman_dbus_get_connection_unix_user_sync(conn,
883 dbus_message_get_sender(msg),
885 DBG("Can not get unix user id!");
886 return __connman_error_permission_denied(msg);
889 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
890 DBG("Not allow this user to operate wifi technology now!");
891 return __connman_error_permission_denied(msg);
895 if (g_str_equal(name, "Tethering")) {
896 dbus_bool_t tethering;
899 if (type != DBUS_TYPE_BOOLEAN)
900 return __connman_error_invalid_arguments(msg);
902 if (!connman_technology_is_tethering_allowed(technology->type)) {
903 DBG("%s tethering not allowed by config file",
904 __connman_service_type2string(technology->type));
905 return __connman_error_not_supported(msg);
908 dbus_message_iter_get_basic(&value, &tethering);
910 if (technology->tethering == tethering) {
912 return __connman_error_already_disabled(msg);
914 return __connman_error_already_enabled(msg);
917 err = set_tethering(technology, tethering);
919 return __connman_error_failed(msg, -err);
921 technology->tethering_persistent = tethering;
923 technology_save(technology);
925 } else if (g_str_equal(name, "TetheringIdentifier")) {
928 dbus_message_iter_get_basic(&value, &str);
930 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
931 return __connman_error_not_supported(msg);
933 if (strlen(str) < 1 || strlen(str) > 32)
934 return __connman_error_invalid_arguments(msg);
936 if (g_strcmp0(technology->tethering_ident, str) != 0) {
937 g_free(technology->tethering_ident);
938 technology->tethering_ident = g_strdup(str);
939 technology_save(technology);
941 connman_dbus_property_changed_basic(technology->path,
942 CONNMAN_TECHNOLOGY_INTERFACE,
943 "TetheringIdentifier",
945 &technology->tethering_ident);
947 } else if (g_str_equal(name, "TetheringPassphrase")) {
950 dbus_message_iter_get_basic(&value, &str);
952 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
953 return __connman_error_not_supported(msg);
955 if (strlen(str) < 8 || strlen(str) > 63) {
956 if (g_str_equal(str, "")) {
957 technology->tethering_passphrase = NULL;
959 connman_dbus_property_changed_basic(technology->path,
960 CONNMAN_TECHNOLOGY_INTERFACE,
961 "TetheringPassphrase",
966 return __connman_error_passphrase_required(msg);
968 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
969 g_free(technology->tethering_passphrase);
970 technology->tethering_passphrase = g_strdup(str);
971 technology_save(technology);
973 connman_dbus_property_changed_basic(technology->path,
974 CONNMAN_TECHNOLOGY_INTERFACE,
975 "TetheringPassphrase",
977 &technology->tethering_passphrase);
980 } else if (g_str_equal(name, "Hidden")) {
983 if (type != DBUS_TYPE_BOOLEAN)
984 return __connman_error_invalid_arguments(msg);
986 dbus_message_iter_get_basic(&value, &hidden);
988 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
989 return __connman_error_not_supported(msg);
991 technology->tethering_hidden = hidden;
992 technology_save(technology);
994 connman_dbus_property_changed_basic(technology->path,
995 CONNMAN_TECHNOLOGY_INTERFACE,
999 } else if (g_str_equal(name, "Powered")) {
1002 if (type != DBUS_TYPE_BOOLEAN)
1003 return __connman_error_invalid_arguments(msg);
1005 dbus_message_iter_get_basic(&value, &enable);
1007 return set_powered(technology, msg, enable);
1009 return __connman_error_invalid_property(msg);
1011 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1014 static void reply_scan_pending(struct connman_technology *technology, int err)
1018 DBG("technology %p err %d", technology, err);
1020 while (technology->scan_pending) {
1021 DBusMessage *msg = technology->scan_pending->data;
1023 DBG("reply to %s", dbus_message_get_sender(msg));
1026 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1028 reply = __connman_error_failed(msg, -err);
1029 g_dbus_send_message(connection, reply);
1030 dbus_message_unref(msg);
1032 technology->scan_pending =
1033 g_slist_delete_link(technology->scan_pending,
1034 technology->scan_pending);
1038 #if defined TIZEN_EXT
1039 dbus_bool_t __connman_technology_notify_scan_changed(const char *key, void *val)
1042 DBusMessage *signal;
1043 DBusMessageIter iter;
1044 dbus_bool_t result = FALSE;
1046 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1047 CONNMAN_MANAGER_INTERFACE, "ScanChanged");
1051 dbus_message_iter_init_append(signal, &iter);
1052 connman_dbus_property_append_basic(&iter, key, DBUS_TYPE_BOOLEAN, val);
1054 result = dbus_connection_send(connection, signal, NULL);
1055 dbus_message_unref(signal);
1057 DBG("Successfuly sent signal");
1063 void __connman_technology_scan_started(struct connman_device *device)
1065 DBG("device %p", device);
1066 #if defined TIZEN_EXT
1067 dbus_bool_t status = 1;
1068 __connman_technology_notify_scan_changed("scan_started", &status);
1072 void __connman_technology_scan_stopped(struct connman_device *device,
1073 enum connman_service_type type)
1076 struct connman_technology *technology;
1079 technology = technology_find(type);
1081 DBG("technology %p device %p", technology, device);
1086 for (list = technology->device_list; list; list = list->next) {
1087 struct connman_device *other_device = list->data;
1089 if (device == other_device)
1092 if (__connman_device_get_service_type(other_device) != type)
1095 if (connman_device_get_scanning(other_device))
1099 #if defined TIZEN_EXT
1101 DBusMessage *signal;
1102 dbus_bool_t status = 0;
1103 __connman_technology_notify_scan_changed("scan_done", &status);
1105 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1106 CONNMAN_MANAGER_INTERFACE, "ScanDone");
1110 dbus_connection_send(connection, signal, NULL);
1111 dbus_message_unref(signal);
1112 reply_scan_pending(technology, 0);
1116 reply_scan_pending(technology, 0);
1120 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
1121 int result, const char *alpha2)
1123 bool regdom_set = false;
1124 struct connman_technology *technology;
1125 enum connman_service_type type;
1126 GSList *tech_drivers;
1128 type = __connman_device_get_service_type(device);
1129 technology = technology_find(type);
1136 for (tech_drivers = technology->driver_list;
1138 tech_drivers = g_slist_next(tech_drivers)) {
1139 struct connman_technology_driver *driver =
1142 if (driver->set_regdom) {
1143 driver->set_regdom(technology, alpha2);
1153 connman_technology_regdom_notify(technology, alpha2);
1156 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
1158 struct connman_technology *technology = data;
1161 DBG("technology %p request from %s", technology,
1162 dbus_message_get_sender(msg));
1164 dbus_message_ref(msg);
1165 technology->scan_pending =
1166 g_slist_prepend(technology->scan_pending, msg);
1168 err = __connman_device_request_scan(technology->type);
1170 reply_scan_pending(technology, err);
1175 #if defined TIZEN_EXT
1176 static DBusMessage *specific_scan(DBusConnection *conn, DBusMessage *msg, void *data)
1178 struct connman_technology *technology = data;
1179 GSList *specific_scan_list = NULL;
1181 const char *name = NULL;
1182 unsigned int freq = 0;
1183 DBusMessageIter iter, dict;
1186 DBG("technology %p request from %s", technology,
1187 dbus_message_get_sender(msg));
1189 if (!dbus_message_iter_init(msg, &iter))
1190 return __connman_error_invalid_arguments(msg);
1192 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
1193 return __connman_error_invalid_arguments(msg);
1195 dbus_message_iter_recurse(&iter, &dict);
1196 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1197 DBusMessageIter entry, value2;
1201 dbus_message_iter_recurse(&dict, &entry);
1202 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1203 return __connman_error_invalid_arguments(msg);
1205 dbus_message_iter_get_basic(&entry, &key);
1206 dbus_message_iter_next(&entry);
1208 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
1209 return __connman_error_invalid_arguments(msg);
1211 dbus_message_iter_recurse(&entry, &value2);
1212 type = dbus_message_iter_get_arg_type(&value2);
1213 if (g_str_equal(key, "SSID")) {
1214 if (type != DBUS_TYPE_STRING)
1215 return __connman_error_invalid_arguments(msg);
1217 scan_type = 1; /* SSID based scan */
1218 dbus_message_iter_get_basic(&value2, &name);
1219 DBG("name %s", name);
1220 specific_scan_list = g_slist_append(specific_scan_list, g_strdup(name));
1221 } else if (g_str_equal(key, "Frequency")) {
1222 if (type != DBUS_TYPE_UINT16) {
1223 g_slist_free_full(specific_scan_list, g_free);
1224 return __connman_error_invalid_arguments(msg);
1227 scan_type = 2; /* Frequency based scan */
1228 dbus_message_iter_get_basic(&value2, &freq);
1229 DBG("freq %d", freq);
1230 specific_scan_list = g_slist_append(specific_scan_list, GINT_TO_POINTER(freq));
1232 dbus_message_iter_next(&dict);
1235 dbus_message_ref(msg);
1236 technology->scan_pending =
1237 g_slist_prepend(technology->scan_pending, msg);
1239 err = __connman_device_request_specific_scan(technology->type, scan_type, specific_scan_list);
1241 reply_scan_pending(technology, err);
1243 g_slist_free_full(specific_scan_list, g_free);
1247 static DBusMessage *get_scan_state(DBusConnection *conn, DBusMessage *msg, void *data)
1250 DBusMessageIter iter, dict;
1252 struct connman_technology *technology = data;
1253 dbus_bool_t scanning = false;
1255 DBG("technology %p", technology);
1257 for (list = technology->device_list; list; list = list->next) {
1258 struct connman_device *device = list->data;
1259 scanning = connman_device_get_scanning(device);
1264 DBG("scanning : %d", scanning);
1265 reply = dbus_message_new_method_return(msg);
1269 dbus_message_iter_init_append(reply, &iter);
1271 connman_dbus_dict_open(&iter, &dict);
1272 connman_dbus_dict_append_basic(&dict, "Scanstate",
1276 connman_dbus_dict_close(&iter, &dict);
1282 static const GDBusMethodTable technology_methods[] = {
1283 { GDBUS_DEPRECATED_METHOD("GetProperties",
1284 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
1286 { GDBUS_ASYNC_METHOD("SetProperty",
1287 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1288 NULL, set_property) },
1289 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
1290 { GDBUS_ASYNC_METHOD("SpecificScan", GDBUS_ARGS({ "specificscan", "a{sv}" }),
1291 NULL, specific_scan) },
1292 #if defined TIZEN_EXT
1293 { GDBUS_METHOD("GetScanState", NULL, GDBUS_ARGS({ "scan_state", "a{sv}" }),
1299 static const GDBusSignalTable technology_signals[] = {
1300 { GDBUS_SIGNAL("PropertyChanged",
1301 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
1302 { GDBUS_SIGNAL("DhcpConnected",
1303 GDBUS_ARGS({ "aptype", "s" },
1306 { "hostname", "s" })) },
1307 { GDBUS_SIGNAL("DhcpLeaseDeleted",
1308 GDBUS_ARGS({ "aptype", "s" },
1311 { "hostname", "s" })) },
1315 static bool technology_dbus_register(struct connman_technology *technology)
1317 if (technology->dbus_registered ||
1318 (technology->rfkill_driven &&
1319 technology->hardblocked))
1322 if (!g_dbus_register_interface(connection, technology->path,
1323 CONNMAN_TECHNOLOGY_INTERFACE,
1324 technology_methods, technology_signals,
1325 NULL, technology, NULL)) {
1326 connman_error("Failed to register %s", technology->path);
1330 technology_added_signal(technology);
1331 technology->dbus_registered = true;
1336 static void technology_dbus_unregister(struct connman_technology *technology)
1338 if (!technology->dbus_registered)
1341 technology_removed_signal(technology);
1342 g_dbus_unregister_interface(connection, technology->path,
1343 CONNMAN_TECHNOLOGY_INTERFACE);
1345 technology->dbus_registered = false;
1348 static void technology_put(struct connman_technology *technology)
1350 DBG("technology %p", technology);
1352 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1355 reply_scan_pending(technology, -EINTR);
1357 while (technology->driver_list) {
1358 struct connman_technology_driver *driver;
1360 driver = technology->driver_list->data;
1363 driver->remove(technology);
1365 technology->driver_list =
1366 g_slist_delete_link(technology->driver_list,
1367 technology->driver_list);
1370 technology_list = g_slist_remove(technology_list, technology);
1372 technology_dbus_unregister(technology);
1374 g_slist_free(technology->device_list);
1376 g_free(technology->path);
1377 g_free(technology->regdom);
1378 g_free(technology->tethering_ident);
1379 g_free(technology->tethering_passphrase);
1383 static struct connman_technology *technology_get(enum connman_service_type type)
1385 GSList *tech_drivers = NULL;
1386 struct connman_technology_driver *driver;
1387 struct connman_technology *technology;
1391 DBG("type %d", type);
1393 str = __connman_service_type2string(type);
1397 technology = technology_find(type);
1399 if (type != CONNMAN_SERVICE_TYPE_P2P)
1400 __sync_fetch_and_add(&technology->refcount, 1);
1404 /* First check if we have a driver for this technology type */
1405 for (list = driver_list; list; list = list->next) {
1406 driver = list->data;
1408 if (driver->type == type) {
1409 DBG("technology %p driver %p", technology, driver);
1410 tech_drivers = g_slist_append(tech_drivers, driver);
1414 if (!tech_drivers) {
1415 DBG("No matching drivers found for %s.",
1416 __connman_service_type2string(type));
1420 technology = g_try_new0(struct connman_technology, 1);
1424 technology->refcount = 1;
1425 technology->type = type;
1426 technology->tethering_hidden = FALSE;
1427 technology->path = g_strdup_printf("%s/technology/%s",
1430 technology_load(technology);
1431 technology_list = g_slist_prepend(technology_list, technology);
1432 technology->driver_list = tech_drivers;
1434 for (list = tech_drivers; list; list = list->next) {
1435 driver = list->data;
1437 if (driver->probe && driver->probe(technology) < 0)
1438 DBG("Driver probe failed for technology %p",
1442 if (!technology_dbus_register(technology)) {
1443 technology_put(technology);
1447 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1448 struct connman_technology *wifi;
1451 enable = technology->enable_persistent;
1453 wifi = technology_find(CONNMAN_SERVICE_TYPE_WIFI);
1455 enable = wifi->enabled;
1457 technology_affect_devices(technology, enable);
1460 DBG("technology %p %s", technology, get_name(technology->type));
1465 int connman_technology_driver_register(struct connman_technology_driver *driver)
1468 struct connman_device *device;
1469 enum connman_service_type type;
1471 for (list = driver_list; list; list = list->next) {
1472 if (list->data == driver)
1476 DBG("Registering %s driver", driver->name);
1478 driver_list = g_slist_insert_sorted(driver_list, driver,
1482 * Check for technology less devices if this driver
1483 * can service any of them.
1485 for (list = techless_device_list; list; list = list->next) {
1486 device = list->data;
1488 type = __connman_device_get_service_type(device);
1489 if (type != driver->type)
1492 techless_device_list = g_slist_remove(techless_device_list,
1495 __connman_technology_add_device(device);
1498 /* Check for orphaned rfkill switches. */
1499 g_hash_table_foreach(rfkill_list, rfkill_check,
1500 GINT_TO_POINTER(driver->type));
1503 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1504 if (!technology_get(CONNMAN_SERVICE_TYPE_P2P))
1511 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
1513 GSList *list, *tech_drivers;
1514 struct connman_technology *technology;
1515 struct connman_technology_driver *current;
1517 DBG("Unregistering driver %p name %s", driver, driver->name);
1519 for (list = technology_list; list; list = list->next) {
1520 technology = list->data;
1522 for (tech_drivers = technology->driver_list; tech_drivers;
1523 tech_drivers = g_slist_next(tech_drivers)) {
1524 current = tech_drivers->data;
1525 if (driver != current)
1529 driver->remove(technology);
1531 technology->driver_list =
1532 g_slist_remove(technology->driver_list,
1538 driver_list = g_slist_remove(driver_list, driver);
1540 if (driver->type == CONNMAN_SERVICE_TYPE_P2P) {
1541 technology = technology_find(CONNMAN_SERVICE_TYPE_P2P);
1543 technology_put(technology);
1547 void __connman_technology_add_interface(enum connman_service_type type,
1548 int index, const char *ident)
1550 struct connman_technology *technology;
1551 GSList *tech_drivers;
1552 struct connman_technology_driver *driver;
1556 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1557 case CONNMAN_SERVICE_TYPE_SYSTEM:
1559 case CONNMAN_SERVICE_TYPE_ETHERNET:
1560 case CONNMAN_SERVICE_TYPE_WIFI:
1561 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1562 case CONNMAN_SERVICE_TYPE_CELLULAR:
1563 case CONNMAN_SERVICE_TYPE_GPS:
1564 case CONNMAN_SERVICE_TYPE_VPN:
1565 case CONNMAN_SERVICE_TYPE_GADGET:
1566 case CONNMAN_SERVICE_TYPE_P2P:
1570 name = connman_inet_ifname(index);
1571 connman_info("Adding interface %s [ %s ]", name,
1572 __connman_service_type2string(type));
1574 technology = technology_find(type);
1579 for (tech_drivers = technology->driver_list; tech_drivers;
1580 tech_drivers = g_slist_next(tech_drivers)) {
1581 driver = tech_drivers->data;
1583 if (driver->add_interface)
1584 driver->add_interface(technology, index, name, ident);
1588 * At this point we can try to enable tethering automatically as
1589 * now the interfaces are set properly.
1591 if (technology->tethering_persistent)
1592 enable_tethering(technology);
1598 void __connman_technology_remove_interface(enum connman_service_type type,
1599 int index, const char *ident)
1601 struct connman_technology *technology;
1602 GSList *tech_drivers;
1603 struct connman_technology_driver *driver;
1607 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1608 case CONNMAN_SERVICE_TYPE_SYSTEM:
1610 case CONNMAN_SERVICE_TYPE_ETHERNET:
1611 case CONNMAN_SERVICE_TYPE_WIFI:
1612 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1613 case CONNMAN_SERVICE_TYPE_CELLULAR:
1614 case CONNMAN_SERVICE_TYPE_GPS:
1615 case CONNMAN_SERVICE_TYPE_VPN:
1616 case CONNMAN_SERVICE_TYPE_GADGET:
1617 case CONNMAN_SERVICE_TYPE_P2P:
1621 name = connman_inet_ifname(index);
1622 connman_info("Remove interface %s [ %s ]", name,
1623 __connman_service_type2string(type));
1626 technology = technology_find(type);
1631 for (tech_drivers = technology->driver_list; tech_drivers;
1632 tech_drivers = g_slist_next(tech_drivers)) {
1633 driver = tech_drivers->data;
1635 if (driver->remove_interface)
1636 driver->remove_interface(technology, index);
1640 int __connman_technology_add_device(struct connman_device *device)
1642 struct connman_technology *technology;
1643 enum connman_service_type type;
1645 type = __connman_device_get_service_type(device);
1647 DBG("device %p type %s", device, get_name(type));
1649 technology = technology_get(type);
1652 * Since no driver can be found for this device at the moment we
1653 * add it to the techless device list.
1655 techless_device_list = g_slist_prepend(techless_device_list,
1661 __sync_synchronize();
1662 if (technology->rfkill_driven) {
1663 if (technology->enabled)
1664 __connman_device_enable(device);
1666 __connman_device_disable(device);
1671 if (technology->enable_persistent &&
1672 !global_offlinemode) {
1673 int err = __connman_device_enable(device);
1675 * connman_technology_add_device() calls __connman_device_enable()
1676 * but since the device is already enabled, the calls does not
1677 * propagate through to connman_technology_enabled via
1678 * connman_device_set_powered.
1680 if (err == -EALREADY)
1681 __connman_technology_enabled(type);
1683 /* if technology persistent state is offline */
1684 if (!technology->enable_persistent)
1685 __connman_device_disable(device);
1688 technology->device_list = g_slist_prepend(technology->device_list,
1694 int __connman_technology_remove_device(struct connman_device *device)
1696 struct connman_technology *technology;
1697 enum connman_service_type type;
1699 DBG("device %p", device);
1701 type = __connman_device_get_service_type(device);
1703 technology = technology_find(type);
1705 techless_device_list = g_slist_remove(techless_device_list,
1710 technology->device_list = g_slist_remove(technology->device_list,
1713 if (technology->tethering)
1714 set_tethering(technology, false);
1716 technology_put(technology);
1721 int __connman_technology_enabled(enum connman_service_type type)
1723 struct connman_technology *technology;
1725 technology = technology_find(type);
1729 DBG("technology %p type %s rfkill %d enabled %d", technology,
1730 get_name(type), technology->rfkill_driven,
1731 technology->enabled);
1732 #if !defined TIZEN_EXT
1733 if (technology->rfkill_driven) {
1734 if (technology->tethering_persistent)
1735 enable_tethering(technology);
1740 return technology_enabled(technology);
1743 int __connman_technology_disabled(enum connman_service_type type)
1745 struct connman_technology *technology;
1748 technology = technology_find(type);
1751 #if !defined TIZEN_EXT
1752 if (technology->rfkill_driven)
1755 for (list = technology->device_list; list; list = list->next) {
1756 struct connman_device *device = list->data;
1758 if (connman_device_get_powered(device))
1762 return technology_disabled(technology);
1765 int __connman_technology_set_offlinemode(bool offlinemode)
1768 int err = -EINVAL, enabled_tech_count = 0;
1770 if (global_offlinemode == offlinemode)
1773 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1776 * This is a bit tricky. When you set offlinemode, there is no
1777 * way to differentiate between attempting offline mode and
1778 * resuming offlinemode from last saved profile. We need that
1779 * information in rfkill_update, otherwise it falls back on the
1780 * technology's persistent state. Hence we set the offline mode here
1781 * but save it & call the notifier only if its successful.
1784 global_offlinemode = offlinemode;
1786 /* Traverse technology list, enable/disable each technology. */
1787 for (list = technology_list; list; list = list->next) {
1788 struct connman_technology *technology = list->data;
1791 err = technology_disable(technology);
1793 if (technology->hardblocked)
1796 if (technology->enable_persistent) {
1797 err = technology_enable(technology);
1798 enabled_tech_count++;
1803 if (err == 0 || err == -EINPROGRESS || err == -EALREADY ||
1804 (err == -EINVAL && enabled_tech_count == 0)) {
1805 connman_technology_save_offlinemode();
1806 __connman_notifier_offlinemode(offlinemode);
1808 global_offlinemode = connman_technology_load_offlinemode();
1813 void __connman_technology_set_connected(enum connman_service_type type,
1816 struct connman_technology *technology;
1819 technology = technology_find(type);
1823 DBG("technology %p connected %d", technology, connected);
1825 technology->connected = connected;
1828 connman_dbus_property_changed_basic(technology->path,
1829 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1830 DBUS_TYPE_BOOLEAN, &val);
1833 static bool technology_apply_rfkill_change(struct connman_technology *technology,
1838 bool hardblock_changed = false;
1840 GList *start, *list;
1842 DBG("technology %p --> %d/%d vs %d/%d",
1843 technology, softblock, hardblock,
1844 technology->softblocked, technology->hardblocked);
1846 if (technology->hardblocked == hardblock)
1847 goto softblock_change;
1849 if (!(new_rfkill && !hardblock)) {
1850 start = g_hash_table_get_values(rfkill_list);
1852 for (list = start; list; list = list->next) {
1853 struct connman_rfkill *rfkill = list->data;
1855 if (rfkill->type != technology->type)
1858 if (rfkill->hardblock != hardblock)
1866 goto softblock_change;
1868 technology->hardblocked = hardblock;
1869 hardblock_changed = true;
1872 if (!apply && technology->softblocked != softblock)
1876 return technology->hardblocked;
1878 technology->softblocked = softblock;
1880 if (technology->hardblocked ||
1881 technology->softblocked) {
1882 if (technology_disabled(technology) != -EALREADY)
1883 technology_affect_devices(technology, false);
1884 } else if (!technology->hardblocked &&
1885 !technology->softblocked) {
1886 if (technology_enabled(technology) != -EALREADY)
1887 technology_affect_devices(technology, true);
1890 if (hardblock_changed) {
1891 if (technology->hardblocked) {
1892 DBG("%s is switched off.", get_name(technology->type));
1893 technology_dbus_unregister(technology);
1895 DBG("%s is switched on.", get_name(technology->type));
1896 technology_dbus_register(technology);
1898 if (global_offlinemode)
1899 __connman_rfkill_block(technology->type, true);
1903 return technology->hardblocked;
1906 int __connman_technology_add_rfkill(unsigned int index,
1907 enum connman_service_type type,
1911 struct connman_technology *technology;
1912 struct connman_rfkill *rfkill;
1914 DBG("index %u type %d soft %u hard %u", index, type,
1915 softblock, hardblock);
1917 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1921 rfkill = g_try_new0(struct connman_rfkill, 1);
1925 rfkill->index = index;
1926 rfkill->type = type;
1927 rfkill->softblock = softblock;
1928 rfkill->hardblock = hardblock;
1930 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1933 #if defined TIZEN_EXT
1934 /* Fix Svace Issue [WGID: 1348]. */
1937 technology = technology_get(type);
1938 /* If there is no driver for this type, ignore it. */
1942 technology->rfkill_driven = true;
1944 #if !defined TIZEN_EXT
1945 /* If hardblocked, there is no need to handle softblocked state */
1946 if (technology_apply_rfkill_change(technology,
1947 softblock, hardblock, true))
1950 if (global_offlinemode)
1954 * Depending on softblocked state we unblock/block according to
1955 * offlinemode and persistente state.
1957 if (technology->softblocked &&
1958 technology->enable_persistent)
1959 return __connman_rfkill_block(type, false);
1960 else if (!technology->softblocked &&
1961 !technology->enable_persistent)
1962 return __connman_rfkill_block(type, true);
1967 int __connman_technology_update_rfkill(unsigned int index,
1968 enum connman_service_type type,
1972 struct connman_technology *technology;
1973 struct connman_rfkill *rfkill;
1975 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1977 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1981 if (rfkill->softblock == softblock &&
1982 rfkill->hardblock == hardblock)
1985 rfkill->softblock = softblock;
1986 rfkill->hardblock = hardblock;
1988 technology = technology_find(type);
1989 /* If there is no driver for this type, ignore it. */
1993 technology_apply_rfkill_change(technology, softblock, hardblock,
1996 if (technology->hardblocked)
1997 DBG("%s hardblocked", get_name(technology->type));
1999 DBG("%s is%s softblocked", get_name(technology->type),
2000 technology->softblocked ? "" : " not");
2005 int __connman_technology_remove_rfkill(unsigned int index,
2006 enum connman_service_type type)
2008 struct connman_technology *technology;
2009 struct connman_rfkill *rfkill;
2011 DBG("index %u", index);
2013 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
2017 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
2019 technology = technology_find(type);
2023 technology_apply_rfkill_change(technology,
2024 technology->softblocked, !technology->hardblocked, false);
2026 technology_put(technology);
2031 int __connman_technology_init(void)
2035 connection = connman_dbus_get_connection();
2037 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2040 global_offlinemode = connman_technology_load_offlinemode();
2042 /* This will create settings file if it is missing */
2043 connman_technology_save_offlinemode();
2048 void __connman_technology_cleanup(void)
2052 while (technology_list) {
2053 struct connman_technology *technology = technology_list->data;
2054 technology_list = g_slist_remove(technology_list, technology);
2055 technology_put(technology);
2058 g_hash_table_destroy(rfkill_list);
2060 dbus_connection_unref(connection);