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_reply(DBusPendingCall *call, void *user_data)
484 DBusMessage *reply = dbus_pending_call_steal_reply(call);
487 dbus_error_init(&err);
489 if (dbus_set_error_from_message(&err, reply) == TRUE) {
490 g_printerr("%s: %s\n", err.name, err.message);
491 dbus_error_free(&err);
494 dbus_message_unref(reply);
496 state = TEST_STATE_RUNNING;
499 static void register_agent()
501 const char *path = "/default";
504 g_print("Gained STK interface, registering agent...\n");
506 status = send_with_reply(STKTEST_PATH, OFONO_STK_INTERFACE,
507 "RegisterAgent", NULL,
508 register_agent_reply, NULL, NULL, 1,
509 DBUS_TYPE_OBJECT_PATH, &path,
513 g_printerr("Unable to register agent with oFono\n");
514 g_main_loop_quit(main_loop);
518 state = TEST_STATE_REGISTERING_AGENT;
521 static gboolean modem_changed(DBusConnection *conn,
522 DBusMessage *msg, void *user_data)
524 DBusMessageIter iter, value;
525 const char *path, *key;
528 if (dbus_message_iter_init(msg, &iter) == FALSE)
531 path = dbus_message_get_path(msg);
533 if (g_str_equal(STKTEST_PATH, path) == FALSE)
536 dbus_message_iter_get_basic(&iter, &key);
538 dbus_message_iter_next(&iter);
539 dbus_message_iter_recurse(&iter, &value);
541 if (g_str_equal(key, "Interfaces") == FALSE)
544 has_stk = has_stk_interface(&value);
547 case TEST_STATE_POWERING_UP:
551 case TEST_STATE_REGISTERING_AGENT:
552 case TEST_STATE_RUNNING:
553 if (has_stk == FALSE)
554 g_printerr("Unexpectedly lost STK interface\n");
556 case TEST_STATE_POWERING_DOWN:
563 static void powerup(void)
565 dbus_bool_t powered = TRUE;
567 state = TEST_STATE_POWERING_UP;
568 set_property(STKTEST_PATH, OFONO_MODEM_INTERFACE, "Powered",
569 DBUS_TYPE_BOOLEAN, &powered,
570 set_property_reply, NULL, NULL);
573 static void get_modems_reply(DBusPendingCall *call, void *user_data)
575 DBusMessage *reply = dbus_pending_call_steal_reply(call);
576 DBusMessageIter iter, list;
578 gboolean found = FALSE;
580 dbus_error_init(&err);
582 if (dbus_set_error_from_message(&err, reply) == TRUE) {
583 g_printerr("%s: %s\n", err.name, err.message);
584 dbus_error_free(&err);
588 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
591 if (dbus_message_iter_init(reply, &iter) == FALSE)
594 dbus_message_iter_recurse(&iter, &list);
596 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
597 DBusMessageIter entry;
600 dbus_message_iter_recurse(&list, &entry);
601 dbus_message_iter_get_basic(&entry, &path);
603 if (g_str_equal(path, STKTEST_PATH))
606 dbus_message_iter_next(&list);
610 dbus_message_unref(reply);
612 if (found == FALSE) {
613 g_printerr("STK Test modem not found\n");
614 g_main_loop_quit(main_loop);
618 g_print("Test modem found\n");
620 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
622 OFONO_MODEM_INTERFACE,
627 if (create_tcp() == FALSE) {
628 g_printerr("Unable to listen on modem emulator socket\n");
629 g_main_loop_quit(main_loop);
635 static int get_modems(DBusConnection *conn)
638 DBusPendingCall *call;
640 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
641 OFONO_MANAGER_INTERFACE, "GetModems");
645 dbus_message_set_auto_start(msg, FALSE);
647 g_print("getting modems\n");
649 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
650 dbus_message_unref(msg);
654 dbus_message_unref(msg);
659 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
661 dbus_pending_call_unref(call);
666 static const GDBusMethodTable agent_methods[] = {
667 { GDBUS_METHOD("Release", NULL, NULL, agent_release) },
668 { GDBUS_METHOD("DisplayText",
669 GDBUS_ARGS({ "text", "s" }, { "icon_id", "y" },
670 { "urgent", "b" }), NULL,
671 agent_display_text) },
675 static void ofono_connect(DBusConnection *conn, void *user_data)
677 g_print("starting telephony interface\n");
679 if (!g_dbus_register_interface(conn, "/default",
680 OFONO_STKAGENT_INTERFACE,
681 agent_methods, NULL, NULL,
683 g_printerr("Unable to register local agent");
684 g_main_loop_quit(main_loop);
687 ofono_running = TRUE;
691 static void ofono_disconnect(DBusConnection *conn, void *user_data)
693 g_print("stopping telephony interface\n");
695 g_dbus_unregister_interface(conn, "/default", OFONO_STKAGENT_INTERFACE);
697 ofono_running = FALSE;
699 g_dbus_remove_watch(conn, modem_changed_watch);
700 modem_changed_watch = 0;
703 g_source_remove(server_watch);
707 g_at_server_unref(emulator);
711 static void sig_term(int sig)
713 if (__terminated > 0)
718 g_print("Terminating\n");
720 g_main_loop_quit(main_loop);
723 static void disconnect_callback(DBusConnection *conn, void *user_data)
725 g_printerr("D-Bus disconnect\n");
727 g_main_loop_quit(main_loop);
730 static gboolean option_version = FALSE;
732 static GOptionEntry options[] = {
733 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
734 "Show version information and exit" },
738 int main(int argc, char **argv)
740 GOptionContext *context;
741 GError *error = NULL;
746 context = g_option_context_new(NULL);
747 g_option_context_add_main_entries(context, options, NULL);
749 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
751 g_printerr("%s\n", error->message);
754 g_printerr("An unknown error occurred\n");
758 g_option_context_free(context);
760 if (option_version == TRUE) {
761 printf("%s\n", VERSION);
765 main_loop = g_main_loop_new(NULL, FALSE);
767 dbus_error_init(&err);
769 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
771 if (dbus_error_is_set(&err) == TRUE) {
772 fprintf(stderr, "%s\n", err.message);
773 dbus_error_free(&err);
775 fprintf(stderr, "Can't register with system bus\n");
779 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
781 memset(&sa, 0, sizeof(sa));
782 sa.sa_handler = sig_term;
783 sigaction(SIGINT, &sa, NULL);
784 sigaction(SIGTERM, &sa, NULL);
786 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
787 ofono_connect, ofono_disconnect, NULL, NULL);
789 g_main_loop_run(main_loop);
791 g_dbus_remove_watch(conn, watch);
793 if (ofono_running == TRUE)
794 ofono_disconnect(conn, NULL);
796 dbus_connection_unref(conn);
798 g_main_loop_unref(main_loop);