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;
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,
122 if (techless_device_list == NULL)
126 * Check for technology less devices if this driver
127 * can service any of them.
129 for (list = techless_device_list; list; list = list->next) {
132 type = __connman_device_get_service_type(device);
133 if (type != driver->type)
136 techless_device_list = g_slist_remove(techless_device_list,
139 __connman_technology_add_device(device);
143 /* Check for orphaned rfkill switches. */
144 g_hash_table_foreach(rfkill_list, rfkill_check,
145 GINT_TO_POINTER(driver->type));
151 * connman_technology_driver_unregister:
152 * @driver: Technology driver definition
154 * Remove a previously registered technology driver
156 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
159 struct connman_technology *technology;
161 DBG("Unregistering driver %p name %s", driver, driver->name);
163 for (list = technology_list; list; list = list->next) {
164 technology = list->data;
166 if (technology->driver == NULL)
169 if (technology->type == driver->type) {
170 technology->driver->remove(technology);
171 technology->driver = NULL;
175 driver_list = g_slist_remove(driver_list, driver);
178 static void tethering_changed(struct connman_technology *technology)
180 connman_bool_t tethering = technology->tethering;
182 connman_dbus_property_changed_basic(technology->path,
183 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
184 DBUS_TYPE_BOOLEAN, &tethering);
187 void connman_technology_tethering_notify(struct connman_technology *technology,
188 connman_bool_t enabled)
192 DBG("technology %p enabled %u", technology, enabled);
194 if (technology->tethering == enabled)
197 technology->tethering = enabled;
199 tethering_changed(technology);
202 __connman_tethering_set_enabled();
204 for (list = technology_list; list; list = list->next) {
205 struct connman_technology *other_tech = list->data;
206 if (other_tech->tethering == TRUE)
210 __connman_tethering_set_disabled();
214 static int set_tethering(struct connman_technology *technology,
215 connman_bool_t enabled)
217 const char *ident, *passphrase, *bridge;
219 ident = technology->tethering_ident;
220 passphrase = technology->tethering_passphrase;
222 if (technology->driver == NULL ||
223 technology->driver->set_tethering == NULL)
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_WIMAX:
309 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
311 case CONNMAN_SERVICE_TYPE_CELLULAR:
318 static void technology_save(struct connman_technology *technology)
323 DBG("technology %p", technology);
325 keyfile = __connman_storage_load_global();
327 keyfile = g_key_file_new();
329 identifier = g_strdup_printf("%s", get_name(technology->type));
330 if (identifier == NULL)
333 g_key_file_set_boolean(keyfile, identifier, "Enable",
334 technology->enable_persistent);
336 if (technology->tethering_ident != NULL)
337 g_key_file_set_string(keyfile, identifier,
338 "Tethering.Identifier",
339 technology->tethering_ident);
341 if (technology->tethering_passphrase != NULL)
342 g_key_file_set_string(keyfile, identifier,
343 "Tethering.Passphrase",
344 technology->tethering_passphrase);
349 __connman_storage_save_global(keyfile);
351 g_key_file_free(keyfile);
356 static void technology_load(struct connman_technology *technology)
360 GError *error = NULL;
361 connman_bool_t enable;
363 DBG("technology %p", technology);
365 keyfile = __connman_storage_load_global();
366 /* Fallback on disabling technology if file not found. */
367 if (keyfile == NULL) {
368 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
369 /* We enable ethernet by default */
370 technology->enable_persistent = TRUE;
372 technology->enable_persistent = FALSE;
376 identifier = g_strdup_printf("%s", get_name(technology->type));
377 if (identifier == NULL)
380 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
382 technology->enable_persistent = enable;
384 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
385 technology->enable_persistent = TRUE;
387 technology->enable_persistent = FALSE;
389 technology_save(technology);
390 g_clear_error(&error);
393 technology->tethering_ident = g_key_file_get_string(keyfile,
394 identifier, "Tethering.Identifier", NULL);
396 technology->tethering_passphrase = g_key_file_get_string(keyfile,
397 identifier, "Tethering.Passphrase", NULL);
401 g_key_file_free(keyfile);
406 connman_bool_t __connman_technology_get_offlinemode(void)
408 return global_offlinemode;
411 static void connman_technology_save_offlinemode()
415 keyfile = __connman_storage_load_global();
417 keyfile = g_key_file_new();
419 g_key_file_set_boolean(keyfile, "global",
420 "OfflineMode", global_offlinemode);
422 __connman_storage_save_global(keyfile);
424 g_key_file_free(keyfile);
429 static connman_bool_t connman_technology_load_offlinemode()
432 GError *error = NULL;
433 connman_bool_t offlinemode;
435 /* If there is a error, we enable offlinemode */
436 keyfile = __connman_storage_load_global();
440 offlinemode = g_key_file_get_boolean(keyfile, "global",
441 "OfflineMode", &error);
444 g_clear_error(&error);
447 g_key_file_free(keyfile);
452 static void append_properties(DBusMessageIter *iter,
453 struct connman_technology *technology)
455 DBusMessageIter dict;
457 connman_bool_t powered;
459 connman_dbus_dict_open(iter, &dict);
461 str = get_name(technology->type);
463 connman_dbus_dict_append_basic(&dict, "Name",
464 DBUS_TYPE_STRING, &str);
466 str = __connman_service_type2string(technology->type);
468 connman_dbus_dict_append_basic(&dict, "Type",
469 DBUS_TYPE_STRING, &str);
471 __sync_synchronize();
472 if (technology->enabled > 0)
476 connman_dbus_dict_append_basic(&dict, "Powered",
477 DBUS_TYPE_BOOLEAN, &powered);
479 connman_dbus_dict_append_basic(&dict, "Connected",
481 &technology->connected);
483 connman_dbus_dict_append_basic(&dict, "Tethering",
485 &technology->tethering);
487 if (technology->tethering_ident != NULL)
488 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
490 &technology->tethering_ident);
492 if (technology->tethering_passphrase != NULL)
493 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
495 &technology->tethering_passphrase);
497 connman_dbus_dict_close(iter, &dict);
500 static void technology_added_signal(struct connman_technology *technology)
503 DBusMessageIter iter;
505 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
506 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
510 dbus_message_iter_init_append(signal, &iter);
511 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
513 append_properties(&iter, technology);
515 dbus_connection_send(connection, signal, NULL);
516 dbus_message_unref(signal);
519 static void technology_removed_signal(struct connman_technology *technology)
521 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
522 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
523 DBUS_TYPE_OBJECT_PATH, &technology->path,
527 static DBusMessage *get_properties(DBusConnection *conn,
528 DBusMessage *message, void *user_data)
530 struct connman_technology *technology = user_data;
532 DBusMessageIter iter;
534 reply = dbus_message_new_method_return(message);
538 dbus_message_iter_init_append(reply, &iter);
539 append_properties(&iter, technology);
544 void __connman_technology_list_struct(DBusMessageIter *array)
547 DBusMessageIter entry;
549 for (list = technology_list; list; list = list->next) {
550 struct connman_technology *technology = list->data;
552 if (technology->path == NULL ||
553 (technology->rfkill_driven == TRUE &&
554 technology->hardblocked == TRUE))
557 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
559 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
561 append_properties(&entry, technology);
562 dbus_message_iter_close_container(array, &entry);
566 static gboolean technology_pending_reply(gpointer user_data)
568 struct connman_technology *technology = user_data;
571 /* Power request timedout, send ETIMEDOUT. */
572 if (technology->pending_reply != NULL) {
573 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
575 g_dbus_send_message(connection, reply);
577 dbus_message_unref(technology->pending_reply);
578 technology->pending_reply = NULL;
579 technology->pending_timeout = 0;
585 static int technology_affect_devices(struct connman_technology *technology,
586 connman_bool_t enable_device)
591 for (list = technology->device_list; list; list = list->next) {
592 struct connman_device *device = list->data;
594 if (enable_device == TRUE)
595 err = __connman_device_enable(device);
597 err = __connman_device_disable(device);
603 static int technology_enable(struct connman_technology *technology,
604 connman_bool_t hardblock)
606 DBG("technology %p enable", technology);
608 __sync_synchronize();
609 if (technology->enabled > 0)
612 if (technology->pending_reply != NULL)
615 if (hardblock == TRUE && technology->enable_persistent == FALSE)
618 __connman_rfkill_block(technology->type, FALSE);
620 return technology_affect_devices(technology, TRUE);
623 static int technology_disable(struct connman_technology *technology,
624 connman_bool_t hardblock)
626 DBG("technology %p disable", technology);
628 __sync_synchronize();
629 if (technology->enabled == 0)
632 if (technology->pending_reply != NULL)
635 if (technology->tethering == TRUE)
636 set_tethering(technology, FALSE);
638 if (hardblock == FALSE)
639 __connman_rfkill_block(technology->type, TRUE);
641 return technology_affect_devices(technology, FALSE);
644 static DBusMessage *set_powered(struct connman_technology *technology,
645 DBusMessage *msg, connman_bool_t powered)
647 DBusMessage *reply = NULL;
650 if (technology->rfkill_driven && technology->hardblocked == TRUE) {
656 err = technology_enable(technology, FALSE);
658 err = technology_disable(technology, FALSE);
661 technology->enable_persistent = powered;
662 technology_save(technology);
666 if (err == -EINPROGRESS) {
667 technology->pending_reply = dbus_message_ref(msg);
668 technology->pending_timeout = g_timeout_add_seconds(10,
669 technology_pending_reply, technology);
670 } else if (err == -EALREADY) {
672 reply = __connman_error_already_enabled(msg);
674 reply = __connman_error_already_disabled(msg);
676 reply = __connman_error_failed(msg, -err);
678 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
683 static DBusMessage *set_property(DBusConnection *conn,
684 DBusMessage *msg, void *data)
686 struct connman_technology *technology = data;
687 DBusMessageIter iter, value;
691 DBG("conn %p", conn);
693 if (dbus_message_iter_init(msg, &iter) == FALSE)
694 return __connman_error_invalid_arguments(msg);
696 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
697 return __connman_error_invalid_arguments(msg);
699 dbus_message_iter_get_basic(&iter, &name);
700 dbus_message_iter_next(&iter);
702 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
703 return __connman_error_invalid_arguments(msg);
705 dbus_message_iter_recurse(&iter, &value);
707 type = dbus_message_iter_get_arg_type(&value);
709 DBG("property %s", name);
711 if (g_str_equal(name, "Tethering") == TRUE) {
713 connman_bool_t tethering;
715 if (type != DBUS_TYPE_BOOLEAN)
716 return __connman_error_invalid_arguments(msg);
718 dbus_message_iter_get_basic(&value, &tethering);
720 if (technology->tethering == tethering) {
721 if (tethering == FALSE)
722 return __connman_error_already_disabled(msg);
724 return __connman_error_already_enabled(msg);
727 err = set_tethering(technology, tethering);
729 return __connman_error_failed(msg, -err);
731 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
734 dbus_message_iter_get_basic(&value, &str);
736 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
737 return __connman_error_not_supported(msg);
739 if (strlen(str) < 1 || strlen(str) > 32)
740 return __connman_error_invalid_arguments(msg);
742 if (g_strcmp0(technology->tethering_ident, str) != 0) {
743 g_free(technology->tethering_ident);
744 technology->tethering_ident = g_strdup(str);
745 technology_save(technology);
747 connman_dbus_property_changed_basic(technology->path,
748 CONNMAN_TECHNOLOGY_INTERFACE,
749 "TetheringIdentifier",
751 &technology->tethering_ident);
753 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
756 dbus_message_iter_get_basic(&value, &str);
758 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
759 return __connman_error_not_supported(msg);
761 if (strlen(str) < 8 || strlen(str) > 63)
762 return __connman_error_passphrase_required(msg);
764 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
765 g_free(technology->tethering_passphrase);
766 technology->tethering_passphrase = g_strdup(str);
767 technology_save(technology);
769 connman_dbus_property_changed_basic(technology->path,
770 CONNMAN_TECHNOLOGY_INTERFACE,
771 "TetheringPassphrase",
773 &technology->tethering_passphrase);
775 } else if (g_str_equal(name, "Powered") == TRUE) {
776 connman_bool_t enable;
778 if (type != DBUS_TYPE_BOOLEAN)
779 return __connman_error_invalid_arguments(msg);
781 dbus_message_iter_get_basic(&value, &enable);
783 return set_powered(technology, msg, enable);
785 return __connman_error_invalid_property(msg);
787 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
790 static struct connman_technology *technology_find(enum connman_service_type type)
794 DBG("type %d", type);
796 for (list = technology_list; list; list = list->next) {
797 struct connman_technology *technology = list->data;
799 if (technology->type == type)
806 static void reply_scan_pending(struct connman_technology *technology, int err)
810 DBG("technology %p err %d", technology, err);
812 while (technology->scan_pending != NULL) {
813 DBusMessage *msg = technology->scan_pending->data;
815 DBG("reply to %s", dbus_message_get_sender(msg));
818 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
820 reply = __connman_error_failed(msg, -err);
821 g_dbus_send_message(connection, reply);
822 dbus_message_unref(msg);
824 technology->scan_pending =
825 g_slist_delete_link(technology->scan_pending,
826 technology->scan_pending);
830 void __connman_technology_scan_started(struct connman_device *device)
832 DBG("device %p", device);
835 void __connman_technology_scan_stopped(struct connman_device *device)
838 struct connman_technology *technology;
839 enum connman_service_type type;
842 type = __connman_device_get_service_type(device);
843 technology = technology_find(type);
845 DBG("technology %p device %p", technology, device);
847 if (technology == NULL)
850 for (list = technology->device_list; list != NULL; list = list->next) {
851 struct connman_device *other_device = list->data;
853 if (device == other_device)
856 if (__connman_device_get_service_type(other_device) != type)
859 if (connman_device_get_scanning(other_device) == TRUE)
864 reply_scan_pending(technology, 0);
867 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
868 int result, const char *alpha2)
870 struct connman_technology *technology;
871 enum connman_service_type type;
873 type = __connman_device_get_service_type(device);
874 technology = technology_find(type);
876 if (technology == NULL)
880 if (technology->driver != NULL &&
881 technology->driver->set_regdom != NULL) {
882 technology->driver->set_regdom(technology, alpha2);
889 connman_technology_regdom_notify(technology, alpha2);
892 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
894 struct connman_technology *technology = data;
897 DBG ("technology %p request from %s", technology,
898 dbus_message_get_sender(msg));
900 dbus_message_ref(msg);
901 technology->scan_pending =
902 g_slist_prepend(technology->scan_pending, msg);
904 err = __connman_device_request_scan(technology->type);
906 reply_scan_pending(technology, err);
911 static const GDBusMethodTable technology_methods[] = {
912 { GDBUS_DEPRECATED_METHOD("GetProperties",
913 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
915 { GDBUS_ASYNC_METHOD("SetProperty",
916 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
917 NULL, set_property) },
918 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
922 static const GDBusSignalTable technology_signals[] = {
923 { GDBUS_SIGNAL("PropertyChanged",
924 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
928 static gboolean technology_dbus_register(struct connman_technology *technology)
930 if (technology->dbus_registered == TRUE ||
931 (technology->rfkill_driven &&
932 technology->hardblocked == TRUE))
935 if (g_dbus_register_interface(connection, technology->path,
936 CONNMAN_TECHNOLOGY_INTERFACE,
937 technology_methods, technology_signals,
938 NULL, technology, NULL) == FALSE) {
939 connman_error("Failed to register %s", technology->path);
943 technology_added_signal(technology);
944 technology->dbus_registered = TRUE;
949 static struct connman_technology *technology_get(enum connman_service_type type)
951 struct connman_technology *technology;
952 struct connman_technology_driver *driver = NULL;
957 DBG("type %d", type);
959 str = __connman_service_type2string(type);
963 technology = technology_find(type);
964 if (technology != NULL) {
965 __sync_fetch_and_add(&technology->refcount, 1);
969 /* First check if we have a driver for this technology type */
970 for (list = driver_list; list; list = list->next) {
973 if (driver->type == type)
979 if (driver == NULL) {
980 DBG("No matching driver found for %s.",
981 __connman_service_type2string(type));
985 technology = g_try_new0(struct connman_technology, 1);
986 if (technology == NULL)
989 technology->refcount = 1;
991 technology->rfkill_driven = FALSE;
992 technology->softblocked = FALSE;
994 if (type == CONNMAN_SERVICE_TYPE_ETHERNET)
995 technology->hardblocked = FALSE;
997 technology->hardblocked = TRUE;
999 technology->type = type;
1000 technology->path = g_strdup_printf("%s/technology/%s",
1003 technology->device_list = NULL;
1005 technology->pending_reply = NULL;
1007 technology_load(technology);
1009 if (technology_dbus_register(technology) == FALSE) {
1014 technology_list = g_slist_prepend(technology_list, technology);
1016 technology->driver = driver;
1017 err = driver->probe(technology);
1019 DBG("Driver probe failed for technology %p", technology);
1021 DBG("technology %p", technology);
1026 static void technology_dbus_unregister(struct connman_technology *technology)
1028 if (technology->dbus_registered == FALSE)
1031 technology_removed_signal(technology);
1032 g_dbus_unregister_interface(connection, technology->path,
1033 CONNMAN_TECHNOLOGY_INTERFACE);
1035 technology->dbus_registered = FALSE;
1038 static void technology_put(struct connman_technology *technology)
1040 DBG("technology %p", technology);
1042 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1045 reply_scan_pending(technology, -EINTR);
1047 if (technology->driver) {
1048 technology->driver->remove(technology);
1049 technology->driver = NULL;
1052 technology_list = g_slist_remove(technology_list, technology);
1054 technology_dbus_unregister(technology);
1056 g_slist_free(technology->device_list);
1058 g_free(technology->path);
1059 g_free(technology->regdom);
1060 g_free(technology->tethering_ident);
1061 g_free(technology->tethering_passphrase);
1065 void __connman_technology_add_interface(enum connman_service_type type,
1066 int index, const char *name, const char *ident)
1068 struct connman_technology *technology;
1071 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1072 case CONNMAN_SERVICE_TYPE_SYSTEM:
1074 case CONNMAN_SERVICE_TYPE_ETHERNET:
1075 case CONNMAN_SERVICE_TYPE_WIFI:
1076 case CONNMAN_SERVICE_TYPE_WIMAX:
1077 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1078 case CONNMAN_SERVICE_TYPE_CELLULAR:
1079 case CONNMAN_SERVICE_TYPE_GPS:
1080 case CONNMAN_SERVICE_TYPE_VPN:
1081 case CONNMAN_SERVICE_TYPE_GADGET:
1085 connman_info("Adding interface %s [ %s ]", name,
1086 __connman_service_type2string(type));
1088 technology = technology_find(type);
1090 if (technology == NULL || technology->driver == NULL
1091 || technology->driver->add_interface == NULL)
1094 technology->driver->add_interface(technology,
1095 index, name, ident);
1098 void __connman_technology_remove_interface(enum connman_service_type type,
1099 int index, const char *name, const char *ident)
1101 struct connman_technology *technology;
1104 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1105 case CONNMAN_SERVICE_TYPE_SYSTEM:
1107 case CONNMAN_SERVICE_TYPE_ETHERNET:
1108 case CONNMAN_SERVICE_TYPE_WIFI:
1109 case CONNMAN_SERVICE_TYPE_WIMAX:
1110 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1111 case CONNMAN_SERVICE_TYPE_CELLULAR:
1112 case CONNMAN_SERVICE_TYPE_GPS:
1113 case CONNMAN_SERVICE_TYPE_VPN:
1114 case CONNMAN_SERVICE_TYPE_GADGET:
1118 connman_info("Remove interface %s [ %s ]", name,
1119 __connman_service_type2string(type));
1121 technology = technology_find(type);
1123 if (technology == NULL || technology->driver == NULL)
1126 if (technology->driver->remove_interface)
1127 technology->driver->remove_interface(technology, index);
1130 int __connman_technology_add_device(struct connman_device *device)
1132 struct connman_technology *technology;
1133 enum connman_service_type type;
1135 DBG("device %p", device);
1137 type = __connman_device_get_service_type(device);
1139 technology = technology_get(type);
1140 if (technology == NULL) {
1142 * Since no driver can be found for this device at the moment we
1143 * add it to the techless device list.
1145 techless_device_list = g_slist_prepend(techless_device_list,
1151 if (technology->enable_persistent &&
1152 global_offlinemode == FALSE &&
1153 technology->hardblocked == FALSE) {
1154 int err = __connman_device_enable(device);
1156 * connman_technology_add_device() calls __connman_device_enable()
1157 * but since the device is already enabled, the calls does not
1158 * propagate through to connman_technology_enabled via
1159 * connman_device_set_powered.
1161 if (err == -EALREADY)
1162 __connman_technology_enabled(type);
1164 /* if technology persistent state is offline or hardblocked */
1165 if (technology->enable_persistent == FALSE ||
1166 technology->hardblocked == TRUE)
1167 __connman_device_disable(device);
1169 technology->device_list = g_slist_prepend(technology->device_list,
1175 int __connman_technology_remove_device(struct connman_device *device)
1177 struct connman_technology *technology;
1178 enum connman_service_type type;
1180 DBG("device %p", device);
1182 type = __connman_device_get_service_type(device);
1184 technology = technology_find(type);
1185 if (technology == NULL) {
1186 techless_device_list = g_slist_remove(techless_device_list,
1191 technology->device_list = g_slist_remove(technology->device_list,
1193 technology_put(technology);
1198 static void powered_changed(struct connman_technology *technology)
1200 connman_bool_t powered;
1202 __sync_synchronize();
1203 if (technology->enabled >0)
1208 connman_dbus_property_changed_basic(technology->path,
1209 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1210 DBUS_TYPE_BOOLEAN, &powered);
1213 int __connman_technology_enabled(enum connman_service_type type)
1215 struct connman_technology *technology;
1217 technology = technology_find(type);
1218 if (technology == NULL)
1221 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1224 powered_changed(technology);
1226 if (technology->pending_reply != NULL) {
1227 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1228 dbus_message_unref(technology->pending_reply);
1229 g_source_remove(technology->pending_timeout);
1230 technology->pending_reply = NULL;
1231 technology->pending_timeout = 0;
1237 int __connman_technology_disabled(enum connman_service_type type)
1239 struct connman_technology *technology;
1241 technology = technology_find(type);
1242 if (technology == NULL)
1245 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1246 return -EINPROGRESS;
1248 if (technology->pending_reply != NULL) {
1249 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1250 dbus_message_unref(technology->pending_reply);
1251 g_source_remove(technology->pending_timeout);
1252 technology->pending_reply = NULL;
1253 technology->pending_timeout = 0;
1256 powered_changed(technology);
1261 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1266 if (global_offlinemode == offlinemode)
1269 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1272 * This is a bit tricky. When you set offlinemode, there is no
1273 * way to differentiate between attempting offline mode and
1274 * resuming offlinemode from last saved profile. We need that
1275 * information in rfkill_update, otherwise it falls back on the
1276 * technology's persistent state. Hence we set the offline mode here
1277 * but save it & call the notifier only if its successful.
1280 global_offlinemode = offlinemode;
1282 /* Traverse technology list, enable/disable each technology. */
1283 for (list = technology_list; list; list = list->next) {
1284 struct connman_technology *technology = list->data;
1287 err = technology_disable(technology, FALSE);
1289 if (!offlinemode && technology->enable_persistent)
1290 err = technology_enable(technology, FALSE);
1293 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1294 connman_technology_save_offlinemode();
1295 __connman_notifier_offlinemode(offlinemode);
1297 global_offlinemode = connman_technology_load_offlinemode();
1302 void __connman_technology_set_connected(enum connman_service_type type,
1303 connman_bool_t connected)
1305 struct connman_technology *technology;
1307 technology = technology_find(type);
1308 if (technology == NULL)
1311 DBG("technology %p connected %d", technology, connected);
1313 technology->connected = connected;
1315 connman_dbus_property_changed_basic(technology->path,
1316 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1317 DBUS_TYPE_BOOLEAN, &connected);
1320 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1321 connman_bool_t softblock,
1322 connman_bool_t hardblock)
1324 gboolean apply = TRUE;
1325 GList *start, *list;
1327 if (technology->hardblocked == hardblock)
1328 goto softblock_change;
1330 start = g_hash_table_get_values(rfkill_list);
1331 for (list = start; list != NULL; list = list->next) {
1332 struct connman_rfkill *rfkill = list->data;
1334 if (rfkill->type != technology->type)
1337 if (rfkill->hardblock != hardblock)
1344 goto softblock_change;
1346 technology->hardblocked = hardblock;
1348 if (hardblock == TRUE) {
1349 DBG("%s is switched off.", get_name(technology->type));
1350 technology_disable(technology, TRUE);
1351 technology_dbus_unregister(technology);
1353 technology_enable(technology, TRUE);
1354 technology_dbus_register(technology);
1358 technology->softblocked = softblock;
1360 return technology->hardblocked;
1363 int __connman_technology_add_rfkill(unsigned int index,
1364 enum connman_service_type type,
1365 connman_bool_t softblock,
1366 connman_bool_t hardblock)
1368 struct connman_technology *technology;
1369 struct connman_rfkill *rfkill;
1371 DBG("index %u type %d soft %u hard %u", index, type,
1372 softblock, hardblock);
1374 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1378 rfkill = g_try_new0(struct connman_rfkill, 1);
1382 rfkill->index = index;
1383 rfkill->type = type;
1384 rfkill->softblock = softblock;
1385 rfkill->hardblock = hardblock;
1387 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1390 technology = technology_get(type);
1391 /* If there is no driver for this type, ignore it. */
1392 if (technology == NULL)
1395 technology->rfkill_driven = TRUE;
1397 /* If hardblocked, there is no need to handle softblocked state */
1398 if (technology_apply_rfkill_change(technology,
1399 softblock, hardblock) == TRUE)
1403 * Depending on softblocked state we unblock/block according to
1404 * offlinemode and persistente state.
1406 if (technology->softblocked == TRUE &&
1407 global_offlinemode == FALSE &&
1408 technology->enable_persistent == TRUE)
1409 return __connman_rfkill_block(type, FALSE);
1410 else if (technology->softblocked == FALSE &&
1411 global_offlinemode == TRUE &&
1412 technology->enable_persistent == FALSE)
1413 return __connman_rfkill_block(type, TRUE);
1418 int __connman_technology_update_rfkill(unsigned int index,
1419 enum connman_service_type type,
1420 connman_bool_t softblock,
1421 connman_bool_t hardblock)
1423 struct connman_technology *technology;
1424 struct connman_rfkill *rfkill;
1426 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1428 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1432 if (rfkill->softblock == softblock &&
1433 rfkill->hardblock == hardblock)
1436 rfkill->softblock = softblock;
1437 rfkill->hardblock = hardblock;
1439 technology = technology_find(type);
1440 /* If there is no driver for this type, ignore it. */
1441 if (technology == NULL)
1444 /* If hardblocked, there is no need to handle softblocked state */
1445 if (technology_apply_rfkill_change(technology,
1446 softblock, hardblock) == TRUE)
1449 if (global_offlinemode == TRUE)
1453 * Depending on softblocked state we unblock/block according to
1456 if (technology->softblocked == TRUE &&
1457 technology->enable_persistent == TRUE)
1458 return __connman_rfkill_block(type, FALSE);
1459 else if (technology->softblocked == FALSE &&
1460 technology->enable_persistent == FALSE)
1461 return __connman_rfkill_block(type, TRUE);
1466 int __connman_technology_remove_rfkill(unsigned int index,
1467 enum connman_service_type type)
1469 struct connman_technology *technology;
1470 struct connman_rfkill *rfkill;
1472 DBG("index %u", index);
1474 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1478 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1480 technology = technology_find(type);
1481 if (technology == NULL)
1484 technology_put(technology);
1489 int __connman_technology_init(void)
1493 connection = connman_dbus_get_connection();
1495 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1498 global_offlinemode = connman_technology_load_offlinemode();
1500 /* This will create settings file if it is missing */
1501 connman_technology_save_offlinemode();
1506 void __connman_technology_cleanup(void)
1510 g_hash_table_destroy(rfkill_list);
1512 dbus_connection_unref(connection);