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 return set_network_active(network, FALSE);
495 static void network_remove(struct connman_network *network)
497 char const *path = connman_network_get_string(network, "Path");
499 DBG("network %p path %s", network, path);
501 g_hash_table_remove(network_hash, path);
504 static struct connman_network_driver network_driver = {
506 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
507 .probe = network_probe,
508 .remove = network_remove,
509 .connect = network_connect,
510 .disconnect = network_disconnect,
513 static void update_settings(DBusMessageIter *array,
514 struct connman_network *network);
516 static void set_connected(struct connman_network *network,
517 connman_bool_t connected);
519 static int add_network(struct connman_device *device,
520 const char *path, DBusMessageIter *dict)
522 struct modem_data *modem = connman_device_get_data(device);
523 struct connman_network *network;
525 const char *hash_path;
526 char const *operator;
527 dbus_bool_t active = FALSE;
529 DBG("modem %p device %p path %s", modem, device, path);
531 ident = get_ident(path);
533 network = connman_device_get_network(device, ident);
537 network = g_hash_table_lookup(network_hash, path);
538 if (network != NULL) {
539 DBG("path %p already exists with device %p", path,
540 connman_network_get_device(network));
541 if (connman_network_get_device(network))
543 g_hash_table_remove(network_hash, path);
546 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
550 connman_network_set_string(network, "Path", path);
551 hash_path = connman_network_get_string(network, "Path");
552 if (hash_path == NULL)
555 create_service(network);
557 connman_network_ref(network);
558 g_hash_table_insert(network_hash, (char *)hash_path, network);
560 connman_network_set_available(network, TRUE);
561 connman_network_set_index(network, -1);
563 operator = connman_device_get_string(device, "Operator");
565 connman_network_set_name(network, operator);
567 if (modem->has_strength)
568 connman_network_set_strength(network, modem->strength);
570 connman_network_set_roaming(network, modem->roaming);
572 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
573 DBusMessageIter entry, value;
576 dbus_message_iter_recurse(dict, &entry);
577 dbus_message_iter_get_basic(&entry, &key);
579 dbus_message_iter_next(&entry);
580 dbus_message_iter_recurse(&entry, &value);
582 if (g_str_equal(key, "Type")) {
585 dbus_message_iter_get_basic(&value, &type);
586 if (g_strcmp0(type, "internet") != 0) {
587 DBG("path %p type %s", path, type);
590 } else if (g_str_equal(key, "Settings"))
591 update_settings(&value, network);
592 else if (g_str_equal(key, "Active") == TRUE)
593 dbus_message_iter_get_basic(&value, &active);
595 dbus_message_iter_next(dict);
598 if (connman_device_add_network(device, network) != 0)
602 set_connected(network, active);
607 connman_network_unref(network);
608 g_hash_table_remove(network_hash, hash_path);
612 static void check_networks_reply(DBusPendingCall *call, void *user_data)
614 char *path = user_data;
615 struct modem_data *modem;
617 DBusMessageIter array, entry, value, properties;
619 DBG("path %s", path);
621 modem = g_hash_table_lookup(modem_hash, path);
624 if (modem->device == NULL)
627 reply = dbus_pending_call_steal_reply(call);
629 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
632 dbus_message_iter_init(reply, &array);
634 dbus_message_iter_recurse(&array, &entry);
636 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
637 char const *network_path;
639 dbus_message_iter_recurse(&entry, &value);
640 dbus_message_iter_get_basic(&value, &network_path);
642 dbus_message_iter_next(&value);
643 dbus_message_iter_recurse(&value, &properties);
645 add_network(modem->device, network_path, &properties);
647 dbus_message_iter_next(&entry);
651 dbus_message_unref(reply);
653 dbus_pending_call_unref(call);
656 static void check_networks(struct modem_data *modem)
658 char const *path = modem->path;
660 DBG("modem %p path %s", modem, path);
662 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
663 check_networks_reply, g_strdup(path), g_free,
667 static void modem_clear_network_errors(struct modem_data *modem)
669 struct connman_device *device = modem->device;
676 g_hash_table_iter_init(&i, network_hash);
678 while (g_hash_table_iter_next(&i, NULL, &value)) {
679 struct connman_network *network = value;
681 if (connman_network_get_device(network) == device)
682 connman_network_clear_error(network);
686 static void modem_operator_name_changed(struct modem_data *modem,
689 struct connman_device *device = modem->device;
696 connman_device_set_string(device, "Operator", name);
698 for (g_hash_table_iter_init(&i, network_hash);
699 g_hash_table_iter_next(&i, NULL, &value);) {
700 struct connman_network *network = value;
702 if (connman_network_get_device(network) == device) {
703 connman_network_set_name(network, name);
704 connman_network_update(network);
709 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
711 struct connman_device *device = modem->device;
715 modem->strength = strength;
716 modem->has_strength = TRUE;
721 for (g_hash_table_iter_init(&i, network_hash);
722 g_hash_table_iter_next(&i, NULL, &value);) {
723 struct connman_network *network = value;
725 if (connman_network_get_device(network) == device) {
726 connman_network_set_strength(network, strength);
727 connman_network_update(network);
732 static void modem_roaming_changed(struct modem_data *modem,
735 struct connman_device *device = modem->device;
736 connman_bool_t roaming = FALSE;
737 connman_bool_t registered = FALSE;
738 connman_bool_t was_roaming = modem->roaming;
742 if (g_str_equal(status, "roaming"))
744 else if (g_str_equal(status, "registered"))
747 registered = registered || roaming;
749 if (modem->roaming == roaming && modem->registered == registered)
752 modem->registered = registered;
753 modem->roaming = roaming;
755 if (roaming == was_roaming)
761 g_hash_table_iter_init(&i, network_hash);
763 while (g_hash_table_iter_next(&i, NULL, &value)) {
764 struct connman_network *network = value;
766 if (connman_network_get_device(network) == device) {
767 connman_network_set_roaming(network, roaming);
768 connman_network_update(network);
773 static void modem_registration_removed(struct modem_data *modem)
775 modem->registered = FALSE;
776 modem->roaming = FALSE;
779 static void modem_registration_changed(struct modem_data *modem,
780 DBusMessageIter *entry)
782 DBusMessageIter iter;
785 connman_uint8_t strength;
786 char const *name, *status, *mcc_s;
788 dbus_message_iter_get_basic(entry, &key);
792 dbus_message_iter_next(entry);
794 dbus_message_iter_recurse(entry, &iter);
796 type = dbus_message_iter_get_arg_type(&iter);
797 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
800 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
801 dbus_message_iter_get_basic(&iter, &name);
802 modem_operator_name_changed(modem, name);
803 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
804 dbus_message_iter_get_basic(&iter, &strength);
805 modem_strength_changed(modem, strength);
806 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
807 dbus_message_iter_get_basic(&iter, &status);
808 modem_roaming_changed(modem, status);
809 } else if (g_str_equal(key, "MobileCountryCode") &&
810 type == DBUS_TYPE_STRING) {
814 dbus_message_iter_get_basic(&iter, &mcc_s);
820 alpha2 = mcc_country_codes[mcc - 200];
821 connman_technology_set_regdom(alpha2);
826 static gboolean reg_changed(DBusConnection *connection,
827 DBusMessage *message, void *user_data)
829 const char *path = dbus_message_get_path(message);
830 struct modem_data *modem;
831 DBusMessageIter iter;
833 DBG("path %s", path);
835 modem = g_hash_table_lookup(modem_hash, path);
839 if (dbus_message_iter_init(message, &iter))
840 modem_registration_changed(modem, &iter);
845 static void check_registration_reply(DBusPendingCall *call, void *user_data)
847 char const *path = user_data;
848 struct modem_data *modem;
850 DBusMessageIter array, dict, entry;
852 DBG("path %s", path);
854 modem = g_hash_table_lookup(modem_hash, path);
858 reply = dbus_pending_call_steal_reply(call);
860 if (dbus_message_iter_init(reply, &array) == FALSE)
863 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
866 dbus_message_iter_recurse(&array, &dict);
867 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
868 dbus_message_iter_recurse(&dict, &entry);
869 modem_registration_changed(modem, &entry);
870 dbus_message_iter_next(&dict);
874 dbus_message_unref(reply);
876 dbus_pending_call_unref(call);
879 static void check_registration(struct modem_data *modem)
881 char const *path = modem->path;
883 DBG("modem %p path %s", modem, path);
885 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
886 check_registration_reply, g_strdup(path), g_free,
890 static void modem_gprs_changed(struct modem_data *modem,
891 DBusMessageIter *entry)
893 DBusMessageIter iter;
898 dbus_message_iter_get_basic(entry, &key);
902 dbus_message_iter_next(entry);
904 dbus_message_iter_recurse(entry, &iter);
906 type = dbus_message_iter_get_arg_type(&iter);
908 if (type != DBUS_TYPE_BOOLEAN)
911 dbus_message_iter_get_basic(&iter, &value);
913 if (g_str_equal(key, "Attached") == TRUE) {
914 DBG("Attached %d", value);
916 modem->attached = value;
919 modem_clear_network_errors(modem);
920 } else if (g_str_equal(key, "Powered") == TRUE) {
921 DBG("Powered %d", value);
923 modem->powered = value;
924 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
925 DBG("RoamingAllowed %d", value);
927 modem->roaming_allowed = value;
931 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
933 char const *path = user_data;
934 struct modem_data *modem;
936 DBusMessageIter array, dict, entry;
938 DBG("path %s", path);
940 modem = g_hash_table_lookup(modem_hash, path);
944 reply = dbus_pending_call_steal_reply(call);
946 if (dbus_message_iter_init(reply, &array) == FALSE)
949 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
952 dbus_message_iter_recurse(&array, &dict);
953 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
954 dbus_message_iter_recurse(&dict, &entry);
955 modem_gprs_changed(modem, &entry);
956 dbus_message_iter_next(&dict);
960 dbus_message_unref(reply);
962 dbus_pending_call_unref(call);
964 check_networks(modem);
967 static void check_gprs(struct modem_data *modem)
969 char const *path = modem->path;
971 DBG("modem %p path %s", modem, path);
973 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
974 check_gprs_reply, g_strdup(path), g_free,
978 static void add_device(const char *path, const char *imsi)
980 struct modem_data *modem;
981 struct connman_device *device;
983 DBG("path %s imsi %s", path, imsi);
991 modem = g_hash_table_lookup(modem_hash, path);
996 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
999 modem_remove_device(modem);
1002 if (strlen(imsi) == 0)
1005 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1009 connman_device_set_ident(device, imsi);
1011 connman_device_set_string(device, "Path", path);
1013 connman_device_set_data(device, modem);
1015 if (connman_device_register(device) < 0) {
1016 connman_device_unref(device);
1020 modem->device = device;
1023 check_registration(modem);
1025 if (modem->has_gprs)
1029 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1031 const char *path = user_data;
1032 const char *imsi = NULL;
1034 DBusMessageIter array, dict;
1036 DBG("path %s", path);
1038 reply = dbus_pending_call_steal_reply(call);
1040 if (dbus_message_iter_init(reply, &array) == FALSE)
1043 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1046 dbus_message_iter_recurse(&array, &dict);
1048 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1049 DBusMessageIter entry, value;
1052 dbus_message_iter_recurse(&dict, &entry);
1053 dbus_message_iter_get_basic(&entry, &key);
1055 dbus_message_iter_next(&entry);
1056 dbus_message_iter_recurse(&entry, &value);
1058 if (g_str_equal(key, "SubscriberIdentity")) {
1059 dbus_message_iter_get_basic(&value, &imsi);
1060 add_device(path, imsi);
1063 dbus_message_iter_next(&dict);
1067 dbus_message_unref(reply);
1069 dbus_pending_call_unref(call);
1072 static void get_imsi(const char *path)
1074 DBG("path %s", path);
1076 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1077 sim_properties_reply, g_strdup(path), g_free,
1081 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1083 DBG("path %s powered %d", path, powered);
1085 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1086 DBUS_TYPE_BOOLEAN, &powered,
1090 static int modem_change_powered(const char *path, dbus_bool_t powered)
1092 DBG("path %s powered %d", path, powered);
1094 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1095 DBUS_TYPE_BOOLEAN, &powered,
1100 static gboolean modem_has_interface(DBusMessageIter *array,
1101 char const *interface)
1103 DBusMessageIter entry;
1105 dbus_message_iter_recurse(array, &entry);
1107 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1108 const char *element;
1110 dbus_message_iter_get_basic(&entry, &element);
1112 if (g_strcmp0(interface, element) == 0)
1115 dbus_message_iter_next(&entry);
1121 static gboolean modem_has_sim(DBusMessageIter *array)
1123 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1126 static gboolean modem_has_reg(DBusMessageIter *array)
1128 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1131 static gboolean modem_has_gprs(DBusMessageIter *array)
1133 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1136 static void add_modem(const char *path, DBusMessageIter *prop)
1138 struct modem_data *modem;
1139 dbus_bool_t powered = FALSE;
1140 dbus_bool_t online = FALSE;
1141 dbus_bool_t has_online = FALSE;
1142 dbus_bool_t locked = FALSE;
1143 gboolean has_sim = FALSE;
1144 gboolean has_reg = FALSE;
1145 gboolean has_gprs = FALSE;
1147 modem = g_hash_table_lookup(modem_hash, path);
1152 modem = g_try_new0(struct modem_data, 1);
1156 modem->path = g_strdup(path);
1157 modem->device = NULL;
1158 modem->available = TRUE;
1160 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1162 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1163 DBusMessageIter entry, value;
1166 dbus_message_iter_recurse(prop, &entry);
1167 dbus_message_iter_get_basic(&entry, &key);
1169 dbus_message_iter_next(&entry);
1170 dbus_message_iter_recurse(&entry, &value);
1172 if (g_str_equal(key, "Powered") == TRUE)
1173 dbus_message_iter_get_basic(&value, &powered);
1174 else if (g_str_equal(key, "Lockdown") == TRUE)
1175 dbus_message_iter_get_basic(&value, &locked);
1176 else if (g_str_equal(key, "Online") == TRUE) {
1178 dbus_message_iter_get_basic(&value, &online);
1179 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1180 has_sim = modem_has_sim(&value);
1181 has_reg = modem_has_reg(&value);
1182 has_gprs = modem_has_gprs(&value);
1185 dbus_message_iter_next(prop);
1192 modem_change_powered(path, TRUE);
1194 modem->has_sim = has_sim;
1195 modem->has_reg = has_reg;
1196 modem->has_gprs = has_gprs;
1198 update_modem_online(modem, online);
1204 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1208 DBusMessageIter array, dict;
1212 reply = dbus_pending_call_steal_reply(call);
1214 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1217 dbus_error_init(&error);
1219 if (dbus_set_error_from_message(&error, reply)) {
1220 connman_error("ModemManager.GetModems() %s %s",
1221 error.name, error.message);
1222 dbus_error_free(&error);
1226 if (dbus_message_iter_init(reply, &array) == FALSE)
1229 dbus_message_iter_recurse(&array, &dict);
1231 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1232 DBusMessageIter value, properties;
1233 const char *modem_path;
1235 dbus_message_iter_recurse(&dict, &value);
1236 dbus_message_iter_get_basic(&value, &modem_path);
1238 dbus_message_iter_next(&value);
1239 dbus_message_iter_recurse(&value, &properties);
1242 add_modem(modem_path, &properties);
1244 dbus_message_iter_next(&dict);
1248 dbus_message_unref(reply);
1250 dbus_pending_call_unref(call);
1253 static void ofono_connect(DBusConnection *connection, void *user_data)
1255 DBG("connection %p", connection);
1257 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1258 g_free, remove_modem);
1260 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1261 NULL, remove_network);
1263 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1264 manager_modems_reply, NULL, NULL,
1268 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1270 DBG("connection %p", connection);
1272 if (modem_hash == NULL)
1275 g_hash_table_destroy(modem_hash);
1280 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1283 const char *path = dbus_message_get_path(message);
1284 struct modem_data *modem;
1285 DBusMessageIter iter, value;
1288 DBG("path %s", path);
1290 modem = g_hash_table_lookup(modem_hash, path);
1294 if (dbus_message_iter_init(message, &iter) == FALSE)
1297 dbus_message_iter_get_basic(&iter, &key);
1299 dbus_message_iter_next(&iter);
1300 dbus_message_iter_recurse(&iter, &value);
1302 if (g_str_equal(key, "Powered") == TRUE) {
1303 dbus_bool_t powered;
1305 dbus_message_iter_get_basic(&value, &powered);
1306 if (powered == TRUE)
1309 modem->has_sim = FALSE;
1310 modem->has_reg = FALSE;
1311 modem->has_gprs = FALSE;
1313 modem_remove_device(modem);
1314 } else if (g_str_equal(key, "Online") == TRUE) {
1317 dbus_message_iter_get_basic(&value, &online);
1319 update_modem_online(modem, online);
1320 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1323 dbus_message_iter_get_basic(&value, &locked);
1326 modem_change_powered(path, TRUE);
1328 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1329 gboolean has_sim = modem_has_sim(&value);
1330 gboolean has_reg = modem_has_reg(&value);
1331 gboolean had_reg = modem->has_reg;
1332 gboolean has_gprs = modem_has_gprs(&value);
1333 gboolean had_gprs = modem->has_gprs;
1335 modem->has_sim = has_sim;
1336 modem->has_reg = has_reg;
1337 modem->has_gprs = has_gprs;
1339 if (modem->device == NULL) {
1341 get_imsi(modem->path);
1342 } else if (!has_sim) {
1343 modem_remove_device(modem);
1345 if (has_reg && !had_reg)
1346 check_registration(modem);
1347 else if (had_reg && !has_reg)
1348 modem_registration_removed(modem);
1350 if (has_gprs && !had_gprs) {
1351 gprs_change_powered(modem->path, TRUE);
1360 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1363 const char *path = dbus_message_get_path(message);
1364 struct modem_data *modem;
1365 DBusMessageIter iter, value;
1368 DBG("path %s", path);
1370 modem = g_hash_table_lookup(modem_hash, path);
1374 if (dbus_message_iter_init(message, &iter) == FALSE)
1377 dbus_message_iter_get_basic(&iter, &key);
1379 dbus_message_iter_next(&iter);
1380 dbus_message_iter_recurse(&iter, &value);
1382 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1385 dbus_message_iter_get_basic(&value, &imsi);
1387 add_device(path, imsi);
1388 } else if (g_str_equal(key, "Present") == TRUE) {
1389 dbus_bool_t present;
1391 dbus_message_iter_get_basic(&value, &present);
1396 if (modem->device != NULL)
1397 modem_remove_device(modem);
1399 modem->has_gprs = FALSE;
1400 modem->has_reg = FALSE;
1406 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1409 const char *path = dbus_message_get_path(message);
1410 struct modem_data *modem;
1411 DBusMessageIter iter;
1413 DBG("path %s", path);
1415 modem = g_hash_table_lookup(modem_hash, path);
1419 if (dbus_message_iter_init(message, &iter))
1420 modem_gprs_changed(modem, &iter);
1425 static gboolean context_added(DBusConnection *connection,
1426 DBusMessage *message, void *user_data)
1428 const char *path = dbus_message_get_path(message);
1429 const char *network_path;
1430 struct modem_data *modem;
1431 DBusMessageIter iter, properties;
1433 DBG("path %s", path);
1435 modem = g_hash_table_lookup(modem_hash, path);
1436 if (modem == NULL || modem->device == NULL)
1439 if (dbus_message_iter_init(message, &iter) == FALSE)
1442 dbus_message_iter_get_basic(&iter, &network_path);
1444 dbus_message_iter_next(&iter);
1445 dbus_message_iter_recurse(&iter, &properties);
1447 add_network(modem->device, network_path, &properties);
1452 static gboolean context_removed(DBusConnection *connection,
1453 DBusMessage *message, void *user_data)
1455 const char *path = dbus_message_get_path(message);
1456 const char *network_path, *identifier;
1457 struct modem_data *modem;
1458 struct connman_network *network;
1459 DBusMessageIter iter;
1461 DBG("path %s", path);
1463 modem = g_hash_table_lookup(modem_hash, path);
1464 if (modem == NULL || modem->device == NULL)
1467 if (dbus_message_iter_init(message, &iter) == FALSE)
1470 dbus_message_iter_get_basic(&iter, &network_path);
1472 network = g_hash_table_lookup(network_hash, network_path);
1473 if (network == NULL)
1476 identifier = connman_network_get_identifier(network);
1477 connman_device_remove_network(modem->device, identifier);
1482 static gboolean modem_added(DBusConnection *connection,
1483 DBusMessage *message, void *user_data)
1485 DBusMessageIter iter, properties;
1486 const char *modem_path;
1490 if (dbus_message_iter_init(message, &iter) == FALSE)
1493 dbus_message_iter_get_basic(&iter, &modem_path);
1495 dbus_message_iter_next(&iter);
1496 dbus_message_iter_recurse(&iter, &properties);
1498 add_modem(modem_path, &properties);
1503 static gboolean modem_removed(DBusConnection *connection,
1504 DBusMessage *message, void *user_data)
1506 DBusMessageIter iter;
1507 const char *modem_path;
1511 if (dbus_message_iter_init(message, &iter) == FALSE)
1514 dbus_message_iter_get_basic(&iter, &modem_path);
1516 g_hash_table_remove(modem_hash, modem_path);
1522 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1524 DBusMessageIter entry;
1525 gchar *nameserver = NULL, *nameserver_old = NULL;
1529 dbus_message_iter_recurse(array, &entry);
1531 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1534 dbus_message_iter_get_basic(&entry, &dns);
1538 if (nameserver == NULL) {
1540 nameserver = g_strdup(dns);
1543 nameserver_old = nameserver;
1544 nameserver = g_strdup_printf("%s %s",
1545 nameserver_old, dns);
1546 g_free(nameserver_old);
1549 dbus_message_iter_next(&entry);
1552 parent->ipv4.nameserver = nameserver;
1555 static void update_settings(DBusMessageIter *array,
1556 struct connman_network *network)
1558 struct connman_element *parent = connman_network_get_element(network);
1559 DBusMessageIter dict;
1560 const char *interface = NULL;
1562 DBG("network %p", network);
1564 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1567 dbus_message_iter_recurse(array, &dict);
1569 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1570 DBusMessageIter entry, value;
1573 dbus_message_iter_recurse(&dict, &entry);
1574 dbus_message_iter_get_basic(&entry, &key);
1578 dbus_message_iter_next(&entry);
1579 dbus_message_iter_recurse(&entry, &value);
1581 if (g_str_equal(key, "Interface") == TRUE) {
1584 dbus_message_iter_get_basic(&value, &interface);
1586 DBG("interface %s", interface);
1588 index = connman_inet_ifindex(interface);
1590 connman_network_set_index(network, index);
1592 connman_error("Can not find interface %s",
1596 } else if (g_str_equal(key, "Method") == TRUE) {
1599 dbus_message_iter_get_basic(&value, &method);
1600 if (g_strcmp0(method, "static") == 0) {
1602 parent->ipv4.method =
1603 CONNMAN_IPCONFIG_METHOD_FIXED;
1604 } else if (g_strcmp0(method, "dhcp") == 0) {
1606 parent->ipv4.method =
1607 CONNMAN_IPCONFIG_METHOD_DHCP;
1610 } else if (g_str_equal(key, "Address") == TRUE) {
1611 const char *address;
1613 dbus_message_iter_get_basic(&value, &address);
1615 DBG("address %s", address);
1617 parent->ipv4.address = g_strdup(address);
1618 } else if (g_str_equal(key, "Netmask") == TRUE) {
1619 const char *netmask;
1621 dbus_message_iter_get_basic(&value, &netmask);
1623 DBG("netmask %s", netmask);
1625 parent->ipv4.netmask = g_strdup(netmask);
1626 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1628 get_dns(&value, parent);
1629 } else if (g_str_equal(key, "Gateway") == TRUE) {
1630 const char *gateway;
1632 dbus_message_iter_get_basic(&value, &gateway);
1634 DBG("gateway %s", gateway);
1636 parent->ipv4.gateway = g_strdup(gateway);
1639 dbus_message_iter_next(&dict);
1642 /* deactive, oFono send NULL inteface before deactive signal */
1643 if (interface == NULL)
1644 connman_network_set_index(network, -1);
1647 static void cleanup_ipconfig(struct connman_network *network)
1649 struct connman_element *parent = connman_network_get_element(network);
1651 g_free(parent->ipv4.address);
1652 parent->ipv4.address = NULL;
1654 g_free(parent->ipv4.netmask);
1655 parent->ipv4.netmask = NULL;
1657 g_free(parent->ipv4.nameserver);
1658 parent->ipv4.nameserver = NULL;
1660 g_free(parent->ipv4.gateway);
1661 parent->ipv4.gateway = NULL;
1663 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1667 static void set_connected(struct connman_network *network,
1668 connman_bool_t connected)
1670 struct connman_element *parent = connman_network_get_element(network);
1671 enum connman_ipconfig_method method = parent->ipv4.method;
1674 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1675 case CONNMAN_IPCONFIG_METHOD_OFF:
1676 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1677 case CONNMAN_IPCONFIG_METHOD_AUTO:
1680 case CONNMAN_IPCONFIG_METHOD_FIXED:
1681 connman_network_set_ipv4_method(network, method);
1683 connman_network_set_connected(network, connected);
1685 if (connected == FALSE)
1686 cleanup_ipconfig(network);
1690 case CONNMAN_IPCONFIG_METHOD_DHCP:
1691 connman_network_set_ipv4_method(network, method);
1693 connman_network_set_connected(network, connected);
1698 static gboolean context_changed(DBusConnection *connection,
1699 DBusMessage *message, void *user_data)
1701 const char *path = dbus_message_get_path(message);
1702 struct connman_network *network;
1703 DBusMessageIter iter, value;
1706 DBG("path %s", path);
1708 network = g_hash_table_lookup(network_hash, path);
1709 if (network == NULL)
1712 if (!pending_network_is_available(network)) {
1713 remove_network(network);
1717 if (dbus_message_iter_init(message, &iter) == FALSE)
1720 dbus_message_iter_get_basic(&iter, &key);
1722 dbus_message_iter_next(&iter);
1723 dbus_message_iter_recurse(&iter, &value);
1725 if (g_str_equal(key, "Settings") == TRUE)
1726 update_settings(&value, network);
1727 else if (g_str_equal(key, "Active") == TRUE) {
1730 dbus_message_iter_get_basic(&value, &active);
1732 set_connected(network, active);
1739 static guint reg_watch;
1740 static guint sim_watch;
1741 static guint gprs_watch;
1742 static guint context_added_watch;
1743 static guint context_removed_watch;
1744 static guint modem_watch;
1745 static guint modem_added_watch;
1746 static guint modem_removed_watch;
1747 static guint context_watch;
1749 static int ofono_init(void)
1753 connection = connman_dbus_get_connection();
1754 if (connection == NULL)
1757 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1758 ofono_connect, ofono_disconnect, NULL, NULL);
1760 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1761 OFONO_REGISTRATION_INTERFACE,
1766 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1767 OFONO_GPRS_INTERFACE,
1772 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1773 OFONO_GPRS_INTERFACE,
1778 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1779 OFONO_GPRS_INTERFACE,
1784 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1785 OFONO_MODEM_INTERFACE,
1790 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1791 OFONO_SIM_INTERFACE,
1796 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1797 OFONO_MANAGER_INTERFACE,
1802 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1803 OFONO_MANAGER_INTERFACE,
1808 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1809 OFONO_CONTEXT_INTERFACE,
1814 if (watch == 0 || gprs_watch == 0 || context_added_watch == 0 ||
1815 context_removed_watch == 0 || modem_watch == 0 ||
1816 reg_watch == 0 || sim_watch == 0 ||
1817 modem_added_watch == 0 || modem_removed_watch == 0 ||
1818 context_watch == 0) {
1823 err = connman_network_driver_register(&network_driver);
1827 err = connman_device_driver_register(&modem_driver);
1829 connman_network_driver_unregister(&network_driver);
1836 g_dbus_remove_watch(connection, watch);
1837 g_dbus_remove_watch(connection, sim_watch);
1838 g_dbus_remove_watch(connection, reg_watch);
1839 g_dbus_remove_watch(connection, gprs_watch);
1840 g_dbus_remove_watch(connection, context_added_watch);
1841 g_dbus_remove_watch(connection, context_removed_watch);
1842 g_dbus_remove_watch(connection, modem_watch);
1843 g_dbus_remove_watch(connection, modem_added_watch);
1844 g_dbus_remove_watch(connection, modem_removed_watch);
1845 g_dbus_remove_watch(connection, context_watch);
1847 dbus_connection_unref(connection);
1852 static void ofono_exit(void)
1854 g_dbus_remove_watch(connection, watch);
1855 g_dbus_remove_watch(connection, sim_watch);
1856 g_dbus_remove_watch(connection, reg_watch);
1857 g_dbus_remove_watch(connection, gprs_watch);
1858 g_dbus_remove_watch(connection, context_added_watch);
1859 g_dbus_remove_watch(connection, context_removed_watch);
1860 g_dbus_remove_watch(connection, modem_watch);
1861 g_dbus_remove_watch(connection, modem_added_watch);
1862 g_dbus_remove_watch(connection, modem_removed_watch);
1863 g_dbus_remove_watch(connection, context_watch);
1865 ofono_disconnect(connection, NULL);
1867 connman_device_driver_unregister(&modem_driver);
1868 connman_network_driver_unregister(&network_driver);
1870 dbus_connection_unref(connection);
1873 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1874 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)