ofono: Listen to oFono's D-Bus signals
[platform/upstream/connman.git] / plugins / ofono.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include <gdbus.h>
31 #include <string.h>
32
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/device.h>
36 #include <connman/network.h>
37 #include <connman/dbus.h>
38 #include <connman/log.h>
39
40 #define OFONO_SERVICE                   "org.ofono"
41
42 #define OFONO_MANAGER_INTERFACE         OFONO_SERVICE ".Manager"
43 #define OFONO_MODEM_INTERFACE           OFONO_SERVICE ".Modem"
44 #define OFONO_SIM_INTERFACE             OFONO_SERVICE ".SimManager"
45 #define OFONO_NETREG_INTERFACE          OFONO_SERVICE ".NetworkRegistration"
46 #define OFONO_CM_INTERFACE              OFONO_SERVICE ".ConnectionManager"
47 #define OFONO_CONTEXT_INTERFACE         OFONO_SERVICE ".ConnectionContext"
48
49 #define MODEM_ADDED                     "ModemAdded"
50 #define MODEM_REMOVED                   "ModemRemoved"
51 #define PROPERTY_CHANGED                "PropertyChanged"
52 #define CONTEXT_ADDED                   "ContextAdded"
53 #define CONTEXT_REMOVED                 "ContextRemoved"
54
55 static DBusConnection *connection;
56
57 static gboolean context_changed(DBusConnection *connection,
58                                 DBusMessage *message,
59                                 void *user_data)
60 {
61         return TRUE;
62 }
63
64 static gboolean cm_context_added(DBusConnection *connection,
65                                         DBusMessage *message,
66                                         void *user_data)
67 {
68         return TRUE;
69 }
70
71 static gboolean cm_context_removed(DBusConnection *connection,
72                                         DBusMessage *message,
73                                         void *user_data)
74 {
75         return TRUE;
76 }
77
78 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
79                                 void *user_data)
80 {
81         return TRUE;
82 }
83
84 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
85                                 void *user_data)
86 {
87         return TRUE;
88 }
89
90 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
91                                 void *user_data)
92 {
93         return TRUE;
94 }
95
96 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
97                                 void *user_data)
98 {
99         return TRUE;
100 }
101
102 static gboolean modem_added(DBusConnection *connection,
103                                 DBusMessage *message, void *user_data)
104 {
105         return TRUE;
106 }
107
108 static gboolean modem_removed(DBusConnection *connection,
109                                 DBusMessage *message, void *user_data)
110 {
111         return TRUE;
112 }
113
114 static void ofono_connect(DBusConnection *conn, void *user_data)
115 {
116 }
117
118 static void ofono_disconnect(DBusConnection *conn, void *user_data)
119 {
120 }
121
122 static int network_probe(struct connman_network *network)
123 {
124         DBG("network %p", network);
125
126         return 0;
127 }
128
129 static void network_remove(struct connman_network *network)
130 {
131         DBG("network %p", network);
132 }
133
134 static int network_connect(struct connman_network *network)
135 {
136         DBG("network %p", network);
137
138         return 0;
139 }
140
141 static int network_disconnect(struct connman_network *network)
142 {
143         DBG("network %p", network);
144
145         return 0;
146 }
147
148 static struct connman_network_driver network_driver = {
149         .name           = "network",
150         .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
151         .probe          = network_probe,
152         .remove         = network_remove,
153         .connect        = network_connect,
154         .disconnect     = network_disconnect,
155 };
156
157 static int modem_probe(struct connman_device *device)
158 {
159         DBG("device %p", device);
160
161         return 0;
162 }
163
164 static void modem_remove(struct connman_device *device)
165 {
166         DBG("device %p", device);
167 }
168
169 static int modem_enable(struct connman_device *device)
170 {
171         DBG("device %p", device);
172
173         return 0;
174 }
175
176 static int modem_disable(struct connman_device *device)
177 {
178         DBG("device %p", device);
179
180         return 0;
181 }
182
183 static struct connman_device_driver modem_driver = {
184         .name           = "modem",
185         .type           = CONNMAN_DEVICE_TYPE_CELLULAR,
186         .probe          = modem_probe,
187         .remove         = modem_remove,
188         .enable         = modem_enable,
189         .disable        = modem_disable,
190 };
191
192 static guint watch;
193 static guint modem_added_watch;
194 static guint modem_removed_watch;
195 static guint modem_watch;
196 static guint cm_watch;
197 static guint sim_watch;
198 static guint context_added_watch;
199 static guint context_removed_watch;
200 static guint netreg_watch;
201 static guint context_watch;
202
203 static int ofono_init(void)
204 {
205         int err;
206
207         DBG("");
208
209         connection = connman_dbus_get_connection();
210         if (connection == NULL)
211                 return -EIO;
212
213         watch = g_dbus_add_service_watch(connection,
214                                         OFONO_SERVICE, ofono_connect,
215                                         ofono_disconnect, NULL, NULL);
216
217         modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
218                                                 OFONO_MANAGER_INTERFACE,
219                                                 MODEM_ADDED,
220                                                 modem_added,
221                                                 NULL, NULL);
222
223         modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
224                                                 OFONO_MANAGER_INTERFACE,
225                                                 MODEM_REMOVED,
226                                                 modem_removed,
227                                                 NULL, NULL);
228
229         modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
230                                                 OFONO_MODEM_INTERFACE,
231                                                 PROPERTY_CHANGED,
232                                                 modem_changed,
233                                                 NULL, NULL);
234
235         cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
236                                                 OFONO_CM_INTERFACE,
237                                                 PROPERTY_CHANGED,
238                                                 cm_changed,
239                                                 NULL, NULL);
240
241         sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
242                                                 OFONO_SIM_INTERFACE,
243                                                 PROPERTY_CHANGED,
244                                                 sim_changed,
245                                                 NULL, NULL);
246
247         context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
248                                                 OFONO_CM_INTERFACE,
249                                                 CONTEXT_ADDED,
250                                                 cm_context_added,
251                                                 NULL, NULL);
252
253         context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
254                                                 OFONO_CM_INTERFACE,
255                                                 CONTEXT_REMOVED,
256                                                 cm_context_removed,
257                                                 NULL, NULL);
258
259         context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
260                                                 OFONO_CONTEXT_INTERFACE,
261                                                 PROPERTY_CHANGED,
262                                                 context_changed,
263                                                 NULL, NULL);
264
265         netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
266                                                 OFONO_NETREG_INTERFACE,
267                                                 PROPERTY_CHANGED,
268                                                 netreg_changed,
269                                                 NULL, NULL);
270
271
272         if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
273                         modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
274                         context_added_watch == 0 ||
275                         context_removed_watch == 0 ||
276                         context_watch == 0 || netreg_watch == 0) {
277                 err = -EIO;
278                 goto remove;
279         }
280
281         err = connman_network_driver_register(&network_driver);
282         if (err < 0)
283                 goto remove;
284
285         err = connman_device_driver_register(&modem_driver);
286         if (err < 0) {
287                 connman_network_driver_unregister(&network_driver);
288                 goto remove;
289         }
290
291         return 0;
292
293 remove:
294         g_dbus_remove_watch(connection, netreg_watch);
295         g_dbus_remove_watch(connection, context_watch);
296         g_dbus_remove_watch(connection, context_removed_watch);
297         g_dbus_remove_watch(connection, context_added_watch);
298         g_dbus_remove_watch(connection, sim_watch);
299         g_dbus_remove_watch(connection, cm_watch);
300         g_dbus_remove_watch(connection, modem_watch);
301         g_dbus_remove_watch(connection, modem_removed_watch);
302         g_dbus_remove_watch(connection, modem_added_watch);
303         g_dbus_remove_watch(connection, watch);
304         dbus_connection_unref(connection);
305
306         return err;
307 }
308
309 static void ofono_exit(void)
310 {
311         DBG("");
312
313         connman_device_driver_unregister(&modem_driver);
314         connman_network_driver_unregister(&network_driver);
315
316         g_dbus_remove_watch(connection, netreg_watch);
317         g_dbus_remove_watch(connection, context_watch);
318         g_dbus_remove_watch(connection, context_removed_watch);
319         g_dbus_remove_watch(connection, context_added_watch);
320         g_dbus_remove_watch(connection, sim_watch);
321         g_dbus_remove_watch(connection, cm_watch);
322         g_dbus_remove_watch(connection, modem_watch);
323         g_dbus_remove_watch(connection, modem_added_watch);
324         g_dbus_remove_watch(connection, modem_removed_watch);
325         g_dbus_remove_watch(connection, watch);
326
327         dbus_connection_unref(connection);
328 }
329
330 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
331                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)