5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011 BWM Car IT GmbH. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42 #include <connman/technology.h>
46 #define uninitialized_var(x) x = x
48 #define OFONO_SERVICE "org.ofono"
50 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
51 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
52 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
53 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
54 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
55 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
56 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
57 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
59 #define MODEM_ADDED "ModemAdded"
60 #define MODEM_REMOVED "ModemRemoved"
61 #define PROPERTY_CHANGED "PropertyChanged"
62 #define CONTEXT_ADDED "ContextAdded"
63 #define CONTEXT_REMOVED "ContextRemoved"
65 #define GET_PROPERTIES "GetProperties"
66 #define SET_PROPERTY "SetProperty"
67 #define GET_MODEMS "GetModems"
68 #define GET_CONTEXTS "GetContexts"
74 OFONO_API_NETREG = 0x2,
76 OFONO_API_CDMA_NETREG = 0x8,
77 OFONO_API_CDMA_CM = 0x10,
81 * The way this plugin works is following:
83 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
84 * attached -> netreg -> ready
86 * Depending on the modem type, this plugin will behave differently.
90 * When a new modem appears, the plugin always powers it up. This
91 * allows the plugin to create a connman_device. The core will call
92 * modem_enable() if the technology is enabled. modem_enable() will
93 * then set the modem online. If the technology is disabled then
94 * modem_disable() will just set the modem offline. The modem is
95 * always kept powered all the time.
97 * After setting the modem online the plugin waits for the
98 * ConnectionManager and ConnectionContext to appear. When the context
99 * signals that it is attached and the NetworkRegistration interface
100 * appears, a new Service will be created and registered at the core.
102 * When asked to connect to the network (network_connect()) the plugin
103 * will set the Active property on the context. If this operation is
104 * successful the modem is connected to the network. oFono will inform
105 * the plugin about IP configuration through the updating the context's
110 * When a new modem appears, the plugin always powers it up. This
111 * allows the plugin to create connman_device either using IMSI either
112 * using modem Serial if the modem got a SIM interface or not.
114 * As for GSM, the core will call modem_enable() if the technology
115 * is enabled. modem_enable() will then set the modem online.
116 * If the technology is disabled then modem_disable() will just set the
117 * modem offline. The modem is always kept powered all the time.
119 * After setting the modem online the plugin waits for CdmaConnectionManager
120 * interface to appear. Then, once CdmaNetworkRegistration appears, a new
121 * Service will be created and registered at the core.
123 * When asked to connect to the network (network_connect()) the plugin
124 * will power up the CdmaConnectionManager interface.
125 * If the operation is successful the modem is connected to the network.
126 * oFono will inform the plugin about IP configuration through the
127 * updating CdmaConnectionManager settings properties.
130 static DBusConnection *connection;
132 static GHashTable *modem_hash;
133 static GHashTable *context_hash;
135 struct network_context {
139 enum connman_ipconfig_method ipv4_method;
140 struct connman_ipaddress *ipv4_address;
141 char *ipv4_nameservers;
143 enum connman_ipconfig_method ipv6_method;
144 struct connman_ipaddress *ipv6_address;
145 char *ipv6_nameservers;
151 struct connman_device *device;
152 struct connman_network *network;
154 struct network_context *context;
156 /* Modem Interface */
158 connman_bool_t powered;
159 connman_bool_t online;
161 connman_bool_t ignore;
163 connman_bool_t set_powered;
165 /* CDMA ConnectionManager Interface */
166 connman_bool_t cdma_cm_powered;
168 /* ConnectionManager Interface */
169 connman_bool_t attached;
170 connman_bool_t cm_powered;
172 /* ConnectionContext Interface */
173 connman_bool_t active;
174 connman_bool_t set_active;
176 /* SimManager Interface */
179 /* Netreg Interface */
182 uint8_t data_strength; /* 1xEVDO signal strength */
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 connman_bool_t setip = FALSE;
262 DBG("%s", modem->path);
264 connman_network_set_index(modem->network, modem->context->index);
266 switch (modem->context->ipv4_method) {
267 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
268 case CONNMAN_IPCONFIG_METHOD_OFF:
269 case CONNMAN_IPCONFIG_METHOD_MANUAL:
270 case CONNMAN_IPCONFIG_METHOD_AUTO:
273 case CONNMAN_IPCONFIG_METHOD_FIXED:
274 connman_network_set_ipv4_method(modem->network,
275 modem->context->ipv4_method);
276 connman_network_set_ipaddress(modem->network,
277 modem->context->ipv4_address);
278 connman_network_set_nameservers(modem->network,
279 modem->context->ipv4_nameservers);
283 case CONNMAN_IPCONFIG_METHOD_DHCP:
284 connman_network_set_ipv4_method(modem->network,
285 modem->context->ipv4_method);
290 switch (modem->context->ipv6_method) {
291 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
292 case CONNMAN_IPCONFIG_METHOD_OFF:
293 case CONNMAN_IPCONFIG_METHOD_MANUAL:
294 case CONNMAN_IPCONFIG_METHOD_DHCP:
295 case CONNMAN_IPCONFIG_METHOD_AUTO:
298 case CONNMAN_IPCONFIG_METHOD_FIXED:
299 connman_network_set_ipv6_method(modem->network,
300 modem->context->ipv6_method);
301 connman_network_set_ipaddress(modem->network,
302 modem->context->ipv6_address);
308 connman_network_set_connected(modem->network, TRUE);
311 static void set_disconnected(struct modem_data *modem)
313 DBG("%s", modem->path);
315 connman_network_set_connected(modem->network, FALSE);
318 typedef void (*set_property_cb)(struct modem_data *data,
319 connman_bool_t success);
320 typedef void (*get_properties_cb)(struct modem_data *data,
321 DBusMessageIter *dict);
323 struct property_info {
324 struct modem_data *modem;
326 const char *interface;
327 const char *property;
328 set_property_cb set_property_cb;
329 get_properties_cb get_properties_cb;
332 static void set_property_reply(DBusPendingCall *call, void *user_data)
334 struct property_info *info = user_data;
337 connman_bool_t success = TRUE;
339 DBG("%s path %s %s.%s", info->modem->path,
340 info->path, info->interface, info->property);
342 info->modem->call_set_property = NULL;
344 dbus_error_init(&error);
346 reply = dbus_pending_call_steal_reply(call);
348 if (dbus_set_error_from_message(&error, reply)) {
349 connman_error("Failed to change property: %s %s.%s: %s %s",
350 info->path, info->interface, info->property,
351 error.name, error.message);
352 dbus_error_free(&error);
356 if (info->set_property_cb != NULL)
357 (*info->set_property_cb)(info->modem, success);
359 dbus_message_unref(reply);
361 dbus_pending_call_unref(call);
364 static int set_property(struct modem_data *modem,
365 const char *path, const char *interface,
366 const char *property, int type, void *value,
367 set_property_cb notify)
369 DBusMessage *message;
370 DBusMessageIter iter;
371 struct property_info *info;
373 DBG("%s path %s %s.%s", modem->path, path, interface, property);
375 if (modem->call_set_property != NULL) {
376 DBG("Cancel pending SetProperty");
378 dbus_pending_call_cancel(modem->call_set_property);
379 modem->call_set_property = NULL;
382 message = dbus_message_new_method_call(OFONO_SERVICE, path,
383 interface, SET_PROPERTY);
387 dbus_message_iter_init_append(message, &iter);
388 connman_dbus_property_append_basic(&iter, property, type, value);
390 if (dbus_connection_send_with_reply(connection, message,
391 &modem->call_set_property, TIMEOUT) == FALSE) {
392 connman_error("Failed to change property: %s %s.%s",
393 path, interface, property);
394 dbus_message_unref(message);
398 if (modem->call_set_property == NULL) {
399 connman_error("D-Bus connection not available");
400 dbus_message_unref(message);
404 info = g_try_new0(struct property_info, 1);
406 dbus_message_unref(message);
412 info->interface = interface;
413 info->property = property;
414 info->set_property_cb = notify;
416 dbus_pending_call_set_notify(modem->call_set_property,
417 set_property_reply, info, g_free);
419 dbus_message_unref(message);
424 static void get_properties_reply(DBusPendingCall *call, void *user_data)
426 struct property_info *info = user_data;
427 DBusMessageIter array, dict;
431 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
433 info->modem->call_get_properties = NULL;
435 dbus_error_init(&error);
437 reply = dbus_pending_call_steal_reply(call);
439 if (dbus_set_error_from_message(&error, reply)) {
440 connman_error("Failed to get properties: %s %s: %s %s",
441 info->path, info->interface,
442 error.name, error.message);
443 dbus_error_free(&error);
448 if (dbus_message_iter_init(reply, &array) == FALSE)
451 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
454 dbus_message_iter_recurse(&array, &dict);
456 if (info->get_properties_cb != NULL)
457 (*info->get_properties_cb)(info->modem, &dict);
461 dbus_message_unref(reply);
463 dbus_pending_call_unref(call);
466 static int get_properties(const char *path, const char *interface,
467 get_properties_cb notify,
468 struct modem_data *modem)
470 DBusMessage *message;
471 struct property_info *info;
473 DBG("%s path %s %s", modem->path, path, interface);
475 if (modem->call_get_properties != NULL) {
476 connman_error("Pending GetProperties");
480 message = dbus_message_new_method_call(OFONO_SERVICE, path,
481 interface, GET_PROPERTIES);
485 if (dbus_connection_send_with_reply(connection, message,
486 &modem->call_get_properties, TIMEOUT) == FALSE) {
487 connman_error("Failed to call %s.GetProperties()", interface);
488 dbus_message_unref(message);
492 if (modem->call_get_properties == NULL) {
493 connman_error("D-Bus connection not available");
494 dbus_message_unref(message);
498 info = g_try_new0(struct property_info, 1);
500 dbus_message_unref(message);
506 info->interface = interface;
507 info->get_properties_cb = notify;
509 dbus_pending_call_set_notify(modem->call_get_properties,
510 get_properties_reply, info, g_free);
512 dbus_message_unref(message);
517 static void context_set_active_reply(struct modem_data *modem,
518 connman_bool_t success)
520 DBG("%s", modem->path);
522 if (success == TRUE) {
524 * Don't handle do anything on success here. oFono will send
525 * the change via PropertyChanged singal.
531 * Active = True might fail due a timeout. That means oFono
532 * still tries to go online. If we retry to set Active = True,
533 * we just get a InProgress error message. Should we power
534 * cycle the modem in such cases?
537 if (modem->network == NULL) {
539 * In the case where we power down the device
540 * we don't wait for the reply, therefore the network
541 * might already be gone.
546 connman_network_set_error(modem->network,
547 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
550 static int context_set_active(struct modem_data *modem,
551 connman_bool_t active)
555 DBG("%s active %d", modem->path, active);
557 err = set_property(modem, modem->context->path,
558 OFONO_CONTEXT_INTERFACE,
559 "Active", DBUS_TYPE_BOOLEAN,
561 context_set_active_reply);
563 if (active == FALSE && err == -EINPROGRESS)
569 static void cdma_cm_set_powered_reply(struct modem_data *modem,
570 connman_bool_t success)
572 DBG("%s", modem->path);
574 if (success == TRUE) {
576 * Don't handle do anything on success here. oFono will send
577 * the change via PropertyChanged singal.
583 * Powered = True might fail due a timeout. That means oFono
584 * still tries to go online. If we retry to set Powered = True,
585 * we just get a InProgress error message. Should we power
586 * cycle the modem in such cases?
589 if (modem->network == NULL) {
591 * In the case where we power down the device
592 * we don't wait for the reply, therefore the network
593 * might already be gone.
598 connman_network_set_error(modem->network,
599 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
602 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
606 DBG("%s powered %d", modem->path, powered);
608 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
609 "Powered", DBUS_TYPE_BOOLEAN,
611 cdma_cm_set_powered_reply);
613 if (powered == FALSE && err == -EINPROGRESS)
619 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
621 DBG("%s online %d", modem->path, online);
623 return set_property(modem, modem->path,
624 OFONO_MODEM_INTERFACE,
625 "Online", DBUS_TYPE_BOOLEAN,
630 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
634 DBG("%s powered %d", modem->path, powered);
636 err = set_property(modem, modem->path,
638 "Powered", DBUS_TYPE_BOOLEAN,
642 if (powered == FALSE && err == -EINPROGRESS)
648 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
652 DBG("%s powered %d", modem->path, powered);
654 modem->set_powered = powered;
656 err = set_property(modem, modem->path,
657 OFONO_MODEM_INTERFACE,
658 "Powered", DBUS_TYPE_BOOLEAN,
662 if (powered == FALSE && err == -EINPROGRESS)
668 static connman_bool_t has_interface(uint8_t interfaces,
671 if ((interfaces & api) == api)
677 static uint8_t extract_interfaces(DBusMessageIter *array)
679 DBusMessageIter entry;
680 uint8_t interfaces = 0;
682 dbus_message_iter_recurse(array, &entry);
684 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
687 dbus_message_iter_get_basic(&entry, &name);
689 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
690 interfaces |= OFONO_API_SIM;
691 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
692 interfaces |= OFONO_API_NETREG;
693 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
694 interfaces |= OFONO_API_CM;
695 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
696 interfaces |= OFONO_API_CDMA_CM;
697 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
698 interfaces |= OFONO_API_CDMA_NETREG;
700 dbus_message_iter_next(&entry);
706 static char *extract_nameservers(DBusMessageIter *array)
708 DBusMessageIter entry;
709 char *nameservers = NULL;
712 dbus_message_iter_recurse(array, &entry);
714 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
715 const char *nameserver;
717 dbus_message_iter_get_basic(&entry, &nameserver);
719 if (nameservers == NULL) {
720 nameservers = g_strdup(nameserver);
723 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
727 dbus_message_iter_next(&entry);
733 static void extract_ipv4_settings(DBusMessageIter *array,
734 struct network_context *context)
736 DBusMessageIter dict;
737 char *address = NULL, *netmask = NULL, *gateway = NULL;
738 char *nameservers = NULL;
739 const char *interface = NULL;
742 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
745 dbus_message_iter_recurse(array, &dict);
747 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
748 DBusMessageIter entry, value;
749 const char *key, *val;
751 dbus_message_iter_recurse(&dict, &entry);
752 dbus_message_iter_get_basic(&entry, &key);
754 dbus_message_iter_next(&entry);
755 dbus_message_iter_recurse(&entry, &value);
757 if (g_str_equal(key, "Interface") == TRUE) {
758 dbus_message_iter_get_basic(&value, &interface);
760 DBG("Interface %s", interface);
762 index = connman_inet_ifindex(interface);
764 DBG("index %d", index);
765 } else if (g_str_equal(key, "Method") == TRUE) {
766 dbus_message_iter_get_basic(&value, &val);
768 DBG("Method %s", val);
770 if (g_strcmp0(val, "static") == 0) {
771 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
772 } else if (g_strcmp0(val, "dhcp") == 0) {
773 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
776 } else if (g_str_equal(key, "Address") == TRUE) {
777 dbus_message_iter_get_basic(&value, &val);
779 address = g_strdup(val);
781 DBG("Address %s", address);
782 } else if (g_str_equal(key, "Netmask") == TRUE) {
783 dbus_message_iter_get_basic(&value, &val);
785 netmask = g_strdup(val);
787 DBG("Netmask %s", netmask);
788 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
789 nameservers = extract_nameservers(&value);
791 DBG("Nameservers %s", nameservers);
792 } else if (g_str_equal(key, "Gateway") == TRUE) {
793 dbus_message_iter_get_basic(&value, &val);
795 gateway = g_strdup(val);
797 DBG("Gateway %s", gateway);
800 dbus_message_iter_next(&dict);
806 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
809 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
810 if (context->ipv4_address == NULL)
813 context->index = index;
814 connman_ipaddress_set_ipv4(context->ipv4_address, address,
817 context->ipv4_nameservers = nameservers;
820 if (context->ipv4_nameservers != nameservers)
828 static void extract_ipv6_settings(DBusMessageIter *array,
829 struct network_context *context)
831 DBusMessageIter dict;
832 char *address = NULL, *gateway = NULL;
833 unsigned char prefix_length;
834 char *nameservers = NULL;
835 const char *interface = NULL;
838 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
841 dbus_message_iter_recurse(array, &dict);
843 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
844 DBusMessageIter entry, value;
845 const char *key, *val;
847 dbus_message_iter_recurse(&dict, &entry);
848 dbus_message_iter_get_basic(&entry, &key);
850 dbus_message_iter_next(&entry);
851 dbus_message_iter_recurse(&entry, &value);
853 if (g_str_equal(key, "Interface") == TRUE) {
854 dbus_message_iter_get_basic(&value, &interface);
856 DBG("Interface %s", interface);
858 index = connman_inet_ifindex(interface);
860 DBG("index %d", index);
861 } else if (g_str_equal(key, "Address") == TRUE) {
862 dbus_message_iter_get_basic(&value, &val);
864 address = g_strdup(val);
866 DBG("Address %s", address);
867 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
868 dbus_message_iter_get_basic(&value, &prefix_length);
870 DBG("prefix length %d", prefix_length);
871 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
872 nameservers = extract_nameservers(&value);
874 DBG("Nameservers %s", nameservers);
875 } else if (g_str_equal(key, "Gateway") == TRUE) {
876 dbus_message_iter_get_basic(&value, &val);
878 gateway = g_strdup(val);
880 DBG("Gateway %s", gateway);
883 dbus_message_iter_next(&dict);
889 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
891 context->ipv6_address =
892 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
893 if (context->ipv6_address == NULL)
896 context->index = index;
897 connman_ipaddress_set_ipv6(context->ipv6_address, address,
898 prefix_length, gateway);
900 context->ipv6_nameservers = nameservers;
903 if (context->ipv6_nameservers != nameservers)
910 static connman_bool_t ready_to_create_device(struct modem_data *modem)
913 * There are three different modem types which behave slightly
915 * - GSM modems will expose the SIM interface then the
917 * - DUN modems will expose first a unique serial number (BDADDR)
918 * and then the CM interface.
919 * - CDMA modems will expose CM first and sometime later
920 * a unique serial number.
922 * This functions tests if we have the necessary information gathered
923 * before we are able to create a device.
926 if (modem->device != NULL)
929 if (modem->imsi != NULL || modem->serial != NULL)
935 static void create_device(struct modem_data *modem)
937 struct connman_device *device;
938 char *uninitialized_var(ident);
940 DBG("%s", modem->path);
942 if (modem->imsi != NULL)
944 else if (modem->serial != NULL)
945 ident = modem->serial;
947 if (connman_dbus_validate_ident(ident) == FALSE)
948 ident = connman_dbus_encode_string(ident);
950 ident = g_strdup(ident);
952 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
956 DBG("device %p", device);
958 connman_device_set_ident(device, ident);
960 connman_device_set_string(device, "Path", modem->path);
962 connman_device_set_data(device, modem);
964 if (connman_device_register(device) < 0) {
965 connman_error("Failed to register cellular device");
966 connman_device_unref(device);
970 modem->device = device;
972 connman_device_set_powered(modem->device, modem->online);
977 static void destroy_device(struct modem_data *modem)
979 DBG("%s", modem->path);
981 connman_device_set_powered(modem->device, FALSE);
983 if (modem->network != NULL) {
984 connman_device_remove_network(modem->device, modem->network);
985 connman_network_unref(modem->network);
986 modem->network = NULL;
989 connman_device_unregister(modem->device);
990 connman_device_unref(modem->device);
992 modem->device = NULL;
995 static void add_network(struct modem_data *modem)
999 DBG("%s", modem->path);
1001 if (modem->network != NULL)
1004 modem->network = connman_network_create(modem->context->path,
1005 CONNMAN_NETWORK_TYPE_CELLULAR);
1006 if (modem->network == NULL)
1009 DBG("network %p", modem->network);
1011 connman_network_set_data(modem->network, modem);
1013 connman_network_set_string(modem->network, "Path",
1014 modem->context->path);
1016 connman_network_set_index(modem->network, modem->context->index);
1018 if (modem->name != NULL)
1019 connman_network_set_name(modem->network, modem->name);
1021 connman_network_set_name(modem->network, "");
1023 connman_network_set_strength(modem->network, modem->strength);
1025 group = get_ident(modem->context->path);
1026 connman_network_set_group(modem->network, group);
1028 connman_network_set_available(modem->network, TRUE);
1030 connman_network_set_bool(modem->network, "Roaming",
1033 if (connman_device_add_network(modem->device, modem->network) < 0) {
1034 connman_network_unref(modem->network);
1035 modem->network = NULL;
1040 * Create the ipconfig layer before trying to connect. Withouth
1041 * the ipconfig layer the core is not ready to process errors.
1043 connman_network_set_index(modem->network, -1);
1046 static void remove_network(struct modem_data *modem)
1048 DBG("%s", modem->path);
1050 if (modem->network == NULL)
1053 DBG("network %p", modem->network);
1055 connman_device_remove_network(modem->device, modem->network);
1056 connman_network_unref(modem->network);
1057 modem->network = NULL;
1060 static int add_cm_context(struct modem_data *modem, const char *context_path,
1061 DBusMessageIter *dict)
1063 const char *context_type;
1064 struct network_context *context = NULL;
1065 connman_bool_t active = FALSE;
1067 DBG("%s context path %s", modem->path, context_path);
1069 if (modem->context != NULL) {
1071 * We have already assigned a context to this modem
1072 * and we do only support one Internet context.
1077 context = network_context_alloc(context_path);
1078 if (context == NULL)
1081 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1082 DBusMessageIter entry, value;
1085 dbus_message_iter_recurse(dict, &entry);
1086 dbus_message_iter_get_basic(&entry, &key);
1088 dbus_message_iter_next(&entry);
1089 dbus_message_iter_recurse(&entry, &value);
1091 if (g_str_equal(key, "Type") == TRUE) {
1092 dbus_message_iter_get_basic(&value, &context_type);
1094 DBG("%s context %s type %s", modem->path,
1095 context_path, context_type);
1096 } else if (g_str_equal(key, "Settings") == TRUE) {
1097 DBG("%s Settings", modem->path);
1099 extract_ipv4_settings(&value, context);
1100 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1101 DBG("%s IPv6.Settings", modem->path);
1103 extract_ipv6_settings(&value, context);
1104 } else if (g_str_equal(key, "Active") == TRUE) {
1105 dbus_message_iter_get_basic(&value, &active);
1107 DBG("%s Active %d", modem->path, active);
1110 dbus_message_iter_next(dict);
1113 if (g_strcmp0(context_type, "internet") != 0) {
1114 network_context_free(context);
1118 modem->context = context;
1119 modem->active = active;
1121 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1126 static void remove_cm_context(struct modem_data *modem,
1127 const char *context_path)
1129 if (modem->context == NULL)
1132 g_hash_table_remove(context_hash, context_path);
1134 network_context_free(modem->context);
1135 modem->context = NULL;
1138 static gboolean context_changed(DBusConnection *connection,
1139 DBusMessage *message,
1142 const char *context_path = dbus_message_get_path(message);
1143 struct modem_data *modem = NULL;
1144 DBusMessageIter iter, value;
1147 DBG("context_path %s", context_path);
1149 modem = g_hash_table_lookup(context_hash, context_path);
1153 if (dbus_message_iter_init(message, &iter) == FALSE)
1156 dbus_message_iter_get_basic(&iter, &key);
1158 dbus_message_iter_next(&iter);
1159 dbus_message_iter_recurse(&iter, &value);
1162 * oFono guarantees the ordering of Settings and
1163 * Active. Settings will always be send before Active = True.
1164 * That means we don't have to order here.
1166 if (g_str_equal(key, "Settings") == TRUE) {
1167 DBG("%s Settings", modem->path);
1169 extract_ipv4_settings(&value, modem->context);
1170 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1171 DBG("%s IPv6.Settings", modem->path);
1173 extract_ipv6_settings(&value, modem->context);
1174 } else if (g_str_equal(key, "Active") == TRUE) {
1175 dbus_message_iter_get_basic(&value, &modem->active);
1177 DBG("%s Active %d", modem->path, modem->active);
1179 if (modem->active == TRUE)
1180 set_connected(modem);
1182 set_disconnected(modem);
1188 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1190 struct modem_data *modem = user_data;
1191 DBusMessageIter array, dict, entry, value;
1195 DBG("%s", modem->path);
1197 modem->call_get_contexts = NULL;
1199 reply = dbus_pending_call_steal_reply(call);
1201 dbus_error_init(&error);
1203 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1204 connman_error("%s", error.message);
1205 dbus_error_free(&error);
1209 if (dbus_message_iter_init(reply, &array) == FALSE)
1212 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1215 dbus_message_iter_recurse(&array, &dict);
1217 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1218 const char *context_path;
1220 dbus_message_iter_recurse(&dict, &entry);
1221 dbus_message_iter_get_basic(&entry, &context_path);
1223 dbus_message_iter_next(&entry);
1224 dbus_message_iter_recurse(&entry, &value);
1226 if (add_cm_context(modem, context_path, &value) == 0)
1229 dbus_message_iter_next(&dict);
1233 dbus_message_unref(reply);
1235 dbus_pending_call_unref(call);
1238 static int cm_get_contexts(struct modem_data *modem)
1240 DBusMessage *message;
1242 DBG("%s", modem->path);
1244 if (modem->call_get_contexts != NULL)
1247 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1248 OFONO_CM_INTERFACE, GET_CONTEXTS);
1249 if (message == NULL)
1252 if (dbus_connection_send_with_reply(connection, message,
1253 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1254 connman_error("Failed to call GetContexts()");
1255 dbus_message_unref(message);
1259 if (modem->call_get_contexts == NULL) {
1260 connman_error("D-Bus connection not available");
1261 dbus_message_unref(message);
1265 dbus_pending_call_set_notify(modem->call_get_contexts,
1266 cm_get_contexts_reply,
1269 dbus_message_unref(message);
1271 return -EINPROGRESS;
1274 static gboolean cm_context_added(DBusConnection *connection,
1275 DBusMessage *message,
1278 const char *path = dbus_message_get_path(message);
1280 struct modem_data *modem;
1281 DBusMessageIter iter, properties;
1285 modem = g_hash_table_lookup(modem_hash, context_path);
1289 if (dbus_message_iter_init(message, &iter) == FALSE)
1292 dbus_message_iter_get_basic(&iter, &context_path);
1294 dbus_message_iter_next(&iter);
1295 dbus_message_iter_recurse(&iter, &properties);
1297 if (add_cm_context(modem, context_path, &properties) != 0)
1303 static gboolean cm_context_removed(DBusConnection *connection,
1304 DBusMessage *message,
1307 const char *path = dbus_message_get_path(message);
1308 const char *context_path;
1309 struct modem_data *modem;
1310 DBusMessageIter iter;
1312 DBG("context path %s", path);
1314 if (dbus_message_iter_init(message, &iter) == FALSE)
1317 dbus_message_iter_get_basic(&iter, &context_path);
1319 modem = g_hash_table_lookup(context_hash, context_path);
1323 remove_cm_context(modem, context_path);
1328 static void netreg_update_name(struct modem_data *modem,
1329 DBusMessageIter* value)
1333 dbus_message_iter_get_basic(value, &name);
1335 DBG("%s Name %s", modem->path, name);
1337 g_free(modem->name);
1338 modem->name = g_strdup(name);
1340 if (modem->network == NULL)
1343 connman_network_set_name(modem->network, modem->name);
1344 connman_network_update(modem->network);
1347 static void netreg_update_strength(struct modem_data *modem,
1348 DBusMessageIter *value)
1350 dbus_message_iter_get_basic(value, &modem->strength);
1352 DBG("%s Strength %d", modem->path, modem->strength);
1354 if (modem->network == NULL)
1359 * We don't have 2 signal notifications we always report the strength
1360 * signal. data_strength is always equal to 0.
1363 * In the case we have a data_strength signal (from 1xEVDO network)
1364 * we don't need to update the value with strength signal (from 1xCDMA)
1365 * because the modem is registered to 1xEVDO network for data call.
1366 * In case we have no data_strength signal (not registered to 1xEVDO
1367 * network), we must report the strength signal (registered to 1xCDMA
1368 * network e.g slow mode).
1370 if (modem->data_strength != 0)
1373 connman_network_set_strength(modem->network, modem->strength);
1374 connman_network_update(modem->network);
1377 /* Retrieve 1xEVDO Data Strength signal */
1378 static void netreg_update_datastrength(struct modem_data *modem,
1379 DBusMessageIter *value)
1381 dbus_message_iter_get_basic(value, &modem->data_strength);
1383 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1385 if (modem->network == NULL)
1389 * CDMA modem is not registered to 1xEVDO network, let
1390 * update_signal_strength() reporting the value on the Strength signal
1393 if (modem->data_strength == 0)
1396 connman_network_set_strength(modem->network, modem->data_strength);
1397 connman_network_update(modem->network);
1400 static void netreg_update_roaming(struct modem_data *modem,
1401 DBusMessageIter *value)
1404 connman_bool_t roaming;
1406 dbus_message_iter_get_basic(value, &status);
1408 if (g_str_equal(status, "roaming") == TRUE)
1413 if (roaming == modem->roaming)
1416 modem->roaming = roaming;
1418 if (modem->network == NULL)
1421 connman_network_set_bool(modem->network,
1422 "Roaming", modem->roaming);
1423 connman_network_update(modem->network);
1426 static void netreg_update_regdom(struct modem_data *modem,
1427 DBusMessageIter *value)
1429 char *mobile_country_code;
1433 dbus_message_iter_get_basic(value, &mobile_country_code);
1435 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1438 mcc = atoi(mobile_country_code);
1439 if (mcc > 799 || mcc < 200)
1442 alpha2 = mcc_country_codes[mcc - 200];
1444 connman_technology_set_regdom(alpha2);
1447 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1450 const char *path = dbus_message_get_path(message);
1451 struct modem_data *modem;
1452 DBusMessageIter iter, value;
1455 modem = g_hash_table_lookup(modem_hash, path);
1459 if (modem->ignore == TRUE)
1462 if (dbus_message_iter_init(message, &iter) == FALSE)
1465 dbus_message_iter_get_basic(&iter, &key);
1467 dbus_message_iter_next(&iter);
1468 dbus_message_iter_recurse(&iter, &value);
1470 if (g_str_equal(key, "Name") == TRUE)
1471 netreg_update_name(modem, &value);
1472 else if (g_str_equal(key, "Strength") == TRUE)
1473 netreg_update_strength(modem, &value);
1474 else if (g_str_equal(key, "Status") == TRUE)
1475 netreg_update_roaming(modem, &value);
1476 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1477 netreg_update_regdom(modem, &value);
1482 static void netreg_properties_reply(struct modem_data *modem,
1483 DBusMessageIter *dict)
1485 DBG("%s", modem->path);
1487 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1488 DBusMessageIter entry, value;
1491 dbus_message_iter_recurse(dict, &entry);
1492 dbus_message_iter_get_basic(&entry, &key);
1494 dbus_message_iter_next(&entry);
1495 dbus_message_iter_recurse(&entry, &value);
1497 if (g_str_equal(key, "Name") == TRUE)
1498 netreg_update_name(modem, &value);
1499 else if (g_str_equal(key, "Strength") == TRUE)
1500 netreg_update_strength(modem, &value);
1501 else if (g_str_equal(key, "Status") == TRUE)
1502 netreg_update_roaming(modem, &value);
1503 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1504 netreg_update_regdom(modem, &value);
1506 dbus_message_iter_next(dict);
1509 if (modem->context == NULL) {
1511 * netgreg_get_properties() was issued after we got
1512 * cm_get_contexts_reply() where we create the
1513 * context. Though before we got the
1514 * netreg_properties_reply the context was removed
1515 * again. Therefore we have to skip the network
1523 if (modem->active == TRUE)
1524 set_connected(modem);
1527 static int netreg_get_properties(struct modem_data *modem)
1529 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1530 netreg_properties_reply, modem);
1533 static void add_cdma_network(struct modem_data *modem)
1535 /* Be sure that device is created before adding CDMA network */
1536 if (modem->device == NULL)
1540 * CDMA modems don't need contexts for data call, however the current
1541 * add_network() logic needs one, so we create one to proceed.
1543 if (modem->context == NULL)
1544 modem->context = network_context_alloc(modem->path);
1546 if (modem->name == NULL)
1547 modem->name = g_strdup("CDMA Network");
1551 if (modem->cdma_cm_powered == TRUE)
1552 set_connected(modem);
1555 static gboolean cdma_netreg_changed(DBusConnection *connection,
1556 DBusMessage *message,
1559 const char *path = dbus_message_get_path(message);
1560 struct modem_data *modem;
1561 DBusMessageIter iter, value;
1566 modem = g_hash_table_lookup(modem_hash, path);
1570 if (modem->ignore == TRUE)
1573 if (dbus_message_iter_init(message, &iter) == FALSE)
1576 dbus_message_iter_get_basic(&iter, &key);
1578 dbus_message_iter_next(&iter);
1579 dbus_message_iter_recurse(&iter, &value);
1581 if (g_str_equal(key, "Name") == TRUE)
1582 netreg_update_name(modem, &value);
1583 else if (g_str_equal(key, "Strength") == TRUE)
1584 netreg_update_strength(modem, &value);
1585 else if (g_str_equal(key, "DataStrength") == TRUE)
1586 netreg_update_datastrength(modem, &value);
1587 else if (g_str_equal(key, "Status") == TRUE)
1588 netreg_update_roaming(modem, &value);
1590 add_cdma_network(modem);
1595 static void cdma_netreg_properties_reply(struct modem_data *modem,
1596 DBusMessageIter *dict)
1598 DBG("%s", modem->path);
1600 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1601 DBusMessageIter entry, value;
1604 dbus_message_iter_recurse(dict, &entry);
1605 dbus_message_iter_get_basic(&entry, &key);
1607 dbus_message_iter_next(&entry);
1608 dbus_message_iter_recurse(&entry, &value);
1610 if (g_str_equal(key, "Name") == TRUE)
1611 netreg_update_name(modem, &value);
1612 else if (g_str_equal(key, "Strength") == TRUE)
1613 netreg_update_strength(modem, &value);
1614 else if (g_str_equal(key, "DataStrength") == TRUE)
1615 netreg_update_datastrength(modem, &value);
1616 else if (g_str_equal(key, "Status") == TRUE)
1617 netreg_update_roaming(modem, &value);
1619 dbus_message_iter_next(dict);
1622 add_cdma_network(modem);
1625 static int cdma_netreg_get_properties(struct modem_data *modem)
1627 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1628 cdma_netreg_properties_reply, modem);
1631 static void cm_update_attached(struct modem_data *modem,
1632 DBusMessageIter *value)
1634 dbus_message_iter_get_basic(value, &modem->attached);
1636 DBG("%s Attached %d", modem->path, modem->attached);
1638 if (modem->attached == FALSE)
1641 if (has_interface(modem->interfaces,
1642 OFONO_API_NETREG) == FALSE) {
1646 netreg_get_properties(modem);
1649 static void cm_update_powered(struct modem_data *modem,
1650 DBusMessageIter *value)
1652 dbus_message_iter_get_basic(value, &modem->cm_powered);
1654 DBG("%s ConnnectionManager Powered %d", modem->path,
1657 if (modem->cm_powered == TRUE)
1660 cm_set_powered(modem, TRUE);
1663 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1666 const char *path = dbus_message_get_path(message);
1667 struct modem_data *modem;
1668 DBusMessageIter iter, value;
1671 modem = g_hash_table_lookup(modem_hash, path);
1675 if (modem->ignore == TRUE)
1678 if (dbus_message_iter_init(message, &iter) == FALSE)
1681 dbus_message_iter_get_basic(&iter, &key);
1683 dbus_message_iter_next(&iter);
1684 dbus_message_iter_recurse(&iter, &value);
1686 if (g_str_equal(key, "Attached") == TRUE)
1687 cm_update_attached(modem, &value);
1688 else if (g_str_equal(key, "Powered") == TRUE)
1689 cm_update_powered(modem, &value);
1694 static void cdma_cm_update_powered(struct modem_data *modem,
1695 DBusMessageIter *value)
1697 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1699 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1701 if (modem->network == NULL)
1704 if (modem->cdma_cm_powered == TRUE)
1705 set_connected(modem);
1707 set_disconnected(modem);
1710 static void cdma_cm_update_settings(struct modem_data *modem,
1711 DBusMessageIter *value)
1713 DBG("%s Settings", modem->path);
1715 extract_ipv4_settings(value, modem->context);
1718 static gboolean cdma_cm_changed(DBusConnection *connection,
1719 DBusMessage *message, void *user_data)
1721 const char *path = dbus_message_get_path(message);
1722 struct modem_data *modem;
1723 DBusMessageIter iter, value;
1726 modem = g_hash_table_lookup(modem_hash, path);
1730 if (modem->online == TRUE && modem->network == NULL)
1731 cdma_netreg_get_properties(modem);
1733 if (dbus_message_iter_init(message, &iter) == FALSE)
1736 dbus_message_iter_get_basic(&iter, &key);
1738 dbus_message_iter_next(&iter);
1739 dbus_message_iter_recurse(&iter, &value);
1741 if (g_str_equal(key, "Powered") == TRUE)
1742 cdma_cm_update_powered(modem, &value);
1743 if (g_str_equal(key, "Settings") == TRUE)
1744 cdma_cm_update_settings(modem, &value);
1749 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1751 DBG("%s", modem->path);
1753 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1754 DBusMessageIter entry, value;
1757 dbus_message_iter_recurse(dict, &entry);
1758 dbus_message_iter_get_basic(&entry, &key);
1760 dbus_message_iter_next(&entry);
1761 dbus_message_iter_recurse(&entry, &value);
1763 if (g_str_equal(key, "Attached") == TRUE)
1764 cm_update_attached(modem, &value);
1765 else if (g_str_equal(key, "Powered") == TRUE)
1766 cm_update_powered(modem, &value);
1768 dbus_message_iter_next(dict);
1772 static int cm_get_properties(struct modem_data *modem)
1774 return get_properties(modem->path, OFONO_CM_INTERFACE,
1775 cm_properties_reply, modem);
1778 static void cdma_cm_properties_reply(struct modem_data *modem,
1779 DBusMessageIter *dict)
1781 DBG("%s", modem->path);
1783 if (modem->online == TRUE)
1784 cdma_netreg_get_properties(modem);
1786 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1787 DBusMessageIter entry, value;
1790 dbus_message_iter_recurse(dict, &entry);
1791 dbus_message_iter_get_basic(&entry, &key);
1793 dbus_message_iter_next(&entry);
1794 dbus_message_iter_recurse(&entry, &value);
1796 if (g_str_equal(key, "Powered") == TRUE)
1797 cdma_cm_update_powered(modem, &value);
1798 if (g_str_equal(key, "Settings") == TRUE)
1799 cdma_cm_update_settings(modem, &value);
1801 dbus_message_iter_next(dict);
1805 static int cdma_cm_get_properties(struct modem_data *modem)
1807 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1808 cdma_cm_properties_reply, modem);
1811 static void sim_update_imsi(struct modem_data *modem,
1812 DBusMessageIter* value)
1816 dbus_message_iter_get_basic(value, &imsi);
1818 DBG("%s imsi %s", modem->path, imsi);
1820 g_free(modem->imsi);
1821 modem->imsi = g_strdup(imsi);
1824 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1827 const char *path = dbus_message_get_path(message);
1828 struct modem_data *modem;
1829 DBusMessageIter iter, value;
1832 modem = g_hash_table_lookup(modem_hash, path);
1836 if (modem->ignore == TRUE)
1839 if (dbus_message_iter_init(message, &iter) == FALSE)
1842 dbus_message_iter_get_basic(&iter, &key);
1844 dbus_message_iter_next(&iter);
1845 dbus_message_iter_recurse(&iter, &value);
1847 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1848 sim_update_imsi(modem, &value);
1850 if (ready_to_create_device(modem) == FALSE)
1854 * This is a GSM modem. Create the device and
1855 * register it at the core. Enabling (setting
1856 * it online is done through the
1857 * modem_enable() callback.
1859 create_device(modem);
1865 static void sim_properties_reply(struct modem_data *modem,
1866 DBusMessageIter *dict)
1868 DBG("%s", modem->path);
1870 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1871 DBusMessageIter entry, value;
1874 dbus_message_iter_recurse(dict, &entry);
1875 dbus_message_iter_get_basic(&entry, &key);
1877 dbus_message_iter_next(&entry);
1878 dbus_message_iter_recurse(&entry, &value);
1880 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1881 sim_update_imsi(modem, &value);
1883 if (ready_to_create_device(modem) == FALSE)
1887 * This is a GSM modem. Create the device and
1888 * register it at the core. Enabling (setting
1889 * it online is done through the
1890 * modem_enable() callback.
1892 create_device(modem);
1894 if (modem->online == FALSE)
1898 * The modem is already online and we have the CM interface.
1899 * There will be no interface update and therefore our
1900 * state machine will not go to next step. We have to
1901 * trigger it from here.
1903 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1904 cm_get_properties(modem);
1905 cm_get_contexts(modem);
1910 dbus_message_iter_next(dict);
1914 static int sim_get_properties(struct modem_data *modem)
1916 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1917 sim_properties_reply, modem);
1920 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1923 if (has_interface(old_iface, api) == FALSE &&
1924 has_interface(new_iface, api) == TRUE) {
1925 DBG("%s added", api2string(api));
1932 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1935 if (has_interface(old_iface, api) == TRUE &&
1936 has_interface(new_iface, api) == FALSE) {
1937 DBG("%s removed", api2string(api));
1944 static void modem_update_interfaces(struct modem_data *modem,
1948 DBG("%s", modem->path);
1950 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
1951 if (modem->imsi == NULL &&
1952 modem->set_powered == FALSE) {
1954 * Only use do GetProperties() when
1955 * device has not been powered up.
1957 sim_get_properties(modem);
1961 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1962 if (modem->device != NULL) {
1963 cm_get_properties(modem);
1964 cm_get_contexts(modem);
1968 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1969 if (ready_to_create_device(modem) == TRUE)
1970 create_device(modem);
1972 if (modem->device != NULL)
1973 cdma_cm_get_properties(modem);
1976 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1977 if (modem->attached == TRUE)
1978 netreg_get_properties(modem);
1981 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
1982 cdma_netreg_get_properties(modem);
1985 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1986 remove_cm_context(modem, modem->context->path);
1989 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1990 remove_cm_context(modem, modem->context->path);
1993 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1994 remove_network(modem);
1997 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
1998 remove_network(modem);
2002 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
2005 const char *path = dbus_message_get_path(message);
2006 struct modem_data *modem;
2007 DBusMessageIter iter, value;
2010 modem = g_hash_table_lookup(modem_hash, path);
2014 if (modem->ignore == TRUE)
2017 if (dbus_message_iter_init(message, &iter) == FALSE)
2020 dbus_message_iter_get_basic(&iter, &key);
2022 dbus_message_iter_next(&iter);
2023 dbus_message_iter_recurse(&iter, &value);
2025 if (g_str_equal(key, "Powered") == TRUE) {
2026 dbus_message_iter_get_basic(&value, &modem->powered);
2028 DBG("%s Powered %d", modem->path, modem->powered);
2030 if (modem->powered == FALSE)
2031 modem_set_powered(modem, TRUE);
2032 } else if (g_str_equal(key, "Online") == TRUE) {
2033 dbus_message_iter_get_basic(&value, &modem->online);
2035 DBG("%s Online %d", modem->path, modem->online);
2037 if (modem->device == NULL)
2040 connman_device_set_powered(modem->device, modem->online);
2041 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2044 interfaces = extract_interfaces(&value);
2046 if (interfaces == modem->interfaces)
2049 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2051 modem_update_interfaces(modem, modem->interfaces, interfaces);
2053 modem->interfaces = interfaces;
2054 } else if (g_str_equal(key, "Serial") == TRUE) {
2057 dbus_message_iter_get_basic(&value, &serial);
2059 g_free(modem->serial);
2060 modem->serial = g_strdup(serial);
2062 DBG("%s Serial %s", modem->path, modem->serial);
2064 if (has_interface(modem->interfaces,
2065 OFONO_API_CDMA_CM) == TRUE) {
2066 if (ready_to_create_device(modem) == TRUE)
2067 create_device(modem);
2074 static void add_modem(const char *path, DBusMessageIter *prop)
2076 struct modem_data *modem;
2080 modem = g_hash_table_lookup(modem_hash, path);
2081 if (modem != NULL) {
2083 * When oFono powers up we ask for the modems and oFono is
2084 * reporting with modem_added signal the modems. Only
2090 modem = g_try_new0(struct modem_data, 1);
2094 modem->path = g_strdup(path);
2096 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2098 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2099 DBusMessageIter entry, value;
2102 dbus_message_iter_recurse(prop, &entry);
2103 dbus_message_iter_get_basic(&entry, &key);
2105 dbus_message_iter_next(&entry);
2106 dbus_message_iter_recurse(&entry, &value);
2108 if (g_str_equal(key, "Powered") == TRUE) {
2109 dbus_message_iter_get_basic(&value, &modem->powered);
2111 DBG("%s Powered %d", modem->path, modem->powered);
2112 } else if (g_str_equal(key, "Online") == TRUE) {
2113 dbus_message_iter_get_basic(&value, &modem->online);
2115 DBG("%s Online %d", modem->path, modem->online);
2116 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2117 modem->interfaces = extract_interfaces(&value);
2119 DBG("%s Interfaces 0x%02x", modem->path,
2121 } else if (g_str_equal(key, "Serial") == TRUE) {
2124 dbus_message_iter_get_basic(&value, &serial);
2125 modem->serial = g_strdup(serial);
2127 DBG("%s Serial %s", modem->path, modem->serial);
2128 } else if (g_str_equal(key, "Type") == TRUE) {
2131 dbus_message_iter_get_basic(&value, &type);
2133 DBG("%s Type %s", modem->path, type);
2134 if (g_strcmp0(type, "hardware") != 0) {
2135 DBG("%s Ignore this modem", modem->path);
2136 modem->ignore = TRUE;
2140 dbus_message_iter_next(prop);
2143 if (modem->ignore == TRUE)
2146 if (modem->powered == FALSE) {
2147 modem_set_powered(modem, TRUE);
2151 modem_update_interfaces(modem, 0, modem->interfaces);
2154 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2156 struct modem_data *modem = value;
2158 DBG("%s", modem->path);
2160 if (modem->ignore == TRUE)
2163 modem_set_powered(modem, FALSE);
2166 static void remove_modem(gpointer data)
2168 struct modem_data *modem = data;
2170 DBG("%s", modem->path);
2172 if (modem->call_set_property != NULL)
2173 dbus_pending_call_cancel(modem->call_set_property);
2175 if (modem->call_get_properties != NULL)
2176 dbus_pending_call_cancel(modem->call_get_properties);
2178 if (modem->call_get_contexts != NULL)
2179 dbus_pending_call_cancel(modem->call_get_contexts);
2181 if (modem->device != NULL)
2182 destroy_device(modem);
2184 if (modem->context != NULL)
2185 remove_cm_context(modem, modem->context->path);
2187 g_free(modem->serial);
2188 g_free(modem->name);
2189 g_free(modem->imsi);
2190 g_free(modem->path);
2195 static gboolean modem_added(DBusConnection *connection,
2196 DBusMessage *message, void *user_data)
2198 DBusMessageIter iter, properties;
2203 if (dbus_message_iter_init(message, &iter) == FALSE)
2206 dbus_message_iter_get_basic(&iter, &path);
2208 dbus_message_iter_next(&iter);
2209 dbus_message_iter_recurse(&iter, &properties);
2211 add_modem(path, &properties);
2216 static gboolean modem_removed(DBusConnection *connection,
2217 DBusMessage *message, void *user_data)
2219 DBusMessageIter iter;
2224 if (dbus_message_iter_init(message, &iter) == FALSE)
2227 dbus_message_iter_get_basic(&iter, &path);
2229 g_hash_table_remove(modem_hash, path);
2234 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2238 DBusMessageIter array, dict;
2242 reply = dbus_pending_call_steal_reply(call);
2244 dbus_error_init(&error);
2246 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2247 connman_error("%s", error.message);
2248 dbus_error_free(&error);
2252 if (dbus_message_iter_init(reply, &array) == FALSE)
2255 dbus_message_iter_recurse(&array, &dict);
2257 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2258 DBusMessageIter value, properties;
2261 dbus_message_iter_recurse(&dict, &value);
2262 dbus_message_iter_get_basic(&value, &path);
2264 dbus_message_iter_next(&value);
2265 dbus_message_iter_recurse(&value, &properties);
2267 add_modem(path, &properties);
2269 dbus_message_iter_next(&dict);
2273 dbus_message_unref(reply);
2275 dbus_pending_call_unref(call);
2278 static int manager_get_modems(void)
2280 DBusMessage *message;
2281 DBusPendingCall *call;
2285 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2286 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2287 if (message == NULL)
2290 if (dbus_connection_send_with_reply(connection, message,
2291 &call, TIMEOUT) == FALSE) {
2292 connman_error("Failed to call GetModems()");
2293 dbus_message_unref(message);
2298 connman_error("D-Bus connection not available");
2299 dbus_message_unref(message);
2303 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2306 dbus_message_unref(message);
2308 return -EINPROGRESS;
2311 static void ofono_connect(DBusConnection *conn, void *user_data)
2315 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2316 g_free, remove_modem);
2317 if (modem_hash == NULL)
2320 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2322 if (context_hash == NULL) {
2323 g_hash_table_destroy(modem_hash);
2327 manager_get_modems();
2330 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2334 if (modem_hash == NULL || context_hash == NULL)
2337 g_hash_table_destroy(modem_hash);
2340 g_hash_table_destroy(context_hash);
2341 context_hash = NULL;
2344 static int network_probe(struct connman_network *network)
2346 struct modem_data *modem = connman_network_get_data(network);
2348 DBG("%s network %p", modem->path, network);
2353 static void network_remove(struct connman_network *network)
2355 struct modem_data *modem = connman_network_get_data(network);
2357 DBG("%s network %p", modem->path, network);
2360 static int network_connect(struct connman_network *network)
2362 struct modem_data *modem = connman_network_get_data(network);
2364 DBG("%s network %p", modem->path, network);
2366 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2367 return context_set_active(modem, TRUE);
2368 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2369 return cdma_cm_set_powered(modem, TRUE);
2371 connman_error("Connection manager interface not available");
2376 static int network_disconnect(struct connman_network *network)
2378 struct modem_data *modem = connman_network_get_data(network);
2380 DBG("%s network %p", modem->path, network);
2382 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2383 return context_set_active(modem, FALSE);
2384 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2385 return cdma_cm_set_powered(modem, FALSE);
2387 connman_error("Connection manager interface not available");
2392 static struct connman_network_driver network_driver = {
2394 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2395 .probe = network_probe,
2396 .remove = network_remove,
2397 .connect = network_connect,
2398 .disconnect = network_disconnect,
2401 static int modem_probe(struct connman_device *device)
2403 struct modem_data *modem = connman_device_get_data(device);
2405 DBG("%s device %p", modem->path, device);
2410 static void modem_remove(struct connman_device *device)
2412 struct modem_data *modem = connman_device_get_data(device);
2414 DBG("%s device %p", modem->path, device);
2417 static int modem_enable(struct connman_device *device)
2419 struct modem_data *modem = connman_device_get_data(device);
2421 DBG("%s device %p", modem->path, device);
2423 if (modem->online == TRUE)
2426 return modem_set_online(modem, TRUE);
2429 static int modem_disable(struct connman_device *device)
2431 struct modem_data *modem = connman_device_get_data(device);
2433 DBG("%s device %p", modem->path, device);
2435 if (modem->online == FALSE)
2438 return modem_set_online(modem, FALSE);
2441 static struct connman_device_driver modem_driver = {
2443 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2444 .probe = modem_probe,
2445 .remove = modem_remove,
2446 .enable = modem_enable,
2447 .disable = modem_disable,
2450 static int tech_probe(struct connman_technology *technology)
2455 static void tech_remove(struct connman_technology *technology)
2459 static struct connman_technology_driver tech_driver = {
2461 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2462 .probe = tech_probe,
2463 .remove = tech_remove,
2467 static guint modem_added_watch;
2468 static guint modem_removed_watch;
2469 static guint modem_watch;
2470 static guint cm_watch;
2471 static guint sim_watch;
2472 static guint context_added_watch;
2473 static guint context_removed_watch;
2474 static guint netreg_watch;
2475 static guint context_watch;
2476 static guint cdma_cm_watch;
2477 static guint cdma_netreg_watch;
2479 static int ofono_init(void)
2485 connection = connman_dbus_get_connection();
2486 if (connection == NULL)
2489 watch = g_dbus_add_service_watch(connection,
2490 OFONO_SERVICE, ofono_connect,
2491 ofono_disconnect, NULL, NULL);
2493 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2494 OFONO_MANAGER_INTERFACE,
2499 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2500 OFONO_MANAGER_INTERFACE,
2505 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2506 OFONO_MODEM_INTERFACE,
2511 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2517 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2518 OFONO_SIM_INTERFACE,
2523 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2529 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2535 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2536 OFONO_CONTEXT_INTERFACE,
2541 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2542 OFONO_NETREG_INTERFACE,
2547 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2548 OFONO_CDMA_CM_INTERFACE,
2553 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2554 OFONO_CDMA_NETREG_INTERFACE,
2556 cdma_netreg_changed,
2560 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2561 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2562 context_added_watch == 0 ||
2563 context_removed_watch == 0 ||
2564 context_watch == 0 || netreg_watch == 0 ||
2565 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2570 err = connman_network_driver_register(&network_driver);
2574 err = connman_device_driver_register(&modem_driver);
2576 connman_network_driver_unregister(&network_driver);
2580 err = connman_technology_driver_register(&tech_driver);
2582 connman_device_driver_unregister(&modem_driver);
2583 connman_network_driver_unregister(&network_driver);
2590 g_dbus_remove_watch(connection, cdma_netreg_watch);
2591 g_dbus_remove_watch(connection, cdma_cm_watch);
2592 g_dbus_remove_watch(connection, netreg_watch);
2593 g_dbus_remove_watch(connection, context_watch);
2594 g_dbus_remove_watch(connection, context_removed_watch);
2595 g_dbus_remove_watch(connection, context_added_watch);
2596 g_dbus_remove_watch(connection, sim_watch);
2597 g_dbus_remove_watch(connection, cm_watch);
2598 g_dbus_remove_watch(connection, modem_watch);
2599 g_dbus_remove_watch(connection, modem_removed_watch);
2600 g_dbus_remove_watch(connection, modem_added_watch);
2601 g_dbus_remove_watch(connection, watch);
2602 dbus_connection_unref(connection);
2607 static void ofono_exit(void)
2611 if (modem_hash != NULL) {
2613 * We should propably wait for the SetProperty() reply
2614 * message, because ...
2616 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2619 * ... here we will cancel the call.
2621 g_hash_table_destroy(modem_hash);
2625 if (context_hash != NULL) {
2626 g_hash_table_destroy(context_hash);
2627 context_hash = NULL;
2630 connman_technology_driver_unregister(&tech_driver);
2631 connman_device_driver_unregister(&modem_driver);
2632 connman_network_driver_unregister(&network_driver);
2634 g_dbus_remove_watch(connection, cdma_netreg_watch);
2635 g_dbus_remove_watch(connection, cdma_cm_watch);
2636 g_dbus_remove_watch(connection, netreg_watch);
2637 g_dbus_remove_watch(connection, context_watch);
2638 g_dbus_remove_watch(connection, context_removed_watch);
2639 g_dbus_remove_watch(connection, context_added_watch);
2640 g_dbus_remove_watch(connection, sim_watch);
2641 g_dbus_remove_watch(connection, cm_watch);
2642 g_dbus_remove_watch(connection, modem_watch);
2643 g_dbus_remove_watch(connection, modem_added_watch);
2644 g_dbus_remove_watch(connection, modem_removed_watch);
2645 g_dbus_remove_watch(connection, watch);
2647 dbus_connection_unref(connection);
2650 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2651 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)