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/device.h>
36 #include <connman/network.h>
37 #include <connman/ipconfig.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;
96 struct connman_network *network;
98 enum connman_ipconfig_method ipv4_method;
99 struct connman_ipaddress ipv4_address;
101 enum connman_ipconfig_method ipv6_method;
102 struct connman_ipaddress ipv6_address;
105 static int modem_probe(struct connman_device *device)
107 DBG("device %p", device);
112 static void modem_remove(struct connman_device *device)
114 DBG("device %p", device);
117 static int call_ofono(const char *path,
118 const char *interface, const char *method,
119 DBusPendingCallNotifyFunction notify, void *user_data,
120 DBusFreeFunction free_function,
123 DBusMessage *message;
124 DBusPendingCall *call;
128 DBG("path %s %s.%s", path, interface, method);
133 message = dbus_message_new_method_call(OFONO_SERVICE, path,
138 dbus_message_set_auto_start(message, FALSE);
141 ok = dbus_message_append_args_valist(message, type, va);
147 if (dbus_connection_send_with_reply(connection, message,
148 &call, TIMEOUT) == FALSE) {
149 connman_error("Failed to call %s.%s", interface, method);
150 dbus_message_unref(message);
155 connman_error("D-Bus connection not available");
156 dbus_message_unref(message);
160 dbus_pending_call_set_notify(call, notify, user_data, free_function);
162 dbus_message_unref(message);
167 static void set_property_reply(DBusPendingCall *call, void *user_data)
171 char const *name = user_data;
175 dbus_error_init(&error);
177 reply = dbus_pending_call_steal_reply(call);
179 if (dbus_set_error_from_message(&error, reply)) {
180 connman_error("SetProperty(%s) %s %s", name,
181 error.name, error.message);
182 dbus_error_free(&error);
185 dbus_message_unref(reply);
187 dbus_pending_call_unref(call);
190 static int set_property(const char *path, const char *interface,
191 const char *property, int type, void *value,
192 DBusPendingCallNotifyFunction notify, void *user_data,
193 DBusFreeFunction free_function)
195 DBusMessage *message;
196 DBusMessageIter iter;
197 DBusPendingCall *call;
199 DBG("path %s %s.%s", path, interface, property);
201 g_assert(notify == NULL ? free_function == NULL : 1);
206 message = dbus_message_new_method_call(OFONO_SERVICE, path,
207 interface, SET_PROPERTY);
211 dbus_message_set_auto_start(message, FALSE);
213 dbus_message_iter_init_append(message, &iter);
214 connman_dbus_property_append_basic(&iter, property, type, value);
216 if (dbus_connection_send_with_reply(connection, message,
217 &call, TIMEOUT) == FALSE) {
218 connman_error("Failed to change \"%s\" property on %s",
219 property, interface);
220 dbus_message_unref(message);
225 connman_error("D-Bus connection not available");
226 dbus_message_unref(message);
230 if (notify == NULL) {
231 notify = set_property_reply;
232 user_data = (void *)property;
233 free_function = NULL;
236 dbus_pending_call_set_notify(call, notify, user_data, free_function);
238 dbus_message_unref(message);
243 static void update_modem_online(struct modem_data *modem,
244 connman_bool_t online)
246 DBG("modem %p path %s online %d", modem, modem->path, online);
248 modem->online = online;
249 modem->requested_online = online;
250 modem->pending_online = FALSE;
253 connman_device_set_powered(modem->device, online);
256 static void set_online_reply(DBusPendingCall *call, void *user_data)
258 struct modem_data *modem;
263 DBG("path %s", (char *)user_data);
265 if (modem_hash == NULL)
268 modem = g_hash_table_lookup(modem_hash, user_data);
272 reply = dbus_pending_call_steal_reply(call);
274 dbus_error_init(&error);
276 if (dbus_set_error_from_message(&error, reply)) {
277 connman_error("SetProperty(Online) %s %s",
278 error.name, error.message);
279 dbus_error_free(&error);
281 result = modem->online;
283 result = modem->requested_online;
285 if (modem->pending_online)
286 update_modem_online(modem, result);
288 dbus_message_unref(reply);
290 dbus_pending_call_unref(call);
293 static int modem_change_online(char const *path, dbus_bool_t online)
295 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
300 if (modem->online == online)
303 modem->requested_online = online;
305 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
306 DBUS_TYPE_BOOLEAN, &online,
308 (void *)g_strdup(path), g_free);
311 static int modem_enable(struct connman_device *device)
313 const char *path = connman_device_get_string(device, "Path");
315 DBG("device %p, path, %s", device, path);
317 return modem_change_online(path, TRUE);
320 static int modem_disable(struct connman_device *device)
322 const char *path = connman_device_get_string(device, "Path");
324 DBG("device %p path %s", device, path);
326 return modem_change_online(path, FALSE);
329 static struct connman_device_driver modem_driver = {
331 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
332 .probe = modem_probe,
333 .remove = modem_remove,
334 .enable = modem_enable,
335 .disable = modem_disable,
338 static void remove_device_networks(struct connman_device *device)
342 GSList *info_list = NULL;
345 if (network_hash == NULL)
348 g_hash_table_iter_init(&iter, network_hash);
350 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
351 struct network_info *info = value;
353 if (connman_network_get_device(info->network) != device)
356 info_list = g_slist_append(info_list, info);
359 for (list = info_list; list != NULL; list = list->next) {
360 struct network_info *info = list->data;
362 connman_device_remove_network(device, info->network);
365 g_slist_free(info_list);
368 static void modem_remove_device(struct modem_data *modem)
370 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
372 if (modem->device == NULL)
375 remove_device_networks(modem->device);
377 connman_device_unregister(modem->device);
378 connman_device_unref(modem->device);
380 modem->device = NULL;
383 static void remove_modem(gpointer data)
385 struct modem_data *modem = data;
387 modem_remove_device(modem);
390 g_free(modem->operator);
395 static void remove_network(gpointer data)
397 struct network_info *info = data;
398 struct connman_device *device;
400 device = connman_network_get_device(info->network);
402 connman_device_remove_network(device, info->network);
404 connman_network_unref(info->network);
409 static char *get_ident(const char *path)
416 pos = strrchr(path, '/');
423 static void create_service(struct connman_network *network)
430 path = connman_network_get_string(network, "Path");
432 group = get_ident(path);
434 connman_network_set_group(network, group);
437 static int network_probe(struct connman_network *network)
442 static gboolean pending_network_is_available(struct connman_network *network)
444 /* Modem or network may be removed */
445 if (network == NULL || connman_network_get_device(network) == NULL) {
446 DBG("Modem or network was removed");
453 static void set_connected(struct network_info *info,
454 connman_bool_t connected)
456 gboolean setip = FALSE;
458 DBG("network %p connected %d", info->network, connected);
460 switch (info->ipv4_method) {
461 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
462 case CONNMAN_IPCONFIG_METHOD_OFF:
463 case CONNMAN_IPCONFIG_METHOD_MANUAL:
464 case CONNMAN_IPCONFIG_METHOD_AUTO:
467 case CONNMAN_IPCONFIG_METHOD_FIXED:
468 connman_network_set_ipv4_method(info->network,
470 connman_network_set_ipaddress(info->network,
471 &info->ipv4_address);
475 case CONNMAN_IPCONFIG_METHOD_DHCP:
476 connman_network_set_ipv4_method(info->network,
482 switch (info->ipv6_method) {
483 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
484 case CONNMAN_IPCONFIG_METHOD_OFF:
485 case CONNMAN_IPCONFIG_METHOD_MANUAL:
486 case CONNMAN_IPCONFIG_METHOD_DHCP:
487 case CONNMAN_IPCONFIG_METHOD_AUTO:
490 case CONNMAN_IPCONFIG_METHOD_FIXED:
491 connman_network_set_ipv6_method(info->network,
493 connman_network_set_ipaddress(info->network,
494 &info->ipv6_address);
500 connman_network_set_connected(info->network, connected);
503 static void set_active_reply(DBusPendingCall *call, void *user_data)
505 char const *path = user_data;
508 struct network_info *info;
510 info = g_hash_table_lookup(network_hash, path);
512 reply = dbus_pending_call_steal_reply(call);
517 DBG("path %s network %p", path, info->network);
519 if (!pending_network_is_available(info->network))
522 dbus_error_init(&error);
524 if (dbus_set_error_from_message(&error, reply)) {
525 connman_error("SetProperty(Active) %s %s",
526 error.name, error.message);
528 if (connman_network_get_index(info->network) < 0)
529 connman_network_set_error(info->network,
530 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
532 dbus_error_free(&error);
533 } else if (connman_network_get_index(info->network) >= 0)
534 set_connected(info, TRUE);
537 dbus_message_unref(reply);
539 dbus_pending_call_unref(call);
542 static int set_network_active(struct connman_network *network)
544 dbus_bool_t value = TRUE;
545 const char *path = connman_network_get_string(network, "Path");
547 DBG("network %p, path %s", network, path);
549 return set_property(path, OFONO_CONTEXT_INTERFACE,
550 "Active", DBUS_TYPE_BOOLEAN, &value,
551 set_active_reply, g_strdup(path), g_free);
554 static int set_network_inactive(struct connman_network *network)
557 dbus_bool_t value = FALSE;
558 const char *path = connman_network_get_string(network, "Path");
560 DBG("network %p, path %s", network, path);
562 err = set_property(path, OFONO_CONTEXT_INTERFACE,
563 "Active", DBUS_TYPE_BOOLEAN, &value,
566 if (err == -EINPROGRESS)
572 static int network_connect(struct connman_network *network)
574 struct connman_device *device;
575 struct modem_data *modem;
577 DBG("network %p", network);
579 device = connman_network_get_device(network);
583 modem = connman_device_get_data(device);
587 if (modem->registered == FALSE)
590 if (modem->powered == FALSE)
593 if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
596 return set_network_active(network);
599 static int network_disconnect(struct connman_network *network)
601 DBG("network %p", network);
603 if (connman_network_get_index(network) < 0)
606 connman_network_set_associating(network, FALSE);
608 return set_network_inactive(network);
611 static void network_remove(struct connman_network *network)
613 char const *path = connman_network_get_string(network, "Path");
615 DBG("network %p path %s", network, path);
617 g_hash_table_remove(network_hash, path);
620 static struct connman_network_driver network_driver = {
622 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
623 .probe = network_probe,
624 .remove = network_remove,
625 .connect = network_connect,
626 .disconnect = network_disconnect,
629 static void get_dns(DBusMessageIter *array, struct network_info *info)
631 DBusMessageIter entry;
632 gchar *nameservers = NULL, *nameservers_old = NULL;
637 dbus_message_iter_recurse(array, &entry);
639 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
642 dbus_message_iter_get_basic(&entry, &dns);
646 if (nameservers == NULL) {
648 nameservers = g_strdup(dns);
651 nameservers_old = nameservers;
652 nameservers = g_strdup_printf("%s %s",
653 nameservers_old, dns);
654 g_free(nameservers_old);
657 dbus_message_iter_next(&entry);
660 connman_network_set_nameservers(info->network, nameservers);
665 static void update_ipv4_settings(DBusMessageIter *array,
666 struct network_info *info)
668 DBusMessageIter dict;
669 char *address = NULL, *netmask = NULL, *gateway = NULL;
670 const char *interface = NULL;
672 DBG("network %p", info->network);
674 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
677 dbus_message_iter_recurse(array, &dict);
679 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
680 DBusMessageIter entry, value;
681 const char *key, *val;
683 dbus_message_iter_recurse(&dict, &entry);
684 dbus_message_iter_get_basic(&entry, &key);
688 dbus_message_iter_next(&entry);
689 dbus_message_iter_recurse(&entry, &value);
691 if (g_str_equal(key, "Interface") == TRUE) {
694 dbus_message_iter_get_basic(&value, &interface);
696 DBG("interface %s", interface);
698 index = connman_inet_ifindex(interface);
700 connman_network_set_index(info->network, index);
702 connman_error("Can not find interface %s",
706 } else if (g_str_equal(key, "Method") == TRUE) {
709 dbus_message_iter_get_basic(&value, &method);
711 if (g_strcmp0(method, "static") == 0) {
714 CONNMAN_IPCONFIG_METHOD_FIXED;
715 } else if (g_strcmp0(method, "dhcp") == 0) {
718 CONNMAN_IPCONFIG_METHOD_DHCP;
721 } else if (g_str_equal(key, "Address") == TRUE) {
722 dbus_message_iter_get_basic(&value, &val);
724 address = g_strdup(val);
726 DBG("address %s", address);
727 } else if (g_str_equal(key, "Netmask") == TRUE) {
728 dbus_message_iter_get_basic(&value, &val);
730 netmask = g_strdup(val);
732 DBG("netmask %s", netmask);
733 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
735 get_dns(&value, info);
736 } else if (g_str_equal(key, "Gateway") == TRUE) {
737 dbus_message_iter_get_basic(&value, &val);
739 gateway = g_strdup(val);
741 DBG("gateway %s", gateway);
744 dbus_message_iter_next(&dict);
748 if (info->ipv4_method == CONNMAN_IPCONFIG_METHOD_FIXED) {
749 connman_ipaddress_set_ipv4(&info->ipv4_address, address,
753 /* deactive, oFono send NULL inteface before deactive signal */
754 if (interface == NULL)
755 connman_network_set_index(info->network, -1);
762 static void update_ipv6_settings(DBusMessageIter *array,
763 struct network_info *info)
765 DBusMessageIter dict;
766 char *address = NULL, *gateway = NULL;
767 unsigned char prefix_length;
768 const char *interface = NULL;
770 DBG("network %p", info->network);
772 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
775 dbus_message_iter_recurse(array, &dict);
777 info->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
779 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
780 DBusMessageIter entry, value;
781 const char *key, *val;
783 dbus_message_iter_recurse(&dict, &entry);
784 dbus_message_iter_get_basic(&entry, &key);
788 dbus_message_iter_next(&entry);
789 dbus_message_iter_recurse(&entry, &value);
791 if (g_str_equal(key, "Interface") == TRUE) {
794 dbus_message_iter_get_basic(&value, &interface);
796 DBG("interface %s", interface);
798 index = connman_inet_ifindex(interface);
800 connman_network_set_index(info->network, index);
802 connman_error("Can not find interface %s",
806 } else if (g_str_equal(key, "Address") == TRUE) {
807 dbus_message_iter_get_basic(&value, &val);
809 address = g_strdup(val);
811 DBG("address %s", address);
812 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
813 dbus_message_iter_get_basic(&value, &prefix_length);
815 DBG("prefix length %d", prefix_length);
816 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
818 get_dns(&value, info);
819 } else if (g_str_equal(key, "Gateway") == TRUE) {
820 dbus_message_iter_get_basic(&value, &val);
822 gateway = g_strdup(val);
824 DBG("gateway %s", gateway);
827 dbus_message_iter_next(&dict);
830 connman_ipaddress_set_ipv6(&info->ipv6_address, address,
831 prefix_length, gateway);
833 /* deactive, oFono send NULL inteface before deactive signal */
834 if (interface == NULL)
835 connman_network_set_index(info->network, -1);
841 static int add_network(struct connman_device *device,
842 const char *path, DBusMessageIter *dict)
844 struct modem_data *modem = connman_device_get_data(device);
845 struct connman_network *network;
846 struct network_info *info;
848 dbus_bool_t active = FALSE;
850 DBG("modem %p device %p path %s", modem, device, path);
852 ident = get_ident(path);
854 network = connman_device_get_network(device, ident);
858 info = g_hash_table_lookup(network_hash, path);
860 DBG("path %p already exists with device %p", path,
861 connman_network_get_device(info->network));
862 if (connman_network_get_device(info->network))
864 g_hash_table_remove(network_hash, path);
867 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
871 info = g_try_new0(struct network_info, 1);
873 connman_network_unref(network);
877 connman_ipaddress_clear(&info->ipv4_address);
878 connman_ipaddress_clear(&info->ipv6_address);
879 info->network = network;
881 connman_network_set_string(network, "Path", path);
883 create_service(network);
885 g_hash_table_insert(network_hash, (char *) path, info);
887 connman_network_set_available(network, TRUE);
888 connman_network_set_index(network, -1);
891 connman_network_set_name(network, modem->operator);
893 if (modem->has_strength)
894 connman_network_set_strength(network, modem->strength);
896 connman_network_set_roaming(network, modem->roaming);
898 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
899 DBusMessageIter entry, value;
902 dbus_message_iter_recurse(dict, &entry);
903 dbus_message_iter_get_basic(&entry, &key);
905 dbus_message_iter_next(&entry);
906 dbus_message_iter_recurse(&entry, &value);
908 if (g_str_equal(key, "Type")) {
911 dbus_message_iter_get_basic(&value, &type);
912 if (g_strcmp0(type, "internet") != 0) {
913 DBG("path %p type %s", path, type);
914 g_hash_table_remove(network_hash, path);
917 } else if (g_str_equal(key, "Settings"))
918 update_ipv4_settings(&value, info);
919 else if (g_str_equal(key, "IPv6.Settings"))
920 update_ipv6_settings(&value, info);
921 else if (g_str_equal(key, "Active") == TRUE)
922 dbus_message_iter_get_basic(&value, &active);
924 dbus_message_iter_next(dict);
927 if (connman_device_add_network(device, network) != 0) {
928 g_hash_table_remove(network_hash, path);
932 /* Connect only if requested to do so */
933 if (active && connman_network_get_connecting(network) == TRUE)
934 set_connected(info, active);
939 static void check_networks_reply(DBusPendingCall *call, void *user_data)
941 char *path = user_data;
942 struct modem_data *modem;
944 DBusMessageIter array, entry, value, properties;
946 DBG("path %s", path);
948 modem = g_hash_table_lookup(modem_hash, path);
951 if (modem->device == NULL)
954 reply = dbus_pending_call_steal_reply(call);
956 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
959 dbus_message_iter_init(reply, &array);
961 dbus_message_iter_recurse(&array, &entry);
963 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
964 char const *network_path;
966 dbus_message_iter_recurse(&entry, &value);
967 dbus_message_iter_get_basic(&value, &network_path);
969 dbus_message_iter_next(&value);
970 dbus_message_iter_recurse(&value, &properties);
972 add_network(modem->device, network_path, &properties);
974 dbus_message_iter_next(&entry);
978 dbus_message_unref(reply);
980 dbus_pending_call_unref(call);
983 static void check_networks(struct modem_data *modem)
985 char const *path = modem->path;
987 DBG("modem %p path %s", modem, path);
989 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
990 check_networks_reply, g_strdup(path), g_free,
994 static void modem_clear_network_errors(struct modem_data *modem)
996 struct connman_device *device = modem->device;
1003 g_hash_table_iter_init(&i, network_hash);
1005 while (g_hash_table_iter_next(&i, NULL, &value)) {
1006 struct network_info *info = value;
1008 if (connman_network_get_device(info->network) == device)
1009 connman_network_clear_error(info->network);
1013 static void modem_operator_name_changed(struct modem_data *modem,
1016 struct connman_device *device = modem->device;
1023 if (modem->operator != NULL)
1024 g_free(modem->operator);
1025 modem->operator = g_strdup(name);
1027 for (g_hash_table_iter_init(&i, network_hash);
1028 g_hash_table_iter_next(&i, NULL, &value);) {
1029 struct network_info *info = value;
1031 if (connman_network_get_device(info->network) == device) {
1032 connman_network_set_name(info->network, name);
1033 connman_network_update(info->network);
1038 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
1040 struct connman_device *device = modem->device;
1044 modem->strength = strength;
1045 modem->has_strength = TRUE;
1050 for (g_hash_table_iter_init(&i, network_hash);
1051 g_hash_table_iter_next(&i, NULL, &value);) {
1052 struct network_info *info = value;
1054 if (connman_network_get_device(info->network) == device) {
1055 connman_network_set_strength(info->network, strength);
1056 connman_network_update(info->network);
1061 static void modem_roaming_changed(struct modem_data *modem,
1064 struct connman_device *device = modem->device;
1065 connman_bool_t roaming = FALSE;
1066 connman_bool_t registered = FALSE;
1067 connman_bool_t was_roaming = modem->roaming;
1071 if (g_str_equal(status, "roaming"))
1073 else if (g_str_equal(status, "registered"))
1076 registered = registered || roaming;
1078 if (modem->roaming == roaming && modem->registered == registered)
1081 modem->registered = registered;
1082 modem->roaming = roaming;
1084 if (roaming == was_roaming)
1090 g_hash_table_iter_init(&i, network_hash);
1092 while (g_hash_table_iter_next(&i, NULL, &value)) {
1093 struct network_info *info = value;
1095 if (connman_network_get_device(info->network) == device) {
1096 connman_network_set_roaming(info->network, roaming);
1097 connman_network_update(info->network);
1102 static void modem_registration_removed(struct modem_data *modem)
1104 modem->registered = FALSE;
1105 modem->roaming = FALSE;
1108 static void modem_registration_changed(struct modem_data *modem,
1109 DBusMessageIter *entry)
1111 DBusMessageIter iter;
1114 connman_uint8_t strength;
1115 char const *name, *status, *mcc_s;
1117 dbus_message_iter_get_basic(entry, &key);
1121 dbus_message_iter_next(entry);
1123 dbus_message_iter_recurse(entry, &iter);
1125 type = dbus_message_iter_get_arg_type(&iter);
1126 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
1129 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
1130 dbus_message_iter_get_basic(&iter, &name);
1131 modem_operator_name_changed(modem, name);
1132 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
1133 dbus_message_iter_get_basic(&iter, &strength);
1134 modem_strength_changed(modem, strength);
1135 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
1136 dbus_message_iter_get_basic(&iter, &status);
1137 modem_roaming_changed(modem, status);
1138 } else if (g_str_equal(key, "MobileCountryCode") &&
1139 type == DBUS_TYPE_STRING) {
1143 dbus_message_iter_get_basic(&iter, &mcc_s);
1149 alpha2 = mcc_country_codes[mcc - 200];
1150 connman_technology_set_regdom(alpha2);
1155 static gboolean reg_changed(DBusConnection *connection,
1156 DBusMessage *message, void *user_data)
1158 const char *path = dbus_message_get_path(message);
1159 struct modem_data *modem;
1160 DBusMessageIter iter;
1162 DBG("path %s", path);
1164 modem = g_hash_table_lookup(modem_hash, path);
1168 if (dbus_message_iter_init(message, &iter))
1169 modem_registration_changed(modem, &iter);
1174 static void check_registration_reply(DBusPendingCall *call, void *user_data)
1176 char const *path = user_data;
1177 struct modem_data *modem;
1179 DBusMessageIter array, dict, entry;
1181 DBG("path %s", path);
1183 modem = g_hash_table_lookup(modem_hash, path);
1187 reply = dbus_pending_call_steal_reply(call);
1189 if (dbus_message_iter_init(reply, &array) == FALSE)
1192 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1195 dbus_message_iter_recurse(&array, &dict);
1196 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1197 dbus_message_iter_recurse(&dict, &entry);
1198 modem_registration_changed(modem, &entry);
1199 dbus_message_iter_next(&dict);
1203 dbus_message_unref(reply);
1205 dbus_pending_call_unref(call);
1208 static void check_registration(struct modem_data *modem)
1210 char const *path = modem->path;
1212 DBG("modem %p path %s", modem, path);
1214 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
1215 check_registration_reply, g_strdup(path), g_free,
1219 static void modem_gprs_changed(struct modem_data *modem,
1220 DBusMessageIter *entry)
1222 DBusMessageIter iter;
1227 dbus_message_iter_get_basic(entry, &key);
1231 dbus_message_iter_next(entry);
1233 dbus_message_iter_recurse(entry, &iter);
1235 type = dbus_message_iter_get_arg_type(&iter);
1237 if (type != DBUS_TYPE_BOOLEAN)
1240 dbus_message_iter_get_basic(&iter, &value);
1242 if (g_str_equal(key, "Attached") == TRUE) {
1243 DBG("Attached %d", value);
1245 modem->attached = value;
1248 modem_clear_network_errors(modem);
1249 } else if (g_str_equal(key, "Powered") == TRUE) {
1250 DBG("Powered %d", value);
1252 modem->powered = value;
1253 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
1254 DBG("RoamingAllowed %d", value);
1256 modem->roaming_allowed = value;
1260 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
1262 char const *path = user_data;
1263 struct modem_data *modem;
1265 DBusMessageIter array, dict, entry;
1267 DBG("path %s", path);
1269 modem = g_hash_table_lookup(modem_hash, path);
1273 reply = dbus_pending_call_steal_reply(call);
1275 if (dbus_message_iter_init(reply, &array) == FALSE)
1278 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1281 dbus_message_iter_recurse(&array, &dict);
1282 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1283 dbus_message_iter_recurse(&dict, &entry);
1284 modem_gprs_changed(modem, &entry);
1285 dbus_message_iter_next(&dict);
1289 dbus_message_unref(reply);
1291 dbus_pending_call_unref(call);
1293 check_networks(modem);
1296 static void check_gprs(struct modem_data *modem)
1298 char const *path = modem->path;
1300 DBG("modem %p path %s", modem, path);
1302 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
1303 check_gprs_reply, g_strdup(path), g_free,
1307 static void add_device(const char *path, const char *imsi)
1309 struct modem_data *modem;
1310 struct connman_device *device;
1312 DBG("path %s imsi %s", path, imsi);
1320 modem = g_hash_table_lookup(modem_hash, path);
1324 if (modem->device) {
1325 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1328 modem_remove_device(modem);
1331 if (strlen(imsi) == 0)
1334 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1338 connman_device_set_ident(device, imsi);
1340 connman_device_set_string(device, "Path", path);
1342 connman_device_set_data(device, modem);
1344 if (connman_device_register(device) < 0) {
1345 connman_device_unref(device);
1349 modem->device = device;
1352 check_registration(modem);
1354 if (modem->has_gprs)
1358 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1360 const char *path = user_data;
1361 const char *imsi = NULL;
1363 DBusMessageIter array, dict;
1365 DBG("path %s", path);
1367 reply = dbus_pending_call_steal_reply(call);
1369 if (dbus_message_iter_init(reply, &array) == FALSE)
1372 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1375 dbus_message_iter_recurse(&array, &dict);
1377 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1378 DBusMessageIter entry, value;
1381 dbus_message_iter_recurse(&dict, &entry);
1382 dbus_message_iter_get_basic(&entry, &key);
1384 dbus_message_iter_next(&entry);
1385 dbus_message_iter_recurse(&entry, &value);
1387 if (g_str_equal(key, "SubscriberIdentity")) {
1388 dbus_message_iter_get_basic(&value, &imsi);
1389 add_device(path, imsi);
1392 dbus_message_iter_next(&dict);
1396 dbus_message_unref(reply);
1398 dbus_pending_call_unref(call);
1401 static void get_imsi(const char *path)
1403 DBG("path %s", path);
1405 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1406 sim_properties_reply, g_strdup(path), g_free,
1410 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1412 DBG("path %s powered %d", path, powered);
1414 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1415 DBUS_TYPE_BOOLEAN, &powered,
1419 static int modem_change_powered(const char *path, dbus_bool_t powered)
1421 DBG("path %s powered %d", path, powered);
1423 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1424 DBUS_TYPE_BOOLEAN, &powered,
1429 static gboolean modem_has_interface(DBusMessageIter *array,
1430 char const *interface)
1432 DBusMessageIter entry;
1434 dbus_message_iter_recurse(array, &entry);
1436 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1437 const char *element;
1439 dbus_message_iter_get_basic(&entry, &element);
1441 if (g_strcmp0(interface, element) == 0)
1444 dbus_message_iter_next(&entry);
1450 static gboolean modem_has_sim(DBusMessageIter *array)
1452 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1455 static gboolean modem_has_reg(DBusMessageIter *array)
1457 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1460 static gboolean modem_has_gprs(DBusMessageIter *array)
1462 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1465 static void add_modem(const char *path, DBusMessageIter *prop)
1467 struct modem_data *modem;
1468 dbus_bool_t powered = FALSE;
1469 dbus_bool_t online = FALSE;
1470 dbus_bool_t locked = FALSE;
1471 gboolean has_sim = FALSE;
1472 gboolean has_reg = FALSE;
1473 gboolean has_gprs = FALSE;
1475 modem = g_hash_table_lookup(modem_hash, path);
1480 modem = g_try_new0(struct modem_data, 1);
1484 modem->path = g_strdup(path);
1485 modem->device = NULL;
1486 modem->available = TRUE;
1488 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1490 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1491 DBusMessageIter entry, value;
1494 dbus_message_iter_recurse(prop, &entry);
1495 dbus_message_iter_get_basic(&entry, &key);
1497 dbus_message_iter_next(&entry);
1498 dbus_message_iter_recurse(&entry, &value);
1500 if (g_str_equal(key, "Powered") == TRUE)
1501 dbus_message_iter_get_basic(&value, &powered);
1502 else if (g_str_equal(key, "Lockdown") == TRUE)
1503 dbus_message_iter_get_basic(&value, &locked);
1504 else if (g_str_equal(key, "Interfaces") == TRUE) {
1505 has_sim = modem_has_sim(&value);
1506 has_reg = modem_has_reg(&value);
1507 has_gprs = modem_has_gprs(&value);
1510 dbus_message_iter_next(prop);
1517 modem_change_powered(path, TRUE);
1519 modem->has_sim = has_sim;
1520 modem->has_reg = has_reg;
1521 modem->has_gprs = has_gprs;
1523 update_modem_online(modem, online);
1529 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1533 DBusMessageIter array, dict;
1537 reply = dbus_pending_call_steal_reply(call);
1539 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1542 dbus_error_init(&error);
1544 if (dbus_set_error_from_message(&error, reply)) {
1545 connman_error("ModemManager.GetModems() %s %s",
1546 error.name, error.message);
1547 dbus_error_free(&error);
1551 if (dbus_message_iter_init(reply, &array) == FALSE)
1554 dbus_message_iter_recurse(&array, &dict);
1556 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1557 DBusMessageIter value, properties;
1558 const char *modem_path;
1560 dbus_message_iter_recurse(&dict, &value);
1561 dbus_message_iter_get_basic(&value, &modem_path);
1563 dbus_message_iter_next(&value);
1564 dbus_message_iter_recurse(&value, &properties);
1567 add_modem(modem_path, &properties);
1569 dbus_message_iter_next(&dict);
1573 dbus_message_unref(reply);
1575 dbus_pending_call_unref(call);
1578 static void ofono_connect(DBusConnection *connection, void *user_data)
1580 DBG("connection %p", connection);
1582 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1583 g_free, remove_modem);
1585 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1586 NULL, remove_network);
1588 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1589 manager_modems_reply, NULL, NULL,
1593 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1595 DBG("connection %p", connection);
1597 if (modem_hash == NULL)
1600 g_hash_table_destroy(modem_hash);
1605 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1608 const char *path = dbus_message_get_path(message);
1609 struct modem_data *modem;
1610 DBusMessageIter iter, value;
1613 DBG("path %s", path);
1615 modem = g_hash_table_lookup(modem_hash, path);
1619 if (dbus_message_iter_init(message, &iter) == FALSE)
1622 dbus_message_iter_get_basic(&iter, &key);
1624 dbus_message_iter_next(&iter);
1625 dbus_message_iter_recurse(&iter, &value);
1627 if (g_str_equal(key, "Powered") == TRUE) {
1628 dbus_bool_t powered;
1630 dbus_message_iter_get_basic(&value, &powered);
1631 if (powered == TRUE)
1634 modem->has_sim = FALSE;
1635 modem->has_reg = FALSE;
1636 modem->has_gprs = FALSE;
1638 modem_remove_device(modem);
1639 } else if (g_str_equal(key, "Online") == TRUE) {
1642 dbus_message_iter_get_basic(&value, &online);
1644 update_modem_online(modem, online);
1645 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1648 dbus_message_iter_get_basic(&value, &locked);
1651 modem_change_powered(path, TRUE);
1653 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1654 gboolean has_sim = modem_has_sim(&value);
1655 gboolean has_reg = modem_has_reg(&value);
1656 gboolean had_reg = modem->has_reg;
1657 gboolean has_gprs = modem_has_gprs(&value);
1658 gboolean had_gprs = modem->has_gprs;
1660 modem->has_sim = has_sim;
1661 modem->has_reg = has_reg;
1662 modem->has_gprs = has_gprs;
1664 if (modem->device == NULL) {
1666 get_imsi(modem->path);
1667 } else if (!has_sim) {
1668 modem_remove_device(modem);
1670 if (has_reg && !had_reg)
1671 check_registration(modem);
1672 else if (had_reg && !has_reg)
1673 modem_registration_removed(modem);
1675 if (has_gprs && !had_gprs) {
1676 gprs_change_powered(modem->path, TRUE);
1685 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1688 const char *path = dbus_message_get_path(message);
1689 struct modem_data *modem;
1690 DBusMessageIter iter, value;
1693 DBG("path %s", path);
1695 modem = g_hash_table_lookup(modem_hash, path);
1699 if (dbus_message_iter_init(message, &iter) == FALSE)
1702 dbus_message_iter_get_basic(&iter, &key);
1704 dbus_message_iter_next(&iter);
1705 dbus_message_iter_recurse(&iter, &value);
1707 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1710 dbus_message_iter_get_basic(&value, &imsi);
1712 add_device(path, imsi);
1713 } else if (g_str_equal(key, "Present") == TRUE) {
1714 dbus_bool_t present;
1716 dbus_message_iter_get_basic(&value, &present);
1721 if (modem->device != NULL)
1722 modem_remove_device(modem);
1724 modem->has_gprs = FALSE;
1725 modem->has_reg = FALSE;
1731 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1734 const char *path = dbus_message_get_path(message);
1735 struct modem_data *modem;
1736 DBusMessageIter iter;
1738 DBG("path %s", path);
1740 modem = g_hash_table_lookup(modem_hash, path);
1744 if (dbus_message_iter_init(message, &iter))
1745 modem_gprs_changed(modem, &iter);
1750 static gboolean context_added(DBusConnection *connection,
1751 DBusMessage *message, void *user_data)
1753 const char *path = dbus_message_get_path(message);
1754 const char *network_path;
1755 struct modem_data *modem;
1756 DBusMessageIter iter, properties;
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 dbus_message_iter_next(&iter);
1770 dbus_message_iter_recurse(&iter, &properties);
1772 add_network(modem->device, network_path, &properties);
1777 static gboolean context_removed(DBusConnection *connection,
1778 DBusMessage *message, void *user_data)
1780 const char *path = dbus_message_get_path(message);
1781 const char *network_path;
1782 struct modem_data *modem;
1783 DBusMessageIter iter;
1785 DBG("path %s", path);
1787 modem = g_hash_table_lookup(modem_hash, path);
1788 if (modem == NULL || modem->device == NULL)
1791 if (dbus_message_iter_init(message, &iter) == FALSE)
1794 dbus_message_iter_get_basic(&iter, &network_path);
1796 g_hash_table_remove(network_hash, network_path);
1800 static gboolean modem_added(DBusConnection *connection,
1801 DBusMessage *message, void *user_data)
1803 DBusMessageIter iter, properties;
1804 const char *modem_path;
1808 if (dbus_message_iter_init(message, &iter) == FALSE)
1811 dbus_message_iter_get_basic(&iter, &modem_path);
1813 dbus_message_iter_next(&iter);
1814 dbus_message_iter_recurse(&iter, &properties);
1816 add_modem(modem_path, &properties);
1821 static gboolean modem_removed(DBusConnection *connection,
1822 DBusMessage *message, void *user_data)
1824 DBusMessageIter iter;
1825 const char *modem_path;
1829 if (dbus_message_iter_init(message, &iter) == FALSE)
1832 dbus_message_iter_get_basic(&iter, &modem_path);
1834 g_hash_table_remove(modem_hash, modem_path);
1839 static gboolean context_changed(DBusConnection *connection,
1840 DBusMessage *message, void *user_data)
1842 const char *path = dbus_message_get_path(message);
1843 struct network_info *info;
1844 DBusMessageIter iter, value;
1847 DBG("path %s", path);
1849 info = g_hash_table_lookup(network_hash, path);
1853 if (!pending_network_is_available(info->network)) {
1854 g_hash_table_remove(network_hash, path);
1858 if (dbus_message_iter_init(message, &iter) == FALSE)
1861 dbus_message_iter_get_basic(&iter, &key);
1863 dbus_message_iter_next(&iter);
1864 dbus_message_iter_recurse(&iter, &value);
1868 if (g_str_equal(key, "Settings") == TRUE)
1869 update_ipv4_settings(&value, info);
1870 else if (g_str_equal(key, "IPv6.Settings"))
1871 update_ipv6_settings(&value, info);
1872 else if (g_str_equal(key, "Active") == TRUE) {
1875 dbus_message_iter_get_basic(&value, &active);
1877 if (active == FALSE)
1878 set_connected(info, active);
1880 /* Connect only if requested to do so */
1881 if (active == TRUE &&
1882 connman_network_get_connecting(info->network) == TRUE)
1883 set_connected(info, active);
1890 static guint reg_watch;
1891 static guint sim_watch;
1892 static guint gprs_watch;
1893 static guint context_added_watch;
1894 static guint context_removed_watch;
1895 static guint modem_watch;
1896 static guint modem_added_watch;
1897 static guint modem_removed_watch;
1898 static guint context_watch;
1900 static int ofono_init(void)
1904 connection = connman_dbus_get_connection();
1905 if (connection == NULL)
1908 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1909 ofono_connect, ofono_disconnect, NULL, NULL);
1911 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1912 OFONO_REGISTRATION_INTERFACE,
1917 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1918 OFONO_GPRS_INTERFACE,
1923 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1924 OFONO_GPRS_INTERFACE,
1929 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1930 OFONO_GPRS_INTERFACE,
1935 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1936 OFONO_MODEM_INTERFACE,
1941 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1942 OFONO_SIM_INTERFACE,
1947 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1948 OFONO_MANAGER_INTERFACE,
1953 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1954 OFONO_MANAGER_INTERFACE,
1959 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1960 OFONO_CONTEXT_INTERFACE,
1965 if (watch == 0 || gprs_watch == 0 || context_added_watch == 0 ||
1966 context_removed_watch == 0 || modem_watch == 0 ||
1967 reg_watch == 0 || sim_watch == 0 ||
1968 modem_added_watch == 0 || modem_removed_watch == 0 ||
1969 context_watch == 0) {
1974 err = connman_network_driver_register(&network_driver);
1978 err = connman_device_driver_register(&modem_driver);
1980 connman_network_driver_unregister(&network_driver);
1987 g_dbus_remove_watch(connection, watch);
1988 g_dbus_remove_watch(connection, sim_watch);
1989 g_dbus_remove_watch(connection, reg_watch);
1990 g_dbus_remove_watch(connection, gprs_watch);
1991 g_dbus_remove_watch(connection, context_added_watch);
1992 g_dbus_remove_watch(connection, context_removed_watch);
1993 g_dbus_remove_watch(connection, modem_watch);
1994 g_dbus_remove_watch(connection, modem_added_watch);
1995 g_dbus_remove_watch(connection, modem_removed_watch);
1996 g_dbus_remove_watch(connection, context_watch);
1998 dbus_connection_unref(connection);
2003 static void ofono_exit(void)
2005 g_dbus_remove_watch(connection, watch);
2006 g_dbus_remove_watch(connection, sim_watch);
2007 g_dbus_remove_watch(connection, reg_watch);
2008 g_dbus_remove_watch(connection, gprs_watch);
2009 g_dbus_remove_watch(connection, context_added_watch);
2010 g_dbus_remove_watch(connection, context_removed_watch);
2011 g_dbus_remove_watch(connection, modem_watch);
2012 g_dbus_remove_watch(connection, modem_added_watch);
2013 g_dbus_remove_watch(connection, modem_removed_watch);
2014 g_dbus_remove_watch(connection, context_watch);
2016 ofono_disconnect(connection, NULL);
2018 connman_device_driver_unregister(&modem_driver);
2019 connman_network_driver_unregister(&network_driver);
2021 dbus_connection_unref(connection);
2024 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2025 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)