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