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
36 #define OFONO_SERVICE "org.ofono"
37 #define STKTEST_PATH "/stktest"
38 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
39 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
40 #define OFONO_STK_INTERFACE OFONO_SERVICE ".SimToolkit"
42 static GMainLoop *main_loop = NULL;
43 static volatile sig_atomic_t __terminated = 0;
44 static DBusConnection *conn;
45 static gboolean ofono_running = FALSE;
46 static guint modem_changed_watch;
47 static gboolean stk_ready = FALSE;
49 static gboolean has_stk_interface(DBusMessageIter *iter)
51 DBusMessageIter entry;
53 dbus_message_iter_recurse(iter, &entry);
55 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
56 const char *interface;
58 dbus_message_iter_get_basic(&entry, &interface);
60 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
63 dbus_message_iter_next(&entry);
69 static gboolean modem_changed(DBusConnection *conn,
70 DBusMessage *msg, void *user_data)
72 DBusMessageIter iter, value;
73 const char *path, *key;
76 if (dbus_message_iter_init(msg, &iter) == FALSE)
79 path = dbus_message_get_path(msg);
81 if (g_str_equal(STKTEST_PATH, path) == FALSE)
84 dbus_message_iter_get_basic(&iter, &key);
86 dbus_message_iter_next(&iter);
87 dbus_message_iter_recurse(&iter, &value);
89 if (g_str_equal(key, "Interfaces") == FALSE)
92 has_stk = has_stk_interface(&value);
94 if (stk_ready && has_stk == FALSE) {
96 g_print("Lost STK interface\n");
97 } else if (stk_ready == FALSE && has_stk == TRUE) {
99 g_print("Gained STK interface\n");
105 static void get_modems_reply(DBusPendingCall *call, void *user_data)
107 DBusMessage *reply = dbus_pending_call_steal_reply(call);
108 DBusMessageIter iter, list;
110 gboolean found = FALSE;
112 dbus_error_init(&err);
114 if (dbus_set_error_from_message(&err, reply) == TRUE) {
115 g_printerr("%s: %s\n", err.name, err.message);
116 dbus_error_free(&err);
120 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
123 if (dbus_message_iter_init(reply, &iter) == FALSE)
126 dbus_message_iter_recurse(&iter, &list);
128 while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
129 DBusMessageIter entry;
132 dbus_message_iter_recurse(&list, &entry);
133 dbus_message_iter_get_basic(&entry, &path);
135 if (g_str_equal(path, STKTEST_PATH))
138 dbus_message_iter_next(&list);
142 dbus_message_unref(reply);
144 if (found == FALSE) {
145 g_printerr("STK Test modem not found\n");
146 g_main_loop_quit(main_loop);
150 g_print("Test modem found\n");
152 modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
154 OFONO_MODEM_INTERFACE,
160 static int get_modems(DBusConnection *conn)
163 DBusPendingCall *call;
165 msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
166 OFONO_MANAGER_INTERFACE, "GetModems");
170 dbus_message_set_auto_start(msg, FALSE);
172 g_print("getting modems\n");
174 if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
175 dbus_message_unref(msg);
179 dbus_message_unref(msg);
184 dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
186 dbus_pending_call_unref(call);
191 static void ofono_connect(DBusConnection *conn, void *user_data)
193 g_print("starting telephony interface\n");
195 ofono_running = TRUE;
200 static void ofono_disconnect(DBusConnection *conn, void *user_data)
202 g_print("stopping telephony interface\n");
204 ofono_running = FALSE;
206 g_dbus_remove_watch(conn, modem_changed_watch);
207 modem_changed_watch = 0;
210 static void sig_term(int sig)
212 if (__terminated > 0)
217 g_print("Terminating\n");
219 g_main_loop_quit(main_loop);
222 static void disconnect_callback(DBusConnection *conn, void *user_data)
224 g_printerr("D-Bus disconnect\n");
226 g_main_loop_quit(main_loop);
229 static gboolean option_version = FALSE;
231 static GOptionEntry options[] = {
232 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
233 "Show version information and exit" },
237 int main(int argc, char **argv)
239 GOptionContext *context;
240 GError *error = NULL;
245 context = g_option_context_new(NULL);
246 g_option_context_add_main_entries(context, options, NULL);
248 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
250 g_printerr("%s\n", error->message);
253 g_printerr("An unknown error occurred\n");
257 g_option_context_free(context);
259 if (option_version == TRUE) {
260 printf("%s\n", VERSION);
264 main_loop = g_main_loop_new(NULL, FALSE);
266 dbus_error_init(&err);
268 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
270 if (dbus_error_is_set(&err) == TRUE) {
271 fprintf(stderr, "%s\n", err.message);
272 dbus_error_free(&err);
274 fprintf(stderr, "Can't register with system bus\n");
278 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
280 memset(&sa, 0, sizeof(sa));
281 sa.sa_handler = sig_term;
282 sigaction(SIGINT, &sa, NULL);
283 sigaction(SIGTERM, &sa, NULL);
285 watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
286 ofono_connect, ofono_disconnect, NULL, NULL);
288 g_main_loop_run(main_loop);
290 g_dbus_remove_watch(conn, watch);
292 if (ofono_running == TRUE)
293 ofono_disconnect(conn, NULL);
295 dbus_connection_unref(conn);
297 g_main_loop_unref(main_loop);