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_notifier_get_state();
54 connman_dbus_dict_append_basic(&dict, "State",
55 DBUS_TYPE_STRING, &str);
57 offlinemode = __connman_technology_get_offlinemode();
58 connman_dbus_dict_append_basic(&dict, "OfflineMode",
59 DBUS_TYPE_BOOLEAN, &offlinemode);
61 connman_dbus_dict_append_array(&dict, "AvailableDebugs",
62 DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
63 connman_dbus_dict_append_array(&dict, "EnabledDebugs",
64 DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
66 sessionmode = __connman_session_mode();
67 connman_dbus_dict_append_basic(&dict, "SessionMode",
71 connman_dbus_dict_close(&array, &dict);
76 static DBusMessage *set_property(DBusConnection *conn,
77 DBusMessage *msg, void *data)
79 DBusMessageIter iter, value;
85 if (dbus_message_iter_init(msg, &iter) == FALSE)
86 return __connman_error_invalid_arguments(msg);
88 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
89 return __connman_error_invalid_arguments(msg);
91 dbus_message_iter_get_basic(&iter, &name);
92 dbus_message_iter_next(&iter);
94 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
95 return __connman_error_invalid_arguments(msg);
97 dbus_message_iter_recurse(&iter, &value);
99 type = dbus_message_iter_get_arg_type(&value);
101 if (g_str_equal(name, "OfflineMode") == TRUE) {
102 connman_bool_t offlinemode;
104 if (type != DBUS_TYPE_BOOLEAN)
105 return __connman_error_invalid_arguments(msg);
107 dbus_message_iter_get_basic(&value, &offlinemode);
109 __connman_technology_set_offlinemode(offlinemode);
110 } else if (g_str_equal(name, "SessionMode") == TRUE) {
111 connman_bool_t sessionmode;
113 if (type != DBUS_TYPE_BOOLEAN)
114 return __connman_error_invalid_arguments(msg);
116 dbus_message_iter_get_basic(&value, &sessionmode);
118 if (session_mode_pending != NULL)
119 return __connman_error_in_progress(msg);
121 __connman_session_set_mode(sessionmode);
123 if (sessionmode == TRUE && connman_state_idle == FALSE) {
124 session_mode_pending = msg;
129 return __connman_error_invalid_property(msg);
131 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
134 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
136 __connman_technology_list_struct(iter);
139 static DBusMessage *get_technologies(DBusConnection *conn,
140 DBusMessage *msg, void *data)
146 reply = dbus_message_new_method_return(msg);
150 __connman_dbus_append_objpath_dict_array(reply,
151 append_technology_structs, NULL);
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 DBusConnection *connection = NULL;
176 static void session_mode_notify(void)
180 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
181 g_dbus_send_message(connection, reply);
183 dbus_message_unref(session_mode_pending);
184 session_mode_pending = NULL;
187 static void idle_state(connman_bool_t idle)
190 DBG("idle %d", idle);
192 connman_state_idle = idle;
194 if (connman_state_idle == FALSE || session_mode_pending == NULL)
197 session_mode_notify();
200 static struct connman_notifier technology_notifier = {
202 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
203 .idle_state = idle_state,
206 static void append_service_structs(DBusMessageIter *iter, void *user_data)
208 __connman_service_list_struct(iter);
211 static DBusMessage *get_services(DBusConnection *conn,
212 DBusMessage *msg, void *data)
216 reply = dbus_message_new_method_return(msg);
220 __connman_dbus_append_objpath_dict_array(reply,
221 append_service_structs, NULL);
226 static DBusMessage *connect_provider(DBusConnection *conn,
227 DBusMessage *msg, void *data)
231 DBG("conn %p", conn);
233 if (__connman_session_mode() == TRUE) {
234 connman_info("Session mode enabled: "
235 "direct provider connect disabled");
237 return __connman_error_failed(msg, -EINVAL);
240 err = __connman_provider_create_and_connect(msg);
242 if (err == -EINPROGRESS) {
243 connman_error("Invalid return code from connect");
247 return __connman_error_failed(msg, -err);
253 static DBusMessage *register_agent(DBusConnection *conn,
254 DBusMessage *msg, void *data)
256 const char *sender, *path;
259 DBG("conn %p", conn);
261 sender = dbus_message_get_sender(msg);
263 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
266 err = __connman_agent_register(sender, path);
268 return __connman_error_failed(msg, -err);
270 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
273 static DBusMessage *unregister_agent(DBusConnection *conn,
274 DBusMessage *msg, void *data)
276 const char *sender, *path;
279 DBG("conn %p", conn);
281 sender = dbus_message_get_sender(msg);
283 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
286 err = __connman_agent_unregister(sender, path);
288 return __connman_error_failed(msg, -err);
290 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
293 static DBusMessage *register_counter(DBusConnection *conn,
294 DBusMessage *msg, void *data)
296 const char *sender, *path;
297 unsigned int accuracy, period;
300 DBG("conn %p", conn);
302 sender = dbus_message_get_sender(msg);
304 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
305 DBUS_TYPE_UINT32, &accuracy,
306 DBUS_TYPE_UINT32, &period,
309 /* FIXME: add handling of accuracy parameter */
311 err = __connman_counter_register(sender, path, period);
313 return __connman_error_failed(msg, -err);
315 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
318 static DBusMessage *unregister_counter(DBusConnection *conn,
319 DBusMessage *msg, void *data)
321 const char *sender, *path;
324 DBG("conn %p", conn);
326 sender = dbus_message_get_sender(msg);
328 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
331 err = __connman_counter_unregister(sender, path);
333 return __connman_error_failed(msg, -err);
335 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
338 static DBusMessage *create_session(DBusConnection *conn,
339 DBusMessage *msg, void *data)
343 DBG("conn %p", conn);
345 err = __connman_session_create(msg);
347 return __connman_error_failed(msg, -err);
349 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
352 static DBusMessage *destroy_session(DBusConnection *conn,
353 DBusMessage *msg, void *data)
357 DBG("conn %p", conn);
359 err = __connman_session_destroy(msg);
361 return __connman_error_failed(msg, -err);
363 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
366 static DBusMessage *request_private_network(DBusConnection *conn,
367 DBusMessage *msg, void *data)
372 DBG("conn %p", conn);
374 sender = dbus_message_get_sender(msg);
376 err = __connman_private_network_request(msg, sender);
378 return __connman_error_failed(msg, -err);
383 static DBusMessage *release_private_network(DBusConnection *conn,
384 DBusMessage *msg, void *data)
389 DBG("conn %p", conn);
391 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
394 err = __connman_private_network_release(path);
396 return __connman_error_failed(msg, -err);
398 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
401 static GDBusMethodTable manager_methods[] = {
402 { "GetProperties", "", "a{sv}", get_properties },
403 { "SetProperty", "sv", "", set_property,
404 G_DBUS_METHOD_FLAG_ASYNC },
405 { "GetTechnologies", "", "a(oa{sv})", get_technologies },
406 { "RemoveProvider", "o", "", remove_provider },
407 { "GetServices", "", "a(oa{sv})", get_services },
408 { "ConnectProvider", "a{sv}", "o", connect_provider,
409 G_DBUS_METHOD_FLAG_ASYNC },
410 { "RegisterAgent", "o", "", register_agent },
411 { "UnregisterAgent", "o", "", unregister_agent },
412 { "RegisterCounter", "ouu", "", register_counter },
413 { "UnregisterCounter", "o", "", unregister_counter },
414 { "CreateSession", "a{sv}o", "o", create_session },
415 { "DestroySession", "o", "", destroy_session },
416 { "RequestPrivateNetwork", "", "oa{sv}h",
417 request_private_network,
418 G_DBUS_METHOD_FLAG_ASYNC },
419 { "ReleasePrivateNetwork", "o", "",
420 release_private_network },
424 static GDBusSignalTable manager_signals[] = {
425 { "PropertyChanged", "sv" },
426 { "TechnologyAdded", "oa{sv}" },
427 { "TechnologyRemoved", "o" },
428 { "ServicesAdded", "a(oa{sv})" },
429 { "ServicesRemoved", "ao" },
433 int __connman_manager_init(void)
437 connection = connman_dbus_get_connection();
438 if (connection == NULL)
441 if (connman_notifier_register(&technology_notifier) < 0)
442 connman_error("Failed to register technology notifier");
444 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
445 CONNMAN_MANAGER_INTERFACE,
447 manager_signals, NULL, NULL, NULL);
449 connman_state_idle = TRUE;
454 void __connman_manager_cleanup(void)
458 if (connection == NULL)
461 connman_notifier_unregister(&technology_notifier);
463 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
464 CONNMAN_MANAGER_INTERFACE);
466 dbus_connection_unref(connection);