5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/device.h>
37 #include <connman/network.h>
38 #include <connman/dbus.h>
39 #include <connman/log.h>
41 #define OFONO_SERVICE "org.ofono"
43 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
45 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
46 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
47 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
48 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
50 #define MODEM_ADDED "ModemAdded"
51 #define MODEM_REMOVED "ModemRemoved"
52 #define PROPERTY_CHANGED "PropertyChanged"
53 #define CONTEXT_ADDED "ContextAdded"
54 #define CONTEXT_REMOVED "ContextRemoved"
56 #define GET_MODEMS "GetModems"
62 OFONO_API_NETREG = 0x2,
66 static DBusConnection *connection;
68 static GHashTable *modem_hash;
75 connman_bool_t powered;
76 connman_bool_t online;
80 static uint8_t extract_interfaces(DBusMessageIter *array)
82 DBusMessageIter entry;
83 uint8_t interfaces = 0;
85 dbus_message_iter_recurse(array, &entry);
87 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
90 dbus_message_iter_get_basic(&entry, &name);
92 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
93 interfaces |= OFONO_API_SIM;
94 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
95 interfaces |= OFONO_API_NETREG;
96 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
97 interfaces |= OFONO_API_CM;
99 dbus_message_iter_next(&entry);
105 static gboolean context_changed(DBusConnection *connection,
106 DBusMessage *message,
112 static gboolean cm_context_added(DBusConnection *connection,
113 DBusMessage *message,
119 static gboolean cm_context_removed(DBusConnection *connection,
120 DBusMessage *message,
126 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
132 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
138 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
144 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
147 const char *path = dbus_message_get_path(message);
148 struct modem_data *modem;
149 DBusMessageIter iter, value;
152 modem = g_hash_table_lookup(modem_hash, path);
156 if (dbus_message_iter_init(message, &iter) == FALSE)
159 dbus_message_iter_get_basic(&iter, &key);
161 dbus_message_iter_next(&iter);
162 dbus_message_iter_recurse(&iter, &value);
164 if (g_str_equal(key, "Powered") == TRUE) {
165 dbus_message_iter_get_basic(&value, &modem->powered);
167 DBG("%s Powered %d", modem->path, modem->powered);
168 } else if (g_str_equal(key, "Online") == TRUE) {
169 dbus_message_iter_get_basic(&value, &modem->online);
171 DBG("%s Online %d", modem->path, modem->online);
172 } else if (g_str_equal(key, "Interfaces") == TRUE) {
173 modem->interfaces = extract_interfaces(&value);
175 DBG("%s Interfaces 0x%02x", modem->path,
177 } else if (g_str_equal(key, "Serial") == TRUE) {
180 dbus_message_iter_get_basic(&value, &serial);
182 g_free(modem->serial);
183 modem->serial = g_strdup(serial);
185 DBG("%s Serial %s", modem->path, modem->serial);
191 static void add_modem(const char *path, DBusMessageIter *prop)
193 struct modem_data *modem;
197 modem = g_hash_table_lookup(modem_hash, path);
200 * When oFono powers up we ask for the modems and oFono is
201 * reporting with modem_added signal the modems. Only
207 modem = g_try_new0(struct modem_data, 1);
211 modem->path = g_strdup(path);
213 g_hash_table_insert(modem_hash, g_strdup(path), modem);
215 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
216 DBusMessageIter entry, value;
219 dbus_message_iter_recurse(prop, &entry);
220 dbus_message_iter_get_basic(&entry, &key);
222 dbus_message_iter_next(&entry);
223 dbus_message_iter_recurse(&entry, &value);
225 if (g_str_equal(key, "Powered") == TRUE) {
226 dbus_message_iter_get_basic(&value, &modem->powered);
228 DBG("%s Powered %d", modem->path, modem->powered);
229 } else if (g_str_equal(key, "Online") == TRUE) {
230 dbus_message_iter_get_basic(&value, &modem->online);
232 DBG("%s Online %d", modem->path, modem->online);
233 } else if (g_str_equal(key, "Interfaces") == TRUE) {
234 modem->interfaces = extract_interfaces(&value);
236 DBG("%s Interfaces 0x%02x", modem->path,
238 } else if (g_str_equal(key, "Serial") == TRUE) {
241 dbus_message_iter_get_basic(&value, &serial);
242 modem->serial = g_strdup(serial);
244 DBG("%s Serial %s", modem->path, modem->serial);
247 dbus_message_iter_next(prop);
251 static void remove_modem(gpointer data)
253 struct modem_data *modem = data;
255 DBG("%s", modem->path);
257 g_free(modem->serial);
263 static gboolean modem_added(DBusConnection *connection,
264 DBusMessage *message, void *user_data)
266 DBusMessageIter iter, properties;
271 if (dbus_message_iter_init(message, &iter) == FALSE)
274 dbus_message_iter_get_basic(&iter, &path);
276 dbus_message_iter_next(&iter);
277 dbus_message_iter_recurse(&iter, &properties);
279 add_modem(path, &properties);
284 static gboolean modem_removed(DBusConnection *connection,
285 DBusMessage *message, void *user_data)
287 DBusMessageIter iter;
292 if (dbus_message_iter_init(message, &iter) == FALSE)
295 dbus_message_iter_get_basic(&iter, &path);
297 g_hash_table_remove(modem_hash, path);
302 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
306 DBusMessageIter array, dict;
310 reply = dbus_pending_call_steal_reply(call);
312 dbus_error_init(&error);
314 if (dbus_set_error_from_message(&error, reply) == TRUE) {
315 connman_error("%s", error.message);
316 dbus_error_free(&error);
320 if (dbus_message_iter_init(reply, &array) == FALSE)
323 dbus_message_iter_recurse(&array, &dict);
325 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
326 DBusMessageIter value, properties;
329 dbus_message_iter_recurse(&dict, &value);
330 dbus_message_iter_get_basic(&value, &path);
332 dbus_message_iter_next(&value);
333 dbus_message_iter_recurse(&value, &properties);
335 add_modem(path, &properties);
337 dbus_message_iter_next(&dict);
341 dbus_message_unref(reply);
343 dbus_pending_call_unref(call);
346 static int manager_get_modems(void)
348 DBusMessage *message;
349 DBusPendingCall *call;
353 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
354 OFONO_MANAGER_INTERFACE, GET_MODEMS);
358 if (dbus_connection_send_with_reply(connection, message,
359 &call, TIMEOUT) == FALSE) {
360 connman_error("Failed to call GetModems()");
361 dbus_message_unref(message);
366 connman_error("D-Bus connection not available");
367 dbus_message_unref(message);
371 dbus_pending_call_set_notify(call, manager_get_modems_reply,
374 dbus_message_unref(message);
379 static void ofono_connect(DBusConnection *conn, void *user_data)
383 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
384 g_free, remove_modem);
385 if (modem_hash == NULL)
388 manager_get_modems();
391 static void ofono_disconnect(DBusConnection *conn, void *user_data)
395 if (modem_hash == NULL)
398 g_hash_table_destroy(modem_hash);
402 static int network_probe(struct connman_network *network)
404 DBG("network %p", network);
409 static void network_remove(struct connman_network *network)
411 DBG("network %p", network);
414 static int network_connect(struct connman_network *network)
416 DBG("network %p", network);
421 static int network_disconnect(struct connman_network *network)
423 DBG("network %p", network);
428 static struct connman_network_driver network_driver = {
430 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
431 .probe = network_probe,
432 .remove = network_remove,
433 .connect = network_connect,
434 .disconnect = network_disconnect,
437 static int modem_probe(struct connman_device *device)
439 DBG("device %p", device);
444 static void modem_remove(struct connman_device *device)
446 DBG("device %p", device);
449 static int modem_enable(struct connman_device *device)
451 DBG("device %p", device);
456 static int modem_disable(struct connman_device *device)
458 DBG("device %p", device);
463 static struct connman_device_driver modem_driver = {
465 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
466 .probe = modem_probe,
467 .remove = modem_remove,
468 .enable = modem_enable,
469 .disable = modem_disable,
473 static guint modem_added_watch;
474 static guint modem_removed_watch;
475 static guint modem_watch;
476 static guint cm_watch;
477 static guint sim_watch;
478 static guint context_added_watch;
479 static guint context_removed_watch;
480 static guint netreg_watch;
481 static guint context_watch;
483 static int ofono_init(void)
489 connection = connman_dbus_get_connection();
490 if (connection == NULL)
493 watch = g_dbus_add_service_watch(connection,
494 OFONO_SERVICE, ofono_connect,
495 ofono_disconnect, NULL, NULL);
497 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
498 OFONO_MANAGER_INTERFACE,
503 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
504 OFONO_MANAGER_INTERFACE,
509 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
510 OFONO_MODEM_INTERFACE,
515 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
521 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
527 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
533 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
539 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
540 OFONO_CONTEXT_INTERFACE,
545 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
546 OFONO_NETREG_INTERFACE,
552 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
553 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
554 context_added_watch == 0 ||
555 context_removed_watch == 0 ||
556 context_watch == 0 || netreg_watch == 0) {
561 err = connman_network_driver_register(&network_driver);
565 err = connman_device_driver_register(&modem_driver);
567 connman_network_driver_unregister(&network_driver);
574 g_dbus_remove_watch(connection, netreg_watch);
575 g_dbus_remove_watch(connection, context_watch);
576 g_dbus_remove_watch(connection, context_removed_watch);
577 g_dbus_remove_watch(connection, context_added_watch);
578 g_dbus_remove_watch(connection, sim_watch);
579 g_dbus_remove_watch(connection, cm_watch);
580 g_dbus_remove_watch(connection, modem_watch);
581 g_dbus_remove_watch(connection, modem_removed_watch);
582 g_dbus_remove_watch(connection, modem_added_watch);
583 g_dbus_remove_watch(connection, watch);
584 dbus_connection_unref(connection);
589 static void ofono_exit(void)
593 if (modem_hash != NULL) {
594 g_hash_table_destroy(modem_hash);
598 connman_device_driver_unregister(&modem_driver);
599 connman_network_driver_unregister(&network_driver);
601 g_dbus_remove_watch(connection, netreg_watch);
602 g_dbus_remove_watch(connection, context_watch);
603 g_dbus_remove_watch(connection, context_removed_watch);
604 g_dbus_remove_watch(connection, context_added_watch);
605 g_dbus_remove_watch(connection, sim_watch);
606 g_dbus_remove_watch(connection, cm_watch);
607 g_dbus_remove_watch(connection, modem_watch);
608 g_dbus_remove_watch(connection, modem_added_watch);
609 g_dbus_remove_watch(connection, modem_removed_watch);
610 g_dbus_remove_watch(connection, watch);
612 dbus_connection_unref(connection);
615 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
616 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)