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, sessionmode;
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, "Services",
54 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
55 connman_dbus_dict_append_array(&dict, "Technologies",
56 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
58 str = __connman_notifier_get_state();
59 connman_dbus_dict_append_basic(&dict, "State",
60 DBUS_TYPE_STRING, &str);
62 offlinemode = __connman_profile_get_offlinemode();
63 connman_dbus_dict_append_basic(&dict, "OfflineMode",
64 DBUS_TYPE_BOOLEAN, &offlinemode);
66 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
67 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
68 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
69 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
70 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
71 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
73 str = __connman_service_default();
75 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
76 DBUS_TYPE_STRING, &str);
78 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
79 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
80 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
81 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
83 sessionmode = __connman_session_mode();
84 connman_dbus_dict_append_basic(&dict, "SessionMode",
88 connman_dbus_dict_close(&array, &dict);
93 static DBusMessage *set_property(DBusConnection *conn,
94 DBusMessage *msg, void *data)
96 DBusMessageIter iter, value;
100 DBG("conn %p", conn);
102 if (dbus_message_iter_init(msg, &iter) == FALSE)
103 return __connman_error_invalid_arguments(msg);
105 dbus_message_iter_get_basic(&iter, &name);
106 dbus_message_iter_next(&iter);
107 dbus_message_iter_recurse(&iter, &value);
109 type = dbus_message_iter_get_arg_type(&value);
111 if (g_str_equal(name, "OfflineMode") == TRUE) {
112 connman_bool_t offlinemode;
114 if (type != DBUS_TYPE_BOOLEAN)
115 return __connman_error_invalid_arguments(msg);
117 dbus_message_iter_get_basic(&value, &offlinemode);
119 __connman_profile_set_offlinemode(offlinemode, TRUE);
121 __connman_profile_save_default();
122 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
125 dbus_message_iter_get_basic(&value, &str);
127 return __connman_error_not_supported(msg);
128 } else if (g_str_equal(name, "SessionMode") == TRUE) {
129 connman_bool_t sessionmode;
131 if (type != DBUS_TYPE_BOOLEAN)
132 return __connman_error_invalid_arguments(msg);
134 dbus_message_iter_get_basic(&value, &sessionmode);
136 __connman_session_set_mode(sessionmode);
138 return __connman_error_invalid_property(msg);
140 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
143 static DBusMessage *get_state(DBusConnection *conn,
144 DBusMessage *msg, void *data)
148 DBG("conn %p", conn);
150 str = __connman_notifier_get_state();
152 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
156 static DBusMessage *remove_provider(DBusConnection *conn,
157 DBusMessage *msg, void *data)
162 DBG("conn %p", conn);
164 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
167 err = __connman_provider_remove(path);
169 return __connman_error_failed(msg, -err);
171 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
174 static DBusMessage *request_scan(DBusConnection *conn,
175 DBusMessage *msg, void *data)
177 enum connman_service_type type;
181 DBG("conn %p", conn);
183 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
186 if (g_strcmp0(str, "") == 0)
187 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
188 else if (g_strcmp0(str, "wifi") == 0)
189 type = CONNMAN_SERVICE_TYPE_WIFI;
190 else if (g_strcmp0(str, "wimax") == 0)
191 type = CONNMAN_SERVICE_TYPE_WIMAX;
193 return __connman_error_invalid_arguments(msg);
195 err = __connman_element_request_scan(type);
197 if (err == -EINPROGRESS) {
198 connman_error("Invalid return code from scan");
202 return __connman_error_failed(msg, -err);
205 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
208 static DBusConnection *connection = NULL;
210 static enum connman_service_type technology_type;
211 static connman_bool_t technology_enabled;
212 static DBusMessage *technology_pending = NULL;
213 static guint technology_timeout = 0;
215 static void technology_reply(int error)
219 if (technology_timeout > 0) {
220 g_source_remove(technology_timeout);
221 technology_timeout = 0;
224 if (technology_pending != NULL) {
228 reply = __connman_error_failed(technology_pending,
231 g_dbus_send_message(connection, reply);
233 g_dbus_send_reply(connection, technology_pending,
236 dbus_message_unref(technology_pending);
237 technology_pending = NULL;
240 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
243 static gboolean technology_abort(gpointer user_data)
247 technology_timeout = 0;
249 technology_reply(ETIMEDOUT);
254 static void technology_notify(enum connman_service_type type,
255 connman_bool_t enabled)
257 DBG("type %d enabled %d", type, enabled);
259 if (type == technology_type && enabled == technology_enabled)
263 static struct connman_notifier technology_notifier = {
265 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
266 .service_enabled= technology_notify,
269 static DBusMessage *enable_technology(DBusConnection *conn,
270 DBusMessage *msg, void *data)
272 enum connman_service_type type;
276 DBG("conn %p", conn);
278 if (technology_pending != NULL)
279 return __connman_error_in_progress(msg);
281 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
284 if (g_strcmp0(str, "ethernet") == 0)
285 type = CONNMAN_SERVICE_TYPE_ETHERNET;
286 else if (g_strcmp0(str, "wifi") == 0)
287 type = CONNMAN_SERVICE_TYPE_WIFI;
288 else if (g_strcmp0(str, "wimax") == 0)
289 type = CONNMAN_SERVICE_TYPE_WIMAX;
290 else if (g_strcmp0(str, "bluetooth") == 0)
291 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
292 else if (g_strcmp0(str, "cellular") == 0)
293 type = CONNMAN_SERVICE_TYPE_CELLULAR;
295 return __connman_error_invalid_arguments(msg);
297 if (__connman_notifier_is_registered(type) == FALSE)
298 return __connman_error_not_registered(msg);
300 if (__connman_notifier_is_enabled(type) == TRUE)
301 return __connman_error_already_enabled(msg);
303 technology_type = type;
304 technology_enabled = TRUE;
305 technology_pending = dbus_message_ref(msg);
307 err = __connman_element_enable_technology(type);
308 if (err < 0 && err != -EINPROGRESS)
309 technology_reply(-err);
311 technology_timeout = g_timeout_add_seconds(15,
312 technology_abort, NULL);
317 static DBusMessage *disable_technology(DBusConnection *conn,
318 DBusMessage *msg, void *data)
320 enum connman_service_type type;
324 DBG("conn %p", conn);
326 if (technology_pending != NULL)
327 return __connman_error_in_progress(msg);
329 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
332 if (g_strcmp0(str, "ethernet") == 0)
333 type = CONNMAN_SERVICE_TYPE_ETHERNET;
334 else if (g_strcmp0(str, "wifi") == 0)
335 type = CONNMAN_SERVICE_TYPE_WIFI;
336 else if (g_strcmp0(str, "wimax") == 0)
337 type = CONNMAN_SERVICE_TYPE_WIMAX;
338 else if (g_strcmp0(str, "bluetooth") == 0)
339 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
340 else if (g_strcmp0(str, "cellular") == 0)
341 type = CONNMAN_SERVICE_TYPE_CELLULAR;
343 return __connman_error_invalid_arguments(msg);
345 if (__connman_notifier_is_registered(type) == FALSE)
346 return __connman_error_not_registered(msg);
348 if (__connman_notifier_is_enabled(type) == FALSE)
349 return __connman_error_already_disabled(msg);
351 technology_type = type;
352 technology_enabled = FALSE;
353 technology_pending = dbus_message_ref(msg);
355 err = __connman_element_disable_technology(type);
356 if (err < 0 && err != -EINPROGRESS)
357 technology_reply(-err);
359 technology_timeout = g_timeout_add_seconds(10,
360 technology_abort, NULL);
365 static DBusMessage *get_services(DBusConnection *conn,
366 DBusMessage *msg, void *data)
369 DBusMessageIter iter, array;
371 reply = dbus_message_new_method_return(msg);
375 dbus_message_iter_init_append(reply, &iter);
377 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
378 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
379 DBUS_TYPE_OBJECT_PATH_AS_STRING
380 DBUS_TYPE_ARRAY_AS_STRING
381 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
382 DBUS_TYPE_STRING_AS_STRING
383 DBUS_TYPE_VARIANT_AS_STRING
384 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
385 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
387 __connman_service_list_struct(&array);
389 dbus_message_iter_close_container(&iter, &array);
394 static DBusMessage *lookup_service(DBusConnection *conn,
395 DBusMessage *msg, void *data)
397 const char *pattern, *path;
400 DBG("conn %p", conn);
402 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
405 err = __connman_service_lookup(pattern, &path);
407 return __connman_error_failed(msg, -err);
409 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
413 static DBusMessage *connect_service(DBusConnection *conn,
414 DBusMessage *msg, void *data)
418 DBG("conn %p", conn);
420 if (__connman_session_mode() == TRUE) {
421 connman_info("Session mode enabled: "
422 "direct service connect disabled");
424 return __connman_error_failed(msg, -EINVAL);
427 err = __connman_service_create_and_connect(msg);
429 if (err == -EINPROGRESS) {
430 connman_error("Invalid return code from connect");
434 return __connman_error_failed(msg, -err);
441 static DBusMessage *connect_provider(DBusConnection *conn,
442 DBusMessage *msg, void *data)
446 DBG("conn %p", conn);
448 if (__connman_session_mode() == TRUE) {
449 connman_info("Session mode enabled: "
450 "direct provider connect disabled");
452 return __connman_error_failed(msg, -EINVAL);
455 err = __connman_provider_create_and_connect(msg);
457 if (err == -EINPROGRESS) {
458 connman_error("Invalid return code from connect");
462 return __connman_error_failed(msg, -err);
468 static DBusMessage *register_agent(DBusConnection *conn,
469 DBusMessage *msg, void *data)
471 const char *sender, *path;
474 DBG("conn %p", conn);
476 sender = dbus_message_get_sender(msg);
478 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
481 err = __connman_agent_register(sender, path);
483 return __connman_error_failed(msg, -err);
485 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
488 static DBusMessage *unregister_agent(DBusConnection *conn,
489 DBusMessage *msg, void *data)
491 const char *sender, *path;
494 DBG("conn %p", conn);
496 sender = dbus_message_get_sender(msg);
498 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
501 err = __connman_agent_unregister(sender, path);
503 return __connman_error_failed(msg, -err);
505 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
508 static DBusMessage *register_counter(DBusConnection *conn,
509 DBusMessage *msg, void *data)
511 const char *sender, *path;
512 unsigned int accuracy, period;
515 DBG("conn %p", conn);
517 sender = dbus_message_get_sender(msg);
519 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
520 DBUS_TYPE_UINT32, &accuracy,
521 DBUS_TYPE_UINT32, &period,
524 /* FIXME: add handling of accuracy parameter */
526 err = __connman_counter_register(sender, path, period);
528 return __connman_error_failed(msg, -err);
530 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
533 static DBusMessage *unregister_counter(DBusConnection *conn,
534 DBusMessage *msg, void *data)
536 const char *sender, *path;
539 DBG("conn %p", conn);
541 sender = dbus_message_get_sender(msg);
543 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
546 err = __connman_counter_unregister(sender, path);
548 return __connman_error_failed(msg, -err);
550 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
553 static DBusMessage *create_session(DBusConnection *conn,
554 DBusMessage *msg, void *data)
558 DBG("conn %p", conn);
560 err = __connman_session_create(msg);
562 return __connman_error_failed(msg, -err);
564 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
567 static DBusMessage *destroy_session(DBusConnection *conn,
568 DBusMessage *msg, void *data)
572 DBG("conn %p", conn);
574 err = __connman_session_destroy(msg);
576 return __connman_error_failed(msg, -err);
578 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
581 static GDBusMethodTable manager_methods[] = {
582 { "GetProperties", "", "a{sv}", get_properties },
583 { "SetProperty", "sv", "", set_property },
584 { "GetState", "", "s", get_state },
585 { "RemoveProvider", "o", "", remove_provider },
586 { "RequestScan", "s", "", request_scan },
587 { "EnableTechnology", "s", "", enable_technology,
588 G_DBUS_METHOD_FLAG_ASYNC },
589 { "DisableTechnology", "s", "", disable_technology,
590 G_DBUS_METHOD_FLAG_ASYNC },
591 { "GetServices", "", "a(oa{sv})", get_services },
592 { "LookupService", "s", "o", lookup_service, },
593 { "ConnectService", "a{sv}", "o", connect_service,
594 G_DBUS_METHOD_FLAG_ASYNC },
595 { "ConnectProvider", "a{sv}", "o", connect_provider,
596 G_DBUS_METHOD_FLAG_ASYNC },
597 { "RegisterAgent", "o", "", register_agent },
598 { "UnregisterAgent", "o", "", unregister_agent },
599 { "RegisterCounter", "ouu", "", register_counter },
600 { "UnregisterCounter", "o", "", unregister_counter },
601 { "CreateSession", "a{sv}o", "o", create_session },
602 { "DestroySession", "o", "", destroy_session },
606 static GDBusSignalTable manager_signals[] = {
607 { "PropertyChanged", "sv" },
608 { "StateChanged", "s" },
612 int __connman_manager_init(void)
616 connection = connman_dbus_get_connection();
617 if (connection == NULL)
620 if (connman_notifier_register(&technology_notifier) < 0)
621 connman_error("Failed to register technology notifier");
623 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
624 CONNMAN_MANAGER_INTERFACE,
626 manager_signals, NULL, NULL, NULL);
631 void __connman_manager_cleanup(void)
635 connman_notifier_unregister(&technology_notifier);
637 if (connection == NULL)
640 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
641 CONNMAN_MANAGER_INTERFACE);
643 dbus_connection_unref(connection);