3 * oFono - Open Source Telephony
5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 * Copyright (C) 2011 Intel Corporation. All rights reserved.
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.
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.
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
37 #define CTM_FLAG_CACHED 0x1
39 static GSList *g_drivers = NULL;
45 const struct ofono_ctm_driver *driver;
47 struct ofono_atom *atom;
50 static DBusMessage *ctm_get_properties_reply(DBusMessage *msg,
51 struct ofono_ctm *ctm)
58 reply = dbus_message_new_method_return(msg);
62 dbus_message_iter_init_append(reply, &iter);
63 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
64 OFONO_PROPERTIES_ARRAY_SIGNATURE,
68 ofono_dbus_dict_append(&dict, "Enabled", DBUS_TYPE_BOOLEAN, &value);
69 dbus_message_iter_close_container(&iter, &dict);
74 static void ctm_signal_enabled(struct ofono_ctm *ctm)
76 DBusConnection *conn = ofono_dbus_get_connection();
77 const char *path = __ofono_atom_get_path(ctm->atom);
78 ofono_bool_t value = ctm->enabled;
80 ofono_dbus_signal_property_changed(conn, path,
81 OFONO_TEXT_TELEPHONY_INTERFACE,
83 DBUS_TYPE_BOOLEAN, &value);
86 static void ctm_set_enabled_callback(const struct ofono_error *error,
89 struct ofono_ctm *ctm = data;
92 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
93 DBG("Error setting ctm enabled property");
95 reply = __ofono_error_failed(ctm->pending);
96 __ofono_dbus_pending_reply(&ctm->pending, reply);
101 ctm->enabled = !ctm->enabled;
103 reply = dbus_message_new_method_return(ctm->pending);
104 __ofono_dbus_pending_reply(&ctm->pending, reply);
106 ctm_signal_enabled(ctm);
109 static void ctm_query_enabled_callback(const struct ofono_error *error,
110 ofono_bool_t enable, void *data)
112 struct ofono_ctm *ctm = data;
114 ofono_bool_t enabled_old;
116 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
117 DBG("Error during ctm enabled query");
119 reply = __ofono_error_failed(ctm->pending);
120 __ofono_dbus_pending_reply(&ctm->pending, reply);
125 ctm->flags |= CTM_FLAG_CACHED;
127 enabled_old = ctm->enabled;
128 ctm->enabled = enable;
130 reply = ctm_get_properties_reply(ctm->pending, ctm);
131 __ofono_dbus_pending_reply(&ctm->pending, reply);
133 if (ctm->enabled != enabled_old)
134 ctm_signal_enabled(ctm);
137 static DBusMessage *ctm_get_properties(DBusConnection *conn,
138 DBusMessage *msg, void *data)
140 struct ofono_ctm *ctm = data;
142 if (ctm->flags & CTM_FLAG_CACHED)
143 return ctm_get_properties_reply(msg, ctm);
146 return __ofono_error_busy(msg);
148 ctm->pending = dbus_message_ref(msg);
150 ctm->driver->query_tty(ctm, ctm_query_enabled_callback, ctm);
155 static DBusMessage *ctm_set_property(DBusConnection *conn, DBusMessage *msg,
158 struct ofono_ctm *ctm = data;
159 DBusMessageIter iter;
161 const char *property;
164 return __ofono_error_busy(msg);
166 if (!dbus_message_iter_init(msg, &iter))
167 return __ofono_error_invalid_args(msg);
169 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
170 return __ofono_error_invalid_args(msg);
172 dbus_message_iter_get_basic(&iter, &property);
173 dbus_message_iter_next(&iter);
175 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
176 return __ofono_error_invalid_args(msg);
178 dbus_message_iter_recurse(&iter, &var);
180 if (g_strcmp0(property, "Enabled") == 0) {
184 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
185 return __ofono_error_invalid_args(msg);
187 dbus_message_iter_get_basic(&var, &value);
190 if (ctm->enabled == target)
191 return dbus_message_new_method_return(msg);
193 ctm->pending = dbus_message_ref(msg);
195 ctm->driver->set_tty(ctm, target,
196 ctm_set_enabled_callback, ctm);
200 return __ofono_error_invalid_args(msg);
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,
213 static const GDBusSignalTable ctm_signals[] = {
214 { GDBUS_SIGNAL("PropertyChanged",
215 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
219 int ofono_ctm_driver_register(const struct ofono_ctm_driver *d)
221 DBG("driver: %p, name: %s", d, d->name);
223 if (d == NULL || d->probe == NULL)
226 g_drivers = g_slist_prepend(g_drivers, (void *)d);
231 void ofono_ctm_driver_unregister(const struct ofono_ctm_driver *d)
233 DBG("driver: %p, name: %s", d, d->name);
238 g_drivers = g_slist_remove(g_drivers, (void *)d);
241 static void text_telephony_unregister(struct ofono_atom *atom)
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);
248 ofono_modem_remove_interface(modem, OFONO_TEXT_TELEPHONY_INTERFACE);
249 g_dbus_unregister_interface(conn, path, OFONO_TEXT_TELEPHONY_INTERFACE);
252 static void text_telephony_remove(struct ofono_atom *atom)
254 struct ofono_ctm *ctm = __ofono_atom_get_data(atom);
256 DBG("atom: %p", atom);
261 if (ctm->driver && ctm->driver->remove)
262 ctm->driver->remove(ctm);
267 struct ofono_ctm *ofono_ctm_create(struct ofono_modem *modem,
269 const char *driver, void *data)
271 struct ofono_ctm *ctm;
277 ctm = g_try_new0(struct ofono_ctm, 1);
281 ctm->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_CTM,
282 text_telephony_remove, ctm);
284 for (l = g_drivers; l; l = l->next) {
285 const struct ofono_ctm_driver *drv = l->data;
287 if (g_strcmp0(drv->name, driver) != 0)
290 if (drv->probe(ctm, vendor, data) < 0)
300 void ofono_ctm_register(struct ofono_ctm *ctm)
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);
306 if (!g_dbus_register_interface(conn, path,
307 OFONO_TEXT_TELEPHONY_INTERFACE,
308 ctm_methods, ctm_signals,
310 ofono_error("Could not create %s interface",
311 OFONO_TEXT_TELEPHONY_INTERFACE);
316 ofono_modem_add_interface(modem, OFONO_TEXT_TELEPHONY_INTERFACE);
317 __ofono_atom_register(ctm->atom, text_telephony_unregister);
320 void ofono_ctm_remove(struct ofono_ctm *ctm)
322 __ofono_atom_free(ctm->atom);
325 void ofono_ctm_set_data(struct ofono_ctm *ctm, void *data)
327 ctm->driver_data = data;
330 void *ofono_ctm_get_data(struct ofono_ctm *ctm)
332 return ctm->driver_data;