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 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_technology_get_offlinemode();
63 connman_dbus_dict_append_basic(&dict, "OfflineMode",
64 DBUS_TYPE_BOOLEAN, &offlinemode);
66 str = __connman_service_default();
68 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
69 DBUS_TYPE_STRING, &str);
71 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
72 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
73 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
74 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
76 sessionmode = __connman_session_mode();
77 connman_dbus_dict_append_basic(&dict, "SessionMode",
81 connman_dbus_dict_close(&array, &dict);
86 static DBusMessage *set_property(DBusConnection *conn,
87 DBusMessage *msg, void *data)
89 DBusMessageIter iter, value;
95 if (dbus_message_iter_init(msg, &iter) == FALSE)
96 return __connman_error_invalid_arguments(msg);
98 dbus_message_iter_get_basic(&iter, &name);
99 dbus_message_iter_next(&iter);
100 dbus_message_iter_recurse(&iter, &value);
102 type = dbus_message_iter_get_arg_type(&value);
104 if (g_str_equal(name, "OfflineMode") == TRUE) {
105 connman_bool_t offlinemode;
107 if (type != DBUS_TYPE_BOOLEAN)
108 return __connman_error_invalid_arguments(msg);
110 dbus_message_iter_get_basic(&value, &offlinemode);
112 __connman_technology_set_offlinemode(offlinemode);
113 } else if (g_str_equal(name, "SessionMode") == TRUE) {
114 connman_bool_t sessionmode;
116 if (type != DBUS_TYPE_BOOLEAN)
117 return __connman_error_invalid_arguments(msg);
119 dbus_message_iter_get_basic(&value, &sessionmode);
121 if (session_mode_pending != NULL)
122 return __connman_error_in_progress(msg);
124 __connman_session_set_mode(sessionmode);
126 if (sessionmode == TRUE && connman_state_idle == FALSE) {
127 session_mode_pending = msg;
132 return __connman_error_invalid_property(msg);
134 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
137 static DBusMessage *remove_provider(DBusConnection *conn,
138 DBusMessage *msg, void *data)
143 DBG("conn %p", conn);
145 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
148 err = __connman_provider_remove(path);
150 return __connman_error_failed(msg, -err);
152 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
155 static DBusMessage *request_scan(DBusConnection *conn,
156 DBusMessage *msg, void *data)
158 enum connman_service_type type;
162 DBG("conn %p", conn);
164 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
167 if (g_strcmp0(str, "") == 0)
168 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
169 else if (g_strcmp0(str, "wifi") == 0)
170 type = CONNMAN_SERVICE_TYPE_WIFI;
171 else if (g_strcmp0(str, "wimax") == 0)
172 type = CONNMAN_SERVICE_TYPE_WIMAX;
174 return __connman_error_invalid_arguments(msg);
176 err = __connman_device_request_scan(type);
178 if (err == -EINPROGRESS) {
179 connman_error("Invalid return code from scan");
183 return __connman_error_failed(msg, -err);
186 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
189 static DBusConnection *connection = NULL;
191 static void session_mode_notify(void)
195 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
196 g_dbus_send_message(connection, reply);
198 dbus_message_unref(session_mode_pending);
199 session_mode_pending = NULL;
202 static void idle_state(connman_bool_t idle)
205 DBG("idle %d", idle);
207 connman_state_idle = idle;
209 if (connman_state_idle == FALSE || session_mode_pending == NULL)
212 session_mode_notify();
215 static struct connman_notifier technology_notifier = {
217 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
218 .idle_state = idle_state,
221 static DBusMessage *enable_technology(DBusConnection *conn,
222 DBusMessage *msg, void *data)
224 enum connman_service_type type;
227 DBG("conn %p", conn);
229 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
232 if (g_strcmp0(str, "ethernet") == 0)
233 type = CONNMAN_SERVICE_TYPE_ETHERNET;
234 else if (g_strcmp0(str, "wifi") == 0)
235 type = CONNMAN_SERVICE_TYPE_WIFI;
236 else if (g_strcmp0(str, "wimax") == 0)
237 type = CONNMAN_SERVICE_TYPE_WIMAX;
238 else if (g_strcmp0(str, "bluetooth") == 0)
239 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
240 else if (g_strcmp0(str, "cellular") == 0)
241 type = CONNMAN_SERVICE_TYPE_CELLULAR;
243 return __connman_error_invalid_arguments(msg);
245 if (__connman_notifier_is_registered(type) == FALSE)
246 return __connman_error_not_registered(msg);
248 if (__connman_notifier_is_enabled(type) == TRUE)
249 return __connman_error_already_enabled(msg);
251 __connman_technology_enable(type, msg);
256 static DBusMessage *disable_technology(DBusConnection *conn,
257 DBusMessage *msg, void *data)
259 enum connman_service_type type;
262 DBG("conn %p", conn);
264 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
267 if (g_strcmp0(str, "ethernet") == 0)
268 type = CONNMAN_SERVICE_TYPE_ETHERNET;
269 else if (g_strcmp0(str, "wifi") == 0)
270 type = CONNMAN_SERVICE_TYPE_WIFI;
271 else if (g_strcmp0(str, "wimax") == 0)
272 type = CONNMAN_SERVICE_TYPE_WIMAX;
273 else if (g_strcmp0(str, "bluetooth") == 0)
274 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
275 else if (g_strcmp0(str, "cellular") == 0)
276 type = CONNMAN_SERVICE_TYPE_CELLULAR;
278 return __connman_error_invalid_arguments(msg);
280 if (__connman_notifier_is_registered(type) == FALSE)
281 return __connman_error_not_registered(msg);
283 if (__connman_notifier_is_enabled(type) == FALSE)
284 return __connman_error_already_disabled(msg);
286 __connman_technology_disable(type, msg);
291 static DBusMessage *get_services(DBusConnection *conn,
292 DBusMessage *msg, void *data)
295 DBusMessageIter iter, array;
297 reply = dbus_message_new_method_return(msg);
301 dbus_message_iter_init_append(reply, &iter);
303 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
304 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
305 DBUS_TYPE_OBJECT_PATH_AS_STRING
306 DBUS_TYPE_ARRAY_AS_STRING
307 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
308 DBUS_TYPE_STRING_AS_STRING
309 DBUS_TYPE_VARIANT_AS_STRING
310 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
311 DBUS_STRUCT_END_CHAR_AS_STRING, &array);
313 __connman_service_list_struct(&array);
315 dbus_message_iter_close_container(&iter, &array);
320 static DBusMessage *connect_provider(DBusConnection *conn,
321 DBusMessage *msg, void *data)
325 DBG("conn %p", conn);
327 if (__connman_session_mode() == TRUE) {
328 connman_info("Session mode enabled: "
329 "direct provider connect disabled");
331 return __connman_error_failed(msg, -EINVAL);
334 err = __connman_provider_create_and_connect(msg);
336 if (err == -EINPROGRESS) {
337 connman_error("Invalid return code from connect");
341 return __connman_error_failed(msg, -err);
347 static DBusMessage *register_agent(DBusConnection *conn,
348 DBusMessage *msg, void *data)
350 const char *sender, *path;
353 DBG("conn %p", conn);
355 sender = dbus_message_get_sender(msg);
357 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
360 err = __connman_agent_register(sender, path);
362 return __connman_error_failed(msg, -err);
364 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
367 static DBusMessage *unregister_agent(DBusConnection *conn,
368 DBusMessage *msg, void *data)
370 const char *sender, *path;
373 DBG("conn %p", conn);
375 sender = dbus_message_get_sender(msg);
377 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
380 err = __connman_agent_unregister(sender, path);
382 return __connman_error_failed(msg, -err);
384 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
387 static DBusMessage *register_counter(DBusConnection *conn,
388 DBusMessage *msg, void *data)
390 const char *sender, *path;
391 unsigned int accuracy, period;
394 DBG("conn %p", conn);
396 sender = dbus_message_get_sender(msg);
398 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
399 DBUS_TYPE_UINT32, &accuracy,
400 DBUS_TYPE_UINT32, &period,
403 /* FIXME: add handling of accuracy parameter */
405 err = __connman_counter_register(sender, path, period);
407 return __connman_error_failed(msg, -err);
409 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
412 static DBusMessage *unregister_counter(DBusConnection *conn,
413 DBusMessage *msg, void *data)
415 const char *sender, *path;
418 DBG("conn %p", conn);
420 sender = dbus_message_get_sender(msg);
422 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
425 err = __connman_counter_unregister(sender, path);
427 return __connman_error_failed(msg, -err);
429 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
432 static DBusMessage *create_session(DBusConnection *conn,
433 DBusMessage *msg, void *data)
437 DBG("conn %p", conn);
439 err = __connman_session_create(msg);
441 return __connman_error_failed(msg, -err);
443 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
446 static DBusMessage *destroy_session(DBusConnection *conn,
447 DBusMessage *msg, void *data)
451 DBG("conn %p", conn);
453 err = __connman_session_destroy(msg);
455 return __connman_error_failed(msg, -err);
457 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
460 static DBusMessage *request_private_network(DBusConnection *conn,
461 DBusMessage *msg, void *data)
466 DBG("conn %p", conn);
468 sender = dbus_message_get_sender(msg);
470 err = __connman_private_network_request(msg, sender);
472 return __connman_error_failed(msg, -err);
477 static DBusMessage *release_private_network(DBusConnection *conn,
478 DBusMessage *msg, void *data)
483 DBG("conn %p", conn);
485 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
488 err = __connman_private_network_release(path);
490 return __connman_error_failed(msg, -err);
492 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
495 static GDBusMethodTable manager_methods[] = {
496 { "GetProperties", "", "a{sv}", get_properties },
497 { "SetProperty", "sv", "", set_property,
498 G_DBUS_METHOD_FLAG_ASYNC },
499 { "RemoveProvider", "o", "", remove_provider },
500 { "RequestScan", "s", "", request_scan },
501 { "EnableTechnology", "s", "", enable_technology,
502 G_DBUS_METHOD_FLAG_ASYNC },
503 { "DisableTechnology", "s", "", disable_technology,
504 G_DBUS_METHOD_FLAG_ASYNC },
505 { "GetServices", "", "a(oa{sv})", get_services },
506 { "ConnectProvider", "a{sv}", "o", connect_provider,
507 G_DBUS_METHOD_FLAG_ASYNC },
508 { "RegisterAgent", "o", "", register_agent },
509 { "UnregisterAgent", "o", "", unregister_agent },
510 { "RegisterCounter", "ouu", "", register_counter },
511 { "UnregisterCounter", "o", "", unregister_counter },
512 { "CreateSession", "a{sv}o", "o", create_session },
513 { "DestroySession", "o", "", destroy_session },
514 { "RequestPrivateNetwork", "", "oa{sv}h",
515 request_private_network,
516 G_DBUS_METHOD_FLAG_ASYNC },
517 { "ReleasePrivateNetwork", "o", "",
518 release_private_network },
522 static GDBusSignalTable manager_signals[] = {
523 { "PropertyChanged", "sv" },
527 int __connman_manager_init(void)
531 connection = connman_dbus_get_connection();
532 if (connection == NULL)
535 if (connman_notifier_register(&technology_notifier) < 0)
536 connman_error("Failed to register technology notifier");
538 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
539 CONNMAN_MANAGER_INTERFACE,
541 manager_signals, NULL, NULL, NULL);
543 connman_state_idle = TRUE;
548 void __connman_manager_cleanup(void)
552 if (connection == NULL)
555 connman_notifier_unregister(&technology_notifier);
557 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
558 CONNMAN_MANAGER_INTERFACE);
560 dbus_connection_unref(connection);