session: Only select new service if not in progress
[framework/connectivity/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 (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 *lookup_service(DBusConnection *conn,
341                                         DBusMessage *msg, void *data)
342 {
343         const char *pattern, *path;
344         int err;
345
346         DBG("conn %p", conn);
347
348         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern,
349                                                         DBUS_TYPE_INVALID);
350
351         err = __connman_service_lookup(pattern, &path);
352         if (err < 0)
353                 return __connman_error_failed(msg, -err);
354
355         return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
356                                                         DBUS_TYPE_INVALID);
357 }
358
359 static DBusMessage *connect_service(DBusConnection *conn,
360                                         DBusMessage *msg, void *data)
361 {
362         int err;
363
364         DBG("conn %p", conn);
365
366         if (__connman_session_mode() == TRUE) {
367                 connman_info("Session mode enabled: "
368                                 "direct service connect disabled");
369
370                 return __connman_error_failed(msg, -EINVAL);
371         }
372
373         err = __connman_service_create_and_connect(msg);
374         if (err < 0) {
375                 if (err == -EINPROGRESS) {
376                         connman_error("Invalid return code from connect");
377                         err = -EINVAL;
378                 }
379
380                 return __connman_error_failed(msg, -err);
381         }
382
383         return NULL;
384 }
385
386 static DBusMessage *provision_service(DBusConnection *conn, DBusMessage *msg,
387                                         void *data)
388 {
389         int err;
390
391         DBG("conn %p", conn);
392
393         err = __connman_service_provision(msg);
394         if (err < 0)
395                 return __connman_error_failed(msg, -err);
396
397         return NULL;
398 }
399
400 static DBusMessage *connect_provider(DBusConnection *conn,
401                                         DBusMessage *msg, void *data)
402 {
403         int err;
404
405         DBG("conn %p", conn);
406
407         if (__connman_session_mode() == TRUE) {
408                 connman_info("Session mode enabled: "
409                                 "direct provider connect disabled");
410
411                 return __connman_error_failed(msg, -EINVAL);
412         }
413
414         err = __connman_provider_create_and_connect(msg);
415         if (err < 0) {
416                 if (err == -EINPROGRESS) {
417                         connman_error("Invalid return code from connect");
418                         err = -EINVAL;
419                 }
420
421                 return __connman_error_failed(msg, -err);
422         }
423
424         return NULL;
425 }
426
427 static DBusMessage *register_agent(DBusConnection *conn,
428                                         DBusMessage *msg, void *data)
429 {
430         const char *sender, *path;
431         int err;
432
433         DBG("conn %p", conn);
434
435         sender = dbus_message_get_sender(msg);
436
437         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
438                                                         DBUS_TYPE_INVALID);
439
440         err = __connman_agent_register(sender, path);
441         if (err < 0)
442                 return __connman_error_failed(msg, -err);
443
444         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
445 }
446
447 static DBusMessage *unregister_agent(DBusConnection *conn,
448                                         DBusMessage *msg, void *data)
449 {
450         const char *sender, *path;
451         int err;
452
453         DBG("conn %p", conn);
454
455         sender = dbus_message_get_sender(msg);
456
457         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
458                                                         DBUS_TYPE_INVALID);
459
460         err = __connman_agent_unregister(sender, path);
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 *register_counter(DBusConnection *conn,
468                                         DBusMessage *msg, void *data)
469 {
470         const char *sender, *path;
471         unsigned int accuracy, period;
472         int err;
473
474         DBG("conn %p", conn);
475
476         sender = dbus_message_get_sender(msg);
477
478         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
479                                                 DBUS_TYPE_UINT32, &accuracy,
480                                                 DBUS_TYPE_UINT32, &period,
481                                                         DBUS_TYPE_INVALID);
482
483         /* FIXME: add handling of accuracy parameter */
484
485         err = __connman_counter_register(sender, path, period);
486         if (err < 0)
487                 return __connman_error_failed(msg, -err);
488
489         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
490 }
491
492 static DBusMessage *unregister_counter(DBusConnection *conn,
493                                         DBusMessage *msg, void *data)
494 {
495         const char *sender, *path;
496         int err;
497
498         DBG("conn %p", conn);
499
500         sender = dbus_message_get_sender(msg);
501
502         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
503                                                         DBUS_TYPE_INVALID);
504
505         err = __connman_counter_unregister(sender, path);
506         if (err < 0)
507                 return __connman_error_failed(msg, -err);
508
509         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
510 }
511
512 static DBusMessage *create_session(DBusConnection *conn,
513                                         DBusMessage *msg, void *data)
514 {
515         int err;
516
517         DBG("conn %p", conn);
518
519         err = __connman_session_create(msg);
520         if (err < 0)
521                 return __connman_error_failed(msg, -err);
522
523         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
524 }
525
526 static DBusMessage *destroy_session(DBusConnection *conn,
527                                         DBusMessage *msg, void *data)
528 {
529         int err;
530
531         DBG("conn %p", conn);
532
533         err = __connman_session_destroy(msg);
534         if (err < 0)
535                 return __connman_error_failed(msg, -err);
536
537         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
538 }
539
540 static DBusMessage *request_private_network(DBusConnection *conn,
541                                         DBusMessage *msg, void *data)
542 {
543         const char *sender;
544         int  err;
545
546         DBG("conn %p", conn);
547
548         sender = dbus_message_get_sender(msg);
549
550         err = __connman_private_network_request(msg, sender);
551         if (err < 0)
552                 return __connman_error_failed(msg, -err);
553
554         return NULL;
555 }
556
557 static DBusMessage *release_private_network(DBusConnection *conn,
558                                         DBusMessage *msg, void *data)
559 {
560         const char *path;
561         int err;
562
563         DBG("conn %p", conn);
564
565         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
566                                                         DBUS_TYPE_INVALID);
567
568         err = __connman_private_network_release(path);
569         if (err < 0)
570                 return __connman_error_failed(msg, -err);
571
572         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
573 }
574
575 static GDBusMethodTable manager_methods[] = {
576         { "GetProperties",     "",      "a{sv}", get_properties     },
577         { "SetProperty",       "sv",    "",      set_property,
578                                                 G_DBUS_METHOD_FLAG_ASYNC },
579         { "GetState",          "",      "s",     get_state          },
580         { "RemoveProvider",    "o",     "",      remove_provider    },
581         { "RequestScan",       "s",     "",      request_scan       },
582         { "EnableTechnology",  "s",     "",      enable_technology,
583                                                 G_DBUS_METHOD_FLAG_ASYNC },
584         { "DisableTechnology", "s",     "",      disable_technology,
585                                                 G_DBUS_METHOD_FLAG_ASYNC },
586         { "GetServices",       "",      "a(oa{sv})", get_services   },
587         { "LookupService",     "s",     "o",     lookup_service,    },
588         { "ConnectService",    "a{sv}", "o",     connect_service,
589                                                 G_DBUS_METHOD_FLAG_ASYNC },
590         { "ProvisionService",  "s",     "",      provision_service,
591                                                 G_DBUS_METHOD_FLAG_ASYNC },
592         { "ConnectProvider",   "a{sv}", "o",     connect_provider,
593                                                 G_DBUS_METHOD_FLAG_ASYNC },
594         { "RegisterAgent",     "o",     "",      register_agent     },
595         { "UnregisterAgent",   "o",     "",      unregister_agent   },
596         { "RegisterCounter",   "ouu",   "",      register_counter   },
597         { "UnregisterCounter", "o",     "",      unregister_counter },
598         { "CreateSession",     "a{sv}o", "o",    create_session     },
599         { "DestroySession",    "o",     "",      destroy_session    },
600         { "RequestPrivateNetwork",    "",     "oa{sv}h",
601                                                 request_private_network,
602                                                 G_DBUS_METHOD_FLAG_ASYNC },
603         { "ReleasePrivateNetwork",    "o",    "",
604                                                 release_private_network },
605         { },
606 };
607
608 static GDBusSignalTable manager_signals[] = {
609         { "PropertyChanged", "sv" },
610         { "StateChanged",    "s"  },
611         { },
612 };
613
614 int __connman_manager_init(void)
615 {
616         DBG("");
617
618         connection = connman_dbus_get_connection();
619         if (connection == NULL)
620                 return -1;
621
622         if (connman_notifier_register(&technology_notifier) < 0)
623                 connman_error("Failed to register technology notifier");
624
625         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
626                                         CONNMAN_MANAGER_INTERFACE,
627                                         manager_methods,
628                                         manager_signals, NULL, NULL, NULL);
629
630         connman_state_idle = TRUE;
631
632         return 0;
633 }
634
635 void __connman_manager_cleanup(void)
636 {
637         DBG("");
638
639         if (connection == NULL)
640                 return;
641
642         connman_notifier_unregister(&technology_notifier);
643
644         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
645                                                 CONNMAN_MANAGER_INTERFACE);
646
647         dbus_connection_unref(connection);
648 }