stktest: Add initial modem emulator logic
[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 OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
42 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
43 #define OFONO_STK_INTERFACE             OFONO_SERVICE ".SimToolkit"
44
45 #define LISTEN_PORT     12765
46
47 static GMainLoop *main_loop = NULL;
48 static volatile sig_atomic_t __terminated = 0;
49
50 /* DBus related */
51 static DBusConnection *conn;
52 static gboolean ofono_running = FALSE;
53 static guint modem_changed_watch;
54 static gboolean stk_ready = FALSE;
55
56 /* Emulator setup */
57 static guint server_watch;
58 static GAtServer *emulator;
59
60 /* Emulated modem state variables */
61 static int modem_mode = 0;
62
63 static gboolean create_tcp(void);
64
65 static void server_debug(const char *str, void *data)
66 {
67         g_print("%s: %s\n", (char *) data, str);
68 }
69
70 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
71                         GAtResult *cmd, gpointer user)
72 {
73         switch (type) {
74         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
75                 g_at_server_send_info(server, "oFono", TRUE);
76                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
77                 break;
78         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
79                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
80                 break;
81         default:
82                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
83         };
84 }
85
86 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
87                         GAtResult *cmd, gpointer user)
88 {
89         switch (type) {
90         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
91                 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
92                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
93                 break;
94         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
95                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
96                 break;
97         default:
98                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
99         };
100 }
101
102 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
103                         GAtResult *cmd, gpointer user)
104 {
105         char buf[256];
106
107         switch (type) {
108         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
109                 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
110                 g_at_server_send_info(server, buf, TRUE);
111                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
112                 break;
113         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
114                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
115                 break;
116         default:
117                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
118         };
119 }
120
121 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
122                         GAtResult *cmd, gpointer user)
123 {
124         switch (type) {
125         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
126                 g_at_server_send_info(server, "123456789", TRUE);
127                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
128                 break;
129         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
130                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
131                 break;
132         default:
133                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
134         };
135 }
136
137 static gboolean send_ok(gpointer user)
138 {
139         GAtServer *server = user;
140
141         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
142
143         return FALSE;
144 }
145
146 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
147                         GAtResult *cmd, gpointer user)
148 {
149         char buf[12];
150
151         switch (type) {
152         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
153                 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
154                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
155                 break;
156         case G_AT_SERVER_REQUEST_TYPE_QUERY:
157                 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
158                 g_at_server_send_info(server, buf, TRUE);
159                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
160                 break;
161         case G_AT_SERVER_REQUEST_TYPE_SET:
162         {
163                 GAtResultIter iter;
164                 int mode;
165
166                 g_at_result_iter_init(&iter, cmd);
167                 g_at_result_iter_next(&iter, "");
168
169                 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
170                         goto error;
171
172                 if (mode != 0 && mode != 1)
173                         goto error;
174
175                 if (modem_mode == mode) {
176                         g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
177                         break;
178                 }
179
180                 modem_mode = mode;
181                 g_timeout_add_seconds(1, send_ok, server);
182                 break;
183         }
184         default:
185                 goto error;
186         };
187
188         return;
189
190 error:
191         g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
192 }
193
194 static void listen_again(gpointer user_data)
195 {
196         if (create_tcp() == TRUE)
197                 return;
198
199         g_print("Error listening to socket\n");
200         g_main_loop_quit(main_loop);
201 }
202
203 static void setup_emulator(GAtServer *server)
204 {
205         g_at_server_set_debug(server, server_debug, "Server");
206
207         g_at_server_register(server, "+CGMI",    cgmi_cb,    NULL, NULL);
208         g_at_server_register(server, "+CGMM",    cgmm_cb,    NULL, NULL);
209         g_at_server_register(server, "+CGMR",    cgmr_cb,    NULL, NULL);
210         g_at_server_register(server, "+CGSN",    cgsn_cb,    NULL, NULL);
211         g_at_server_register(server, "+CFUN",    cfun_cb,    NULL, NULL);
212
213         g_at_server_set_disconnect_function(server, listen_again, NULL);
214 }
215
216 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
217                                                         gpointer user)
218 {
219         struct sockaddr saddr;
220         unsigned int len = sizeof(saddr);
221         int fd;
222         GIOChannel *client_io = NULL;
223
224         if (cond != G_IO_IN)
225                 goto error;
226
227         fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
228         if (fd == -1)
229                 goto error;
230
231         client_io = g_io_channel_unix_new(fd);
232
233         emulator = g_at_server_new(client_io);
234         g_io_channel_unref(client_io);
235
236         if (emulator == NULL)
237                 goto error;
238
239         setup_emulator(emulator);
240
241 error:
242         server_watch = 0;
243         return FALSE;
244 }
245
246 static gboolean create_tcp(void)
247 {
248         struct sockaddr_in addr;
249         int sk;
250         int reuseaddr = 1;
251         GIOChannel *server_io;
252
253         sk = socket(PF_INET, SOCK_STREAM, 0);
254         if (sk < 0) {
255                 g_print("Can't create tcp/ip socket: %s (%d)\n",
256                                                 strerror(errno), errno);
257                 return FALSE;
258         }
259
260         memset(&addr, 0, sizeof(addr));
261
262         addr.sin_family = AF_INET;
263         addr.sin_addr.s_addr = INADDR_ANY;
264         addr.sin_port = htons(LISTEN_PORT);
265
266         setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
267         if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
268                 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
269                 close(sk);
270                 return FALSE;
271         }
272
273         if (listen(sk, 1) < 0) {
274                 g_print("Can't listen on socket: %s (%d)",
275                                                 strerror(errno), errno);
276                 close(sk);
277                 return FALSE;
278         }
279
280         g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
281
282         server_io = g_io_channel_unix_new(sk);
283         g_io_channel_set_close_on_unref(server_io, TRUE);
284
285         server_watch = g_io_add_watch_full(server_io,
286                                 G_PRIORITY_DEFAULT,
287                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
288                                 on_socket_connected, NULL, NULL);
289
290         g_io_channel_unref(server_io);
291
292         return TRUE;
293 }
294
295 static gboolean has_stk_interface(DBusMessageIter *iter)
296 {
297         DBusMessageIter entry;
298
299         dbus_message_iter_recurse(iter, &entry);
300
301         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
302                 const char *interface;
303
304                 dbus_message_iter_get_basic(&entry, &interface);
305
306                 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
307                         return TRUE;
308
309                 dbus_message_iter_next(&entry);
310         }
311
312         return FALSE;
313 }
314
315 static gboolean modem_changed(DBusConnection *conn,
316                                 DBusMessage *msg, void *user_data)
317 {
318         DBusMessageIter iter, value;
319         const char *path, *key;
320         gboolean has_stk;
321
322         if (dbus_message_iter_init(msg, &iter) == FALSE)
323                 return TRUE;
324
325         path = dbus_message_get_path(msg);
326
327         if (g_str_equal(STKTEST_PATH, path) == FALSE)
328                 return TRUE;
329
330         dbus_message_iter_get_basic(&iter, &key);
331
332         dbus_message_iter_next(&iter);
333         dbus_message_iter_recurse(&iter, &value);
334
335         if (g_str_equal(key, "Interfaces") == FALSE)
336                 return TRUE;
337
338         has_stk = has_stk_interface(&value);
339
340         if (stk_ready && has_stk == FALSE) {
341                 stk_ready = FALSE;
342                 g_print("Lost STK interface\n");
343         } else if (stk_ready == FALSE && has_stk == TRUE) {
344                 stk_ready = TRUE;
345                 g_print("Gained STK interface\n");
346         }
347
348         return TRUE;
349 }
350
351 static void get_modems_reply(DBusPendingCall *call, void *user_data)
352 {
353         DBusMessage *reply = dbus_pending_call_steal_reply(call);
354         DBusMessageIter iter, list;
355         DBusError err;
356         gboolean found = FALSE;
357
358         dbus_error_init(&err);
359
360         if (dbus_set_error_from_message(&err, reply) == TRUE) {
361                 g_printerr("%s: %s\n", err.name, err.message);
362                 dbus_error_free(&err);
363                 goto done;
364         }
365
366         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
367                 goto done;
368
369         if (dbus_message_iter_init(reply, &iter) == FALSE)
370                 goto done;
371
372         dbus_message_iter_recurse(&iter, &list);
373
374         while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
375                 DBusMessageIter entry;
376                 const char *path;
377
378                 dbus_message_iter_recurse(&list, &entry);
379                 dbus_message_iter_get_basic(&entry, &path);
380
381                 if (g_str_equal(path, STKTEST_PATH))
382                         found = TRUE;
383
384                 dbus_message_iter_next(&list);
385         }
386
387 done:
388         dbus_message_unref(reply);
389
390         if (found == FALSE) {
391                 g_printerr("STK Test modem not found\n");
392                 g_main_loop_quit(main_loop);
393                 return;
394         }
395
396         g_print("Test modem found\n");
397
398         modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
399                                                         STKTEST_PATH,
400                                                         OFONO_MODEM_INTERFACE,
401                                                         "PropertyChanged",
402                                                         modem_changed,
403                                                         NULL, NULL);
404
405         if (create_tcp() == FALSE) {
406                 g_printerr("Unable to listen on modem emulator socket\n");
407                 g_main_loop_quit(main_loop);
408         }
409 }
410
411 static int get_modems(DBusConnection *conn)
412 {
413         DBusMessage *msg;
414         DBusPendingCall *call;
415
416         msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
417                                         OFONO_MANAGER_INTERFACE, "GetModems");
418         if (msg == NULL)
419                 return -ENOMEM;
420
421         dbus_message_set_auto_start(msg, FALSE);
422
423         g_print("getting modems\n");
424
425         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
426                 dbus_message_unref(msg);
427                 return -EIO;
428         }
429
430         dbus_message_unref(msg);
431
432         if (call == NULL)
433                 return -EINVAL;
434
435         dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
436
437         dbus_pending_call_unref(call);
438
439         return 0;
440 }
441
442 static void ofono_connect(DBusConnection *conn, void *user_data)
443 {
444         g_print("starting telephony interface\n");
445
446         ofono_running = TRUE;
447
448         get_modems(conn);
449 }
450
451 static void ofono_disconnect(DBusConnection *conn, void *user_data)
452 {
453         g_print("stopping telephony interface\n");
454
455         ofono_running = FALSE;
456
457         g_dbus_remove_watch(conn, modem_changed_watch);
458         modem_changed_watch = 0;
459
460         if (server_watch) {
461                 g_source_remove(server_watch);
462                 server_watch = 0;
463         }
464
465         g_at_server_unref(emulator);
466         emulator = NULL;
467 }
468
469 static void sig_term(int sig)
470 {
471         if (__terminated > 0)
472                 return;
473
474         __terminated = 1;
475
476         g_print("Terminating\n");
477
478         g_main_loop_quit(main_loop);
479 }
480
481 static void disconnect_callback(DBusConnection *conn, void *user_data)
482 {
483         g_printerr("D-Bus disconnect\n");
484
485         g_main_loop_quit(main_loop);
486 }
487
488 static gboolean option_version = FALSE;
489
490 static GOptionEntry options[] = {
491         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
492                                 "Show version information and exit" },
493         { NULL },
494 };
495
496 int main(int argc, char **argv)
497 {
498         GOptionContext *context;
499         GError *error = NULL;
500         DBusError err;
501         guint watch;
502         struct sigaction sa;
503
504         context = g_option_context_new(NULL);
505         g_option_context_add_main_entries(context, options, NULL);
506
507         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
508                 if (error != NULL) {
509                         g_printerr("%s\n", error->message);
510                         g_error_free(error);
511                 } else
512                         g_printerr("An unknown error occurred\n");
513                 exit(1);
514         }
515
516         g_option_context_free(context);
517
518         if (option_version == TRUE) {
519                 printf("%s\n", VERSION);
520                 exit(0);
521         }
522
523         main_loop = g_main_loop_new(NULL, FALSE);
524
525         dbus_error_init(&err);
526
527         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
528         if (conn == NULL) {
529                 if (dbus_error_is_set(&err) == TRUE) {
530                         fprintf(stderr, "%s\n", err.message);
531                         dbus_error_free(&err);
532                 } else
533                         fprintf(stderr, "Can't register with system bus\n");
534                 exit(1);
535         }
536
537         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
538
539         memset(&sa, 0, sizeof(sa));
540         sa.sa_handler = sig_term;
541         sigaction(SIGINT, &sa, NULL);
542         sigaction(SIGTERM, &sa, NULL);
543
544         watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
545                                 ofono_connect, ofono_disconnect, NULL, NULL);
546
547         g_main_loop_run(main_loop);
548
549         g_dbus_remove_watch(conn, watch);
550
551         if (ofono_running == TRUE)
552                 ofono_disconnect(conn, NULL);
553
554         dbus_connection_unref(conn);
555
556         g_main_loop_unref(main_loop);
557
558         return 0;
559 }