5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (C) 2011-2014 BMW Car IT GmbH.
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 OFONO_SERVICE "org.ofono"
48 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
49 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
50 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
51 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
52 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
53 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
54 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
55 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
57 #define MODEM_ADDED "ModemAdded"
58 #define MODEM_REMOVED "ModemRemoved"
59 #define PROPERTY_CHANGED "PropertyChanged"
60 #define CONTEXT_ADDED "ContextAdded"
61 #define CONTEXT_REMOVED "ContextRemoved"
63 #define GET_PROPERTIES "GetProperties"
64 #define SET_PROPERTY "SetProperty"
65 #define GET_MODEMS "GetModems"
66 #define GET_CONTEXTS "GetContexts"
72 OFONO_API_NETREG = 0x2,
74 OFONO_API_CDMA_NETREG = 0x8,
75 OFONO_API_CDMA_CM = 0x10,
79 * The way this plugin works is following:
81 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
82 * attached -> netreg -> ready
84 * Depending on the modem type, this plugin will behave differently.
88 * When a new modem appears, the plugin always powers it up. This
89 * allows the plugin to create a connman_device. The core will call
90 * modem_enable() if the technology is enabled. modem_enable() will
91 * then set the modem online. If the technology is disabled then
92 * modem_disable() will just set the modem offline. The modem is
93 * always kept powered all the time.
95 * After setting the modem online the plugin waits for the
96 * ConnectionManager and ConnectionContext to appear. When the context
97 * signals that it is attached and the NetworkRegistration interface
98 * appears, a new Service will be created and registered at the core.
100 * When asked to connect to the network (network_connect()) the plugin
101 * will set the Active property on the context. If this operation is
102 * successful the modem is connected to the network. oFono will inform
103 * the plugin about IP configuration through the updating the context's
108 * When a new modem appears, the plugin always powers it up. This
109 * allows the plugin to create connman_device either using IMSI either
110 * using modem Serial if the modem got a SIM interface or not.
112 * As for GSM, the core will call modem_enable() if the technology
113 * is enabled. modem_enable() will then set the modem online.
114 * If the technology is disabled then modem_disable() will just set the
115 * modem offline. The modem is always kept powered all the time.
117 * After setting the modem online the plugin waits for CdmaConnectionManager
118 * interface to appear. Then, once CdmaNetworkRegistration appears, a new
119 * Service will be created and registered at the core.
121 * When asked to connect to the network (network_connect()) the plugin
122 * will power up the CdmaConnectionManager interface.
123 * If the operation is successful the modem is connected to the network.
124 * oFono will inform the plugin about IP configuration through the
125 * updating CdmaConnectionManager settings properties.
128 static DBusConnection *connection;
130 static GHashTable *modem_hash;
131 static GHashTable *context_hash;
133 struct network_context {
136 struct connman_network *network;
138 enum connman_ipconfig_method ipv4_method;
139 struct connman_ipaddress *ipv4_address;
140 char *ipv4_nameservers;
142 enum connman_ipconfig_method ipv6_method;
143 struct connman_ipaddress *ipv6_address;
144 char *ipv6_nameservers;
149 bool valid_apn; /* APN is 'valid' if length > 0 */
155 struct connman_device *device;
157 GSList *context_list;
159 /* Modem Interface */
168 /* CDMA ConnectionManager Interface */
169 bool cdma_cm_powered;
171 /* ConnectionManager Interface */
175 /* SimManager Interface */
178 /* Netreg Interface */
181 uint8_t data_strength; /* 1xEVDO signal strength */
186 DBusPendingCall *call_set_property;
187 DBusPendingCall *call_get_properties;
188 DBusPendingCall *call_get_contexts;
191 static const char *api2string(enum ofono_api api)
196 case OFONO_API_NETREG:
200 case OFONO_API_CDMA_NETREG:
201 return "cdma-netreg";
202 case OFONO_API_CDMA_CM:
209 static char *get_ident(const char *path)
216 pos = strrchr(path, '/');
223 static struct network_context *get_context_with_path(GSList *context_list,
228 DBG("path %s", path);
230 for (list = context_list; list; list = list->next) {
231 struct network_context *context = list->data;
233 if (g_strcmp0(context->path, path) == 0)
240 static struct network_context *get_context_with_network(GSList *context_list,
241 const struct connman_network *network)
245 DBG("network %p", network);
247 for (list = context_list; list; list = list->next) {
248 struct network_context *context = list->data;
250 if (context->network == network)
257 static struct network_context *network_context_alloc(const char *path)
259 struct network_context *context;
261 context = g_try_new0(struct network_context, 1);
265 context->path = g_strdup(path);
268 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
269 context->ipv4_address = NULL;
270 context->ipv4_nameservers = NULL;
272 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
273 context->ipv6_address = NULL;
274 context->ipv6_nameservers = NULL;
276 context->refcount = 1;
281 static void network_context_ref(struct network_context *context)
283 DBG("%p ref %d", context, context->refcount + 1);
285 __sync_fetch_and_add(&context->refcount, 1);
288 static void network_context_unref(struct network_context *context)
290 DBG("%p ref %d", context, context->refcount - 1);
292 if (__sync_fetch_and_sub(&context->refcount, 1) != 1)
295 g_free(context->path);
297 connman_ipaddress_free(context->ipv4_address);
298 g_free(context->ipv4_nameservers);
300 connman_ipaddress_free(context->ipv6_address);
301 g_free(context->ipv6_nameservers);
306 static void set_connected(struct modem_data *modem,
307 struct network_context *context)
309 struct connman_service *service;
311 enum connman_ipconfig_method method;
315 DBG("%s", modem->path);
317 index = context->index;
319 method = context->ipv4_method;
320 if (index < 0 || (!context->ipv4_address &&
321 method == CONNMAN_IPCONFIG_METHOD_FIXED)) {
322 connman_error("Invalid index and/or address");
326 service = connman_service_lookup_from_network(context->network);
330 connman_service_create_ip4config(service, index);
331 connman_network_set_ipv4_method(context->network, method);
333 if (method == CONNMAN_IPCONFIG_METHOD_FIXED ||
334 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
338 if (method == CONNMAN_IPCONFIG_METHOD_FIXED) {
339 connman_network_set_ipaddress(context->network,
340 context->ipv4_address);
343 method = context->ipv6_method;
344 connman_service_create_ip6config(service, index);
345 connman_network_set_ipv6_method(context->network, method);
347 if (method == CONNMAN_IPCONFIG_METHOD_AUTO) {
351 /* Set the nameservers */
352 if (context->ipv4_nameservers &&
353 context->ipv6_nameservers) {
354 nameservers = g_strdup_printf("%s %s",
355 context->ipv4_nameservers,
356 context->ipv6_nameservers);
357 connman_network_set_nameservers(context->network, nameservers);
359 } else if (context->ipv4_nameservers) {
360 connman_network_set_nameservers(context->network,
361 context->ipv4_nameservers);
362 } else if (context->ipv6_nameservers) {
363 connman_network_set_nameservers(context->network,
364 context->ipv6_nameservers);
368 connman_network_set_index(context->network, index);
369 connman_network_set_connected(context->network, true);
373 static void set_disconnected(struct network_context *context)
375 DBG("%s", context->path);
377 if (context->network)
378 connman_network_set_connected(context->network, false);
380 g_free(context->ipv4_nameservers);
381 context->ipv4_nameservers = NULL;
382 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_OFF)
383 context->ipv4_method =
384 CONNMAN_IPCONFIG_METHOD_UNKNOWN;
386 g_free(context->ipv6_nameservers);
387 context->ipv6_nameservers = NULL;
388 if (context->ipv6_method != CONNMAN_IPCONFIG_METHOD_OFF)
389 context->ipv6_method =
390 CONNMAN_IPCONFIG_METHOD_UNKNOWN;
393 typedef void (*set_property_cb)(struct modem_data *data,
394 struct network_context *context, bool success);
395 typedef void (*get_properties_cb)(struct modem_data *data,
396 DBusMessageIter *dict);
398 struct property_info {
399 struct modem_data *modem;
400 struct network_context *context;
402 const char *interface;
403 const char *property;
404 set_property_cb set_property_cb;
405 get_properties_cb get_properties_cb;
408 static void free_property_info(void * memory)
410 struct property_info * info = memory;
413 network_context_unref(info->context);
418 static void set_property_reply(DBusPendingCall *call, void *user_data)
420 struct property_info *info = user_data;
425 DBG("%s path %s %s.%s", info->modem->path,
426 info->path, info->interface, info->property);
428 info->modem->call_set_property = NULL;
430 dbus_error_init(&error);
432 reply = dbus_pending_call_steal_reply(call);
434 if (dbus_set_error_from_message(&error, reply)) {
435 connman_error("Failed to change property: %s %s.%s: %s %s",
436 info->path, info->interface, info->property,
437 error.name, error.message);
438 dbus_error_free(&error);
442 if (info->set_property_cb)
443 (*info->set_property_cb)(info->modem, info->context,
446 dbus_message_unref(reply);
448 dbus_pending_call_unref(call);
451 static int set_property(struct modem_data *modem,
452 struct network_context *context,
453 const char *path, const char *interface,
454 const char *property, int type, void *value,
455 set_property_cb notify)
457 DBusMessage *message;
458 DBusMessageIter iter;
459 struct property_info *info;
461 DBG("%s path %s %s.%s", modem->path, path, interface, property);
463 if (modem->call_set_property) {
464 DBG("Cancel pending SetProperty");
465 dbus_pending_call_cancel(modem->call_set_property);
466 dbus_pending_call_unref(modem->call_set_property);
467 modem->call_set_property = NULL;
470 message = dbus_message_new_method_call(OFONO_SERVICE, path,
471 interface, SET_PROPERTY);
475 dbus_message_iter_init_append(message, &iter);
476 connman_dbus_property_append_basic(&iter, property, type, value);
478 if (!dbus_connection_send_with_reply(connection, message,
479 &modem->call_set_property, TIMEOUT)) {
480 connman_error("Failed to change property: %s %s.%s",
481 path, interface, property);
482 dbus_message_unref(message);
486 if (!modem->call_set_property) {
487 connman_error("D-Bus connection not available");
488 dbus_message_unref(message);
492 info = g_try_new0(struct property_info, 1);
494 dbus_message_unref(message);
499 info->context = context;
501 info->interface = interface;
502 info->property = property;
503 info->set_property_cb = notify;
506 network_context_ref(info->context);
508 dbus_pending_call_set_notify(modem->call_set_property,
509 set_property_reply, info, free_property_info);
511 dbus_message_unref(message);
516 static void get_properties_reply(DBusPendingCall *call, void *user_data)
518 struct property_info *info = user_data;
519 DBusMessageIter array, dict;
523 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
525 info->modem->call_get_properties = NULL;
527 dbus_error_init(&error);
529 reply = dbus_pending_call_steal_reply(call);
531 if (dbus_set_error_from_message(&error, reply)) {
532 connman_error("Failed to get properties: %s %s: %s %s",
533 info->path, info->interface,
534 error.name, error.message);
535 dbus_error_free(&error);
540 if (!dbus_message_iter_init(reply, &array))
543 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
546 dbus_message_iter_recurse(&array, &dict);
548 if (info->get_properties_cb)
549 (*info->get_properties_cb)(info->modem, &dict);
553 dbus_message_unref(reply);
555 dbus_pending_call_unref(call);
558 static int get_properties(const char *path, const char *interface,
559 get_properties_cb notify,
560 struct modem_data *modem)
562 DBusMessage *message;
563 struct property_info *info;
565 DBG("%s path %s %s", modem->path, path, interface);
567 if (modem->call_get_properties) {
568 connman_error("Pending GetProperties");
572 message = dbus_message_new_method_call(OFONO_SERVICE, path,
573 interface, GET_PROPERTIES);
577 if (!dbus_connection_send_with_reply(connection, message,
578 &modem->call_get_properties, TIMEOUT)) {
579 connman_error("Failed to call %s.GetProperties()", interface);
580 dbus_message_unref(message);
584 if (!modem->call_get_properties) {
585 connman_error("D-Bus connection not available");
586 dbus_message_unref(message);
590 info = g_try_new0(struct property_info, 1);
592 dbus_message_unref(message);
598 info->interface = interface;
599 info->get_properties_cb = notify;
601 dbus_pending_call_set_notify(modem->call_get_properties,
602 get_properties_reply, info, g_free);
604 dbus_message_unref(message);
609 static void context_set_active_reply(struct modem_data *modem,
610 struct network_context *context, bool success)
612 DBG("%s", context->path);
616 * Don't handle do anything on success here. oFono will send
617 * the change via PropertyChanged singal.
623 * Active = True might fail due a timeout. That means oFono
624 * still tries to go online. If we retry to set Active = True,
625 * we just get a InProgress error message. Should we power
626 * cycle the modem in such cases?
629 if (!context->network) {
631 * In the case where we power down the device
632 * we don't wait for the reply, therefore the network
633 * might already be gone.
638 connman_network_set_error(context->network,
639 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
642 static int context_set_active(struct modem_data *modem,
643 struct network_context *context,
648 DBG("%s active %d", modem->path, active);
650 err = set_property(modem, context, context->path,
651 OFONO_CONTEXT_INTERFACE,
652 "Active", DBUS_TYPE_BOOLEAN,
654 context_set_active_reply);
656 if (!active && err == -EINPROGRESS)
662 static void cdma_cm_set_powered_reply(struct modem_data *modem,
663 struct network_context *context, bool success)
665 DBG("%s", context->path);
669 * Don't handle do anything on success here. oFono will send
670 * the change via PropertyChanged singal.
676 * Powered = True might fail due a timeout. That means oFono
677 * still tries to go online. If we retry to set Powered = True,
678 * we just get a InProgress error message. Should we power
679 * cycle the modem in such cases?
682 if (!context->network) {
684 * In the case where we power down the device
685 * we don't wait for the reply, therefore the network
686 * might already be gone.
691 connman_network_set_error(context->network,
692 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
695 static int cdma_cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
698 struct network_context *context = NULL;
700 if (!modem->context_list)
703 DBG("%s powered %d", modem->path, powered);
705 /* In case of CDMA, there is only one context */
706 context = modem->context_list->data;
707 err = set_property(modem, context, modem->path,
708 OFONO_CDMA_CM_INTERFACE,
709 "Powered", DBUS_TYPE_BOOLEAN,
711 cdma_cm_set_powered_reply);
713 if (!powered && err == -EINPROGRESS)
719 static int modem_set_online(struct modem_data *modem, dbus_bool_t online)
721 DBG("%s online %d", modem->path, online);
723 return set_property(modem, NULL, modem->path,
724 OFONO_MODEM_INTERFACE,
725 "Online", DBUS_TYPE_BOOLEAN,
730 static int cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
734 DBG("%s powered %d", modem->path, powered);
736 err = set_property(modem, NULL, modem->path,
738 "Powered", DBUS_TYPE_BOOLEAN,
742 if (!powered && err == -EINPROGRESS)
748 static int modem_set_powered(struct modem_data *modem, dbus_bool_t powered)
752 DBG("%s powered %d", modem->path, powered);
754 modem->set_powered = powered;
756 err = set_property(modem, NULL, modem->path,
757 OFONO_MODEM_INTERFACE,
758 "Powered", DBUS_TYPE_BOOLEAN,
762 if (!powered && err == -EINPROGRESS)
768 static bool has_interface(uint8_t interfaces,
771 if ((interfaces & api) == api)
777 static uint8_t extract_interfaces(DBusMessageIter *array)
779 DBusMessageIter entry;
780 uint8_t interfaces = 0;
782 dbus_message_iter_recurse(array, &entry);
784 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
787 dbus_message_iter_get_basic(&entry, &name);
789 if (g_str_equal(name, OFONO_SIM_INTERFACE))
790 interfaces |= OFONO_API_SIM;
791 else if (g_str_equal(name, OFONO_NETREG_INTERFACE))
792 interfaces |= OFONO_API_NETREG;
793 else if (g_str_equal(name, OFONO_CM_INTERFACE))
794 interfaces |= OFONO_API_CM;
795 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE))
796 interfaces |= OFONO_API_CDMA_CM;
797 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE))
798 interfaces |= OFONO_API_CDMA_NETREG;
800 dbus_message_iter_next(&entry);
806 static char *extract_nameservers(DBusMessageIter *array)
808 DBusMessageIter entry;
809 char *nameservers = NULL;
812 dbus_message_iter_recurse(array, &entry);
814 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
815 const char *nameserver;
817 dbus_message_iter_get_basic(&entry, &nameserver);
820 nameservers = g_strdup(nameserver);
823 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
827 dbus_message_iter_next(&entry);
833 static void extract_ipv4_settings(DBusMessageIter *array,
834 struct network_context *context)
836 DBusMessageIter dict;
837 char *address = NULL, *netmask = NULL, *gateway = NULL;
838 char *nameservers = NULL;
839 const char *interface = NULL;
842 connman_ipaddress_free(context->ipv4_address);
843 context->ipv4_address = NULL;
845 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
848 dbus_message_iter_recurse(array, &dict);
850 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
851 DBusMessageIter entry, value;
852 const char *key, *val;
854 dbus_message_iter_recurse(&dict, &entry);
855 dbus_message_iter_get_basic(&entry, &key);
857 dbus_message_iter_next(&entry);
858 dbus_message_iter_recurse(&entry, &value);
860 if (g_str_equal(key, "Interface")) {
861 dbus_message_iter_get_basic(&value, &interface);
863 DBG("Interface %s", interface);
865 index = connman_inet_ifindex(interface);
867 DBG("index %d", index);
868 } else if (g_str_equal(key, "Method")) {
869 dbus_message_iter_get_basic(&value, &val);
871 DBG("Method %s", val);
873 if (g_strcmp0(val, "static") == 0)
874 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
875 else if (g_strcmp0(val, "dhcp") == 0)
876 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
878 } else if (g_str_equal(key, "Address")) {
879 dbus_message_iter_get_basic(&value, &val);
881 address = g_strdup(val);
883 DBG("Address %s", address);
884 } else if (g_str_equal(key, "Netmask")) {
885 dbus_message_iter_get_basic(&value, &val);
887 netmask = g_strdup(val);
889 DBG("Netmask %s", netmask);
890 } else if (g_str_equal(key, "DomainNameServers")) {
891 nameservers = extract_nameservers(&value);
893 DBG("Nameservers %s", nameservers);
894 } else if (g_str_equal(key, "Gateway")) {
895 dbus_message_iter_get_basic(&value, &val);
897 gateway = g_strdup(val);
899 DBG("Gateway %s", gateway);
902 dbus_message_iter_next(&dict);
908 context->index = index;
910 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
913 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
914 if (!context->ipv4_address) {
919 connman_ipaddress_set_ipv4(context->ipv4_address, address,
922 g_free(context->ipv4_nameservers);
923 context->ipv4_nameservers = nameservers;
926 if (context->ipv4_nameservers != nameservers)
934 static void extract_ipv6_settings(DBusMessageIter *array,
935 struct network_context *context)
937 DBusMessageIter dict;
938 char *address = NULL, *gateway = NULL;
939 unsigned char prefix_length = 0;
940 char *nameservers = NULL;
941 const char *interface = NULL;
944 connman_ipaddress_free(context->ipv6_address);
945 context->ipv6_address = NULL;
947 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
950 dbus_message_iter_recurse(array, &dict);
952 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
953 DBusMessageIter entry, value;
954 const char *key, *val;
956 dbus_message_iter_recurse(&dict, &entry);
957 dbus_message_iter_get_basic(&entry, &key);
959 dbus_message_iter_next(&entry);
960 dbus_message_iter_recurse(&entry, &value);
962 if (g_str_equal(key, "Interface")) {
963 dbus_message_iter_get_basic(&value, &interface);
965 DBG("Interface %s", interface);
967 index = connman_inet_ifindex(interface);
969 DBG("index %d", index);
970 } else if (g_str_equal(key, "Address")) {
971 dbus_message_iter_get_basic(&value, &val);
973 address = g_strdup(val);
975 DBG("Address %s", address);
976 } else if (g_str_equal(key, "PrefixLength")) {
977 dbus_message_iter_get_basic(&value, &prefix_length);
979 DBG("prefix length %d", prefix_length);
980 } else if (g_str_equal(key, "DomainNameServers")) {
981 nameservers = extract_nameservers(&value);
983 DBG("Nameservers %s", nameservers);
984 } else if (g_str_equal(key, "Gateway")) {
985 dbus_message_iter_get_basic(&value, &val);
987 gateway = g_strdup(val);
989 DBG("Gateway %s", gateway);
992 dbus_message_iter_next(&dict);
998 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_AUTO;
1000 context->ipv6_address =
1001 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
1002 if (!context->ipv6_address)
1005 context->index = index;
1006 connman_ipaddress_set_ipv6(context->ipv6_address, address,
1007 prefix_length, gateway);
1009 g_free(context->ipv6_nameservers);
1010 context->ipv6_nameservers = nameservers;
1013 if (context->ipv6_nameservers != nameservers)
1014 g_free(nameservers);
1020 static bool ready_to_create_device(struct modem_data *modem)
1023 * There are three different modem types which behave slightly
1025 * - GSM modems will expose the SIM interface then the
1027 * - CDMA modems will expose CM first and sometime later
1028 * a unique serial number.
1030 * This functions tests if we have the necessary information gathered
1031 * before we are able to create a device.
1037 if (modem->imsi || modem->serial)
1043 static void create_device(struct modem_data *modem)
1045 struct connman_device *device;
1048 DBG("%s", modem->path);
1051 ident = modem->imsi;
1052 else if (modem->serial)
1053 ident = modem->serial;
1055 if (!connman_dbus_validate_ident(ident))
1056 ident = connman_dbus_encode_string(ident);
1058 ident = g_strdup(ident);
1060 device = connman_device_create("ofono", CONNMAN_DEVICE_TYPE_CELLULAR);
1064 DBG("device %p", device);
1066 connman_device_set_ident(device, ident);
1068 connman_device_set_string(device, "Path", modem->path);
1070 connman_device_set_data(device, modem);
1072 if (connman_device_register(device) < 0) {
1073 connman_error("Failed to register cellular device");
1074 connman_device_unref(device);
1078 modem->device = device;
1080 connman_device_set_powered(modem->device, modem->online);
1085 static void destroy_device(struct modem_data *modem)
1087 DBG("%s", modem->path);
1089 connman_device_set_powered(modem->device, false);
1091 connman_device_unregister(modem->device);
1092 connman_device_unref(modem->device);
1094 modem->device = NULL;
1097 static void add_network(struct modem_data *modem,
1098 struct network_context *context)
1102 DBG("%s", modem->path);
1104 if (context->network)
1107 context->network = connman_network_create(context->path,
1108 CONNMAN_NETWORK_TYPE_CELLULAR);
1109 if (!context->network)
1112 DBG("network %p", context->network);
1114 connman_network_set_data(context->network, modem);
1116 connman_network_set_string(context->network, "Path",
1120 connman_network_set_name(context->network, modem->name);
1122 connman_network_set_name(context->network, "");
1124 connman_network_set_strength(context->network, modem->strength);
1126 group = get_ident(context->path);
1127 connman_network_set_group(context->network, group);
1129 connman_network_set_bool(context->network, "Roaming",
1132 if (connman_device_add_network(modem->device, context->network) < 0) {
1133 connman_network_unref(context->network);
1134 context->network = NULL;
1139 static void remove_network(struct modem_data *modem,
1140 struct network_context *context)
1142 DBG("%s", modem->path);
1144 if (!context || !context->network)
1147 DBG("network %p", context->network);
1150 connman_device_remove_network(modem->device, context->network);
1151 connman_network_unref(context->network);
1152 context->network = NULL;
1155 static int set_context_ipconfig(struct network_context *context,
1156 const char *protocol)
1158 DBG("context %p protocol %s", context, protocol);
1160 if (!context || !protocol)
1163 if (g_str_equal(protocol, "ip")) {
1164 if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
1165 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1167 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_OFF;
1169 connman_ipaddress_free(context->ipv6_address);
1170 context->ipv6_address = NULL;
1172 } else if (g_str_equal(protocol, "ipv6")) {
1173 if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
1174 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1176 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_OFF;
1178 connman_ipaddress_free(context->ipv4_address);
1179 context->ipv4_address = NULL;
1181 } else if (g_str_equal(protocol, "dual")) {
1182 if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
1183 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1185 if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
1186 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1189 DBG("ipv4 method %d ipv6 method %d", context->ipv4_method,
1190 context->ipv6_method);
1195 static int add_cm_context(struct modem_data *modem, const char *context_path,
1196 DBusMessageIter *dict)
1198 const char *context_type = NULL;
1199 struct network_context *context = NULL;
1200 dbus_bool_t active = FALSE;
1201 const char *ip_protocol = NULL;
1203 DBG("%s context path %s", modem->path, context_path);
1205 context = network_context_alloc(context_path);
1209 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1210 DBusMessageIter entry, value;
1213 dbus_message_iter_recurse(dict, &entry);
1214 dbus_message_iter_get_basic(&entry, &key);
1216 dbus_message_iter_next(&entry);
1217 dbus_message_iter_recurse(&entry, &value);
1219 if (g_str_equal(key, "Type")) {
1220 dbus_message_iter_get_basic(&value, &context_type);
1222 DBG("%s context %s type %s", modem->path,
1223 context_path, context_type);
1224 } else if (g_str_equal(key, "Settings")) {
1225 DBG("%s Settings", modem->path);
1227 extract_ipv4_settings(&value, context);
1228 } else if (g_str_equal(key, "IPv6.Settings")) {
1229 DBG("%s IPv6.Settings", modem->path);
1231 extract_ipv6_settings(&value, context);
1232 } else if (g_str_equal(key, "Active")) {
1233 dbus_message_iter_get_basic(&value, &active);
1235 DBG("%s Active %d", modem->path, active);
1236 } else if (g_str_equal(key, "AccessPointName")) {
1239 dbus_message_iter_get_basic(&value, &apn);
1240 if (apn && strlen(apn) > 0)
1241 context->valid_apn = true;
1243 context->valid_apn = false;
1245 DBG("%s AccessPointName '%s'", modem->path, apn);
1246 } else if (g_str_equal(key, "Protocol") &&
1247 dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
1249 dbus_message_iter_get_basic(&value, &ip_protocol);
1251 DBG("%s Protocol %s", modem->path, ip_protocol);
1254 dbus_message_iter_next(dict);
1257 if (g_strcmp0(context_type, "internet") != 0) {
1258 network_context_unref(context);
1263 set_context_ipconfig(context, ip_protocol);
1265 context->active = active;
1267 modem->context_list = g_slist_prepend(modem->context_list, context);
1268 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1270 if (context->valid_apn && modem->attached &&
1271 has_interface(modem->interfaces, OFONO_API_NETREG))
1272 add_network(modem, context);
1277 static void remove_cm_context(struct modem_data *modem,
1278 struct network_context *context)
1280 if (!modem->context_list)
1285 g_hash_table_remove(context_hash, context->path);
1287 if (context->network)
1288 remove_network(modem, context);
1289 modem->context_list = g_slist_remove(modem->context_list, context);
1291 network_context_unref(context);
1295 static void remove_all_contexts(struct modem_data *modem)
1297 GSList *list = NULL;
1301 if (modem->context_list == NULL)
1304 for (list = modem->context_list; list; list = list->next) {
1305 struct network_context *context = list->data;
1307 remove_cm_context(modem, context);
1309 g_slist_free(modem->context_list);
1310 modem->context_list = NULL;
1313 static void remove_all_networks(struct modem_data *modem)
1317 for (list = modem->context_list; list; list = list->next) {
1318 struct network_context *context = list->data;
1320 remove_network(modem, context);
1324 static gboolean context_changed(DBusConnection *conn,
1325 DBusMessage *message,
1328 struct network_context *context = NULL;
1329 const char *context_path = dbus_message_get_path(message);
1330 struct modem_data *modem = NULL;
1331 DBusMessageIter iter, value;
1334 DBG("context_path %s", context_path);
1336 modem = g_hash_table_lookup(context_hash, context_path);
1340 context = get_context_with_path(modem->context_list, context_path);
1344 if (!dbus_message_iter_init(message, &iter))
1347 dbus_message_iter_get_basic(&iter, &key);
1349 dbus_message_iter_next(&iter);
1350 dbus_message_iter_recurse(&iter, &value);
1353 * oFono guarantees the ordering of Settings and
1354 * Active. Settings will always be send before Active = True.
1355 * That means we don't have to order here.
1357 if (g_str_equal(key, "Settings")) {
1358 DBG("%s Settings", modem->path);
1360 extract_ipv4_settings(&value, context);
1361 } else if (g_str_equal(key, "IPv6.Settings")) {
1362 DBG("%s IPv6.Settings", modem->path);
1364 extract_ipv6_settings(&value, context);
1365 } else if (g_str_equal(key, "Active")) {
1368 dbus_message_iter_get_basic(&value, &active);
1369 context->active = active;
1371 DBG("%s Active %d", modem->path, context->active);
1373 if (context->active)
1374 set_connected(modem, context);
1376 set_disconnected(context);
1377 } else if (g_str_equal(key, "AccessPointName")) {
1380 dbus_message_iter_get_basic(&value, &apn);
1382 DBG("%s AccessPointName %s", modem->path, apn);
1384 if (apn && strlen(apn) > 0) {
1385 context->valid_apn = true;
1387 if (context->network)
1390 if (!modem->attached)
1393 if (!has_interface(modem->interfaces,
1397 add_network(modem, context);
1399 if (context->active)
1400 set_connected(modem, context);
1402 context->valid_apn = false;
1404 if (!context->network)
1407 remove_network(modem, context);
1410 } else if (g_str_equal(key, "Protocol") &&
1411 dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
1412 const char *ip_protocol;
1414 dbus_message_iter_get_basic(&value, &ip_protocol);
1416 set_context_ipconfig(context, ip_protocol);
1422 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1424 struct modem_data *modem = user_data;
1425 DBusMessageIter array, dict, entry, value;
1429 DBG("%s", modem->path);
1431 modem->call_get_contexts = NULL;
1433 reply = dbus_pending_call_steal_reply(call);
1435 dbus_error_init(&error);
1437 if (dbus_set_error_from_message(&error, reply)) {
1438 connman_error("%s", error.message);
1439 dbus_error_free(&error);
1443 if (!dbus_message_iter_init(reply, &array))
1446 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1449 dbus_message_iter_recurse(&array, &dict);
1451 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1452 const char *context_path;
1454 dbus_message_iter_recurse(&dict, &entry);
1455 dbus_message_iter_get_basic(&entry, &context_path);
1457 dbus_message_iter_next(&entry);
1458 dbus_message_iter_recurse(&entry, &value);
1460 if (add_cm_context(modem, context_path, &value))
1463 dbus_message_iter_next(&dict);
1467 dbus_message_unref(reply);
1469 dbus_pending_call_unref(call);
1472 static int cm_get_contexts(struct modem_data *modem)
1474 DBusMessage *message;
1476 DBG("%s", modem->path);
1478 if (modem->call_get_contexts)
1481 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1482 OFONO_CM_INTERFACE, GET_CONTEXTS);
1486 if (!dbus_connection_send_with_reply(connection, message,
1487 &modem->call_get_contexts, TIMEOUT)) {
1488 connman_error("Failed to call GetContexts()");
1489 dbus_message_unref(message);
1493 if (!modem->call_get_contexts) {
1494 connman_error("D-Bus connection not available");
1495 dbus_message_unref(message);
1499 dbus_pending_call_set_notify(modem->call_get_contexts,
1500 cm_get_contexts_reply,
1503 dbus_message_unref(message);
1505 return -EINPROGRESS;
1508 static gboolean cm_context_added(DBusConnection *conn,
1509 DBusMessage *message,
1512 const char *path = dbus_message_get_path(message);
1514 struct modem_data *modem;
1515 DBusMessageIter iter, properties, dict;
1519 modem = g_hash_table_lookup(modem_hash, path);
1523 if (!dbus_message_iter_init(message, &iter))
1526 dbus_message_iter_get_basic(&iter, &context_path);
1528 dbus_message_iter_next(&iter);
1529 dbus_message_iter_recurse(&iter, &properties);
1531 /* Sometimes, we get an array instead of dict */
1532 if (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_ARRAY) {
1533 /* Must recurse again */
1534 dbus_message_iter_recurse(&properties, &dict);
1535 if (add_cm_context(modem, context_path, &dict) != 0)
1538 if (add_cm_context(modem, context_path, &properties) != 0)
1544 static gboolean cm_context_removed(DBusConnection *conn,
1545 DBusMessage *message,
1548 const char *path = dbus_message_get_path(message);
1549 const char *context_path;
1550 struct modem_data *modem;
1551 struct network_context *context;
1552 DBusMessageIter iter;
1554 DBG("context path %s", path);
1556 if (!dbus_message_iter_init(message, &iter))
1559 dbus_message_iter_get_basic(&iter, &context_path);
1561 modem = g_hash_table_lookup(context_hash, context_path);
1565 context = get_context_with_path(modem->context_list, context_path);
1566 remove_cm_context(modem, context);
1571 static void netreg_update_name(struct modem_data *modem,
1572 DBusMessageIter* value)
1577 dbus_message_iter_get_basic(value, &name);
1579 DBG("%s Name %s", modem->path, name);
1581 g_free(modem->name);
1582 modem->name = g_strdup(name);
1584 if (!modem->context_list)
1587 /* For all the context */
1588 for (list = modem->context_list; list; list = list->next) {
1589 struct network_context *context = list->data;
1591 if (context->network) {
1592 connman_network_set_name(context->network, modem->name);
1593 connman_network_update(context->network);
1598 static void netreg_update_strength(struct modem_data *modem,
1599 DBusMessageIter *value)
1603 dbus_message_iter_get_basic(value, &modem->strength);
1605 DBG("%s Strength %d", modem->path, modem->strength);
1607 if (!modem->context_list)
1612 * We don't have 2 signal notifications we always report the strength
1613 * signal. data_strength is always equal to 0.
1616 * In the case we have a data_strength signal (from 1xEVDO network)
1617 * we don't need to update the value with strength signal (from 1xCDMA)
1618 * because the modem is registered to 1xEVDO network for data call.
1619 * In case we have no data_strength signal (not registered to 1xEVDO
1620 * network), we must report the strength signal (registered to 1xCDMA
1621 * network e.g slow mode).
1623 if (modem->data_strength != 0)
1626 /* For all the context */
1627 for (list = modem->context_list; list; list = list->next) {
1628 struct network_context *context = list->data;
1630 if (context->network) {
1631 connman_network_set_strength(context->network,
1633 connman_network_update(context->network);
1638 /* Retrieve 1xEVDO Data Strength signal */
1639 static void netreg_update_datastrength(struct modem_data *modem,
1640 DBusMessageIter *value)
1644 dbus_message_iter_get_basic(value, &modem->data_strength);
1646 DBG("%s Data Strength %d", modem->path, modem->data_strength);
1648 if (!modem->context_list)
1652 * CDMA modem is not registered to 1xEVDO network, let
1653 * update_signal_strength() reporting the value on the Strength signal
1656 if (modem->data_strength == 0)
1659 /* For all the context */
1660 for (list = modem->context_list; list; list = list->next) {
1661 struct network_context *context = list->data;
1663 if (context->network) {
1664 connman_network_set_strength(context->network,
1665 modem->data_strength);
1666 connman_network_update(context->network);
1671 static void netreg_update_status(struct modem_data *modem,
1672 DBusMessageIter *value)
1678 dbus_message_iter_get_basic(value, &status);
1680 roaming = g_str_equal(status, "roaming");
1681 modem->registered = roaming || g_str_equal(status, "registered");
1683 if (roaming == modem->roaming)
1686 modem->roaming = roaming;
1688 if (!modem->context_list)
1691 /* For all the context */
1692 for (list = modem->context_list; list; list = list->next) {
1693 struct network_context *context = list->data;
1695 if (context->network) {
1696 connman_network_set_bool(context->network,
1697 "Roaming", modem->roaming);
1698 connman_network_update(context->network);
1703 static void netreg_update_regdom(struct modem_data *modem,
1704 DBusMessageIter *value)
1706 char *mobile_country_code;
1710 dbus_message_iter_get_basic(value, &mobile_country_code);
1712 DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
1715 mcc = atoi(mobile_country_code);
1716 if (mcc > 799 || mcc < 200)
1719 alpha2 = mcc_country_codes[mcc - 200];
1721 connman_technology_set_regdom(alpha2);
1724 static gboolean netreg_changed(DBusConnection *conn, DBusMessage *message,
1727 const char *path = dbus_message_get_path(message);
1728 struct modem_data *modem;
1729 DBusMessageIter iter, value;
1732 modem = g_hash_table_lookup(modem_hash, path);
1739 if (!dbus_message_iter_init(message, &iter))
1742 dbus_message_iter_get_basic(&iter, &key);
1744 dbus_message_iter_next(&iter);
1745 dbus_message_iter_recurse(&iter, &value);
1747 if (g_str_equal(key, "Name"))
1748 netreg_update_name(modem, &value);
1749 else if (g_str_equal(key, "Strength"))
1750 netreg_update_strength(modem, &value);
1751 else if (g_str_equal(key, "Status"))
1752 netreg_update_status(modem, &value);
1753 else if (g_str_equal(key, "MobileCountryCode"))
1754 netreg_update_regdom(modem, &value);
1759 static void netreg_properties_reply(struct modem_data *modem,
1760 DBusMessageIter *dict)
1762 GSList *list = NULL;
1764 DBG("%s", modem->path);
1766 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1767 DBusMessageIter entry, value;
1770 dbus_message_iter_recurse(dict, &entry);
1771 dbus_message_iter_get_basic(&entry, &key);
1773 dbus_message_iter_next(&entry);
1774 dbus_message_iter_recurse(&entry, &value);
1776 if (g_str_equal(key, "Name"))
1777 netreg_update_name(modem, &value);
1778 else if (g_str_equal(key, "Strength"))
1779 netreg_update_strength(modem, &value);
1780 else if (g_str_equal(key, "Status"))
1781 netreg_update_status(modem, &value);
1782 else if (g_str_equal(key, "MobileCountryCode"))
1783 netreg_update_regdom(modem, &value);
1785 dbus_message_iter_next(dict);
1788 if (!modem->context_list) {
1790 * netgreg_get_properties() was issued after we got
1791 * cm_get_contexts_reply() where we create the
1792 * context. Though before we got the
1793 * netreg_properties_reply the context was removed
1794 * again. Therefore we have to skip the network
1799 /* Check for all contexts if they are valids and/or actives */
1800 for (list = modem->context_list; list; list = list->next) {
1801 struct network_context *context = list->data;
1803 if (context->valid_apn)
1804 add_network(modem, context);
1805 if (context->active)
1806 set_connected(modem, context);
1810 static int netreg_get_properties(struct modem_data *modem)
1812 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1813 netreg_properties_reply, modem);
1816 static void add_cdma_network(struct modem_data *modem)
1818 struct network_context *context = NULL;
1819 /* Be sure that device is created before adding CDMA network */
1824 * CDMA modems don't need contexts for data call, however the current
1825 * add_network() logic needs one, so we create one to proceed.
1827 if (!modem->context_list) {
1828 context = network_context_alloc(modem->path);
1829 modem->context_list = g_slist_prepend(modem->context_list,
1832 context = modem->context_list->data;
1835 modem->name = g_strdup("CDMA Network");
1837 add_network(modem, context);
1839 if (modem->cdma_cm_powered)
1840 set_connected(modem, context);
1843 static gboolean cdma_netreg_changed(DBusConnection *conn,
1844 DBusMessage *message,
1847 const char *path = dbus_message_get_path(message);
1848 struct modem_data *modem;
1849 DBusMessageIter iter, value;
1854 modem = g_hash_table_lookup(modem_hash, path);
1861 if (!dbus_message_iter_init(message, &iter))
1864 dbus_message_iter_get_basic(&iter, &key);
1866 dbus_message_iter_next(&iter);
1867 dbus_message_iter_recurse(&iter, &value);
1869 if (g_str_equal(key, "Name"))
1870 netreg_update_name(modem, &value);
1871 else if (g_str_equal(key, "Strength"))
1872 netreg_update_strength(modem, &value);
1873 else if (g_str_equal(key, "DataStrength"))
1874 netreg_update_datastrength(modem, &value);
1875 else if (g_str_equal(key, "Status"))
1876 netreg_update_status(modem, &value);
1878 if (modem->registered)
1879 add_cdma_network(modem);
1881 remove_all_networks(modem);
1886 static void cdma_netreg_properties_reply(struct modem_data *modem,
1887 DBusMessageIter *dict)
1889 DBG("%s", modem->path);
1891 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1892 DBusMessageIter entry, value;
1895 dbus_message_iter_recurse(dict, &entry);
1896 dbus_message_iter_get_basic(&entry, &key);
1898 dbus_message_iter_next(&entry);
1899 dbus_message_iter_recurse(&entry, &value);
1901 if (g_str_equal(key, "Name"))
1902 netreg_update_name(modem, &value);
1903 else if (g_str_equal(key, "Strength"))
1904 netreg_update_strength(modem, &value);
1905 else if (g_str_equal(key, "DataStrength"))
1906 netreg_update_datastrength(modem, &value);
1907 else if (g_str_equal(key, "Status"))
1908 netreg_update_status(modem, &value);
1910 dbus_message_iter_next(dict);
1913 if (modem->registered)
1914 add_cdma_network(modem);
1916 remove_all_networks(modem);
1919 static int cdma_netreg_get_properties(struct modem_data *modem)
1921 return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
1922 cdma_netreg_properties_reply, modem);
1925 static void cm_update_attached(struct modem_data *modem,
1926 DBusMessageIter *value)
1928 dbus_bool_t attached;
1930 dbus_message_iter_get_basic(value, &attached);
1931 modem->attached = attached;
1933 DBG("%s Attached %d", modem->path, modem->attached);
1935 if (!modem->attached) {
1936 remove_all_networks(modem);
1940 if (!has_interface(modem->interfaces, OFONO_API_NETREG))
1943 netreg_get_properties(modem);
1946 static void cm_update_powered(struct modem_data *modem,
1947 DBusMessageIter *value)
1949 dbus_bool_t cm_powered;
1951 dbus_message_iter_get_basic(value, &cm_powered);
1952 modem->cm_powered = cm_powered;
1954 DBG("%s ConnnectionManager Powered %d", modem->path,
1957 if (modem->cm_powered)
1960 cm_set_powered(modem, TRUE);
1963 static gboolean cm_changed(DBusConnection *conn, DBusMessage *message,
1966 const char *path = dbus_message_get_path(message);
1967 struct modem_data *modem;
1968 DBusMessageIter iter, value;
1971 modem = g_hash_table_lookup(modem_hash, path);
1978 if (!dbus_message_iter_init(message, &iter))
1981 dbus_message_iter_get_basic(&iter, &key);
1983 dbus_message_iter_next(&iter);
1984 dbus_message_iter_recurse(&iter, &value);
1986 if (g_str_equal(key, "Attached"))
1987 cm_update_attached(modem, &value);
1988 else if (g_str_equal(key, "Powered"))
1989 cm_update_powered(modem, &value);
1994 static void cdma_cm_update_powered(struct modem_data *modem,
1995 DBusMessageIter *value)
1997 struct network_context *context = NULL;
1998 dbus_bool_t cdma_cm_powered;
2000 dbus_message_iter_get_basic(value, &cdma_cm_powered);
2001 modem->cdma_cm_powered = cdma_cm_powered;
2003 DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
2005 if (!modem->context_list)
2008 /* In case of CDMA, there is only one context */
2009 context = modem->context_list->data;
2010 if (modem->cdma_cm_powered)
2011 set_connected(modem, context);
2013 set_disconnected(context);
2016 static void cdma_cm_update_settings(struct modem_data *modem,
2017 DBusMessageIter *value)
2019 DBG("%s Settings", modem->path);
2021 extract_ipv4_settings(value, modem->context_list->data);
2024 static gboolean cdma_cm_changed(DBusConnection *conn,
2025 DBusMessage *message, void *user_data)
2027 const char *path = dbus_message_get_path(message);
2028 struct modem_data *modem;
2029 DBusMessageIter iter, value;
2032 modem = g_hash_table_lookup(modem_hash, path);
2036 if (modem->online && !modem->context_list)
2037 cdma_netreg_get_properties(modem);
2039 if (!dbus_message_iter_init(message, &iter))
2042 dbus_message_iter_get_basic(&iter, &key);
2044 dbus_message_iter_next(&iter);
2045 dbus_message_iter_recurse(&iter, &value);
2047 if (g_str_equal(key, "Powered"))
2048 cdma_cm_update_powered(modem, &value);
2049 if (g_str_equal(key, "Settings"))
2050 cdma_cm_update_settings(modem, &value);
2055 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
2057 DBG("%s", modem->path);
2059 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
2060 DBusMessageIter entry, value;
2063 dbus_message_iter_recurse(dict, &entry);
2064 dbus_message_iter_get_basic(&entry, &key);
2066 dbus_message_iter_next(&entry);
2067 dbus_message_iter_recurse(&entry, &value);
2069 if (g_str_equal(key, "Attached"))
2070 cm_update_attached(modem, &value);
2071 else if (g_str_equal(key, "Powered"))
2072 cm_update_powered(modem, &value);
2074 dbus_message_iter_next(dict);
2078 static int cm_get_properties(struct modem_data *modem)
2080 return get_properties(modem->path, OFONO_CM_INTERFACE,
2081 cm_properties_reply, modem);
2084 static void cdma_cm_properties_reply(struct modem_data *modem,
2085 DBusMessageIter *dict)
2087 DBG("%s", modem->path);
2090 cdma_netreg_get_properties(modem);
2092 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
2093 DBusMessageIter entry, value;
2096 dbus_message_iter_recurse(dict, &entry);
2097 dbus_message_iter_get_basic(&entry, &key);
2099 dbus_message_iter_next(&entry);
2100 dbus_message_iter_recurse(&entry, &value);
2102 if (g_str_equal(key, "Powered"))
2103 cdma_cm_update_powered(modem, &value);
2104 if (g_str_equal(key, "Settings"))
2105 cdma_cm_update_settings(modem, &value);
2107 dbus_message_iter_next(dict);
2111 static int cdma_cm_get_properties(struct modem_data *modem)
2113 return get_properties(modem->path, OFONO_CDMA_CM_INTERFACE,
2114 cdma_cm_properties_reply, modem);
2117 static void sim_update_imsi(struct modem_data *modem,
2118 DBusMessageIter *value)
2122 dbus_message_iter_get_basic(value, &imsi);
2124 DBG("%s imsi %s", modem->path, imsi);
2126 g_free(modem->imsi);
2127 modem->imsi = g_strdup(imsi);
2130 static gboolean sim_changed(DBusConnection *conn, DBusMessage *message,
2133 const char *path = dbus_message_get_path(message);
2134 struct modem_data *modem;
2135 DBusMessageIter iter, value;
2138 modem = g_hash_table_lookup(modem_hash, path);
2145 if (!dbus_message_iter_init(message, &iter))
2148 dbus_message_iter_get_basic(&iter, &key);
2150 dbus_message_iter_next(&iter);
2151 dbus_message_iter_recurse(&iter, &value);
2153 if (g_str_equal(key, "SubscriberIdentity")) {
2154 sim_update_imsi(modem, &value);
2156 if (!ready_to_create_device(modem))
2160 * This is a GSM modem. Create the device and
2161 * register it at the core. Enabling (setting
2162 * it online is done through the
2163 * modem_enable() callback.
2165 create_device(modem);
2171 static void sim_properties_reply(struct modem_data *modem,
2172 DBusMessageIter *dict)
2174 DBG("%s", modem->path);
2176 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
2177 DBusMessageIter entry, value;
2180 dbus_message_iter_recurse(dict, &entry);
2181 dbus_message_iter_get_basic(&entry, &key);
2183 dbus_message_iter_next(&entry);
2184 dbus_message_iter_recurse(&entry, &value);
2186 if (g_str_equal(key, "SubscriberIdentity")) {
2187 sim_update_imsi(modem, &value);
2189 if (!ready_to_create_device(modem))
2193 * This is a GSM modem. Create the device and
2194 * register it at the core. Enabling (setting
2195 * it online is done through the
2196 * modem_enable() callback.
2198 create_device(modem);
2204 * The modem is already online and we have the CM interface.
2205 * There will be no interface update and therefore our
2206 * state machine will not go to next step. We have to
2207 * trigger it from here.
2209 if (has_interface(modem->interfaces, OFONO_API_CM)) {
2210 cm_get_properties(modem);
2211 cm_get_contexts(modem);
2216 dbus_message_iter_next(dict);
2220 static int sim_get_properties(struct modem_data *modem)
2222 return get_properties(modem->path, OFONO_SIM_INTERFACE,
2223 sim_properties_reply, modem);
2226 static bool api_added(uint8_t old_iface, uint8_t new_iface,
2229 if (!has_interface(old_iface, api) &&
2230 has_interface(new_iface, api)) {
2231 DBG("%s added", api2string(api));
2238 static bool api_removed(uint8_t old_iface, uint8_t new_iface,
2241 if (has_interface(old_iface, api) &&
2242 !has_interface(new_iface, api)) {
2243 DBG("%s removed", api2string(api));
2250 static void modem_update_interfaces(struct modem_data *modem,
2254 DBG("%s", modem->path);
2256 if (api_added(old_ifaces, new_ifaces, OFONO_API_SIM)) {
2258 !modem->set_powered) {
2260 * Only use do GetProperties() when
2261 * device has not been powered up.
2263 sim_get_properties(modem);
2267 if (api_added(old_ifaces, new_ifaces, OFONO_API_CM)) {
2268 if (modem->device) {
2269 cm_get_properties(modem);
2270 cm_get_contexts(modem);
2274 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_CM)) {
2275 if (ready_to_create_device(modem)) {
2276 create_device(modem);
2277 if (modem->registered)
2278 add_cdma_network(modem);
2282 cdma_cm_get_properties(modem);
2285 if (api_added(old_ifaces, new_ifaces, OFONO_API_NETREG)) {
2286 if (modem->attached)
2287 netreg_get_properties(modem);
2290 if (api_added(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG))
2291 cdma_netreg_get_properties(modem);
2293 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CM)) {
2294 if (modem->call_get_contexts) {
2295 DBG("cancelling pending GetContexts call");
2296 dbus_pending_call_cancel(modem->call_get_contexts);
2297 dbus_pending_call_unref(modem->call_get_contexts);
2298 modem->call_get_contexts = NULL;
2300 remove_all_contexts(modem);
2303 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_CM))
2304 remove_all_contexts(modem);
2306 if (api_removed(old_ifaces, new_ifaces, OFONO_API_NETREG))
2307 remove_all_networks(modem);
2309 if (api_removed(old_ifaces, new_ifaces, OFONO_API_CDMA_NETREG))
2310 remove_all_networks(modem);
2313 static gboolean modem_changed(DBusConnection *conn, DBusMessage *message,
2316 const char *path = dbus_message_get_path(message);
2317 struct modem_data *modem;
2318 DBusMessageIter iter, value;
2321 modem = g_hash_table_lookup(modem_hash, path);
2328 if (!dbus_message_iter_init(message, &iter))
2331 dbus_message_iter_get_basic(&iter, &key);
2333 dbus_message_iter_next(&iter);
2334 dbus_message_iter_recurse(&iter, &value);
2336 if (g_str_equal(key, "Powered")) {
2337 dbus_bool_t powered;
2339 dbus_message_iter_get_basic(&value, &powered);
2340 modem->powered = powered;
2342 DBG("%s Powered %d", modem->path, modem->powered);
2344 /* Set the powered according to the value */
2345 modem_set_powered(modem, powered);
2346 } else if (g_str_equal(key, "Online")) {
2349 dbus_message_iter_get_basic(&value, &online);
2350 modem->online = online;
2352 DBG("%s Online %d", modem->path, modem->online);
2357 connman_device_set_powered(modem->device, modem->online);
2358 } else if (g_str_equal(key, "Interfaces")) {
2361 interfaces = extract_interfaces(&value);
2363 if (interfaces == modem->interfaces)
2366 DBG("%s Interfaces 0x%02x", modem->path, interfaces);
2368 modem_update_interfaces(modem, modem->interfaces, interfaces);
2370 modem->interfaces = interfaces;
2371 } else if (g_str_equal(key, "Serial")) {
2374 dbus_message_iter_get_basic(&value, &serial);
2376 g_free(modem->serial);
2377 modem->serial = g_strdup(serial);
2379 DBG("%s Serial %s", modem->path, modem->serial);
2381 if (has_interface(modem->interfaces,
2382 OFONO_API_CDMA_CM)) {
2383 if (ready_to_create_device(modem)) {
2384 create_device(modem);
2385 if (modem->registered)
2386 add_cdma_network(modem);
2394 static void add_modem(const char *path, DBusMessageIter *prop)
2396 struct modem_data *modem;
2400 modem = g_hash_table_lookup(modem_hash, path);
2403 * When oFono powers up we ask for the modems and oFono is
2404 * reporting with modem_added signal the modems. Only
2410 modem = g_try_new0(struct modem_data, 1);
2414 modem->path = g_strdup(path);
2416 g_hash_table_insert(modem_hash, g_strdup(path), modem);
2418 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
2419 DBusMessageIter entry, value;
2422 dbus_message_iter_recurse(prop, &entry);
2423 dbus_message_iter_get_basic(&entry, &key);
2425 dbus_message_iter_next(&entry);
2426 dbus_message_iter_recurse(&entry, &value);
2428 if (g_str_equal(key, "Powered")) {
2429 dbus_bool_t powered;
2431 dbus_message_iter_get_basic(&value, &powered);
2432 modem->powered = powered;
2434 DBG("%s Powered %d", modem->path, modem->powered);
2435 } else if (g_str_equal(key, "Online")) {
2438 dbus_message_iter_get_basic(&value, &online);
2439 modem->online = online;
2441 DBG("%s Online %d", modem->path, modem->online);
2442 } else if (g_str_equal(key, "Interfaces")) {
2443 modem->interfaces = extract_interfaces(&value);
2445 DBG("%s Interfaces 0x%02x", modem->path,
2447 } else if (g_str_equal(key, "Serial")) {
2450 dbus_message_iter_get_basic(&value, &serial);
2451 modem->serial = g_strdup(serial);
2453 DBG("%s Serial %s", modem->path, modem->serial);
2454 } else if (g_str_equal(key, "Type")) {
2457 dbus_message_iter_get_basic(&value, &type);
2459 DBG("%s Type %s", modem->path, type);
2460 if (g_strcmp0(type, "hardware") != 0) {
2461 DBG("%s Ignore this modem", modem->path);
2462 modem->ignore = true;
2466 dbus_message_iter_next(prop);
2472 if (!modem->powered) {
2473 modem_set_powered(modem, TRUE);
2477 modem_update_interfaces(modem, 0, modem->interfaces);
2480 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
2482 struct modem_data *modem = value;
2484 DBG("%s", modem->path);
2489 modem_set_powered(modem, FALSE);
2492 static void remove_modem(gpointer data)
2494 struct modem_data *modem = data;
2496 DBG("%s", modem->path);
2498 if (modem->call_set_property) {
2499 dbus_pending_call_cancel(modem->call_set_property);
2500 dbus_pending_call_unref(modem->call_set_property);
2501 modem->call_set_property = NULL;
2504 if (modem->call_get_properties) {
2505 dbus_pending_call_cancel(modem->call_get_properties);
2506 dbus_pending_call_unref(modem->call_get_properties);
2507 modem->call_get_properties = NULL;
2510 if (modem->call_get_contexts) {
2511 dbus_pending_call_cancel(modem->call_get_contexts);
2512 dbus_pending_call_unref(modem->call_get_contexts);
2513 modem->call_get_contexts = NULL;
2516 /* Must remove the contexts before the device */
2517 if (modem->context_list)
2518 remove_all_contexts(modem);
2521 destroy_device(modem);
2523 g_free(modem->serial);
2524 g_free(modem->name);
2525 g_free(modem->imsi);
2526 g_free(modem->path);
2531 static gboolean modem_added(DBusConnection *conn,
2532 DBusMessage *message, void *user_data)
2534 DBusMessageIter iter, properties;
2539 if (!dbus_message_iter_init(message, &iter))
2542 dbus_message_iter_get_basic(&iter, &path);
2544 dbus_message_iter_next(&iter);
2545 dbus_message_iter_recurse(&iter, &properties);
2547 add_modem(path, &properties);
2552 static gboolean modem_removed(DBusConnection *conn,
2553 DBusMessage *message, void *user_data)
2555 DBusMessageIter iter;
2560 if (!dbus_message_iter_init(message, &iter))
2563 dbus_message_iter_get_basic(&iter, &path);
2565 g_hash_table_remove(modem_hash, path);
2570 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
2574 DBusMessageIter array, dict;
2578 reply = dbus_pending_call_steal_reply(call);
2580 dbus_error_init(&error);
2582 if (dbus_set_error_from_message(&error, reply)) {
2583 connman_error("%s", error.message);
2584 dbus_error_free(&error);
2588 if (!dbus_message_iter_init(reply, &array))
2591 dbus_message_iter_recurse(&array, &dict);
2593 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
2594 DBusMessageIter value, properties;
2597 dbus_message_iter_recurse(&dict, &value);
2598 dbus_message_iter_get_basic(&value, &path);
2600 dbus_message_iter_next(&value);
2601 dbus_message_iter_recurse(&value, &properties);
2603 add_modem(path, &properties);
2605 dbus_message_iter_next(&dict);
2609 dbus_message_unref(reply);
2611 dbus_pending_call_unref(call);
2614 static int manager_get_modems(void)
2616 DBusMessage *message;
2617 DBusPendingCall *call;
2621 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
2622 OFONO_MANAGER_INTERFACE, GET_MODEMS);
2626 if (!dbus_connection_send_with_reply(connection, message,
2628 connman_error("Failed to call GetModems()");
2629 dbus_message_unref(message);
2634 connman_error("D-Bus connection not available");
2635 dbus_message_unref(message);
2639 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2642 dbus_message_unref(message);
2644 return -EINPROGRESS;
2647 static void ofono_connect(DBusConnection *conn, void *user_data)
2651 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2652 g_free, remove_modem);
2656 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2658 if (!context_hash) {
2659 g_hash_table_destroy(modem_hash);
2663 manager_get_modems();
2666 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2670 if (!modem_hash || !context_hash)
2673 g_hash_table_destroy(modem_hash);
2676 g_hash_table_destroy(context_hash);
2677 context_hash = NULL;
2680 static int network_probe(struct connman_network *network)
2682 struct modem_data *modem = connman_network_get_data(network);
2684 DBG("%s network %p", modem->path, network);
2689 static void network_remove(struct connman_network *network)
2691 struct modem_data *modem = connman_network_get_data(network);
2693 DBG("%s network %p", modem->path, network);
2696 static int network_connect(struct connman_network *network)
2698 struct network_context *context;
2699 struct modem_data *modem = connman_network_get_data(network);
2701 DBG("%s network %p", modem->path, network);
2703 if (!g_hash_table_lookup(modem_hash, modem->path))
2706 context = get_context_with_network(modem->context_list, network);
2710 if (has_interface(modem->interfaces, OFONO_API_CM))
2711 return context_set_active(modem, context, TRUE);
2712 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM))
2713 return cdma_cm_set_powered(modem, TRUE);
2715 connman_error("Connection manager interface not available");
2720 static int network_disconnect(struct connman_network *network)
2722 struct network_context *context;
2723 struct modem_data *modem = connman_network_get_data(network);
2725 DBG("%s network %p", modem->path, network);
2727 if (!g_hash_table_lookup(modem_hash, modem->path))
2730 context = get_context_with_network(modem->context_list, network);
2734 if (has_interface(modem->interfaces, OFONO_API_CM))
2735 return context_set_active(modem, context, FALSE);
2736 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM))
2737 return cdma_cm_set_powered(modem, FALSE);
2739 connman_error("Connection manager interface not available");
2744 static struct connman_network_driver network_driver = {
2746 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2747 .probe = network_probe,
2748 .remove = network_remove,
2749 .connect = network_connect,
2750 .disconnect = network_disconnect,
2753 static int modem_probe(struct connman_device *device)
2755 struct modem_data *modem = connman_device_get_data(device);
2757 DBG("%s device %p", modem->path, device);
2762 static void modem_remove(struct connman_device *device)
2764 struct modem_data *modem = connman_device_get_data(device);
2766 DBG("%s device %p", modem->path, device);
2769 static int modem_enable(struct connman_device *device)
2771 struct modem_data *modem = connman_device_get_data(device);
2773 DBG("%s device %p", modem->path, device);
2778 return modem_set_online(modem, TRUE);
2781 static int modem_disable(struct connman_device *device)
2783 struct modem_data *modem = connman_device_get_data(device);
2785 DBG("%s device %p", modem->path, device);
2790 return modem_set_online(modem, FALSE);
2793 static struct connman_device_driver modem_driver = {
2795 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2796 .probe = modem_probe,
2797 .remove = modem_remove,
2798 .enable = modem_enable,
2799 .disable = modem_disable,
2802 static int tech_probe(struct connman_technology *technology)
2807 static void tech_remove(struct connman_technology *technology)
2811 static struct connman_technology_driver tech_driver = {
2813 .type = CONNMAN_SERVICE_TYPE_CELLULAR,
2814 .probe = tech_probe,
2815 .remove = tech_remove,
2819 static guint modem_added_watch;
2820 static guint modem_removed_watch;
2821 static guint modem_watch;
2822 static guint cm_watch;
2823 static guint sim_watch;
2824 static guint context_added_watch;
2825 static guint context_removed_watch;
2826 static guint netreg_watch;
2827 static guint context_watch;
2828 static guint cdma_cm_watch;
2829 static guint cdma_netreg_watch;
2831 static int ofono_init(void)
2837 connection = connman_dbus_get_connection();
2841 watch = g_dbus_add_service_watch(connection,
2842 OFONO_SERVICE, ofono_connect,
2843 ofono_disconnect, NULL, NULL);
2845 modem_added_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2846 NULL, OFONO_MANAGER_INTERFACE,
2851 modem_removed_watch = g_dbus_add_signal_watch(connection,
2852 OFONO_SERVICE, NULL,
2853 OFONO_MANAGER_INTERFACE,
2858 modem_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2859 OFONO_MODEM_INTERFACE,
2864 cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2870 sim_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2871 OFONO_SIM_INTERFACE,
2876 context_added_watch = g_dbus_add_signal_watch(connection,
2877 OFONO_SERVICE, NULL,
2883 context_removed_watch = g_dbus_add_signal_watch(connection,
2884 OFONO_SERVICE, NULL,
2890 context_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2891 NULL, OFONO_CONTEXT_INTERFACE,
2896 netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
2897 OFONO_NETREG_INTERFACE,
2902 cdma_cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2903 NULL, OFONO_CDMA_CM_INTERFACE,
2908 cdma_netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
2909 NULL, OFONO_CDMA_NETREG_INTERFACE,
2911 cdma_netreg_changed,
2915 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2916 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2917 context_added_watch == 0 ||
2918 context_removed_watch == 0 ||
2919 context_watch == 0 || netreg_watch == 0 ||
2920 cdma_cm_watch == 0 || cdma_netreg_watch == 0) {
2925 err = connman_network_driver_register(&network_driver);
2929 err = connman_device_driver_register(&modem_driver);
2931 connman_network_driver_unregister(&network_driver);
2935 err = connman_technology_driver_register(&tech_driver);
2937 connman_device_driver_unregister(&modem_driver);
2938 connman_network_driver_unregister(&network_driver);
2945 g_dbus_remove_watch(connection, cdma_netreg_watch);
2946 g_dbus_remove_watch(connection, cdma_cm_watch);
2947 g_dbus_remove_watch(connection, netreg_watch);
2948 g_dbus_remove_watch(connection, context_watch);
2949 g_dbus_remove_watch(connection, context_removed_watch);
2950 g_dbus_remove_watch(connection, context_added_watch);
2951 g_dbus_remove_watch(connection, sim_watch);
2952 g_dbus_remove_watch(connection, cm_watch);
2953 g_dbus_remove_watch(connection, modem_watch);
2954 g_dbus_remove_watch(connection, modem_removed_watch);
2955 g_dbus_remove_watch(connection, modem_added_watch);
2956 g_dbus_remove_watch(connection, watch);
2957 dbus_connection_unref(connection);
2962 static void ofono_exit(void)
2968 * We should propably wait for the SetProperty() reply
2969 * message, because ...
2971 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2974 * ... here we will cancel the call.
2976 g_hash_table_destroy(modem_hash);
2981 g_hash_table_destroy(context_hash);
2982 context_hash = NULL;
2985 connman_technology_driver_unregister(&tech_driver);
2986 connman_device_driver_unregister(&modem_driver);
2987 connman_network_driver_unregister(&network_driver);
2989 g_dbus_remove_watch(connection, cdma_netreg_watch);
2990 g_dbus_remove_watch(connection, cdma_cm_watch);
2991 g_dbus_remove_watch(connection, netreg_watch);
2992 g_dbus_remove_watch(connection, context_watch);
2993 g_dbus_remove_watch(connection, context_removed_watch);
2994 g_dbus_remove_watch(connection, context_added_watch);
2995 g_dbus_remove_watch(connection, sim_watch);
2996 g_dbus_remove_watch(connection, cm_watch);
2997 g_dbus_remove_watch(connection, modem_watch);
2998 g_dbus_remove_watch(connection, modem_added_watch);
2999 g_dbus_remove_watch(connection, modem_removed_watch);
3000 g_dbus_remove_watch(connection, watch);
3002 dbus_connection_unref(connection);
3005 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
3006 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)