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