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 #include "unit/stk-test-data.h"
41 #define OFONO_SERVICE "org.ofono"
42 #define STKTEST_PATH "/stktest"
43 #define STKTEST_ERROR "org.ofono.stktest.Error"
44 #define OFONO_ERROR "org.ofono.Error"
45 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
46 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
47 #define OFONO_STK_INTERFACE OFONO_SERVICE ".SimToolkit"
48 #define OFONO_STKAGENT_INTERFACE OFONO_SERVICE ".SimToolkitAgent"
50 #define LISTEN_PORT 12765
53 TEST_STATE_POWERING_UP = 1,
54 TEST_STATE_REGISTERING_AGENT,
56 TEST_STATE_POWERING_DOWN,
60 TEST_RESULT_NOT_RUN = 0,
65 typedef DBusMessage *(*display_text_cb_t)(DBusMessage *msg, const char *text,
66 unsigned char icon_id,
68 typedef void (*terminal_response_func)(const unsigned char *pdu,
74 unsigned char *req_pdu;
76 unsigned char *rsp_pdu;
79 terminal_response_func tr_func;
80 enum test_result result;
83 static GMainLoop *main_loop = NULL;
84 static volatile sig_atomic_t __terminated = 0;
86 GList *cur_test = NULL;
89 static DBusConnection *conn;
90 static gboolean ofono_running = FALSE;
91 static guint modem_changed_watch;
92 enum test_state state;
93 DBusMessage *pending = NULL;
96 static guint server_watch;
97 static GAtServer *emulator;
99 /* Emulated modem state variables */
100 static int modem_mode = 0;
102 void __stktest_test_next();
103 void __stktest_test_finish(gboolean successful);
104 static gboolean create_tcp(void);
106 #define STKTEST_AGENT_ASSERT(expr) \
109 g_printerr("Assertion Failed %s:%d %s\n", \
110 __FILE__, __LINE__, #expr); \
111 __stktest_test_finish(FALSE); \
112 return stktest_error_failed(msg); \
116 #define STKTEST_RESPONSE_ASSERT(expect_pdu, expect_pdu_len, \
117 got_pdu, got_pdu_len) \
119 if ((expect_pdu_len) != (got_pdu_len)) { \
120 g_printerr("Assertion Failed %s:%d" \
121 " Wrong response len" \
122 " want: %d, got: %d\n", \
123 __FILE__, __LINE__, \
124 expect_pdu_len, got_pdu_len); \
125 __stktest_test_finish(FALSE); \
129 if (memcmp(expect_pdu, got_pdu, expect_pdu_len) != 0) { \
130 g_printerr("Assertion Failed %s:%d" \
131 "Wrong response\n", \
132 __FILE__, __LINE__); \
133 __stktest_test_finish(FALSE); \
138 static const char *to_hex(const unsigned char *data, unsigned int len)
140 static char buf[512+1];
143 for (i = 0; i < len; i++)
144 sprintf(buf + i * 2, "%02hhX", data[i]);
151 static void send_proactive_command(const unsigned char *pdu, unsigned int len)
155 sprintf(buf, "+CUSATP: %s", to_hex(pdu, len));
156 g_at_server_send_unsolicited(emulator, buf);
159 static DBusMessage *stktest_error_invalid_args(DBusMessage *msg)
161 return g_dbus_create_error(msg, STKTEST_ERROR ".InvalidArguments",
162 "Invalid arguments provided");
165 static DBusMessage *stktest_error_failed(DBusMessage *msg)
167 return g_dbus_create_error(msg, STKTEST_ERROR ".Failed",
171 static DBusMessage *stktest_error_end_session(DBusMessage *msg)
173 return g_dbus_create_error(msg, OFONO_ERROR ".EndSession",
174 "End Session Request");
177 static DBusMessage *stktest_error_go_back(DBusMessage *msg)
179 return g_dbus_create_error(msg, OFONO_ERROR ".GoBack",
183 static DBusMessage *stktest_error_busy(DBusMessage *msg)
185 return g_dbus_create_error(msg, OFONO_ERROR ".Busy",
189 static DBusMessage *agent_release(DBusConnection *conn, DBusMessage *msg,
192 g_print("Got Release\n");
195 dbus_message_unref(pending);
199 return dbus_message_new_method_return(msg);
202 static DBusMessage *agent_cancel(DBusConnection *conn, DBusMessage *msg,
206 dbus_message_unref(pending);
213 static DBusMessage *agent_display_text(DBusConnection *conn, DBusMessage *msg,
217 unsigned char icon_id;
220 display_text_cb_t func;
223 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &text,
224 DBUS_TYPE_BYTE, &icon_id,
225 DBUS_TYPE_BOOLEAN, &urgent,
226 DBUS_TYPE_INVALID) == FALSE)
227 return stktest_error_invalid_args(msg);
229 if (cur_test == NULL)
230 return stktest_error_failed(msg);
232 test = cur_test->data;
233 func = test->agent_func;
235 if (strcmp(test->method, "DisplayText")) {
236 g_printerr("Wrong method called!\n");
237 __stktest_test_finish(FALSE);
238 return stktest_error_failed(msg);
242 g_printerr("DisplayText not expected to be called");
243 __stktest_test_finish(FALSE);
244 return stktest_error_failed(msg);
247 reply = func(msg, text, icon_id, urgent);
249 pending = dbus_message_ref(msg);
254 static void server_debug(const char *str, void *data)
256 g_print("%s: %s\n", (char *) data, str);
259 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
260 GAtResult *cmd, gpointer user)
263 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
264 g_at_server_send_info(server, "oFono", TRUE);
265 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
267 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
268 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
271 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
275 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
276 GAtResult *cmd, gpointer user)
279 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
280 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
281 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
283 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
284 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
287 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
291 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
292 GAtResult *cmd, gpointer user)
297 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
298 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
299 g_at_server_send_info(server, buf, TRUE);
300 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
302 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
303 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
306 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
310 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
311 GAtResult *cmd, gpointer user)
314 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
315 g_at_server_send_info(server, "123456789", TRUE);
316 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
318 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
319 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
322 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
326 static gboolean send_ok(gpointer user)
328 GAtServer *server = user;
330 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
335 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
336 GAtResult *cmd, gpointer user)
341 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
342 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
343 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
345 case G_AT_SERVER_REQUEST_TYPE_QUERY:
346 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
347 g_at_server_send_info(server, buf, TRUE);
348 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
350 case G_AT_SERVER_REQUEST_TYPE_SET:
355 g_at_result_iter_init(&iter, cmd);
356 g_at_result_iter_next(&iter, "");
358 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
361 if (mode != 0 && mode != 1)
364 if (modem_mode == mode) {
365 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
370 g_timeout_add_seconds(1, send_ok, server);
380 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
383 static void cusatt_cb(GAtServer *server, GAtServerRequestType type,
384 GAtResult *cmd, gpointer user)
387 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
388 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
390 case G_AT_SERVER_REQUEST_TYPE_QUERY:
391 g_at_server_send_ext_final(server, "+CME ERROR: 4");
393 case G_AT_SERVER_REQUEST_TYPE_SET:
396 const unsigned char *pdu;
399 terminal_response_func func;
401 g_at_result_iter_init(&iter, cmd);
402 g_at_result_iter_next(&iter, "");
404 if (g_at_result_iter_next_hexstring(&iter, &pdu, &len) == FALSE)
407 if (cur_test == NULL)
410 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
412 test = cur_test->data;
413 func = test->tr_func;
424 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
427 static void listen_again(gpointer user_data)
429 g_at_server_unref(emulator);
432 if (create_tcp() == TRUE)
435 g_print("Error listening to socket\n");
436 g_main_loop_quit(main_loop);
439 static void setup_emulator(GAtServer *server)
441 g_at_server_set_debug(server, server_debug, "Server");
443 g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
444 g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
445 g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
446 g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
447 g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
448 g_at_server_register(server, "+CUSATT", cusatt_cb, NULL, NULL);
450 g_at_server_set_disconnect_function(server, listen_again, NULL);
453 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
456 struct sockaddr saddr;
457 unsigned int len = sizeof(saddr);
459 GIOChannel *client_io = NULL;
464 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
468 client_io = g_io_channel_unix_new(fd);
470 emulator = g_at_server_new(client_io);
471 g_at_server_set_echo(emulator, FALSE);
472 g_io_channel_unref(client_io);
474 if (emulator == NULL)
477 setup_emulator(emulator);
484 static gboolean create_tcp(void)
486 struct sockaddr_in addr;
489 GIOChannel *server_io;
491 sk = socket(PF_INET, SOCK_STREAM, 0);
493 g_print("Can't create tcp/ip socket: %s (%d)\n",
494 strerror(errno), errno);
498 memset(&addr, 0, sizeof(addr));
500 addr.sin_family = AF_INET;
501 addr.sin_addr.s_addr = INADDR_ANY;
502 addr.sin_port = htons(LISTEN_PORT);
504 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
505 if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
506 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
511 if (listen(sk, 1) < 0) {
512 g_print("Can't listen on socket: %s (%d)",
513 strerror(errno), errno);
518 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
520 server_io = g_io_channel_unix_new(sk);
521 g_io_channel_set_close_on_unref(server_io, TRUE);
523 server_watch = g_io_add_watch_full(server_io,
525 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
526 on_socket_connected, NULL, NULL);
528 g_io_channel_unref(server_io);
533 static gboolean has_stk_interface(DBusMessageIter *iter)
535 DBusMessageIter entry;
537 dbus_message_iter_recurse(iter, &entry);
539 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
540 const char *interface;
542 dbus_message_iter_get_basic(&entry, &interface);
544 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
547 dbus_message_iter_next(&entry);
553 static int send_with_reply(const char *path, const char *interface,
554 const char *method, DBusPendingCall **call,
555 DBusPendingCallNotifyFunction cb,
556 void *user_data, DBusFreeFunction free_func,
557 int timeout, int type, ...)
564 msg = dbus_message_new_method_call(OFONO_SERVICE, path,
567 g_printerr("Unable to allocate new D-Bus %s message\n", method);
572 va_start(args, type);
574 if (!dbus_message_append_args_valist(msg, type, args)) {
585 if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
586 g_printerr("Sending %s failed\n", method);
594 dbus_pending_call_set_notify(c, cb, user_data, free_func);
595 dbus_pending_call_unref(c);
597 dbus_message_unref(msg);
602 if (free_func && user_data)
603 free_func(user_data);
606 dbus_message_unref(msg);
611 static void set_property_reply(DBusPendingCall *call, void *user_data)
613 DBusMessage *reply = dbus_pending_call_steal_reply(call);
616 dbus_error_init(&err);
618 if (dbus_set_error_from_message(&err, reply) == TRUE) {
619 g_printerr("%s: %s\n", err.name, err.message);
620 dbus_error_free(&err);
623 dbus_message_unref(reply);
626 static int set_property(const char *path, const char *interface,
627 const char *key, int type, const void *val,
628 DBusPendingCallNotifyFunction notify,
630 DBusFreeFunction destroy)
633 DBusMessageIter iter, value;
634 DBusPendingCall *call;
635 const char *signature;
637 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
642 dbus_message_set_auto_start(msg, FALSE);
644 dbus_message_iter_init_append(msg, &iter);
646 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
649 case DBUS_TYPE_BOOLEAN:
650 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
653 dbus_message_unref(msg);
657 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
659 dbus_message_iter_append_basic(&value, type, val);
660 dbus_message_iter_close_container(&iter, &value);
662 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
663 dbus_message_unref(msg);
667 dbus_message_unref(msg);
672 dbus_pending_call_set_notify(call, notify, user_data, destroy);
674 dbus_pending_call_unref(call);
679 static void register_agent_reply(DBusPendingCall *call, void *user_data)
681 DBusMessage *reply = dbus_pending_call_steal_reply(call);
685 dbus_error_init(&err);
687 if (dbus_set_error_from_message(&err, reply) == TRUE) {
688 g_printerr("%s: %s\n", err.name, err.message);
689 dbus_error_free(&err);
692 dbus_message_unref(reply);
694 state = TEST_STATE_RUNNING;
695 test = cur_test->data;
696 send_proactive_command(test->req_pdu, test->req_len);
699 static void register_agent()
701 const char *path = "/default";
704 g_print("Gained STK interface, registering agent...\n");
706 status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
707 "RegisterAgent", NULL,
708 register_agent_reply, NULL, NULL, 1,
709 DBUS_TYPE_OBJECT_PATH, &path,
713 g_printerr("Unable to register agent with oFono\n");
714 g_main_loop_quit(main_loop);
718 state = TEST_STATE_REGISTERING_AGENT;
721 static gboolean modem_changed(DBusConnection *conn,
722 DBusMessage *msg, void *user_data)
724 DBusMessageIter iter, value;
725 const char *path, *key;
728 if (dbus_message_iter_init(msg, &iter) == FALSE)
731 path = dbus_message_get_path(msg);
733 if (g_str_equal(STKTEST_PATH, path) == FALSE)
736 dbus_message_iter_get_basic(&iter, &key);
738 dbus_message_iter_next(&iter);
739 dbus_message_iter_recurse(&iter, &value);
741 if (g_str_equal(key, "Interfaces") == FALSE)
744 has_stk = has_stk_interface(&value);
747 case TEST_STATE_POWERING_UP:
751 case TEST_STATE_REGISTERING_AGENT:
752 case TEST_STATE_RUNNING:
753 if (has_stk == FALSE)
754 g_printerr("Unexpectedly lost STK interface\n");
756 case TEST_STATE_POWERING_DOWN:
763 static void powerup(void)
765 dbus_bool_t powered = TRUE;
767 state = TEST_STATE_POWERING_UP;
768 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
769 DBUS_TYPE_BOOLEAN, &powered,
770 set_property_reply, NULL, NULL);
773 static void get_modems_reply(DBusPendingCall *call, void *user_data)
775 DBusMessage *reply = dbus_pending_call_steal_reply(call);
776 DBusMessageIter iter, list;
778 gboolean found = FALSE;
780 dbus_error_init(&err);
782 if (dbus_set_error_from_message(&err, reply) == TRUE) {
783 g_printerr("%s: %s\n", err.name, err.message);
784 dbus_error_free(&err);
788 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
791 if (dbus_message_iter_init(reply, &iter) == FALSE)
794 dbus_message_iter_recurse(&iter, &list);
796 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
797 DBusMessageIter entry;
800 dbus_message_iter_recurse(&list, &entry);
801 dbus_message_iter_get_basic(&entry, &path);
803 if (g_str_equal(path, STKTEST_PATH))
806 dbus_message_iter_next(&list);
810 dbus_message_unref(reply);
812 if (found == FALSE) {
813 g_printerr("STK Test modem not found\n");
814 g_main_loop_quit(main_loop);
818 g_print("Test modem found\n");
820 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
822 OFONO_MODEM_INTERFACE,
827 if (create_tcp() == FALSE) {
828 g_printerr("Unable to listen on modem emulator socket\n");
829 g_main_loop_quit(main_loop);
832 __stktest_test_next();
835 static int get_modems(DBusConnection *conn)
838 DBusPendingCall *call;
840 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
841 OFONO_MANAGER_INTERFACE, "GetModems");
845 dbus_message_set_auto_start(msg, FALSE);
847 g_print("getting modems\n");
849 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
850 dbus_message_unref(msg);
854 dbus_message_unref(msg);
859 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
861 dbus_pending_call_unref(call);
866 static const GDBusMethodTable agent_methods[] = {
867 { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
868 { GDBUS_ASYNC_METHOD("DisplayText",
869 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
870 { "urgent", "b" }), NULL,
871 agent_display_text) },
872 { GDBUS_NOREPLY_METHOD("Cancel", NULL, NULL, agent_cancel) },
876 static void ofono_connect(DBusConnection *conn, void *user_data)
878 g_print("starting telephony interface\n");
880 if (!g_dbus_register_interface(conn, "/default",
881 OFONO_STKAGENT_INTERFACE,
882 agent_methods, NULL, NULL,
884 g_printerr("Unable to register local agent");
885 g_main_loop_quit(main_loop);
888 ofono_running = TRUE;
892 static void ofono_disconnect(DBusConnection *conn, void *user_data)
894 g_print("stopping telephony interface\n");
896 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
898 ofono_running = FALSE;
900 g_dbus_remove_watch(conn, modem_changed_watch);
901 modem_changed_watch = 0;
904 g_source_remove(server_watch);
908 g_at_server_unref(emulator);
912 static void sig_term(int sig)
914 if (__terminated > 0)
919 g_print("Terminating\n");
921 g_main_loop_quit(main_loop);
924 static void disconnect_callback(DBusConnection *conn, void *user_data)
926 g_printerr("D-Bus disconnect\n");
928 g_main_loop_quit(main_loop);
931 static gboolean end_session_and_finish(gpointer user_data)
933 g_at_server_send_unsolicited(emulator, "+CUSATEND");
934 __stktest_test_finish(TRUE);
939 static void expect_response(const unsigned char *pdu, unsigned int len)
941 struct test *test = cur_test->data;
943 STKTEST_RESPONSE_ASSERT(test->rsp_pdu, test->rsp_len, pdu, len);
945 g_idle_add(end_session_and_finish, NULL);
948 static DBusMessage *test_display_text_11(DBusMessage *msg,
950 unsigned char icon_id,
953 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 1"));
954 STKTEST_AGENT_ASSERT(icon_id == 0);
955 STKTEST_AGENT_ASSERT(urgent == FALSE);
957 return dbus_message_new_method_return(msg);
960 static DBusMessage *test_display_text_12(DBusMessage *msg,
962 unsigned char icon_id,
965 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 1"));
966 STKTEST_AGENT_ASSERT(icon_id == 0);
967 STKTEST_AGENT_ASSERT(urgent == FALSE);
969 return stktest_error_busy(msg);
972 static DBusMessage *test_display_text_13(DBusMessage *msg,
974 unsigned char icon_id,
977 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 2"));
978 STKTEST_AGENT_ASSERT(icon_id == 0);
979 STKTEST_AGENT_ASSERT(urgent == TRUE);
981 return dbus_message_new_method_return(msg);
984 static DBusMessage *test_display_text_14(DBusMessage *msg,
986 unsigned char icon_id,
989 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 3"));
990 STKTEST_AGENT_ASSERT(icon_id == 0);
991 STKTEST_AGENT_ASSERT(urgent == FALSE);
993 return dbus_message_new_method_return(msg);
996 static DBusMessage *test_display_text_15(DBusMessage *msg,
998 unsigned char icon_id,
1001 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 4"));
1002 STKTEST_AGENT_ASSERT(icon_id == 0);
1003 STKTEST_AGENT_ASSERT(urgent == FALSE);
1008 static DBusMessage *test_display_text_16(DBusMessage *msg,
1010 unsigned char icon_id,
1013 STKTEST_AGENT_ASSERT(g_str_equal(text, "This command instructs the ME"
1014 " to display a text message. "
1015 "It allows the SIM to define "
1016 "the priority of that message, "
1017 "and the text string format. "
1018 "Two types of prio"));
1019 STKTEST_AGENT_ASSERT(icon_id == 0);
1020 STKTEST_AGENT_ASSERT(urgent == FALSE);
1022 return dbus_message_new_method_return(msg);
1025 static DBusMessage *test_display_text_17(DBusMessage *msg,
1027 unsigned char icon_id,
1030 /* oFono gives rich text formatting in HTML */
1031 STKTEST_AGENT_ASSERT(g_str_equal(text, "<GO-BACKWARDS>"));
1032 STKTEST_AGENT_ASSERT(icon_id == 0);
1033 STKTEST_AGENT_ASSERT(urgent == FALSE);
1035 return stktest_error_go_back(msg);
1038 static DBusMessage *test_display_text_18(DBusMessage *msg,
1040 unsigned char icon_id,
1043 /* oFono gives rich text formatting in HTML */
1044 STKTEST_AGENT_ASSERT(g_str_equal(text, "<ABORT>"));
1045 STKTEST_AGENT_ASSERT(icon_id == 0);
1046 STKTEST_AGENT_ASSERT(urgent == FALSE);
1048 return stktest_error_end_session(msg);
1051 static DBusMessage *test_display_text_21(DBusMessage *msg,
1053 unsigned char icon_id,
1056 STKTEST_AGENT_ASSERT(g_str_equal(text, "<TIME-OUT>"));
1057 STKTEST_AGENT_ASSERT(icon_id == 0);
1058 STKTEST_AGENT_ASSERT(urgent == FALSE);
1063 static DBusMessage *test_display_text_31(DBusMessage *msg,
1065 unsigned char icon_id,
1068 static const char *expected = "This command instructs the ME to display"
1069 " a text message, and/or an icon "
1070 "(see 6.5.4). It allows the "
1071 "SIM to define the priority of that "
1072 "message, and the text string format. "
1073 "Two types of priority are defined:- "
1074 "display normal priority text and/";
1075 STKTEST_AGENT_ASSERT(g_str_equal(text, expected));
1076 STKTEST_AGENT_ASSERT(icon_id == 0);
1077 STKTEST_AGENT_ASSERT(urgent == FALSE);
1079 return dbus_message_new_method_return(msg);
1082 static void power_down_reply(DBusPendingCall *call, void *user_data)
1084 __stktest_test_next();
1087 void __stktest_test_finish(gboolean successful)
1089 struct test *test = cur_test->data;
1090 dbus_bool_t powered = FALSE;
1092 test->result = successful ? TEST_RESULT_PASSED : TEST_RESULT_FAILED;
1094 state = TEST_STATE_POWERING_DOWN;
1095 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
1096 DBUS_TYPE_BOOLEAN, &powered,
1097 power_down_reply, NULL, NULL);
1100 void __stktest_test_next()
1102 if (cur_test == NULL)
1105 cur_test = cur_test->next;
1107 if (cur_test == NULL)
1108 g_main_loop_quit(main_loop);
1113 static void stktest_add_test(const char *name, const char *method,
1114 const unsigned char *req, unsigned int req_len,
1115 const unsigned char *rsp, unsigned int rsp_len,
1117 terminal_response_func tr_func)
1119 struct test *test = g_new0(struct test, 1);
1121 test->name = g_strdup(name);
1122 test->method = g_strdup(method);
1123 test->req_pdu = g_memdup(req, req_len);
1124 test->req_len = req_len;
1125 test->rsp_pdu = g_memdup(rsp, rsp_len);
1126 test->rsp_len = rsp_len;
1127 test->agent_func = agent_func;
1128 test->tr_func = tr_func;
1130 tests = g_list_append(tests, test);
1133 static void __stktest_test_init(void)
1135 stktest_add_test("Display Text 1.1", "DisplayText",
1136 display_text_111, sizeof(display_text_111),
1137 display_text_response_111,
1138 sizeof(display_text_response_111),
1139 test_display_text_11, expect_response);
1140 stktest_add_test("Display Text 1.2", "DisplayText",
1141 display_text_111, sizeof(display_text_111),
1142 display_text_response_121,
1143 sizeof(display_text_response_121),
1144 test_display_text_12, expect_response);
1145 stktest_add_test("Display Text 1.3", "DisplayText",
1146 display_text_131, sizeof(display_text_131),
1147 display_text_response_131,
1148 sizeof(display_text_response_131),
1149 test_display_text_13, expect_response);
1150 stktest_add_test("Display Text 1.4", "DisplayText",
1151 display_text_141, sizeof(display_text_141),
1152 display_text_response_141,
1153 sizeof(display_text_response_141),
1154 test_display_text_14, expect_response);
1155 stktest_add_test("Display Text 1.5", "DisplayText",
1156 display_text_151, sizeof(display_text_151),
1157 display_text_response_151,
1158 sizeof(display_text_response_151),
1159 test_display_text_15, expect_response);
1160 stktest_add_test("Display Text 1.6", "DisplayText",
1161 display_text_161, sizeof(display_text_161),
1162 display_text_response_161,
1163 sizeof(display_text_response_161),
1164 test_display_text_16, expect_response);
1165 stktest_add_test("Display Text 1.7", "DisplayText",
1166 display_text_171, sizeof(display_text_171),
1167 display_text_response_171,
1168 sizeof(display_text_response_171),
1169 test_display_text_17, expect_response);
1170 stktest_add_test("Display Text 1.8", "DisplayText",
1171 display_text_181, sizeof(display_text_181),
1172 display_text_response_181,
1173 sizeof(display_text_response_181),
1174 test_display_text_18, expect_response);
1175 stktest_add_test("Display Text 1.9", "DisplayText",
1176 display_text_191, sizeof(display_text_191),
1177 display_text_response_191,
1178 sizeof(display_text_response_191),
1179 NULL, expect_response);
1180 stktest_add_test("Display Text 2.1", "DisplayText",
1181 display_text_211, sizeof(display_text_211),
1182 display_text_response_211,
1183 sizeof(display_text_response_211),
1184 test_display_text_21, expect_response);
1185 stktest_add_test("Display Text 3.1", "DisplayText",
1186 display_text_311, sizeof(display_text_311),
1187 display_text_response_311,
1188 sizeof(display_text_response_311),
1189 test_display_text_31, expect_response);
1192 static void test_destroy(gpointer user_data)
1194 struct test *test = user_data;
1197 g_free(test->method);
1198 g_free(test->req_pdu);
1199 g_free(test->rsp_pdu);
1204 static void __stktest_test_summarize(void)
1208 g_print("\n\nTest Summary\n");
1209 g_print("============\n");
1211 for (l = tests; l; l = l->next) {
1212 struct test *test = l->data;
1214 g_print("%-60s", test->name);
1216 switch (test->result) {
1217 case TEST_RESULT_NOT_RUN:
1218 g_print("Not Run\n");
1220 case TEST_RESULT_PASSED:
1221 g_print("Passed\n");
1223 case TEST_RESULT_FAILED:
1224 g_print("Failed\n");
1230 static void __stktest_test_cleanup(void)
1232 g_list_free_full(tests, test_destroy);
1237 static gboolean option_version = FALSE;
1239 static GOptionEntry options[] = {
1240 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
1241 "Show version information and exit" },
1245 int main(int argc, char **argv)
1247 GOptionContext *context;
1248 GError *error = NULL;
1251 struct sigaction sa;
1253 context = g_option_context_new(NULL);
1254 g_option_context_add_main_entries(context, options, NULL);
1256 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
1257 if (error != NULL) {
1258 g_printerr("%s\n", error->message);
1259 g_error_free(error);
1261 g_printerr("An unknown error occurred\n");
1265 g_option_context_free(context);
1267 if (option_version == TRUE) {
1268 printf("%s\n", VERSION);
1272 __stktest_test_init();
1274 main_loop = g_main_loop_new(NULL, FALSE);
1276 dbus_error_init(&err);
1278 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
1280 if (dbus_error_is_set(&err) == TRUE) {
1281 fprintf(stderr, "%s\n", err.message);
1282 dbus_error_free(&err);
1284 fprintf(stderr, "Can't register with system bus\n");
1288 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
1290 memset(&sa, 0, sizeof(sa));
1291 sa.sa_handler = sig_term;
1292 sigaction(SIGINT, &sa, NULL);
1293 sigaction(SIGTERM, &sa, NULL);
1295 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
1296 ofono_connect, ofono_disconnect, NULL, NULL);
1298 g_main_loop_run(main_loop);
1300 g_dbus_remove_watch(conn, watch);
1302 if (ofono_running == TRUE)
1303 ofono_disconnect(conn, NULL);
1305 dbus_connection_unref(conn);
1307 g_main_loop_unref(main_loop);
1309 __stktest_test_summarize();
1310 __stktest_test_cleanup();