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 OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
42 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
43 #define OFONO_STK_INTERFACE OFONO_SERVICE ".SimToolkit"
45 #define LISTEN_PORT 12765
47 static GMainLoop *main_loop = NULL;
48 static volatile sig_atomic_t __terminated = 0;
51 static DBusConnection *conn;
52 static gboolean ofono_running = FALSE;
53 static guint modem_changed_watch;
54 static gboolean stk_ready = FALSE;
57 static guint server_watch;
58 static GAtServer *emulator;
60 /* Emulated modem state variables */
61 static int modem_mode = 0;
63 static gboolean create_tcp(void);
65 static void server_debug(const char *str, void *data)
67 g_print("%s: %s\n", (char *) data, str);
70 static void cgmi_cb(GAtServer *server, GAtServerRequestType type,
71 GAtResult *cmd, gpointer user)
74 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
75 g_at_server_send_info(server, "oFono", TRUE);
76 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
78 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
79 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
82 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
86 static void cgmm_cb(GAtServer *server, GAtServerRequestType type,
87 GAtResult *cmd, gpointer user)
90 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
91 g_at_server_send_info(server, "oFono pre-1.0", TRUE);
92 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
94 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
95 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
98 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
102 static void cgmr_cb(GAtServer *server, GAtServerRequestType type,
103 GAtResult *cmd, gpointer user)
108 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
109 sprintf(buf, "oFono pre-1.0 version: %s", VERSION);
110 g_at_server_send_info(server, buf, TRUE);
111 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
113 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
114 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
117 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
121 static void cgsn_cb(GAtServer *server, GAtServerRequestType type,
122 GAtResult *cmd, gpointer user)
125 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
126 g_at_server_send_info(server, "123456789", TRUE);
127 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
129 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
130 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
133 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
137 static gboolean send_ok(gpointer user)
139 GAtServer *server = user;
141 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
146 static void cfun_cb(GAtServer *server, GAtServerRequestType type,
147 GAtResult *cmd, gpointer user)
152 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
153 g_at_server_send_info(server, "+CFUN: (0-1,4)", TRUE);
154 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
156 case G_AT_SERVER_REQUEST_TYPE_QUERY:
157 snprintf(buf, sizeof(buf), "+CFUN: %d", modem_mode);
158 g_at_server_send_info(server, buf, TRUE);
159 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
161 case G_AT_SERVER_REQUEST_TYPE_SET:
166 g_at_result_iter_init(&iter, cmd);
167 g_at_result_iter_next(&iter, "");
169 if (g_at_result_iter_next_number(&iter, &mode) == FALSE)
172 if (mode != 0 && mode != 1)
175 if (modem_mode == mode) {
176 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
181 g_timeout_add_seconds(1, send_ok, server);
191 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
194 static void listen_again(gpointer user_data)
196 if (create_tcp() == TRUE)
199 g_print("Error listening to socket\n");
200 g_main_loop_quit(main_loop);
203 static void setup_emulator(GAtServer *server)
205 g_at_server_set_debug(server, server_debug, "Server");
207 g_at_server_register(server, "+CGMI", cgmi_cb, NULL, NULL);
208 g_at_server_register(server, "+CGMM", cgmm_cb, NULL, NULL);
209 g_at_server_register(server, "+CGMR", cgmr_cb, NULL, NULL);
210 g_at_server_register(server, "+CGSN", cgsn_cb, NULL, NULL);
211 g_at_server_register(server, "+CFUN", cfun_cb, NULL, NULL);
213 g_at_server_set_disconnect_function(server, listen_again, NULL);
216 static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
219 struct sockaddr saddr;
220 unsigned int len = sizeof(saddr);
222 GIOChannel *client_io = NULL;
227 fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
231 client_io = g_io_channel_unix_new(fd);
233 emulator = g_at_server_new(client_io);
234 g_io_channel_unref(client_io);
236 if (emulator == NULL)
239 setup_emulator(emulator);
246 static gboolean create_tcp(void)
248 struct sockaddr_in addr;
251 GIOChannel *server_io;
253 sk = socket(PF_INET, SOCK_STREAM, 0);
255 g_print("Can't create tcp/ip socket: %s (%d)\n",
256 strerror(errno), errno);
260 memset(&addr, 0, sizeof(addr));
262 addr.sin_family = AF_INET;
263 addr.sin_addr.s_addr = INADDR_ANY;
264 addr.sin_port = htons(LISTEN_PORT);
266 setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
267 if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) {
268 g_print("Can't bind socket: %s (%d)", strerror(errno), errno);
273 if (listen(sk, 1) < 0) {
274 g_print("Can't listen on socket: %s (%d)",
275 strerror(errno), errno);
280 g_print("new tcp is created at tcp port %d\n", LISTEN_PORT);
282 server_io = g_io_channel_unix_new(sk);
283 g_io_channel_set_close_on_unref(server_io, TRUE);
285 server_watch = g_io_add_watch_full(server_io,
287 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
288 on_socket_connected, NULL, NULL);
290 g_io_channel_unref(server_io);
295 static gboolean has_stk_interface(DBusMessageIter *iter)
297 DBusMessageIter entry;
299 dbus_message_iter_recurse(iter, &entry);
301 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
302 const char *interface;
304 dbus_message_iter_get_basic(&entry, &interface);
306 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
309 dbus_message_iter_next(&entry);
315 static gboolean modem_changed(DBusConnection *conn,
316 DBusMessage *msg, void *user_data)
318 DBusMessageIter iter, value;
319 const char *path, *key;
322 if (dbus_message_iter_init(msg, &iter) == FALSE)
325 path = dbus_message_get_path(msg);
327 if (g_str_equal(STKTEST_PATH, path) == FALSE)
330 dbus_message_iter_get_basic(&iter, &key);
332 dbus_message_iter_next(&iter);
333 dbus_message_iter_recurse(&iter, &value);
335 if (g_str_equal(key, "Interfaces") == FALSE)
338 has_stk = has_stk_interface(&value);
340 if (stk_ready && has_stk == FALSE) {
342 g_print("Lost STK interface\n");
343 } else if (stk_ready == FALSE && has_stk == TRUE) {
345 g_print("Gained STK interface\n");
351 static void get_modems_reply(DBusPendingCall *call, void *user_data)
353 DBusMessage *reply = dbus_pending_call_steal_reply(call);
354 DBusMessageIter iter, list;
356 gboolean found = FALSE;
358 dbus_error_init(&err);
360 if (dbus_set_error_from_message(&err, reply) == TRUE) {
361 g_printerr("%s: %s\n", err.name, err.message);
362 dbus_error_free(&err);
366 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
369 if (dbus_message_iter_init(reply, &iter) == FALSE)
372 dbus_message_iter_recurse(&iter, &list);
374 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
375 DBusMessageIter entry;
378 dbus_message_iter_recurse(&list, &entry);
379 dbus_message_iter_get_basic(&entry, &path);
381 if (g_str_equal(path, STKTEST_PATH))
384 dbus_message_iter_next(&list);
388 dbus_message_unref(reply);
390 if (found == FALSE) {
391 g_printerr("STK Test modem not found\n");
392 g_main_loop_quit(main_loop);
396 g_print("Test modem found\n");
398 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
400 OFONO_MODEM_INTERFACE,
405 if (create_tcp() == FALSE) {
406 g_printerr("Unable to listen on modem emulator socket\n");
407 g_main_loop_quit(main_loop);
411 static int get_modems(DBusConnection *conn)
414 DBusPendingCall *call;
416 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
417 OFONO_MANAGER_INTERFACE, "GetModems");
421 dbus_message_set_auto_start(msg, FALSE);
423 g_print("getting modems\n");
425 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
426 dbus_message_unref(msg);
430 dbus_message_unref(msg);
435 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
437 dbus_pending_call_unref(call);
442 static void ofono_connect(DBusConnection *conn, void *user_data)
444 g_print("starting telephony interface\n");
446 ofono_running = TRUE;
451 static void ofono_disconnect(DBusConnection *conn, void *user_data)
453 g_print("stopping telephony interface\n");
455 ofono_running = FALSE;
457 g_dbus_remove_watch(conn, modem_changed_watch);
458 modem_changed_watch = 0;
461 g_source_remove(server_watch);
465 g_at_server_unref(emulator);
469 static void sig_term(int sig)
471 if (__terminated > 0)
476 g_print("Terminating\n");
478 g_main_loop_quit(main_loop);
481 static void disconnect_callback(DBusConnection *conn, void *user_data)
483 g_printerr("D-Bus disconnect\n");
485 g_main_loop_quit(main_loop);
488 static gboolean option_version = FALSE;
490 static GOptionEntry options[] = {
491 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
492 "Show version information and exit" },
496 int main(int argc, char **argv)
498 GOptionContext *context;
499 GError *error = NULL;
504 context = g_option_context_new(NULL);
505 g_option_context_add_main_entries(context, options, NULL);
507 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
509 g_printerr("%s\n", error->message);
512 g_printerr("An unknown error occurred\n");
516 g_option_context_free(context);
518 if (option_version == TRUE) {
519 printf("%s\n", VERSION);
523 main_loop = g_main_loop_new(NULL, FALSE);
525 dbus_error_init(&err);
527 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
529 if (dbus_error_is_set(&err) == TRUE) {
530 fprintf(stderr, "%s\n", err.message);
531 dbus_error_free(&err);
533 fprintf(stderr, "Can't register with system bus\n");
537 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
539 memset(&sa, 0, sizeof(sa));
540 sa.sa_handler = sig_term;
541 sigaction(SIGINT, &sa, NULL);
542 sigaction(SIGTERM, &sa, NULL);
544 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
545 ofono_connect, ofono_disconnect, NULL, NULL);
547 g_main_loop_run(main_loop);
549 g_dbus_remove_watch(conn, watch);
551 if (ofono_running == TRUE)
552 ofono_disconnect(conn, NULL);
554 dbus_connection_unref(conn);
556 g_main_loop_unref(main_loop);