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;
97 struct connman_network *network;
99 enum connman_ipconfig_method ipv4_method;
100 struct connman_ipaddress ipv4_address;
102 enum connman_ipconfig_method ipv6_method;
103 struct connman_ipaddress ipv6_address;
106 static int modem_probe(struct connman_device *device)
108 DBG("device %p", device);
113 static void modem_remove(struct connman_device *device)
115 DBG("device %p", device);
118 static int call_ofono(const char *path,
119 const char *interface, const char *method,
120 DBusPendingCallNotifyFunction notify, void *user_data,
121 DBusFreeFunction free_function,
124 DBusMessage *message;
125 DBusPendingCall *call;
129 DBG("path %s %s.%s", path, interface, method);
134 message = dbus_message_new_method_call(OFONO_SERVICE, path,
139 dbus_message_set_auto_start(message, FALSE);
142 ok = dbus_message_append_args_valist(message, type, va);
148 if (dbus_connection_send_with_reply(connection, message,
149 &call, TIMEOUT) == FALSE) {
150 connman_error("Failed to call %s.%s", interface, method);
151 dbus_message_unref(message);
156 connman_error("D-Bus connection not available");
157 dbus_message_unref(message);
161 dbus_pending_call_set_notify(call, notify, user_data, free_function);
163 dbus_message_unref(message);
168 static void set_property_reply(DBusPendingCall *call, void *user_data)
172 char const *name = user_data;
176 dbus_error_init(&error);
178 reply = dbus_pending_call_steal_reply(call);
180 if (dbus_set_error_from_message(&error, reply)) {
181 connman_error("SetProperty(%s) %s %s", name,
182 error.name, error.message);
183 dbus_error_free(&error);
186 dbus_message_unref(reply);
188 dbus_pending_call_unref(call);
191 static int set_property(const char *path, const char *interface,
192 const char *property, int type, void *value,
193 DBusPendingCallNotifyFunction notify, void *user_data,
194 DBusFreeFunction free_function)
196 DBusMessage *message;
197 DBusMessageIter iter;
198 DBusPendingCall *call;
200 DBG("path %s %s.%s", path, interface, property);
202 g_assert(notify == NULL ? free_function == NULL : 1);
207 message = dbus_message_new_method_call(OFONO_SERVICE, path,
208 interface, SET_PROPERTY);
212 dbus_message_set_auto_start(message, FALSE);
214 dbus_message_iter_init_append(message, &iter);
215 connman_dbus_property_append_basic(&iter, property, type, value);
217 if (dbus_connection_send_with_reply(connection, message,
218 &call, TIMEOUT) == FALSE) {
219 connman_error("Failed to change \"%s\" property on %s",
220 property, interface);
221 dbus_message_unref(message);
226 connman_error("D-Bus connection not available");
227 dbus_message_unref(message);
231 if (notify == NULL) {
232 notify = set_property_reply;
233 user_data = (void *)property;
234 free_function = NULL;
237 dbus_pending_call_set_notify(call, notify, user_data, free_function);
239 dbus_message_unref(message);
244 static void update_modem_online(struct modem_data *modem,
245 connman_bool_t online)
247 DBG("modem %p path %s online %d", modem, modem->path, online);
249 modem->online = online;
250 modem->requested_online = online;
251 modem->pending_online = FALSE;
254 connman_device_set_powered(modem->device, online);
257 static void set_online_reply(DBusPendingCall *call, void *user_data)
259 struct modem_data *modem;
264 DBG("path %s", (char *)user_data);
266 if (modem_hash == NULL)
269 modem = g_hash_table_lookup(modem_hash, user_data);
273 reply = dbus_pending_call_steal_reply(call);
275 dbus_error_init(&error);
277 if (dbus_set_error_from_message(&error, reply)) {
278 connman_error("SetProperty(Online) %s %s",
279 error.name, error.message);
280 dbus_error_free(&error);
282 result = modem->online;
284 result = modem->requested_online;
286 if (modem->pending_online)
287 update_modem_online(modem, result);
289 dbus_message_unref(reply);
291 dbus_pending_call_unref(call);
294 static int modem_change_online(char const *path, dbus_bool_t online)
296 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
301 if (modem->online == online)
304 modem->requested_online = online;
306 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
307 DBUS_TYPE_BOOLEAN, &online,
309 (void *)g_strdup(path), g_free);
312 static int modem_enable(struct connman_device *device)
314 const char *path = connman_device_get_string(device, "Path");
316 DBG("device %p, path, %s", device, path);
318 return modem_change_online(path, TRUE);
321 static int modem_disable(struct connman_device *device)
323 const char *path = connman_device_get_string(device, "Path");
325 DBG("device %p path %s", device, path);
327 return modem_change_online(path, FALSE);
330 static struct connman_device_driver modem_driver = {
332 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
333 .probe = modem_probe,
334 .remove = modem_remove,
335 .enable = modem_enable,
336 .disable = modem_disable,
339 static void modem_remove_device(struct modem_data *modem)
341 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
343 if (modem->device == NULL)
346 connman_device_remove_all_networks(modem->device);
347 connman_device_unregister(modem->device);
348 connman_device_unref(modem->device);
350 modem->device = NULL;
353 static void remove_modem(gpointer data)
355 struct modem_data *modem = data;
357 modem_remove_device(modem);
360 g_free(modem->operator);
365 static void remove_network(gpointer data)
367 struct network_info *info = data;
369 connman_network_unref(info->network);
374 static char *get_ident(const char *path)
381 pos = strrchr(path, '/');
388 static void create_service(struct connman_network *network)
395 path = connman_network_get_string(network, "Path");
397 group = get_ident(path);
399 connman_network_set_group(network, group);
402 static int network_probe(struct connman_network *network)
407 static gboolean pending_network_is_available(struct connman_network *network)
409 /* Modem or network may be removed */
410 if (network == NULL || connman_network_get_device(network) == NULL) {
411 DBG("Modem or network was removed");
418 static void set_connected(struct network_info *info,
419 connman_bool_t connected)
421 DBG("network %p connected %d", info->network, connected);
423 switch (info->ipv4_method) {
424 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
425 case CONNMAN_IPCONFIG_METHOD_OFF:
426 case CONNMAN_IPCONFIG_METHOD_MANUAL:
427 case CONNMAN_IPCONFIG_METHOD_AUTO:
430 case CONNMAN_IPCONFIG_METHOD_FIXED:
431 connman_network_set_ipv4_method(info->network,
433 connman_network_set_ipaddress(info->network,
434 &info->ipv4_address);
438 case CONNMAN_IPCONFIG_METHOD_DHCP:
439 connman_network_set_ipv4_method(info->network,
445 switch (info->ipv6_method) {
446 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
447 case CONNMAN_IPCONFIG_METHOD_OFF:
448 case CONNMAN_IPCONFIG_METHOD_MANUAL:
449 case CONNMAN_IPCONFIG_METHOD_DHCP:
450 case CONNMAN_IPCONFIG_METHOD_AUTO:
453 case CONNMAN_IPCONFIG_METHOD_FIXED:
454 connman_network_set_ipv6_method(info->network,
456 connman_network_set_ipaddress(info->network,
457 &info->ipv6_address);
462 connman_network_set_connected(info->network, connected);
465 static void set_active_reply(DBusPendingCall *call, void *user_data)
467 char const *path = user_data;
470 struct network_info *info;
472 info = g_hash_table_lookup(network_hash, path);
474 reply = dbus_pending_call_steal_reply(call);
479 DBG("path %s network %p", path, info->network);
481 if (!pending_network_is_available(info->network))
484 dbus_error_init(&error);
486 if (dbus_set_error_from_message(&error, reply)) {
487 connman_error("SetProperty(Active) %s %s",
488 error.name, error.message);
490 if (connman_network_get_index(info->network) < 0)
491 connman_network_set_error(info->network,
492 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
494 dbus_error_free(&error);
495 } else if (connman_network_get_index(info->network) >= 0)
496 set_connected(info, TRUE);
499 dbus_message_unref(reply);
501 dbus_pending_call_unref(call);
504 static int set_network_active(struct connman_network *network)
506 dbus_bool_t value = TRUE;
507 const char *path = connman_network_get_string(network, "Path");
509 DBG("network %p, path %s", network, path);
511 return set_property(path, OFONO_CONTEXT_INTERFACE,
512 "Active", DBUS_TYPE_BOOLEAN, &value,
513 set_active_reply, g_strdup(path), g_free);
516 static int set_network_inactive(struct connman_network *network)
519 dbus_bool_t value = FALSE;
520 const char *path = connman_network_get_string(network, "Path");
522 DBG("network %p, path %s", network, path);
524 err = set_property(path, OFONO_CONTEXT_INTERFACE,
525 "Active", DBUS_TYPE_BOOLEAN, &value,
528 if (err == -EINPROGRESS)
534 static int network_connect(struct connman_network *network)
536 struct connman_device *device;
537 struct modem_data *modem;
539 DBG("network %p", network);
541 device = connman_network_get_device(network);
545 modem = connman_device_get_data(device);
549 if (modem->registered == FALSE)
552 if (modem->powered == FALSE)
555 if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
558 return set_network_active(network);
561 static int network_disconnect(struct connman_network *network)
563 DBG("network %p", network);
565 if (connman_network_get_index(network) < 0)
568 connman_network_set_associating(network, FALSE);
570 return set_network_inactive(network);
573 static void network_remove(struct connman_network *network)
575 char const *path = connman_network_get_string(network, "Path");
577 DBG("network %p path %s", network, path);
579 g_hash_table_remove(network_hash, path);
582 static struct connman_network_driver network_driver = {
584 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
585 .probe = network_probe,
586 .remove = network_remove,
587 .connect = network_connect,
588 .disconnect = network_disconnect,
591 static void get_dns(DBusMessageIter *array, struct network_info *info)
593 DBusMessageIter entry;
594 gchar *nameservers = NULL, *nameservers_old = NULL;
599 dbus_message_iter_recurse(array, &entry);
601 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
604 dbus_message_iter_get_basic(&entry, &dns);
608 if (nameservers == NULL) {
610 nameservers = g_strdup(dns);
613 nameservers_old = nameservers;
614 nameservers = g_strdup_printf("%s %s",
615 nameservers_old, dns);
616 g_free(nameservers_old);
619 dbus_message_iter_next(&entry);
622 connman_network_set_nameservers(info->network, nameservers);
627 static void update_ipv4_settings(DBusMessageIter *array,
628 struct network_info *info)
630 DBusMessageIter dict;
631 char *address = NULL, *netmask = NULL, *gateway = NULL;
632 const char *interface = NULL;
634 DBG("network %p", info->network);
636 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
639 dbus_message_iter_recurse(array, &dict);
641 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
642 DBusMessageIter entry, value;
643 const char *key, *val;
645 dbus_message_iter_recurse(&dict, &entry);
646 dbus_message_iter_get_basic(&entry, &key);
650 dbus_message_iter_next(&entry);
651 dbus_message_iter_recurse(&entry, &value);
653 if (g_str_equal(key, "Interface") == TRUE) {
656 dbus_message_iter_get_basic(&value, &interface);
658 DBG("interface %s", interface);
660 index = connman_inet_ifindex(interface);
662 connman_network_set_index(info->network, index);
664 connman_error("Can not find interface %s",
668 } else if (g_str_equal(key, "Method") == TRUE) {
671 dbus_message_iter_get_basic(&value, &method);
673 if (g_strcmp0(method, "static") == 0) {
676 CONNMAN_IPCONFIG_METHOD_FIXED;
677 } else if (g_strcmp0(method, "dhcp") == 0) {
680 CONNMAN_IPCONFIG_METHOD_DHCP;
683 } else if (g_str_equal(key, "Address") == TRUE) {
684 dbus_message_iter_get_basic(&value, &val);
686 address = g_strdup(val);
688 DBG("address %s", address);
689 } else if (g_str_equal(key, "Netmask") == TRUE) {
690 dbus_message_iter_get_basic(&value, &val);
692 netmask = g_strdup(val);
694 DBG("netmask %s", netmask);
695 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
697 get_dns(&value, info);
698 } else if (g_str_equal(key, "Gateway") == TRUE) {
699 dbus_message_iter_get_basic(&value, &val);
701 gateway = g_strdup(val);
703 DBG("gateway %s", gateway);
706 dbus_message_iter_next(&dict);
710 if (info->ipv4_method == CONNMAN_IPCONFIG_METHOD_FIXED) {
711 connman_ipaddress_set_ipv4(&info->ipv4_address, address,
715 /* deactive, oFono send NULL inteface before deactive signal */
716 if (interface == NULL)
717 connman_network_set_index(info->network, -1);
724 static void update_ipv6_settings(DBusMessageIter *array,
725 struct network_info *info)
727 DBusMessageIter dict;
728 char *address = NULL, *gateway = NULL;
729 unsigned char prefix_length;
730 const char *interface = NULL;
732 DBG("network %p", info->network);
734 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
737 dbus_message_iter_recurse(array, &dict);
739 info->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
741 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
742 DBusMessageIter entry, value;
743 const char *key, *val;
745 dbus_message_iter_recurse(&dict, &entry);
746 dbus_message_iter_get_basic(&entry, &key);
750 dbus_message_iter_next(&entry);
751 dbus_message_iter_recurse(&entry, &value);
753 if (g_str_equal(key, "Interface") == TRUE) {
756 dbus_message_iter_get_basic(&value, &interface);
758 DBG("interface %s", interface);
760 index = connman_inet_ifindex(interface);
762 connman_network_set_index(info->network, index);
764 connman_error("Can not find interface %s",
768 } else if (g_str_equal(key, "Address") == TRUE) {
769 dbus_message_iter_get_basic(&value, &val);
771 address = g_strdup(val);
773 DBG("address %s", address);
774 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
775 dbus_message_iter_get_basic(&value, &prefix_length);
777 DBG("prefix length %d", prefix_length);
778 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
780 get_dns(&value, info);
781 } else if (g_str_equal(key, "Gateway") == TRUE) {
782 dbus_message_iter_get_basic(&value, &val);
784 gateway = g_strdup(val);
786 DBG("gateway %s", gateway);
789 dbus_message_iter_next(&dict);
792 connman_ipaddress_set_ipv6(&info->ipv6_address, address,
793 prefix_length, gateway);
795 /* deactive, oFono send NULL inteface before deactive signal */
796 if (interface == NULL)
797 connman_network_set_index(info->network, -1);
803 static int add_network(struct connman_device *device,
804 const char *path, DBusMessageIter *dict)
806 struct modem_data *modem = connman_device_get_data(device);
807 struct connman_network *network;
808 struct network_info *info;
810 const char *hash_path;
811 dbus_bool_t active = FALSE;
813 DBG("modem %p device %p path %s", modem, device, path);
815 ident = get_ident(path);
817 network = connman_device_get_network(device, ident);
821 info = g_hash_table_lookup(network_hash, path);
823 DBG("path %p already exists with device %p", path,
824 connman_network_get_device(info->network));
825 if (connman_network_get_device(info->network))
827 g_hash_table_remove(network_hash, path);
830 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
834 info = g_try_new0(struct network_info, 1);
836 connman_network_unref(network);
840 connman_ipaddress_clear(&info->ipv4_address);
841 connman_ipaddress_clear(&info->ipv6_address);
842 info->network = network;
844 connman_network_set_string(network, "Path", path);
845 hash_path = connman_network_get_string(network, "Path");
846 if (hash_path == NULL) {
847 connman_network_unref(network);
852 create_service(network);
854 connman_network_ref(network);
855 g_hash_table_insert(network_hash, (char *) hash_path, info);
857 connman_network_set_available(network, TRUE);
858 connman_network_set_index(network, -1);
861 connman_network_set_name(network, modem->operator);
863 if (modem->has_strength)
864 connman_network_set_strength(network, modem->strength);
866 connman_network_set_roaming(network, modem->roaming);
868 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
869 DBusMessageIter entry, value;
872 dbus_message_iter_recurse(dict, &entry);
873 dbus_message_iter_get_basic(&entry, &key);
875 dbus_message_iter_next(&entry);
876 dbus_message_iter_recurse(&entry, &value);
878 if (g_str_equal(key, "Type")) {
881 dbus_message_iter_get_basic(&value, &type);
882 if (g_strcmp0(type, "internet") != 0) {
883 DBG("path %p type %s", path, type);
886 } else if (g_str_equal(key, "Settings"))
887 update_ipv4_settings(&value, info);
888 else if (g_str_equal(key, "IPv6.Settings"))
889 update_ipv6_settings(&value, info);
890 else if (g_str_equal(key, "Active") == TRUE)
891 dbus_message_iter_get_basic(&value, &active);
893 dbus_message_iter_next(dict);
896 if (connman_device_add_network(device, network) != 0)
899 /* Connect only if requested to do so */
900 if (active && connman_network_get_connecting(network) == TRUE)
901 set_connected(info, active);
906 connman_network_unref(network);
907 g_hash_table_remove(network_hash, hash_path);
911 static void check_networks_reply(DBusPendingCall *call, void *user_data)
913 char *path = user_data;
914 struct modem_data *modem;
916 DBusMessageIter array, entry, value, properties;
918 DBG("path %s", path);
920 modem = g_hash_table_lookup(modem_hash, path);
923 if (modem->device == NULL)
926 reply = dbus_pending_call_steal_reply(call);
928 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
931 dbus_message_iter_init(reply, &array);
933 dbus_message_iter_recurse(&array, &entry);
935 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
936 char const *network_path;
938 dbus_message_iter_recurse(&entry, &value);
939 dbus_message_iter_get_basic(&value, &network_path);
941 dbus_message_iter_next(&value);
942 dbus_message_iter_recurse(&value, &properties);
944 add_network(modem->device, network_path, &properties);
946 dbus_message_iter_next(&entry);
950 dbus_message_unref(reply);
952 dbus_pending_call_unref(call);
955 static void check_networks(struct modem_data *modem)
957 char const *path = modem->path;
959 DBG("modem %p path %s", modem, path);
961 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
962 check_networks_reply, g_strdup(path), g_free,
966 static void modem_clear_network_errors(struct modem_data *modem)
968 struct connman_device *device = modem->device;
975 g_hash_table_iter_init(&i, network_hash);
977 while (g_hash_table_iter_next(&i, NULL, &value)) {
978 struct network_info *info = value;
980 if (connman_network_get_device(info->network) == device)
981 connman_network_clear_error(info->network);
985 static void modem_operator_name_changed(struct modem_data *modem,
988 struct connman_device *device = modem->device;
995 if (modem->operator != NULL)
996 g_free(modem->operator);
997 modem->operator = g_strdup(name);
999 for (g_hash_table_iter_init(&i, network_hash);
1000 g_hash_table_iter_next(&i, NULL, &value);) {
1001 struct network_info *info = value;
1003 if (connman_network_get_device(info->network) == device) {
1004 connman_network_set_name(info->network, name);
1005 connman_network_update(info->network);
1010 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
1012 struct connman_device *device = modem->device;
1016 modem->strength = strength;
1017 modem->has_strength = TRUE;
1022 for (g_hash_table_iter_init(&i, network_hash);
1023 g_hash_table_iter_next(&i, NULL, &value);) {
1024 struct network_info *info = value;
1026 if (connman_network_get_device(info->network) == device) {
1027 connman_network_set_strength(info->network, strength);
1028 connman_network_update(info->network);
1033 static void modem_roaming_changed(struct modem_data *modem,
1036 struct connman_device *device = modem->device;
1037 connman_bool_t roaming = FALSE;
1038 connman_bool_t registered = FALSE;
1039 connman_bool_t was_roaming = modem->roaming;
1043 if (g_str_equal(status, "roaming"))
1045 else if (g_str_equal(status, "registered"))
1048 registered = registered || roaming;
1050 if (modem->roaming == roaming && modem->registered == registered)
1053 modem->registered = registered;
1054 modem->roaming = roaming;
1056 if (roaming == was_roaming)
1062 g_hash_table_iter_init(&i, network_hash);
1064 while (g_hash_table_iter_next(&i, NULL, &value)) {
1065 struct network_info *info = value;
1067 if (connman_network_get_device(info->network) == device) {
1068 connman_network_set_roaming(info->network, roaming);
1069 connman_network_update(info->network);
1074 static void modem_registration_removed(struct modem_data *modem)
1076 modem->registered = FALSE;
1077 modem->roaming = FALSE;
1080 static void modem_registration_changed(struct modem_data *modem,
1081 DBusMessageIter *entry)
1083 DBusMessageIter iter;
1086 connman_uint8_t strength;
1087 char const *name, *status, *mcc_s;
1089 dbus_message_iter_get_basic(entry, &key);
1093 dbus_message_iter_next(entry);
1095 dbus_message_iter_recurse(entry, &iter);
1097 type = dbus_message_iter_get_arg_type(&iter);
1098 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
1101 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
1102 dbus_message_iter_get_basic(&iter, &name);
1103 modem_operator_name_changed(modem, name);
1104 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
1105 dbus_message_iter_get_basic(&iter, &strength);
1106 modem_strength_changed(modem, strength);
1107 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
1108 dbus_message_iter_get_basic(&iter, &status);
1109 modem_roaming_changed(modem, status);
1110 } else if (g_str_equal(key, "MobileCountryCode") &&
1111 type == DBUS_TYPE_STRING) {
1115 dbus_message_iter_get_basic(&iter, &mcc_s);
1121 alpha2 = mcc_country_codes[mcc - 200];
1122 connman_technology_set_regdom(alpha2);
1127 static gboolean reg_changed(DBusConnection *connection,
1128 DBusMessage *message, void *user_data)
1130 const char *path = dbus_message_get_path(message);
1131 struct modem_data *modem;
1132 DBusMessageIter iter;
1134 DBG("path %s", path);
1136 modem = g_hash_table_lookup(modem_hash, path);
1140 if (dbus_message_iter_init(message, &iter))
1141 modem_registration_changed(modem, &iter);
1146 static void check_registration_reply(DBusPendingCall *call, void *user_data)
1148 char const *path = user_data;
1149 struct modem_data *modem;
1151 DBusMessageIter array, dict, entry;
1153 DBG("path %s", path);
1155 modem = g_hash_table_lookup(modem_hash, path);
1159 reply = dbus_pending_call_steal_reply(call);
1161 if (dbus_message_iter_init(reply, &array) == FALSE)
1164 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1167 dbus_message_iter_recurse(&array, &dict);
1168 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1169 dbus_message_iter_recurse(&dict, &entry);
1170 modem_registration_changed(modem, &entry);
1171 dbus_message_iter_next(&dict);
1175 dbus_message_unref(reply);
1177 dbus_pending_call_unref(call);
1180 static void check_registration(struct modem_data *modem)
1182 char const *path = modem->path;
1184 DBG("modem %p path %s", modem, path);
1186 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
1187 check_registration_reply, g_strdup(path), g_free,
1191 static void modem_gprs_changed(struct modem_data *modem,
1192 DBusMessageIter *entry)
1194 DBusMessageIter iter;
1199 dbus_message_iter_get_basic(entry, &key);
1203 dbus_message_iter_next(entry);
1205 dbus_message_iter_recurse(entry, &iter);
1207 type = dbus_message_iter_get_arg_type(&iter);
1209 if (type != DBUS_TYPE_BOOLEAN)
1212 dbus_message_iter_get_basic(&iter, &value);
1214 if (g_str_equal(key, "Attached") == TRUE) {
1215 DBG("Attached %d", value);
1217 modem->attached = value;
1220 modem_clear_network_errors(modem);
1221 } else if (g_str_equal(key, "Powered") == TRUE) {
1222 DBG("Powered %d", value);
1224 modem->powered = value;
1225 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
1226 DBG("RoamingAllowed %d", value);
1228 modem->roaming_allowed = value;
1232 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
1234 char const *path = user_data;
1235 struct modem_data *modem;
1237 DBusMessageIter array, dict, entry;
1239 DBG("path %s", path);
1241 modem = g_hash_table_lookup(modem_hash, path);
1245 reply = dbus_pending_call_steal_reply(call);
1247 if (dbus_message_iter_init(reply, &array) == FALSE)
1250 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1253 dbus_message_iter_recurse(&array, &dict);
1254 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1255 dbus_message_iter_recurse(&dict, &entry);
1256 modem_gprs_changed(modem, &entry);
1257 dbus_message_iter_next(&dict);
1261 dbus_message_unref(reply);
1263 dbus_pending_call_unref(call);
1265 check_networks(modem);
1268 static void check_gprs(struct modem_data *modem)
1270 char const *path = modem->path;
1272 DBG("modem %p path %s", modem, path);
1274 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
1275 check_gprs_reply, g_strdup(path), g_free,
1279 static void add_device(const char *path, const char *imsi)
1281 struct modem_data *modem;
1282 struct connman_device *device;
1284 DBG("path %s imsi %s", path, imsi);
1292 modem = g_hash_table_lookup(modem_hash, path);
1296 if (modem->device) {
1297 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1300 modem_remove_device(modem);
1303 if (strlen(imsi) == 0)
1306 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1310 connman_device_set_ident(device, imsi);
1312 connman_device_set_string(device, "Path", path);
1314 connman_device_set_data(device, modem);
1316 if (connman_device_register(device) < 0) {
1317 connman_device_unref(device);
1321 modem->device = device;
1324 check_registration(modem);
1326 if (modem->has_gprs)
1330 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1332 const char *path = user_data;
1333 const char *imsi = NULL;
1335 DBusMessageIter array, dict;
1337 DBG("path %s", path);
1339 reply = dbus_pending_call_steal_reply(call);
1341 if (dbus_message_iter_init(reply, &array) == FALSE)
1344 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1347 dbus_message_iter_recurse(&array, &dict);
1349 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1350 DBusMessageIter entry, value;
1353 dbus_message_iter_recurse(&dict, &entry);
1354 dbus_message_iter_get_basic(&entry, &key);
1356 dbus_message_iter_next(&entry);
1357 dbus_message_iter_recurse(&entry, &value);
1359 if (g_str_equal(key, "SubscriberIdentity")) {
1360 dbus_message_iter_get_basic(&value, &imsi);
1361 add_device(path, imsi);
1364 dbus_message_iter_next(&dict);
1368 dbus_message_unref(reply);
1370 dbus_pending_call_unref(call);
1373 static void get_imsi(const char *path)
1375 DBG("path %s", path);
1377 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1378 sim_properties_reply, g_strdup(path), g_free,
1382 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1384 DBG("path %s powered %d", path, powered);
1386 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1387 DBUS_TYPE_BOOLEAN, &powered,
1391 static int modem_change_powered(const char *path, dbus_bool_t powered)
1393 DBG("path %s powered %d", path, powered);
1395 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1396 DBUS_TYPE_BOOLEAN, &powered,
1401 static gboolean modem_has_interface(DBusMessageIter *array,
1402 char const *interface)
1404 DBusMessageIter entry;
1406 dbus_message_iter_recurse(array, &entry);
1408 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1409 const char *element;
1411 dbus_message_iter_get_basic(&entry, &element);
1413 if (g_strcmp0(interface, element) == 0)
1416 dbus_message_iter_next(&entry);
1422 static gboolean modem_has_sim(DBusMessageIter *array)
1424 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1427 static gboolean modem_has_reg(DBusMessageIter *array)
1429 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1432 static gboolean modem_has_gprs(DBusMessageIter *array)
1434 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1437 static void add_modem(const char *path, DBusMessageIter *prop)
1439 struct modem_data *modem;
1440 dbus_bool_t powered = FALSE;
1441 dbus_bool_t online = FALSE;
1442 dbus_bool_t locked = FALSE;
1443 gboolean has_sim = FALSE;
1444 gboolean has_reg = FALSE;
1445 gboolean has_gprs = FALSE;
1447 modem = g_hash_table_lookup(modem_hash, path);
1452 modem = g_try_new0(struct modem_data, 1);
1456 modem->path = g_strdup(path);
1457 modem->device = NULL;
1458 modem->available = TRUE;
1460 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1462 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1463 DBusMessageIter entry, value;
1466 dbus_message_iter_recurse(prop, &entry);
1467 dbus_message_iter_get_basic(&entry, &key);
1469 dbus_message_iter_next(&entry);
1470 dbus_message_iter_recurse(&entry, &value);
1472 if (g_str_equal(key, "Powered") == TRUE)
1473 dbus_message_iter_get_basic(&value, &powered);
1474 else if (g_str_equal(key, "Lockdown") == TRUE)
1475 dbus_message_iter_get_basic(&value, &locked);
1476 else if (g_str_equal(key, "Interfaces") == TRUE) {
1477 has_sim = modem_has_sim(&value);
1478 has_reg = modem_has_reg(&value);
1479 has_gprs = modem_has_gprs(&value);
1482 dbus_message_iter_next(prop);
1489 modem_change_powered(path, TRUE);
1491 modem->has_sim = has_sim;
1492 modem->has_reg = has_reg;
1493 modem->has_gprs = has_gprs;
1495 update_modem_online(modem, online);
1501 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1505 DBusMessageIter array, dict;
1509 reply = dbus_pending_call_steal_reply(call);
1511 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1514 dbus_error_init(&error);
1516 if (dbus_set_error_from_message(&error, reply)) {
1517 connman_error("ModemManager.GetModems() %s %s",
1518 error.name, error.message);
1519 dbus_error_free(&error);
1523 if (dbus_message_iter_init(reply, &array) == FALSE)
1526 dbus_message_iter_recurse(&array, &dict);
1528 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1529 DBusMessageIter value, properties;
1530 const char *modem_path;
1532 dbus_message_iter_recurse(&dict, &value);
1533 dbus_message_iter_get_basic(&value, &modem_path);
1535 dbus_message_iter_next(&value);
1536 dbus_message_iter_recurse(&value, &properties);
1539 add_modem(modem_path, &properties);
1541 dbus_message_iter_next(&dict);
1545 dbus_message_unref(reply);
1547 dbus_pending_call_unref(call);
1550 static void ofono_connect(DBusConnection *connection, void *user_data)
1552 DBG("connection %p", connection);
1554 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1555 g_free, remove_modem);
1557 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1558 NULL, remove_network);
1560 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1561 manager_modems_reply, NULL, NULL,
1565 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1567 DBG("connection %p", connection);
1569 if (modem_hash == NULL)
1572 g_hash_table_destroy(modem_hash);
1577 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1580 const char *path = dbus_message_get_path(message);
1581 struct modem_data *modem;
1582 DBusMessageIter iter, value;
1585 DBG("path %s", path);
1587 modem = g_hash_table_lookup(modem_hash, path);
1591 if (dbus_message_iter_init(message, &iter) == FALSE)
1594 dbus_message_iter_get_basic(&iter, &key);
1596 dbus_message_iter_next(&iter);
1597 dbus_message_iter_recurse(&iter, &value);
1599 if (g_str_equal(key, "Powered") == TRUE) {
1600 dbus_bool_t powered;
1602 dbus_message_iter_get_basic(&value, &powered);
1603 if (powered == TRUE)
1606 modem->has_sim = FALSE;
1607 modem->has_reg = FALSE;
1608 modem->has_gprs = FALSE;
1610 modem_remove_device(modem);
1611 } else if (g_str_equal(key, "Online") == TRUE) {
1614 dbus_message_iter_get_basic(&value, &online);
1616 update_modem_online(modem, online);
1617 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1620 dbus_message_iter_get_basic(&value, &locked);
1623 modem_change_powered(path, TRUE);
1625 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1626 gboolean has_sim = modem_has_sim(&value);
1627 gboolean has_reg = modem_has_reg(&value);
1628 gboolean had_reg = modem->has_reg;
1629 gboolean has_gprs = modem_has_gprs(&value);
1630 gboolean had_gprs = modem->has_gprs;
1632 modem->has_sim = has_sim;
1633 modem->has_reg = has_reg;
1634 modem->has_gprs = has_gprs;
1636 if (modem->device == NULL) {
1638 get_imsi(modem->path);
1639 } else if (!has_sim) {
1640 modem_remove_device(modem);
1642 if (has_reg && !had_reg)
1643 check_registration(modem);
1644 else if (had_reg && !has_reg)
1645 modem_registration_removed(modem);
1647 if (has_gprs && !had_gprs) {
1648 gprs_change_powered(modem->path, TRUE);
1657 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1660 const char *path = dbus_message_get_path(message);
1661 struct modem_data *modem;
1662 DBusMessageIter iter, value;
1665 DBG("path %s", path);
1667 modem = g_hash_table_lookup(modem_hash, path);
1671 if (dbus_message_iter_init(message, &iter) == FALSE)
1674 dbus_message_iter_get_basic(&iter, &key);
1676 dbus_message_iter_next(&iter);
1677 dbus_message_iter_recurse(&iter, &value);
1679 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1682 dbus_message_iter_get_basic(&value, &imsi);
1684 add_device(path, imsi);
1685 } else if (g_str_equal(key, "Present") == TRUE) {
1686 dbus_bool_t present;
1688 dbus_message_iter_get_basic(&value, &present);
1693 if (modem->device != NULL)
1694 modem_remove_device(modem);
1696 modem->has_gprs = FALSE;
1697 modem->has_reg = FALSE;
1703 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1706 const char *path = dbus_message_get_path(message);
1707 struct modem_data *modem;
1708 DBusMessageIter iter;
1710 DBG("path %s", path);
1712 modem = g_hash_table_lookup(modem_hash, path);
1716 if (dbus_message_iter_init(message, &iter))
1717 modem_gprs_changed(modem, &iter);
1722 static gboolean context_added(DBusConnection *connection,
1723 DBusMessage *message, void *user_data)
1725 const char *path = dbus_message_get_path(message);
1726 const char *network_path;
1727 struct modem_data *modem;
1728 DBusMessageIter iter, properties;
1730 DBG("path %s", path);
1732 modem = g_hash_table_lookup(modem_hash, path);
1733 if (modem == NULL || modem->device == NULL)
1736 if (dbus_message_iter_init(message, &iter) == FALSE)
1739 dbus_message_iter_get_basic(&iter, &network_path);
1741 dbus_message_iter_next(&iter);
1742 dbus_message_iter_recurse(&iter, &properties);
1744 add_network(modem->device, network_path, &properties);
1749 static gboolean context_removed(DBusConnection *connection,
1750 DBusMessage *message, void *user_data)
1752 const char *path = dbus_message_get_path(message);
1753 const char *network_path, *identifier;
1754 struct modem_data *modem;
1755 struct network_info *info;
1756 DBusMessageIter iter;
1758 DBG("path %s", path);
1760 modem = g_hash_table_lookup(modem_hash, path);
1761 if (modem == NULL || modem->device == NULL)
1764 if (dbus_message_iter_init(message, &iter) == FALSE)
1767 dbus_message_iter_get_basic(&iter, &network_path);
1769 info = g_hash_table_lookup(network_hash, network_path);
1773 identifier = connman_network_get_identifier(info->network);
1774 connman_device_remove_network(modem->device, identifier);
1779 static gboolean modem_added(DBusConnection *connection,
1780 DBusMessage *message, void *user_data)
1782 DBusMessageIter iter, properties;
1783 const char *modem_path;
1787 if (dbus_message_iter_init(message, &iter) == FALSE)
1790 dbus_message_iter_get_basic(&iter, &modem_path);
1792 dbus_message_iter_next(&iter);
1793 dbus_message_iter_recurse(&iter, &properties);
1795 add_modem(modem_path, &properties);
1800 static gboolean modem_removed(DBusConnection *connection,
1801 DBusMessage *message, void *user_data)
1803 DBusMessageIter iter;
1804 const char *modem_path;
1808 if (dbus_message_iter_init(message, &iter) == FALSE)
1811 dbus_message_iter_get_basic(&iter, &modem_path);
1813 g_hash_table_remove(modem_hash, modem_path);
1818 static gboolean context_changed(DBusConnection *connection,
1819 DBusMessage *message, void *user_data)
1821 const char *path = dbus_message_get_path(message);
1822 struct network_info *info;
1823 DBusMessageIter iter, value;
1826 DBG("path %s", path);
1828 info = g_hash_table_lookup(network_hash, path);
1832 if (!pending_network_is_available(info->network)) {
1833 g_hash_table_remove(network_hash, path);
1837 if (dbus_message_iter_init(message, &iter) == FALSE)
1840 dbus_message_iter_get_basic(&iter, &key);
1842 dbus_message_iter_next(&iter);
1843 dbus_message_iter_recurse(&iter, &value);
1847 if (g_str_equal(key, "Settings") == TRUE)
1848 update_ipv4_settings(&value, info);
1849 else if (g_str_equal(key, "IPv6.Settings"))
1850 update_ipv6_settings(&value, info);
1851 else if (g_str_equal(key, "Active") == TRUE) {
1854 dbus_message_iter_get_basic(&value, &active);
1856 if (active == FALSE)
1857 set_connected(info, active);
1859 /* Connect only if requested to do so */
1860 if (active == TRUE &&
1861 connman_network_get_connecting(info->network) == TRUE)
1862 set_connected(info, active);
1869 static guint reg_watch;
1870 static guint sim_watch;
1871 static guint gprs_watch;
1872 static guint context_added_watch;
1873 static guint context_removed_watch;
1874 static guint modem_watch;
1875 static guint modem_added_watch;
1876 static guint modem_removed_watch;
1877 static guint context_watch;
1879 static int ofono_init(void)
1883 connection = connman_dbus_get_connection();
1884 if (connection == NULL)
1887 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1888 ofono_connect, ofono_disconnect, NULL, NULL);
1890 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1891 OFONO_REGISTRATION_INTERFACE,
1896 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1897 OFONO_GPRS_INTERFACE,
1902 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1903 OFONO_GPRS_INTERFACE,
1908 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1909 OFONO_GPRS_INTERFACE,
1914 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1915 OFONO_MODEM_INTERFACE,
1920 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1921 OFONO_SIM_INTERFACE,
1926 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1927 OFONO_MANAGER_INTERFACE,
1932 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1933 OFONO_MANAGER_INTERFACE,
1938 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1939 OFONO_CONTEXT_INTERFACE,
1944 if (watch == 0 || gprs_watch == 0 || context_added_watch == 0 ||
1945 context_removed_watch == 0 || modem_watch == 0 ||
1946 reg_watch == 0 || sim_watch == 0 ||
1947 modem_added_watch == 0 || modem_removed_watch == 0 ||
1948 context_watch == 0) {
1953 err = connman_network_driver_register(&network_driver);
1957 err = connman_device_driver_register(&modem_driver);
1959 connman_network_driver_unregister(&network_driver);
1966 g_dbus_remove_watch(connection, watch);
1967 g_dbus_remove_watch(connection, sim_watch);
1968 g_dbus_remove_watch(connection, reg_watch);
1969 g_dbus_remove_watch(connection, gprs_watch);
1970 g_dbus_remove_watch(connection, context_added_watch);
1971 g_dbus_remove_watch(connection, context_removed_watch);
1972 g_dbus_remove_watch(connection, modem_watch);
1973 g_dbus_remove_watch(connection, modem_added_watch);
1974 g_dbus_remove_watch(connection, modem_removed_watch);
1975 g_dbus_remove_watch(connection, context_watch);
1977 dbus_connection_unref(connection);
1982 static void ofono_exit(void)
1984 g_dbus_remove_watch(connection, watch);
1985 g_dbus_remove_watch(connection, sim_watch);
1986 g_dbus_remove_watch(connection, reg_watch);
1987 g_dbus_remove_watch(connection, gprs_watch);
1988 g_dbus_remove_watch(connection, context_added_watch);
1989 g_dbus_remove_watch(connection, context_removed_watch);
1990 g_dbus_remove_watch(connection, modem_watch);
1991 g_dbus_remove_watch(connection, modem_added_watch);
1992 g_dbus_remove_watch(connection, modem_removed_watch);
1993 g_dbus_remove_watch(connection, context_watch);
1995 ofono_disconnect(connection, NULL);
1997 connman_device_driver_unregister(&modem_driver);
1998 connman_network_driver_unregister(&network_driver);
2000 dbus_connection_unref(connection);
2003 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2004 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)