Register connection interface
[framework/connectivity/connman.git] / src / connection.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusMessage *get_properties(DBusConnection *conn,
31                                         DBusMessage *msg, void *data)
32 {
33         DBusMessage *reply;
34         DBusMessageIter array, dict;
35
36         DBG("conn %p", conn);
37
38         reply = dbus_message_new_method_return(msg);
39         if (reply == NULL)
40                 return NULL;
41
42         dbus_message_iter_init_append(reply, &array);
43
44         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
45                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
46                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
47                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
48
49         dbus_message_iter_close_container(&array, &dict);
50
51         return reply;
52 }
53
54 static DBusMessage *set_property(DBusConnection *conn,
55                                         DBusMessage *msg, void *data)
56 {
57         DBusMessageIter iter, value;
58         const char *name;
59
60         DBG("conn %p", conn);
61
62         if (dbus_message_iter_init(msg, &iter) == FALSE)
63                 return __connman_error_invalid_arguments(msg);
64
65         dbus_message_iter_get_basic(&iter, &name);
66         dbus_message_iter_next(&iter);
67         dbus_message_iter_recurse(&iter, &value);
68
69         if (__connman_security_check_privileges(msg) < 0)
70                 return __connman_error_permission_denied(msg);
71
72         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
73 }
74
75 static GDBusMethodTable connection_methods[] = {
76         { "GetProperties", "",   "a{sv}", get_properties },
77         { "SetProperty",   "sv", "",      set_property   },
78         { },
79 };
80
81 static GDBusSignalTable connection_signals[] = {
82         { "PropertyChanged", "sv" },
83         { },
84 };
85
86 static DBusConnection *connection;
87
88 static void emit_connections_signal(void)
89 {
90 }
91
92 static int register_interface(struct connman_element *element)
93 {
94         DBG("element %p name %s", element, element->name);
95
96         if (g_dbus_register_interface(connection, element->path,
97                                         CONNMAN_CONNECTION_INTERFACE,
98                                         connection_methods, connection_signals,
99                                         NULL, element, NULL) == FALSE) {
100                 connman_error("Failed to register %s connection", element->path);
101                 return -EIO;
102         }
103
104         emit_connections_signal();
105
106         return 0;
107 }
108
109 static void unregister_interface(struct connman_element *element)
110 {
111         DBG("element %p name %s", element, element->name);
112
113         emit_connections_signal();
114
115         g_dbus_unregister_interface(connection, element->path,
116                                                 CONNMAN_CONNECTION_INTERFACE);
117 }
118
119 static int connection_probe(struct connman_element *element)
120 {
121         DBG("element %p name %s", element, element->name);
122
123         return register_interface(element);
124 }
125
126 static void connection_remove(struct connman_element *element)
127 {
128         DBG("element %p name %s", element, element->name);
129
130         unregister_interface(element);
131 }
132
133 static struct connman_driver connection_driver = {
134         .name           = "connection",
135         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
136         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
137         .probe          = connection_probe,
138         .remove         = connection_remove,
139 };
140
141 int __connman_connection_init(void)
142 {
143         DBG("");
144
145         connection = connman_dbus_get_connection();
146
147         return connman_driver_register(&connection_driver);
148 }
149
150 void __connman_connection_cleanup(void)
151 {
152         DBG("");
153
154         connman_driver_unregister(&connection_driver);
155
156         dbus_connection_unref(connection);
157 }