5 * Copyright (C) 2007-2012 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 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 */
156 connman_bool_t powered;
157 connman_bool_t online;
159 connman_bool_t ignore;
161 connman_bool_t set_powered;
163 /* CDMA ConnectionManager Interface */
164 connman_bool_t cdma_cm_powered;
166 /* ConnectionManager Interface */
167 connman_bool_t attached;
168 connman_bool_t cm_powered;
170 /* ConnectionContext Interface */
171 connman_bool_t active;
172 connman_bool_t set_active;
173 connman_bool_t valid_apn; /* APN is 'valid' if length > 0 */
175 /* SimManager Interface */
178 /* Netreg Interface */
181 uint8_t data_strength; /* 1xEVDO signal strength */
182 connman_bool_t registered;
183 connman_bool_t roaming;
186 DBusPendingCall *call_set_property;
187 DBusPendingCall *call_get_properties;
188 DBusPendingCall *call_get_contexts;
191 static const char *api2string(enum ofono_api api)
196 case OFONO_API_NETREG:
200 case OFONO_API_CDMA_NETREG:
201 return "cdma-netreg";
202 case OFONO_API_CDMA_CM:
209 static char *get_ident(const char *path)
216 pos = strrchr(path, '/');
223 static struct network_context *network_context_alloc(const char *path)
225 struct network_context *context;
227 context = g_try_new0(struct network_context, 1);
231 context->path = g_strdup(path);
234 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
235 context->ipv4_address = NULL;
236 context->ipv4_nameservers = NULL;
238 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
239 context->ipv6_address = NULL;
240 context->ipv6_nameservers = NULL;
245 static void network_context_free(struct network_context *context)
247 g_free(context->path);
249 connman_ipaddress_free(context->ipv4_address);
250 g_free(context->ipv4_nameservers);
252 connman_ipaddress_free(context->ipv6_address);
253 g_free(context->ipv6_nameservers);
258 static void set_connected(struct modem_data *modem)
260 struct connman_service *service;
261 connman_bool_t setip = FALSE;
262 enum connman_ipconfig_method method;
266 DBG("%s", modem->path);
268 index = modem->context->index;
270 if (index < 0 || modem->context->ipv4_address == NULL) {
271 connman_error("Invalid index and/or address");
275 service = connman_service_lookup_from_network(modem->network);
279 method = modem->context->ipv4_method;
280 if (method == CONNMAN_IPCONFIG_METHOD_FIXED ||
281 method == CONNMAN_IPCONFIG_METHOD_DHCP)
283 connman_service_create_ip4config(service, index);
284 connman_network_set_index(modem->network, index);
286 connman_network_set_ipv4_method(modem->network, method);
291 if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
292 connman_network_set_ipaddress(modem->network,
293 modem->context->ipv4_address);
296 method = modem->context->ipv6_method;
297 if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
298 connman_service_create_ip6config(service, index);
299 connman_network_set_ipv6_method(modem->network, method);
300 connman_network_set_ipaddress(modem->network,
301 modem->context->ipv6_address);
305 /* Set the nameservers */
306 if (modem->context->ipv4_nameservers != NULL &&
307 modem->context->ipv6_nameservers != NULL) {
308 nameservers = g_strdup_printf("%s %s",
309 modem->context->ipv4_nameservers,
310 modem->context->ipv6_nameservers);
311 connman_network_set_nameservers(modem->network, nameservers);
313 } else if (modem->context->ipv4_nameservers != NULL) {
314 connman_network_set_nameservers(modem->network,
315 modem->context->ipv4_nameservers);
316 } else if (modem->context->ipv6_nameservers != NULL) {
317 connman_network_set_nameservers(modem->network,
318 modem->context->ipv6_nameservers);
322 connman_network_set_connected(modem->network, TRUE);
325 static void set_disconnected(struct modem_data *modem)
327 DBG("%s", modem->path);
329 if (modem->network == NULL)
332 connman_network_set_connected(modem->network, FALSE);
335 typedef void (*set_property_cb)(struct modem_data *data,
336 connman_bool_t success);
337 typedef void (*get_properties_cb)(struct modem_data *data,
338 DBusMessageIter *dict);
340 struct property_info {
341 struct modem_data *modem;
343 const char *interface;
344 const char *property;
345 set_property_cb set_property_cb;
346 get_properties_cb get_properties_cb;
349 static void set_property_reply(DBusPendingCall *call, void *user_data)
351 struct property_info *info = user_data;
354 connman_bool_t success = TRUE;
356 DBG("%s path %s %s.%s", info->modem->path,
357 info->path, info->interface, info->property);
359 info->modem->call_set_property = NULL;
361 dbus_error_init(&error);
363 reply = dbus_pending_call_steal_reply(call);
365 if (dbus_set_error_from_message(&error, reply)) {
366 connman_error("Failed to change property: %s %s.%s: %s %s",
367 info->path, info->interface, info->property,
368 error.name, error.message);
369 dbus_error_free(&error);
373 if (info->set_property_cb != NULL)
374 (*info->set_property_cb)(info->modem, success);
376 dbus_message_unref(reply);
378 dbus_pending_call_unref(call);
381 static int set_property(struct modem_data *modem,
382 const char *path, const char *interface,
383 const char *property, int type, void *value,
384 set_property_cb notify)
386 DBusMessage *message;
387 DBusMessageIter iter;
388 struct property_info *info;
390 DBG("%s path %s %s.%s", modem->path, path, interface, property);
392 if (modem->call_set_property != NULL) {
393 DBG("Cancel pending SetProperty");
395 dbus_pending_call_cancel(modem->call_set_property);
396 modem->call_set_property = NULL;
399 message = dbus_message_new_method_call(OFONO_SERVICE, path,
400 interface, SET_PROPERTY);
404 dbus_message_iter_init_append(message, &iter);
405 connman_dbus_property_append_basic(&iter, property, type, value);
407 if (dbus_connection_send_with_reply(connection, message,
408 &modem->call_set_property, TIMEOUT) == FALSE) {
409 connman_error("Failed to change property: %s %s.%s",
410 path, interface, property);
411 dbus_message_unref(message);
415 if (modem->call_set_property == NULL) {
416 connman_error("D-Bus connection not available");
417 dbus_message_unref(message);
421 info = g_try_new0(struct property_info, 1);
423 dbus_message_unref(message);
429 info->interface = interface;
430 info->property = property;
431 info->set_property_cb = notify;
433 dbus_pending_call_set_notify(modem->call_set_property,
434 set_property_reply, info, g_free);
436 dbus_message_unref(message);
441 static void get_properties_reply(DBusPendingCall *call, void *user_data)
443 struct property_info *info = user_data;
444 DBusMessageIter array, dict;
448 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
450 info->modem->call_get_properties = NULL;
452 dbus_error_init(&error);
454 reply = dbus_pending_call_steal_reply(call);
456 if (dbus_set_error_from_message(&error, reply)) {
457 connman_error("Failed to get properties: %s %s: %s %s",
458 info->path, info->interface,
459 error.name, error.message);
460 dbus_error_free(&error);
465 if (dbus_message_iter_init(reply, &array) == FALSE)
468 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
471 dbus_message_iter_recurse(&array, &dict);
473 if (info->get_properties_cb != NULL)
474 (*info->get_properties_cb)(info->modem, &dict);
478 dbus_message_unref(reply);
480 dbus_pending_call_unref(call);
483 static int get_properties(const char *path, const char *interface,
484 get_properties_cb notify,
485 struct modem_data *modem)
487 DBusMessage *message;
488 struct property_info *info;
490 DBG("%s path %s %s", modem->path, path, interface);
492 if (modem->call_get_properties != NULL) {
493 connman_error("Pending GetProperties");
497 message = dbus_message_new_method_call(OFONO_SERVICE, path,
498 interface, GET_PROPERTIES);
502 if (dbus_connection_send_with_reply(connection, message,
503 &modem->call_get_properties, TIMEOUT) == FALSE) {
504 connman_error("Failed to call %s.GetProperties()", interface);
505 dbus_message_unref(message);
509 if (modem->call_get_properties == NULL) {
510 connman_error("D-Bus connection not available");
511 dbus_message_unref(message);
515 info = g_try_new0(struct property_info, 1);
517 dbus_message_unref(message);
523 info->interface = interface;
524 info->get_properties_cb = notify;
526 dbus_pending_call_set_notify(modem->call_get_properties,
527 get_properties_reply, info, g_free);
529 dbus_message_unref(message);
534 static void context_set_active_reply(struct modem_data *modem,
535 connman_bool_t success)
537 DBG("%s", modem->path);
539 if (success == TRUE) {
541 * Don't handle do anything on success here. oFono will send
542 * the change via PropertyChanged singal.
548 * Active = True might fail due a timeout. That means oFono
549 * still tries to go online. If we retry to set Active = True,
550 * we just get a InProgress error message. Should we power
551 * cycle the modem in such cases?
554 if (modem->network == NULL) {
556 * In the case where we power down the device
557 * we don't wait for the reply, therefore the network
558 * might already be gone.
563 connman_network_set_error(modem->network,
564 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
567 static int context_set_active(struct modem_data *modem,
568 connman_bool_t active)
572 DBG("%s active %d", modem->path, active);
574 err = set_property(modem, modem->context->path,
575 OFONO_CONTEXT_INTERFACE,
576 "Active", DBUS_TYPE_BOOLEAN,
578 context_set_active_reply);
580 if (active == FALSE && err == -EINPROGRESS)
586 static void cdma_cm_set_powered_reply(struct modem_data *modem,
587 connman_bool_t success)
589 DBG("%s", modem->path);
591 if (success == TRUE) {
593 * Don't handle do anything on success here. oFono will send
594 * the change via PropertyChanged singal.
600 * Powered = True might fail due a timeout. That means oFono
601 * still tries to go online. If we retry to set Powered = True,
602 * we just get a InProgress error message. Should we power
603 * cycle the modem in such cases?
606 if (modem->network == NULL) {
608 * In the case where we power down the device
609 * we don't wait for the reply, therefore the network
610 * might already be gone.
615 connman_network_set_error(modem->network,
616 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
619 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
623 DBG("%s powered %d", modem->path, powered);
625 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
626 "Powered", DBUS_TYPE_BOOLEAN,
628 cdma_cm_set_powered_reply);
630 if (powered == FALSE && err == -EINPROGRESS)
636 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
638 DBG("%s online %d", modem->path, online);
640 return set_property(modem, modem->path,
641 OFONO_MODEM_INTERFACE,
642 "Online", DBUS_TYPE_BOOLEAN,
647 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
651 DBG("%s powered %d", modem->path, powered);
653 err = set_property(modem, modem->path,
655 "Powered", DBUS_TYPE_BOOLEAN,
659 if (powered == FALSE && err == -EINPROGRESS)
665 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
669 DBG("%s powered %d", modem->path, powered);
671 modem->set_powered = powered;
673 err = set_property(modem, modem->path,
674 OFONO_MODEM_INTERFACE,
675 "Powered", DBUS_TYPE_BOOLEAN,
679 if (powered == FALSE && err == -EINPROGRESS)
685 static connman_bool_t has_interface(uint8_t interfaces,
688 if ((interfaces & api) == api)
694 static uint8_t extract_interfaces(DBusMessageIter *array)
696 DBusMessageIter entry;
697 uint8_t interfaces = 0;
699 dbus_message_iter_recurse(array, &entry);
701 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
704 dbus_message_iter_get_basic(&entry, &name);
706 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
707 interfaces |= OFONO_API_SIM;
708 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
709 interfaces |= OFONO_API_NETREG;
710 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
711 interfaces |= OFONO_API_CM;
712 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
713 interfaces |= OFONO_API_CDMA_CM;
714 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
715 interfaces |= OFONO_API_CDMA_NETREG;
717 dbus_message_iter_next(&entry);
723 static char *extract_nameservers(DBusMessageIter *array)
725 DBusMessageIter entry;
726 char *nameservers = NULL;
729 dbus_message_iter_recurse(array, &entry);
731 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
732 const char *nameserver;
734 dbus_message_iter_get_basic(&entry, &nameserver);
736 if (nameservers == NULL) {
737 nameservers = g_strdup(nameserver);
740 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
744 dbus_message_iter_next(&entry);
750 static void extract_ipv4_settings(DBusMessageIter *array,
751 struct network_context *context)
753 DBusMessageIter dict;
754 char *address = NULL, *netmask = NULL, *gateway = NULL;
755 char *nameservers = NULL;
756 const char *interface = NULL;
759 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
762 dbus_message_iter_recurse(array, &dict);
764 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
765 DBusMessageIter entry, value;
766 const char *key, *val;
768 dbus_message_iter_recurse(&dict, &entry);
769 dbus_message_iter_get_basic(&entry, &key);
771 dbus_message_iter_next(&entry);
772 dbus_message_iter_recurse(&entry, &value);
774 if (g_str_equal(key, "Interface") == TRUE) {
775 dbus_message_iter_get_basic(&value, &interface);
777 DBG("Interface %s", interface);
779 index = connman_inet_ifindex(interface);
781 DBG("index %d", index);
782 } else if (g_str_equal(key, "Method") == TRUE) {
783 dbus_message_iter_get_basic(&value, &val);
785 DBG("Method %s", val);
787 if (g_strcmp0(val, "static") == 0) {
788 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
789 } else if (g_strcmp0(val, "dhcp") == 0) {
790 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
793 } else if (g_str_equal(key, "Address") == TRUE) {
794 dbus_message_iter_get_basic(&value, &val);
796 address = g_strdup(val);
798 DBG("Address %s", address);
799 } else if (g_str_equal(key, "Netmask") == TRUE) {
800 dbus_message_iter_get_basic(&value, &val);
802 netmask = g_strdup(val);
804 DBG("Netmask %s", netmask);
805 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
806 nameservers = extract_nameservers(&value);
808 DBG("Nameservers %s", nameservers);
809 } else if (g_str_equal(key, "Gateway") == TRUE) {
810 dbus_message_iter_get_basic(&value, &val);
812 gateway = g_strdup(val);
814 DBG("Gateway %s", gateway);
817 dbus_message_iter_next(&dict);
823 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
826 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
827 if (context->ipv4_address == NULL)
830 context->index = index;
831 connman_ipaddress_set_ipv4(context->ipv4_address, address,
834 context->ipv4_nameservers = nameservers;
837 if (context->ipv4_nameservers != nameservers)
845 static void extract_ipv6_settings(DBusMessageIter *array,
846 struct network_context *context)
848 DBusMessageIter dict;
849 char *address = NULL, *gateway = NULL;
850 unsigned char prefix_length = 0;
851 char *nameservers = NULL;
852 const char *interface = NULL;
855 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
858 dbus_message_iter_recurse(array, &dict);
860 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
861 DBusMessageIter entry, value;
862 const char *key, *val;
864 dbus_message_iter_recurse(&dict, &entry);
865 dbus_message_iter_get_basic(&entry, &key);
867 dbus_message_iter_next(&entry);
868 dbus_message_iter_recurse(&entry, &value);
870 if (g_str_equal(key, "Interface") == TRUE) {
871 dbus_message_iter_get_basic(&value, &interface);
873 DBG("Interface %s", interface);
875 index = connman_inet_ifindex(interface);
877 DBG("index %d", index);
878 } else if (g_str_equal(key, "Address") == TRUE) {
879 dbus_message_iter_get_basic(&value, &val);
881 address = g_strdup(val);
883 DBG("Address %s", address);
884 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
885 dbus_message_iter_get_basic(&value, &prefix_length);
887 DBG("prefix length %d", prefix_length);
888 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
889 nameservers = extract_nameservers(&value);
891 DBG("Nameservers %s", nameservers);
892 } else if (g_str_equal(key, "Gateway") == TRUE) {
893 dbus_message_iter_get_basic(&value, &val);
895 gateway = g_strdup(val);
897 DBG("Gateway %s", gateway);
900 dbus_message_iter_next(&dict);
906 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
908 context->ipv6_address =
909 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
910 if (context->ipv6_address == NULL)
913 context->index = index;
914 connman_ipaddress_set_ipv6(context->ipv6_address, address,
915 prefix_length, gateway);
917 context->ipv6_nameservers = nameservers;
920 if (context->ipv6_nameservers != nameservers)
927 static connman_bool_t ready_to_create_device(struct modem_data *modem)
930 * There are three different modem types which behave slightly
932 * - GSM modems will expose the SIM interface then the
934 * - CDMA modems will expose CM first and sometime later
935 * a unique serial number.
937 * This functions tests if we have the necessary information gathered
938 * before we are able to create a device.
941 if (modem->device != NULL)
944 if (modem->imsi != NULL || modem->serial != NULL)
950 static void create_device(struct modem_data *modem)
952 struct connman_device *device;
955 DBG("%s", modem->path);
957 if (modem->imsi != NULL)
959 else if (modem->serial != NULL)
960 ident = modem->serial;
962 if (connman_dbus_validate_ident(ident) == FALSE)
963 ident = connman_dbus_encode_string(ident);
965 ident = g_strdup(ident);
967 device = connman_device_create("ofono", CONNMAN_DEVICE_TYPE_CELLULAR);
971 DBG("device %p", device);
973 connman_device_set_ident(device, ident);
975 connman_device_set_string(device, "Path", modem->path);
977 connman_device_set_data(device, modem);
979 if (connman_device_register(device) < 0) {
980 connman_error("Failed to register cellular device");
981 connman_device_unref(device);
985 modem->device = device;
987 connman_device_set_powered(modem->device, modem->online);
992 static void destroy_device(struct modem_data *modem)
994 DBG("%s", modem->path);
996 connman_device_set_powered(modem->device, FALSE);
998 if (modem->network != NULL) {
999 connman_device_remove_network(modem->device, modem->network);
1000 connman_network_unref(modem->network);
1001 modem->network = NULL;
1004 connman_device_unregister(modem->device);
1005 connman_device_unref(modem->device);
1007 modem->device = NULL;
1010 static void add_network(struct modem_data *modem)
1014 DBG("%s", modem->path);
1016 if (modem->network != NULL)
1019 modem->network = connman_network_create(modem->context->path,
1020 CONNMAN_NETWORK_TYPE_CELLULAR);
1021 if (modem->network == NULL)
1024 DBG("network %p", modem->network);
1026 connman_network_set_data(modem->network, modem);
1028 connman_network_set_string(modem->network, "Path",
1029 modem->context->path);
1031 if (modem->name != NULL)
1032 connman_network_set_name(modem->network, modem->name);
1034 connman_network_set_name(modem->network, "");
1036 connman_network_set_strength(modem->network, modem->strength);
1038 group = get_ident(modem->context->path);
1039 connman_network_set_group(modem->network, group);
1041 connman_network_set_bool(modem->network, "Roaming",
1044 if (connman_device_add_network(modem->device, modem->network) < 0) {
1045 connman_network_unref(modem->network);
1046 modem->network = NULL;
1051 static void remove_network(struct modem_data *modem)
1053 DBG("%s", modem->path);
1055 if (modem->network == NULL)
1058 DBG("network %p", modem->network);
1060 connman_device_remove_network(modem->device, modem->network);
1061 connman_network_unref(modem->network);
1062 modem->network = NULL;
1065 static int add_cm_context(struct modem_data *modem, const char *context_path,
1066 DBusMessageIter *dict)
1068 const char *context_type = NULL;
1069 struct network_context *context = NULL;
1070 connman_bool_t active = FALSE;
1072 DBG("%s context path %s", modem->path, context_path);
1074 if (modem->context != NULL) {
1076 * We have already assigned a context to this modem
1077 * and we do only support one Internet context.
1082 context = network_context_alloc(context_path);
1083 if (context == NULL)
1086 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1087 DBusMessageIter entry, value;
1090 dbus_message_iter_recurse(dict, &entry);
1091 dbus_message_iter_get_basic(&entry, &key);
1093 dbus_message_iter_next(&entry);
1094 dbus_message_iter_recurse(&entry, &value);
1096 if (g_str_equal(key, "Type") == TRUE) {
1097 dbus_message_iter_get_basic(&value, &context_type);
1099 DBG("%s context %s type %s", modem->path,
1100 context_path, context_type);
1101 } else if (g_str_equal(key, "Settings") == TRUE) {
1102 DBG("%s Settings", modem->path);
1104 extract_ipv4_settings(&value, context);
1105 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1106 DBG("%s IPv6.Settings", modem->path);
1108 extract_ipv6_settings(&value, context);
1109 } else if (g_str_equal(key, "Active") == TRUE) {
1110 dbus_message_iter_get_basic(&value, &active);
1112 DBG("%s Active %d", modem->path, active);
1113 } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1116 dbus_message_iter_get_basic(&value, &apn);
1117 if (apn != NULL && strlen(apn) > 0)
1118 modem->valid_apn = TRUE;
1120 modem->valid_apn = FALSE;
1122 DBG("%s AccessPointName '%s'", modem->path, apn);
1124 dbus_message_iter_next(dict);
1127 if (g_strcmp0(context_type, "internet") != 0) {
1128 network_context_free(context);
1132 modem->context = context;
1133 modem->active = active;
1135 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1137 if (modem->valid_apn == TRUE && modem->attached == TRUE &&
1138 has_interface(modem->interfaces,
1139 OFONO_API_NETREG) == TRUE) {
1146 static void remove_cm_context(struct modem_data *modem,
1147 const char *context_path)
1149 if (modem->context == NULL)
1152 if (modem->network != NULL)
1153 remove_network(modem);
1155 g_hash_table_remove(context_hash, context_path);
1157 network_context_free(modem->context);
1158 modem->context = NULL;
1160 modem->valid_apn = FALSE;
1162 if (modem->network != NULL)
1163 remove_network(modem);
1166 static gboolean context_changed(DBusConnection *conn,
1167 DBusMessage *message,
1170 const char *context_path = dbus_message_get_path(message);
1171 struct modem_data *modem = NULL;
1172 DBusMessageIter iter, value;
1175 DBG("context_path %s", context_path);
1177 modem = g_hash_table_lookup(context_hash, context_path);
1181 if (dbus_message_iter_init(message, &iter) == FALSE)
1184 dbus_message_iter_get_basic(&iter, &key);
1186 dbus_message_iter_next(&iter);
1187 dbus_message_iter_recurse(&iter, &value);
1190 * oFono guarantees the ordering of Settings and
1191 * Active. Settings will always be send before Active = True.
1192 * That means we don't have to order here.
1194 if (g_str_equal(key, "Settings") == TRUE) {
1195 DBG("%s Settings", modem->path);
1197 extract_ipv4_settings(&value, modem->context);
1198 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1199 DBG("%s IPv6.Settings", modem->path);
1201 extract_ipv6_settings(&value, modem->context);
1202 } else if (g_str_equal(key, "Active") == TRUE) {
1203 dbus_message_iter_get_basic(&value, &modem->active);
1205 DBG("%s Active %d", modem->path, modem->active);
1207 if (modem->active == TRUE)
1208 set_connected(modem);
1210 set_disconnected(modem);
1211 } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1214 dbus_message_iter_get_basic(&value, &apn);
1216 DBG("%s AccessPointName %s", modem->path, apn);
1218 if (apn != NULL && strlen(apn) > 0) {
1219 modem->valid_apn = TRUE;
1221 if (modem->network != NULL)
1224 if (modem->attached == FALSE)
1227 if (has_interface(modem->interfaces,
1228 OFONO_API_NETREG) == FALSE) {
1234 if (modem->active == TRUE)
1235 set_connected(modem);
1237 modem->valid_apn = FALSE;
1239 if (modem->network == NULL)
1242 remove_network(modem);
1249 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1251 struct modem_data *modem = user_data;
1252 DBusMessageIter array, dict, entry, value;
1256 DBG("%s", modem->path);
1258 modem->call_get_contexts = NULL;
1260 reply = dbus_pending_call_steal_reply(call);
1262 dbus_error_init(&error);
1264 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1265 connman_error("%s", error.message);
1266 dbus_error_free(&error);
1270 if (dbus_message_iter_init(reply, &array) == FALSE)
1273 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1276 dbus_message_iter_recurse(&array, &dict);
1278 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1279 const char *context_path;
1281 dbus_message_iter_recurse(&dict, &entry);
1282 dbus_message_iter_get_basic(&entry, &context_path);
1284 dbus_message_iter_next(&entry);
1285 dbus_message_iter_recurse(&entry, &value);
1287 if (add_cm_context(modem, context_path, &value) == 0)
1290 dbus_message_iter_next(&dict);
1294 dbus_message_unref(reply);
1296 dbus_pending_call_unref(call);
1299 static int cm_get_contexts(struct modem_data *modem)
1301 DBusMessage *message;
1303 DBG("%s", modem->path);
1305 if (modem->call_get_contexts != NULL)
1308 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1309 OFONO_CM_INTERFACE, GET_CONTEXTS);
1310 if (message == NULL)
1313 if (dbus_connection_send_with_reply(connection, message,
1314 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1315 connman_error("Failed to call GetContexts()");
1316 dbus_message_unref(message);
1320 if (modem->call_get_contexts == NULL) {
1321 connman_error("D-Bus connection not available");
1322 dbus_message_unref(message);
1326 dbus_pending_call_set_notify(modem->call_get_contexts,
1327 cm_get_contexts_reply,
1330 dbus_message_unref(message);
1332 return -EINPROGRESS;
1335 static gboolean cm_context_added(DBusConnection *conn,
1336 DBusMessage *message,
1339 const char *path = dbus_message_get_path(message);
1341 struct modem_data *modem;
1342 DBusMessageIter iter, properties;
1346 modem = g_hash_table_lookup(modem_hash, path);
1350 if (dbus_message_iter_init(message, &iter) == FALSE)
1353 dbus_message_iter_get_basic(&iter, &context_path);
1355 dbus_message_iter_next(&iter);
1356 dbus_message_iter_recurse(&iter, &properties);
1358 if (add_cm_context(modem, context_path, &properties) != 0)
1364 static gboolean cm_context_removed(DBusConnection *conn,
1365 DBusMessage *message,
1368 const char *path = dbus_message_get_path(message);
1369 const char *context_path;
1370 struct modem_data *modem;
1371 DBusMessageIter iter;
1373 DBG("context path %s", path);
1375 if (dbus_message_iter_init(message, &iter) == FALSE)
1378 dbus_message_iter_get_basic(&iter, &context_path);
1380 modem = g_hash_table_lookup(context_hash, context_path);
1384 remove_cm_context(modem, context_path);
1389 static void netreg_update_name(struct modem_data *modem,
1390 DBusMessageIter* value)
1394 dbus_message_iter_get_basic(value, &name);
1396 DBG("%s Name %s", modem->path, name);
1398 g_free(modem->name);
1399 modem->name = g_strdup(name);
1401 if (modem->network == NULL)
1404 connman_network_set_name(modem->network, modem->name);
1405 connman_network_update(modem->network);
1408 static void netreg_update_strength(struct modem_data *modem,
1409 DBusMessageIter *value)
1411 dbus_message_iter_get_basic(value, &modem->strength);
1413 DBG("%s Strength %d", modem->path, modem->strength);
1415 if (modem->network == NULL)
1420 * We don't have 2 signal notifications we always report the strength
1421 * signal. data_strength is always equal to 0.
1424 * In the case we have a data_strength signal (from 1xEVDO network)
1425 * we don't need to update the value with strength signal (from 1xCDMA)
1426 * because the modem is registered to 1xEVDO network for data call.
1427 * In case we have no data_strength signal (not registered to 1xEVDO
1428 * network), we must report the strength signal (registered to 1xCDMA
1429 * network e.g slow mode).
1431 if (modem->data_strength != 0)
1434 connman_network_set_strength(modem->network, modem->strength);
1435 connman_network_update(modem->network);
1438 /* Retrieve 1xEVDO Data Strength signal */
1439 static void netreg_update_datastrength(struct modem_data *modem,
1440 DBusMessageIter *value)
1442 dbus_message_iter_get_basic(value, &modem->data_strength);
1444 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1446 if (modem->network == NULL)
1450 * CDMA modem is not registered to 1xEVDO network, let
1451 * update_signal_strength() reporting the value on the Strength signal
1454 if (modem->data_strength == 0)
1457 connman_network_set_strength(modem->network, modem->data_strength);
1458 connman_network_update(modem->network);
1461 static void netreg_update_status(struct modem_data *modem,
1462 DBusMessageIter *value)
1465 connman_bool_t roaming;
1467 dbus_message_iter_get_basic(value, &status);
1469 roaming = g_str_equal(status, "roaming");
1470 modem->registered = roaming || g_str_equal(status, "registered");
1472 if (roaming == modem->roaming)
1475 modem->roaming = roaming;
1477 if (modem->network == NULL)
1480 connman_network_set_bool(modem->network,
1481 "Roaming", modem->roaming);
1482 connman_network_update(modem->network);
1485 static void netreg_update_regdom(struct modem_data *modem,
1486 DBusMessageIter *value)
1488 char *mobile_country_code;
1492 dbus_message_iter_get_basic(value, &mobile_country_code);
1494 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1497 mcc = atoi(mobile_country_code);
1498 if (mcc > 799 || mcc < 200)
1501 alpha2 = mcc_country_codes[mcc - 200];
1503 connman_technology_set_regdom(alpha2);
1506 static gboolean netreg_changed(DBusConnection *conn, DBusMessage *message,
1509 const char *path = dbus_message_get_path(message);
1510 struct modem_data *modem;
1511 DBusMessageIter iter, value;
1514 modem = g_hash_table_lookup(modem_hash, path);
1518 if (modem->ignore == TRUE)
1521 if (dbus_message_iter_init(message, &iter) == FALSE)
1524 dbus_message_iter_get_basic(&iter, &key);
1526 dbus_message_iter_next(&iter);
1527 dbus_message_iter_recurse(&iter, &value);
1529 if (g_str_equal(key, "Name") == TRUE)
1530 netreg_update_name(modem, &value);
1531 else if (g_str_equal(key, "Strength") == TRUE)
1532 netreg_update_strength(modem, &value);
1533 else if (g_str_equal(key, "Status") == TRUE)
1534 netreg_update_status(modem, &value);
1535 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1536 netreg_update_regdom(modem, &value);
1541 static void netreg_properties_reply(struct modem_data *modem,
1542 DBusMessageIter *dict)
1544 DBG("%s", modem->path);
1546 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1547 DBusMessageIter entry, value;
1550 dbus_message_iter_recurse(dict, &entry);
1551 dbus_message_iter_get_basic(&entry, &key);
1553 dbus_message_iter_next(&entry);
1554 dbus_message_iter_recurse(&entry, &value);
1556 if (g_str_equal(key, "Name") == TRUE)
1557 netreg_update_name(modem, &value);
1558 else if (g_str_equal(key, "Strength") == TRUE)
1559 netreg_update_strength(modem, &value);
1560 else if (g_str_equal(key, "Status") == TRUE)
1561 netreg_update_status(modem, &value);
1562 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1563 netreg_update_regdom(modem, &value);
1565 dbus_message_iter_next(dict);
1568 if (modem->context == NULL) {
1570 * netgreg_get_properties() was issued after we got
1571 * cm_get_contexts_reply() where we create the
1572 * context. Though before we got the
1573 * netreg_properties_reply the context was removed
1574 * again. Therefore we have to skip the network
1580 if (modem->valid_apn == TRUE)
1583 if (modem->active == TRUE)
1584 set_connected(modem);
1587 static int netreg_get_properties(struct modem_data *modem)
1589 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1590 netreg_properties_reply, modem);
1593 static void add_cdma_network(struct modem_data *modem)
1595 /* Be sure that device is created before adding CDMA network */
1596 if (modem->device == NULL)
1600 * CDMA modems don't need contexts for data call, however the current
1601 * add_network() logic needs one, so we create one to proceed.
1603 if (modem->context == NULL)
1604 modem->context = network_context_alloc(modem->path);
1606 if (modem->name == NULL)
1607 modem->name = g_strdup("CDMA Network");
1611 if (modem->cdma_cm_powered == TRUE)
1612 set_connected(modem);
1615 static gboolean cdma_netreg_changed(DBusConnection *conn,
1616 DBusMessage *message,
1619 const char *path = dbus_message_get_path(message);
1620 struct modem_data *modem;
1621 DBusMessageIter iter, value;
1626 modem = g_hash_table_lookup(modem_hash, path);
1630 if (modem->ignore == TRUE)
1633 if (dbus_message_iter_init(message, &iter) == FALSE)
1636 dbus_message_iter_get_basic(&iter, &key);
1638 dbus_message_iter_next(&iter);
1639 dbus_message_iter_recurse(&iter, &value);
1641 if (g_str_equal(key, "Name") == TRUE)
1642 netreg_update_name(modem, &value);
1643 else if (g_str_equal(key, "Strength") == TRUE)
1644 netreg_update_strength(modem, &value);
1645 else if (g_str_equal(key, "DataStrength") == TRUE)
1646 netreg_update_datastrength(modem, &value);
1647 else if (g_str_equal(key, "Status") == TRUE)
1648 netreg_update_status(modem, &value);
1650 if (modem->registered == TRUE)
1651 add_cdma_network(modem);
1653 remove_network(modem);
1658 static void cdma_netreg_properties_reply(struct modem_data *modem,
1659 DBusMessageIter *dict)
1661 DBG("%s", modem->path);
1663 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1664 DBusMessageIter entry, value;
1667 dbus_message_iter_recurse(dict, &entry);
1668 dbus_message_iter_get_basic(&entry, &key);
1670 dbus_message_iter_next(&entry);
1671 dbus_message_iter_recurse(&entry, &value);
1673 if (g_str_equal(key, "Name") == TRUE)
1674 netreg_update_name(modem, &value);
1675 else if (g_str_equal(key, "Strength") == TRUE)
1676 netreg_update_strength(modem, &value);
1677 else if (g_str_equal(key, "DataStrength") == TRUE)
1678 netreg_update_datastrength(modem, &value);
1679 else if (g_str_equal(key, "Status") == TRUE)
1680 netreg_update_status(modem, &value);
1682 dbus_message_iter_next(dict);
1685 if (modem->registered == TRUE)
1686 add_cdma_network(modem);
1688 remove_network(modem);
1691 static int cdma_netreg_get_properties(struct modem_data *modem)
1693 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1694 cdma_netreg_properties_reply, modem);
1697 static void cm_update_attached(struct modem_data *modem,
1698 DBusMessageIter *value)
1700 dbus_message_iter_get_basic(value, &modem->attached);
1702 DBG("%s Attached %d", modem->path, modem->attached);
1704 if (modem->attached == FALSE) {
1705 remove_network(modem);
1709 if (has_interface(modem->interfaces,
1710 OFONO_API_NETREG) == FALSE) {
1714 netreg_get_properties(modem);
1717 static void cm_update_powered(struct modem_data *modem,
1718 DBusMessageIter *value)
1720 dbus_message_iter_get_basic(value, &modem->cm_powered);
1722 DBG("%s ConnnectionManager Powered %d", modem->path,
1725 if (modem->cm_powered == TRUE)
1728 cm_set_powered(modem, TRUE);
1731 static gboolean cm_changed(DBusConnection *conn, DBusMessage *message,
1734 const char *path = dbus_message_get_path(message);
1735 struct modem_data *modem;
1736 DBusMessageIter iter, value;
1739 modem = g_hash_table_lookup(modem_hash, path);
1743 if (modem->ignore == TRUE)
1746 if (dbus_message_iter_init(message, &iter) == FALSE)
1749 dbus_message_iter_get_basic(&iter, &key);
1751 dbus_message_iter_next(&iter);
1752 dbus_message_iter_recurse(&iter, &value);
1754 if (g_str_equal(key, "Attached") == TRUE)
1755 cm_update_attached(modem, &value);
1756 else if (g_str_equal(key, "Powered") == TRUE)
1757 cm_update_powered(modem, &value);
1762 static void cdma_cm_update_powered(struct modem_data *modem,
1763 DBusMessageIter *value)
1765 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1767 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1769 if (modem->network == NULL)
1772 if (modem->cdma_cm_powered == TRUE)
1773 set_connected(modem);
1775 set_disconnected(modem);
1778 static void cdma_cm_update_settings(struct modem_data *modem,
1779 DBusMessageIter *value)
1781 DBG("%s Settings", modem->path);
1783 extract_ipv4_settings(value, modem->context);
1786 static gboolean cdma_cm_changed(DBusConnection *conn,
1787 DBusMessage *message, void *user_data)
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->online == TRUE && modem->network == NULL)
1799 cdma_netreg_get_properties(modem);
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, "Powered") == TRUE)
1810 cdma_cm_update_powered(modem, &value);
1811 if (g_str_equal(key, "Settings") == TRUE)
1812 cdma_cm_update_settings(modem, &value);
1817 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1819 DBG("%s", modem->path);
1821 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1822 DBusMessageIter entry, value;
1825 dbus_message_iter_recurse(dict, &entry);
1826 dbus_message_iter_get_basic(&entry, &key);
1828 dbus_message_iter_next(&entry);
1829 dbus_message_iter_recurse(&entry, &value);
1831 if (g_str_equal(key, "Attached") == TRUE)
1832 cm_update_attached(modem, &value);
1833 else if (g_str_equal(key, "Powered") == TRUE)
1834 cm_update_powered(modem, &value);
1836 dbus_message_iter_next(dict);
1840 static int cm_get_properties(struct modem_data *modem)
1842 return get_properties(modem->path, OFONO_CM_INTERFACE,
1843 cm_properties_reply, modem);
1846 static void cdma_cm_properties_reply(struct modem_data *modem,
1847 DBusMessageIter *dict)
1849 DBG("%s", modem->path);
1851 if (modem->online == TRUE)
1852 cdma_netreg_get_properties(modem);
1854 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1855 DBusMessageIter entry, value;
1858 dbus_message_iter_recurse(dict, &entry);
1859 dbus_message_iter_get_basic(&entry, &key);
1861 dbus_message_iter_next(&entry);
1862 dbus_message_iter_recurse(&entry, &value);
1864 if (g_str_equal(key, "Powered") == TRUE)
1865 cdma_cm_update_powered(modem, &value);
1866 if (g_str_equal(key, "Settings") == TRUE)
1867 cdma_cm_update_settings(modem, &value);
1869 dbus_message_iter_next(dict);
1873 static int cdma_cm_get_properties(struct modem_data *modem)
1875 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1876 cdma_cm_properties_reply, modem);
1879 static void sim_update_imsi(struct modem_data *modem,
1880 DBusMessageIter* value)
1884 dbus_message_iter_get_basic(value, &imsi);
1886 DBG("%s imsi %s", modem->path, imsi);
1888 g_free(modem->imsi);
1889 modem->imsi = g_strdup(imsi);
1892 static gboolean sim_changed(DBusConnection *conn, DBusMessage *message,
1895 const char *path = dbus_message_get_path(message);
1896 struct modem_data *modem;
1897 DBusMessageIter iter, value;
1900 modem = g_hash_table_lookup(modem_hash, path);
1904 if (modem->ignore == TRUE)
1907 if (dbus_message_iter_init(message, &iter) == FALSE)
1910 dbus_message_iter_get_basic(&iter, &key);
1912 dbus_message_iter_next(&iter);
1913 dbus_message_iter_recurse(&iter, &value);
1915 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1916 sim_update_imsi(modem, &value);
1918 if (ready_to_create_device(modem) == FALSE)
1922 * This is a GSM modem. Create the device and
1923 * register it at the core. Enabling (setting
1924 * it online is done through the
1925 * modem_enable() callback.
1927 create_device(modem);
1933 static void sim_properties_reply(struct modem_data *modem,
1934 DBusMessageIter *dict)
1936 DBG("%s", modem->path);
1938 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1939 DBusMessageIter entry, value;
1942 dbus_message_iter_recurse(dict, &entry);
1943 dbus_message_iter_get_basic(&entry, &key);
1945 dbus_message_iter_next(&entry);
1946 dbus_message_iter_recurse(&entry, &value);
1948 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1949 sim_update_imsi(modem, &value);
1951 if (ready_to_create_device(modem) == FALSE)
1955 * This is a GSM modem. Create the device and
1956 * register it at the core. Enabling (setting
1957 * it online is done through the
1958 * modem_enable() callback.
1960 create_device(modem);
1962 if (modem->online == FALSE)
1966 * The modem is already online and we have the CM interface.
1967 * There will be no interface update and therefore our
1968 * state machine will not go to next step. We have to
1969 * trigger it from here.
1971 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1972 cm_get_properties(modem);
1973 cm_get_contexts(modem);
1978 dbus_message_iter_next(dict);
1982 static int sim_get_properties(struct modem_data *modem)
1984 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1985 sim_properties_reply, modem);
1988 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1991 if (has_interface(old_iface, api) == FALSE &&
1992 has_interface(new_iface, api) == TRUE) {
1993 DBG("%s added", api2string(api));
2000 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
2003 if (has_interface(old_iface, api) == TRUE &&
2004 has_interface(new_iface, api) == FALSE) {
2005 DBG("%s removed", api2string(api));
2012 static void modem_update_interfaces(struct modem_data *modem,
2016 DBG("%s", modem->path);
2018 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
2019 if (modem->imsi == NULL &&
2020 modem->set_powered == FALSE) {
2022 * Only use do GetProperties() when
2023 * device has not been powered up.
2025 sim_get_properties(modem);
2029 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2030 if (modem->device != NULL) {
2031 cm_get_properties(modem);
2032 cm_get_contexts(modem);
2036 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2037 if (ready_to_create_device(modem) == TRUE) {
2038 create_device(modem);
2039 if (modem->registered == TRUE)
2040 add_cdma_network(modem);
2043 if (modem->device != NULL)
2044 cdma_cm_get_properties(modem);
2047 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2048 if (modem->attached == TRUE)
2049 netreg_get_properties(modem);
2052 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
2053 cdma_netreg_get_properties(modem);
2056 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2057 remove_cm_context(modem, modem->context->path);
2060 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2061 remove_cm_context(modem, modem->context->path);
2064 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2065 remove_network(modem);
2068 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
2069 remove_network(modem);
2073 static gboolean modem_changed(DBusConnection *conn, DBusMessage *message,
2076 const char *path = dbus_message_get_path(message);
2077 struct modem_data *modem;
2078 DBusMessageIter iter, value;
2081 modem = g_hash_table_lookup(modem_hash, path);
2085 if (modem->ignore == TRUE)
2088 if (dbus_message_iter_init(message, &iter) == FALSE)
2091 dbus_message_iter_get_basic(&iter, &key);
2093 dbus_message_iter_next(&iter);
2094 dbus_message_iter_recurse(&iter, &value);
2096 if (g_str_equal(key, "Powered") == TRUE) {
2097 dbus_message_iter_get_basic(&value, &modem->powered);
2099 DBG("%s Powered %d", modem->path, modem->powered);
2101 if (modem->powered == FALSE)
2102 modem_set_powered(modem, TRUE);
2103 } else if (g_str_equal(key, "Online") == TRUE) {
2104 dbus_message_iter_get_basic(&value, &modem->online);
2106 DBG("%s Online %d", modem->path, modem->online);
2108 if (modem->device == NULL)
2111 connman_device_set_powered(modem->device, modem->online);
2112 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2115 interfaces = extract_interfaces(&value);
2117 if (interfaces == modem->interfaces)
2120 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2122 modem_update_interfaces(modem, modem->interfaces, interfaces);
2124 modem->interfaces = interfaces;
2125 } else if (g_str_equal(key, "Serial") == TRUE) {
2128 dbus_message_iter_get_basic(&value, &serial);
2130 g_free(modem->serial);
2131 modem->serial = g_strdup(serial);
2133 DBG("%s Serial %s", modem->path, modem->serial);
2135 if (has_interface(modem->interfaces,
2136 OFONO_API_CDMA_CM) == TRUE) {
2137 if (ready_to_create_device(modem) == TRUE) {
2138 create_device(modem);
2139 if (modem->registered == TRUE)
2140 add_cdma_network(modem);
2148 static void add_modem(const char *path, DBusMessageIter *prop)
2150 struct modem_data *modem;
2154 modem = g_hash_table_lookup(modem_hash, path);
2155 if (modem != NULL) {
2157 * When oFono powers up we ask for the modems and oFono is
2158 * reporting with modem_added signal the modems. Only
2164 modem = g_try_new0(struct modem_data, 1);
2168 modem->path = g_strdup(path);
2170 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2172 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2173 DBusMessageIter entry, value;
2176 dbus_message_iter_recurse(prop, &entry);
2177 dbus_message_iter_get_basic(&entry, &key);
2179 dbus_message_iter_next(&entry);
2180 dbus_message_iter_recurse(&entry, &value);
2182 if (g_str_equal(key, "Powered") == TRUE) {
2183 dbus_message_iter_get_basic(&value, &modem->powered);
2185 DBG("%s Powered %d", modem->path, modem->powered);
2186 } else if (g_str_equal(key, "Online") == TRUE) {
2187 dbus_message_iter_get_basic(&value, &modem->online);
2189 DBG("%s Online %d", modem->path, modem->online);
2190 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2191 modem->interfaces = extract_interfaces(&value);
2193 DBG("%s Interfaces 0x%02x", modem->path,
2195 } else if (g_str_equal(key, "Serial") == TRUE) {
2198 dbus_message_iter_get_basic(&value, &serial);
2199 modem->serial = g_strdup(serial);
2201 DBG("%s Serial %s", modem->path, modem->serial);
2202 } else if (g_str_equal(key, "Type") == TRUE) {
2205 dbus_message_iter_get_basic(&value, &type);
2207 DBG("%s Type %s", modem->path, type);
2208 if (g_strcmp0(type, "hardware") != 0) {
2209 DBG("%s Ignore this modem", modem->path);
2210 modem->ignore = TRUE;
2214 dbus_message_iter_next(prop);
2217 if (modem->ignore == TRUE)
2220 if (modem->powered == FALSE) {
2221 modem_set_powered(modem, TRUE);
2225 modem_update_interfaces(modem, 0, modem->interfaces);
2228 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2230 struct modem_data *modem = value;
2232 DBG("%s", modem->path);
2234 if (modem->ignore == TRUE)
2237 modem_set_powered(modem, FALSE);
2240 static void remove_modem(gpointer data)
2242 struct modem_data *modem = data;
2244 DBG("%s", modem->path);
2246 if (modem->call_set_property != NULL)
2247 dbus_pending_call_cancel(modem->call_set_property);
2249 if (modem->call_get_properties != NULL)
2250 dbus_pending_call_cancel(modem->call_get_properties);
2252 if (modem->call_get_contexts != NULL)
2253 dbus_pending_call_cancel(modem->call_get_contexts);
2255 if (modem->device != NULL)
2256 destroy_device(modem);
2258 if (modem->context != NULL)
2259 remove_cm_context(modem, modem->context->path);
2261 g_free(modem->serial);
2262 g_free(modem->name);
2263 g_free(modem->imsi);
2264 g_free(modem->path);
2269 static gboolean modem_added(DBusConnection *conn,
2270 DBusMessage *message, void *user_data)
2272 DBusMessageIter iter, properties;
2277 if (dbus_message_iter_init(message, &iter) == FALSE)
2280 dbus_message_iter_get_basic(&iter, &path);
2282 dbus_message_iter_next(&iter);
2283 dbus_message_iter_recurse(&iter, &properties);
2285 add_modem(path, &properties);
2290 static gboolean modem_removed(DBusConnection *conn,
2291 DBusMessage *message, void *user_data)
2293 DBusMessageIter iter;
2298 if (dbus_message_iter_init(message, &iter) == FALSE)
2301 dbus_message_iter_get_basic(&iter, &path);
2303 g_hash_table_remove(modem_hash, path);
2308 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2312 DBusMessageIter array, dict;
2316 reply = dbus_pending_call_steal_reply(call);
2318 dbus_error_init(&error);
2320 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2321 connman_error("%s", error.message);
2322 dbus_error_free(&error);
2326 if (dbus_message_iter_init(reply, &array) == FALSE)
2329 dbus_message_iter_recurse(&array, &dict);
2331 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2332 DBusMessageIter value, properties;
2335 dbus_message_iter_recurse(&dict, &value);
2336 dbus_message_iter_get_basic(&value, &path);
2338 dbus_message_iter_next(&value);
2339 dbus_message_iter_recurse(&value, &properties);
2341 add_modem(path, &properties);
2343 dbus_message_iter_next(&dict);
2347 dbus_message_unref(reply);
2349 dbus_pending_call_unref(call);
2352 static int manager_get_modems(void)
2354 DBusMessage *message;
2355 DBusPendingCall *call;
2359 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2360 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2361 if (message == NULL)
2364 if (dbus_connection_send_with_reply(connection, message,
2365 &call, TIMEOUT) == FALSE) {
2366 connman_error("Failed to call GetModems()");
2367 dbus_message_unref(message);
2372 connman_error("D-Bus connection not available");
2373 dbus_message_unref(message);
2377 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2380 dbus_message_unref(message);
2382 return -EINPROGRESS;
2385 static void ofono_connect(DBusConnection *conn, void *user_data)
2389 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2390 g_free, remove_modem);
2391 if (modem_hash == NULL)
2394 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2396 if (context_hash == NULL) {
2397 g_hash_table_destroy(modem_hash);
2401 manager_get_modems();
2404 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2408 if (modem_hash == NULL || context_hash == NULL)
2411 g_hash_table_destroy(modem_hash);
2414 g_hash_table_destroy(context_hash);
2415 context_hash = NULL;
2418 static int network_probe(struct connman_network *network)
2420 struct modem_data *modem = connman_network_get_data(network);
2422 DBG("%s network %p", modem->path, network);
2427 static void network_remove(struct connman_network *network)
2429 struct modem_data *modem = connman_network_get_data(network);
2431 DBG("%s network %p", modem->path, network);
2434 static int network_connect(struct connman_network *network)
2436 struct modem_data *modem = connman_network_get_data(network);
2438 DBG("%s network %p", modem->path, network);
2440 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2441 return context_set_active(modem, TRUE);
2442 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2443 return cdma_cm_set_powered(modem, TRUE);
2445 connman_error("Connection manager interface not available");
2450 static int network_disconnect(struct connman_network *network)
2452 struct modem_data *modem = connman_network_get_data(network);
2454 DBG("%s network %p", modem->path, network);
2456 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2457 return context_set_active(modem, FALSE);
2458 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2459 return cdma_cm_set_powered(modem, FALSE);
2461 connman_error("Connection manager interface not available");
2466 static struct connman_network_driver network_driver = {
2468 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2469 .probe = network_probe,
2470 .remove = network_remove,
2471 .connect = network_connect,
2472 .disconnect = network_disconnect,
2475 static int modem_probe(struct connman_device *device)
2477 struct modem_data *modem = connman_device_get_data(device);
2479 DBG("%s device %p", modem->path, device);
2484 static void modem_remove(struct connman_device *device)
2486 struct modem_data *modem = connman_device_get_data(device);
2488 DBG("%s device %p", modem->path, device);
2491 static int modem_enable(struct connman_device *device)
2493 struct modem_data *modem = connman_device_get_data(device);
2495 DBG("%s device %p", modem->path, device);
2497 if (modem->online == TRUE)
2500 return modem_set_online(modem, TRUE);
2503 static int modem_disable(struct connman_device *device)
2505 struct modem_data *modem = connman_device_get_data(device);
2507 DBG("%s device %p", modem->path, device);
2509 if (modem->online == FALSE)
2512 return modem_set_online(modem, FALSE);
2515 static struct connman_device_driver modem_driver = {
2517 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2518 .probe = modem_probe,
2519 .remove = modem_remove,
2520 .enable = modem_enable,
2521 .disable = modem_disable,
2524 static int tech_probe(struct connman_technology *technology)
2529 static void tech_remove(struct connman_technology *technology)
2533 static struct connman_technology_driver tech_driver = {
2535 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2536 .probe = tech_probe,
2537 .remove = tech_remove,
2541 static guint modem_added_watch;
2542 static guint modem_removed_watch;
2543 static guint modem_watch;
2544 static guint cm_watch;
2545 static guint sim_watch;
2546 static guint context_added_watch;
2547 static guint context_removed_watch;
2548 static guint netreg_watch;
2549 static guint context_watch;
2550 static guint cdma_cm_watch;
2551 static guint cdma_netreg_watch;
2553 static int ofono_init(void)
2559 connection = connman_dbus_get_connection();
2560 if (connection == NULL)
2563 watch = g_dbus_add_service_watch(connection,
2564 OFONO_SERVICE, ofono_connect,
2565 ofono_disconnect, NULL, NULL);
2567 modem_added_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2568 NULL, OFONO_MANAGER_INTERFACE,
2573 modem_removed_watch = g_dbus_add_signal_watch(connection,
2574 OFONO_SERVICE, NULL,
2575 OFONO_MANAGER_INTERFACE,
2580 modem_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2581 OFONO_MODEM_INTERFACE,
2586 cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2592 sim_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2593 OFONO_SIM_INTERFACE,
2598 context_added_watch = g_dbus_add_signal_watch(connection,
2599 OFONO_SERVICE, NULL,
2605 context_removed_watch = g_dbus_add_signal_watch(connection,
2606 OFONO_SERVICE, NULL,
2612 context_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2613 NULL, OFONO_CONTEXT_INTERFACE,
2618 netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2619 OFONO_NETREG_INTERFACE,
2624 cdma_cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2625 NULL, OFONO_CDMA_CM_INTERFACE,
2630 cdma_netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2631 NULL, OFONO_CDMA_NETREG_INTERFACE,
2633 cdma_netreg_changed,
2637 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2638 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2639 context_added_watch == 0 ||
2640 context_removed_watch == 0 ||
2641 context_watch == 0 || netreg_watch == 0 ||
2642 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2647 err = connman_network_driver_register(&network_driver);
2651 err = connman_device_driver_register(&modem_driver);
2653 connman_network_driver_unregister(&network_driver);
2657 err = connman_technology_driver_register(&tech_driver);
2659 connman_device_driver_unregister(&modem_driver);
2660 connman_network_driver_unregister(&network_driver);
2667 g_dbus_remove_watch(connection, cdma_netreg_watch);
2668 g_dbus_remove_watch(connection, cdma_cm_watch);
2669 g_dbus_remove_watch(connection, netreg_watch);
2670 g_dbus_remove_watch(connection, context_watch);
2671 g_dbus_remove_watch(connection, context_removed_watch);
2672 g_dbus_remove_watch(connection, context_added_watch);
2673 g_dbus_remove_watch(connection, sim_watch);
2674 g_dbus_remove_watch(connection, cm_watch);
2675 g_dbus_remove_watch(connection, modem_watch);
2676 g_dbus_remove_watch(connection, modem_removed_watch);
2677 g_dbus_remove_watch(connection, modem_added_watch);
2678 g_dbus_remove_watch(connection, watch);
2679 dbus_connection_unref(connection);
2684 static void ofono_exit(void)
2688 if (modem_hash != NULL) {
2690 * We should propably wait for the SetProperty() reply
2691 * message, because ...
2693 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2696 * ... here we will cancel the call.
2698 g_hash_table_destroy(modem_hash);
2702 if (context_hash != NULL) {
2703 g_hash_table_destroy(context_hash);
2704 context_hash = NULL;
2707 connman_technology_driver_unregister(&tech_driver);
2708 connman_device_driver_unregister(&modem_driver);
2709 connman_network_driver_unregister(&network_driver);
2711 g_dbus_remove_watch(connection, cdma_netreg_watch);
2712 g_dbus_remove_watch(connection, cdma_cm_watch);
2713 g_dbus_remove_watch(connection, netreg_watch);
2714 g_dbus_remove_watch(connection, context_watch);
2715 g_dbus_remove_watch(connection, context_removed_watch);
2716 g_dbus_remove_watch(connection, context_added_watch);
2717 g_dbus_remove_watch(connection, sim_watch);
2718 g_dbus_remove_watch(connection, cm_watch);
2719 g_dbus_remove_watch(connection, modem_watch);
2720 g_dbus_remove_watch(connection, modem_added_watch);
2721 g_dbus_remove_watch(connection, modem_removed_watch);
2722 g_dbus_remove_watch(connection, watch);
2724 dbus_connection_unref(connection);
2727 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2728 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)