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
30 static DBusMessage *get_properties(DBusConnection *conn,
31 DBusMessage *msg, void *data)
34 DBusMessageIter array, dict;
35 connman_bool_t offlinemode, tethering;
40 reply = dbus_message_new_method_return(msg);
44 dbus_message_iter_init_append(reply, &array);
46 connman_dbus_dict_open(&array, &dict);
48 str = __connman_profile_active_path();
50 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
51 DBUS_TYPE_OBJECT_PATH, &str);
53 connman_dbus_dict_append_array(&dict, "Profiles",
54 DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
55 connman_dbus_dict_append_array(&dict, "Services",
56 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
57 connman_dbus_dict_append_array(&dict, "Technologies",
58 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
60 str = __connman_notifier_get_state();
61 connman_dbus_dict_append_basic(&dict, "State",
62 DBUS_TYPE_STRING, &str);
64 offlinemode = __connman_profile_get_offlinemode();
65 connman_dbus_dict_append_basic(&dict, "OfflineMode",
66 DBUS_TYPE_BOOLEAN, &offlinemode);
68 tethering = __connman_tethering_get_status();
69 connman_dbus_dict_append_basic(&dict, "Tethering",
70 DBUS_TYPE_BOOLEAN, &tethering);
72 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
73 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
74 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
75 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
76 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
77 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
79 str = __connman_service_default();
81 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
82 DBUS_TYPE_STRING, &str);
84 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
85 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
86 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
87 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
89 connman_dbus_dict_close(&array, &dict);
94 static DBusMessage *set_property(DBusConnection *conn,
95 DBusMessage *msg, void *data)
97 DBusMessageIter iter, value;
101 DBG("conn %p", conn);
103 if (dbus_message_iter_init(msg, &iter) == FALSE)
104 return __connman_error_invalid_arguments(msg);
106 dbus_message_iter_get_basic(&iter, &name);
107 dbus_message_iter_next(&iter);
108 dbus_message_iter_recurse(&iter, &value);
110 type = dbus_message_iter_get_arg_type(&value);
112 if (g_str_equal(name, "OfflineMode") == TRUE) {
113 connman_bool_t offlinemode;
115 if (type != DBUS_TYPE_BOOLEAN)
116 return __connman_error_invalid_arguments(msg);
118 dbus_message_iter_get_basic(&value, &offlinemode);
120 __connman_profile_set_offlinemode(offlinemode, TRUE);
122 __connman_profile_save_default();
123 } else if (g_str_equal(name, "Tethering") == TRUE) {
124 connman_bool_t tethering;
126 if (type != DBUS_TYPE_BOOLEAN)
127 return __connman_error_invalid_arguments(msg);
129 dbus_message_iter_get_basic(&value, &tethering);
131 if (__connman_tethering_set_status(tethering) < 0)
132 return __connman_error_invalid_arguments(msg);
134 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
135 CONNMAN_MANAGER_INTERFACE, "Tethering",
136 DBUS_TYPE_BOOLEAN, &tethering);
137 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
140 dbus_message_iter_get_basic(&value, &str);
142 return __connman_error_not_supported(msg);
144 return __connman_error_invalid_property(msg);
146 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
149 static DBusMessage *get_state(DBusConnection *conn,
150 DBusMessage *msg, void *data)
154 DBG("conn %p", conn);
156 str = __connman_notifier_get_state();
158 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
162 static DBusMessage *create_profile(DBusConnection *conn,
163 DBusMessage *msg, void *data)
165 const char *name, *path;
168 DBG("conn %p", conn);
170 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
173 err = __connman_profile_create(name, &path);
175 return __connman_error_failed(msg, -err);
177 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
181 static DBusMessage *remove_profile(DBusConnection *conn,
182 DBusMessage *msg, void *data)
187 DBG("conn %p", conn);
189 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
192 err = __connman_profile_remove(path);
194 return __connman_error_failed(msg, -err);
196 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
199 static DBusMessage *remove_provider(DBusConnection *conn,
200 DBusMessage *msg, void *data)
205 DBG("conn %p", conn);
207 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
210 err = __connman_provider_remove(path);
212 return __connman_error_failed(msg, -err);
214 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
217 static DBusMessage *request_scan(DBusConnection *conn,
218 DBusMessage *msg, void *data)
220 enum connman_service_type type;
224 DBG("conn %p", conn);
226 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
229 if (g_strcmp0(str, "") == 0)
230 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
231 else if (g_strcmp0(str, "wifi") == 0)
232 type = CONNMAN_SERVICE_TYPE_WIFI;
233 else if (g_strcmp0(str, "wimax") == 0)
234 type = CONNMAN_SERVICE_TYPE_WIMAX;
236 return __connman_error_invalid_arguments(msg);
238 err = __connman_element_request_scan(type);
240 if (err == -EINPROGRESS) {
241 connman_error("Invalid return code from scan");
245 return __connman_error_failed(msg, -err);
248 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
251 static DBusConnection *connection = NULL;
253 static enum connman_service_type technology_type;
254 static connman_bool_t technology_enabled;
255 static DBusMessage *technology_pending = NULL;
256 static guint technology_timeout = 0;
258 static void technology_reply(int error)
262 if (technology_timeout > 0) {
263 g_source_remove(technology_timeout);
264 technology_timeout = 0;
267 if (technology_pending != NULL) {
271 reply = __connman_error_failed(technology_pending,
274 g_dbus_send_message(connection, reply);
276 g_dbus_send_reply(connection, technology_pending,
279 dbus_message_unref(technology_pending);
280 technology_pending = NULL;
283 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
286 static gboolean technology_abort(gpointer user_data)
290 technology_timeout = 0;
292 technology_reply(ETIMEDOUT);
297 static void technology_notify(enum connman_service_type type,
298 connman_bool_t enabled)
300 DBG("type %d enabled %d", type, enabled);
302 if (type == technology_type && enabled == technology_enabled)
306 static struct connman_notifier technology_notifier = {
308 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
309 .service_enabled= technology_notify,
312 static DBusMessage *enable_technology(DBusConnection *conn,
313 DBusMessage *msg, void *data)
315 enum connman_service_type type;
319 DBG("conn %p", conn);
321 if (technology_pending != NULL)
322 return __connman_error_in_progress(msg);
324 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
327 if (g_strcmp0(str, "ethernet") == 0)
328 type = CONNMAN_SERVICE_TYPE_ETHERNET;
329 else if (g_strcmp0(str, "wifi") == 0)
330 type = CONNMAN_SERVICE_TYPE_WIFI;
331 else if (g_strcmp0(str, "wimax") == 0)
332 type = CONNMAN_SERVICE_TYPE_WIMAX;
333 else if (g_strcmp0(str, "bluetooth") == 0)
334 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
335 else if (g_strcmp0(str, "cellular") == 0)
336 type = CONNMAN_SERVICE_TYPE_CELLULAR;
338 return __connman_error_invalid_arguments(msg);
340 if (__connman_notifier_is_registered(type) == FALSE)
341 return __connman_error_not_registered(msg);
343 if (__connman_notifier_is_enabled(type) == TRUE)
344 return __connman_error_already_enabled(msg);
346 technology_type = type;
347 technology_enabled = TRUE;
348 technology_pending = dbus_message_ref(msg);
350 err = __connman_element_enable_technology(type);
351 if (err < 0 && err != -EINPROGRESS)
352 technology_reply(-err);
354 technology_timeout = g_timeout_add_seconds(15,
355 technology_abort, NULL);
360 static DBusMessage *disable_technology(DBusConnection *conn,
361 DBusMessage *msg, void *data)
363 enum connman_service_type type;
367 DBG("conn %p", conn);
369 if (technology_pending != NULL)
370 return __connman_error_in_progress(msg);
372 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
375 if (g_strcmp0(str, "ethernet") == 0)
376 type = CONNMAN_SERVICE_TYPE_ETHERNET;
377 else if (g_strcmp0(str, "wifi") == 0)
378 type = CONNMAN_SERVICE_TYPE_WIFI;
379 else if (g_strcmp0(str, "wimax") == 0)
380 type = CONNMAN_SERVICE_TYPE_WIMAX;
381 else if (g_strcmp0(str, "bluetooth") == 0)
382 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
383 else if (g_strcmp0(str, "cellular") == 0)
384 type = CONNMAN_SERVICE_TYPE_CELLULAR;
386 return __connman_error_invalid_arguments(msg);
388 if (__connman_notifier_is_registered(type) == FALSE)
389 return __connman_error_not_registered(msg);
391 if (__connman_notifier_is_enabled(type) == FALSE)
392 return __connman_error_already_disabled(msg);
394 technology_type = type;
395 technology_enabled = FALSE;
396 technology_pending = dbus_message_ref(msg);
398 err = __connman_element_disable_technology(type);
399 if (err < 0 && err != -EINPROGRESS)
400 technology_reply(-err);
402 technology_timeout = g_timeout_add_seconds(10,
403 technology_abort, NULL);
408 static DBusMessage *get_services(DBusConnection *conn,
409 DBusMessage *msg, void *data)
412 DBusMessageIter iter, array;
414 reply = dbus_message_new_method_return(msg);
418 dbus_message_iter_init_append(reply, &iter);
420 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
421 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
422 DBUS_TYPE_OBJECT_PATH_AS_STRING
423 DBUS_TYPE_ARRAY_AS_STRING
424 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
425 DBUS_TYPE_STRING_AS_STRING
426 DBUS_TYPE_VARIANT_AS_STRING
427 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
428 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
430 __connman_service_list_struct(&array);
432 dbus_message_iter_close_container(&iter, &array);
437 static DBusMessage *lookup_service(DBusConnection *conn,
438 DBusMessage *msg, void *data)
440 const char *pattern, *path;
443 DBG("conn %p", conn);
445 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
448 err = __connman_service_lookup(pattern, &path);
450 return __connman_error_failed(msg, -err);
452 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
456 static DBusMessage *connect_service(DBusConnection *conn,
457 DBusMessage *msg, void *data)
461 DBG("conn %p", conn);
463 err = __connman_service_create_and_connect(msg);
465 if (err == -EINPROGRESS) {
466 connman_error("Invalid return code from connect");
470 return __connman_error_failed(msg, -err);
477 static DBusMessage *connect_provider(DBusConnection *conn,
478 DBusMessage *msg, void *data)
482 DBG("conn %p", conn);
484 err = __connman_provider_create_and_connect(msg);
486 if (err == -EINPROGRESS) {
487 connman_error("Invalid return code from connect");
491 return __connman_error_failed(msg, -err);
497 static DBusMessage *register_agent(DBusConnection *conn,
498 DBusMessage *msg, void *data)
500 const char *sender, *path;
503 DBG("conn %p", conn);
505 sender = dbus_message_get_sender(msg);
507 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
510 err = __connman_agent_register(sender, path);
512 return __connman_error_failed(msg, -err);
514 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
517 static DBusMessage *unregister_agent(DBusConnection *conn,
518 DBusMessage *msg, void *data)
520 const char *sender, *path;
523 DBG("conn %p", conn);
525 sender = dbus_message_get_sender(msg);
527 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
530 err = __connman_agent_unregister(sender, path);
532 return __connman_error_failed(msg, -err);
534 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
537 static DBusMessage *register_counter(DBusConnection *conn,
538 DBusMessage *msg, void *data)
540 const char *sender, *path;
541 unsigned int accuracy, period;
544 DBG("conn %p", conn);
546 sender = dbus_message_get_sender(msg);
548 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
549 DBUS_TYPE_UINT32, &accuracy,
550 DBUS_TYPE_UINT32, &period,
553 /* FIXME: add handling of accuracy parameter */
555 err = __connman_counter_register(sender, path, period);
557 return __connman_error_failed(msg, -err);
559 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
562 static DBusMessage *unregister_counter(DBusConnection *conn,
563 DBusMessage *msg, void *data)
565 const char *sender, *path;
568 DBG("conn %p", conn);
570 sender = dbus_message_get_sender(msg);
572 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
575 err = __connman_counter_unregister(sender, path);
577 return __connman_error_failed(msg, -err);
579 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
582 static DBusMessage *request_session(DBusConnection *conn,
583 DBusMessage *msg, void *data)
585 const char *bearer, *sender, *service_path;
586 struct connman_service *service;
588 DBG("conn %p", conn);
590 sender = dbus_message_get_sender(msg);
592 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &bearer,
595 service = __connman_session_request(bearer, sender);
597 return __connman_error_failed(msg, EINVAL);
599 service_path = __connman_service_get_path(service);
601 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &service_path,
605 static DBusMessage *release_session(DBusConnection *conn,
606 DBusMessage *msg, void *data)
611 DBG("conn %p", conn);
613 sender = dbus_message_get_sender(msg);
615 err = __connman_session_release(sender);
617 return __connman_error_failed(msg, -err);
619 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
622 static GDBusMethodTable manager_methods[] = {
623 { "GetProperties", "", "a{sv}", get_properties },
624 { "SetProperty", "sv", "", set_property },
625 { "GetState", "", "s", get_state },
626 { "CreateProfile", "s", "o", create_profile },
627 { "RemoveProfile", "o", "", remove_profile },
628 { "RemoveProvider", "o", "", remove_provider },
629 { "RequestScan", "s", "", request_scan },
630 { "EnableTechnology", "s", "", enable_technology,
631 G_DBUS_METHOD_FLAG_ASYNC },
632 { "DisableTechnology", "s", "", disable_technology,
633 G_DBUS_METHOD_FLAG_ASYNC },
634 { "GetServices", "", "a(oa{sv})", get_services },
635 { "LookupService", "s", "o", lookup_service, },
636 { "ConnectService", "a{sv}", "o", connect_service,
637 G_DBUS_METHOD_FLAG_ASYNC },
638 { "ConnectProvider", "a{sv}", "o", connect_provider,
639 G_DBUS_METHOD_FLAG_ASYNC },
640 { "RegisterAgent", "o", "", register_agent },
641 { "UnregisterAgent", "o", "", unregister_agent },
642 { "RegisterCounter", "ouu", "", register_counter },
643 { "UnregisterCounter", "o", "", unregister_counter },
644 { "RequestSession", "s", "o", request_session },
645 { "ReleaseSession", "s", "", release_session },
649 static GDBusSignalTable manager_signals[] = {
650 { "PropertyChanged", "sv" },
651 { "StateChanged", "s" },
655 static DBusMessage *nm_sleep(DBusConnection *conn,
656 DBusMessage *msg, void *data)
660 DBG("conn %p", conn);
662 reply = dbus_message_new_method_return(msg);
666 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
671 static DBusMessage *nm_wake(DBusConnection *conn,
672 DBusMessage *msg, void *data)
676 DBG("conn %p", conn);
678 reply = dbus_message_new_method_return(msg);
682 dbus_message_append_args(reply, DBUS_TYPE_INVALID);
688 NM_STATE_UNKNOWN = 0,
692 NM_STATE_DISCONNECTED
695 static DBusMessage *nm_state(DBusConnection *conn,
696 DBusMessage *msg, void *data)
701 DBG("conn %p", conn);
703 reply = dbus_message_new_method_return(msg);
707 if (__connman_notifier_count_connected() > 0)
708 state = NM_STATE_CONNECTED;
710 state = NM_STATE_DISCONNECTED;
712 dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
718 static GDBusMethodTable nm_methods[] = {
719 { "sleep", "", "", nm_sleep },
720 { "wake", "", "", nm_wake },
721 { "state", "", "u", nm_state },
725 static gboolean nm_compat = FALSE;
727 int __connman_manager_init(gboolean compat)
731 connection = connman_dbus_get_connection();
732 if (connection == NULL)
735 if (connman_notifier_register(&technology_notifier) < 0)
736 connman_error("Failed to register technology notifier");
738 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
739 CONNMAN_MANAGER_INTERFACE,
741 manager_signals, NULL, NULL, NULL);
743 if (compat == TRUE) {
744 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
745 nm_methods, NULL, NULL, NULL, NULL);
753 void __connman_manager_cleanup(void)
757 connman_notifier_unregister(&technology_notifier);
759 if (connection == NULL)
762 if (nm_compat == TRUE) {
763 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
766 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
767 CONNMAN_MANAGER_INTERFACE);
769 dbus_connection_unref(connection);