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;
73 static GSList *driver_list = NULL;
75 static gint compare_priority(gconstpointer a, gconstpointer b)
77 const struct connman_technology_driver *driver1 = a;
78 const struct connman_technology_driver *driver2 = b;
80 return driver2->priority - driver1->priority;
84 * connman_technology_driver_register:
85 * @driver: Technology driver definition
87 * Register a new technology driver
89 * Returns: %0 on success
91 int connman_technology_driver_register(struct connman_technology_driver *driver)
94 struct connman_technology *technology;
96 DBG("driver %p name %s", driver, driver->name);
98 driver_list = g_slist_insert_sorted(driver_list, driver,
101 for (list = technology_list; list; list = list->next) {
102 technology = list->data;
104 if (technology->driver != NULL)
107 if (technology->type == driver->type)
108 technology->driver = driver;
115 * connman_technology_driver_unregister:
116 * @driver: Technology driver definition
118 * Remove a previously registered technology driver
120 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
123 struct connman_technology *technology;
125 DBG("driver %p name %s", driver, driver->name);
127 for (list = technology_list; list; list = list->next) {
128 technology = list->data;
130 if (technology->driver == NULL)
133 if (technology->type == driver->type) {
134 technology->driver->remove(technology);
135 technology->driver = NULL;
139 driver_list = g_slist_remove(driver_list, driver);
142 static void tethering_changed(struct connman_technology *technology)
144 connman_bool_t tethering = technology->tethering;
146 connman_dbus_property_changed_basic(technology->path,
147 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
148 DBUS_TYPE_BOOLEAN, &tethering);
151 void connman_technology_tethering_notify(struct connman_technology *technology,
152 connman_bool_t enabled)
154 DBG("technology %p enabled %u", technology, enabled);
156 if (technology->tethering == enabled)
159 technology->tethering = enabled;
161 tethering_changed(technology);
164 __connman_tethering_set_enabled();
166 __connman_tethering_set_disabled();
169 static int set_tethering(struct connman_technology *technology,
170 const char *bridge, connman_bool_t enabled)
172 const char *ident, *passphrase;
174 ident = technology->tethering_ident;
175 passphrase = technology->tethering_passphrase;
177 if (technology->driver == NULL ||
178 technology->driver->set_tethering == NULL)
181 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
182 (ident == NULL || passphrase == NULL))
185 return technology->driver->set_tethering(technology, ident, passphrase,
189 void connman_technology_regdom_notify(struct connman_technology *technology,
195 connman_error("Failed to set regulatory domain");
197 DBG("Regulatory domain set to %s", alpha2);
199 g_free(technology->regdom);
200 technology->regdom = g_strdup(alpha2);
203 int connman_technology_set_regdom(const char *alpha2)
207 for (list = technology_list; list; list = list->next) {
208 struct connman_technology *technology = list->data;
210 if (technology->driver == NULL)
213 if (technology->driver->set_regdom)
214 technology->driver->set_regdom(technology, alpha2);
220 static void free_rfkill(gpointer data)
222 struct connman_rfkill *rfkill = data;
227 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
231 for (list = technology_list; list; list = list->next) {
232 struct connman_technology *technology = list->data;
234 if (technology->path == NULL)
237 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
242 static void technologies_changed(void)
244 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
245 CONNMAN_MANAGER_INTERFACE, "Technologies",
246 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
249 static const char *state2string(enum connman_technology_state state)
252 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
254 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
256 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
258 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
260 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
267 static void state_changed(struct connman_technology *technology)
271 str = state2string(technology->state);
275 connman_dbus_property_changed_basic(technology->path,
276 CONNMAN_TECHNOLOGY_INTERFACE, "State",
277 DBUS_TYPE_STRING, &str);
280 static const char *get_name(enum connman_service_type type)
283 case CONNMAN_SERVICE_TYPE_UNKNOWN:
284 case CONNMAN_SERVICE_TYPE_SYSTEM:
285 case CONNMAN_SERVICE_TYPE_GPS:
286 case CONNMAN_SERVICE_TYPE_VPN:
287 case CONNMAN_SERVICE_TYPE_GADGET:
289 case CONNMAN_SERVICE_TYPE_ETHERNET:
291 case CONNMAN_SERVICE_TYPE_WIFI:
293 case CONNMAN_SERVICE_TYPE_WIMAX:
295 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
297 case CONNMAN_SERVICE_TYPE_CELLULAR:
304 static DBusMessage *get_properties(DBusConnection *conn,
305 DBusMessage *message, void *user_data)
307 struct connman_technology *technology = user_data;
309 DBusMessageIter array, dict;
312 reply = dbus_message_new_method_return(message);
316 dbus_message_iter_init_append(reply, &array);
318 connman_dbus_dict_open(&array, &dict);
320 str = state2string(technology->state);
322 connman_dbus_dict_append_basic(&dict, "State",
323 DBUS_TYPE_STRING, &str);
325 str = get_name(technology->type);
327 connman_dbus_dict_append_basic(&dict, "Name",
328 DBUS_TYPE_STRING, &str);
330 str = __connman_service_type2string(technology->type);
332 connman_dbus_dict_append_basic(&dict, "Type",
333 DBUS_TYPE_STRING, &str);
335 connman_dbus_dict_append_basic(&dict, "Tethering",
337 &technology->tethering);
339 if (technology->tethering_ident != NULL)
340 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
342 &technology->tethering_ident);
344 if (technology->tethering_passphrase != NULL)
345 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
347 &technology->tethering_passphrase);
349 connman_dbus_dict_close(&array, &dict);
354 static DBusMessage *set_property(DBusConnection *conn,
355 DBusMessage *msg, void *data)
357 struct connman_technology *technology = data;
358 DBusMessageIter iter, value;
362 DBG("conn %p", conn);
364 if (dbus_message_iter_init(msg, &iter) == FALSE)
365 return __connman_error_invalid_arguments(msg);
367 dbus_message_iter_get_basic(&iter, &name);
368 dbus_message_iter_next(&iter);
369 dbus_message_iter_recurse(&iter, &value);
371 type = dbus_message_iter_get_arg_type(&value);
373 DBG("property %s", name);
375 if (g_str_equal(name, "Tethering") == TRUE) {
377 connman_bool_t tethering;
380 if (type != DBUS_TYPE_BOOLEAN)
381 return __connman_error_invalid_arguments(msg);
383 dbus_message_iter_get_basic(&value, &tethering);
385 if (technology->tethering == tethering)
386 return __connman_error_in_progress(msg);
388 bridge = __connman_tethering_get_bridge();
390 return __connman_error_not_supported(msg);
392 err = set_tethering(technology, bridge, tethering);
394 return __connman_error_failed(msg, -err);
396 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
399 dbus_message_iter_get_basic(&value, &str);
401 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
402 return __connman_error_not_supported(msg);
404 technology->tethering_ident = g_strdup(str);
405 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
408 dbus_message_iter_get_basic(&value, &str);
410 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
411 return __connman_error_not_supported(msg);
414 return __connman_error_invalid_arguments(msg);
416 technology->tethering_passphrase = g_strdup(str);
418 return __connman_error_invalid_property(msg);
420 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
423 static GDBusMethodTable technology_methods[] = {
424 { "GetProperties", "", "a{sv}", get_properties },
425 { "SetProperty", "sv", "", set_property },
429 static GDBusSignalTable technology_signals[] = {
430 { "PropertyChanged", "sv" },
434 static struct connman_technology *technology_find(enum connman_service_type type)
438 DBG("type %d", type);
440 for (list = technology_list; list; list = list->next) {
441 struct connman_technology *technology = list->data;
443 if (technology->type == type)
450 static struct connman_technology *technology_get(enum connman_service_type type)
452 struct connman_technology *technology;
456 DBG("type %d", type);
458 technology = technology_find(type);
459 if (technology != NULL) {
460 g_atomic_int_inc(&technology->refcount);
464 str = __connman_service_type2string(type);
468 technology = g_try_new0(struct connman_technology, 1);
469 if (technology == NULL)
472 technology->refcount = 1;
474 technology->type = type;
475 technology->path = g_strdup_printf("%s/technology/%s",
478 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
480 technology->device_list = NULL;
482 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
484 if (g_dbus_register_interface(connection, technology->path,
485 CONNMAN_TECHNOLOGY_INTERFACE,
486 technology_methods, technology_signals,
487 NULL, technology, NULL) == FALSE) {
488 connman_error("Failed to register %s", technology->path);
493 technology_list = g_slist_append(technology_list, technology);
495 technologies_changed();
497 if (technology->driver != NULL)
500 for (list = driver_list; list; list = list->next) {
501 struct connman_technology_driver *driver = list->data;
503 DBG("driver %p name %s", driver, driver->name);
505 if (driver->type != technology->type)
508 if (driver->probe(technology) == 0) {
509 technology->driver = driver;
515 DBG("technology %p", technology);
520 static void technology_put(struct connman_technology *technology)
522 DBG("technology %p", technology);
524 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
527 if (technology->driver) {
528 technology->driver->remove(technology);
529 technology->driver = NULL;
532 technology_list = g_slist_remove(technology_list, technology);
534 technologies_changed();
536 g_dbus_unregister_interface(connection, technology->path,
537 CONNMAN_TECHNOLOGY_INTERFACE);
539 g_slist_free(technology->device_list);
540 g_hash_table_destroy(technology->rfkill_list);
542 g_free(technology->path);
543 g_free(technology->regdom);
547 void __connman_technology_add_interface(enum connman_service_type type,
548 int index, const char *name, const char *ident)
550 struct connman_technology *technology;
553 case CONNMAN_SERVICE_TYPE_UNKNOWN:
554 case CONNMAN_SERVICE_TYPE_SYSTEM:
556 case CONNMAN_SERVICE_TYPE_ETHERNET:
557 case CONNMAN_SERVICE_TYPE_WIFI:
558 case CONNMAN_SERVICE_TYPE_WIMAX:
559 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
560 case CONNMAN_SERVICE_TYPE_CELLULAR:
561 case CONNMAN_SERVICE_TYPE_GPS:
562 case CONNMAN_SERVICE_TYPE_VPN:
563 case CONNMAN_SERVICE_TYPE_GADGET:
567 connman_info("Create interface %s [ %s ]", name,
568 __connman_service_type2string(type));
570 technology = technology_get(type);
572 if (technology == NULL || technology->driver == NULL
573 || technology->driver->add_interface == NULL)
576 technology->driver->add_interface(technology,
580 void __connman_technology_remove_interface(enum connman_service_type type,
581 int index, const char *name, const char *ident)
583 struct connman_technology *technology;
586 case CONNMAN_SERVICE_TYPE_UNKNOWN:
587 case CONNMAN_SERVICE_TYPE_SYSTEM:
589 case CONNMAN_SERVICE_TYPE_ETHERNET:
590 case CONNMAN_SERVICE_TYPE_WIFI:
591 case CONNMAN_SERVICE_TYPE_WIMAX:
592 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
593 case CONNMAN_SERVICE_TYPE_CELLULAR:
594 case CONNMAN_SERVICE_TYPE_GPS:
595 case CONNMAN_SERVICE_TYPE_VPN:
596 case CONNMAN_SERVICE_TYPE_GADGET:
600 connman_info("Remove interface %s [ %s ]", name,
601 __connman_service_type2string(type));
603 technology = technology_find(type);
605 if (technology == NULL || technology->driver == NULL)
608 if (technology->driver->remove_interface)
609 technology->driver->remove_interface(technology, index);
611 technology_put(technology);
614 static void unregister_technology(gpointer data)
616 struct connman_technology *technology = data;
618 technology_put(technology);
621 int __connman_technology_add_device(struct connman_device *device)
623 struct connman_technology *technology;
624 enum connman_service_type type;
626 DBG("device %p", device);
628 type = __connman_device_get_service_type(device);
629 __connman_notifier_register(type);
631 technology = technology_get(type);
632 if (technology == NULL)
635 g_hash_table_insert(device_table, device, technology);
637 if (g_atomic_int_get(&technology->blocked))
640 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
642 state_changed(technology);
646 technology->device_list = g_slist_append(technology->device_list,
652 int __connman_technology_remove_device(struct connman_device *device)
654 struct connman_technology *technology;
655 enum connman_service_type type;
657 DBG("device %p", device);
659 type = __connman_device_get_service_type(device);
660 __connman_notifier_unregister(type);
662 technology = g_hash_table_lookup(device_table, device);
663 if (technology == NULL)
666 technology->device_list = g_slist_remove(technology->device_list,
668 if (technology->device_list == NULL) {
669 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
670 state_changed(technology);
673 g_hash_table_remove(device_table, device);
678 int __connman_technology_enabled(enum connman_service_type type)
680 struct connman_technology *technology;
682 technology = technology_find(type);
683 if (technology == NULL)
686 if (g_atomic_int_get(&technology->blocked))
689 __connman_notifier_enable(type);
691 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
692 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
693 state_changed(technology);
699 int __connman_technology_enable(enum connman_service_type type)
701 struct connman_technology *technology;
706 DBG("type %d enable", type);
708 technology = technology_find(type);
709 if (technology == NULL)
712 for (list = technology->device_list; list; list = list->next) {
713 struct connman_device *device = list->data;
715 err = __connman_device_enable_persistent(device);
717 * err = 0 : Device was enabled right away.
718 * err = -EINPROGRESS : DBus call was successful.
719 * If atleast one device gets enabled, we consider
720 * the technology to be enabled.
722 if (err == 0 || err == -EINPROGRESS)
729 int __connman_technology_disabled(enum connman_service_type type)
731 struct connman_technology *technology;
734 technology = technology_find(type);
735 if (technology == NULL)
738 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
739 __connman_notifier_disable(type);
741 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
742 state_changed(technology);
745 for (list = technology->device_list; list; list = list->next) {
746 struct connman_device *device = list->data;
748 if (__connman_device_get_blocked(device) == FALSE)
752 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
753 state_changed(technology);
758 int __connman_technology_disable(enum connman_service_type type)
760 struct connman_technology *technology;
765 DBG("type %d disable", type);
767 technology = technology_find(type);
768 if (technology == NULL)
771 for (list = technology->device_list; list; list = list->next) {
772 struct connman_device *device = list->data;
774 err = __connman_device_disable_persistent(device);
775 if (err == 0 || err == -EINPROGRESS)
782 static void technology_blocked(struct connman_technology *technology,
783 connman_bool_t blocked)
787 for (list = technology->device_list; list; list = list->next) {
788 struct connman_device *device = list->data;
790 __connman_device_set_blocked(device, blocked);
794 int __connman_technology_add_rfkill(unsigned int index,
795 enum connman_service_type type,
796 connman_bool_t softblock,
797 connman_bool_t hardblock)
799 struct connman_technology *technology;
800 struct connman_rfkill *rfkill;
801 connman_bool_t blocked;
803 DBG("index %u type %d soft %u hard %u", index, type,
804 softblock, hardblock);
806 technology = technology_get(type);
807 if (technology == NULL)
810 rfkill = g_try_new0(struct connman_rfkill, 1);
814 rfkill->index = index;
816 rfkill->softblock = softblock;
817 rfkill->hardblock = hardblock;
819 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
821 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
823 blocked = (softblock || hardblock) ? TRUE : FALSE;
824 if (blocked == FALSE)
827 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
828 technology_blocked(technology, TRUE);
830 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
831 state_changed(technology);
837 int __connman_technology_update_rfkill(unsigned int index,
838 connman_bool_t softblock,
839 connman_bool_t hardblock)
841 struct connman_technology *technology;
842 struct connman_rfkill *rfkill;
843 connman_bool_t blocked, old_blocked;
845 DBG("index %u soft %u hard %u", index, softblock, hardblock);
847 technology = g_hash_table_lookup(rfkill_table, &index);
848 if (technology == NULL)
851 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
855 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
856 blocked = (softblock || hardblock) ? TRUE : FALSE;
858 rfkill->softblock = softblock;
859 rfkill->hardblock = hardblock;
861 if (blocked == old_blocked)
868 g_atomic_int_exchange_and_add(&technology->blocked, 1);
869 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
872 technology_blocked(technology, blocked);
873 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
874 state_changed(technology);
876 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
879 technology_blocked(technology, blocked);
880 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
881 state_changed(technology);
887 int __connman_technology_remove_rfkill(unsigned int index)
889 struct connman_technology *technology;
890 struct connman_rfkill *rfkill;
891 connman_bool_t blocked;
893 DBG("index %u", index);
895 technology = g_hash_table_lookup(rfkill_table, &index);
896 if (technology == NULL)
899 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
903 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
905 g_hash_table_remove(technology->rfkill_list, &index);
907 g_hash_table_remove(rfkill_table, &index);
910 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
911 technology_blocked(technology, FALSE);
912 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
913 state_changed(technology);
919 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
921 struct connman_technology *technology;
923 technology = technology_find(type);
924 if (technology == NULL)
927 if (g_atomic_int_get(&technology->blocked))
933 int __connman_technology_init(void)
937 connection = connman_dbus_get_connection();
939 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
940 NULL, unregister_technology);
941 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
942 NULL, unregister_technology);
947 void __connman_technology_cleanup(void)
951 g_hash_table_destroy(device_table);
952 g_hash_table_destroy(rfkill_table);
954 dbus_connection_unref(connection);