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 hardblocked;
78 connman_bool_t dbus_registered;
81 static GSList *driver_list = NULL;
83 static gint compare_priority(gconstpointer a, gconstpointer b)
85 const struct connman_technology_driver *driver1 = a;
86 const struct connman_technology_driver *driver2 = b;
88 return driver2->priority - driver1->priority;
91 static void rfkill_check(gpointer key, gpointer value, gpointer user_data)
93 struct connman_rfkill *rfkill = value;
94 enum connman_service_type type = GPOINTER_TO_INT(user_data);
96 /* Calling _technology_rfkill_add will update the tech. */
97 if (rfkill->type == type)
98 __connman_technology_add_rfkill(rfkill->index, type,
99 rfkill->softblock, rfkill->hardblock);
103 * connman_technology_driver_register:
104 * @driver: Technology driver definition
106 * Register a new technology driver
108 * Returns: %0 on success
110 int connman_technology_driver_register(struct connman_technology_driver *driver)
113 struct connman_device *device;
114 enum connman_service_type type;
116 DBG("Registering %s driver", driver->name);
118 driver_list = g_slist_insert_sorted(driver_list, driver,
121 if (techless_device_list == NULL)
125 * Check for technology less devices if this driver
126 * can service any of them.
128 for (list = techless_device_list; list; list = list->next) {
131 type = __connman_device_get_service_type(device);
132 if (type != driver->type)
135 techless_device_list = g_slist_remove(techless_device_list,
138 __connman_technology_add_device(device);
142 /* Check for orphaned rfkill switches. */
143 g_hash_table_foreach(rfkill_list, rfkill_check,
144 GINT_TO_POINTER(driver->type));
150 * connman_technology_driver_unregister:
151 * @driver: Technology driver definition
153 * Remove a previously registered technology driver
155 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
158 struct connman_technology *technology;
160 DBG("Unregistering driver %p name %s", driver, driver->name);
162 for (list = technology_list; list; list = list->next) {
163 technology = list->data;
165 if (technology->driver == NULL)
168 if (technology->type == driver->type) {
169 technology->driver->remove(technology);
170 technology->driver = NULL;
174 driver_list = g_slist_remove(driver_list, driver);
177 static void tethering_changed(struct connman_technology *technology)
179 connman_bool_t tethering = technology->tethering;
181 connman_dbus_property_changed_basic(technology->path,
182 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
183 DBUS_TYPE_BOOLEAN, &tethering);
186 void connman_technology_tethering_notify(struct connman_technology *technology,
187 connman_bool_t enabled)
191 DBG("technology %p enabled %u", technology, enabled);
193 if (technology->tethering == enabled)
196 technology->tethering = enabled;
198 tethering_changed(technology);
201 __connman_tethering_set_enabled();
203 for (list = technology_list; list; list = list->next) {
204 struct connman_technology *other_tech = list->data;
205 if (other_tech->tethering == TRUE)
209 __connman_tethering_set_disabled();
213 static int set_tethering(struct connman_technology *technology,
214 connman_bool_t enabled)
216 const char *ident, *passphrase, *bridge;
218 ident = technology->tethering_ident;
219 passphrase = technology->tethering_passphrase;
221 if (technology->driver == NULL ||
222 technology->driver->set_tethering == NULL)
225 bridge = __connman_tethering_get_bridge();
229 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
230 (ident == NULL || passphrase == NULL))
233 return technology->driver->set_tethering(technology, ident, passphrase,
237 void connman_technology_regdom_notify(struct connman_technology *technology,
243 connman_error("Failed to set regulatory domain");
245 DBG("Regulatory domain set to %s", alpha2);
247 g_free(technology->regdom);
248 technology->regdom = g_strdup(alpha2);
251 static int set_regdom_by_device(struct connman_technology *technology,
256 for (list = technology->device_list; list; list = list->next) {
257 struct connman_device *device = list->data;
259 if (connman_device_set_regdom(device, alpha2) != 0)
266 int connman_technology_set_regdom(const char *alpha2)
270 for (list = technology_list; list; list = list->next) {
271 struct connman_technology *technology = list->data;
273 if (set_regdom_by_device(technology, alpha2) != 0) {
274 if (technology->driver == NULL)
277 if (technology->driver->set_regdom != NULL)
278 technology->driver->set_regdom(technology,
286 static void free_rfkill(gpointer data)
288 struct connman_rfkill *rfkill = data;
293 static const char *get_name(enum connman_service_type type)
296 case CONNMAN_SERVICE_TYPE_UNKNOWN:
297 case CONNMAN_SERVICE_TYPE_SYSTEM:
298 case CONNMAN_SERVICE_TYPE_GPS:
299 case CONNMAN_SERVICE_TYPE_VPN:
300 case CONNMAN_SERVICE_TYPE_GADGET:
302 case CONNMAN_SERVICE_TYPE_ETHERNET:
304 case CONNMAN_SERVICE_TYPE_WIFI:
306 case CONNMAN_SERVICE_TYPE_WIMAX:
308 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
310 case CONNMAN_SERVICE_TYPE_CELLULAR:
317 static void technology_save(struct connman_technology *technology)
322 DBG("technology %p", technology);
324 keyfile = __connman_storage_load_global();
326 keyfile = g_key_file_new();
328 identifier = g_strdup_printf("%s", get_name(technology->type));
329 if (identifier == NULL)
332 g_key_file_set_boolean(keyfile, identifier, "Enable",
333 technology->enable_persistent);
335 if (technology->tethering_ident != NULL)
336 g_key_file_set_string(keyfile, identifier,
337 "Tethering.Identifier",
338 technology->tethering_ident);
340 if (technology->tethering_passphrase != NULL)
341 g_key_file_set_string(keyfile, identifier,
342 "Tethering.Passphrase",
343 technology->tethering_passphrase);
348 __connman_storage_save_global(keyfile);
350 g_key_file_free(keyfile);
355 static void technology_load(struct connman_technology *technology)
359 GError *error = NULL;
360 connman_bool_t enable;
362 DBG("technology %p", technology);
364 keyfile = __connman_storage_load_global();
365 /* Fallback on disabling technology if file not found. */
366 if (keyfile == NULL) {
367 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
368 /* We enable ethernet by default */
369 technology->enable_persistent = TRUE;
371 technology->enable_persistent = FALSE;
375 identifier = g_strdup_printf("%s", get_name(technology->type));
376 if (identifier == NULL)
379 enable = g_key_file_get_boolean(keyfile, identifier, "Enable", &error);
381 technology->enable_persistent = enable;
383 if (technology->type == CONNMAN_SERVICE_TYPE_ETHERNET)
384 technology->enable_persistent = TRUE;
386 technology->enable_persistent = FALSE;
388 technology_save(technology);
389 g_clear_error(&error);
392 technology->tethering_ident = g_key_file_get_string(keyfile,
393 identifier, "Tethering.Identifier", NULL);
395 technology->tethering_passphrase = g_key_file_get_string(keyfile,
396 identifier, "Tethering.Passphrase", NULL);
400 g_key_file_free(keyfile);
405 connman_bool_t __connman_technology_get_offlinemode(void)
407 return global_offlinemode;
410 static void connman_technology_save_offlinemode()
414 keyfile = __connman_storage_load_global();
416 keyfile = g_key_file_new();
418 g_key_file_set_boolean(keyfile, "global",
419 "OfflineMode", global_offlinemode);
421 __connman_storage_save_global(keyfile);
423 g_key_file_free(keyfile);
428 static connman_bool_t connman_technology_load_offlinemode()
431 GError *error = NULL;
432 connman_bool_t offlinemode;
434 /* If there is a error, we enable offlinemode */
435 keyfile = __connman_storage_load_global();
439 offlinemode = g_key_file_get_boolean(keyfile, "global",
440 "OfflineMode", &error);
443 g_clear_error(&error);
446 g_key_file_free(keyfile);
451 static void append_properties(DBusMessageIter *iter,
452 struct connman_technology *technology)
454 DBusMessageIter dict;
456 connman_bool_t powered;
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 if (technology->enabled > 0)
475 connman_dbus_dict_append_basic(&dict, "Powered",
476 DBUS_TYPE_BOOLEAN, &powered);
478 connman_dbus_dict_append_basic(&dict, "Connected",
480 &technology->connected);
482 connman_dbus_dict_append_basic(&dict, "Tethering",
484 &technology->tethering);
486 if (technology->tethering_ident != NULL)
487 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
489 &technology->tethering_ident);
491 if (technology->tethering_passphrase != NULL)
492 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
494 &technology->tethering_passphrase);
496 connman_dbus_dict_close(iter, &dict);
499 static void technology_added_signal(struct connman_technology *technology)
502 DBusMessageIter iter;
504 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
505 CONNMAN_MANAGER_INTERFACE, "TechnologyAdded");
509 dbus_message_iter_init_append(signal, &iter);
510 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
512 append_properties(&iter, technology);
514 dbus_connection_send(connection, signal, NULL);
515 dbus_message_unref(signal);
518 static void technology_removed_signal(struct connman_technology *technology)
520 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
521 CONNMAN_MANAGER_INTERFACE, "TechnologyRemoved",
522 DBUS_TYPE_OBJECT_PATH, &technology->path,
526 static DBusMessage *get_properties(DBusConnection *conn,
527 DBusMessage *message, void *user_data)
529 struct connman_technology *technology = user_data;
531 DBusMessageIter iter;
533 reply = dbus_message_new_method_return(message);
537 dbus_message_iter_init_append(reply, &iter);
538 append_properties(&iter, technology);
543 void __connman_technology_list_struct(DBusMessageIter *array)
546 DBusMessageIter entry;
548 for (list = technology_list; list; list = list->next) {
549 struct connman_technology *technology = list->data;
551 if (technology->path == NULL ||
552 (technology->rfkill_driven == TRUE &&
553 technology->hardblocked == TRUE))
556 dbus_message_iter_open_container(array, DBUS_TYPE_STRUCT,
558 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
560 append_properties(&entry, technology);
561 dbus_message_iter_close_container(array, &entry);
565 static gboolean technology_pending_reply(gpointer user_data)
567 struct connman_technology *technology = user_data;
570 /* Power request timedout, send ETIMEDOUT. */
571 if (technology->pending_reply != NULL) {
572 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
574 g_dbus_send_message(connection, reply);
576 dbus_message_unref(technology->pending_reply);
577 technology->pending_reply = NULL;
578 technology->pending_timeout = 0;
584 static int technology_affect_devices(struct connman_technology *technology,
585 connman_bool_t enable_device)
590 for (list = technology->device_list; list; list = list->next) {
591 struct connman_device *device = list->data;
593 if (enable_device == TRUE)
594 err = __connman_device_enable(device);
596 err = __connman_device_disable(device);
602 static int technology_enable(struct connman_technology *technology,
603 connman_bool_t hardblock)
605 DBG("technology %p enable", technology);
607 __sync_synchronize();
608 if (technology->enabled > 0)
611 if (technology->pending_reply != NULL)
614 if (hardblock == TRUE && technology->enable_persistent == FALSE)
617 __connman_rfkill_block(technology->type, FALSE);
619 return technology_affect_devices(technology, TRUE);
622 static int technology_disable(struct connman_technology *technology,
623 connman_bool_t hardblock)
625 DBG("technology %p disable", technology);
627 __sync_synchronize();
628 if (technology->enabled == 0)
631 if (technology->pending_reply != NULL)
634 if (technology->tethering == TRUE)
635 set_tethering(technology, FALSE);
637 if (hardblock == FALSE)
638 __connman_rfkill_block(technology->type, TRUE);
640 return technology_affect_devices(technology, FALSE);
643 static DBusMessage *set_powered(struct connman_technology *technology,
644 DBusMessage *msg, connman_bool_t powered)
646 DBusMessage *reply = NULL;
649 if (technology->rfkill_driven && technology->hardblocked == TRUE) {
655 err = technology_enable(technology, FALSE);
657 err = technology_disable(technology, FALSE);
660 technology->enable_persistent = powered;
661 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 gboolean technology_dbus_register(struct connman_technology *technology)
929 if (technology->dbus_registered == TRUE ||
930 (technology->rfkill_driven &&
931 technology->hardblocked == TRUE))
934 if (g_dbus_register_interface(connection, technology->path,
935 CONNMAN_TECHNOLOGY_INTERFACE,
936 technology_methods, technology_signals,
937 NULL, technology, NULL) == FALSE) {
938 connman_error("Failed to register %s", technology->path);
942 technology_added_signal(technology);
943 technology->dbus_registered = TRUE;
948 static struct connman_technology *technology_get(enum connman_service_type type)
950 struct connman_technology *technology;
951 struct connman_technology_driver *driver = NULL;
956 DBG("type %d", type);
958 str = __connman_service_type2string(type);
962 technology = technology_find(type);
963 if (technology != NULL) {
964 __sync_fetch_and_add(&technology->refcount, 1);
968 /* First check if we have a driver for this technology type */
969 for (list = driver_list; list; list = list->next) {
972 if (driver->type == type)
978 if (driver == NULL) {
979 DBG("No matching driver found for %s.",
980 __connman_service_type2string(type));
984 technology = g_try_new0(struct connman_technology, 1);
985 if (technology == NULL)
988 technology->refcount = 1;
990 technology->rfkill_driven = FALSE;
992 if (type == CONNMAN_SERVICE_TYPE_ETHERNET)
993 technology->hardblocked = FALSE;
995 technology->hardblocked = TRUE;
997 technology->type = type;
998 technology->path = g_strdup_printf("%s/technology/%s",
1001 technology->device_list = NULL;
1003 technology->pending_reply = NULL;
1005 technology_load(technology);
1007 if (technology_dbus_register(technology) == FALSE) {
1012 technology_list = g_slist_prepend(technology_list, technology);
1014 technology->driver = driver;
1015 err = driver->probe(technology);
1017 DBG("Driver probe failed for technology %p", technology);
1019 DBG("technology %p", technology);
1024 static void technology_dbus_unregister(struct connman_technology *technology)
1026 if (technology->dbus_registered == FALSE)
1029 technology_removed_signal(technology);
1030 g_dbus_unregister_interface(connection, technology->path,
1031 CONNMAN_TECHNOLOGY_INTERFACE);
1033 technology->dbus_registered = FALSE;
1036 static void technology_put(struct connman_technology *technology)
1038 DBG("technology %p", technology);
1040 if (__sync_sub_and_fetch(&technology->refcount, 1) > 0)
1043 reply_scan_pending(technology, -EINTR);
1045 if (technology->driver) {
1046 technology->driver->remove(technology);
1047 technology->driver = NULL;
1050 technology_list = g_slist_remove(technology_list, technology);
1052 technology_dbus_unregister(technology);
1054 g_slist_free(technology->device_list);
1056 g_free(technology->path);
1057 g_free(technology->regdom);
1058 g_free(technology->tethering_ident);
1059 g_free(technology->tethering_passphrase);
1063 void __connman_technology_add_interface(enum connman_service_type type,
1064 int index, const char *name, const char *ident)
1066 struct connman_technology *technology;
1069 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1070 case CONNMAN_SERVICE_TYPE_SYSTEM:
1072 case CONNMAN_SERVICE_TYPE_ETHERNET:
1073 case CONNMAN_SERVICE_TYPE_WIFI:
1074 case CONNMAN_SERVICE_TYPE_WIMAX:
1075 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1076 case CONNMAN_SERVICE_TYPE_CELLULAR:
1077 case CONNMAN_SERVICE_TYPE_GPS:
1078 case CONNMAN_SERVICE_TYPE_VPN:
1079 case CONNMAN_SERVICE_TYPE_GADGET:
1083 connman_info("Adding interface %s [ %s ]", name,
1084 __connman_service_type2string(type));
1086 technology = technology_find(type);
1088 if (technology == NULL || technology->driver == NULL
1089 || technology->driver->add_interface == NULL)
1092 technology->driver->add_interface(technology,
1093 index, name, ident);
1096 void __connman_technology_remove_interface(enum connman_service_type type,
1097 int index, const char *name, const char *ident)
1099 struct connman_technology *technology;
1102 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1103 case CONNMAN_SERVICE_TYPE_SYSTEM:
1105 case CONNMAN_SERVICE_TYPE_ETHERNET:
1106 case CONNMAN_SERVICE_TYPE_WIFI:
1107 case CONNMAN_SERVICE_TYPE_WIMAX:
1108 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1109 case CONNMAN_SERVICE_TYPE_CELLULAR:
1110 case CONNMAN_SERVICE_TYPE_GPS:
1111 case CONNMAN_SERVICE_TYPE_VPN:
1112 case CONNMAN_SERVICE_TYPE_GADGET:
1116 connman_info("Remove interface %s [ %s ]", name,
1117 __connman_service_type2string(type));
1119 technology = technology_find(type);
1121 if (technology == NULL || technology->driver == NULL)
1124 if (technology->driver->remove_interface)
1125 technology->driver->remove_interface(technology, index);
1128 int __connman_technology_add_device(struct connman_device *device)
1130 struct connman_technology *technology;
1131 enum connman_service_type type;
1133 DBG("device %p", device);
1135 type = __connman_device_get_service_type(device);
1137 technology = technology_get(type);
1138 if (technology == NULL) {
1140 * Since no driver can be found for this device at the moment we
1141 * add it to the techless device list.
1143 techless_device_list = g_slist_prepend(techless_device_list,
1149 if (technology->enable_persistent &&
1150 global_offlinemode == FALSE &&
1151 technology->hardblocked == FALSE) {
1152 int err = __connman_device_enable(device);
1154 * connman_technology_add_device() calls __connman_device_enable()
1155 * but since the device is already enabled, the calls does not
1156 * propagate through to connman_technology_enabled via
1157 * connman_device_set_powered.
1159 if (err == -EALREADY)
1160 __connman_technology_enabled(type);
1162 /* if technology persistent state is offline or hardblocked */
1163 if (technology->enable_persistent == FALSE ||
1164 technology->hardblocked == TRUE)
1165 __connman_device_disable(device);
1167 technology->device_list = g_slist_prepend(technology->device_list,
1173 int __connman_technology_remove_device(struct connman_device *device)
1175 struct connman_technology *technology;
1176 enum connman_service_type type;
1178 DBG("device %p", device);
1180 type = __connman_device_get_service_type(device);
1182 technology = technology_find(type);
1183 if (technology == NULL) {
1184 techless_device_list = g_slist_remove(techless_device_list,
1189 technology->device_list = g_slist_remove(technology->device_list,
1191 technology_put(technology);
1196 static void powered_changed(struct connman_technology *technology)
1198 connman_bool_t powered;
1200 __sync_synchronize();
1201 if (technology->enabled >0)
1206 connman_dbus_property_changed_basic(technology->path,
1207 CONNMAN_TECHNOLOGY_INTERFACE, "Powered",
1208 DBUS_TYPE_BOOLEAN, &powered);
1211 int __connman_technology_enabled(enum connman_service_type type)
1213 struct connman_technology *technology;
1215 technology = technology_find(type);
1216 if (technology == NULL)
1219 if (__sync_fetch_and_add(&technology->enabled, 1) != 0)
1222 powered_changed(technology);
1224 if (technology->pending_reply != NULL) {
1225 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1226 dbus_message_unref(technology->pending_reply);
1227 g_source_remove(technology->pending_timeout);
1228 technology->pending_reply = NULL;
1229 technology->pending_timeout = 0;
1235 int __connman_technology_disabled(enum connman_service_type type)
1237 struct connman_technology *technology;
1239 technology = technology_find(type);
1240 if (technology == NULL)
1243 if (__sync_fetch_and_sub(&technology->enabled, 1) != 1)
1244 return -EINPROGRESS;
1246 if (technology->pending_reply != NULL) {
1247 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
1248 dbus_message_unref(technology->pending_reply);
1249 g_source_remove(technology->pending_timeout);
1250 technology->pending_reply = NULL;
1251 technology->pending_timeout = 0;
1254 powered_changed(technology);
1259 int __connman_technology_set_offlinemode(connman_bool_t offlinemode)
1264 if (global_offlinemode == offlinemode)
1267 DBG("offlinemode %s", offlinemode ? "On" : "Off");
1270 * This is a bit tricky. When you set offlinemode, there is no
1271 * way to differentiate between attempting offline mode and
1272 * resuming offlinemode from last saved profile. We need that
1273 * information in rfkill_update, otherwise it falls back on the
1274 * technology's persistent state. Hence we set the offline mode here
1275 * but save it & call the notifier only if its successful.
1278 global_offlinemode = offlinemode;
1280 /* Traverse technology list, enable/disable each technology. */
1281 for (list = technology_list; list; list = list->next) {
1282 struct connman_technology *technology = list->data;
1285 err = technology_disable(technology, FALSE);
1287 if (!offlinemode && technology->enable_persistent)
1288 err = technology_enable(technology, FALSE);
1291 if (err == 0 || err == -EINPROGRESS || err == -EALREADY) {
1292 connman_technology_save_offlinemode();
1293 __connman_notifier_offlinemode(offlinemode);
1295 global_offlinemode = connman_technology_load_offlinemode();
1300 void __connman_technology_set_connected(enum connman_service_type type,
1301 connman_bool_t connected)
1303 struct connman_technology *technology;
1305 technology = technology_find(type);
1306 if (technology == NULL)
1309 DBG("technology %p connected %d", technology, connected);
1311 technology->connected = connected;
1313 connman_dbus_property_changed_basic(technology->path,
1314 CONNMAN_TECHNOLOGY_INTERFACE, "Connected",
1315 DBUS_TYPE_BOOLEAN, &connected);
1318 static void technology_apply_hardblock_change(struct connman_technology *technology,
1319 connman_bool_t hardblock)
1321 gboolean apply = TRUE;
1322 GList *start, *list;
1324 if (technology->hardblocked == hardblock)
1327 start = g_hash_table_get_values(rfkill_list);
1328 for (list = start; list != NULL; list = list->next) {
1329 struct connman_rfkill *rfkill = list->data;
1331 if (rfkill->type != technology->type)
1334 if (rfkill->hardblock != hardblock)
1343 technology->hardblocked = hardblock;
1345 if (hardblock == TRUE) {
1346 DBG("%s is switched off.", get_name(technology->type));
1347 technology_disable(technology, TRUE);
1348 technology_dbus_unregister(technology);
1350 technology_enable(technology, TRUE);
1351 technology_dbus_register(technology);
1355 int __connman_technology_add_rfkill(unsigned int index,
1356 enum connman_service_type type,
1357 connman_bool_t softblock,
1358 connman_bool_t hardblock)
1360 struct connman_technology *technology;
1361 struct connman_rfkill *rfkill;
1363 DBG("index %u type %d soft %u hard %u", index, type,
1364 softblock, hardblock);
1366 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1370 rfkill = g_try_new0(struct connman_rfkill, 1);
1374 rfkill->index = index;
1375 rfkill->type = type;
1376 rfkill->softblock = softblock;
1377 rfkill->hardblock = hardblock;
1379 g_hash_table_insert(rfkill_list, GINT_TO_POINTER(index), rfkill);
1382 technology = technology_get(type);
1383 /* If there is no driver for this type, ignore it. */
1384 if (technology == NULL)
1387 technology->rfkill_driven = TRUE;
1389 technology_apply_hardblock_change(technology, hardblock);
1392 * If Offline mode is on, we softblock the device if it isnt already.
1393 * If Offline mode is off, we rely on the persistent state of tech.
1395 if (global_offlinemode) {
1397 return __connman_rfkill_block(type, TRUE);
1399 if (technology->enable_persistent && softblock)
1400 return __connman_rfkill_block(type, FALSE);
1401 /* if technology persistent state is offline */
1402 if (!technology->enable_persistent && !softblock)
1403 return __connman_rfkill_block(type, TRUE);
1409 int __connman_technology_update_rfkill(unsigned int index,
1410 enum connman_service_type type,
1411 connman_bool_t softblock,
1412 connman_bool_t hardblock)
1414 struct connman_technology *technology;
1415 struct connman_rfkill *rfkill;
1417 DBG("index %u soft %u hard %u", index, softblock, hardblock);
1419 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1423 if (rfkill->softblock == softblock &&
1424 rfkill->hardblock == hardblock)
1427 rfkill->softblock = softblock;
1428 rfkill->hardblock = hardblock;
1430 technology = technology_find(type);
1431 /* If there is no driver for this type, ignore it. */
1432 if (technology == NULL)
1435 technology_apply_hardblock_change(technology, hardblock);
1437 if (!global_offlinemode) {
1438 if (technology->enable_persistent && softblock)
1439 return __connman_rfkill_block(type, FALSE);
1440 if (!technology->enable_persistent && !softblock)
1441 return __connman_rfkill_block(type, TRUE);
1447 int __connman_technology_remove_rfkill(unsigned int index,
1448 enum connman_service_type type)
1450 struct connman_technology *technology;
1451 struct connman_rfkill *rfkill;
1453 DBG("index %u", index);
1455 rfkill = g_hash_table_lookup(rfkill_list, GINT_TO_POINTER(index));
1459 g_hash_table_remove(rfkill_list, GINT_TO_POINTER(index));
1461 technology = technology_find(type);
1462 if (technology == NULL)
1465 technology_put(technology);
1470 int __connman_technology_init(void)
1474 connection = connman_dbus_get_connection();
1476 rfkill_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1479 global_offlinemode = connman_technology_load_offlinemode();
1481 /* This will create settings file if it is missing */
1482 connman_technology_save_offlinemode();
1487 void __connman_technology_cleanup(void)
1491 g_hash_table_destroy(rfkill_list);
1493 dbus_connection_unref(connection);