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.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"
48 #define LISTEN_PORT 12765
51 TEST_STATE_POWERING_UP = 1,
52 TEST_STATE_REGISTERING_AGENT,
54 TEST_STATE_POWERING_DOWN,
58 TEST_RESULT_NOT_RUN = 0,
63 typedef DBusMessage *(*display_text_cb_t)(DBusMessage *msg, const char *text,
64 unsigned char icon_id,
66 typedef void (*terminal_response_func)(const unsigned char *pdu,
72 unsigned char *req_pdu;
74 unsigned char *rsp_pdu;
77 terminal_response_func tr_func;
78 enum test_result result;
81 static GMainLoop *main_loop = NULL;
82 static volatile sig_atomic_t __terminated = 0;
84 GList *cur_test = NULL;
87 static DBusConnection *conn;
88 static gboolean ofono_running = FALSE;
89 static guint modem_changed_watch;
90 enum test_state state;
93 static guint server_watch;
94 static GAtServer *emulator;
96 /* Emulated modem state variables */
97 static int modem_mode = 0;
99 static gboolean create_tcp(void);
101 static const char *to_hex(const unsigned char *data, unsigned int len)
103 static char buf[512+1];
106 for (i = 0; i < len; i++)
107 sprintf(buf + i * 2, "%02hhX", data[i]);
114 static void send_proactive_command(const unsigned char *pdu, unsigned int len)
118 sprintf(buf, "+CUSATP: %s", to_hex(pdu, len));
119 g_at_server_send_unsolicited(emulator, buf);
122 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
124 return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
125 "Invalid arguments provided");
128 static DBusMessage *stktest_error_failed(DBusMessage *msg)
130 return g_dbus_create_error(msg, STKTEST_ERROR ".Failed",
134 static DBusMessage *stktest_error_end_session(DBusMessage *msg)
136 return g_dbus_create_error(msg, OFONO_ERROR ".EndSession",
137 "End Session Request");
140 static DBusMessage *stktest_error_go_back(DBusMessage *msg)
142 return g_dbus_create_error(msg, OFONO_ERROR ".GoBack",
146 static DBusMessage *stktest_error_busy(DBusMessage *msg)
148 return g_dbus_create_error(msg, OFONO_ERROR ".Busy",
152 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
155 g_print("Got Release\n");
157 return dbus_message_new_method_return(msg);
160 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
164 unsigned char icon_id;
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);
173 g_print("Got DisplayText with: %s, %u, %d\n", text, icon_id, urgent);
175 return dbus_message_new_method_return(msg);
178 static void server_debug(const char *str, void *data)
180 g_print("%s: %s\n", (char *) data, str);
183 static void cgmi_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, "oFono", 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 void cgmm_cb(GAtServer *server, GAtServerRequestType type,
200 GAtResult *cmd, gpointer user)
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);
207 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
208 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
211 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
215 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
216 GAtResult *cmd, gpointer user)
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);
226 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
227 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
230 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
234 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
235 GAtResult *cmd, gpointer user)
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);
242 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
243 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
246 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
250 static gboolean send_ok(gpointer user)
252 GAtServer *server = user;
254 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
259 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
260 GAtResult *cmd, gpointer user)
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);
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);
274 case G_AT_SERVER_REQUEST_TYPE_SET:
279 g_at_result_iter_init(&iter, cmd);
280 g_at_result_iter_next(&iter, "");
282 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
285 if (mode != 0 && mode != 1)
288 if (modem_mode == mode) {
289 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
294 g_timeout_add_seconds(1, send_ok, server);
304 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
307 static void cusatt_cb(GAtServer *server, GAtServerRequestType type,
308 GAtResult *cmd, gpointer user)
311 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
312 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
314 case G_AT_SERVER_REQUEST_TYPE_QUERY:
315 g_at_server_send_ext_final(server, "+CME ERROR: 4");
317 case G_AT_SERVER_REQUEST_TYPE_SET:
320 const unsigned char *pdu;
323 g_at_result_iter_init(&iter, cmd);
324 g_at_result_iter_next(&iter, "");
326 if (g_at_result_iter_next_hexstring(&iter, &pdu, &len) == FALSE)
329 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
339 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
342 static void listen_again(gpointer user_data)
344 if (create_tcp() == TRUE)
347 g_print("Error listening to socket\n");
348 g_main_loop_quit(main_loop);
351 static void setup_emulator(GAtServer *server)
353 g_at_server_set_debug(server, server_debug, "Server");
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);
362 g_at_server_set_disconnect_function(server, listen_again, NULL);
365 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
368 struct sockaddr saddr;
369 unsigned int len = sizeof(saddr);
371 GIOChannel *client_io = NULL;
376 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
380 client_io = g_io_channel_unix_new(fd);
382 emulator = g_at_server_new(client_io);
383 g_at_server_set_echo(emulator, FALSE);
384 g_io_channel_unref(client_io);
386 if (emulator == NULL)
389 setup_emulator(emulator);
396 static gboolean create_tcp(void)
398 struct sockaddr_in addr;
401 GIOChannel *server_io;
403 sk = socket(PF_INET, SOCK_STREAM, 0);
405 g_print("Can't create tcp/ip socket: %s (%d)\n",
406 strerror(errno), errno);
410 memset(&addr, 0, sizeof(addr));
412 addr.sin_family = AF_INET;
413 addr.sin_addr.s_addr = INADDR_ANY;
414 addr.sin_port = htons(LISTEN_PORT);
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);
423 if (listen(sk, 1) < 0) {
424 g_print("Can't listen on socket: %s (%d)",
425 strerror(errno), errno);
430 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
432 server_io = g_io_channel_unix_new(sk);
433 g_io_channel_set_close_on_unref(server_io, TRUE);
435 server_watch = g_io_add_watch_full(server_io,
437 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
438 on_socket_connected, NULL, NULL);
440 g_io_channel_unref(server_io);
445 static gboolean has_stk_interface(DBusMessageIter *iter)
447 DBusMessageIter entry;
449 dbus_message_iter_recurse(iter, &entry);
451 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
452 const char *interface;
454 dbus_message_iter_get_basic(&entry, &interface);
456 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
459 dbus_message_iter_next(&entry);
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, ...)
476 msg = dbus_message_new_method_call(OFONO_SERVICE, path,
479 g_printerr("Unable to allocate new D-Bus %s message\n", method);
484 va_start(args, type);
486 if (!dbus_message_append_args_valist(msg, type, args)) {
497 if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
498 g_printerr("Sending %s failed\n", method);
506 dbus_pending_call_set_notify(c, cb, user_data, free_func);
507 dbus_pending_call_unref(c);
509 dbus_message_unref(msg);
514 if (free_func && user_data)
515 free_func(user_data);
518 dbus_message_unref(msg);
523 static void set_property_reply(DBusPendingCall *call, void *user_data)
525 DBusMessage *reply = dbus_pending_call_steal_reply(call);
528 dbus_error_init(&err);
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);
535 dbus_message_unref(reply);
538 static int set_property(const char *path, const char *interface,
539 const char *key, int type, const void *val,
540 DBusPendingCallNotifyFunction notify,
542 DBusFreeFunction destroy)
545 DBusMessageIter iter, value;
546 DBusPendingCall *call;
547 const char *signature;
549 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
554 dbus_message_set_auto_start(msg, FALSE);
556 dbus_message_iter_init_append(msg, &iter);
558 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
561 case DBUS_TYPE_BOOLEAN:
562 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
565 dbus_message_unref(msg);
569 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
571 dbus_message_iter_append_basic(&value, type, val);
572 dbus_message_iter_close_container(&iter, &value);
574 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
575 dbus_message_unref(msg);
579 dbus_message_unref(msg);
584 dbus_pending_call_set_notify(call, notify, user_data, destroy);
586 dbus_pending_call_unref(call);
591 static void register_agent_reply(DBusPendingCall *call, void *user_data)
593 DBusMessage *reply = dbus_pending_call_steal_reply(call);
596 dbus_error_init(&err);
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);
603 dbus_message_unref(reply);
605 state = TEST_STATE_RUNNING;
608 static void register_agent()
610 const char *path = "/default";
613 g_print("Gained STK interface, registering agent...\n");
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,
622 g_printerr("Unable to register agent with oFono\n");
623 g_main_loop_quit(main_loop);
627 state = TEST_STATE_REGISTERING_AGENT;
630 static gboolean modem_changed(DBusConnection *conn,
631 DBusMessage *msg, void *user_data)
633 DBusMessageIter iter, value;
634 const char *path, *key;
637 if (dbus_message_iter_init(msg, &iter) == FALSE)
640 path = dbus_message_get_path(msg);
642 if (g_str_equal(STKTEST_PATH, path) == FALSE)
645 dbus_message_iter_get_basic(&iter, &key);
647 dbus_message_iter_next(&iter);
648 dbus_message_iter_recurse(&iter, &value);
650 if (g_str_equal(key, "Interfaces") == FALSE)
653 has_stk = has_stk_interface(&value);
656 case TEST_STATE_POWERING_UP:
660 case TEST_STATE_REGISTERING_AGENT:
661 case TEST_STATE_RUNNING:
662 if (has_stk == FALSE)
663 g_printerr("Unexpectedly lost STK interface\n");
665 case TEST_STATE_POWERING_DOWN:
672 static void powerup(void)
674 dbus_bool_t powered = TRUE;
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);
682 static void get_modems_reply(DBusPendingCall *call, void *user_data)
684 DBusMessage *reply = dbus_pending_call_steal_reply(call);
685 DBusMessageIter iter, list;
687 gboolean found = FALSE;
689 dbus_error_init(&err);
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);
697 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
700 if (dbus_message_iter_init(reply, &iter) == FALSE)
703 dbus_message_iter_recurse(&iter, &list);
705 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
706 DBusMessageIter entry;
709 dbus_message_iter_recurse(&list, &entry);
710 dbus_message_iter_get_basic(&entry, &path);
712 if (g_str_equal(path, STKTEST_PATH))
715 dbus_message_iter_next(&list);
719 dbus_message_unref(reply);
721 if (found == FALSE) {
722 g_printerr("STK Test modem not found\n");
723 g_main_loop_quit(main_loop);
727 g_print("Test modem found\n");
729 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
731 OFONO_MODEM_INTERFACE,
736 if (create_tcp() == FALSE) {
737 g_printerr("Unable to listen on modem emulator socket\n");
738 g_main_loop_quit(main_loop);
744 static int get_modems(DBusConnection *conn)
747 DBusPendingCall *call;
749 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
750 OFONO_MANAGER_INTERFACE, "GetModems");
754 dbus_message_set_auto_start(msg, FALSE);
756 g_print("getting modems\n");
758 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
759 dbus_message_unref(msg);
763 dbus_message_unref(msg);
768 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
770 dbus_pending_call_unref(call);
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) },
784 static void ofono_connect(DBusConnection *conn, void *user_data)
786 g_print("starting telephony interface\n");
788 if (!g_dbus_register_interface(conn, "/default",
789 OFONO_STKAGENT_INTERFACE,
790 agent_methods, NULL, NULL,
792 g_printerr("Unable to register local agent");
793 g_main_loop_quit(main_loop);
796 ofono_running = TRUE;
800 static void ofono_disconnect(DBusConnection *conn, void *user_data)
802 g_print("stopping telephony interface\n");
804 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
806 ofono_running = FALSE;
808 g_dbus_remove_watch(conn, modem_changed_watch);
809 modem_changed_watch = 0;
812 g_source_remove(server_watch);
816 g_at_server_unref(emulator);
820 static void sig_term(int sig)
822 if (__terminated > 0)
827 g_print("Terminating\n");
829 g_main_loop_quit(main_loop);
832 static void disconnect_callback(DBusConnection *conn, void *user_data)
834 g_printerr("D-Bus disconnect\n");
836 g_main_loop_quit(main_loop);
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,
843 terminal_response_func tr_func)
845 struct test *test = g_new0(struct test, 1);
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;
856 tests = g_list_append(tests, test);
859 static void __stktest_test_init(void)
863 static void test_destroy(gpointer user_data)
865 struct test *test = user_data;
868 g_free(test->method);
869 g_free(test->req_pdu);
870 g_free(test->rsp_pdu);
875 static void __stktest_test_summarize(void)
879 g_print("\n\nTest Summary\n");
880 g_print("============\n");
882 for (l = tests; l; l = l->next) {
883 struct test *test = l->data;
885 g_print("%-60s", test->name);
887 switch (test->result) {
888 case TEST_RESULT_NOT_RUN:
889 g_print("Not Run\n");
891 case TEST_RESULT_PASSED:
894 case TEST_RESULT_FAILED:
901 static void __stktest_test_cleanup(void)
903 g_list_free_full(tests, test_destroy);
908 static gboolean option_version = FALSE;
910 static GOptionEntry options[] = {
911 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
912 "Show version information and exit" },
916 int main(int argc, char **argv)
918 GOptionContext *context;
919 GError *error = NULL;
924 context = g_option_context_new(NULL);
925 g_option_context_add_main_entries(context, options, NULL);
927 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
929 g_printerr("%s\n", error->message);
932 g_printerr("An unknown error occurred\n");
936 g_option_context_free(context);
938 if (option_version == TRUE) {
939 printf("%s\n", VERSION);
943 __stktest_test_init();
945 main_loop = g_main_loop_new(NULL, FALSE);
947 dbus_error_init(&err);
949 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
951 if (dbus_error_is_set(&err) == TRUE) {
952 fprintf(stderr, "%s\n", err.message);
953 dbus_error_free(&err);
955 fprintf(stderr, "Can't register with system bus\n");
959 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
961 memset(&sa, 0, sizeof(sa));
962 sa.sa_handler = sig_term;
963 sigaction(SIGINT, &sa, NULL);
964 sigaction(SIGTERM, &sa, NULL);
966 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
967 ofono_connect, ofono_disconnect, NULL, NULL);
969 g_main_loop_run(main_loop);
971 g_dbus_remove_watch(conn, watch);
973 if (ofono_running == TRUE)
974 ofono_disconnect(conn, NULL);
976 dbus_connection_unref(conn);
978 g_main_loop_unref(main_loop);
980 __stktest_test_summarize();
981 __stktest_test_cleanup();