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 = 0;
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 && modem->attached == 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 (modem->attached == FALSE)
1224 if (has_interface(modem->interfaces,
1225 OFONO_API_NETREG) == FALSE) {
1231 if (modem->active == TRUE)
1232 set_connected(modem);
1234 modem->valid_apn = FALSE;
1236 if (modem->network == NULL)
1239 remove_network(modem);
1246 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1248 struct modem_data *modem = user_data;
1249 DBusMessageIter array, dict, entry, value;
1253 DBG("%s", modem->path);
1255 modem->call_get_contexts = NULL;
1257 reply = dbus_pending_call_steal_reply(call);
1259 dbus_error_init(&error);
1261 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1262 connman_error("%s", error.message);
1263 dbus_error_free(&error);
1267 if (dbus_message_iter_init(reply, &array) == FALSE)
1270 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1273 dbus_message_iter_recurse(&array, &dict);
1275 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1276 const char *context_path;
1278 dbus_message_iter_recurse(&dict, &entry);
1279 dbus_message_iter_get_basic(&entry, &context_path);
1281 dbus_message_iter_next(&entry);
1282 dbus_message_iter_recurse(&entry, &value);
1284 if (add_cm_context(modem, context_path, &value) == 0)
1287 dbus_message_iter_next(&dict);
1291 dbus_message_unref(reply);
1293 dbus_pending_call_unref(call);
1296 static int cm_get_contexts(struct modem_data *modem)
1298 DBusMessage *message;
1300 DBG("%s", modem->path);
1302 if (modem->call_get_contexts != NULL)
1305 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1306 OFONO_CM_INTERFACE, GET_CONTEXTS);
1307 if (message == NULL)
1310 if (dbus_connection_send_with_reply(connection, message,
1311 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1312 connman_error("Failed to call GetContexts()");
1313 dbus_message_unref(message);
1317 if (modem->call_get_contexts == NULL) {
1318 connman_error("D-Bus connection not available");
1319 dbus_message_unref(message);
1323 dbus_pending_call_set_notify(modem->call_get_contexts,
1324 cm_get_contexts_reply,
1327 dbus_message_unref(message);
1329 return -EINPROGRESS;
1332 static gboolean cm_context_added(DBusConnection *connection,
1333 DBusMessage *message,
1336 const char *path = dbus_message_get_path(message);
1338 struct modem_data *modem;
1339 DBusMessageIter iter, properties;
1343 modem = g_hash_table_lookup(modem_hash, path);
1347 if (dbus_message_iter_init(message, &iter) == FALSE)
1350 dbus_message_iter_get_basic(&iter, &context_path);
1352 dbus_message_iter_next(&iter);
1353 dbus_message_iter_recurse(&iter, &properties);
1355 if (add_cm_context(modem, context_path, &properties) != 0)
1361 static gboolean cm_context_removed(DBusConnection *connection,
1362 DBusMessage *message,
1365 const char *path = dbus_message_get_path(message);
1366 const char *context_path;
1367 struct modem_data *modem;
1368 DBusMessageIter iter;
1370 DBG("context path %s", path);
1372 if (dbus_message_iter_init(message, &iter) == FALSE)
1375 dbus_message_iter_get_basic(&iter, &context_path);
1377 modem = g_hash_table_lookup(context_hash, context_path);
1381 remove_cm_context(modem, context_path);
1386 static void netreg_update_name(struct modem_data *modem,
1387 DBusMessageIter* value)
1391 dbus_message_iter_get_basic(value, &name);
1393 DBG("%s Name %s", modem->path, name);
1395 g_free(modem->name);
1396 modem->name = g_strdup(name);
1398 if (modem->network == NULL)
1401 connman_network_set_name(modem->network, modem->name);
1402 connman_network_update(modem->network);
1405 static void netreg_update_strength(struct modem_data *modem,
1406 DBusMessageIter *value)
1408 dbus_message_iter_get_basic(value, &modem->strength);
1410 DBG("%s Strength %d", modem->path, modem->strength);
1412 if (modem->network == NULL)
1417 * We don't have 2 signal notifications we always report the strength
1418 * signal. data_strength is always equal to 0.
1421 * In the case we have a data_strength signal (from 1xEVDO network)
1422 * we don't need to update the value with strength signal (from 1xCDMA)
1423 * because the modem is registered to 1xEVDO network for data call.
1424 * In case we have no data_strength signal (not registered to 1xEVDO
1425 * network), we must report the strength signal (registered to 1xCDMA
1426 * network e.g slow mode).
1428 if (modem->data_strength != 0)
1431 connman_network_set_strength(modem->network, modem->strength);
1432 connman_network_update(modem->network);
1435 /* Retrieve 1xEVDO Data Strength signal */
1436 static void netreg_update_datastrength(struct modem_data *modem,
1437 DBusMessageIter *value)
1439 dbus_message_iter_get_basic(value, &modem->data_strength);
1441 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1443 if (modem->network == NULL)
1447 * CDMA modem is not registered to 1xEVDO network, let
1448 * update_signal_strength() reporting the value on the Strength signal
1451 if (modem->data_strength == 0)
1454 connman_network_set_strength(modem->network, modem->data_strength);
1455 connman_network_update(modem->network);
1458 static void netreg_update_roaming(struct modem_data *modem,
1459 DBusMessageIter *value)
1462 connman_bool_t roaming;
1464 dbus_message_iter_get_basic(value, &status);
1466 if (g_str_equal(status, "roaming") == TRUE)
1471 if (roaming == modem->roaming)
1474 modem->roaming = roaming;
1476 if (modem->network == NULL)
1479 connman_network_set_bool(modem->network,
1480 "Roaming", modem->roaming);
1481 connman_network_update(modem->network);
1484 static void netreg_update_regdom(struct modem_data *modem,
1485 DBusMessageIter *value)
1487 char *mobile_country_code;
1491 dbus_message_iter_get_basic(value, &mobile_country_code);
1493 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1496 mcc = atoi(mobile_country_code);
1497 if (mcc > 799 || mcc < 200)
1500 alpha2 = mcc_country_codes[mcc - 200];
1502 connman_technology_set_regdom(alpha2);
1505 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1508 const char *path = dbus_message_get_path(message);
1509 struct modem_data *modem;
1510 DBusMessageIter iter, value;
1513 modem = g_hash_table_lookup(modem_hash, path);
1517 if (modem->ignore == TRUE)
1520 if (dbus_message_iter_init(message, &iter) == FALSE)
1523 dbus_message_iter_get_basic(&iter, &key);
1525 dbus_message_iter_next(&iter);
1526 dbus_message_iter_recurse(&iter, &value);
1528 if (g_str_equal(key, "Name") == TRUE)
1529 netreg_update_name(modem, &value);
1530 else if (g_str_equal(key, "Strength") == TRUE)
1531 netreg_update_strength(modem, &value);
1532 else if (g_str_equal(key, "Status") == TRUE)
1533 netreg_update_roaming(modem, &value);
1534 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1535 netreg_update_regdom(modem, &value);
1540 static void netreg_properties_reply(struct modem_data *modem,
1541 DBusMessageIter *dict)
1543 DBG("%s", modem->path);
1545 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1546 DBusMessageIter entry, value;
1549 dbus_message_iter_recurse(dict, &entry);
1550 dbus_message_iter_get_basic(&entry, &key);
1552 dbus_message_iter_next(&entry);
1553 dbus_message_iter_recurse(&entry, &value);
1555 if (g_str_equal(key, "Name") == TRUE)
1556 netreg_update_name(modem, &value);
1557 else if (g_str_equal(key, "Strength") == TRUE)
1558 netreg_update_strength(modem, &value);
1559 else if (g_str_equal(key, "Status") == TRUE)
1560 netreg_update_roaming(modem, &value);
1561 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1562 netreg_update_regdom(modem, &value);
1564 dbus_message_iter_next(dict);
1567 if (modem->context == NULL) {
1569 * netgreg_get_properties() was issued after we got
1570 * cm_get_contexts_reply() where we create the
1571 * context. Though before we got the
1572 * netreg_properties_reply the context was removed
1573 * again. Therefore we have to skip the network
1579 if (modem->valid_apn == TRUE)
1582 if (modem->active == TRUE)
1583 set_connected(modem);
1586 static int netreg_get_properties(struct modem_data *modem)
1588 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1589 netreg_properties_reply, modem);
1592 static void add_cdma_network(struct modem_data *modem)
1594 /* Be sure that device is created before adding CDMA network */
1595 if (modem->device == NULL)
1599 * CDMA modems don't need contexts for data call, however the current
1600 * add_network() logic needs one, so we create one to proceed.
1602 if (modem->context == NULL)
1603 modem->context = network_context_alloc(modem->path);
1605 if (modem->name == NULL)
1606 modem->name = g_strdup("CDMA Network");
1610 if (modem->cdma_cm_powered == TRUE)
1611 set_connected(modem);
1614 static gboolean cdma_netreg_changed(DBusConnection *connection,
1615 DBusMessage *message,
1618 const char *path = dbus_message_get_path(message);
1619 struct modem_data *modem;
1620 DBusMessageIter iter, value;
1625 modem = g_hash_table_lookup(modem_hash, path);
1629 if (modem->ignore == TRUE)
1632 if (dbus_message_iter_init(message, &iter) == FALSE)
1635 dbus_message_iter_get_basic(&iter, &key);
1637 dbus_message_iter_next(&iter);
1638 dbus_message_iter_recurse(&iter, &value);
1640 if (g_str_equal(key, "Name") == TRUE)
1641 netreg_update_name(modem, &value);
1642 else if (g_str_equal(key, "Strength") == TRUE)
1643 netreg_update_strength(modem, &value);
1644 else if (g_str_equal(key, "DataStrength") == TRUE)
1645 netreg_update_datastrength(modem, &value);
1646 else if (g_str_equal(key, "Status") == TRUE)
1647 netreg_update_roaming(modem, &value);
1649 add_cdma_network(modem);
1654 static void cdma_netreg_properties_reply(struct modem_data *modem,
1655 DBusMessageIter *dict)
1657 DBG("%s", modem->path);
1659 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1660 DBusMessageIter entry, value;
1663 dbus_message_iter_recurse(dict, &entry);
1664 dbus_message_iter_get_basic(&entry, &key);
1666 dbus_message_iter_next(&entry);
1667 dbus_message_iter_recurse(&entry, &value);
1669 if (g_str_equal(key, "Name") == TRUE)
1670 netreg_update_name(modem, &value);
1671 else if (g_str_equal(key, "Strength") == TRUE)
1672 netreg_update_strength(modem, &value);
1673 else if (g_str_equal(key, "DataStrength") == TRUE)
1674 netreg_update_datastrength(modem, &value);
1675 else if (g_str_equal(key, "Status") == TRUE)
1676 netreg_update_roaming(modem, &value);
1678 dbus_message_iter_next(dict);
1681 add_cdma_network(modem);
1684 static int cdma_netreg_get_properties(struct modem_data *modem)
1686 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1687 cdma_netreg_properties_reply, modem);
1690 static void cm_update_attached(struct modem_data *modem,
1691 DBusMessageIter *value)
1693 dbus_message_iter_get_basic(value, &modem->attached);
1695 DBG("%s Attached %d", modem->path, modem->attached);
1697 if (modem->attached == FALSE)
1700 if (has_interface(modem->interfaces,
1701 OFONO_API_NETREG) == FALSE) {
1705 netreg_get_properties(modem);
1708 static void cm_update_powered(struct modem_data *modem,
1709 DBusMessageIter *value)
1711 dbus_message_iter_get_basic(value, &modem->cm_powered);
1713 DBG("%s ConnnectionManager Powered %d", modem->path,
1716 if (modem->cm_powered == TRUE)
1719 cm_set_powered(modem, TRUE);
1722 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1725 const char *path = dbus_message_get_path(message);
1726 struct modem_data *modem;
1727 DBusMessageIter iter, value;
1730 modem = g_hash_table_lookup(modem_hash, path);
1734 if (modem->ignore == TRUE)
1737 if (dbus_message_iter_init(message, &iter) == FALSE)
1740 dbus_message_iter_get_basic(&iter, &key);
1742 dbus_message_iter_next(&iter);
1743 dbus_message_iter_recurse(&iter, &value);
1745 if (g_str_equal(key, "Attached") == TRUE)
1746 cm_update_attached(modem, &value);
1747 else if (g_str_equal(key, "Powered") == TRUE)
1748 cm_update_powered(modem, &value);
1753 static void cdma_cm_update_powered(struct modem_data *modem,
1754 DBusMessageIter *value)
1756 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1758 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1760 if (modem->network == NULL)
1763 if (modem->cdma_cm_powered == TRUE)
1764 set_connected(modem);
1766 set_disconnected(modem);
1769 static void cdma_cm_update_settings(struct modem_data *modem,
1770 DBusMessageIter *value)
1772 DBG("%s Settings", modem->path);
1774 extract_ipv4_settings(value, modem->context);
1777 static gboolean cdma_cm_changed(DBusConnection *connection,
1778 DBusMessage *message, void *user_data)
1780 const char *path = dbus_message_get_path(message);
1781 struct modem_data *modem;
1782 DBusMessageIter iter, value;
1785 modem = g_hash_table_lookup(modem_hash, path);
1789 if (modem->online == TRUE && modem->network == NULL)
1790 cdma_netreg_get_properties(modem);
1792 if (dbus_message_iter_init(message, &iter) == FALSE)
1795 dbus_message_iter_get_basic(&iter, &key);
1797 dbus_message_iter_next(&iter);
1798 dbus_message_iter_recurse(&iter, &value);
1800 if (g_str_equal(key, "Powered") == TRUE)
1801 cdma_cm_update_powered(modem, &value);
1802 if (g_str_equal(key, "Settings") == TRUE)
1803 cdma_cm_update_settings(modem, &value);
1808 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1810 DBG("%s", modem->path);
1812 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1813 DBusMessageIter entry, value;
1816 dbus_message_iter_recurse(dict, &entry);
1817 dbus_message_iter_get_basic(&entry, &key);
1819 dbus_message_iter_next(&entry);
1820 dbus_message_iter_recurse(&entry, &value);
1822 if (g_str_equal(key, "Attached") == TRUE)
1823 cm_update_attached(modem, &value);
1824 else if (g_str_equal(key, "Powered") == TRUE)
1825 cm_update_powered(modem, &value);
1827 dbus_message_iter_next(dict);
1831 static int cm_get_properties(struct modem_data *modem)
1833 return get_properties(modem->path, OFONO_CM_INTERFACE,
1834 cm_properties_reply, modem);
1837 static void cdma_cm_properties_reply(struct modem_data *modem,
1838 DBusMessageIter *dict)
1840 DBG("%s", modem->path);
1842 if (modem->online == TRUE)
1843 cdma_netreg_get_properties(modem);
1845 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1846 DBusMessageIter entry, value;
1849 dbus_message_iter_recurse(dict, &entry);
1850 dbus_message_iter_get_basic(&entry, &key);
1852 dbus_message_iter_next(&entry);
1853 dbus_message_iter_recurse(&entry, &value);
1855 if (g_str_equal(key, "Powered") == TRUE)
1856 cdma_cm_update_powered(modem, &value);
1857 if (g_str_equal(key, "Settings") == TRUE)
1858 cdma_cm_update_settings(modem, &value);
1860 dbus_message_iter_next(dict);
1864 static int cdma_cm_get_properties(struct modem_data *modem)
1866 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1867 cdma_cm_properties_reply, modem);
1870 static void sim_update_imsi(struct modem_data *modem,
1871 DBusMessageIter* value)
1875 dbus_message_iter_get_basic(value, &imsi);
1877 DBG("%s imsi %s", modem->path, imsi);
1879 g_free(modem->imsi);
1880 modem->imsi = g_strdup(imsi);
1883 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1886 const char *path = dbus_message_get_path(message);
1887 struct modem_data *modem;
1888 DBusMessageIter iter, value;
1891 modem = g_hash_table_lookup(modem_hash, path);
1895 if (modem->ignore == TRUE)
1898 if (dbus_message_iter_init(message, &iter) == FALSE)
1901 dbus_message_iter_get_basic(&iter, &key);
1903 dbus_message_iter_next(&iter);
1904 dbus_message_iter_recurse(&iter, &value);
1906 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1907 sim_update_imsi(modem, &value);
1909 if (ready_to_create_device(modem) == FALSE)
1913 * This is a GSM modem. Create the device and
1914 * register it at the core. Enabling (setting
1915 * it online is done through the
1916 * modem_enable() callback.
1918 create_device(modem);
1924 static void sim_properties_reply(struct modem_data *modem,
1925 DBusMessageIter *dict)
1927 DBG("%s", modem->path);
1929 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1930 DBusMessageIter entry, value;
1933 dbus_message_iter_recurse(dict, &entry);
1934 dbus_message_iter_get_basic(&entry, &key);
1936 dbus_message_iter_next(&entry);
1937 dbus_message_iter_recurse(&entry, &value);
1939 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1940 sim_update_imsi(modem, &value);
1942 if (ready_to_create_device(modem) == FALSE)
1946 * This is a GSM modem. Create the device and
1947 * register it at the core. Enabling (setting
1948 * it online is done through the
1949 * modem_enable() callback.
1951 create_device(modem);
1953 if (modem->online == FALSE)
1957 * The modem is already online and we have the CM interface.
1958 * There will be no interface update and therefore our
1959 * state machine will not go to next step. We have to
1960 * trigger it from here.
1962 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1963 cm_get_properties(modem);
1964 cm_get_contexts(modem);
1969 dbus_message_iter_next(dict);
1973 static int sim_get_properties(struct modem_data *modem)
1975 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1976 sim_properties_reply, modem);
1979 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1982 if (has_interface(old_iface, api) == FALSE &&
1983 has_interface(new_iface, api) == TRUE) {
1984 DBG("%s added", api2string(api));
1991 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1994 if (has_interface(old_iface, api) == TRUE &&
1995 has_interface(new_iface, api) == FALSE) {
1996 DBG("%s removed", api2string(api));
2003 static void modem_update_interfaces(struct modem_data *modem,
2007 DBG("%s", modem->path);
2009 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
2010 if (modem->imsi == NULL &&
2011 modem->set_powered == FALSE) {
2013 * Only use do GetProperties() when
2014 * device has not been powered up.
2016 sim_get_properties(modem);
2020 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2021 if (modem->device != NULL) {
2022 cm_get_properties(modem);
2023 cm_get_contexts(modem);
2027 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2028 if (ready_to_create_device(modem) == TRUE)
2029 create_device(modem);
2031 if (modem->device != NULL)
2032 cdma_cm_get_properties(modem);
2035 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2036 if (modem->attached == TRUE)
2037 netreg_get_properties(modem);
2040 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
2041 cdma_netreg_get_properties(modem);
2044 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
2045 remove_cm_context(modem, modem->context->path);
2048 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
2049 remove_cm_context(modem, modem->context->path);
2052 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
2053 remove_network(modem);
2056 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
2057 remove_network(modem);
2061 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
2064 const char *path = dbus_message_get_path(message);
2065 struct modem_data *modem;
2066 DBusMessageIter iter, value;
2069 modem = g_hash_table_lookup(modem_hash, path);
2073 if (modem->ignore == TRUE)
2076 if (dbus_message_iter_init(message, &iter) == FALSE)
2079 dbus_message_iter_get_basic(&iter, &key);
2081 dbus_message_iter_next(&iter);
2082 dbus_message_iter_recurse(&iter, &value);
2084 if (g_str_equal(key, "Powered") == TRUE) {
2085 dbus_message_iter_get_basic(&value, &modem->powered);
2087 DBG("%s Powered %d", modem->path, modem->powered);
2089 if (modem->powered == FALSE)
2090 modem_set_powered(modem, TRUE);
2091 } else if (g_str_equal(key, "Online") == TRUE) {
2092 dbus_message_iter_get_basic(&value, &modem->online);
2094 DBG("%s Online %d", modem->path, modem->online);
2096 if (modem->device == NULL)
2099 connman_device_set_powered(modem->device, modem->online);
2100 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2103 interfaces = extract_interfaces(&value);
2105 if (interfaces == modem->interfaces)
2108 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2110 modem_update_interfaces(modem, modem->interfaces, interfaces);
2112 modem->interfaces = interfaces;
2113 } else if (g_str_equal(key, "Serial") == TRUE) {
2116 dbus_message_iter_get_basic(&value, &serial);
2118 g_free(modem->serial);
2119 modem->serial = g_strdup(serial);
2121 DBG("%s Serial %s", modem->path, modem->serial);
2123 if (has_interface(modem->interfaces,
2124 OFONO_API_CDMA_CM) == TRUE) {
2125 if (ready_to_create_device(modem) == TRUE)
2126 create_device(modem);
2133 static void add_modem(const char *path, DBusMessageIter *prop)
2135 struct modem_data *modem;
2139 modem = g_hash_table_lookup(modem_hash, path);
2140 if (modem != NULL) {
2142 * When oFono powers up we ask for the modems and oFono is
2143 * reporting with modem_added signal the modems. Only
2149 modem = g_try_new0(struct modem_data, 1);
2153 modem->path = g_strdup(path);
2155 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2157 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2158 DBusMessageIter entry, value;
2161 dbus_message_iter_recurse(prop, &entry);
2162 dbus_message_iter_get_basic(&entry, &key);
2164 dbus_message_iter_next(&entry);
2165 dbus_message_iter_recurse(&entry, &value);
2167 if (g_str_equal(key, "Powered") == TRUE) {
2168 dbus_message_iter_get_basic(&value, &modem->powered);
2170 DBG("%s Powered %d", modem->path, modem->powered);
2171 } else if (g_str_equal(key, "Online") == TRUE) {
2172 dbus_message_iter_get_basic(&value, &modem->online);
2174 DBG("%s Online %d", modem->path, modem->online);
2175 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2176 modem->interfaces = extract_interfaces(&value);
2178 DBG("%s Interfaces 0x%02x", modem->path,
2180 } else if (g_str_equal(key, "Serial") == TRUE) {
2183 dbus_message_iter_get_basic(&value, &serial);
2184 modem->serial = g_strdup(serial);
2186 DBG("%s Serial %s", modem->path, modem->serial);
2187 } else if (g_str_equal(key, "Type") == TRUE) {
2190 dbus_message_iter_get_basic(&value, &type);
2192 DBG("%s Type %s", modem->path, type);
2193 if (g_strcmp0(type, "hardware") != 0) {
2194 DBG("%s Ignore this modem", modem->path);
2195 modem->ignore = TRUE;
2199 dbus_message_iter_next(prop);
2202 if (modem->ignore == TRUE)
2205 if (modem->powered == FALSE) {
2206 modem_set_powered(modem, TRUE);
2210 modem_update_interfaces(modem, 0, modem->interfaces);
2213 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2215 struct modem_data *modem = value;
2217 DBG("%s", modem->path);
2219 if (modem->ignore == TRUE)
2222 modem_set_powered(modem, FALSE);
2225 static void remove_modem(gpointer data)
2227 struct modem_data *modem = data;
2229 DBG("%s", modem->path);
2231 if (modem->call_set_property != NULL)
2232 dbus_pending_call_cancel(modem->call_set_property);
2234 if (modem->call_get_properties != NULL)
2235 dbus_pending_call_cancel(modem->call_get_properties);
2237 if (modem->call_get_contexts != NULL)
2238 dbus_pending_call_cancel(modem->call_get_contexts);
2240 if (modem->device != NULL)
2241 destroy_device(modem);
2243 if (modem->context != NULL)
2244 remove_cm_context(modem, modem->context->path);
2246 g_free(modem->serial);
2247 g_free(modem->name);
2248 g_free(modem->imsi);
2249 g_free(modem->path);
2254 static gboolean modem_added(DBusConnection *connection,
2255 DBusMessage *message, void *user_data)
2257 DBusMessageIter iter, properties;
2262 if (dbus_message_iter_init(message, &iter) == FALSE)
2265 dbus_message_iter_get_basic(&iter, &path);
2267 dbus_message_iter_next(&iter);
2268 dbus_message_iter_recurse(&iter, &properties);
2270 add_modem(path, &properties);
2275 static gboolean modem_removed(DBusConnection *connection,
2276 DBusMessage *message, void *user_data)
2278 DBusMessageIter iter;
2283 if (dbus_message_iter_init(message, &iter) == FALSE)
2286 dbus_message_iter_get_basic(&iter, &path);
2288 g_hash_table_remove(modem_hash, path);
2293 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2297 DBusMessageIter array, dict;
2301 reply = dbus_pending_call_steal_reply(call);
2303 dbus_error_init(&error);
2305 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2306 connman_error("%s", error.message);
2307 dbus_error_free(&error);
2311 if (dbus_message_iter_init(reply, &array) == FALSE)
2314 dbus_message_iter_recurse(&array, &dict);
2316 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2317 DBusMessageIter value, properties;
2320 dbus_message_iter_recurse(&dict, &value);
2321 dbus_message_iter_get_basic(&value, &path);
2323 dbus_message_iter_next(&value);
2324 dbus_message_iter_recurse(&value, &properties);
2326 add_modem(path, &properties);
2328 dbus_message_iter_next(&dict);
2332 dbus_message_unref(reply);
2334 dbus_pending_call_unref(call);
2337 static int manager_get_modems(void)
2339 DBusMessage *message;
2340 DBusPendingCall *call;
2344 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2345 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2346 if (message == NULL)
2349 if (dbus_connection_send_with_reply(connection, message,
2350 &call, TIMEOUT) == FALSE) {
2351 connman_error("Failed to call GetModems()");
2352 dbus_message_unref(message);
2357 connman_error("D-Bus connection not available");
2358 dbus_message_unref(message);
2362 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2365 dbus_message_unref(message);
2367 return -EINPROGRESS;
2370 static void ofono_connect(DBusConnection *conn, void *user_data)
2374 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2375 g_free, remove_modem);
2376 if (modem_hash == NULL)
2379 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2381 if (context_hash == NULL) {
2382 g_hash_table_destroy(modem_hash);
2386 manager_get_modems();
2389 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2393 if (modem_hash == NULL || context_hash == NULL)
2396 g_hash_table_destroy(modem_hash);
2399 g_hash_table_destroy(context_hash);
2400 context_hash = NULL;
2403 static int network_probe(struct connman_network *network)
2405 struct modem_data *modem = connman_network_get_data(network);
2407 DBG("%s network %p", modem->path, network);
2412 static void network_remove(struct connman_network *network)
2414 struct modem_data *modem = connman_network_get_data(network);
2416 DBG("%s network %p", modem->path, network);
2419 static int network_connect(struct connman_network *network)
2421 struct modem_data *modem = connman_network_get_data(network);
2423 DBG("%s network %p", modem->path, network);
2425 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2426 return context_set_active(modem, TRUE);
2427 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2428 return cdma_cm_set_powered(modem, TRUE);
2430 connman_error("Connection manager interface not available");
2435 static int network_disconnect(struct connman_network *network)
2437 struct modem_data *modem = connman_network_get_data(network);
2439 DBG("%s network %p", modem->path, network);
2441 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2442 return context_set_active(modem, FALSE);
2443 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2444 return cdma_cm_set_powered(modem, FALSE);
2446 connman_error("Connection manager interface not available");
2451 static struct connman_network_driver network_driver = {
2453 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2454 .probe = network_probe,
2455 .remove = network_remove,
2456 .connect = network_connect,
2457 .disconnect = network_disconnect,
2460 static int modem_probe(struct connman_device *device)
2462 struct modem_data *modem = connman_device_get_data(device);
2464 DBG("%s device %p", modem->path, device);
2469 static void modem_remove(struct connman_device *device)
2471 struct modem_data *modem = connman_device_get_data(device);
2473 DBG("%s device %p", modem->path, device);
2476 static int modem_enable(struct connman_device *device)
2478 struct modem_data *modem = connman_device_get_data(device);
2480 DBG("%s device %p", modem->path, device);
2482 if (modem->online == TRUE)
2485 return modem_set_online(modem, TRUE);
2488 static int modem_disable(struct connman_device *device)
2490 struct modem_data *modem = connman_device_get_data(device);
2492 DBG("%s device %p", modem->path, device);
2494 if (modem->online == FALSE)
2497 return modem_set_online(modem, FALSE);
2500 static struct connman_device_driver modem_driver = {
2502 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2503 .probe = modem_probe,
2504 .remove = modem_remove,
2505 .enable = modem_enable,
2506 .disable = modem_disable,
2509 static int tech_probe(struct connman_technology *technology)
2514 static void tech_remove(struct connman_technology *technology)
2518 static struct connman_technology_driver tech_driver = {
2520 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2521 .probe = tech_probe,
2522 .remove = tech_remove,
2526 static guint modem_added_watch;
2527 static guint modem_removed_watch;
2528 static guint modem_watch;
2529 static guint cm_watch;
2530 static guint sim_watch;
2531 static guint context_added_watch;
2532 static guint context_removed_watch;
2533 static guint netreg_watch;
2534 static guint context_watch;
2535 static guint cdma_cm_watch;
2536 static guint cdma_netreg_watch;
2538 static int ofono_init(void)
2544 connection = connman_dbus_get_connection();
2545 if (connection == NULL)
2548 watch = g_dbus_add_service_watch(connection,
2549 OFONO_SERVICE, ofono_connect,
2550 ofono_disconnect, NULL, NULL);
2552 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2553 OFONO_MANAGER_INTERFACE,
2558 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2559 OFONO_MANAGER_INTERFACE,
2564 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2565 OFONO_MODEM_INTERFACE,
2570 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2576 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2577 OFONO_SIM_INTERFACE,
2582 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2588 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2594 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2595 OFONO_CONTEXT_INTERFACE,
2600 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2601 OFONO_NETREG_INTERFACE,
2606 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2607 OFONO_CDMA_CM_INTERFACE,
2612 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2613 OFONO_CDMA_NETREG_INTERFACE,
2615 cdma_netreg_changed,
2619 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2620 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2621 context_added_watch == 0 ||
2622 context_removed_watch == 0 ||
2623 context_watch == 0 || netreg_watch == 0 ||
2624 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2629 err = connman_network_driver_register(&network_driver);
2633 err = connman_device_driver_register(&modem_driver);
2635 connman_network_driver_unregister(&network_driver);
2639 err = connman_technology_driver_register(&tech_driver);
2641 connman_device_driver_unregister(&modem_driver);
2642 connman_network_driver_unregister(&network_driver);
2649 g_dbus_remove_watch(connection, cdma_netreg_watch);
2650 g_dbus_remove_watch(connection, cdma_cm_watch);
2651 g_dbus_remove_watch(connection, netreg_watch);
2652 g_dbus_remove_watch(connection, context_watch);
2653 g_dbus_remove_watch(connection, context_removed_watch);
2654 g_dbus_remove_watch(connection, context_added_watch);
2655 g_dbus_remove_watch(connection, sim_watch);
2656 g_dbus_remove_watch(connection, cm_watch);
2657 g_dbus_remove_watch(connection, modem_watch);
2658 g_dbus_remove_watch(connection, modem_removed_watch);
2659 g_dbus_remove_watch(connection, modem_added_watch);
2660 g_dbus_remove_watch(connection, watch);
2661 dbus_connection_unref(connection);
2666 static void ofono_exit(void)
2670 if (modem_hash != NULL) {
2672 * We should propably wait for the SetProperty() reply
2673 * message, because ...
2675 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2678 * ... here we will cancel the call.
2680 g_hash_table_destroy(modem_hash);
2684 if (context_hash != NULL) {
2685 g_hash_table_destroy(context_hash);
2686 context_hash = NULL;
2689 connman_technology_driver_unregister(&tech_driver);
2690 connman_device_driver_unregister(&modem_driver);
2691 connman_network_driver_unregister(&network_driver);
2693 g_dbus_remove_watch(connection, cdma_netreg_watch);
2694 g_dbus_remove_watch(connection, cdma_cm_watch);
2695 g_dbus_remove_watch(connection, netreg_watch);
2696 g_dbus_remove_watch(connection, context_watch);
2697 g_dbus_remove_watch(connection, context_removed_watch);
2698 g_dbus_remove_watch(connection, context_added_watch);
2699 g_dbus_remove_watch(connection, sim_watch);
2700 g_dbus_remove_watch(connection, cm_watch);
2701 g_dbus_remove_watch(connection, modem_watch);
2702 g_dbus_remove_watch(connection, modem_added_watch);
2703 g_dbus_remove_watch(connection, modem_removed_watch);
2704 g_dbus_remove_watch(connection, watch);
2706 dbus_connection_unref(connection);
2709 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2710 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)