3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
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.
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.
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
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
37 #include <gatchat/gatserver.h>
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"
45 #define LISTEN_PORT 12765
48 TEST_STATE_POWERING_UP = 1,
49 TEST_STATE_REGISTERING_AGENT,
51 TEST_STATE_POWERING_DOWN,
54 static GMainLoop *main_loop = NULL;
55 static volatile sig_atomic_t __terminated = 0;
58 static DBusConnection *conn;
59 static gboolean ofono_running = FALSE;
60 static guint modem_changed_watch;
61 enum test_state state;
64 static guint server_watch;
65 static GAtServer *emulator;
67 /* Emulated modem state variables */
68 static int modem_mode = 0;
70 static gboolean create_tcp(void);
72 static void server_debug(const char *str, void *data)
74 g_print("%s: %s\n", (char *) data, str);
77 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
78 GAtResult *cmd, gpointer user)
81 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
82 g_at_server_send_info(server, "oFono", TRUE);
83 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
85 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
86 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
89 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
93 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
94 GAtResult *cmd, gpointer user)
97 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
98 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
99 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
101 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
102 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
105 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
109 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
110 GAtResult *cmd, gpointer user)
115 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
116 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
117 g_at_server_send_info(server, buf, TRUE);
118 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
120 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
121 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
124 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
128 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
129 GAtResult *cmd, gpointer user)
132 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
133 g_at_server_send_info(server, "123456789", TRUE);
134 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
136 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
137 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
140 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
144 static gboolean send_ok(gpointer user)
146 GAtServer *server = user;
148 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
153 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
154 GAtResult *cmd, gpointer user)
159 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
160 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
161 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
163 case G_AT_SERVER_REQUEST_TYPE_QUERY:
164 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
165 g_at_server_send_info(server, buf, TRUE);
166 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
168 case G_AT_SERVER_REQUEST_TYPE_SET:
173 g_at_result_iter_init(&iter, cmd);
174 g_at_result_iter_next(&iter, "");
176 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
179 if (mode != 0 && mode != 1)
182 if (modem_mode == mode) {
183 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
188 g_timeout_add_seconds(1, send_ok, server);
198 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
201 static void listen_again(gpointer user_data)
203 if (create_tcp() == TRUE)
206 g_print("Error listening to socket\n");
207 g_main_loop_quit(main_loop);
210 static void setup_emulator(GAtServer *server)
212 g_at_server_set_debug(server, server_debug, "Server");
214 g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
215 g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
216 g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
217 g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
218 g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
220 g_at_server_set_disconnect_function(server, listen_again, NULL);
223 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
226 struct sockaddr saddr;
227 unsigned int len = sizeof(saddr);
229 GIOChannel *client_io = NULL;
234 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
238 client_io = g_io_channel_unix_new(fd);
240 emulator = g_at_server_new(client_io);
241 g_io_channel_unref(client_io);
243 if (emulator == NULL)
246 setup_emulator(emulator);
253 static gboolean create_tcp(void)
255 struct sockaddr_in addr;
258 GIOChannel *server_io;
260 sk = socket(PF_INET, SOCK_STREAM, 0);
262 g_print("Can't create tcp/ip socket: %s (%d)\n",
263 strerror(errno), errno);
267 memset(&addr, 0, sizeof(addr));
269 addr.sin_family = AF_INET;
270 addr.sin_addr.s_addr = INADDR_ANY;
271 addr.sin_port = htons(LISTEN_PORT);
273 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
274 if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
275 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
280 if (listen(sk, 1) < 0) {
281 g_print("Can't listen on socket: %s (%d)",
282 strerror(errno), errno);
287 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
289 server_io = g_io_channel_unix_new(sk);
290 g_io_channel_set_close_on_unref(server_io, TRUE);
292 server_watch = g_io_add_watch_full(server_io,
294 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
295 on_socket_connected, NULL, NULL);
297 g_io_channel_unref(server_io);
302 static gboolean has_stk_interface(DBusMessageIter *iter)
304 DBusMessageIter entry;
306 dbus_message_iter_recurse(iter, &entry);
308 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
309 const char *interface;
311 dbus_message_iter_get_basic(&entry, &interface);
313 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
316 dbus_message_iter_next(&entry);
322 static void set_property_reply(DBusPendingCall *call, void *user_data)
324 DBusMessage *reply = dbus_pending_call_steal_reply(call);
327 dbus_error_init(&err);
329 if (dbus_set_error_from_message(&err, reply) == TRUE) {
330 g_printerr("%s: %s\n", err.name, err.message);
331 dbus_error_free(&err);
334 dbus_message_unref(reply);
337 static int set_property(const char *path, const char *interface,
338 const char *key, int type, const void *val,
339 DBusPendingCallNotifyFunction notify,
341 DBusFreeFunction destroy)
344 DBusMessageIter iter, value;
345 DBusPendingCall *call;
346 const char *signature;
348 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
353 dbus_message_set_auto_start(msg, FALSE);
355 dbus_message_iter_init_append(msg, &iter);
357 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
360 case DBUS_TYPE_BOOLEAN:
361 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
364 dbus_message_unref(msg);
368 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
370 dbus_message_iter_append_basic(&value, type, val);
371 dbus_message_iter_close_container(&iter, &value);
373 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
374 dbus_message_unref(msg);
378 dbus_message_unref(msg);
383 dbus_pending_call_set_notify(call, notify, user_data, destroy);
385 dbus_pending_call_unref(call);
390 static void register_agent()
392 state = TEST_STATE_REGISTERING_AGENT;
393 g_print("Gained STK interface, registering agent...\n");
396 static gboolean modem_changed(DBusConnection *conn,
397 DBusMessage *msg, void *user_data)
399 DBusMessageIter iter, value;
400 const char *path, *key;
403 if (dbus_message_iter_init(msg, &iter) == FALSE)
406 path = dbus_message_get_path(msg);
408 if (g_str_equal(STKTEST_PATH, path) == FALSE)
411 dbus_message_iter_get_basic(&iter, &key);
413 dbus_message_iter_next(&iter);
414 dbus_message_iter_recurse(&iter, &value);
416 if (g_str_equal(key, "Interfaces") == FALSE)
419 has_stk = has_stk_interface(&value);
422 case TEST_STATE_POWERING_UP:
426 case TEST_STATE_REGISTERING_AGENT:
427 case TEST_STATE_RUNNING:
428 if (has_stk == FALSE)
429 g_printerr("Unexpectedly lost STK interface\n");
431 case TEST_STATE_POWERING_DOWN:
438 static void powerup(void)
440 dbus_bool_t powered = TRUE;
442 state = TEST_STATE_POWERING_UP;
443 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
444 DBUS_TYPE_BOOLEAN, &powered,
445 set_property_reply, NULL, NULL);
448 static void get_modems_reply(DBusPendingCall *call, void *user_data)
450 DBusMessage *reply = dbus_pending_call_steal_reply(call);
451 DBusMessageIter iter, list;
453 gboolean found = FALSE;
455 dbus_error_init(&err);
457 if (dbus_set_error_from_message(&err, reply) == TRUE) {
458 g_printerr("%s: %s\n", err.name, err.message);
459 dbus_error_free(&err);
463 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
466 if (dbus_message_iter_init(reply, &iter) == FALSE)
469 dbus_message_iter_recurse(&iter, &list);
471 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
472 DBusMessageIter entry;
475 dbus_message_iter_recurse(&list, &entry);
476 dbus_message_iter_get_basic(&entry, &path);
478 if (g_str_equal(path, STKTEST_PATH))
481 dbus_message_iter_next(&list);
485 dbus_message_unref(reply);
487 if (found == FALSE) {
488 g_printerr("STK Test modem not found\n");
489 g_main_loop_quit(main_loop);
493 g_print("Test modem found\n");
495 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
497 OFONO_MODEM_INTERFACE,
502 if (create_tcp() == FALSE) {
503 g_printerr("Unable to listen on modem emulator socket\n");
504 g_main_loop_quit(main_loop);
510 static int get_modems(DBusConnection *conn)
513 DBusPendingCall *call;
515 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
516 OFONO_MANAGER_INTERFACE, "GetModems");
520 dbus_message_set_auto_start(msg, FALSE);
522 g_print("getting modems\n");
524 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
525 dbus_message_unref(msg);
529 dbus_message_unref(msg);
534 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
536 dbus_pending_call_unref(call);
541 static void ofono_connect(DBusConnection *conn, void *user_data)
543 g_print("starting telephony interface\n");
545 ofono_running = TRUE;
550 static void ofono_disconnect(DBusConnection *conn, void *user_data)
552 g_print("stopping telephony interface\n");
554 ofono_running = FALSE;
556 g_dbus_remove_watch(conn, modem_changed_watch);
557 modem_changed_watch = 0;
560 g_source_remove(server_watch);
564 g_at_server_unref(emulator);
568 static void sig_term(int sig)
570 if (__terminated > 0)
575 g_print("Terminating\n");
577 g_main_loop_quit(main_loop);
580 static void disconnect_callback(DBusConnection *conn, void *user_data)
582 g_printerr("D-Bus disconnect\n");
584 g_main_loop_quit(main_loop);
587 static gboolean option_version = FALSE;
589 static GOptionEntry options[] = {
590 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
591 "Show version information and exit" },
595 int main(int argc, char **argv)
597 GOptionContext *context;
598 GError *error = NULL;
603 context = g_option_context_new(NULL);
604 g_option_context_add_main_entries(context, options, NULL);
606 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
608 g_printerr("%s\n", error->message);
611 g_printerr("An unknown error occurred\n");
615 g_option_context_free(context);
617 if (option_version == TRUE) {
618 printf("%s\n", VERSION);
622 main_loop = g_main_loop_new(NULL, FALSE);
624 dbus_error_init(&err);
626 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
628 if (dbus_error_is_set(&err) == TRUE) {
629 fprintf(stderr, "%s\n", err.message);
630 dbus_error_free(&err);
632 fprintf(stderr, "Can't register with system bus\n");
636 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
638 memset(&sa, 0, sizeof(sa));
639 sa.sa_handler = sig_term;
640 sigaction(SIGINT, &sa, NULL);
641 sigaction(SIGTERM, &sa, NULL);
643 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
644 ofono_connect, ofono_disconnect, NULL, NULL);
646 g_main_loop_run(main_loop);
648 g_dbus_remove_watch(conn, watch);
650 if (ofono_running == TRUE)
651 ofono_disconnect(conn, NULL);
653 dbus_connection_unref(conn);
655 g_main_loop_unref(main_loop);