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_PROPERTIES "GetProperties"
57 #define SET_PROPERTY "SetProperty"
58 #define GET_MODEMS "GetModems"
64 OFONO_API_NETREG = 0x2,
68 static DBusConnection *connection;
70 static GHashTable *modem_hash;
77 connman_bool_t powered;
78 connman_bool_t online;
81 connman_bool_t set_powered;
83 /* SimManager Interface */
87 DBusPendingCall *call_set_property;
88 DBusPendingCall *call_get_properties;
91 typedef void (*set_property_cb)(struct modem_data *data,
92 connman_bool_t success);
93 typedef void (*get_properties_cb)(struct modem_data *data,
94 DBusMessageIter *dict);
96 struct property_info {
97 struct modem_data *modem;
99 const char *interface;
100 const char *property;
101 set_property_cb set_property_cb;
102 get_properties_cb get_properties_cb;
105 static void set_property_reply(DBusPendingCall *call, void *user_data)
107 struct property_info *info = user_data;
110 connman_bool_t success = TRUE;
112 DBG("%s path %s %s.%s", info->modem->path,
113 info->path, info->interface, info->property);
115 info->modem->call_set_property = NULL;
117 dbus_error_init(&error);
119 reply = dbus_pending_call_steal_reply(call);
121 if (dbus_set_error_from_message(&error, reply)) {
122 connman_error("Failed to change property: %s %s.%s: %s %s",
123 info->path, info->interface, info->property,
124 error.name, error.message);
125 dbus_error_free(&error);
129 if (info->set_property_cb != NULL)
130 (*info->set_property_cb)(info->modem, success);
132 dbus_message_unref(reply);
134 dbus_pending_call_unref(call);
137 static int set_property(struct modem_data *modem,
138 const char *path, const char *interface,
139 const char *property, int type, void *value,
140 set_property_cb notify)
142 DBusMessage *message;
143 DBusMessageIter iter;
144 struct property_info *info;
146 DBG("%s path %s %s.%s", modem->path, path, interface, property);
148 if (modem->call_set_property != NULL) {
149 connman_error("Pending SetProperty");
153 message = dbus_message_new_method_call(OFONO_SERVICE, path,
154 interface, SET_PROPERTY);
158 dbus_message_iter_init_append(message, &iter);
159 connman_dbus_property_append_basic(&iter, property, type, value);
161 if (dbus_connection_send_with_reply(connection, message,
162 &modem->call_set_property, TIMEOUT) == FALSE) {
163 connman_error("Failed to change property: %s %s.%s",
164 path, interface, property);
165 dbus_message_unref(message);
169 if (modem->call_set_property == NULL) {
170 connman_error("D-Bus connection not available");
171 dbus_message_unref(message);
175 info = g_try_new0(struct property_info, 1);
177 dbus_message_unref(message);
183 info->interface = interface;
184 info->property = property;
185 info->set_property_cb = notify;
187 dbus_pending_call_set_notify(modem->call_set_property,
188 set_property_reply, info, g_free);
190 dbus_message_unref(message);
195 static void get_properties_reply(DBusPendingCall *call, void *user_data)
197 struct property_info *info = user_data;
198 DBusMessageIter array, dict;
202 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
204 info->modem->call_get_properties = NULL;
206 dbus_error_init(&error);
208 reply = dbus_pending_call_steal_reply(call);
210 if (dbus_set_error_from_message(&error, reply)) {
211 connman_error("Failed to get properties: %s %s: %s %s",
212 info->path, info->interface,
213 error.name, error.message);
214 dbus_error_free(&error);
219 if (dbus_message_iter_init(reply, &array) == FALSE)
222 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
225 dbus_message_iter_recurse(&array, &dict);
227 if (info->get_properties_cb != NULL)
228 (*info->get_properties_cb)(info->modem, &dict);
232 dbus_message_unref(reply);
234 dbus_pending_call_unref(call);
237 static int get_properties(const char *path, const char *interface,
238 get_properties_cb notify,
239 struct modem_data *modem)
241 DBusMessage *message;
242 struct property_info *info;
244 DBG("%s path %s %s", modem->path, path, interface);
246 if (modem->call_get_properties != NULL) {
247 connman_error("Pending GetProperties");
251 message = dbus_message_new_method_call(OFONO_SERVICE, path,
252 interface, GET_PROPERTIES);
256 if (dbus_connection_send_with_reply(connection, message,
257 &modem->call_get_properties, TIMEOUT) == FALSE) {
258 connman_error("Failed to call %s.GetProperties()", interface);
259 dbus_message_unref(message);
263 if (modem->call_get_properties == NULL) {
264 connman_error("D-Bus connection not available");
265 dbus_message_unref(message);
269 info = g_try_new0(struct property_info, 1);
271 dbus_message_unref(message);
277 info->interface = interface;
278 info->get_properties_cb = notify;
280 dbus_pending_call_set_notify(modem->call_get_properties,
281 get_properties_reply, info, g_free);
283 dbus_message_unref(message);
288 static int modem_set_powered(struct modem_data *modem)
290 DBG("%s", modem->path);
292 modem->set_powered = TRUE;
294 return set_property(modem, modem->path,
295 OFONO_MODEM_INTERFACE,
296 "Powered", DBUS_TYPE_BOOLEAN,
301 static connman_bool_t has_interface(uint8_t interfaces,
304 if ((interfaces & api) == api)
310 static uint8_t extract_interfaces(DBusMessageIter *array)
312 DBusMessageIter entry;
313 uint8_t interfaces = 0;
315 dbus_message_iter_recurse(array, &entry);
317 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
320 dbus_message_iter_get_basic(&entry, &name);
322 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
323 interfaces |= OFONO_API_SIM;
324 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
325 interfaces |= OFONO_API_NETREG;
326 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
327 interfaces |= OFONO_API_CM;
329 dbus_message_iter_next(&entry);
335 static gboolean context_changed(DBusConnection *connection,
336 DBusMessage *message,
342 static gboolean cm_context_added(DBusConnection *connection,
343 DBusMessage *message,
349 static gboolean cm_context_removed(DBusConnection *connection,
350 DBusMessage *message,
356 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
362 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
368 static void update_sim_imsi(struct modem_data *modem,
371 DBG("%s imsi %s", modem->path, imsi);
373 if (g_strcmp0(modem->imsi, imsi) == 0)
377 modem->imsi = g_strdup(imsi);
380 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
383 const char *path = dbus_message_get_path(message);
384 struct modem_data *modem;
385 DBusMessageIter iter, value;
388 modem = g_hash_table_lookup(modem_hash, path);
392 if (dbus_message_iter_init(message, &iter) == FALSE)
395 dbus_message_iter_get_basic(&iter, &key);
397 dbus_message_iter_next(&iter);
398 dbus_message_iter_recurse(&iter, &value);
400 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
403 dbus_message_iter_get_basic(&value, &imsi);
405 update_sim_imsi(modem, imsi);
411 static void sim_properties_reply(struct modem_data *modem,
412 DBusMessageIter *dict)
414 DBG("%s", modem->path);
416 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
417 DBusMessageIter entry, value;
420 dbus_message_iter_recurse(dict, &entry);
421 dbus_message_iter_get_basic(&entry, &key);
423 dbus_message_iter_next(&entry);
424 dbus_message_iter_recurse(&entry, &value);
426 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
429 dbus_message_iter_get_basic(&value, &imsi);
431 update_sim_imsi(modem, imsi);
436 dbus_message_iter_next(dict);
440 static int sim_get_properties(struct modem_data *modem)
442 return get_properties(modem->path, OFONO_SIM_INTERFACE,
443 sim_properties_reply, modem);
446 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
449 const char *path = dbus_message_get_path(message);
450 struct modem_data *modem;
451 DBusMessageIter iter, value;
454 modem = g_hash_table_lookup(modem_hash, path);
458 if (dbus_message_iter_init(message, &iter) == FALSE)
461 dbus_message_iter_get_basic(&iter, &key);
463 dbus_message_iter_next(&iter);
464 dbus_message_iter_recurse(&iter, &value);
466 if (g_str_equal(key, "Powered") == TRUE) {
467 dbus_message_iter_get_basic(&value, &modem->powered);
469 DBG("%s Powered %d", modem->path, modem->powered);
471 if (modem->powered == FALSE)
472 modem_set_powered(modem);
473 } else if (g_str_equal(key, "Online") == TRUE) {
474 dbus_message_iter_get_basic(&value, &modem->online);
476 DBG("%s Online %d", modem->path, modem->online);
477 } else if (g_str_equal(key, "Interfaces") == TRUE) {
478 modem->interfaces = extract_interfaces(&value);
480 DBG("%s Interfaces 0x%02x", modem->path,
483 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
484 if (modem->imsi == NULL &&
485 modem->set_powered == FALSE) {
487 * Only use do GetProperties() when
488 * device has not been powered up.
490 sim_get_properties(modem);
494 } else if (g_str_equal(key, "Serial") == TRUE) {
497 dbus_message_iter_get_basic(&value, &serial);
499 g_free(modem->serial);
500 modem->serial = g_strdup(serial);
502 DBG("%s Serial %s", modem->path, modem->serial);
508 static void add_modem(const char *path, DBusMessageIter *prop)
510 struct modem_data *modem;
514 modem = g_hash_table_lookup(modem_hash, path);
517 * When oFono powers up we ask for the modems and oFono is
518 * reporting with modem_added signal the modems. Only
524 modem = g_try_new0(struct modem_data, 1);
528 modem->path = g_strdup(path);
530 g_hash_table_insert(modem_hash, g_strdup(path), modem);
532 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
533 DBusMessageIter entry, value;
536 dbus_message_iter_recurse(prop, &entry);
537 dbus_message_iter_get_basic(&entry, &key);
539 dbus_message_iter_next(&entry);
540 dbus_message_iter_recurse(&entry, &value);
542 if (g_str_equal(key, "Powered") == TRUE) {
543 dbus_message_iter_get_basic(&value, &modem->powered);
545 DBG("%s Powered %d", modem->path, modem->powered);
546 } else if (g_str_equal(key, "Online") == TRUE) {
547 dbus_message_iter_get_basic(&value, &modem->online);
549 DBG("%s Online %d", modem->path, modem->online);
550 } else if (g_str_equal(key, "Interfaces") == TRUE) {
551 modem->interfaces = extract_interfaces(&value);
553 DBG("%s Interfaces 0x%02x", modem->path,
555 } else if (g_str_equal(key, "Serial") == TRUE) {
558 dbus_message_iter_get_basic(&value, &serial);
559 modem->serial = g_strdup(serial);
561 DBG("%s Serial %s", modem->path, modem->serial);
564 dbus_message_iter_next(prop);
567 if (modem->powered == FALSE)
568 modem_set_powered(modem);
569 else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE)
570 sim_get_properties(modem);
573 static void remove_modem(gpointer data)
575 struct modem_data *modem = data;
577 DBG("%s", modem->path);
579 if (modem->call_set_property != NULL)
580 dbus_pending_call_cancel(modem->call_set_property);
582 if (modem->call_get_properties != NULL)
583 dbus_pending_call_cancel(modem->call_get_properties);
585 g_free(modem->serial);
592 static gboolean modem_added(DBusConnection *connection,
593 DBusMessage *message, void *user_data)
595 DBusMessageIter iter, properties;
600 if (dbus_message_iter_init(message, &iter) == FALSE)
603 dbus_message_iter_get_basic(&iter, &path);
605 dbus_message_iter_next(&iter);
606 dbus_message_iter_recurse(&iter, &properties);
608 add_modem(path, &properties);
613 static gboolean modem_removed(DBusConnection *connection,
614 DBusMessage *message, void *user_data)
616 DBusMessageIter iter;
621 if (dbus_message_iter_init(message, &iter) == FALSE)
624 dbus_message_iter_get_basic(&iter, &path);
626 g_hash_table_remove(modem_hash, path);
631 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
635 DBusMessageIter array, dict;
639 reply = dbus_pending_call_steal_reply(call);
641 dbus_error_init(&error);
643 if (dbus_set_error_from_message(&error, reply) == TRUE) {
644 connman_error("%s", error.message);
645 dbus_error_free(&error);
649 if (dbus_message_iter_init(reply, &array) == FALSE)
652 dbus_message_iter_recurse(&array, &dict);
654 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
655 DBusMessageIter value, properties;
658 dbus_message_iter_recurse(&dict, &value);
659 dbus_message_iter_get_basic(&value, &path);
661 dbus_message_iter_next(&value);
662 dbus_message_iter_recurse(&value, &properties);
664 add_modem(path, &properties);
666 dbus_message_iter_next(&dict);
670 dbus_message_unref(reply);
672 dbus_pending_call_unref(call);
675 static int manager_get_modems(void)
677 DBusMessage *message;
678 DBusPendingCall *call;
682 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
683 OFONO_MANAGER_INTERFACE, GET_MODEMS);
687 if (dbus_connection_send_with_reply(connection, message,
688 &call, TIMEOUT) == FALSE) {
689 connman_error("Failed to call GetModems()");
690 dbus_message_unref(message);
695 connman_error("D-Bus connection not available");
696 dbus_message_unref(message);
700 dbus_pending_call_set_notify(call, manager_get_modems_reply,
703 dbus_message_unref(message);
708 static void ofono_connect(DBusConnection *conn, void *user_data)
712 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
713 g_free, remove_modem);
714 if (modem_hash == NULL)
717 manager_get_modems();
720 static void ofono_disconnect(DBusConnection *conn, void *user_data)
724 if (modem_hash == NULL)
727 g_hash_table_destroy(modem_hash);
731 static int network_probe(struct connman_network *network)
733 DBG("network %p", network);
738 static void network_remove(struct connman_network *network)
740 DBG("network %p", network);
743 static int network_connect(struct connman_network *network)
745 DBG("network %p", network);
750 static int network_disconnect(struct connman_network *network)
752 DBG("network %p", network);
757 static struct connman_network_driver network_driver = {
759 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
760 .probe = network_probe,
761 .remove = network_remove,
762 .connect = network_connect,
763 .disconnect = network_disconnect,
766 static int modem_probe(struct connman_device *device)
768 DBG("device %p", device);
773 static void modem_remove(struct connman_device *device)
775 DBG("device %p", device);
778 static int modem_enable(struct connman_device *device)
780 DBG("device %p", device);
785 static int modem_disable(struct connman_device *device)
787 DBG("device %p", device);
792 static struct connman_device_driver modem_driver = {
794 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
795 .probe = modem_probe,
796 .remove = modem_remove,
797 .enable = modem_enable,
798 .disable = modem_disable,
802 static guint modem_added_watch;
803 static guint modem_removed_watch;
804 static guint modem_watch;
805 static guint cm_watch;
806 static guint sim_watch;
807 static guint context_added_watch;
808 static guint context_removed_watch;
809 static guint netreg_watch;
810 static guint context_watch;
812 static int ofono_init(void)
818 connection = connman_dbus_get_connection();
819 if (connection == NULL)
822 watch = g_dbus_add_service_watch(connection,
823 OFONO_SERVICE, ofono_connect,
824 ofono_disconnect, NULL, NULL);
826 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
827 OFONO_MANAGER_INTERFACE,
832 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
833 OFONO_MANAGER_INTERFACE,
838 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
839 OFONO_MODEM_INTERFACE,
844 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
850 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
856 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
862 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
868 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
869 OFONO_CONTEXT_INTERFACE,
874 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
875 OFONO_NETREG_INTERFACE,
881 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
882 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
883 context_added_watch == 0 ||
884 context_removed_watch == 0 ||
885 context_watch == 0 || netreg_watch == 0) {
890 err = connman_network_driver_register(&network_driver);
894 err = connman_device_driver_register(&modem_driver);
896 connman_network_driver_unregister(&network_driver);
903 g_dbus_remove_watch(connection, netreg_watch);
904 g_dbus_remove_watch(connection, context_watch);
905 g_dbus_remove_watch(connection, context_removed_watch);
906 g_dbus_remove_watch(connection, context_added_watch);
907 g_dbus_remove_watch(connection, sim_watch);
908 g_dbus_remove_watch(connection, cm_watch);
909 g_dbus_remove_watch(connection, modem_watch);
910 g_dbus_remove_watch(connection, modem_removed_watch);
911 g_dbus_remove_watch(connection, modem_added_watch);
912 g_dbus_remove_watch(connection, watch);
913 dbus_connection_unref(connection);
918 static void ofono_exit(void)
922 if (modem_hash != NULL) {
923 g_hash_table_destroy(modem_hash);
927 connman_device_driver_unregister(&modem_driver);
928 connman_network_driver_unregister(&network_driver);
930 g_dbus_remove_watch(connection, netreg_watch);
931 g_dbus_remove_watch(connection, context_watch);
932 g_dbus_remove_watch(connection, context_removed_watch);
933 g_dbus_remove_watch(connection, context_added_watch);
934 g_dbus_remove_watch(connection, sim_watch);
935 g_dbus_remove_watch(connection, cm_watch);
936 g_dbus_remove_watch(connection, modem_watch);
937 g_dbus_remove_watch(connection, modem_added_watch);
938 g_dbus_remove_watch(connection, modem_removed_watch);
939 g_dbus_remove_watch(connection, watch);
941 dbus_connection_unref(connection);
944 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
945 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)