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
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/element.h>
36 #include <connman/device.h>
37 #include <connman/network.h>
38 #include <connman/dbus.h>
39 #include <connman/inet.h>
40 #include <connman/technology.h>
41 #include <connman/log.h>
45 #define OFONO_SERVICE "org.ofono"
47 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
49 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
50 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
51 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
52 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
54 #define PROPERTY_CHANGED "PropertyChanged"
55 #define GET_PROPERTIES "GetProperties"
56 #define SET_PROPERTY "SetProperty"
57 #define CONTEXT_ADDED "ContextAdded"
58 #define CONTEXT_REMOVED "ContextRemoved"
59 #define ADD_CONTEXT "AddContext"
60 #define GET_MODEMS "GetModems"
61 #define MODEM_ADDED "ModemAdded"
62 #define MODEM_REMOVED "ModemRemoved"
67 static DBusConnection *connection;
69 static GHashTable *modem_hash = NULL;
71 static GHashTable *network_hash;
75 struct connman_device *device;
80 gboolean pending_online;
81 dbus_bool_t requested_online;
84 /* org.ofono.ConnectionManager properties */
87 dbus_bool_t roaming_allowed;
89 connman_bool_t registered;
90 connman_bool_t roaming;
91 uint8_t strength, has_strength;
94 static int modem_probe(struct connman_device *device)
96 DBG("device %p", device);
101 static void modem_remove(struct connman_device *device)
103 DBG("device %p", device);
106 static int call_ofono(const char *path,
107 const char *interface, const char *method,
108 DBusPendingCallNotifyFunction notify, void *user_data,
109 DBusFreeFunction free_function,
112 DBusMessage *message;
113 DBusPendingCall *call;
117 DBG("path %s %s.%s", path, interface, method);
122 message = dbus_message_new_method_call(OFONO_SERVICE, path,
127 dbus_message_set_auto_start(message, FALSE);
130 ok = dbus_message_append_args_valist(message, type, va);
136 if (dbus_connection_send_with_reply(connection, message,
137 &call, TIMEOUT) == FALSE) {
138 connman_error("Failed to call %s.%s", interface, method);
139 dbus_message_unref(message);
144 connman_error("D-Bus connection not available");
145 dbus_message_unref(message);
149 dbus_pending_call_set_notify(call, notify, user_data, free_function);
151 dbus_message_unref(message);
156 static void set_property_reply(DBusPendingCall *call, void *user_data)
160 char const *name = user_data;
164 dbus_error_init(&error);
166 reply = dbus_pending_call_steal_reply(call);
168 if (dbus_set_error_from_message(&error, reply)) {
169 connman_error("SetProperty(%s) %s %s", name,
170 error.name, error.message);
171 dbus_error_free(&error);
174 dbus_message_unref(reply);
176 dbus_pending_call_unref(call);
179 static int set_property(const char *path, const char *interface,
180 const char *property, int type, void *value,
181 DBusPendingCallNotifyFunction notify, void *user_data,
182 DBusFreeFunction free_function)
184 DBusMessage *message;
185 DBusMessageIter iter;
186 DBusPendingCall *call;
188 DBG("path %s %s.%s", path, interface, property);
190 g_assert(notify == NULL ? free_function == NULL : 1);
195 message = dbus_message_new_method_call(OFONO_SERVICE, path,
196 interface, SET_PROPERTY);
200 dbus_message_set_auto_start(message, FALSE);
202 dbus_message_iter_init_append(message, &iter);
203 connman_dbus_property_append_basic(&iter, property, type, value);
205 if (dbus_connection_send_with_reply(connection, message,
206 &call, TIMEOUT) == FALSE) {
207 connman_error("Failed to change \"%s\" property on %s",
208 property, interface);
209 dbus_message_unref(message);
214 connman_error("D-Bus connection not available");
215 dbus_message_unref(message);
219 if (notify == NULL) {
220 notify = set_property_reply;
221 user_data = (void *)property;
222 free_function = NULL;
225 dbus_pending_call_set_notify(call, notify, user_data, free_function);
227 dbus_message_unref(message);
232 static void update_modem_online(struct modem_data *modem,
233 connman_bool_t online)
235 DBG("modem %p path %s online %d", modem, modem->path, online);
237 modem->online = online;
238 modem->requested_online = online;
239 modem->pending_online = FALSE;
242 connman_device_set_powered(modem->device, online);
245 static void set_online_reply(DBusPendingCall *call, void *user_data)
247 struct modem_data *modem;
252 DBG("path %s", (char *)user_data);
254 if (modem_hash == NULL)
257 modem = g_hash_table_lookup(modem_hash, user_data);
261 reply = dbus_pending_call_steal_reply(call);
263 dbus_error_init(&error);
265 if (dbus_set_error_from_message(&error, reply)) {
266 connman_error("SetProperty(Online) %s %s",
267 error.name, error.message);
268 dbus_error_free(&error);
270 result = modem->online;
272 result = modem->requested_online;
274 if (modem->pending_online)
275 update_modem_online(modem, result);
277 dbus_message_unref(reply);
279 dbus_pending_call_unref(call);
282 static int modem_change_online(char const *path, dbus_bool_t online)
284 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
289 if (modem->online == online)
292 modem->requested_online = online;
294 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
295 DBUS_TYPE_BOOLEAN, &online,
297 (void *)g_strdup(path), g_free);
300 static int modem_enable(struct connman_device *device)
302 const char *path = connman_device_get_string(device, "Path");
304 DBG("device %p, path, %s", device, path);
306 return modem_change_online(path, TRUE);
309 static int modem_disable(struct connman_device *device)
311 const char *path = connman_device_get_string(device, "Path");
313 DBG("device %p path %s", device, path);
315 return modem_change_online(path, FALSE);
318 static struct connman_device_driver modem_driver = {
320 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
321 .probe = modem_probe,
322 .remove = modem_remove,
323 .enable = modem_enable,
324 .disable = modem_disable,
327 static void modem_remove_device(struct modem_data *modem)
329 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
331 if (modem->device == NULL)
334 connman_device_remove_all_networks(modem->device);
335 connman_device_unregister(modem->device);
336 connman_device_unref(modem->device);
338 modem->device = NULL;
341 static void remove_modem(gpointer data)
343 struct modem_data *modem = data;
345 modem_remove_device(modem);
352 static void remove_network(gpointer data)
354 connman_network_unref(data);
357 static char *get_ident(const char *path)
364 pos = strrchr(path, '/');
371 static void create_service(struct connman_network *network)
378 path = connman_network_get_string(network, "Path");
380 group = get_ident(path);
382 connman_network_set_group(network, group);
385 static int network_probe(struct connman_network *network)
390 static gboolean pending_network_is_available(struct connman_network *network)
392 /* Modem or network may be removed */
393 if (network == NULL || connman_network_get_device(network) == NULL) {
394 DBG("Modem or network was removed");
401 static void set_active_reply(DBusPendingCall *call, void *user_data)
403 char const *path = user_data;
406 struct connman_network *network;
408 network = g_hash_table_lookup(network_hash, path);
410 DBG("path %s network %p", path, network);
412 reply = dbus_pending_call_steal_reply(call);
414 if (!pending_network_is_available(network))
417 dbus_error_init(&error);
419 if (dbus_set_error_from_message(&error, reply)) {
420 connman_error("SetProperty(Active) %s %s",
421 error.name, error.message);
423 if (connman_network_get_index(network) < 0)
424 connman_network_set_error(network,
425 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
427 dbus_error_free(&error);
431 dbus_message_unref(reply);
433 dbus_pending_call_unref(call);
436 static int set_network_active(struct connman_network *network,
441 const char *path = connman_network_get_string(network, "Path");
443 DBG("network %p, path %s, active %d", network, path, active);
445 error = set_property(path, OFONO_CONTEXT_INTERFACE,
446 "Active", DBUS_TYPE_BOOLEAN, &active,
447 set_active_reply, g_strdup(path), g_free);
449 if (active == FALSE && error == -EINPROGRESS)
455 static int network_connect(struct connman_network *network)
457 struct connman_device *device;
458 struct modem_data *modem;
460 DBG("network %p", network);
462 if (connman_network_get_index(network) >= 0)
465 device = connman_network_get_device(network);
469 modem = connman_device_get_data(device);
473 if (modem->registered == FALSE)
476 if (modem->powered == FALSE)
479 if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
482 return set_network_active(network, TRUE);
485 static int network_disconnect(struct connman_network *network)
487 DBG("network %p", network);
489 if (connman_network_get_index(network) < 0)
492 connman_network_set_associating(network, FALSE);
494 return set_network_active(network, FALSE);
497 static void network_remove(struct connman_network *network)
499 char const *path = connman_network_get_string(network, "Path");
501 DBG("network %p path %s", network, path);
503 g_hash_table_remove(network_hash, path);
506 static struct connman_network_driver network_driver = {
508 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
509 .probe = network_probe,
510 .remove = network_remove,
511 .connect = network_connect,
512 .disconnect = network_disconnect,
515 static void update_settings(DBusMessageIter *array,
516 struct connman_network *network);
518 static void set_connected(struct connman_network *network,
519 connman_bool_t connected);
521 static int add_network(struct connman_device *device,
522 const char *path, DBusMessageIter *dict)
524 struct modem_data *modem = connman_device_get_data(device);
525 struct connman_network *network;
527 const char *hash_path;
528 char const *operator;
529 dbus_bool_t active = FALSE;
531 DBG("modem %p device %p path %s", modem, device, path);
533 ident = get_ident(path);
535 network = connman_device_get_network(device, ident);
539 network = g_hash_table_lookup(network_hash, path);
540 if (network != NULL) {
541 DBG("path %p already exists with device %p", path,
542 connman_network_get_device(network));
543 if (connman_network_get_device(network))
545 g_hash_table_remove(network_hash, path);
548 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
552 connman_network_set_string(network, "Path", path);
553 hash_path = connman_network_get_string(network, "Path");
554 if (hash_path == NULL)
557 create_service(network);
559 connman_network_ref(network);
560 g_hash_table_insert(network_hash, (char *)hash_path, network);
562 connman_network_set_available(network, TRUE);
563 connman_network_set_index(network, -1);
565 operator = connman_device_get_string(device, "Operator");
567 connman_network_set_name(network, operator);
569 if (modem->has_strength)
570 connman_network_set_strength(network, modem->strength);
572 connman_network_set_roaming(network, modem->roaming);
574 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
575 DBusMessageIter entry, value;
578 dbus_message_iter_recurse(dict, &entry);
579 dbus_message_iter_get_basic(&entry, &key);
581 dbus_message_iter_next(&entry);
582 dbus_message_iter_recurse(&entry, &value);
584 if (g_str_equal(key, "Type")) {
587 dbus_message_iter_get_basic(&value, &type);
588 if (g_strcmp0(type, "internet") != 0) {
589 DBG("path %p type %s", path, type);
592 } else if (g_str_equal(key, "Settings"))
593 update_settings(&value, network);
594 else if (g_str_equal(key, "Active") == TRUE)
595 dbus_message_iter_get_basic(&value, &active);
597 dbus_message_iter_next(dict);
600 if (connman_device_add_network(device, network) != 0)
604 set_connected(network, active);
609 connman_network_unref(network);
610 g_hash_table_remove(network_hash, hash_path);
614 static void check_networks_reply(DBusPendingCall *call, void *user_data)
616 char *path = user_data;
617 struct modem_data *modem;
619 DBusMessageIter array, entry, value, properties;
621 DBG("path %s", path);
623 modem = g_hash_table_lookup(modem_hash, path);
626 if (modem->device == NULL)
629 reply = dbus_pending_call_steal_reply(call);
631 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
634 dbus_message_iter_init(reply, &array);
636 dbus_message_iter_recurse(&array, &entry);
638 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
639 char const *network_path;
641 dbus_message_iter_recurse(&entry, &value);
642 dbus_message_iter_get_basic(&value, &network_path);
644 dbus_message_iter_next(&value);
645 dbus_message_iter_recurse(&value, &properties);
647 add_network(modem->device, network_path, &properties);
649 dbus_message_iter_next(&entry);
653 dbus_message_unref(reply);
655 dbus_pending_call_unref(call);
658 static void check_networks(struct modem_data *modem)
660 char const *path = modem->path;
662 DBG("modem %p path %s", modem, path);
664 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
665 check_networks_reply, g_strdup(path), g_free,
669 static void modem_clear_network_errors(struct modem_data *modem)
671 struct connman_device *device = modem->device;
678 g_hash_table_iter_init(&i, network_hash);
680 while (g_hash_table_iter_next(&i, NULL, &value)) {
681 struct connman_network *network = value;
683 if (connman_network_get_device(network) == device)
684 connman_network_clear_error(network);
688 static void modem_operator_name_changed(struct modem_data *modem,
691 struct connman_device *device = modem->device;
698 connman_device_set_string(device, "Operator", name);
700 for (g_hash_table_iter_init(&i, network_hash);
701 g_hash_table_iter_next(&i, NULL, &value);) {
702 struct connman_network *network = value;
704 if (connman_network_get_device(network) == device) {
705 connman_network_set_name(network, name);
706 connman_network_update(network);
711 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
713 struct connman_device *device = modem->device;
717 modem->strength = strength;
718 modem->has_strength = TRUE;
723 for (g_hash_table_iter_init(&i, network_hash);
724 g_hash_table_iter_next(&i, NULL, &value);) {
725 struct connman_network *network = value;
727 if (connman_network_get_device(network) == device) {
728 connman_network_set_strength(network, strength);
729 connman_network_update(network);
734 static void modem_roaming_changed(struct modem_data *modem,
737 struct connman_device *device = modem->device;
738 connman_bool_t roaming = FALSE;
739 connman_bool_t registered = FALSE;
740 connman_bool_t was_roaming = modem->roaming;
744 if (g_str_equal(status, "roaming"))
746 else if (g_str_equal(status, "registered"))
749 registered = registered || roaming;
751 if (modem->roaming == roaming && modem->registered == registered)
754 modem->registered = registered;
755 modem->roaming = roaming;
757 if (roaming == was_roaming)
763 g_hash_table_iter_init(&i, network_hash);
765 while (g_hash_table_iter_next(&i, NULL, &value)) {
766 struct connman_network *network = value;
768 if (connman_network_get_device(network) == device) {
769 connman_network_set_roaming(network, roaming);
770 connman_network_update(network);
775 static void modem_registration_removed(struct modem_data *modem)
777 modem->registered = FALSE;
778 modem->roaming = FALSE;
781 static void modem_registration_changed(struct modem_data *modem,
782 DBusMessageIter *entry)
784 DBusMessageIter iter;
787 connman_uint8_t strength;
788 char const *name, *status, *mcc_s;
790 dbus_message_iter_get_basic(entry, &key);
794 dbus_message_iter_next(entry);
796 dbus_message_iter_recurse(entry, &iter);
798 type = dbus_message_iter_get_arg_type(&iter);
799 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
802 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
803 dbus_message_iter_get_basic(&iter, &name);
804 modem_operator_name_changed(modem, name);
805 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
806 dbus_message_iter_get_basic(&iter, &strength);
807 modem_strength_changed(modem, strength);
808 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
809 dbus_message_iter_get_basic(&iter, &status);
810 modem_roaming_changed(modem, status);
811 } else if (g_str_equal(key, "MobileCountryCode") &&
812 type == DBUS_TYPE_STRING) {
816 dbus_message_iter_get_basic(&iter, &mcc_s);
822 alpha2 = mcc_country_codes[mcc - 200];
823 connman_technology_set_regdom(alpha2);
828 static gboolean reg_changed(DBusConnection *connection,
829 DBusMessage *message, void *user_data)
831 const char *path = dbus_message_get_path(message);
832 struct modem_data *modem;
833 DBusMessageIter iter;
835 DBG("path %s", path);
837 modem = g_hash_table_lookup(modem_hash, path);
841 if (dbus_message_iter_init(message, &iter))
842 modem_registration_changed(modem, &iter);
847 static void check_registration_reply(DBusPendingCall *call, void *user_data)
849 char const *path = user_data;
850 struct modem_data *modem;
852 DBusMessageIter array, dict, entry;
854 DBG("path %s", path);
856 modem = g_hash_table_lookup(modem_hash, path);
860 reply = dbus_pending_call_steal_reply(call);
862 if (dbus_message_iter_init(reply, &array) == FALSE)
865 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
868 dbus_message_iter_recurse(&array, &dict);
869 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
870 dbus_message_iter_recurse(&dict, &entry);
871 modem_registration_changed(modem, &entry);
872 dbus_message_iter_next(&dict);
876 dbus_message_unref(reply);
878 dbus_pending_call_unref(call);
881 static void check_registration(struct modem_data *modem)
883 char const *path = modem->path;
885 DBG("modem %p path %s", modem, path);
887 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
888 check_registration_reply, g_strdup(path), g_free,
892 static void modem_gprs_changed(struct modem_data *modem,
893 DBusMessageIter *entry)
895 DBusMessageIter iter;
900 dbus_message_iter_get_basic(entry, &key);
904 dbus_message_iter_next(entry);
906 dbus_message_iter_recurse(entry, &iter);
908 type = dbus_message_iter_get_arg_type(&iter);
910 if (type != DBUS_TYPE_BOOLEAN)
913 dbus_message_iter_get_basic(&iter, &value);
915 if (g_str_equal(key, "Attached") == TRUE) {
916 DBG("Attached %d", value);
918 modem->attached = value;
921 modem_clear_network_errors(modem);
922 } else if (g_str_equal(key, "Powered") == TRUE) {
923 DBG("Powered %d", value);
925 modem->powered = value;
926 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
927 DBG("RoamingAllowed %d", value);
929 modem->roaming_allowed = value;
933 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
935 char const *path = user_data;
936 struct modem_data *modem;
938 DBusMessageIter array, dict, entry;
940 DBG("path %s", path);
942 modem = g_hash_table_lookup(modem_hash, path);
946 reply = dbus_pending_call_steal_reply(call);
948 if (dbus_message_iter_init(reply, &array) == FALSE)
951 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
954 dbus_message_iter_recurse(&array, &dict);
955 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
956 dbus_message_iter_recurse(&dict, &entry);
957 modem_gprs_changed(modem, &entry);
958 dbus_message_iter_next(&dict);
962 dbus_message_unref(reply);
964 dbus_pending_call_unref(call);
966 check_networks(modem);
969 static void check_gprs(struct modem_data *modem)
971 char const *path = modem->path;
973 DBG("modem %p path %s", modem, path);
975 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
976 check_gprs_reply, g_strdup(path), g_free,
980 static void add_device(const char *path, const char *imsi)
982 struct modem_data *modem;
983 struct connman_device *device;
985 DBG("path %s imsi %s", path, imsi);
993 modem = g_hash_table_lookup(modem_hash, path);
998 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1001 modem_remove_device(modem);
1004 if (strlen(imsi) == 0)
1007 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1011 connman_device_set_ident(device, imsi);
1013 connman_device_set_string(device, "Path", path);
1015 connman_device_set_data(device, modem);
1017 if (connman_device_register(device) < 0) {
1018 connman_device_unref(device);
1022 modem->device = device;
1025 check_registration(modem);
1027 if (modem->has_gprs)
1031 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1033 const char *path = user_data;
1034 const char *imsi = NULL;
1036 DBusMessageIter array, dict;
1038 DBG("path %s", path);
1040 reply = dbus_pending_call_steal_reply(call);
1042 if (dbus_message_iter_init(reply, &array) == FALSE)
1045 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1048 dbus_message_iter_recurse(&array, &dict);
1050 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1051 DBusMessageIter entry, value;
1054 dbus_message_iter_recurse(&dict, &entry);
1055 dbus_message_iter_get_basic(&entry, &key);
1057 dbus_message_iter_next(&entry);
1058 dbus_message_iter_recurse(&entry, &value);
1060 if (g_str_equal(key, "SubscriberIdentity")) {
1061 dbus_message_iter_get_basic(&value, &imsi);
1062 add_device(path, imsi);
1065 dbus_message_iter_next(&dict);
1069 dbus_message_unref(reply);
1071 dbus_pending_call_unref(call);
1074 static void get_imsi(const char *path)
1076 DBG("path %s", path);
1078 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1079 sim_properties_reply, g_strdup(path), g_free,
1083 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1085 DBG("path %s powered %d", path, powered);
1087 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1088 DBUS_TYPE_BOOLEAN, &powered,
1092 static int modem_change_powered(const char *path, dbus_bool_t powered)
1094 DBG("path %s powered %d", path, powered);
1096 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1097 DBUS_TYPE_BOOLEAN, &powered,
1102 static gboolean modem_has_interface(DBusMessageIter *array,
1103 char const *interface)
1105 DBusMessageIter entry;
1107 dbus_message_iter_recurse(array, &entry);
1109 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1110 const char *element;
1112 dbus_message_iter_get_basic(&entry, &element);
1114 if (g_strcmp0(interface, element) == 0)
1117 dbus_message_iter_next(&entry);
1123 static gboolean modem_has_sim(DBusMessageIter *array)
1125 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1128 static gboolean modem_has_reg(DBusMessageIter *array)
1130 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1133 static gboolean modem_has_gprs(DBusMessageIter *array)
1135 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1138 static void add_modem(const char *path, DBusMessageIter *prop)
1140 struct modem_data *modem;
1141 dbus_bool_t powered = FALSE;
1142 dbus_bool_t online = FALSE;
1143 dbus_bool_t has_online = FALSE;
1144 dbus_bool_t locked = FALSE;
1145 gboolean has_sim = FALSE;
1146 gboolean has_reg = FALSE;
1147 gboolean has_gprs = FALSE;
1149 modem = g_hash_table_lookup(modem_hash, path);
1154 modem = g_try_new0(struct modem_data, 1);
1158 modem->path = g_strdup(path);
1159 modem->device = NULL;
1160 modem->available = TRUE;
1162 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1164 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1165 DBusMessageIter entry, value;
1168 dbus_message_iter_recurse(prop, &entry);
1169 dbus_message_iter_get_basic(&entry, &key);
1171 dbus_message_iter_next(&entry);
1172 dbus_message_iter_recurse(&entry, &value);
1174 if (g_str_equal(key, "Powered") == TRUE)
1175 dbus_message_iter_get_basic(&value, &powered);
1176 else if (g_str_equal(key, "Lockdown") == TRUE)
1177 dbus_message_iter_get_basic(&value, &locked);
1178 else if (g_str_equal(key, "Online") == TRUE) {
1180 dbus_message_iter_get_basic(&value, &online);
1181 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1182 has_sim = modem_has_sim(&value);
1183 has_reg = modem_has_reg(&value);
1184 has_gprs = modem_has_gprs(&value);
1187 dbus_message_iter_next(prop);
1194 modem_change_powered(path, TRUE);
1196 modem->has_sim = has_sim;
1197 modem->has_reg = has_reg;
1198 modem->has_gprs = has_gprs;
1200 update_modem_online(modem, online);
1206 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1210 DBusMessageIter array, dict;
1214 reply = dbus_pending_call_steal_reply(call);
1216 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1219 dbus_error_init(&error);
1221 if (dbus_set_error_from_message(&error, reply)) {
1222 connman_error("ModemManager.GetModems() %s %s",
1223 error.name, error.message);
1224 dbus_error_free(&error);
1228 if (dbus_message_iter_init(reply, &array) == FALSE)
1231 dbus_message_iter_recurse(&array, &dict);
1233 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1234 DBusMessageIter value, properties;
1235 const char *modem_path;
1237 dbus_message_iter_recurse(&dict, &value);
1238 dbus_message_iter_get_basic(&value, &modem_path);
1240 dbus_message_iter_next(&value);
1241 dbus_message_iter_recurse(&value, &properties);
1244 add_modem(modem_path, &properties);
1246 dbus_message_iter_next(&dict);
1250 dbus_message_unref(reply);
1252 dbus_pending_call_unref(call);
1255 static void ofono_connect(DBusConnection *connection, void *user_data)
1257 DBG("connection %p", connection);
1259 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1260 g_free, remove_modem);
1262 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1263 NULL, remove_network);
1265 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1266 manager_modems_reply, NULL, NULL,
1270 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1272 DBG("connection %p", connection);
1274 if (modem_hash == NULL)
1277 g_hash_table_destroy(modem_hash);
1282 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1285 const char *path = dbus_message_get_path(message);
1286 struct modem_data *modem;
1287 DBusMessageIter iter, value;
1290 DBG("path %s", path);
1292 modem = g_hash_table_lookup(modem_hash, path);
1296 if (dbus_message_iter_init(message, &iter) == FALSE)
1299 dbus_message_iter_get_basic(&iter, &key);
1301 dbus_message_iter_next(&iter);
1302 dbus_message_iter_recurse(&iter, &value);
1304 if (g_str_equal(key, "Powered") == TRUE) {
1305 dbus_bool_t powered;
1307 dbus_message_iter_get_basic(&value, &powered);
1308 if (powered == TRUE)
1311 modem->has_sim = FALSE;
1312 modem->has_reg = FALSE;
1313 modem->has_gprs = FALSE;
1315 modem_remove_device(modem);
1316 } else if (g_str_equal(key, "Online") == TRUE) {
1319 dbus_message_iter_get_basic(&value, &online);
1321 update_modem_online(modem, online);
1322 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1325 dbus_message_iter_get_basic(&value, &locked);
1328 modem_change_powered(path, TRUE);
1330 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1331 gboolean has_sim = modem_has_sim(&value);
1332 gboolean has_reg = modem_has_reg(&value);
1333 gboolean had_reg = modem->has_reg;
1334 gboolean has_gprs = modem_has_gprs(&value);
1335 gboolean had_gprs = modem->has_gprs;
1337 modem->has_sim = has_sim;
1338 modem->has_reg = has_reg;
1339 modem->has_gprs = has_gprs;
1341 if (modem->device == NULL) {
1343 get_imsi(modem->path);
1344 } else if (!has_sim) {
1345 modem_remove_device(modem);
1347 if (has_reg && !had_reg)
1348 check_registration(modem);
1349 else if (had_reg && !has_reg)
1350 modem_registration_removed(modem);
1352 if (has_gprs && !had_gprs) {
1353 gprs_change_powered(modem->path, TRUE);
1362 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1365 const char *path = dbus_message_get_path(message);
1366 struct modem_data *modem;
1367 DBusMessageIter iter, value;
1370 DBG("path %s", path);
1372 modem = g_hash_table_lookup(modem_hash, path);
1376 if (dbus_message_iter_init(message, &iter) == FALSE)
1379 dbus_message_iter_get_basic(&iter, &key);
1381 dbus_message_iter_next(&iter);
1382 dbus_message_iter_recurse(&iter, &value);
1384 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1387 dbus_message_iter_get_basic(&value, &imsi);
1389 add_device(path, imsi);
1390 } else if (g_str_equal(key, "Present") == TRUE) {
1391 dbus_bool_t present;
1393 dbus_message_iter_get_basic(&value, &present);
1398 if (modem->device != NULL)
1399 modem_remove_device(modem);
1401 modem->has_gprs = FALSE;
1402 modem->has_reg = FALSE;
1408 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1411 const char *path = dbus_message_get_path(message);
1412 struct modem_data *modem;
1413 DBusMessageIter iter;
1415 DBG("path %s", path);
1417 modem = g_hash_table_lookup(modem_hash, path);
1421 if (dbus_message_iter_init(message, &iter))
1422 modem_gprs_changed(modem, &iter);
1427 static gboolean context_added(DBusConnection *connection,
1428 DBusMessage *message, void *user_data)
1430 const char *path = dbus_message_get_path(message);
1431 const char *network_path;
1432 struct modem_data *modem;
1433 DBusMessageIter iter, properties;
1435 DBG("path %s", path);
1437 modem = g_hash_table_lookup(modem_hash, path);
1438 if (modem == NULL || modem->device == NULL)
1441 if (dbus_message_iter_init(message, &iter) == FALSE)
1444 dbus_message_iter_get_basic(&iter, &network_path);
1446 dbus_message_iter_next(&iter);
1447 dbus_message_iter_recurse(&iter, &properties);
1449 add_network(modem->device, network_path, &properties);
1454 static gboolean context_removed(DBusConnection *connection,
1455 DBusMessage *message, void *user_data)
1457 const char *path = dbus_message_get_path(message);
1458 const char *network_path, *identifier;
1459 struct modem_data *modem;
1460 struct connman_network *network;
1461 DBusMessageIter iter;
1463 DBG("path %s", path);
1465 modem = g_hash_table_lookup(modem_hash, path);
1466 if (modem == NULL || modem->device == NULL)
1469 if (dbus_message_iter_init(message, &iter) == FALSE)
1472 dbus_message_iter_get_basic(&iter, &network_path);
1474 network = g_hash_table_lookup(network_hash, network_path);
1475 if (network == NULL)
1478 identifier = connman_network_get_identifier(network);
1479 connman_device_remove_network(modem->device, identifier);
1484 static gboolean modem_added(DBusConnection *connection,
1485 DBusMessage *message, void *user_data)
1487 DBusMessageIter iter, properties;
1488 const char *modem_path;
1492 if (dbus_message_iter_init(message, &iter) == FALSE)
1495 dbus_message_iter_get_basic(&iter, &modem_path);
1497 dbus_message_iter_next(&iter);
1498 dbus_message_iter_recurse(&iter, &properties);
1500 add_modem(modem_path, &properties);
1505 static gboolean modem_removed(DBusConnection *connection,
1506 DBusMessage *message, void *user_data)
1508 DBusMessageIter iter;
1509 const char *modem_path;
1513 if (dbus_message_iter_init(message, &iter) == FALSE)
1516 dbus_message_iter_get_basic(&iter, &modem_path);
1518 g_hash_table_remove(modem_hash, modem_path);
1524 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1526 DBusMessageIter entry;
1527 gchar *nameserver = NULL, *nameserver_old = NULL;
1531 dbus_message_iter_recurse(array, &entry);
1533 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1536 dbus_message_iter_get_basic(&entry, &dns);
1540 if (nameserver == NULL) {
1542 nameserver = g_strdup(dns);
1545 nameserver_old = nameserver;
1546 nameserver = g_strdup_printf("%s %s",
1547 nameserver_old, dns);
1548 g_free(nameserver_old);
1551 dbus_message_iter_next(&entry);
1554 parent->ipv4.nameserver = nameserver;
1557 static void update_settings(DBusMessageIter *array,
1558 struct connman_network *network)
1560 struct connman_element *parent = connman_network_get_element(network);
1561 DBusMessageIter dict;
1562 const char *interface = NULL;
1564 DBG("network %p", network);
1566 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1569 dbus_message_iter_recurse(array, &dict);
1571 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1572 DBusMessageIter entry, value;
1575 dbus_message_iter_recurse(&dict, &entry);
1576 dbus_message_iter_get_basic(&entry, &key);
1580 dbus_message_iter_next(&entry);
1581 dbus_message_iter_recurse(&entry, &value);
1583 if (g_str_equal(key, "Interface") == TRUE) {
1586 dbus_message_iter_get_basic(&value, &interface);
1588 DBG("interface %s", interface);
1590 index = connman_inet_ifindex(interface);
1592 connman_network_set_index(network, index);
1594 connman_error("Can not find interface %s",
1598 } else if (g_str_equal(key, "Method") == TRUE) {
1601 dbus_message_iter_get_basic(&value, &method);
1602 if (g_strcmp0(method, "static") == 0) {
1604 parent->ipv4.method =
1605 CONNMAN_IPCONFIG_METHOD_FIXED;
1606 } else if (g_strcmp0(method, "dhcp") == 0) {
1608 parent->ipv4.method =
1609 CONNMAN_IPCONFIG_METHOD_DHCP;
1612 } else if (g_str_equal(key, "Address") == TRUE) {
1613 const char *address;
1615 dbus_message_iter_get_basic(&value, &address);
1617 DBG("address %s", address);
1619 parent->ipv4.address = g_strdup(address);
1620 } else if (g_str_equal(key, "Netmask") == TRUE) {
1621 const char *netmask;
1623 dbus_message_iter_get_basic(&value, &netmask);
1625 DBG("netmask %s", netmask);
1627 parent->ipv4.netmask = g_strdup(netmask);
1628 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1630 get_dns(&value, parent);
1631 } else if (g_str_equal(key, "Gateway") == TRUE) {
1632 const char *gateway;
1634 dbus_message_iter_get_basic(&value, &gateway);
1636 DBG("gateway %s", gateway);
1638 parent->ipv4.gateway = g_strdup(gateway);
1641 dbus_message_iter_next(&dict);
1644 /* deactive, oFono send NULL inteface before deactive signal */
1645 if (interface == NULL)
1646 connman_network_set_index(network, -1);
1649 static void cleanup_ipconfig(struct connman_network *network)
1651 struct connman_element *parent = connman_network_get_element(network);
1653 g_free(parent->ipv4.address);
1654 parent->ipv4.address = NULL;
1656 g_free(parent->ipv4.netmask);
1657 parent->ipv4.netmask = NULL;
1659 g_free(parent->ipv4.nameserver);
1660 parent->ipv4.nameserver = NULL;
1662 g_free(parent->ipv4.gateway);
1663 parent->ipv4.gateway = NULL;
1665 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1669 static void set_connected(struct connman_network *network,
1670 connman_bool_t connected)
1672 struct connman_element *parent = connman_network_get_element(network);
1673 enum connman_ipconfig_method method = parent->ipv4.method;
1676 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1677 case CONNMAN_IPCONFIG_METHOD_OFF:
1678 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1679 case CONNMAN_IPCONFIG_METHOD_AUTO:
1682 case CONNMAN_IPCONFIG_METHOD_FIXED:
1683 connman_network_set_ipv4_method(network, method);
1685 connman_network_set_connected(network, connected);
1687 if (connected == FALSE)
1688 cleanup_ipconfig(network);
1692 case CONNMAN_IPCONFIG_METHOD_DHCP:
1693 connman_network_set_ipv4_method(network, method);
1695 connman_network_set_connected(network, connected);
1700 static gboolean context_changed(DBusConnection *connection,
1701 DBusMessage *message, void *user_data)
1703 const char *path = dbus_message_get_path(message);
1704 struct connman_network *network;
1705 DBusMessageIter iter, value;
1708 DBG("path %s", path);
1710 network = g_hash_table_lookup(network_hash, path);
1711 if (network == NULL)
1714 if (!pending_network_is_available(network)) {
1715 remove_network(network);
1719 if (dbus_message_iter_init(message, &iter) == FALSE)
1722 dbus_message_iter_get_basic(&iter, &key);
1724 dbus_message_iter_next(&iter);
1725 dbus_message_iter_recurse(&iter, &value);
1727 if (g_str_equal(key, "Settings") == TRUE)
1728 update_settings(&value, network);
1729 else if (g_str_equal(key, "Active") == TRUE) {
1732 dbus_message_iter_get_basic(&value, &active);
1734 set_connected(network, active);
1741 static guint reg_watch;
1742 static guint sim_watch;
1743 static guint gprs_watch;
1744 static guint context_added_watch;
1745 static guint context_removed_watch;
1746 static guint modem_watch;
1747 static guint modem_added_watch;
1748 static guint modem_removed_watch;
1749 static guint context_watch;
1751 static int ofono_init(void)
1755 connection = connman_dbus_get_connection();
1756 if (connection == NULL)
1759 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1760 ofono_connect, ofono_disconnect, NULL, NULL);
1762 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1763 OFONO_REGISTRATION_INTERFACE,
1768 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1769 OFONO_GPRS_INTERFACE,
1774 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1775 OFONO_GPRS_INTERFACE,
1780 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1781 OFONO_GPRS_INTERFACE,
1786 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1787 OFONO_MODEM_INTERFACE,
1792 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1793 OFONO_SIM_INTERFACE,
1798 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1799 OFONO_MANAGER_INTERFACE,
1804 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1805 OFONO_MANAGER_INTERFACE,
1810 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1811 OFONO_CONTEXT_INTERFACE,
1816 if (watch == 0 || gprs_watch == 0 || context_added_watch == 0 ||
1817 context_removed_watch == 0 || modem_watch == 0 ||
1818 reg_watch == 0 || sim_watch == 0 ||
1819 modem_added_watch == 0 || modem_removed_watch == 0 ||
1820 context_watch == 0) {
1825 err = connman_network_driver_register(&network_driver);
1829 err = connman_device_driver_register(&modem_driver);
1831 connman_network_driver_unregister(&network_driver);
1838 g_dbus_remove_watch(connection, watch);
1839 g_dbus_remove_watch(connection, sim_watch);
1840 g_dbus_remove_watch(connection, reg_watch);
1841 g_dbus_remove_watch(connection, gprs_watch);
1842 g_dbus_remove_watch(connection, context_added_watch);
1843 g_dbus_remove_watch(connection, context_removed_watch);
1844 g_dbus_remove_watch(connection, modem_watch);
1845 g_dbus_remove_watch(connection, modem_added_watch);
1846 g_dbus_remove_watch(connection, modem_removed_watch);
1847 g_dbus_remove_watch(connection, context_watch);
1849 dbus_connection_unref(connection);
1854 static void ofono_exit(void)
1856 g_dbus_remove_watch(connection, watch);
1857 g_dbus_remove_watch(connection, sim_watch);
1858 g_dbus_remove_watch(connection, reg_watch);
1859 g_dbus_remove_watch(connection, gprs_watch);
1860 g_dbus_remove_watch(connection, context_added_watch);
1861 g_dbus_remove_watch(connection, context_removed_watch);
1862 g_dbus_remove_watch(connection, modem_watch);
1863 g_dbus_remove_watch(connection, modem_added_watch);
1864 g_dbus_remove_watch(connection, modem_removed_watch);
1865 g_dbus_remove_watch(connection, context_watch);
1867 ofono_disconnect(connection, NULL);
1869 connman_device_driver_unregister(&modem_driver);
1870 connman_network_driver_unregister(&network_driver);
1872 dbus_connection_unref(connection);
1875 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1876 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)