stktest: Add agent registration
[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_reply(DBusPendingCall *call, void *user_data)
483 {
484         DBusMessage *reply = dbus_pending_call_steal_reply(call);
485         DBusError err;
486
487         dbus_error_init(&err);
488
489         if (dbus_set_error_from_message(&err, reply) == TRUE) {
490                 g_printerr("%s: %s\n", err.name, err.message);
491                 dbus_error_free(&err);
492         }
493
494         dbus_message_unref(reply);
495
496         state = TEST_STATE_RUNNING;
497 }
498
499 static void register_agent()
500 {
501         const char *path = "/default";
502         int status;
503
504         g_print("Gained STK interface, registering agent...\n");
505
506         status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
507                                         "RegisterAgent", NULL,
508                                         register_agent_reply, NULL, NULL, 1,
509                                         DBUS_TYPE_OBJECT_PATH, &path,
510                                         DBUS_TYPE_INVALID);
511
512         if (status < 0) {
513                 g_printerr("Unable to register agent with oFono\n");
514                 g_main_loop_quit(main_loop);
515                 return;
516         }
517
518         state = TEST_STATE_REGISTERING_AGENT;
519 }
520
521 static gboolean modem_changed(DBusConnection *conn,
522                                 DBusMessage *msg, void *user_data)
523 {
524         DBusMessageIter iter, value;
525         const char *path, *key;
526         gboolean has_stk;
527
528         if (dbus_message_iter_init(msg, &iter) == FALSE)
529                 return TRUE;
530
531         path = dbus_message_get_path(msg);
532
533         if (g_str_equal(STKTEST_PATH, path) == FALSE)
534                 return TRUE;
535
536         dbus_message_iter_get_basic(&iter, &key);
537
538         dbus_message_iter_next(&iter);
539         dbus_message_iter_recurse(&iter, &value);
540
541         if (g_str_equal(key, "Interfaces") == FALSE)
542                 return TRUE;
543
544         has_stk = has_stk_interface(&value);
545
546         switch (state) {
547         case TEST_STATE_POWERING_UP:
548                 if (has_stk)
549                         register_agent();
550                 break;
551         case TEST_STATE_REGISTERING_AGENT:
552         case TEST_STATE_RUNNING:
553                 if (has_stk == FALSE)
554                         g_printerr("Unexpectedly lost STK interface\n");
555                 /* Fall through */
556         case TEST_STATE_POWERING_DOWN:
557                 break;
558         };
559
560         return TRUE;
561 }
562
563 static void powerup(void)
564 {
565         dbus_bool_t powered = TRUE;
566
567         state = TEST_STATE_POWERING_UP;
568         set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
569                         DBUS_TYPE_BOOLEAN, &powered,
570                         set_property_reply, NULL, NULL);
571 }
572
573 static void get_modems_reply(DBusPendingCall *call, void *user_data)
574 {
575         DBusMessage *reply = dbus_pending_call_steal_reply(call);
576         DBusMessageIter iter, list;
577         DBusError err;
578         gboolean found = FALSE;
579
580         dbus_error_init(&err);
581
582         if (dbus_set_error_from_message(&err, reply) == TRUE) {
583                 g_printerr("%s: %s\n", err.name, err.message);
584                 dbus_error_free(&err);
585                 goto done;
586         }
587
588         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
589                 goto done;
590
591         if (dbus_message_iter_init(reply, &iter) == FALSE)
592                 goto done;
593
594         dbus_message_iter_recurse(&iter, &list);
595
596         while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
597                 DBusMessageIter entry;
598                 const char *path;
599
600                 dbus_message_iter_recurse(&list, &entry);
601                 dbus_message_iter_get_basic(&entry, &path);
602
603                 if (g_str_equal(path, STKTEST_PATH))
604                         found = TRUE;
605
606                 dbus_message_iter_next(&list);
607         }
608
609 done:
610         dbus_message_unref(reply);
611
612         if (found == FALSE) {
613                 g_printerr("STK Test modem not found\n");
614                 g_main_loop_quit(main_loop);
615                 return;
616         }
617
618         g_print("Test modem found\n");
619
620         modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
621                                                         STKTEST_PATH,
622                                                         OFONO_MODEM_INTERFACE,
623                                                         "PropertyChanged",
624                                                         modem_changed,
625                                                         NULL, NULL);
626
627         if (create_tcp() == FALSE) {
628                 g_printerr("Unable to listen on modem emulator socket\n");
629                 g_main_loop_quit(main_loop);
630         }
631
632         powerup();
633 }
634
635 static int get_modems(DBusConnection *conn)
636 {
637         DBusMessage *msg;
638         DBusPendingCall *call;
639
640         msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
641                                         OFONO_MANAGER_INTERFACE, "GetModems");
642         if (msg == NULL)
643                 return -ENOMEM;
644
645         dbus_message_set_auto_start(msg, FALSE);
646
647         g_print("getting modems\n");
648
649         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
650                 dbus_message_unref(msg);
651                 return -EIO;
652         }
653
654         dbus_message_unref(msg);
655
656         if (call == NULL)
657                 return -EINVAL;
658
659         dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
660
661         dbus_pending_call_unref(call);
662
663         return 0;
664 }
665
666 static const GDBusMethodTable agent_methods[] = {
667         { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
668         { GDBUS_METHOD("DisplayText",
669                 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
670                                 { "urgent", "b" }), NULL,
671                                 agent_display_text) },
672         { },
673 };
674
675 static void ofono_connect(DBusConnection *conn, void *user_data)
676 {
677         g_print("starting telephony interface\n");
678
679         if (!g_dbus_register_interface(conn, "/default",
680                                         OFONO_STKAGENT_INTERFACE,
681                                         agent_methods, NULL, NULL,
682                                         NULL, NULL)) {
683                 g_printerr("Unable to register local agent");
684                 g_main_loop_quit(main_loop);
685         }
686
687         ofono_running = TRUE;
688         get_modems(conn);
689 }
690
691 static void ofono_disconnect(DBusConnection *conn, void *user_data)
692 {
693         g_print("stopping telephony interface\n");
694
695         g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
696
697         ofono_running = FALSE;
698
699         g_dbus_remove_watch(conn, modem_changed_watch);
700         modem_changed_watch = 0;
701
702         if (server_watch) {
703                 g_source_remove(server_watch);
704                 server_watch = 0;
705         }
706
707         g_at_server_unref(emulator);
708         emulator = NULL;
709 }
710
711 static void sig_term(int sig)
712 {
713         if (__terminated > 0)
714                 return;
715
716         __terminated = 1;
717
718         g_print("Terminating\n");
719
720         g_main_loop_quit(main_loop);
721 }
722
723 static void disconnect_callback(DBusConnection *conn, void *user_data)
724 {
725         g_printerr("D-Bus disconnect\n");
726
727         g_main_loop_quit(main_loop);
728 }
729
730 static gboolean option_version = FALSE;
731
732 static GOptionEntry options[] = {
733         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
734                                 "Show version information and exit" },
735         { NULL },
736 };
737
738 int main(int argc, char **argv)
739 {
740         GOptionContext *context;
741         GError *error = NULL;
742         DBusError err;
743         guint watch;
744         struct sigaction sa;
745
746         context = g_option_context_new(NULL);
747         g_option_context_add_main_entries(context, options, NULL);
748
749         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
750                 if (error != NULL) {
751                         g_printerr("%s\n", error->message);
752                         g_error_free(error);
753                 } else
754                         g_printerr("An unknown error occurred\n");
755                 exit(1);
756         }
757
758         g_option_context_free(context);
759
760         if (option_version == TRUE) {
761                 printf("%s\n", VERSION);
762                 exit(0);
763         }
764
765         main_loop = g_main_loop_new(NULL, FALSE);
766
767         dbus_error_init(&err);
768
769         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
770         if (conn == NULL) {
771                 if (dbus_error_is_set(&err) == TRUE) {
772                         fprintf(stderr, "%s\n", err.message);
773                         dbus_error_free(&err);
774                 } else
775                         fprintf(stderr, "Can't register with system bus\n");
776                 exit(1);
777         }
778
779         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
780
781         memset(&sa, 0, sizeof(sa));
782         sa.sa_handler = sig_term;
783         sigaction(SIGINT, &sa, NULL);
784         sigaction(SIGTERM, &sa, NULL);
785
786         watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
787                                 ofono_connect, ofono_disconnect, NULL, NULL);
788
789         g_main_loop_run(main_loop);
790
791         g_dbus_remove_watch(conn, watch);
792
793         if (ofono_running == TRUE)
794                 ofono_disconnect(conn, NULL);
795
796         dbus_connection_unref(conn);
797
798         g_main_loop_unref(main_loop);
799
800         return 0;
801 }