5 * Copyright (C) 2007-2012 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 static connman_bool_t connman_state_idle;
33 static 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 sessionmode = __connman_session_mode();
62 connman_dbus_dict_append_basic(&dict, "SessionMode",
66 connman_dbus_dict_close(&array, &dict);
71 static DBusMessage *set_property(DBusConnection *conn,
72 DBusMessage *msg, void *data)
74 DBusMessageIter iter, value;
80 if (dbus_message_iter_init(msg, &iter) == FALSE)
81 return __connman_error_invalid_arguments(msg);
83 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
84 return __connman_error_invalid_arguments(msg);
86 dbus_message_iter_get_basic(&iter, &name);
87 dbus_message_iter_next(&iter);
89 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
90 return __connman_error_invalid_arguments(msg);
92 dbus_message_iter_recurse(&iter, &value);
94 type = dbus_message_iter_get_arg_type(&value);
96 if (g_str_equal(name, "OfflineMode") == TRUE) {
97 connman_bool_t offlinemode;
99 if (type != DBUS_TYPE_BOOLEAN)
100 return __connman_error_invalid_arguments(msg);
102 dbus_message_iter_get_basic(&value, &offlinemode);
104 __connman_technology_set_offlinemode(offlinemode);
105 } else if (g_str_equal(name, "SessionMode") == TRUE) {
106 connman_bool_t sessionmode;
108 if (type != DBUS_TYPE_BOOLEAN)
109 return __connman_error_invalid_arguments(msg);
111 dbus_message_iter_get_basic(&value, &sessionmode);
113 if (session_mode_pending != NULL)
114 return __connman_error_in_progress(msg);
116 __connman_session_set_mode(sessionmode);
118 if (sessionmode == TRUE && connman_state_idle == FALSE) {
119 session_mode_pending = dbus_message_ref(msg);
124 return __connman_error_invalid_property(msg);
126 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
129 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
131 __connman_technology_list_struct(iter);
134 static DBusMessage *get_technologies(DBusConnection *conn,
135 DBusMessage *msg, void *data)
141 reply = dbus_message_new_method_return(msg);
145 __connman_dbus_append_objpath_dict_array(reply,
146 append_technology_structs, NULL);
151 static DBusMessage *remove_provider(DBusConnection *conn,
152 DBusMessage *msg, void *data)
157 DBG("conn %p", conn);
159 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
162 err = __connman_provider_remove(path);
164 return __connman_error_failed(msg, -err);
166 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
169 static DBusConnection *connection = NULL;
171 static void session_mode_notify(void)
175 reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
176 g_dbus_send_message(connection, reply);
178 dbus_message_unref(session_mode_pending);
179 session_mode_pending = NULL;
182 static void idle_state(connman_bool_t idle)
185 DBG("idle %d", idle);
187 connman_state_idle = idle;
189 if (connman_state_idle == FALSE || session_mode_pending == NULL)
192 session_mode_notify();
195 static struct connman_notifier technology_notifier = {
197 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
198 .idle_state = idle_state,
201 static void append_service_structs(DBusMessageIter *iter, void *user_data)
203 __connman_service_list_struct(iter);
206 static DBusMessage *get_services(DBusConnection *conn,
207 DBusMessage *msg, void *data)
211 reply = dbus_message_new_method_return(msg);
215 __connman_dbus_append_objpath_dict_array(reply,
216 append_service_structs, NULL);
221 static DBusMessage *connect_provider(DBusConnection *conn,
222 DBusMessage *msg, void *data)
226 DBG("conn %p", conn);
228 if (__connman_session_mode() == TRUE) {
229 connman_info("Session mode enabled: "
230 "direct provider connect disabled");
232 return __connman_error_failed(msg, -EINVAL);
235 err = __connman_provider_create_and_connect(msg);
237 if (err == -EINPROGRESS) {
238 connman_error("Invalid return code from connect");
242 return __connman_error_failed(msg, -err);
248 static DBusMessage *register_agent(DBusConnection *conn,
249 DBusMessage *msg, void *data)
251 const char *sender, *path;
254 DBG("conn %p", conn);
256 sender = dbus_message_get_sender(msg);
258 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
261 err = __connman_agent_register(sender, path);
263 return __connman_error_failed(msg, -err);
265 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
268 static DBusMessage *unregister_agent(DBusConnection *conn,
269 DBusMessage *msg, void *data)
271 const char *sender, *path;
274 DBG("conn %p", conn);
276 sender = dbus_message_get_sender(msg);
278 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
281 err = __connman_agent_unregister(sender, path);
283 return __connman_error_failed(msg, -err);
285 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
288 static DBusMessage *register_counter(DBusConnection *conn,
289 DBusMessage *msg, void *data)
291 const char *sender, *path;
292 unsigned int accuracy, period;
295 DBG("conn %p", conn);
297 sender = dbus_message_get_sender(msg);
299 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
300 DBUS_TYPE_UINT32, &accuracy,
301 DBUS_TYPE_UINT32, &period,
304 /* FIXME: add handling of accuracy parameter */
306 err = __connman_counter_register(sender, path, period);
308 return __connman_error_failed(msg, -err);
310 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
313 static DBusMessage *unregister_counter(DBusConnection *conn,
314 DBusMessage *msg, void *data)
316 const char *sender, *path;
319 DBG("conn %p", conn);
321 sender = dbus_message_get_sender(msg);
323 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
326 err = __connman_counter_unregister(sender, path);
328 return __connman_error_failed(msg, -err);
330 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
333 static DBusMessage *create_session(DBusConnection *conn,
334 DBusMessage *msg, void *data)
338 DBG("conn %p", conn);
340 err = __connman_session_create(msg);
342 return __connman_error_failed(msg, -err);
344 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
347 static DBusMessage *destroy_session(DBusConnection *conn,
348 DBusMessage *msg, void *data)
352 DBG("conn %p", conn);
354 err = __connman_session_destroy(msg);
356 return __connman_error_failed(msg, -err);
358 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
361 static DBusMessage *request_private_network(DBusConnection *conn,
362 DBusMessage *msg, void *data)
367 DBG("conn %p", conn);
369 sender = dbus_message_get_sender(msg);
371 err = __connman_private_network_request(msg, sender);
373 return __connman_error_failed(msg, -err);
378 static DBusMessage *release_private_network(DBusConnection *conn,
379 DBusMessage *msg, void *data)
384 DBG("conn %p", conn);
386 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
389 err = __connman_private_network_release(path);
391 return __connman_error_failed(msg, -err);
393 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
396 static const GDBusMethodTable manager_methods[] = {
397 { GDBUS_METHOD("GetProperties",
398 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
400 { GDBUS_ASYNC_METHOD("SetProperty",
401 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
402 NULL, set_property) },
403 { GDBUS_METHOD("GetTechnologies",
404 NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
406 { GDBUS_METHOD("RemoveProvider",
407 GDBUS_ARGS({ "provider", "o" }), NULL,
409 { GDBUS_METHOD("GetServices",
410 NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
412 { GDBUS_ASYNC_METHOD("ConnectProvider",
413 GDBUS_ARGS({ "provider", "a{sv}" }),
414 GDBUS_ARGS({ "path", "o" }),
416 { GDBUS_METHOD("RegisterAgent",
417 GDBUS_ARGS({ "path", "o" }), NULL,
419 { GDBUS_METHOD("UnregisterAgent",
420 GDBUS_ARGS({ "path", "o" }), NULL,
422 { GDBUS_METHOD("RegisterCounter",
423 GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
425 NULL, register_counter) },
426 { GDBUS_METHOD("UnregisterCounter",
427 GDBUS_ARGS({ "path", "o" }), NULL,
428 unregister_counter) },
429 { GDBUS_METHOD("CreateSession",
430 GDBUS_ARGS({ "settings", "a{sv}" },
431 { "notifier", "o" }),
432 GDBUS_ARGS({ "session", "o" }),
434 { GDBUS_METHOD("DestroySession",
435 GDBUS_ARGS({ "session", "o" }), NULL,
437 { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
438 NULL, GDBUS_ARGS({ "path", "o" },
439 { "settings", "a{sv}" },
441 request_private_network) },
442 { GDBUS_METHOD("ReleasePrivateNetwork",
443 GDBUS_ARGS({ "path", "o" }), NULL,
444 release_private_network) },
448 static const GDBusSignalTable manager_signals[] = {
449 { GDBUS_SIGNAL("PropertyChanged",
450 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
451 { GDBUS_SIGNAL("TechnologyAdded",
452 GDBUS_ARGS({ "path", "o" },
453 { "properties", "a{sv}" })) },
454 { GDBUS_SIGNAL("TechnologyRemoved",
455 GDBUS_ARGS({ "path", "o" })) },
456 { GDBUS_SIGNAL("ServicesChanged",
457 GDBUS_ARGS({ "changed", "a(oa{sv})" },
458 { "removed", "ao" })) },
462 int __connman_manager_init(void)
466 connection = connman_dbus_get_connection();
467 if (connection == NULL)
470 if (connman_notifier_register(&technology_notifier) < 0)
471 connman_error("Failed to register technology notifier");
473 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
474 CONNMAN_MANAGER_INTERFACE,
476 manager_signals, NULL, NULL, NULL);
478 connman_state_idle = TRUE;
483 void __connman_manager_cleanup(void)
487 if (connection == NULL)
490 if (session_mode_pending != NULL)
491 dbus_message_unref(session_mode_pending);
493 connman_notifier_unregister(&technology_notifier);
495 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
496 CONNMAN_MANAGER_INTERFACE);
498 dbus_connection_unref(connection);