5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection;
35 static GSList *technology_list = NULL;
38 * List of devices with no technology associated with them either because of
39 * no compiled in support or the driver is not yet loaded.
41 static GSList *techless_device_list = NULL;
42 static GHashTable *rfkill_list;
44 static connman_bool_t global_offlinemode;
46 struct connman_rfkill {
48 enum connman_service_type type;
49 connman_bool_t softblock;
50 connman_bool_t hardblock;
53 struct connman_technology {
55 enum connman_service_type type;
58 connman_bool_t enabled;
60 connman_bool_t connected;
62 connman_bool_t tethering;
63 char *tethering_ident;
64 char *tethering_passphrase;
66 connman_bool_t enable_persistent; /* Save the tech state */
68 struct connman_technology_driver *driver;
71 DBusMessage *pending_reply;
72 guint pending_timeout;
76 connman_bool_t rfkill_driven;
77 connman_bool_t softblocked;
78 connman_bool_t hardblocked;
79 connman_bool_t dbus_registered;
82 static GSList *driver_list = NULL;
84 static gint compare_priority(gconstpointer a, gconstpointer b)
86 const struct connman_technology_driver *driver1 = a;
87 const struct connman_technology_driver *driver2 = b;
89 return driver2->priority - driver1->priority;
92 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
94 struct connman_rfkill *rfkill = value;
95 enum connman_service_type type = GPOINTER_TO_INT(user_data);
97 /* Calling _technology_rfkill_add will update the tech. */
98 if (rfkill->type == type)
99 __connman_technology_add_rfkill(rfkill->index, type,
100 rfkill->softblock, rfkill->hardblock);
104 * connman_technology_driver_register:
105 * @driver: Technology driver definition
107 * Register a new technology driver
109 * Returns: %0 on success
111 int connman_technology_driver_register(struct connman_technology_driver *driver)
114 struct connman_device *device;
115 enum connman_service_type type;
117 DBG("Registering %s driver", driver->name);
119 driver_list = g_slist_insert_sorted(driver_list, driver,
123 * Check for technology less devices if this driver
124 * can service any of them.
126 for (list = techless_device_list; list != NULL; list = list->next) {
129 type = __connman_device_get_service_type(device);
130 if (type != driver->type)
133 techless_device_list = g_slist_remove(techless_device_list,
136 __connman_technology_add_device(device);
139 /* Check for orphaned rfkill switches. */
140 g_hash_table_foreach(rfkill_list, rfkill_check,
141 GINT_TO_POINTER(driver->type));
147 * connman_technology_driver_unregister:
148 * @driver: Technology driver definition
150 * Remove a previously registered technology driver
152 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
155 struct connman_technology *technology;
157 DBG("Unregistering driver %p name %s", driver, driver->name);
159 for (list = technology_list; list; list = list->next) {
160 technology = list->data;
162 if (technology->driver == NULL)
165 if (technology->type == driver->type) {
166 technology->driver->remove(technology);
167 technology->driver = NULL;
171 driver_list = g_slist_remove(driver_list, driver);
174 static void tethering_changed(struct connman_technology *technology)
176 connman_bool_t tethering = technology->tethering;
178 connman_dbus_property_changed_basic(technology->path,
179 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
180 DBUS_TYPE_BOOLEAN, &tethering);
183 void connman_technology_tethering_notify(struct connman_technology *technology,
184 connman_bool_t enabled)
188 DBG("technology %p enabled %u", technology, enabled);
190 if (technology->tethering == enabled)
193 technology->tethering = enabled;
195 tethering_changed(technology);
198 __connman_tethering_set_enabled();
200 for (list = technology_list; list; list = list->next) {
201 struct connman_technology *other_tech = list->data;
202 if (other_tech->tethering == TRUE)
206 __connman_tethering_set_disabled();
210 static int set_tethering(struct connman_technology *technology,
211 connman_bool_t enabled)
213 const char *ident, *passphrase, *bridge;
215 ident = technology->tethering_ident;
216 passphrase = technology->tethering_passphrase;
218 if (technology->driver == NULL ||
219 technology->driver->set_tethering == NULL)
222 __sync_synchronize();
223 if (technology->enabled == FALSE)
226 bridge = __connman_tethering_get_bridge();
230 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
231 (ident == NULL || passphrase == NULL))
234 return technology->driver->set_tethering(technology, ident, passphrase,
238 void connman_technology_regdom_notify(struct connman_technology *technology,
244 connman_error("Failed to set regulatory domain");
246 DBG("Regulatory domain set to %s", alpha2);
248 g_free(technology->regdom);
249 technology->regdom = g_strdup(alpha2);
252 static int set_regdom_by_device(struct connman_technology *technology,
257 for (list = technology->device_list; list; list = list->next) {
258 struct connman_device *device = list->data;
260 if (connman_device_set_regdom(device, alpha2) != 0)
267 int connman_technology_set_regdom(const char *alpha2)
271 for (list = technology_list; list; list = list->next) {
272 struct connman_technology *technology = list->data;
274 if (set_regdom_by_device(technology, alpha2) != 0) {
275 if (technology->driver == NULL)
278 if (technology->driver->set_regdom != NULL)
279 technology->driver->set_regdom(technology,
287 static void free_rfkill(gpointer data)
289 struct connman_rfkill *rfkill = data;
294 static const char *get_name(enum connman_service_type type)
297 case CONNMAN_SERVICE_TYPE_UNKNOWN:
298 case CONNMAN_SERVICE_TYPE_SYSTEM:
299 case CONNMAN_SERVICE_TYPE_GPS:
300 case CONNMAN_SERVICE_TYPE_VPN:
301 case CONNMAN_SERVICE_TYPE_GADGET:
303 case CONNMAN_SERVICE_TYPE_ETHERNET:
305 case CONNMAN_SERVICE_TYPE_WIFI:
307 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
309 case CONNMAN_SERVICE_TYPE_CELLULAR:
316 static void technology_save(struct connman_technology *technology)
321 DBG("technology %p", technology);
323 keyfile = __connman_storage_load_global();
325 keyfile = g_key_file_new();
327 identifier = g_strdup_printf("%s", get_name(technology->type));
328 if (identifier == NULL)
331 g_key_file_set_boolean(keyfile, identifier, "Enable",
332 technology->enable_persistent);
334 if (technology->tethering_ident != NULL)
335 g_key_file_set_string(keyfile, identifier,
336 "Tethering.Identifier",
337 technology->tethering_ident);
339 if (technology->tethering_passphrase != NULL)
340 g_key_file_set_string(keyfile, identifier,
341 "Tethering.Passphrase",
342 technology->tethering_passphrase);
347 __connman_storage_save_global(keyfile);
349 g_key_file_free(keyfile);
354 static void technology_load(struct connman_technology *technology)
358 GError *error = NULL;
359 connman_bool_t enable;
361 DBG("technology %p", technology);
363 keyfile = __connman_storage_load_global();
364 /* Fallback on disabling technology if file not found. */
365 if (keyfile == NULL) {
366 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
367 /* We enable ethernet by default */
368 technology->enable_persistent = TRUE;
370 technology->enable_persistent = FALSE;
374 identifier = g_strdup_printf("%s", get_name(technology->type));
375 if (identifier == NULL)
378 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
380 technology->enable_persistent = enable;
382 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
383 technology->enable_persistent = TRUE;
385 technology->enable_persistent = FALSE;
387 technology_save(technology);
388 g_clear_error(&error);
391 technology->tethering_ident = g_key_file_get_string(keyfile,
392 identifier, "Tethering.Identifier", NULL);
394 technology->tethering_passphrase = g_key_file_get_string(keyfile,
395 identifier, "Tethering.Passphrase", NULL);
399 g_key_file_free(keyfile);
404 connman_bool_t __connman_technology_get_offlinemode(void)
406 return global_offlinemode;
409 static void connman_technology_save_offlinemode()
413 keyfile = __connman_storage_load_global();
415 keyfile = g_key_file_new();
417 g_key_file_set_boolean(keyfile, "global",
418 "OfflineMode", global_offlinemode);
420 __connman_storage_save_global(keyfile);
422 g_key_file_free(keyfile);
427 static connman_bool_t connman_technology_load_offlinemode()
430 GError *error = NULL;
431 connman_bool_t offlinemode;
433 /* If there is a error, we enable offlinemode */
434 keyfile = __connman_storage_load_global();
438 offlinemode = g_key_file_get_boolean(keyfile, "global",
439 "OfflineMode", &error);
442 g_clear_error(&error);
445 g_key_file_free(keyfile);
450 static void append_properties(DBusMessageIter *iter,
451 struct connman_technology *technology)
453 DBusMessageIter dict;
456 connman_dbus_dict_open(iter, &dict);
458 str = get_name(technology->type);
460 connman_dbus_dict_append_basic(&dict, "Name",
461 DBUS_TYPE_STRING, &str);
463 str = __connman_service_type2string(technology->type);
465 connman_dbus_dict_append_basic(&dict, "Type",
466 DBUS_TYPE_STRING, &str);
468 __sync_synchronize();
469 connman_dbus_dict_append_basic(&dict, "Powered",
471 &technology->enabled);
473 connman_dbus_dict_append_basic(&dict, "Connected",
475 &technology->connected);
477 connman_dbus_dict_append_basic(&dict, "Tethering",
479 &technology->tethering);
481 if (technology->tethering_ident != NULL)
482 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
484 &technology->tethering_ident);
486 if (technology->tethering_passphrase != NULL)
487 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
489 &technology->tethering_passphrase);
491 connman_dbus_dict_close(iter, &dict);
494 static void technology_added_signal(struct connman_technology *technology)
497 DBusMessageIter iter;
499 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
500 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
504 dbus_message_iter_init_append(signal, &iter);
505 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
507 append_properties(&iter, technology);
509 dbus_connection_send(connection, signal, NULL);
510 dbus_message_unref(signal);
513 static void technology_removed_signal(struct connman_technology *technology)
515 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
516 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
517 DBUS_TYPE_OBJECT_PATH, &technology->path,
521 static DBusMessage *get_properties(DBusConnection *conn,
522 DBusMessage *message, void *user_data)
524 struct connman_technology *technology = user_data;
526 DBusMessageIter iter;
528 reply = dbus_message_new_method_return(message);
532 dbus_message_iter_init_append(reply, &iter);
533 append_properties(&iter, technology);
538 void __connman_technology_list_struct(DBusMessageIter *array)
541 DBusMessageIter entry;
543 for (list = technology_list; list; list = list->next) {
544 struct connman_technology *technology = list->data;
546 if (technology->path == NULL ||
547 (technology->rfkill_driven == TRUE &&
548 technology->hardblocked == TRUE))
551 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
553 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
555 append_properties(&entry, technology);
556 dbus_message_iter_close_container(array, &entry);
560 static gboolean technology_pending_reply(gpointer user_data)
562 struct connman_technology *technology = user_data;
565 /* Power request timedout, send ETIMEDOUT. */
566 if (technology->pending_reply != NULL) {
567 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
569 g_dbus_send_message(connection, reply);
571 dbus_message_unref(technology->pending_reply);
572 technology->pending_reply = NULL;
573 technology->pending_timeout = 0;
579 static int technology_affect_devices(struct connman_technology *technology,
580 connman_bool_t enable_device)
585 for (list = technology->device_list; list; list = list->next) {
586 struct connman_device *device = list->data;
588 if (enable_device == TRUE)
589 err = __connman_device_enable(device);
591 err = __connman_device_disable(device);
597 static int technology_enable(struct connman_technology *technology)
599 DBG("technology %p enable", technology);
601 __sync_synchronize();
602 if (technology->enabled == TRUE)
605 if (technology->pending_reply != NULL)
608 if (technology->rfkill_driven == TRUE)
609 return __connman_rfkill_block(technology->type, FALSE);
611 return technology_affect_devices(technology, TRUE);
614 static int technology_disable(struct connman_technology *technology)
616 DBG("technology %p disable", technology);
618 __sync_synchronize();
619 if (technology->enabled == FALSE)
622 if (technology->pending_reply != NULL)
625 if (technology->tethering == TRUE)
626 set_tethering(technology, FALSE);
628 if (technology->rfkill_driven == TRUE)
629 return __connman_rfkill_block(technology->type, TRUE);
631 return technology_affect_devices(technology, FALSE);
634 static DBusMessage *set_powered(struct connman_technology *technology,
635 DBusMessage *msg, connman_bool_t powered)
637 DBusMessage *reply = NULL;
640 if (technology->rfkill_driven && technology->hardblocked == TRUE) {
646 err = technology_enable(technology);
648 err = technology_disable(technology);
651 technology->enable_persistent = powered;
652 technology_save(technology);
656 if (err == -EINPROGRESS) {
657 technology->pending_reply = dbus_message_ref(msg);
658 technology->pending_timeout = g_timeout_add_seconds(10,
659 technology_pending_reply, technology);
660 } else if (err == -EALREADY) {
662 reply = __connman_error_already_enabled(msg);
664 reply = __connman_error_already_disabled(msg);
666 reply = __connman_error_failed(msg, -err);
668 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
673 static DBusMessage *set_property(DBusConnection *conn,
674 DBusMessage *msg, void *data)
676 struct connman_technology *technology = data;
677 DBusMessageIter iter, value;
681 DBG("conn %p", conn);
683 if (dbus_message_iter_init(msg, &iter) == FALSE)
684 return __connman_error_invalid_arguments(msg);
686 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
687 return __connman_error_invalid_arguments(msg);
689 dbus_message_iter_get_basic(&iter, &name);
690 dbus_message_iter_next(&iter);
692 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
693 return __connman_error_invalid_arguments(msg);
695 dbus_message_iter_recurse(&iter, &value);
697 type = dbus_message_iter_get_arg_type(&value);
699 DBG("property %s", name);
701 if (g_str_equal(name, "Tethering") == TRUE) {
703 connman_bool_t tethering;
705 if (type != DBUS_TYPE_BOOLEAN)
706 return __connman_error_invalid_arguments(msg);
708 dbus_message_iter_get_basic(&value, &tethering);
710 if (technology->tethering == tethering) {
711 if (tethering == FALSE)
712 return __connman_error_already_disabled(msg);
714 return __connman_error_already_enabled(msg);
717 err = set_tethering(technology, tethering);
719 return __connman_error_failed(msg, -err);
721 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
724 dbus_message_iter_get_basic(&value, &str);
726 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
727 return __connman_error_not_supported(msg);
729 if (strlen(str) < 1 || strlen(str) > 32)
730 return __connman_error_invalid_arguments(msg);
732 if (g_strcmp0(technology->tethering_ident, str) != 0) {
733 g_free(technology->tethering_ident);
734 technology->tethering_ident = g_strdup(str);
735 technology_save(technology);
737 connman_dbus_property_changed_basic(technology->path,
738 CONNMAN_TECHNOLOGY_INTERFACE,
739 "TetheringIdentifier",
741 &technology->tethering_ident);
743 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
746 dbus_message_iter_get_basic(&value, &str);
748 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
749 return __connman_error_not_supported(msg);
751 if (strlen(str) < 8 || strlen(str) > 63)
752 return __connman_error_passphrase_required(msg);
754 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
755 g_free(technology->tethering_passphrase);
756 technology->tethering_passphrase = g_strdup(str);
757 technology_save(technology);
759 connman_dbus_property_changed_basic(technology->path,
760 CONNMAN_TECHNOLOGY_INTERFACE,
761 "TetheringPassphrase",
763 &technology->tethering_passphrase);
765 } else if (g_str_equal(name, "Powered") == TRUE) {
766 connman_bool_t enable;
768 if (type != DBUS_TYPE_BOOLEAN)
769 return __connman_error_invalid_arguments(msg);
771 dbus_message_iter_get_basic(&value, &enable);
773 return set_powered(technology, msg, enable);
775 return __connman_error_invalid_property(msg);
777 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
780 static struct connman_technology *technology_find(enum connman_service_type type)
784 DBG("type %d", type);
786 for (list = technology_list; list; list = list->next) {
787 struct connman_technology *technology = list->data;
789 if (technology->type == type)
796 static void reply_scan_pending(struct connman_technology *technology, int err)
800 DBG("technology %p err %d", technology, err);
802 while (technology->scan_pending != NULL) {
803 DBusMessage *msg = technology->scan_pending->data;
805 DBG("reply to %s", dbus_message_get_sender(msg));
808 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
810 reply = __connman_error_failed(msg, -err);
811 g_dbus_send_message(connection, reply);
812 dbus_message_unref(msg);
814 technology->scan_pending =
815 g_slist_delete_link(technology->scan_pending,
816 technology->scan_pending);
820 void __connman_technology_scan_started(struct connman_device *device)
822 DBG("device %p", device);
825 void __connman_technology_scan_stopped(struct connman_device *device)
828 struct connman_technology *technology;
829 enum connman_service_type type;
832 type = __connman_device_get_service_type(device);
833 technology = technology_find(type);
835 DBG("technology %p device %p", technology, device);
837 if (technology == NULL)
840 for (list = technology->device_list; list != NULL; list = list->next) {
841 struct connman_device *other_device = list->data;
843 if (device == other_device)
846 if (__connman_device_get_service_type(other_device) != type)
849 if (connman_device_get_scanning(other_device) == TRUE)
854 reply_scan_pending(technology, 0);
857 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
858 int result, const char *alpha2)
860 struct connman_technology *technology;
861 enum connman_service_type type;
863 type = __connman_device_get_service_type(device);
864 technology = technology_find(type);
866 if (technology == NULL)
870 if (technology->driver != NULL &&
871 technology->driver->set_regdom != NULL) {
872 technology->driver->set_regdom(technology, alpha2);
879 connman_technology_regdom_notify(technology, alpha2);
882 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
884 struct connman_technology *technology = data;
887 DBG ("technology %p request from %s", technology,
888 dbus_message_get_sender(msg));
890 dbus_message_ref(msg);
891 technology->scan_pending =
892 g_slist_prepend(technology->scan_pending, msg);
894 err = __connman_device_request_scan(technology->type);
896 reply_scan_pending(technology, err);
901 static const GDBusMethodTable technology_methods[] = {
902 { GDBUS_DEPRECATED_METHOD("GetProperties",
903 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
905 { GDBUS_ASYNC_METHOD("SetProperty",
906 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
907 NULL, set_property) },
908 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
912 static const GDBusSignalTable technology_signals[] = {
913 { GDBUS_SIGNAL("PropertyChanged",
914 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
918 static gboolean technology_dbus_register(struct connman_technology *technology)
920 if (technology->dbus_registered == TRUE ||
921 (technology->rfkill_driven == TRUE &&
922 technology->hardblocked == TRUE))
925 if (g_dbus_register_interface(connection, technology->path,
926 CONNMAN_TECHNOLOGY_INTERFACE,
927 technology_methods, technology_signals,
928 NULL, technology, NULL) == FALSE) {
929 connman_error("Failed to register %s", technology->path);
933 technology_added_signal(technology);
934 technology->dbus_registered = TRUE;
939 static struct connman_technology *technology_get(enum connman_service_type type)
941 struct connman_technology *technology;
942 struct connman_technology_driver *driver = NULL;
947 DBG("type %d", type);
949 str = __connman_service_type2string(type);
953 technology = technology_find(type);
954 if (technology != NULL) {
955 __sync_fetch_and_add(&technology->refcount, 1);
959 /* First check if we have a driver for this technology type */
960 for (list = driver_list; list; list = list->next) {
963 if (driver->type == type)
969 if (driver == NULL) {
970 DBG("No matching driver found for %s.",
971 __connman_service_type2string(type));
975 technology = g_try_new0(struct connman_technology, 1);
976 if (technology == NULL)
979 technology->refcount = 1;
981 technology->rfkill_driven = FALSE;
982 technology->softblocked = FALSE;
983 technology->hardblocked = FALSE;
985 technology->type = type;
986 technology->path = g_strdup_printf("%s/technology/%s",
989 technology->device_list = NULL;
991 technology->pending_reply = NULL;
993 technology_load(technology);
995 if (technology_dbus_register(technology) == FALSE) {
1000 technology_list = g_slist_prepend(technology_list, technology);
1002 technology->driver = driver;
1003 err = driver->probe(technology);
1005 DBG("Driver probe failed for technology %p", technology);
1007 DBG("technology %p", technology);
1012 static void technology_dbus_unregister(struct connman_technology *technology)
1014 if (technology->dbus_registered == FALSE)
1017 technology_removed_signal(technology);
1018 g_dbus_unregister_interface(connection, technology->path,
1019 CONNMAN_TECHNOLOGY_INTERFACE);
1021 technology->dbus_registered = FALSE;
1024 static void technology_put(struct connman_technology *technology)
1026 DBG("technology %p", technology);
1028 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1031 reply_scan_pending(technology, -EINTR);
1033 if (technology->driver) {
1034 technology->driver->remove(technology);
1035 technology->driver = NULL;
1038 technology_list = g_slist_remove(technology_list, technology);
1040 technology_dbus_unregister(technology);
1042 g_slist_free(technology->device_list);
1044 g_free(technology->path);
1045 g_free(technology->regdom);
1046 g_free(technology->tethering_ident);
1047 g_free(technology->tethering_passphrase);
1051 void __connman_technology_add_interface(enum connman_service_type type,
1052 int index, const char *name, const char *ident)
1054 struct connman_technology *technology;
1057 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1058 case CONNMAN_SERVICE_TYPE_SYSTEM:
1060 case CONNMAN_SERVICE_TYPE_ETHERNET:
1061 case CONNMAN_SERVICE_TYPE_WIFI:
1062 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1063 case CONNMAN_SERVICE_TYPE_CELLULAR:
1064 case CONNMAN_SERVICE_TYPE_GPS:
1065 case CONNMAN_SERVICE_TYPE_VPN:
1066 case CONNMAN_SERVICE_TYPE_GADGET:
1070 connman_info("Adding interface %s [ %s ]", name,
1071 __connman_service_type2string(type));
1073 technology = technology_find(type);
1075 if (technology == NULL || technology->driver == NULL
1076 || technology->driver->add_interface == NULL)
1079 technology->driver->add_interface(technology,
1080 index, name, ident);
1083 void __connman_technology_remove_interface(enum connman_service_type type,
1084 int index, const char *name, const char *ident)
1086 struct connman_technology *technology;
1089 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1090 case CONNMAN_SERVICE_TYPE_SYSTEM:
1092 case CONNMAN_SERVICE_TYPE_ETHERNET:
1093 case CONNMAN_SERVICE_TYPE_WIFI:
1094 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1095 case CONNMAN_SERVICE_TYPE_CELLULAR:
1096 case CONNMAN_SERVICE_TYPE_GPS:
1097 case CONNMAN_SERVICE_TYPE_VPN:
1098 case CONNMAN_SERVICE_TYPE_GADGET:
1102 connman_info("Remove interface %s [ %s ]", name,
1103 __connman_service_type2string(type));
1105 technology = technology_find(type);
1107 if (technology == NULL || technology->driver == NULL)
1110 if (technology->driver->remove_interface)
1111 technology->driver->remove_interface(technology, index);
1114 int __connman_technology_add_device(struct connman_device *device)
1116 struct connman_technology *technology;
1117 enum connman_service_type type;
1119 DBG("device %p", device);
1121 type = __connman_device_get_service_type(device);
1123 technology = technology_get(type);
1124 if (technology == NULL) {
1126 * Since no driver can be found for this device at the moment we
1127 * add it to the techless device list.
1129 techless_device_list = g_slist_prepend(techless_device_list,
1135 __sync_synchronize();
1136 if (technology->rfkill_driven == TRUE) {
1137 if (technology->enabled == TRUE)
1138 __connman_device_enable(device);
1140 __connman_device_disable(device);
1145 if (technology->enable_persistent == TRUE &&
1146 global_offlinemode == FALSE) {
1147 int err = __connman_device_enable(device);
1149 * connman_technology_add_device() calls __connman_device_enable()
1150 * but since the device is already enabled, the calls does not
1151 * propagate through to connman_technology_enabled via
1152 * connman_device_set_powered.
1154 if (err == -EALREADY)
1155 __connman_technology_enabled(type);
1157 /* if technology persistent state is offline */
1158 if (technology->enable_persistent == FALSE)
1159 __connman_device_disable(device);
1162 technology->device_list = g_slist_prepend(technology->device_list,
1168 int __connman_technology_remove_device(struct connman_device *device)
1170 struct connman_technology *technology;
1171 enum connman_service_type type;
1173 DBG("device %p", device);
1175 type = __connman_device_get_service_type(device);
1177 technology = technology_find(type);
1178 if (technology == NULL) {
1179 techless_device_list = g_slist_remove(techless_device_list,
1184 technology->device_list = g_slist_remove(technology->device_list,
1186 technology_put(technology);
1191 static void powered_changed(struct connman_technology *technology)
1193 if (technology->dbus_registered == FALSE)
1196 if (technology->pending_reply != NULL) {
1197 g_dbus_send_reply(connection,
1198 technology->pending_reply, DBUS_TYPE_INVALID);
1199 dbus_message_unref(technology->pending_reply);
1200 technology->pending_reply = NULL;
1202 g_source_remove(technology->pending_timeout);
1203 technology->pending_timeout = 0;
1206 __sync_synchronize();
1207 connman_dbus_property_changed_basic(technology->path,
1208 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1209 DBUS_TYPE_BOOLEAN, &technology->enabled);
1212 static int technology_enabled(struct connman_technology *technology)
1214 __sync_synchronize();
1215 if (technology->enabled == TRUE)
1218 technology->enabled = TRUE;
1220 powered_changed(technology);
1225 int __connman_technology_enabled(enum connman_service_type type)
1227 struct connman_technology *technology;
1229 technology = technology_find(type);
1230 if (technology == NULL)
1233 if (technology->rfkill_driven == TRUE)
1236 return technology_enabled(technology);
1239 static int technology_disabled(struct connman_technology *technology)
1241 __sync_synchronize();
1242 if (technology->enabled == FALSE)
1245 technology->enabled = FALSE;
1247 powered_changed(technology);
1252 int __connman_technology_disabled(enum connman_service_type type)
1254 struct connman_technology *technology;
1257 technology = technology_find(type);
1258 if (technology == NULL)
1261 if (technology->rfkill_driven == TRUE)
1264 for (list = technology->device_list; list != NULL; list = list->next) {
1265 struct connman_device *device = list->data;
1267 if (connman_device_get_powered(device) == TRUE)
1271 return technology_disabled(technology);
1274 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1279 if (global_offlinemode == offlinemode)
1282 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1285 * This is a bit tricky. When you set offlinemode, there is no
1286 * way to differentiate between attempting offline mode and
1287 * resuming offlinemode from last saved profile. We need that
1288 * information in rfkill_update, otherwise it falls back on the
1289 * technology's persistent state. Hence we set the offline mode here
1290 * but save it & call the notifier only if its successful.
1293 global_offlinemode = offlinemode;
1295 /* Traverse technology list, enable/disable each technology. */
1296 for (list = technology_list; list; list = list->next) {
1297 struct connman_technology *technology = list->data;
1300 err = technology_disable(technology);
1302 if (!offlinemode && technology->enable_persistent)
1303 err = technology_enable(technology);
1306 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1307 connman_technology_save_offlinemode();
1308 __connman_notifier_offlinemode(offlinemode);
1310 global_offlinemode = connman_technology_load_offlinemode();
1315 void __connman_technology_set_connected(enum connman_service_type type,
1316 connman_bool_t connected)
1318 struct connman_technology *technology;
1320 technology = technology_find(type);
1321 if (technology == NULL)
1324 DBG("technology %p connected %d", technology, connected);
1326 technology->connected = connected;
1328 connman_dbus_property_changed_basic(technology->path,
1329 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1330 DBUS_TYPE_BOOLEAN, &connected);
1333 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1334 connman_bool_t softblock,
1335 connman_bool_t hardblock,
1336 connman_bool_t new_rfkill)
1338 gboolean hardblock_changed = FALSE;
1339 gboolean apply = TRUE;
1340 GList *start, *list;
1342 DBG("technology %p --> %d/%d vs %d/%d",
1343 technology, softblock, hardblock,
1344 technology->softblocked, technology->hardblocked);
1346 if (technology->hardblocked == hardblock)
1347 goto softblock_change;
1349 if (!(new_rfkill == TRUE && hardblock == FALSE)) {
1350 start = g_hash_table_get_values(rfkill_list);
1352 for (list = start; list != NULL; list = list->next) {
1353 struct connman_rfkill *rfkill = list->data;
1355 if (rfkill->type != technology->type)
1358 if (rfkill->hardblock != hardblock)
1366 goto softblock_change;
1368 technology->hardblocked = hardblock;
1369 hardblock_changed = TRUE;
1372 if (apply == FALSE && technology->softblocked != softblock)
1376 return technology->hardblocked;
1378 technology->softblocked = softblock;
1380 if (technology->hardblocked == TRUE ||
1381 technology->softblocked == TRUE) {
1382 if (technology_disabled(technology) != -EALREADY)
1383 technology_affect_devices(technology, FALSE);
1384 } else if (technology->hardblocked == FALSE &&
1385 technology->softblocked == FALSE) {
1386 if (technology_enabled(technology) != -EALREADY)
1387 technology_affect_devices(technology, TRUE);
1390 if (hardblock_changed == TRUE) {
1391 if (technology->hardblocked == TRUE) {
1392 DBG("%s is switched off.", get_name(technology->type));
1393 technology_dbus_unregister(technology);
1395 technology_dbus_register(technology);
1398 return technology->hardblocked;
1401 int __connman_technology_add_rfkill(unsigned int index,
1402 enum connman_service_type type,
1403 connman_bool_t softblock,
1404 connman_bool_t hardblock)
1406 struct connman_technology *technology;
1407 struct connman_rfkill *rfkill;
1409 DBG("index %u type %d soft %u hard %u", index, type,
1410 softblock, hardblock);
1412 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1416 rfkill = g_try_new0(struct connman_rfkill, 1);
1420 rfkill->index = index;
1421 rfkill->type = type;
1422 rfkill->softblock = softblock;
1423 rfkill->hardblock = hardblock;
1425 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1428 technology = technology_get(type);
1429 /* If there is no driver for this type, ignore it. */
1430 if (technology == NULL)
1433 technology->rfkill_driven = TRUE;
1435 /* If hardblocked, there is no need to handle softblocked state */
1436 if (technology_apply_rfkill_change(technology,
1437 softblock, hardblock, TRUE) == TRUE)
1441 * Depending on softblocked state we unblock/block according to
1442 * offlinemode and persistente state.
1444 if (technology->softblocked == TRUE &&
1445 global_offlinemode == FALSE &&
1446 technology->enable_persistent == TRUE)
1447 return __connman_rfkill_block(type, FALSE);
1448 else if (technology->softblocked == FALSE &&
1449 (global_offlinemode == TRUE ||
1450 technology->enable_persistent == FALSE))
1451 return __connman_rfkill_block(type, TRUE);
1456 int __connman_technology_update_rfkill(unsigned int index,
1457 enum connman_service_type type,
1458 connman_bool_t softblock,
1459 connman_bool_t hardblock)
1461 struct connman_technology *technology;
1462 struct connman_rfkill *rfkill;
1464 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1466 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1470 if (rfkill->softblock == softblock &&
1471 rfkill->hardblock == hardblock)
1474 rfkill->softblock = softblock;
1475 rfkill->hardblock = hardblock;
1477 technology = technology_find(type);
1478 /* If there is no driver for this type, ignore it. */
1479 if (technology == NULL)
1482 /* If hardblocked, there is no need to handle softblocked state */
1483 if (technology_apply_rfkill_change(technology,
1484 softblock, hardblock, FALSE) == TRUE)
1487 if (global_offlinemode == TRUE)
1491 * Depending on softblocked state we unblock/block according to
1494 if (technology->softblocked == TRUE &&
1495 technology->enable_persistent == TRUE)
1496 return __connman_rfkill_block(type, FALSE);
1497 else if (technology->softblocked == FALSE &&
1498 technology->enable_persistent == FALSE)
1499 return __connman_rfkill_block(type, TRUE);
1504 int __connman_technology_remove_rfkill(unsigned int index,
1505 enum connman_service_type type)
1507 struct connman_technology *technology;
1508 struct connman_rfkill *rfkill;
1510 DBG("index %u", index);
1512 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1516 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1518 technology = technology_find(type);
1519 if (technology == NULL)
1522 technology_apply_rfkill_change(technology,
1523 technology->softblocked, !technology->hardblocked, FALSE);
1525 technology_put(technology);
1530 int __connman_technology_init(void)
1534 connection = connman_dbus_get_connection();
1536 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1539 global_offlinemode = connman_technology_load_offlinemode();
1541 /* This will create settings file if it is missing */
1542 connman_technology_save_offlinemode();
1547 void __connman_technology_cleanup(void)
1551 g_hash_table_destroy(rfkill_list);
1553 dbus_connection_unref(connection);