5 * Copyright (C) 2007-2013 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 #include <connman/agent.h>
34 static bool connman_state_idle;
35 static dbus_bool_t sessionmode;
37 static DBusMessage *get_properties(DBusConnection *conn,
38 DBusMessage *msg, void *data)
41 DBusMessageIter array, dict;
42 dbus_bool_t offlinemode;
47 reply = dbus_message_new_method_return(msg);
51 dbus_message_iter_init_append(reply, &array);
53 connman_dbus_dict_open(&array, &dict);
55 str = __connman_notifier_get_state();
56 connman_dbus_dict_append_basic(&dict, "State",
57 DBUS_TYPE_STRING, &str);
59 offlinemode = __connman_technology_get_offlinemode();
60 connman_dbus_dict_append_basic(&dict, "OfflineMode",
61 DBUS_TYPE_BOOLEAN, &offlinemode);
63 connman_dbus_dict_append_basic(&dict, "SessionMode",
67 connman_dbus_dict_close(&array, &dict);
72 static DBusMessage *set_property(DBusConnection *conn,
73 DBusMessage *msg, void *data)
75 DBusMessageIter iter, value;
81 if (!dbus_message_iter_init(msg, &iter))
82 return __connman_error_invalid_arguments(msg);
84 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
85 return __connman_error_invalid_arguments(msg);
87 dbus_message_iter_get_basic(&iter, &name);
88 dbus_message_iter_next(&iter);
90 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
91 return __connman_error_invalid_arguments(msg);
93 dbus_message_iter_recurse(&iter, &value);
95 type = dbus_message_iter_get_arg_type(&value);
97 if (g_str_equal(name, "OfflineMode")) {
98 dbus_bool_t offlinemode;
100 if (type != DBUS_TYPE_BOOLEAN)
101 return __connman_error_invalid_arguments(msg);
103 dbus_message_iter_get_basic(&value, &offlinemode);
107 if (connman_dbus_get_connection_unix_user_sync(conn,
108 dbus_message_get_sender(msg),
110 DBG("Can not get unix user id!");
111 return __connman_error_permission_denied(msg);
114 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
115 DBG("Not allow this user to turn on offlinemode now!");
116 return __connman_error_permission_denied(msg);
119 __connman_technology_set_offlinemode(offlinemode);
120 } else if (g_str_equal(name, "SessionMode")) {
122 if (type != DBUS_TYPE_BOOLEAN)
123 return __connman_error_invalid_arguments(msg);
125 dbus_message_iter_get_basic(&value, &sessionmode);
128 return __connman_error_invalid_property(msg);
130 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
133 static void append_technology_structs(DBusMessageIter *iter, void *user_data)
135 __connman_technology_list_struct(iter);
138 static DBusMessage *get_technologies(DBusConnection *conn,
139 DBusMessage *msg, void *data)
145 reply = dbus_message_new_method_return(msg);
149 __connman_dbus_append_objpath_dict_array(reply,
150 append_technology_structs, NULL);
155 static DBusMessage *remove_provider(DBusConnection *conn,
156 DBusMessage *msg, void *data)
161 DBG("conn %p", conn);
163 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
166 err = __connman_provider_remove_by_path(path);
168 return __connman_error_failed(msg, -err);
170 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
173 static DBusConnection *connection = NULL;
175 static void idle_state(bool idle)
178 DBG("idle %d", idle);
180 connman_state_idle = idle;
182 if (!connman_state_idle)
186 static struct connman_notifier technology_notifier = {
188 .priority = CONNMAN_NOTIFIER_PRIORITY_HIGH,
189 .idle_state = idle_state,
192 static void append_service_structs(DBusMessageIter *iter, void *user_data)
194 __connman_service_list_struct(iter);
197 static DBusMessage *get_services(DBusConnection *conn,
198 DBusMessage *msg, void *data)
202 reply = dbus_message_new_method_return(msg);
206 __connman_dbus_append_objpath_dict_array(reply,
207 append_service_structs, NULL);
212 static void append_peer_structs(DBusMessageIter *iter, void *user_data)
214 __connman_peer_list_struct(iter);
217 static DBusMessage *get_peers(DBusConnection *conn,
218 DBusMessage *msg, void *data)
222 reply = dbus_message_new_method_return(msg);
226 __connman_dbus_append_objpath_dict_array(reply,
227 append_peer_structs, NULL);
231 static DBusMessage *connect_provider(DBusConnection *conn,
232 DBusMessage *msg, void *data)
236 DBG("conn %p", conn);
238 err = __connman_provider_create_and_connect(msg);
240 return __connman_error_failed(msg, -err);
245 static DBusMessage *register_agent(DBusConnection *conn,
246 DBusMessage *msg, void *data)
248 const char *sender, *path;
251 DBG("conn %p", conn);
253 sender = dbus_message_get_sender(msg);
255 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
258 err = connman_agent_register(sender, path);
260 return __connman_error_failed(msg, -err);
262 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
265 static DBusMessage *unregister_agent(DBusConnection *conn,
266 DBusMessage *msg, void *data)
268 const char *sender, *path;
271 DBG("conn %p", conn);
273 sender = dbus_message_get_sender(msg);
275 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
278 err = connman_agent_unregister(sender, path);
280 return __connman_error_failed(msg, -err);
282 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
285 static DBusMessage *register_counter(DBusConnection *conn,
286 DBusMessage *msg, void *data)
288 const char *sender, *path;
289 unsigned int accuracy, period;
292 DBG("conn %p", conn);
294 sender = dbus_message_get_sender(msg);
296 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
297 DBUS_TYPE_UINT32, &accuracy,
298 DBUS_TYPE_UINT32, &period,
301 /* FIXME: add handling of accuracy parameter */
303 err = __connman_counter_register(sender, path, period);
305 return __connman_error_failed(msg, -err);
307 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
310 static DBusMessage *unregister_counter(DBusConnection *conn,
311 DBusMessage *msg, void *data)
313 const char *sender, *path;
316 DBG("conn %p", conn);
318 sender = dbus_message_get_sender(msg);
320 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
323 err = __connman_counter_unregister(sender, path);
325 return __connman_error_failed(msg, -err);
327 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
330 static DBusMessage *create_session(DBusConnection *conn,
331 DBusMessage *msg, void *data)
335 DBG("conn %p", conn);
337 err = __connman_session_create(msg);
339 if (err == -EINPROGRESS)
342 return __connman_error_failed(msg, -err);
345 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
348 static DBusMessage *destroy_session(DBusConnection *conn,
349 DBusMessage *msg, void *data)
353 DBG("conn %p", conn);
355 err = __connman_session_destroy(msg);
357 return __connman_error_failed(msg, -err);
359 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
362 static DBusMessage *request_private_network(DBusConnection *conn,
363 DBusMessage *msg, void *data)
368 DBG("conn %p", conn);
370 sender = dbus_message_get_sender(msg);
372 err = __connman_private_network_request(msg, sender);
374 return __connman_error_failed(msg, -err);
379 static DBusMessage *release_private_network(DBusConnection *conn,
380 DBusMessage *msg, void *data)
385 DBG("conn %p", conn);
387 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
390 err = __connman_private_network_release(path);
392 return __connman_error_failed(msg, -err);
394 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
397 static int parse_peers_service_specs(DBusMessageIter *array,
398 const unsigned char **spec, int *spec_len,
399 const unsigned char **query, int *query_len,
402 *spec = *query = NULL;
403 *spec_len = *query_len = *version = 0;
405 while (dbus_message_iter_get_arg_type(array) ==
406 DBUS_TYPE_DICT_ENTRY) {
407 DBusMessageIter entry, inter, value;
410 dbus_message_iter_recurse(array, &entry);
411 dbus_message_iter_get_basic(&entry, &key);
413 dbus_message_iter_next(&entry);
415 dbus_message_iter_recurse(&entry, &inter);
417 if (!g_strcmp0(key, "BonjourResponse")) {
418 dbus_message_iter_recurse(&inter, &value);
419 dbus_message_iter_get_fixed_array(&value,
421 } else if (!g_strcmp0(key, "BonjourQuery")) {
422 dbus_message_iter_recurse(&inter, &value);
423 dbus_message_iter_get_fixed_array(&value,
425 } else if (!g_strcmp0(key, "UpnpService")) {
426 dbus_message_iter_get_basic(&inter, spec);
427 *spec_len = strlen((const char *)*spec)+1;
428 } else if (!g_strcmp0(key, "UpnpVersion")) {
429 dbus_message_iter_get_basic(&inter, version);
430 } else if (!g_strcmp0(key, "WiFiDisplayIEs")) {
434 dbus_message_iter_recurse(&inter, &value);
435 dbus_message_iter_get_fixed_array(&value,
440 dbus_message_iter_next(array);
443 if ((*query && !*spec && !*version) ||
444 (!*spec && !*query) || (!*spec && *version))
450 static DBusMessage *register_peer_service(DBusConnection *conn,
451 DBusMessage *msg, void *data)
453 const unsigned char *spec, *query;
454 DBusMessageIter iter, array;
455 int spec_len, query_len;
463 owner = dbus_message_get_sender(msg);
465 dbus_message_iter_init(msg, &iter);
466 dbus_message_iter_recurse(&iter, &array);
468 ret = parse_peers_service_specs(&array, &spec, &spec_len,
469 &query, &query_len, &version);
473 dbus_message_iter_next(&iter);
474 dbus_message_iter_get_basic(&iter, &master);
476 ret = __connman_peer_service_register(owner, msg, spec, spec_len,
477 query, query_len, version,master);
479 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
480 if (ret == -EINPROGRESS)
483 return __connman_error_failed(msg, -ret);
486 static DBusMessage *unregister_peer_service(DBusConnection *conn,
487 DBusMessage *msg, void *data)
489 const unsigned char *spec, *query;
490 DBusMessageIter iter, array;
491 int spec_len, query_len;
498 owner = dbus_message_get_sender(msg);
500 dbus_message_iter_init(msg, &iter);
501 dbus_message_iter_recurse(&iter, &array);
503 ret = parse_peers_service_specs(&array, &spec, &spec_len,
504 &query, &query_len, &version);
508 ret = __connman_peer_service_unregister(owner, spec, spec_len,
509 query, query_len, version);
511 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
513 return __connman_error_failed(msg, -ret);
517 static const GDBusMethodTable manager_methods[] = {
518 { GDBUS_METHOD("GetProperties",
519 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
521 { GDBUS_ASYNC_METHOD("SetProperty",
522 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
523 NULL, set_property) },
524 { GDBUS_METHOD("GetTechnologies",
525 NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
527 { GDBUS_DEPRECATED_METHOD("RemoveProvider",
528 GDBUS_ARGS({ "provider", "o" }), NULL,
530 { GDBUS_METHOD("GetServices",
531 NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
533 { GDBUS_METHOD("GetPeers",
534 NULL, GDBUS_ARGS({ "peers", "a(oa{sv})" }),
536 { GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
537 GDBUS_ARGS({ "provider", "a{sv}" }),
538 GDBUS_ARGS({ "path", "o" }),
540 { GDBUS_METHOD("RegisterAgent",
541 GDBUS_ARGS({ "path", "o" }), NULL,
543 { GDBUS_METHOD("UnregisterAgent",
544 GDBUS_ARGS({ "path", "o" }), NULL,
546 { GDBUS_METHOD("RegisterCounter",
547 GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
549 NULL, register_counter) },
550 { GDBUS_METHOD("UnregisterCounter",
551 GDBUS_ARGS({ "path", "o" }), NULL,
552 unregister_counter) },
553 { GDBUS_ASYNC_METHOD("CreateSession",
554 GDBUS_ARGS({ "settings", "a{sv}" },
555 { "notifier", "o" }),
556 GDBUS_ARGS({ "session", "o" }),
558 { GDBUS_METHOD("DestroySession",
559 GDBUS_ARGS({ "session", "o" }), NULL,
561 { GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
562 NULL, GDBUS_ARGS({ "path", "o" },
563 { "settings", "a{sv}" },
565 request_private_network) },
566 { GDBUS_METHOD("ReleasePrivateNetwork",
567 GDBUS_ARGS({ "path", "o" }), NULL,
568 release_private_network) },
569 { GDBUS_ASYNC_METHOD("RegisterPeerService",
570 GDBUS_ARGS({ "specification", "a{sv}" },
571 { "master", "b" }), NULL,
572 register_peer_service) },
573 { GDBUS_METHOD("UnregisterPeerService",
574 GDBUS_ARGS({ "specification", "a{sv}" }), NULL,
575 unregister_peer_service) },
579 static const GDBusSignalTable manager_signals[] = {
580 { GDBUS_SIGNAL("PropertyChanged",
581 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
582 { GDBUS_SIGNAL("TechnologyAdded",
583 GDBUS_ARGS({ "path", "o" },
584 { "properties", "a{sv}" })) },
585 { GDBUS_SIGNAL("TechnologyRemoved",
586 GDBUS_ARGS({ "path", "o" })) },
587 { GDBUS_SIGNAL("ServicesChanged",
588 GDBUS_ARGS({ "changed", "a(oa{sv})" },
589 { "removed", "ao" })) },
590 { GDBUS_SIGNAL("PeersChanged",
591 GDBUS_ARGS({ "changed", "a(oa{sv})" },
592 { "removed", "ao" })) },
596 int __connman_manager_init(void)
600 connection = connman_dbus_get_connection();
604 if (connman_notifier_register(&technology_notifier) < 0)
605 connman_error("Failed to register technology notifier");
607 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
608 CONNMAN_MANAGER_INTERFACE,
610 manager_signals, NULL, NULL, NULL);
612 connman_state_idle = true;
617 void __connman_manager_cleanup(void)
624 connman_notifier_unregister(&technology_notifier);
626 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
627 CONNMAN_MANAGER_INTERFACE);
629 dbus_connection_unref(connection);