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 SET_PROPERTY "SetProperty"
57 #define GET_MODEMS "GetModems"
63 OFONO_API_NETREG = 0x2,
67 static DBusConnection *connection;
69 static GHashTable *modem_hash;
76 connman_bool_t powered;
77 connman_bool_t online;
80 connman_bool_t set_powered;
83 DBusPendingCall *call_set_property;
86 typedef void (*set_property_cb)(struct modem_data *data,
87 connman_bool_t success);
89 struct property_info {
90 struct modem_data *modem;
92 const char *interface;
94 set_property_cb set_property_cb;
97 static void set_property_reply(DBusPendingCall *call, void *user_data)
99 struct property_info *info = user_data;
102 connman_bool_t success = TRUE;
104 DBG("%s path %s %s.%s", info->modem->path,
105 info->path, info->interface, info->property);
107 info->modem->call_set_property = NULL;
109 dbus_error_init(&error);
111 reply = dbus_pending_call_steal_reply(call);
113 if (dbus_set_error_from_message(&error, reply)) {
114 connman_error("Failed to change property: %s %s.%s: %s %s",
115 info->path, info->interface, info->property,
116 error.name, error.message);
117 dbus_error_free(&error);
121 if (info->set_property_cb != NULL)
122 (*info->set_property_cb)(info->modem, success);
124 dbus_message_unref(reply);
126 dbus_pending_call_unref(call);
129 static int set_property(struct modem_data *modem,
130 const char *path, const char *interface,
131 const char *property, int type, void *value,
132 set_property_cb notify)
134 DBusMessage *message;
135 DBusMessageIter iter;
136 struct property_info *info;
138 DBG("%s path %s %s.%s", modem->path, path, interface, property);
140 if (modem->call_set_property != NULL) {
141 connman_error("Pending SetProperty");
145 message = dbus_message_new_method_call(OFONO_SERVICE, path,
146 interface, SET_PROPERTY);
150 dbus_message_iter_init_append(message, &iter);
151 connman_dbus_property_append_basic(&iter, property, type, value);
153 if (dbus_connection_send_with_reply(connection, message,
154 &modem->call_set_property, TIMEOUT) == FALSE) {
155 connman_error("Failed to change property: %s %s.%s",
156 path, interface, property);
157 dbus_message_unref(message);
161 if (modem->call_set_property == NULL) {
162 connman_error("D-Bus connection not available");
163 dbus_message_unref(message);
167 info = g_try_new0(struct property_info, 1);
169 dbus_message_unref(message);
175 info->interface = interface;
176 info->property = property;
177 info->set_property_cb = notify;
179 dbus_pending_call_set_notify(modem->call_set_property,
180 set_property_reply, info, g_free);
182 dbus_message_unref(message);
187 static int modem_set_powered(struct modem_data *modem)
189 DBG("%s", modem->path);
191 modem->set_powered = TRUE;
193 return set_property(modem, modem->path,
194 OFONO_MODEM_INTERFACE,
195 "Powered", DBUS_TYPE_BOOLEAN,
200 static uint8_t extract_interfaces(DBusMessageIter *array)
202 DBusMessageIter entry;
203 uint8_t interfaces = 0;
205 dbus_message_iter_recurse(array, &entry);
207 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
210 dbus_message_iter_get_basic(&entry, &name);
212 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
213 interfaces |= OFONO_API_SIM;
214 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
215 interfaces |= OFONO_API_NETREG;
216 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
217 interfaces |= OFONO_API_CM;
219 dbus_message_iter_next(&entry);
225 static gboolean context_changed(DBusConnection *connection,
226 DBusMessage *message,
232 static gboolean cm_context_added(DBusConnection *connection,
233 DBusMessage *message,
239 static gboolean cm_context_removed(DBusConnection *connection,
240 DBusMessage *message,
246 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
252 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
258 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
264 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
267 const char *path = dbus_message_get_path(message);
268 struct modem_data *modem;
269 DBusMessageIter iter, value;
272 modem = g_hash_table_lookup(modem_hash, path);
276 if (dbus_message_iter_init(message, &iter) == FALSE)
279 dbus_message_iter_get_basic(&iter, &key);
281 dbus_message_iter_next(&iter);
282 dbus_message_iter_recurse(&iter, &value);
284 if (g_str_equal(key, "Powered") == TRUE) {
285 dbus_message_iter_get_basic(&value, &modem->powered);
287 DBG("%s Powered %d", modem->path, modem->powered);
289 if (modem->powered == FALSE)
290 modem_set_powered(modem);
291 } else if (g_str_equal(key, "Online") == TRUE) {
292 dbus_message_iter_get_basic(&value, &modem->online);
294 DBG("%s Online %d", modem->path, modem->online);
295 } else if (g_str_equal(key, "Interfaces") == TRUE) {
296 modem->interfaces = extract_interfaces(&value);
298 DBG("%s Interfaces 0x%02x", modem->path,
300 } else if (g_str_equal(key, "Serial") == TRUE) {
303 dbus_message_iter_get_basic(&value, &serial);
305 g_free(modem->serial);
306 modem->serial = g_strdup(serial);
308 DBG("%s Serial %s", modem->path, modem->serial);
314 static void add_modem(const char *path, DBusMessageIter *prop)
316 struct modem_data *modem;
320 modem = g_hash_table_lookup(modem_hash, path);
323 * When oFono powers up we ask for the modems and oFono is
324 * reporting with modem_added signal the modems. Only
330 modem = g_try_new0(struct modem_data, 1);
334 modem->path = g_strdup(path);
336 g_hash_table_insert(modem_hash, g_strdup(path), modem);
338 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
339 DBusMessageIter entry, value;
342 dbus_message_iter_recurse(prop, &entry);
343 dbus_message_iter_get_basic(&entry, &key);
345 dbus_message_iter_next(&entry);
346 dbus_message_iter_recurse(&entry, &value);
348 if (g_str_equal(key, "Powered") == TRUE) {
349 dbus_message_iter_get_basic(&value, &modem->powered);
351 DBG("%s Powered %d", modem->path, modem->powered);
352 } else if (g_str_equal(key, "Online") == TRUE) {
353 dbus_message_iter_get_basic(&value, &modem->online);
355 DBG("%s Online %d", modem->path, modem->online);
356 } else if (g_str_equal(key, "Interfaces") == TRUE) {
357 modem->interfaces = extract_interfaces(&value);
359 DBG("%s Interfaces 0x%02x", modem->path,
361 } else if (g_str_equal(key, "Serial") == TRUE) {
364 dbus_message_iter_get_basic(&value, &serial);
365 modem->serial = g_strdup(serial);
367 DBG("%s Serial %s", modem->path, modem->serial);
370 dbus_message_iter_next(prop);
373 if (modem->powered == FALSE)
374 modem_set_powered(modem);
377 static void remove_modem(gpointer data)
379 struct modem_data *modem = data;
381 DBG("%s", modem->path);
383 if (modem->call_set_property != NULL)
384 dbus_pending_call_cancel(modem->call_set_property);
386 g_free(modem->serial);
392 static gboolean modem_added(DBusConnection *connection,
393 DBusMessage *message, void *user_data)
395 DBusMessageIter iter, properties;
400 if (dbus_message_iter_init(message, &iter) == FALSE)
403 dbus_message_iter_get_basic(&iter, &path);
405 dbus_message_iter_next(&iter);
406 dbus_message_iter_recurse(&iter, &properties);
408 add_modem(path, &properties);
413 static gboolean modem_removed(DBusConnection *connection,
414 DBusMessage *message, void *user_data)
416 DBusMessageIter iter;
421 if (dbus_message_iter_init(message, &iter) == FALSE)
424 dbus_message_iter_get_basic(&iter, &path);
426 g_hash_table_remove(modem_hash, path);
431 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
435 DBusMessageIter array, dict;
439 reply = dbus_pending_call_steal_reply(call);
441 dbus_error_init(&error);
443 if (dbus_set_error_from_message(&error, reply) == TRUE) {
444 connman_error("%s", error.message);
445 dbus_error_free(&error);
449 if (dbus_message_iter_init(reply, &array) == FALSE)
452 dbus_message_iter_recurse(&array, &dict);
454 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
455 DBusMessageIter value, properties;
458 dbus_message_iter_recurse(&dict, &value);
459 dbus_message_iter_get_basic(&value, &path);
461 dbus_message_iter_next(&value);
462 dbus_message_iter_recurse(&value, &properties);
464 add_modem(path, &properties);
466 dbus_message_iter_next(&dict);
470 dbus_message_unref(reply);
472 dbus_pending_call_unref(call);
475 static int manager_get_modems(void)
477 DBusMessage *message;
478 DBusPendingCall *call;
482 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
483 OFONO_MANAGER_INTERFACE, GET_MODEMS);
487 if (dbus_connection_send_with_reply(connection, message,
488 &call, TIMEOUT) == FALSE) {
489 connman_error("Failed to call GetModems()");
490 dbus_message_unref(message);
495 connman_error("D-Bus connection not available");
496 dbus_message_unref(message);
500 dbus_pending_call_set_notify(call, manager_get_modems_reply,
503 dbus_message_unref(message);
508 static void ofono_connect(DBusConnection *conn, void *user_data)
512 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
513 g_free, remove_modem);
514 if (modem_hash == NULL)
517 manager_get_modems();
520 static void ofono_disconnect(DBusConnection *conn, void *user_data)
524 if (modem_hash == NULL)
527 g_hash_table_destroy(modem_hash);
531 static int network_probe(struct connman_network *network)
533 DBG("network %p", network);
538 static void network_remove(struct connman_network *network)
540 DBG("network %p", network);
543 static int network_connect(struct connman_network *network)
545 DBG("network %p", network);
550 static int network_disconnect(struct connman_network *network)
552 DBG("network %p", network);
557 static struct connman_network_driver network_driver = {
559 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
560 .probe = network_probe,
561 .remove = network_remove,
562 .connect = network_connect,
563 .disconnect = network_disconnect,
566 static int modem_probe(struct connman_device *device)
568 DBG("device %p", device);
573 static void modem_remove(struct connman_device *device)
575 DBG("device %p", device);
578 static int modem_enable(struct connman_device *device)
580 DBG("device %p", device);
585 static int modem_disable(struct connman_device *device)
587 DBG("device %p", device);
592 static struct connman_device_driver modem_driver = {
594 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
595 .probe = modem_probe,
596 .remove = modem_remove,
597 .enable = modem_enable,
598 .disable = modem_disable,
602 static guint modem_added_watch;
603 static guint modem_removed_watch;
604 static guint modem_watch;
605 static guint cm_watch;
606 static guint sim_watch;
607 static guint context_added_watch;
608 static guint context_removed_watch;
609 static guint netreg_watch;
610 static guint context_watch;
612 static int ofono_init(void)
618 connection = connman_dbus_get_connection();
619 if (connection == NULL)
622 watch = g_dbus_add_service_watch(connection,
623 OFONO_SERVICE, ofono_connect,
624 ofono_disconnect, NULL, NULL);
626 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
627 OFONO_MANAGER_INTERFACE,
632 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
633 OFONO_MANAGER_INTERFACE,
638 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
639 OFONO_MODEM_INTERFACE,
644 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
650 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
656 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
662 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
668 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
669 OFONO_CONTEXT_INTERFACE,
674 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
675 OFONO_NETREG_INTERFACE,
681 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
682 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
683 context_added_watch == 0 ||
684 context_removed_watch == 0 ||
685 context_watch == 0 || netreg_watch == 0) {
690 err = connman_network_driver_register(&network_driver);
694 err = connman_device_driver_register(&modem_driver);
696 connman_network_driver_unregister(&network_driver);
703 g_dbus_remove_watch(connection, netreg_watch);
704 g_dbus_remove_watch(connection, context_watch);
705 g_dbus_remove_watch(connection, context_removed_watch);
706 g_dbus_remove_watch(connection, context_added_watch);
707 g_dbus_remove_watch(connection, sim_watch);
708 g_dbus_remove_watch(connection, cm_watch);
709 g_dbus_remove_watch(connection, modem_watch);
710 g_dbus_remove_watch(connection, modem_removed_watch);
711 g_dbus_remove_watch(connection, modem_added_watch);
712 g_dbus_remove_watch(connection, watch);
713 dbus_connection_unref(connection);
718 static void ofono_exit(void)
722 if (modem_hash != NULL) {
723 g_hash_table_destroy(modem_hash);
727 connman_device_driver_unregister(&modem_driver);
728 connman_network_driver_unregister(&network_driver);
730 g_dbus_remove_watch(connection, netreg_watch);
731 g_dbus_remove_watch(connection, context_watch);
732 g_dbus_remove_watch(connection, context_removed_watch);
733 g_dbus_remove_watch(connection, context_added_watch);
734 g_dbus_remove_watch(connection, sim_watch);
735 g_dbus_remove_watch(connection, cm_watch);
736 g_dbus_remove_watch(connection, modem_watch);
737 g_dbus_remove_watch(connection, modem_added_watch);
738 g_dbus_remove_watch(connection, modem_removed_watch);
739 g_dbus_remove_watch(connection, watch);
741 dbus_connection_unref(connection);
744 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
745 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)