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;
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 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
69 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
70 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
71 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
72 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
73 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
75 str = __connman_service_default();
77 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
78 DBUS_TYPE_STRING, &str);
80 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
81 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
82 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
83 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
85 connman_dbus_dict_close(&array, &dict);
90 static DBusMessage *set_property(DBusConnection *conn,
91 DBusMessage *msg, void *data)
93 DBusMessageIter iter, value;
99 if (dbus_message_iter_init(msg, &iter) == FALSE)
100 return __connman_error_invalid_arguments(msg);
102 dbus_message_iter_get_basic(&iter, &name);
103 dbus_message_iter_next(&iter);
104 dbus_message_iter_recurse(&iter, &value);
106 type = dbus_message_iter_get_arg_type(&value);
108 if (g_str_equal(name, "OfflineMode") == TRUE) {
109 connman_bool_t offlinemode;
111 if (type != DBUS_TYPE_BOOLEAN)
112 return __connman_error_invalid_arguments(msg);
114 dbus_message_iter_get_basic(&value, &offlinemode);
116 __connman_profile_set_offlinemode(offlinemode, TRUE);
118 __connman_profile_save_default();
119 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
122 dbus_message_iter_get_basic(&value, &str);
124 return __connman_error_not_supported(msg);
126 return __connman_error_invalid_property(msg);
128 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
131 static DBusMessage *get_state(DBusConnection *conn,
132 DBusMessage *msg, void *data)
136 DBG("conn %p", conn);
138 str = __connman_notifier_get_state();
140 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
144 static DBusMessage *create_profile(DBusConnection *conn,
145 DBusMessage *msg, void *data)
147 const char *name, *path;
150 DBG("conn %p", conn);
152 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
155 err = __connman_profile_create(name, &path);
157 return __connman_error_failed(msg, -err);
159 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
163 static DBusMessage *remove_profile(DBusConnection *conn,
164 DBusMessage *msg, void *data)
169 DBG("conn %p", conn);
171 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
174 err = __connman_profile_remove(path);
176 return __connman_error_failed(msg, -err);
178 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
181 static DBusMessage *remove_provider(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_provider_remove(path);
194 return __connman_error_failed(msg, -err);
196 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
199 static DBusMessage *request_scan(DBusConnection *conn,
200 DBusMessage *msg, void *data)
202 enum connman_service_type type;
206 DBG("conn %p", conn);
208 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
211 if (g_strcmp0(str, "") == 0)
212 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
213 else if (g_strcmp0(str, "wifi") == 0)
214 type = CONNMAN_SERVICE_TYPE_WIFI;
215 else if (g_strcmp0(str, "wimax") == 0)
216 type = CONNMAN_SERVICE_TYPE_WIMAX;
218 return __connman_error_invalid_arguments(msg);
220 err = __connman_element_request_scan(type);
222 if (err == -EINPROGRESS) {
223 connman_error("Invalid return code from scan");
227 return __connman_error_failed(msg, -err);
230 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
233 static DBusConnection *connection = NULL;
235 static enum connman_service_type technology_type;
236 static connman_bool_t technology_enabled;
237 static DBusMessage *technology_pending = NULL;
238 static guint technology_timeout = 0;
240 static void technology_reply(int error)
244 if (technology_timeout > 0) {
245 g_source_remove(technology_timeout);
246 technology_timeout = 0;
249 if (technology_pending != NULL) {
253 reply = __connman_error_failed(technology_pending,
256 g_dbus_send_message(connection, reply);
258 g_dbus_send_reply(connection, technology_pending,
261 dbus_message_unref(technology_pending);
262 technology_pending = NULL;
265 technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
268 static gboolean technology_abort(gpointer user_data)
272 technology_timeout = 0;
274 technology_reply(ETIMEDOUT);
279 static void technology_notify(enum connman_service_type type,
280 connman_bool_t enabled)
282 DBG("type %d enabled %d", type, enabled);
284 if (type == technology_type && enabled == technology_enabled)
288 static struct connman_notifier technology_notifier = {
290 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
291 .service_enabled= technology_notify,
294 static DBusMessage *enable_technology(DBusConnection *conn,
295 DBusMessage *msg, void *data)
297 enum connman_service_type type;
301 DBG("conn %p", conn);
303 if (technology_pending != NULL)
304 return __connman_error_in_progress(msg);
306 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
309 if (g_strcmp0(str, "ethernet") == 0)
310 type = CONNMAN_SERVICE_TYPE_ETHERNET;
311 else if (g_strcmp0(str, "wifi") == 0)
312 type = CONNMAN_SERVICE_TYPE_WIFI;
313 else if (g_strcmp0(str, "wimax") == 0)
314 type = CONNMAN_SERVICE_TYPE_WIMAX;
315 else if (g_strcmp0(str, "bluetooth") == 0)
316 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
317 else if (g_strcmp0(str, "cellular") == 0)
318 type = CONNMAN_SERVICE_TYPE_CELLULAR;
320 return __connman_error_invalid_arguments(msg);
322 if (__connman_notifier_is_registered(type) == FALSE)
323 return __connman_error_not_registered(msg);
325 if (__connman_notifier_is_enabled(type) == TRUE)
326 return __connman_error_already_enabled(msg);
328 technology_type = type;
329 technology_enabled = TRUE;
330 technology_pending = dbus_message_ref(msg);
332 err = __connman_element_enable_technology(type);
333 if (err < 0 && err != -EINPROGRESS)
334 technology_reply(-err);
336 technology_timeout = g_timeout_add_seconds(15,
337 technology_abort, NULL);
342 static DBusMessage *disable_technology(DBusConnection *conn,
343 DBusMessage *msg, void *data)
345 enum connman_service_type type;
349 DBG("conn %p", conn);
351 if (technology_pending != NULL)
352 return __connman_error_in_progress(msg);
354 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
357 if (g_strcmp0(str, "ethernet") == 0)
358 type = CONNMAN_SERVICE_TYPE_ETHERNET;
359 else if (g_strcmp0(str, "wifi") == 0)
360 type = CONNMAN_SERVICE_TYPE_WIFI;
361 else if (g_strcmp0(str, "wimax") == 0)
362 type = CONNMAN_SERVICE_TYPE_WIMAX;
363 else if (g_strcmp0(str, "bluetooth") == 0)
364 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
365 else if (g_strcmp0(str, "cellular") == 0)
366 type = CONNMAN_SERVICE_TYPE_CELLULAR;
368 return __connman_error_invalid_arguments(msg);
370 if (__connman_notifier_is_registered(type) == FALSE)
371 return __connman_error_not_registered(msg);
373 if (__connman_notifier_is_enabled(type) == FALSE)
374 return __connman_error_already_disabled(msg);
376 technology_type = type;
377 technology_enabled = FALSE;
378 technology_pending = dbus_message_ref(msg);
380 err = __connman_element_disable_technology(type);
381 if (err < 0 && err != -EINPROGRESS)
382 technology_reply(-err);
384 technology_timeout = g_timeout_add_seconds(10,
385 technology_abort, NULL);
390 static DBusMessage *get_services(DBusConnection *conn,
391 DBusMessage *msg, void *data)
394 DBusMessageIter iter, array;
396 reply = dbus_message_new_method_return(msg);
400 dbus_message_iter_init_append(reply, &iter);
402 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
403 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
404 DBUS_TYPE_OBJECT_PATH_AS_STRING
405 DBUS_TYPE_ARRAY_AS_STRING
406 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
407 DBUS_TYPE_STRING_AS_STRING
408 DBUS_TYPE_VARIANT_AS_STRING
409 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
410 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
412 __connman_service_list_struct(&array);
414 dbus_message_iter_close_container(&iter, &array);
419 static DBusMessage *lookup_service(DBusConnection *conn,
420 DBusMessage *msg, void *data)
422 const char *pattern, *path;
425 DBG("conn %p", conn);
427 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
430 err = __connman_service_lookup(pattern, &path);
432 return __connman_error_failed(msg, -err);
434 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
438 static DBusMessage *connect_service(DBusConnection *conn,
439 DBusMessage *msg, void *data)
443 DBG("conn %p", conn);
445 err = __connman_service_create_and_connect(msg);
447 if (err == -EINPROGRESS) {
448 connman_error("Invalid return code from connect");
452 return __connman_error_failed(msg, -err);
459 static DBusMessage *connect_provider(DBusConnection *conn,
460 DBusMessage *msg, void *data)
464 DBG("conn %p", conn);
466 err = __connman_provider_create_and_connect(msg);
468 if (err == -EINPROGRESS) {
469 connman_error("Invalid return code from connect");
473 return __connman_error_failed(msg, -err);
479 static DBusMessage *register_agent(DBusConnection *conn,
480 DBusMessage *msg, void *data)
482 const char *sender, *path;
485 DBG("conn %p", conn);
487 sender = dbus_message_get_sender(msg);
489 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
492 err = __connman_agent_register(sender, path);
494 return __connman_error_failed(msg, -err);
496 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
499 static DBusMessage *unregister_agent(DBusConnection *conn,
500 DBusMessage *msg, void *data)
502 const char *sender, *path;
505 DBG("conn %p", conn);
507 sender = dbus_message_get_sender(msg);
509 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
512 err = __connman_agent_unregister(sender, path);
514 return __connman_error_failed(msg, -err);
516 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
519 static DBusMessage *register_counter(DBusConnection *conn,
520 DBusMessage *msg, void *data)
522 const char *sender, *path;
523 unsigned int accuracy, period;
526 DBG("conn %p", conn);
528 sender = dbus_message_get_sender(msg);
530 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
531 DBUS_TYPE_UINT32, &accuracy,
532 DBUS_TYPE_UINT32, &period,
535 /* FIXME: add handling of accuracy parameter */
537 err = __connman_counter_register(sender, path, period);
539 return __connman_error_failed(msg, -err);
541 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
544 static DBusMessage *unregister_counter(DBusConnection *conn,
545 DBusMessage *msg, void *data)
547 const char *sender, *path;
550 DBG("conn %p", conn);
552 sender = dbus_message_get_sender(msg);
554 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
557 err = __connman_counter_unregister(sender, path);
559 return __connman_error_failed(msg, -err);
561 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
564 static DBusMessage *request_session(DBusConnection *conn,
565 DBusMessage *msg, void *data)
567 const char *bearer, *sender, *service_path;
568 struct connman_service *service;
570 DBG("conn %p", conn);
572 sender = dbus_message_get_sender(msg);
574 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &bearer,
577 service = __connman_session_request(bearer, sender);
579 return __connman_error_failed(msg, EINVAL);
581 service_path = __connman_service_get_path(service);
583 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &service_path,
587 static DBusMessage *release_session(DBusConnection *conn,
588 DBusMessage *msg, void *data)
593 DBG("conn %p", conn);
595 sender = dbus_message_get_sender(msg);
597 err = __connman_session_release(sender);
599 return __connman_error_failed(msg, -err);
601 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
604 static GDBusMethodTable manager_methods[] = {
605 { "GetProperties", "", "a{sv}", get_properties },
606 { "SetProperty", "sv", "", set_property },
607 { "GetState", "", "s", get_state },
608 { "CreateProfile", "s", "o", create_profile },
609 { "RemoveProfile", "o", "", remove_profile },
610 { "RemoveProvider", "o", "", remove_provider },
611 { "RequestScan", "s", "", request_scan },
612 { "EnableTechnology", "s", "", enable_technology,
613 G_DBUS_METHOD_FLAG_ASYNC },
614 { "DisableTechnology", "s", "", disable_technology,
615 G_DBUS_METHOD_FLAG_ASYNC },
616 { "GetServices", "", "a(oa{sv})", get_services },
617 { "LookupService", "s", "o", lookup_service, },
618 { "ConnectService", "a{sv}", "o", connect_service,
619 G_DBUS_METHOD_FLAG_ASYNC },
620 { "ConnectProvider", "a{sv}", "o", connect_provider,
621 G_DBUS_METHOD_FLAG_ASYNC },
622 { "RegisterAgent", "o", "", register_agent },
623 { "UnregisterAgent", "o", "", unregister_agent },
624 { "RegisterCounter", "ouu", "", register_counter },
625 { "UnregisterCounter", "o", "", unregister_counter },
626 { "RequestSession", "s", "o", request_session },
627 { "ReleaseSession", "s", "", release_session },
631 static GDBusSignalTable manager_signals[] = {
632 { "PropertyChanged", "sv" },
633 { "StateChanged", "s" },
637 int __connman_manager_init(void)
641 connection = connman_dbus_get_connection();
642 if (connection == NULL)
645 if (connman_notifier_register(&technology_notifier) < 0)
646 connman_error("Failed to register technology notifier");
648 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
649 CONNMAN_MANAGER_INTERFACE,
651 manager_signals, NULL, NULL, NULL);
656 void __connman_manager_cleanup(void)
660 connman_notifier_unregister(&technology_notifier);
662 if (connection == NULL)
665 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
666 CONNMAN_MANAGER_INTERFACE);
668 dbus_connection_unref(connection);