5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011 BWM Car IT GmbH. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42 #include <connman/technology.h>
46 #define uninitialized_var(x) x = x
48 #define OFONO_SERVICE "org.ofono"
50 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
51 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
52 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
53 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
54 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
55 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
56 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
57 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
59 #define MODEM_ADDED "ModemAdded"
60 #define MODEM_REMOVED "ModemRemoved"
61 #define PROPERTY_CHANGED "PropertyChanged"
62 #define CONTEXT_ADDED "ContextAdded"
63 #define CONTEXT_REMOVED "ContextRemoved"
65 #define GET_PROPERTIES "GetProperties"
66 #define SET_PROPERTY "SetProperty"
67 #define GET_MODEMS "GetModems"
68 #define GET_CONTEXTS "GetContexts"
74 OFONO_API_NETREG = 0x2,
76 OFONO_API_CDMA_NETREG = 0x8,
77 OFONO_API_CDMA_CM = 0x10,
81 * The way this plugin works is following:
83 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
84 * attached -> netreg -> ready
86 * Enabling and disabling modems are steered through the rfkill
87 * interface. That means when ConnMan toggles the rfkill bit oFono
88 * will add or remove the modems.
90 * ConnMan will always power up (set Powered and Online) the
91 * modems. No need to power them down because this will be done
92 * through the rfkill inteface.
95 static DBusConnection *connection;
97 static GHashTable *modem_hash;
98 static GHashTable *context_hash;
100 struct network_context {
104 enum connman_ipconfig_method ipv4_method;
105 struct connman_ipaddress *ipv4_address;
106 char *ipv4_nameservers;
108 enum connman_ipconfig_method ipv6_method;
109 struct connman_ipaddress *ipv6_address;
110 char *ipv6_nameservers;
116 struct connman_device *device;
117 struct connman_network *network;
119 struct network_context *context;
121 /* Modem Interface */
123 connman_bool_t powered;
124 connman_bool_t online;
126 connman_bool_t ignore;
128 connman_bool_t set_powered;
130 /* CDMA ConnectionManager Interface */
131 connman_bool_t cdma_cm_powered;
133 /* ConnectionManager Interface */
134 connman_bool_t attached;
135 connman_bool_t cm_powered;
137 /* ConnectionContext Interface */
138 connman_bool_t active;
139 connman_bool_t set_active;
141 /* SimManager Interface */
144 /* Netreg Interface */
147 uint8_t data_strength; /* 1xEVDO signal strength */
148 connman_bool_t roaming;
151 DBusPendingCall *call_set_property;
152 DBusPendingCall *call_get_properties;
153 DBusPendingCall *call_get_contexts;
156 static const char *api2string(enum ofono_api api)
161 case OFONO_API_NETREG:
165 case OFONO_API_CDMA_NETREG:
166 return "cdma-netreg";
167 case OFONO_API_CDMA_CM:
174 static char *get_ident(const char *path)
181 pos = strrchr(path, '/');
188 static struct network_context *network_context_alloc(const char *path)
190 struct network_context *context;
192 context = g_try_new0(struct network_context, 1);
196 context->path = g_strdup(path);
199 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
200 context->ipv4_address = NULL;
201 context->ipv4_nameservers = NULL;
203 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
204 context->ipv6_address = NULL;
205 context->ipv6_nameservers = NULL;
210 static void network_context_free(struct network_context *context)
212 g_free(context->path);
214 connman_ipaddress_free(context->ipv4_address);
215 g_free(context->ipv4_nameservers);
217 connman_ipaddress_free(context->ipv6_address);
218 g_free(context->ipv6_nameservers);
223 static void set_connected(struct modem_data *modem)
225 connman_bool_t setip = FALSE;
227 DBG("%s", modem->path);
229 connman_network_set_index(modem->network, modem->context->index);
231 switch (modem->context->ipv4_method) {
232 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
233 case CONNMAN_IPCONFIG_METHOD_OFF:
234 case CONNMAN_IPCONFIG_METHOD_MANUAL:
235 case CONNMAN_IPCONFIG_METHOD_AUTO:
238 case CONNMAN_IPCONFIG_METHOD_FIXED:
239 connman_network_set_ipv4_method(modem->network,
240 modem->context->ipv4_method);
241 connman_network_set_ipaddress(modem->network,
242 modem->context->ipv4_address);
243 connman_network_set_nameservers(modem->network,
244 modem->context->ipv4_nameservers);
248 case CONNMAN_IPCONFIG_METHOD_DHCP:
249 connman_network_set_ipv4_method(modem->network,
250 modem->context->ipv4_method);
255 switch (modem->context->ipv6_method) {
256 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
257 case CONNMAN_IPCONFIG_METHOD_OFF:
258 case CONNMAN_IPCONFIG_METHOD_MANUAL:
259 case CONNMAN_IPCONFIG_METHOD_DHCP:
260 case CONNMAN_IPCONFIG_METHOD_AUTO:
263 case CONNMAN_IPCONFIG_METHOD_FIXED:
264 connman_network_set_ipv6_method(modem->network,
265 modem->context->ipv6_method);
266 connman_network_set_ipaddress(modem->network,
267 modem->context->ipv6_address);
273 connman_network_set_connected(modem->network, TRUE);
276 static void set_disconnected(struct modem_data *modem)
278 DBG("%s", modem->path);
280 connman_network_set_connected(modem->network, FALSE);
283 typedef void (*set_property_cb)(struct modem_data *data,
284 connman_bool_t success);
285 typedef void (*get_properties_cb)(struct modem_data *data,
286 DBusMessageIter *dict);
288 struct property_info {
289 struct modem_data *modem;
291 const char *interface;
292 const char *property;
293 set_property_cb set_property_cb;
294 get_properties_cb get_properties_cb;
297 static void set_property_reply(DBusPendingCall *call, void *user_data)
299 struct property_info *info = user_data;
302 connman_bool_t success = TRUE;
304 DBG("%s path %s %s.%s", info->modem->path,
305 info->path, info->interface, info->property);
307 info->modem->call_set_property = NULL;
309 dbus_error_init(&error);
311 reply = dbus_pending_call_steal_reply(call);
313 if (dbus_set_error_from_message(&error, reply)) {
314 connman_error("Failed to change property: %s %s.%s: %s %s",
315 info->path, info->interface, info->property,
316 error.name, error.message);
317 dbus_error_free(&error);
321 if (info->set_property_cb != NULL)
322 (*info->set_property_cb)(info->modem, success);
324 dbus_message_unref(reply);
326 dbus_pending_call_unref(call);
329 static int set_property(struct modem_data *modem,
330 const char *path, const char *interface,
331 const char *property, int type, void *value,
332 set_property_cb notify)
334 DBusMessage *message;
335 DBusMessageIter iter;
336 struct property_info *info;
338 DBG("%s path %s %s.%s", modem->path, path, interface, property);
340 if (modem->call_set_property != NULL) {
341 connman_error("Pending SetProperty");
345 message = dbus_message_new_method_call(OFONO_SERVICE, path,
346 interface, SET_PROPERTY);
350 dbus_message_iter_init_append(message, &iter);
351 connman_dbus_property_append_basic(&iter, property, type, value);
353 if (dbus_connection_send_with_reply(connection, message,
354 &modem->call_set_property, TIMEOUT) == FALSE) {
355 connman_error("Failed to change property: %s %s.%s",
356 path, interface, property);
357 dbus_message_unref(message);
361 if (modem->call_set_property == NULL) {
362 connman_error("D-Bus connection not available");
363 dbus_message_unref(message);
367 info = g_try_new0(struct property_info, 1);
369 dbus_message_unref(message);
375 info->interface = interface;
376 info->property = property;
377 info->set_property_cb = notify;
379 dbus_pending_call_set_notify(modem->call_set_property,
380 set_property_reply, info, g_free);
382 dbus_message_unref(message);
387 static void get_properties_reply(DBusPendingCall *call, void *user_data)
389 struct property_info *info = user_data;
390 DBusMessageIter array, dict;
394 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
396 info->modem->call_get_properties = NULL;
398 dbus_error_init(&error);
400 reply = dbus_pending_call_steal_reply(call);
402 if (dbus_set_error_from_message(&error, reply)) {
403 connman_error("Failed to get properties: %s %s: %s %s",
404 info->path, info->interface,
405 error.name, error.message);
406 dbus_error_free(&error);
411 if (dbus_message_iter_init(reply, &array) == FALSE)
414 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
417 dbus_message_iter_recurse(&array, &dict);
419 if (info->get_properties_cb != NULL)
420 (*info->get_properties_cb)(info->modem, &dict);
424 dbus_message_unref(reply);
426 dbus_pending_call_unref(call);
429 static int get_properties(const char *path, const char *interface,
430 get_properties_cb notify,
431 struct modem_data *modem)
433 DBusMessage *message;
434 struct property_info *info;
436 DBG("%s path %s %s", modem->path, path, interface);
438 if (modem->call_get_properties != NULL) {
439 connman_error("Pending GetProperties");
443 message = dbus_message_new_method_call(OFONO_SERVICE, path,
444 interface, GET_PROPERTIES);
448 if (dbus_connection_send_with_reply(connection, message,
449 &modem->call_get_properties, TIMEOUT) == FALSE) {
450 connman_error("Failed to call %s.GetProperties()", interface);
451 dbus_message_unref(message);
455 if (modem->call_get_properties == NULL) {
456 connman_error("D-Bus connection not available");
457 dbus_message_unref(message);
461 info = g_try_new0(struct property_info, 1);
463 dbus_message_unref(message);
469 info->interface = interface;
470 info->get_properties_cb = notify;
472 dbus_pending_call_set_notify(modem->call_get_properties,
473 get_properties_reply, info, g_free);
475 dbus_message_unref(message);
480 static void context_set_active_reply(struct modem_data *modem,
481 connman_bool_t success)
483 DBG("%s", modem->path);
485 if (success == TRUE) {
487 * Don't handle do anything on success here. oFono will send
488 * the change via PropertyChanged singal.
494 * Active = True might fail due a timeout. That means oFono
495 * still tries to go online. If we retry to set Active = True,
496 * we just get a InProgress error message. Should we power
497 * cycle the modem in such cases?
500 if (modem->network == NULL) {
502 * In the case where we power down the device
503 * we don't wait for the reply, therefore the network
504 * might already be gone.
509 connman_network_set_error(modem->network,
510 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
513 static int context_set_active(struct modem_data *modem,
514 connman_bool_t active)
518 DBG("%s active %d", modem->path, active);
520 err = set_property(modem, modem->context->path,
521 OFONO_CONTEXT_INTERFACE,
522 "Active", DBUS_TYPE_BOOLEAN,
524 context_set_active_reply);
526 if (active == FALSE && err == -EINPROGRESS)
532 static void cdma_cm_set_powered_reply(struct modem_data *modem,
533 connman_bool_t success)
535 DBG("%s", modem->path);
537 if (success == TRUE) {
539 * Don't handle do anything on success here. oFono will send
540 * the change via PropertyChanged singal.
546 * Powered = True might fail due a timeout. That means oFono
547 * still tries to go online. If we retry to set Powered = True,
548 * we just get a InProgress error message. Should we power
549 * cycle the modem in such cases?
552 if (modem->network == NULL) {
554 * In the case where we power down the device
555 * we don't wait for the reply, therefore the network
556 * might already be gone.
561 connman_network_set_error(modem->network,
562 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
565 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
569 DBG("%s powered %d", modem->path, powered);
571 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
572 "Powered", DBUS_TYPE_BOOLEAN,
574 cdma_cm_set_powered_reply);
576 if (powered == FALSE && err == -EINPROGRESS)
582 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
584 DBG("%s online %d", modem->path, online);
586 return set_property(modem, modem->path,
587 OFONO_MODEM_INTERFACE,
588 "Online", DBUS_TYPE_BOOLEAN,
593 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
597 DBG("%s powered %d", modem->path, powered);
599 err = set_property(modem, modem->path,
601 "Powered", DBUS_TYPE_BOOLEAN,
605 if (powered == FALSE && err == -EINPROGRESS)
611 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
615 DBG("%s powered %d", modem->path, powered);
617 modem->set_powered = powered;
619 err = set_property(modem, modem->path,
620 OFONO_MODEM_INTERFACE,
621 "Powered", DBUS_TYPE_BOOLEAN,
625 if (powered == FALSE && err == -EINPROGRESS)
631 static connman_bool_t has_interface(uint8_t interfaces,
634 if ((interfaces & api) == api)
640 static uint8_t extract_interfaces(DBusMessageIter *array)
642 DBusMessageIter entry;
643 uint8_t interfaces = 0;
645 dbus_message_iter_recurse(array, &entry);
647 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
650 dbus_message_iter_get_basic(&entry, &name);
652 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
653 interfaces |= OFONO_API_SIM;
654 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
655 interfaces |= OFONO_API_NETREG;
656 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
657 interfaces |= OFONO_API_CM;
658 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
659 interfaces |= OFONO_API_CDMA_CM;
660 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
661 interfaces |= OFONO_API_CDMA_NETREG;
663 dbus_message_iter_next(&entry);
669 static char *extract_nameservers(DBusMessageIter *array)
671 DBusMessageIter entry;
672 char *nameservers = NULL;
675 dbus_message_iter_recurse(array, &entry);
677 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
678 const char *nameserver;
680 dbus_message_iter_get_basic(&entry, &nameserver);
682 if (nameservers == NULL) {
683 nameservers = g_strdup(nameserver);
686 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
690 dbus_message_iter_next(&entry);
696 static void extract_ipv4_settings(DBusMessageIter *array,
697 struct network_context *context)
699 DBusMessageIter dict;
700 char *address = NULL, *netmask = NULL, *gateway = NULL;
701 char *nameservers = NULL;
702 const char *interface = NULL;
705 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
708 dbus_message_iter_recurse(array, &dict);
710 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
711 DBusMessageIter entry, value;
712 const char *key, *val;
714 dbus_message_iter_recurse(&dict, &entry);
715 dbus_message_iter_get_basic(&entry, &key);
717 dbus_message_iter_next(&entry);
718 dbus_message_iter_recurse(&entry, &value);
720 if (g_str_equal(key, "Interface") == TRUE) {
721 dbus_message_iter_get_basic(&value, &interface);
723 DBG("Interface %s", interface);
725 index = connman_inet_ifindex(interface);
727 DBG("index %d", index);
728 } else if (g_str_equal(key, "Method") == TRUE) {
729 dbus_message_iter_get_basic(&value, &val);
731 DBG("Method %s", val);
733 if (g_strcmp0(val, "static") == 0) {
734 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
735 } else if (g_strcmp0(val, "dhcp") == 0) {
736 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
739 } else if (g_str_equal(key, "Address") == TRUE) {
740 dbus_message_iter_get_basic(&value, &val);
742 address = g_strdup(val);
744 DBG("Address %s", address);
745 } else if (g_str_equal(key, "Netmask") == TRUE) {
746 dbus_message_iter_get_basic(&value, &val);
748 netmask = g_strdup(val);
750 DBG("Netmask %s", netmask);
751 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
752 nameservers = extract_nameservers(&value);
754 DBG("Nameservers %s", nameservers);
755 } else if (g_str_equal(key, "Gateway") == TRUE) {
756 dbus_message_iter_get_basic(&value, &val);
758 gateway = g_strdup(val);
760 DBG("Gateway %s", gateway);
763 dbus_message_iter_next(&dict);
769 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
772 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
773 if (context->ipv4_address == NULL)
776 context->index = index;
777 connman_ipaddress_set_ipv4(context->ipv4_address, address,
780 context->ipv4_nameservers = nameservers;
783 if (context->ipv4_nameservers != nameservers)
791 static void extract_ipv6_settings(DBusMessageIter *array,
792 struct network_context *context)
794 DBusMessageIter dict;
795 char *address = NULL, *gateway = NULL;
796 unsigned char prefix_length;
797 char *nameservers = NULL;
798 const char *interface = NULL;
801 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
804 dbus_message_iter_recurse(array, &dict);
806 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
807 DBusMessageIter entry, value;
808 const char *key, *val;
810 dbus_message_iter_recurse(&dict, &entry);
811 dbus_message_iter_get_basic(&entry, &key);
813 dbus_message_iter_next(&entry);
814 dbus_message_iter_recurse(&entry, &value);
816 if (g_str_equal(key, "Interface") == TRUE) {
817 dbus_message_iter_get_basic(&value, &interface);
819 DBG("Interface %s", interface);
821 index = connman_inet_ifindex(interface);
823 DBG("index %d", index);
824 } else if (g_str_equal(key, "Address") == TRUE) {
825 dbus_message_iter_get_basic(&value, &val);
827 address = g_strdup(val);
829 DBG("Address %s", address);
830 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
831 dbus_message_iter_get_basic(&value, &prefix_length);
833 DBG("prefix length %d", prefix_length);
834 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
835 nameservers = extract_nameservers(&value);
837 DBG("Nameservers %s", nameservers);
838 } else if (g_str_equal(key, "Gateway") == TRUE) {
839 dbus_message_iter_get_basic(&value, &val);
841 gateway = g_strdup(val);
843 DBG("Gateway %s", gateway);
846 dbus_message_iter_next(&dict);
852 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
854 context->ipv6_address =
855 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
856 if (context->ipv6_address == NULL)
859 context->index = index;
860 connman_ipaddress_set_ipv6(context->ipv6_address, address,
861 prefix_length, gateway);
863 context->ipv6_nameservers = nameservers;
866 if (context->ipv6_nameservers != nameservers)
873 static connman_bool_t ready_to_create_device(struct modem_data *modem)
876 * There are three different modem types which behave slightly
878 * - GSM modems will expose the SIM interface then the
880 * - DUN modems will expose first a unique serial number (BDADDR)
881 * and then the CM interface.
882 * - CDMA modems will expose CM first and sometime later
883 * a unique serial number.
885 * This functions tests if we have the necessary information gathered
886 * before we are able to create a device.
889 if (modem->device != NULL)
892 if (modem->imsi != NULL || modem->serial != NULL)
898 static void create_device(struct modem_data *modem)
900 struct connman_device *device;
901 char *uninitialized_var(ident);
903 DBG("%s", modem->path);
905 if (modem->imsi != NULL)
907 else if (modem->serial != NULL)
908 ident = modem->serial;
910 if (connman_dbus_validate_ident(ident) == FALSE)
911 ident = connman_dbus_encode_string(ident);
913 ident = g_strdup(ident);
915 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
919 DBG("device %p", device);
921 connman_device_set_ident(device, ident);
923 connman_device_set_string(device, "Path", modem->path);
925 connman_device_set_data(device, modem);
927 if (connman_device_register(device) < 0) {
928 connman_error("Failed to register cellular device");
929 connman_device_unref(device);
933 modem->device = device;
939 static void destroy_device(struct modem_data *modem)
941 DBG("%s", modem->path);
943 connman_device_set_powered(modem->device, FALSE);
945 if (modem->network != NULL) {
946 connman_device_remove_network(modem->device, modem->network);
947 connman_network_unref(modem->network);
948 modem->network = NULL;
951 connman_device_unregister(modem->device);
952 connman_device_unref(modem->device);
954 modem->device = NULL;
957 static void add_network(struct modem_data *modem)
961 DBG("%s", modem->path);
963 if (modem->network != NULL)
966 modem->network = connman_network_create(modem->context->path,
967 CONNMAN_NETWORK_TYPE_CELLULAR);
968 if (modem->network == NULL)
971 DBG("network %p", modem->network);
973 connman_network_set_data(modem->network, modem);
975 connman_network_set_string(modem->network, "Path",
976 modem->context->path);
978 connman_network_set_index(modem->network, modem->context->index);
980 if (modem->name != NULL)
981 connman_network_set_name(modem->network, modem->name);
983 connman_network_set_name(modem->network, "");
985 connman_network_set_strength(modem->network, modem->strength);
987 group = get_ident(modem->context->path);
988 connman_network_set_group(modem->network, group);
990 connman_network_set_available(modem->network, TRUE);
992 connman_network_set_bool(modem->network, "Roaming",
995 if (connman_device_add_network(modem->device, modem->network) < 0) {
996 connman_network_unref(modem->network);
997 modem->network = NULL;
1002 static void remove_network(struct modem_data *modem)
1004 DBG("%s", modem->path);
1006 if (modem->network == NULL)
1009 DBG("network %p", modem->network);
1011 connman_device_remove_network(modem->device, modem->network);
1012 connman_network_unref(modem->network);
1013 modem->network = NULL;
1016 static int add_cm_context(struct modem_data *modem, const char *context_path,
1017 DBusMessageIter *dict)
1019 const char *context_type;
1020 struct network_context *context = NULL;
1021 connman_bool_t active = FALSE;
1023 DBG("%s context path %s", modem->path, context_path);
1025 if (modem->context != NULL) {
1027 * We have already assigned a context to this modem
1028 * and we do only support one Internet context.
1033 context = network_context_alloc(context_path);
1034 if (context == NULL)
1037 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1038 DBusMessageIter entry, value;
1041 dbus_message_iter_recurse(dict, &entry);
1042 dbus_message_iter_get_basic(&entry, &key);
1044 dbus_message_iter_next(&entry);
1045 dbus_message_iter_recurse(&entry, &value);
1047 if (g_str_equal(key, "Type") == TRUE) {
1048 dbus_message_iter_get_basic(&value, &context_type);
1050 DBG("%s context %s type %s", modem->path,
1051 context_path, context_type);
1052 } else if (g_str_equal(key, "Settings") == TRUE) {
1053 DBG("%s Settings", modem->path);
1055 extract_ipv4_settings(&value, context);
1056 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1057 DBG("%s IPv6.Settings", modem->path);
1059 extract_ipv6_settings(&value, context);
1060 } else if (g_str_equal(key, "Active") == TRUE) {
1061 dbus_message_iter_get_basic(&value, &active);
1063 DBG("%s Active %d", modem->path, active);
1066 dbus_message_iter_next(dict);
1069 if (g_strcmp0(context_type, "internet") != 0) {
1070 network_context_free(context);
1074 modem->context = context;
1075 modem->active = active;
1077 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1082 static void remove_cm_context(struct modem_data *modem,
1083 const char *context_path)
1085 if (modem->context == NULL)
1088 g_hash_table_remove(context_hash, context_path);
1090 network_context_free(modem->context);
1091 modem->context = NULL;
1094 static gboolean context_changed(DBusConnection *connection,
1095 DBusMessage *message,
1098 const char *context_path = dbus_message_get_path(message);
1099 struct modem_data *modem = NULL;
1100 DBusMessageIter iter, value;
1103 DBG("context_path %s", context_path);
1105 modem = g_hash_table_lookup(context_hash, context_path);
1109 if (dbus_message_iter_init(message, &iter) == FALSE)
1112 dbus_message_iter_get_basic(&iter, &key);
1114 dbus_message_iter_next(&iter);
1115 dbus_message_iter_recurse(&iter, &value);
1118 * oFono guarantees the ordering of Settings and
1119 * Active. Settings will always be send before Active = True.
1120 * That means we don't have to order here.
1122 if (g_str_equal(key, "Settings") == TRUE) {
1123 DBG("%s Settings", modem->path);
1125 extract_ipv4_settings(&value, modem->context);
1126 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1127 DBG("%s IPv6.Settings", modem->path);
1129 extract_ipv6_settings(&value, modem->context);
1130 } else if (g_str_equal(key, "Active") == TRUE) {
1131 dbus_message_iter_get_basic(&value, &modem->active);
1133 DBG("%s Active %d", modem->path, modem->active);
1135 if (modem->active == TRUE)
1136 set_connected(modem);
1138 set_disconnected(modem);
1144 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1146 struct modem_data *modem = user_data;
1147 DBusMessageIter array, dict, entry, value;
1151 DBG("%s", modem->path);
1153 modem->call_get_contexts = NULL;
1155 reply = dbus_pending_call_steal_reply(call);
1157 dbus_error_init(&error);
1159 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1160 connman_error("%s", error.message);
1161 dbus_error_free(&error);
1165 if (dbus_message_iter_init(reply, &array) == FALSE)
1168 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1171 dbus_message_iter_recurse(&array, &dict);
1173 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1174 const char *context_path;
1176 dbus_message_iter_recurse(&dict, &entry);
1177 dbus_message_iter_get_basic(&entry, &context_path);
1179 dbus_message_iter_next(&entry);
1180 dbus_message_iter_recurse(&entry, &value);
1182 if (add_cm_context(modem, context_path, &value) == 0)
1185 dbus_message_iter_next(&dict);
1189 dbus_message_unref(reply);
1191 dbus_pending_call_unref(call);
1194 static int cm_get_contexts(struct modem_data *modem)
1196 DBusMessage *message;
1198 DBG("%s", modem->path);
1200 if (modem->call_get_contexts != NULL)
1203 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1204 OFONO_CM_INTERFACE, GET_CONTEXTS);
1205 if (message == NULL)
1208 if (dbus_connection_send_with_reply(connection, message,
1209 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1210 connman_error("Failed to call GetContexts()");
1211 dbus_message_unref(message);
1215 if (modem->call_get_contexts == NULL) {
1216 connman_error("D-Bus connection not available");
1217 dbus_message_unref(message);
1221 dbus_pending_call_set_notify(modem->call_get_contexts,
1222 cm_get_contexts_reply,
1225 dbus_message_unref(message);
1227 return -EINPROGRESS;
1230 static gboolean cm_context_added(DBusConnection *connection,
1231 DBusMessage *message,
1234 const char *path = dbus_message_get_path(message);
1236 struct modem_data *modem;
1237 DBusMessageIter iter, properties;
1241 modem = g_hash_table_lookup(modem_hash, context_path);
1245 if (dbus_message_iter_init(message, &iter) == FALSE)
1248 dbus_message_iter_get_basic(&iter, &context_path);
1250 dbus_message_iter_next(&iter);
1251 dbus_message_iter_recurse(&iter, &properties);
1253 if (add_cm_context(modem, context_path, &properties) != 0)
1259 static gboolean cm_context_removed(DBusConnection *connection,
1260 DBusMessage *message,
1263 const char *path = dbus_message_get_path(message);
1264 const char *context_path;
1265 struct modem_data *modem;
1266 DBusMessageIter iter;
1268 DBG("context path %s", path);
1270 if (dbus_message_iter_init(message, &iter) == FALSE)
1273 dbus_message_iter_get_basic(&iter, &context_path);
1275 modem = g_hash_table_lookup(context_hash, context_path);
1279 remove_cm_context(modem, context_path);
1284 static void netreg_update_name(struct modem_data *modem,
1285 DBusMessageIter* value)
1289 dbus_message_iter_get_basic(value, &name);
1291 DBG("%s Name %s", modem->path, name);
1293 g_free(modem->name);
1294 modem->name = g_strdup(name);
1296 if (modem->network == NULL)
1299 connman_network_set_name(modem->network, modem->name);
1300 connman_network_update(modem->network);
1303 static void netreg_update_strength(struct modem_data *modem,
1304 DBusMessageIter *value)
1306 dbus_message_iter_get_basic(value, &modem->strength);
1308 DBG("%s Strength %d", modem->path, modem->strength);
1310 if (modem->network == NULL)
1315 * We don't have 2 signal notifications we always report the strength
1316 * signal. data_strength is always equal to 0.
1319 * In the case we have a data_strength signal (from 1xEVDO network)
1320 * we don't need to update the value with strength signal (from 1xCDMA)
1321 * because the modem is registered to 1xEVDO network for data call.
1322 * In case we have no data_strength signal (not registered to 1xEVDO
1323 * network), we must report the strength signal (registered to 1xCDMA
1324 * network e.g slow mode).
1326 if (modem->data_strength != 0)
1329 connman_network_set_strength(modem->network, modem->strength);
1330 connman_network_update(modem->network);
1333 /* Retrieve 1xEVDO Data Strength signal */
1334 static void netreg_update_datastrength(struct modem_data *modem,
1335 DBusMessageIter *value)
1337 dbus_message_iter_get_basic(value, &modem->data_strength);
1339 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1341 if (modem->network == NULL)
1345 * CDMA modem is not registered to 1xEVDO network, let
1346 * update_signal_strength() reporting the value on the Strength signal
1349 if (modem->data_strength == 0)
1352 connman_network_set_strength(modem->network, modem->data_strength);
1353 connman_network_update(modem->network);
1356 static void netreg_update_roaming(struct modem_data *modem,
1357 DBusMessageIter *value)
1360 connman_bool_t roaming;
1362 dbus_message_iter_get_basic(value, &status);
1364 if (g_str_equal(status, "roaming") == TRUE)
1369 if (roaming == modem->roaming)
1372 modem->roaming = roaming;
1374 if (modem->network == NULL)
1377 connman_network_set_bool(modem->network,
1378 "Roaming", modem->roaming);
1379 connman_network_update(modem->network);
1382 static void netreg_update_regdom(struct modem_data *modem,
1383 DBusMessageIter *value)
1385 char *mobile_country_code;
1389 dbus_message_iter_get_basic(value, &mobile_country_code);
1391 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1394 mcc = atoi(mobile_country_code);
1398 alpha2 = mcc_country_codes[mcc - 200];
1399 connman_technology_set_regdom(alpha2);
1402 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1405 const char *path = dbus_message_get_path(message);
1406 struct modem_data *modem;
1407 DBusMessageIter iter, value;
1410 modem = g_hash_table_lookup(modem_hash, path);
1414 if (modem->ignore == TRUE)
1417 if (dbus_message_iter_init(message, &iter) == FALSE)
1420 dbus_message_iter_get_basic(&iter, &key);
1422 dbus_message_iter_next(&iter);
1423 dbus_message_iter_recurse(&iter, &value);
1425 if (g_str_equal(key, "Name") == TRUE)
1426 netreg_update_name(modem, &value);
1427 else if (g_str_equal(key, "Strength") == TRUE)
1428 netreg_update_strength(modem, &value);
1429 else if (g_str_equal(key, "Status") == TRUE)
1430 netreg_update_roaming(modem, &value);
1431 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1432 netreg_update_regdom(modem, &value);
1437 static void netreg_properties_reply(struct modem_data *modem,
1438 DBusMessageIter *dict)
1440 DBG("%s", modem->path);
1442 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1443 DBusMessageIter entry, value;
1446 dbus_message_iter_recurse(dict, &entry);
1447 dbus_message_iter_get_basic(&entry, &key);
1449 dbus_message_iter_next(&entry);
1450 dbus_message_iter_recurse(&entry, &value);
1452 if (g_str_equal(key, "Name") == TRUE)
1453 netreg_update_name(modem, &value);
1454 else if (g_str_equal(key, "Strength") == TRUE)
1455 netreg_update_strength(modem, &value);
1456 else if (g_str_equal(key, "Status") == TRUE)
1457 netreg_update_roaming(modem, &value);
1458 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1459 netreg_update_regdom(modem, &value);
1461 dbus_message_iter_next(dict);
1464 if (modem->context == NULL) {
1466 * netgreg_get_properties() was issued after we got
1467 * cm_get_contexts_reply() where we create the
1468 * context. Though before we got the
1469 * netreg_properties_reply the context was removed
1470 * again. Therefore we have to skip the network
1478 if (modem->active == TRUE)
1479 set_connected(modem);
1482 static int netreg_get_properties(struct modem_data *modem)
1484 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1485 netreg_properties_reply, modem);
1488 static void add_cdma_network(struct modem_data *modem)
1490 /* Be sure that device is created before adding CDMA network */
1491 if (modem->device == NULL)
1495 * CDMA modems don't need contexts for data call, however the current
1496 * add_network() logic needs one, so we create one to proceed.
1498 if (modem->context == NULL)
1499 modem->context = network_context_alloc(modem->path);
1503 if (modem->cdma_cm_powered == TRUE)
1504 set_connected(modem);
1507 static gboolean cdma_netreg_changed(DBusConnection *connection,
1508 DBusMessage *message,
1511 const char *path = dbus_message_get_path(message);
1512 struct modem_data *modem;
1513 DBusMessageIter iter, value;
1518 modem = g_hash_table_lookup(modem_hash, path);
1522 if (modem->ignore == TRUE)
1525 if (dbus_message_iter_init(message, &iter) == FALSE)
1528 dbus_message_iter_get_basic(&iter, &key);
1530 dbus_message_iter_next(&iter);
1531 dbus_message_iter_recurse(&iter, &value);
1533 if (g_str_equal(key, "Name") == TRUE)
1534 netreg_update_name(modem, &value);
1535 else if (g_str_equal(key, "Strength") == TRUE)
1536 netreg_update_strength(modem, &value);
1537 else if (g_str_equal(key, "DataStrength") == TRUE)
1538 netreg_update_datastrength(modem, &value);
1539 else if (g_str_equal(key, "Status") == TRUE)
1540 netreg_update_roaming(modem, &value);
1542 add_cdma_network(modem);
1547 static void cdma_netreg_properties_reply(struct modem_data *modem,
1548 DBusMessageIter *dict)
1550 DBG("%s", modem->path);
1552 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1553 DBusMessageIter entry, value;
1556 dbus_message_iter_recurse(dict, &entry);
1557 dbus_message_iter_get_basic(&entry, &key);
1559 dbus_message_iter_next(&entry);
1560 dbus_message_iter_recurse(&entry, &value);
1562 if (g_str_equal(key, "Name") == TRUE)
1563 netreg_update_name(modem, &value);
1564 else if (g_str_equal(key, "Strength") == TRUE)
1565 netreg_update_strength(modem, &value);
1566 else if (g_str_equal(key, "DataStrength") == TRUE)
1567 netreg_update_datastrength(modem, &value);
1568 else if (g_str_equal(key, "Status") == TRUE)
1569 netreg_update_roaming(modem, &value);
1571 dbus_message_iter_next(dict);
1574 add_cdma_network(modem);
1577 static int cdma_netreg_get_properties(struct modem_data *modem)
1579 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1580 cdma_netreg_properties_reply, modem);
1583 static void cm_update_attached(struct modem_data *modem,
1584 DBusMessageIter *value)
1586 dbus_message_iter_get_basic(value, &modem->attached);
1588 DBG("%s Attached %d", modem->path, modem->attached);
1590 if (modem->attached == FALSE)
1593 if (has_interface(modem->interfaces,
1594 OFONO_API_NETREG) == FALSE) {
1598 netreg_get_properties(modem);
1601 static void cm_update_powered(struct modem_data *modem,
1602 DBusMessageIter *value)
1604 dbus_message_iter_get_basic(value, &modem->cm_powered);
1606 DBG("%s ConnnectionManager Powered %d", modem->path,
1609 if (modem->cm_powered == TRUE)
1612 cm_set_powered(modem, TRUE);
1615 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1618 const char *path = dbus_message_get_path(message);
1619 struct modem_data *modem;
1620 DBusMessageIter iter, value;
1623 modem = g_hash_table_lookup(modem_hash, path);
1627 if (modem->ignore == TRUE)
1630 if (dbus_message_iter_init(message, &iter) == FALSE)
1633 dbus_message_iter_get_basic(&iter, &key);
1635 dbus_message_iter_next(&iter);
1636 dbus_message_iter_recurse(&iter, &value);
1638 if (g_str_equal(key, "Attached") == TRUE)
1639 cm_update_attached(modem, &value);
1640 else if (g_str_equal(key, "Powered") == TRUE)
1641 cm_update_powered(modem, &value);
1646 static void cdma_cm_update_powered(struct modem_data *modem,
1647 DBusMessageIter *value)
1649 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1651 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1653 if (modem->network == NULL)
1656 if (modem->cdma_cm_powered == TRUE)
1657 set_connected(modem);
1659 set_disconnected(modem);
1662 static void cdma_cm_update_settings(struct modem_data *modem,
1663 DBusMessageIter *value)
1665 DBG("%s Settings", modem->path);
1667 if (modem->context != NULL)
1670 extract_ipv4_settings(value, modem->context);
1673 static gboolean cdma_cm_changed(DBusConnection *connection,
1674 DBusMessage *message, void *user_data)
1676 const char *path = dbus_message_get_path(message);
1677 struct modem_data *modem;
1678 DBusMessageIter iter, value;
1681 modem = g_hash_table_lookup(modem_hash, path);
1685 if (modem->online == TRUE && modem->network == NULL)
1686 cdma_netreg_get_properties(modem);
1688 if (dbus_message_iter_init(message, &iter) == FALSE)
1691 dbus_message_iter_get_basic(&iter, &key);
1693 dbus_message_iter_next(&iter);
1694 dbus_message_iter_recurse(&iter, &value);
1696 if (g_str_equal(key, "Powered") == TRUE)
1697 cdma_cm_update_powered(modem, &value);
1698 if (g_str_equal(key, "Settings") == TRUE)
1699 cdma_cm_update_settings(modem, &value);
1704 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1706 DBG("%s", modem->path);
1708 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1709 DBusMessageIter entry, value;
1712 dbus_message_iter_recurse(dict, &entry);
1713 dbus_message_iter_get_basic(&entry, &key);
1715 dbus_message_iter_next(&entry);
1716 dbus_message_iter_recurse(&entry, &value);
1718 if (g_str_equal(key, "Attached") == TRUE)
1719 cm_update_attached(modem, &value);
1720 else if (g_str_equal(key, "Powered") == TRUE)
1721 cm_update_powered(modem, &value);
1723 dbus_message_iter_next(dict);
1727 static int cm_get_properties(struct modem_data *modem)
1729 return get_properties(modem->path, OFONO_CM_INTERFACE,
1730 cm_properties_reply, modem);
1733 static void cdma_cm_properties_reply(struct modem_data *modem,
1734 DBusMessageIter *dict)
1736 DBG("%s", modem->path);
1738 if (modem->online == TRUE)
1739 cdma_netreg_get_properties(modem);
1741 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1742 DBusMessageIter entry, value;
1745 dbus_message_iter_recurse(dict, &entry);
1746 dbus_message_iter_get_basic(&entry, &key);
1748 dbus_message_iter_next(&entry);
1749 dbus_message_iter_recurse(&entry, &value);
1751 if (g_str_equal(key, "Powered") == TRUE)
1752 cdma_cm_update_powered(modem, &value);
1753 if (g_str_equal(key, "Settings") == TRUE)
1754 cdma_cm_update_settings(modem, &value);
1756 dbus_message_iter_next(dict);
1760 static int cdma_cm_get_properties(struct modem_data *modem)
1762 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1763 cdma_cm_properties_reply, modem);
1766 static void sim_update_imsi(struct modem_data *modem,
1767 DBusMessageIter* value)
1771 dbus_message_iter_get_basic(value, &imsi);
1773 DBG("%s imsi %s", modem->path, imsi);
1775 g_free(modem->imsi);
1776 modem->imsi = g_strdup(imsi);
1779 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1782 const char *path = dbus_message_get_path(message);
1783 struct modem_data *modem;
1784 DBusMessageIter iter, value;
1787 modem = g_hash_table_lookup(modem_hash, path);
1791 if (modem->ignore == TRUE)
1794 if (dbus_message_iter_init(message, &iter) == FALSE)
1797 dbus_message_iter_get_basic(&iter, &key);
1799 dbus_message_iter_next(&iter);
1800 dbus_message_iter_recurse(&iter, &value);
1802 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1803 sim_update_imsi(modem, &value);
1805 if (ready_to_create_device(modem) == FALSE)
1809 * This is a GSM modem. Create the device and
1810 * register it at the core. Enabling (setting
1811 * it online is done through the
1812 * modem_enable() callback.
1814 create_device(modem);
1820 static void sim_properties_reply(struct modem_data *modem,
1821 DBusMessageIter *dict)
1823 DBG("%s", modem->path);
1825 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1826 DBusMessageIter entry, value;
1829 dbus_message_iter_recurse(dict, &entry);
1830 dbus_message_iter_get_basic(&entry, &key);
1832 dbus_message_iter_next(&entry);
1833 dbus_message_iter_recurse(&entry, &value);
1835 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1836 sim_update_imsi(modem, &value);
1838 if (ready_to_create_device(modem) == FALSE)
1842 * This is a GSM modem. Create the device and
1843 * register it at the core. Enabling (setting
1844 * it online is done through the
1845 * modem_enable() callback.
1847 create_device(modem);
1851 dbus_message_iter_next(dict);
1855 static int sim_get_properties(struct modem_data *modem)
1857 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1858 sim_properties_reply, modem);
1861 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1864 if (has_interface(old_iface, api) == FALSE &&
1865 has_interface(new_iface, api) == TRUE) {
1866 DBG("%s added", api2string(api));
1873 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1876 if (has_interface(old_iface, api) == TRUE &&
1877 has_interface(new_iface, api) == FALSE) {
1878 DBG("%s removed", api2string(api));
1885 static void modem_update_interfaces(struct modem_data *modem,
1889 DBG("%s", modem->path);
1891 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
1892 if (modem->imsi == NULL &&
1893 modem->set_powered == FALSE) {
1895 * Only use do GetProperties() when
1896 * device has not been powered up.
1898 sim_get_properties(modem);
1902 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1903 if (modem->device != NULL) {
1904 cm_get_properties(modem);
1905 cm_get_contexts(modem);
1909 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1910 if (ready_to_create_device(modem) == TRUE)
1911 create_device(modem);
1913 if (modem->device != NULL)
1914 cdma_cm_get_properties(modem);
1917 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1918 if (modem->attached == TRUE)
1919 netreg_get_properties(modem);
1922 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
1923 cdma_netreg_get_properties(modem);
1926 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1927 remove_cm_context(modem, modem->context->path);
1930 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1931 remove_cm_context(modem, modem->context->path);
1934 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1935 remove_network(modem);
1938 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
1939 remove_network(modem);
1943 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1946 const char *path = dbus_message_get_path(message);
1947 struct modem_data *modem;
1948 DBusMessageIter iter, value;
1951 modem = g_hash_table_lookup(modem_hash, path);
1955 if (modem->ignore == TRUE)
1958 if (dbus_message_iter_init(message, &iter) == FALSE)
1961 dbus_message_iter_get_basic(&iter, &key);
1963 dbus_message_iter_next(&iter);
1964 dbus_message_iter_recurse(&iter, &value);
1966 if (g_str_equal(key, "Powered") == TRUE) {
1967 dbus_message_iter_get_basic(&value, &modem->powered);
1969 DBG("%s Powered %d", modem->path, modem->powered);
1971 if (modem->powered == FALSE)
1972 modem_set_powered(modem, TRUE);
1973 } else if (g_str_equal(key, "Online") == TRUE) {
1974 dbus_message_iter_get_basic(&value, &modem->online);
1976 DBG("%s Online %d", modem->path, modem->online);
1977 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1980 interfaces = extract_interfaces(&value);
1982 if (interfaces == modem->interfaces)
1985 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
1987 modem_update_interfaces(modem, modem->interfaces, interfaces);
1989 modem->interfaces = interfaces;
1990 } else if (g_str_equal(key, "Serial") == TRUE) {
1993 dbus_message_iter_get_basic(&value, &serial);
1995 g_free(modem->serial);
1996 modem->serial = g_strdup(serial);
1998 DBG("%s Serial %s", modem->path, modem->serial);
2004 static void add_modem(const char *path, DBusMessageIter *prop)
2006 struct modem_data *modem;
2010 modem = g_hash_table_lookup(modem_hash, path);
2011 if (modem != NULL) {
2013 * When oFono powers up we ask for the modems and oFono is
2014 * reporting with modem_added signal the modems. Only
2020 modem = g_try_new0(struct modem_data, 1);
2024 modem->path = g_strdup(path);
2026 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2028 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2029 DBusMessageIter entry, value;
2032 dbus_message_iter_recurse(prop, &entry);
2033 dbus_message_iter_get_basic(&entry, &key);
2035 dbus_message_iter_next(&entry);
2036 dbus_message_iter_recurse(&entry, &value);
2038 if (g_str_equal(key, "Powered") == TRUE) {
2039 dbus_message_iter_get_basic(&value, &modem->powered);
2041 DBG("%s Powered %d", modem->path, modem->powered);
2042 } else if (g_str_equal(key, "Online") == TRUE) {
2043 dbus_message_iter_get_basic(&value, &modem->online);
2045 DBG("%s Online %d", modem->path, modem->online);
2046 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2047 modem->interfaces = extract_interfaces(&value);
2049 DBG("%s Interfaces 0x%02x", modem->path,
2051 } else if (g_str_equal(key, "Serial") == TRUE) {
2054 dbus_message_iter_get_basic(&value, &serial);
2055 modem->serial = g_strdup(serial);
2057 DBG("%s Serial %s", modem->path, modem->serial);
2058 } else if (g_str_equal(key, "Type") == TRUE) {
2061 dbus_message_iter_get_basic(&value, &type);
2063 DBG("%s Type %s", modem->path, type);
2064 if (g_strcmp0(type, "hardware") != 0) {
2065 DBG("%s Ignore this modem", modem->path);
2066 modem->ignore = TRUE;
2070 dbus_message_iter_next(prop);
2073 if (modem->ignore == TRUE)
2076 if (modem->powered == FALSE) {
2077 modem_set_powered(modem, TRUE);
2081 modem_update_interfaces(modem, 0, modem->interfaces);
2084 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2086 struct modem_data *modem = value;
2088 DBG("%s", modem->path);
2090 if (modem->ignore == TRUE)
2093 modem_set_powered(modem, FALSE);
2096 static void remove_modem(gpointer data)
2098 struct modem_data *modem = data;
2100 DBG("%s", modem->path);
2102 if (modem->call_set_property != NULL)
2103 dbus_pending_call_cancel(modem->call_set_property);
2105 if (modem->call_get_properties != NULL)
2106 dbus_pending_call_cancel(modem->call_get_properties);
2108 if (modem->call_get_contexts != NULL)
2109 dbus_pending_call_cancel(modem->call_get_contexts);
2111 if (modem->device != NULL)
2112 destroy_device(modem);
2114 if (modem->context != NULL)
2115 remove_cm_context(modem, modem->context->path);
2117 g_free(modem->serial);
2118 g_free(modem->name);
2119 g_free(modem->imsi);
2120 g_free(modem->path);
2125 static gboolean modem_added(DBusConnection *connection,
2126 DBusMessage *message, void *user_data)
2128 DBusMessageIter iter, properties;
2133 if (dbus_message_iter_init(message, &iter) == FALSE)
2136 dbus_message_iter_get_basic(&iter, &path);
2138 dbus_message_iter_next(&iter);
2139 dbus_message_iter_recurse(&iter, &properties);
2141 add_modem(path, &properties);
2146 static gboolean modem_removed(DBusConnection *connection,
2147 DBusMessage *message, void *user_data)
2149 DBusMessageIter iter;
2154 if (dbus_message_iter_init(message, &iter) == FALSE)
2157 dbus_message_iter_get_basic(&iter, &path);
2159 g_hash_table_remove(modem_hash, path);
2164 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2168 DBusMessageIter array, dict;
2172 reply = dbus_pending_call_steal_reply(call);
2174 dbus_error_init(&error);
2176 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2177 connman_error("%s", error.message);
2178 dbus_error_free(&error);
2182 if (dbus_message_iter_init(reply, &array) == FALSE)
2185 dbus_message_iter_recurse(&array, &dict);
2187 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2188 DBusMessageIter value, properties;
2191 dbus_message_iter_recurse(&dict, &value);
2192 dbus_message_iter_get_basic(&value, &path);
2194 dbus_message_iter_next(&value);
2195 dbus_message_iter_recurse(&value, &properties);
2197 add_modem(path, &properties);
2199 dbus_message_iter_next(&dict);
2203 dbus_message_unref(reply);
2205 dbus_pending_call_unref(call);
2208 static int manager_get_modems(void)
2210 DBusMessage *message;
2211 DBusPendingCall *call;
2215 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2216 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2217 if (message == NULL)
2220 if (dbus_connection_send_with_reply(connection, message,
2221 &call, TIMEOUT) == FALSE) {
2222 connman_error("Failed to call GetModems()");
2223 dbus_message_unref(message);
2228 connman_error("D-Bus connection not available");
2229 dbus_message_unref(message);
2233 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2236 dbus_message_unref(message);
2238 return -EINPROGRESS;
2241 static void ofono_connect(DBusConnection *conn, void *user_data)
2245 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2246 g_free, remove_modem);
2247 if (modem_hash == NULL)
2250 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2252 if (context_hash == NULL) {
2253 g_hash_table_destroy(modem_hash);
2257 manager_get_modems();
2260 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2264 if (modem_hash == NULL || context_hash == NULL)
2267 g_hash_table_destroy(modem_hash);
2270 g_hash_table_destroy(context_hash);
2271 context_hash = NULL;
2274 static int network_probe(struct connman_network *network)
2276 struct modem_data *modem = connman_network_get_data(network);
2278 DBG("%s network %p", modem->path, network);
2283 static void network_remove(struct connman_network *network)
2285 struct modem_data *modem = connman_network_get_data(network);
2287 DBG("%s network %p", modem->path, network);
2290 static int network_connect(struct connman_network *network)
2292 struct modem_data *modem = connman_network_get_data(network);
2294 DBG("%s network %p", modem->path, network);
2296 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2297 return context_set_active(modem, TRUE);
2298 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2299 return cdma_cm_set_powered(modem, TRUE);
2301 connman_error("Connection manager interface not available");
2306 static int network_disconnect(struct connman_network *network)
2308 struct modem_data *modem = connman_network_get_data(network);
2310 DBG("%s network %p", modem->path, network);
2312 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2313 return context_set_active(modem, FALSE);
2314 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2315 return cdma_cm_set_powered(modem, FALSE);
2317 connman_error("Connection manager interface not available");
2322 static struct connman_network_driver network_driver = {
2324 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2325 .probe = network_probe,
2326 .remove = network_remove,
2327 .connect = network_connect,
2328 .disconnect = network_disconnect,
2331 static int modem_probe(struct connman_device *device)
2333 struct modem_data *modem = connman_device_get_data(device);
2335 DBG("%s device %p", modem->path, device);
2340 static void modem_remove(struct connman_device *device)
2342 struct modem_data *modem = connman_device_get_data(device);
2344 DBG("%s device %p", modem->path, device);
2347 static int modem_enable(struct connman_device *device)
2349 struct modem_data *modem = connman_device_get_data(device);
2351 DBG("%s device %p", modem->path, device);
2353 if (modem->online == TRUE)
2356 return modem_set_online(modem, TRUE);
2359 static int modem_disable(struct connman_device *device)
2361 struct modem_data *modem = connman_device_get_data(device);
2363 DBG("%s device %p", modem->path, device);
2365 if (modem->online == FALSE)
2368 return modem_set_online(modem, FALSE);
2371 static struct connman_device_driver modem_driver = {
2373 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2374 .probe = modem_probe,
2375 .remove = modem_remove,
2376 .enable = modem_enable,
2377 .disable = modem_disable,
2381 static guint modem_added_watch;
2382 static guint modem_removed_watch;
2383 static guint modem_watch;
2384 static guint cm_watch;
2385 static guint sim_watch;
2386 static guint context_added_watch;
2387 static guint context_removed_watch;
2388 static guint netreg_watch;
2389 static guint context_watch;
2390 static guint cdma_cm_watch;
2391 static guint cdma_netreg_watch;
2393 static int ofono_init(void)
2399 connection = connman_dbus_get_connection();
2400 if (connection == NULL)
2403 watch = g_dbus_add_service_watch(connection,
2404 OFONO_SERVICE, ofono_connect,
2405 ofono_disconnect, NULL, NULL);
2407 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2408 OFONO_MANAGER_INTERFACE,
2413 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2414 OFONO_MANAGER_INTERFACE,
2419 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2420 OFONO_MODEM_INTERFACE,
2425 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2431 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2432 OFONO_SIM_INTERFACE,
2437 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2443 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2449 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2450 OFONO_CONTEXT_INTERFACE,
2455 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2456 OFONO_NETREG_INTERFACE,
2461 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2462 OFONO_CDMA_CM_INTERFACE,
2467 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2468 OFONO_CDMA_NETREG_INTERFACE,
2470 cdma_netreg_changed,
2474 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2475 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2476 context_added_watch == 0 ||
2477 context_removed_watch == 0 ||
2478 context_watch == 0 || netreg_watch == 0 ||
2479 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2484 err = connman_network_driver_register(&network_driver);
2488 err = connman_device_driver_register(&modem_driver);
2490 connman_network_driver_unregister(&network_driver);
2497 g_dbus_remove_watch(connection, cdma_netreg_watch);
2498 g_dbus_remove_watch(connection, cdma_cm_watch);
2499 g_dbus_remove_watch(connection, netreg_watch);
2500 g_dbus_remove_watch(connection, context_watch);
2501 g_dbus_remove_watch(connection, context_removed_watch);
2502 g_dbus_remove_watch(connection, context_added_watch);
2503 g_dbus_remove_watch(connection, sim_watch);
2504 g_dbus_remove_watch(connection, cm_watch);
2505 g_dbus_remove_watch(connection, modem_watch);
2506 g_dbus_remove_watch(connection, modem_removed_watch);
2507 g_dbus_remove_watch(connection, modem_added_watch);
2508 g_dbus_remove_watch(connection, watch);
2509 dbus_connection_unref(connection);
2514 static void ofono_exit(void)
2518 if (modem_hash != NULL) {
2520 * We should propably wait for the SetProperty() reply
2521 * message, because ...
2523 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2526 * ... here we will cancel the call.
2528 g_hash_table_destroy(modem_hash);
2532 if (context_hash != NULL) {
2533 g_hash_table_destroy(context_hash);
2534 context_hash = NULL;
2537 connman_device_driver_unregister(&modem_driver);
2538 connman_network_driver_unregister(&network_driver);
2540 g_dbus_remove_watch(connection, cdma_netreg_watch);
2541 g_dbus_remove_watch(connection, cdma_cm_watch);
2542 g_dbus_remove_watch(connection, netreg_watch);
2543 g_dbus_remove_watch(connection, context_watch);
2544 g_dbus_remove_watch(connection, context_removed_watch);
2545 g_dbus_remove_watch(connection, context_added_watch);
2546 g_dbus_remove_watch(connection, sim_watch);
2547 g_dbus_remove_watch(connection, cm_watch);
2548 g_dbus_remove_watch(connection, modem_watch);
2549 g_dbus_remove_watch(connection, modem_added_watch);
2550 g_dbus_remove_watch(connection, modem_removed_watch);
2551 g_dbus_remove_watch(connection, watch);
2553 dbus_connection_unref(connection);
2556 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2557 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)