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;
935 connman_device_set_powered(modem->device, modem->online);
940 static void destroy_device(struct modem_data *modem)
942 DBG("%s", modem->path);
944 connman_device_set_powered(modem->device, FALSE);
946 if (modem->network != NULL) {
947 connman_device_remove_network(modem->device, modem->network);
948 connman_network_unref(modem->network);
949 modem->network = NULL;
952 connman_device_unregister(modem->device);
953 connman_device_unref(modem->device);
955 modem->device = NULL;
958 static void add_network(struct modem_data *modem)
962 DBG("%s", modem->path);
964 if (modem->network != NULL)
967 modem->network = connman_network_create(modem->context->path,
968 CONNMAN_NETWORK_TYPE_CELLULAR);
969 if (modem->network == NULL)
972 DBG("network %p", modem->network);
974 connman_network_set_data(modem->network, modem);
976 connman_network_set_string(modem->network, "Path",
977 modem->context->path);
979 connman_network_set_index(modem->network, modem->context->index);
981 if (modem->name != NULL)
982 connman_network_set_name(modem->network, modem->name);
984 connman_network_set_name(modem->network, "");
986 connman_network_set_strength(modem->network, modem->strength);
988 group = get_ident(modem->context->path);
989 connman_network_set_group(modem->network, group);
991 connman_network_set_available(modem->network, TRUE);
993 connman_network_set_bool(modem->network, "Roaming",
996 if (connman_device_add_network(modem->device, modem->network) < 0) {
997 connman_network_unref(modem->network);
998 modem->network = NULL;
1003 * Create the ipconfig layer before trying to connect. Withouth
1004 * the ipconfig layer the core is not ready to process errors.
1006 connman_network_set_index(modem->network, -1);
1009 static void remove_network(struct modem_data *modem)
1011 DBG("%s", modem->path);
1013 if (modem->network == NULL)
1016 DBG("network %p", modem->network);
1018 connman_device_remove_network(modem->device, modem->network);
1019 connman_network_unref(modem->network);
1020 modem->network = NULL;
1023 static int add_cm_context(struct modem_data *modem, const char *context_path,
1024 DBusMessageIter *dict)
1026 const char *context_type;
1027 struct network_context *context = NULL;
1028 connman_bool_t active = FALSE;
1030 DBG("%s context path %s", modem->path, context_path);
1032 if (modem->context != NULL) {
1034 * We have already assigned a context to this modem
1035 * and we do only support one Internet context.
1040 context = network_context_alloc(context_path);
1041 if (context == NULL)
1044 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1045 DBusMessageIter entry, value;
1048 dbus_message_iter_recurse(dict, &entry);
1049 dbus_message_iter_get_basic(&entry, &key);
1051 dbus_message_iter_next(&entry);
1052 dbus_message_iter_recurse(&entry, &value);
1054 if (g_str_equal(key, "Type") == TRUE) {
1055 dbus_message_iter_get_basic(&value, &context_type);
1057 DBG("%s context %s type %s", modem->path,
1058 context_path, context_type);
1059 } else if (g_str_equal(key, "Settings") == TRUE) {
1060 DBG("%s Settings", modem->path);
1062 extract_ipv4_settings(&value, context);
1063 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1064 DBG("%s IPv6.Settings", modem->path);
1066 extract_ipv6_settings(&value, context);
1067 } else if (g_str_equal(key, "Active") == TRUE) {
1068 dbus_message_iter_get_basic(&value, &active);
1070 DBG("%s Active %d", modem->path, active);
1073 dbus_message_iter_next(dict);
1076 if (g_strcmp0(context_type, "internet") != 0) {
1077 network_context_free(context);
1081 modem->context = context;
1082 modem->active = active;
1084 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1089 static void remove_cm_context(struct modem_data *modem,
1090 const char *context_path)
1092 if (modem->context == NULL)
1095 g_hash_table_remove(context_hash, context_path);
1097 network_context_free(modem->context);
1098 modem->context = NULL;
1101 static gboolean context_changed(DBusConnection *connection,
1102 DBusMessage *message,
1105 const char *context_path = dbus_message_get_path(message);
1106 struct modem_data *modem = NULL;
1107 DBusMessageIter iter, value;
1110 DBG("context_path %s", context_path);
1112 modem = g_hash_table_lookup(context_hash, context_path);
1116 if (dbus_message_iter_init(message, &iter) == FALSE)
1119 dbus_message_iter_get_basic(&iter, &key);
1121 dbus_message_iter_next(&iter);
1122 dbus_message_iter_recurse(&iter, &value);
1125 * oFono guarantees the ordering of Settings and
1126 * Active. Settings will always be send before Active = True.
1127 * That means we don't have to order here.
1129 if (g_str_equal(key, "Settings") == TRUE) {
1130 DBG("%s Settings", modem->path);
1132 extract_ipv4_settings(&value, modem->context);
1133 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1134 DBG("%s IPv6.Settings", modem->path);
1136 extract_ipv6_settings(&value, modem->context);
1137 } else if (g_str_equal(key, "Active") == TRUE) {
1138 dbus_message_iter_get_basic(&value, &modem->active);
1140 DBG("%s Active %d", modem->path, modem->active);
1142 if (modem->active == TRUE)
1143 set_connected(modem);
1145 set_disconnected(modem);
1151 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1153 struct modem_data *modem = user_data;
1154 DBusMessageIter array, dict, entry, value;
1158 DBG("%s", modem->path);
1160 modem->call_get_contexts = NULL;
1162 reply = dbus_pending_call_steal_reply(call);
1164 dbus_error_init(&error);
1166 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1167 connman_error("%s", error.message);
1168 dbus_error_free(&error);
1172 if (dbus_message_iter_init(reply, &array) == FALSE)
1175 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1178 dbus_message_iter_recurse(&array, &dict);
1180 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1181 const char *context_path;
1183 dbus_message_iter_recurse(&dict, &entry);
1184 dbus_message_iter_get_basic(&entry, &context_path);
1186 dbus_message_iter_next(&entry);
1187 dbus_message_iter_recurse(&entry, &value);
1189 if (add_cm_context(modem, context_path, &value) == 0)
1192 dbus_message_iter_next(&dict);
1196 dbus_message_unref(reply);
1198 dbus_pending_call_unref(call);
1201 static int cm_get_contexts(struct modem_data *modem)
1203 DBusMessage *message;
1205 DBG("%s", modem->path);
1207 if (modem->call_get_contexts != NULL)
1210 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1211 OFONO_CM_INTERFACE, GET_CONTEXTS);
1212 if (message == NULL)
1215 if (dbus_connection_send_with_reply(connection, message,
1216 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1217 connman_error("Failed to call GetContexts()");
1218 dbus_message_unref(message);
1222 if (modem->call_get_contexts == NULL) {
1223 connman_error("D-Bus connection not available");
1224 dbus_message_unref(message);
1228 dbus_pending_call_set_notify(modem->call_get_contexts,
1229 cm_get_contexts_reply,
1232 dbus_message_unref(message);
1234 return -EINPROGRESS;
1237 static gboolean cm_context_added(DBusConnection *connection,
1238 DBusMessage *message,
1241 const char *path = dbus_message_get_path(message);
1243 struct modem_data *modem;
1244 DBusMessageIter iter, properties;
1248 modem = g_hash_table_lookup(modem_hash, context_path);
1252 if (dbus_message_iter_init(message, &iter) == FALSE)
1255 dbus_message_iter_get_basic(&iter, &context_path);
1257 dbus_message_iter_next(&iter);
1258 dbus_message_iter_recurse(&iter, &properties);
1260 if (add_cm_context(modem, context_path, &properties) != 0)
1266 static gboolean cm_context_removed(DBusConnection *connection,
1267 DBusMessage *message,
1270 const char *path = dbus_message_get_path(message);
1271 const char *context_path;
1272 struct modem_data *modem;
1273 DBusMessageIter iter;
1275 DBG("context path %s", path);
1277 if (dbus_message_iter_init(message, &iter) == FALSE)
1280 dbus_message_iter_get_basic(&iter, &context_path);
1282 modem = g_hash_table_lookup(context_hash, context_path);
1286 remove_cm_context(modem, context_path);
1291 static void netreg_update_name(struct modem_data *modem,
1292 DBusMessageIter* value)
1296 dbus_message_iter_get_basic(value, &name);
1298 DBG("%s Name %s", modem->path, name);
1300 g_free(modem->name);
1301 modem->name = g_strdup(name);
1303 if (modem->network == NULL)
1306 connman_network_set_name(modem->network, modem->name);
1307 connman_network_update(modem->network);
1310 static void netreg_update_strength(struct modem_data *modem,
1311 DBusMessageIter *value)
1313 dbus_message_iter_get_basic(value, &modem->strength);
1315 DBG("%s Strength %d", modem->path, modem->strength);
1317 if (modem->network == NULL)
1322 * We don't have 2 signal notifications we always report the strength
1323 * signal. data_strength is always equal to 0.
1326 * In the case we have a data_strength signal (from 1xEVDO network)
1327 * we don't need to update the value with strength signal (from 1xCDMA)
1328 * because the modem is registered to 1xEVDO network for data call.
1329 * In case we have no data_strength signal (not registered to 1xEVDO
1330 * network), we must report the strength signal (registered to 1xCDMA
1331 * network e.g slow mode).
1333 if (modem->data_strength != 0)
1336 connman_network_set_strength(modem->network, modem->strength);
1337 connman_network_update(modem->network);
1340 /* Retrieve 1xEVDO Data Strength signal */
1341 static void netreg_update_datastrength(struct modem_data *modem,
1342 DBusMessageIter *value)
1344 dbus_message_iter_get_basic(value, &modem->data_strength);
1346 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1348 if (modem->network == NULL)
1352 * CDMA modem is not registered to 1xEVDO network, let
1353 * update_signal_strength() reporting the value on the Strength signal
1356 if (modem->data_strength == 0)
1359 connman_network_set_strength(modem->network, modem->data_strength);
1360 connman_network_update(modem->network);
1363 static void netreg_update_roaming(struct modem_data *modem,
1364 DBusMessageIter *value)
1367 connman_bool_t roaming;
1369 dbus_message_iter_get_basic(value, &status);
1371 if (g_str_equal(status, "roaming") == TRUE)
1376 if (roaming == modem->roaming)
1379 modem->roaming = roaming;
1381 if (modem->network == NULL)
1384 connman_network_set_bool(modem->network,
1385 "Roaming", modem->roaming);
1386 connman_network_update(modem->network);
1389 static void netreg_update_regdom(struct modem_data *modem,
1390 DBusMessageIter *value)
1392 char *mobile_country_code;
1396 dbus_message_iter_get_basic(value, &mobile_country_code);
1398 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1401 mcc = atoi(mobile_country_code);
1405 alpha2 = mcc_country_codes[mcc - 200];
1406 connman_technology_set_regdom(alpha2);
1409 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1412 const char *path = dbus_message_get_path(message);
1413 struct modem_data *modem;
1414 DBusMessageIter iter, value;
1417 modem = g_hash_table_lookup(modem_hash, path);
1421 if (modem->ignore == TRUE)
1424 if (dbus_message_iter_init(message, &iter) == FALSE)
1427 dbus_message_iter_get_basic(&iter, &key);
1429 dbus_message_iter_next(&iter);
1430 dbus_message_iter_recurse(&iter, &value);
1432 if (g_str_equal(key, "Name") == TRUE)
1433 netreg_update_name(modem, &value);
1434 else if (g_str_equal(key, "Strength") == TRUE)
1435 netreg_update_strength(modem, &value);
1436 else if (g_str_equal(key, "Status") == TRUE)
1437 netreg_update_roaming(modem, &value);
1438 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1439 netreg_update_regdom(modem, &value);
1444 static void netreg_properties_reply(struct modem_data *modem,
1445 DBusMessageIter *dict)
1447 DBG("%s", modem->path);
1449 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1450 DBusMessageIter entry, value;
1453 dbus_message_iter_recurse(dict, &entry);
1454 dbus_message_iter_get_basic(&entry, &key);
1456 dbus_message_iter_next(&entry);
1457 dbus_message_iter_recurse(&entry, &value);
1459 if (g_str_equal(key, "Name") == TRUE)
1460 netreg_update_name(modem, &value);
1461 else if (g_str_equal(key, "Strength") == TRUE)
1462 netreg_update_strength(modem, &value);
1463 else if (g_str_equal(key, "Status") == TRUE)
1464 netreg_update_roaming(modem, &value);
1465 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1466 netreg_update_regdom(modem, &value);
1468 dbus_message_iter_next(dict);
1471 if (modem->context == NULL) {
1473 * netgreg_get_properties() was issued after we got
1474 * cm_get_contexts_reply() where we create the
1475 * context. Though before we got the
1476 * netreg_properties_reply the context was removed
1477 * again. Therefore we have to skip the network
1485 if (modem->active == TRUE)
1486 set_connected(modem);
1489 static int netreg_get_properties(struct modem_data *modem)
1491 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1492 netreg_properties_reply, modem);
1495 static void add_cdma_network(struct modem_data *modem)
1497 /* Be sure that device is created before adding CDMA network */
1498 if (modem->device == NULL)
1502 * CDMA modems don't need contexts for data call, however the current
1503 * add_network() logic needs one, so we create one to proceed.
1505 if (modem->context == NULL)
1506 modem->context = network_context_alloc(modem->path);
1510 if (modem->cdma_cm_powered == TRUE)
1511 set_connected(modem);
1514 static gboolean cdma_netreg_changed(DBusConnection *connection,
1515 DBusMessage *message,
1518 const char *path = dbus_message_get_path(message);
1519 struct modem_data *modem;
1520 DBusMessageIter iter, value;
1525 modem = g_hash_table_lookup(modem_hash, path);
1529 if (modem->ignore == TRUE)
1532 if (dbus_message_iter_init(message, &iter) == FALSE)
1535 dbus_message_iter_get_basic(&iter, &key);
1537 dbus_message_iter_next(&iter);
1538 dbus_message_iter_recurse(&iter, &value);
1540 if (g_str_equal(key, "Name") == TRUE)
1541 netreg_update_name(modem, &value);
1542 else if (g_str_equal(key, "Strength") == TRUE)
1543 netreg_update_strength(modem, &value);
1544 else if (g_str_equal(key, "DataStrength") == TRUE)
1545 netreg_update_datastrength(modem, &value);
1546 else if (g_str_equal(key, "Status") == TRUE)
1547 netreg_update_roaming(modem, &value);
1549 add_cdma_network(modem);
1554 static void cdma_netreg_properties_reply(struct modem_data *modem,
1555 DBusMessageIter *dict)
1557 DBG("%s", modem->path);
1559 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1560 DBusMessageIter entry, value;
1563 dbus_message_iter_recurse(dict, &entry);
1564 dbus_message_iter_get_basic(&entry, &key);
1566 dbus_message_iter_next(&entry);
1567 dbus_message_iter_recurse(&entry, &value);
1569 if (g_str_equal(key, "Name") == TRUE)
1570 netreg_update_name(modem, &value);
1571 else if (g_str_equal(key, "Strength") == TRUE)
1572 netreg_update_strength(modem, &value);
1573 else if (g_str_equal(key, "DataStrength") == TRUE)
1574 netreg_update_datastrength(modem, &value);
1575 else if (g_str_equal(key, "Status") == TRUE)
1576 netreg_update_roaming(modem, &value);
1578 dbus_message_iter_next(dict);
1581 add_cdma_network(modem);
1584 static int cdma_netreg_get_properties(struct modem_data *modem)
1586 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1587 cdma_netreg_properties_reply, modem);
1590 static void cm_update_attached(struct modem_data *modem,
1591 DBusMessageIter *value)
1593 dbus_message_iter_get_basic(value, &modem->attached);
1595 DBG("%s Attached %d", modem->path, modem->attached);
1597 if (modem->attached == FALSE)
1600 if (has_interface(modem->interfaces,
1601 OFONO_API_NETREG) == FALSE) {
1605 netreg_get_properties(modem);
1608 static void cm_update_powered(struct modem_data *modem,
1609 DBusMessageIter *value)
1611 dbus_message_iter_get_basic(value, &modem->cm_powered);
1613 DBG("%s ConnnectionManager Powered %d", modem->path,
1616 if (modem->cm_powered == TRUE)
1619 cm_set_powered(modem, TRUE);
1622 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1625 const char *path = dbus_message_get_path(message);
1626 struct modem_data *modem;
1627 DBusMessageIter iter, value;
1630 modem = g_hash_table_lookup(modem_hash, path);
1634 if (modem->ignore == TRUE)
1637 if (dbus_message_iter_init(message, &iter) == FALSE)
1640 dbus_message_iter_get_basic(&iter, &key);
1642 dbus_message_iter_next(&iter);
1643 dbus_message_iter_recurse(&iter, &value);
1645 if (g_str_equal(key, "Attached") == TRUE)
1646 cm_update_attached(modem, &value);
1647 else if (g_str_equal(key, "Powered") == TRUE)
1648 cm_update_powered(modem, &value);
1653 static void cdma_cm_update_powered(struct modem_data *modem,
1654 DBusMessageIter *value)
1656 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1658 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1660 if (modem->network == NULL)
1663 if (modem->cdma_cm_powered == TRUE)
1664 set_connected(modem);
1666 set_disconnected(modem);
1669 static void cdma_cm_update_settings(struct modem_data *modem,
1670 DBusMessageIter *value)
1672 DBG("%s Settings", modem->path);
1674 if (modem->context != NULL)
1677 extract_ipv4_settings(value, modem->context);
1680 static gboolean cdma_cm_changed(DBusConnection *connection,
1681 DBusMessage *message, void *user_data)
1683 const char *path = dbus_message_get_path(message);
1684 struct modem_data *modem;
1685 DBusMessageIter iter, value;
1688 modem = g_hash_table_lookup(modem_hash, path);
1692 if (modem->online == TRUE && modem->network == NULL)
1693 cdma_netreg_get_properties(modem);
1695 if (dbus_message_iter_init(message, &iter) == FALSE)
1698 dbus_message_iter_get_basic(&iter, &key);
1700 dbus_message_iter_next(&iter);
1701 dbus_message_iter_recurse(&iter, &value);
1703 if (g_str_equal(key, "Powered") == TRUE)
1704 cdma_cm_update_powered(modem, &value);
1705 if (g_str_equal(key, "Settings") == TRUE)
1706 cdma_cm_update_settings(modem, &value);
1711 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1713 DBG("%s", modem->path);
1715 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1716 DBusMessageIter entry, value;
1719 dbus_message_iter_recurse(dict, &entry);
1720 dbus_message_iter_get_basic(&entry, &key);
1722 dbus_message_iter_next(&entry);
1723 dbus_message_iter_recurse(&entry, &value);
1725 if (g_str_equal(key, "Attached") == TRUE)
1726 cm_update_attached(modem, &value);
1727 else if (g_str_equal(key, "Powered") == TRUE)
1728 cm_update_powered(modem, &value);
1730 dbus_message_iter_next(dict);
1734 static int cm_get_properties(struct modem_data *modem)
1736 return get_properties(modem->path, OFONO_CM_INTERFACE,
1737 cm_properties_reply, modem);
1740 static void cdma_cm_properties_reply(struct modem_data *modem,
1741 DBusMessageIter *dict)
1743 DBG("%s", modem->path);
1745 if (modem->online == TRUE)
1746 cdma_netreg_get_properties(modem);
1748 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1749 DBusMessageIter entry, value;
1752 dbus_message_iter_recurse(dict, &entry);
1753 dbus_message_iter_get_basic(&entry, &key);
1755 dbus_message_iter_next(&entry);
1756 dbus_message_iter_recurse(&entry, &value);
1758 if (g_str_equal(key, "Powered") == TRUE)
1759 cdma_cm_update_powered(modem, &value);
1760 if (g_str_equal(key, "Settings") == TRUE)
1761 cdma_cm_update_settings(modem, &value);
1763 dbus_message_iter_next(dict);
1767 static int cdma_cm_get_properties(struct modem_data *modem)
1769 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1770 cdma_cm_properties_reply, modem);
1773 static void sim_update_imsi(struct modem_data *modem,
1774 DBusMessageIter* value)
1778 dbus_message_iter_get_basic(value, &imsi);
1780 DBG("%s imsi %s", modem->path, imsi);
1782 g_free(modem->imsi);
1783 modem->imsi = g_strdup(imsi);
1786 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1789 const char *path = dbus_message_get_path(message);
1790 struct modem_data *modem;
1791 DBusMessageIter iter, value;
1794 modem = g_hash_table_lookup(modem_hash, path);
1798 if (modem->ignore == TRUE)
1801 if (dbus_message_iter_init(message, &iter) == FALSE)
1804 dbus_message_iter_get_basic(&iter, &key);
1806 dbus_message_iter_next(&iter);
1807 dbus_message_iter_recurse(&iter, &value);
1809 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1810 sim_update_imsi(modem, &value);
1812 if (ready_to_create_device(modem) == FALSE)
1816 * This is a GSM modem. Create the device and
1817 * register it at the core. Enabling (setting
1818 * it online is done through the
1819 * modem_enable() callback.
1821 create_device(modem);
1827 static void sim_properties_reply(struct modem_data *modem,
1828 DBusMessageIter *dict)
1830 DBG("%s", modem->path);
1832 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1833 DBusMessageIter entry, value;
1836 dbus_message_iter_recurse(dict, &entry);
1837 dbus_message_iter_get_basic(&entry, &key);
1839 dbus_message_iter_next(&entry);
1840 dbus_message_iter_recurse(&entry, &value);
1842 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1843 sim_update_imsi(modem, &value);
1845 if (ready_to_create_device(modem) == FALSE)
1849 * This is a GSM modem. Create the device and
1850 * register it at the core. Enabling (setting
1851 * it online is done through the
1852 * modem_enable() callback.
1854 create_device(modem);
1856 if (modem->online == FALSE)
1860 * The modem is already online and we have the CM interface.
1861 * There will be no interface update and therefore our
1862 * state machine will not go to next step. We have to
1863 * trigger it from here.
1865 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1866 cm_get_properties(modem);
1867 cm_get_contexts(modem);
1872 dbus_message_iter_next(dict);
1876 static int sim_get_properties(struct modem_data *modem)
1878 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1879 sim_properties_reply, modem);
1882 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1885 if (has_interface(old_iface, api) == FALSE &&
1886 has_interface(new_iface, api) == TRUE) {
1887 DBG("%s added", api2string(api));
1894 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1897 if (has_interface(old_iface, api) == TRUE &&
1898 has_interface(new_iface, api) == FALSE) {
1899 DBG("%s removed", api2string(api));
1906 static void modem_update_interfaces(struct modem_data *modem,
1910 DBG("%s", modem->path);
1912 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
1913 if (modem->imsi == NULL &&
1914 modem->set_powered == FALSE) {
1916 * Only use do GetProperties() when
1917 * device has not been powered up.
1919 sim_get_properties(modem);
1923 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1924 if (modem->device != NULL) {
1925 cm_get_properties(modem);
1926 cm_get_contexts(modem);
1930 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1931 if (ready_to_create_device(modem) == TRUE)
1932 create_device(modem);
1934 if (modem->device != NULL)
1935 cdma_cm_get_properties(modem);
1938 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1939 if (modem->attached == TRUE)
1940 netreg_get_properties(modem);
1943 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
1944 cdma_netreg_get_properties(modem);
1947 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1948 remove_cm_context(modem, modem->context->path);
1951 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1952 remove_cm_context(modem, modem->context->path);
1955 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1956 remove_network(modem);
1959 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
1960 remove_network(modem);
1964 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1967 const char *path = dbus_message_get_path(message);
1968 struct modem_data *modem;
1969 DBusMessageIter iter, value;
1972 modem = g_hash_table_lookup(modem_hash, path);
1976 if (modem->ignore == TRUE)
1979 if (dbus_message_iter_init(message, &iter) == FALSE)
1982 dbus_message_iter_get_basic(&iter, &key);
1984 dbus_message_iter_next(&iter);
1985 dbus_message_iter_recurse(&iter, &value);
1987 if (g_str_equal(key, "Powered") == TRUE) {
1988 dbus_message_iter_get_basic(&value, &modem->powered);
1990 DBG("%s Powered %d", modem->path, modem->powered);
1992 if (modem->powered == FALSE)
1993 modem_set_powered(modem, TRUE);
1994 } else if (g_str_equal(key, "Online") == TRUE) {
1995 dbus_message_iter_get_basic(&value, &modem->online);
1997 DBG("%s Online %d", modem->path, modem->online);
1999 if (modem->device == NULL)
2002 connman_device_set_powered(modem->device, modem->online);
2003 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2006 interfaces = extract_interfaces(&value);
2008 if (interfaces == modem->interfaces)
2011 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2013 modem_update_interfaces(modem, modem->interfaces, interfaces);
2015 modem->interfaces = interfaces;
2016 } else if (g_str_equal(key, "Serial") == TRUE) {
2019 dbus_message_iter_get_basic(&value, &serial);
2021 g_free(modem->serial);
2022 modem->serial = g_strdup(serial);
2024 DBG("%s Serial %s", modem->path, modem->serial);
2026 if (has_interface(modem->interfaces,
2027 OFONO_API_CDMA_CM) == TRUE) {
2028 if (ready_to_create_device(modem) == TRUE)
2029 create_device(modem);
2036 static void add_modem(const char *path, DBusMessageIter *prop)
2038 struct modem_data *modem;
2042 modem = g_hash_table_lookup(modem_hash, path);
2043 if (modem != NULL) {
2045 * When oFono powers up we ask for the modems and oFono is
2046 * reporting with modem_added signal the modems. Only
2052 modem = g_try_new0(struct modem_data, 1);
2056 modem->path = g_strdup(path);
2058 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2060 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2061 DBusMessageIter entry, value;
2064 dbus_message_iter_recurse(prop, &entry);
2065 dbus_message_iter_get_basic(&entry, &key);
2067 dbus_message_iter_next(&entry);
2068 dbus_message_iter_recurse(&entry, &value);
2070 if (g_str_equal(key, "Powered") == TRUE) {
2071 dbus_message_iter_get_basic(&value, &modem->powered);
2073 DBG("%s Powered %d", modem->path, modem->powered);
2074 } else if (g_str_equal(key, "Online") == TRUE) {
2075 dbus_message_iter_get_basic(&value, &modem->online);
2077 DBG("%s Online %d", modem->path, modem->online);
2078 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2079 modem->interfaces = extract_interfaces(&value);
2081 DBG("%s Interfaces 0x%02x", modem->path,
2083 } else if (g_str_equal(key, "Serial") == TRUE) {
2086 dbus_message_iter_get_basic(&value, &serial);
2087 modem->serial = g_strdup(serial);
2089 DBG("%s Serial %s", modem->path, modem->serial);
2090 } else if (g_str_equal(key, "Type") == TRUE) {
2093 dbus_message_iter_get_basic(&value, &type);
2095 DBG("%s Type %s", modem->path, type);
2096 if (g_strcmp0(type, "hardware") != 0) {
2097 DBG("%s Ignore this modem", modem->path);
2098 modem->ignore = TRUE;
2102 dbus_message_iter_next(prop);
2105 if (modem->ignore == TRUE)
2108 if (modem->powered == FALSE) {
2109 modem_set_powered(modem, TRUE);
2113 modem_update_interfaces(modem, 0, modem->interfaces);
2116 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2118 struct modem_data *modem = value;
2120 DBG("%s", modem->path);
2122 if (modem->ignore == TRUE)
2125 modem_set_powered(modem, FALSE);
2128 static void remove_modem(gpointer data)
2130 struct modem_data *modem = data;
2132 DBG("%s", modem->path);
2134 if (modem->call_set_property != NULL)
2135 dbus_pending_call_cancel(modem->call_set_property);
2137 if (modem->call_get_properties != NULL)
2138 dbus_pending_call_cancel(modem->call_get_properties);
2140 if (modem->call_get_contexts != NULL)
2141 dbus_pending_call_cancel(modem->call_get_contexts);
2143 if (modem->device != NULL)
2144 destroy_device(modem);
2146 if (modem->context != NULL)
2147 remove_cm_context(modem, modem->context->path);
2149 g_free(modem->serial);
2150 g_free(modem->name);
2151 g_free(modem->imsi);
2152 g_free(modem->path);
2157 static gboolean modem_added(DBusConnection *connection,
2158 DBusMessage *message, void *user_data)
2160 DBusMessageIter iter, properties;
2165 if (dbus_message_iter_init(message, &iter) == FALSE)
2168 dbus_message_iter_get_basic(&iter, &path);
2170 dbus_message_iter_next(&iter);
2171 dbus_message_iter_recurse(&iter, &properties);
2173 add_modem(path, &properties);
2178 static gboolean modem_removed(DBusConnection *connection,
2179 DBusMessage *message, void *user_data)
2181 DBusMessageIter iter;
2186 if (dbus_message_iter_init(message, &iter) == FALSE)
2189 dbus_message_iter_get_basic(&iter, &path);
2191 g_hash_table_remove(modem_hash, path);
2196 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2200 DBusMessageIter array, dict;
2204 reply = dbus_pending_call_steal_reply(call);
2206 dbus_error_init(&error);
2208 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2209 connman_error("%s", error.message);
2210 dbus_error_free(&error);
2214 if (dbus_message_iter_init(reply, &array) == FALSE)
2217 dbus_message_iter_recurse(&array, &dict);
2219 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2220 DBusMessageIter value, properties;
2223 dbus_message_iter_recurse(&dict, &value);
2224 dbus_message_iter_get_basic(&value, &path);
2226 dbus_message_iter_next(&value);
2227 dbus_message_iter_recurse(&value, &properties);
2229 add_modem(path, &properties);
2231 dbus_message_iter_next(&dict);
2235 dbus_message_unref(reply);
2237 dbus_pending_call_unref(call);
2240 static int manager_get_modems(void)
2242 DBusMessage *message;
2243 DBusPendingCall *call;
2247 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2248 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2249 if (message == NULL)
2252 if (dbus_connection_send_with_reply(connection, message,
2253 &call, TIMEOUT) == FALSE) {
2254 connman_error("Failed to call GetModems()");
2255 dbus_message_unref(message);
2260 connman_error("D-Bus connection not available");
2261 dbus_message_unref(message);
2265 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2268 dbus_message_unref(message);
2270 return -EINPROGRESS;
2273 static void ofono_connect(DBusConnection *conn, void *user_data)
2277 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2278 g_free, remove_modem);
2279 if (modem_hash == NULL)
2282 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2284 if (context_hash == NULL) {
2285 g_hash_table_destroy(modem_hash);
2289 manager_get_modems();
2292 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2296 if (modem_hash == NULL || context_hash == NULL)
2299 g_hash_table_destroy(modem_hash);
2302 g_hash_table_destroy(context_hash);
2303 context_hash = NULL;
2306 static int network_probe(struct connman_network *network)
2308 struct modem_data *modem = connman_network_get_data(network);
2310 DBG("%s network %p", modem->path, network);
2315 static void network_remove(struct connman_network *network)
2317 struct modem_data *modem = connman_network_get_data(network);
2319 DBG("%s network %p", modem->path, network);
2322 static int network_connect(struct connman_network *network)
2324 struct modem_data *modem = connman_network_get_data(network);
2326 DBG("%s network %p", modem->path, network);
2328 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2329 return context_set_active(modem, TRUE);
2330 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2331 return cdma_cm_set_powered(modem, TRUE);
2333 connman_error("Connection manager interface not available");
2338 static int network_disconnect(struct connman_network *network)
2340 struct modem_data *modem = connman_network_get_data(network);
2342 DBG("%s network %p", modem->path, network);
2344 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2345 return context_set_active(modem, FALSE);
2346 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2347 return cdma_cm_set_powered(modem, FALSE);
2349 connman_error("Connection manager interface not available");
2354 static struct connman_network_driver network_driver = {
2356 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2357 .probe = network_probe,
2358 .remove = network_remove,
2359 .connect = network_connect,
2360 .disconnect = network_disconnect,
2363 static int modem_probe(struct connman_device *device)
2365 struct modem_data *modem = connman_device_get_data(device);
2367 DBG("%s device %p", modem->path, device);
2372 static void modem_remove(struct connman_device *device)
2374 struct modem_data *modem = connman_device_get_data(device);
2376 DBG("%s device %p", modem->path, device);
2379 static int modem_enable(struct connman_device *device)
2381 struct modem_data *modem = connman_device_get_data(device);
2383 DBG("%s device %p", modem->path, device);
2385 if (modem->online == TRUE)
2388 return modem_set_online(modem, TRUE);
2391 static int modem_disable(struct connman_device *device)
2393 struct modem_data *modem = connman_device_get_data(device);
2395 DBG("%s device %p", modem->path, device);
2397 if (modem->online == FALSE)
2400 return modem_set_online(modem, FALSE);
2403 static struct connman_device_driver modem_driver = {
2405 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2406 .probe = modem_probe,
2407 .remove = modem_remove,
2408 .enable = modem_enable,
2409 .disable = modem_disable,
2413 static guint modem_added_watch;
2414 static guint modem_removed_watch;
2415 static guint modem_watch;
2416 static guint cm_watch;
2417 static guint sim_watch;
2418 static guint context_added_watch;
2419 static guint context_removed_watch;
2420 static guint netreg_watch;
2421 static guint context_watch;
2422 static guint cdma_cm_watch;
2423 static guint cdma_netreg_watch;
2425 static int ofono_init(void)
2431 connection = connman_dbus_get_connection();
2432 if (connection == NULL)
2435 watch = g_dbus_add_service_watch(connection,
2436 OFONO_SERVICE, ofono_connect,
2437 ofono_disconnect, NULL, NULL);
2439 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2440 OFONO_MANAGER_INTERFACE,
2445 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2446 OFONO_MANAGER_INTERFACE,
2451 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2452 OFONO_MODEM_INTERFACE,
2457 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2463 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2464 OFONO_SIM_INTERFACE,
2469 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2475 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2481 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2482 OFONO_CONTEXT_INTERFACE,
2487 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2488 OFONO_NETREG_INTERFACE,
2493 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2494 OFONO_CDMA_CM_INTERFACE,
2499 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2500 OFONO_CDMA_NETREG_INTERFACE,
2502 cdma_netreg_changed,
2506 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2507 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2508 context_added_watch == 0 ||
2509 context_removed_watch == 0 ||
2510 context_watch == 0 || netreg_watch == 0 ||
2511 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2516 err = connman_network_driver_register(&network_driver);
2520 err = connman_device_driver_register(&modem_driver);
2522 connman_network_driver_unregister(&network_driver);
2529 g_dbus_remove_watch(connection, cdma_netreg_watch);
2530 g_dbus_remove_watch(connection, cdma_cm_watch);
2531 g_dbus_remove_watch(connection, netreg_watch);
2532 g_dbus_remove_watch(connection, context_watch);
2533 g_dbus_remove_watch(connection, context_removed_watch);
2534 g_dbus_remove_watch(connection, context_added_watch);
2535 g_dbus_remove_watch(connection, sim_watch);
2536 g_dbus_remove_watch(connection, cm_watch);
2537 g_dbus_remove_watch(connection, modem_watch);
2538 g_dbus_remove_watch(connection, modem_removed_watch);
2539 g_dbus_remove_watch(connection, modem_added_watch);
2540 g_dbus_remove_watch(connection, watch);
2541 dbus_connection_unref(connection);
2546 static void ofono_exit(void)
2550 if (modem_hash != NULL) {
2552 * We should propably wait for the SetProperty() reply
2553 * message, because ...
2555 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2558 * ... here we will cancel the call.
2560 g_hash_table_destroy(modem_hash);
2564 if (context_hash != NULL) {
2565 g_hash_table_destroy(context_hash);
2566 context_hash = NULL;
2569 connman_device_driver_unregister(&modem_driver);
2570 connman_network_driver_unregister(&network_driver);
2572 g_dbus_remove_watch(connection, cdma_netreg_watch);
2573 g_dbus_remove_watch(connection, cdma_cm_watch);
2574 g_dbus_remove_watch(connection, netreg_watch);
2575 g_dbus_remove_watch(connection, context_watch);
2576 g_dbus_remove_watch(connection, context_removed_watch);
2577 g_dbus_remove_watch(connection, context_added_watch);
2578 g_dbus_remove_watch(connection, sim_watch);
2579 g_dbus_remove_watch(connection, cm_watch);
2580 g_dbus_remove_watch(connection, modem_watch);
2581 g_dbus_remove_watch(connection, modem_added_watch);
2582 g_dbus_remove_watch(connection, modem_removed_watch);
2583 g_dbus_remove_watch(connection, watch);
2585 dbus_connection_unref(connection);
2588 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2589 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)