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