manager: Add session mode
[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 <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         connman_bool_t offlinemode, sessionmode;
36         const char *str;
37
38         DBG("conn %p", conn);
39
40         reply = dbus_message_new_method_return(msg);
41         if (reply == NULL)
42                 return NULL;
43
44         dbus_message_iter_init_append(reply, &array);
45
46         connman_dbus_dict_open(&array, &dict);
47
48         str = __connman_profile_active_path();
49         if (str != NULL)
50                 connman_dbus_dict_append_basic(&dict, "ActiveProfile",
51                                                 DBUS_TYPE_OBJECT_PATH, &str);
52
53         connman_dbus_dict_append_array(&dict, "Profiles",
54                         DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
55         connman_dbus_dict_append_array(&dict, "Services",
56                         DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
57         connman_dbus_dict_append_array(&dict, "Technologies",
58                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
59
60         str = __connman_notifier_get_state();
61         connman_dbus_dict_append_basic(&dict, "State",
62                                                 DBUS_TYPE_STRING, &str);
63
64         offlinemode = __connman_profile_get_offlinemode();
65         connman_dbus_dict_append_basic(&dict, "OfflineMode",
66                                         DBUS_TYPE_BOOLEAN, &offlinemode);
67
68         connman_dbus_dict_append_array(&dict, "AvailableTechnologies",
69                 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
70         connman_dbus_dict_append_array(&dict, "EnabledTechnologies",
71                 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
72         connman_dbus_dict_append_array(&dict, "ConnectedTechnologies",
73                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
74
75         str = __connman_service_default();
76         if (str != NULL)
77                 connman_dbus_dict_append_basic(&dict, "DefaultTechnology",
78                                                 DBUS_TYPE_STRING, &str);
79
80         connman_dbus_dict_append_array(&dict, "AvailableDebugs",
81                         DBUS_TYPE_STRING, __connman_debug_list_available, NULL);
82         connman_dbus_dict_append_array(&dict, "EnabledDebugs",
83                         DBUS_TYPE_STRING, __connman_debug_list_enabled, NULL);
84
85         sessionmode = __connman_session_mode();
86         connman_dbus_dict_append_basic(&dict, "SessionMode",
87                                         DBUS_TYPE_BOOLEAN,
88                                         &sessionmode);
89
90         connman_dbus_dict_close(&array, &dict);
91
92         return reply;
93 }
94
95 static DBusMessage *set_property(DBusConnection *conn,
96                                         DBusMessage *msg, void *data)
97 {
98         DBusMessageIter iter, value;
99         const char *name;
100         int type;
101
102         DBG("conn %p", conn);
103
104         if (dbus_message_iter_init(msg, &iter) == FALSE)
105                 return __connman_error_invalid_arguments(msg);
106
107         dbus_message_iter_get_basic(&iter, &name);
108         dbus_message_iter_next(&iter);
109         dbus_message_iter_recurse(&iter, &value);
110
111         type = dbus_message_iter_get_arg_type(&value);
112
113         if (g_str_equal(name, "OfflineMode") == TRUE) {
114                 connman_bool_t offlinemode;
115
116                 if (type != DBUS_TYPE_BOOLEAN)
117                         return __connman_error_invalid_arguments(msg);
118
119                 dbus_message_iter_get_basic(&value, &offlinemode);
120
121                 __connman_profile_set_offlinemode(offlinemode, TRUE);
122
123                 __connman_profile_save_default();
124         } else if (g_str_equal(name, "ActiveProfile") == TRUE) {
125                 const char *str;
126
127                 dbus_message_iter_get_basic(&value, &str);
128
129                 return __connman_error_not_supported(msg);
130         } else if (g_str_equal(name, "SessionMode") == TRUE) {
131                 connman_bool_t sessionmode;
132
133                 if (type != DBUS_TYPE_BOOLEAN)
134                         return __connman_error_invalid_arguments(msg);
135
136                 dbus_message_iter_get_basic(&value, &sessionmode);
137
138                 __connman_session_set_mode(sessionmode);
139         } else
140                 return __connman_error_invalid_property(msg);
141
142         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
143 }
144
145 static DBusMessage *get_state(DBusConnection *conn,
146                                         DBusMessage *msg, void *data)
147 {
148         const char *str;
149
150         DBG("conn %p", conn);
151
152         str = __connman_notifier_get_state();
153
154         return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &str,
155                                                 DBUS_TYPE_INVALID);
156 }
157
158 static DBusMessage *create_profile(DBusConnection *conn,
159                                         DBusMessage *msg, void *data)
160 {
161         const char *name, *path;
162         int err;
163
164         DBG("conn %p", conn);
165
166         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
167                                                         DBUS_TYPE_INVALID);
168
169         err = __connman_profile_create(name, &path);
170         if (err < 0)
171                 return __connman_error_failed(msg, -err);
172
173         return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
174                                                         DBUS_TYPE_INVALID);
175 }
176
177 static DBusMessage *remove_profile(DBusConnection *conn,
178                                         DBusMessage *msg, void *data)
179 {
180         const char *path;
181         int err;
182
183         DBG("conn %p", conn);
184
185         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
186                                                         DBUS_TYPE_INVALID);
187
188         err = __connman_profile_remove(path);
189         if (err < 0)
190                 return __connman_error_failed(msg, -err);
191
192         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
193 }
194
195 static DBusMessage *remove_provider(DBusConnection *conn,
196                                     DBusMessage *msg, void *data)
197 {
198         const char *path;
199         int err;
200
201         DBG("conn %p", conn);
202
203         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
204                                                         DBUS_TYPE_INVALID);
205
206         err = __connman_provider_remove(path);
207         if (err < 0)
208                 return __connman_error_failed(msg, -err);
209
210         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
211 }
212
213 static DBusMessage *request_scan(DBusConnection *conn,
214                                         DBusMessage *msg, void *data)
215 {
216         enum connman_service_type type;
217         const char *str;
218         int err;
219
220         DBG("conn %p", conn);
221
222         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
223                                                         DBUS_TYPE_INVALID);
224
225         if (g_strcmp0(str, "") == 0)
226                 type = CONNMAN_SERVICE_TYPE_UNKNOWN;
227         else if (g_strcmp0(str, "wifi") == 0)
228                 type = CONNMAN_SERVICE_TYPE_WIFI;
229         else if (g_strcmp0(str, "wimax") == 0)
230                 type = CONNMAN_SERVICE_TYPE_WIMAX;
231         else
232                 return __connman_error_invalid_arguments(msg);
233
234         err = __connman_element_request_scan(type);
235         if (err < 0) {
236                 if (err == -EINPROGRESS) {
237                         connman_error("Invalid return code from scan");
238                         err = -EINVAL;
239                 }
240
241                 return __connman_error_failed(msg, -err);
242         }
243
244         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
245 }
246
247 static DBusConnection *connection = NULL;
248
249 static enum connman_service_type technology_type;
250 static connman_bool_t technology_enabled;
251 static DBusMessage *technology_pending = NULL;
252 static guint technology_timeout = 0;
253
254 static void technology_reply(int error)
255 {
256         DBG("");
257
258         if (technology_timeout > 0) {
259                 g_source_remove(technology_timeout);
260                 technology_timeout = 0;
261         }
262
263         if (technology_pending != NULL) {
264                 if (error > 0) {
265                         DBusMessage *reply;
266
267                         reply = __connman_error_failed(technology_pending,
268                                                                 error);
269                         if (reply != NULL)
270                                 g_dbus_send_message(connection, reply);
271                 } else
272                         g_dbus_send_reply(connection, technology_pending,
273                                                         DBUS_TYPE_INVALID);
274
275                 dbus_message_unref(technology_pending);
276                 technology_pending = NULL;
277         }
278
279         technology_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
280 }
281
282 static gboolean technology_abort(gpointer user_data)
283 {
284         DBG("");
285
286         technology_timeout = 0;
287
288         technology_reply(ETIMEDOUT);
289
290         return FALSE;
291 }
292
293 static void technology_notify(enum connman_service_type type,
294                                                 connman_bool_t enabled)
295 {
296         DBG("type %d enabled %d", type, enabled);
297
298         if (type == technology_type && enabled == technology_enabled)
299                 technology_reply(0);
300 }
301
302 static struct connman_notifier technology_notifier = {
303         .name           = "manager",
304         .priority       = CONNMAN_NOTIFIER_PRIORITY_HIGH,
305         .service_enabled= technology_notify,
306 };
307
308 static DBusMessage *enable_technology(DBusConnection *conn,
309                                         DBusMessage *msg, void *data)
310 {
311         enum connman_service_type type;
312         const char *str;
313         int err;
314
315         DBG("conn %p", conn);
316
317         if (technology_pending != NULL)
318                 return __connman_error_in_progress(msg);
319
320         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
321                                                         DBUS_TYPE_INVALID);
322
323         if (g_strcmp0(str, "ethernet") == 0)
324                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
325         else if (g_strcmp0(str, "wifi") == 0)
326                 type = CONNMAN_SERVICE_TYPE_WIFI;
327         else if (g_strcmp0(str, "wimax") == 0)
328                 type = CONNMAN_SERVICE_TYPE_WIMAX;
329         else if (g_strcmp0(str, "bluetooth") == 0)
330                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
331         else if (g_strcmp0(str, "cellular") == 0)
332                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
333         else
334                 return __connman_error_invalid_arguments(msg);
335
336         if (__connman_notifier_is_registered(type) == FALSE)
337                 return __connman_error_not_registered(msg);
338
339         if (__connman_notifier_is_enabled(type) == TRUE)
340                 return __connman_error_already_enabled(msg);
341
342         technology_type = type;
343         technology_enabled = TRUE;
344         technology_pending = dbus_message_ref(msg);
345
346         err = __connman_element_enable_technology(type);
347         if (err < 0 && err != -EINPROGRESS)
348                 technology_reply(-err);
349         else
350                 technology_timeout = g_timeout_add_seconds(15,
351                                                 technology_abort, NULL);
352
353         return NULL;
354 }
355
356 static DBusMessage *disable_technology(DBusConnection *conn,
357                                         DBusMessage *msg, void *data)
358 {
359         enum connman_service_type type;
360         const char *str;
361         int err;
362
363         DBG("conn %p", conn);
364
365         if (technology_pending != NULL)
366                 return __connman_error_in_progress(msg);
367
368         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
369                                                         DBUS_TYPE_INVALID);
370
371         if (g_strcmp0(str, "ethernet") == 0)
372                 type = CONNMAN_SERVICE_TYPE_ETHERNET;
373         else if (g_strcmp0(str, "wifi") == 0)
374                 type = CONNMAN_SERVICE_TYPE_WIFI;
375         else if (g_strcmp0(str, "wimax") == 0)
376                 type = CONNMAN_SERVICE_TYPE_WIMAX;
377         else if (g_strcmp0(str, "bluetooth") == 0)
378                 type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
379         else if (g_strcmp0(str, "cellular") == 0)
380                 type = CONNMAN_SERVICE_TYPE_CELLULAR;
381         else
382                 return __connman_error_invalid_arguments(msg);
383
384         if (__connman_notifier_is_registered(type) == FALSE)
385                 return __connman_error_not_registered(msg);
386
387         if (__connman_notifier_is_enabled(type) == FALSE)
388                 return __connman_error_already_disabled(msg);
389
390         technology_type = type;
391         technology_enabled = FALSE;
392         technology_pending = dbus_message_ref(msg);
393
394         err = __connman_element_disable_technology(type);
395         if (err < 0 && err != -EINPROGRESS)
396                 technology_reply(-err);
397         else
398                 technology_timeout = g_timeout_add_seconds(10,
399                                                 technology_abort, NULL);
400
401         return NULL;
402 }
403
404 static DBusMessage *get_services(DBusConnection *conn,
405                                         DBusMessage *msg, void *data)
406 {
407         DBusMessage *reply;
408         DBusMessageIter iter, array;
409
410         reply = dbus_message_new_method_return(msg);
411         if (reply == NULL)
412                 return NULL;
413
414         dbus_message_iter_init_append(reply, &iter);
415
416         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
417                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
418                         DBUS_TYPE_OBJECT_PATH_AS_STRING
419                         DBUS_TYPE_ARRAY_AS_STRING
420                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
421                                         DBUS_TYPE_STRING_AS_STRING
422                                         DBUS_TYPE_VARIANT_AS_STRING
423                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
424                         DBUS_STRUCT_END_CHAR_AS_STRING, &array);
425
426         __connman_service_list_struct(&array);
427
428         dbus_message_iter_close_container(&iter, &array);
429
430         return reply;
431 }
432
433 static DBusMessage *lookup_service(DBusConnection *conn,
434                                         DBusMessage *msg, void *data)
435 {
436         const char *pattern, *path;
437         int err;
438
439         DBG("conn %p", conn);
440
441         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
442                                                         DBUS_TYPE_INVALID);
443
444         err = __connman_service_lookup(pattern, &path);
445         if (err < 0)
446                 return __connman_error_failed(msg, -err);
447
448         return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
449                                                         DBUS_TYPE_INVALID);
450 }
451
452 static DBusMessage *connect_service(DBusConnection *conn,
453                                         DBusMessage *msg, void *data)
454 {
455         int err;
456
457         DBG("conn %p", conn);
458
459         if (__connman_session_mode() == TRUE) {
460                 connman_info("Session mode enabled: "
461                                 "direct service connect disabled");
462
463                 return __connman_error_failed(msg, -EINVAL);
464         }
465
466         err = __connman_service_create_and_connect(msg);
467         if (err < 0) {
468                 if (err == -EINPROGRESS) {
469                         connman_error("Invalid return code from connect");
470                         err = -EINVAL;
471                 }
472
473                 return __connman_error_failed(msg, -err);
474         }
475
476         return NULL;
477 }
478
479
480 static DBusMessage *connect_provider(DBusConnection *conn,
481                                         DBusMessage *msg, void *data)
482 {
483         int err;
484
485         DBG("conn %p", conn);
486
487         if (__connman_session_mode() == TRUE) {
488                 connman_info("Session mode enabled: "
489                                 "direct provider connect disabled");
490
491                 return __connman_error_failed(msg, -EINVAL);
492         }
493
494         err = __connman_provider_create_and_connect(msg);
495         if (err < 0) {
496                 if (err == -EINPROGRESS) {
497                         connman_error("Invalid return code from connect");
498                         err = -EINVAL;
499                 }
500
501                 return __connman_error_failed(msg, -err);
502         }
503
504         return NULL;
505 }
506
507 static DBusMessage *register_agent(DBusConnection *conn,
508                                         DBusMessage *msg, void *data)
509 {
510         const char *sender, *path;
511         int err;
512
513         DBG("conn %p", conn);
514
515         sender = dbus_message_get_sender(msg);
516
517         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
518                                                         DBUS_TYPE_INVALID);
519
520         err = __connman_agent_register(sender, path);
521         if (err < 0)
522                 return __connman_error_failed(msg, -err);
523
524         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
525 }
526
527 static DBusMessage *unregister_agent(DBusConnection *conn,
528                                         DBusMessage *msg, void *data)
529 {
530         const char *sender, *path;
531         int err;
532
533         DBG("conn %p", conn);
534
535         sender = dbus_message_get_sender(msg);
536
537         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
538                                                         DBUS_TYPE_INVALID);
539
540         err = __connman_agent_unregister(sender, path);
541         if (err < 0)
542                 return __connman_error_failed(msg, -err);
543
544         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
545 }
546
547 static DBusMessage *register_counter(DBusConnection *conn,
548                                         DBusMessage *msg, void *data)
549 {
550         const char *sender, *path;
551         unsigned int accuracy, period;
552         int err;
553
554         DBG("conn %p", conn);
555
556         sender = dbus_message_get_sender(msg);
557
558         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
559                                                 DBUS_TYPE_UINT32, &accuracy,
560                                                 DBUS_TYPE_UINT32, &period,
561                                                         DBUS_TYPE_INVALID);
562
563         /* FIXME: add handling of accuracy parameter */
564
565         err = __connman_counter_register(sender, path, period);
566         if (err < 0)
567                 return __connman_error_failed(msg, -err);
568
569         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
570 }
571
572 static DBusMessage *unregister_counter(DBusConnection *conn,
573                                         DBusMessage *msg, void *data)
574 {
575         const char *sender, *path;
576         int err;
577
578         DBG("conn %p", conn);
579
580         sender = dbus_message_get_sender(msg);
581
582         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
583                                                         DBUS_TYPE_INVALID);
584
585         err = __connman_counter_unregister(sender, path);
586         if (err < 0)
587                 return __connman_error_failed(msg, -err);
588
589         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
590 }
591
592 static DBusMessage *request_session(DBusConnection *conn,
593                                         DBusMessage *msg, void *data)
594 {
595         const char *bearer, *sender, *service_path;
596         struct connman_service *service;
597
598         DBG("conn %p", conn);
599
600         sender = dbus_message_get_sender(msg);
601
602         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &bearer,
603                                                         DBUS_TYPE_INVALID);
604
605         service = __connman_session_request(bearer, sender);
606         if (service == NULL)
607                 return __connman_error_failed(msg, EINVAL);
608
609         service_path = __connman_service_get_path(service);
610
611         return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &service_path,
612                                                 DBUS_TYPE_INVALID);
613 }
614
615 static DBusMessage *release_session(DBusConnection *conn,
616                                         DBusMessage *msg, void *data)
617 {
618         const char *sender;
619         int err;
620
621         DBG("conn %p", conn);
622
623         sender = dbus_message_get_sender(msg);
624
625         err = __connman_session_release(sender);
626         if (err < 0)
627                 return __connman_error_failed(msg, -err);
628
629         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
630 }
631
632 static GDBusMethodTable manager_methods[] = {
633         { "GetProperties",     "",      "a{sv}", get_properties     },
634         { "SetProperty",       "sv",    "",      set_property       },
635         { "GetState",          "",      "s",     get_state          },
636         { "CreateProfile",     "s",     "o",     create_profile     },
637         { "RemoveProfile",     "o",     "",      remove_profile     },
638         { "RemoveProvider",    "o",     "",      remove_provider    },
639         { "RequestScan",       "s",     "",      request_scan       },
640         { "EnableTechnology",  "s",     "",      enable_technology,
641                                                 G_DBUS_METHOD_FLAG_ASYNC },
642         { "DisableTechnology", "s",     "",      disable_technology,
643                                                 G_DBUS_METHOD_FLAG_ASYNC },
644         { "GetServices",       "",      "a(oa{sv})", get_services   },
645         { "LookupService",     "s",     "o",     lookup_service,    },
646         { "ConnectService",    "a{sv}", "o",     connect_service,
647                                                 G_DBUS_METHOD_FLAG_ASYNC },
648         { "ConnectProvider",   "a{sv}", "o",     connect_provider,
649                                                 G_DBUS_METHOD_FLAG_ASYNC },
650         { "RegisterAgent",     "o",     "",      register_agent     },
651         { "UnregisterAgent",   "o",     "",      unregister_agent   },
652         { "RegisterCounter",   "ouu",   "",      register_counter   },
653         { "UnregisterCounter", "o",     "",      unregister_counter },
654         { "RequestSession",    "s",     "o",     request_session    },
655         { "ReleaseSession",    "s",     "",      release_session    },
656         { },
657 };
658
659 static GDBusSignalTable manager_signals[] = {
660         { "PropertyChanged", "sv" },
661         { "StateChanged",    "s"  },
662         { },
663 };
664
665 int __connman_manager_init(void)
666 {
667         DBG("");
668
669         connection = connman_dbus_get_connection();
670         if (connection == NULL)
671                 return -1;
672
673         if (connman_notifier_register(&technology_notifier) < 0)
674                 connman_error("Failed to register technology notifier");
675
676         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
677                                         CONNMAN_MANAGER_INTERFACE,
678                                         manager_methods,
679                                         manager_signals, NULL, NULL, NULL);
680
681         return 0;
682 }
683
684 void __connman_manager_cleanup(void)
685 {
686         DBG("");
687
688         connman_notifier_unregister(&technology_notifier);
689
690         if (connection == NULL)
691                 return;
692
693         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
694                                                 CONNMAN_MANAGER_INTERFACE);
695
696         dbus_connection_unref(connection);
697 }