5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011 BWM Car IT GmbH. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/device.h>
38 #include <connman/network.h>
39 #include <connman/inet.h>
40 #include <connman/dbus.h>
41 #include <connman/log.h>
42 #include <connman/technology.h>
46 #define uninitialized_var(x) x = x
48 #define OFONO_SERVICE "org.ofono"
50 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
51 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
52 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
53 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
54 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
55 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
56 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
57 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
59 #define MODEM_ADDED "ModemAdded"
60 #define MODEM_REMOVED "ModemRemoved"
61 #define PROPERTY_CHANGED "PropertyChanged"
62 #define CONTEXT_ADDED "ContextAdded"
63 #define CONTEXT_REMOVED "ContextRemoved"
65 #define GET_PROPERTIES "GetProperties"
66 #define SET_PROPERTY "SetProperty"
67 #define GET_MODEMS "GetModems"
68 #define GET_CONTEXTS "GetContexts"
74 OFONO_API_NETREG = 0x2,
76 OFONO_API_CDMA_NETREG = 0x8,
77 OFONO_API_CDMA_CM = 0x10,
81 * The way this plugin works is following:
83 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
84 * attached -> netreg -> ready
86 * Depending on the modem type, this plugin will behave differently.
87 * For example CDMA CDMA modems don't have an IMSI. The following
88 * description is strictly about GSM modems.
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
109 static DBusConnection *connection;
111 static GHashTable *modem_hash;
112 static GHashTable *context_hash;
114 struct network_context {
118 enum connman_ipconfig_method ipv4_method;
119 struct connman_ipaddress *ipv4_address;
120 char *ipv4_nameservers;
122 enum connman_ipconfig_method ipv6_method;
123 struct connman_ipaddress *ipv6_address;
124 char *ipv6_nameservers;
130 struct connman_device *device;
131 struct connman_network *network;
133 struct network_context *context;
135 /* Modem Interface */
137 connman_bool_t powered;
138 connman_bool_t online;
140 connman_bool_t ignore;
142 connman_bool_t set_powered;
144 /* CDMA ConnectionManager Interface */
145 connman_bool_t cdma_cm_powered;
147 /* ConnectionManager Interface */
148 connman_bool_t attached;
149 connman_bool_t cm_powered;
151 /* ConnectionContext Interface */
152 connman_bool_t active;
153 connman_bool_t set_active;
155 /* SimManager Interface */
158 /* Netreg Interface */
161 uint8_t data_strength; /* 1xEVDO signal strength */
162 connman_bool_t roaming;
165 DBusPendingCall *call_set_property;
166 DBusPendingCall *call_get_properties;
167 DBusPendingCall *call_get_contexts;
170 static const char *api2string(enum ofono_api api)
175 case OFONO_API_NETREG:
179 case OFONO_API_CDMA_NETREG:
180 return "cdma-netreg";
181 case OFONO_API_CDMA_CM:
188 static char *get_ident(const char *path)
195 pos = strrchr(path, '/');
202 static struct network_context *network_context_alloc(const char *path)
204 struct network_context *context;
206 context = g_try_new0(struct network_context, 1);
210 context->path = g_strdup(path);
213 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
214 context->ipv4_address = NULL;
215 context->ipv4_nameservers = NULL;
217 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
218 context->ipv6_address = NULL;
219 context->ipv6_nameservers = NULL;
224 static void network_context_free(struct network_context *context)
226 g_free(context->path);
228 connman_ipaddress_free(context->ipv4_address);
229 g_free(context->ipv4_nameservers);
231 connman_ipaddress_free(context->ipv6_address);
232 g_free(context->ipv6_nameservers);
237 static void set_connected(struct modem_data *modem)
239 connman_bool_t setip = FALSE;
241 DBG("%s", modem->path);
243 connman_network_set_index(modem->network, modem->context->index);
245 switch (modem->context->ipv4_method) {
246 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
247 case CONNMAN_IPCONFIG_METHOD_OFF:
248 case CONNMAN_IPCONFIG_METHOD_MANUAL:
249 case CONNMAN_IPCONFIG_METHOD_AUTO:
252 case CONNMAN_IPCONFIG_METHOD_FIXED:
253 connman_network_set_ipv4_method(modem->network,
254 modem->context->ipv4_method);
255 connman_network_set_ipaddress(modem->network,
256 modem->context->ipv4_address);
257 connman_network_set_nameservers(modem->network,
258 modem->context->ipv4_nameservers);
262 case CONNMAN_IPCONFIG_METHOD_DHCP:
263 connman_network_set_ipv4_method(modem->network,
264 modem->context->ipv4_method);
269 switch (modem->context->ipv6_method) {
270 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
271 case CONNMAN_IPCONFIG_METHOD_OFF:
272 case CONNMAN_IPCONFIG_METHOD_MANUAL:
273 case CONNMAN_IPCONFIG_METHOD_DHCP:
274 case CONNMAN_IPCONFIG_METHOD_AUTO:
277 case CONNMAN_IPCONFIG_METHOD_FIXED:
278 connman_network_set_ipv6_method(modem->network,
279 modem->context->ipv6_method);
280 connman_network_set_ipaddress(modem->network,
281 modem->context->ipv6_address);
287 connman_network_set_connected(modem->network, TRUE);
290 static void set_disconnected(struct modem_data *modem)
292 DBG("%s", modem->path);
294 connman_network_set_connected(modem->network, FALSE);
297 typedef void (*set_property_cb)(struct modem_data *data,
298 connman_bool_t success);
299 typedef void (*get_properties_cb)(struct modem_data *data,
300 DBusMessageIter *dict);
302 struct property_info {
303 struct modem_data *modem;
305 const char *interface;
306 const char *property;
307 set_property_cb set_property_cb;
308 get_properties_cb get_properties_cb;
311 static void set_property_reply(DBusPendingCall *call, void *user_data)
313 struct property_info *info = user_data;
316 connman_bool_t success = TRUE;
318 DBG("%s path %s %s.%s", info->modem->path,
319 info->path, info->interface, info->property);
321 info->modem->call_set_property = NULL;
323 dbus_error_init(&error);
325 reply = dbus_pending_call_steal_reply(call);
327 if (dbus_set_error_from_message(&error, reply)) {
328 connman_error("Failed to change property: %s %s.%s: %s %s",
329 info->path, info->interface, info->property,
330 error.name, error.message);
331 dbus_error_free(&error);
335 if (info->set_property_cb != NULL)
336 (*info->set_property_cb)(info->modem, success);
338 dbus_message_unref(reply);
340 dbus_pending_call_unref(call);
343 static int set_property(struct modem_data *modem,
344 const char *path, const char *interface,
345 const char *property, int type, void *value,
346 set_property_cb notify)
348 DBusMessage *message;
349 DBusMessageIter iter;
350 struct property_info *info;
352 DBG("%s path %s %s.%s", modem->path, path, interface, property);
354 if (modem->call_set_property != NULL) {
355 DBG("Cancel pending SetProperty");
357 dbus_pending_call_cancel(modem->call_set_property);
358 modem->call_set_property = NULL;
361 message = dbus_message_new_method_call(OFONO_SERVICE, path,
362 interface, SET_PROPERTY);
366 dbus_message_iter_init_append(message, &iter);
367 connman_dbus_property_append_basic(&iter, property, type, value);
369 if (dbus_connection_send_with_reply(connection, message,
370 &modem->call_set_property, TIMEOUT) == FALSE) {
371 connman_error("Failed to change property: %s %s.%s",
372 path, interface, property);
373 dbus_message_unref(message);
377 if (modem->call_set_property == NULL) {
378 connman_error("D-Bus connection not available");
379 dbus_message_unref(message);
383 info = g_try_new0(struct property_info, 1);
385 dbus_message_unref(message);
391 info->interface = interface;
392 info->property = property;
393 info->set_property_cb = notify;
395 dbus_pending_call_set_notify(modem->call_set_property,
396 set_property_reply, info, g_free);
398 dbus_message_unref(message);
403 static void get_properties_reply(DBusPendingCall *call, void *user_data)
405 struct property_info *info = user_data;
406 DBusMessageIter array, dict;
410 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
412 info->modem->call_get_properties = NULL;
414 dbus_error_init(&error);
416 reply = dbus_pending_call_steal_reply(call);
418 if (dbus_set_error_from_message(&error, reply)) {
419 connman_error("Failed to get properties: %s %s: %s %s",
420 info->path, info->interface,
421 error.name, error.message);
422 dbus_error_free(&error);
427 if (dbus_message_iter_init(reply, &array) == FALSE)
430 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
433 dbus_message_iter_recurse(&array, &dict);
435 if (info->get_properties_cb != NULL)
436 (*info->get_properties_cb)(info->modem, &dict);
440 dbus_message_unref(reply);
442 dbus_pending_call_unref(call);
445 static int get_properties(const char *path, const char *interface,
446 get_properties_cb notify,
447 struct modem_data *modem)
449 DBusMessage *message;
450 struct property_info *info;
452 DBG("%s path %s %s", modem->path, path, interface);
454 if (modem->call_get_properties != NULL) {
455 connman_error("Pending GetProperties");
459 message = dbus_message_new_method_call(OFONO_SERVICE, path,
460 interface, GET_PROPERTIES);
464 if (dbus_connection_send_with_reply(connection, message,
465 &modem->call_get_properties, TIMEOUT) == FALSE) {
466 connman_error("Failed to call %s.GetProperties()", interface);
467 dbus_message_unref(message);
471 if (modem->call_get_properties == NULL) {
472 connman_error("D-Bus connection not available");
473 dbus_message_unref(message);
477 info = g_try_new0(struct property_info, 1);
479 dbus_message_unref(message);
485 info->interface = interface;
486 info->get_properties_cb = notify;
488 dbus_pending_call_set_notify(modem->call_get_properties,
489 get_properties_reply, info, g_free);
491 dbus_message_unref(message);
496 static void context_set_active_reply(struct modem_data *modem,
497 connman_bool_t success)
499 DBG("%s", modem->path);
501 if (success == TRUE) {
503 * Don't handle do anything on success here. oFono will send
504 * the change via PropertyChanged singal.
510 * Active = True might fail due a timeout. That means oFono
511 * still tries to go online. If we retry to set Active = True,
512 * we just get a InProgress error message. Should we power
513 * cycle the modem in such cases?
516 if (modem->network == NULL) {
518 * In the case where we power down the device
519 * we don't wait for the reply, therefore the network
520 * might already be gone.
525 connman_network_set_error(modem->network,
526 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
529 static int context_set_active(struct modem_data *modem,
530 connman_bool_t active)
534 DBG("%s active %d", modem->path, active);
536 err = set_property(modem, modem->context->path,
537 OFONO_CONTEXT_INTERFACE,
538 "Active", DBUS_TYPE_BOOLEAN,
540 context_set_active_reply);
542 if (active == FALSE && err == -EINPROGRESS)
548 static void cdma_cm_set_powered_reply(struct modem_data *modem,
549 connman_bool_t success)
551 DBG("%s", modem->path);
553 if (success == TRUE) {
555 * Don't handle do anything on success here. oFono will send
556 * the change via PropertyChanged singal.
562 * Powered = True might fail due a timeout. That means oFono
563 * still tries to go online. If we retry to set Powered = True,
564 * we just get a InProgress error message. Should we power
565 * cycle the modem in such cases?
568 if (modem->network == NULL) {
570 * In the case where we power down the device
571 * we don't wait for the reply, therefore the network
572 * might already be gone.
577 connman_network_set_error(modem->network,
578 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
581 static int cdma_cm_set_powered(struct modem_data *modem, connman_bool_t powered)
585 DBG("%s powered %d", modem->path, powered);
587 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
588 "Powered", DBUS_TYPE_BOOLEAN,
590 cdma_cm_set_powered_reply);
592 if (powered == FALSE && err == -EINPROGRESS)
598 static int modem_set_online(struct modem_data *modem, connman_bool_t online)
600 DBG("%s online %d", modem->path, online);
602 return set_property(modem, modem->path,
603 OFONO_MODEM_INTERFACE,
604 "Online", DBUS_TYPE_BOOLEAN,
609 static int cm_set_powered(struct modem_data *modem, connman_bool_t powered)
613 DBG("%s powered %d", modem->path, powered);
615 err = set_property(modem, modem->path,
617 "Powered", DBUS_TYPE_BOOLEAN,
621 if (powered == FALSE && err == -EINPROGRESS)
627 static int modem_set_powered(struct modem_data *modem, connman_bool_t powered)
631 DBG("%s powered %d", modem->path, powered);
633 modem->set_powered = powered;
635 err = set_property(modem, modem->path,
636 OFONO_MODEM_INTERFACE,
637 "Powered", DBUS_TYPE_BOOLEAN,
641 if (powered == FALSE && err == -EINPROGRESS)
647 static connman_bool_t has_interface(uint8_t interfaces,
650 if ((interfaces & api) == api)
656 static uint8_t extract_interfaces(DBusMessageIter *array)
658 DBusMessageIter entry;
659 uint8_t interfaces = 0;
661 dbus_message_iter_recurse(array, &entry);
663 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
666 dbus_message_iter_get_basic(&entry, &name);
668 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
669 interfaces |= OFONO_API_SIM;
670 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
671 interfaces |= OFONO_API_NETREG;
672 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
673 interfaces |= OFONO_API_CM;
674 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
675 interfaces |= OFONO_API_CDMA_CM;
676 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
677 interfaces |= OFONO_API_CDMA_NETREG;
679 dbus_message_iter_next(&entry);
685 static char *extract_nameservers(DBusMessageIter *array)
687 DBusMessageIter entry;
688 char *nameservers = NULL;
691 dbus_message_iter_recurse(array, &entry);
693 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
694 const char *nameserver;
696 dbus_message_iter_get_basic(&entry, &nameserver);
698 if (nameservers == NULL) {
699 nameservers = g_strdup(nameserver);
702 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
706 dbus_message_iter_next(&entry);
712 static void extract_ipv4_settings(DBusMessageIter *array,
713 struct network_context *context)
715 DBusMessageIter dict;
716 char *address = NULL, *netmask = NULL, *gateway = NULL;
717 char *nameservers = NULL;
718 const char *interface = NULL;
721 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
724 dbus_message_iter_recurse(array, &dict);
726 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
727 DBusMessageIter entry, value;
728 const char *key, *val;
730 dbus_message_iter_recurse(&dict, &entry);
731 dbus_message_iter_get_basic(&entry, &key);
733 dbus_message_iter_next(&entry);
734 dbus_message_iter_recurse(&entry, &value);
736 if (g_str_equal(key, "Interface") == TRUE) {
737 dbus_message_iter_get_basic(&value, &interface);
739 DBG("Interface %s", interface);
741 index = connman_inet_ifindex(interface);
743 DBG("index %d", index);
744 } else if (g_str_equal(key, "Method") == TRUE) {
745 dbus_message_iter_get_basic(&value, &val);
747 DBG("Method %s", val);
749 if (g_strcmp0(val, "static") == 0) {
750 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
751 } else if (g_strcmp0(val, "dhcp") == 0) {
752 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
755 } else if (g_str_equal(key, "Address") == TRUE) {
756 dbus_message_iter_get_basic(&value, &val);
758 address = g_strdup(val);
760 DBG("Address %s", address);
761 } else if (g_str_equal(key, "Netmask") == TRUE) {
762 dbus_message_iter_get_basic(&value, &val);
764 netmask = g_strdup(val);
766 DBG("Netmask %s", netmask);
767 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
768 nameservers = extract_nameservers(&value);
770 DBG("Nameservers %s", nameservers);
771 } else if (g_str_equal(key, "Gateway") == TRUE) {
772 dbus_message_iter_get_basic(&value, &val);
774 gateway = g_strdup(val);
776 DBG("Gateway %s", gateway);
779 dbus_message_iter_next(&dict);
785 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
788 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
789 if (context->ipv4_address == NULL)
792 context->index = index;
793 connman_ipaddress_set_ipv4(context->ipv4_address, address,
796 context->ipv4_nameservers = nameservers;
799 if (context->ipv4_nameservers != nameservers)
807 static void extract_ipv6_settings(DBusMessageIter *array,
808 struct network_context *context)
810 DBusMessageIter dict;
811 char *address = NULL, *gateway = NULL;
812 unsigned char prefix_length;
813 char *nameservers = NULL;
814 const char *interface = NULL;
817 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
820 dbus_message_iter_recurse(array, &dict);
822 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
823 DBusMessageIter entry, value;
824 const char *key, *val;
826 dbus_message_iter_recurse(&dict, &entry);
827 dbus_message_iter_get_basic(&entry, &key);
829 dbus_message_iter_next(&entry);
830 dbus_message_iter_recurse(&entry, &value);
832 if (g_str_equal(key, "Interface") == TRUE) {
833 dbus_message_iter_get_basic(&value, &interface);
835 DBG("Interface %s", interface);
837 index = connman_inet_ifindex(interface);
839 DBG("index %d", index);
840 } else if (g_str_equal(key, "Address") == TRUE) {
841 dbus_message_iter_get_basic(&value, &val);
843 address = g_strdup(val);
845 DBG("Address %s", address);
846 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
847 dbus_message_iter_get_basic(&value, &prefix_length);
849 DBG("prefix length %d", prefix_length);
850 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
851 nameservers = extract_nameservers(&value);
853 DBG("Nameservers %s", nameservers);
854 } else if (g_str_equal(key, "Gateway") == TRUE) {
855 dbus_message_iter_get_basic(&value, &val);
857 gateway = g_strdup(val);
859 DBG("Gateway %s", gateway);
862 dbus_message_iter_next(&dict);
868 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
870 context->ipv6_address =
871 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
872 if (context->ipv6_address == NULL)
875 context->index = index;
876 connman_ipaddress_set_ipv6(context->ipv6_address, address,
877 prefix_length, gateway);
879 context->ipv6_nameservers = nameservers;
882 if (context->ipv6_nameservers != nameservers)
889 static connman_bool_t ready_to_create_device(struct modem_data *modem)
892 * There are three different modem types which behave slightly
894 * - GSM modems will expose the SIM interface then the
896 * - DUN modems will expose first a unique serial number (BDADDR)
897 * and then the CM interface.
898 * - CDMA modems will expose CM first and sometime later
899 * a unique serial number.
901 * This functions tests if we have the necessary information gathered
902 * before we are able to create a device.
905 if (modem->device != NULL)
908 if (modem->imsi != NULL || modem->serial != NULL)
914 static void create_device(struct modem_data *modem)
916 struct connman_device *device;
917 char *uninitialized_var(ident);
919 DBG("%s", modem->path);
921 if (modem->imsi != NULL)
923 else if (modem->serial != NULL)
924 ident = modem->serial;
926 if (connman_dbus_validate_ident(ident) == FALSE)
927 ident = connman_dbus_encode_string(ident);
929 ident = g_strdup(ident);
931 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
935 DBG("device %p", device);
937 connman_device_set_ident(device, ident);
939 connman_device_set_string(device, "Path", modem->path);
941 connman_device_set_data(device, modem);
943 if (connman_device_register(device) < 0) {
944 connman_error("Failed to register cellular device");
945 connman_device_unref(device);
949 modem->device = device;
951 connman_device_set_powered(modem->device, modem->online);
956 static void destroy_device(struct modem_data *modem)
958 DBG("%s", modem->path);
960 connman_device_set_powered(modem->device, FALSE);
962 if (modem->network != NULL) {
963 connman_device_remove_network(modem->device, modem->network);
964 connman_network_unref(modem->network);
965 modem->network = NULL;
968 connman_device_unregister(modem->device);
969 connman_device_unref(modem->device);
971 modem->device = NULL;
974 static void add_network(struct modem_data *modem)
978 DBG("%s", modem->path);
980 if (modem->network != NULL)
983 modem->network = connman_network_create(modem->context->path,
984 CONNMAN_NETWORK_TYPE_CELLULAR);
985 if (modem->network == NULL)
988 DBG("network %p", modem->network);
990 connman_network_set_data(modem->network, modem);
992 connman_network_set_string(modem->network, "Path",
993 modem->context->path);
995 connman_network_set_index(modem->network, modem->context->index);
997 if (modem->name != NULL)
998 connman_network_set_name(modem->network, modem->name);
1000 connman_network_set_name(modem->network, "");
1002 connman_network_set_strength(modem->network, modem->strength);
1004 group = get_ident(modem->context->path);
1005 connman_network_set_group(modem->network, group);
1007 connman_network_set_available(modem->network, TRUE);
1009 connman_network_set_bool(modem->network, "Roaming",
1012 if (connman_device_add_network(modem->device, modem->network) < 0) {
1013 connman_network_unref(modem->network);
1014 modem->network = NULL;
1019 * Create the ipconfig layer before trying to connect. Withouth
1020 * the ipconfig layer the core is not ready to process errors.
1022 connman_network_set_index(modem->network, -1);
1025 static void remove_network(struct modem_data *modem)
1027 DBG("%s", modem->path);
1029 if (modem->network == NULL)
1032 DBG("network %p", modem->network);
1034 connman_device_remove_network(modem->device, modem->network);
1035 connman_network_unref(modem->network);
1036 modem->network = NULL;
1039 static int add_cm_context(struct modem_data *modem, const char *context_path,
1040 DBusMessageIter *dict)
1042 const char *context_type;
1043 struct network_context *context = NULL;
1044 connman_bool_t active = FALSE;
1046 DBG("%s context path %s", modem->path, context_path);
1048 if (modem->context != NULL) {
1050 * We have already assigned a context to this modem
1051 * and we do only support one Internet context.
1056 context = network_context_alloc(context_path);
1057 if (context == NULL)
1060 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1061 DBusMessageIter entry, value;
1064 dbus_message_iter_recurse(dict, &entry);
1065 dbus_message_iter_get_basic(&entry, &key);
1067 dbus_message_iter_next(&entry);
1068 dbus_message_iter_recurse(&entry, &value);
1070 if (g_str_equal(key, "Type") == TRUE) {
1071 dbus_message_iter_get_basic(&value, &context_type);
1073 DBG("%s context %s type %s", modem->path,
1074 context_path, context_type);
1075 } else if (g_str_equal(key, "Settings") == TRUE) {
1076 DBG("%s Settings", modem->path);
1078 extract_ipv4_settings(&value, context);
1079 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1080 DBG("%s IPv6.Settings", modem->path);
1082 extract_ipv6_settings(&value, context);
1083 } else if (g_str_equal(key, "Active") == TRUE) {
1084 dbus_message_iter_get_basic(&value, &active);
1086 DBG("%s Active %d", modem->path, active);
1089 dbus_message_iter_next(dict);
1092 if (g_strcmp0(context_type, "internet") != 0) {
1093 network_context_free(context);
1097 modem->context = context;
1098 modem->active = active;
1100 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1105 static void remove_cm_context(struct modem_data *modem,
1106 const char *context_path)
1108 if (modem->context == NULL)
1111 g_hash_table_remove(context_hash, context_path);
1113 network_context_free(modem->context);
1114 modem->context = NULL;
1117 static gboolean context_changed(DBusConnection *connection,
1118 DBusMessage *message,
1121 const char *context_path = dbus_message_get_path(message);
1122 struct modem_data *modem = NULL;
1123 DBusMessageIter iter, value;
1126 DBG("context_path %s", context_path);
1128 modem = g_hash_table_lookup(context_hash, context_path);
1132 if (dbus_message_iter_init(message, &iter) == FALSE)
1135 dbus_message_iter_get_basic(&iter, &key);
1137 dbus_message_iter_next(&iter);
1138 dbus_message_iter_recurse(&iter, &value);
1141 * oFono guarantees the ordering of Settings and
1142 * Active. Settings will always be send before Active = True.
1143 * That means we don't have to order here.
1145 if (g_str_equal(key, "Settings") == TRUE) {
1146 DBG("%s Settings", modem->path);
1148 extract_ipv4_settings(&value, modem->context);
1149 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1150 DBG("%s IPv6.Settings", modem->path);
1152 extract_ipv6_settings(&value, modem->context);
1153 } else if (g_str_equal(key, "Active") == TRUE) {
1154 dbus_message_iter_get_basic(&value, &modem->active);
1156 DBG("%s Active %d", modem->path, modem->active);
1158 if (modem->active == TRUE)
1159 set_connected(modem);
1161 set_disconnected(modem);
1167 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1169 struct modem_data *modem = user_data;
1170 DBusMessageIter array, dict, entry, value;
1174 DBG("%s", modem->path);
1176 modem->call_get_contexts = NULL;
1178 reply = dbus_pending_call_steal_reply(call);
1180 dbus_error_init(&error);
1182 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1183 connman_error("%s", error.message);
1184 dbus_error_free(&error);
1188 if (dbus_message_iter_init(reply, &array) == FALSE)
1191 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1194 dbus_message_iter_recurse(&array, &dict);
1196 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1197 const char *context_path;
1199 dbus_message_iter_recurse(&dict, &entry);
1200 dbus_message_iter_get_basic(&entry, &context_path);
1202 dbus_message_iter_next(&entry);
1203 dbus_message_iter_recurse(&entry, &value);
1205 if (add_cm_context(modem, context_path, &value) == 0)
1208 dbus_message_iter_next(&dict);
1212 dbus_message_unref(reply);
1214 dbus_pending_call_unref(call);
1217 static int cm_get_contexts(struct modem_data *modem)
1219 DBusMessage *message;
1221 DBG("%s", modem->path);
1223 if (modem->call_get_contexts != NULL)
1226 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1227 OFONO_CM_INTERFACE, GET_CONTEXTS);
1228 if (message == NULL)
1231 if (dbus_connection_send_with_reply(connection, message,
1232 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1233 connman_error("Failed to call GetContexts()");
1234 dbus_message_unref(message);
1238 if (modem->call_get_contexts == NULL) {
1239 connman_error("D-Bus connection not available");
1240 dbus_message_unref(message);
1244 dbus_pending_call_set_notify(modem->call_get_contexts,
1245 cm_get_contexts_reply,
1248 dbus_message_unref(message);
1250 return -EINPROGRESS;
1253 static gboolean cm_context_added(DBusConnection *connection,
1254 DBusMessage *message,
1257 const char *path = dbus_message_get_path(message);
1259 struct modem_data *modem;
1260 DBusMessageIter iter, properties;
1264 modem = g_hash_table_lookup(modem_hash, context_path);
1268 if (dbus_message_iter_init(message, &iter) == FALSE)
1271 dbus_message_iter_get_basic(&iter, &context_path);
1273 dbus_message_iter_next(&iter);
1274 dbus_message_iter_recurse(&iter, &properties);
1276 if (add_cm_context(modem, context_path, &properties) != 0)
1282 static gboolean cm_context_removed(DBusConnection *connection,
1283 DBusMessage *message,
1286 const char *path = dbus_message_get_path(message);
1287 const char *context_path;
1288 struct modem_data *modem;
1289 DBusMessageIter iter;
1291 DBG("context path %s", path);
1293 if (dbus_message_iter_init(message, &iter) == FALSE)
1296 dbus_message_iter_get_basic(&iter, &context_path);
1298 modem = g_hash_table_lookup(context_hash, context_path);
1302 remove_cm_context(modem, context_path);
1307 static void netreg_update_name(struct modem_data *modem,
1308 DBusMessageIter* value)
1312 dbus_message_iter_get_basic(value, &name);
1314 DBG("%s Name %s", modem->path, name);
1316 g_free(modem->name);
1317 modem->name = g_strdup(name);
1319 if (modem->network == NULL)
1322 connman_network_set_name(modem->network, modem->name);
1323 connman_network_update(modem->network);
1326 static void netreg_update_strength(struct modem_data *modem,
1327 DBusMessageIter *value)
1329 dbus_message_iter_get_basic(value, &modem->strength);
1331 DBG("%s Strength %d", modem->path, modem->strength);
1333 if (modem->network == NULL)
1338 * We don't have 2 signal notifications we always report the strength
1339 * signal. data_strength is always equal to 0.
1342 * In the case we have a data_strength signal (from 1xEVDO network)
1343 * we don't need to update the value with strength signal (from 1xCDMA)
1344 * because the modem is registered to 1xEVDO network for data call.
1345 * In case we have no data_strength signal (not registered to 1xEVDO
1346 * network), we must report the strength signal (registered to 1xCDMA
1347 * network e.g slow mode).
1349 if (modem->data_strength != 0)
1352 connman_network_set_strength(modem->network, modem->strength);
1353 connman_network_update(modem->network);
1356 /* Retrieve 1xEVDO Data Strength signal */
1357 static void netreg_update_datastrength(struct modem_data *modem,
1358 DBusMessageIter *value)
1360 dbus_message_iter_get_basic(value, &modem->data_strength);
1362 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1364 if (modem->network == NULL)
1368 * CDMA modem is not registered to 1xEVDO network, let
1369 * update_signal_strength() reporting the value on the Strength signal
1372 if (modem->data_strength == 0)
1375 connman_network_set_strength(modem->network, modem->data_strength);
1376 connman_network_update(modem->network);
1379 static void netreg_update_roaming(struct modem_data *modem,
1380 DBusMessageIter *value)
1383 connman_bool_t roaming;
1385 dbus_message_iter_get_basic(value, &status);
1387 if (g_str_equal(status, "roaming") == TRUE)
1392 if (roaming == modem->roaming)
1395 modem->roaming = roaming;
1397 if (modem->network == NULL)
1400 connman_network_set_bool(modem->network,
1401 "Roaming", modem->roaming);
1402 connman_network_update(modem->network);
1405 static void netreg_update_regdom(struct modem_data *modem,
1406 DBusMessageIter *value)
1408 char *mobile_country_code;
1412 dbus_message_iter_get_basic(value, &mobile_country_code);
1414 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1417 mcc = atoi(mobile_country_code);
1418 if (mcc > 799 || mcc < 200)
1421 alpha2 = mcc_country_codes[mcc - 200];
1423 connman_technology_set_regdom(alpha2);
1426 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1429 const char *path = dbus_message_get_path(message);
1430 struct modem_data *modem;
1431 DBusMessageIter iter, value;
1434 modem = g_hash_table_lookup(modem_hash, path);
1438 if (modem->ignore == TRUE)
1441 if (dbus_message_iter_init(message, &iter) == FALSE)
1444 dbus_message_iter_get_basic(&iter, &key);
1446 dbus_message_iter_next(&iter);
1447 dbus_message_iter_recurse(&iter, &value);
1449 if (g_str_equal(key, "Name") == TRUE)
1450 netreg_update_name(modem, &value);
1451 else if (g_str_equal(key, "Strength") == TRUE)
1452 netreg_update_strength(modem, &value);
1453 else if (g_str_equal(key, "Status") == TRUE)
1454 netreg_update_roaming(modem, &value);
1455 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1456 netreg_update_regdom(modem, &value);
1461 static void netreg_properties_reply(struct modem_data *modem,
1462 DBusMessageIter *dict)
1464 DBG("%s", modem->path);
1466 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1467 DBusMessageIter entry, value;
1470 dbus_message_iter_recurse(dict, &entry);
1471 dbus_message_iter_get_basic(&entry, &key);
1473 dbus_message_iter_next(&entry);
1474 dbus_message_iter_recurse(&entry, &value);
1476 if (g_str_equal(key, "Name") == TRUE)
1477 netreg_update_name(modem, &value);
1478 else if (g_str_equal(key, "Strength") == TRUE)
1479 netreg_update_strength(modem, &value);
1480 else if (g_str_equal(key, "Status") == TRUE)
1481 netreg_update_roaming(modem, &value);
1482 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1483 netreg_update_regdom(modem, &value);
1485 dbus_message_iter_next(dict);
1488 if (modem->context == NULL) {
1490 * netgreg_get_properties() was issued after we got
1491 * cm_get_contexts_reply() where we create the
1492 * context. Though before we got the
1493 * netreg_properties_reply the context was removed
1494 * again. Therefore we have to skip the network
1502 if (modem->active == TRUE)
1503 set_connected(modem);
1506 static int netreg_get_properties(struct modem_data *modem)
1508 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1509 netreg_properties_reply, modem);
1512 static void add_cdma_network(struct modem_data *modem)
1514 /* Be sure that device is created before adding CDMA network */
1515 if (modem->device == NULL)
1519 * CDMA modems don't need contexts for data call, however the current
1520 * add_network() logic needs one, so we create one to proceed.
1522 if (modem->context == NULL)
1523 modem->context = network_context_alloc(modem->path);
1525 if (modem->name == NULL)
1526 modem->name = g_strdup("CDMA Network");
1530 if (modem->cdma_cm_powered == TRUE)
1531 set_connected(modem);
1534 static gboolean cdma_netreg_changed(DBusConnection *connection,
1535 DBusMessage *message,
1538 const char *path = dbus_message_get_path(message);
1539 struct modem_data *modem;
1540 DBusMessageIter iter, value;
1545 modem = g_hash_table_lookup(modem_hash, path);
1549 if (modem->ignore == TRUE)
1552 if (dbus_message_iter_init(message, &iter) == FALSE)
1555 dbus_message_iter_get_basic(&iter, &key);
1557 dbus_message_iter_next(&iter);
1558 dbus_message_iter_recurse(&iter, &value);
1560 if (g_str_equal(key, "Name") == TRUE)
1561 netreg_update_name(modem, &value);
1562 else if (g_str_equal(key, "Strength") == TRUE)
1563 netreg_update_strength(modem, &value);
1564 else if (g_str_equal(key, "DataStrength") == TRUE)
1565 netreg_update_datastrength(modem, &value);
1566 else if (g_str_equal(key, "Status") == TRUE)
1567 netreg_update_roaming(modem, &value);
1569 add_cdma_network(modem);
1574 static void cdma_netreg_properties_reply(struct modem_data *modem,
1575 DBusMessageIter *dict)
1577 DBG("%s", modem->path);
1579 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1580 DBusMessageIter entry, value;
1583 dbus_message_iter_recurse(dict, &entry);
1584 dbus_message_iter_get_basic(&entry, &key);
1586 dbus_message_iter_next(&entry);
1587 dbus_message_iter_recurse(&entry, &value);
1589 if (g_str_equal(key, "Name") == TRUE)
1590 netreg_update_name(modem, &value);
1591 else if (g_str_equal(key, "Strength") == TRUE)
1592 netreg_update_strength(modem, &value);
1593 else if (g_str_equal(key, "DataStrength") == TRUE)
1594 netreg_update_datastrength(modem, &value);
1595 else if (g_str_equal(key, "Status") == TRUE)
1596 netreg_update_roaming(modem, &value);
1598 dbus_message_iter_next(dict);
1601 add_cdma_network(modem);
1604 static int cdma_netreg_get_properties(struct modem_data *modem)
1606 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1607 cdma_netreg_properties_reply, modem);
1610 static void cm_update_attached(struct modem_data *modem,
1611 DBusMessageIter *value)
1613 dbus_message_iter_get_basic(value, &modem->attached);
1615 DBG("%s Attached %d", modem->path, modem->attached);
1617 if (modem->attached == FALSE)
1620 if (has_interface(modem->interfaces,
1621 OFONO_API_NETREG) == FALSE) {
1625 netreg_get_properties(modem);
1628 static void cm_update_powered(struct modem_data *modem,
1629 DBusMessageIter *value)
1631 dbus_message_iter_get_basic(value, &modem->cm_powered);
1633 DBG("%s ConnnectionManager Powered %d", modem->path,
1636 if (modem->cm_powered == TRUE)
1639 cm_set_powered(modem, TRUE);
1642 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1645 const char *path = dbus_message_get_path(message);
1646 struct modem_data *modem;
1647 DBusMessageIter iter, value;
1650 modem = g_hash_table_lookup(modem_hash, path);
1654 if (modem->ignore == TRUE)
1657 if (dbus_message_iter_init(message, &iter) == FALSE)
1660 dbus_message_iter_get_basic(&iter, &key);
1662 dbus_message_iter_next(&iter);
1663 dbus_message_iter_recurse(&iter, &value);
1665 if (g_str_equal(key, "Attached") == TRUE)
1666 cm_update_attached(modem, &value);
1667 else if (g_str_equal(key, "Powered") == TRUE)
1668 cm_update_powered(modem, &value);
1673 static void cdma_cm_update_powered(struct modem_data *modem,
1674 DBusMessageIter *value)
1676 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1678 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1680 if (modem->network == NULL)
1683 if (modem->cdma_cm_powered == TRUE)
1684 set_connected(modem);
1686 set_disconnected(modem);
1689 static void cdma_cm_update_settings(struct modem_data *modem,
1690 DBusMessageIter *value)
1692 DBG("%s Settings", modem->path);
1694 extract_ipv4_settings(value, modem->context);
1697 static gboolean cdma_cm_changed(DBusConnection *connection,
1698 DBusMessage *message, void *user_data)
1700 const char *path = dbus_message_get_path(message);
1701 struct modem_data *modem;
1702 DBusMessageIter iter, value;
1705 modem = g_hash_table_lookup(modem_hash, path);
1709 if (modem->online == TRUE && modem->network == NULL)
1710 cdma_netreg_get_properties(modem);
1712 if (dbus_message_iter_init(message, &iter) == FALSE)
1715 dbus_message_iter_get_basic(&iter, &key);
1717 dbus_message_iter_next(&iter);
1718 dbus_message_iter_recurse(&iter, &value);
1720 if (g_str_equal(key, "Powered") == TRUE)
1721 cdma_cm_update_powered(modem, &value);
1722 if (g_str_equal(key, "Settings") == TRUE)
1723 cdma_cm_update_settings(modem, &value);
1728 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1730 DBG("%s", modem->path);
1732 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1733 DBusMessageIter entry, value;
1736 dbus_message_iter_recurse(dict, &entry);
1737 dbus_message_iter_get_basic(&entry, &key);
1739 dbus_message_iter_next(&entry);
1740 dbus_message_iter_recurse(&entry, &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);
1747 dbus_message_iter_next(dict);
1751 static int cm_get_properties(struct modem_data *modem)
1753 return get_properties(modem->path, OFONO_CM_INTERFACE,
1754 cm_properties_reply, modem);
1757 static void cdma_cm_properties_reply(struct modem_data *modem,
1758 DBusMessageIter *dict)
1760 DBG("%s", modem->path);
1762 if (modem->online == TRUE)
1763 cdma_netreg_get_properties(modem);
1765 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1766 DBusMessageIter entry, value;
1769 dbus_message_iter_recurse(dict, &entry);
1770 dbus_message_iter_get_basic(&entry, &key);
1772 dbus_message_iter_next(&entry);
1773 dbus_message_iter_recurse(&entry, &value);
1775 if (g_str_equal(key, "Powered") == TRUE)
1776 cdma_cm_update_powered(modem, &value);
1777 if (g_str_equal(key, "Settings") == TRUE)
1778 cdma_cm_update_settings(modem, &value);
1780 dbus_message_iter_next(dict);
1784 static int cdma_cm_get_properties(struct modem_data *modem)
1786 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1787 cdma_cm_properties_reply, modem);
1790 static void sim_update_imsi(struct modem_data *modem,
1791 DBusMessageIter* value)
1795 dbus_message_iter_get_basic(value, &imsi);
1797 DBG("%s imsi %s", modem->path, imsi);
1799 g_free(modem->imsi);
1800 modem->imsi = g_strdup(imsi);
1803 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1806 const char *path = dbus_message_get_path(message);
1807 struct modem_data *modem;
1808 DBusMessageIter iter, value;
1811 modem = g_hash_table_lookup(modem_hash, path);
1815 if (modem->ignore == TRUE)
1818 if (dbus_message_iter_init(message, &iter) == FALSE)
1821 dbus_message_iter_get_basic(&iter, &key);
1823 dbus_message_iter_next(&iter);
1824 dbus_message_iter_recurse(&iter, &value);
1826 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1827 sim_update_imsi(modem, &value);
1829 if (ready_to_create_device(modem) == FALSE)
1833 * This is a GSM modem. Create the device and
1834 * register it at the core. Enabling (setting
1835 * it online is done through the
1836 * modem_enable() callback.
1838 create_device(modem);
1844 static void sim_properties_reply(struct modem_data *modem,
1845 DBusMessageIter *dict)
1847 DBG("%s", modem->path);
1849 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1850 DBusMessageIter entry, value;
1853 dbus_message_iter_recurse(dict, &entry);
1854 dbus_message_iter_get_basic(&entry, &key);
1856 dbus_message_iter_next(&entry);
1857 dbus_message_iter_recurse(&entry, &value);
1859 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1860 sim_update_imsi(modem, &value);
1862 if (ready_to_create_device(modem) == FALSE)
1866 * This is a GSM modem. Create the device and
1867 * register it at the core. Enabling (setting
1868 * it online is done through the
1869 * modem_enable() callback.
1871 create_device(modem);
1873 if (modem->online == FALSE)
1877 * The modem is already online and we have the CM interface.
1878 * There will be no interface update and therefore our
1879 * state machine will not go to next step. We have to
1880 * trigger it from here.
1882 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1883 cm_get_properties(modem);
1884 cm_get_contexts(modem);
1889 dbus_message_iter_next(dict);
1893 static int sim_get_properties(struct modem_data *modem)
1895 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1896 sim_properties_reply, modem);
1899 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1902 if (has_interface(old_iface, api) == FALSE &&
1903 has_interface(new_iface, api) == TRUE) {
1904 DBG("%s added", api2string(api));
1911 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1914 if (has_interface(old_iface, api) == TRUE &&
1915 has_interface(new_iface, api) == FALSE) {
1916 DBG("%s removed", api2string(api));
1923 static void modem_update_interfaces(struct modem_data *modem,
1927 DBG("%s", modem->path);
1929 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
1930 if (modem->imsi == NULL &&
1931 modem->set_powered == FALSE) {
1933 * Only use do GetProperties() when
1934 * device has not been powered up.
1936 sim_get_properties(modem);
1940 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1941 if (modem->device != NULL) {
1942 cm_get_properties(modem);
1943 cm_get_contexts(modem);
1947 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1948 if (ready_to_create_device(modem) == TRUE)
1949 create_device(modem);
1951 if (modem->device != NULL)
1952 cdma_cm_get_properties(modem);
1955 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1956 if (modem->attached == TRUE)
1957 netreg_get_properties(modem);
1960 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
1961 cdma_netreg_get_properties(modem);
1964 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1965 remove_cm_context(modem, modem->context->path);
1968 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1969 remove_cm_context(modem, modem->context->path);
1972 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1973 remove_network(modem);
1976 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
1977 remove_network(modem);
1981 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1984 const char *path = dbus_message_get_path(message);
1985 struct modem_data *modem;
1986 DBusMessageIter iter, value;
1989 modem = g_hash_table_lookup(modem_hash, path);
1993 if (modem->ignore == TRUE)
1996 if (dbus_message_iter_init(message, &iter) == FALSE)
1999 dbus_message_iter_get_basic(&iter, &key);
2001 dbus_message_iter_next(&iter);
2002 dbus_message_iter_recurse(&iter, &value);
2004 if (g_str_equal(key, "Powered") == TRUE) {
2005 dbus_message_iter_get_basic(&value, &modem->powered);
2007 DBG("%s Powered %d", modem->path, modem->powered);
2009 if (modem->powered == FALSE)
2010 modem_set_powered(modem, TRUE);
2011 } else if (g_str_equal(key, "Online") == TRUE) {
2012 dbus_message_iter_get_basic(&value, &modem->online);
2014 DBG("%s Online %d", modem->path, modem->online);
2016 if (modem->device == NULL)
2019 connman_device_set_powered(modem->device, modem->online);
2020 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2023 interfaces = extract_interfaces(&value);
2025 if (interfaces == modem->interfaces)
2028 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2030 modem_update_interfaces(modem, modem->interfaces, interfaces);
2032 modem->interfaces = interfaces;
2033 } else if (g_str_equal(key, "Serial") == TRUE) {
2036 dbus_message_iter_get_basic(&value, &serial);
2038 g_free(modem->serial);
2039 modem->serial = g_strdup(serial);
2041 DBG("%s Serial %s", modem->path, modem->serial);
2043 if (has_interface(modem->interfaces,
2044 OFONO_API_CDMA_CM) == TRUE) {
2045 if (ready_to_create_device(modem) == TRUE)
2046 create_device(modem);
2053 static void add_modem(const char *path, DBusMessageIter *prop)
2055 struct modem_data *modem;
2059 modem = g_hash_table_lookup(modem_hash, path);
2060 if (modem != NULL) {
2062 * When oFono powers up we ask for the modems and oFono is
2063 * reporting with modem_added signal the modems. Only
2069 modem = g_try_new0(struct modem_data, 1);
2073 modem->path = g_strdup(path);
2075 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2077 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2078 DBusMessageIter entry, value;
2081 dbus_message_iter_recurse(prop, &entry);
2082 dbus_message_iter_get_basic(&entry, &key);
2084 dbus_message_iter_next(&entry);
2085 dbus_message_iter_recurse(&entry, &value);
2087 if (g_str_equal(key, "Powered") == TRUE) {
2088 dbus_message_iter_get_basic(&value, &modem->powered);
2090 DBG("%s Powered %d", modem->path, modem->powered);
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);
2095 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2096 modem->interfaces = extract_interfaces(&value);
2098 DBG("%s Interfaces 0x%02x", modem->path,
2100 } else if (g_str_equal(key, "Serial") == TRUE) {
2103 dbus_message_iter_get_basic(&value, &serial);
2104 modem->serial = g_strdup(serial);
2106 DBG("%s Serial %s", modem->path, modem->serial);
2107 } else if (g_str_equal(key, "Type") == TRUE) {
2110 dbus_message_iter_get_basic(&value, &type);
2112 DBG("%s Type %s", modem->path, type);
2113 if (g_strcmp0(type, "hardware") != 0) {
2114 DBG("%s Ignore this modem", modem->path);
2115 modem->ignore = TRUE;
2119 dbus_message_iter_next(prop);
2122 if (modem->ignore == TRUE)
2125 if (modem->powered == FALSE) {
2126 modem_set_powered(modem, TRUE);
2130 modem_update_interfaces(modem, 0, modem->interfaces);
2133 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2135 struct modem_data *modem = value;
2137 DBG("%s", modem->path);
2139 if (modem->ignore == TRUE)
2142 modem_set_powered(modem, FALSE);
2145 static void remove_modem(gpointer data)
2147 struct modem_data *modem = data;
2149 DBG("%s", modem->path);
2151 if (modem->call_set_property != NULL)
2152 dbus_pending_call_cancel(modem->call_set_property);
2154 if (modem->call_get_properties != NULL)
2155 dbus_pending_call_cancel(modem->call_get_properties);
2157 if (modem->call_get_contexts != NULL)
2158 dbus_pending_call_cancel(modem->call_get_contexts);
2160 if (modem->device != NULL)
2161 destroy_device(modem);
2163 if (modem->context != NULL)
2164 remove_cm_context(modem, modem->context->path);
2166 g_free(modem->serial);
2167 g_free(modem->name);
2168 g_free(modem->imsi);
2169 g_free(modem->path);
2174 static gboolean modem_added(DBusConnection *connection,
2175 DBusMessage *message, void *user_data)
2177 DBusMessageIter iter, properties;
2182 if (dbus_message_iter_init(message, &iter) == FALSE)
2185 dbus_message_iter_get_basic(&iter, &path);
2187 dbus_message_iter_next(&iter);
2188 dbus_message_iter_recurse(&iter, &properties);
2190 add_modem(path, &properties);
2195 static gboolean modem_removed(DBusConnection *connection,
2196 DBusMessage *message, void *user_data)
2198 DBusMessageIter iter;
2203 if (dbus_message_iter_init(message, &iter) == FALSE)
2206 dbus_message_iter_get_basic(&iter, &path);
2208 g_hash_table_remove(modem_hash, path);
2213 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2217 DBusMessageIter array, dict;
2221 reply = dbus_pending_call_steal_reply(call);
2223 dbus_error_init(&error);
2225 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2226 connman_error("%s", error.message);
2227 dbus_error_free(&error);
2231 if (dbus_message_iter_init(reply, &array) == FALSE)
2234 dbus_message_iter_recurse(&array, &dict);
2236 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2237 DBusMessageIter value, properties;
2240 dbus_message_iter_recurse(&dict, &value);
2241 dbus_message_iter_get_basic(&value, &path);
2243 dbus_message_iter_next(&value);
2244 dbus_message_iter_recurse(&value, &properties);
2246 add_modem(path, &properties);
2248 dbus_message_iter_next(&dict);
2252 dbus_message_unref(reply);
2254 dbus_pending_call_unref(call);
2257 static int manager_get_modems(void)
2259 DBusMessage *message;
2260 DBusPendingCall *call;
2264 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2265 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2266 if (message == NULL)
2269 if (dbus_connection_send_with_reply(connection, message,
2270 &call, TIMEOUT) == FALSE) {
2271 connman_error("Failed to call GetModems()");
2272 dbus_message_unref(message);
2277 connman_error("D-Bus connection not available");
2278 dbus_message_unref(message);
2282 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2285 dbus_message_unref(message);
2287 return -EINPROGRESS;
2290 static void ofono_connect(DBusConnection *conn, void *user_data)
2294 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2295 g_free, remove_modem);
2296 if (modem_hash == NULL)
2299 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2301 if (context_hash == NULL) {
2302 g_hash_table_destroy(modem_hash);
2306 manager_get_modems();
2309 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2313 if (modem_hash == NULL || context_hash == NULL)
2316 g_hash_table_destroy(modem_hash);
2319 g_hash_table_destroy(context_hash);
2320 context_hash = NULL;
2323 static int network_probe(struct connman_network *network)
2325 struct modem_data *modem = connman_network_get_data(network);
2327 DBG("%s network %p", modem->path, network);
2332 static void network_remove(struct connman_network *network)
2334 struct modem_data *modem = connman_network_get_data(network);
2336 DBG("%s network %p", modem->path, network);
2339 static int network_connect(struct connman_network *network)
2341 struct modem_data *modem = connman_network_get_data(network);
2343 DBG("%s network %p", modem->path, network);
2345 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2346 return context_set_active(modem, TRUE);
2347 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2348 return cdma_cm_set_powered(modem, TRUE);
2350 connman_error("Connection manager interface not available");
2355 static int network_disconnect(struct connman_network *network)
2357 struct modem_data *modem = connman_network_get_data(network);
2359 DBG("%s network %p", modem->path, network);
2361 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2362 return context_set_active(modem, FALSE);
2363 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2364 return cdma_cm_set_powered(modem, FALSE);
2366 connman_error("Connection manager interface not available");
2371 static struct connman_network_driver network_driver = {
2373 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2374 .probe = network_probe,
2375 .remove = network_remove,
2376 .connect = network_connect,
2377 .disconnect = network_disconnect,
2380 static int modem_probe(struct connman_device *device)
2382 struct modem_data *modem = connman_device_get_data(device);
2384 DBG("%s device %p", modem->path, device);
2389 static void modem_remove(struct connman_device *device)
2391 struct modem_data *modem = connman_device_get_data(device);
2393 DBG("%s device %p", modem->path, device);
2396 static int modem_enable(struct connman_device *device)
2398 struct modem_data *modem = connman_device_get_data(device);
2400 DBG("%s device %p", modem->path, device);
2402 if (modem->online == TRUE)
2405 return modem_set_online(modem, TRUE);
2408 static int modem_disable(struct connman_device *device)
2410 struct modem_data *modem = connman_device_get_data(device);
2412 DBG("%s device %p", modem->path, device);
2414 if (modem->online == FALSE)
2417 return modem_set_online(modem, FALSE);
2420 static struct connman_device_driver modem_driver = {
2422 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2423 .probe = modem_probe,
2424 .remove = modem_remove,
2425 .enable = modem_enable,
2426 .disable = modem_disable,
2429 static int tech_probe(struct connman_technology *technology)
2434 static void tech_remove(struct connman_technology *technology)
2438 static struct connman_technology_driver tech_driver = {
2440 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2441 .probe = tech_probe,
2442 .remove = tech_remove,
2446 static guint modem_added_watch;
2447 static guint modem_removed_watch;
2448 static guint modem_watch;
2449 static guint cm_watch;
2450 static guint sim_watch;
2451 static guint context_added_watch;
2452 static guint context_removed_watch;
2453 static guint netreg_watch;
2454 static guint context_watch;
2455 static guint cdma_cm_watch;
2456 static guint cdma_netreg_watch;
2458 static int ofono_init(void)
2464 connection = connman_dbus_get_connection();
2465 if (connection == NULL)
2468 watch = g_dbus_add_service_watch(connection,
2469 OFONO_SERVICE, ofono_connect,
2470 ofono_disconnect, NULL, NULL);
2472 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2473 OFONO_MANAGER_INTERFACE,
2478 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2479 OFONO_MANAGER_INTERFACE,
2484 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2485 OFONO_MODEM_INTERFACE,
2490 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2496 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2497 OFONO_SIM_INTERFACE,
2502 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2508 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2514 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2515 OFONO_CONTEXT_INTERFACE,
2520 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2521 OFONO_NETREG_INTERFACE,
2526 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2527 OFONO_CDMA_CM_INTERFACE,
2532 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2533 OFONO_CDMA_NETREG_INTERFACE,
2535 cdma_netreg_changed,
2539 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2540 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2541 context_added_watch == 0 ||
2542 context_removed_watch == 0 ||
2543 context_watch == 0 || netreg_watch == 0 ||
2544 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2549 err = connman_network_driver_register(&network_driver);
2553 err = connman_device_driver_register(&modem_driver);
2555 connman_network_driver_unregister(&network_driver);
2559 err = connman_technology_driver_register(&tech_driver);
2561 connman_device_driver_unregister(&modem_driver);
2562 connman_network_driver_unregister(&network_driver);
2569 g_dbus_remove_watch(connection, cdma_netreg_watch);
2570 g_dbus_remove_watch(connection, cdma_cm_watch);
2571 g_dbus_remove_watch(connection, netreg_watch);
2572 g_dbus_remove_watch(connection, context_watch);
2573 g_dbus_remove_watch(connection, context_removed_watch);
2574 g_dbus_remove_watch(connection, context_added_watch);
2575 g_dbus_remove_watch(connection, sim_watch);
2576 g_dbus_remove_watch(connection, cm_watch);
2577 g_dbus_remove_watch(connection, modem_watch);
2578 g_dbus_remove_watch(connection, modem_removed_watch);
2579 g_dbus_remove_watch(connection, modem_added_watch);
2580 g_dbus_remove_watch(connection, watch);
2581 dbus_connection_unref(connection);
2586 static void ofono_exit(void)
2590 if (modem_hash != NULL) {
2592 * We should propably wait for the SetProperty() reply
2593 * message, because ...
2595 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2598 * ... here we will cancel the call.
2600 g_hash_table_destroy(modem_hash);
2604 if (context_hash != NULL) {
2605 g_hash_table_destroy(context_hash);
2606 context_hash = NULL;
2609 connman_technology_driver_unregister(&tech_driver);
2610 connman_device_driver_unregister(&modem_driver);
2611 connman_network_driver_unregister(&network_driver);
2613 g_dbus_remove_watch(connection, cdma_netreg_watch);
2614 g_dbus_remove_watch(connection, cdma_cm_watch);
2615 g_dbus_remove_watch(connection, netreg_watch);
2616 g_dbus_remove_watch(connection, context_watch);
2617 g_dbus_remove_watch(connection, context_removed_watch);
2618 g_dbus_remove_watch(connection, context_added_watch);
2619 g_dbus_remove_watch(connection, sim_watch);
2620 g_dbus_remove_watch(connection, cm_watch);
2621 g_dbus_remove_watch(connection, modem_watch);
2622 g_dbus_remove_watch(connection, modem_added_watch);
2623 g_dbus_remove_watch(connection, modem_removed_watch);
2624 g_dbus_remove_watch(connection, watch);
2626 dbus_connection_unref(connection);
2629 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2630 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)