manager: Remove support for GetState method and StateChanged signal
[platform/upstream/connman.git] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <errno.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 connman_bool_t connman_state_idle;
33 DBusMessage *session_mode_pending = NULL;
34
35 static DBusMessage *get_properties(DBusConnection *conn,
36                                         DBusMessage *msg, void *data)
37 {
38         DBusMessage *reply;
39         DBusMessageIter array, dict;
40         connman_bool_t offlinemode, sessionmode;
41         const char *str;
42
43         DBG("conn %p", conn);
44
45         reply = dbus_message_new_method_return(msg);
46         if (reply == NULL)
47                 return NULL;
48
49         dbus_message_iter_init_append(reply, &array);
50
51         connman_dbus_dict_open(&array, &dict);
52
53         connman_dbus_dict_append_array(&dict, "Services",
54                         DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
55         connman_dbus_dict_append_array(&dict, "Technologies",
56                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
57
58         str = __connman_notifier_get_state();
59         connman_dbus_dict_append_basic(&dict, "State",
60                                                 DBUS_TYPE_STRING, &str);
61
62         offlinemode = __connman_technology_get_offlinemode();
63         connman_dbus_dict_append_basic(&dict, "OfflineMode",
64                                         DBUS_TYPE_BOOLEAN, &offlinemode);
65
66         connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
67                 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
68         connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
69                 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
70         connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
71                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
72
73         str = __connman_service_default();
74         if (str != NULL)
75                 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
76                                                 DBUS_TYPE_STRING, &str);
77
78         connman_dbus_dict_append_array(&dict, "AvailableDebugs",
79                         DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
80         connman_dbus_dict_append_array(&dict, "EnabledDebugs",
81                         DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
82
83         sessionmode = __connman_session_mode();
84         connman_dbus_dict_append_basic(&dict, "SessionMode",
85                                         DBUS_TYPE_BOOLEAN,
86                                         &sessionmode);
87
88         connman_dbus_dict_close(&array, &dict);
89
90         return reply;
91 }
92
93 static DBusMessage *set_property(DBusConnection *conn,
94                                         DBusMessage *msg, void *data)
95 {
96         DBusMessageIter iter, value;
97         const char *name;
98         int type;
99
100         DBG("conn %p", conn);
101
102         if (dbus_message_iter_init(msg, &iter) == FALSE)
103                 return __connman_error_invalid_arguments(msg);
104
105         dbus_message_iter_get_basic(&iter, &name);
106         dbus_message_iter_next(&iter);
107         dbus_message_iter_recurse(&iter, &value);
108
109         type = dbus_message_iter_get_arg_type(&value);
110
111         if (g_str_equal(name, "OfflineMode") == TRUE) {
112                 connman_bool_t offlinemode;
113
114                 if (type != DBUS_TYPE_BOOLEAN)
115                         return __connman_error_invalid_arguments(msg);
116
117                 dbus_message_iter_get_basic(&value, &offlinemode);
118
119                 __connman_technology_set_offlinemode(offlinemode);
120         } else if (g_str_equal(name, "SessionMode") == TRUE) {
121                 connman_bool_t sessionmode;
122
123                 if (type != DBUS_TYPE_BOOLEAN)
124                         return __connman_error_invalid_arguments(msg);
125
126                 dbus_message_iter_get_basic(&value, &sessionmode);
127
128                 if (session_mode_pending != NULL)
129                         return __connman_error_in_progress(msg);
130
131                 __connman_session_set_mode(sessionmode);
132
133                 if (sessionmode == TRUE && connman_state_idle == FALSE) {
134                         session_mode_pending = msg;
135                         return NULL;
136                 }
137
138         } else
139                 return __connman_error_invalid_property(msg);
140
141         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
142 }
143
144 static DBusMessage *remove_provider(DBusConnection *conn,
145                                     DBusMessage *msg, void *data)
146 {
147         const char *path;
148         int err;
149
150         DBG("conn %p", conn);
151
152         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
153                                                         DBUS_TYPE_INVALID);
154
155         err = __connman_provider_remove(path);
156         if (err < 0)
157                 return __connman_error_failed(msg, -err);
158
159         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
160 }
161
162 static DBusMessage *request_scan(DBusConnection *conn,
163                                         DBusMessage *msg, void *data)
164 {
165         enum connman_service_type type;
166         const char *str;
167         int err;
168
169         DBG("conn %p", conn);
170
171         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
172                                                         DBUS_TYPE_INVALID);
173
174         if (g_strcmp0(str, "") == 0)
175                 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
176         else if (g_strcmp0(str, "wifi") == 0)
177                 type = CONNMAN_SERVICE_TYPE_WIFI;
178         else if (g_strcmp0(str, "wimax") == 0)
179                 type = CONNMAN_SERVICE_TYPE_WIMAX;
180         else
181                 return __connman_error_invalid_arguments(msg);
182
183         err = __connman_device_request_scan(type);
184         if (err < 0) {
185                 if (err == -EINPROGRESS) {
186                         connman_error("Invalid return code from scan");
187                         err = -EINVAL;
188                 }
189
190                 return __connman_error_failed(msg, -err);
191         }
192
193         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
194 }
195
196 static DBusConnection *connection = NULL;
197
198 static void session_mode_notify(void)
199 {
200         DBusMessage *reply;
201
202         reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
203         g_dbus_send_message(connection, reply);
204
205         dbus_message_unref(session_mode_pending);
206         session_mode_pending = NULL;
207 }
208
209 static void idle_state(connman_bool_t idle)
210 {
211
212         DBG("idle %d", idle);
213
214         connman_state_idle = idle;
215
216         if (connman_state_idle == FALSE || session_mode_pending == NULL)
217                 return;
218
219         session_mode_notify();
220 }
221
222 static struct connman_notifier technology_notifier = {
223         .name           = "manager",
224         .priority       = CONNMAN_NOTIFIER_PRIORITY_HIGH,
225         .idle_state     = idle_state,
226 };
227
228 static DBusMessage *enable_technology(DBusConnection *conn,
229                                         DBusMessage *msg, void *data)
230 {
231         enum connman_service_type type;
232         const char *str;
233
234         DBG("conn %p", conn);
235
236         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
237                                                         DBUS_TYPE_INVALID);
238
239         if (g_strcmp0(str, "ethernet") == 0)
240                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
241         else if (g_strcmp0(str, "wifi") == 0)
242                 type = CONNMAN_SERVICE_TYPE_WIFI;
243         else if (g_strcmp0(str, "wimax") == 0)
244                 type = CONNMAN_SERVICE_TYPE_WIMAX;
245         else if (g_strcmp0(str, "bluetooth") == 0)
246                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
247         else if (g_strcmp0(str, "cellular") == 0)
248                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
249         else
250                 return __connman_error_invalid_arguments(msg);
251
252         if (__connman_notifier_is_registered(type) == FALSE)
253                 return __connman_error_not_registered(msg);
254
255         if (__connman_notifier_is_enabled(type) == TRUE)
256                 return __connman_error_already_enabled(msg);
257
258          __connman_technology_enable(type, msg);
259
260         return NULL;
261 }
262
263 static DBusMessage *disable_technology(DBusConnection *conn,
264                                         DBusMessage *msg, void *data)
265 {
266         enum connman_service_type type;
267         const char *str;
268
269         DBG("conn %p", conn);
270
271         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
272                                                         DBUS_TYPE_INVALID);
273
274         if (g_strcmp0(str, "ethernet") == 0)
275                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
276         else if (g_strcmp0(str, "wifi") == 0)
277                 type = CONNMAN_SERVICE_TYPE_WIFI;
278         else if (g_strcmp0(str, "wimax") == 0)
279                 type = CONNMAN_SERVICE_TYPE_WIMAX;
280         else if (g_strcmp0(str, "bluetooth") == 0)
281                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
282         else if (g_strcmp0(str, "cellular") == 0)
283                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
284         else
285                 return __connman_error_invalid_arguments(msg);
286
287         if (__connman_notifier_is_registered(type) == FALSE)
288                 return __connman_error_not_registered(msg);
289
290         if (__connman_notifier_is_enabled(type) == FALSE)
291                 return __connman_error_already_disabled(msg);
292
293         __connman_technology_disable(type, msg);
294
295         return NULL;
296 }
297
298 static DBusMessage *get_services(DBusConnection *conn,
299                                         DBusMessage *msg, void *data)
300 {
301         DBusMessage *reply;
302         DBusMessageIter iter, array;
303
304         reply = dbus_message_new_method_return(msg);
305         if (reply == NULL)
306                 return NULL;
307
308         dbus_message_iter_init_append(reply, &iter);
309
310         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
311                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
312                         DBUS_TYPE_OBJECT_PATH_AS_STRING
313                         DBUS_TYPE_ARRAY_AS_STRING
314                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
315                                         DBUS_TYPE_STRING_AS_STRING
316                                         DBUS_TYPE_VARIANT_AS_STRING
317                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
318                         DBUS_STRUCT_END_CHAR_AS_STRING, &array);
319
320         __connman_service_list_struct(&array);
321
322         dbus_message_iter_close_container(&iter, &array);
323
324         return reply;
325 }
326
327 static DBusMessage *connect_provider(DBusConnection *conn,
328                                         DBusMessage *msg, void *data)
329 {
330         int err;
331
332         DBG("conn %p", conn);
333
334         if (__connman_session_mode() == TRUE) {
335                 connman_info("Session mode enabled: "
336                                 "direct provider connect disabled");
337
338                 return __connman_error_failed(msg, -EINVAL);
339         }
340
341         err = __connman_provider_create_and_connect(msg);
342         if (err < 0) {
343                 if (err == -EINPROGRESS) {
344                         connman_error("Invalid return code from connect");
345                         err = -EINVAL;
346                 }
347
348                 return __connman_error_failed(msg, -err);
349         }
350
351         return NULL;
352 }
353
354 static DBusMessage *register_agent(DBusConnection *conn,
355                                         DBusMessage *msg, void *data)
356 {
357         const char *sender, *path;
358         int err;
359
360         DBG("conn %p", conn);
361
362         sender = dbus_message_get_sender(msg);
363
364         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
365                                                         DBUS_TYPE_INVALID);
366
367         err = __connman_agent_register(sender, path);
368         if (err < 0)
369                 return __connman_error_failed(msg, -err);
370
371         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
372 }
373
374 static DBusMessage *unregister_agent(DBusConnection *conn,
375                                         DBusMessage *msg, void *data)
376 {
377         const char *sender, *path;
378         int err;
379
380         DBG("conn %p", conn);
381
382         sender = dbus_message_get_sender(msg);
383
384         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
385                                                         DBUS_TYPE_INVALID);
386
387         err = __connman_agent_unregister(sender, path);
388         if (err < 0)
389                 return __connman_error_failed(msg, -err);
390
391         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
392 }
393
394 static DBusMessage *register_counter(DBusConnection *conn,
395                                         DBusMessage *msg, void *data)
396 {
397         const char *sender, *path;
398         unsigned int accuracy, period;
399         int err;
400
401         DBG("conn %p", conn);
402
403         sender = dbus_message_get_sender(msg);
404
405         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
406                                                 DBUS_TYPE_UINT32, &accuracy,
407                                                 DBUS_TYPE_UINT32, &period,
408                                                         DBUS_TYPE_INVALID);
409
410         /* FIXME: add handling of accuracy parameter */
411
412         err = __connman_counter_register(sender, path, period);
413         if (err < 0)
414                 return __connman_error_failed(msg, -err);
415
416         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
417 }
418
419 static DBusMessage *unregister_counter(DBusConnection *conn,
420                                         DBusMessage *msg, void *data)
421 {
422         const char *sender, *path;
423         int err;
424
425         DBG("conn %p", conn);
426
427         sender = dbus_message_get_sender(msg);
428
429         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
430                                                         DBUS_TYPE_INVALID);
431
432         err = __connman_counter_unregister(sender, path);
433         if (err < 0)
434                 return __connman_error_failed(msg, -err);
435
436         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
437 }
438
439 static DBusMessage *create_session(DBusConnection *conn,
440                                         DBusMessage *msg, void *data)
441 {
442         int err;
443
444         DBG("conn %p", conn);
445
446         err = __connman_session_create(msg);
447         if (err < 0)
448                 return __connman_error_failed(msg, -err);
449
450         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
451 }
452
453 static DBusMessage *destroy_session(DBusConnection *conn,
454                                         DBusMessage *msg, void *data)
455 {
456         int err;
457
458         DBG("conn %p", conn);
459
460         err = __connman_session_destroy(msg);
461         if (err < 0)
462                 return __connman_error_failed(msg, -err);
463
464         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
465 }
466
467 static DBusMessage *request_private_network(DBusConnection *conn,
468                                         DBusMessage *msg, void *data)
469 {
470         const char *sender;
471         int  err;
472
473         DBG("conn %p", conn);
474
475         sender = dbus_message_get_sender(msg);
476
477         err = __connman_private_network_request(msg, sender);
478         if (err < 0)
479                 return __connman_error_failed(msg, -err);
480
481         return NULL;
482 }
483
484 static DBusMessage *release_private_network(DBusConnection *conn,
485                                         DBusMessage *msg, void *data)
486 {
487         const char *path;
488         int err;
489
490         DBG("conn %p", conn);
491
492         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
493                                                         DBUS_TYPE_INVALID);
494
495         err = __connman_private_network_release(path);
496         if (err < 0)
497                 return __connman_error_failed(msg, -err);
498
499         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
500 }
501
502 static GDBusMethodTable manager_methods[] = {
503         { "GetProperties",     "",      "a{sv}", get_properties     },
504         { "SetProperty",       "sv",    "",      set_property,
505                                                 G_DBUS_METHOD_FLAG_ASYNC },
506         { "RemoveProvider",    "o",     "",      remove_provider    },
507         { "RequestScan",       "s",     "",      request_scan       },
508         { "EnableTechnology",  "s",     "",      enable_technology,
509                                                 G_DBUS_METHOD_FLAG_ASYNC },
510         { "DisableTechnology", "s",     "",      disable_technology,
511                                                 G_DBUS_METHOD_FLAG_ASYNC },
512         { "GetServices",       "",      "a(oa{sv})", get_services   },
513         { "ConnectProvider",   "a{sv}", "o",     connect_provider,
514                                                 G_DBUS_METHOD_FLAG_ASYNC },
515         { "RegisterAgent",     "o",     "",      register_agent     },
516         { "UnregisterAgent",   "o",     "",      unregister_agent   },
517         { "RegisterCounter",   "ouu",   "",      register_counter   },
518         { "UnregisterCounter", "o",     "",      unregister_counter },
519         { "CreateSession",     "a{sv}o", "o",    create_session     },
520         { "DestroySession",    "o",     "",      destroy_session    },
521         { "RequestPrivateNetwork",    "",     "oa{sv}h",
522                                                 request_private_network,
523                                                 G_DBUS_METHOD_FLAG_ASYNC },
524         { "ReleasePrivateNetwork",    "o",    "",
525                                                 release_private_network },
526         { },
527 };
528
529 static GDBusSignalTable manager_signals[] = {
530         { "PropertyChanged", "sv" },
531         { },
532 };
533
534 int __connman_manager_init(void)
535 {
536         DBG("");
537
538         connection = connman_dbus_get_connection();
539         if (connection == NULL)
540                 return -1;
541
542         if (connman_notifier_register(&technology_notifier) < 0)
543                 connman_error("Failed to register technology notifier");
544
545         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
546                                         CONNMAN_MANAGER_INTERFACE,
547                                         manager_methods,
548                                         manager_signals, NULL, NULL, NULL);
549
550         connman_state_idle = TRUE;
551
552         return 0;
553 }
554
555 void __connman_manager_cleanup(void)
556 {
557         DBG("");
558
559         if (connection == NULL)
560                 return;
561
562         connman_notifier_unregister(&technology_notifier);
563
564         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
565                                                 CONNMAN_MANAGER_INTERFACE);
566
567         dbus_connection_unref(connection);
568 }