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 *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;
71 DBusMessage *pending_reply;
72 guint pending_timeout;
75 static GSList *driver_list = NULL;
77 static gint compare_priority(gconstpointer a, gconstpointer b)
79 const struct connman_technology_driver *driver1 = a;
80 const struct connman_technology_driver *driver2 = b;
82 return driver2->priority - driver1->priority;
86 * connman_technology_driver_register:
87 * @driver: Technology driver definition
89 * Register a new technology driver
91 * Returns: %0 on success
93 int connman_technology_driver_register(struct connman_technology_driver *driver)
96 struct connman_technology *technology;
98 DBG("driver %p name %s", driver, driver->name);
100 driver_list = g_slist_insert_sorted(driver_list, driver,
103 for (list = technology_list; list; list = list->next) {
104 technology = list->data;
106 if (technology->driver != NULL)
109 if (technology->type == driver->type)
110 technology->driver = driver;
117 * connman_technology_driver_unregister:
118 * @driver: Technology driver definition
120 * Remove a previously registered technology driver
122 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
125 struct connman_technology *technology;
127 DBG("driver %p name %s", driver, driver->name);
129 for (list = technology_list; list; list = list->next) {
130 technology = list->data;
132 if (technology->driver == NULL)
135 if (technology->type == driver->type) {
136 technology->driver->remove(technology);
137 technology->driver = NULL;
141 driver_list = g_slist_remove(driver_list, driver);
144 static void tethering_changed(struct connman_technology *technology)
146 connman_bool_t tethering = technology->tethering;
148 connman_dbus_property_changed_basic(technology->path,
149 CONNMAN_TECHNOLOGY_INTERFACE, "Tethering",
150 DBUS_TYPE_BOOLEAN, &tethering);
153 void connman_technology_tethering_notify(struct connman_technology *technology,
154 connman_bool_t enabled)
156 DBG("technology %p enabled %u", technology, enabled);
158 if (technology->tethering == enabled)
161 technology->tethering = enabled;
163 tethering_changed(technology);
166 __connman_tethering_set_enabled();
168 __connman_tethering_set_disabled();
171 static int set_tethering(struct connman_technology *technology,
172 const char *bridge, connman_bool_t enabled)
174 const char *ident, *passphrase;
176 ident = technology->tethering_ident;
177 passphrase = technology->tethering_passphrase;
179 if (technology->driver == NULL ||
180 technology->driver->set_tethering == NULL)
183 if (technology->type == CONNMAN_SERVICE_TYPE_WIFI &&
184 (ident == NULL || passphrase == NULL))
187 return technology->driver->set_tethering(technology, ident, passphrase,
191 void connman_technology_regdom_notify(struct connman_technology *technology,
197 connman_error("Failed to set regulatory domain");
199 DBG("Regulatory domain set to %s", alpha2);
201 g_free(technology->regdom);
202 technology->regdom = g_strdup(alpha2);
205 int connman_technology_set_regdom(const char *alpha2)
209 for (list = technology_list; list; list = list->next) {
210 struct connman_technology *technology = list->data;
212 if (technology->driver == NULL)
215 if (technology->driver->set_regdom)
216 technology->driver->set_regdom(technology, alpha2);
222 static void free_rfkill(gpointer data)
224 struct connman_rfkill *rfkill = data;
229 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
233 for (list = technology_list; list; list = list->next) {
234 struct connman_technology *technology = list->data;
236 if (technology->path == NULL)
239 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
244 static void technologies_changed(void)
246 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
247 CONNMAN_MANAGER_INTERFACE, "Technologies",
248 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
251 static const char *state2string(enum connman_technology_state state)
254 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
256 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
258 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
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->pending_reply = NULL;
485 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
487 if (g_dbus_register_interface(connection, technology->path,
488 CONNMAN_TECHNOLOGY_INTERFACE,
489 technology_methods, technology_signals,
490 NULL, technology, NULL) == FALSE) {
491 connman_error("Failed to register %s", technology->path);
496 technology_list = g_slist_append(technology_list, technology);
498 technologies_changed();
500 if (technology->driver != NULL)
503 for (list = driver_list; list; list = list->next) {
504 struct connman_technology_driver *driver = list->data;
506 DBG("driver %p name %s", driver, driver->name);
508 if (driver->type != technology->type)
511 if (driver->probe(technology) == 0) {
512 technology->driver = driver;
518 DBG("technology %p", technology);
523 static void technology_put(struct connman_technology *technology)
525 DBG("technology %p", technology);
527 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
530 if (technology->driver) {
531 technology->driver->remove(technology);
532 technology->driver = NULL;
535 technology_list = g_slist_remove(technology_list, technology);
537 technologies_changed();
539 g_dbus_unregister_interface(connection, technology->path,
540 CONNMAN_TECHNOLOGY_INTERFACE);
542 g_slist_free(technology->device_list);
543 g_hash_table_destroy(technology->rfkill_list);
545 g_free(technology->path);
546 g_free(technology->regdom);
550 void __connman_technology_add_interface(enum connman_service_type type,
551 int index, const char *name, const char *ident)
553 struct connman_technology *technology;
556 case CONNMAN_SERVICE_TYPE_UNKNOWN:
557 case CONNMAN_SERVICE_TYPE_SYSTEM:
559 case CONNMAN_SERVICE_TYPE_ETHERNET:
560 case CONNMAN_SERVICE_TYPE_WIFI:
561 case CONNMAN_SERVICE_TYPE_WIMAX:
562 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
563 case CONNMAN_SERVICE_TYPE_CELLULAR:
564 case CONNMAN_SERVICE_TYPE_GPS:
565 case CONNMAN_SERVICE_TYPE_VPN:
566 case CONNMAN_SERVICE_TYPE_GADGET:
570 connman_info("Create interface %s [ %s ]", name,
571 __connman_service_type2string(type));
573 technology = technology_get(type);
575 if (technology == NULL || technology->driver == NULL
576 || technology->driver->add_interface == NULL)
579 technology->driver->add_interface(technology,
583 void __connman_technology_remove_interface(enum connman_service_type type,
584 int index, const char *name, const char *ident)
586 struct connman_technology *technology;
589 case CONNMAN_SERVICE_TYPE_UNKNOWN:
590 case CONNMAN_SERVICE_TYPE_SYSTEM:
592 case CONNMAN_SERVICE_TYPE_ETHERNET:
593 case CONNMAN_SERVICE_TYPE_WIFI:
594 case CONNMAN_SERVICE_TYPE_WIMAX:
595 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
596 case CONNMAN_SERVICE_TYPE_CELLULAR:
597 case CONNMAN_SERVICE_TYPE_GPS:
598 case CONNMAN_SERVICE_TYPE_VPN:
599 case CONNMAN_SERVICE_TYPE_GADGET:
603 connman_info("Remove interface %s [ %s ]", name,
604 __connman_service_type2string(type));
606 technology = technology_find(type);
608 if (technology == NULL || technology->driver == NULL)
611 if (technology->driver->remove_interface)
612 technology->driver->remove_interface(technology, index);
614 technology_put(technology);
617 static void unregister_technology(gpointer data)
619 struct connman_technology *technology = data;
621 technology_put(technology);
624 int __connman_technology_add_device(struct connman_device *device)
626 struct connman_technology *technology;
627 enum connman_service_type type;
629 DBG("device %p", device);
631 type = __connman_device_get_service_type(device);
632 __connman_notifier_register(type);
634 technology = technology_get(type);
635 if (technology == NULL)
638 g_hash_table_insert(device_table, device, technology);
640 if (g_atomic_int_get(&technology->blocked))
643 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
645 state_changed(technology);
649 technology->device_list = g_slist_append(technology->device_list,
655 int __connman_technology_remove_device(struct connman_device *device)
657 struct connman_technology *technology;
658 enum connman_service_type type;
660 DBG("device %p", device);
662 type = __connman_device_get_service_type(device);
663 __connman_notifier_unregister(type);
665 technology = g_hash_table_lookup(device_table, device);
666 if (technology == NULL)
669 technology->device_list = g_slist_remove(technology->device_list,
671 if (technology->device_list == NULL) {
672 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
673 state_changed(technology);
676 g_hash_table_remove(device_table, device);
681 static gboolean technology_pending_reply(gpointer user_data)
683 struct connman_technology *technology = user_data;
686 /* Power request timedout, send ETIMEDOUT. */
687 if (technology->pending_reply != NULL) {
688 reply = __connman_error_failed(technology->pending_reply, ETIMEDOUT);
690 g_dbus_send_message(connection, reply);
692 dbus_message_unref(technology->pending_reply);
693 technology->pending_reply = NULL;
694 technology->pending_timeout = 0;
700 int __connman_technology_enabled(enum connman_service_type type)
702 struct connman_technology *technology;
704 technology = technology_find(type);
705 if (technology == NULL)
708 if (g_atomic_int_get(&technology->blocked))
711 __connman_notifier_enable(type);
713 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
714 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
715 state_changed(technology);
718 if (__connman_profile_get_offlinemode() == TRUE) {
719 __connman_profile_set_offlinemode(FALSE, FALSE);
720 __connman_profile_save_default();
723 if (technology->pending_reply != NULL) {
724 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
725 dbus_message_unref(technology->pending_reply);
726 technology->pending_reply = NULL;
727 technology->pending_timeout = 0;
733 int __connman_technology_enable(enum connman_service_type type, DBusMessage *msg)
735 struct connman_technology *technology;
741 DBG("type %d enable", type);
743 technology = technology_find(type);
744 if (technology == NULL) {
749 if (technology->pending_reply != NULL) {
755 technology->pending_reply = dbus_message_ref(msg);
757 for (list = technology->device_list; list; list = list->next) {
758 struct connman_device *device = list->data;
760 err = __connman_device_enable_persistent(device);
762 * err = 0 : Device was enabled right away.
763 * If atleast one device gets enabled, we consider
764 * the technology to be enabled.
775 if (err == -EINPROGRESS)
776 technology->pending_timeout = g_timeout_add_seconds(10,
777 technology_pending_reply, technology);
779 reply = __connman_error_failed(msg, -err);
781 g_dbus_send_message(connection, reply);
788 int __connman_technology_disabled(enum connman_service_type type)
790 struct connman_technology *technology;
793 technology = technology_find(type);
794 if (technology == NULL)
797 if (technology->pending_reply != NULL) {
798 g_dbus_send_reply(connection, technology->pending_reply, DBUS_TYPE_INVALID);
799 dbus_message_unref(technology->pending_reply);
800 technology->pending_reply = NULL;
803 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
804 __connman_notifier_disable(type);
806 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
807 state_changed(technology);
810 for (list = technology->device_list; list; list = list->next) {
811 struct connman_device *device = list->data;
813 if (__connman_device_get_blocked(device) == FALSE)
817 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
818 state_changed(technology);
823 int __connman_technology_disable(enum connman_service_type type, DBusMessage *msg)
825 struct connman_technology *technology;
831 DBG("type %d disable", type);
833 technology = technology_find(type);
834 if (technology == NULL) {
839 if (technology->pending_reply != NULL) {
845 technology->pending_reply = dbus_message_ref(msg);
847 for (list = technology->device_list; list; list = list->next) {
848 struct connman_device *device = list->data;
850 err = __connman_device_disable_persistent(device);
860 if (err == -EINPROGRESS)
861 technology->pending_timeout = g_timeout_add_seconds(10,
862 technology_pending_reply, technology);
864 reply = __connman_error_failed(msg, -err);
866 g_dbus_send_message(connection, reply);
873 static void technology_blocked(struct connman_technology *technology,
874 connman_bool_t blocked)
878 for (list = technology->device_list; list; list = list->next) {
879 struct connman_device *device = list->data;
881 __connman_device_set_blocked(device, blocked);
885 int __connman_technology_add_rfkill(unsigned int index,
886 enum connman_service_type type,
887 connman_bool_t softblock,
888 connman_bool_t hardblock)
890 struct connman_technology *technology;
891 struct connman_rfkill *rfkill;
892 connman_bool_t blocked;
894 DBG("index %u type %d soft %u hard %u", index, type,
895 softblock, hardblock);
897 technology = technology_get(type);
898 if (technology == NULL)
901 rfkill = g_try_new0(struct connman_rfkill, 1);
905 rfkill->index = index;
907 rfkill->softblock = softblock;
908 rfkill->hardblock = hardblock;
910 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
912 blocked = (softblock || hardblock) ? TRUE : FALSE;
913 if (blocked == FALSE)
916 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
917 technology_blocked(technology, TRUE);
919 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
920 state_changed(technology);
926 int __connman_technology_update_rfkill(unsigned int index,
927 enum connman_service_type type,
928 connman_bool_t softblock,
929 connman_bool_t hardblock)
931 struct connman_technology *technology;
932 struct connman_rfkill *rfkill;
933 connman_bool_t blocked, old_blocked;
935 DBG("index %u soft %u hard %u", index, softblock, hardblock);
937 technology = technology_find(type);
938 if (technology == NULL)
941 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
945 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
946 blocked = (softblock || hardblock) ? TRUE : FALSE;
948 rfkill->softblock = softblock;
949 rfkill->hardblock = hardblock;
951 if (blocked == old_blocked)
958 g_atomic_int_exchange_and_add(&technology->blocked, 1);
959 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
962 technology_blocked(technology, blocked);
963 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
964 state_changed(technology);
966 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
969 technology_blocked(technology, blocked);
970 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
971 state_changed(technology);
977 int __connman_technology_remove_rfkill(unsigned int index,
978 enum connman_service_type type)
980 struct connman_technology *technology;
981 struct connman_rfkill *rfkill;
982 connman_bool_t blocked;
984 DBG("index %u", index);
986 technology = technology_find(type);
987 if (technology == NULL)
990 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
994 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
996 g_hash_table_remove(technology->rfkill_list, &index);
999 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
1000 technology_blocked(technology, FALSE);
1001 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
1002 state_changed(technology);
1008 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
1010 struct connman_technology *technology;
1012 technology = technology_find(type);
1013 if (technology == NULL)
1016 if (g_atomic_int_get(&technology->blocked))
1022 int __connman_technology_init(void)
1026 connection = connman_dbus_get_connection();
1028 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1029 NULL, unregister_technology);
1034 void __connman_technology_cleanup(void)
1038 g_hash_table_destroy(device_table);
1040 dbus_connection_unref(connection);