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 GSList *technology_list = NULL;
37 struct connman_rfkill {
39 enum connman_service_type type;
40 connman_bool_t softblock;
41 connman_bool_t hardblock;
44 enum connman_technology_state {
45 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
46 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
47 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
48 CONNMAN_TECHNOLOGY_STATE_ENABLED = 3,
49 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
52 struct connman_technology {
54 enum connman_service_type type;
55 enum connman_technology_state state;
57 GHashTable *rfkill_list;
63 connman_bool_t tethering;
64 char *tethering_ident;
65 char *tethering_passphrase;
67 struct connman_technology_driver *driver;
70 DBusMessage *pending_reply;
71 guint pending_timeout;
74 static GSList *driver_list = NULL;
76 static gint compare_priority(gconstpointer a, gconstpointer b)
78 const struct connman_technology_driver *driver1 = a;
79 const struct connman_technology_driver *driver2 = b;
81 return driver2->priority - driver1->priority;
85 * connman_technology_driver_register:
86 * @driver: Technology driver definition
88 * Register a new technology driver
90 * Returns: %0 on success
92 int connman_technology_driver_register(struct connman_technology_driver *driver)
95 struct connman_technology *technology;
97 DBG("driver %p name %s", driver, driver->name);
99 driver_list = g_slist_insert_sorted(driver_list, driver,
102 for (list = technology_list; list; list = list->next) {
103 technology = list->data;
105 if (technology->driver != NULL)
108 if (technology->type == driver->type)
109 technology->driver = driver;
116 * connman_technology_driver_unregister:
117 * @driver: Technology driver definition
119 * Remove a previously registered technology driver
121 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
124 struct connman_technology *technology;
126 DBG("driver %p name %s", driver, driver->name);
128 for (list = technology_list; list; list = list->next) {
129 technology = list->data;
131 if (technology->driver == NULL)
134 if (technology->type == driver->type) {
135 technology->driver->remove(technology);
136 technology->driver = NULL;
140 driver_list = g_slist_remove(driver_list, driver);
143 static void tethering_changed(struct connman_technology *technology)
145 connman_bool_t tethering = technology->tethering;
147 connman_dbus_property_changed_basic(technology->path,
148 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
149 DBUS_TYPE_BOOLEAN, &tethering);
152 void connman_technology_tethering_notify(struct connman_technology *technology,
153 connman_bool_t enabled)
155 DBG("technology %p enabled %u", technology, enabled);
157 if (technology->tethering == enabled)
160 technology->tethering = enabled;
162 tethering_changed(technology);
165 __connman_tethering_set_enabled();
167 __connman_tethering_set_disabled();
170 static int set_tethering(struct connman_technology *technology,
171 const char *bridge, connman_bool_t enabled)
173 const char *ident, *passphrase;
175 ident = technology->tethering_ident;
176 passphrase = technology->tethering_passphrase;
178 if (technology->driver == NULL ||
179 technology->driver->set_tethering == NULL)
182 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
183 (ident == NULL || passphrase == NULL))
186 return technology->driver->set_tethering(technology, ident, passphrase,
190 void connman_technology_regdom_notify(struct connman_technology *technology,
196 connman_error("Failed to set regulatory domain");
198 DBG("Regulatory domain set to %s", alpha2);
200 g_free(technology->regdom);
201 technology->regdom = g_strdup(alpha2);
204 int connman_technology_set_regdom(const char *alpha2)
208 for (list = technology_list; list; list = list->next) {
209 struct connman_technology *technology = list->data;
211 if (technology->driver == NULL)
214 if (technology->driver->set_regdom)
215 technology->driver->set_regdom(technology, alpha2);
221 static void free_rfkill(gpointer data)
223 struct connman_rfkill *rfkill = data;
228 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
232 for (list = technology_list; list; list = list->next) {
233 struct connman_technology *technology = list->data;
235 if (technology->path == NULL)
238 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
243 static void technologies_changed(void)
245 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
246 CONNMAN_MANAGER_INTERFACE, "Technologies",
247 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
250 static const char *state2string(enum connman_technology_state state)
253 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
255 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
257 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
259 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
261 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
268 static void state_changed(struct connman_technology *technology)
272 str = state2string(technology->state);
276 connman_dbus_property_changed_basic(technology->path,
277 CONNMAN_TECHNOLOGY_INTERFACE, "State",
278 DBUS_TYPE_STRING, &str);
281 static const char *get_name(enum connman_service_type type)
284 case CONNMAN_SERVICE_TYPE_UNKNOWN:
285 case CONNMAN_SERVICE_TYPE_SYSTEM:
286 case CONNMAN_SERVICE_TYPE_GPS:
287 case CONNMAN_SERVICE_TYPE_VPN:
288 case CONNMAN_SERVICE_TYPE_GADGET:
290 case CONNMAN_SERVICE_TYPE_ETHERNET:
292 case CONNMAN_SERVICE_TYPE_WIFI:
294 case CONNMAN_SERVICE_TYPE_WIMAX:
296 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
298 case CONNMAN_SERVICE_TYPE_CELLULAR:
305 static DBusMessage *get_properties(DBusConnection *conn,
306 DBusMessage *message, void *user_data)
308 struct connman_technology *technology = user_data;
310 DBusMessageIter array, dict;
313 reply = dbus_message_new_method_return(message);
317 dbus_message_iter_init_append(reply, &array);
319 connman_dbus_dict_open(&array, &dict);
321 str = state2string(technology->state);
323 connman_dbus_dict_append_basic(&dict, "State",
324 DBUS_TYPE_STRING, &str);
326 str = get_name(technology->type);
328 connman_dbus_dict_append_basic(&dict, "Name",
329 DBUS_TYPE_STRING, &str);
331 str = __connman_service_type2string(technology->type);
333 connman_dbus_dict_append_basic(&dict, "Type",
334 DBUS_TYPE_STRING, &str);
336 connman_dbus_dict_append_basic(&dict, "Tethering",
338 &technology->tethering);
340 if (technology->tethering_ident != NULL)
341 connman_dbus_dict_append_basic(&dict, "TetheringIdentifier",
343 &technology->tethering_ident);
345 if (technology->tethering_passphrase != NULL)
346 connman_dbus_dict_append_basic(&dict, "TetheringPassphrase",
348 &technology->tethering_passphrase);
350 connman_dbus_dict_close(&array, &dict);
355 static DBusMessage *set_property(DBusConnection *conn,
356 DBusMessage *msg, void *data)
358 struct connman_technology *technology = data;
359 DBusMessageIter iter, value;
363 DBG("conn %p", conn);
365 if (dbus_message_iter_init(msg, &iter) == FALSE)
366 return __connman_error_invalid_arguments(msg);
368 dbus_message_iter_get_basic(&iter, &name);
369 dbus_message_iter_next(&iter);
370 dbus_message_iter_recurse(&iter, &value);
372 type = dbus_message_iter_get_arg_type(&value);
374 DBG("property %s", name);
376 if (g_str_equal(name, "Tethering") == TRUE) {
378 connman_bool_t tethering;
381 if (type != DBUS_TYPE_BOOLEAN)
382 return __connman_error_invalid_arguments(msg);
384 dbus_message_iter_get_basic(&value, &tethering);
386 if (technology->tethering == tethering)
387 return __connman_error_in_progress(msg);
389 bridge = __connman_tethering_get_bridge();
391 return __connman_error_not_supported(msg);
393 err = set_tethering(technology, bridge, tethering);
395 return __connman_error_failed(msg, -err);
397 } else if (g_str_equal(name, "TetheringIdentifier") == TRUE) {
400 dbus_message_iter_get_basic(&value, &str);
402 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
403 return __connman_error_not_supported(msg);
405 technology->tethering_ident = g_strdup(str);
406 } else if (g_str_equal(name, "TetheringPassphrase") == TRUE) {
409 dbus_message_iter_get_basic(&value, &str);
411 if (technology->type != CONNMAN_SERVICE_TYPE_WIFI)
412 return __connman_error_not_supported(msg);
415 return __connman_error_invalid_arguments(msg);
417 technology->tethering_passphrase = g_strdup(str);
419 return __connman_error_invalid_property(msg);
421 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
424 static GDBusMethodTable technology_methods[] = {
425 { "GetProperties", "", "a{sv}", get_properties },
426 { "SetProperty", "sv", "", set_property },
430 static GDBusSignalTable technology_signals[] = {
431 { "PropertyChanged", "sv" },
435 static struct connman_technology *technology_find(enum connman_service_type type)
439 DBG("type %d", type);
441 for (list = technology_list; list; list = list->next) {
442 struct connman_technology *technology = list->data;
444 if (technology->type == type)
451 static struct connman_technology *technology_get(enum connman_service_type type)
453 struct connman_technology *technology;
457 DBG("type %d", type);
459 technology = technology_find(type);
460 if (technology != NULL) {
461 g_atomic_int_inc(&technology->refcount);
465 str = __connman_service_type2string(type);
469 technology = g_try_new0(struct connman_technology, 1);
470 if (technology == NULL)
473 technology->refcount = 1;
475 technology->type = type;
476 technology->path = g_strdup_printf("%s/technology/%s",
479 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
481 technology->device_list = NULL;
483 technology->pending_reply = 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 int __connman_technology_add_device(struct connman_device *device)
618 struct connman_technology *technology;
619 enum connman_service_type type;
621 DBG("device %p", device);
623 type = __connman_device_get_service_type(device);
624 __connman_notifier_register(type);
626 technology = technology_get(type);
627 if (technology == NULL)
630 if (g_atomic_int_get(&technology->blocked))
633 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
635 state_changed(technology);
639 technology->device_list = g_slist_append(technology->device_list,
645 int __connman_technology_remove_device(struct connman_device *device)
647 struct connman_technology *technology;
648 enum connman_service_type type;
650 DBG("device %p", device);
652 type = __connman_device_get_service_type(device);
653 __connman_notifier_unregister(type);
655 technology = technology_find(type);
656 if (technology == NULL)
659 technology->device_list = g_slist_remove(technology->device_list,
661 if (technology->device_list == NULL) {
662 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
663 state_changed(technology);
669 static gboolean technology_pending_reply(gpointer user_data)
671 struct connman_technology *technology = user_data;
674 /* Power request timedout, send ETIMEDOUT. */
675 if (technology->pending_reply != NULL) {
676 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
678 g_dbus_send_message(connection, reply);
680 dbus_message_unref(technology->pending_reply);
681 technology->pending_reply = NULL;
682 technology->pending_timeout = 0;
688 int __connman_technology_enabled(enum connman_service_type type)
690 struct connman_technology *technology;
692 technology = technology_find(type);
693 if (technology == NULL)
696 if (g_atomic_int_get(&technology->blocked))
699 __connman_notifier_enable(type);
701 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
702 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
703 state_changed(technology);
706 if (__connman_profile_get_offlinemode() == TRUE) {
707 __connman_profile_set_offlinemode(FALSE, FALSE);
708 __connman_profile_save_default();
711 if (technology->pending_reply != NULL) {
712 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
713 dbus_message_unref(technology->pending_reply);
714 technology->pending_reply = NULL;
715 technology->pending_timeout = 0;
721 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
723 struct connman_technology *technology;
729 DBG("type %d enable", type);
731 technology = technology_find(type);
732 if (technology == NULL) {
737 if (technology->pending_reply != NULL) {
743 technology->pending_reply = dbus_message_ref(msg);
745 for (list = technology->device_list; list; list = list->next) {
746 struct connman_device *device = list->data;
748 err = __connman_device_enable_persistent(device);
750 * err = 0 : Device was enabled right away.
751 * If atleast one device gets enabled, we consider
752 * the technology to be enabled.
763 if (err == -EINPROGRESS)
764 technology->pending_timeout = g_timeout_add_seconds(10,
765 technology_pending_reply, technology);
767 reply = __connman_error_failed(msg, -err);
769 g_dbus_send_message(connection, reply);
776 int __connman_technology_disabled(enum connman_service_type type)
778 struct connman_technology *technology;
781 technology = technology_find(type);
782 if (technology == NULL)
785 if (technology->pending_reply != NULL) {
786 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
787 dbus_message_unref(technology->pending_reply);
788 technology->pending_reply = NULL;
791 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
792 __connman_notifier_disable(type);
794 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
795 state_changed(technology);
798 for (list = technology->device_list; list; list = list->next) {
799 struct connman_device *device = list->data;
801 if (__connman_device_get_blocked(device) == FALSE)
805 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
806 state_changed(technology);
811 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
813 struct connman_technology *technology;
819 DBG("type %d disable", type);
821 technology = technology_find(type);
822 if (technology == NULL) {
827 if (technology->pending_reply != NULL) {
833 technology->pending_reply = dbus_message_ref(msg);
835 for (list = technology->device_list; list; list = list->next) {
836 struct connman_device *device = list->data;
838 err = __connman_device_disable_persistent(device);
848 if (err == -EINPROGRESS)
849 technology->pending_timeout = g_timeout_add_seconds(10,
850 technology_pending_reply, technology);
852 reply = __connman_error_failed(msg, -err);
854 g_dbus_send_message(connection, reply);
861 static void technology_blocked(struct connman_technology *technology,
862 connman_bool_t blocked)
866 for (list = technology->device_list; list; list = list->next) {
867 struct connman_device *device = list->data;
869 __connman_device_set_blocked(device, blocked);
873 int __connman_technology_add_rfkill(unsigned int index,
874 enum connman_service_type type,
875 connman_bool_t softblock,
876 connman_bool_t hardblock)
878 struct connman_technology *technology;
879 struct connman_rfkill *rfkill;
880 connman_bool_t blocked;
882 DBG("index %u type %d soft %u hard %u", index, type,
883 softblock, hardblock);
885 technology = technology_get(type);
886 if (technology == NULL)
889 rfkill = g_try_new0(struct connman_rfkill, 1);
893 rfkill->index = index;
895 rfkill->softblock = softblock;
896 rfkill->hardblock = hardblock;
898 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
900 blocked = (softblock || hardblock) ? TRUE : FALSE;
901 if (blocked == FALSE)
904 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
905 technology_blocked(technology, TRUE);
907 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
908 state_changed(technology);
914 int __connman_technology_update_rfkill(unsigned int index,
915 enum connman_service_type type,
916 connman_bool_t softblock,
917 connman_bool_t hardblock)
919 struct connman_technology *technology;
920 struct connman_rfkill *rfkill;
921 connman_bool_t blocked, old_blocked;
923 DBG("index %u soft %u hard %u", index, softblock, hardblock);
925 technology = technology_find(type);
926 if (technology == NULL)
929 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
933 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
934 blocked = (softblock || hardblock) ? TRUE : FALSE;
936 rfkill->softblock = softblock;
937 rfkill->hardblock = hardblock;
939 if (blocked == old_blocked)
946 g_atomic_int_exchange_and_add(&technology->blocked, 1);
947 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
950 technology_blocked(technology, blocked);
951 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
952 state_changed(technology);
954 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
957 technology_blocked(technology, blocked);
958 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
959 state_changed(technology);
965 int __connman_technology_remove_rfkill(unsigned int index,
966 enum connman_service_type type)
968 struct connman_technology *technology;
969 struct connman_rfkill *rfkill;
970 connman_bool_t blocked;
972 DBG("index %u", index);
974 technology = technology_find(type);
975 if (technology == NULL)
978 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
982 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
984 g_hash_table_remove(technology->rfkill_list, &index);
987 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
988 technology_blocked(technology, FALSE);
989 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
990 state_changed(technology);
996 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
998 struct connman_technology *technology;
1000 technology = technology_find(type);
1001 if (technology == NULL)
1004 if (g_atomic_int_get(&technology->blocked))
1010 int __connman_technology_init(void)
1014 connection = connman_dbus_get_connection();
1019 void __connman_technology_cleanup(void)
1023 dbus_connection_unref(connection);