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 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"
47 #define LISTEN_PORT 12765
50 TEST_STATE_POWERING_UP = 1,
51 TEST_STATE_REGISTERING_AGENT,
53 TEST_STATE_POWERING_DOWN,
56 static GMainLoop *main_loop = NULL;
57 static volatile sig_atomic_t __terminated = 0;
60 static DBusConnection *conn;
61 static gboolean ofono_running = FALSE;
62 static guint modem_changed_watch;
63 enum test_state state;
66 static guint server_watch;
67 static GAtServer *emulator;
69 /* Emulated modem state variables */
70 static int modem_mode = 0;
72 static gboolean create_tcp(void);
74 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
76 return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
77 "Invalid arguments provided");
80 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
83 g_print("Got Release");
85 return dbus_message_new_method_return(msg);
88 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
92 unsigned char icon_id;
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);
101 g_print("Got DisplayText with: %s, %u, %d\n", text, icon_id, urgent);
103 return dbus_message_new_method_return(msg);
106 static void server_debug(const char *str, void *data)
108 g_print("%s: %s\n", (char *) data, str);
111 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
112 GAtResult *cmd, gpointer user)
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);
119 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
120 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
123 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
127 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
128 GAtResult *cmd, gpointer user)
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);
135 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
136 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
139 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
143 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
144 GAtResult *cmd, gpointer user)
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);
154 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
155 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
158 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
162 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
163 GAtResult *cmd, gpointer user)
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);
170 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
171 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
174 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
178 static gboolean send_ok(gpointer user)
180 GAtServer *server = user;
182 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
187 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
188 GAtResult *cmd, gpointer user)
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);
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);
202 case G_AT_SERVER_REQUEST_TYPE_SET:
207 g_at_result_iter_init(&iter, cmd);
208 g_at_result_iter_next(&iter, "");
210 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
213 if (mode != 0 && mode != 1)
216 if (modem_mode == mode) {
217 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
222 g_timeout_add_seconds(1, send_ok, server);
232 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
235 static void listen_again(gpointer user_data)
237 if (create_tcp() == TRUE)
240 g_print("Error listening to socket\n");
241 g_main_loop_quit(main_loop);
244 static void setup_emulator(GAtServer *server)
246 g_at_server_set_debug(server, server_debug, "Server");
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);
254 g_at_server_set_disconnect_function(server, listen_again, NULL);
257 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
260 struct sockaddr saddr;
261 unsigned int len = sizeof(saddr);
263 GIOChannel *client_io = NULL;
268 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
272 client_io = g_io_channel_unix_new(fd);
274 emulator = g_at_server_new(client_io);
275 g_io_channel_unref(client_io);
277 if (emulator == NULL)
280 setup_emulator(emulator);
287 static gboolean create_tcp(void)
289 struct sockaddr_in addr;
292 GIOChannel *server_io;
294 sk = socket(PF_INET, SOCK_STREAM, 0);
296 g_print("Can't create tcp/ip socket: %s (%d)\n",
297 strerror(errno), errno);
301 memset(&addr, 0, sizeof(addr));
303 addr.sin_family = AF_INET;
304 addr.sin_addr.s_addr = INADDR_ANY;
305 addr.sin_port = htons(LISTEN_PORT);
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);
314 if (listen(sk, 1) < 0) {
315 g_print("Can't listen on socket: %s (%d)",
316 strerror(errno), errno);
321 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
323 server_io = g_io_channel_unix_new(sk);
324 g_io_channel_set_close_on_unref(server_io, TRUE);
326 server_watch = g_io_add_watch_full(server_io,
328 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
329 on_socket_connected, NULL, NULL);
331 g_io_channel_unref(server_io);
336 static gboolean has_stk_interface(DBusMessageIter *iter)
338 DBusMessageIter entry;
340 dbus_message_iter_recurse(iter, &entry);
342 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
343 const char *interface;
345 dbus_message_iter_get_basic(&entry, &interface);
347 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
350 dbus_message_iter_next(&entry);
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, ...)
367 msg = dbus_message_new_method_call(OFONO_SERVICE, path,
370 g_printerr("Unable to allocate new D-Bus %s message\n", method);
375 va_start(args, type);
377 if (!dbus_message_append_args_valist(msg, type, args)) {
388 if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
389 g_printerr("Sending %s failed\n", method);
397 dbus_pending_call_set_notify(c, cb, user_data, free_func);
398 dbus_pending_call_unref(c);
400 dbus_message_unref(msg);
405 if (free_func && user_data)
406 free_func(user_data);
409 dbus_message_unref(msg);
414 static void set_property_reply(DBusPendingCall *call, void *user_data)
416 DBusMessage *reply = dbus_pending_call_steal_reply(call);
419 dbus_error_init(&err);
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);
426 dbus_message_unref(reply);
429 static int set_property(const char *path, const char *interface,
430 const char *key, int type, const void *val,
431 DBusPendingCallNotifyFunction notify,
433 DBusFreeFunction destroy)
436 DBusMessageIter iter, value;
437 DBusPendingCall *call;
438 const char *signature;
440 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
445 dbus_message_set_auto_start(msg, FALSE);
447 dbus_message_iter_init_append(msg, &iter);
449 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
452 case DBUS_TYPE_BOOLEAN:
453 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
456 dbus_message_unref(msg);
460 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
462 dbus_message_iter_append_basic(&value, type, val);
463 dbus_message_iter_close_container(&iter, &value);
465 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
466 dbus_message_unref(msg);
470 dbus_message_unref(msg);
475 dbus_pending_call_set_notify(call, notify, user_data, destroy);
477 dbus_pending_call_unref(call);
482 static void register_agent()
484 state = TEST_STATE_REGISTERING_AGENT;
485 g_print("Gained STK interface, registering agent...\n");
488 static gboolean modem_changed(DBusConnection *conn,
489 DBusMessage *msg, void *user_data)
491 DBusMessageIter iter, value;
492 const char *path, *key;
495 if (dbus_message_iter_init(msg, &iter) == FALSE)
498 path = dbus_message_get_path(msg);
500 if (g_str_equal(STKTEST_PATH, path) == FALSE)
503 dbus_message_iter_get_basic(&iter, &key);
505 dbus_message_iter_next(&iter);
506 dbus_message_iter_recurse(&iter, &value);
508 if (g_str_equal(key, "Interfaces") == FALSE)
511 has_stk = has_stk_interface(&value);
514 case TEST_STATE_POWERING_UP:
518 case TEST_STATE_REGISTERING_AGENT:
519 case TEST_STATE_RUNNING:
520 if (has_stk == FALSE)
521 g_printerr("Unexpectedly lost STK interface\n");
523 case TEST_STATE_POWERING_DOWN:
530 static void powerup(void)
532 dbus_bool_t powered = TRUE;
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);
540 static void get_modems_reply(DBusPendingCall *call, void *user_data)
542 DBusMessage *reply = dbus_pending_call_steal_reply(call);
543 DBusMessageIter iter, list;
545 gboolean found = FALSE;
547 dbus_error_init(&err);
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);
555 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
558 if (dbus_message_iter_init(reply, &iter) == FALSE)
561 dbus_message_iter_recurse(&iter, &list);
563 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
564 DBusMessageIter entry;
567 dbus_message_iter_recurse(&list, &entry);
568 dbus_message_iter_get_basic(&entry, &path);
570 if (g_str_equal(path, STKTEST_PATH))
573 dbus_message_iter_next(&list);
577 dbus_message_unref(reply);
579 if (found == FALSE) {
580 g_printerr("STK Test modem not found\n");
581 g_main_loop_quit(main_loop);
585 g_print("Test modem found\n");
587 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
589 OFONO_MODEM_INTERFACE,
594 if (create_tcp() == FALSE) {
595 g_printerr("Unable to listen on modem emulator socket\n");
596 g_main_loop_quit(main_loop);
602 static int get_modems(DBusConnection *conn)
605 DBusPendingCall *call;
607 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
608 OFONO_MANAGER_INTERFACE, "GetModems");
612 dbus_message_set_auto_start(msg, FALSE);
614 g_print("getting modems\n");
616 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
617 dbus_message_unref(msg);
621 dbus_message_unref(msg);
626 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
628 dbus_pending_call_unref(call);
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) },
642 static void ofono_connect(DBusConnection *conn, void *user_data)
644 g_print("starting telephony interface\n");
646 if (!g_dbus_register_interface(conn, "/default",
647 OFONO_STKAGENT_INTERFACE,
648 agent_methods, NULL, NULL,
650 g_printerr("Unable to register local agent");
651 g_main_loop_quit(main_loop);
654 ofono_running = TRUE;
658 static void ofono_disconnect(DBusConnection *conn, void *user_data)
660 g_print("stopping telephony interface\n");
662 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
664 ofono_running = FALSE;
666 g_dbus_remove_watch(conn, modem_changed_watch);
667 modem_changed_watch = 0;
670 g_source_remove(server_watch);
674 g_at_server_unref(emulator);
678 static void sig_term(int sig)
680 if (__terminated > 0)
685 g_print("Terminating\n");
687 g_main_loop_quit(main_loop);
690 static void disconnect_callback(DBusConnection *conn, void *user_data)
692 g_printerr("D-Bus disconnect\n");
694 g_main_loop_quit(main_loop);
697 static gboolean option_version = FALSE;
699 static GOptionEntry options[] = {
700 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
701 "Show version information and exit" },
705 int main(int argc, char **argv)
707 GOptionContext *context;
708 GError *error = NULL;
713 context = g_option_context_new(NULL);
714 g_option_context_add_main_entries(context, options, NULL);
716 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
718 g_printerr("%s\n", error->message);
721 g_printerr("An unknown error occurred\n");
725 g_option_context_free(context);
727 if (option_version == TRUE) {
728 printf("%s\n", VERSION);
732 main_loop = g_main_loop_new(NULL, FALSE);
734 dbus_error_init(&err);
736 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
738 if (dbus_error_is_set(&err) == TRUE) {
739 fprintf(stderr, "%s\n", err.message);
740 dbus_error_free(&err);
742 fprintf(stderr, "Can't register with system bus\n");
746 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
748 memset(&sa, 0, sizeof(sa));
749 sa.sa_handler = sig_term;
750 sigaction(SIGINT, &sa, NULL);
751 sigaction(SIGTERM, &sa, NULL);
753 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
754 ofono_connect, ofono_disconnect, NULL, NULL);
756 g_main_loop_run(main_loop);
758 g_dbus_remove_watch(conn, watch);
760 if (ofono_running == TRUE)
761 ofono_disconnect(conn, NULL);
763 dbus_connection_unref(conn);
765 g_main_loop_unref(main_loop);