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 const char *to_hex(const unsigned char *data, unsigned int len)
76 static char buf[512+1];
79 for (i = 0; i < len; i++)
80 sprintf(buf + i * 2, "%02hhX", data[i]);
87 static void send_proactive_command(const unsigned char *pdu, unsigned int len)
91 sprintf(buf, "+CUSATP: %s", to_hex(pdu, len));
92 g_at_server_send_unsolicited(emulator, buf);
95 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
97 return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
98 "Invalid arguments provided");
101 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
104 g_print("Got Release");
106 return dbus_message_new_method_return(msg);
109 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
113 unsigned char icon_id;
116 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &text,
117 DBUS_TYPE_BYTE, &icon_id,
118 DBUS_TYPE_BOOLEAN, &urgent,
119 DBUS_TYPE_INVALID) == FALSE)
120 return stktest_error_invalid_args(msg);
122 g_print("Got DisplayText with: %s, %u, %d\n", text, icon_id, urgent);
124 return dbus_message_new_method_return(msg);
127 static void server_debug(const char *str, void *data)
129 g_print("%s: %s\n", (char *) data, str);
132 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
133 GAtResult *cmd, gpointer user)
136 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
137 g_at_server_send_info(server, "oFono", TRUE);
138 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
140 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
141 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
144 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
148 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
149 GAtResult *cmd, gpointer user)
152 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
153 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
154 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
156 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
157 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
160 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
164 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
165 GAtResult *cmd, gpointer user)
170 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
171 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
172 g_at_server_send_info(server, buf, TRUE);
173 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
175 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
176 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
179 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
183 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
184 GAtResult *cmd, gpointer user)
187 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
188 g_at_server_send_info(server, "123456789", TRUE);
189 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
191 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
192 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
195 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
199 static gboolean send_ok(gpointer user)
201 GAtServer *server = user;
203 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
208 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
209 GAtResult *cmd, gpointer user)
214 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
215 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
216 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
218 case G_AT_SERVER_REQUEST_TYPE_QUERY:
219 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
220 g_at_server_send_info(server, buf, TRUE);
221 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
223 case G_AT_SERVER_REQUEST_TYPE_SET:
228 g_at_result_iter_init(&iter, cmd);
229 g_at_result_iter_next(&iter, "");
231 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
234 if (mode != 0 && mode != 1)
237 if (modem_mode == mode) {
238 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
243 g_timeout_add_seconds(1, send_ok, server);
253 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
256 static void cusatt_cb(GAtServer *server, GAtServerRequestType type,
257 GAtResult *cmd, gpointer user)
262 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
263 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
265 case G_AT_SERVER_REQUEST_TYPE_QUERY:
266 g_at_server_send_ext_final(server, "+CME ERROR: 4");
268 case G_AT_SERVER_REQUEST_TYPE_SET:
271 const unsigned char *pdu;
274 g_at_result_iter_init(&iter, cmd);
275 g_at_result_iter_next(&iter, "");
277 if (g_at_result_iter_next_hexstring(&iter, &pdu, &len) == FALSE)
280 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
290 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
292 static void listen_again(gpointer user_data)
294 if (create_tcp() == TRUE)
297 g_print("Error listening to socket\n");
298 g_main_loop_quit(main_loop);
301 static void setup_emulator(GAtServer *server)
303 g_at_server_set_debug(server, server_debug, "Server");
305 g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
306 g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
307 g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
308 g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
309 g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
310 g_at_server_register(server, "+CUSATT", cusatt_cb, NULL, NULL);
312 g_at_server_set_disconnect_function(server, listen_again, NULL);
315 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
318 struct sockaddr saddr;
319 unsigned int len = sizeof(saddr);
321 GIOChannel *client_io = NULL;
326 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
330 client_io = g_io_channel_unix_new(fd);
332 emulator = g_at_server_new(client_io);
333 g_io_channel_unref(client_io);
335 if (emulator == NULL)
338 setup_emulator(emulator);
345 static gboolean create_tcp(void)
347 struct sockaddr_in addr;
350 GIOChannel *server_io;
352 sk = socket(PF_INET, SOCK_STREAM, 0);
354 g_print("Can't create tcp/ip socket: %s (%d)\n",
355 strerror(errno), errno);
359 memset(&addr, 0, sizeof(addr));
361 addr.sin_family = AF_INET;
362 addr.sin_addr.s_addr = INADDR_ANY;
363 addr.sin_port = htons(LISTEN_PORT);
365 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
366 if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
367 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
372 if (listen(sk, 1) < 0) {
373 g_print("Can't listen on socket: %s (%d)",
374 strerror(errno), errno);
379 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
381 server_io = g_io_channel_unix_new(sk);
382 g_io_channel_set_close_on_unref(server_io, TRUE);
384 server_watch = g_io_add_watch_full(server_io,
386 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
387 on_socket_connected, NULL, NULL);
389 g_io_channel_unref(server_io);
394 static gboolean has_stk_interface(DBusMessageIter *iter)
396 DBusMessageIter entry;
398 dbus_message_iter_recurse(iter, &entry);
400 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
401 const char *interface;
403 dbus_message_iter_get_basic(&entry, &interface);
405 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
408 dbus_message_iter_next(&entry);
414 static int send_with_reply(const char *path, const char *interface,
415 const char *method, DBusPendingCall **call,
416 DBusPendingCallNotifyFunction cb,
417 void *user_data, DBusFreeFunction free_func,
418 int timeout, int type, ...)
425 msg = dbus_message_new_method_call(OFONO_SERVICE, path,
428 g_printerr("Unable to allocate new D-Bus %s message\n", method);
433 va_start(args, type);
435 if (!dbus_message_append_args_valist(msg, type, args)) {
446 if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
447 g_printerr("Sending %s failed\n", method);
455 dbus_pending_call_set_notify(c, cb, user_data, free_func);
456 dbus_pending_call_unref(c);
458 dbus_message_unref(msg);
463 if (free_func && user_data)
464 free_func(user_data);
467 dbus_message_unref(msg);
472 static void set_property_reply(DBusPendingCall *call, void *user_data)
474 DBusMessage *reply = dbus_pending_call_steal_reply(call);
477 dbus_error_init(&err);
479 if (dbus_set_error_from_message(&err, reply) == TRUE) {
480 g_printerr("%s: %s\n", err.name, err.message);
481 dbus_error_free(&err);
484 dbus_message_unref(reply);
487 static int set_property(const char *path, const char *interface,
488 const char *key, int type, const void *val,
489 DBusPendingCallNotifyFunction notify,
491 DBusFreeFunction destroy)
494 DBusMessageIter iter, value;
495 DBusPendingCall *call;
496 const char *signature;
498 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
503 dbus_message_set_auto_start(msg, FALSE);
505 dbus_message_iter_init_append(msg, &iter);
507 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
510 case DBUS_TYPE_BOOLEAN:
511 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
514 dbus_message_unref(msg);
518 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
520 dbus_message_iter_append_basic(&value, type, val);
521 dbus_message_iter_close_container(&iter, &value);
523 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
524 dbus_message_unref(msg);
528 dbus_message_unref(msg);
533 dbus_pending_call_set_notify(call, notify, user_data, destroy);
535 dbus_pending_call_unref(call);
540 static void register_agent_reply(DBusPendingCall *call, void *user_data)
542 DBusMessage *reply = dbus_pending_call_steal_reply(call);
545 dbus_error_init(&err);
547 if (dbus_set_error_from_message(&err, reply) == TRUE) {
548 g_printerr("%s: %s\n", err.name, err.message);
549 dbus_error_free(&err);
552 dbus_message_unref(reply);
554 state = TEST_STATE_RUNNING;
557 static void register_agent()
559 const char *path = "/default";
562 g_print("Gained STK interface, registering agent...\n");
564 status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
565 "RegisterAgent", NULL,
566 register_agent_reply, NULL, NULL, 1,
567 DBUS_TYPE_OBJECT_PATH, &path,
571 g_printerr("Unable to register agent with oFono\n");
572 g_main_loop_quit(main_loop);
576 state = TEST_STATE_REGISTERING_AGENT;
579 static gboolean modem_changed(DBusConnection *conn,
580 DBusMessage *msg, void *user_data)
582 DBusMessageIter iter, value;
583 const char *path, *key;
586 if (dbus_message_iter_init(msg, &iter) == FALSE)
589 path = dbus_message_get_path(msg);
591 if (g_str_equal(STKTEST_PATH, path) == FALSE)
594 dbus_message_iter_get_basic(&iter, &key);
596 dbus_message_iter_next(&iter);
597 dbus_message_iter_recurse(&iter, &value);
599 if (g_str_equal(key, "Interfaces") == FALSE)
602 has_stk = has_stk_interface(&value);
605 case TEST_STATE_POWERING_UP:
609 case TEST_STATE_REGISTERING_AGENT:
610 case TEST_STATE_RUNNING:
611 if (has_stk == FALSE)
612 g_printerr("Unexpectedly lost STK interface\n");
614 case TEST_STATE_POWERING_DOWN:
621 static void powerup(void)
623 dbus_bool_t powered = TRUE;
625 state = TEST_STATE_POWERING_UP;
626 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
627 DBUS_TYPE_BOOLEAN, &powered,
628 set_property_reply, NULL, NULL);
631 static void get_modems_reply(DBusPendingCall *call, void *user_data)
633 DBusMessage *reply = dbus_pending_call_steal_reply(call);
634 DBusMessageIter iter, list;
636 gboolean found = FALSE;
638 dbus_error_init(&err);
640 if (dbus_set_error_from_message(&err, reply) == TRUE) {
641 g_printerr("%s: %s\n", err.name, err.message);
642 dbus_error_free(&err);
646 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
649 if (dbus_message_iter_init(reply, &iter) == FALSE)
652 dbus_message_iter_recurse(&iter, &list);
654 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
655 DBusMessageIter entry;
658 dbus_message_iter_recurse(&list, &entry);
659 dbus_message_iter_get_basic(&entry, &path);
661 if (g_str_equal(path, STKTEST_PATH))
664 dbus_message_iter_next(&list);
668 dbus_message_unref(reply);
670 if (found == FALSE) {
671 g_printerr("STK Test modem not found\n");
672 g_main_loop_quit(main_loop);
676 g_print("Test modem found\n");
678 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
680 OFONO_MODEM_INTERFACE,
685 if (create_tcp() == FALSE) {
686 g_printerr("Unable to listen on modem emulator socket\n");
687 g_main_loop_quit(main_loop);
693 static int get_modems(DBusConnection *conn)
696 DBusPendingCall *call;
698 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
699 OFONO_MANAGER_INTERFACE, "GetModems");
703 dbus_message_set_auto_start(msg, FALSE);
705 g_print("getting modems\n");
707 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
708 dbus_message_unref(msg);
712 dbus_message_unref(msg);
717 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
719 dbus_pending_call_unref(call);
724 static const GDBusMethodTable agent_methods[] = {
725 { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
726 { GDBUS_METHOD("DisplayText",
727 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
728 { "urgent", "b" }), NULL,
729 agent_display_text) },
733 static void ofono_connect(DBusConnection *conn, void *user_data)
735 g_print("starting telephony interface\n");
737 if (!g_dbus_register_interface(conn, "/default",
738 OFONO_STKAGENT_INTERFACE,
739 agent_methods, NULL, NULL,
741 g_printerr("Unable to register local agent");
742 g_main_loop_quit(main_loop);
745 ofono_running = TRUE;
749 static void ofono_disconnect(DBusConnection *conn, void *user_data)
751 g_print("stopping telephony interface\n");
753 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
755 ofono_running = FALSE;
757 g_dbus_remove_watch(conn, modem_changed_watch);
758 modem_changed_watch = 0;
761 g_source_remove(server_watch);
765 g_at_server_unref(emulator);
769 static void sig_term(int sig)
771 if (__terminated > 0)
776 g_print("Terminating\n");
778 g_main_loop_quit(main_loop);
781 static void disconnect_callback(DBusConnection *conn, void *user_data)
783 g_printerr("D-Bus disconnect\n");
785 g_main_loop_quit(main_loop);
788 static gboolean option_version = FALSE;
790 static GOptionEntry options[] = {
791 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
792 "Show version information and exit" },
796 int main(int argc, char **argv)
798 GOptionContext *context;
799 GError *error = NULL;
804 context = g_option_context_new(NULL);
805 g_option_context_add_main_entries(context, options, NULL);
807 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
809 g_printerr("%s\n", error->message);
812 g_printerr("An unknown error occurred\n");
816 g_option_context_free(context);
818 if (option_version == TRUE) {
819 printf("%s\n", VERSION);
823 main_loop = g_main_loop_new(NULL, FALSE);
825 dbus_error_init(&err);
827 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
829 if (dbus_error_is_set(&err) == TRUE) {
830 fprintf(stderr, "%s\n", err.message);
831 dbus_error_free(&err);
833 fprintf(stderr, "Can't register with system bus\n");
837 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
839 memset(&sa, 0, sizeof(sa));
840 sa.sa_handler = sig_term;
841 sigaction(SIGINT, &sa, NULL);
842 sigaction(SIGTERM, &sa, NULL);
844 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
845 ofono_connect, ofono_disconnect, NULL, NULL);
847 g_main_loop_run(main_loop);
849 g_dbus_remove_watch(conn, watch);
851 if (ofono_running == TRUE)
852 ofono_disconnect(conn, NULL);
854 dbus_connection_unref(conn);
856 g_main_loop_unref(main_loop);