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 void __connman_technology_add_interface(enum connman_service_type type,
143 int index, const char *name, const char *ident)
148 case CONNMAN_SERVICE_TYPE_UNKNOWN:
149 case CONNMAN_SERVICE_TYPE_SYSTEM:
151 case CONNMAN_SERVICE_TYPE_ETHERNET:
152 case CONNMAN_SERVICE_TYPE_WIFI:
153 case CONNMAN_SERVICE_TYPE_WIMAX:
154 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
155 case CONNMAN_SERVICE_TYPE_CELLULAR:
156 case CONNMAN_SERVICE_TYPE_GPS:
157 case CONNMAN_SERVICE_TYPE_VPN:
158 case CONNMAN_SERVICE_TYPE_GADGET:
162 connman_info("Create interface %s [ %s ]", name,
163 __connman_service_type2string(type));
165 for (list = technology_list; list; list = list->next) {
166 struct connman_technology *technology = list->data;
168 if (technology->type != type)
171 if (technology->driver == NULL)
174 if (technology->driver->add_interface)
175 technology->driver->add_interface(technology,
180 void __connman_technology_remove_interface(enum connman_service_type type,
181 int index, const char *name, const char *ident)
186 case CONNMAN_SERVICE_TYPE_UNKNOWN:
187 case CONNMAN_SERVICE_TYPE_SYSTEM:
189 case CONNMAN_SERVICE_TYPE_ETHERNET:
190 case CONNMAN_SERVICE_TYPE_WIFI:
191 case CONNMAN_SERVICE_TYPE_WIMAX:
192 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
193 case CONNMAN_SERVICE_TYPE_CELLULAR:
194 case CONNMAN_SERVICE_TYPE_GPS:
195 case CONNMAN_SERVICE_TYPE_VPN:
196 case CONNMAN_SERVICE_TYPE_GADGET:
200 connman_info("Remove interface %s [ %s ]", name,
201 __connman_service_type2string(type));
203 for (list = technology_list; list; list = list->next) {
204 struct connman_technology *technology = list->data;
206 if (technology->type != type)
209 if (technology->driver == NULL)
212 if (technology->driver->remove_interface)
213 technology->driver->remove_interface(technology, index);
217 static void tethering_changed(struct connman_technology *technology)
219 connman_bool_t tethering = technology->tethering;
221 connman_dbus_property_changed_basic(technology->path,
222 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
223 DBUS_TYPE_BOOLEAN, &tethering);
226 void connman_technology_tethering_notify(struct connman_technology *technology,
227 connman_bool_t enabled)
229 DBG("technology %p enabled %u", technology, enabled);
231 if (technology->tethering == enabled)
234 technology->tethering = enabled;
236 tethering_changed(technology);
239 __connman_tethering_set_enabled();
241 __connman_tethering_set_disabled();
244 static int set_tethering(struct connman_technology *technology,
245 const char *bridge, connman_bool_t enabled)
247 const char *ident, *passphrase;
249 ident = technology->tethering_ident;
250 passphrase = technology->tethering_passphrase;
252 if (technology->driver == NULL ||
253 technology->driver->set_tethering == NULL)
256 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
257 (ident == NULL || passphrase == NULL))
260 return technology->driver->set_tethering(technology, ident, passphrase,
264 void connman_technology_regdom_notify(struct connman_technology *technology,
270 connman_error("Failed to set regulatory domain");
272 DBG("Regulatory domain set to %s", alpha2);
274 g_free(technology->regdom);
275 technology->regdom = g_strdup(alpha2);
278 int connman_technology_set_regdom(const char *alpha2)
282 for (list = technology_list; list; list = list->next) {
283 struct connman_technology *technology = list->data;
285 if (technology->driver == NULL)
288 if (technology->driver->set_regdom)
289 technology->driver->set_regdom(technology, alpha2);
295 static void free_rfkill(gpointer data)
297 struct connman_rfkill *rfkill = data;
302 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
306 for (list = technology_list; list; list = list->next) {
307 struct connman_technology *technology = list->data;
309 if (technology->path == NULL)
312 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
317 static void technologies_changed(void)
319 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
320 CONNMAN_MANAGER_INTERFACE, "Technologies",
321 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
324 static const char *state2string(enum connman_technology_state state)
327 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
329 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
331 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
333 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
335 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
337 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
344 static void state_changed(struct connman_technology *technology)
348 str = state2string(technology->state);
352 connman_dbus_property_changed_basic(technology->path,
353 CONNMAN_TECHNOLOGY_INTERFACE, "State",
354 DBUS_TYPE_STRING, &str);
357 static const char *get_name(enum connman_service_type type)
360 case CONNMAN_SERVICE_TYPE_UNKNOWN:
361 case CONNMAN_SERVICE_TYPE_SYSTEM:
362 case CONNMAN_SERVICE_TYPE_GPS:
363 case CONNMAN_SERVICE_TYPE_VPN:
364 case CONNMAN_SERVICE_TYPE_GADGET:
366 case CONNMAN_SERVICE_TYPE_ETHERNET:
368 case CONNMAN_SERVICE_TYPE_WIFI:
370 case CONNMAN_SERVICE_TYPE_WIMAX:
372 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
374 case CONNMAN_SERVICE_TYPE_CELLULAR:
381 static DBusMessage *get_properties(DBusConnection *conn,
382 DBusMessage *message, void *user_data)
384 struct connman_technology *technology = user_data;
386 DBusMessageIter array, dict;
389 reply = dbus_message_new_method_return(message);
393 dbus_message_iter_init_append(reply, &array);
395 connman_dbus_dict_open(&array, &dict);
397 str = state2string(technology->state);
399 connman_dbus_dict_append_basic(&dict, "State",
400 DBUS_TYPE_STRING, &str);
402 str = get_name(technology->type);
404 connman_dbus_dict_append_basic(&dict, "Name",
405 DBUS_TYPE_STRING, &str);
407 str = __connman_service_type2string(technology->type);
409 connman_dbus_dict_append_basic(&dict, "Type",
410 DBUS_TYPE_STRING, &str);
412 connman_dbus_dict_append_basic(&dict, "Tethering",
414 &technology->tethering);
416 if (technology->tethering_ident != NULL)
417 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
419 &technology->tethering_ident);
421 if (technology->tethering_passphrase != NULL)
422 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
424 &technology->tethering_passphrase);
426 connman_dbus_dict_close(&array, &dict);
431 static DBusMessage *set_property(DBusConnection *conn,
432 DBusMessage *msg, void *data)
434 struct connman_technology *technology = data;
435 DBusMessageIter iter, value;
439 DBG("conn %p", conn);
441 if (dbus_message_iter_init(msg, &iter) == FALSE)
442 return __connman_error_invalid_arguments(msg);
444 dbus_message_iter_get_basic(&iter, &name);
445 dbus_message_iter_next(&iter);
446 dbus_message_iter_recurse(&iter, &value);
448 type = dbus_message_iter_get_arg_type(&value);
450 DBG("property %s", name);
452 if (g_str_equal(name, "Tethering") == TRUE) {
454 connman_bool_t tethering;
457 if (type != DBUS_TYPE_BOOLEAN)
458 return __connman_error_invalid_arguments(msg);
460 dbus_message_iter_get_basic(&value, &tethering);
462 if (technology->tethering == tethering)
463 return __connman_error_in_progress(msg);
465 bridge = __connman_tethering_get_bridge();
467 err = set_tethering(technology, bridge, tethering);
469 return __connman_error_failed(msg, -err);
471 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
474 dbus_message_iter_get_basic(&value, &str);
476 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
477 return __connman_error_not_supported(msg);
479 technology->tethering_ident = g_strdup(str);
480 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
483 dbus_message_iter_get_basic(&value, &str);
485 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
486 return __connman_error_not_supported(msg);
489 return __connman_error_invalid_arguments(msg);
491 technology->tethering_passphrase = g_strdup(str);
493 return __connman_error_invalid_property(msg);
495 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
498 static GDBusMethodTable technology_methods[] = {
499 { "GetProperties", "", "a{sv}", get_properties },
500 { "SetProperty", "sv", "", set_property },
504 static GDBusSignalTable technology_signals[] = {
505 { "PropertyChanged", "sv" },
509 static struct connman_technology *technology_find(enum connman_service_type type)
513 DBG("type %d", type);
515 for (list = technology_list; list; list = list->next) {
516 struct connman_technology *technology = list->data;
518 if (technology->type == type)
525 static struct connman_technology *technology_get(enum connman_service_type type)
527 struct connman_technology *technology;
531 DBG("type %d", type);
533 technology = technology_find(type);
534 if (technology != NULL) {
535 g_atomic_int_inc(&technology->refcount);
539 str = __connman_service_type2string(type);
543 technology = g_try_new0(struct connman_technology, 1);
544 if (technology == NULL)
547 technology->refcount = 1;
549 technology->type = type;
550 technology->path = g_strdup_printf("%s/technology/%s",
553 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
555 technology->device_list = NULL;
557 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
559 if (g_dbus_register_interface(connection, technology->path,
560 CONNMAN_TECHNOLOGY_INTERFACE,
561 technology_methods, technology_signals,
562 NULL, technology, NULL) == FALSE) {
563 connman_error("Failed to register %s", technology->path);
568 technology_list = g_slist_append(technology_list, technology);
570 technologies_changed();
572 if (technology->driver != NULL)
575 for (list = driver_list; list; list = list->next) {
576 struct connman_technology_driver *driver = list->data;
578 DBG("driver %p name %s", driver, driver->name);
580 if (driver->type != technology->type)
583 if (driver->probe(technology) == 0) {
584 technology->driver = driver;
590 DBG("technology %p", technology);
595 static void technology_put(struct connman_technology *technology)
597 DBG("technology %p", technology);
599 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
602 if (technology->driver) {
603 technology->driver->remove(technology);
604 technology->driver = NULL;
607 technology_list = g_slist_remove(technology_list, technology);
609 technologies_changed();
611 g_dbus_unregister_interface(connection, technology->path,
612 CONNMAN_TECHNOLOGY_INTERFACE);
614 g_slist_free(technology->device_list);
615 g_hash_table_destroy(technology->rfkill_list);
617 g_free(technology->path);
618 g_free(technology->regdom);
622 static void unregister_technology(gpointer data)
624 struct connman_technology *technology = data;
626 technology_put(technology);
629 int __connman_technology_add_device(struct connman_device *device)
631 struct connman_technology *technology;
632 enum connman_service_type type;
634 DBG("device %p", device);
636 type = __connman_device_get_service_type(device);
637 __connman_notifier_register(type);
639 technology = technology_get(type);
640 if (technology == NULL)
643 g_hash_table_insert(device_table, device, technology);
645 if (g_atomic_int_get(&technology->blocked))
648 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
650 state_changed(technology);
654 technology->device_list = g_slist_append(technology->device_list,
660 int __connman_technology_remove_device(struct connman_device *device)
662 struct connman_technology *technology;
663 enum connman_service_type type;
665 DBG("device %p", device);
667 type = __connman_device_get_service_type(device);
668 __connman_notifier_unregister(type);
670 technology = g_hash_table_lookup(device_table, device);
671 if (technology == NULL)
674 technology->device_list = g_slist_remove(technology->device_list,
676 if (technology->device_list == NULL) {
677 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
678 state_changed(technology);
681 g_hash_table_remove(device_table, device);
686 int __connman_technology_enable_device(struct connman_device *device)
688 struct connman_technology *technology;
689 enum connman_service_type type;
691 DBG("device %p", device);
693 technology = g_hash_table_lookup(device_table, device);
694 if (technology == NULL)
697 if (g_atomic_int_get(&technology->blocked))
700 type = __connman_device_get_service_type(device);
701 __connman_notifier_enable(type);
703 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
704 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
705 state_changed(technology);
711 int __connman_technology_disable_device(struct connman_device *device)
713 struct connman_technology *technology;
714 enum connman_service_type type;
717 DBG("device %p", device);
719 type = __connman_device_get_service_type(device);
720 __connman_notifier_disable(type);
722 technology = g_hash_table_lookup(device_table, device);
723 if (technology == NULL)
726 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
727 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
728 state_changed(technology);
731 for (list = technology->device_list; list; list = list->next) {
732 struct connman_device *device = list->data;
734 if (__connman_device_get_blocked(device) == FALSE)
738 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
739 state_changed(technology);
744 static void technology_blocked(struct connman_technology *technology,
745 connman_bool_t blocked)
749 for (list = technology->device_list; list; list = list->next) {
750 struct connman_device *device = list->data;
752 __connman_device_set_blocked(device, blocked);
756 int __connman_technology_add_rfkill(unsigned int index,
757 enum connman_service_type type,
758 connman_bool_t softblock,
759 connman_bool_t hardblock)
761 struct connman_technology *technology;
762 struct connman_rfkill *rfkill;
763 connman_bool_t blocked;
765 DBG("index %u type %d soft %u hard %u", index, type,
766 softblock, hardblock);
768 technology = technology_get(type);
769 if (technology == NULL)
772 rfkill = g_try_new0(struct connman_rfkill, 1);
776 rfkill->index = index;
778 rfkill->softblock = softblock;
779 rfkill->hardblock = hardblock;
781 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
783 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
785 blocked = (softblock || hardblock) ? TRUE : FALSE;
786 if (blocked == FALSE)
789 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
790 technology_blocked(technology, TRUE);
792 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
793 state_changed(technology);
799 int __connman_technology_update_rfkill(unsigned int index,
800 connman_bool_t softblock,
801 connman_bool_t hardblock)
803 struct connman_technology *technology;
804 struct connman_rfkill *rfkill;
805 connman_bool_t blocked, old_blocked;
807 DBG("index %u soft %u hard %u", index, softblock, hardblock);
809 technology = g_hash_table_lookup(rfkill_table, &index);
810 if (technology == NULL)
813 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
817 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
818 blocked = (softblock || hardblock) ? TRUE : FALSE;
820 rfkill->softblock = softblock;
821 rfkill->hardblock = hardblock;
823 if (blocked == old_blocked)
830 g_atomic_int_exchange_and_add(&technology->blocked, 1);
831 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
834 technology_blocked(technology, blocked);
835 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
836 state_changed(technology);
838 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
841 technology_blocked(technology, blocked);
842 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
843 state_changed(technology);
849 int __connman_technology_remove_rfkill(unsigned int index)
851 struct connman_technology *technology;
852 struct connman_rfkill *rfkill;
853 connman_bool_t blocked;
855 DBG("index %u", index);
857 technology = g_hash_table_lookup(rfkill_table, &index);
858 if (technology == NULL)
861 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
865 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
867 g_hash_table_remove(technology->rfkill_list, &index);
869 g_hash_table_remove(rfkill_table, &index);
872 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
873 technology_blocked(technology, FALSE);
874 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
875 state_changed(technology);
881 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
883 struct connman_technology *technology;
885 technology = technology_find(type);
886 if (technology == NULL)
889 if (g_atomic_int_get(&technology->blocked))
895 int __connman_technology_init(void)
899 connection = connman_dbus_get_connection();
901 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
902 NULL, unregister_technology);
903 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
904 NULL, unregister_technology);
909 void __connman_technology_cleanup(void)
913 g_hash_table_destroy(device_table);
914 g_hash_table_destroy(rfkill_table);
916 dbus_connection_unref(connection);