manager: Remove support for LookupService method
[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 *get_state(DBusConnection *conn,
145                                         DBusMessage *msg, void *data)
146 {
147         const char *str;
148
149         DBG("conn %p", conn);
150
151         str = __connman_notifier_get_state();
152
153         return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
154                                                 DBUS_TYPE_INVALID);
155 }
156
157 static DBusMessage *remove_provider(DBusConnection *conn,
158                                     DBusMessage *msg, void *data)
159 {
160         const char *path;
161         int err;
162
163         DBG("conn %p", conn);
164
165         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
166                                                         DBUS_TYPE_INVALID);
167
168         err = __connman_provider_remove(path);
169         if (err < 0)
170                 return __connman_error_failed(msg, -err);
171
172         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
173 }
174
175 static DBusMessage *request_scan(DBusConnection *conn,
176                                         DBusMessage *msg, void *data)
177 {
178         enum connman_service_type type;
179         const char *str;
180         int err;
181
182         DBG("conn %p", conn);
183
184         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
185                                                         DBUS_TYPE_INVALID);
186
187         if (g_strcmp0(str, "") == 0)
188                 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
189         else if (g_strcmp0(str, "wifi") == 0)
190                 type = CONNMAN_SERVICE_TYPE_WIFI;
191         else if (g_strcmp0(str, "wimax") == 0)
192                 type = CONNMAN_SERVICE_TYPE_WIMAX;
193         else
194                 return __connman_error_invalid_arguments(msg);
195
196         err = __connman_device_request_scan(type);
197         if (err < 0) {
198                 if (err == -EINPROGRESS) {
199                         connman_error("Invalid return code from scan");
200                         err = -EINVAL;
201                 }
202
203                 return __connman_error_failed(msg, -err);
204         }
205
206         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
207 }
208
209 static DBusConnection *connection = NULL;
210
211 static void session_mode_notify(void)
212 {
213         DBusMessage *reply;
214
215         reply = g_dbus_create_reply(session_mode_pending, DBUS_TYPE_INVALID);
216         g_dbus_send_message(connection, reply);
217
218         dbus_message_unref(session_mode_pending);
219         session_mode_pending = NULL;
220 }
221
222 static void idle_state(connman_bool_t idle)
223 {
224
225         DBG("idle %d", idle);
226
227         connman_state_idle = idle;
228
229         if (connman_state_idle == FALSE || session_mode_pending == NULL)
230                 return;
231
232         session_mode_notify();
233 }
234
235 static struct connman_notifier technology_notifier = {
236         .name           = "manager",
237         .priority       = CONNMAN_NOTIFIER_PRIORITY_HIGH,
238         .idle_state     = idle_state,
239 };
240
241 static DBusMessage *enable_technology(DBusConnection *conn,
242                                         DBusMessage *msg, void *data)
243 {
244         enum connman_service_type type;
245         const char *str;
246
247         DBG("conn %p", conn);
248
249         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
250                                                         DBUS_TYPE_INVALID);
251
252         if (g_strcmp0(str, "ethernet") == 0)
253                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
254         else if (g_strcmp0(str, "wifi") == 0)
255                 type = CONNMAN_SERVICE_TYPE_WIFI;
256         else if (g_strcmp0(str, "wimax") == 0)
257                 type = CONNMAN_SERVICE_TYPE_WIMAX;
258         else if (g_strcmp0(str, "bluetooth") == 0)
259                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
260         else if (g_strcmp0(str, "cellular") == 0)
261                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
262         else
263                 return __connman_error_invalid_arguments(msg);
264
265         if (__connman_notifier_is_registered(type) == FALSE)
266                 return __connman_error_not_registered(msg);
267
268         if (__connman_notifier_is_enabled(type) == TRUE)
269                 return __connman_error_already_enabled(msg);
270
271          __connman_technology_enable(type, msg);
272
273         return NULL;
274 }
275
276 static DBusMessage *disable_technology(DBusConnection *conn,
277                                         DBusMessage *msg, void *data)
278 {
279         enum connman_service_type type;
280         const char *str;
281
282         DBG("conn %p", conn);
283
284         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
285                                                         DBUS_TYPE_INVALID);
286
287         if (g_strcmp0(str, "ethernet") == 0)
288                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
289         else if (g_strcmp0(str, "wifi") == 0)
290                 type = CONNMAN_SERVICE_TYPE_WIFI;
291         else if (g_strcmp0(str, "wimax") == 0)
292                 type = CONNMAN_SERVICE_TYPE_WIMAX;
293         else if (g_strcmp0(str, "bluetooth") == 0)
294                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
295         else if (g_strcmp0(str, "cellular") == 0)
296                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
297         else
298                 return __connman_error_invalid_arguments(msg);
299
300         if (__connman_notifier_is_registered(type) == FALSE)
301                 return __connman_error_not_registered(msg);
302
303         if (__connman_notifier_is_enabled(type) == FALSE)
304                 return __connman_error_already_disabled(msg);
305
306         __connman_technology_disable(type, msg);
307
308         return NULL;
309 }
310
311 static DBusMessage *get_services(DBusConnection *conn,
312                                         DBusMessage *msg, void *data)
313 {
314         DBusMessage *reply;
315         DBusMessageIter iter, array;
316
317         reply = dbus_message_new_method_return(msg);
318         if (reply == NULL)
319                 return NULL;
320
321         dbus_message_iter_init_append(reply, &iter);
322
323         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
324                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
325                         DBUS_TYPE_OBJECT_PATH_AS_STRING
326                         DBUS_TYPE_ARRAY_AS_STRING
327                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
328                                         DBUS_TYPE_STRING_AS_STRING
329                                         DBUS_TYPE_VARIANT_AS_STRING
330                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
331                         DBUS_STRUCT_END_CHAR_AS_STRING, &array);
332
333         __connman_service_list_struct(&array);
334
335         dbus_message_iter_close_container(&iter, &array);
336
337         return reply;
338 }
339
340 static DBusMessage *connect_service(DBusConnection *conn,
341                                         DBusMessage *msg, void *data)
342 {
343         int err;
344
345         DBG("conn %p", conn);
346
347         if (__connman_session_mode() == TRUE) {
348                 connman_info("Session mode enabled: "
349                                 "direct service connect disabled");
350
351                 return __connman_error_failed(msg, -EINVAL);
352         }
353
354         err = __connman_service_create_and_connect(msg);
355         if (err < 0) {
356                 if (err == -EINPROGRESS) {
357                         connman_error("Invalid return code from connect");
358                         err = -EINVAL;
359                 }
360
361                 return __connman_error_failed(msg, -err);
362         }
363
364         return NULL;
365 }
366
367 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
368                                         void *data)
369 {
370         int err;
371
372         DBG("conn %p", conn);
373
374         err = __connman_service_provision(msg);
375         if (err < 0)
376                 return __connman_error_failed(msg, -err);
377
378         return NULL;
379 }
380
381 static DBusMessage *connect_provider(DBusConnection *conn,
382                                         DBusMessage *msg, void *data)
383 {
384         int err;
385
386         DBG("conn %p", conn);
387
388         if (__connman_session_mode() == TRUE) {
389                 connman_info("Session mode enabled: "
390                                 "direct provider connect disabled");
391
392                 return __connman_error_failed(msg, -EINVAL);
393         }
394
395         err = __connman_provider_create_and_connect(msg);
396         if (err < 0) {
397                 if (err == -EINPROGRESS) {
398                         connman_error("Invalid return code from connect");
399                         err = -EINVAL;
400                 }
401
402                 return __connman_error_failed(msg, -err);
403         }
404
405         return NULL;
406 }
407
408 static DBusMessage *register_agent(DBusConnection *conn,
409                                         DBusMessage *msg, void *data)
410 {
411         const char *sender, *path;
412         int err;
413
414         DBG("conn %p", conn);
415
416         sender = dbus_message_get_sender(msg);
417
418         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
419                                                         DBUS_TYPE_INVALID);
420
421         err = __connman_agent_register(sender, path);
422         if (err < 0)
423                 return __connman_error_failed(msg, -err);
424
425         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
426 }
427
428 static DBusMessage *unregister_agent(DBusConnection *conn,
429                                         DBusMessage *msg, void *data)
430 {
431         const char *sender, *path;
432         int err;
433
434         DBG("conn %p", conn);
435
436         sender = dbus_message_get_sender(msg);
437
438         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
439                                                         DBUS_TYPE_INVALID);
440
441         err = __connman_agent_unregister(sender, path);
442         if (err < 0)
443                 return __connman_error_failed(msg, -err);
444
445         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
446 }
447
448 static DBusMessage *register_counter(DBusConnection *conn,
449                                         DBusMessage *msg, void *data)
450 {
451         const char *sender, *path;
452         unsigned int accuracy, period;
453         int err;
454
455         DBG("conn %p", conn);
456
457         sender = dbus_message_get_sender(msg);
458
459         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
460                                                 DBUS_TYPE_UINT32, &accuracy,
461                                                 DBUS_TYPE_UINT32, &period,
462                                                         DBUS_TYPE_INVALID);
463
464         /* FIXME: add handling of accuracy parameter */
465
466         err = __connman_counter_register(sender, path, period);
467         if (err < 0)
468                 return __connman_error_failed(msg, -err);
469
470         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
471 }
472
473 static DBusMessage *unregister_counter(DBusConnection *conn,
474                                         DBusMessage *msg, void *data)
475 {
476         const char *sender, *path;
477         int err;
478
479         DBG("conn %p", conn);
480
481         sender = dbus_message_get_sender(msg);
482
483         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
484                                                         DBUS_TYPE_INVALID);
485
486         err = __connman_counter_unregister(sender, path);
487         if (err < 0)
488                 return __connman_error_failed(msg, -err);
489
490         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
491 }
492
493 static DBusMessage *create_session(DBusConnection *conn,
494                                         DBusMessage *msg, void *data)
495 {
496         int err;
497
498         DBG("conn %p", conn);
499
500         err = __connman_session_create(msg);
501         if (err < 0)
502                 return __connman_error_failed(msg, -err);
503
504         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
505 }
506
507 static DBusMessage *destroy_session(DBusConnection *conn,
508                                         DBusMessage *msg, void *data)
509 {
510         int err;
511
512         DBG("conn %p", conn);
513
514         err = __connman_session_destroy(msg);
515         if (err < 0)
516                 return __connman_error_failed(msg, -err);
517
518         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
519 }
520
521 static DBusMessage *request_private_network(DBusConnection *conn,
522                                         DBusMessage *msg, void *data)
523 {
524         const char *sender;
525         int  err;
526
527         DBG("conn %p", conn);
528
529         sender = dbus_message_get_sender(msg);
530
531         err = __connman_private_network_request(msg, sender);
532         if (err < 0)
533                 return __connman_error_failed(msg, -err);
534
535         return NULL;
536 }
537
538 static DBusMessage *release_private_network(DBusConnection *conn,
539                                         DBusMessage *msg, void *data)
540 {
541         const char *path;
542         int err;
543
544         DBG("conn %p", conn);
545
546         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
547                                                         DBUS_TYPE_INVALID);
548
549         err = __connman_private_network_release(path);
550         if (err < 0)
551                 return __connman_error_failed(msg, -err);
552
553         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
554 }
555
556 static GDBusMethodTable manager_methods[] = {
557         { "GetProperties",     "",      "a{sv}", get_properties     },
558         { "SetProperty",       "sv",    "",      set_property,
559                                                 G_DBUS_METHOD_FLAG_ASYNC },
560         { "GetState",          "",      "s",     get_state          },
561         { "RemoveProvider",    "o",     "",      remove_provider    },
562         { "RequestScan",       "s",     "",      request_scan       },
563         { "EnableTechnology",  "s",     "",      enable_technology,
564                                                 G_DBUS_METHOD_FLAG_ASYNC },
565         { "DisableTechnology", "s",     "",      disable_technology,
566                                                 G_DBUS_METHOD_FLAG_ASYNC },
567         { "GetServices",       "",      "a(oa{sv})", get_services   },
568         { "ConnectService",    "a{sv}", "o",     connect_service,
569                                                 G_DBUS_METHOD_FLAG_ASYNC },
570         { "ProvisionService",  "s",     "",      provision_service,
571                                                 G_DBUS_METHOD_FLAG_ASYNC },
572         { "ConnectProvider",   "a{sv}", "o",     connect_provider,
573                                                 G_DBUS_METHOD_FLAG_ASYNC },
574         { "RegisterAgent",     "o",     "",      register_agent     },
575         { "UnregisterAgent",   "o",     "",      unregister_agent   },
576         { "RegisterCounter",   "ouu",   "",      register_counter   },
577         { "UnregisterCounter", "o",     "",      unregister_counter },
578         { "CreateSession",     "a{sv}o", "o",    create_session     },
579         { "DestroySession",    "o",     "",      destroy_session    },
580         { "RequestPrivateNetwork",    "",     "oa{sv}h",
581                                                 request_private_network,
582                                                 G_DBUS_METHOD_FLAG_ASYNC },
583         { "ReleasePrivateNetwork",    "o",    "",
584                                                 release_private_network },
585         { },
586 };
587
588 static GDBusSignalTable manager_signals[] = {
589         { "PropertyChanged", "sv" },
590         { "StateChanged",    "s"  },
591         { },
592 };
593
594 int __connman_manager_init(void)
595 {
596         DBG("");
597
598         connection = connman_dbus_get_connection();
599         if (connection == NULL)
600                 return -1;
601
602         if (connman_notifier_register(&technology_notifier) < 0)
603                 connman_error("Failed to register technology notifier");
604
605         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
606                                         CONNMAN_MANAGER_INTERFACE,
607                                         manager_methods,
608                                         manager_signals, NULL, NULL, NULL);
609
610         connman_state_idle = TRUE;
611
612         return 0;
613 }
614
615 void __connman_manager_cleanup(void)
616 {
617         DBG("");
618
619         if (connection == NULL)
620                 return;
621
622         connman_notifier_unregister(&technology_notifier);
623
624         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
625                                                 CONNMAN_MANAGER_INTERFACE);
626
627         dbus_connection_unref(connection);
628 }