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