tools: Add basic stktest skeleton
[platform/upstream/ofono.git] / tools / stktest.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <signal.h>
33
34 #include <gdbus.h>
35
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"
41
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;
48
49 static gboolean has_stk_interface(DBusMessageIter *iter)
50 {
51         DBusMessageIter entry;
52
53         dbus_message_iter_recurse(iter, &entry);
54
55         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
56                 const char *interface;
57
58                 dbus_message_iter_get_basic(&entry, &interface);
59
60                 if (g_str_equal(interface, OFONO_STK_INTERFACE) == TRUE)
61                         return TRUE;
62
63                 dbus_message_iter_next(&entry);
64         }
65
66         return FALSE;
67 }
68
69 static gboolean modem_changed(DBusConnection *conn,
70                                 DBusMessage *msg, void *user_data)
71 {
72         DBusMessageIter iter, value;
73         const char *path, *key;
74         gboolean has_stk;
75
76         if (dbus_message_iter_init(msg, &iter) == FALSE)
77                 return TRUE;
78
79         path = dbus_message_get_path(msg);
80
81         if (g_str_equal(STKTEST_PATH, path) == FALSE)
82                 return TRUE;
83
84         dbus_message_iter_get_basic(&iter, &key);
85
86         dbus_message_iter_next(&iter);
87         dbus_message_iter_recurse(&iter, &value);
88
89         if (g_str_equal(key, "Interfaces") == FALSE)
90                 return TRUE;
91
92         has_stk = has_stk_interface(&value);
93
94         if (stk_ready && has_stk == FALSE) {
95                 stk_ready = FALSE;
96                 g_print("Lost STK interface\n");
97         } else if (stk_ready == FALSE && has_stk == TRUE) {
98                 stk_ready = TRUE;
99                 g_print("Gained STK interface\n");
100         }
101
102         return TRUE;
103 }
104
105 static void get_modems_reply(DBusPendingCall *call, void *user_data)
106 {
107         DBusMessage *reply = dbus_pending_call_steal_reply(call);
108         DBusMessageIter iter, list;
109         DBusError err;
110         gboolean found = FALSE;
111
112         dbus_error_init(&err);
113
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);
117                 goto done;
118         }
119
120         if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
121                 goto done;
122
123         if (dbus_message_iter_init(reply, &iter) == FALSE)
124                 goto done;
125
126         dbus_message_iter_recurse(&iter, &list);
127
128         while (dbus_message_iter_get_arg_type(&list) == DBUS_TYPE_STRUCT) {
129                 DBusMessageIter entry;
130                 const char *path;
131
132                 dbus_message_iter_recurse(&list, &entry);
133                 dbus_message_iter_get_basic(&entry, &path);
134
135                 if (g_str_equal(path, STKTEST_PATH))
136                         found = TRUE;
137
138                 dbus_message_iter_next(&list);
139         }
140
141 done:
142         dbus_message_unref(reply);
143
144         if (found == FALSE) {
145                 g_printerr("STK Test modem not found\n");
146                 g_main_loop_quit(main_loop);
147                 return;
148         }
149
150         g_print("Test modem found\n");
151
152         modem_changed_watch = g_dbus_add_signal_watch(conn, OFONO_SERVICE,
153                                                         STKTEST_PATH,
154                                                         OFONO_MODEM_INTERFACE,
155                                                         "PropertyChanged",
156                                                         modem_changed,
157                                                         NULL, NULL);
158 }
159
160 static int get_modems(DBusConnection *conn)
161 {
162         DBusMessage *msg;
163         DBusPendingCall *call;
164
165         msg = dbus_message_new_method_call(OFONO_SERVICE, "/",
166                                         OFONO_MANAGER_INTERFACE, "GetModems");
167         if (msg == NULL)
168                 return -ENOMEM;
169
170         dbus_message_set_auto_start(msg, FALSE);
171
172         g_print("getting modems\n");
173
174         if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
175                 dbus_message_unref(msg);
176                 return -EIO;
177         }
178
179         dbus_message_unref(msg);
180
181         if (call == NULL)
182                 return -EINVAL;
183
184         dbus_pending_call_set_notify(call, get_modems_reply, conn, NULL);
185
186         dbus_pending_call_unref(call);
187
188         return 0;
189 }
190
191 static void ofono_connect(DBusConnection *conn, void *user_data)
192 {
193         g_print("starting telephony interface\n");
194
195         ofono_running = TRUE;
196
197         get_modems(conn);
198 }
199
200 static void ofono_disconnect(DBusConnection *conn, void *user_data)
201 {
202         g_print("stopping telephony interface\n");
203
204         ofono_running = FALSE;
205
206         g_dbus_remove_watch(conn, modem_changed_watch);
207         modem_changed_watch = 0;
208 }
209
210 static void sig_term(int sig)
211 {
212         if (__terminated > 0)
213                 return;
214
215         __terminated = 1;
216
217         g_print("Terminating\n");
218
219         g_main_loop_quit(main_loop);
220 }
221
222 static void disconnect_callback(DBusConnection *conn, void *user_data)
223 {
224         g_printerr("D-Bus disconnect\n");
225
226         g_main_loop_quit(main_loop);
227 }
228
229 static gboolean option_version = FALSE;
230
231 static GOptionEntry options[] = {
232         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
233                                 "Show version information and exit" },
234         { NULL },
235 };
236
237 int main(int argc, char **argv)
238 {
239         GOptionContext *context;
240         GError *error = NULL;
241         DBusError err;
242         guint watch;
243         struct sigaction sa;
244
245         context = g_option_context_new(NULL);
246         g_option_context_add_main_entries(context, options, NULL);
247
248         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
249                 if (error != NULL) {
250                         g_printerr("%s\n", error->message);
251                         g_error_free(error);
252                 } else
253                         g_printerr("An unknown error occurred\n");
254                 exit(1);
255         }
256
257         g_option_context_free(context);
258
259         if (option_version == TRUE) {
260                 printf("%s\n", VERSION);
261                 exit(0);
262         }
263
264         main_loop = g_main_loop_new(NULL, FALSE);
265
266         dbus_error_init(&err);
267
268         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
269         if (conn == NULL) {
270                 if (dbus_error_is_set(&err) == TRUE) {
271                         fprintf(stderr, "%s\n", err.message);
272                         dbus_error_free(&err);
273                 } else
274                         fprintf(stderr, "Can't register with system bus\n");
275                 exit(1);
276         }
277
278         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
279
280         memset(&sa, 0, sizeof(sa));
281         sa.sa_handler = sig_term;
282         sigaction(SIGINT, &sa, NULL);
283         sigaction(SIGTERM, &sa, NULL);
284
285         watch = g_dbus_add_service_watch(conn, OFONO_SERVICE,
286                                 ofono_connect, ofono_disconnect, NULL, NULL);
287
288         g_main_loop_run(main_loop);
289
290         g_dbus_remove_watch(conn, watch);
291
292         if (ofono_running == TRUE)
293                 ofono_disconnect(conn, NULL);
294
295         dbus_connection_unref(conn);
296
297         g_main_loop_unref(main_loop);
298
299         return 0;
300 }