Upgrade ofono to 1.11 and merge to 2.0alpha
[profile/ivi/ofono.git] / src / ctm.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2010  Nokia Corporation and/or its subsidiary(-ies).
6  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
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 <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <glib.h>
32 #include <gdbus.h>
33
34 #include "ofono.h"
35 #include "common.h"
36
37 #define CTM_FLAG_CACHED 0x1
38
39 static GSList *g_drivers = NULL;
40
41 struct ofono_ctm {
42         DBusMessage *pending;
43         int flags;
44         ofono_bool_t enabled;
45         const struct ofono_ctm_driver *driver;
46         void *driver_data;
47         struct ofono_atom *atom;
48 };
49
50 static DBusMessage *ctm_get_properties_reply(DBusMessage *msg,
51                                                 struct ofono_ctm *ctm)
52 {
53         DBusMessage *reply;
54         DBusMessageIter iter;
55         DBusMessageIter dict;
56         dbus_bool_t value;
57
58         reply = dbus_message_new_method_return(msg);
59         if (reply == NULL)
60                 return NULL;
61
62         dbus_message_iter_init_append(reply, &iter);
63         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
64                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
65                                         &dict);
66
67         value = ctm->enabled;
68         ofono_dbus_dict_append(&dict, "Enabled", DBUS_TYPE_BOOLEAN, &value);
69         dbus_message_iter_close_container(&iter, &dict);
70
71         return reply;
72 }
73
74 static void ctm_signal_enabled(struct ofono_ctm *ctm)
75 {
76         DBusConnection *conn = ofono_dbus_get_connection();
77         const char *path = __ofono_atom_get_path(ctm->atom);
78         ofono_bool_t value = ctm->enabled;
79
80         ofono_dbus_signal_property_changed(conn, path,
81                                                 OFONO_TEXT_TELEPHONY_INTERFACE,
82                                                 "Enabled",
83                                                 DBUS_TYPE_BOOLEAN, &value);
84 }
85
86 static void ctm_set_enabled_callback(const struct ofono_error *error,
87                                         void *data)
88 {
89         struct ofono_ctm *ctm = data;
90         DBusMessage *reply;
91
92         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
93                 DBG("Error setting ctm enabled property");
94
95                 reply = __ofono_error_failed(ctm->pending);
96                 __ofono_dbus_pending_reply(&ctm->pending, reply);
97
98                 return;
99         }
100
101         ctm->enabled = !ctm->enabled;
102
103         reply = dbus_message_new_method_return(ctm->pending);
104         __ofono_dbus_pending_reply(&ctm->pending, reply);
105
106         ctm_signal_enabled(ctm);
107 }
108
109 static void ctm_query_enabled_callback(const struct ofono_error *error,
110                                                 ofono_bool_t enable, void *data)
111 {
112         struct ofono_ctm *ctm = data;
113         DBusMessage *reply;
114         ofono_bool_t enabled_old;
115
116         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
117                 DBG("Error during ctm enabled query");
118
119                 reply = __ofono_error_failed(ctm->pending);
120                 __ofono_dbus_pending_reply(&ctm->pending, reply);
121
122                 return;
123         }
124
125         ctm->flags |= CTM_FLAG_CACHED;
126
127         enabled_old = ctm->enabled;
128         ctm->enabled = enable;
129
130         reply = ctm_get_properties_reply(ctm->pending, ctm);
131         __ofono_dbus_pending_reply(&ctm->pending, reply);
132
133         if (ctm->enabled != enabled_old)
134                 ctm_signal_enabled(ctm);
135 }
136
137 static DBusMessage *ctm_get_properties(DBusConnection *conn,
138                                                 DBusMessage *msg, void *data)
139 {
140         struct ofono_ctm *ctm = data;
141
142         if (ctm->flags & CTM_FLAG_CACHED)
143                 return ctm_get_properties_reply(msg, ctm);
144
145         if (ctm->pending)
146                 return __ofono_error_busy(msg);
147
148         ctm->pending = dbus_message_ref(msg);
149
150         ctm->driver->query_tty(ctm, ctm_query_enabled_callback, ctm);
151
152         return NULL;
153 }
154
155 static DBusMessage *ctm_set_property(DBusConnection *conn, DBusMessage *msg,
156                                         void *data)
157 {
158         struct ofono_ctm *ctm = data;
159         DBusMessageIter iter;
160         DBusMessageIter var;
161         const char *property;
162
163         if (ctm->pending)
164                 return __ofono_error_busy(msg);
165
166         if (!dbus_message_iter_init(msg, &iter))
167                 return __ofono_error_invalid_args(msg);
168
169         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
170                 return __ofono_error_invalid_args(msg);
171
172         dbus_message_iter_get_basic(&iter, &property);
173         dbus_message_iter_next(&iter);
174
175         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
176                 return __ofono_error_invalid_args(msg);
177
178         dbus_message_iter_recurse(&iter, &var);
179
180         if (g_strcmp0(property, "Enabled") == 0) {
181                 dbus_bool_t value;
182                 int target;
183
184                 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
185                         return __ofono_error_invalid_args(msg);
186
187                 dbus_message_iter_get_basic(&var, &value);
188                 target = value;
189
190                 if (ctm->enabled == target)
191                         return dbus_message_new_method_return(msg);
192
193                 ctm->pending = dbus_message_ref(msg);
194
195                 ctm->driver->set_tty(ctm, target,
196                                         ctm_set_enabled_callback, ctm);
197                 return NULL;
198         }
199
200         return __ofono_error_invalid_args(msg);
201 }
202
203 static const GDBusMethodTable ctm_methods[] = {
204         { GDBUS_ASYNC_METHOD("GetProperties",
205                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
206                         ctm_get_properties) },
207         { GDBUS_ASYNC_METHOD("SetProperty",
208                         GDBUS_ARGS({ "property", "s" }, { "value", "v" }), NULL,
209                         ctm_set_property) },
210         { }
211 };
212
213 static const GDBusSignalTable ctm_signals[] = {
214         { GDBUS_SIGNAL("PropertyChanged",
215                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
216         { }
217 };
218
219 int ofono_ctm_driver_register(const struct ofono_ctm_driver *d)
220 {
221         DBG("driver: %p, name: %s", d, d->name);
222
223         if (d == NULL || d->probe == NULL)
224                 return -EINVAL;
225
226         g_drivers = g_slist_prepend(g_drivers, (void *)d);
227
228         return 0;
229 }
230
231 void ofono_ctm_driver_unregister(const struct ofono_ctm_driver *d)
232 {
233         DBG("driver: %p, name: %s", d, d->name);
234
235         if (d == NULL)
236                 return;
237
238         g_drivers = g_slist_remove(g_drivers, (void *)d);
239 }
240
241 static void text_telephony_unregister(struct ofono_atom *atom)
242 {
243         struct ofono_ctm *ctm = __ofono_atom_get_data(atom);
244         const char *path = __ofono_atom_get_path(ctm->atom);
245         DBusConnection *conn = ofono_dbus_get_connection();
246         struct ofono_modem *modem = __ofono_atom_get_modem(ctm->atom);
247
248         ofono_modem_remove_interface(modem, OFONO_TEXT_TELEPHONY_INTERFACE);
249         g_dbus_unregister_interface(conn, path, OFONO_TEXT_TELEPHONY_INTERFACE);
250 }
251
252 static void text_telephony_remove(struct ofono_atom *atom)
253 {
254         struct ofono_ctm *ctm = __ofono_atom_get_data(atom);
255
256         DBG("atom: %p", atom);
257
258         if (ctm == NULL)
259                 return;
260
261         if (ctm->driver && ctm->driver->remove)
262                 ctm->driver->remove(ctm);
263
264         g_free(ctm);
265 }
266
267 struct ofono_ctm *ofono_ctm_create(struct ofono_modem *modem,
268                                         unsigned int vendor,
269                                         const char *driver, void *data)
270 {
271         struct ofono_ctm *ctm;
272         GSList *l;
273
274         if (driver == NULL)
275                 return NULL;
276
277         ctm = g_try_new0(struct ofono_ctm, 1);
278         if (ctm == NULL)
279                 return NULL;
280
281         ctm->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_CTM,
282                                                 text_telephony_remove, ctm);
283
284         for (l = g_drivers; l; l = l->next) {
285                 const struct ofono_ctm_driver *drv = l->data;
286
287                 if (g_strcmp0(drv->name, driver) != 0)
288                         continue;
289
290                 if (drv->probe(ctm, vendor, data) < 0)
291                         continue;
292
293                 ctm->driver = drv;
294                 break;
295         }
296
297         return ctm;
298 }
299
300 void ofono_ctm_register(struct ofono_ctm *ctm)
301 {
302         DBusConnection *conn = ofono_dbus_get_connection();
303         struct ofono_modem *modem = __ofono_atom_get_modem(ctm->atom);
304         const char *path = __ofono_atom_get_path(ctm->atom);
305
306         if (!g_dbus_register_interface(conn, path,
307                                         OFONO_TEXT_TELEPHONY_INTERFACE,
308                                         ctm_methods, ctm_signals,
309                                         NULL, ctm, NULL)) {
310                 ofono_error("Could not create %s interface",
311                                 OFONO_TEXT_TELEPHONY_INTERFACE);
312
313                 return;
314         }
315
316         ofono_modem_add_interface(modem, OFONO_TEXT_TELEPHONY_INTERFACE);
317         __ofono_atom_register(ctm->atom, text_telephony_unregister);
318 }
319
320 void ofono_ctm_remove(struct ofono_ctm *ctm)
321 {
322         __ofono_atom_free(ctm->atom);
323 }
324
325 void ofono_ctm_set_data(struct ofono_ctm *ctm, void *data)
326 {
327         ctm->driver_data = data;
328 }
329
330 void *ofono_ctm_get_data(struct ofono_ctm *ctm)
331 {
332         return ctm->driver_data;
333 }