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>
43 #define uninitialized_var(x) x = x
45 #define OFONO_SERVICE "org.ofono"
47 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
49 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
50 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
51 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
52 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
53 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
54 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
56 #define MODEM_ADDED "ModemAdded"
57 #define MODEM_REMOVED "ModemRemoved"
58 #define PROPERTY_CHANGED "PropertyChanged"
59 #define CONTEXT_ADDED "ContextAdded"
60 #define CONTEXT_REMOVED "ContextRemoved"
62 #define GET_PROPERTIES "GetProperties"
63 #define SET_PROPERTY "SetProperty"
64 #define GET_MODEMS "GetModems"
65 #define GET_CONTEXTS "GetContexts"
71 OFONO_API_NETREG = 0x2,
73 OFONO_API_CDMA_NETREG = 0x8,
74 OFONO_API_CDMA_CM = 0x10,
78 * The way this plugin works is following:
80 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
81 * attached -> netreg -> ready
83 * Enabling and disabling modems are steered through the rfkill
84 * interface. That means when ConnMan toggles the rfkill bit oFono
85 * will add or remove the modems.
87 * ConnMan will always power up (set Powered and Online) the
88 * modems. No need to power them down because this will be done
89 * through the rfkill inteface.
92 static DBusConnection *connection;
94 static GHashTable *modem_hash;
95 static GHashTable *context_hash;
97 struct network_context {
101 enum connman_ipconfig_method ipv4_method;
102 struct connman_ipaddress *ipv4_address;
103 char *ipv4_nameservers;
105 enum connman_ipconfig_method ipv6_method;
106 struct connman_ipaddress *ipv6_address;
107 char *ipv6_nameservers;
113 struct connman_device *device;
114 struct connman_network *network;
116 struct network_context *context;
118 /* Modem Interface */
120 connman_bool_t powered;
121 connman_bool_t online;
123 connman_bool_t ignore;
125 connman_bool_t set_powered;
126 connman_bool_t set_online;
128 /* CDMA ConnectionManager Interface */
129 connman_bool_t cdma_cm_powered;
131 /* ConnectionManager Interface */
132 connman_bool_t attached;
133 connman_bool_t cm_powered;
135 connman_bool_t set_cm_powered;
137 /* ConnectionContext Interface */
138 connman_bool_t active;
139 connman_bool_t set_active;
141 /* SimManager Interface */
144 /* Netreg Interface */
147 uint8_t data_strength; /* 1xEVDO signal strength */
148 connman_bool_t roaming;
151 DBusPendingCall *call_set_property;
152 DBusPendingCall *call_get_properties;
153 DBusPendingCall *call_get_contexts;
156 static char *get_ident(const char *path)
163 pos = strrchr(path, '/');
170 static struct network_context *network_context_alloc(const char *path)
172 struct network_context *context;
174 context = g_try_new0(struct network_context, 1);
178 context->path = g_strdup(path);
181 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
182 context->ipv4_address = NULL;
183 context->ipv4_nameservers = NULL;
185 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
186 context->ipv6_address = NULL;
187 context->ipv6_nameservers = NULL;
192 static void network_context_free(struct network_context *context)
194 g_free(context->path);
196 connman_ipaddress_free(context->ipv4_address);
197 g_free(context->ipv4_nameservers);
199 connman_ipaddress_free(context->ipv6_address);
200 g_free(context->ipv6_nameservers);
205 static void set_connected(struct modem_data *modem)
207 connman_bool_t setip = FALSE;
209 DBG("%s", modem->path);
211 connman_network_set_index(modem->network, modem->context->index);
213 switch (modem->context->ipv4_method) {
214 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
215 case CONNMAN_IPCONFIG_METHOD_OFF:
216 case CONNMAN_IPCONFIG_METHOD_MANUAL:
217 case CONNMAN_IPCONFIG_METHOD_AUTO:
220 case CONNMAN_IPCONFIG_METHOD_FIXED:
221 connman_network_set_ipv4_method(modem->network,
222 modem->context->ipv4_method);
223 connman_network_set_ipaddress(modem->network,
224 modem->context->ipv4_address);
225 connman_network_set_nameservers(modem->network,
226 modem->context->ipv4_nameservers);
230 case CONNMAN_IPCONFIG_METHOD_DHCP:
231 connman_network_set_ipv4_method(modem->network,
232 modem->context->ipv4_method);
237 switch (modem->context->ipv6_method) {
238 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
239 case CONNMAN_IPCONFIG_METHOD_OFF:
240 case CONNMAN_IPCONFIG_METHOD_MANUAL:
241 case CONNMAN_IPCONFIG_METHOD_DHCP:
242 case CONNMAN_IPCONFIG_METHOD_AUTO:
245 case CONNMAN_IPCONFIG_METHOD_FIXED:
246 connman_network_set_ipv6_method(modem->network,
247 modem->context->ipv6_method);
248 connman_network_set_ipaddress(modem->network,
249 modem->context->ipv6_address);
255 connman_network_set_connected(modem->network, TRUE);
258 static void set_disconnected(struct modem_data *modem)
260 DBG("%s", modem->path);
262 connman_network_set_connected(modem->network, FALSE);
265 typedef void (*set_property_cb)(struct modem_data *data,
266 connman_bool_t success);
267 typedef void (*get_properties_cb)(struct modem_data *data,
268 DBusMessageIter *dict);
270 struct property_info {
271 struct modem_data *modem;
273 const char *interface;
274 const char *property;
275 set_property_cb set_property_cb;
276 get_properties_cb get_properties_cb;
279 static void set_property_reply(DBusPendingCall *call, void *user_data)
281 struct property_info *info = user_data;
284 connman_bool_t success = TRUE;
286 DBG("%s path %s %s.%s", info->modem->path,
287 info->path, info->interface, info->property);
289 info->modem->call_set_property = NULL;
291 dbus_error_init(&error);
293 reply = dbus_pending_call_steal_reply(call);
295 if (dbus_set_error_from_message(&error, reply)) {
296 connman_error("Failed to change property: %s %s.%s: %s %s",
297 info->path, info->interface, info->property,
298 error.name, error.message);
299 dbus_error_free(&error);
303 if (info->set_property_cb != NULL)
304 (*info->set_property_cb)(info->modem, success);
306 dbus_message_unref(reply);
308 dbus_pending_call_unref(call);
311 static int set_property(struct modem_data *modem,
312 const char *path, const char *interface,
313 const char *property, int type, void *value,
314 set_property_cb notify)
316 DBusMessage *message;
317 DBusMessageIter iter;
318 struct property_info *info;
320 DBG("%s path %s %s.%s", modem->path, path, interface, property);
322 if (modem->call_set_property != NULL) {
323 connman_error("Pending SetProperty");
327 message = dbus_message_new_method_call(OFONO_SERVICE, path,
328 interface, SET_PROPERTY);
332 dbus_message_iter_init_append(message, &iter);
333 connman_dbus_property_append_basic(&iter, property, type, value);
335 if (dbus_connection_send_with_reply(connection, message,
336 &modem->call_set_property, TIMEOUT) == FALSE) {
337 connman_error("Failed to change property: %s %s.%s",
338 path, interface, property);
339 dbus_message_unref(message);
343 if (modem->call_set_property == NULL) {
344 connman_error("D-Bus connection not available");
345 dbus_message_unref(message);
349 info = g_try_new0(struct property_info, 1);
351 dbus_message_unref(message);
357 info->interface = interface;
358 info->property = property;
359 info->set_property_cb = notify;
361 dbus_pending_call_set_notify(modem->call_set_property,
362 set_property_reply, info, g_free);
364 dbus_message_unref(message);
369 static void get_properties_reply(DBusPendingCall *call, void *user_data)
371 struct property_info *info = user_data;
372 DBusMessageIter array, dict;
376 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
378 info->modem->call_get_properties = NULL;
380 dbus_error_init(&error);
382 reply = dbus_pending_call_steal_reply(call);
384 if (dbus_set_error_from_message(&error, reply)) {
385 connman_error("Failed to get properties: %s %s: %s %s",
386 info->path, info->interface,
387 error.name, error.message);
388 dbus_error_free(&error);
393 if (dbus_message_iter_init(reply, &array) == FALSE)
396 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
399 dbus_message_iter_recurse(&array, &dict);
401 if (info->get_properties_cb != NULL)
402 (*info->get_properties_cb)(info->modem, &dict);
406 dbus_message_unref(reply);
408 dbus_pending_call_unref(call);
411 static int get_properties(const char *path, const char *interface,
412 get_properties_cb notify,
413 struct modem_data *modem)
415 DBusMessage *message;
416 struct property_info *info;
418 DBG("%s path %s %s", modem->path, path, interface);
420 if (modem->call_get_properties != NULL) {
421 connman_error("Pending GetProperties");
425 message = dbus_message_new_method_call(OFONO_SERVICE, path,
426 interface, GET_PROPERTIES);
430 if (dbus_connection_send_with_reply(connection, message,
431 &modem->call_get_properties, TIMEOUT) == FALSE) {
432 connman_error("Failed to call %s.GetProperties()", interface);
433 dbus_message_unref(message);
437 if (modem->call_get_properties == NULL) {
438 connman_error("D-Bus connection not available");
439 dbus_message_unref(message);
443 info = g_try_new0(struct property_info, 1);
445 dbus_message_unref(message);
451 info->interface = interface;
452 info->get_properties_cb = notify;
454 dbus_pending_call_set_notify(modem->call_get_properties,
455 get_properties_reply, info, g_free);
457 dbus_message_unref(message);
462 static void context_set_active_reply(struct modem_data *modem,
463 connman_bool_t success)
465 DBG("%s", modem->path);
467 if (success == TRUE) {
469 * Don't handle do anything on success here. oFono will send
470 * the change via PropertyChanged singal.
476 * Active = True might fail due a timeout. That means oFono
477 * still tries to go online. If we retry to set Active = True,
478 * we just get a InProgress error message. Should we power
479 * cycle the modem in such cases?
482 connman_network_set_error(modem->network,
483 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
486 static int context_set_active(struct modem_data *modem)
488 dbus_bool_t active = TRUE;
490 DBG("%s", modem->path);
492 return set_property(modem, modem->context->path,
493 OFONO_CONTEXT_INTERFACE,
494 "Active", DBUS_TYPE_BOOLEAN,
496 context_set_active_reply);
499 static int context_set_inactive(struct modem_data *modem)
501 dbus_bool_t active = FALSE;
504 DBG("%s", modem->path);
506 err = set_property(modem, modem->context->path,
507 OFONO_CONTEXT_INTERFACE,
508 "Active", DBUS_TYPE_BOOLEAN,
511 if (err == -EINPROGRESS)
517 static void cdma_cm_set_powered_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 * Powered = True might fail due a timeout. That means oFono
532 * still tries to go online. If we retry to set Powered = True,
533 * we just get a InProgress error message. Should we power
534 * cycle the modem in such cases?
537 connman_network_set_error(modem->network,
538 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
541 static int cdma_cm_set_powered(struct modem_data *modem)
543 dbus_bool_t powered = TRUE;
545 DBG("%s", modem->path);
547 return set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
548 "Powered", DBUS_TYPE_BOOLEAN, &powered,
549 cdma_cm_set_powered_reply);
552 static int cdma_cm_shutdown(struct modem_data *modem)
554 dbus_bool_t powered = FALSE;
557 DBG("%s", modem->path);
559 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
560 "Powered", DBUS_TYPE_BOOLEAN, &powered, NULL);
561 if (err == -EINPROGRESS)
567 static void modem_set_online_reply(struct modem_data *modem,
568 connman_bool_t success)
570 DBG("%s", modem->path);
572 if (success == TRUE) {
574 * Don't handle do anything on success here. oFono will send
575 * the change via PropertyChanged singal.
580 modem->set_online = FALSE;
583 static int modem_set_online(struct modem_data *modem)
585 DBG("%s", modem->path);
587 modem->set_online = TRUE;
589 return set_property(modem, modem->path,
590 OFONO_MODEM_INTERFACE,
591 "Online", DBUS_TYPE_BOOLEAN,
593 modem_set_online_reply);
596 static void cm_set_powered_reply(struct modem_data *modem,
597 connman_bool_t success)
599 DBG("%s", modem->path);
601 if (success == TRUE) {
603 * Don't handle do anything on success here. oFono will send
604 * the change via PropertyChanged singal.
609 modem->set_cm_powered = FALSE;
612 static int cm_set_powered(struct modem_data *modem)
614 DBG("%s", modem->path);
616 modem->set_cm_powered = TRUE;
618 return set_property(modem, modem->path,
620 "Powered", DBUS_TYPE_BOOLEAN,
621 &modem->set_cm_powered,
622 cm_set_powered_reply);
625 static int modem_set_powered(struct modem_data *modem)
627 DBG("%s", modem->path);
629 modem->set_powered = TRUE;
631 return set_property(modem, modem->path,
632 OFONO_MODEM_INTERFACE,
633 "Powered", DBUS_TYPE_BOOLEAN,
638 static int modem_set_unpowered(struct modem_data *modem)
640 DBG("%s", modem->path);
642 modem->set_powered = FALSE;
644 return set_property(modem, modem->path,
645 OFONO_MODEM_INTERFACE,
646 "Powered", DBUS_TYPE_BOOLEAN,
651 static connman_bool_t has_interface(uint8_t interfaces,
654 if ((interfaces & api) == api)
660 static uint8_t extract_interfaces(DBusMessageIter *array)
662 DBusMessageIter entry;
663 uint8_t interfaces = 0;
665 dbus_message_iter_recurse(array, &entry);
667 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
670 dbus_message_iter_get_basic(&entry, &name);
672 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
673 interfaces |= OFONO_API_SIM;
674 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
675 interfaces |= OFONO_API_NETREG;
676 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
677 interfaces |= OFONO_API_CM;
678 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
679 interfaces |= OFONO_API_CDMA_CM;
680 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
681 interfaces |= OFONO_API_CDMA_NETREG;
683 dbus_message_iter_next(&entry);
689 static char *extract_nameservers(DBusMessageIter *array)
691 DBusMessageIter entry;
692 char *nameservers = NULL;
695 dbus_message_iter_recurse(array, &entry);
697 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
698 const char *nameserver;
700 dbus_message_iter_get_basic(&entry, &nameserver);
702 if (nameservers == NULL) {
703 nameservers = g_strdup(nameserver);
706 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
710 dbus_message_iter_next(&entry);
716 static void extract_ipv4_settings(DBusMessageIter *array,
717 struct network_context *context)
719 DBusMessageIter dict;
720 char *address = NULL, *netmask = NULL, *gateway = NULL;
721 char *nameservers = NULL;
722 const char *interface = NULL;
725 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
728 dbus_message_iter_recurse(array, &dict);
730 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
731 DBusMessageIter entry, value;
732 const char *key, *val;
734 dbus_message_iter_recurse(&dict, &entry);
735 dbus_message_iter_get_basic(&entry, &key);
737 dbus_message_iter_next(&entry);
738 dbus_message_iter_recurse(&entry, &value);
740 if (g_str_equal(key, "Interface") == TRUE) {
741 dbus_message_iter_get_basic(&value, &interface);
743 DBG("Interface %s", interface);
745 index = connman_inet_ifindex(interface);
747 DBG("index %d", index);
748 } else if (g_str_equal(key, "Method") == TRUE) {
749 dbus_message_iter_get_basic(&value, &val);
751 DBG("Method %s", val);
753 if (g_strcmp0(val, "static") == 0) {
754 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
755 } else if (g_strcmp0(val, "dhcp") == 0) {
756 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
759 } else if (g_str_equal(key, "Address") == TRUE) {
760 dbus_message_iter_get_basic(&value, &val);
762 address = g_strdup(val);
764 DBG("Address %s", address);
765 } else if (g_str_equal(key, "Netmask") == TRUE) {
766 dbus_message_iter_get_basic(&value, &val);
768 netmask = g_strdup(val);
770 DBG("Netmask %s", netmask);
771 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
772 nameservers = extract_nameservers(&value);
774 DBG("Nameservers %s", nameservers);
775 } else if (g_str_equal(key, "Gateway") == TRUE) {
776 dbus_message_iter_get_basic(&value, &val);
778 gateway = g_strdup(val);
780 DBG("Gateway %s", gateway);
783 dbus_message_iter_next(&dict);
789 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
792 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
793 if (context->ipv4_address == NULL)
796 context->index = index;
797 connman_ipaddress_set_ipv4(context->ipv4_address, address,
800 context->ipv4_nameservers = nameservers;
803 if (context->ipv4_nameservers != nameservers)
811 static void extract_ipv6_settings(DBusMessageIter *array,
812 struct network_context *context)
814 DBusMessageIter dict;
815 char *address = NULL, *gateway = NULL;
816 unsigned char prefix_length;
817 char *nameservers = NULL;
818 const char *interface = NULL;
821 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
824 dbus_message_iter_recurse(array, &dict);
826 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
827 DBusMessageIter entry, value;
828 const char *key, *val;
830 dbus_message_iter_recurse(&dict, &entry);
831 dbus_message_iter_get_basic(&entry, &key);
833 dbus_message_iter_next(&entry);
834 dbus_message_iter_recurse(&entry, &value);
836 if (g_str_equal(key, "Interface") == TRUE) {
837 dbus_message_iter_get_basic(&value, &interface);
839 DBG("Interface %s", interface);
841 index = connman_inet_ifindex(interface);
843 DBG("index %d", index);
844 } else if (g_str_equal(key, "Address") == TRUE) {
845 dbus_message_iter_get_basic(&value, &val);
847 address = g_strdup(val);
849 DBG("Address %s", address);
850 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
851 dbus_message_iter_get_basic(&value, &prefix_length);
853 DBG("prefix length %d", prefix_length);
854 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
855 nameservers = extract_nameservers(&value);
857 DBG("Nameservers %s", nameservers);
858 } else if (g_str_equal(key, "Gateway") == TRUE) {
859 dbus_message_iter_get_basic(&value, &val);
861 gateway = g_strdup(val);
863 DBG("Gateway %s", gateway);
866 dbus_message_iter_next(&dict);
872 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
874 context->ipv6_address =
875 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
876 if (context->ipv6_address == NULL)
879 context->index = index;
880 connman_ipaddress_set_ipv6(context->ipv6_address, address,
881 prefix_length, gateway);
883 context->ipv6_nameservers = nameservers;
886 if (context->ipv6_nameservers != nameservers)
893 static connman_bool_t ready_to_create_device(struct modem_data *modem)
896 * There are three different modem types which behave slightly
898 * - GSM modems will expose the SIM interface then the
900 * - DUN modems will expose first a unique serial number (BDADDR)
901 * and then the CM interface.
902 * - CDMA modems will expose CM first and sometime later
903 * a unique serial number.
905 * This functions tests if we have the necessary information gathered
906 * before we are able to create a device.
909 if (modem->device != NULL)
912 if (modem->imsi != NULL || modem->serial != NULL)
918 static void create_device(struct modem_data *modem)
920 struct connman_device *device;
921 char *uninitialized_var(ident);
923 DBG("%s", modem->path);
925 if (modem->imsi != NULL)
927 else if (modem->serial != NULL)
928 ident = modem->serial;
930 if (connman_dbus_validate_ident(ident) == FALSE)
931 ident = connman_dbus_encode_string(ident);
933 ident = g_strdup(ident);
935 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
939 DBG("device %p", device);
941 connman_device_set_ident(device, ident);
943 connman_device_set_string(device, "Path", modem->path);
945 connman_device_set_data(device, modem);
947 if (connman_device_register(device) < 0) {
948 connman_error("Failed to register cellular device");
949 connman_device_unref(device);
953 modem->device = device;
959 static void destroy_device(struct modem_data *modem)
961 DBG("%s", modem->path);
963 connman_device_set_powered(modem->device, FALSE);
965 if (modem->network != NULL) {
966 connman_device_remove_network(modem->device, modem->network);
967 connman_network_unref(modem->network);
968 modem->network = NULL;
971 connman_device_unregister(modem->device);
972 connman_device_unref(modem->device);
974 modem->device = NULL;
977 static void add_network(struct modem_data *modem)
981 DBG("%s", modem->path);
983 if (modem->network != NULL)
986 modem->network = connman_network_create(modem->context->path,
987 CONNMAN_NETWORK_TYPE_CELLULAR);
988 if (modem->network == NULL)
991 DBG("network %p", modem->network);
993 connman_network_set_data(modem->network, modem);
995 connman_network_set_string(modem->network, "Path",
996 modem->context->path);
998 connman_network_set_index(modem->network, modem->context->index);
1000 if (modem->name != NULL)
1001 connman_network_set_name(modem->network, modem->name);
1003 connman_network_set_name(modem->network, "");
1005 connman_network_set_strength(modem->network, modem->strength);
1007 group = get_ident(modem->context->path);
1008 connman_network_set_group(modem->network, group);
1010 connman_network_set_available(modem->network, TRUE);
1012 connman_network_set_bool(modem->network, "Roaming",
1015 if (connman_device_add_network(modem->device, modem->network) < 0) {
1016 connman_network_unref(modem->network);
1017 modem->network = NULL;
1022 static int add_cm_context(struct modem_data *modem, const char *context_path,
1023 DBusMessageIter *dict)
1025 const char *context_type;
1026 struct network_context *context = NULL;
1027 connman_bool_t active = FALSE;
1029 DBG("%s context path %s", modem->path, context_path);
1031 if (modem->context != NULL) {
1033 * We have already assigned a context to this modem
1034 * and we do only support one Internet context.
1039 context = network_context_alloc(context_path);
1040 if (context == NULL)
1043 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1044 DBusMessageIter entry, value;
1047 dbus_message_iter_recurse(dict, &entry);
1048 dbus_message_iter_get_basic(&entry, &key);
1050 dbus_message_iter_next(&entry);
1051 dbus_message_iter_recurse(&entry, &value);
1053 if (g_str_equal(key, "Type") == TRUE) {
1054 dbus_message_iter_get_basic(&value, &context_type);
1056 DBG("%s context %s type %s", modem->path,
1057 context_path, context_type);
1058 } else if (g_str_equal(key, "Settings") == TRUE) {
1059 DBG("%s Settings", modem->path);
1061 extract_ipv4_settings(&value, context);
1062 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1063 DBG("%s IPv6.Settings", modem->path);
1065 extract_ipv6_settings(&value, context);
1066 } else if (g_str_equal(key, "Active") == TRUE) {
1067 dbus_message_iter_get_basic(&value, &active);
1069 DBG("%s Active %d", modem->path, active);
1072 dbus_message_iter_next(dict);
1075 if (g_strcmp0(context_type, "internet") != 0) {
1076 network_context_free(context);
1080 modem->context = context;
1081 modem->active = active;
1083 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1088 static void remove_cm_context(struct modem_data *modem,
1089 const char *context_path)
1091 if (modem->context == NULL)
1094 g_hash_table_remove(context_hash, context_path);
1096 network_context_free(modem->context);
1097 modem->context = NULL;
1100 static gboolean context_changed(DBusConnection *connection,
1101 DBusMessage *message,
1104 const char *context_path = dbus_message_get_path(message);
1105 struct modem_data *modem = NULL;
1106 DBusMessageIter iter, value;
1109 DBG("context_path %s", context_path);
1111 modem = g_hash_table_lookup(context_hash, context_path);
1115 if (dbus_message_iter_init(message, &iter) == FALSE)
1118 dbus_message_iter_get_basic(&iter, &key);
1120 dbus_message_iter_next(&iter);
1121 dbus_message_iter_recurse(&iter, &value);
1124 * oFono guarantees the ordering of Settings and
1125 * Active. Settings will always be send before Active = True.
1126 * That means we don't have to order here.
1128 if (g_str_equal(key, "Settings") == TRUE) {
1129 DBG("%s Settings", modem->path);
1131 extract_ipv4_settings(&value, modem->context);
1132 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1133 DBG("%s IPv6.Settings", modem->path);
1135 extract_ipv6_settings(&value, modem->context);
1136 } else if (g_str_equal(key, "Active") == TRUE) {
1137 dbus_message_iter_get_basic(&value, &modem->active);
1139 DBG("%s Active %d", modem->path, modem->active);
1141 if (modem->active == TRUE)
1142 set_connected(modem);
1144 set_disconnected(modem);
1150 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1152 struct modem_data *modem = user_data;
1153 DBusMessageIter array, dict, entry, value;
1157 DBG("%s", modem->path);
1159 modem->call_get_contexts = NULL;
1161 reply = dbus_pending_call_steal_reply(call);
1163 dbus_error_init(&error);
1165 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1166 connman_error("%s", error.message);
1167 dbus_error_free(&error);
1171 if (dbus_message_iter_init(reply, &array) == FALSE)
1174 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1177 dbus_message_iter_recurse(&array, &dict);
1179 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1180 const char *context_path;
1182 dbus_message_iter_recurse(&dict, &entry);
1183 dbus_message_iter_get_basic(&entry, &context_path);
1185 dbus_message_iter_next(&entry);
1186 dbus_message_iter_recurse(&entry, &value);
1188 if (add_cm_context(modem, context_path, &value) == 0)
1191 dbus_message_iter_next(&dict);
1195 dbus_message_unref(reply);
1197 dbus_pending_call_unref(call);
1200 static int cm_get_contexts(struct modem_data *modem)
1202 DBusMessage *message;
1204 DBG("%s", modem->path);
1206 if (modem->call_get_contexts != NULL)
1209 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1210 OFONO_CM_INTERFACE, GET_CONTEXTS);
1211 if (message == NULL)
1214 if (dbus_connection_send_with_reply(connection, message,
1215 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1216 connman_error("Failed to call GetContexts()");
1217 dbus_message_unref(message);
1221 if (modem->call_get_contexts == NULL) {
1222 connman_error("D-Bus connection not available");
1223 dbus_message_unref(message);
1227 dbus_pending_call_set_notify(modem->call_get_contexts,
1228 cm_get_contexts_reply,
1231 dbus_message_unref(message);
1233 return -EINPROGRESS;
1236 static gboolean cm_context_added(DBusConnection *connection,
1237 DBusMessage *message,
1240 const char *path = dbus_message_get_path(message);
1242 struct modem_data *modem;
1243 DBusMessageIter iter, properties;
1247 modem = g_hash_table_lookup(modem_hash, context_path);
1251 if (dbus_message_iter_init(message, &iter) == FALSE)
1254 dbus_message_iter_get_basic(&iter, &context_path);
1256 dbus_message_iter_next(&iter);
1257 dbus_message_iter_recurse(&iter, &properties);
1259 if (add_cm_context(modem, context_path, &properties) != 0)
1265 static gboolean cm_context_removed(DBusConnection *connection,
1266 DBusMessage *message,
1269 const char *path = dbus_message_get_path(message);
1270 const char *context_path;
1271 struct modem_data *modem;
1272 DBusMessageIter iter;
1274 DBG("context path %s", path);
1276 if (dbus_message_iter_init(message, &iter) == FALSE)
1279 dbus_message_iter_get_basic(&iter, &context_path);
1281 modem = g_hash_table_lookup(context_hash, context_path);
1285 remove_cm_context(modem, context_path);
1290 static void netreg_update_name(struct modem_data *modem,
1291 DBusMessageIter* value)
1295 dbus_message_iter_get_basic(value, &name);
1297 DBG("%s Name %s", modem->path, name);
1299 g_free(modem->name);
1300 modem->name = g_strdup(name);
1302 if (modem->network == NULL)
1305 connman_network_set_name(modem->network, modem->name);
1306 connman_network_update(modem->network);
1309 static void netreg_update_strength(struct modem_data *modem,
1310 DBusMessageIter *value)
1312 dbus_message_iter_get_basic(value, &modem->strength);
1314 DBG("%s Strength %d", modem->path, modem->strength);
1316 if (modem->network == NULL)
1321 * We don't have 2 signal notifications we always report the strength
1322 * signal. data_strength is always equal to 0.
1325 * In the case we have a data_strength signal (from 1xEVDO network)
1326 * we don't need to update the value with strength signal (from 1xCDMA)
1327 * because the modem is registered to 1xEVDO network for data call.
1328 * In case we have no data_strength signal (not registered to 1xEVDO
1329 * network), we must report the strength signal (registered to 1xCDMA
1330 * network e.g slow mode).
1332 if (modem->data_strength != 0)
1335 connman_network_set_strength(modem->network, modem->strength);
1336 connman_network_update(modem->network);
1339 /* Retrieve 1xEVDO Data Strength signal */
1340 static void netreg_update_datastrength(struct modem_data *modem,
1341 DBusMessageIter *value)
1343 dbus_message_iter_get_basic(value, &modem->data_strength);
1345 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1347 if (modem->network == NULL)
1351 * CDMA modem is not registered to 1xEVDO network, let
1352 * update_signal_strength() reporting the value on the Strength signal
1355 if (modem->data_strength == 0)
1358 connman_network_set_strength(modem->network, modem->data_strength);
1359 connman_network_update(modem->network);
1362 static void netreg_update_roaming(struct modem_data *modem,
1363 DBusMessageIter *value)
1366 connman_bool_t roaming;
1368 dbus_message_iter_get_basic(value, &status);
1370 if (g_str_equal(status, "roaming") == TRUE)
1375 if (roaming == modem->roaming)
1378 modem->roaming = roaming;
1380 if (modem->network == NULL)
1383 connman_network_set_bool(modem->network,
1384 "Roaming", modem->roaming);
1385 connman_network_update(modem->network);
1388 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1391 const char *path = dbus_message_get_path(message);
1392 struct modem_data *modem;
1393 DBusMessageIter iter, value;
1396 modem = g_hash_table_lookup(modem_hash, path);
1400 if (modem->ignore == TRUE)
1403 if (dbus_message_iter_init(message, &iter) == FALSE)
1406 dbus_message_iter_get_basic(&iter, &key);
1408 dbus_message_iter_next(&iter);
1409 dbus_message_iter_recurse(&iter, &value);
1411 if (g_str_equal(key, "Name") == TRUE)
1412 netreg_update_name(modem, &value);
1413 else if (g_str_equal(key, "Strength") == TRUE)
1414 netreg_update_strength(modem, &value);
1415 else if (g_str_equal(key, "Status") == TRUE)
1416 netreg_update_roaming(modem, &value);
1421 static void netreg_properties_reply(struct modem_data *modem,
1422 DBusMessageIter *dict)
1424 DBG("%s", modem->path);
1426 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1427 DBusMessageIter entry, value;
1430 dbus_message_iter_recurse(dict, &entry);
1431 dbus_message_iter_get_basic(&entry, &key);
1433 dbus_message_iter_next(&entry);
1434 dbus_message_iter_recurse(&entry, &value);
1436 if (g_str_equal(key, "Name") == TRUE)
1437 netreg_update_name(modem, &value);
1438 else if (g_str_equal(key, "Strength") == TRUE)
1439 netreg_update_strength(modem, &value);
1440 else if (g_str_equal(key, "Status") == TRUE)
1441 netreg_update_roaming(modem, &value);
1443 dbus_message_iter_next(dict);
1446 if (modem->context == NULL) {
1448 * netgreg_get_properties() was issued after we got
1449 * cm_get_contexts_reply() where we create the
1450 * context. Though before we got the
1451 * netreg_properties_reply the context was removed
1452 * again. Therefore we have to skip the network
1460 if (modem->active == TRUE)
1461 set_connected(modem);
1464 static int netreg_get_properties(struct modem_data *modem)
1466 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1467 netreg_properties_reply, modem);
1470 static void add_cdma_network(struct modem_data *modem)
1472 /* Be sure that device is created before adding CDMA network */
1473 if (modem->device == NULL)
1477 * CDMA modems don't need contexts for data call, however the current
1478 * add_network() logic needs one, so we create one to proceed.
1480 if (modem->context == NULL)
1481 modem->context = network_context_alloc(modem->path);
1485 if (modem->cdma_cm_powered == TRUE)
1486 set_connected(modem);
1489 static gboolean cdma_netreg_changed(DBusConnection *connection,
1490 DBusMessage *message,
1493 const char *path = dbus_message_get_path(message);
1494 struct modem_data *modem;
1495 DBusMessageIter iter, value;
1500 modem = g_hash_table_lookup(modem_hash, path);
1504 if (modem->ignore == TRUE)
1507 if (dbus_message_iter_init(message, &iter) == FALSE)
1510 dbus_message_iter_get_basic(&iter, &key);
1512 dbus_message_iter_next(&iter);
1513 dbus_message_iter_recurse(&iter, &value);
1515 if (g_str_equal(key, "Name") == TRUE)
1516 netreg_update_name(modem, &value);
1517 else if (g_str_equal(key, "Strength") == TRUE)
1518 netreg_update_strength(modem, &value);
1519 else if (g_str_equal(key, "DataStrength") == TRUE)
1520 netreg_update_datastrength(modem, &value);
1521 else if (g_str_equal(key, "Status") == TRUE)
1522 netreg_update_roaming(modem, &value);
1524 add_cdma_network(modem);
1529 static void cdma_netreg_properties_reply(struct modem_data *modem,
1530 DBusMessageIter *dict)
1532 DBG("%s", modem->path);
1534 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1535 DBusMessageIter entry, value;
1538 dbus_message_iter_recurse(dict, &entry);
1539 dbus_message_iter_get_basic(&entry, &key);
1541 dbus_message_iter_next(&entry);
1542 dbus_message_iter_recurse(&entry, &value);
1544 if (g_str_equal(key, "Name") == TRUE)
1545 netreg_update_name(modem, &value);
1546 else if (g_str_equal(key, "Strength") == TRUE)
1547 netreg_update_strength(modem, &value);
1548 else if (g_str_equal(key, "DataStrength") == TRUE)
1549 netreg_update_datastrength(modem, &value);
1550 else if (g_str_equal(key, "Status") == TRUE)
1551 netreg_update_roaming(modem, &value);
1553 dbus_message_iter_next(dict);
1556 add_cdma_network(modem);
1559 static int cdma_netreg_get_properties(struct modem_data *modem)
1561 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1562 cdma_netreg_properties_reply, modem);
1565 static void cm_update_attached(struct modem_data *modem,
1566 DBusMessageIter *value)
1568 dbus_message_iter_get_basic(value, &modem->attached);
1570 DBG("%s Attached %d", modem->path, modem->attached);
1572 if (modem->attached == FALSE)
1575 if (has_interface(modem->interfaces,
1576 OFONO_API_NETREG) == FALSE) {
1580 netreg_get_properties(modem);
1583 static void cm_update_powered(struct modem_data *modem,
1584 DBusMessageIter *value)
1586 dbus_message_iter_get_basic(value, &modem->cm_powered);
1588 DBG("%s ConnnectionManager Powered %d", modem->path,
1591 if (modem->cm_powered == TRUE)
1594 cm_set_powered(modem);
1597 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1600 const char *path = dbus_message_get_path(message);
1601 struct modem_data *modem;
1602 DBusMessageIter iter, value;
1605 modem = g_hash_table_lookup(modem_hash, path);
1609 if (modem->ignore == TRUE)
1612 if (dbus_message_iter_init(message, &iter) == FALSE)
1615 dbus_message_iter_get_basic(&iter, &key);
1617 dbus_message_iter_next(&iter);
1618 dbus_message_iter_recurse(&iter, &value);
1620 if (g_str_equal(key, "Attached") == TRUE)
1621 cm_update_attached(modem, &value);
1622 else if (g_str_equal(key, "Powered") == TRUE)
1623 cm_update_powered(modem, &value);
1628 static void cdma_cm_update_powered(struct modem_data *modem,
1629 DBusMessageIter *value)
1631 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1633 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1635 if (modem->network == NULL)
1638 if (modem->cdma_cm_powered == TRUE)
1639 set_connected(modem);
1641 set_disconnected(modem);
1644 static void cdma_cm_update_settings(struct modem_data *modem,
1645 DBusMessageIter *value)
1647 DBG("%s Settings", modem->path);
1649 if (modem->context != NULL)
1652 extract_ipv4_settings(value, modem->context);
1655 static gboolean cdma_cm_changed(DBusConnection *connection,
1656 DBusMessage *message, void *user_data)
1658 const char *path = dbus_message_get_path(message);
1659 struct modem_data *modem;
1660 DBusMessageIter iter, value;
1663 modem = g_hash_table_lookup(modem_hash, path);
1667 if (modem->online == TRUE && modem->network == NULL)
1668 cdma_netreg_get_properties(modem);
1670 if (dbus_message_iter_init(message, &iter) == FALSE)
1673 dbus_message_iter_get_basic(&iter, &key);
1675 dbus_message_iter_next(&iter);
1676 dbus_message_iter_recurse(&iter, &value);
1678 if (g_str_equal(key, "Powered") == TRUE)
1679 cdma_cm_update_powered(modem, &value);
1680 if (g_str_equal(key, "Settings") == TRUE)
1681 cdma_cm_update_settings(modem, &value);
1686 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1688 DBG("%s", modem->path);
1690 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1691 DBusMessageIter entry, value;
1694 dbus_message_iter_recurse(dict, &entry);
1695 dbus_message_iter_get_basic(&entry, &key);
1697 dbus_message_iter_next(&entry);
1698 dbus_message_iter_recurse(&entry, &value);
1700 if (g_str_equal(key, "Attached") == TRUE)
1701 cm_update_attached(modem, &value);
1702 else if (g_str_equal(key, "Powered") == TRUE)
1703 cm_update_powered(modem, &value);
1705 dbus_message_iter_next(dict);
1709 static int cm_get_properties(struct modem_data *modem)
1711 return get_properties(modem->path, OFONO_CM_INTERFACE,
1712 cm_properties_reply, modem);
1715 static void cdma_cm_properties_reply(struct modem_data *modem,
1716 DBusMessageIter *dict)
1718 DBG("%s", modem->path);
1720 if (modem->online == TRUE)
1721 cdma_netreg_get_properties(modem);
1723 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1724 DBusMessageIter entry, value;
1727 dbus_message_iter_recurse(dict, &entry);
1728 dbus_message_iter_get_basic(&entry, &key);
1730 dbus_message_iter_next(&entry);
1731 dbus_message_iter_recurse(&entry, &value);
1733 if (g_str_equal(key, "Powered") == TRUE)
1734 cdma_cm_update_powered(modem, &value);
1735 if (g_str_equal(key, "Settings") == TRUE)
1736 cdma_cm_update_settings(modem, &value);
1738 dbus_message_iter_next(dict);
1742 static int cdma_cm_get_properties(struct modem_data *modem)
1744 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1745 cdma_cm_properties_reply, modem);
1748 static int connection_manager_init(struct modem_data *modem)
1750 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1751 if (ready_to_create_device(modem) == TRUE)
1752 create_device(modem);
1754 if (modem->device != NULL) {
1755 cm_get_properties(modem);
1756 cm_get_contexts(modem);
1762 if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE) {
1763 if (ready_to_create_device(modem) == TRUE)
1764 create_device(modem);
1766 if (modem->device != NULL)
1767 cdma_cm_get_properties(modem);
1775 static void update_sim_imsi(struct modem_data *modem,
1778 DBG("%s imsi %s", modem->path, imsi);
1780 if (g_strcmp0(modem->imsi, imsi) == 0)
1783 g_free(modem->imsi);
1784 modem->imsi = g_strdup(imsi);
1787 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1790 const char *path = dbus_message_get_path(message);
1791 struct modem_data *modem;
1792 DBusMessageIter iter, value;
1795 modem = g_hash_table_lookup(modem_hash, path);
1799 if (modem->ignore == TRUE)
1802 if (dbus_message_iter_init(message, &iter) == FALSE)
1805 dbus_message_iter_get_basic(&iter, &key);
1807 dbus_message_iter_next(&iter);
1808 dbus_message_iter_recurse(&iter, &value);
1810 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1813 dbus_message_iter_get_basic(&value, &imsi);
1815 update_sim_imsi(modem, imsi);
1817 if (modem->online == FALSE) {
1818 modem_set_online(modem);
1819 } else if (has_interface(modem->interfaces,
1820 OFONO_API_CM) == TRUE) {
1821 if (ready_to_create_device(modem) == TRUE)
1822 create_device(modem);
1823 } else if (has_interface(modem->interfaces,
1824 OFONO_API_CDMA_CM) == TRUE) {
1825 if (ready_to_create_device(modem) == TRUE)
1826 create_device(modem);
1833 static void sim_properties_reply(struct modem_data *modem,
1834 DBusMessageIter *dict)
1836 DBG("%s", modem->path);
1838 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1839 DBusMessageIter entry, value;
1842 dbus_message_iter_recurse(dict, &entry);
1843 dbus_message_iter_get_basic(&entry, &key);
1845 dbus_message_iter_next(&entry);
1846 dbus_message_iter_recurse(&entry, &value);
1848 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1851 dbus_message_iter_get_basic(&value, &imsi);
1853 update_sim_imsi(modem, imsi);
1855 if (modem->online == FALSE) {
1856 modem_set_online(modem);
1860 connection_manager_init(modem);
1865 dbus_message_iter_next(dict);
1869 static int sim_get_properties(struct modem_data *modem)
1871 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1872 sim_properties_reply, modem);
1875 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1878 const char *path = dbus_message_get_path(message);
1879 struct modem_data *modem;
1880 DBusMessageIter iter, value;
1883 modem = g_hash_table_lookup(modem_hash, path);
1887 if (modem->ignore == TRUE)
1890 if (dbus_message_iter_init(message, &iter) == FALSE)
1893 dbus_message_iter_get_basic(&iter, &key);
1895 dbus_message_iter_next(&iter);
1896 dbus_message_iter_recurse(&iter, &value);
1898 if (g_str_equal(key, "Powered") == TRUE) {
1899 dbus_message_iter_get_basic(&value, &modem->powered);
1901 DBG("%s Powered %d", modem->path, modem->powered);
1903 if (modem->powered == FALSE)
1904 modem_set_powered(modem);
1905 } else if (g_str_equal(key, "Online") == TRUE) {
1906 dbus_message_iter_get_basic(&value, &modem->online);
1908 DBG("%s Online %d", modem->path, modem->online);
1910 if (modem->online == FALSE)
1913 connection_manager_init(modem);
1914 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1915 modem->interfaces = extract_interfaces(&value);
1917 DBG("%s Interfaces 0x%02x", modem->path,
1920 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1921 if (modem->imsi == NULL &&
1922 modem->set_powered == FALSE) {
1924 * Only use do GetProperties() when
1925 * device has not been powered up.
1927 sim_get_properties(modem);
1932 if (connection_manager_init(modem) == FALSE) {
1933 if (modem->context != NULL) {
1934 remove_cm_context(modem,
1935 modem->context->path);
1938 if (modem->device != NULL)
1939 destroy_device(modem);
1944 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1945 if (modem->attached == TRUE)
1946 netreg_get_properties(modem);
1947 } else if (has_interface(modem->interfaces,
1948 OFONO_API_CDMA_NETREG) == TRUE)
1949 cdma_netreg_get_properties(modem);
1950 } else if (g_str_equal(key, "Serial") == TRUE) {
1953 dbus_message_iter_get_basic(&value, &serial);
1955 g_free(modem->serial);
1956 modem->serial = g_strdup(serial);
1958 DBG("%s Serial %s", modem->path, modem->serial);
1960 connection_manager_init(modem);
1966 static void add_modem(const char *path, DBusMessageIter *prop)
1968 struct modem_data *modem;
1972 modem = g_hash_table_lookup(modem_hash, path);
1973 if (modem != NULL) {
1975 * When oFono powers up we ask for the modems and oFono is
1976 * reporting with modem_added signal the modems. Only
1982 modem = g_try_new0(struct modem_data, 1);
1986 modem->path = g_strdup(path);
1988 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1990 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1991 DBusMessageIter entry, value;
1994 dbus_message_iter_recurse(prop, &entry);
1995 dbus_message_iter_get_basic(&entry, &key);
1997 dbus_message_iter_next(&entry);
1998 dbus_message_iter_recurse(&entry, &value);
2000 if (g_str_equal(key, "Powered") == TRUE) {
2001 dbus_message_iter_get_basic(&value, &modem->powered);
2003 DBG("%s Powered %d", modem->path, modem->powered);
2004 } else if (g_str_equal(key, "Online") == TRUE) {
2005 dbus_message_iter_get_basic(&value, &modem->online);
2007 DBG("%s Online %d", modem->path, modem->online);
2008 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2009 modem->interfaces = extract_interfaces(&value);
2011 DBG("%s Interfaces 0x%02x", modem->path,
2013 } else if (g_str_equal(key, "Serial") == TRUE) {
2016 dbus_message_iter_get_basic(&value, &serial);
2017 modem->serial = g_strdup(serial);
2019 DBG("%s Serial %s", modem->path, modem->serial);
2020 } else if (g_str_equal(key, "Type") == TRUE) {
2023 dbus_message_iter_get_basic(&value, &type);
2025 DBG("%s Type %s", modem->path, type);
2026 if (g_strcmp0(type, "hardware") != 0) {
2027 DBG("%s Ignore this modem", modem->path);
2028 modem->ignore = TRUE;
2032 dbus_message_iter_next(prop);
2035 if (modem->ignore == TRUE)
2038 if (modem->powered == FALSE)
2039 modem_set_powered(modem);
2040 else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE)
2041 sim_get_properties(modem);
2043 connection_manager_init(modem);
2046 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2048 struct modem_data *modem = value;
2050 DBG("%s", modem->path);
2052 if (modem->ignore == TRUE)
2055 modem_set_unpowered(modem);
2058 static void remove_modem(gpointer data)
2060 struct modem_data *modem = data;
2062 DBG("%s", modem->path);
2064 if (modem->call_set_property != NULL)
2065 dbus_pending_call_cancel(modem->call_set_property);
2067 if (modem->call_get_properties != NULL)
2068 dbus_pending_call_cancel(modem->call_get_properties);
2070 if (modem->call_get_contexts != NULL)
2071 dbus_pending_call_cancel(modem->call_get_contexts);
2073 if (modem->device != NULL)
2074 destroy_device(modem);
2076 if (modem->context != NULL)
2077 remove_cm_context(modem, modem->context->path);
2079 g_free(modem->serial);
2080 g_free(modem->name);
2081 g_free(modem->imsi);
2082 g_free(modem->path);
2087 static gboolean modem_added(DBusConnection *connection,
2088 DBusMessage *message, void *user_data)
2090 DBusMessageIter iter, properties;
2095 if (dbus_message_iter_init(message, &iter) == FALSE)
2098 dbus_message_iter_get_basic(&iter, &path);
2100 dbus_message_iter_next(&iter);
2101 dbus_message_iter_recurse(&iter, &properties);
2103 add_modem(path, &properties);
2108 static gboolean modem_removed(DBusConnection *connection,
2109 DBusMessage *message, void *user_data)
2111 DBusMessageIter iter;
2116 if (dbus_message_iter_init(message, &iter) == FALSE)
2119 dbus_message_iter_get_basic(&iter, &path);
2121 g_hash_table_remove(modem_hash, path);
2126 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2130 DBusMessageIter array, dict;
2134 reply = dbus_pending_call_steal_reply(call);
2136 dbus_error_init(&error);
2138 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2139 connman_error("%s", error.message);
2140 dbus_error_free(&error);
2144 if (dbus_message_iter_init(reply, &array) == FALSE)
2147 dbus_message_iter_recurse(&array, &dict);
2149 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2150 DBusMessageIter value, properties;
2153 dbus_message_iter_recurse(&dict, &value);
2154 dbus_message_iter_get_basic(&value, &path);
2156 dbus_message_iter_next(&value);
2157 dbus_message_iter_recurse(&value, &properties);
2159 add_modem(path, &properties);
2161 dbus_message_iter_next(&dict);
2165 dbus_message_unref(reply);
2167 dbus_pending_call_unref(call);
2170 static int manager_get_modems(void)
2172 DBusMessage *message;
2173 DBusPendingCall *call;
2177 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2178 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2179 if (message == NULL)
2182 if (dbus_connection_send_with_reply(connection, message,
2183 &call, TIMEOUT) == FALSE) {
2184 connman_error("Failed to call GetModems()");
2185 dbus_message_unref(message);
2190 connman_error("D-Bus connection not available");
2191 dbus_message_unref(message);
2195 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2198 dbus_message_unref(message);
2200 return -EINPROGRESS;
2203 static void ofono_connect(DBusConnection *conn, void *user_data)
2207 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2208 g_free, remove_modem);
2209 if (modem_hash == NULL)
2212 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2214 if (context_hash == NULL) {
2215 g_hash_table_destroy(modem_hash);
2219 manager_get_modems();
2222 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2226 if (modem_hash == NULL || context_hash == NULL)
2229 g_hash_table_destroy(modem_hash);
2232 g_hash_table_destroy(context_hash);
2233 context_hash = NULL;
2236 static int network_probe(struct connman_network *network)
2238 struct modem_data *modem = connman_network_get_data(network);
2240 DBG("%s network %p", modem->path, network);
2245 static void network_remove(struct connman_network *network)
2247 struct modem_data *modem = connman_network_get_data(network);
2249 DBG("%s network %p", modem->path, network);
2252 static int network_connect(struct connman_network *network)
2254 struct modem_data *modem = connman_network_get_data(network);
2256 DBG("%s network %p", modem->path, network);
2258 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2259 return context_set_active(modem);
2260 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2261 return cdma_cm_set_powered(modem);
2263 connman_error("Connection manager interface not available");
2268 static int network_disconnect(struct connman_network *network)
2270 struct modem_data *modem = connman_network_get_data(network);
2272 DBG("%s network %p", modem->path, network);
2274 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2275 return context_set_inactive(modem);
2276 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2277 return cdma_cm_shutdown(modem);
2279 connman_error("Connection manager interface not available");
2284 static struct connman_network_driver network_driver = {
2286 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2287 .probe = network_probe,
2288 .remove = network_remove,
2289 .connect = network_connect,
2290 .disconnect = network_disconnect,
2293 static int modem_probe(struct connman_device *device)
2295 struct modem_data *modem = connman_device_get_data(device);
2297 DBG("%s device %p", modem->path, device);
2302 static void modem_remove(struct connman_device *device)
2304 struct modem_data *modem = connman_device_get_data(device);
2306 DBG("%s device %p", modem->path, device);
2309 static int modem_enable(struct connman_device *device)
2311 struct modem_data *modem = connman_device_get_data(device);
2313 DBG("%s device %p", modem->path, device);
2318 static int modem_disable(struct connman_device *device)
2320 struct modem_data *modem = connman_device_get_data(device);
2322 DBG("%s device %p", modem->path, device);
2327 static struct connman_device_driver modem_driver = {
2329 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2330 .probe = modem_probe,
2331 .remove = modem_remove,
2332 .enable = modem_enable,
2333 .disable = modem_disable,
2337 static guint modem_added_watch;
2338 static guint modem_removed_watch;
2339 static guint modem_watch;
2340 static guint cm_watch;
2341 static guint sim_watch;
2342 static guint context_added_watch;
2343 static guint context_removed_watch;
2344 static guint netreg_watch;
2345 static guint context_watch;
2346 static guint cdma_cm_watch;
2347 static guint cdma_netreg_watch;
2349 static int ofono_init(void)
2355 connection = connman_dbus_get_connection();
2356 if (connection == NULL)
2359 watch = g_dbus_add_service_watch(connection,
2360 OFONO_SERVICE, ofono_connect,
2361 ofono_disconnect, NULL, NULL);
2363 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2364 OFONO_MANAGER_INTERFACE,
2369 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2370 OFONO_MANAGER_INTERFACE,
2375 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2376 OFONO_MODEM_INTERFACE,
2381 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2387 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2388 OFONO_SIM_INTERFACE,
2393 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2399 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2405 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2406 OFONO_CONTEXT_INTERFACE,
2411 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2412 OFONO_NETREG_INTERFACE,
2417 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2418 OFONO_CDMA_CM_INTERFACE,
2423 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2424 OFONO_CDMA_NETREG_INTERFACE,
2426 cdma_netreg_changed,
2430 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2431 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2432 context_added_watch == 0 ||
2433 context_removed_watch == 0 ||
2434 context_watch == 0 || netreg_watch == 0 ||
2435 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2440 err = connman_network_driver_register(&network_driver);
2444 err = connman_device_driver_register(&modem_driver);
2446 connman_network_driver_unregister(&network_driver);
2453 g_dbus_remove_watch(connection, cdma_netreg_watch);
2454 g_dbus_remove_watch(connection, cdma_cm_watch);
2455 g_dbus_remove_watch(connection, netreg_watch);
2456 g_dbus_remove_watch(connection, context_watch);
2457 g_dbus_remove_watch(connection, context_removed_watch);
2458 g_dbus_remove_watch(connection, context_added_watch);
2459 g_dbus_remove_watch(connection, sim_watch);
2460 g_dbus_remove_watch(connection, cm_watch);
2461 g_dbus_remove_watch(connection, modem_watch);
2462 g_dbus_remove_watch(connection, modem_removed_watch);
2463 g_dbus_remove_watch(connection, modem_added_watch);
2464 g_dbus_remove_watch(connection, watch);
2465 dbus_connection_unref(connection);
2470 static void ofono_exit(void)
2474 if (modem_hash != NULL) {
2476 * We should propably wait for the SetProperty() reply
2477 * message, because ...
2479 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2482 * ... here we will cancel the call.
2484 g_hash_table_destroy(modem_hash);
2488 if (context_hash != NULL) {
2489 g_hash_table_destroy(context_hash);
2490 context_hash = NULL;
2493 connman_device_driver_unregister(&modem_driver);
2494 connman_network_driver_unregister(&network_driver);
2496 g_dbus_remove_watch(connection, cdma_netreg_watch);
2497 g_dbus_remove_watch(connection, cdma_cm_watch);
2498 g_dbus_remove_watch(connection, netreg_watch);
2499 g_dbus_remove_watch(connection, context_watch);
2500 g_dbus_remove_watch(connection, context_removed_watch);
2501 g_dbus_remove_watch(connection, context_added_watch);
2502 g_dbus_remove_watch(connection, sim_watch);
2503 g_dbus_remove_watch(connection, cm_watch);
2504 g_dbus_remove_watch(connection, modem_watch);
2505 g_dbus_remove_watch(connection, modem_added_watch);
2506 g_dbus_remove_watch(connection, modem_removed_watch);
2507 g_dbus_remove_watch(connection, watch);
2509 dbus_connection_unref(connection);
2512 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2513 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)