5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011 BWM Car IT GmbH. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42 #include <connman/technology.h>
46 #define 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;
175 connman_bool_t valid_apn; /* APN is 'valid' if length > 0 */
177 /* SimManager Interface */
180 /* Netreg Interface */
183 uint8_t data_strength; /* 1xEVDO signal strength */
184 connman_bool_t roaming;
187 DBusPendingCall *call_set_property;
188 DBusPendingCall *call_get_properties;
189 DBusPendingCall *call_get_contexts;
192 static const char *api2string(enum ofono_api api)
197 case OFONO_API_NETREG:
201 case OFONO_API_CDMA_NETREG:
202 return "cdma-netreg";
203 case OFONO_API_CDMA_CM:
210 static char *get_ident(const char *path)
217 pos = strrchr(path, '/');
224 static struct network_context *network_context_alloc(const char *path)
226 struct network_context *context;
228 context = g_try_new0(struct network_context, 1);
232 context->path = g_strdup(path);
235 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
236 context->ipv4_address = NULL;
237 context->ipv4_nameservers = NULL;
239 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
240 context->ipv6_address = NULL;
241 context->ipv6_nameservers = NULL;
246 static void network_context_free(struct network_context *context)
248 g_free(context->path);
250 connman_ipaddress_free(context->ipv4_address);
251 g_free(context->ipv4_nameservers);
253 connman_ipaddress_free(context->ipv6_address);
254 g_free(context->ipv6_nameservers);
259 static void set_connected(struct modem_data *modem)
261 connman_bool_t setip = FALSE;
263 DBG("%s", modem->path);
265 connman_network_set_index(modem->network, modem->context->index);
267 switch (modem->context->ipv4_method) {
268 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
269 case CONNMAN_IPCONFIG_METHOD_OFF:
270 case CONNMAN_IPCONFIG_METHOD_MANUAL:
271 case CONNMAN_IPCONFIG_METHOD_AUTO:
274 case CONNMAN_IPCONFIG_METHOD_FIXED:
275 connman_network_set_ipv4_method(modem->network,
276 modem->context->ipv4_method);
277 connman_network_set_ipaddress(modem->network,
278 modem->context->ipv4_address);
279 connman_network_set_nameservers(modem->network,
280 modem->context->ipv4_nameservers);
284 case CONNMAN_IPCONFIG_METHOD_DHCP:
285 connman_network_set_ipv4_method(modem->network,
286 modem->context->ipv4_method);
291 switch (modem->context->ipv6_method) {
292 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
293 case CONNMAN_IPCONFIG_METHOD_OFF:
294 case CONNMAN_IPCONFIG_METHOD_MANUAL:
295 case CONNMAN_IPCONFIG_METHOD_DHCP:
296 case CONNMAN_IPCONFIG_METHOD_AUTO:
299 case CONNMAN_IPCONFIG_METHOD_FIXED:
300 connman_network_set_ipv6_method(modem->network,
301 modem->context->ipv6_method);
302 connman_network_set_ipaddress(modem->network,
303 modem->context->ipv6_address);
309 connman_network_set_connected(modem->network, TRUE);
312 static void set_disconnected(struct modem_data *modem)
314 DBG("%s", modem->path);
316 if (modem->network == NULL)
319 connman_network_set_connected(modem->network, FALSE);
322 typedef void (*set_property_cb)(struct modem_data *data,
323 connman_bool_t success);
324 typedef void (*get_properties_cb)(struct modem_data *data,
325 DBusMessageIter *dict);
327 struct property_info {
328 struct modem_data *modem;
330 const char *interface;
331 const char *property;
332 set_property_cb set_property_cb;
333 get_properties_cb get_properties_cb;
336 static void set_property_reply(DBusPendingCall *call, void *user_data)
338 struct property_info *info = user_data;
341 connman_bool_t success = TRUE;
343 DBG("%s path %s %s.%s", info->modem->path,
344 info->path, info->interface, info->property);
346 info->modem->call_set_property = NULL;
348 dbus_error_init(&error);
350 reply = dbus_pending_call_steal_reply(call);
352 if (dbus_set_error_from_message(&error, reply)) {
353 connman_error("Failed to change property: %s %s.%s: %s %s",
354 info->path, info->interface, info->property,
355 error.name, error.message);
356 dbus_error_free(&error);
360 if (info->set_property_cb != NULL)
361 (*info->set_property_cb)(info->modem, success);
363 dbus_message_unref(reply);
365 dbus_pending_call_unref(call);
368 static int set_property(struct modem_data *modem,
369 const char *path, const char *interface,
370 const char *property, int type, void *value,
371 set_property_cb notify)
373 DBusMessage *message;
374 DBusMessageIter iter;
375 struct property_info *info;
377 DBG("%s path %s %s.%s", modem->path, path, interface, property);
379 if (modem->call_set_property != NULL) {
380 DBG("Cancel pending SetProperty");
382 dbus_pending_call_cancel(modem->call_set_property);
383 modem->call_set_property = NULL;
386 message = dbus_message_new_method_call(OFONO_SERVICE, path,
387 interface, SET_PROPERTY);
391 dbus_message_iter_init_append(message, &iter);
392 connman_dbus_property_append_basic(&iter, property, type, value);
394 if (dbus_connection_send_with_reply(connection, message,
395 &modem->call_set_property, TIMEOUT) == FALSE) {
396 connman_error("Failed to change property: %s %s.%s",
397 path, interface, property);
398 dbus_message_unref(message);
402 if (modem->call_set_property == NULL) {
403 connman_error("D-Bus connection not available");
404 dbus_message_unref(message);
408 info = g_try_new0(struct property_info, 1);
410 dbus_message_unref(message);
416 info->interface = interface;
417 info->property = property;
418 info->set_property_cb = notify;
420 dbus_pending_call_set_notify(modem->call_set_property,
421 set_property_reply, info, g_free);
423 dbus_message_unref(message);
428 static void get_properties_reply(DBusPendingCall *call, void *user_data)
430 struct property_info *info = user_data;
431 DBusMessageIter array, dict;
435 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
437 info->modem->call_get_properties = NULL;
439 dbus_error_init(&error);
441 reply = dbus_pending_call_steal_reply(call);
443 if (dbus_set_error_from_message(&error, reply)) {
444 connman_error("Failed to get properties: %s %s: %s %s",
445 info->path, info->interface,
446 error.name, error.message);
447 dbus_error_free(&error);
452 if (dbus_message_iter_init(reply, &array) == FALSE)
455 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
458 dbus_message_iter_recurse(&array, &dict);
460 if (info->get_properties_cb != NULL)
461 (*info->get_properties_cb)(info->modem, &dict);
465 dbus_message_unref(reply);
467 dbus_pending_call_unref(call);
470 static int get_properties(const char *path, const char *interface,
471 get_properties_cb notify,
472 struct modem_data *modem)
474 DBusMessage *message;
475 struct property_info *info;
477 DBG("%s path %s %s", modem->path, path, interface);
479 if (modem->call_get_properties != NULL) {
480 connman_error("Pending GetProperties");
484 message = dbus_message_new_method_call(OFONO_SERVICE, path,
485 interface, GET_PROPERTIES);
489 if (dbus_connection_send_with_reply(connection, message,
490 &modem->call_get_properties, TIMEOUT) == FALSE) {
491 connman_error("Failed to call %s.GetProperties()", interface);
492 dbus_message_unref(message);
496 if (modem->call_get_properties == NULL) {
497 connman_error("D-Bus connection not available");
498 dbus_message_unref(message);
502 info = g_try_new0(struct property_info, 1);
504 dbus_message_unref(message);
510 info->interface = interface;
511 info->get_properties_cb = notify;
513 dbus_pending_call_set_notify(modem->call_get_properties,
514 get_properties_reply, info, g_free);
516 dbus_message_unref(message);
521 static void context_set_active_reply(struct modem_data *modem,
522 connman_bool_t success)
524 DBG("%s", modem->path);
526 if (success == TRUE) {
528 * Don't handle do anything on success here. oFono will send
529 * the change via PropertyChanged singal.
535 * Active = True might fail due a timeout. That means oFono
536 * still tries to go online. If we retry to set Active = True,
537 * we just get a InProgress error message. Should we power
538 * cycle the modem in such cases?
541 if (modem->network == NULL) {
543 * In the case where we power down the device
544 * we don't wait for the reply, therefore the network
545 * might already be gone.
550 connman_network_set_error(modem->network,
551 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
554 static int context_set_active(struct modem_data *modem,
555 connman_bool_t active)
559 DBG("%s active %d", modem->path, active);
561 err = set_property(modem, modem->context->path,
562 OFONO_CONTEXT_INTERFACE,
563 "Active", DBUS_TYPE_BOOLEAN,
565 context_set_active_reply);
567 if (active == FALSE && err == -EINPROGRESS)
573 static void cdma_cm_set_powered_reply(struct modem_data *modem,
574 connman_bool_t success)
576 DBG("%s", modem->path);
578 if (success == TRUE) {
580 * Don't handle do anything on success here. oFono will send
581 * the change via PropertyChanged singal.
587 * Powered = True might fail due a timeout. That means oFono
588 * still tries to go online. If we retry to set Powered = True,
589 * we just get a InProgress error message. Should we power
590 * cycle the modem in such cases?
593 if (modem->network == NULL) {
595 * In the case where we power down the device
596 * we don't wait for the reply, therefore the network
597 * might already be gone.
602 connman_network_set_error(modem->network,
603 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
606 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
610 DBG("%s powered %d", modem->path, powered);
612 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
613 "Powered", DBUS_TYPE_BOOLEAN,
615 cdma_cm_set_powered_reply);
617 if (powered == FALSE && err == -EINPROGRESS)
623 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
625 DBG("%s online %d", modem->path, online);
627 return set_property(modem, modem->path,
628 OFONO_MODEM_INTERFACE,
629 "Online", DBUS_TYPE_BOOLEAN,
634 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
638 DBG("%s powered %d", modem->path, powered);
640 err = set_property(modem, modem->path,
642 "Powered", DBUS_TYPE_BOOLEAN,
646 if (powered == FALSE && err == -EINPROGRESS)
652 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
656 DBG("%s powered %d", modem->path, powered);
658 modem->set_powered = powered;
660 err = set_property(modem, modem->path,
661 OFONO_MODEM_INTERFACE,
662 "Powered", DBUS_TYPE_BOOLEAN,
666 if (powered == FALSE && err == -EINPROGRESS)
672 static connman_bool_t has_interface(uint8_t interfaces,
675 if ((interfaces & api) == api)
681 static uint8_t extract_interfaces(DBusMessageIter *array)
683 DBusMessageIter entry;
684 uint8_t interfaces = 0;
686 dbus_message_iter_recurse(array, &entry);
688 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
691 dbus_message_iter_get_basic(&entry, &name);
693 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
694 interfaces |= OFONO_API_SIM;
695 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
696 interfaces |= OFONO_API_NETREG;
697 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
698 interfaces |= OFONO_API_CM;
699 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
700 interfaces |= OFONO_API_CDMA_CM;
701 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
702 interfaces |= OFONO_API_CDMA_NETREG;
704 dbus_message_iter_next(&entry);
710 static char *extract_nameservers(DBusMessageIter *array)
712 DBusMessageIter entry;
713 char *nameservers = NULL;
716 dbus_message_iter_recurse(array, &entry);
718 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
719 const char *nameserver;
721 dbus_message_iter_get_basic(&entry, &nameserver);
723 if (nameservers == NULL) {
724 nameservers = g_strdup(nameserver);
727 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
731 dbus_message_iter_next(&entry);
737 static void extract_ipv4_settings(DBusMessageIter *array,
738 struct network_context *context)
740 DBusMessageIter dict;
741 char *address = NULL, *netmask = NULL, *gateway = NULL;
742 char *nameservers = NULL;
743 const char *interface = NULL;
746 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
749 dbus_message_iter_recurse(array, &dict);
751 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
752 DBusMessageIter entry, value;
753 const char *key, *val;
755 dbus_message_iter_recurse(&dict, &entry);
756 dbus_message_iter_get_basic(&entry, &key);
758 dbus_message_iter_next(&entry);
759 dbus_message_iter_recurse(&entry, &value);
761 if (g_str_equal(key, "Interface") == TRUE) {
762 dbus_message_iter_get_basic(&value, &interface);
764 DBG("Interface %s", interface);
766 index = connman_inet_ifindex(interface);
768 DBG("index %d", index);
769 } else if (g_str_equal(key, "Method") == TRUE) {
770 dbus_message_iter_get_basic(&value, &val);
772 DBG("Method %s", val);
774 if (g_strcmp0(val, "static") == 0) {
775 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
776 } else if (g_strcmp0(val, "dhcp") == 0) {
777 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
780 } else if (g_str_equal(key, "Address") == TRUE) {
781 dbus_message_iter_get_basic(&value, &val);
783 address = g_strdup(val);
785 DBG("Address %s", address);
786 } else if (g_str_equal(key, "Netmask") == TRUE) {
787 dbus_message_iter_get_basic(&value, &val);
789 netmask = g_strdup(val);
791 DBG("Netmask %s", netmask);
792 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
793 nameservers = extract_nameservers(&value);
795 DBG("Nameservers %s", nameservers);
796 } else if (g_str_equal(key, "Gateway") == TRUE) {
797 dbus_message_iter_get_basic(&value, &val);
799 gateway = g_strdup(val);
801 DBG("Gateway %s", gateway);
804 dbus_message_iter_next(&dict);
810 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
813 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
814 if (context->ipv4_address == NULL)
817 context->index = index;
818 connman_ipaddress_set_ipv4(context->ipv4_address, address,
821 context->ipv4_nameservers = nameservers;
824 if (context->ipv4_nameservers != nameservers)
832 static void extract_ipv6_settings(DBusMessageIter *array,
833 struct network_context *context)
835 DBusMessageIter dict;
836 char *address = NULL, *gateway = NULL;
837 unsigned char prefix_length;
838 char *nameservers = NULL;
839 const char *interface = NULL;
842 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
845 dbus_message_iter_recurse(array, &dict);
847 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
848 DBusMessageIter entry, value;
849 const char *key, *val;
851 dbus_message_iter_recurse(&dict, &entry);
852 dbus_message_iter_get_basic(&entry, &key);
854 dbus_message_iter_next(&entry);
855 dbus_message_iter_recurse(&entry, &value);
857 if (g_str_equal(key, "Interface") == TRUE) {
858 dbus_message_iter_get_basic(&value, &interface);
860 DBG("Interface %s", interface);
862 index = connman_inet_ifindex(interface);
864 DBG("index %d", index);
865 } else if (g_str_equal(key, "Address") == TRUE) {
866 dbus_message_iter_get_basic(&value, &val);
868 address = g_strdup(val);
870 DBG("Address %s", address);
871 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
872 dbus_message_iter_get_basic(&value, &prefix_length);
874 DBG("prefix length %d", prefix_length);
875 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
876 nameservers = extract_nameservers(&value);
878 DBG("Nameservers %s", nameservers);
879 } else if (g_str_equal(key, "Gateway") == TRUE) {
880 dbus_message_iter_get_basic(&value, &val);
882 gateway = g_strdup(val);
884 DBG("Gateway %s", gateway);
887 dbus_message_iter_next(&dict);
893 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
895 context->ipv6_address =
896 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
897 if (context->ipv6_address == NULL)
900 context->index = index;
901 connman_ipaddress_set_ipv6(context->ipv6_address, address,
902 prefix_length, gateway);
904 context->ipv6_nameservers = nameservers;
907 if (context->ipv6_nameservers != nameservers)
914 static connman_bool_t ready_to_create_device(struct modem_data *modem)
917 * There are three different modem types which behave slightly
919 * - GSM modems will expose the SIM interface then the
921 * - CDMA modems will expose CM first and sometime later
922 * a unique serial number.
924 * This functions tests if we have the necessary information gathered
925 * before we are able to create a device.
928 if (modem->device != NULL)
931 if (modem->imsi != NULL || modem->serial != NULL)
937 static void create_device(struct modem_data *modem)
939 struct connman_device *device;
940 char *uninitialized_var(ident);
942 DBG("%s", modem->path);
944 if (modem->imsi != NULL)
946 else if (modem->serial != NULL)
947 ident = modem->serial;
949 if (connman_dbus_validate_ident(ident) == FALSE)
950 ident = connman_dbus_encode_string(ident);
952 ident = g_strdup(ident);
954 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
958 DBG("device %p", device);
960 connman_device_set_ident(device, ident);
962 connman_device_set_string(device, "Path", modem->path);
964 connman_device_set_data(device, modem);
966 if (connman_device_register(device) < 0) {
967 connman_error("Failed to register cellular device");
968 connman_device_unref(device);
972 modem->device = device;
974 connman_device_set_powered(modem->device, modem->online);
979 static void destroy_device(struct modem_data *modem)
981 DBG("%s", modem->path);
983 connman_device_set_powered(modem->device, FALSE);
985 if (modem->network != NULL) {
986 connman_device_remove_network(modem->device, modem->network);
987 connman_network_unref(modem->network);
988 modem->network = NULL;
991 connman_device_unregister(modem->device);
992 connman_device_unref(modem->device);
994 modem->device = NULL;
997 static void add_network(struct modem_data *modem)
1001 DBG("%s", modem->path);
1003 if (modem->network != NULL)
1006 modem->network = connman_network_create(modem->context->path,
1007 CONNMAN_NETWORK_TYPE_CELLULAR);
1008 if (modem->network == NULL)
1011 DBG("network %p", modem->network);
1013 connman_network_set_data(modem->network, modem);
1015 connman_network_set_string(modem->network, "Path",
1016 modem->context->path);
1018 connman_network_set_index(modem->network, modem->context->index);
1020 if (modem->name != NULL)
1021 connman_network_set_name(modem->network, modem->name);
1023 connman_network_set_name(modem->network, "");
1025 connman_network_set_strength(modem->network, modem->strength);
1027 group = get_ident(modem->context->path);
1028 connman_network_set_group(modem->network, group);
1030 connman_network_set_available(modem->network, TRUE);
1032 connman_network_set_bool(modem->network, "Roaming",
1035 if (connman_device_add_network(modem->device, modem->network) < 0) {
1036 connman_network_unref(modem->network);
1037 modem->network = NULL;
1042 * Create the ipconfig layer before trying to connect. Withouth
1043 * the ipconfig layer the core is not ready to process errors.
1045 connman_network_set_index(modem->network, -1);
1048 static void remove_network(struct modem_data *modem)
1050 DBG("%s", modem->path);
1052 if (modem->network == NULL)
1055 DBG("network %p", modem->network);
1057 connman_device_remove_network(modem->device, modem->network);
1058 connman_network_unref(modem->network);
1059 modem->network = NULL;
1062 static int add_cm_context(struct modem_data *modem, const char *context_path,
1063 DBusMessageIter *dict)
1065 const char *context_type;
1066 struct network_context *context = NULL;
1067 connman_bool_t active = FALSE;
1069 DBG("%s context path %s", modem->path, context_path);
1071 if (modem->context != NULL) {
1073 * We have already assigned a context to this modem
1074 * and we do only support one Internet context.
1079 context = network_context_alloc(context_path);
1080 if (context == NULL)
1083 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1084 DBusMessageIter entry, value;
1087 dbus_message_iter_recurse(dict, &entry);
1088 dbus_message_iter_get_basic(&entry, &key);
1090 dbus_message_iter_next(&entry);
1091 dbus_message_iter_recurse(&entry, &value);
1093 if (g_str_equal(key, "Type") == TRUE) {
1094 dbus_message_iter_get_basic(&value, &context_type);
1096 DBG("%s context %s type %s", modem->path,
1097 context_path, context_type);
1098 } else if (g_str_equal(key, "Settings") == TRUE) {
1099 DBG("%s Settings", modem->path);
1101 extract_ipv4_settings(&value, context);
1102 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1103 DBG("%s IPv6.Settings", modem->path);
1105 extract_ipv6_settings(&value, context);
1106 } else if (g_str_equal(key, "Active") == TRUE) {
1107 dbus_message_iter_get_basic(&value, &active);
1109 DBG("%s Active %d", modem->path, active);
1110 } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1113 dbus_message_iter_get_basic(&value, &apn);
1114 if (apn != NULL && strlen(apn) > 0)
1115 modem->valid_apn = TRUE;
1117 modem->valid_apn = FALSE;
1119 DBG("%s AccessPointName '%s'", modem->path, apn);
1121 dbus_message_iter_next(dict);
1124 if (g_strcmp0(context_type, "internet") != 0) {
1125 network_context_free(context);
1129 modem->context = context;
1130 modem->active = active;
1132 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1134 if (modem->valid_apn == TRUE &&
1135 has_interface(modem->interfaces,
1136 OFONO_API_NETREG) == TRUE) {
1143 static void remove_cm_context(struct modem_data *modem,
1144 const char *context_path)
1146 if (modem->context == NULL)
1149 if (modem->network != NULL)
1150 remove_network(modem);
1152 g_hash_table_remove(context_hash, context_path);
1154 network_context_free(modem->context);
1155 modem->context = NULL;
1157 modem->valid_apn = FALSE;
1159 if (modem->network != NULL)
1160 remove_network(modem);
1163 static gboolean context_changed(DBusConnection *connection,
1164 DBusMessage *message,
1167 const char *context_path = dbus_message_get_path(message);
1168 struct modem_data *modem = NULL;
1169 DBusMessageIter iter, value;
1172 DBG("context_path %s", context_path);
1174 modem = g_hash_table_lookup(context_hash, context_path);
1178 if (dbus_message_iter_init(message, &iter) == FALSE)
1181 dbus_message_iter_get_basic(&iter, &key);
1183 dbus_message_iter_next(&iter);
1184 dbus_message_iter_recurse(&iter, &value);
1187 * oFono guarantees the ordering of Settings and
1188 * Active. Settings will always be send before Active = True.
1189 * That means we don't have to order here.
1191 if (g_str_equal(key, "Settings") == TRUE) {
1192 DBG("%s Settings", modem->path);
1194 extract_ipv4_settings(&value, modem->context);
1195 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1196 DBG("%s IPv6.Settings", modem->path);
1198 extract_ipv6_settings(&value, modem->context);
1199 } else if (g_str_equal(key, "Active") == TRUE) {
1200 dbus_message_iter_get_basic(&value, &modem->active);
1202 DBG("%s Active %d", modem->path, modem->active);
1204 if (modem->active == TRUE)
1205 set_connected(modem);
1207 set_disconnected(modem);
1208 } else if (g_str_equal(key, "AccessPointName") == TRUE) {
1211 dbus_message_iter_get_basic(&value, &apn);
1213 DBG("%s AccessPointName %s", modem->path, apn);
1215 if (apn != NULL && strlen(apn) > 0) {
1216 modem->valid_apn = TRUE;
1218 if (modem->network != NULL)
1221 if (has_interface(modem->interfaces,
1222 OFONO_API_NETREG) == FALSE) {
1228 if (modem->active == TRUE)
1229 set_connected(modem);
1231 modem->valid_apn = FALSE;
1233 if (modem->network == NULL)
1236 remove_network(modem);
1243 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1245 struct modem_data *modem = user_data;
1246 DBusMessageIter array, dict, entry, value;
1250 DBG("%s", modem->path);
1252 modem->call_get_contexts = NULL;
1254 reply = dbus_pending_call_steal_reply(call);
1256 dbus_error_init(&error);
1258 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1259 connman_error("%s", error.message);
1260 dbus_error_free(&error);
1264 if (dbus_message_iter_init(reply, &array) == FALSE)
1267 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1270 dbus_message_iter_recurse(&array, &dict);
1272 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1273 const char *context_path;
1275 dbus_message_iter_recurse(&dict, &entry);
1276 dbus_message_iter_get_basic(&entry, &context_path);
1278 dbus_message_iter_next(&entry);
1279 dbus_message_iter_recurse(&entry, &value);
1281 if (add_cm_context(modem, context_path, &value) == 0)
1284 dbus_message_iter_next(&dict);
1288 dbus_message_unref(reply);
1290 dbus_pending_call_unref(call);
1293 static int cm_get_contexts(struct modem_data *modem)
1295 DBusMessage *message;
1297 DBG("%s", modem->path);
1299 if (modem->call_get_contexts != NULL)
1302 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1303 OFONO_CM_INTERFACE, GET_CONTEXTS);
1304 if (message == NULL)
1307 if (dbus_connection_send_with_reply(connection, message,
1308 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1309 connman_error("Failed to call GetContexts()");
1310 dbus_message_unref(message);
1314 if (modem->call_get_contexts == NULL) {
1315 connman_error("D-Bus connection not available");
1316 dbus_message_unref(message);
1320 dbus_pending_call_set_notify(modem->call_get_contexts,
1321 cm_get_contexts_reply,
1324 dbus_message_unref(message);
1326 return -EINPROGRESS;
1329 static gboolean cm_context_added(DBusConnection *connection,
1330 DBusMessage *message,
1333 const char *path = dbus_message_get_path(message);
1335 struct modem_data *modem;
1336 DBusMessageIter iter, properties;
1340 modem = g_hash_table_lookup(modem_hash, path);
1344 if (dbus_message_iter_init(message, &iter) == FALSE)
1347 dbus_message_iter_get_basic(&iter, &context_path);
1349 dbus_message_iter_next(&iter);
1350 dbus_message_iter_recurse(&iter, &properties);
1352 if (add_cm_context(modem, context_path, &properties) != 0)
1358 static gboolean cm_context_removed(DBusConnection *connection,
1359 DBusMessage *message,
1362 const char *path = dbus_message_get_path(message);
1363 const char *context_path;
1364 struct modem_data *modem;
1365 DBusMessageIter iter;
1367 DBG("context path %s", path);
1369 if (dbus_message_iter_init(message, &iter) == FALSE)
1372 dbus_message_iter_get_basic(&iter, &context_path);
1374 modem = g_hash_table_lookup(context_hash, context_path);
1378 remove_cm_context(modem, context_path);
1383 static void netreg_update_name(struct modem_data *modem,
1384 DBusMessageIter* value)
1388 dbus_message_iter_get_basic(value, &name);
1390 DBG("%s Name %s", modem->path, name);
1392 g_free(modem->name);
1393 modem->name = g_strdup(name);
1395 if (modem->network == NULL)
1398 connman_network_set_name(modem->network, modem->name);
1399 connman_network_update(modem->network);
1402 static void netreg_update_strength(struct modem_data *modem,
1403 DBusMessageIter *value)
1405 dbus_message_iter_get_basic(value, &modem->strength);
1407 DBG("%s Strength %d", modem->path, modem->strength);
1409 if (modem->network == NULL)
1414 * We don't have 2 signal notifications we always report the strength
1415 * signal. data_strength is always equal to 0.
1418 * In the case we have a data_strength signal (from 1xEVDO network)
1419 * we don't need to update the value with strength signal (from 1xCDMA)
1420 * because the modem is registered to 1xEVDO network for data call.
1421 * In case we have no data_strength signal (not registered to 1xEVDO
1422 * network), we must report the strength signal (registered to 1xCDMA
1423 * network e.g slow mode).
1425 if (modem->data_strength != 0)
1428 connman_network_set_strength(modem->network, modem->strength);
1429 connman_network_update(modem->network);
1432 /* Retrieve 1xEVDO Data Strength signal */
1433 static void netreg_update_datastrength(struct modem_data *modem,
1434 DBusMessageIter *value)
1436 dbus_message_iter_get_basic(value, &modem->data_strength);
1438 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1440 if (modem->network == NULL)
1444 * CDMA modem is not registered to 1xEVDO network, let
1445 * update_signal_strength() reporting the value on the Strength signal
1448 if (modem->data_strength == 0)
1451 connman_network_set_strength(modem->network, modem->data_strength);
1452 connman_network_update(modem->network);
1455 static void netreg_update_roaming(struct modem_data *modem,
1456 DBusMessageIter *value)
1459 connman_bool_t roaming;
1461 dbus_message_iter_get_basic(value, &status);
1463 if (g_str_equal(status, "roaming") == TRUE)
1468 if (roaming == modem->roaming)
1471 modem->roaming = roaming;
1473 if (modem->network == NULL)
1476 connman_network_set_bool(modem->network,
1477 "Roaming", modem->roaming);
1478 connman_network_update(modem->network);
1481 static void netreg_update_regdom(struct modem_data *modem,
1482 DBusMessageIter *value)
1484 char *mobile_country_code;
1488 dbus_message_iter_get_basic(value, &mobile_country_code);
1490 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1493 mcc = atoi(mobile_country_code);
1494 if (mcc > 799 || mcc < 200)
1497 alpha2 = mcc_country_codes[mcc - 200];
1499 connman_technology_set_regdom(alpha2);
1502 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1505 const char *path = dbus_message_get_path(message);
1506 struct modem_data *modem;
1507 DBusMessageIter iter, value;
1510 modem = g_hash_table_lookup(modem_hash, path);
1514 if (modem->ignore == TRUE)
1517 if (dbus_message_iter_init(message, &iter) == FALSE)
1520 dbus_message_iter_get_basic(&iter, &key);
1522 dbus_message_iter_next(&iter);
1523 dbus_message_iter_recurse(&iter, &value);
1525 if (g_str_equal(key, "Name") == TRUE)
1526 netreg_update_name(modem, &value);
1527 else if (g_str_equal(key, "Strength") == TRUE)
1528 netreg_update_strength(modem, &value);
1529 else if (g_str_equal(key, "Status") == TRUE)
1530 netreg_update_roaming(modem, &value);
1531 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1532 netreg_update_regdom(modem, &value);
1537 static void netreg_properties_reply(struct modem_data *modem,
1538 DBusMessageIter *dict)
1540 DBG("%s", modem->path);
1542 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1543 DBusMessageIter entry, value;
1546 dbus_message_iter_recurse(dict, &entry);
1547 dbus_message_iter_get_basic(&entry, &key);
1549 dbus_message_iter_next(&entry);
1550 dbus_message_iter_recurse(&entry, &value);
1552 if (g_str_equal(key, "Name") == TRUE)
1553 netreg_update_name(modem, &value);
1554 else if (g_str_equal(key, "Strength") == TRUE)
1555 netreg_update_strength(modem, &value);
1556 else if (g_str_equal(key, "Status") == TRUE)
1557 netreg_update_roaming(modem, &value);
1558 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1559 netreg_update_regdom(modem, &value);
1561 dbus_message_iter_next(dict);
1564 if (modem->context == NULL) {
1566 * netgreg_get_properties() was issued after we got
1567 * cm_get_contexts_reply() where we create the
1568 * context. Though before we got the
1569 * netreg_properties_reply the context was removed
1570 * again. Therefore we have to skip the network
1576 if (modem->valid_apn == TRUE)
1579 if (modem->active == TRUE)
1580 set_connected(modem);
1583 static int netreg_get_properties(struct modem_data *modem)
1585 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1586 netreg_properties_reply, modem);
1589 static void add_cdma_network(struct modem_data *modem)
1591 /* Be sure that device is created before adding CDMA network */
1592 if (modem->device == NULL)
1596 * CDMA modems don't need contexts for data call, however the current
1597 * add_network() logic needs one, so we create one to proceed.
1599 if (modem->context == NULL)
1600 modem->context = network_context_alloc(modem->path);
1602 if (modem->name == NULL)
1603 modem->name = g_strdup("CDMA Network");
1607 if (modem->cdma_cm_powered == TRUE)
1608 set_connected(modem);
1611 static gboolean cdma_netreg_changed(DBusConnection *connection,
1612 DBusMessage *message,
1615 const char *path = dbus_message_get_path(message);
1616 struct modem_data *modem;
1617 DBusMessageIter iter, value;
1622 modem = g_hash_table_lookup(modem_hash, path);
1626 if (modem->ignore == TRUE)
1629 if (dbus_message_iter_init(message, &iter) == FALSE)
1632 dbus_message_iter_get_basic(&iter, &key);
1634 dbus_message_iter_next(&iter);
1635 dbus_message_iter_recurse(&iter, &value);
1637 if (g_str_equal(key, "Name") == TRUE)
1638 netreg_update_name(modem, &value);
1639 else if (g_str_equal(key, "Strength") == TRUE)
1640 netreg_update_strength(modem, &value);
1641 else if (g_str_equal(key, "DataStrength") == TRUE)
1642 netreg_update_datastrength(modem, &value);
1643 else if (g_str_equal(key, "Status") == TRUE)
1644 netreg_update_roaming(modem, &value);
1646 add_cdma_network(modem);
1651 static void cdma_netreg_properties_reply(struct modem_data *modem,
1652 DBusMessageIter *dict)
1654 DBG("%s", modem->path);
1656 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1657 DBusMessageIter entry, value;
1660 dbus_message_iter_recurse(dict, &entry);
1661 dbus_message_iter_get_basic(&entry, &key);
1663 dbus_message_iter_next(&entry);
1664 dbus_message_iter_recurse(&entry, &value);
1666 if (g_str_equal(key, "Name") == TRUE)
1667 netreg_update_name(modem, &value);
1668 else if (g_str_equal(key, "Strength") == TRUE)
1669 netreg_update_strength(modem, &value);
1670 else if (g_str_equal(key, "DataStrength") == TRUE)
1671 netreg_update_datastrength(modem, &value);
1672 else if (g_str_equal(key, "Status") == TRUE)
1673 netreg_update_roaming(modem, &value);
1675 dbus_message_iter_next(dict);
1678 add_cdma_network(modem);
1681 static int cdma_netreg_get_properties(struct modem_data *modem)
1683 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1684 cdma_netreg_properties_reply, modem);
1687 static void cm_update_attached(struct modem_data *modem,
1688 DBusMessageIter *value)
1690 dbus_message_iter_get_basic(value, &modem->attached);
1692 DBG("%s Attached %d", modem->path, modem->attached);
1694 if (modem->attached == FALSE)
1697 if (has_interface(modem->interfaces,
1698 OFONO_API_NETREG) == FALSE) {
1702 netreg_get_properties(modem);
1705 static void cm_update_powered(struct modem_data *modem,
1706 DBusMessageIter *value)
1708 dbus_message_iter_get_basic(value, &modem->cm_powered);
1710 DBG("%s ConnnectionManager Powered %d", modem->path,
1713 if (modem->cm_powered == TRUE)
1716 cm_set_powered(modem, TRUE);
1719 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1722 const char *path = dbus_message_get_path(message);
1723 struct modem_data *modem;
1724 DBusMessageIter iter, value;
1727 modem = g_hash_table_lookup(modem_hash, path);
1731 if (modem->ignore == TRUE)
1734 if (dbus_message_iter_init(message, &iter) == FALSE)
1737 dbus_message_iter_get_basic(&iter, &key);
1739 dbus_message_iter_next(&iter);
1740 dbus_message_iter_recurse(&iter, &value);
1742 if (g_str_equal(key, "Attached") == TRUE)
1743 cm_update_attached(modem, &value);
1744 else if (g_str_equal(key, "Powered") == TRUE)
1745 cm_update_powered(modem, &value);
1750 static void cdma_cm_update_powered(struct modem_data *modem,
1751 DBusMessageIter *value)
1753 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1755 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1757 if (modem->network == NULL)
1760 if (modem->cdma_cm_powered == TRUE)
1761 set_connected(modem);
1763 set_disconnected(modem);
1766 static void cdma_cm_update_settings(struct modem_data *modem,
1767 DBusMessageIter *value)
1769 DBG("%s Settings", modem->path);
1771 extract_ipv4_settings(value, modem->context);
1774 static gboolean cdma_cm_changed(DBusConnection *connection,
1775 DBusMessage *message, void *user_data)
1777 const char *path = dbus_message_get_path(message);
1778 struct modem_data *modem;
1779 DBusMessageIter iter, value;
1782 modem = g_hash_table_lookup(modem_hash, path);
1786 if (modem->online == TRUE && modem->network == NULL)
1787 cdma_netreg_get_properties(modem);
1789 if (dbus_message_iter_init(message, &iter) == FALSE)
1792 dbus_message_iter_get_basic(&iter, &key);
1794 dbus_message_iter_next(&iter);
1795 dbus_message_iter_recurse(&iter, &value);
1797 if (g_str_equal(key, "Powered") == TRUE)
1798 cdma_cm_update_powered(modem, &value);
1799 if (g_str_equal(key, "Settings") == TRUE)
1800 cdma_cm_update_settings(modem, &value);
1805 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1807 DBG("%s", modem->path);
1809 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1810 DBusMessageIter entry, value;
1813 dbus_message_iter_recurse(dict, &entry);
1814 dbus_message_iter_get_basic(&entry, &key);
1816 dbus_message_iter_next(&entry);
1817 dbus_message_iter_recurse(&entry, &value);
1819 if (g_str_equal(key, "Attached") == TRUE)
1820 cm_update_attached(modem, &value);
1821 else if (g_str_equal(key, "Powered") == TRUE)
1822 cm_update_powered(modem, &value);
1824 dbus_message_iter_next(dict);
1828 static int cm_get_properties(struct modem_data *modem)
1830 return get_properties(modem->path, OFONO_CM_INTERFACE,
1831 cm_properties_reply, modem);
1834 static void cdma_cm_properties_reply(struct modem_data *modem,
1835 DBusMessageIter *dict)
1837 DBG("%s", modem->path);
1839 if (modem->online == TRUE)
1840 cdma_netreg_get_properties(modem);
1842 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1843 DBusMessageIter entry, value;
1846 dbus_message_iter_recurse(dict, &entry);
1847 dbus_message_iter_get_basic(&entry, &key);
1849 dbus_message_iter_next(&entry);
1850 dbus_message_iter_recurse(&entry, &value);
1852 if (g_str_equal(key, "Powered") == TRUE)
1853 cdma_cm_update_powered(modem, &value);
1854 if (g_str_equal(key, "Settings") == TRUE)
1855 cdma_cm_update_settings(modem, &value);
1857 dbus_message_iter_next(dict);
1861 static int cdma_cm_get_properties(struct modem_data *modem)
1863 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1864 cdma_cm_properties_reply, modem);
1867 static void sim_update_imsi(struct modem_data *modem,
1868 DBusMessageIter* value)
1872 dbus_message_iter_get_basic(value, &imsi);
1874 DBG("%s imsi %s", modem->path, imsi);
1876 g_free(modem->imsi);
1877 modem->imsi = g_strdup(imsi);
1880 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1883 const char *path = dbus_message_get_path(message);
1884 struct modem_data *modem;
1885 DBusMessageIter iter, value;
1888 modem = g_hash_table_lookup(modem_hash, path);
1892 if (modem->ignore == TRUE)
1895 if (dbus_message_iter_init(message, &iter) == FALSE)
1898 dbus_message_iter_get_basic(&iter, &key);
1900 dbus_message_iter_next(&iter);
1901 dbus_message_iter_recurse(&iter, &value);
1903 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1904 sim_update_imsi(modem, &value);
1906 if (ready_to_create_device(modem) == FALSE)
1910 * This is a GSM modem. Create the device and
1911 * register it at the core. Enabling (setting
1912 * it online is done through the
1913 * modem_enable() callback.
1915 create_device(modem);
1921 static void sim_properties_reply(struct modem_data *modem,
1922 DBusMessageIter *dict)
1924 DBG("%s", modem->path);
1926 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1927 DBusMessageIter entry, value;
1930 dbus_message_iter_recurse(dict, &entry);
1931 dbus_message_iter_get_basic(&entry, &key);
1933 dbus_message_iter_next(&entry);
1934 dbus_message_iter_recurse(&entry, &value);
1936 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1937 sim_update_imsi(modem, &value);
1939 if (ready_to_create_device(modem) == FALSE)
1943 * This is a GSM modem. Create the device and
1944 * register it at the core. Enabling (setting
1945 * it online is done through the
1946 * modem_enable() callback.
1948 create_device(modem);
1950 if (modem->online == FALSE)
1954 * The modem is already online and we have the CM interface.
1955 * There will be no interface update and therefore our
1956 * state machine will not go to next step. We have to
1957 * trigger it from here.
1959 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1960 cm_get_properties(modem);
1961 cm_get_contexts(modem);
1966 dbus_message_iter_next(dict);
1970 static int sim_get_properties(struct modem_data *modem)
1972 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1973 sim_properties_reply, modem);
1976 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1979 if (has_interface(old_iface, api) == FALSE &&
1980 has_interface(new_iface, api) == TRUE) {
1981 DBG("%s added", api2string(api));
1988 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1991 if (has_interface(old_iface, api) == TRUE &&
1992 has_interface(new_iface, api) == FALSE) {
1993 DBG("%s removed", api2string(api));
2000 static void modem_update_interfaces(struct modem_data *modem,
2004 DBG("%s", modem->path);
2006 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
2007 if (modem->imsi == NULL &&
2008 modem->set_powered == FALSE) {
2010 * Only use do GetProperties() when
2011 * device has not been powered up.
2013 sim_get_properties(modem);
2017 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2018 if (modem->device != NULL) {
2019 cm_get_properties(modem);
2020 cm_get_contexts(modem);
2024 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2025 if (ready_to_create_device(modem) == TRUE)
2026 create_device(modem);
2028 if (modem->device != NULL)
2029 cdma_cm_get_properties(modem);
2032 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2033 if (modem->attached == TRUE)
2034 netreg_get_properties(modem);
2037 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
2038 cdma_netreg_get_properties(modem);
2041 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2042 remove_cm_context(modem, modem->context->path);
2045 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2046 remove_cm_context(modem, modem->context->path);
2049 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2050 remove_network(modem);
2053 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
2054 remove_network(modem);
2058 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
2061 const char *path = dbus_message_get_path(message);
2062 struct modem_data *modem;
2063 DBusMessageIter iter, value;
2066 modem = g_hash_table_lookup(modem_hash, path);
2070 if (modem->ignore == TRUE)
2073 if (dbus_message_iter_init(message, &iter) == FALSE)
2076 dbus_message_iter_get_basic(&iter, &key);
2078 dbus_message_iter_next(&iter);
2079 dbus_message_iter_recurse(&iter, &value);
2081 if (g_str_equal(key, "Powered") == TRUE) {
2082 dbus_message_iter_get_basic(&value, &modem->powered);
2084 DBG("%s Powered %d", modem->path, modem->powered);
2086 if (modem->powered == FALSE)
2087 modem_set_powered(modem, TRUE);
2088 } else if (g_str_equal(key, "Online") == TRUE) {
2089 dbus_message_iter_get_basic(&value, &modem->online);
2091 DBG("%s Online %d", modem->path, modem->online);
2093 if (modem->device == NULL)
2096 connman_device_set_powered(modem->device, modem->online);
2097 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2100 interfaces = extract_interfaces(&value);
2102 if (interfaces == modem->interfaces)
2105 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2107 modem_update_interfaces(modem, modem->interfaces, interfaces);
2109 modem->interfaces = interfaces;
2110 } else if (g_str_equal(key, "Serial") == TRUE) {
2113 dbus_message_iter_get_basic(&value, &serial);
2115 g_free(modem->serial);
2116 modem->serial = g_strdup(serial);
2118 DBG("%s Serial %s", modem->path, modem->serial);
2120 if (has_interface(modem->interfaces,
2121 OFONO_API_CDMA_CM) == TRUE) {
2122 if (ready_to_create_device(modem) == TRUE)
2123 create_device(modem);
2130 static void add_modem(const char *path, DBusMessageIter *prop)
2132 struct modem_data *modem;
2136 modem = g_hash_table_lookup(modem_hash, path);
2137 if (modem != NULL) {
2139 * When oFono powers up we ask for the modems and oFono is
2140 * reporting with modem_added signal the modems. Only
2146 modem = g_try_new0(struct modem_data, 1);
2150 modem->path = g_strdup(path);
2152 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2154 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2155 DBusMessageIter entry, value;
2158 dbus_message_iter_recurse(prop, &entry);
2159 dbus_message_iter_get_basic(&entry, &key);
2161 dbus_message_iter_next(&entry);
2162 dbus_message_iter_recurse(&entry, &value);
2164 if (g_str_equal(key, "Powered") == TRUE) {
2165 dbus_message_iter_get_basic(&value, &modem->powered);
2167 DBG("%s Powered %d", modem->path, modem->powered);
2168 } else if (g_str_equal(key, "Online") == TRUE) {
2169 dbus_message_iter_get_basic(&value, &modem->online);
2171 DBG("%s Online %d", modem->path, modem->online);
2172 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2173 modem->interfaces = extract_interfaces(&value);
2175 DBG("%s Interfaces 0x%02x", modem->path,
2177 } else if (g_str_equal(key, "Serial") == TRUE) {
2180 dbus_message_iter_get_basic(&value, &serial);
2181 modem->serial = g_strdup(serial);
2183 DBG("%s Serial %s", modem->path, modem->serial);
2184 } else if (g_str_equal(key, "Type") == TRUE) {
2187 dbus_message_iter_get_basic(&value, &type);
2189 DBG("%s Type %s", modem->path, type);
2190 if (g_strcmp0(type, "hardware") != 0) {
2191 DBG("%s Ignore this modem", modem->path);
2192 modem->ignore = TRUE;
2196 dbus_message_iter_next(prop);
2199 if (modem->ignore == TRUE)
2202 if (modem->powered == FALSE) {
2203 modem_set_powered(modem, TRUE);
2207 modem_update_interfaces(modem, 0, modem->interfaces);
2210 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2212 struct modem_data *modem = value;
2214 DBG("%s", modem->path);
2216 if (modem->ignore == TRUE)
2219 modem_set_powered(modem, FALSE);
2222 static void remove_modem(gpointer data)
2224 struct modem_data *modem = data;
2226 DBG("%s", modem->path);
2228 if (modem->call_set_property != NULL)
2229 dbus_pending_call_cancel(modem->call_set_property);
2231 if (modem->call_get_properties != NULL)
2232 dbus_pending_call_cancel(modem->call_get_properties);
2234 if (modem->call_get_contexts != NULL)
2235 dbus_pending_call_cancel(modem->call_get_contexts);
2237 if (modem->device != NULL)
2238 destroy_device(modem);
2240 if (modem->context != NULL)
2241 remove_cm_context(modem, modem->context->path);
2243 g_free(modem->serial);
2244 g_free(modem->name);
2245 g_free(modem->imsi);
2246 g_free(modem->path);
2251 static gboolean modem_added(DBusConnection *connection,
2252 DBusMessage *message, void *user_data)
2254 DBusMessageIter iter, properties;
2259 if (dbus_message_iter_init(message, &iter) == FALSE)
2262 dbus_message_iter_get_basic(&iter, &path);
2264 dbus_message_iter_next(&iter);
2265 dbus_message_iter_recurse(&iter, &properties);
2267 add_modem(path, &properties);
2272 static gboolean modem_removed(DBusConnection *connection,
2273 DBusMessage *message, void *user_data)
2275 DBusMessageIter iter;
2280 if (dbus_message_iter_init(message, &iter) == FALSE)
2283 dbus_message_iter_get_basic(&iter, &path);
2285 g_hash_table_remove(modem_hash, path);
2290 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2294 DBusMessageIter array, dict;
2298 reply = dbus_pending_call_steal_reply(call);
2300 dbus_error_init(&error);
2302 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2303 connman_error("%s", error.message);
2304 dbus_error_free(&error);
2308 if (dbus_message_iter_init(reply, &array) == FALSE)
2311 dbus_message_iter_recurse(&array, &dict);
2313 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2314 DBusMessageIter value, properties;
2317 dbus_message_iter_recurse(&dict, &value);
2318 dbus_message_iter_get_basic(&value, &path);
2320 dbus_message_iter_next(&value);
2321 dbus_message_iter_recurse(&value, &properties);
2323 add_modem(path, &properties);
2325 dbus_message_iter_next(&dict);
2329 dbus_message_unref(reply);
2331 dbus_pending_call_unref(call);
2334 static int manager_get_modems(void)
2336 DBusMessage *message;
2337 DBusPendingCall *call;
2341 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2342 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2343 if (message == NULL)
2346 if (dbus_connection_send_with_reply(connection, message,
2347 &call, TIMEOUT) == FALSE) {
2348 connman_error("Failed to call GetModems()");
2349 dbus_message_unref(message);
2354 connman_error("D-Bus connection not available");
2355 dbus_message_unref(message);
2359 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2362 dbus_message_unref(message);
2364 return -EINPROGRESS;
2367 static void ofono_connect(DBusConnection *conn, void *user_data)
2371 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2372 g_free, remove_modem);
2373 if (modem_hash == NULL)
2376 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2378 if (context_hash == NULL) {
2379 g_hash_table_destroy(modem_hash);
2383 manager_get_modems();
2386 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2390 if (modem_hash == NULL || context_hash == NULL)
2393 g_hash_table_destroy(modem_hash);
2396 g_hash_table_destroy(context_hash);
2397 context_hash = NULL;
2400 static int network_probe(struct connman_network *network)
2402 struct modem_data *modem = connman_network_get_data(network);
2404 DBG("%s network %p", modem->path, network);
2409 static void network_remove(struct connman_network *network)
2411 struct modem_data *modem = connman_network_get_data(network);
2413 DBG("%s network %p", modem->path, network);
2416 static int network_connect(struct connman_network *network)
2418 struct modem_data *modem = connman_network_get_data(network);
2420 DBG("%s network %p", modem->path, network);
2422 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2423 return context_set_active(modem, TRUE);
2424 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2425 return cdma_cm_set_powered(modem, TRUE);
2427 connman_error("Connection manager interface not available");
2432 static int network_disconnect(struct connman_network *network)
2434 struct modem_data *modem = connman_network_get_data(network);
2436 DBG("%s network %p", modem->path, network);
2438 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2439 return context_set_active(modem, FALSE);
2440 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2441 return cdma_cm_set_powered(modem, FALSE);
2443 connman_error("Connection manager interface not available");
2448 static struct connman_network_driver network_driver = {
2450 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2451 .probe = network_probe,
2452 .remove = network_remove,
2453 .connect = network_connect,
2454 .disconnect = network_disconnect,
2457 static int modem_probe(struct connman_device *device)
2459 struct modem_data *modem = connman_device_get_data(device);
2461 DBG("%s device %p", modem->path, device);
2466 static void modem_remove(struct connman_device *device)
2468 struct modem_data *modem = connman_device_get_data(device);
2470 DBG("%s device %p", modem->path, device);
2473 static int modem_enable(struct connman_device *device)
2475 struct modem_data *modem = connman_device_get_data(device);
2477 DBG("%s device %p", modem->path, device);
2479 if (modem->online == TRUE)
2482 return modem_set_online(modem, TRUE);
2485 static int modem_disable(struct connman_device *device)
2487 struct modem_data *modem = connman_device_get_data(device);
2489 DBG("%s device %p", modem->path, device);
2491 if (modem->online == FALSE)
2494 return modem_set_online(modem, FALSE);
2497 static struct connman_device_driver modem_driver = {
2499 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2500 .probe = modem_probe,
2501 .remove = modem_remove,
2502 .enable = modem_enable,
2503 .disable = modem_disable,
2506 static int tech_probe(struct connman_technology *technology)
2511 static void tech_remove(struct connman_technology *technology)
2515 static struct connman_technology_driver tech_driver = {
2517 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2518 .probe = tech_probe,
2519 .remove = tech_remove,
2523 static guint modem_added_watch;
2524 static guint modem_removed_watch;
2525 static guint modem_watch;
2526 static guint cm_watch;
2527 static guint sim_watch;
2528 static guint context_added_watch;
2529 static guint context_removed_watch;
2530 static guint netreg_watch;
2531 static guint context_watch;
2532 static guint cdma_cm_watch;
2533 static guint cdma_netreg_watch;
2535 static int ofono_init(void)
2541 connection = connman_dbus_get_connection();
2542 if (connection == NULL)
2545 watch = g_dbus_add_service_watch(connection,
2546 OFONO_SERVICE, ofono_connect,
2547 ofono_disconnect, NULL, NULL);
2549 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2550 OFONO_MANAGER_INTERFACE,
2555 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2556 OFONO_MANAGER_INTERFACE,
2561 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2562 OFONO_MODEM_INTERFACE,
2567 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2573 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2574 OFONO_SIM_INTERFACE,
2579 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2585 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2591 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2592 OFONO_CONTEXT_INTERFACE,
2597 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2598 OFONO_NETREG_INTERFACE,
2603 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2604 OFONO_CDMA_CM_INTERFACE,
2609 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2610 OFONO_CDMA_NETREG_INTERFACE,
2612 cdma_netreg_changed,
2616 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2617 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2618 context_added_watch == 0 ||
2619 context_removed_watch == 0 ||
2620 context_watch == 0 || netreg_watch == 0 ||
2621 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2626 err = connman_network_driver_register(&network_driver);
2630 err = connman_device_driver_register(&modem_driver);
2632 connman_network_driver_unregister(&network_driver);
2636 err = connman_technology_driver_register(&tech_driver);
2638 connman_device_driver_unregister(&modem_driver);
2639 connman_network_driver_unregister(&network_driver);
2646 g_dbus_remove_watch(connection, cdma_netreg_watch);
2647 g_dbus_remove_watch(connection, cdma_cm_watch);
2648 g_dbus_remove_watch(connection, netreg_watch);
2649 g_dbus_remove_watch(connection, context_watch);
2650 g_dbus_remove_watch(connection, context_removed_watch);
2651 g_dbus_remove_watch(connection, context_added_watch);
2652 g_dbus_remove_watch(connection, sim_watch);
2653 g_dbus_remove_watch(connection, cm_watch);
2654 g_dbus_remove_watch(connection, modem_watch);
2655 g_dbus_remove_watch(connection, modem_removed_watch);
2656 g_dbus_remove_watch(connection, modem_added_watch);
2657 g_dbus_remove_watch(connection, watch);
2658 dbus_connection_unref(connection);
2663 static void ofono_exit(void)
2667 if (modem_hash != NULL) {
2669 * We should propably wait for the SetProperty() reply
2670 * message, because ...
2672 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2675 * ... here we will cancel the call.
2677 g_hash_table_destroy(modem_hash);
2681 if (context_hash != NULL) {
2682 g_hash_table_destroy(context_hash);
2683 context_hash = NULL;
2686 connman_technology_driver_unregister(&tech_driver);
2687 connman_device_driver_unregister(&modem_driver);
2688 connman_network_driver_unregister(&network_driver);
2690 g_dbus_remove_watch(connection, cdma_netreg_watch);
2691 g_dbus_remove_watch(connection, cdma_cm_watch);
2692 g_dbus_remove_watch(connection, netreg_watch);
2693 g_dbus_remove_watch(connection, context_watch);
2694 g_dbus_remove_watch(connection, context_removed_watch);
2695 g_dbus_remove_watch(connection, context_added_watch);
2696 g_dbus_remove_watch(connection, sim_watch);
2697 g_dbus_remove_watch(connection, cm_watch);
2698 g_dbus_remove_watch(connection, modem_watch);
2699 g_dbus_remove_watch(connection, modem_added_watch);
2700 g_dbus_remove_watch(connection, modem_removed_watch);
2701 g_dbus_remove_watch(connection, watch);
2703 dbus_connection_unref(connection);
2706 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2707 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)