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;
222 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &text,
223 DBUS_TYPE_BYTE, &icon_id,
224 DBUS_TYPE_BOOLEAN, &urgent,
225 DBUS_TYPE_INVALID) == FALSE)
226 return stktest_error_invalid_args(msg);
228 if (cur_test == NULL)
229 return stktest_error_failed(msg);
231 test = cur_test->data;
232 func = test->agent_func;
234 return func(msg, text, icon_id, urgent);
237 static void server_debug(const char *str, void *data)
239 g_print("%s: %s\n", (char *) data, str);
242 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
243 GAtResult *cmd, gpointer user)
246 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
247 g_at_server_send_info(server, "oFono", TRUE);
248 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
250 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
251 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
254 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
258 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
259 GAtResult *cmd, gpointer user)
262 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
263 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
264 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
266 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
267 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
270 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
274 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
275 GAtResult *cmd, gpointer user)
280 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
281 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
282 g_at_server_send_info(server, buf, TRUE);
283 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
285 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
286 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
289 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
293 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
294 GAtResult *cmd, gpointer user)
297 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
298 g_at_server_send_info(server, "123456789", TRUE);
299 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
301 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
302 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
305 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
309 static gboolean send_ok(gpointer user)
311 GAtServer *server = user;
313 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
318 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
319 GAtResult *cmd, gpointer user)
324 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
325 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
326 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
328 case G_AT_SERVER_REQUEST_TYPE_QUERY:
329 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
330 g_at_server_send_info(server, buf, TRUE);
331 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
333 case G_AT_SERVER_REQUEST_TYPE_SET:
338 g_at_result_iter_init(&iter, cmd);
339 g_at_result_iter_next(&iter, "");
341 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
344 if (mode != 0 && mode != 1)
347 if (modem_mode == mode) {
348 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
353 g_timeout_add_seconds(1, send_ok, server);
363 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
366 static void cusatt_cb(GAtServer *server, GAtServerRequestType type,
367 GAtResult *cmd, gpointer user)
370 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
371 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
373 case G_AT_SERVER_REQUEST_TYPE_QUERY:
374 g_at_server_send_ext_final(server, "+CME ERROR: 4");
376 case G_AT_SERVER_REQUEST_TYPE_SET:
379 const unsigned char *pdu;
382 terminal_response_func func;
384 g_at_result_iter_init(&iter, cmd);
385 g_at_result_iter_next(&iter, "");
387 if (g_at_result_iter_next_hexstring(&iter, &pdu, &len) == FALSE)
390 if (cur_test == NULL)
393 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
395 test = cur_test->data;
396 func = test->tr_func;
407 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
410 static void listen_again(gpointer user_data)
412 g_at_server_unref(emulator);
415 if (create_tcp() == TRUE)
418 g_print("Error listening to socket\n");
419 g_main_loop_quit(main_loop);
422 static void setup_emulator(GAtServer *server)
424 g_at_server_set_debug(server, server_debug, "Server");
426 g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
427 g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
428 g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
429 g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
430 g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
431 g_at_server_register(server, "+CUSATT", cusatt_cb, NULL, NULL);
433 g_at_server_set_disconnect_function(server, listen_again, NULL);
436 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
439 struct sockaddr saddr;
440 unsigned int len = sizeof(saddr);
442 GIOChannel *client_io = NULL;
447 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
451 client_io = g_io_channel_unix_new(fd);
453 emulator = g_at_server_new(client_io);
454 g_at_server_set_echo(emulator, FALSE);
455 g_io_channel_unref(client_io);
457 if (emulator == NULL)
460 setup_emulator(emulator);
467 static gboolean create_tcp(void)
469 struct sockaddr_in addr;
472 GIOChannel *server_io;
474 sk = socket(PF_INET, SOCK_STREAM, 0);
476 g_print("Can't create tcp/ip socket: %s (%d)\n",
477 strerror(errno), errno);
481 memset(&addr, 0, sizeof(addr));
483 addr.sin_family = AF_INET;
484 addr.sin_addr.s_addr = INADDR_ANY;
485 addr.sin_port = htons(LISTEN_PORT);
487 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
488 if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
489 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
494 if (listen(sk, 1) < 0) {
495 g_print("Can't listen on socket: %s (%d)",
496 strerror(errno), errno);
501 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
503 server_io = g_io_channel_unix_new(sk);
504 g_io_channel_set_close_on_unref(server_io, TRUE);
506 server_watch = g_io_add_watch_full(server_io,
508 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
509 on_socket_connected, NULL, NULL);
511 g_io_channel_unref(server_io);
516 static gboolean has_stk_interface(DBusMessageIter *iter)
518 DBusMessageIter entry;
520 dbus_message_iter_recurse(iter, &entry);
522 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
523 const char *interface;
525 dbus_message_iter_get_basic(&entry, &interface);
527 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
530 dbus_message_iter_next(&entry);
536 static int send_with_reply(const char *path, const char *interface,
537 const char *method, DBusPendingCall **call,
538 DBusPendingCallNotifyFunction cb,
539 void *user_data, DBusFreeFunction free_func,
540 int timeout, int type, ...)
547 msg = dbus_message_new_method_call(OFONO_SERVICE, path,
550 g_printerr("Unable to allocate new D-Bus %s message\n", method);
555 va_start(args, type);
557 if (!dbus_message_append_args_valist(msg, type, args)) {
568 if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
569 g_printerr("Sending %s failed\n", method);
577 dbus_pending_call_set_notify(c, cb, user_data, free_func);
578 dbus_pending_call_unref(c);
580 dbus_message_unref(msg);
585 if (free_func && user_data)
586 free_func(user_data);
589 dbus_message_unref(msg);
594 static void set_property_reply(DBusPendingCall *call, void *user_data)
596 DBusMessage *reply = dbus_pending_call_steal_reply(call);
599 dbus_error_init(&err);
601 if (dbus_set_error_from_message(&err, reply) == TRUE) {
602 g_printerr("%s: %s\n", err.name, err.message);
603 dbus_error_free(&err);
606 dbus_message_unref(reply);
609 static int set_property(const char *path, const char *interface,
610 const char *key, int type, const void *val,
611 DBusPendingCallNotifyFunction notify,
613 DBusFreeFunction destroy)
616 DBusMessageIter iter, value;
617 DBusPendingCall *call;
618 const char *signature;
620 msg = dbus_message_new_method_call(OFONO_SERVICE, path, interface,
625 dbus_message_set_auto_start(msg, FALSE);
627 dbus_message_iter_init_append(msg, &iter);
629 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
632 case DBUS_TYPE_BOOLEAN:
633 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
636 dbus_message_unref(msg);
640 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
642 dbus_message_iter_append_basic(&value, type, val);
643 dbus_message_iter_close_container(&iter, &value);
645 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
646 dbus_message_unref(msg);
650 dbus_message_unref(msg);
655 dbus_pending_call_set_notify(call, notify, user_data, destroy);
657 dbus_pending_call_unref(call);
662 static void register_agent_reply(DBusPendingCall *call, void *user_data)
664 DBusMessage *reply = dbus_pending_call_steal_reply(call);
668 dbus_error_init(&err);
670 if (dbus_set_error_from_message(&err, reply) == TRUE) {
671 g_printerr("%s: %s\n", err.name, err.message);
672 dbus_error_free(&err);
675 dbus_message_unref(reply);
677 state = TEST_STATE_RUNNING;
678 test = cur_test->data;
679 send_proactive_command(test->req_pdu, test->req_len);
682 static void register_agent()
684 const char *path = "/default";
687 g_print("Gained STK interface, registering agent...\n");
689 status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
690 "RegisterAgent", NULL,
691 register_agent_reply, NULL, NULL, 1,
692 DBUS_TYPE_OBJECT_PATH, &path,
696 g_printerr("Unable to register agent with oFono\n");
697 g_main_loop_quit(main_loop);
701 state = TEST_STATE_REGISTERING_AGENT;
704 static gboolean modem_changed(DBusConnection *conn,
705 DBusMessage *msg, void *user_data)
707 DBusMessageIter iter, value;
708 const char *path, *key;
711 if (dbus_message_iter_init(msg, &iter) == FALSE)
714 path = dbus_message_get_path(msg);
716 if (g_str_equal(STKTEST_PATH, path) == FALSE)
719 dbus_message_iter_get_basic(&iter, &key);
721 dbus_message_iter_next(&iter);
722 dbus_message_iter_recurse(&iter, &value);
724 if (g_str_equal(key, "Interfaces") == FALSE)
727 has_stk = has_stk_interface(&value);
730 case TEST_STATE_POWERING_UP:
734 case TEST_STATE_REGISTERING_AGENT:
735 case TEST_STATE_RUNNING:
736 if (has_stk == FALSE)
737 g_printerr("Unexpectedly lost STK interface\n");
739 case TEST_STATE_POWERING_DOWN:
746 static void powerup(void)
748 dbus_bool_t powered = TRUE;
750 state = TEST_STATE_POWERING_UP;
751 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
752 DBUS_TYPE_BOOLEAN, &powered,
753 set_property_reply, NULL, NULL);
756 static void get_modems_reply(DBusPendingCall *call, void *user_data)
758 DBusMessage *reply = dbus_pending_call_steal_reply(call);
759 DBusMessageIter iter, list;
761 gboolean found = FALSE;
763 dbus_error_init(&err);
765 if (dbus_set_error_from_message(&err, reply) == TRUE) {
766 g_printerr("%s: %s\n", err.name, err.message);
767 dbus_error_free(&err);
771 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
774 if (dbus_message_iter_init(reply, &iter) == FALSE)
777 dbus_message_iter_recurse(&iter, &list);
779 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
780 DBusMessageIter entry;
783 dbus_message_iter_recurse(&list, &entry);
784 dbus_message_iter_get_basic(&entry, &path);
786 if (g_str_equal(path, STKTEST_PATH))
789 dbus_message_iter_next(&list);
793 dbus_message_unref(reply);
795 if (found == FALSE) {
796 g_printerr("STK Test modem not found\n");
797 g_main_loop_quit(main_loop);
801 g_print("Test modem found\n");
803 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
805 OFONO_MODEM_INTERFACE,
810 if (create_tcp() == FALSE) {
811 g_printerr("Unable to listen on modem emulator socket\n");
812 g_main_loop_quit(main_loop);
815 __stktest_test_next();
818 static int get_modems(DBusConnection *conn)
821 DBusPendingCall *call;
823 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
824 OFONO_MANAGER_INTERFACE, "GetModems");
828 dbus_message_set_auto_start(msg, FALSE);
830 g_print("getting modems\n");
832 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
833 dbus_message_unref(msg);
837 dbus_message_unref(msg);
842 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
844 dbus_pending_call_unref(call);
849 static const GDBusMethodTable agent_methods[] = {
850 { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
851 { GDBUS_ASYNC_METHOD("DisplayText",
852 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
853 { "urgent", "b" }), NULL,
854 agent_display_text) },
855 { GDBUS_NOREPLY_METHOD("Cancel", NULL, NULL, agent_cancel) },
859 static void ofono_connect(DBusConnection *conn, void *user_data)
861 g_print("starting telephony interface\n");
863 if (!g_dbus_register_interface(conn, "/default",
864 OFONO_STKAGENT_INTERFACE,
865 agent_methods, NULL, NULL,
867 g_printerr("Unable to register local agent");
868 g_main_loop_quit(main_loop);
871 ofono_running = TRUE;
875 static void ofono_disconnect(DBusConnection *conn, void *user_data)
877 g_print("stopping telephony interface\n");
879 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
881 ofono_running = FALSE;
883 g_dbus_remove_watch(conn, modem_changed_watch);
884 modem_changed_watch = 0;
887 g_source_remove(server_watch);
891 g_at_server_unref(emulator);
895 static void sig_term(int sig)
897 if (__terminated > 0)
902 g_print("Terminating\n");
904 g_main_loop_quit(main_loop);
907 static void disconnect_callback(DBusConnection *conn, void *user_data)
909 g_printerr("D-Bus disconnect\n");
911 g_main_loop_quit(main_loop);
914 static gboolean end_session_and_finish(gpointer user_data)
916 g_at_server_send_unsolicited(emulator, "+CUSATEND");
917 __stktest_test_finish(TRUE);
922 static void expect_response(const unsigned char *pdu, unsigned int len)
924 struct test *test = cur_test->data;
926 STKTEST_RESPONSE_ASSERT(test->rsp_pdu, test->rsp_len, pdu, len);
928 g_idle_add(end_session_and_finish, NULL);
931 static DBusMessage *test_display_text_11(DBusMessage *msg,
933 unsigned char icon_id,
936 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 1"));
937 STKTEST_AGENT_ASSERT(icon_id == 0);
938 STKTEST_AGENT_ASSERT(urgent == FALSE);
940 return dbus_message_new_method_return(msg);
943 static DBusMessage *test_display_text_12(DBusMessage *msg,
945 unsigned char icon_id,
948 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 1"));
949 STKTEST_AGENT_ASSERT(icon_id == 0);
950 STKTEST_AGENT_ASSERT(urgent == FALSE);
952 return stktest_error_busy(msg);
955 static DBusMessage *test_display_text_13(DBusMessage *msg,
957 unsigned char icon_id,
960 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 2"));
961 STKTEST_AGENT_ASSERT(icon_id == 0);
962 STKTEST_AGENT_ASSERT(urgent == TRUE);
964 return dbus_message_new_method_return(msg);
967 static DBusMessage *test_display_text_14(DBusMessage *msg,
969 unsigned char icon_id,
972 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 3"));
973 STKTEST_AGENT_ASSERT(icon_id == 0);
974 STKTEST_AGENT_ASSERT(urgent == FALSE);
976 return dbus_message_new_method_return(msg);
979 static DBusMessage *test_display_text_15(DBusMessage *msg,
981 unsigned char icon_id,
984 STKTEST_AGENT_ASSERT(g_str_equal(text, "Toolkit Test 4"));
985 STKTEST_AGENT_ASSERT(icon_id == 0);
986 STKTEST_AGENT_ASSERT(urgent == FALSE);
988 pending = dbus_message_ref(msg);
993 static void power_down_reply(DBusPendingCall *call, void *user_data)
995 __stktest_test_next();
998 void __stktest_test_finish(gboolean successful)
1000 struct test *test = cur_test->data;
1001 dbus_bool_t powered = FALSE;
1003 test->result = successful ? TEST_RESULT_PASSED : TEST_RESULT_FAILED;
1005 state = TEST_STATE_POWERING_DOWN;
1006 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
1007 DBUS_TYPE_BOOLEAN, &powered,
1008 power_down_reply, NULL, NULL);
1011 void __stktest_test_next()
1013 if (cur_test == NULL)
1016 cur_test = cur_test->next;
1018 if (cur_test == NULL)
1019 g_main_loop_quit(main_loop);
1024 static void stktest_add_test(const char *name, const char *method,
1025 const unsigned char *req, unsigned int req_len,
1026 const unsigned char *rsp, unsigned int rsp_len,
1028 terminal_response_func tr_func)
1030 struct test *test = g_new0(struct test, 1);
1032 test->name = g_strdup(name);
1033 test->method = g_strdup(method);
1034 test->req_pdu = g_memdup(req, req_len);
1035 test->req_len = req_len;
1036 test->rsp_pdu = g_memdup(rsp, rsp_len);
1037 test->rsp_len = rsp_len;
1038 test->agent_func = agent_func;
1039 test->tr_func = tr_func;
1041 tests = g_list_append(tests, test);
1044 static void __stktest_test_init(void)
1046 stktest_add_test("Display Text 1.1", "DisplayText",
1047 display_text_111, sizeof(display_text_111),
1048 display_text_response_111,
1049 sizeof(display_text_response_111),
1050 test_display_text_11, expect_response);
1051 stktest_add_test("Display Text 1.2", "DisplayText",
1052 display_text_111, sizeof(display_text_111),
1053 display_text_response_121,
1054 sizeof(display_text_response_121),
1055 test_display_text_12, expect_response);
1056 stktest_add_test("Display Text 1.3", "DisplayText",
1057 display_text_131, sizeof(display_text_131),
1058 display_text_response_131,
1059 sizeof(display_text_response_131),
1060 test_display_text_13, expect_response);
1061 stktest_add_test("Display Text 1.4", "DisplayText",
1062 display_text_141, sizeof(display_text_141),
1063 display_text_response_141,
1064 sizeof(display_text_response_141),
1065 test_display_text_14, expect_response);
1066 stktest_add_test("Display Text 1.5", "DisplayText",
1067 display_text_151, sizeof(display_text_151),
1068 display_text_response_151,
1069 sizeof(display_text_response_151),
1070 test_display_text_15, expect_response);
1073 static void test_destroy(gpointer user_data)
1075 struct test *test = user_data;
1078 g_free(test->method);
1079 g_free(test->req_pdu);
1080 g_free(test->rsp_pdu);
1085 static void __stktest_test_summarize(void)
1089 g_print("\n\nTest Summary\n");
1090 g_print("============\n");
1092 for (l = tests; l; l = l->next) {
1093 struct test *test = l->data;
1095 g_print("%-60s", test->name);
1097 switch (test->result) {
1098 case TEST_RESULT_NOT_RUN:
1099 g_print("Not Run\n");
1101 case TEST_RESULT_PASSED:
1102 g_print("Passed\n");
1104 case TEST_RESULT_FAILED:
1105 g_print("Failed\n");
1111 static void __stktest_test_cleanup(void)
1113 g_list_free_full(tests, test_destroy);
1118 static gboolean option_version = FALSE;
1120 static GOptionEntry options[] = {
1121 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
1122 "Show version information and exit" },
1126 int main(int argc, char **argv)
1128 GOptionContext *context;
1129 GError *error = NULL;
1132 struct sigaction sa;
1134 context = g_option_context_new(NULL);
1135 g_option_context_add_main_entries(context, options, NULL);
1137 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
1138 if (error != NULL) {
1139 g_printerr("%s\n", error->message);
1140 g_error_free(error);
1142 g_printerr("An unknown error occurred\n");
1146 g_option_context_free(context);
1148 if (option_version == TRUE) {
1149 printf("%s\n", VERSION);
1153 __stktest_test_init();
1155 main_loop = g_main_loop_new(NULL, FALSE);
1157 dbus_error_init(&err);
1159 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
1161 if (dbus_error_is_set(&err) == TRUE) {
1162 fprintf(stderr, "%s\n", err.message);
1163 dbus_error_free(&err);
1165 fprintf(stderr, "Can't register with system bus\n");
1169 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
1171 memset(&sa, 0, sizeof(sa));
1172 sa.sa_handler = sig_term;
1173 sigaction(SIGINT, &sa, NULL);
1174 sigaction(SIGTERM, &sa, NULL);
1176 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
1177 ofono_connect, ofono_disconnect, NULL, NULL);
1179 g_main_loop_run(main_loop);
1181 g_dbus_remove_watch(conn, watch);
1183 if (ofono_running == TRUE)
1184 ofono_disconnect(conn, NULL);
1186 dbus_connection_unref(conn);
1188 g_main_loop_unref(main_loop);
1190 __stktest_test_summarize();
1191 __stktest_test_cleanup();