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/ipconfig.h>
39 #include <connman/dbus.h>
40 #include <connman/inet.h>
41 #include <connman/technology.h>
42 #include <connman/log.h>
46 #define OFONO_SERVICE "org.ofono"
48 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
49 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
50 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
51 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
52 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
53 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
55 #define PROPERTY_CHANGED "PropertyChanged"
56 #define GET_PROPERTIES "GetProperties"
57 #define SET_PROPERTY "SetProperty"
58 #define CONTEXT_ADDED "ContextAdded"
59 #define CONTEXT_REMOVED "ContextRemoved"
60 #define ADD_CONTEXT "AddContext"
61 #define GET_MODEMS "GetModems"
62 #define MODEM_ADDED "ModemAdded"
63 #define MODEM_REMOVED "ModemRemoved"
68 static DBusConnection *connection;
70 static GHashTable *modem_hash = NULL;
72 static GHashTable *network_hash;
76 struct connman_device *device;
81 gboolean pending_online;
82 dbus_bool_t requested_online;
85 /* org.ofono.ConnectionManager properties */
88 dbus_bool_t roaming_allowed;
90 connman_bool_t registered;
91 connman_bool_t roaming;
92 uint8_t strength, has_strength;
96 struct connman_network *network;
98 enum connman_ipconfig_method method;
99 struct connman_ipaddress ipaddress;
102 static int modem_probe(struct connman_device *device)
104 DBG("device %p", device);
109 static void modem_remove(struct connman_device *device)
111 DBG("device %p", device);
114 static int call_ofono(const char *path,
115 const char *interface, const char *method,
116 DBusPendingCallNotifyFunction notify, void *user_data,
117 DBusFreeFunction free_function,
120 DBusMessage *message;
121 DBusPendingCall *call;
125 DBG("path %s %s.%s", path, interface, method);
130 message = dbus_message_new_method_call(OFONO_SERVICE, path,
135 dbus_message_set_auto_start(message, FALSE);
138 ok = dbus_message_append_args_valist(message, type, va);
144 if (dbus_connection_send_with_reply(connection, message,
145 &call, TIMEOUT) == FALSE) {
146 connman_error("Failed to call %s.%s", interface, method);
147 dbus_message_unref(message);
152 connman_error("D-Bus connection not available");
153 dbus_message_unref(message);
157 dbus_pending_call_set_notify(call, notify, user_data, free_function);
159 dbus_message_unref(message);
164 static void set_property_reply(DBusPendingCall *call, void *user_data)
168 char const *name = user_data;
172 dbus_error_init(&error);
174 reply = dbus_pending_call_steal_reply(call);
176 if (dbus_set_error_from_message(&error, reply)) {
177 connman_error("SetProperty(%s) %s %s", name,
178 error.name, error.message);
179 dbus_error_free(&error);
182 dbus_message_unref(reply);
184 dbus_pending_call_unref(call);
187 static int set_property(const char *path, const char *interface,
188 const char *property, int type, void *value,
189 DBusPendingCallNotifyFunction notify, void *user_data,
190 DBusFreeFunction free_function)
192 DBusMessage *message;
193 DBusMessageIter iter;
194 DBusPendingCall *call;
196 DBG("path %s %s.%s", path, interface, property);
198 g_assert(notify == NULL ? free_function == NULL : 1);
203 message = dbus_message_new_method_call(OFONO_SERVICE, path,
204 interface, SET_PROPERTY);
208 dbus_message_set_auto_start(message, FALSE);
210 dbus_message_iter_init_append(message, &iter);
211 connman_dbus_property_append_basic(&iter, property, type, value);
213 if (dbus_connection_send_with_reply(connection, message,
214 &call, TIMEOUT) == FALSE) {
215 connman_error("Failed to change \"%s\" property on %s",
216 property, interface);
217 dbus_message_unref(message);
222 connman_error("D-Bus connection not available");
223 dbus_message_unref(message);
227 if (notify == NULL) {
228 notify = set_property_reply;
229 user_data = (void *)property;
230 free_function = NULL;
233 dbus_pending_call_set_notify(call, notify, user_data, free_function);
235 dbus_message_unref(message);
240 static void update_modem_online(struct modem_data *modem,
241 connman_bool_t online)
243 DBG("modem %p path %s online %d", modem, modem->path, online);
245 modem->online = online;
246 modem->requested_online = online;
247 modem->pending_online = FALSE;
250 connman_device_set_powered(modem->device, online);
253 static void set_online_reply(DBusPendingCall *call, void *user_data)
255 struct modem_data *modem;
260 DBG("path %s", (char *)user_data);
262 if (modem_hash == NULL)
265 modem = g_hash_table_lookup(modem_hash, user_data);
269 reply = dbus_pending_call_steal_reply(call);
271 dbus_error_init(&error);
273 if (dbus_set_error_from_message(&error, reply)) {
274 connman_error("SetProperty(Online) %s %s",
275 error.name, error.message);
276 dbus_error_free(&error);
278 result = modem->online;
280 result = modem->requested_online;
282 if (modem->pending_online)
283 update_modem_online(modem, result);
285 dbus_message_unref(reply);
287 dbus_pending_call_unref(call);
290 static int modem_change_online(char const *path, dbus_bool_t online)
292 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
297 if (modem->online == online)
300 modem->requested_online = online;
302 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
303 DBUS_TYPE_BOOLEAN, &online,
305 (void *)g_strdup(path), g_free);
308 static int modem_enable(struct connman_device *device)
310 const char *path = connman_device_get_string(device, "Path");
312 DBG("device %p, path, %s", device, path);
314 return modem_change_online(path, TRUE);
317 static int modem_disable(struct connman_device *device)
319 const char *path = connman_device_get_string(device, "Path");
321 DBG("device %p path %s", device, path);
323 return modem_change_online(path, FALSE);
326 static struct connman_device_driver modem_driver = {
328 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
329 .probe = modem_probe,
330 .remove = modem_remove,
331 .enable = modem_enable,
332 .disable = modem_disable,
335 static void modem_remove_device(struct modem_data *modem)
337 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
339 if (modem->device == NULL)
342 connman_device_remove_all_networks(modem->device);
343 connman_device_unregister(modem->device);
344 connman_device_unref(modem->device);
346 modem->device = NULL;
349 static void remove_modem(gpointer data)
351 struct modem_data *modem = data;
353 modem_remove_device(modem);
360 static void remove_network(gpointer data)
362 struct network_info *info = data;
364 connman_network_unref(info->network);
367 static char *get_ident(const char *path)
374 pos = strrchr(path, '/');
381 static void create_service(struct connman_network *network)
388 path = connman_network_get_string(network, "Path");
390 group = get_ident(path);
392 connman_network_set_group(network, group);
395 static int network_probe(struct connman_network *network)
400 static gboolean pending_network_is_available(struct connman_network *network)
402 /* Modem or network may be removed */
403 if (network == NULL || connman_network_get_device(network) == NULL) {
404 DBG("Modem or network was removed");
411 static void set_connected(struct network_info *info,
412 connman_bool_t connected);
414 static void set_active_reply(DBusPendingCall *call, void *user_data)
416 char const *path = user_data;
419 struct network_info *info;
421 info = g_hash_table_lookup(network_hash, path);
423 DBG("path %s network %p", path, info->network);
425 reply = dbus_pending_call_steal_reply(call);
427 if (!pending_network_is_available(info->network))
430 dbus_error_init(&error);
432 if (dbus_set_error_from_message(&error, reply)) {
433 connman_error("SetProperty(Active) %s %s",
434 error.name, error.message);
436 if (connman_network_get_index(info->network) < 0)
437 connman_network_set_error(info->network,
438 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
440 dbus_error_free(&error);
441 } else if (connman_network_get_index(info->network) >= 0)
442 set_connected(info, TRUE);
445 dbus_message_unref(reply);
447 dbus_pending_call_unref(call);
450 static int set_network_active(struct connman_network *network)
452 dbus_bool_t value = TRUE;
453 const char *path = connman_network_get_string(network, "Path");
455 DBG("network %p, path %s", network, path);
457 return set_property(path, OFONO_CONTEXT_INTERFACE,
458 "Active", DBUS_TYPE_BOOLEAN, &value,
459 set_active_reply, g_strdup(path), g_free);
462 static int set_network_inactive(struct connman_network *network)
465 dbus_bool_t value = FALSE;
466 const char *path = connman_network_get_string(network, "Path");
468 DBG("network %p, path %s", network, path);
470 err = set_property(path, OFONO_CONTEXT_INTERFACE,
471 "Active", DBUS_TYPE_BOOLEAN, &value,
474 if (err == -EINPROGRESS)
480 static int network_connect(struct connman_network *network)
482 struct connman_device *device;
483 struct modem_data *modem;
485 DBG("network %p", network);
487 device = connman_network_get_device(network);
491 modem = connman_device_get_data(device);
495 if (modem->registered == FALSE)
498 if (modem->powered == FALSE)
501 if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
504 return set_network_active(network);
507 static int network_disconnect(struct connman_network *network)
509 DBG("network %p", network);
511 if (connman_network_get_index(network) < 0)
514 connman_network_set_associating(network, FALSE);
516 return set_network_inactive(network);
519 static void network_remove(struct connman_network *network)
521 char const *path = connman_network_get_string(network, "Path");
523 DBG("network %p path %s", network, path);
525 g_hash_table_remove(network_hash, path);
528 static struct connman_network_driver network_driver = {
530 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
531 .probe = network_probe,
532 .remove = network_remove,
533 .connect = network_connect,
534 .disconnect = network_disconnect,
537 static void update_settings(DBusMessageIter *array,
538 struct network_info *info);
540 static int add_network(struct connman_device *device,
541 const char *path, DBusMessageIter *dict)
543 struct modem_data *modem = connman_device_get_data(device);
544 struct connman_network *network;
545 struct network_info *info;
547 const char *hash_path;
548 char const *operator;
549 dbus_bool_t active = FALSE;
551 DBG("modem %p device %p path %s", modem, device, path);
553 ident = get_ident(path);
555 network = connman_device_get_network(device, ident);
559 info = g_hash_table_lookup(network_hash, path);
561 DBG("path %p already exists with device %p", path,
562 connman_network_get_device(info->network));
563 if (connman_network_get_device(info->network))
565 g_hash_table_remove(network_hash, path);
568 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
572 info = g_try_new0(struct network_info, 1);
574 connman_network_unref(network);
578 connman_ipaddress_clear(&info->ipaddress);
579 info->network = network;
581 connman_network_set_string(network, "Path", path);
582 hash_path = connman_network_get_string(network, "Path");
583 if (hash_path == NULL)
586 create_service(network);
588 connman_network_ref(network);
589 g_hash_table_insert(network_hash, (char *) hash_path, info);
591 connman_network_set_available(network, TRUE);
592 connman_network_set_index(network, -1);
594 operator = connman_device_get_string(device, "Operator");
596 connman_network_set_name(network, operator);
598 if (modem->has_strength)
599 connman_network_set_strength(network, modem->strength);
601 connman_network_set_roaming(network, modem->roaming);
603 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
604 DBusMessageIter entry, value;
607 dbus_message_iter_recurse(dict, &entry);
608 dbus_message_iter_get_basic(&entry, &key);
610 dbus_message_iter_next(&entry);
611 dbus_message_iter_recurse(&entry, &value);
613 if (g_str_equal(key, "Type")) {
616 dbus_message_iter_get_basic(&value, &type);
617 if (g_strcmp0(type, "internet") != 0) {
618 DBG("path %p type %s", path, type);
621 } else if (g_str_equal(key, "Settings"))
622 update_settings(&value, info);
623 else if (g_str_equal(key, "Active") == TRUE)
624 dbus_message_iter_get_basic(&value, &active);
626 dbus_message_iter_next(dict);
629 if (connman_device_add_network(device, network) != 0)
632 /* Connect only if requested to do so */
633 if (active && connman_network_get_connecting(network) == TRUE)
634 set_connected(info, active);
639 connman_network_unref(network);
640 g_hash_table_remove(network_hash, hash_path);
644 static void check_networks_reply(DBusPendingCall *call, void *user_data)
646 char *path = user_data;
647 struct modem_data *modem;
649 DBusMessageIter array, entry, value, properties;
651 DBG("path %s", path);
653 modem = g_hash_table_lookup(modem_hash, path);
656 if (modem->device == NULL)
659 reply = dbus_pending_call_steal_reply(call);
661 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
664 dbus_message_iter_init(reply, &array);
666 dbus_message_iter_recurse(&array, &entry);
668 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
669 char const *network_path;
671 dbus_message_iter_recurse(&entry, &value);
672 dbus_message_iter_get_basic(&value, &network_path);
674 dbus_message_iter_next(&value);
675 dbus_message_iter_recurse(&value, &properties);
677 add_network(modem->device, network_path, &properties);
679 dbus_message_iter_next(&entry);
683 dbus_message_unref(reply);
685 dbus_pending_call_unref(call);
688 static void check_networks(struct modem_data *modem)
690 char const *path = modem->path;
692 DBG("modem %p path %s", modem, path);
694 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
695 check_networks_reply, g_strdup(path), g_free,
699 static void modem_clear_network_errors(struct modem_data *modem)
701 struct connman_device *device = modem->device;
708 g_hash_table_iter_init(&i, network_hash);
710 while (g_hash_table_iter_next(&i, NULL, &value)) {
711 struct network_info *info = value;
713 if (connman_network_get_device(info->network) == device)
714 connman_network_clear_error(info->network);
718 static void modem_operator_name_changed(struct modem_data *modem,
721 struct connman_device *device = modem->device;
728 connman_device_set_string(device, "Operator", name);
730 for (g_hash_table_iter_init(&i, network_hash);
731 g_hash_table_iter_next(&i, NULL, &value);) {
732 struct network_info *info = value;
734 if (connman_network_get_device(info->network) == device) {
735 connman_network_set_name(info->network, name);
736 connman_network_update(info->network);
741 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
743 struct connman_device *device = modem->device;
747 modem->strength = strength;
748 modem->has_strength = TRUE;
753 for (g_hash_table_iter_init(&i, network_hash);
754 g_hash_table_iter_next(&i, NULL, &value);) {
755 struct network_info *info = value;
757 if (connman_network_get_device(info->network) == device) {
758 connman_network_set_strength(info->network, strength);
759 connman_network_update(info->network);
764 static void modem_roaming_changed(struct modem_data *modem,
767 struct connman_device *device = modem->device;
768 connman_bool_t roaming = FALSE;
769 connman_bool_t registered = FALSE;
770 connman_bool_t was_roaming = modem->roaming;
774 if (g_str_equal(status, "roaming"))
776 else if (g_str_equal(status, "registered"))
779 registered = registered || roaming;
781 if (modem->roaming == roaming && modem->registered == registered)
784 modem->registered = registered;
785 modem->roaming = roaming;
787 if (roaming == was_roaming)
793 g_hash_table_iter_init(&i, network_hash);
795 while (g_hash_table_iter_next(&i, NULL, &value)) {
796 struct network_info *info = value;
798 if (connman_network_get_device(info->network) == device) {
799 connman_network_set_roaming(info->network, roaming);
800 connman_network_update(info->network);
805 static void modem_registration_removed(struct modem_data *modem)
807 modem->registered = FALSE;
808 modem->roaming = FALSE;
811 static void modem_registration_changed(struct modem_data *modem,
812 DBusMessageIter *entry)
814 DBusMessageIter iter;
817 connman_uint8_t strength;
818 char const *name, *status, *mcc_s;
820 dbus_message_iter_get_basic(entry, &key);
824 dbus_message_iter_next(entry);
826 dbus_message_iter_recurse(entry, &iter);
828 type = dbus_message_iter_get_arg_type(&iter);
829 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
832 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
833 dbus_message_iter_get_basic(&iter, &name);
834 modem_operator_name_changed(modem, name);
835 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
836 dbus_message_iter_get_basic(&iter, &strength);
837 modem_strength_changed(modem, strength);
838 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
839 dbus_message_iter_get_basic(&iter, &status);
840 modem_roaming_changed(modem, status);
841 } else if (g_str_equal(key, "MobileCountryCode") &&
842 type == DBUS_TYPE_STRING) {
846 dbus_message_iter_get_basic(&iter, &mcc_s);
852 alpha2 = mcc_country_codes[mcc - 200];
853 connman_technology_set_regdom(alpha2);
858 static gboolean reg_changed(DBusConnection *connection,
859 DBusMessage *message, void *user_data)
861 const char *path = dbus_message_get_path(message);
862 struct modem_data *modem;
863 DBusMessageIter iter;
865 DBG("path %s", path);
867 modem = g_hash_table_lookup(modem_hash, path);
871 if (dbus_message_iter_init(message, &iter))
872 modem_registration_changed(modem, &iter);
877 static void check_registration_reply(DBusPendingCall *call, void *user_data)
879 char const *path = user_data;
880 struct modem_data *modem;
882 DBusMessageIter array, dict, entry;
884 DBG("path %s", path);
886 modem = g_hash_table_lookup(modem_hash, path);
890 reply = dbus_pending_call_steal_reply(call);
892 if (dbus_message_iter_init(reply, &array) == FALSE)
895 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
898 dbus_message_iter_recurse(&array, &dict);
899 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
900 dbus_message_iter_recurse(&dict, &entry);
901 modem_registration_changed(modem, &entry);
902 dbus_message_iter_next(&dict);
906 dbus_message_unref(reply);
908 dbus_pending_call_unref(call);
911 static void check_registration(struct modem_data *modem)
913 char const *path = modem->path;
915 DBG("modem %p path %s", modem, path);
917 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
918 check_registration_reply, g_strdup(path), g_free,
922 static void modem_gprs_changed(struct modem_data *modem,
923 DBusMessageIter *entry)
925 DBusMessageIter iter;
930 dbus_message_iter_get_basic(entry, &key);
934 dbus_message_iter_next(entry);
936 dbus_message_iter_recurse(entry, &iter);
938 type = dbus_message_iter_get_arg_type(&iter);
940 if (type != DBUS_TYPE_BOOLEAN)
943 dbus_message_iter_get_basic(&iter, &value);
945 if (g_str_equal(key, "Attached") == TRUE) {
946 DBG("Attached %d", value);
948 modem->attached = value;
951 modem_clear_network_errors(modem);
952 } else if (g_str_equal(key, "Powered") == TRUE) {
953 DBG("Powered %d", value);
955 modem->powered = value;
956 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
957 DBG("RoamingAllowed %d", value);
959 modem->roaming_allowed = value;
963 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
965 char const *path = user_data;
966 struct modem_data *modem;
968 DBusMessageIter array, dict, entry;
970 DBG("path %s", path);
972 modem = g_hash_table_lookup(modem_hash, path);
976 reply = dbus_pending_call_steal_reply(call);
978 if (dbus_message_iter_init(reply, &array) == FALSE)
981 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
984 dbus_message_iter_recurse(&array, &dict);
985 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
986 dbus_message_iter_recurse(&dict, &entry);
987 modem_gprs_changed(modem, &entry);
988 dbus_message_iter_next(&dict);
992 dbus_message_unref(reply);
994 dbus_pending_call_unref(call);
996 check_networks(modem);
999 static void check_gprs(struct modem_data *modem)
1001 char const *path = modem->path;
1003 DBG("modem %p path %s", modem, path);
1005 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
1006 check_gprs_reply, g_strdup(path), g_free,
1010 static void add_device(const char *path, const char *imsi)
1012 struct modem_data *modem;
1013 struct connman_device *device;
1015 DBG("path %s imsi %s", path, imsi);
1023 modem = g_hash_table_lookup(modem_hash, path);
1027 if (modem->device) {
1028 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1031 modem_remove_device(modem);
1034 if (strlen(imsi) == 0)
1037 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1041 connman_device_set_ident(device, imsi);
1043 connman_device_set_string(device, "Path", path);
1045 connman_device_set_data(device, modem);
1047 if (connman_device_register(device) < 0) {
1048 connman_device_unref(device);
1052 modem->device = device;
1055 check_registration(modem);
1057 if (modem->has_gprs)
1061 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1063 const char *path = user_data;
1064 const char *imsi = NULL;
1066 DBusMessageIter array, dict;
1068 DBG("path %s", path);
1070 reply = dbus_pending_call_steal_reply(call);
1072 if (dbus_message_iter_init(reply, &array) == FALSE)
1075 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1078 dbus_message_iter_recurse(&array, &dict);
1080 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1081 DBusMessageIter entry, value;
1084 dbus_message_iter_recurse(&dict, &entry);
1085 dbus_message_iter_get_basic(&entry, &key);
1087 dbus_message_iter_next(&entry);
1088 dbus_message_iter_recurse(&entry, &value);
1090 if (g_str_equal(key, "SubscriberIdentity")) {
1091 dbus_message_iter_get_basic(&value, &imsi);
1092 add_device(path, imsi);
1095 dbus_message_iter_next(&dict);
1099 dbus_message_unref(reply);
1101 dbus_pending_call_unref(call);
1104 static void get_imsi(const char *path)
1106 DBG("path %s", path);
1108 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1109 sim_properties_reply, g_strdup(path), g_free,
1113 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1115 DBG("path %s powered %d", path, powered);
1117 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1118 DBUS_TYPE_BOOLEAN, &powered,
1122 static int modem_change_powered(const char *path, dbus_bool_t powered)
1124 DBG("path %s powered %d", path, powered);
1126 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1127 DBUS_TYPE_BOOLEAN, &powered,
1132 static gboolean modem_has_interface(DBusMessageIter *array,
1133 char const *interface)
1135 DBusMessageIter entry;
1137 dbus_message_iter_recurse(array, &entry);
1139 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1140 const char *element;
1142 dbus_message_iter_get_basic(&entry, &element);
1144 if (g_strcmp0(interface, element) == 0)
1147 dbus_message_iter_next(&entry);
1153 static gboolean modem_has_sim(DBusMessageIter *array)
1155 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1158 static gboolean modem_has_reg(DBusMessageIter *array)
1160 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1163 static gboolean modem_has_gprs(DBusMessageIter *array)
1165 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1168 static void add_modem(const char *path, DBusMessageIter *prop)
1170 struct modem_data *modem;
1171 dbus_bool_t powered = FALSE;
1172 dbus_bool_t online = FALSE;
1173 dbus_bool_t has_online = FALSE;
1174 dbus_bool_t locked = FALSE;
1175 gboolean has_sim = FALSE;
1176 gboolean has_reg = FALSE;
1177 gboolean has_gprs = FALSE;
1179 modem = g_hash_table_lookup(modem_hash, path);
1184 modem = g_try_new0(struct modem_data, 1);
1188 modem->path = g_strdup(path);
1189 modem->device = NULL;
1190 modem->available = TRUE;
1192 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1194 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1195 DBusMessageIter entry, value;
1198 dbus_message_iter_recurse(prop, &entry);
1199 dbus_message_iter_get_basic(&entry, &key);
1201 dbus_message_iter_next(&entry);
1202 dbus_message_iter_recurse(&entry, &value);
1204 if (g_str_equal(key, "Powered") == TRUE)
1205 dbus_message_iter_get_basic(&value, &powered);
1206 else if (g_str_equal(key, "Lockdown") == TRUE)
1207 dbus_message_iter_get_basic(&value, &locked);
1208 else if (g_str_equal(key, "Online") == TRUE) {
1210 dbus_message_iter_get_basic(&value, &online);
1211 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1212 has_sim = modem_has_sim(&value);
1213 has_reg = modem_has_reg(&value);
1214 has_gprs = modem_has_gprs(&value);
1217 dbus_message_iter_next(prop);
1224 modem_change_powered(path, TRUE);
1226 modem->has_sim = has_sim;
1227 modem->has_reg = has_reg;
1228 modem->has_gprs = has_gprs;
1230 update_modem_online(modem, online);
1236 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1240 DBusMessageIter array, dict;
1244 reply = dbus_pending_call_steal_reply(call);
1246 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1249 dbus_error_init(&error);
1251 if (dbus_set_error_from_message(&error, reply)) {
1252 connman_error("ModemManager.GetModems() %s %s",
1253 error.name, error.message);
1254 dbus_error_free(&error);
1258 if (dbus_message_iter_init(reply, &array) == FALSE)
1261 dbus_message_iter_recurse(&array, &dict);
1263 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1264 DBusMessageIter value, properties;
1265 const char *modem_path;
1267 dbus_message_iter_recurse(&dict, &value);
1268 dbus_message_iter_get_basic(&value, &modem_path);
1270 dbus_message_iter_next(&value);
1271 dbus_message_iter_recurse(&value, &properties);
1274 add_modem(modem_path, &properties);
1276 dbus_message_iter_next(&dict);
1280 dbus_message_unref(reply);
1282 dbus_pending_call_unref(call);
1285 static void ofono_connect(DBusConnection *connection, void *user_data)
1287 DBG("connection %p", connection);
1289 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1290 g_free, remove_modem);
1292 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1293 NULL, remove_network);
1295 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1296 manager_modems_reply, NULL, NULL,
1300 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1302 DBG("connection %p", connection);
1304 if (modem_hash == NULL)
1307 g_hash_table_destroy(modem_hash);
1312 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1315 const char *path = dbus_message_get_path(message);
1316 struct modem_data *modem;
1317 DBusMessageIter iter, value;
1320 DBG("path %s", path);
1322 modem = g_hash_table_lookup(modem_hash, path);
1326 if (dbus_message_iter_init(message, &iter) == FALSE)
1329 dbus_message_iter_get_basic(&iter, &key);
1331 dbus_message_iter_next(&iter);
1332 dbus_message_iter_recurse(&iter, &value);
1334 if (g_str_equal(key, "Powered") == TRUE) {
1335 dbus_bool_t powered;
1337 dbus_message_iter_get_basic(&value, &powered);
1338 if (powered == TRUE)
1341 modem->has_sim = FALSE;
1342 modem->has_reg = FALSE;
1343 modem->has_gprs = FALSE;
1345 modem_remove_device(modem);
1346 } else if (g_str_equal(key, "Online") == TRUE) {
1349 dbus_message_iter_get_basic(&value, &online);
1351 update_modem_online(modem, online);
1352 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1355 dbus_message_iter_get_basic(&value, &locked);
1358 modem_change_powered(path, TRUE);
1360 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1361 gboolean has_sim = modem_has_sim(&value);
1362 gboolean has_reg = modem_has_reg(&value);
1363 gboolean had_reg = modem->has_reg;
1364 gboolean has_gprs = modem_has_gprs(&value);
1365 gboolean had_gprs = modem->has_gprs;
1367 modem->has_sim = has_sim;
1368 modem->has_reg = has_reg;
1369 modem->has_gprs = has_gprs;
1371 if (modem->device == NULL) {
1373 get_imsi(modem->path);
1374 } else if (!has_sim) {
1375 modem_remove_device(modem);
1377 if (has_reg && !had_reg)
1378 check_registration(modem);
1379 else if (had_reg && !has_reg)
1380 modem_registration_removed(modem);
1382 if (has_gprs && !had_gprs) {
1383 gprs_change_powered(modem->path, TRUE);
1392 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1395 const char *path = dbus_message_get_path(message);
1396 struct modem_data *modem;
1397 DBusMessageIter iter, value;
1400 DBG("path %s", path);
1402 modem = g_hash_table_lookup(modem_hash, path);
1406 if (dbus_message_iter_init(message, &iter) == FALSE)
1409 dbus_message_iter_get_basic(&iter, &key);
1411 dbus_message_iter_next(&iter);
1412 dbus_message_iter_recurse(&iter, &value);
1414 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1417 dbus_message_iter_get_basic(&value, &imsi);
1419 add_device(path, imsi);
1420 } else if (g_str_equal(key, "Present") == TRUE) {
1421 dbus_bool_t present;
1423 dbus_message_iter_get_basic(&value, &present);
1428 if (modem->device != NULL)
1429 modem_remove_device(modem);
1431 modem->has_gprs = FALSE;
1432 modem->has_reg = FALSE;
1438 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1441 const char *path = dbus_message_get_path(message);
1442 struct modem_data *modem;
1443 DBusMessageIter iter;
1445 DBG("path %s", path);
1447 modem = g_hash_table_lookup(modem_hash, path);
1451 if (dbus_message_iter_init(message, &iter))
1452 modem_gprs_changed(modem, &iter);
1457 static gboolean context_added(DBusConnection *connection,
1458 DBusMessage *message, void *user_data)
1460 const char *path = dbus_message_get_path(message);
1461 const char *network_path;
1462 struct modem_data *modem;
1463 DBusMessageIter iter, properties;
1465 DBG("path %s", path);
1467 modem = g_hash_table_lookup(modem_hash, path);
1468 if (modem == NULL || modem->device == NULL)
1471 if (dbus_message_iter_init(message, &iter) == FALSE)
1474 dbus_message_iter_get_basic(&iter, &network_path);
1476 dbus_message_iter_next(&iter);
1477 dbus_message_iter_recurse(&iter, &properties);
1479 add_network(modem->device, network_path, &properties);
1484 static gboolean context_removed(DBusConnection *connection,
1485 DBusMessage *message, void *user_data)
1487 const char *path = dbus_message_get_path(message);
1488 const char *network_path, *identifier;
1489 struct modem_data *modem;
1490 struct network_info *info;
1491 DBusMessageIter iter;
1493 DBG("path %s", path);
1495 modem = g_hash_table_lookup(modem_hash, path);
1496 if (modem == NULL || modem->device == NULL)
1499 if (dbus_message_iter_init(message, &iter) == FALSE)
1502 dbus_message_iter_get_basic(&iter, &network_path);
1504 info = g_hash_table_lookup(network_hash, network_path);
1508 identifier = connman_network_get_identifier(info->network);
1509 connman_device_remove_network(modem->device, identifier);
1514 static gboolean modem_added(DBusConnection *connection,
1515 DBusMessage *message, void *user_data)
1517 DBusMessageIter iter, properties;
1518 const char *modem_path;
1522 if (dbus_message_iter_init(message, &iter) == FALSE)
1525 dbus_message_iter_get_basic(&iter, &modem_path);
1527 dbus_message_iter_next(&iter);
1528 dbus_message_iter_recurse(&iter, &properties);
1530 add_modem(modem_path, &properties);
1535 static gboolean modem_removed(DBusConnection *connection,
1536 DBusMessage *message, void *user_data)
1538 DBusMessageIter iter;
1539 const char *modem_path;
1543 if (dbus_message_iter_init(message, &iter) == FALSE)
1546 dbus_message_iter_get_basic(&iter, &modem_path);
1548 g_hash_table_remove(modem_hash, modem_path);
1554 static void get_dns(DBusMessageIter *array, struct network_info *info)
1556 DBusMessageIter entry;
1557 gchar *nameservers = NULL, *nameservers_old = NULL;
1562 dbus_message_iter_recurse(array, &entry);
1564 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1567 dbus_message_iter_get_basic(&entry, &dns);
1571 if (nameservers == NULL) {
1573 nameservers = g_strdup(dns);
1576 nameservers_old = nameservers;
1577 nameservers = g_strdup_printf("%s %s",
1578 nameservers_old, dns);
1579 g_free(nameservers_old);
1582 dbus_message_iter_next(&entry);
1585 connman_network_set_nameservers(info->network, nameservers);
1587 g_free(nameservers);
1590 static void update_settings(DBusMessageIter *array,
1591 struct network_info *info)
1593 DBusMessageIter dict;
1594 char *address = NULL, *netmask = NULL, *gateway = NULL;
1595 const char *interface = NULL;
1597 DBG("network %p", info->network);
1599 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1602 dbus_message_iter_recurse(array, &dict);
1604 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1605 DBusMessageIter entry, value;
1606 const char *key, *val;
1608 dbus_message_iter_recurse(&dict, &entry);
1609 dbus_message_iter_get_basic(&entry, &key);
1613 dbus_message_iter_next(&entry);
1614 dbus_message_iter_recurse(&entry, &value);
1616 if (g_str_equal(key, "Interface") == TRUE) {
1619 dbus_message_iter_get_basic(&value, &interface);
1621 DBG("interface %s", interface);
1623 index = connman_inet_ifindex(interface);
1625 connman_network_set_index(info->network, index);
1627 connman_error("Can not find interface %s",
1631 } else if (g_str_equal(key, "Method") == TRUE) {
1634 dbus_message_iter_get_basic(&value, &method);
1636 if (g_strcmp0(method, "static") == 0) {
1638 info->method = CONNMAN_IPCONFIG_METHOD_FIXED;
1639 } else if (g_strcmp0(method, "dhcp") == 0) {
1641 info->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1644 } else if (g_str_equal(key, "Address") == TRUE) {
1645 dbus_message_iter_get_basic(&value, &val);
1647 address = g_strdup(val);
1649 DBG("address %s", address);
1650 } else if (g_str_equal(key, "Netmask") == TRUE) {
1651 dbus_message_iter_get_basic(&value, &val);
1653 netmask = g_strdup(val);
1655 DBG("netmask %s", netmask);
1656 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1658 get_dns(&value, info);
1659 } else if (g_str_equal(key, "Gateway") == TRUE) {
1660 dbus_message_iter_get_basic(&value, &val);
1662 gateway = g_strdup(val);
1664 DBG("gateway %s", gateway);
1667 dbus_message_iter_next(&dict);
1671 if (info->method == CONNMAN_IPCONFIG_METHOD_FIXED) {
1672 connman_ipaddress_set_ipv4(&info->ipaddress, address,
1676 /* deactive, oFono send NULL inteface before deactive signal */
1677 if (interface == NULL)
1678 connman_network_set_index(info->network, -1);
1685 static void set_connected(struct network_info *info,
1686 connman_bool_t connected)
1688 DBG("network %p connected %d", info->network, connected);
1690 switch (info->method) {
1691 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1692 case CONNMAN_IPCONFIG_METHOD_OFF:
1693 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1694 case CONNMAN_IPCONFIG_METHOD_AUTO:
1697 case CONNMAN_IPCONFIG_METHOD_FIXED:
1698 connman_network_set_ipv4_method(info->network, info->method);
1699 connman_network_set_ipaddress(info->network, &info->ipaddress);
1703 case CONNMAN_IPCONFIG_METHOD_DHCP:
1704 connman_network_set_ipv4_method(info->network, info->method);
1709 connman_network_set_connected(info->network, connected);
1712 static gboolean context_changed(DBusConnection *connection,
1713 DBusMessage *message, void *user_data)
1715 const char *path = dbus_message_get_path(message);
1716 struct network_info *info;
1717 DBusMessageIter iter, value;
1720 DBG("path %s", path);
1722 info = g_hash_table_lookup(network_hash, path);
1726 if (!pending_network_is_available(info->network)) {
1727 remove_network(info->network);
1731 if (dbus_message_iter_init(message, &iter) == FALSE)
1734 dbus_message_iter_get_basic(&iter, &key);
1736 dbus_message_iter_next(&iter);
1737 dbus_message_iter_recurse(&iter, &value);
1741 if (g_str_equal(key, "Settings") == TRUE)
1742 update_settings(&value, info);
1743 else if (g_str_equal(key, "Active") == TRUE) {
1746 dbus_message_iter_get_basic(&value, &active);
1748 if (active == FALSE)
1749 set_connected(info, active);
1751 /* Connect only if requested to do so */
1752 if (active == TRUE &&
1753 connman_network_get_connecting(info->network) == TRUE)
1754 set_connected(info, active);
1761 static guint reg_watch;
1762 static guint sim_watch;
1763 static guint gprs_watch;
1764 static guint context_added_watch;
1765 static guint context_removed_watch;
1766 static guint modem_watch;
1767 static guint modem_added_watch;
1768 static guint modem_removed_watch;
1769 static guint context_watch;
1771 static int ofono_init(void)
1775 connection = connman_dbus_get_connection();
1776 if (connection == NULL)
1779 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1780 ofono_connect, ofono_disconnect, NULL, NULL);
1782 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1783 OFONO_REGISTRATION_INTERFACE,
1788 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1789 OFONO_GPRS_INTERFACE,
1794 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1795 OFONO_GPRS_INTERFACE,
1800 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1801 OFONO_GPRS_INTERFACE,
1806 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1807 OFONO_MODEM_INTERFACE,
1812 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1813 OFONO_SIM_INTERFACE,
1818 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1819 OFONO_MANAGER_INTERFACE,
1824 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1825 OFONO_MANAGER_INTERFACE,
1830 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1831 OFONO_CONTEXT_INTERFACE,
1836 if (watch == 0 || gprs_watch == 0 || context_added_watch == 0 ||
1837 context_removed_watch == 0 || modem_watch == 0 ||
1838 reg_watch == 0 || sim_watch == 0 ||
1839 modem_added_watch == 0 || modem_removed_watch == 0 ||
1840 context_watch == 0) {
1845 err = connman_network_driver_register(&network_driver);
1849 err = connman_device_driver_register(&modem_driver);
1851 connman_network_driver_unregister(&network_driver);
1858 g_dbus_remove_watch(connection, watch);
1859 g_dbus_remove_watch(connection, sim_watch);
1860 g_dbus_remove_watch(connection, reg_watch);
1861 g_dbus_remove_watch(connection, gprs_watch);
1862 g_dbus_remove_watch(connection, context_added_watch);
1863 g_dbus_remove_watch(connection, context_removed_watch);
1864 g_dbus_remove_watch(connection, modem_watch);
1865 g_dbus_remove_watch(connection, modem_added_watch);
1866 g_dbus_remove_watch(connection, modem_removed_watch);
1867 g_dbus_remove_watch(connection, context_watch);
1869 dbus_connection_unref(connection);
1874 static void ofono_exit(void)
1876 g_dbus_remove_watch(connection, watch);
1877 g_dbus_remove_watch(connection, sim_watch);
1878 g_dbus_remove_watch(connection, reg_watch);
1879 g_dbus_remove_watch(connection, gprs_watch);
1880 g_dbus_remove_watch(connection, context_added_watch);
1881 g_dbus_remove_watch(connection, context_removed_watch);
1882 g_dbus_remove_watch(connection, modem_watch);
1883 g_dbus_remove_watch(connection, modem_added_watch);
1884 g_dbus_remove_watch(connection, modem_removed_watch);
1885 g_dbus_remove_watch(connection, context_watch);
1887 ofono_disconnect(connection, NULL);
1889 connman_device_driver_unregister(&modem_driver);
1890 connman_network_driver_unregister(&network_driver);
1892 dbus_connection_unref(connection);
1895 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1896 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)