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
32 static DBusConnection *connection;
34 static GHashTable *rfkill_table;
35 static GHashTable *device_table;
36 static GSList *technology_list = NULL;
38 struct connman_rfkill {
40 enum connman_service_type type;
41 connman_bool_t softblock;
42 connman_bool_t hardblock;
45 enum connman_technology_state {
46 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
47 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
48 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
49 CONNMAN_TECHNOLOGY_STATE_BLOCKED = 3,
50 CONNMAN_TECHNOLOGY_STATE_ENABLED = 4,
51 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 5,
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_BLOCKED:
260 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
262 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
269 static void state_changed(struct connman_technology *technology)
273 str = state2string(technology->state);
277 connman_dbus_property_changed_basic(technology->path,
278 CONNMAN_TECHNOLOGY_INTERFACE, "State",
279 DBUS_TYPE_STRING, &str);
282 static const char *get_name(enum connman_service_type type)
285 case CONNMAN_SERVICE_TYPE_UNKNOWN:
286 case CONNMAN_SERVICE_TYPE_SYSTEM:
287 case CONNMAN_SERVICE_TYPE_GPS:
288 case CONNMAN_SERVICE_TYPE_VPN:
289 case CONNMAN_SERVICE_TYPE_GADGET:
291 case CONNMAN_SERVICE_TYPE_ETHERNET:
293 case CONNMAN_SERVICE_TYPE_WIFI:
295 case CONNMAN_SERVICE_TYPE_WIMAX:
297 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
299 case CONNMAN_SERVICE_TYPE_CELLULAR:
306 static DBusMessage *get_properties(DBusConnection *conn,
307 DBusMessage *message, void *user_data)
309 struct connman_technology *technology = user_data;
311 DBusMessageIter array, dict;
314 reply = dbus_message_new_method_return(message);
318 dbus_message_iter_init_append(reply, &array);
320 connman_dbus_dict_open(&array, &dict);
322 str = state2string(technology->state);
324 connman_dbus_dict_append_basic(&dict, "State",
325 DBUS_TYPE_STRING, &str);
327 str = get_name(technology->type);
329 connman_dbus_dict_append_basic(&dict, "Name",
330 DBUS_TYPE_STRING, &str);
332 str = __connman_service_type2string(technology->type);
334 connman_dbus_dict_append_basic(&dict, "Type",
335 DBUS_TYPE_STRING, &str);
337 connman_dbus_dict_append_basic(&dict, "Tethering",
339 &technology->tethering);
341 if (technology->tethering_ident != NULL)
342 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
344 &technology->tethering_ident);
346 if (technology->tethering_passphrase != NULL)
347 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
349 &technology->tethering_passphrase);
351 connman_dbus_dict_close(&array, &dict);
356 static DBusMessage *set_property(DBusConnection *conn,
357 DBusMessage *msg, void *data)
359 struct connman_technology *technology = data;
360 DBusMessageIter iter, value;
364 DBG("conn %p", conn);
366 if (dbus_message_iter_init(msg, &iter) == FALSE)
367 return __connman_error_invalid_arguments(msg);
369 dbus_message_iter_get_basic(&iter, &name);
370 dbus_message_iter_next(&iter);
371 dbus_message_iter_recurse(&iter, &value);
373 type = dbus_message_iter_get_arg_type(&value);
375 DBG("property %s", name);
377 if (g_str_equal(name, "Tethering") == TRUE) {
379 connman_bool_t tethering;
382 if (type != DBUS_TYPE_BOOLEAN)
383 return __connman_error_invalid_arguments(msg);
385 dbus_message_iter_get_basic(&value, &tethering);
387 if (technology->tethering == tethering)
388 return __connman_error_in_progress(msg);
390 bridge = __connman_tethering_get_bridge();
392 return __connman_error_not_supported(msg);
394 err = set_tethering(technology, bridge, tethering);
396 return __connman_error_failed(msg, -err);
398 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
401 dbus_message_iter_get_basic(&value, &str);
403 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
404 return __connman_error_not_supported(msg);
406 technology->tethering_ident = g_strdup(str);
407 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
410 dbus_message_iter_get_basic(&value, &str);
412 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
413 return __connman_error_not_supported(msg);
416 return __connman_error_invalid_arguments(msg);
418 technology->tethering_passphrase = g_strdup(str);
420 return __connman_error_invalid_property(msg);
422 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
425 static GDBusMethodTable technology_methods[] = {
426 { "GetProperties", "", "a{sv}", get_properties },
427 { "SetProperty", "sv", "", set_property },
431 static GDBusSignalTable technology_signals[] = {
432 { "PropertyChanged", "sv" },
436 static struct connman_technology *technology_find(enum connman_service_type type)
440 DBG("type %d", type);
442 for (list = technology_list; list; list = list->next) {
443 struct connman_technology *technology = list->data;
445 if (technology->type == type)
452 static struct connman_technology *technology_get(enum connman_service_type type)
454 struct connman_technology *technology;
458 DBG("type %d", type);
460 technology = technology_find(type);
461 if (technology != NULL) {
462 g_atomic_int_inc(&technology->refcount);
466 str = __connman_service_type2string(type);
470 technology = g_try_new0(struct connman_technology, 1);
471 if (technology == NULL)
474 technology->refcount = 1;
476 technology->type = type;
477 technology->path = g_strdup_printf("%s/technology/%s",
480 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
482 technology->device_list = NULL;
484 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
486 if (g_dbus_register_interface(connection, technology->path,
487 CONNMAN_TECHNOLOGY_INTERFACE,
488 technology_methods, technology_signals,
489 NULL, technology, NULL) == FALSE) {
490 connman_error("Failed to register %s", technology->path);
495 technology_list = g_slist_append(technology_list, technology);
497 technologies_changed();
499 if (technology->driver != NULL)
502 for (list = driver_list; list; list = list->next) {
503 struct connman_technology_driver *driver = list->data;
505 DBG("driver %p name %s", driver, driver->name);
507 if (driver->type != technology->type)
510 if (driver->probe(technology) == 0) {
511 technology->driver = driver;
517 DBG("technology %p", technology);
522 static void technology_put(struct connman_technology *technology)
524 DBG("technology %p", technology);
526 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
529 if (technology->driver) {
530 technology->driver->remove(technology);
531 technology->driver = NULL;
534 technology_list = g_slist_remove(technology_list, technology);
536 technologies_changed();
538 g_dbus_unregister_interface(connection, technology->path,
539 CONNMAN_TECHNOLOGY_INTERFACE);
541 g_slist_free(technology->device_list);
542 g_hash_table_destroy(technology->rfkill_list);
544 g_free(technology->path);
545 g_free(technology->regdom);
549 void __connman_technology_add_interface(enum connman_service_type type,
550 int index, const char *name, const char *ident)
552 struct connman_technology *technology;
555 case CONNMAN_SERVICE_TYPE_UNKNOWN:
556 case CONNMAN_SERVICE_TYPE_SYSTEM:
558 case CONNMAN_SERVICE_TYPE_ETHERNET:
559 case CONNMAN_SERVICE_TYPE_WIFI:
560 case CONNMAN_SERVICE_TYPE_WIMAX:
561 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
562 case CONNMAN_SERVICE_TYPE_CELLULAR:
563 case CONNMAN_SERVICE_TYPE_GPS:
564 case CONNMAN_SERVICE_TYPE_VPN:
565 case CONNMAN_SERVICE_TYPE_GADGET:
569 connman_info("Create interface %s [ %s ]", name,
570 __connman_service_type2string(type));
572 technology = technology_get(type);
574 if (technology == NULL || technology->driver == NULL
575 || technology->driver->add_interface == NULL)
578 technology->driver->add_interface(technology,
582 void __connman_technology_remove_interface(enum connman_service_type type,
583 int index, const char *name, const char *ident)
585 struct connman_technology *technology;
588 case CONNMAN_SERVICE_TYPE_UNKNOWN:
589 case CONNMAN_SERVICE_TYPE_SYSTEM:
591 case CONNMAN_SERVICE_TYPE_ETHERNET:
592 case CONNMAN_SERVICE_TYPE_WIFI:
593 case CONNMAN_SERVICE_TYPE_WIMAX:
594 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
595 case CONNMAN_SERVICE_TYPE_CELLULAR:
596 case CONNMAN_SERVICE_TYPE_GPS:
597 case CONNMAN_SERVICE_TYPE_VPN:
598 case CONNMAN_SERVICE_TYPE_GADGET:
602 connman_info("Remove interface %s [ %s ]", name,
603 __connman_service_type2string(type));
605 technology = technology_find(type);
607 if (technology == NULL || technology->driver == NULL)
610 if (technology->driver->remove_interface)
611 technology->driver->remove_interface(technology, index);
613 technology_put(technology);
616 static void unregister_technology(gpointer data)
618 struct connman_technology *technology = data;
620 technology_put(technology);
623 int __connman_technology_add_device(struct connman_device *device)
625 struct connman_technology *technology;
626 enum connman_service_type type;
628 DBG("device %p", device);
630 type = __connman_device_get_service_type(device);
631 __connman_notifier_register(type);
633 technology = technology_get(type);
634 if (technology == NULL)
637 g_hash_table_insert(device_table, device, technology);
639 if (g_atomic_int_get(&technology->blocked))
642 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
644 state_changed(technology);
648 technology->device_list = g_slist_append(technology->device_list,
654 int __connman_technology_remove_device(struct connman_device *device)
656 struct connman_technology *technology;
657 enum connman_service_type type;
659 DBG("device %p", device);
661 type = __connman_device_get_service_type(device);
662 __connman_notifier_unregister(type);
664 technology = g_hash_table_lookup(device_table, device);
665 if (technology == NULL)
668 technology->device_list = g_slist_remove(technology->device_list,
670 if (technology->device_list == NULL) {
671 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
672 state_changed(technology);
675 g_hash_table_remove(device_table, device);
680 int __connman_technology_enable_device(struct connman_device *device)
682 struct connman_technology *technology;
683 enum connman_service_type type;
685 DBG("device %p", device);
687 technology = g_hash_table_lookup(device_table, device);
688 if (technology == NULL)
691 if (g_atomic_int_get(&technology->blocked))
694 type = __connman_device_get_service_type(device);
695 __connman_notifier_enable(type);
697 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
698 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
699 state_changed(technology);
705 int __connman_technology_disable_device(struct connman_device *device)
707 struct connman_technology *technology;
708 enum connman_service_type type;
711 DBG("device %p", device);
713 type = __connman_device_get_service_type(device);
714 __connman_notifier_disable(type);
716 technology = g_hash_table_lookup(device_table, device);
717 if (technology == NULL)
720 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
721 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
722 state_changed(technology);
725 for (list = technology->device_list; list; list = list->next) {
726 struct connman_device *device = list->data;
728 if (__connman_device_get_blocked(device) == FALSE)
732 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
733 state_changed(technology);
738 static void technology_blocked(struct connman_technology *technology,
739 connman_bool_t blocked)
743 for (list = technology->device_list; list; list = list->next) {
744 struct connman_device *device = list->data;
746 __connman_device_set_blocked(device, blocked);
750 int __connman_technology_add_rfkill(unsigned int index,
751 enum connman_service_type type,
752 connman_bool_t softblock,
753 connman_bool_t hardblock)
755 struct connman_technology *technology;
756 struct connman_rfkill *rfkill;
757 connman_bool_t blocked;
759 DBG("index %u type %d soft %u hard %u", index, type,
760 softblock, hardblock);
762 technology = technology_get(type);
763 if (technology == NULL)
766 rfkill = g_try_new0(struct connman_rfkill, 1);
770 rfkill->index = index;
772 rfkill->softblock = softblock;
773 rfkill->hardblock = hardblock;
775 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
777 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
779 blocked = (softblock || hardblock) ? TRUE : FALSE;
780 if (blocked == FALSE)
783 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
784 technology_blocked(technology, TRUE);
786 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
787 state_changed(technology);
793 int __connman_technology_update_rfkill(unsigned int index,
794 connman_bool_t softblock,
795 connman_bool_t hardblock)
797 struct connman_technology *technology;
798 struct connman_rfkill *rfkill;
799 connman_bool_t blocked, old_blocked;
801 DBG("index %u soft %u hard %u", index, softblock, hardblock);
803 technology = g_hash_table_lookup(rfkill_table, &index);
804 if (technology == NULL)
807 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
811 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
812 blocked = (softblock || hardblock) ? TRUE : FALSE;
814 rfkill->softblock = softblock;
815 rfkill->hardblock = hardblock;
817 if (blocked == old_blocked)
824 g_atomic_int_exchange_and_add(&technology->blocked, 1);
825 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
828 technology_blocked(technology, blocked);
829 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
830 state_changed(technology);
832 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
835 technology_blocked(technology, blocked);
836 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
837 state_changed(technology);
843 int __connman_technology_remove_rfkill(unsigned int index)
845 struct connman_technology *technology;
846 struct connman_rfkill *rfkill;
847 connman_bool_t blocked;
849 DBG("index %u", index);
851 technology = g_hash_table_lookup(rfkill_table, &index);
852 if (technology == NULL)
855 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
859 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
861 g_hash_table_remove(technology->rfkill_list, &index);
863 g_hash_table_remove(rfkill_table, &index);
866 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
867 technology_blocked(technology, FALSE);
868 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
869 state_changed(technology);
875 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
877 struct connman_technology *technology;
879 technology = technology_find(type);
880 if (technology == NULL)
883 if (g_atomic_int_get(&technology->blocked))
889 int __connman_technology_init(void)
893 connection = connman_dbus_get_connection();
895 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
896 NULL, unregister_technology);
897 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
898 NULL, unregister_technology);
903 void __connman_technology_cleanup(void)
907 g_hash_table_destroy(device_table);
908 g_hash_table_destroy(rfkill_table);
910 dbus_connection_unref(connection);