5 * Copyright (C) 2007-2010 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 GHashTable *rfkill_table;
36 static GHashTable *device_table;
37 static GSList *technology_list = NULL;
39 struct connman_rfkill {
41 enum connman_service_type type;
42 connman_bool_t softblock;
43 connman_bool_t hardblock;
46 enum connman_technology_state {
47 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
48 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
49 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
50 CONNMAN_TECHNOLOGY_STATE_ENABLED = 3,
51 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
54 struct connman_technology {
56 enum connman_service_type type;
57 enum connman_technology_state state;
59 GHashTable *rfkill_list;
65 connman_bool_t tethering;
66 char *tethering_ident;
67 char *tethering_passphrase;
69 struct connman_technology_driver *driver;
72 DBusMessage *pending_reply;
73 guint pending_timeout;
76 static GSList *driver_list = NULL;
78 static gint compare_priority(gconstpointer a, gconstpointer b)
80 const struct connman_technology_driver *driver1 = a;
81 const struct connman_technology_driver *driver2 = b;
83 return driver2->priority - driver1->priority;
87 * connman_technology_driver_register:
88 * @driver: Technology driver definition
90 * Register a new technology driver
92 * Returns: %0 on success
94 int connman_technology_driver_register(struct connman_technology_driver *driver)
97 struct connman_technology *technology;
99 DBG("driver %p name %s", driver, driver->name);
101 driver_list = g_slist_insert_sorted(driver_list, driver,
104 for (list = technology_list; list; list = list->next) {
105 technology = list->data;
107 if (technology->driver != NULL)
110 if (technology->type == driver->type)
111 technology->driver = driver;
118 * connman_technology_driver_unregister:
119 * @driver: Technology driver definition
121 * Remove a previously registered technology driver
123 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
126 struct connman_technology *technology;
128 DBG("driver %p name %s", driver, driver->name);
130 for (list = technology_list; list; list = list->next) {
131 technology = list->data;
133 if (technology->driver == NULL)
136 if (technology->type == driver->type) {
137 technology->driver->remove(technology);
138 technology->driver = NULL;
142 driver_list = g_slist_remove(driver_list, driver);
145 static void tethering_changed(struct connman_technology *technology)
147 connman_bool_t tethering = technology->tethering;
149 connman_dbus_property_changed_basic(technology->path,
150 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
151 DBUS_TYPE_BOOLEAN, &tethering);
154 void connman_technology_tethering_notify(struct connman_technology *technology,
155 connman_bool_t enabled)
157 DBG("technology %p enabled %u", technology, enabled);
159 if (technology->tethering == enabled)
162 technology->tethering = enabled;
164 tethering_changed(technology);
167 __connman_tethering_set_enabled();
169 __connman_tethering_set_disabled();
172 static int set_tethering(struct connman_technology *technology,
173 const char *bridge, connman_bool_t enabled)
175 const char *ident, *passphrase;
177 ident = technology->tethering_ident;
178 passphrase = technology->tethering_passphrase;
180 if (technology->driver == NULL ||
181 technology->driver->set_tethering == NULL)
184 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
185 (ident == NULL || passphrase == NULL))
188 return technology->driver->set_tethering(technology, ident, passphrase,
192 void connman_technology_regdom_notify(struct connman_technology *technology,
198 connman_error("Failed to set regulatory domain");
200 DBG("Regulatory domain set to %s", alpha2);
202 g_free(technology->regdom);
203 technology->regdom = g_strdup(alpha2);
206 int connman_technology_set_regdom(const char *alpha2)
210 for (list = technology_list; list; list = list->next) {
211 struct connman_technology *technology = list->data;
213 if (technology->driver == NULL)
216 if (technology->driver->set_regdom)
217 technology->driver->set_regdom(technology, alpha2);
223 static void free_rfkill(gpointer data)
225 struct connman_rfkill *rfkill = data;
230 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
234 for (list = technology_list; list; list = list->next) {
235 struct connman_technology *technology = list->data;
237 if (technology->path == NULL)
240 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
245 static void technologies_changed(void)
247 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
248 CONNMAN_MANAGER_INTERFACE, "Technologies",
249 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
252 static const char *state2string(enum connman_technology_state state)
255 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
257 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
259 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
261 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
263 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
270 static void state_changed(struct connman_technology *technology)
274 str = state2string(technology->state);
278 connman_dbus_property_changed_basic(technology->path,
279 CONNMAN_TECHNOLOGY_INTERFACE, "State",
280 DBUS_TYPE_STRING, &str);
283 static const char *get_name(enum connman_service_type type)
286 case CONNMAN_SERVICE_TYPE_UNKNOWN:
287 case CONNMAN_SERVICE_TYPE_SYSTEM:
288 case CONNMAN_SERVICE_TYPE_GPS:
289 case CONNMAN_SERVICE_TYPE_VPN:
290 case CONNMAN_SERVICE_TYPE_GADGET:
292 case CONNMAN_SERVICE_TYPE_ETHERNET:
294 case CONNMAN_SERVICE_TYPE_WIFI:
296 case CONNMAN_SERVICE_TYPE_WIMAX:
298 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
300 case CONNMAN_SERVICE_TYPE_CELLULAR:
307 static DBusMessage *get_properties(DBusConnection *conn,
308 DBusMessage *message, void *user_data)
310 struct connman_technology *technology = user_data;
312 DBusMessageIter array, dict;
315 reply = dbus_message_new_method_return(message);
319 dbus_message_iter_init_append(reply, &array);
321 connman_dbus_dict_open(&array, &dict);
323 str = state2string(technology->state);
325 connman_dbus_dict_append_basic(&dict, "State",
326 DBUS_TYPE_STRING, &str);
328 str = get_name(technology->type);
330 connman_dbus_dict_append_basic(&dict, "Name",
331 DBUS_TYPE_STRING, &str);
333 str = __connman_service_type2string(technology->type);
335 connman_dbus_dict_append_basic(&dict, "Type",
336 DBUS_TYPE_STRING, &str);
338 connman_dbus_dict_append_basic(&dict, "Tethering",
340 &technology->tethering);
342 if (technology->tethering_ident != NULL)
343 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
345 &technology->tethering_ident);
347 if (technology->tethering_passphrase != NULL)
348 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
350 &technology->tethering_passphrase);
352 connman_dbus_dict_close(&array, &dict);
357 static DBusMessage *set_property(DBusConnection *conn,
358 DBusMessage *msg, void *data)
360 struct connman_technology *technology = data;
361 DBusMessageIter iter, value;
365 DBG("conn %p", conn);
367 if (dbus_message_iter_init(msg, &iter) == FALSE)
368 return __connman_error_invalid_arguments(msg);
370 dbus_message_iter_get_basic(&iter, &name);
371 dbus_message_iter_next(&iter);
372 dbus_message_iter_recurse(&iter, &value);
374 type = dbus_message_iter_get_arg_type(&value);
376 DBG("property %s", name);
378 if (g_str_equal(name, "Tethering") == TRUE) {
380 connman_bool_t tethering;
383 if (type != DBUS_TYPE_BOOLEAN)
384 return __connman_error_invalid_arguments(msg);
386 dbus_message_iter_get_basic(&value, &tethering);
388 if (technology->tethering == tethering)
389 return __connman_error_in_progress(msg);
391 bridge = __connman_tethering_get_bridge();
393 return __connman_error_not_supported(msg);
395 err = set_tethering(technology, bridge, tethering);
397 return __connman_error_failed(msg, -err);
399 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
402 dbus_message_iter_get_basic(&value, &str);
404 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
405 return __connman_error_not_supported(msg);
407 technology->tethering_ident = g_strdup(str);
408 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
411 dbus_message_iter_get_basic(&value, &str);
413 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
414 return __connman_error_not_supported(msg);
417 return __connman_error_invalid_arguments(msg);
419 technology->tethering_passphrase = g_strdup(str);
421 return __connman_error_invalid_property(msg);
423 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
426 static GDBusMethodTable technology_methods[] = {
427 { "GetProperties", "", "a{sv}", get_properties },
428 { "SetProperty", "sv", "", set_property },
432 static GDBusSignalTable technology_signals[] = {
433 { "PropertyChanged", "sv" },
437 static struct connman_technology *technology_find(enum connman_service_type type)
441 DBG("type %d", type);
443 for (list = technology_list; list; list = list->next) {
444 struct connman_technology *technology = list->data;
446 if (technology->type == type)
453 static struct connman_technology *technology_get(enum connman_service_type type)
455 struct connman_technology *technology;
459 DBG("type %d", type);
461 technology = technology_find(type);
462 if (technology != NULL) {
463 g_atomic_int_inc(&technology->refcount);
467 str = __connman_service_type2string(type);
471 technology = g_try_new0(struct connman_technology, 1);
472 if (technology == NULL)
475 technology->refcount = 1;
477 technology->type = type;
478 technology->path = g_strdup_printf("%s/technology/%s",
481 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
483 technology->device_list = NULL;
485 technology->pending_reply = NULL;
486 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
488 if (g_dbus_register_interface(connection, technology->path,
489 CONNMAN_TECHNOLOGY_INTERFACE,
490 technology_methods, technology_signals,
491 NULL, technology, NULL) == FALSE) {
492 connman_error("Failed to register %s", technology->path);
497 technology_list = g_slist_append(technology_list, technology);
499 technologies_changed();
501 if (technology->driver != NULL)
504 for (list = driver_list; list; list = list->next) {
505 struct connman_technology_driver *driver = list->data;
507 DBG("driver %p name %s", driver, driver->name);
509 if (driver->type != technology->type)
512 if (driver->probe(technology) == 0) {
513 technology->driver = driver;
519 DBG("technology %p", technology);
524 static void technology_put(struct connman_technology *technology)
526 DBG("technology %p", technology);
528 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
531 if (technology->driver) {
532 technology->driver->remove(technology);
533 technology->driver = NULL;
536 technology_list = g_slist_remove(technology_list, technology);
538 technologies_changed();
540 g_dbus_unregister_interface(connection, technology->path,
541 CONNMAN_TECHNOLOGY_INTERFACE);
543 g_slist_free(technology->device_list);
544 g_hash_table_destroy(technology->rfkill_list);
546 g_free(technology->path);
547 g_free(technology->regdom);
551 void __connman_technology_add_interface(enum connman_service_type type,
552 int index, const char *name, const char *ident)
554 struct connman_technology *technology;
557 case CONNMAN_SERVICE_TYPE_UNKNOWN:
558 case CONNMAN_SERVICE_TYPE_SYSTEM:
560 case CONNMAN_SERVICE_TYPE_ETHERNET:
561 case CONNMAN_SERVICE_TYPE_WIFI:
562 case CONNMAN_SERVICE_TYPE_WIMAX:
563 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
564 case CONNMAN_SERVICE_TYPE_CELLULAR:
565 case CONNMAN_SERVICE_TYPE_GPS:
566 case CONNMAN_SERVICE_TYPE_VPN:
567 case CONNMAN_SERVICE_TYPE_GADGET:
571 connman_info("Create interface %s [ %s ]", name,
572 __connman_service_type2string(type));
574 technology = technology_get(type);
576 if (technology == NULL || technology->driver == NULL
577 || technology->driver->add_interface == NULL)
580 technology->driver->add_interface(technology,
584 void __connman_technology_remove_interface(enum connman_service_type type,
585 int index, const char *name, const char *ident)
587 struct connman_technology *technology;
590 case CONNMAN_SERVICE_TYPE_UNKNOWN:
591 case CONNMAN_SERVICE_TYPE_SYSTEM:
593 case CONNMAN_SERVICE_TYPE_ETHERNET:
594 case CONNMAN_SERVICE_TYPE_WIFI:
595 case CONNMAN_SERVICE_TYPE_WIMAX:
596 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
597 case CONNMAN_SERVICE_TYPE_CELLULAR:
598 case CONNMAN_SERVICE_TYPE_GPS:
599 case CONNMAN_SERVICE_TYPE_VPN:
600 case CONNMAN_SERVICE_TYPE_GADGET:
604 connman_info("Remove interface %s [ %s ]", name,
605 __connman_service_type2string(type));
607 technology = technology_find(type);
609 if (technology == NULL || technology->driver == NULL)
612 if (technology->driver->remove_interface)
613 technology->driver->remove_interface(technology, index);
615 technology_put(technology);
618 static void unregister_technology(gpointer data)
620 struct connman_technology *technology = data;
622 technology_put(technology);
625 int __connman_technology_add_device(struct connman_device *device)
627 struct connman_technology *technology;
628 enum connman_service_type type;
630 DBG("device %p", device);
632 type = __connman_device_get_service_type(device);
633 __connman_notifier_register(type);
635 technology = technology_get(type);
636 if (technology == NULL)
639 g_hash_table_insert(device_table, device, technology);
641 if (g_atomic_int_get(&technology->blocked))
644 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
646 state_changed(technology);
650 technology->device_list = g_slist_append(technology->device_list,
656 int __connman_technology_remove_device(struct connman_device *device)
658 struct connman_technology *technology;
659 enum connman_service_type type;
661 DBG("device %p", device);
663 type = __connman_device_get_service_type(device);
664 __connman_notifier_unregister(type);
666 technology = g_hash_table_lookup(device_table, device);
667 if (technology == NULL)
670 technology->device_list = g_slist_remove(technology->device_list,
672 if (technology->device_list == NULL) {
673 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
674 state_changed(technology);
677 g_hash_table_remove(device_table, device);
682 static gboolean technology_pending_reply(gpointer user_data)
684 struct connman_technology *technology = user_data;
687 /* Power request timedout, send ETIMEDOUT. */
688 if (technology->pending_reply != NULL) {
689 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
691 g_dbus_send_message(connection, reply);
693 dbus_message_unref(technology->pending_reply);
694 technology->pending_reply = NULL;
695 technology->pending_timeout = 0;
701 int __connman_technology_enabled(enum connman_service_type type)
703 struct connman_technology *technology;
705 technology = technology_find(type);
706 if (technology == NULL)
709 if (g_atomic_int_get(&technology->blocked))
712 __connman_notifier_enable(type);
714 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
715 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
716 state_changed(technology);
719 if (__connman_profile_get_offlinemode() == TRUE) {
720 __connman_profile_set_offlinemode(FALSE, FALSE);
721 __connman_profile_save_default();
724 if (technology->pending_reply != NULL) {
725 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
726 dbus_message_unref(technology->pending_reply);
727 technology->pending_reply = NULL;
728 technology->pending_timeout = 0;
734 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
736 struct connman_technology *technology;
742 DBG("type %d enable", type);
744 technology = technology_find(type);
745 if (technology == NULL) {
750 if (technology->pending_reply != NULL) {
756 technology->pending_reply = dbus_message_ref(msg);
758 for (list = technology->device_list; list; list = list->next) {
759 struct connman_device *device = list->data;
761 err = __connman_device_enable_persistent(device);
763 * err = 0 : Device was enabled right away.
764 * If atleast one device gets enabled, we consider
765 * the technology to be enabled.
776 if (err == -EINPROGRESS)
777 technology->pending_timeout = g_timeout_add_seconds(10,
778 technology_pending_reply, technology);
780 reply = __connman_error_failed(msg, -err);
782 g_dbus_send_message(connection, reply);
789 int __connman_technology_disabled(enum connman_service_type type)
791 struct connman_technology *technology;
794 technology = technology_find(type);
795 if (technology == NULL)
798 if (technology->pending_reply != NULL) {
799 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
800 dbus_message_unref(technology->pending_reply);
801 technology->pending_reply = NULL;
804 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
805 __connman_notifier_disable(type);
807 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
808 state_changed(technology);
811 for (list = technology->device_list; list; list = list->next) {
812 struct connman_device *device = list->data;
814 if (__connman_device_get_blocked(device) == FALSE)
818 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
819 state_changed(technology);
824 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
826 struct connman_technology *technology;
832 DBG("type %d disable", type);
834 technology = technology_find(type);
835 if (technology == NULL) {
840 if (technology->pending_reply != NULL) {
846 technology->pending_reply = dbus_message_ref(msg);
848 for (list = technology->device_list; list; list = list->next) {
849 struct connman_device *device = list->data;
851 err = __connman_device_disable_persistent(device);
861 if (err == -EINPROGRESS)
862 technology->pending_timeout = g_timeout_add_seconds(10,
863 technology_pending_reply, technology);
865 reply = __connman_error_failed(msg, -err);
867 g_dbus_send_message(connection, reply);
874 static void technology_blocked(struct connman_technology *technology,
875 connman_bool_t blocked)
879 for (list = technology->device_list; list; list = list->next) {
880 struct connman_device *device = list->data;
882 __connman_device_set_blocked(device, blocked);
886 int __connman_technology_add_rfkill(unsigned int index,
887 enum connman_service_type type,
888 connman_bool_t softblock,
889 connman_bool_t hardblock)
891 struct connman_technology *technology;
892 struct connman_rfkill *rfkill;
893 connman_bool_t blocked;
895 DBG("index %u type %d soft %u hard %u", index, type,
896 softblock, hardblock);
898 technology = technology_get(type);
899 if (technology == NULL)
902 rfkill = g_try_new0(struct connman_rfkill, 1);
906 rfkill->index = index;
908 rfkill->softblock = softblock;
909 rfkill->hardblock = hardblock;
911 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
913 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
915 blocked = (softblock || hardblock) ? TRUE : FALSE;
916 if (blocked == FALSE)
919 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
920 technology_blocked(technology, TRUE);
922 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
923 state_changed(technology);
929 int __connman_technology_update_rfkill(unsigned int index,
930 connman_bool_t softblock,
931 connman_bool_t hardblock)
933 struct connman_technology *technology;
934 struct connman_rfkill *rfkill;
935 connman_bool_t blocked, old_blocked;
937 DBG("index %u soft %u hard %u", index, softblock, hardblock);
939 technology = g_hash_table_lookup(rfkill_table, &index);
940 if (technology == NULL)
943 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
947 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
948 blocked = (softblock || hardblock) ? TRUE : FALSE;
950 rfkill->softblock = softblock;
951 rfkill->hardblock = hardblock;
953 if (blocked == old_blocked)
960 g_atomic_int_exchange_and_add(&technology->blocked, 1);
961 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
964 technology_blocked(technology, blocked);
965 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
966 state_changed(technology);
968 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
971 technology_blocked(technology, blocked);
972 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
973 state_changed(technology);
979 int __connman_technology_remove_rfkill(unsigned int index)
981 struct connman_technology *technology;
982 struct connman_rfkill *rfkill;
983 connman_bool_t blocked;
985 DBG("index %u", index);
987 technology = g_hash_table_lookup(rfkill_table, &index);
988 if (technology == NULL)
991 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
995 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
997 g_hash_table_remove(technology->rfkill_list, &index);
999 g_hash_table_remove(rfkill_table, &index);
1002 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
1003 technology_blocked(technology, FALSE);
1004 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
1005 state_changed(technology);
1011 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
1013 struct connman_technology *technology;
1015 technology = technology_find(type);
1016 if (technology == NULL)
1019 if (g_atomic_int_get(&technology->blocked))
1025 int __connman_technology_init(void)
1029 connection = connman_dbus_get_connection();
1031 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
1032 NULL, unregister_technology);
1033 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1034 NULL, unregister_technology);
1039 void __connman_technology_cleanup(void)
1043 g_hash_table_destroy(device_table);
1044 g_hash_table_destroy(rfkill_table);
1046 dbus_connection_unref(connection);