stktest: Add send_with_reply utility
[platform/upstream/ofono.git] / tools / stktest.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  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 <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35
36 #include <gdbus.h>
37 #include <gatchat/gatserver.h>
38
39 #define OFONO_SERVICE   "org.ofono"
40 #define STKTEST_PATH    "/stktest"
41 #define STKTEST_ERROR   "org.ofono.stktest"
42 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
43 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
44 #define OFONO_STK_INTERFACE             OFONO_SERVICE ".SimToolkit"
45 #define OFONO_STKAGENT_INTERFACE        OFONO_SERVICE ".SimToolkitAgent"
46
47 #define LISTEN_PORT     12765
48
49 enum test_state {
50         TEST_STATE_POWERING_UP = 1,
51         TEST_STATE_REGISTERING_AGENT,
52         TEST_STATE_RUNNING,
53         TEST_STATE_POWERING_DOWN,
54 };
55
56 static GMainLoop *main_loop = NULL;
57 static volatile sig_atomic_t __terminated = 0;
58
59 /* DBus related */
60 static DBusConnection *conn;
61 static gboolean ofono_running = FALSE;
62 static guint modem_changed_watch;
63 enum test_state state;
64
65 /* Emulator setup */
66 static guint server_watch;
67 static GAtServer *emulator;
68
69 /* Emulated modem state variables */
70 static int modem_mode = 0;
71
72 static gboolean create_tcp(void);
73
74 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
75 {
76         return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
77                                         "Invalid arguments provided");
78 }
79
80 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
81                                         void *data)
82 {
83         g_print("Got Release");
84
85         return dbus_message_new_method_return(msg);
86 }
87
88 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
89                                         void *data)
90 {
91         const char *text;
92         unsigned char icon_id;
93         dbus_bool_t urgent;
94
95         if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &text,
96                                                 DBUS_TYPE_BYTE, &icon_id,
97                                                 DBUS_TYPE_BOOLEAN, &urgent,
98                                                 DBUS_TYPE_INVALID) == FALSE)
99                 return stktest_error_invalid_args(msg);
100
101         g_print("Got DisplayText with: %s, %u, %d\n", text, icon_id, urgent);
102
103         return dbus_message_new_method_return(msg);
104 }
105
106 static void server_debug(const char *str, void *data)
107 {
108         g_print("%s: %s\n", (char *) data, str);
109 }
110
111 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
112                         GAtResult *cmd, gpointer user)
113 {
114         switch (type) {
115         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
116                 g_at_server_send_info(server, "oFono", TRUE);
117                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
118                 break;
119         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
120                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
121                 break;
122         default:
123                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
124         };
125 }
126
127 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
128                         GAtResult *cmd, gpointer user)
129 {
130         switch (type) {
131         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
132                 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
133                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
134                 break;
135         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
136                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
137                 break;
138         default:
139                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
140         };
141 }
142
143 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
144                         GAtResult *cmd, gpointer user)
145 {
146         char buf[256];
147
148         switch (type) {
149         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
150                 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
151                 g_at_server_send_info(server, buf, TRUE);
152                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
153                 break;
154         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
155                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
156                 break;
157         default:
158                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
159         };
160 }
161
162 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
163                         GAtResult *cmd, gpointer user)
164 {
165         switch (type) {
166         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
167                 g_at_server_send_info(server, "123456789", TRUE);
168                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
169                 break;
170         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
171                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
172                 break;
173         default:
174                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
175         };
176 }
177
178 static gboolean send_ok(gpointer user)
179 {
180         GAtServer *server = user;
181
182         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
183
184         return FALSE;
185 }
186
187 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
188                         GAtResult *cmd, gpointer user)
189 {
190         char buf[12];
191
192         switch (type) {
193         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
194                 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
195                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
196                 break;
197         case G_AT_SERVER_REQUEST_TYPE_QUERY:
198                 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
199                 g_at_server_send_info(server, buf, TRUE);
200                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
201                 break;
202         case G_AT_SERVER_REQUEST_TYPE_SET:
203         {
204                 GAtResultIter iter;
205                 int mode;
206
207                 g_at_result_iter_init(&iter, cmd);
208                 g_at_result_iter_next(&iter, "");
209
210                 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
211                         goto error;
212
213                 if (mode != 0 && mode != 1)
214                         goto error;
215
216                 if (modem_mode == mode) {
217                         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
218                         break;
219                 }
220
221                 modem_mode = mode;
222                 g_timeout_add_seconds(1, send_ok, server);
223                 break;
224         }
225         default:
226                 goto error;
227         };
228
229         return;
230
231 error:
232         g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
233 }
234
235 static void listen_again(gpointer user_data)
236 {
237         if (create_tcp() == TRUE)
238                 return;
239
240         g_print("Error listening to socket\n");
241         g_main_loop_quit(main_loop);
242 }
243
244 static void setup_emulator(GAtServer *server)
245 {
246         g_at_server_set_debug(server, server_debug, "Server");
247
248         g_at_server_register(server, "+CGMI",    cgmi_cb,    NULL, NULL);
249         g_at_server_register(server, "+CGMM",    cgmm_cb,    NULL, NULL);
250         g_at_server_register(server, "+CGMR",    cgmr_cb,    NULL, NULL);
251         g_at_server_register(server, "+CGSN",    cgsn_cb,    NULL, NULL);
252         g_at_server_register(server, "+CFUN",    cfun_cb,    NULL, NULL);
253
254         g_at_server_set_disconnect_function(server, listen_again, NULL);
255 }
256
257 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
258                                                         gpointer user)
259 {
260         struct sockaddr saddr;
261         unsigned int len = sizeof(saddr);
262         int fd;
263         GIOChannel *client_io = NULL;
264
265         if (cond != G_IO_IN)
266                 goto error;
267
268         fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
269         if (fd == -1)
270                 goto error;
271
272         client_io = g_io_channel_unix_new(fd);
273
274         emulator = g_at_server_new(client_io);
275         g_io_channel_unref(client_io);
276
277         if (emulator == NULL)
278                 goto error;
279
280         setup_emulator(emulator);
281
282 error:
283         server_watch = 0;
284         return FALSE;
285 }
286
287 static gboolean create_tcp(void)
288 {
289         struct sockaddr_in addr;
290         int sk;
291         int reuseaddr = 1;
292         GIOChannel *server_io;
293
294         sk = socket(PF_INET, SOCK_STREAM, 0);
295         if (sk < 0) {
296                 g_print("Can't create tcp/ip socket: %s (%d)\n",
297                                                 strerror(errno), errno);
298                 return FALSE;
299         }
300
301         memset(&addr, 0, sizeof(addr));
302
303         addr.sin_family = AF_INET;
304         addr.sin_addr.s_addr = INADDR_ANY;
305         addr.sin_port = htons(LISTEN_PORT);
306
307         setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
308         if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
309                 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
310                 close(sk);
311                 return FALSE;
312         }
313
314         if (listen(sk, 1) < 0) {
315                 g_print("Can't listen on socket: %s (%d)",
316                                                 strerror(errno), errno);
317                 close(sk);
318                 return FALSE;
319         }
320
321         g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
322
323         server_io = g_io_channel_unix_new(sk);
324         g_io_channel_set_close_on_unref(server_io, TRUE);
325
326         server_watch = g_io_add_watch_full(server_io,
327                                 G_PRIORITY_DEFAULT,
328                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
329                                 on_socket_connected, NULL, NULL);
330
331         g_io_channel_unref(server_io);
332
333         return TRUE;
334 }
335
336 static gboolean has_stk_interface(DBusMessageIter *iter)
337 {
338         DBusMessageIter entry;
339
340         dbus_message_iter_recurse(iter, &entry);
341
342         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
343                 const char *interface;
344
345                 dbus_message_iter_get_basic(&entry, &interface);
346
347                 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
348                         return TRUE;
349
350                 dbus_message_iter_next(&entry);
351         }
352
353         return FALSE;
354 }
355
356 static int send_with_reply(const char *path, const char *interface,
357                                 const char *method, DBusPendingCall **call,
358                                 DBusPendingCallNotifyFunction cb,
359                                 void *user_data, DBusFreeFunction free_func,
360                                 int timeout, int type, ...)
361 {
362         DBusMessage *msg;
363         DBusPendingCall *c;
364         va_list args;
365         int err;
366
367         msg = dbus_message_new_method_call(OFONO_SERVICE, path,
368                                                 interface, method);
369         if (msg == NULL) {
370                 g_printerr("Unable to allocate new D-Bus %s message\n", method);
371                 err = -ENOMEM;
372                 goto fail;
373         }
374
375         va_start(args, type);
376
377         if (!dbus_message_append_args_valist(msg, type, args)) {
378                 va_end(args);
379                 err = -EIO;
380                 goto fail;
381         }
382
383         va_end(args);
384
385         if (timeout > 0)
386                 timeout *= 1000;
387
388         if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
389                 g_printerr("Sending %s failed\n", method);
390                 err = -EIO;
391                 goto fail;
392         }
393
394         if (call != NULL)
395                 *call = c;
396
397         dbus_pending_call_set_notify(c, cb, user_data, free_func);
398         dbus_pending_call_unref(c);
399
400         dbus_message_unref(msg);
401
402         return 0;
403
404 fail:
405         if (free_func && user_data)
406                 free_func(user_data);
407
408         if (msg)
409                 dbus_message_unref(msg);
410
411         return err;
412 }
413
414 static void set_property_reply(DBusPendingCall *call, void *user_data)
415 {
416         DBusMessage *reply = dbus_pending_call_steal_reply(call);
417         DBusError err;
418
419         dbus_error_init(&err);
420
421         if (dbus_set_error_from_message(&err, reply) == TRUE) {
422                 g_printerr("%s: %s\n", err.name, err.message);
423                 dbus_error_free(&err);
424         }
425
426         dbus_message_unref(reply);
427 }
428
429 static int set_property(const char *path, const char *interface,
430                         const char *key, int type, const void *val,
431                         DBusPendingCallNotifyFunction notify,
432                         gpointer user_data,
433                         DBusFreeFunction destroy)
434 {
435         DBusMessage *msg;
436         DBusMessageIter iter, value;
437         DBusPendingCall *call;
438         const char *signature;
439
440         msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
441                                                 "SetProperty");
442         if (msg == NULL)
443                 return -ENOMEM;
444
445         dbus_message_set_auto_start(msg, FALSE);
446
447         dbus_message_iter_init_append(msg, &iter);
448
449         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
450
451         switch (type) {
452         case DBUS_TYPE_BOOLEAN:
453                 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
454                 break;
455         default:
456                 dbus_message_unref(msg);
457                 return -EINVAL;
458         }
459
460         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
461                                                         signature, &value);
462         dbus_message_iter_append_basic(&value, type, val);
463         dbus_message_iter_close_container(&iter, &value);
464
465         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
466                 dbus_message_unref(msg);
467                 return -EIO;
468         }
469
470         dbus_message_unref(msg);
471
472         if (call == NULL)
473                 return -EINVAL;
474
475         dbus_pending_call_set_notify(call, notify, user_data, destroy);
476
477         dbus_pending_call_unref(call);
478
479         return 0;
480 }
481
482 static void register_agent()
483 {
484         state = TEST_STATE_REGISTERING_AGENT;
485         g_print("Gained STK interface, registering agent...\n");
486 }
487
488 static gboolean modem_changed(DBusConnection *conn,
489                                 DBusMessage *msg, void *user_data)
490 {
491         DBusMessageIter iter, value;
492         const char *path, *key;
493         gboolean has_stk;
494
495         if (dbus_message_iter_init(msg, &iter) == FALSE)
496                 return TRUE;
497
498         path = dbus_message_get_path(msg);
499
500         if (g_str_equal(STKTEST_PATH, path) == FALSE)
501                 return TRUE;
502
503         dbus_message_iter_get_basic(&iter, &key);
504
505         dbus_message_iter_next(&iter);
506         dbus_message_iter_recurse(&iter, &value);
507
508         if (g_str_equal(key, "Interfaces") == FALSE)
509                 return TRUE;
510
511         has_stk = has_stk_interface(&value);
512
513         switch (state) {
514         case TEST_STATE_POWERING_UP:
515                 if (has_stk)
516                         register_agent();
517                 break;
518         case TEST_STATE_REGISTERING_AGENT:
519         case TEST_STATE_RUNNING:
520                 if (has_stk == FALSE)
521                         g_printerr("Unexpectedly lost STK interface\n");
522                 /* Fall through */
523         case TEST_STATE_POWERING_DOWN:
524                 break;
525         };
526
527         return TRUE;
528 }
529
530 static void powerup(void)
531 {
532         dbus_bool_t powered = TRUE;
533
534         state = TEST_STATE_POWERING_UP;
535         set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
536                         DBUS_TYPE_BOOLEAN, &powered,
537                         set_property_reply, NULL, NULL);
538 }
539
540 static void get_modems_reply(DBusPendingCall *call, void *user_data)
541 {
542         DBusMessage *reply = dbus_pending_call_steal_reply(call);
543         DBusMessageIter iter, list;
544         DBusError err;
545         gboolean found = FALSE;
546
547         dbus_error_init(&err);
548
549         if (dbus_set_error_from_message(&err, reply) == TRUE) {
550                 g_printerr("%s: %s\n", err.name, err.message);
551                 dbus_error_free(&err);
552                 goto done;
553         }
554
555         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
556                 goto done;
557
558         if (dbus_message_iter_init(reply, &iter) == FALSE)
559                 goto done;
560
561         dbus_message_iter_recurse(&iter, &list);
562
563         while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
564                 DBusMessageIter entry;
565                 const char *path;
566
567                 dbus_message_iter_recurse(&list, &entry);
568                 dbus_message_iter_get_basic(&entry, &path);
569
570                 if (g_str_equal(path, STKTEST_PATH))
571                         found = TRUE;
572
573                 dbus_message_iter_next(&list);
574         }
575
576 done:
577         dbus_message_unref(reply);
578
579         if (found == FALSE) {
580                 g_printerr("STK Test modem not found\n");
581                 g_main_loop_quit(main_loop);
582                 return;
583         }
584
585         g_print("Test modem found\n");
586
587         modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
588                                                         STKTEST_PATH,
589                                                         OFONO_MODEM_INTERFACE,
590                                                         "PropertyChanged",
591                                                         modem_changed,
592                                                         NULL, NULL);
593
594         if (create_tcp() == FALSE) {
595                 g_printerr("Unable to listen on modem emulator socket\n");
596                 g_main_loop_quit(main_loop);
597         }
598
599         powerup();
600 }
601
602 static int get_modems(DBusConnection *conn)
603 {
604         DBusMessage *msg;
605         DBusPendingCall *call;
606
607         msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
608                                         OFONO_MANAGER_INTERFACE, "GetModems");
609         if (msg == NULL)
610                 return -ENOMEM;
611
612         dbus_message_set_auto_start(msg, FALSE);
613
614         g_print("getting modems\n");
615
616         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
617                 dbus_message_unref(msg);
618                 return -EIO;
619         }
620
621         dbus_message_unref(msg);
622
623         if (call == NULL)
624                 return -EINVAL;
625
626         dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
627
628         dbus_pending_call_unref(call);
629
630         return 0;
631 }
632
633 static const GDBusMethodTable agent_methods[] = {
634         { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
635         { GDBUS_METHOD("DisplayText",
636                 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
637                                 { "urgent", "b" }), NULL,
638                                 agent_display_text) },
639         { },
640 };
641
642 static void ofono_connect(DBusConnection *conn, void *user_data)
643 {
644         g_print("starting telephony interface\n");
645
646         if (!g_dbus_register_interface(conn, "/default",
647                                         OFONO_STKAGENT_INTERFACE,
648                                         agent_methods, NULL, NULL,
649                                         NULL, NULL)) {
650                 g_printerr("Unable to register local agent");
651                 g_main_loop_quit(main_loop);
652         }
653
654         ofono_running = TRUE;
655         get_modems(conn);
656 }
657
658 static void ofono_disconnect(DBusConnection *conn, void *user_data)
659 {
660         g_print("stopping telephony interface\n");
661
662         g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
663
664         ofono_running = FALSE;
665
666         g_dbus_remove_watch(conn, modem_changed_watch);
667         modem_changed_watch = 0;
668
669         if (server_watch) {
670                 g_source_remove(server_watch);
671                 server_watch = 0;
672         }
673
674         g_at_server_unref(emulator);
675         emulator = NULL;
676 }
677
678 static void sig_term(int sig)
679 {
680         if (__terminated > 0)
681                 return;
682
683         __terminated = 1;
684
685         g_print("Terminating\n");
686
687         g_main_loop_quit(main_loop);
688 }
689
690 static void disconnect_callback(DBusConnection *conn, void *user_data)
691 {
692         g_printerr("D-Bus disconnect\n");
693
694         g_main_loop_quit(main_loop);
695 }
696
697 static gboolean option_version = FALSE;
698
699 static GOptionEntry options[] = {
700         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
701                                 "Show version information and exit" },
702         { NULL },
703 };
704
705 int main(int argc, char **argv)
706 {
707         GOptionContext *context;
708         GError *error = NULL;
709         DBusError err;
710         guint watch;
711         struct sigaction sa;
712
713         context = g_option_context_new(NULL);
714         g_option_context_add_main_entries(context, options, NULL);
715
716         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
717                 if (error != NULL) {
718                         g_printerr("%s\n", error->message);
719                         g_error_free(error);
720                 } else
721                         g_printerr("An unknown error occurred\n");
722                 exit(1);
723         }
724
725         g_option_context_free(context);
726
727         if (option_version == TRUE) {
728                 printf("%s\n", VERSION);
729                 exit(0);
730         }
731
732         main_loop = g_main_loop_new(NULL, FALSE);
733
734         dbus_error_init(&err);
735
736         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
737         if (conn == NULL) {
738                 if (dbus_error_is_set(&err) == TRUE) {
739                         fprintf(stderr, "%s\n", err.message);
740                         dbus_error_free(&err);
741                 } else
742                         fprintf(stderr, "Can't register with system bus\n");
743                 exit(1);
744         }
745
746         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
747
748         memset(&sa, 0, sizeof(sa));
749         sa.sa_handler = sig_term;
750         sigaction(SIGINT, &sa, NULL);
751         sigaction(SIGTERM, &sa, NULL);
752
753         watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
754                                 ofono_connect, ofono_disconnect, NULL, NULL);
755
756         g_main_loop_run(main_loop);
757
758         g_dbus_remove_watch(conn, watch);
759
760         if (ofono_running == TRUE)
761                 ofono_disconnect(conn, NULL);
762
763         dbus_connection_unref(conn);
764
765         g_main_loop_unref(main_loop);
766
767         return 0;
768 }