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_ENABLED = 3,
50 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
53 struct connman_technology {
55 enum connman_service_type type;
56 enum connman_technology_state state;
58 GHashTable *rfkill_list;
64 connman_bool_t tethering;
65 char *tethering_ident;
66 char *tethering_passphrase;
68 struct connman_technology_driver *driver;
72 static GSList *driver_list = NULL;
74 static gint compare_priority(gconstpointer a, gconstpointer b)
76 const struct connman_technology_driver *driver1 = a;
77 const struct connman_technology_driver *driver2 = b;
79 return driver2->priority - driver1->priority;
83 * connman_technology_driver_register:
84 * @driver: Technology driver definition
86 * Register a new technology driver
88 * Returns: %0 on success
90 int connman_technology_driver_register(struct connman_technology_driver *driver)
93 struct connman_technology *technology;
95 DBG("driver %p name %s", driver, driver->name);
97 driver_list = g_slist_insert_sorted(driver_list, driver,
100 for (list = technology_list; list; list = list->next) {
101 technology = list->data;
103 if (technology->driver != NULL)
106 if (technology->type == driver->type)
107 technology->driver = driver;
114 * connman_technology_driver_unregister:
115 * @driver: Technology driver definition
117 * Remove a previously registered technology driver
119 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
122 struct connman_technology *technology;
124 DBG("driver %p name %s", driver, driver->name);
126 for (list = technology_list; list; list = list->next) {
127 technology = list->data;
129 if (technology->driver == NULL)
132 if (technology->type == driver->type) {
133 technology->driver->remove(technology);
134 technology->driver = NULL;
138 driver_list = g_slist_remove(driver_list, driver);
141 static void tethering_changed(struct connman_technology *technology)
143 connman_bool_t tethering = technology->tethering;
145 connman_dbus_property_changed_basic(technology->path,
146 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
147 DBUS_TYPE_BOOLEAN, &tethering);
150 void connman_technology_tethering_notify(struct connman_technology *technology,
151 connman_bool_t enabled)
153 DBG("technology %p enabled %u", technology, enabled);
155 if (technology->tethering == enabled)
158 technology->tethering = enabled;
160 tethering_changed(technology);
163 __connman_tethering_set_enabled();
165 __connman_tethering_set_disabled();
168 static int set_tethering(struct connman_technology *technology,
169 const char *bridge, connman_bool_t enabled)
171 const char *ident, *passphrase;
173 ident = technology->tethering_ident;
174 passphrase = technology->tethering_passphrase;
176 if (technology->driver == NULL ||
177 technology->driver->set_tethering == NULL)
180 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
181 (ident == NULL || passphrase == NULL))
184 return technology->driver->set_tethering(technology, ident, passphrase,
188 void connman_technology_regdom_notify(struct connman_technology *technology,
194 connman_error("Failed to set regulatory domain");
196 DBG("Regulatory domain set to %s", alpha2);
198 g_free(technology->regdom);
199 technology->regdom = g_strdup(alpha2);
202 int connman_technology_set_regdom(const char *alpha2)
206 for (list = technology_list; list; list = list->next) {
207 struct connman_technology *technology = list->data;
209 if (technology->driver == NULL)
212 if (technology->driver->set_regdom)
213 technology->driver->set_regdom(technology, alpha2);
219 static void free_rfkill(gpointer data)
221 struct connman_rfkill *rfkill = data;
226 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
230 for (list = technology_list; list; list = list->next) {
231 struct connman_technology *technology = list->data;
233 if (technology->path == NULL)
236 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
241 static void technologies_changed(void)
243 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
244 CONNMAN_MANAGER_INTERFACE, "Technologies",
245 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
248 static const char *state2string(enum connman_technology_state state)
251 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
253 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
255 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
257 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
259 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
266 static void state_changed(struct connman_technology *technology)
270 str = state2string(technology->state);
274 connman_dbus_property_changed_basic(technology->path,
275 CONNMAN_TECHNOLOGY_INTERFACE, "State",
276 DBUS_TYPE_STRING, &str);
279 static const char *get_name(enum connman_service_type type)
282 case CONNMAN_SERVICE_TYPE_UNKNOWN:
283 case CONNMAN_SERVICE_TYPE_SYSTEM:
284 case CONNMAN_SERVICE_TYPE_GPS:
285 case CONNMAN_SERVICE_TYPE_VPN:
286 case CONNMAN_SERVICE_TYPE_GADGET:
288 case CONNMAN_SERVICE_TYPE_ETHERNET:
290 case CONNMAN_SERVICE_TYPE_WIFI:
292 case CONNMAN_SERVICE_TYPE_WIMAX:
294 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
296 case CONNMAN_SERVICE_TYPE_CELLULAR:
303 static DBusMessage *get_properties(DBusConnection *conn,
304 DBusMessage *message, void *user_data)
306 struct connman_technology *technology = user_data;
308 DBusMessageIter array, dict;
311 reply = dbus_message_new_method_return(message);
315 dbus_message_iter_init_append(reply, &array);
317 connman_dbus_dict_open(&array, &dict);
319 str = state2string(technology->state);
321 connman_dbus_dict_append_basic(&dict, "State",
322 DBUS_TYPE_STRING, &str);
324 str = get_name(technology->type);
326 connman_dbus_dict_append_basic(&dict, "Name",
327 DBUS_TYPE_STRING, &str);
329 str = __connman_service_type2string(technology->type);
331 connman_dbus_dict_append_basic(&dict, "Type",
332 DBUS_TYPE_STRING, &str);
334 connman_dbus_dict_append_basic(&dict, "Tethering",
336 &technology->tethering);
338 if (technology->tethering_ident != NULL)
339 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
341 &technology->tethering_ident);
343 if (technology->tethering_passphrase != NULL)
344 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
346 &technology->tethering_passphrase);
348 connman_dbus_dict_close(&array, &dict);
353 static DBusMessage *set_property(DBusConnection *conn,
354 DBusMessage *msg, void *data)
356 struct connman_technology *technology = data;
357 DBusMessageIter iter, value;
361 DBG("conn %p", conn);
363 if (dbus_message_iter_init(msg, &iter) == FALSE)
364 return __connman_error_invalid_arguments(msg);
366 dbus_message_iter_get_basic(&iter, &name);
367 dbus_message_iter_next(&iter);
368 dbus_message_iter_recurse(&iter, &value);
370 type = dbus_message_iter_get_arg_type(&value);
372 DBG("property %s", name);
374 if (g_str_equal(name, "Tethering") == TRUE) {
376 connman_bool_t tethering;
379 if (type != DBUS_TYPE_BOOLEAN)
380 return __connman_error_invalid_arguments(msg);
382 dbus_message_iter_get_basic(&value, &tethering);
384 if (technology->tethering == tethering)
385 return __connman_error_in_progress(msg);
387 bridge = __connman_tethering_get_bridge();
389 return __connman_error_not_supported(msg);
391 err = set_tethering(technology, bridge, tethering);
393 return __connman_error_failed(msg, -err);
395 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
398 dbus_message_iter_get_basic(&value, &str);
400 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
401 return __connman_error_not_supported(msg);
403 technology->tethering_ident = g_strdup(str);
404 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
407 dbus_message_iter_get_basic(&value, &str);
409 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
410 return __connman_error_not_supported(msg);
413 return __connman_error_invalid_arguments(msg);
415 technology->tethering_passphrase = g_strdup(str);
417 return __connman_error_invalid_property(msg);
419 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
422 static GDBusMethodTable technology_methods[] = {
423 { "GetProperties", "", "a{sv}", get_properties },
424 { "SetProperty", "sv", "", set_property },
428 static GDBusSignalTable technology_signals[] = {
429 { "PropertyChanged", "sv" },
433 static struct connman_technology *technology_find(enum connman_service_type type)
437 DBG("type %d", type);
439 for (list = technology_list; list; list = list->next) {
440 struct connman_technology *technology = list->data;
442 if (technology->type == type)
449 static struct connman_technology *technology_get(enum connman_service_type type)
451 struct connman_technology *technology;
455 DBG("type %d", type);
457 technology = technology_find(type);
458 if (technology != NULL) {
459 g_atomic_int_inc(&technology->refcount);
463 str = __connman_service_type2string(type);
467 technology = g_try_new0(struct connman_technology, 1);
468 if (technology == NULL)
471 technology->refcount = 1;
473 technology->type = type;
474 technology->path = g_strdup_printf("%s/technology/%s",
477 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
479 technology->device_list = NULL;
481 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
483 if (g_dbus_register_interface(connection, technology->path,
484 CONNMAN_TECHNOLOGY_INTERFACE,
485 technology_methods, technology_signals,
486 NULL, technology, NULL) == FALSE) {
487 connman_error("Failed to register %s", technology->path);
492 technology_list = g_slist_append(technology_list, technology);
494 technologies_changed();
496 if (technology->driver != NULL)
499 for (list = driver_list; list; list = list->next) {
500 struct connman_technology_driver *driver = list->data;
502 DBG("driver %p name %s", driver, driver->name);
504 if (driver->type != technology->type)
507 if (driver->probe(technology) == 0) {
508 technology->driver = driver;
514 DBG("technology %p", technology);
519 static void technology_put(struct connman_technology *technology)
521 DBG("technology %p", technology);
523 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
526 if (technology->driver) {
527 technology->driver->remove(technology);
528 technology->driver = NULL;
531 technology_list = g_slist_remove(technology_list, technology);
533 technologies_changed();
535 g_dbus_unregister_interface(connection, technology->path,
536 CONNMAN_TECHNOLOGY_INTERFACE);
538 g_slist_free(technology->device_list);
539 g_hash_table_destroy(technology->rfkill_list);
541 g_free(technology->path);
542 g_free(technology->regdom);
546 void __connman_technology_add_interface(enum connman_service_type type,
547 int index, const char *name, const char *ident)
549 struct connman_technology *technology;
552 case CONNMAN_SERVICE_TYPE_UNKNOWN:
553 case CONNMAN_SERVICE_TYPE_SYSTEM:
555 case CONNMAN_SERVICE_TYPE_ETHERNET:
556 case CONNMAN_SERVICE_TYPE_WIFI:
557 case CONNMAN_SERVICE_TYPE_WIMAX:
558 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
559 case CONNMAN_SERVICE_TYPE_CELLULAR:
560 case CONNMAN_SERVICE_TYPE_GPS:
561 case CONNMAN_SERVICE_TYPE_VPN:
562 case CONNMAN_SERVICE_TYPE_GADGET:
566 connman_info("Create interface %s [ %s ]", name,
567 __connman_service_type2string(type));
569 technology = technology_get(type);
571 if (technology == NULL || technology->driver == NULL
572 || technology->driver->add_interface == NULL)
575 technology->driver->add_interface(technology,
579 void __connman_technology_remove_interface(enum connman_service_type type,
580 int index, const char *name, const char *ident)
582 struct connman_technology *technology;
585 case CONNMAN_SERVICE_TYPE_UNKNOWN:
586 case CONNMAN_SERVICE_TYPE_SYSTEM:
588 case CONNMAN_SERVICE_TYPE_ETHERNET:
589 case CONNMAN_SERVICE_TYPE_WIFI:
590 case CONNMAN_SERVICE_TYPE_WIMAX:
591 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
592 case CONNMAN_SERVICE_TYPE_CELLULAR:
593 case CONNMAN_SERVICE_TYPE_GPS:
594 case CONNMAN_SERVICE_TYPE_VPN:
595 case CONNMAN_SERVICE_TYPE_GADGET:
599 connman_info("Remove interface %s [ %s ]", name,
600 __connman_service_type2string(type));
602 technology = technology_find(type);
604 if (technology == NULL || technology->driver == NULL)
607 if (technology->driver->remove_interface)
608 technology->driver->remove_interface(technology, index);
610 technology_put(technology);
613 static void unregister_technology(gpointer data)
615 struct connman_technology *technology = data;
617 technology_put(technology);
620 int __connman_technology_add_device(struct connman_device *device)
622 struct connman_technology *technology;
623 enum connman_service_type type;
625 DBG("device %p", device);
627 type = __connman_device_get_service_type(device);
628 __connman_notifier_register(type);
630 technology = technology_get(type);
631 if (technology == NULL)
634 g_hash_table_insert(device_table, device, technology);
636 if (g_atomic_int_get(&technology->blocked))
639 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
641 state_changed(technology);
645 technology->device_list = g_slist_append(technology->device_list,
651 int __connman_technology_remove_device(struct connman_device *device)
653 struct connman_technology *technology;
654 enum connman_service_type type;
656 DBG("device %p", device);
658 type = __connman_device_get_service_type(device);
659 __connman_notifier_unregister(type);
661 technology = g_hash_table_lookup(device_table, device);
662 if (technology == NULL)
665 technology->device_list = g_slist_remove(technology->device_list,
667 if (technology->device_list == NULL) {
668 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
669 state_changed(technology);
672 g_hash_table_remove(device_table, device);
677 int __connman_technology_enable(enum connman_service_type type)
679 struct connman_technology *technology;
681 technology = technology_find(type);
682 if (technology == NULL)
685 if (g_atomic_int_get(&technology->blocked))
688 __connman_notifier_enable(type);
690 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
691 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
692 state_changed(technology);
698 int __connman_technology_disable(enum connman_service_type type)
700 struct connman_technology *technology;
703 technology = technology_find(type);
704 if (technology == NULL)
707 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
708 __connman_notifier_disable(type);
710 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
711 state_changed(technology);
714 for (list = technology->device_list; list; list = list->next) {
715 struct connman_device *device = list->data;
717 if (__connman_device_get_blocked(device) == FALSE)
721 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
722 state_changed(technology);
727 static void technology_blocked(struct connman_technology *technology,
728 connman_bool_t blocked)
732 for (list = technology->device_list; list; list = list->next) {
733 struct connman_device *device = list->data;
735 __connman_device_set_blocked(device, blocked);
739 int __connman_technology_add_rfkill(unsigned int index,
740 enum connman_service_type type,
741 connman_bool_t softblock,
742 connman_bool_t hardblock)
744 struct connman_technology *technology;
745 struct connman_rfkill *rfkill;
746 connman_bool_t blocked;
748 DBG("index %u type %d soft %u hard %u", index, type,
749 softblock, hardblock);
751 technology = technology_get(type);
752 if (technology == NULL)
755 rfkill = g_try_new0(struct connman_rfkill, 1);
759 rfkill->index = index;
761 rfkill->softblock = softblock;
762 rfkill->hardblock = hardblock;
764 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
766 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
768 blocked = (softblock || hardblock) ? TRUE : FALSE;
769 if (blocked == FALSE)
772 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
773 technology_blocked(technology, TRUE);
775 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
776 state_changed(technology);
782 int __connman_technology_update_rfkill(unsigned int index,
783 connman_bool_t softblock,
784 connman_bool_t hardblock)
786 struct connman_technology *technology;
787 struct connman_rfkill *rfkill;
788 connman_bool_t blocked, old_blocked;
790 DBG("index %u soft %u hard %u", index, softblock, hardblock);
792 technology = g_hash_table_lookup(rfkill_table, &index);
793 if (technology == NULL)
796 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
800 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
801 blocked = (softblock || hardblock) ? TRUE : FALSE;
803 rfkill->softblock = softblock;
804 rfkill->hardblock = hardblock;
806 if (blocked == old_blocked)
813 g_atomic_int_exchange_and_add(&technology->blocked, 1);
814 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
817 technology_blocked(technology, blocked);
818 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
819 state_changed(technology);
821 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
824 technology_blocked(technology, blocked);
825 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
826 state_changed(technology);
832 int __connman_technology_remove_rfkill(unsigned int index)
834 struct connman_technology *technology;
835 struct connman_rfkill *rfkill;
836 connman_bool_t blocked;
838 DBG("index %u", index);
840 technology = g_hash_table_lookup(rfkill_table, &index);
841 if (technology == NULL)
844 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
848 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
850 g_hash_table_remove(technology->rfkill_list, &index);
852 g_hash_table_remove(rfkill_table, &index);
855 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
856 technology_blocked(technology, FALSE);
857 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
858 state_changed(technology);
864 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
866 struct connman_technology *technology;
868 technology = technology_find(type);
869 if (technology == NULL)
872 if (g_atomic_int_get(&technology->blocked))
878 int __connman_technology_init(void)
882 connection = connman_dbus_get_connection();
884 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
885 NULL, unregister_technology);
886 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
887 NULL, unregister_technology);
892 void __connman_technology_cleanup(void)
896 g_hash_table_destroy(device_table);
897 g_hash_table_destroy(rfkill_table);
899 dbus_connection_unref(connection);