stktest: Add test framework skeleton
[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.Error"
42 #define OFONO_ERROR     "org.ofono.Error"
43 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
45 #define OFONO_STK_INTERFACE             OFONO_SERVICE ".SimToolkit"
46 #define OFONO_STKAGENT_INTERFACE        OFONO_SERVICE ".SimToolkitAgent"
47
48 #define LISTEN_PORT     12765
49
50 enum test_state {
51         TEST_STATE_POWERING_UP = 1,
52         TEST_STATE_REGISTERING_AGENT,
53         TEST_STATE_RUNNING,
54         TEST_STATE_POWERING_DOWN,
55 };
56
57 enum test_result {
58         TEST_RESULT_NOT_RUN = 0,
59         TEST_RESULT_PASSED,
60         TEST_RESULT_FAILED
61 };
62
63 typedef DBusMessage *(*display_text_cb_t)(DBusMessage *msg, const char *text,
64                                                 unsigned char icon_id,
65                                                 gboolean urgent);
66 typedef void (*terminal_response_func)(const unsigned char *pdu,
67                                         unsigned int len);
68
69 struct test {
70         char *name;
71         char *method;
72         unsigned char *req_pdu;
73         unsigned int req_len;
74         unsigned char *rsp_pdu;
75         unsigned int rsp_len;
76         void *agent_func;
77         terminal_response_func tr_func;
78         enum test_result result;
79 };
80
81 static GMainLoop *main_loop = NULL;
82 static volatile sig_atomic_t __terminated = 0;
83 GList *tests = NULL;
84 GList *cur_test = NULL;
85
86 /* DBus related */
87 static DBusConnection *conn;
88 static gboolean ofono_running = FALSE;
89 static guint modem_changed_watch;
90 enum test_state state;
91
92 /* Emulator setup */
93 static guint server_watch;
94 static GAtServer *emulator;
95
96 /* Emulated modem state variables */
97 static int modem_mode = 0;
98
99 static gboolean create_tcp(void);
100
101 static const char *to_hex(const unsigned char *data, unsigned int len)
102 {
103         static char buf[512+1];
104         unsigned int i;
105
106         for (i = 0; i < len; i++)
107                 sprintf(buf + i * 2, "%02hhX", data[i]);
108
109         buf[i*2] = '\0';
110
111         return buf;
112 }
113
114 static void send_proactive_command(const unsigned char *pdu, unsigned int len)
115 {
116         char buf[1024];
117
118         sprintf(buf, "+CUSATP: %s", to_hex(pdu, len));
119         g_at_server_send_unsolicited(emulator, buf);
120 }
121
122 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
123 {
124         return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
125                                         "Invalid arguments provided");
126 }
127
128 static DBusMessage *stktest_error_failed(DBusMessage *msg)
129 {
130         return g_dbus_create_error(msg, STKTEST_ERROR ".Failed",
131                                         "Operation failed");
132 }
133
134 static DBusMessage *stktest_error_end_session(DBusMessage *msg)
135 {
136         return g_dbus_create_error(msg, OFONO_ERROR ".EndSession",
137                                         "End Session Request");
138 }
139
140 static DBusMessage *stktest_error_go_back(DBusMessage *msg)
141 {
142         return g_dbus_create_error(msg, OFONO_ERROR ".GoBack",
143                                         "Go Back Request");
144 }
145
146 static DBusMessage *stktest_error_busy(DBusMessage *msg)
147 {
148         return g_dbus_create_error(msg, OFONO_ERROR ".Busy",
149                                         "UI Busy");
150 }
151
152 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
153                                         void *data)
154 {
155         g_print("Got Release\n");
156
157         return dbus_message_new_method_return(msg);
158 }
159
160 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
161                                         void *data)
162 {
163         const char *text;
164         unsigned char icon_id;
165         dbus_bool_t urgent;
166
167         if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &text,
168                                                 DBUS_TYPE_BYTE, &icon_id,
169                                                 DBUS_TYPE_BOOLEAN, &urgent,
170                                                 DBUS_TYPE_INVALID) == FALSE)
171                 return stktest_error_invalid_args(msg);
172
173         g_print("Got DisplayText with: %s, %u, %d\n", text, icon_id, urgent);
174
175         return dbus_message_new_method_return(msg);
176 }
177
178 static void server_debug(const char *str, void *data)
179 {
180         g_print("%s: %s\n", (char *) data, str);
181 }
182
183 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
184                         GAtResult *cmd, gpointer user)
185 {
186         switch (type) {
187         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
188                 g_at_server_send_info(server, "oFono", TRUE);
189                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
190                 break;
191         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
192                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
193                 break;
194         default:
195                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
196         };
197 }
198
199 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
200                         GAtResult *cmd, gpointer user)
201 {
202         switch (type) {
203         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
204                 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
205                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
206                 break;
207         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
208                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
209                 break;
210         default:
211                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
212         };
213 }
214
215 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
216                         GAtResult *cmd, gpointer user)
217 {
218         char buf[256];
219
220         switch (type) {
221         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
222                 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
223                 g_at_server_send_info(server, buf, TRUE);
224                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
225                 break;
226         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
227                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
228                 break;
229         default:
230                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
231         };
232 }
233
234 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
235                         GAtResult *cmd, gpointer user)
236 {
237         switch (type) {
238         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
239                 g_at_server_send_info(server, "123456789", TRUE);
240                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
241                 break;
242         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
243                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
244                 break;
245         default:
246                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
247         };
248 }
249
250 static gboolean send_ok(gpointer user)
251 {
252         GAtServer *server = user;
253
254         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
255
256         return FALSE;
257 }
258
259 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
260                         GAtResult *cmd, gpointer user)
261 {
262         char buf[12];
263
264         switch (type) {
265         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
266                 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
267                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
268                 break;
269         case G_AT_SERVER_REQUEST_TYPE_QUERY:
270                 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
271                 g_at_server_send_info(server, buf, TRUE);
272                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
273                 break;
274         case G_AT_SERVER_REQUEST_TYPE_SET:
275         {
276                 GAtResultIter iter;
277                 int mode;
278
279                 g_at_result_iter_init(&iter, cmd);
280                 g_at_result_iter_next(&iter, "");
281
282                 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
283                         goto error;
284
285                 if (mode != 0 && mode != 1)
286                         goto error;
287
288                 if (modem_mode == mode) {
289                         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
290                         break;
291                 }
292
293                 modem_mode = mode;
294                 g_timeout_add_seconds(1, send_ok, server);
295                 break;
296         }
297         default:
298                 goto error;
299         };
300
301         return;
302
303 error:
304         g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
305 }
306
307 static void cusatt_cb(GAtServer *server, GAtServerRequestType type,
308                         GAtResult *cmd, gpointer user)
309 {
310         switch (type) {
311         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
312                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
313                 break;
314         case G_AT_SERVER_REQUEST_TYPE_QUERY:
315                 g_at_server_send_ext_final(server, "+CME ERROR: 4");
316                 break;
317         case G_AT_SERVER_REQUEST_TYPE_SET:
318         {
319                 GAtResultIter iter;
320                 const unsigned char *pdu;
321                 int len;
322
323                 g_at_result_iter_init(&iter, cmd);
324                 g_at_result_iter_next(&iter, "");
325
326                 if (g_at_result_iter_next_hexstring(&iter, &pdu, &len) == FALSE)
327                         goto error;
328
329                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
330                 break;
331         }
332         default:
333                 goto error;
334         };
335
336         return;
337
338 error:
339         g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
340 }
341
342 static void listen_again(gpointer user_data)
343 {
344         if (create_tcp() == TRUE)
345                 return;
346
347         g_print("Error listening to socket\n");
348         g_main_loop_quit(main_loop);
349 }
350
351 static void setup_emulator(GAtServer *server)
352 {
353         g_at_server_set_debug(server, server_debug, "Server");
354
355         g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
356         g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
357         g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
358         g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
359         g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
360         g_at_server_register(server, "+CUSATT", cusatt_cb, NULL, NULL);
361
362         g_at_server_set_disconnect_function(server, listen_again, NULL);
363 }
364
365 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
366                                                         gpointer user)
367 {
368         struct sockaddr saddr;
369         unsigned int len = sizeof(saddr);
370         int fd;
371         GIOChannel *client_io = NULL;
372
373         if (cond != G_IO_IN)
374                 goto error;
375
376         fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
377         if (fd == -1)
378                 goto error;
379
380         client_io = g_io_channel_unix_new(fd);
381
382         emulator = g_at_server_new(client_io);
383         g_at_server_set_echo(emulator, FALSE);
384         g_io_channel_unref(client_io);
385
386         if (emulator == NULL)
387                 goto error;
388
389         setup_emulator(emulator);
390
391 error:
392         server_watch = 0;
393         return FALSE;
394 }
395
396 static gboolean create_tcp(void)
397 {
398         struct sockaddr_in addr;
399         int sk;
400         int reuseaddr = 1;
401         GIOChannel *server_io;
402
403         sk = socket(PF_INET, SOCK_STREAM, 0);
404         if (sk < 0) {
405                 g_print("Can't create tcp/ip socket: %s (%d)\n",
406                                                 strerror(errno), errno);
407                 return FALSE;
408         }
409
410         memset(&addr, 0, sizeof(addr));
411
412         addr.sin_family = AF_INET;
413         addr.sin_addr.s_addr = INADDR_ANY;
414         addr.sin_port = htons(LISTEN_PORT);
415
416         setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
417         if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
418                 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
419                 close(sk);
420                 return FALSE;
421         }
422
423         if (listen(sk, 1) < 0) {
424                 g_print("Can't listen on socket: %s (%d)",
425                                                 strerror(errno), errno);
426                 close(sk);
427                 return FALSE;
428         }
429
430         g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
431
432         server_io = g_io_channel_unix_new(sk);
433         g_io_channel_set_close_on_unref(server_io, TRUE);
434
435         server_watch = g_io_add_watch_full(server_io,
436                                 G_PRIORITY_DEFAULT,
437                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
438                                 on_socket_connected, NULL, NULL);
439
440         g_io_channel_unref(server_io);
441
442         return TRUE;
443 }
444
445 static gboolean has_stk_interface(DBusMessageIter *iter)
446 {
447         DBusMessageIter entry;
448
449         dbus_message_iter_recurse(iter, &entry);
450
451         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
452                 const char *interface;
453
454                 dbus_message_iter_get_basic(&entry, &interface);
455
456                 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
457                         return TRUE;
458
459                 dbus_message_iter_next(&entry);
460         }
461
462         return FALSE;
463 }
464
465 static int send_with_reply(const char *path, const char *interface,
466                                 const char *method, DBusPendingCall **call,
467                                 DBusPendingCallNotifyFunction cb,
468                                 void *user_data, DBusFreeFunction free_func,
469                                 int timeout, int type, ...)
470 {
471         DBusMessage *msg;
472         DBusPendingCall *c;
473         va_list args;
474         int err;
475
476         msg = dbus_message_new_method_call(OFONO_SERVICE, path,
477                                                 interface, method);
478         if (msg == NULL) {
479                 g_printerr("Unable to allocate new D-Bus %s message\n", method);
480                 err = -ENOMEM;
481                 goto fail;
482         }
483
484         va_start(args, type);
485
486         if (!dbus_message_append_args_valist(msg, type, args)) {
487                 va_end(args);
488                 err = -EIO;
489                 goto fail;
490         }
491
492         va_end(args);
493
494         if (timeout > 0)
495                 timeout *= 1000;
496
497         if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
498                 g_printerr("Sending %s failed\n", method);
499                 err = -EIO;
500                 goto fail;
501         }
502
503         if (call != NULL)
504                 *call = c;
505
506         dbus_pending_call_set_notify(c, cb, user_data, free_func);
507         dbus_pending_call_unref(c);
508
509         dbus_message_unref(msg);
510
511         return 0;
512
513 fail:
514         if (free_func && user_data)
515                 free_func(user_data);
516
517         if (msg)
518                 dbus_message_unref(msg);
519
520         return err;
521 }
522
523 static void set_property_reply(DBusPendingCall *call, void *user_data)
524 {
525         DBusMessage *reply = dbus_pending_call_steal_reply(call);
526         DBusError err;
527
528         dbus_error_init(&err);
529
530         if (dbus_set_error_from_message(&err, reply) == TRUE) {
531                 g_printerr("%s: %s\n", err.name, err.message);
532                 dbus_error_free(&err);
533         }
534
535         dbus_message_unref(reply);
536 }
537
538 static int set_property(const char *path, const char *interface,
539                         const char *key, int type, const void *val,
540                         DBusPendingCallNotifyFunction notify,
541                         gpointer user_data,
542                         DBusFreeFunction destroy)
543 {
544         DBusMessage *msg;
545         DBusMessageIter iter, value;
546         DBusPendingCall *call;
547         const char *signature;
548
549         msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
550                                                 "SetProperty");
551         if (msg == NULL)
552                 return -ENOMEM;
553
554         dbus_message_set_auto_start(msg, FALSE);
555
556         dbus_message_iter_init_append(msg, &iter);
557
558         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
559
560         switch (type) {
561         case DBUS_TYPE_BOOLEAN:
562                 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
563                 break;
564         default:
565                 dbus_message_unref(msg);
566                 return -EINVAL;
567         }
568
569         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
570                                                         signature, &value);
571         dbus_message_iter_append_basic(&value, type, val);
572         dbus_message_iter_close_container(&iter, &value);
573
574         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
575                 dbus_message_unref(msg);
576                 return -EIO;
577         }
578
579         dbus_message_unref(msg);
580
581         if (call == NULL)
582                 return -EINVAL;
583
584         dbus_pending_call_set_notify(call, notify, user_data, destroy);
585
586         dbus_pending_call_unref(call);
587
588         return 0;
589 }
590
591 static void register_agent_reply(DBusPendingCall *call, void *user_data)
592 {
593         DBusMessage *reply = dbus_pending_call_steal_reply(call);
594         DBusError err;
595
596         dbus_error_init(&err);
597
598         if (dbus_set_error_from_message(&err, reply) == TRUE) {
599                 g_printerr("%s: %s\n", err.name, err.message);
600                 dbus_error_free(&err);
601         }
602
603         dbus_message_unref(reply);
604
605         state = TEST_STATE_RUNNING;
606 }
607
608 static void register_agent()
609 {
610         const char *path = "/default";
611         int status;
612
613         g_print("Gained STK interface, registering agent...\n");
614
615         status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
616                                         "RegisterAgent", NULL,
617                                         register_agent_reply, NULL, NULL, 1,
618                                         DBUS_TYPE_OBJECT_PATH, &path,
619                                         DBUS_TYPE_INVALID);
620
621         if (status < 0) {
622                 g_printerr("Unable to register agent with oFono\n");
623                 g_main_loop_quit(main_loop);
624                 return;
625         }
626
627         state = TEST_STATE_REGISTERING_AGENT;
628 }
629
630 static gboolean modem_changed(DBusConnection *conn,
631                                 DBusMessage *msg, void *user_data)
632 {
633         DBusMessageIter iter, value;
634         const char *path, *key;
635         gboolean has_stk;
636
637         if (dbus_message_iter_init(msg, &iter) == FALSE)
638                 return TRUE;
639
640         path = dbus_message_get_path(msg);
641
642         if (g_str_equal(STKTEST_PATH, path) == FALSE)
643                 return TRUE;
644
645         dbus_message_iter_get_basic(&iter, &key);
646
647         dbus_message_iter_next(&iter);
648         dbus_message_iter_recurse(&iter, &value);
649
650         if (g_str_equal(key, "Interfaces") == FALSE)
651                 return TRUE;
652
653         has_stk = has_stk_interface(&value);
654
655         switch (state) {
656         case TEST_STATE_POWERING_UP:
657                 if (has_stk)
658                         register_agent();
659                 break;
660         case TEST_STATE_REGISTERING_AGENT:
661         case TEST_STATE_RUNNING:
662                 if (has_stk == FALSE)
663                         g_printerr("Unexpectedly lost STK interface\n");
664                 /* Fall through */
665         case TEST_STATE_POWERING_DOWN:
666                 break;
667         };
668
669         return TRUE;
670 }
671
672 static void powerup(void)
673 {
674         dbus_bool_t powered = TRUE;
675
676         state = TEST_STATE_POWERING_UP;
677         set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
678                         DBUS_TYPE_BOOLEAN, &powered,
679                         set_property_reply, NULL, NULL);
680 }
681
682 static void get_modems_reply(DBusPendingCall *call, void *user_data)
683 {
684         DBusMessage *reply = dbus_pending_call_steal_reply(call);
685         DBusMessageIter iter, list;
686         DBusError err;
687         gboolean found = FALSE;
688
689         dbus_error_init(&err);
690
691         if (dbus_set_error_from_message(&err, reply) == TRUE) {
692                 g_printerr("%s: %s\n", err.name, err.message);
693                 dbus_error_free(&err);
694                 goto done;
695         }
696
697         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
698                 goto done;
699
700         if (dbus_message_iter_init(reply, &iter) == FALSE)
701                 goto done;
702
703         dbus_message_iter_recurse(&iter, &list);
704
705         while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
706                 DBusMessageIter entry;
707                 const char *path;
708
709                 dbus_message_iter_recurse(&list, &entry);
710                 dbus_message_iter_get_basic(&entry, &path);
711
712                 if (g_str_equal(path, STKTEST_PATH))
713                         found = TRUE;
714
715                 dbus_message_iter_next(&list);
716         }
717
718 done:
719         dbus_message_unref(reply);
720
721         if (found == FALSE) {
722                 g_printerr("STK Test modem not found\n");
723                 g_main_loop_quit(main_loop);
724                 return;
725         }
726
727         g_print("Test modem found\n");
728
729         modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
730                                                         STKTEST_PATH,
731                                                         OFONO_MODEM_INTERFACE,
732                                                         "PropertyChanged",
733                                                         modem_changed,
734                                                         NULL, NULL);
735
736         if (create_tcp() == FALSE) {
737                 g_printerr("Unable to listen on modem emulator socket\n");
738                 g_main_loop_quit(main_loop);
739         }
740
741         powerup();
742 }
743
744 static int get_modems(DBusConnection *conn)
745 {
746         DBusMessage *msg;
747         DBusPendingCall *call;
748
749         msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
750                                         OFONO_MANAGER_INTERFACE, "GetModems");
751         if (msg == NULL)
752                 return -ENOMEM;
753
754         dbus_message_set_auto_start(msg, FALSE);
755
756         g_print("getting modems\n");
757
758         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
759                 dbus_message_unref(msg);
760                 return -EIO;
761         }
762
763         dbus_message_unref(msg);
764
765         if (call == NULL)
766                 return -EINVAL;
767
768         dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
769
770         dbus_pending_call_unref(call);
771
772         return 0;
773 }
774
775 static const GDBusMethodTable agent_methods[] = {
776         { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
777         { GDBUS_METHOD("DisplayText",
778                 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
779                                 { "urgent", "b" }), NULL,
780                                 agent_display_text) },
781         { },
782 };
783
784 static void ofono_connect(DBusConnection *conn, void *user_data)
785 {
786         g_print("starting telephony interface\n");
787
788         if (!g_dbus_register_interface(conn, "/default",
789                                         OFONO_STKAGENT_INTERFACE,
790                                         agent_methods, NULL, NULL,
791                                         NULL, NULL)) {
792                 g_printerr("Unable to register local agent");
793                 g_main_loop_quit(main_loop);
794         }
795
796         ofono_running = TRUE;
797         get_modems(conn);
798 }
799
800 static void ofono_disconnect(DBusConnection *conn, void *user_data)
801 {
802         g_print("stopping telephony interface\n");
803
804         g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
805
806         ofono_running = FALSE;
807
808         g_dbus_remove_watch(conn, modem_changed_watch);
809         modem_changed_watch = 0;
810
811         if (server_watch) {
812                 g_source_remove(server_watch);
813                 server_watch = 0;
814         }
815
816         g_at_server_unref(emulator);
817         emulator = NULL;
818 }
819
820 static void sig_term(int sig)
821 {
822         if (__terminated > 0)
823                 return;
824
825         __terminated = 1;
826
827         g_print("Terminating\n");
828
829         g_main_loop_quit(main_loop);
830 }
831
832 static void disconnect_callback(DBusConnection *conn, void *user_data)
833 {
834         g_printerr("D-Bus disconnect\n");
835
836         g_main_loop_quit(main_loop);
837 }
838
839 static void stktest_add_test(const char *name, const char *method,
840                                 const unsigned char *req, unsigned int req_len,
841                                 const unsigned char *rsp, unsigned int rsp_len,
842                                 void *agent_func,
843                                 terminal_response_func tr_func)
844 {
845         struct test *test = g_new0(struct test, 1);
846
847         test->name = g_strdup(name);
848         test->method = g_strdup(method);
849         test->req_pdu = g_memdup(req, req_len);
850         test->req_len = req_len;
851         test->rsp_pdu = g_memdup(rsp, rsp_len);
852         test->rsp_len = rsp_len;
853         test->agent_func = agent_func;
854         test->tr_func = tr_func;
855
856         tests = g_list_append(tests, test);
857 }
858
859 static void __stktest_test_init(void)
860 {
861 }
862
863 static void test_destroy(gpointer user_data)
864 {
865         struct test *test = user_data;
866
867         g_free(test->name);
868         g_free(test->method);
869         g_free(test->req_pdu);
870         g_free(test->rsp_pdu);
871
872         g_free(test);
873 }
874
875 static void __stktest_test_summarize(void)
876 {
877         GList *l;
878
879         g_print("\n\nTest Summary\n");
880         g_print("============\n");
881
882         for (l = tests; l; l = l->next) {
883                 struct test *test = l->data;
884
885                 g_print("%-60s", test->name);
886
887                 switch (test->result) {
888                 case TEST_RESULT_NOT_RUN:
889                         g_print("Not Run\n");
890                         break;
891                 case TEST_RESULT_PASSED:
892                         g_print("Passed\n");
893                         break;
894                 case TEST_RESULT_FAILED:
895                         g_print("Failed\n");
896                 break;
897                 }
898         }
899 }
900
901 static void __stktest_test_cleanup(void)
902 {
903         g_list_free_full(tests, test_destroy);
904         tests = NULL;
905         cur_test = NULL;
906 }
907
908 static gboolean option_version = FALSE;
909
910 static GOptionEntry options[] = {
911         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
912                                 "Show version information and exit" },
913         { NULL },
914 };
915
916 int main(int argc, char **argv)
917 {
918         GOptionContext *context;
919         GError *error = NULL;
920         DBusError err;
921         guint watch;
922         struct sigaction sa;
923
924         context = g_option_context_new(NULL);
925         g_option_context_add_main_entries(context, options, NULL);
926
927         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
928                 if (error != NULL) {
929                         g_printerr("%s\n", error->message);
930                         g_error_free(error);
931                 } else
932                         g_printerr("An unknown error occurred\n");
933                 exit(1);
934         }
935
936         g_option_context_free(context);
937
938         if (option_version == TRUE) {
939                 printf("%s\n", VERSION);
940                 exit(0);
941         }
942
943         __stktest_test_init();
944
945         main_loop = g_main_loop_new(NULL, FALSE);
946
947         dbus_error_init(&err);
948
949         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
950         if (conn == NULL) {
951                 if (dbus_error_is_set(&err) == TRUE) {
952                         fprintf(stderr, "%s\n", err.message);
953                         dbus_error_free(&err);
954                 } else
955                         fprintf(stderr, "Can't register with system bus\n");
956                 exit(1);
957         }
958
959         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
960
961         memset(&sa, 0, sizeof(sa));
962         sa.sa_handler = sig_term;
963         sigaction(SIGINT, &sa, NULL);
964         sigaction(SIGTERM, &sa, NULL);
965
966         watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
967                                 ofono_connect, ofono_disconnect, NULL, NULL);
968
969         g_main_loop_run(main_loop);
970
971         g_dbus_remove_watch(conn, watch);
972
973         if (ofono_running == TRUE)
974                 ofono_disconnect(conn, NULL);
975
976         dbus_connection_unref(conn);
977
978         g_main_loop_unref(main_loop);
979
980         __stktest_test_summarize();
981         __stktest_test_cleanup();
982
983         return 0;
984 }