27f6fc5a28e5ea32df34d204e76173e762eccdb3
[connman] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 *register_agent(DBusConnection *conn,
31                                         DBusMessage *msg, void *data)
32 {
33         DBusMessage *reply;
34         const char *sender, *path;
35
36         DBG("conn %p", conn);
37
38         sender = dbus_message_get_sender(msg);
39
40         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
41                                                         DBUS_TYPE_INVALID);
42
43         reply = dbus_message_new_method_return(msg);
44         if (reply == NULL)
45                 return NULL;
46
47         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
48
49         __connman_agent_register(sender, path);
50
51         return reply;
52 }
53
54 static DBusMessage *unregister_agent(DBusConnection *conn,
55                                         DBusMessage *msg, void *data)
56 {
57         DBusMessage *reply;
58         const char *sender, *path;
59
60         DBG("conn %p", conn);
61
62         sender = dbus_message_get_sender(msg);
63
64         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
65                                                         DBUS_TYPE_INVALID);
66
67         reply = dbus_message_new_method_return(msg);
68         if (reply == NULL)
69                 return NULL;
70
71         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
72
73         __connman_agent_unregister(sender, path);
74
75         return reply;
76 }
77
78 static DBusMessage *list_profiles(DBusConnection *conn,
79                                         DBusMessage *msg, void *data)
80 {
81         DBusMessage *reply;
82         DBusMessageIter array, iter;
83
84         DBG("conn %p", conn);
85
86         reply = dbus_message_new_method_return(msg);
87         if (reply == NULL)
88                 return NULL;
89
90         dbus_message_iter_init_append(reply, &array);
91
92         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
93                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
94
95         __connman_profile_list(&iter);
96
97         dbus_message_iter_close_container(&array, &iter);
98
99         return reply;
100 }
101
102 static DBusMessage *list_elements(DBusConnection *conn,
103                                         DBusMessage *msg, void *data)
104 {
105         DBusMessage *reply;
106         DBusMessageIter array, iter;
107
108         DBG("conn %p", conn);
109
110         reply = dbus_message_new_method_return(msg);
111         if (reply == NULL)
112                 return NULL;
113
114         dbus_message_iter_init_append(reply, &array);
115
116         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
117                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
118
119         __connman_element_list(CONNMAN_ELEMENT_TYPE_UNKNOWN, &iter);
120
121         dbus_message_iter_close_container(&array, &iter);
122
123         return reply;
124 }
125
126 static GDBusMethodTable manager_methods[] = {
127         { "RegisterAgent",   "o", "",   register_agent   },
128         { "UnregisterAgent", "o", "",   unregister_agent },
129         { "ListProfiles",    "",  "ao", list_profiles    },
130         { "ListElements",    "",  "ao", list_elements    },
131         { },
132 };
133
134 static GDBusSignalTable manager_signals[] = {
135         { "ElementAdded",   "o" },
136         { "ElementUpdated", "o" },
137         { "ElementRemoved", "o" },
138         { },
139 };
140
141 static DBusMessage *nm_sleep(DBusConnection *conn,
142                                         DBusMessage *msg, void *data)
143 {
144         DBusMessage *reply;
145
146         DBG("conn %p", conn);
147
148         reply = dbus_message_new_method_return(msg);
149         if (reply == NULL)
150                 return NULL;
151
152         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
153
154         return reply;
155 }
156
157 static DBusMessage *nm_wake(DBusConnection *conn,
158                                         DBusMessage *msg, void *data)
159 {
160         DBusMessage *reply;
161
162         DBG("conn %p", conn);
163
164         reply = dbus_message_new_method_return(msg);
165         if (reply == NULL)
166                 return NULL;
167
168         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
169
170         return reply;
171 }
172
173 enum {
174         NM_STATE_UNKNOWN = 0,
175         NM_STATE_ASLEEP,
176         NM_STATE_CONNECTING,
177         NM_STATE_CONNECTED,
178         NM_STATE_DISCONNECTED
179 };
180
181 static DBusMessage *nm_state(DBusConnection *conn,
182                                         DBusMessage *msg, void *data)
183 {
184         DBusMessage *reply;
185         dbus_uint32_t state;
186
187         DBG("conn %p", conn);
188
189         reply = dbus_message_new_method_return(msg);
190         if (reply == NULL)
191                 return NULL;
192
193         state = NM_STATE_DISCONNECTED;
194
195         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
196                                                         DBUS_TYPE_INVALID);
197
198         return reply;
199 }
200
201 static GDBusMethodTable nm_methods[] = {
202         { "sleep", "",  "",   nm_sleep        },
203         { "wake",  "",  "",   nm_wake         },
204         { "state", "",  "u",  nm_state        },
205         { },
206 };
207
208 static DBusConnection *connection = NULL;
209 static gboolean nm_compat = FALSE;
210
211 int __connman_manager_init(DBusConnection *conn, gboolean compat)
212 {
213         DBG("conn %p", conn);
214
215         connection = dbus_connection_ref(conn);
216         if (connection == NULL)
217                 return -1;
218
219         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
220                                         CONNMAN_MANAGER_INTERFACE,
221                                         manager_methods,
222                                         manager_signals, NULL, NULL, NULL);
223
224         if (compat == TRUE) {
225                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
226                                         nm_methods, NULL, NULL, NULL, NULL);
227
228                 nm_compat = TRUE;
229         }
230
231         return 0;
232 }
233
234 void __connman_manager_cleanup(void)
235 {
236         DBG("conn %p", connection);
237
238         if (nm_compat == TRUE) {
239                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
240         }
241
242         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
243                                                 CONNMAN_MANAGER_INTERFACE);
244
245         dbus_connection_unref(connection);
246 }