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 hardblocked;
79 static GSList *driver_list = NULL;
81 static gint compare_priority(gconstpointer a, gconstpointer b)
83 const struct connman_technology_driver *driver1 = a;
84 const struct connman_technology_driver *driver2 = b;
86 return driver2->priority - driver1->priority;
89 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
91 struct connman_rfkill *rfkill = value;
92 enum connman_service_type type = GPOINTER_TO_INT(user_data);
94 /* Calling _technology_rfkill_add will update the tech. */
95 if (rfkill->type == type)
96 __connman_technology_add_rfkill(rfkill->index, type,
97 rfkill->softblock, rfkill->hardblock);
101 * connman_technology_driver_register:
102 * @driver: Technology driver definition
104 * Register a new technology driver
106 * Returns: %0 on success
108 int connman_technology_driver_register(struct connman_technology_driver *driver)
111 struct connman_device *device;
112 enum connman_service_type type;
114 DBG("Registering %s driver", driver->name);
116 driver_list = g_slist_insert_sorted(driver_list, driver,
119 if (techless_device_list == NULL)
123 * Check for technology less devices if this driver
124 * can service any of them.
126 for (list = techless_device_list; list; 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);
140 /* Check for orphaned rfkill switches. */
141 g_hash_table_foreach(rfkill_list, rfkill_check,
142 GINT_TO_POINTER(driver->type));
148 * connman_technology_driver_unregister:
149 * @driver: Technology driver definition
151 * Remove a previously registered technology driver
153 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
156 struct connman_technology *technology;
158 DBG("Unregistering driver %p name %s", driver, driver->name);
160 for (list = technology_list; list; list = list->next) {
161 technology = list->data;
163 if (technology->driver == NULL)
166 if (technology->type == driver->type) {
167 technology->driver->remove(technology);
168 technology->driver = NULL;
172 driver_list = g_slist_remove(driver_list, driver);
175 static void tethering_changed(struct connman_technology *technology)
177 connman_bool_t tethering = technology->tethering;
179 connman_dbus_property_changed_basic(technology->path,
180 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
181 DBUS_TYPE_BOOLEAN, &tethering);
184 void connman_technology_tethering_notify(struct connman_technology *technology,
185 connman_bool_t enabled)
189 DBG("technology %p enabled %u", technology, enabled);
191 if (technology->tethering == enabled)
194 technology->tethering = enabled;
196 tethering_changed(technology);
199 __connman_tethering_set_enabled();
201 for (list = technology_list; list; list = list->next) {
202 struct connman_technology *other_tech = list->data;
203 if (other_tech->tethering == TRUE)
207 __connman_tethering_set_disabled();
211 static int set_tethering(struct connman_technology *technology,
212 connman_bool_t enabled)
214 const char *ident, *passphrase, *bridge;
216 ident = technology->tethering_ident;
217 passphrase = technology->tethering_passphrase;
219 if (technology->driver == NULL ||
220 technology->driver->set_tethering == NULL)
223 bridge = __connman_tethering_get_bridge();
227 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
228 (ident == NULL || passphrase == NULL))
231 return technology->driver->set_tethering(technology, ident, passphrase,
235 void connman_technology_regdom_notify(struct connman_technology *technology,
241 connman_error("Failed to set regulatory domain");
243 DBG("Regulatory domain set to %s", alpha2);
245 g_free(technology->regdom);
246 technology->regdom = g_strdup(alpha2);
249 static int set_regdom_by_device(struct connman_technology *technology,
254 for (list = technology->device_list; list; list = list->next) {
255 struct connman_device *device = list->data;
257 if (connman_device_set_regdom(device, alpha2) != 0)
264 int connman_technology_set_regdom(const char *alpha2)
268 for (list = technology_list; list; list = list->next) {
269 struct connman_technology *technology = list->data;
271 if (set_regdom_by_device(technology, alpha2) != 0) {
272 if (technology->driver == NULL)
275 if (technology->driver->set_regdom != NULL)
276 technology->driver->set_regdom(technology,
284 static void free_rfkill(gpointer data)
286 struct connman_rfkill *rfkill = data;
291 static const char *get_name(enum connman_service_type type)
294 case CONNMAN_SERVICE_TYPE_UNKNOWN:
295 case CONNMAN_SERVICE_TYPE_SYSTEM:
296 case CONNMAN_SERVICE_TYPE_GPS:
297 case CONNMAN_SERVICE_TYPE_VPN:
298 case CONNMAN_SERVICE_TYPE_GADGET:
300 case CONNMAN_SERVICE_TYPE_ETHERNET:
302 case CONNMAN_SERVICE_TYPE_WIFI:
304 case CONNMAN_SERVICE_TYPE_WIMAX:
306 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
308 case CONNMAN_SERVICE_TYPE_CELLULAR:
315 static void technology_save(struct connman_technology *technology)
320 DBG("technology %p", technology);
322 keyfile = __connman_storage_load_global();
324 keyfile = g_key_file_new();
326 identifier = g_strdup_printf("%s", get_name(technology->type));
327 if (identifier == NULL)
330 g_key_file_set_boolean(keyfile, identifier, "Enable",
331 technology->enable_persistent);
333 if (technology->tethering_ident != NULL)
334 g_key_file_set_string(keyfile, identifier,
335 "Tethering.Identifier",
336 technology->tethering_ident);
338 if (technology->tethering_passphrase != NULL)
339 g_key_file_set_string(keyfile, identifier,
340 "Tethering.Passphrase",
341 technology->tethering_passphrase);
346 __connman_storage_save_global(keyfile);
348 g_key_file_free(keyfile);
353 static void technology_load(struct connman_technology *technology)
357 GError *error = NULL;
358 connman_bool_t enable;
360 DBG("technology %p", technology);
362 keyfile = __connman_storage_load_global();
363 /* Fallback on disabling technology if file not found. */
364 if (keyfile == NULL) {
365 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
366 /* We enable ethernet by default */
367 technology->enable_persistent = TRUE;
369 technology->enable_persistent = FALSE;
373 identifier = g_strdup_printf("%s", get_name(technology->type));
374 if (identifier == NULL)
377 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
379 technology->enable_persistent = enable;
381 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
382 technology->enable_persistent = TRUE;
384 technology->enable_persistent = FALSE;
386 technology_save(technology);
387 g_clear_error(&error);
390 technology->tethering_ident = g_key_file_get_string(keyfile,
391 identifier, "Tethering.Identifier", NULL);
393 technology->tethering_passphrase = g_key_file_get_string(keyfile,
394 identifier, "Tethering.Passphrase", NULL);
398 g_key_file_free(keyfile);
403 connman_bool_t __connman_technology_get_offlinemode(void)
405 return global_offlinemode;
408 static void connman_technology_save_offlinemode()
412 keyfile = __connman_storage_load_global();
414 keyfile = g_key_file_new();
416 g_key_file_set_boolean(keyfile, "global",
417 "OfflineMode", global_offlinemode);
419 __connman_storage_save_global(keyfile);
421 g_key_file_free(keyfile);
426 static connman_bool_t connman_technology_load_offlinemode()
429 GError *error = NULL;
430 connman_bool_t offlinemode;
432 /* If there is a error, we enable offlinemode */
433 keyfile = __connman_storage_load_global();
437 offlinemode = g_key_file_get_boolean(keyfile, "global",
438 "OfflineMode", &error);
441 g_clear_error(&error);
444 g_key_file_free(keyfile);
449 static void append_properties(DBusMessageIter *iter,
450 struct connman_technology *technology)
452 DBusMessageIter dict;
454 connman_bool_t powered;
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 if (technology->enabled > 0)
473 connman_dbus_dict_append_basic(&dict, "Powered",
474 DBUS_TYPE_BOOLEAN, &powered);
476 connman_dbus_dict_append_basic(&dict, "Connected",
478 &technology->connected);
480 connman_dbus_dict_append_basic(&dict, "Tethering",
482 &technology->tethering);
484 if (technology->tethering_ident != NULL)
485 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
487 &technology->tethering_ident);
489 if (technology->tethering_passphrase != NULL)
490 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
492 &technology->tethering_passphrase);
494 connman_dbus_dict_close(iter, &dict);
497 static void technology_added_signal(struct connman_technology *technology)
500 DBusMessageIter iter;
502 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
503 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
507 dbus_message_iter_init_append(signal, &iter);
508 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
510 append_properties(&iter, technology);
512 dbus_connection_send(connection, signal, NULL);
513 dbus_message_unref(signal);
516 static void technology_removed_signal(struct connman_technology *technology)
518 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
519 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
520 DBUS_TYPE_OBJECT_PATH, &technology->path,
524 static DBusMessage *get_properties(DBusConnection *conn,
525 DBusMessage *message, void *user_data)
527 struct connman_technology *technology = user_data;
529 DBusMessageIter iter;
531 reply = dbus_message_new_method_return(message);
535 dbus_message_iter_init_append(reply, &iter);
536 append_properties(&iter, technology);
541 void __connman_technology_list_struct(DBusMessageIter *array)
544 DBusMessageIter entry;
546 for (list = technology_list; list; list = list->next) {
547 struct connman_technology *technology = list->data;
549 if (technology->path == NULL)
552 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
554 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
556 append_properties(&entry, technology);
557 dbus_message_iter_close_container(array, &entry);
561 static gboolean technology_pending_reply(gpointer user_data)
563 struct connman_technology *technology = user_data;
566 /* Power request timedout, send ETIMEDOUT. */
567 if (technology->pending_reply != NULL) {
568 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
570 g_dbus_send_message(connection, reply);
572 dbus_message_unref(technology->pending_reply);
573 technology->pending_reply = NULL;
574 technology->pending_timeout = 0;
580 static int technology_enable(struct connman_technology *technology,
581 connman_bool_t hardblock)
586 DBG("technology %p enable", technology);
588 __sync_synchronize();
589 if (technology->enabled > 0) {
594 if (technology->pending_reply != NULL) {
599 if (hardblock == TRUE && technology->enable_persistent == FALSE)
602 __connman_rfkill_block(technology->type, FALSE);
604 for (list = technology->device_list; list; list = list->next) {
605 struct connman_device *device = list->data;
607 err = __connman_device_enable(device);
614 static int technology_disable(struct connman_technology *technology,
615 connman_bool_t hardblock)
620 DBG("technology %p disable", technology);
622 __sync_synchronize();
623 if (technology->enabled == 0) {
628 if (technology->pending_reply != NULL) {
633 if (technology->tethering == TRUE)
634 set_tethering(technology, FALSE);
636 if (hardblock == FALSE)
637 __connman_rfkill_block(technology->type, TRUE);
639 for (list = technology->device_list; list; list = list->next) {
640 struct connman_device *device = list->data;
642 err = __connman_device_disable(device);
649 static DBusMessage *set_powered(struct connman_technology *technology,
650 DBusMessage *msg, connman_bool_t powered)
652 DBusMessage *reply = NULL;
656 err = technology_enable(technology, FALSE);
658 err = technology_disable(technology, FALSE);
661 technology->enable_persistent = powered;
662 technology_save(technology);
665 if (err == -EINPROGRESS) {
666 technology->pending_reply = dbus_message_ref(msg);
667 technology->pending_timeout = g_timeout_add_seconds(10,
668 technology_pending_reply, technology);
669 } else if (err == -EALREADY) {
671 reply = __connman_error_already_enabled(msg);
673 reply = __connman_error_already_disabled(msg);
675 reply = __connman_error_failed(msg, -err);
677 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
682 static DBusMessage *set_property(DBusConnection *conn,
683 DBusMessage *msg, void *data)
685 struct connman_technology *technology = data;
686 DBusMessageIter iter, value;
690 DBG("conn %p", conn);
692 if (dbus_message_iter_init(msg, &iter) == FALSE)
693 return __connman_error_invalid_arguments(msg);
695 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
696 return __connman_error_invalid_arguments(msg);
698 dbus_message_iter_get_basic(&iter, &name);
699 dbus_message_iter_next(&iter);
701 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
702 return __connman_error_invalid_arguments(msg);
704 dbus_message_iter_recurse(&iter, &value);
706 type = dbus_message_iter_get_arg_type(&value);
708 DBG("property %s", name);
710 if (g_str_equal(name, "Tethering") == TRUE) {
712 connman_bool_t tethering;
714 if (type != DBUS_TYPE_BOOLEAN)
715 return __connman_error_invalid_arguments(msg);
717 dbus_message_iter_get_basic(&value, &tethering);
719 if (technology->tethering == tethering) {
720 if (tethering == FALSE)
721 return __connman_error_already_disabled(msg);
723 return __connman_error_already_enabled(msg);
726 err = set_tethering(technology, tethering);
728 return __connman_error_failed(msg, -err);
730 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
733 dbus_message_iter_get_basic(&value, &str);
735 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
736 return __connman_error_not_supported(msg);
738 if (strlen(str) < 1 || strlen(str) > 32)
739 return __connman_error_invalid_arguments(msg);
741 if (g_strcmp0(technology->tethering_ident, str) != 0) {
742 g_free(technology->tethering_ident);
743 technology->tethering_ident = g_strdup(str);
744 technology_save(technology);
746 connman_dbus_property_changed_basic(technology->path,
747 CONNMAN_TECHNOLOGY_INTERFACE,
748 "TetheringIdentifier",
750 &technology->tethering_ident);
752 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
755 dbus_message_iter_get_basic(&value, &str);
757 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
758 return __connman_error_not_supported(msg);
760 if (strlen(str) < 8 || strlen(str) > 63)
761 return __connman_error_passphrase_required(msg);
763 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
764 g_free(technology->tethering_passphrase);
765 technology->tethering_passphrase = g_strdup(str);
766 technology_save(technology);
768 connman_dbus_property_changed_basic(technology->path,
769 CONNMAN_TECHNOLOGY_INTERFACE,
770 "TetheringPassphrase",
772 &technology->tethering_passphrase);
774 } else if (g_str_equal(name, "Powered") == TRUE) {
775 connman_bool_t enable;
777 if (type != DBUS_TYPE_BOOLEAN)
778 return __connman_error_invalid_arguments(msg);
780 dbus_message_iter_get_basic(&value, &enable);
782 return set_powered(technology, msg, enable);
784 return __connman_error_invalid_property(msg);
786 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
789 static struct connman_technology *technology_find(enum connman_service_type type)
793 DBG("type %d", type);
795 for (list = technology_list; list; list = list->next) {
796 struct connman_technology *technology = list->data;
798 if (technology->type == type)
805 static void reply_scan_pending(struct connman_technology *technology, int err)
809 DBG("technology %p err %d", technology, err);
811 while (technology->scan_pending != NULL) {
812 DBusMessage *msg = technology->scan_pending->data;
814 DBG("reply to %s", dbus_message_get_sender(msg));
817 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
819 reply = __connman_error_failed(msg, -err);
820 g_dbus_send_message(connection, reply);
821 dbus_message_unref(msg);
823 technology->scan_pending =
824 g_slist_delete_link(technology->scan_pending,
825 technology->scan_pending);
829 void __connman_technology_scan_started(struct connman_device *device)
831 DBG("device %p", device);
834 void __connman_technology_scan_stopped(struct connman_device *device)
837 struct connman_technology *technology;
838 enum connman_service_type type;
841 type = __connman_device_get_service_type(device);
842 technology = technology_find(type);
844 DBG("technology %p device %p", technology, device);
846 if (technology == NULL)
849 for (list = technology->device_list; list != NULL; list = list->next) {
850 struct connman_device *other_device = list->data;
852 if (device == other_device)
855 if (__connman_device_get_service_type(other_device) != type)
858 if (connman_device_get_scanning(other_device) == TRUE)
863 reply_scan_pending(technology, 0);
866 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
867 int result, const char *alpha2)
869 struct connman_technology *technology;
870 enum connman_service_type type;
872 type = __connman_device_get_service_type(device);
873 technology = technology_find(type);
875 if (technology == NULL)
879 if (technology->driver != NULL &&
880 technology->driver->set_regdom != NULL) {
881 technology->driver->set_regdom(technology, alpha2);
888 connman_technology_regdom_notify(technology, alpha2);
891 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
893 struct connman_technology *technology = data;
896 DBG ("technology %p request from %s", technology,
897 dbus_message_get_sender(msg));
899 dbus_message_ref(msg);
900 technology->scan_pending =
901 g_slist_prepend(technology->scan_pending, msg);
903 err = __connman_device_request_scan(technology->type);
905 reply_scan_pending(technology, err);
910 static const GDBusMethodTable technology_methods[] = {
911 { GDBUS_DEPRECATED_METHOD("GetProperties",
912 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
914 { GDBUS_ASYNC_METHOD("SetProperty",
915 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
916 NULL, set_property) },
917 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
921 static const GDBusSignalTable technology_signals[] = {
922 { GDBUS_SIGNAL("PropertyChanged",
923 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
927 static struct connman_technology *technology_get(enum connman_service_type type)
929 struct connman_technology *technology;
930 struct connman_technology_driver *driver = NULL;
935 DBG("type %d", type);
937 str = __connman_service_type2string(type);
941 technology = technology_find(type);
942 if (technology != NULL) {
943 __sync_fetch_and_add(&technology->refcount, 1);
947 /* First check if we have a driver for this technology type */
948 for (list = driver_list; list; list = list->next) {
951 if (driver->type == type)
957 if (driver == NULL) {
958 DBG("No matching driver found for %s.",
959 __connman_service_type2string(type));
963 technology = g_try_new0(struct connman_technology, 1);
964 if (technology == NULL)
967 technology->refcount = 1;
969 if (type == CONNMAN_SERVICE_TYPE_ETHERNET)
970 technology->hardblocked = FALSE;
972 technology->hardblocked = TRUE;
974 technology->type = type;
975 technology->path = g_strdup_printf("%s/technology/%s",
978 technology->device_list = NULL;
980 technology->pending_reply = NULL;
982 technology_load(technology);
984 if (g_dbus_register_interface(connection, technology->path,
985 CONNMAN_TECHNOLOGY_INTERFACE,
986 technology_methods, technology_signals,
987 NULL, technology, NULL) == FALSE) {
988 connman_error("Failed to register %s", technology->path);
993 technology_list = g_slist_prepend(technology_list, technology);
995 technology_added_signal(technology);
997 technology->driver = driver;
998 err = driver->probe(technology);
1000 DBG("Driver probe failed for technology %p", technology);
1002 DBG("technology %p", technology);
1007 static void technology_put(struct connman_technology *technology)
1009 DBG("technology %p", technology);
1011 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1014 reply_scan_pending(technology, -EINTR);
1016 if (technology->driver) {
1017 technology->driver->remove(technology);
1018 technology->driver = NULL;
1021 technology_list = g_slist_remove(technology_list, technology);
1023 technology_removed_signal(technology);
1025 g_dbus_unregister_interface(connection, technology->path,
1026 CONNMAN_TECHNOLOGY_INTERFACE);
1028 g_slist_free(technology->device_list);
1030 g_free(technology->path);
1031 g_free(technology->regdom);
1032 g_free(technology->tethering_ident);
1033 g_free(technology->tethering_passphrase);
1037 void __connman_technology_add_interface(enum connman_service_type type,
1038 int index, const char *name, const char *ident)
1040 struct connman_technology *technology;
1043 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1044 case CONNMAN_SERVICE_TYPE_SYSTEM:
1046 case CONNMAN_SERVICE_TYPE_ETHERNET:
1047 case CONNMAN_SERVICE_TYPE_WIFI:
1048 case CONNMAN_SERVICE_TYPE_WIMAX:
1049 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1050 case CONNMAN_SERVICE_TYPE_CELLULAR:
1051 case CONNMAN_SERVICE_TYPE_GPS:
1052 case CONNMAN_SERVICE_TYPE_VPN:
1053 case CONNMAN_SERVICE_TYPE_GADGET:
1057 connman_info("Adding interface %s [ %s ]", name,
1058 __connman_service_type2string(type));
1060 technology = technology_find(type);
1062 if (technology == NULL || technology->driver == NULL
1063 || technology->driver->add_interface == NULL)
1066 technology->driver->add_interface(technology,
1067 index, name, ident);
1070 void __connman_technology_remove_interface(enum connman_service_type type,
1071 int index, const char *name, const char *ident)
1073 struct connman_technology *technology;
1076 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1077 case CONNMAN_SERVICE_TYPE_SYSTEM:
1079 case CONNMAN_SERVICE_TYPE_ETHERNET:
1080 case CONNMAN_SERVICE_TYPE_WIFI:
1081 case CONNMAN_SERVICE_TYPE_WIMAX:
1082 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1083 case CONNMAN_SERVICE_TYPE_CELLULAR:
1084 case CONNMAN_SERVICE_TYPE_GPS:
1085 case CONNMAN_SERVICE_TYPE_VPN:
1086 case CONNMAN_SERVICE_TYPE_GADGET:
1090 connman_info("Remove interface %s [ %s ]", name,
1091 __connman_service_type2string(type));
1093 technology = technology_find(type);
1095 if (technology == NULL || technology->driver == NULL)
1098 if (technology->driver->remove_interface)
1099 technology->driver->remove_interface(technology, index);
1102 int __connman_technology_add_device(struct connman_device *device)
1104 struct connman_technology *technology;
1105 enum connman_service_type type;
1107 DBG("device %p", device);
1109 type = __connman_device_get_service_type(device);
1111 technology = technology_get(type);
1112 if (technology == NULL) {
1114 * Since no driver can be found for this device at the moment we
1115 * add it to the techless device list.
1117 techless_device_list = g_slist_prepend(techless_device_list,
1123 if (technology->enable_persistent &&
1124 global_offlinemode == FALSE &&
1125 technology->hardblocked == FALSE) {
1126 int err = __connman_device_enable(device);
1128 * connman_technology_add_device() calls __connman_device_enable()
1129 * but since the device is already enabled, the calls does not
1130 * propagate through to connman_technology_enabled via
1131 * connman_device_set_powered.
1133 if (err == -EALREADY)
1134 __connman_technology_enabled(type);
1136 /* if technology persistent state is offline or hardblocked */
1137 if (technology->enable_persistent == FALSE ||
1138 technology->hardblocked == TRUE)
1139 __connman_device_disable(device);
1141 technology->device_list = g_slist_prepend(technology->device_list,
1147 int __connman_technology_remove_device(struct connman_device *device)
1149 struct connman_technology *technology;
1150 enum connman_service_type type;
1152 DBG("device %p", device);
1154 type = __connman_device_get_service_type(device);
1156 technology = technology_find(type);
1157 if (technology == NULL) {
1158 techless_device_list = g_slist_remove(techless_device_list,
1163 technology->device_list = g_slist_remove(technology->device_list,
1165 technology_put(technology);
1170 static void powered_changed(struct connman_technology *technology)
1172 connman_bool_t powered;
1174 __sync_synchronize();
1175 if (technology->enabled >0)
1180 connman_dbus_property_changed_basic(technology->path,
1181 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1182 DBUS_TYPE_BOOLEAN, &powered);
1185 int __connman_technology_enabled(enum connman_service_type type)
1187 struct connman_technology *technology;
1189 technology = technology_find(type);
1190 if (technology == NULL)
1193 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1196 powered_changed(technology);
1198 if (technology->pending_reply != NULL) {
1199 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1200 dbus_message_unref(technology->pending_reply);
1201 g_source_remove(technology->pending_timeout);
1202 technology->pending_reply = NULL;
1203 technology->pending_timeout = 0;
1209 int __connman_technology_disabled(enum connman_service_type type)
1211 struct connman_technology *technology;
1213 technology = technology_find(type);
1214 if (technology == NULL)
1217 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1218 return -EINPROGRESS;
1220 if (technology->pending_reply != NULL) {
1221 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1222 dbus_message_unref(technology->pending_reply);
1223 g_source_remove(technology->pending_timeout);
1224 technology->pending_reply = NULL;
1225 technology->pending_timeout = 0;
1228 powered_changed(technology);
1233 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1238 if (global_offlinemode == offlinemode)
1241 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1244 * This is a bit tricky. When you set offlinemode, there is no
1245 * way to differentiate between attempting offline mode and
1246 * resuming offlinemode from last saved profile. We need that
1247 * information in rfkill_update, otherwise it falls back on the
1248 * technology's persistent state. Hence we set the offline mode here
1249 * but save it & call the notifier only if its successful.
1252 global_offlinemode = offlinemode;
1254 /* Traverse technology list, enable/disable each technology. */
1255 for (list = technology_list; list; list = list->next) {
1256 struct connman_technology *technology = list->data;
1259 err = technology_disable(technology, FALSE);
1261 if (!offlinemode && technology->enable_persistent)
1262 err = technology_enable(technology, FALSE);
1265 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1266 connman_technology_save_offlinemode();
1267 __connman_notifier_offlinemode(offlinemode);
1269 global_offlinemode = connman_technology_load_offlinemode();
1274 void __connman_technology_set_connected(enum connman_service_type type,
1275 connman_bool_t connected)
1277 struct connman_technology *technology;
1279 technology = technology_find(type);
1280 if (technology == NULL)
1283 DBG("technology %p connected %d", technology, connected);
1285 technology->connected = connected;
1287 connman_dbus_property_changed_basic(technology->path,
1288 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1289 DBUS_TYPE_BOOLEAN, &connected);
1292 static void technology_apply_hardblock_change(struct connman_technology *technology,
1293 connman_bool_t hardblock)
1295 gboolean apply = TRUE;
1296 GList *start, *list;
1298 if (technology->hardblocked == hardblock)
1301 start = g_hash_table_get_values(rfkill_list);
1302 for (list = start; list != NULL; list = list->next) {
1303 struct connman_rfkill *rfkill = list->data;
1305 if (rfkill->type != technology->type)
1308 if (rfkill->hardblock != hardblock)
1317 technology->hardblocked = hardblock;
1319 if (hardblock == TRUE) {
1320 DBG("%s is switched off.", get_name(technology->type));
1321 technology_disable(technology, TRUE);
1323 technology_enable(technology, TRUE);
1327 int __connman_technology_add_rfkill(unsigned int index,
1328 enum connman_service_type type,
1329 connman_bool_t softblock,
1330 connman_bool_t hardblock)
1332 struct connman_technology *technology;
1333 struct connman_rfkill *rfkill;
1335 DBG("index %u type %d soft %u hard %u", index, type,
1336 softblock, hardblock);
1338 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1342 rfkill = g_try_new0(struct connman_rfkill, 1);
1346 rfkill->index = index;
1347 rfkill->type = type;
1348 rfkill->softblock = softblock;
1349 rfkill->hardblock = hardblock;
1351 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1354 technology = technology_get(type);
1355 /* If there is no driver for this type, ignore it. */
1356 if (technology == NULL)
1359 technology_apply_hardblock_change(technology, hardblock);
1362 * If Offline mode is on, we softblock the device if it isnt already.
1363 * If Offline mode is off, we rely on the persistent state of tech.
1365 if (global_offlinemode) {
1367 return __connman_rfkill_block(type, TRUE);
1369 if (technology->enable_persistent && softblock)
1370 return __connman_rfkill_block(type, FALSE);
1371 /* if technology persistent state is offline */
1372 if (!technology->enable_persistent && !softblock)
1373 return __connman_rfkill_block(type, TRUE);
1379 int __connman_technology_update_rfkill(unsigned int index,
1380 enum connman_service_type type,
1381 connman_bool_t softblock,
1382 connman_bool_t hardblock)
1384 struct connman_technology *technology;
1385 struct connman_rfkill *rfkill;
1387 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1389 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1393 if (rfkill->softblock == softblock &&
1394 rfkill->hardblock == hardblock)
1397 rfkill->softblock = softblock;
1398 rfkill->hardblock = hardblock;
1400 technology = technology_find(type);
1401 /* If there is no driver for this type, ignore it. */
1402 if (technology == NULL)
1405 technology_apply_hardblock_change(technology, hardblock);
1407 if (!global_offlinemode) {
1408 if (technology->enable_persistent && softblock)
1409 return __connman_rfkill_block(type, FALSE);
1410 if (!technology->enable_persistent && !softblock)
1411 return __connman_rfkill_block(type, TRUE);
1417 int __connman_technology_remove_rfkill(unsigned int index,
1418 enum connman_service_type type)
1420 struct connman_technology *technology;
1421 struct connman_rfkill *rfkill;
1423 DBG("index %u", index);
1425 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1429 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1431 technology = technology_find(type);
1432 if (technology == NULL)
1435 technology_put(technology);
1440 int __connman_technology_init(void)
1444 connection = connman_dbus_get_connection();
1446 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1449 global_offlinemode = connman_technology_load_offlinemode();
1451 /* This will create settings file if it is missing */
1452 connman_technology_save_offlinemode();
1457 void __connman_technology_cleanup(void)
1461 g_hash_table_destroy(rfkill_list);
1463 dbus_connection_unref(connection);