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,
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;
458 connman_dbus_dict_open(iter, &dict);
460 str = get_name(technology->type);
462 connman_dbus_dict_append_basic(&dict, "Name",
463 DBUS_TYPE_STRING, &str);
465 str = __connman_service_type2string(technology->type);
467 connman_dbus_dict_append_basic(&dict, "Type",
468 DBUS_TYPE_STRING, &str);
470 __sync_synchronize();
471 connman_dbus_dict_append_basic(&dict, "Powered",
473 &technology->enabled);
475 connman_dbus_dict_append_basic(&dict, "Connected",
477 &technology->connected);
479 connman_dbus_dict_append_basic(&dict, "Tethering",
481 &technology->tethering);
483 if (technology->tethering_ident != NULL)
484 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
486 &technology->tethering_ident);
488 if (technology->tethering_passphrase != NULL)
489 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
491 &technology->tethering_passphrase);
493 connman_dbus_dict_close(iter, &dict);
496 static void technology_added_signal(struct connman_technology *technology)
499 DBusMessageIter iter;
501 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
502 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
506 dbus_message_iter_init_append(signal, &iter);
507 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
509 append_properties(&iter, technology);
511 dbus_connection_send(connection, signal, NULL);
512 dbus_message_unref(signal);
515 static void technology_removed_signal(struct connman_technology *technology)
517 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
518 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
519 DBUS_TYPE_OBJECT_PATH, &technology->path,
523 static DBusMessage *get_properties(DBusConnection *conn,
524 DBusMessage *message, void *user_data)
526 struct connman_technology *technology = user_data;
528 DBusMessageIter iter;
530 reply = dbus_message_new_method_return(message);
534 dbus_message_iter_init_append(reply, &iter);
535 append_properties(&iter, technology);
540 void __connman_technology_list_struct(DBusMessageIter *array)
543 DBusMessageIter entry;
545 for (list = technology_list; list; list = list->next) {
546 struct connman_technology *technology = list->data;
548 if (technology->path == NULL ||
549 (technology->rfkill_driven == TRUE &&
550 technology->hardblocked == TRUE))
553 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
555 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
557 append_properties(&entry, technology);
558 dbus_message_iter_close_container(array, &entry);
562 static gboolean technology_pending_reply(gpointer user_data)
564 struct connman_technology *technology = user_data;
567 /* Power request timedout, send ETIMEDOUT. */
568 if (technology->pending_reply != NULL) {
569 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
571 g_dbus_send_message(connection, reply);
573 dbus_message_unref(technology->pending_reply);
574 technology->pending_reply = NULL;
575 technology->pending_timeout = 0;
581 static int technology_affect_devices(struct connman_technology *technology,
582 connman_bool_t enable_device)
587 for (list = technology->device_list; list; list = list->next) {
588 struct connman_device *device = list->data;
590 if (enable_device == TRUE)
591 err = __connman_device_enable(device);
593 err = __connman_device_disable(device);
599 static int technology_enable(struct connman_technology *technology)
601 DBG("technology %p enable", technology);
603 __sync_synchronize();
604 if (technology->enabled == TRUE)
607 if (technology->pending_reply != NULL)
610 if (technology->rfkill_driven == TRUE)
611 return __connman_rfkill_block(technology->type, FALSE);
613 return technology_affect_devices(technology, TRUE);
616 static int technology_disable(struct connman_technology *technology)
618 DBG("technology %p disable", technology);
620 __sync_synchronize();
621 if (technology->enabled == FALSE)
624 if (technology->pending_reply != NULL)
627 if (technology->tethering == TRUE)
628 set_tethering(technology, FALSE);
630 if (technology->rfkill_driven == TRUE)
631 return __connman_rfkill_block(technology->type, TRUE);
633 return technology_affect_devices(technology, FALSE);
636 static DBusMessage *set_powered(struct connman_technology *technology,
637 DBusMessage *msg, connman_bool_t powered)
639 DBusMessage *reply = NULL;
642 if (technology->rfkill_driven && technology->hardblocked == TRUE) {
648 err = technology_enable(technology);
650 err = technology_disable(technology);
653 technology->enable_persistent = powered;
654 technology_save(technology);
658 if (err == -EINPROGRESS) {
659 technology->pending_reply = dbus_message_ref(msg);
660 technology->pending_timeout = g_timeout_add_seconds(10,
661 technology_pending_reply, technology);
662 } else if (err == -EALREADY) {
664 reply = __connman_error_already_enabled(msg);
666 reply = __connman_error_already_disabled(msg);
668 reply = __connman_error_failed(msg, -err);
670 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
675 static DBusMessage *set_property(DBusConnection *conn,
676 DBusMessage *msg, void *data)
678 struct connman_technology *technology = data;
679 DBusMessageIter iter, value;
683 DBG("conn %p", conn);
685 if (dbus_message_iter_init(msg, &iter) == FALSE)
686 return __connman_error_invalid_arguments(msg);
688 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
689 return __connman_error_invalid_arguments(msg);
691 dbus_message_iter_get_basic(&iter, &name);
692 dbus_message_iter_next(&iter);
694 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
695 return __connman_error_invalid_arguments(msg);
697 dbus_message_iter_recurse(&iter, &value);
699 type = dbus_message_iter_get_arg_type(&value);
701 DBG("property %s", name);
703 if (g_str_equal(name, "Tethering") == TRUE) {
705 connman_bool_t tethering;
707 if (type != DBUS_TYPE_BOOLEAN)
708 return __connman_error_invalid_arguments(msg);
710 dbus_message_iter_get_basic(&value, &tethering);
712 if (technology->tethering == tethering) {
713 if (tethering == FALSE)
714 return __connman_error_already_disabled(msg);
716 return __connman_error_already_enabled(msg);
719 err = set_tethering(technology, tethering);
721 return __connman_error_failed(msg, -err);
723 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
726 dbus_message_iter_get_basic(&value, &str);
728 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
729 return __connman_error_not_supported(msg);
731 if (strlen(str) < 1 || strlen(str) > 32)
732 return __connman_error_invalid_arguments(msg);
734 if (g_strcmp0(technology->tethering_ident, str) != 0) {
735 g_free(technology->tethering_ident);
736 technology->tethering_ident = g_strdup(str);
737 technology_save(technology);
739 connman_dbus_property_changed_basic(technology->path,
740 CONNMAN_TECHNOLOGY_INTERFACE,
741 "TetheringIdentifier",
743 &technology->tethering_ident);
745 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
748 dbus_message_iter_get_basic(&value, &str);
750 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
751 return __connman_error_not_supported(msg);
753 if (strlen(str) < 8 || strlen(str) > 63)
754 return __connman_error_passphrase_required(msg);
756 if (g_strcmp0(technology->tethering_passphrase, str) != 0) {
757 g_free(technology->tethering_passphrase);
758 technology->tethering_passphrase = g_strdup(str);
759 technology_save(technology);
761 connman_dbus_property_changed_basic(technology->path,
762 CONNMAN_TECHNOLOGY_INTERFACE,
763 "TetheringPassphrase",
765 &technology->tethering_passphrase);
767 } else if (g_str_equal(name, "Powered") == TRUE) {
768 connman_bool_t enable;
770 if (type != DBUS_TYPE_BOOLEAN)
771 return __connman_error_invalid_arguments(msg);
773 dbus_message_iter_get_basic(&value, &enable);
775 return set_powered(technology, msg, enable);
777 return __connman_error_invalid_property(msg);
779 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
782 static struct connman_technology *technology_find(enum connman_service_type type)
786 DBG("type %d", type);
788 for (list = technology_list; list; list = list->next) {
789 struct connman_technology *technology = list->data;
791 if (technology->type == type)
798 static void reply_scan_pending(struct connman_technology *technology, int err)
802 DBG("technology %p err %d", technology, err);
804 while (technology->scan_pending != NULL) {
805 DBusMessage *msg = technology->scan_pending->data;
807 DBG("reply to %s", dbus_message_get_sender(msg));
810 reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
812 reply = __connman_error_failed(msg, -err);
813 g_dbus_send_message(connection, reply);
814 dbus_message_unref(msg);
816 technology->scan_pending =
817 g_slist_delete_link(technology->scan_pending,
818 technology->scan_pending);
822 void __connman_technology_scan_started(struct connman_device *device)
824 DBG("device %p", device);
827 void __connman_technology_scan_stopped(struct connman_device *device)
830 struct connman_technology *technology;
831 enum connman_service_type type;
834 type = __connman_device_get_service_type(device);
835 technology = technology_find(type);
837 DBG("technology %p device %p", technology, device);
839 if (technology == NULL)
842 for (list = technology->device_list; list != NULL; list = list->next) {
843 struct connman_device *other_device = list->data;
845 if (device == other_device)
848 if (__connman_device_get_service_type(other_device) != type)
851 if (connman_device_get_scanning(other_device) == TRUE)
856 reply_scan_pending(technology, 0);
859 void __connman_technology_notify_regdom_by_device(struct connman_device *device,
860 int result, const char *alpha2)
862 struct connman_technology *technology;
863 enum connman_service_type type;
865 type = __connman_device_get_service_type(device);
866 technology = technology_find(type);
868 if (technology == NULL)
872 if (technology->driver != NULL &&
873 technology->driver->set_regdom != NULL) {
874 technology->driver->set_regdom(technology, alpha2);
881 connman_technology_regdom_notify(technology, alpha2);
884 static DBusMessage *scan(DBusConnection *conn, DBusMessage *msg, void *data)
886 struct connman_technology *technology = data;
889 DBG ("technology %p request from %s", technology,
890 dbus_message_get_sender(msg));
892 dbus_message_ref(msg);
893 technology->scan_pending =
894 g_slist_prepend(technology->scan_pending, msg);
896 err = __connman_device_request_scan(technology->type);
898 reply_scan_pending(technology, err);
903 static const GDBusMethodTable technology_methods[] = {
904 { GDBUS_DEPRECATED_METHOD("GetProperties",
905 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
907 { GDBUS_ASYNC_METHOD("SetProperty",
908 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
909 NULL, set_property) },
910 { GDBUS_ASYNC_METHOD("Scan", NULL, NULL, scan) },
914 static const GDBusSignalTable technology_signals[] = {
915 { GDBUS_SIGNAL("PropertyChanged",
916 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
920 static gboolean technology_dbus_register(struct connman_technology *technology)
922 if (technology->dbus_registered == TRUE ||
923 (technology->rfkill_driven &&
924 technology->hardblocked == TRUE))
927 if (g_dbus_register_interface(connection, technology->path,
928 CONNMAN_TECHNOLOGY_INTERFACE,
929 technology_methods, technology_signals,
930 NULL, technology, NULL) == FALSE) {
931 connman_error("Failed to register %s", technology->path);
935 technology_added_signal(technology);
936 technology->dbus_registered = TRUE;
941 static struct connman_technology *technology_get(enum connman_service_type type)
943 struct connman_technology *technology;
944 struct connman_technology_driver *driver = NULL;
949 DBG("type %d", type);
951 str = __connman_service_type2string(type);
955 technology = technology_find(type);
956 if (technology != NULL) {
957 __sync_fetch_and_add(&technology->refcount, 1);
961 /* First check if we have a driver for this technology type */
962 for (list = driver_list; list; list = list->next) {
965 if (driver->type == type)
971 if (driver == NULL) {
972 DBG("No matching driver found for %s.",
973 __connman_service_type2string(type));
977 technology = g_try_new0(struct connman_technology, 1);
978 if (technology == NULL)
981 technology->refcount = 1;
983 technology->rfkill_driven = FALSE;
984 technology->softblocked = FALSE;
986 if (type == CONNMAN_SERVICE_TYPE_ETHERNET)
987 technology->hardblocked = FALSE;
989 technology->hardblocked = TRUE;
991 technology->type = type;
992 technology->path = g_strdup_printf("%s/technology/%s",
995 technology->device_list = NULL;
997 technology->pending_reply = NULL;
999 technology_load(technology);
1001 if (technology_dbus_register(technology) == FALSE) {
1006 technology_list = g_slist_prepend(technology_list, technology);
1008 technology->driver = driver;
1009 err = driver->probe(technology);
1011 DBG("Driver probe failed for technology %p", technology);
1013 DBG("technology %p", technology);
1018 static void technology_dbus_unregister(struct connman_technology *technology)
1020 if (technology->dbus_registered == FALSE)
1023 technology_removed_signal(technology);
1024 g_dbus_unregister_interface(connection, technology->path,
1025 CONNMAN_TECHNOLOGY_INTERFACE);
1027 technology->dbus_registered = FALSE;
1030 static void technology_put(struct connman_technology *technology)
1032 DBG("technology %p", technology);
1034 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1037 reply_scan_pending(technology, -EINTR);
1039 if (technology->driver) {
1040 technology->driver->remove(technology);
1041 technology->driver = NULL;
1044 technology_list = g_slist_remove(technology_list, technology);
1046 technology_dbus_unregister(technology);
1048 g_slist_free(technology->device_list);
1050 g_free(technology->path);
1051 g_free(technology->regdom);
1052 g_free(technology->tethering_ident);
1053 g_free(technology->tethering_passphrase);
1057 void __connman_technology_add_interface(enum connman_service_type type,
1058 int index, const char *name, const char *ident)
1060 struct connman_technology *technology;
1063 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1064 case CONNMAN_SERVICE_TYPE_SYSTEM:
1066 case CONNMAN_SERVICE_TYPE_ETHERNET:
1067 case CONNMAN_SERVICE_TYPE_WIFI:
1068 case CONNMAN_SERVICE_TYPE_WIMAX:
1069 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1070 case CONNMAN_SERVICE_TYPE_CELLULAR:
1071 case CONNMAN_SERVICE_TYPE_GPS:
1072 case CONNMAN_SERVICE_TYPE_VPN:
1073 case CONNMAN_SERVICE_TYPE_GADGET:
1077 connman_info("Adding interface %s [ %s ]", name,
1078 __connman_service_type2string(type));
1080 technology = technology_find(type);
1082 if (technology == NULL || technology->driver == NULL
1083 || technology->driver->add_interface == NULL)
1086 technology->driver->add_interface(technology,
1087 index, name, ident);
1090 void __connman_technology_remove_interface(enum connman_service_type type,
1091 int index, const char *name, const char *ident)
1093 struct connman_technology *technology;
1096 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1097 case CONNMAN_SERVICE_TYPE_SYSTEM:
1099 case CONNMAN_SERVICE_TYPE_ETHERNET:
1100 case CONNMAN_SERVICE_TYPE_WIFI:
1101 case CONNMAN_SERVICE_TYPE_WIMAX:
1102 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1103 case CONNMAN_SERVICE_TYPE_CELLULAR:
1104 case CONNMAN_SERVICE_TYPE_GPS:
1105 case CONNMAN_SERVICE_TYPE_VPN:
1106 case CONNMAN_SERVICE_TYPE_GADGET:
1110 connman_info("Remove interface %s [ %s ]", name,
1111 __connman_service_type2string(type));
1113 technology = technology_find(type);
1115 if (technology == NULL || technology->driver == NULL)
1118 if (technology->driver->remove_interface)
1119 technology->driver->remove_interface(technology, index);
1122 int __connman_technology_add_device(struct connman_device *device)
1124 struct connman_technology *technology;
1125 enum connman_service_type type;
1127 DBG("device %p", device);
1129 type = __connman_device_get_service_type(device);
1131 technology = technology_get(type);
1132 if (technology == NULL) {
1134 * Since no driver can be found for this device at the moment we
1135 * add it to the techless device list.
1137 techless_device_list = g_slist_prepend(techless_device_list,
1143 __sync_synchronize();
1144 if (technology->rfkill_driven == TRUE) {
1145 if (technology->enabled == TRUE)
1146 __connman_device_enable(device);
1148 __connman_device_disable(device);
1153 if (technology->enable_persistent == TRUE &&
1154 global_offlinemode == FALSE) {
1155 int err = __connman_device_enable(device);
1157 * connman_technology_add_device() calls __connman_device_enable()
1158 * but since the device is already enabled, the calls does not
1159 * propagate through to connman_technology_enabled via
1160 * connman_device_set_powered.
1162 if (err == -EALREADY)
1163 __connman_technology_enabled(type);
1165 /* if technology persistent state is offline */
1166 if (technology->enable_persistent == FALSE)
1167 __connman_device_disable(device);
1170 technology->device_list = g_slist_prepend(technology->device_list,
1176 int __connman_technology_remove_device(struct connman_device *device)
1178 struct connman_technology *technology;
1179 enum connman_service_type type;
1181 DBG("device %p", device);
1183 type = __connman_device_get_service_type(device);
1185 technology = technology_find(type);
1186 if (technology == NULL) {
1187 techless_device_list = g_slist_remove(techless_device_list,
1192 technology->device_list = g_slist_remove(technology->device_list,
1194 technology_put(technology);
1199 static void powered_changed(struct connman_technology *technology)
1201 if (technology->pending_reply != NULL) {
1202 g_dbus_send_reply(connection,
1203 technology->pending_reply, DBUS_TYPE_INVALID);
1204 dbus_message_unref(technology->pending_reply);
1205 technology->pending_reply = NULL;
1207 g_source_remove(technology->pending_timeout);
1208 technology->pending_timeout = 0;
1211 __sync_synchronize();
1212 connman_dbus_property_changed_basic(technology->path,
1213 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1214 DBUS_TYPE_BOOLEAN, &technology->enabled);
1217 static int technology_enabled(struct connman_technology *technology)
1219 __sync_synchronize();
1220 if (technology->enabled == TRUE)
1223 technology->enabled = TRUE;
1225 powered_changed(technology);
1230 int __connman_technology_enabled(enum connman_service_type type)
1232 struct connman_technology *technology;
1234 technology = technology_find(type);
1235 if (technology == NULL)
1238 return technology_enabled(technology);
1241 static int technology_disabled(struct connman_technology *technology)
1243 __sync_synchronize();
1244 if (technology->enabled == FALSE)
1247 technology->enabled = FALSE;
1249 powered_changed(technology);
1254 int __connman_technology_disabled(enum connman_service_type type)
1256 struct connman_technology *technology;
1258 technology = technology_find(type);
1259 if (technology == NULL)
1262 return technology_disabled(technology);
1265 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1270 if (global_offlinemode == offlinemode)
1273 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1276 * This is a bit tricky. When you set offlinemode, there is no
1277 * way to differentiate between attempting offline mode and
1278 * resuming offlinemode from last saved profile. We need that
1279 * information in rfkill_update, otherwise it falls back on the
1280 * technology's persistent state. Hence we set the offline mode here
1281 * but save it & call the notifier only if its successful.
1284 global_offlinemode = offlinemode;
1286 /* Traverse technology list, enable/disable each technology. */
1287 for (list = technology_list; list; list = list->next) {
1288 struct connman_technology *technology = list->data;
1291 err = technology_disable(technology);
1293 if (!offlinemode && technology->enable_persistent)
1294 err = technology_enable(technology);
1297 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1298 connman_technology_save_offlinemode();
1299 __connman_notifier_offlinemode(offlinemode);
1301 global_offlinemode = connman_technology_load_offlinemode();
1306 void __connman_technology_set_connected(enum connman_service_type type,
1307 connman_bool_t connected)
1309 struct connman_technology *technology;
1311 technology = technology_find(type);
1312 if (technology == NULL)
1315 DBG("technology %p connected %d", technology, connected);
1317 technology->connected = connected;
1319 connman_dbus_property_changed_basic(technology->path,
1320 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1321 DBUS_TYPE_BOOLEAN, &connected);
1324 static connman_bool_t technology_apply_rfkill_change(struct connman_technology *technology,
1325 connman_bool_t softblock,
1326 connman_bool_t hardblock)
1328 gboolean apply = TRUE;
1329 GList *start, *list;
1331 if (technology->hardblocked == hardblock)
1332 goto softblock_change;
1334 start = g_hash_table_get_values(rfkill_list);
1335 for (list = start; list != NULL; list = list->next) {
1336 struct connman_rfkill *rfkill = list->data;
1338 if (rfkill->type != technology->type)
1341 if (rfkill->hardblock != hardblock)
1348 goto softblock_change;
1350 technology->hardblocked = hardblock;
1352 if (hardblock == TRUE) {
1353 DBG("%s is switched off.", get_name(technology->type));
1354 technology_disable(technology);
1355 technology_dbus_unregister(technology);
1357 technology_enable(technology);
1358 technology_dbus_register(technology);
1362 technology->softblocked = softblock;
1364 return technology->hardblocked;
1367 int __connman_technology_add_rfkill(unsigned int index,
1368 enum connman_service_type type,
1369 connman_bool_t softblock,
1370 connman_bool_t hardblock)
1372 struct connman_technology *technology;
1373 struct connman_rfkill *rfkill;
1375 DBG("index %u type %d soft %u hard %u", index, type,
1376 softblock, hardblock);
1378 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1382 rfkill = g_try_new0(struct connman_rfkill, 1);
1386 rfkill->index = index;
1387 rfkill->type = type;
1388 rfkill->softblock = softblock;
1389 rfkill->hardblock = hardblock;
1391 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1394 technology = technology_get(type);
1395 /* If there is no driver for this type, ignore it. */
1396 if (technology == NULL)
1399 technology->rfkill_driven = TRUE;
1401 /* If hardblocked, there is no need to handle softblocked state */
1402 if (technology_apply_rfkill_change(technology,
1403 softblock, hardblock) == TRUE)
1407 * Depending on softblocked state we unblock/block according to
1408 * offlinemode and persistente state.
1410 if (technology->softblocked == TRUE &&
1411 global_offlinemode == FALSE &&
1412 technology->enable_persistent == TRUE)
1413 return __connman_rfkill_block(type, FALSE);
1414 else if (technology->softblocked == FALSE &&
1415 global_offlinemode == TRUE &&
1416 technology->enable_persistent == FALSE)
1417 return __connman_rfkill_block(type, TRUE);
1422 int __connman_technology_update_rfkill(unsigned int index,
1423 enum connman_service_type type,
1424 connman_bool_t softblock,
1425 connman_bool_t hardblock)
1427 struct connman_technology *technology;
1428 struct connman_rfkill *rfkill;
1430 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1432 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1436 if (rfkill->softblock == softblock &&
1437 rfkill->hardblock == hardblock)
1440 rfkill->softblock = softblock;
1441 rfkill->hardblock = hardblock;
1443 technology = technology_find(type);
1444 /* If there is no driver for this type, ignore it. */
1445 if (technology == NULL)
1448 /* If hardblocked, there is no need to handle softblocked state */
1449 if (technology_apply_rfkill_change(technology,
1450 softblock, hardblock) == TRUE)
1453 if (global_offlinemode == TRUE)
1457 * Depending on softblocked state we unblock/block according to
1460 if (technology->softblocked == TRUE &&
1461 technology->enable_persistent == TRUE)
1462 return __connman_rfkill_block(type, FALSE);
1463 else if (technology->softblocked == FALSE &&
1464 technology->enable_persistent == FALSE)
1465 return __connman_rfkill_block(type, TRUE);
1470 int __connman_technology_remove_rfkill(unsigned int index,
1471 enum connman_service_type type)
1473 struct connman_technology *technology;
1474 struct connman_rfkill *rfkill;
1476 DBG("index %u", index);
1478 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1482 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1484 technology = technology_find(type);
1485 if (technology == NULL)
1488 technology_put(technology);
1493 int __connman_technology_init(void)
1497 connection = connman_dbus_get_connection();
1499 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1502 global_offlinemode = connman_technology_load_offlinemode();
1504 /* This will create settings file if it is missing */
1505 connman_technology_save_offlinemode();
1510 void __connman_technology_cleanup(void)
1514 g_hash_table_destroy(rfkill_list);
1516 dbus_connection_unref(connection);