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
32 connman_bool_t connman_state_idle;
33 DBusMessage *session_mode_pending = NULL;
35 static DBusMessage *get_properties(DBusConnection *conn,
36 DBusMessage *msg, void *data)
39 DBusMessageIter array, dict;
40 connman_bool_t offlinemode, sessionmode;
45 reply = dbus_message_new_method_return(msg);
49 dbus_message_iter_init_append(reply, &array);
51 connman_dbus_dict_open(&array, &dict);
53 str = __connman_profile_active_path();
55 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
56 DBUS_TYPE_OBJECT_PATH, &str);
58 connman_dbus_dict_append_array(&dict, "Services",
59 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
60 connman_dbus_dict_append_array(&dict, "Technologies",
61 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
63 str = __connman_notifier_get_state();
64 connman_dbus_dict_append_basic(&dict, "State",
65 DBUS_TYPE_STRING, &str);
67 offlinemode = __connman_profile_get_offlinemode();
68 connman_dbus_dict_append_basic(&dict, "OfflineMode",
69 DBUS_TYPE_BOOLEAN, &offlinemode);
71 connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
72 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
73 connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
74 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
75 connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
76 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
78 str = __connman_service_default();
80 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
81 DBUS_TYPE_STRING, &str);
83 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
84 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
85 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
86 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
88 sessionmode = __connman_session_mode();
89 connman_dbus_dict_append_basic(&dict, "SessionMode",
93 connman_dbus_dict_close(&array, &dict);
98 static DBusMessage *set_property(DBusConnection *conn,
99 DBusMessage *msg, void *data)
101 DBusMessageIter iter, value;
105 DBG("conn %p", conn);
107 if (dbus_message_iter_init(msg, &iter) == FALSE)
108 return __connman_error_invalid_arguments(msg);
110 dbus_message_iter_get_basic(&iter, &name);
111 dbus_message_iter_next(&iter);
112 dbus_message_iter_recurse(&iter, &value);
114 type = dbus_message_iter_get_arg_type(&value);
116 if (g_str_equal(name, "OfflineMode") == TRUE) {
117 connman_bool_t offlinemode;
119 if (type != DBUS_TYPE_BOOLEAN)
120 return __connman_error_invalid_arguments(msg);
122 dbus_message_iter_get_basic(&value, &offlinemode);
124 __connman_technology_set_offlinemode(offlinemode);
125 } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
128 dbus_message_iter_get_basic(&value, &str);
130 return __connman_error_not_supported(msg);
131 } else if (g_str_equal(name, "SessionMode") == TRUE) {
132 connman_bool_t sessionmode;
134 if (type != DBUS_TYPE_BOOLEAN)
135 return __connman_error_invalid_arguments(msg);
137 dbus_message_iter_get_basic(&value, &sessionmode);
139 if (session_mode_pending != NULL)
140 return __connman_error_in_progress(msg);
142 __connman_session_set_mode(sessionmode);
144 if (connman_state_idle == FALSE) {
145 session_mode_pending = msg;
150 return __connman_error_invalid_property(msg);
152 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
155 static DBusMessage *get_state(DBusConnection *conn,
156 DBusMessage *msg, void *data)
160 DBG("conn %p", conn);
162 str = __connman_notifier_get_state();
164 return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
168 static DBusMessage *remove_provider(DBusConnection *conn,
169 DBusMessage *msg, void *data)
174 DBG("conn %p", conn);
176 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
179 err = __connman_provider_remove(path);
181 return __connman_error_failed(msg, -err);
183 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
186 static DBusMessage *request_scan(DBusConnection *conn,
187 DBusMessage *msg, void *data)
189 enum connman_service_type type;
193 DBG("conn %p", conn);
195 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
198 if (g_strcmp0(str, "") == 0)
199 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
200 else if (g_strcmp0(str, "wifi") == 0)
201 type = CONNMAN_SERVICE_TYPE_WIFI;
202 else if (g_strcmp0(str, "wimax") == 0)
203 type = CONNMAN_SERVICE_TYPE_WIMAX;
205 return __connman_error_invalid_arguments(msg);
207 err = __connman_device_request_scan(type);
209 if (err == -EINPROGRESS) {
210 connman_error("Invalid return code from scan");
214 return __connman_error_failed(msg, -err);
217 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
220 static DBusConnection *connection = NULL;
222 static void session_mode_notify(void)
226 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
227 g_dbus_send_message(connection, reply);
229 dbus_message_unref(session_mode_pending);
230 session_mode_pending = NULL;
233 static void idle_state(connman_bool_t idle)
236 DBG("idle %d", idle);
238 connman_state_idle = idle;
240 if (connman_state_idle == FALSE || session_mode_pending == NULL)
243 session_mode_notify();
246 static struct connman_notifier technology_notifier = {
248 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
249 .idle_state = idle_state,
252 static DBusMessage *enable_technology(DBusConnection *conn,
253 DBusMessage *msg, void *data)
255 enum connman_service_type type;
258 DBG("conn %p", conn);
260 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
263 if (g_strcmp0(str, "ethernet") == 0)
264 type = CONNMAN_SERVICE_TYPE_ETHERNET;
265 else if (g_strcmp0(str, "wifi") == 0)
266 type = CONNMAN_SERVICE_TYPE_WIFI;
267 else if (g_strcmp0(str, "wimax") == 0)
268 type = CONNMAN_SERVICE_TYPE_WIMAX;
269 else if (g_strcmp0(str, "bluetooth") == 0)
270 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
271 else if (g_strcmp0(str, "cellular") == 0)
272 type = CONNMAN_SERVICE_TYPE_CELLULAR;
274 return __connman_error_invalid_arguments(msg);
276 if (__connman_notifier_is_registered(type) == FALSE)
277 return __connman_error_not_registered(msg);
279 if (__connman_notifier_is_enabled(type) == TRUE)
280 return __connman_error_already_enabled(msg);
282 __connman_technology_enable(type, msg);
287 static DBusMessage *disable_technology(DBusConnection *conn,
288 DBusMessage *msg, void *data)
290 enum connman_service_type type;
293 DBG("conn %p", conn);
295 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
298 if (g_strcmp0(str, "ethernet") == 0)
299 type = CONNMAN_SERVICE_TYPE_ETHERNET;
300 else if (g_strcmp0(str, "wifi") == 0)
301 type = CONNMAN_SERVICE_TYPE_WIFI;
302 else if (g_strcmp0(str, "wimax") == 0)
303 type = CONNMAN_SERVICE_TYPE_WIMAX;
304 else if (g_strcmp0(str, "bluetooth") == 0)
305 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
306 else if (g_strcmp0(str, "cellular") == 0)
307 type = CONNMAN_SERVICE_TYPE_CELLULAR;
309 return __connman_error_invalid_arguments(msg);
311 if (__connman_notifier_is_registered(type) == FALSE)
312 return __connman_error_not_registered(msg);
314 if (__connman_notifier_is_enabled(type) == FALSE)
315 return __connman_error_already_disabled(msg);
317 __connman_technology_disable(type, msg);
322 static DBusMessage *get_services(DBusConnection *conn,
323 DBusMessage *msg, void *data)
326 DBusMessageIter iter, array;
328 reply = dbus_message_new_method_return(msg);
332 dbus_message_iter_init_append(reply, &iter);
334 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
335 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
336 DBUS_TYPE_OBJECT_PATH_AS_STRING
337 DBUS_TYPE_ARRAY_AS_STRING
338 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
339 DBUS_TYPE_STRING_AS_STRING
340 DBUS_TYPE_VARIANT_AS_STRING
341 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
342 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
344 __connman_service_list_struct(&array);
346 dbus_message_iter_close_container(&iter, &array);
351 static DBusMessage *lookup_service(DBusConnection *conn,
352 DBusMessage *msg, void *data)
354 const char *pattern, *path;
357 DBG("conn %p", conn);
359 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
362 err = __connman_service_lookup(pattern, &path);
364 return __connman_error_failed(msg, -err);
366 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
370 static DBusMessage *connect_service(DBusConnection *conn,
371 DBusMessage *msg, void *data)
375 DBG("conn %p", conn);
377 if (__connman_session_mode() == TRUE) {
378 connman_info("Session mode enabled: "
379 "direct service connect disabled");
381 return __connman_error_failed(msg, -EINVAL);
384 err = __connman_service_create_and_connect(msg);
386 if (err == -EINPROGRESS) {
387 connman_error("Invalid return code from connect");
391 return __connman_error_failed(msg, -err);
397 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
402 DBG("conn %p", conn);
404 err = __connman_service_provision(msg);
406 return __connman_error_failed(msg, -err);
411 static DBusMessage *connect_provider(DBusConnection *conn,
412 DBusMessage *msg, void *data)
416 DBG("conn %p", conn);
418 if (__connman_session_mode() == TRUE) {
419 connman_info("Session mode enabled: "
420 "direct provider connect disabled");
422 return __connman_error_failed(msg, -EINVAL);
425 err = __connman_provider_create_and_connect(msg);
427 if (err == -EINPROGRESS) {
428 connman_error("Invalid return code from connect");
432 return __connman_error_failed(msg, -err);
438 static DBusMessage *register_agent(DBusConnection *conn,
439 DBusMessage *msg, void *data)
441 const char *sender, *path;
444 DBG("conn %p", conn);
446 sender = dbus_message_get_sender(msg);
448 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
451 err = __connman_agent_register(sender, path);
453 return __connman_error_failed(msg, -err);
455 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
458 static DBusMessage *unregister_agent(DBusConnection *conn,
459 DBusMessage *msg, void *data)
461 const char *sender, *path;
464 DBG("conn %p", conn);
466 sender = dbus_message_get_sender(msg);
468 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
471 err = __connman_agent_unregister(sender, path);
473 return __connman_error_failed(msg, -err);
475 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
478 static DBusMessage *register_counter(DBusConnection *conn,
479 DBusMessage *msg, void *data)
481 const char *sender, *path;
482 unsigned int accuracy, period;
485 DBG("conn %p", conn);
487 sender = dbus_message_get_sender(msg);
489 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
490 DBUS_TYPE_UINT32, &accuracy,
491 DBUS_TYPE_UINT32, &period,
494 /* FIXME: add handling of accuracy parameter */
496 err = __connman_counter_register(sender, path, period);
498 return __connman_error_failed(msg, -err);
500 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
503 static DBusMessage *unregister_counter(DBusConnection *conn,
504 DBusMessage *msg, void *data)
506 const char *sender, *path;
509 DBG("conn %p", conn);
511 sender = dbus_message_get_sender(msg);
513 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
516 err = __connman_counter_unregister(sender, path);
518 return __connman_error_failed(msg, -err);
520 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
523 static DBusMessage *create_session(DBusConnection *conn,
524 DBusMessage *msg, void *data)
528 DBG("conn %p", conn);
530 err = __connman_session_create(msg);
532 return __connman_error_failed(msg, -err);
534 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
537 static DBusMessage *destroy_session(DBusConnection *conn,
538 DBusMessage *msg, void *data)
542 DBG("conn %p", conn);
544 err = __connman_session_destroy(msg);
546 return __connman_error_failed(msg, -err);
548 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
551 static DBusMessage *request_private_network(DBusConnection *conn,
552 DBusMessage *msg, void *data)
557 DBG("conn %p", conn);
559 sender = dbus_message_get_sender(msg);
561 err = __connman_private_network_request(msg, sender);
563 return __connman_error_failed(msg, -err);
568 static DBusMessage *release_private_network(DBusConnection *conn,
569 DBusMessage *msg, void *data)
574 DBG("conn %p", conn);
576 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
579 err = __connman_private_network_release(path);
581 return __connman_error_failed(msg, -err);
583 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
586 static GDBusMethodTable manager_methods[] = {
587 { "GetProperties", "", "a{sv}", get_properties },
588 { "SetProperty", "sv", "", set_property,
589 G_DBUS_METHOD_FLAG_ASYNC },
590 { "GetState", "", "s", get_state },
591 { "RemoveProvider", "o", "", remove_provider },
592 { "RequestScan", "s", "", request_scan },
593 { "EnableTechnology", "s", "", enable_technology,
594 G_DBUS_METHOD_FLAG_ASYNC },
595 { "DisableTechnology", "s", "", disable_technology,
596 G_DBUS_METHOD_FLAG_ASYNC },
597 { "GetServices", "", "a(oa{sv})", get_services },
598 { "LookupService", "s", "o", lookup_service, },
599 { "ConnectService", "a{sv}", "o", connect_service,
600 G_DBUS_METHOD_FLAG_ASYNC },
601 { "ProvisionService", "s", "", provision_service,
602 G_DBUS_METHOD_FLAG_ASYNC },
603 { "ConnectProvider", "a{sv}", "o", connect_provider,
604 G_DBUS_METHOD_FLAG_ASYNC },
605 { "RegisterAgent", "o", "", register_agent },
606 { "UnregisterAgent", "o", "", unregister_agent },
607 { "RegisterCounter", "ouu", "", register_counter },
608 { "UnregisterCounter", "o", "", unregister_counter },
609 { "CreateSession", "a{sv}o", "o", create_session },
610 { "DestroySession", "o", "", destroy_session },
611 { "RequestPrivateNetwork", "", "oa{sv}h",
612 request_private_network,
613 G_DBUS_METHOD_FLAG_ASYNC },
614 { "ReleasePrivateNetwork", "o", "",
615 release_private_network },
619 static GDBusSignalTable manager_signals[] = {
620 { "PropertyChanged", "sv" },
621 { "StateChanged", "s" },
625 int __connman_manager_init(void)
629 connection = connman_dbus_get_connection();
630 if (connection == NULL)
633 if (connman_notifier_register(&technology_notifier) < 0)
634 connman_error("Failed to register technology notifier");
636 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
637 CONNMAN_MANAGER_INTERFACE,
639 manager_signals, NULL, NULL, NULL);
641 connman_state_idle = TRUE;
646 void __connman_manager_cleanup(void)
650 if (connection == NULL)
653 connman_notifier_unregister(&technology_notifier);
655 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
656 CONNMAN_MANAGER_INTERFACE);
658 dbus_connection_unref(connection);