5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011-2014 BMW Car IT GmbH.
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 OFONO_SERVICE "org.ofono"
48 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
49 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
50 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
51 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
52 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
53 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
54 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
55 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
57 #define MODEM_ADDED "ModemAdded"
58 #define MODEM_REMOVED "ModemRemoved"
59 #define PROPERTY_CHANGED "PropertyChanged"
60 #define CONTEXT_ADDED "ContextAdded"
61 #define CONTEXT_REMOVED "ContextRemoved"
63 #define GET_PROPERTIES "GetProperties"
64 #define SET_PROPERTY "SetProperty"
65 #define GET_MODEMS "GetModems"
66 #define GET_CONTEXTS "GetContexts"
72 OFONO_API_NETREG = 0x2,
74 OFONO_API_CDMA_NETREG = 0x8,
75 OFONO_API_CDMA_CM = 0x10,
79 * The way this plugin works is following:
81 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
82 * attached -> netreg -> ready
84 * Depending on the modem type, this plugin will behave differently.
88 * When a new modem appears, the plugin always powers it up. This
89 * allows the plugin to create a connman_device. The core will call
90 * modem_enable() if the technology is enabled. modem_enable() will
91 * then set the modem online. If the technology is disabled then
92 * modem_disable() will just set the modem offline. The modem is
93 * always kept powered all the time.
95 * After setting the modem online the plugin waits for the
96 * ConnectionManager and ConnectionContext to appear. When the context
97 * signals that it is attached and the NetworkRegistration interface
98 * appears, a new Service will be created and registered at the core.
100 * When asked to connect to the network (network_connect()) the plugin
101 * will set the Active property on the context. If this operation is
102 * successful the modem is connected to the network. oFono will inform
103 * the plugin about IP configuration through the updating the context's
108 * When a new modem appears, the plugin always powers it up. This
109 * allows the plugin to create connman_device either using IMSI either
110 * using modem Serial if the modem got a SIM interface or not.
112 * As for GSM, the core will call modem_enable() if the technology
113 * is enabled. modem_enable() will then set the modem online.
114 * If the technology is disabled then modem_disable() will just set the
115 * modem offline. The modem is always kept powered all the time.
117 * After setting the modem online the plugin waits for CdmaConnectionManager
118 * interface to appear. Then, once CdmaNetworkRegistration appears, a new
119 * Service will be created and registered at the core.
121 * When asked to connect to the network (network_connect()) the plugin
122 * will power up the CdmaConnectionManager interface.
123 * If the operation is successful the modem is connected to the network.
124 * oFono will inform the plugin about IP configuration through the
125 * updating CdmaConnectionManager settings properties.
128 static DBusConnection *connection;
130 static GHashTable *modem_hash;
131 static GHashTable *context_hash;
133 struct network_context {
137 enum connman_ipconfig_method ipv4_method;
138 struct connman_ipaddress *ipv4_address;
139 char *ipv4_nameservers;
141 enum connman_ipconfig_method ipv6_method;
142 struct connman_ipaddress *ipv6_address;
143 char *ipv6_nameservers;
149 struct connman_device *device;
150 struct connman_network *network;
152 struct network_context *context;
154 /* Modem Interface */
163 /* CDMA ConnectionManager Interface */
164 bool cdma_cm_powered;
166 /* ConnectionManager Interface */
170 /* ConnectionContext Interface */
172 bool valid_apn; /* APN is 'valid' if length > 0 */
174 /* SimManager Interface */
177 /* Netreg Interface */
180 uint8_t data_strength; /* 1xEVDO signal strength */
185 DBusPendingCall *call_set_property;
186 DBusPendingCall *call_get_properties;
187 DBusPendingCall *call_get_contexts;
190 static const char *api2string(enum ofono_api api)
195 case OFONO_API_NETREG:
199 case OFONO_API_CDMA_NETREG:
200 return "cdma-netreg";
201 case OFONO_API_CDMA_CM:
208 static char *get_ident(const char *path)
215 pos = strrchr(path, '/');
222 static struct network_context *network_context_alloc(const char *path)
224 struct network_context *context;
226 context = g_try_new0(struct network_context, 1);
230 context->path = g_strdup(path);
233 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
234 context->ipv4_address = NULL;
235 context->ipv4_nameservers = NULL;
237 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
238 context->ipv6_address = NULL;
239 context->ipv6_nameservers = NULL;
244 static void network_context_free(struct network_context *context)
246 g_free(context->path);
248 connman_ipaddress_free(context->ipv4_address);
249 g_free(context->ipv4_nameservers);
251 connman_ipaddress_free(context->ipv6_address);
252 g_free(context->ipv6_nameservers);
257 static void set_connected(struct modem_data *modem)
259 struct connman_service *service;
261 enum connman_ipconfig_method method;
265 DBG("%s", modem->path);
267 index = modem->context->index;
269 method = modem->context->ipv4_method;
270 if (index < 0 || (!modem->context->ipv4_address &&
271 method == CONNMAN_IPCONFIG_METHOD_FIXED)) {
272 connman_error("Invalid index and/or address");
276 service = connman_service_lookup_from_network(modem->network);
280 connman_service_create_ip4config(service, index);
281 connman_network_set_ipv4_method(modem->network, method);
283 if (method == CONNMAN_IPCONFIG_METHOD_FIXED ||
284 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
288 if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
289 connman_network_set_ipaddress(modem->network,
290 modem->context->ipv4_address);
293 method = modem->context->ipv6_method;
294 connman_service_create_ip6config(service, index);
295 connman_network_set_ipv6_method(modem->network, method);
297 if (method == CONNMAN_IPCONFIG_METHOD_AUTO) {
301 /* Set the nameservers */
302 if (modem->context->ipv4_nameservers &&
303 modem->context->ipv6_nameservers) {
304 nameservers = g_strdup_printf("%s %s",
305 modem->context->ipv4_nameservers,
306 modem->context->ipv6_nameservers);
307 connman_network_set_nameservers(modem->network, nameservers);
309 } else if (modem->context->ipv4_nameservers) {
310 connman_network_set_nameservers(modem->network,
311 modem->context->ipv4_nameservers);
312 } else if (modem->context->ipv6_nameservers) {
313 connman_network_set_nameservers(modem->network,
314 modem->context->ipv6_nameservers);
318 connman_network_set_index(modem->network, index);
319 connman_network_set_connected(modem->network, true);
323 static void set_disconnected(struct modem_data *modem)
325 DBG("%s", modem->path);
328 connman_network_set_connected(modem->network, false);
330 if (modem->context) {
331 g_free(modem->context->ipv4_nameservers);
332 modem->context->ipv4_nameservers = NULL;
333 if (modem->context->ipv4_method != CONNMAN_IPCONFIG_METHOD_OFF)
334 modem->context->ipv4_method =
335 CONNMAN_IPCONFIG_METHOD_UNKNOWN;
337 g_free(modem->context->ipv6_nameservers);
338 modem->context->ipv6_nameservers = NULL;
339 if (modem->context->ipv6_method != CONNMAN_IPCONFIG_METHOD_OFF)
340 modem->context->ipv6_method =
341 CONNMAN_IPCONFIG_METHOD_UNKNOWN;
345 typedef void (*set_property_cb)(struct modem_data *data,
347 typedef void (*get_properties_cb)(struct modem_data *data,
348 DBusMessageIter *dict);
350 struct property_info {
351 struct modem_data *modem;
353 const char *interface;
354 const char *property;
355 set_property_cb set_property_cb;
356 get_properties_cb get_properties_cb;
359 static void set_property_reply(DBusPendingCall *call, void *user_data)
361 struct property_info *info = user_data;
366 DBG("%s path %s %s.%s", info->modem->path,
367 info->path, info->interface, info->property);
369 info->modem->call_set_property = NULL;
371 dbus_error_init(&error);
373 reply = dbus_pending_call_steal_reply(call);
375 if (dbus_set_error_from_message(&error, reply)) {
376 connman_error("Failed to change property: %s %s.%s: %s %s",
377 info->path, info->interface, info->property,
378 error.name, error.message);
379 dbus_error_free(&error);
383 if (info->set_property_cb)
384 (*info->set_property_cb)(info->modem, success);
386 dbus_message_unref(reply);
388 dbus_pending_call_unref(call);
391 static int set_property(struct modem_data *modem,
392 const char *path, const char *interface,
393 const char *property, int type, void *value,
394 set_property_cb notify)
396 DBusMessage *message;
397 DBusMessageIter iter;
398 struct property_info *info;
400 DBG("%s path %s %s.%s", modem->path, path, interface, property);
402 if (modem->call_set_property) {
403 DBG("Cancel pending SetProperty");
405 dbus_pending_call_cancel(modem->call_set_property);
406 modem->call_set_property = NULL;
409 message = dbus_message_new_method_call(OFONO_SERVICE, path,
410 interface, SET_PROPERTY);
414 dbus_message_iter_init_append(message, &iter);
415 connman_dbus_property_append_basic(&iter, property, type, value);
417 if (!dbus_connection_send_with_reply(connection, message,
418 &modem->call_set_property, TIMEOUT)) {
419 connman_error("Failed to change property: %s %s.%s",
420 path, interface, property);
421 dbus_message_unref(message);
425 if (!modem->call_set_property) {
426 connman_error("D-Bus connection not available");
427 dbus_message_unref(message);
431 info = g_try_new0(struct property_info, 1);
433 dbus_message_unref(message);
439 info->interface = interface;
440 info->property = property;
441 info->set_property_cb = notify;
443 dbus_pending_call_set_notify(modem->call_set_property,
444 set_property_reply, info, g_free);
446 dbus_message_unref(message);
451 static void get_properties_reply(DBusPendingCall *call, void *user_data)
453 struct property_info *info = user_data;
454 DBusMessageIter array, dict;
458 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
460 info->modem->call_get_properties = NULL;
462 dbus_error_init(&error);
464 reply = dbus_pending_call_steal_reply(call);
466 if (dbus_set_error_from_message(&error, reply)) {
467 connman_error("Failed to get properties: %s %s: %s %s",
468 info->path, info->interface,
469 error.name, error.message);
470 dbus_error_free(&error);
475 if (!dbus_message_iter_init(reply, &array))
478 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
481 dbus_message_iter_recurse(&array, &dict);
483 if (info->get_properties_cb)
484 (*info->get_properties_cb)(info->modem, &dict);
488 dbus_message_unref(reply);
490 dbus_pending_call_unref(call);
493 static int get_properties(const char *path, const char *interface,
494 get_properties_cb notify,
495 struct modem_data *modem)
497 DBusMessage *message;
498 struct property_info *info;
500 DBG("%s path %s %s", modem->path, path, interface);
502 if (modem->call_get_properties) {
503 connman_error("Pending GetProperties");
507 message = dbus_message_new_method_call(OFONO_SERVICE, path,
508 interface, GET_PROPERTIES);
512 if (!dbus_connection_send_with_reply(connection, message,
513 &modem->call_get_properties, TIMEOUT)) {
514 connman_error("Failed to call %s.GetProperties()", interface);
515 dbus_message_unref(message);
519 if (!modem->call_get_properties) {
520 connman_error("D-Bus connection not available");
521 dbus_message_unref(message);
525 info = g_try_new0(struct property_info, 1);
527 dbus_message_unref(message);
533 info->interface = interface;
534 info->get_properties_cb = notify;
536 dbus_pending_call_set_notify(modem->call_get_properties,
537 get_properties_reply, info, g_free);
539 dbus_message_unref(message);
544 static void context_set_active_reply(struct modem_data *modem,
547 DBG("%s", modem->path);
551 * Don't handle do anything on success here. oFono will send
552 * the change via PropertyChanged singal.
558 * Active = True might fail due a timeout. That means oFono
559 * still tries to go online. If we retry to set Active = True,
560 * we just get a InProgress error message. Should we power
561 * cycle the modem in such cases?
564 if (!modem->network) {
566 * In the case where we power down the device
567 * we don't wait for the reply, therefore the network
568 * might already be gone.
573 connman_network_set_error(modem->network,
574 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
577 static int context_set_active(struct modem_data *modem,
582 DBG("%s active %d", modem->path, active);
584 err = set_property(modem, modem->context->path,
585 OFONO_CONTEXT_INTERFACE,
586 "Active", DBUS_TYPE_BOOLEAN,
588 context_set_active_reply);
590 if (!active && err == -EINPROGRESS)
596 static void cdma_cm_set_powered_reply(struct modem_data *modem,
599 DBG("%s", modem->path);
603 * Don't handle do anything on success here. oFono will send
604 * the change via PropertyChanged singal.
610 * Powered = True might fail due a timeout. That means oFono
611 * still tries to go online. If we retry to set Powered = True,
612 * we just get a InProgress error message. Should we power
613 * cycle the modem in such cases?
616 if (!modem->network) {
618 * In the case where we power down the device
619 * we don't wait for the reply, therefore the network
620 * might already be gone.
625 connman_network_set_error(modem->network,
626 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
629 static int cdma_cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
633 DBG("%s powered %d", modem->path, powered);
635 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
636 "Powered", DBUS_TYPE_BOOLEAN,
638 cdma_cm_set_powered_reply);
640 if (!powered && err == -EINPROGRESS)
646 static int modem_set_online(struct modem_data *modem, dbus_bool_t online)
648 DBG("%s online %d", modem->path, online);
650 return set_property(modem, modem->path,
651 OFONO_MODEM_INTERFACE,
652 "Online", DBUS_TYPE_BOOLEAN,
657 static int cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
661 DBG("%s powered %d", modem->path, powered);
663 err = set_property(modem, modem->path,
665 "Powered", DBUS_TYPE_BOOLEAN,
669 if (!powered && err == -EINPROGRESS)
675 static int modem_set_powered(struct modem_data *modem, dbus_bool_t powered)
679 DBG("%s powered %d", modem->path, powered);
681 modem->set_powered = powered;
683 err = set_property(modem, modem->path,
684 OFONO_MODEM_INTERFACE,
685 "Powered", DBUS_TYPE_BOOLEAN,
689 if (!powered && err == -EINPROGRESS)
695 static bool has_interface(uint8_t interfaces,
698 if ((interfaces & api) == api)
704 static uint8_t extract_interfaces(DBusMessageIter *array)
706 DBusMessageIter entry;
707 uint8_t interfaces = 0;
709 dbus_message_iter_recurse(array, &entry);
711 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
714 dbus_message_iter_get_basic(&entry, &name);
716 if (g_str_equal(name, OFONO_SIM_INTERFACE))
717 interfaces |= OFONO_API_SIM;
718 else if (g_str_equal(name, OFONO_NETREG_INTERFACE))
719 interfaces |= OFONO_API_NETREG;
720 else if (g_str_equal(name, OFONO_CM_INTERFACE))
721 interfaces |= OFONO_API_CM;
722 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE))
723 interfaces |= OFONO_API_CDMA_CM;
724 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE))
725 interfaces |= OFONO_API_CDMA_NETREG;
727 dbus_message_iter_next(&entry);
733 static char *extract_nameservers(DBusMessageIter *array)
735 DBusMessageIter entry;
736 char *nameservers = NULL;
739 dbus_message_iter_recurse(array, &entry);
741 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
742 const char *nameserver;
744 dbus_message_iter_get_basic(&entry, &nameserver);
747 nameservers = g_strdup(nameserver);
750 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
754 dbus_message_iter_next(&entry);
760 static void extract_ipv4_settings(DBusMessageIter *array,
761 struct network_context *context)
763 DBusMessageIter dict;
764 char *address = NULL, *netmask = NULL, *gateway = NULL;
765 char *nameservers = NULL;
766 const char *interface = NULL;
769 connman_ipaddress_free(context->ipv4_address);
770 context->ipv4_address = NULL;
773 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
776 dbus_message_iter_recurse(array, &dict);
778 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
779 DBusMessageIter entry, value;
780 const char *key, *val;
782 dbus_message_iter_recurse(&dict, &entry);
783 dbus_message_iter_get_basic(&entry, &key);
785 dbus_message_iter_next(&entry);
786 dbus_message_iter_recurse(&entry, &value);
788 if (g_str_equal(key, "Interface")) {
789 dbus_message_iter_get_basic(&value, &interface);
791 DBG("Interface %s", interface);
793 index = connman_inet_ifindex(interface);
795 DBG("index %d", index);
796 } else if (g_str_equal(key, "Method")) {
797 dbus_message_iter_get_basic(&value, &val);
799 DBG("Method %s", val);
801 if (g_strcmp0(val, "static") == 0)
802 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
803 else if (g_strcmp0(val, "dhcp") == 0)
804 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
806 } else if (g_str_equal(key, "Address")) {
807 dbus_message_iter_get_basic(&value, &val);
809 address = g_strdup(val);
811 DBG("Address %s", address);
812 } else if (g_str_equal(key, "Netmask")) {
813 dbus_message_iter_get_basic(&value, &val);
815 netmask = g_strdup(val);
817 DBG("Netmask %s", netmask);
818 } else if (g_str_equal(key, "DomainNameServers")) {
819 nameservers = extract_nameservers(&value);
821 DBG("Nameservers %s", nameservers);
822 } else if (g_str_equal(key, "Gateway")) {
823 dbus_message_iter_get_basic(&value, &val);
825 gateway = g_strdup(val);
827 DBG("Gateway %s", gateway);
830 dbus_message_iter_next(&dict);
836 context->index = index;
838 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
841 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
842 if (!context->ipv4_address) {
847 connman_ipaddress_set_ipv4(context->ipv4_address, address,
850 g_free(context->ipv4_nameservers);
851 context->ipv4_nameservers = nameservers;
854 if (context->ipv4_nameservers != nameservers)
862 static void extract_ipv6_settings(DBusMessageIter *array,
863 struct network_context *context)
865 DBusMessageIter dict;
866 char *address = NULL, *gateway = NULL;
867 unsigned char prefix_length = 0;
868 char *nameservers = NULL;
869 const char *interface = NULL;
872 connman_ipaddress_free(context->ipv6_address);
873 context->ipv6_address = NULL;
876 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
879 dbus_message_iter_recurse(array, &dict);
881 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
882 DBusMessageIter entry, value;
883 const char *key, *val;
885 dbus_message_iter_recurse(&dict, &entry);
886 dbus_message_iter_get_basic(&entry, &key);
888 dbus_message_iter_next(&entry);
889 dbus_message_iter_recurse(&entry, &value);
891 if (g_str_equal(key, "Interface")) {
892 dbus_message_iter_get_basic(&value, &interface);
894 DBG("Interface %s", interface);
896 index = connman_inet_ifindex(interface);
898 DBG("index %d", index);
899 } else if (g_str_equal(key, "Address")) {
900 dbus_message_iter_get_basic(&value, &val);
902 address = g_strdup(val);
904 DBG("Address %s", address);
905 } else if (g_str_equal(key, "PrefixLength")) {
906 dbus_message_iter_get_basic(&value, &prefix_length);
908 DBG("prefix length %d", prefix_length);
909 } else if (g_str_equal(key, "DomainNameServers")) {
910 nameservers = extract_nameservers(&value);
912 DBG("Nameservers %s", nameservers);
913 } else if (g_str_equal(key, "Gateway")) {
914 dbus_message_iter_get_basic(&value, &val);
916 gateway = g_strdup(val);
918 DBG("Gateway %s", gateway);
921 dbus_message_iter_next(&dict);
927 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_AUTO;
929 context->ipv6_address =
930 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
931 if (!context->ipv6_address)
934 context->index = index;
935 connman_ipaddress_set_ipv6(context->ipv6_address, address,
936 prefix_length, gateway);
938 g_free(context->ipv6_nameservers);
939 context->ipv6_nameservers = nameservers;
942 if (context->ipv6_nameservers != nameservers)
949 static bool ready_to_create_device(struct modem_data *modem)
952 * There are three different modem types which behave slightly
954 * - GSM modems will expose the SIM interface then the
956 * - CDMA modems will expose CM first and sometime later
957 * a unique serial number.
959 * This functions tests if we have the necessary information gathered
960 * before we are able to create a device.
966 if (modem->imsi || modem->serial)
972 static void create_device(struct modem_data *modem)
974 struct connman_device *device;
977 DBG("%s", modem->path);
981 else if (modem->serial)
982 ident = modem->serial;
984 if (!connman_dbus_validate_ident(ident))
985 ident = connman_dbus_encode_string(ident);
987 ident = g_strdup(ident);
989 device = connman_device_create("ofono", CONNMAN_DEVICE_TYPE_CELLULAR);
993 DBG("device %p", device);
995 connman_device_set_ident(device, ident);
997 connman_device_set_string(device, "Path", modem->path);
999 connman_device_set_data(device, modem);
1001 if (connman_device_register(device) < 0) {
1002 connman_error("Failed to register cellular device");
1003 connman_device_unref(device);
1007 modem->device = device;
1009 connman_device_set_powered(modem->device, modem->online);
1014 static void destroy_device(struct modem_data *modem)
1016 DBG("%s", modem->path);
1018 connman_device_set_powered(modem->device, false);
1020 if (modem->network) {
1021 connman_device_remove_network(modem->device, modem->network);
1022 connman_network_unref(modem->network);
1023 modem->network = NULL;
1026 connman_device_unregister(modem->device);
1027 connman_device_unref(modem->device);
1029 modem->device = NULL;
1032 static void add_network(struct modem_data *modem)
1036 DBG("%s", modem->path);
1041 modem->network = connman_network_create(modem->context->path,
1042 CONNMAN_NETWORK_TYPE_CELLULAR);
1043 if (!modem->network)
1046 DBG("network %p", modem->network);
1048 connman_network_set_data(modem->network, modem);
1050 connman_network_set_string(modem->network, "Path",
1051 modem->context->path);
1054 connman_network_set_name(modem->network, modem->name);
1056 connman_network_set_name(modem->network, "");
1058 connman_network_set_strength(modem->network, modem->strength);
1060 group = get_ident(modem->context->path);
1061 connman_network_set_group(modem->network, group);
1063 connman_network_set_bool(modem->network, "Roaming",
1066 if (connman_device_add_network(modem->device, modem->network) < 0) {
1067 connman_network_unref(modem->network);
1068 modem->network = NULL;
1073 static void remove_network(struct modem_data *modem)
1075 DBG("%s", modem->path);
1077 if (!modem->network)
1080 DBG("network %p", modem->network);
1082 connman_device_remove_network(modem->device, modem->network);
1083 connman_network_unref(modem->network);
1084 modem->network = NULL;
1087 static int set_context_ipconfig(struct network_context *context,
1088 const char *protocol)
1090 DBG("context %p protocol %s", context, protocol);
1092 if (!context || !protocol)
1095 if (g_str_equal(protocol, "ip")) {
1096 if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
1097 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1099 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_OFF;
1101 connman_ipaddress_free(context->ipv6_address);
1102 context->ipv6_address = NULL;
1104 } else if (g_str_equal(protocol, "ipv6")) {
1105 if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
1106 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1108 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_OFF;
1110 connman_ipaddress_free(context->ipv4_address);
1111 context->ipv4_address = NULL;
1113 } else if (g_str_equal(protocol, "dual")) {
1114 if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
1115 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1117 if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
1118 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1121 DBG("ipv4 method %d ipv6 method %d", context->ipv4_method,
1122 context->ipv6_method);
1127 static int add_cm_context(struct modem_data *modem, const char *context_path,
1128 DBusMessageIter *dict)
1130 const char *context_type = NULL;
1131 struct network_context *context = NULL;
1132 dbus_bool_t active = FALSE;
1133 const char *ip_protocol = NULL;
1135 DBG("%s context path %s", modem->path, context_path);
1137 if (modem->context) {
1139 * We have already assigned a context to this modem
1140 * and we do only support one Internet context.
1145 context = network_context_alloc(context_path);
1149 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1150 DBusMessageIter entry, value;
1153 dbus_message_iter_recurse(dict, &entry);
1154 dbus_message_iter_get_basic(&entry, &key);
1156 dbus_message_iter_next(&entry);
1157 dbus_message_iter_recurse(&entry, &value);
1159 if (g_str_equal(key, "Type")) {
1160 dbus_message_iter_get_basic(&value, &context_type);
1162 DBG("%s context %s type %s", modem->path,
1163 context_path, context_type);
1164 } else if (g_str_equal(key, "Settings")) {
1165 DBG("%s Settings", modem->path);
1167 extract_ipv4_settings(&value, context);
1168 } else if (g_str_equal(key, "IPv6.Settings")) {
1169 DBG("%s IPv6.Settings", modem->path);
1171 extract_ipv6_settings(&value, context);
1172 } else if (g_str_equal(key, "Active")) {
1173 dbus_message_iter_get_basic(&value, &active);
1175 DBG("%s Active %d", modem->path, active);
1176 } else if (g_str_equal(key, "AccessPointName")) {
1179 dbus_message_iter_get_basic(&value, &apn);
1180 if (apn && strlen(apn) > 0)
1181 modem->valid_apn = true;
1183 modem->valid_apn = false;
1185 DBG("%s AccessPointName '%s'", modem->path, apn);
1186 } else if (g_str_equal(key, "Protocol") &&
1187 dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
1189 dbus_message_iter_get_basic(&value, &ip_protocol);
1191 DBG("%s Protocol %s", modem->path, ip_protocol);
1194 dbus_message_iter_next(dict);
1197 if (g_strcmp0(context_type, "internet") != 0) {
1198 network_context_free(context);
1203 set_context_ipconfig(context, ip_protocol);
1205 modem->context = context;
1206 modem->active = active;
1208 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1210 if (modem->valid_apn && modem->attached &&
1211 has_interface(modem->interfaces,
1212 OFONO_API_NETREG)) {
1219 static void remove_cm_context(struct modem_data *modem,
1220 const char *context_path)
1222 if (!modem->context)
1226 remove_network(modem);
1228 g_hash_table_remove(context_hash, context_path);
1230 network_context_free(modem->context);
1231 modem->context = NULL;
1233 modem->valid_apn = false;
1236 remove_network(modem);
1239 static gboolean context_changed(DBusConnection *conn,
1240 DBusMessage *message,
1243 const char *context_path = dbus_message_get_path(message);
1244 struct modem_data *modem = NULL;
1245 DBusMessageIter iter, value;
1248 DBG("context_path %s", context_path);
1250 modem = g_hash_table_lookup(context_hash, context_path);
1254 if (!dbus_message_iter_init(message, &iter))
1257 dbus_message_iter_get_basic(&iter, &key);
1259 dbus_message_iter_next(&iter);
1260 dbus_message_iter_recurse(&iter, &value);
1263 * oFono guarantees the ordering of Settings and
1264 * Active. Settings will always be send before Active = True.
1265 * That means we don't have to order here.
1267 if (g_str_equal(key, "Settings")) {
1268 DBG("%s Settings", modem->path);
1270 extract_ipv4_settings(&value, modem->context);
1271 } else if (g_str_equal(key, "IPv6.Settings")) {
1272 DBG("%s IPv6.Settings", modem->path);
1274 extract_ipv6_settings(&value, modem->context);
1275 } else if (g_str_equal(key, "Active")) {
1278 dbus_message_iter_get_basic(&value, &active);
1279 modem->active = active;
1281 DBG("%s Active %d", modem->path, modem->active);
1284 set_connected(modem);
1286 set_disconnected(modem);
1287 } else if (g_str_equal(key, "AccessPointName")) {
1290 dbus_message_iter_get_basic(&value, &apn);
1292 DBG("%s AccessPointName %s", modem->path, apn);
1294 if (apn && strlen(apn) > 0) {
1295 modem->valid_apn = true;
1300 if (!modem->attached)
1303 if (!has_interface(modem->interfaces,
1310 set_connected(modem);
1312 modem->valid_apn = false;
1314 if (!modem->network)
1317 remove_network(modem);
1320 } else if (g_str_equal(key, "Protocol") &&
1321 dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
1322 const char *ip_protocol;
1324 dbus_message_iter_get_basic(&value, &ip_protocol);
1326 set_context_ipconfig(modem->context, ip_protocol);
1332 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1334 struct modem_data *modem = user_data;
1335 DBusMessageIter array, dict, entry, value;
1339 DBG("%s", modem->path);
1341 modem->call_get_contexts = NULL;
1343 reply = dbus_pending_call_steal_reply(call);
1345 dbus_error_init(&error);
1347 if (dbus_set_error_from_message(&error, reply)) {
1348 connman_error("%s", error.message);
1349 dbus_error_free(&error);
1353 if (!dbus_message_iter_init(reply, &array))
1356 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1359 dbus_message_iter_recurse(&array, &dict);
1361 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1362 const char *context_path;
1364 dbus_message_iter_recurse(&dict, &entry);
1365 dbus_message_iter_get_basic(&entry, &context_path);
1367 dbus_message_iter_next(&entry);
1368 dbus_message_iter_recurse(&entry, &value);
1370 if (add_cm_context(modem, context_path, &value) == 0)
1373 dbus_message_iter_next(&dict);
1377 dbus_message_unref(reply);
1379 dbus_pending_call_unref(call);
1382 static int cm_get_contexts(struct modem_data *modem)
1384 DBusMessage *message;
1386 DBG("%s", modem->path);
1388 if (modem->call_get_contexts)
1391 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1392 OFONO_CM_INTERFACE, GET_CONTEXTS);
1396 if (!dbus_connection_send_with_reply(connection, message,
1397 &modem->call_get_contexts, TIMEOUT)) {
1398 connman_error("Failed to call GetContexts()");
1399 dbus_message_unref(message);
1403 if (!modem->call_get_contexts) {
1404 connman_error("D-Bus connection not available");
1405 dbus_message_unref(message);
1409 dbus_pending_call_set_notify(modem->call_get_contexts,
1410 cm_get_contexts_reply,
1413 dbus_message_unref(message);
1415 return -EINPROGRESS;
1418 static gboolean cm_context_added(DBusConnection *conn,
1419 DBusMessage *message,
1422 const char *path = dbus_message_get_path(message);
1424 struct modem_data *modem;
1425 DBusMessageIter iter, properties;
1429 modem = g_hash_table_lookup(modem_hash, path);
1433 if (!dbus_message_iter_init(message, &iter))
1436 dbus_message_iter_get_basic(&iter, &context_path);
1438 dbus_message_iter_next(&iter);
1439 dbus_message_iter_recurse(&iter, &properties);
1441 if (add_cm_context(modem, context_path, &properties) != 0)
1447 static gboolean cm_context_removed(DBusConnection *conn,
1448 DBusMessage *message,
1451 const char *path = dbus_message_get_path(message);
1452 const char *context_path;
1453 struct modem_data *modem;
1454 DBusMessageIter iter;
1456 DBG("context path %s", path);
1458 if (!dbus_message_iter_init(message, &iter))
1461 dbus_message_iter_get_basic(&iter, &context_path);
1463 modem = g_hash_table_lookup(context_hash, context_path);
1467 remove_cm_context(modem, context_path);
1472 static void netreg_update_name(struct modem_data *modem,
1473 DBusMessageIter* value)
1477 dbus_message_iter_get_basic(value, &name);
1479 DBG("%s Name %s", modem->path, name);
1481 g_free(modem->name);
1482 modem->name = g_strdup(name);
1484 if (!modem->network)
1487 connman_network_set_name(modem->network, modem->name);
1488 connman_network_update(modem->network);
1491 static void netreg_update_strength(struct modem_data *modem,
1492 DBusMessageIter *value)
1494 dbus_message_iter_get_basic(value, &modem->strength);
1496 DBG("%s Strength %d", modem->path, modem->strength);
1498 if (!modem->network)
1503 * We don't have 2 signal notifications we always report the strength
1504 * signal. data_strength is always equal to 0.
1507 * In the case we have a data_strength signal (from 1xEVDO network)
1508 * we don't need to update the value with strength signal (from 1xCDMA)
1509 * because the modem is registered to 1xEVDO network for data call.
1510 * In case we have no data_strength signal (not registered to 1xEVDO
1511 * network), we must report the strength signal (registered to 1xCDMA
1512 * network e.g slow mode).
1514 if (modem->data_strength != 0)
1517 connman_network_set_strength(modem->network, modem->strength);
1518 connman_network_update(modem->network);
1521 /* Retrieve 1xEVDO Data Strength signal */
1522 static void netreg_update_datastrength(struct modem_data *modem,
1523 DBusMessageIter *value)
1525 dbus_message_iter_get_basic(value, &modem->data_strength);
1527 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1529 if (!modem->network)
1533 * CDMA modem is not registered to 1xEVDO network, let
1534 * update_signal_strength() reporting the value on the Strength signal
1537 if (modem->data_strength == 0)
1540 connman_network_set_strength(modem->network, modem->data_strength);
1541 connman_network_update(modem->network);
1544 static void netreg_update_status(struct modem_data *modem,
1545 DBusMessageIter *value)
1550 dbus_message_iter_get_basic(value, &status);
1552 roaming = g_str_equal(status, "roaming");
1553 modem->registered = roaming || g_str_equal(status, "registered");
1555 if (roaming == modem->roaming)
1558 modem->roaming = roaming;
1560 if (!modem->network)
1563 connman_network_set_bool(modem->network,
1564 "Roaming", modem->roaming);
1565 connman_network_update(modem->network);
1568 static void netreg_update_regdom(struct modem_data *modem,
1569 DBusMessageIter *value)
1571 char *mobile_country_code;
1575 dbus_message_iter_get_basic(value, &mobile_country_code);
1577 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1580 mcc = atoi(mobile_country_code);
1581 if (mcc > 799 || mcc < 200)
1584 alpha2 = mcc_country_codes[mcc - 200];
1586 connman_technology_set_regdom(alpha2);
1589 static gboolean netreg_changed(DBusConnection *conn, DBusMessage *message,
1592 const char *path = dbus_message_get_path(message);
1593 struct modem_data *modem;
1594 DBusMessageIter iter, value;
1597 modem = g_hash_table_lookup(modem_hash, path);
1604 if (!dbus_message_iter_init(message, &iter))
1607 dbus_message_iter_get_basic(&iter, &key);
1609 dbus_message_iter_next(&iter);
1610 dbus_message_iter_recurse(&iter, &value);
1612 if (g_str_equal(key, "Name"))
1613 netreg_update_name(modem, &value);
1614 else if (g_str_equal(key, "Strength"))
1615 netreg_update_strength(modem, &value);
1616 else if (g_str_equal(key, "Status"))
1617 netreg_update_status(modem, &value);
1618 else if (g_str_equal(key, "MobileCountryCode"))
1619 netreg_update_regdom(modem, &value);
1624 static void netreg_properties_reply(struct modem_data *modem,
1625 DBusMessageIter *dict)
1627 DBG("%s", modem->path);
1629 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1630 DBusMessageIter entry, value;
1633 dbus_message_iter_recurse(dict, &entry);
1634 dbus_message_iter_get_basic(&entry, &key);
1636 dbus_message_iter_next(&entry);
1637 dbus_message_iter_recurse(&entry, &value);
1639 if (g_str_equal(key, "Name"))
1640 netreg_update_name(modem, &value);
1641 else if (g_str_equal(key, "Strength"))
1642 netreg_update_strength(modem, &value);
1643 else if (g_str_equal(key, "Status"))
1644 netreg_update_status(modem, &value);
1645 else if (g_str_equal(key, "MobileCountryCode"))
1646 netreg_update_regdom(modem, &value);
1648 dbus_message_iter_next(dict);
1651 if (!modem->context) {
1653 * netgreg_get_properties() was issued after we got
1654 * cm_get_contexts_reply() where we create the
1655 * context. Though before we got the
1656 * netreg_properties_reply the context was removed
1657 * again. Therefore we have to skip the network
1663 if (modem->valid_apn)
1667 set_connected(modem);
1670 static int netreg_get_properties(struct modem_data *modem)
1672 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1673 netreg_properties_reply, modem);
1676 static void add_cdma_network(struct modem_data *modem)
1678 /* Be sure that device is created before adding CDMA network */
1683 * CDMA modems don't need contexts for data call, however the current
1684 * add_network() logic needs one, so we create one to proceed.
1686 if (!modem->context)
1687 modem->context = network_context_alloc(modem->path);
1690 modem->name = g_strdup("CDMA Network");
1694 if (modem->cdma_cm_powered)
1695 set_connected(modem);
1698 static gboolean cdma_netreg_changed(DBusConnection *conn,
1699 DBusMessage *message,
1702 const char *path = dbus_message_get_path(message);
1703 struct modem_data *modem;
1704 DBusMessageIter iter, value;
1709 modem = g_hash_table_lookup(modem_hash, path);
1716 if (!dbus_message_iter_init(message, &iter))
1719 dbus_message_iter_get_basic(&iter, &key);
1721 dbus_message_iter_next(&iter);
1722 dbus_message_iter_recurse(&iter, &value);
1724 if (g_str_equal(key, "Name"))
1725 netreg_update_name(modem, &value);
1726 else if (g_str_equal(key, "Strength"))
1727 netreg_update_strength(modem, &value);
1728 else if (g_str_equal(key, "DataStrength"))
1729 netreg_update_datastrength(modem, &value);
1730 else if (g_str_equal(key, "Status"))
1731 netreg_update_status(modem, &value);
1733 if (modem->registered)
1734 add_cdma_network(modem);
1736 remove_network(modem);
1741 static void cdma_netreg_properties_reply(struct modem_data *modem,
1742 DBusMessageIter *dict)
1744 DBG("%s", modem->path);
1746 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1747 DBusMessageIter entry, value;
1750 dbus_message_iter_recurse(dict, &entry);
1751 dbus_message_iter_get_basic(&entry, &key);
1753 dbus_message_iter_next(&entry);
1754 dbus_message_iter_recurse(&entry, &value);
1756 if (g_str_equal(key, "Name"))
1757 netreg_update_name(modem, &value);
1758 else if (g_str_equal(key, "Strength"))
1759 netreg_update_strength(modem, &value);
1760 else if (g_str_equal(key, "DataStrength"))
1761 netreg_update_datastrength(modem, &value);
1762 else if (g_str_equal(key, "Status"))
1763 netreg_update_status(modem, &value);
1765 dbus_message_iter_next(dict);
1768 if (modem->registered)
1769 add_cdma_network(modem);
1771 remove_network(modem);
1774 static int cdma_netreg_get_properties(struct modem_data *modem)
1776 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1777 cdma_netreg_properties_reply, modem);
1780 static void cm_update_attached(struct modem_data *modem,
1781 DBusMessageIter *value)
1783 dbus_bool_t attached;
1785 dbus_message_iter_get_basic(value, &attached);
1786 modem->attached = attached;
1788 DBG("%s Attached %d", modem->path, modem->attached);
1790 if (!modem->attached) {
1791 remove_network(modem);
1795 if (!has_interface(modem->interfaces, OFONO_API_NETREG))
1798 netreg_get_properties(modem);
1801 static void cm_update_powered(struct modem_data *modem,
1802 DBusMessageIter *value)
1804 dbus_bool_t cm_powered;
1806 dbus_message_iter_get_basic(value, &cm_powered);
1807 modem->cm_powered = cm_powered;
1809 DBG("%s ConnnectionManager Powered %d", modem->path,
1812 if (modem->cm_powered)
1815 cm_set_powered(modem, TRUE);
1818 static gboolean cm_changed(DBusConnection *conn, DBusMessage *message,
1821 const char *path = dbus_message_get_path(message);
1822 struct modem_data *modem;
1823 DBusMessageIter iter, value;
1826 modem = g_hash_table_lookup(modem_hash, path);
1833 if (!dbus_message_iter_init(message, &iter))
1836 dbus_message_iter_get_basic(&iter, &key);
1838 dbus_message_iter_next(&iter);
1839 dbus_message_iter_recurse(&iter, &value);
1841 if (g_str_equal(key, "Attached"))
1842 cm_update_attached(modem, &value);
1843 else if (g_str_equal(key, "Powered"))
1844 cm_update_powered(modem, &value);
1849 static void cdma_cm_update_powered(struct modem_data *modem,
1850 DBusMessageIter *value)
1852 dbus_bool_t cdma_cm_powered;
1854 dbus_message_iter_get_basic(value, &cdma_cm_powered);
1855 modem->cdma_cm_powered = cdma_cm_powered;
1857 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1859 if (!modem->network)
1862 if (modem->cdma_cm_powered)
1863 set_connected(modem);
1865 set_disconnected(modem);
1868 static void cdma_cm_update_settings(struct modem_data *modem,
1869 DBusMessageIter *value)
1871 DBG("%s Settings", modem->path);
1873 extract_ipv4_settings(value, modem->context);
1876 static gboolean cdma_cm_changed(DBusConnection *conn,
1877 DBusMessage *message, void *user_data)
1879 const char *path = dbus_message_get_path(message);
1880 struct modem_data *modem;
1881 DBusMessageIter iter, value;
1884 modem = g_hash_table_lookup(modem_hash, path);
1888 if (modem->online && !modem->network)
1889 cdma_netreg_get_properties(modem);
1891 if (!dbus_message_iter_init(message, &iter))
1894 dbus_message_iter_get_basic(&iter, &key);
1896 dbus_message_iter_next(&iter);
1897 dbus_message_iter_recurse(&iter, &value);
1899 if (g_str_equal(key, "Powered"))
1900 cdma_cm_update_powered(modem, &value);
1901 if (g_str_equal(key, "Settings"))
1902 cdma_cm_update_settings(modem, &value);
1907 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1909 DBG("%s", modem->path);
1911 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1912 DBusMessageIter entry, value;
1915 dbus_message_iter_recurse(dict, &entry);
1916 dbus_message_iter_get_basic(&entry, &key);
1918 dbus_message_iter_next(&entry);
1919 dbus_message_iter_recurse(&entry, &value);
1921 if (g_str_equal(key, "Attached"))
1922 cm_update_attached(modem, &value);
1923 else if (g_str_equal(key, "Powered"))
1924 cm_update_powered(modem, &value);
1926 dbus_message_iter_next(dict);
1930 static int cm_get_properties(struct modem_data *modem)
1932 return get_properties(modem->path, OFONO_CM_INTERFACE,
1933 cm_properties_reply, modem);
1936 static void cdma_cm_properties_reply(struct modem_data *modem,
1937 DBusMessageIter *dict)
1939 DBG("%s", modem->path);
1942 cdma_netreg_get_properties(modem);
1944 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1945 DBusMessageIter entry, value;
1948 dbus_message_iter_recurse(dict, &entry);
1949 dbus_message_iter_get_basic(&entry, &key);
1951 dbus_message_iter_next(&entry);
1952 dbus_message_iter_recurse(&entry, &value);
1954 if (g_str_equal(key, "Powered"))
1955 cdma_cm_update_powered(modem, &value);
1956 if (g_str_equal(key, "Settings"))
1957 cdma_cm_update_settings(modem, &value);
1959 dbus_message_iter_next(dict);
1963 static int cdma_cm_get_properties(struct modem_data *modem)
1965 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1966 cdma_cm_properties_reply, modem);
1969 static void sim_update_imsi(struct modem_data *modem,
1970 DBusMessageIter *value)
1974 dbus_message_iter_get_basic(value, &imsi);
1976 DBG("%s imsi %s", modem->path, imsi);
1978 g_free(modem->imsi);
1979 modem->imsi = g_strdup(imsi);
1982 static gboolean sim_changed(DBusConnection *conn, DBusMessage *message,
1985 const char *path = dbus_message_get_path(message);
1986 struct modem_data *modem;
1987 DBusMessageIter iter, value;
1990 modem = g_hash_table_lookup(modem_hash, path);
1997 if (!dbus_message_iter_init(message, &iter))
2000 dbus_message_iter_get_basic(&iter, &key);
2002 dbus_message_iter_next(&iter);
2003 dbus_message_iter_recurse(&iter, &value);
2005 if (g_str_equal(key, "SubscriberIdentity")) {
2006 sim_update_imsi(modem, &value);
2008 if (!ready_to_create_device(modem))
2012 * This is a GSM modem. Create the device and
2013 * register it at the core. Enabling (setting
2014 * it online is done through the
2015 * modem_enable() callback.
2017 create_device(modem);
2023 static void sim_properties_reply(struct modem_data *modem,
2024 DBusMessageIter *dict)
2026 DBG("%s", modem->path);
2028 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
2029 DBusMessageIter entry, value;
2032 dbus_message_iter_recurse(dict, &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, "SubscriberIdentity")) {
2039 sim_update_imsi(modem, &value);
2041 if (!ready_to_create_device(modem))
2045 * This is a GSM modem. Create the device and
2046 * register it at the core. Enabling (setting
2047 * it online is done through the
2048 * modem_enable() callback.
2050 create_device(modem);
2056 * The modem is already online and we have the CM interface.
2057 * There will be no interface update and therefore our
2058 * state machine will not go to next step. We have to
2059 * trigger it from here.
2061 if (has_interface(modem->interfaces, OFONO_API_CM)) {
2062 cm_get_properties(modem);
2063 cm_get_contexts(modem);
2068 dbus_message_iter_next(dict);
2072 static int sim_get_properties(struct modem_data *modem)
2074 return get_properties(modem->path, OFONO_SIM_INTERFACE,
2075 sim_properties_reply, modem);
2078 static bool api_added(uint8_t old_iface, uint8_t new_iface,
2081 if (!has_interface(old_iface, api) &&
2082 has_interface(new_iface, api)) {
2083 DBG("%s added", api2string(api));
2090 static bool api_removed(uint8_t old_iface, uint8_t new_iface,
2093 if (has_interface(old_iface, api) &&
2094 !has_interface(new_iface, api)) {
2095 DBG("%s removed", api2string(api));
2102 static void modem_update_interfaces(struct modem_data *modem,
2106 DBG("%s", modem->path);
2108 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM)) {
2110 !modem->set_powered) {
2112 * Only use do GetProperties() when
2113 * device has not been powered up.
2115 sim_get_properties(modem);
2119 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM)) {
2120 if (modem->device) {
2121 cm_get_properties(modem);
2122 cm_get_contexts(modem);
2126 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM)) {
2127 if (ready_to_create_device(modem)) {
2128 create_device(modem);
2129 if (modem->registered)
2130 add_cdma_network(modem);
2134 cdma_cm_get_properties(modem);
2137 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG)) {
2138 if (modem->attached)
2139 netreg_get_properties(modem);
2142 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG))
2143 cdma_netreg_get_properties(modem);
2145 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM))
2146 remove_cm_context(modem, modem->context->path);
2148 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM))
2149 remove_cm_context(modem, modem->context->path);
2151 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG))
2152 remove_network(modem);
2154 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG))
2155 remove_network(modem);
2158 static gboolean modem_changed(DBusConnection *conn, DBusMessage *message,
2161 const char *path = dbus_message_get_path(message);
2162 struct modem_data *modem;
2163 DBusMessageIter iter, value;
2166 modem = g_hash_table_lookup(modem_hash, path);
2173 if (!dbus_message_iter_init(message, &iter))
2176 dbus_message_iter_get_basic(&iter, &key);
2178 dbus_message_iter_next(&iter);
2179 dbus_message_iter_recurse(&iter, &value);
2181 if (g_str_equal(key, "Powered")) {
2182 dbus_bool_t powered;
2184 dbus_message_iter_get_basic(&value, &powered);
2185 modem->powered = powered;
2187 DBG("%s Powered %d", modem->path, modem->powered);
2189 if (!modem->powered)
2190 modem_set_powered(modem, TRUE);
2191 } else if (g_str_equal(key, "Online")) {
2194 dbus_message_iter_get_basic(&value, &online);
2195 modem->online = online;
2197 DBG("%s Online %d", modem->path, modem->online);
2202 connman_device_set_powered(modem->device, modem->online);
2203 } else if (g_str_equal(key, "Interfaces")) {
2206 interfaces = extract_interfaces(&value);
2208 if (interfaces == modem->interfaces)
2211 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2213 modem_update_interfaces(modem, modem->interfaces, interfaces);
2215 modem->interfaces = interfaces;
2216 } else if (g_str_equal(key, "Serial")) {
2219 dbus_message_iter_get_basic(&value, &serial);
2221 g_free(modem->serial);
2222 modem->serial = g_strdup(serial);
2224 DBG("%s Serial %s", modem->path, modem->serial);
2226 if (has_interface(modem->interfaces,
2227 OFONO_API_CDMA_CM)) {
2228 if (ready_to_create_device(modem)) {
2229 create_device(modem);
2230 if (modem->registered)
2231 add_cdma_network(modem);
2239 static void add_modem(const char *path, DBusMessageIter *prop)
2241 struct modem_data *modem;
2245 modem = g_hash_table_lookup(modem_hash, path);
2248 * When oFono powers up we ask for the modems and oFono is
2249 * reporting with modem_added signal the modems. Only
2255 modem = g_try_new0(struct modem_data, 1);
2259 modem->path = g_strdup(path);
2261 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2263 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2264 DBusMessageIter entry, value;
2267 dbus_message_iter_recurse(prop, &entry);
2268 dbus_message_iter_get_basic(&entry, &key);
2270 dbus_message_iter_next(&entry);
2271 dbus_message_iter_recurse(&entry, &value);
2273 if (g_str_equal(key, "Powered")) {
2274 dbus_bool_t powered;
2276 dbus_message_iter_get_basic(&value, &powered);
2277 modem->powered = powered;
2279 DBG("%s Powered %d", modem->path, modem->powered);
2280 } else if (g_str_equal(key, "Online")) {
2283 dbus_message_iter_get_basic(&value, &online);
2284 modem->online = online;
2286 DBG("%s Online %d", modem->path, modem->online);
2287 } else if (g_str_equal(key, "Interfaces")) {
2288 modem->interfaces = extract_interfaces(&value);
2290 DBG("%s Interfaces 0x%02x", modem->path,
2292 } else if (g_str_equal(key, "Serial")) {
2295 dbus_message_iter_get_basic(&value, &serial);
2296 modem->serial = g_strdup(serial);
2298 DBG("%s Serial %s", modem->path, modem->serial);
2299 } else if (g_str_equal(key, "Type")) {
2302 dbus_message_iter_get_basic(&value, &type);
2304 DBG("%s Type %s", modem->path, type);
2305 if (g_strcmp0(type, "hardware") != 0) {
2306 DBG("%s Ignore this modem", modem->path);
2307 modem->ignore = true;
2311 dbus_message_iter_next(prop);
2317 if (!modem->powered) {
2318 modem_set_powered(modem, TRUE);
2322 modem_update_interfaces(modem, 0, modem->interfaces);
2325 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2327 struct modem_data *modem = value;
2329 DBG("%s", modem->path);
2334 modem_set_powered(modem, FALSE);
2337 static void remove_modem(gpointer data)
2339 struct modem_data *modem = data;
2341 DBG("%s", modem->path);
2343 if (modem->call_set_property)
2344 dbus_pending_call_cancel(modem->call_set_property);
2346 if (modem->call_get_properties)
2347 dbus_pending_call_cancel(modem->call_get_properties);
2349 if (modem->call_get_contexts)
2350 dbus_pending_call_cancel(modem->call_get_contexts);
2353 destroy_device(modem);
2356 remove_cm_context(modem, modem->context->path);
2358 g_free(modem->serial);
2359 g_free(modem->name);
2360 g_free(modem->imsi);
2361 g_free(modem->path);
2366 static gboolean modem_added(DBusConnection *conn,
2367 DBusMessage *message, void *user_data)
2369 DBusMessageIter iter, properties;
2374 if (!dbus_message_iter_init(message, &iter))
2377 dbus_message_iter_get_basic(&iter, &path);
2379 dbus_message_iter_next(&iter);
2380 dbus_message_iter_recurse(&iter, &properties);
2382 add_modem(path, &properties);
2387 static gboolean modem_removed(DBusConnection *conn,
2388 DBusMessage *message, void *user_data)
2390 DBusMessageIter iter;
2395 if (!dbus_message_iter_init(message, &iter))
2398 dbus_message_iter_get_basic(&iter, &path);
2400 g_hash_table_remove(modem_hash, path);
2405 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2409 DBusMessageIter array, dict;
2413 reply = dbus_pending_call_steal_reply(call);
2415 dbus_error_init(&error);
2417 if (dbus_set_error_from_message(&error, reply)) {
2418 connman_error("%s", error.message);
2419 dbus_error_free(&error);
2423 if (!dbus_message_iter_init(reply, &array))
2426 dbus_message_iter_recurse(&array, &dict);
2428 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2429 DBusMessageIter value, properties;
2432 dbus_message_iter_recurse(&dict, &value);
2433 dbus_message_iter_get_basic(&value, &path);
2435 dbus_message_iter_next(&value);
2436 dbus_message_iter_recurse(&value, &properties);
2438 add_modem(path, &properties);
2440 dbus_message_iter_next(&dict);
2444 dbus_message_unref(reply);
2446 dbus_pending_call_unref(call);
2449 static int manager_get_modems(void)
2451 DBusMessage *message;
2452 DBusPendingCall *call;
2456 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2457 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2461 if (!dbus_connection_send_with_reply(connection, message,
2463 connman_error("Failed to call GetModems()");
2464 dbus_message_unref(message);
2469 connman_error("D-Bus connection not available");
2470 dbus_message_unref(message);
2474 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2477 dbus_message_unref(message);
2479 return -EINPROGRESS;
2482 static void ofono_connect(DBusConnection *conn, void *user_data)
2486 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2487 g_free, remove_modem);
2491 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2493 if (!context_hash) {
2494 g_hash_table_destroy(modem_hash);
2498 manager_get_modems();
2501 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2505 if (!modem_hash || !context_hash)
2508 g_hash_table_destroy(modem_hash);
2511 g_hash_table_destroy(context_hash);
2512 context_hash = NULL;
2515 static int network_probe(struct connman_network *network)
2517 struct modem_data *modem = connman_network_get_data(network);
2519 DBG("%s network %p", modem->path, network);
2524 static void network_remove(struct connman_network *network)
2526 struct modem_data *modem = connman_network_get_data(network);
2528 DBG("%s network %p", modem->path, network);
2531 static int network_connect(struct connman_network *network)
2533 struct modem_data *modem = connman_network_get_data(network);
2535 DBG("%s network %p", modem->path, network);
2537 if (has_interface(modem->interfaces, OFONO_API_CM))
2538 return context_set_active(modem, TRUE);
2539 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM))
2540 return cdma_cm_set_powered(modem, TRUE);
2542 connman_error("Connection manager interface not available");
2547 static int network_disconnect(struct connman_network *network)
2549 struct modem_data *modem = connman_network_get_data(network);
2551 DBG("%s network %p", modem->path, network);
2553 if (has_interface(modem->interfaces, OFONO_API_CM))
2554 return context_set_active(modem, FALSE);
2555 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM))
2556 return cdma_cm_set_powered(modem, FALSE);
2558 connman_error("Connection manager interface not available");
2563 static struct connman_network_driver network_driver = {
2565 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2566 .probe = network_probe,
2567 .remove = network_remove,
2568 .connect = network_connect,
2569 .disconnect = network_disconnect,
2572 static int modem_probe(struct connman_device *device)
2574 struct modem_data *modem = connman_device_get_data(device);
2576 DBG("%s device %p", modem->path, device);
2581 static void modem_remove(struct connman_device *device)
2583 struct modem_data *modem = connman_device_get_data(device);
2585 DBG("%s device %p", modem->path, device);
2588 static int modem_enable(struct connman_device *device)
2590 struct modem_data *modem = connman_device_get_data(device);
2592 DBG("%s device %p", modem->path, device);
2597 return modem_set_online(modem, TRUE);
2600 static int modem_disable(struct connman_device *device)
2602 struct modem_data *modem = connman_device_get_data(device);
2604 DBG("%s device %p", modem->path, device);
2609 return modem_set_online(modem, FALSE);
2612 static struct connman_device_driver modem_driver = {
2614 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2615 .probe = modem_probe,
2616 .remove = modem_remove,
2617 .enable = modem_enable,
2618 .disable = modem_disable,
2621 static int tech_probe(struct connman_technology *technology)
2626 static void tech_remove(struct connman_technology *technology)
2630 static struct connman_technology_driver tech_driver = {
2632 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2633 .probe = tech_probe,
2634 .remove = tech_remove,
2638 static guint modem_added_watch;
2639 static guint modem_removed_watch;
2640 static guint modem_watch;
2641 static guint cm_watch;
2642 static guint sim_watch;
2643 static guint context_added_watch;
2644 static guint context_removed_watch;
2645 static guint netreg_watch;
2646 static guint context_watch;
2647 static guint cdma_cm_watch;
2648 static guint cdma_netreg_watch;
2650 static int ofono_init(void)
2656 connection = connman_dbus_get_connection();
2660 watch = g_dbus_add_service_watch(connection,
2661 OFONO_SERVICE, ofono_connect,
2662 ofono_disconnect, NULL, NULL);
2664 modem_added_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2665 NULL, OFONO_MANAGER_INTERFACE,
2670 modem_removed_watch = g_dbus_add_signal_watch(connection,
2671 OFONO_SERVICE, NULL,
2672 OFONO_MANAGER_INTERFACE,
2677 modem_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2678 OFONO_MODEM_INTERFACE,
2683 cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2689 sim_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2690 OFONO_SIM_INTERFACE,
2695 context_added_watch = g_dbus_add_signal_watch(connection,
2696 OFONO_SERVICE, NULL,
2702 context_removed_watch = g_dbus_add_signal_watch(connection,
2703 OFONO_SERVICE, NULL,
2709 context_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2710 NULL, OFONO_CONTEXT_INTERFACE,
2715 netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2716 OFONO_NETREG_INTERFACE,
2721 cdma_cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2722 NULL, OFONO_CDMA_CM_INTERFACE,
2727 cdma_netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2728 NULL, OFONO_CDMA_NETREG_INTERFACE,
2730 cdma_netreg_changed,
2734 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2735 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2736 context_added_watch == 0 ||
2737 context_removed_watch == 0 ||
2738 context_watch == 0 || netreg_watch == 0 ||
2739 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2744 err = connman_network_driver_register(&network_driver);
2748 err = connman_device_driver_register(&modem_driver);
2750 connman_network_driver_unregister(&network_driver);
2754 err = connman_technology_driver_register(&tech_driver);
2756 connman_device_driver_unregister(&modem_driver);
2757 connman_network_driver_unregister(&network_driver);
2764 g_dbus_remove_watch(connection, cdma_netreg_watch);
2765 g_dbus_remove_watch(connection, cdma_cm_watch);
2766 g_dbus_remove_watch(connection, netreg_watch);
2767 g_dbus_remove_watch(connection, context_watch);
2768 g_dbus_remove_watch(connection, context_removed_watch);
2769 g_dbus_remove_watch(connection, context_added_watch);
2770 g_dbus_remove_watch(connection, sim_watch);
2771 g_dbus_remove_watch(connection, cm_watch);
2772 g_dbus_remove_watch(connection, modem_watch);
2773 g_dbus_remove_watch(connection, modem_removed_watch);
2774 g_dbus_remove_watch(connection, modem_added_watch);
2775 g_dbus_remove_watch(connection, watch);
2776 dbus_connection_unref(connection);
2781 static void ofono_exit(void)
2787 * We should propably wait for the SetProperty() reply
2788 * message, because ...
2790 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2793 * ... here we will cancel the call.
2795 g_hash_table_destroy(modem_hash);
2800 g_hash_table_destroy(context_hash);
2801 context_hash = NULL;
2804 connman_technology_driver_unregister(&tech_driver);
2805 connman_device_driver_unregister(&modem_driver);
2806 connman_network_driver_unregister(&network_driver);
2808 g_dbus_remove_watch(connection, cdma_netreg_watch);
2809 g_dbus_remove_watch(connection, cdma_cm_watch);
2810 g_dbus_remove_watch(connection, netreg_watch);
2811 g_dbus_remove_watch(connection, context_watch);
2812 g_dbus_remove_watch(connection, context_removed_watch);
2813 g_dbus_remove_watch(connection, context_added_watch);
2814 g_dbus_remove_watch(connection, sim_watch);
2815 g_dbus_remove_watch(connection, cm_watch);
2816 g_dbus_remove_watch(connection, modem_watch);
2817 g_dbus_remove_watch(connection, modem_added_watch);
2818 g_dbus_remove_watch(connection, modem_removed_watch);
2819 g_dbus_remove_watch(connection, watch);
2821 dbus_connection_unref(connection);
2824 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2825 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)