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);
1421 alpha2 = mcc_country_codes[mcc - 200];
1422 connman_technology_set_regdom(alpha2);
1425 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1428 const char *path = dbus_message_get_path(message);
1429 struct modem_data *modem;
1430 DBusMessageIter iter, value;
1433 modem = g_hash_table_lookup(modem_hash, path);
1437 if (modem->ignore == TRUE)
1440 if (dbus_message_iter_init(message, &iter) == FALSE)
1443 dbus_message_iter_get_basic(&iter, &key);
1445 dbus_message_iter_next(&iter);
1446 dbus_message_iter_recurse(&iter, &value);
1448 if (g_str_equal(key, "Name") == TRUE)
1449 netreg_update_name(modem, &value);
1450 else if (g_str_equal(key, "Strength") == TRUE)
1451 netreg_update_strength(modem, &value);
1452 else if (g_str_equal(key, "Status") == TRUE)
1453 netreg_update_roaming(modem, &value);
1454 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1455 netreg_update_regdom(modem, &value);
1460 static void netreg_properties_reply(struct modem_data *modem,
1461 DBusMessageIter *dict)
1463 DBG("%s", modem->path);
1465 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1466 DBusMessageIter entry, value;
1469 dbus_message_iter_recurse(dict, &entry);
1470 dbus_message_iter_get_basic(&entry, &key);
1472 dbus_message_iter_next(&entry);
1473 dbus_message_iter_recurse(&entry, &value);
1475 if (g_str_equal(key, "Name") == TRUE)
1476 netreg_update_name(modem, &value);
1477 else if (g_str_equal(key, "Strength") == TRUE)
1478 netreg_update_strength(modem, &value);
1479 else if (g_str_equal(key, "Status") == TRUE)
1480 netreg_update_roaming(modem, &value);
1481 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
1482 netreg_update_regdom(modem, &value);
1484 dbus_message_iter_next(dict);
1487 if (modem->context == NULL) {
1489 * netgreg_get_properties() was issued after we got
1490 * cm_get_contexts_reply() where we create the
1491 * context. Though before we got the
1492 * netreg_properties_reply the context was removed
1493 * again. Therefore we have to skip the network
1501 if (modem->active == TRUE)
1502 set_connected(modem);
1505 static int netreg_get_properties(struct modem_data *modem)
1507 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1508 netreg_properties_reply, modem);
1511 static void add_cdma_network(struct modem_data *modem)
1513 /* Be sure that device is created before adding CDMA network */
1514 if (modem->device == NULL)
1518 * CDMA modems don't need contexts for data call, however the current
1519 * add_network() logic needs one, so we create one to proceed.
1521 if (modem->context == NULL)
1522 modem->context = network_context_alloc(modem->path);
1524 if (modem->name == NULL)
1525 modem->name = g_strdup("CDMA Network");
1529 if (modem->cdma_cm_powered == TRUE)
1530 set_connected(modem);
1533 static gboolean cdma_netreg_changed(DBusConnection *connection,
1534 DBusMessage *message,
1537 const char *path = dbus_message_get_path(message);
1538 struct modem_data *modem;
1539 DBusMessageIter iter, value;
1544 modem = g_hash_table_lookup(modem_hash, path);
1548 if (modem->ignore == TRUE)
1551 if (dbus_message_iter_init(message, &iter) == FALSE)
1554 dbus_message_iter_get_basic(&iter, &key);
1556 dbus_message_iter_next(&iter);
1557 dbus_message_iter_recurse(&iter, &value);
1559 if (g_str_equal(key, "Name") == TRUE)
1560 netreg_update_name(modem, &value);
1561 else if (g_str_equal(key, "Strength") == TRUE)
1562 netreg_update_strength(modem, &value);
1563 else if (g_str_equal(key, "DataStrength") == TRUE)
1564 netreg_update_datastrength(modem, &value);
1565 else if (g_str_equal(key, "Status") == TRUE)
1566 netreg_update_roaming(modem, &value);
1568 add_cdma_network(modem);
1573 static void cdma_netreg_properties_reply(struct modem_data *modem,
1574 DBusMessageIter *dict)
1576 DBG("%s", modem->path);
1578 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1579 DBusMessageIter entry, value;
1582 dbus_message_iter_recurse(dict, &entry);
1583 dbus_message_iter_get_basic(&entry, &key);
1585 dbus_message_iter_next(&entry);
1586 dbus_message_iter_recurse(&entry, &value);
1588 if (g_str_equal(key, "Name") == TRUE)
1589 netreg_update_name(modem, &value);
1590 else if (g_str_equal(key, "Strength") == TRUE)
1591 netreg_update_strength(modem, &value);
1592 else if (g_str_equal(key, "DataStrength") == TRUE)
1593 netreg_update_datastrength(modem, &value);
1594 else if (g_str_equal(key, "Status") == TRUE)
1595 netreg_update_roaming(modem, &value);
1597 dbus_message_iter_next(dict);
1600 add_cdma_network(modem);
1603 static int cdma_netreg_get_properties(struct modem_data *modem)
1605 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1606 cdma_netreg_properties_reply, modem);
1609 static void cm_update_attached(struct modem_data *modem,
1610 DBusMessageIter *value)
1612 dbus_message_iter_get_basic(value, &modem->attached);
1614 DBG("%s Attached %d", modem->path, modem->attached);
1616 if (modem->attached == FALSE)
1619 if (has_interface(modem->interfaces,
1620 OFONO_API_NETREG) == FALSE) {
1624 netreg_get_properties(modem);
1627 static void cm_update_powered(struct modem_data *modem,
1628 DBusMessageIter *value)
1630 dbus_message_iter_get_basic(value, &modem->cm_powered);
1632 DBG("%s ConnnectionManager Powered %d", modem->path,
1635 if (modem->cm_powered == TRUE)
1638 cm_set_powered(modem, TRUE);
1641 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1644 const char *path = dbus_message_get_path(message);
1645 struct modem_data *modem;
1646 DBusMessageIter iter, value;
1649 modem = g_hash_table_lookup(modem_hash, path);
1653 if (modem->ignore == TRUE)
1656 if (dbus_message_iter_init(message, &iter) == FALSE)
1659 dbus_message_iter_get_basic(&iter, &key);
1661 dbus_message_iter_next(&iter);
1662 dbus_message_iter_recurse(&iter, &value);
1664 if (g_str_equal(key, "Attached") == TRUE)
1665 cm_update_attached(modem, &value);
1666 else if (g_str_equal(key, "Powered") == TRUE)
1667 cm_update_powered(modem, &value);
1672 static void cdma_cm_update_powered(struct modem_data *modem,
1673 DBusMessageIter *value)
1675 dbus_message_iter_get_basic(value, &modem->cdma_cm_powered);
1677 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
1679 if (modem->network == NULL)
1682 if (modem->cdma_cm_powered == TRUE)
1683 set_connected(modem);
1685 set_disconnected(modem);
1688 static void cdma_cm_update_settings(struct modem_data *modem,
1689 DBusMessageIter *value)
1691 DBG("%s Settings", modem->path);
1693 extract_ipv4_settings(value, modem->context);
1696 static gboolean cdma_cm_changed(DBusConnection *connection,
1697 DBusMessage *message, void *user_data)
1699 const char *path = dbus_message_get_path(message);
1700 struct modem_data *modem;
1701 DBusMessageIter iter, value;
1704 modem = g_hash_table_lookup(modem_hash, path);
1708 if (modem->online == TRUE && modem->network == NULL)
1709 cdma_netreg_get_properties(modem);
1711 if (dbus_message_iter_init(message, &iter) == FALSE)
1714 dbus_message_iter_get_basic(&iter, &key);
1716 dbus_message_iter_next(&iter);
1717 dbus_message_iter_recurse(&iter, &value);
1719 if (g_str_equal(key, "Powered") == TRUE)
1720 cdma_cm_update_powered(modem, &value);
1721 if (g_str_equal(key, "Settings") == TRUE)
1722 cdma_cm_update_settings(modem, &value);
1727 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1729 DBG("%s", modem->path);
1731 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1732 DBusMessageIter entry, value;
1735 dbus_message_iter_recurse(dict, &entry);
1736 dbus_message_iter_get_basic(&entry, &key);
1738 dbus_message_iter_next(&entry);
1739 dbus_message_iter_recurse(&entry, &value);
1741 if (g_str_equal(key, "Attached") == TRUE)
1742 cm_update_attached(modem, &value);
1743 else if (g_str_equal(key, "Powered") == TRUE)
1744 cm_update_powered(modem, &value);
1746 dbus_message_iter_next(dict);
1750 static int cm_get_properties(struct modem_data *modem)
1752 return get_properties(modem->path, OFONO_CM_INTERFACE,
1753 cm_properties_reply, modem);
1756 static void cdma_cm_properties_reply(struct modem_data *modem,
1757 DBusMessageIter *dict)
1759 DBG("%s", modem->path);
1761 if (modem->online == TRUE)
1762 cdma_netreg_get_properties(modem);
1764 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1765 DBusMessageIter entry, value;
1768 dbus_message_iter_recurse(dict, &entry);
1769 dbus_message_iter_get_basic(&entry, &key);
1771 dbus_message_iter_next(&entry);
1772 dbus_message_iter_recurse(&entry, &value);
1774 if (g_str_equal(key, "Powered") == TRUE)
1775 cdma_cm_update_powered(modem, &value);
1776 if (g_str_equal(key, "Settings") == TRUE)
1777 cdma_cm_update_settings(modem, &value);
1779 dbus_message_iter_next(dict);
1783 static int cdma_cm_get_properties(struct modem_data *modem)
1785 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
1786 cdma_cm_properties_reply, modem);
1789 static void sim_update_imsi(struct modem_data *modem,
1790 DBusMessageIter* value)
1794 dbus_message_iter_get_basic(value, &imsi);
1796 DBG("%s imsi %s", modem->path, imsi);
1798 g_free(modem->imsi);
1799 modem->imsi = g_strdup(imsi);
1802 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1805 const char *path = dbus_message_get_path(message);
1806 struct modem_data *modem;
1807 DBusMessageIter iter, value;
1810 modem = g_hash_table_lookup(modem_hash, path);
1814 if (modem->ignore == TRUE)
1817 if (dbus_message_iter_init(message, &iter) == FALSE)
1820 dbus_message_iter_get_basic(&iter, &key);
1822 dbus_message_iter_next(&iter);
1823 dbus_message_iter_recurse(&iter, &value);
1825 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1826 sim_update_imsi(modem, &value);
1828 if (ready_to_create_device(modem) == FALSE)
1832 * This is a GSM modem. Create the device and
1833 * register it at the core. Enabling (setting
1834 * it online is done through the
1835 * modem_enable() callback.
1837 create_device(modem);
1843 static void sim_properties_reply(struct modem_data *modem,
1844 DBusMessageIter *dict)
1846 DBG("%s", modem->path);
1848 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1849 DBusMessageIter entry, value;
1852 dbus_message_iter_recurse(dict, &entry);
1853 dbus_message_iter_get_basic(&entry, &key);
1855 dbus_message_iter_next(&entry);
1856 dbus_message_iter_recurse(&entry, &value);
1858 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1859 sim_update_imsi(modem, &value);
1861 if (ready_to_create_device(modem) == FALSE)
1865 * This is a GSM modem. Create the device and
1866 * register it at the core. Enabling (setting
1867 * it online is done through the
1868 * modem_enable() callback.
1870 create_device(modem);
1872 if (modem->online == FALSE)
1876 * The modem is already online and we have the CM interface.
1877 * There will be no interface update and therefore our
1878 * state machine will not go to next step. We have to
1879 * trigger it from here.
1881 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1882 cm_get_properties(modem);
1883 cm_get_contexts(modem);
1888 dbus_message_iter_next(dict);
1892 static int sim_get_properties(struct modem_data *modem)
1894 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1895 sim_properties_reply, modem);
1898 static connman_bool_t api_added(uint8_t old_iface, uint8_t new_iface,
1901 if (has_interface(old_iface, api) == FALSE &&
1902 has_interface(new_iface, api) == TRUE) {
1903 DBG("%s added", api2string(api));
1910 static connman_bool_t api_removed(uint8_t old_iface, uint8_t new_iface,
1913 if (has_interface(old_iface, api) == TRUE &&
1914 has_interface(new_iface, api) == FALSE) {
1915 DBG("%s removed", api2string(api));
1922 static void modem_update_interfaces(struct modem_data *modem,
1926 DBG("%s", modem->path);
1928 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM) == TRUE) {
1929 if (modem->imsi == NULL &&
1930 modem->set_powered == FALSE) {
1932 * Only use do GetProperties() when
1933 * device has not been powered up.
1935 sim_get_properties(modem);
1939 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1940 if (modem->device != NULL) {
1941 cm_get_properties(modem);
1942 cm_get_contexts(modem);
1946 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1947 if (ready_to_create_device(modem) == TRUE)
1948 create_device(modem);
1950 if (modem->device != NULL)
1951 cdma_cm_get_properties(modem);
1954 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1955 if (modem->attached == TRUE)
1956 netreg_get_properties(modem);
1959 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG) == TRUE) {
1960 cdma_netreg_get_properties(modem);
1963 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM) == TRUE) {
1964 remove_cm_context(modem, modem->context->path);
1967 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM) == TRUE) {
1968 remove_cm_context(modem, modem->context->path);
1971 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG) == TRUE) {
1972 remove_network(modem);
1975 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG == TRUE)) {
1976 remove_network(modem);
1980 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1983 const char *path = dbus_message_get_path(message);
1984 struct modem_data *modem;
1985 DBusMessageIter iter, value;
1988 modem = g_hash_table_lookup(modem_hash, path);
1992 if (modem->ignore == TRUE)
1995 if (dbus_message_iter_init(message, &iter) == FALSE)
1998 dbus_message_iter_get_basic(&iter, &key);
2000 dbus_message_iter_next(&iter);
2001 dbus_message_iter_recurse(&iter, &value);
2003 if (g_str_equal(key, "Powered") == TRUE) {
2004 dbus_message_iter_get_basic(&value, &modem->powered);
2006 DBG("%s Powered %d", modem->path, modem->powered);
2008 if (modem->powered == FALSE)
2009 modem_set_powered(modem, TRUE);
2010 } else if (g_str_equal(key, "Online") == TRUE) {
2011 dbus_message_iter_get_basic(&value, &modem->online);
2013 DBG("%s Online %d", modem->path, modem->online);
2015 if (modem->device == NULL)
2018 connman_device_set_powered(modem->device, modem->online);
2019 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2022 interfaces = extract_interfaces(&value);
2024 if (interfaces == modem->interfaces)
2027 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2029 modem_update_interfaces(modem, modem->interfaces, interfaces);
2031 modem->interfaces = interfaces;
2032 } else if (g_str_equal(key, "Serial") == TRUE) {
2035 dbus_message_iter_get_basic(&value, &serial);
2037 g_free(modem->serial);
2038 modem->serial = g_strdup(serial);
2040 DBG("%s Serial %s", modem->path, modem->serial);
2042 if (has_interface(modem->interfaces,
2043 OFONO_API_CDMA_CM) == TRUE) {
2044 if (ready_to_create_device(modem) == TRUE)
2045 create_device(modem);
2052 static void add_modem(const char *path, DBusMessageIter *prop)
2054 struct modem_data *modem;
2058 modem = g_hash_table_lookup(modem_hash, path);
2059 if (modem != NULL) {
2061 * When oFono powers up we ask for the modems and oFono is
2062 * reporting with modem_added signal the modems. Only
2068 modem = g_try_new0(struct modem_data, 1);
2072 modem->path = g_strdup(path);
2074 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2076 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2077 DBusMessageIter entry, value;
2080 dbus_message_iter_recurse(prop, &entry);
2081 dbus_message_iter_get_basic(&entry, &key);
2083 dbus_message_iter_next(&entry);
2084 dbus_message_iter_recurse(&entry, &value);
2086 if (g_str_equal(key, "Powered") == TRUE) {
2087 dbus_message_iter_get_basic(&value, &modem->powered);
2089 DBG("%s Powered %d", modem->path, modem->powered);
2090 } else if (g_str_equal(key, "Online") == TRUE) {
2091 dbus_message_iter_get_basic(&value, &modem->online);
2093 DBG("%s Online %d", modem->path, modem->online);
2094 } else if (g_str_equal(key, "Interfaces") == TRUE) {
2095 modem->interfaces = extract_interfaces(&value);
2097 DBG("%s Interfaces 0x%02x", modem->path,
2099 } else if (g_str_equal(key, "Serial") == TRUE) {
2102 dbus_message_iter_get_basic(&value, &serial);
2103 modem->serial = g_strdup(serial);
2105 DBG("%s Serial %s", modem->path, modem->serial);
2106 } else if (g_str_equal(key, "Type") == TRUE) {
2109 dbus_message_iter_get_basic(&value, &type);
2111 DBG("%s Type %s", modem->path, type);
2112 if (g_strcmp0(type, "hardware") != 0) {
2113 DBG("%s Ignore this modem", modem->path);
2114 modem->ignore = TRUE;
2118 dbus_message_iter_next(prop);
2121 if (modem->ignore == TRUE)
2124 if (modem->powered == FALSE) {
2125 modem_set_powered(modem, TRUE);
2129 modem_update_interfaces(modem, 0, modem->interfaces);
2132 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2134 struct modem_data *modem = value;
2136 DBG("%s", modem->path);
2138 if (modem->ignore == TRUE)
2141 modem_set_powered(modem, FALSE);
2144 static void remove_modem(gpointer data)
2146 struct modem_data *modem = data;
2148 DBG("%s", modem->path);
2150 if (modem->call_set_property != NULL)
2151 dbus_pending_call_cancel(modem->call_set_property);
2153 if (modem->call_get_properties != NULL)
2154 dbus_pending_call_cancel(modem->call_get_properties);
2156 if (modem->call_get_contexts != NULL)
2157 dbus_pending_call_cancel(modem->call_get_contexts);
2159 if (modem->device != NULL)
2160 destroy_device(modem);
2162 if (modem->context != NULL)
2163 remove_cm_context(modem, modem->context->path);
2165 g_free(modem->serial);
2166 g_free(modem->name);
2167 g_free(modem->imsi);
2168 g_free(modem->path);
2173 static gboolean modem_added(DBusConnection *connection,
2174 DBusMessage *message, void *user_data)
2176 DBusMessageIter iter, properties;
2181 if (dbus_message_iter_init(message, &iter) == FALSE)
2184 dbus_message_iter_get_basic(&iter, &path);
2186 dbus_message_iter_next(&iter);
2187 dbus_message_iter_recurse(&iter, &properties);
2189 add_modem(path, &properties);
2194 static gboolean modem_removed(DBusConnection *connection,
2195 DBusMessage *message, void *user_data)
2197 DBusMessageIter iter;
2202 if (dbus_message_iter_init(message, &iter) == FALSE)
2205 dbus_message_iter_get_basic(&iter, &path);
2207 g_hash_table_remove(modem_hash, path);
2212 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2216 DBusMessageIter array, dict;
2220 reply = dbus_pending_call_steal_reply(call);
2222 dbus_error_init(&error);
2224 if (dbus_set_error_from_message(&error, reply) == TRUE) {
2225 connman_error("%s", error.message);
2226 dbus_error_free(&error);
2230 if (dbus_message_iter_init(reply, &array) == FALSE)
2233 dbus_message_iter_recurse(&array, &dict);
2235 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2236 DBusMessageIter value, properties;
2239 dbus_message_iter_recurse(&dict, &value);
2240 dbus_message_iter_get_basic(&value, &path);
2242 dbus_message_iter_next(&value);
2243 dbus_message_iter_recurse(&value, &properties);
2245 add_modem(path, &properties);
2247 dbus_message_iter_next(&dict);
2251 dbus_message_unref(reply);
2253 dbus_pending_call_unref(call);
2256 static int manager_get_modems(void)
2258 DBusMessage *message;
2259 DBusPendingCall *call;
2263 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2264 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2265 if (message == NULL)
2268 if (dbus_connection_send_with_reply(connection, message,
2269 &call, TIMEOUT) == FALSE) {
2270 connman_error("Failed to call GetModems()");
2271 dbus_message_unref(message);
2276 connman_error("D-Bus connection not available");
2277 dbus_message_unref(message);
2281 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2284 dbus_message_unref(message);
2286 return -EINPROGRESS;
2289 static void ofono_connect(DBusConnection *conn, void *user_data)
2293 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2294 g_free, remove_modem);
2295 if (modem_hash == NULL)
2298 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2300 if (context_hash == NULL) {
2301 g_hash_table_destroy(modem_hash);
2305 manager_get_modems();
2308 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2312 if (modem_hash == NULL || context_hash == NULL)
2315 g_hash_table_destroy(modem_hash);
2318 g_hash_table_destroy(context_hash);
2319 context_hash = NULL;
2322 static int network_probe(struct connman_network *network)
2324 struct modem_data *modem = connman_network_get_data(network);
2326 DBG("%s network %p", modem->path, network);
2331 static void network_remove(struct connman_network *network)
2333 struct modem_data *modem = connman_network_get_data(network);
2335 DBG("%s network %p", modem->path, network);
2338 static int network_connect(struct connman_network *network)
2340 struct modem_data *modem = connman_network_get_data(network);
2342 DBG("%s network %p", modem->path, network);
2344 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2345 return context_set_active(modem, TRUE);
2346 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2347 return cdma_cm_set_powered(modem, TRUE);
2349 connman_error("Connection manager interface not available");
2354 static int network_disconnect(struct connman_network *network)
2356 struct modem_data *modem = connman_network_get_data(network);
2358 DBG("%s network %p", modem->path, network);
2360 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2361 return context_set_active(modem, FALSE);
2362 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2363 return cdma_cm_set_powered(modem, FALSE);
2365 connman_error("Connection manager interface not available");
2370 static struct connman_network_driver network_driver = {
2372 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2373 .probe = network_probe,
2374 .remove = network_remove,
2375 .connect = network_connect,
2376 .disconnect = network_disconnect,
2379 static int modem_probe(struct connman_device *device)
2381 struct modem_data *modem = connman_device_get_data(device);
2383 DBG("%s device %p", modem->path, device);
2388 static void modem_remove(struct connman_device *device)
2390 struct modem_data *modem = connman_device_get_data(device);
2392 DBG("%s device %p", modem->path, device);
2395 static int modem_enable(struct connman_device *device)
2397 struct modem_data *modem = connman_device_get_data(device);
2399 DBG("%s device %p", modem->path, device);
2401 if (modem->online == TRUE)
2404 return modem_set_online(modem, TRUE);
2407 static int modem_disable(struct connman_device *device)
2409 struct modem_data *modem = connman_device_get_data(device);
2411 DBG("%s device %p", modem->path, device);
2413 if (modem->online == FALSE)
2416 return modem_set_online(modem, FALSE);
2419 static struct connman_device_driver modem_driver = {
2421 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2422 .probe = modem_probe,
2423 .remove = modem_remove,
2424 .enable = modem_enable,
2425 .disable = modem_disable,
2428 static int tech_probe(struct connman_technology *technology)
2433 static void tech_remove(struct connman_technology *technology)
2437 static struct connman_technology_driver tech_driver = {
2439 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2440 .probe = tech_probe,
2441 .remove = tech_remove,
2445 static guint modem_added_watch;
2446 static guint modem_removed_watch;
2447 static guint modem_watch;
2448 static guint cm_watch;
2449 static guint sim_watch;
2450 static guint context_added_watch;
2451 static guint context_removed_watch;
2452 static guint netreg_watch;
2453 static guint context_watch;
2454 static guint cdma_cm_watch;
2455 static guint cdma_netreg_watch;
2457 static int ofono_init(void)
2463 connection = connman_dbus_get_connection();
2464 if (connection == NULL)
2467 watch = g_dbus_add_service_watch(connection,
2468 OFONO_SERVICE, ofono_connect,
2469 ofono_disconnect, NULL, NULL);
2471 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2472 OFONO_MANAGER_INTERFACE,
2477 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2478 OFONO_MANAGER_INTERFACE,
2483 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2484 OFONO_MODEM_INTERFACE,
2489 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2495 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2496 OFONO_SIM_INTERFACE,
2501 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2507 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2513 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2514 OFONO_CONTEXT_INTERFACE,
2519 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2520 OFONO_NETREG_INTERFACE,
2525 cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2526 OFONO_CDMA_CM_INTERFACE,
2531 cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2532 OFONO_CDMA_NETREG_INTERFACE,
2534 cdma_netreg_changed,
2538 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2539 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2540 context_added_watch == 0 ||
2541 context_removed_watch == 0 ||
2542 context_watch == 0 || netreg_watch == 0 ||
2543 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2548 err = connman_network_driver_register(&network_driver);
2552 err = connman_device_driver_register(&modem_driver);
2554 connman_network_driver_unregister(&network_driver);
2558 err = connman_technology_driver_register(&tech_driver);
2560 connman_device_driver_unregister(&modem_driver);
2561 connman_network_driver_unregister(&network_driver);
2568 g_dbus_remove_watch(connection, cdma_netreg_watch);
2569 g_dbus_remove_watch(connection, cdma_cm_watch);
2570 g_dbus_remove_watch(connection, netreg_watch);
2571 g_dbus_remove_watch(connection, context_watch);
2572 g_dbus_remove_watch(connection, context_removed_watch);
2573 g_dbus_remove_watch(connection, context_added_watch);
2574 g_dbus_remove_watch(connection, sim_watch);
2575 g_dbus_remove_watch(connection, cm_watch);
2576 g_dbus_remove_watch(connection, modem_watch);
2577 g_dbus_remove_watch(connection, modem_removed_watch);
2578 g_dbus_remove_watch(connection, modem_added_watch);
2579 g_dbus_remove_watch(connection, watch);
2580 dbus_connection_unref(connection);
2585 static void ofono_exit(void)
2589 if (modem_hash != NULL) {
2591 * We should propably wait for the SetProperty() reply
2592 * message, because ...
2594 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2597 * ... here we will cancel the call.
2599 g_hash_table_destroy(modem_hash);
2603 if (context_hash != NULL) {
2604 g_hash_table_destroy(context_hash);
2605 context_hash = NULL;
2608 connman_technology_driver_unregister(&tech_driver);
2609 connman_device_driver_unregister(&modem_driver);
2610 connman_network_driver_unregister(&network_driver);
2612 g_dbus_remove_watch(connection, cdma_netreg_watch);
2613 g_dbus_remove_watch(connection, cdma_cm_watch);
2614 g_dbus_remove_watch(connection, netreg_watch);
2615 g_dbus_remove_watch(connection, context_watch);
2616 g_dbus_remove_watch(connection, context_removed_watch);
2617 g_dbus_remove_watch(connection, context_added_watch);
2618 g_dbus_remove_watch(connection, sim_watch);
2619 g_dbus_remove_watch(connection, cm_watch);
2620 g_dbus_remove_watch(connection, modem_watch);
2621 g_dbus_remove_watch(connection, modem_added_watch);
2622 g_dbus_remove_watch(connection, modem_removed_watch);
2623 g_dbus_remove_watch(connection, watch);
2625 dbus_connection_unref(connection);
2628 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2629 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)