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>
43 #define uninitialized_var(x) x = x
45 #define OFONO_SERVICE "org.ofono"
47 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
49 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
50 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
51 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
52 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
53 #define OFONO_CDMA_CM_INTERFACE OFONO_SERVICE ".cdma.ConnectionManager"
54 #define OFONO_CDMA_NETREG_INTERFACE OFONO_SERVICE ".cdma.NetworkRegistration"
56 #define MODEM_ADDED "ModemAdded"
57 #define MODEM_REMOVED "ModemRemoved"
58 #define PROPERTY_CHANGED "PropertyChanged"
59 #define CONTEXT_ADDED "ContextAdded"
60 #define CONTEXT_REMOVED "ContextRemoved"
62 #define GET_PROPERTIES "GetProperties"
63 #define SET_PROPERTY "SetProperty"
64 #define GET_MODEMS "GetModems"
65 #define GET_CONTEXTS "GetContexts"
71 OFONO_API_NETREG = 0x2,
73 OFONO_API_CDMA_NETREG = 0x8,
74 OFONO_API_CDMA_CM = 0x10,
78 * The way this plugin works is following:
80 * powered -> SubscriberIdentity or Online = True -> gprs, context ->
81 * attached -> netreg -> ready
83 * Enabling and disabling modems are steered through the rfkill
84 * interface. That means when ConnMan toggles the rfkill bit oFono
85 * will add or remove the modems.
87 * ConnMan will always power up (set Powered and Online) the
88 * modems. No need to power them down because this will be done
89 * through the rfkill inteface.
92 static DBusConnection *connection;
94 static GHashTable *modem_hash;
95 static GHashTable *context_hash;
97 struct network_context {
101 enum connman_ipconfig_method ipv4_method;
102 struct connman_ipaddress *ipv4_address;
103 char *ipv4_nameservers;
105 enum connman_ipconfig_method ipv6_method;
106 struct connman_ipaddress *ipv6_address;
107 char *ipv6_nameservers;
113 struct connman_device *device;
114 struct connman_network *network;
116 struct network_context *context;
118 /* Modem Interface */
120 connman_bool_t powered;
121 connman_bool_t online;
123 connman_bool_t ignore;
125 connman_bool_t set_powered;
126 connman_bool_t set_online;
128 /* ConnectionManager Interface */
129 connman_bool_t attached;
130 connman_bool_t cm_powered;
132 connman_bool_t set_cm_powered;
134 /* ConnectionContext Interface */
135 connman_bool_t active;
136 connman_bool_t set_active;
138 /* SimManager Interface */
141 /* Netreg Interface */
144 connman_bool_t roaming;
147 DBusPendingCall *call_set_property;
148 DBusPendingCall *call_get_properties;
149 DBusPendingCall *call_get_contexts;
152 static char *get_ident(const char *path)
159 pos = strrchr(path, '/');
166 static struct network_context *network_context_alloc(const char *path)
168 struct network_context *context;
170 context = g_try_new0(struct network_context, 1);
174 context->path = g_strdup(path);
177 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
178 context->ipv4_address = NULL;
179 context->ipv4_nameservers = NULL;
181 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
182 context->ipv6_address = NULL;
183 context->ipv6_nameservers = NULL;
188 static void network_context_free(struct network_context *context)
190 g_free(context->path);
192 connman_ipaddress_free(context->ipv4_address);
193 g_free(context->ipv4_nameservers);
195 connman_ipaddress_free(context->ipv6_address);
196 g_free(context->ipv6_nameservers);
201 static void set_connected(struct modem_data *modem)
203 connman_bool_t setip = FALSE;
205 DBG("%s", modem->path);
207 connman_network_set_index(modem->network, modem->context->index);
209 switch (modem->context->ipv4_method) {
210 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
211 case CONNMAN_IPCONFIG_METHOD_OFF:
212 case CONNMAN_IPCONFIG_METHOD_MANUAL:
213 case CONNMAN_IPCONFIG_METHOD_AUTO:
216 case CONNMAN_IPCONFIG_METHOD_FIXED:
217 connman_network_set_ipv4_method(modem->network,
218 modem->context->ipv4_method);
219 connman_network_set_ipaddress(modem->network,
220 modem->context->ipv4_address);
221 connman_network_set_nameservers(modem->network,
222 modem->context->ipv4_nameservers);
226 case CONNMAN_IPCONFIG_METHOD_DHCP:
227 connman_network_set_ipv4_method(modem->network,
228 modem->context->ipv4_method);
233 switch (modem->context->ipv6_method) {
234 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
235 case CONNMAN_IPCONFIG_METHOD_OFF:
236 case CONNMAN_IPCONFIG_METHOD_MANUAL:
237 case CONNMAN_IPCONFIG_METHOD_DHCP:
238 case CONNMAN_IPCONFIG_METHOD_AUTO:
241 case CONNMAN_IPCONFIG_METHOD_FIXED:
242 connman_network_set_ipv6_method(modem->network,
243 modem->context->ipv6_method);
244 connman_network_set_ipaddress(modem->network,
245 modem->context->ipv6_address);
251 connman_network_set_connected(modem->network, TRUE);
254 static void set_disconnected(struct modem_data *modem)
256 DBG("%s", modem->path);
258 connman_network_set_connected(modem->network, FALSE);
261 typedef void (*set_property_cb)(struct modem_data *data,
262 connman_bool_t success);
263 typedef void (*get_properties_cb)(struct modem_data *data,
264 DBusMessageIter *dict);
266 struct property_info {
267 struct modem_data *modem;
269 const char *interface;
270 const char *property;
271 set_property_cb set_property_cb;
272 get_properties_cb get_properties_cb;
275 static void set_property_reply(DBusPendingCall *call, void *user_data)
277 struct property_info *info = user_data;
280 connman_bool_t success = TRUE;
282 DBG("%s path %s %s.%s", info->modem->path,
283 info->path, info->interface, info->property);
285 info->modem->call_set_property = NULL;
287 dbus_error_init(&error);
289 reply = dbus_pending_call_steal_reply(call);
291 if (dbus_set_error_from_message(&error, reply)) {
292 connman_error("Failed to change property: %s %s.%s: %s %s",
293 info->path, info->interface, info->property,
294 error.name, error.message);
295 dbus_error_free(&error);
299 if (info->set_property_cb != NULL)
300 (*info->set_property_cb)(info->modem, success);
302 dbus_message_unref(reply);
304 dbus_pending_call_unref(call);
307 static int set_property(struct modem_data *modem,
308 const char *path, const char *interface,
309 const char *property, int type, void *value,
310 set_property_cb notify)
312 DBusMessage *message;
313 DBusMessageIter iter;
314 struct property_info *info;
316 DBG("%s path %s %s.%s", modem->path, path, interface, property);
318 if (modem->call_set_property != NULL) {
319 connman_error("Pending SetProperty");
323 message = dbus_message_new_method_call(OFONO_SERVICE, path,
324 interface, SET_PROPERTY);
328 dbus_message_iter_init_append(message, &iter);
329 connman_dbus_property_append_basic(&iter, property, type, value);
331 if (dbus_connection_send_with_reply(connection, message,
332 &modem->call_set_property, TIMEOUT) == FALSE) {
333 connman_error("Failed to change property: %s %s.%s",
334 path, interface, property);
335 dbus_message_unref(message);
339 if (modem->call_set_property == NULL) {
340 connman_error("D-Bus connection not available");
341 dbus_message_unref(message);
345 info = g_try_new0(struct property_info, 1);
347 dbus_message_unref(message);
353 info->interface = interface;
354 info->property = property;
355 info->set_property_cb = notify;
357 dbus_pending_call_set_notify(modem->call_set_property,
358 set_property_reply, info, g_free);
360 dbus_message_unref(message);
365 static void get_properties_reply(DBusPendingCall *call, void *user_data)
367 struct property_info *info = user_data;
368 DBusMessageIter array, dict;
372 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
374 info->modem->call_get_properties = NULL;
376 dbus_error_init(&error);
378 reply = dbus_pending_call_steal_reply(call);
380 if (dbus_set_error_from_message(&error, reply)) {
381 connman_error("Failed to get properties: %s %s: %s %s",
382 info->path, info->interface,
383 error.name, error.message);
384 dbus_error_free(&error);
389 if (dbus_message_iter_init(reply, &array) == FALSE)
392 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
395 dbus_message_iter_recurse(&array, &dict);
397 if (info->get_properties_cb != NULL)
398 (*info->get_properties_cb)(info->modem, &dict);
402 dbus_message_unref(reply);
404 dbus_pending_call_unref(call);
407 static int get_properties(const char *path, const char *interface,
408 get_properties_cb notify,
409 struct modem_data *modem)
411 DBusMessage *message;
412 struct property_info *info;
414 DBG("%s path %s %s", modem->path, path, interface);
416 if (modem->call_get_properties != NULL) {
417 connman_error("Pending GetProperties");
421 message = dbus_message_new_method_call(OFONO_SERVICE, path,
422 interface, GET_PROPERTIES);
426 if (dbus_connection_send_with_reply(connection, message,
427 &modem->call_get_properties, TIMEOUT) == FALSE) {
428 connman_error("Failed to call %s.GetProperties()", interface);
429 dbus_message_unref(message);
433 if (modem->call_get_properties == NULL) {
434 connman_error("D-Bus connection not available");
435 dbus_message_unref(message);
439 info = g_try_new0(struct property_info, 1);
441 dbus_message_unref(message);
447 info->interface = interface;
448 info->get_properties_cb = notify;
450 dbus_pending_call_set_notify(modem->call_get_properties,
451 get_properties_reply, info, g_free);
453 dbus_message_unref(message);
458 static void context_set_active_reply(struct modem_data *modem,
459 connman_bool_t success)
461 DBG("%s", modem->path);
463 if (success == TRUE) {
465 * Don't handle do anything on success here. oFono will send
466 * the change via PropertyChanged singal.
472 * Active = True might fail due a timeout. That means oFono
473 * still tries to go online. If we retry to set Active = True,
474 * we just get a InProgress error message. Should we power
475 * cycle the modem in such cases?
478 connman_network_set_error(modem->network,
479 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
482 static int context_set_active(struct modem_data *modem)
484 dbus_bool_t active = TRUE;
486 DBG("%s", modem->path);
488 return set_property(modem, modem->context->path,
489 OFONO_CONTEXT_INTERFACE,
490 "Active", DBUS_TYPE_BOOLEAN,
492 context_set_active_reply);
495 static int context_set_inactive(struct modem_data *modem)
497 dbus_bool_t active = FALSE;
500 DBG("%s", modem->path);
502 err = set_property(modem, modem->context->path,
503 OFONO_CONTEXT_INTERFACE,
504 "Active", DBUS_TYPE_BOOLEAN,
507 if (err == -EINPROGRESS)
513 static void cdma_cm_set_powered_reply(struct modem_data *modem,
514 connman_bool_t success)
516 DBG("%s", modem->path);
518 if (success == TRUE) {
520 * Don't handle do anything on success here. oFono will send
521 * the change via PropertyChanged singal.
527 * Powered = True might fail due a timeout. That means oFono
528 * still tries to go online. If we retry to set Powered = True,
529 * we just get a InProgress error message. Should we power
530 * cycle the modem in such cases?
533 connman_network_set_error(modem->network,
534 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
537 static int cdma_cm_set_powered(struct modem_data *modem)
539 dbus_bool_t powered = TRUE;
541 DBG("%s", modem->path);
543 return set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
544 "Powered", DBUS_TYPE_BOOLEAN, &powered,
545 cdma_cm_set_powered_reply);
548 static int cdma_cm_shutdown(struct modem_data *modem)
550 dbus_bool_t powered = FALSE;
553 DBG("%s", modem->path);
555 err = set_property(modem, modem->path, OFONO_CDMA_CM_INTERFACE,
556 "Powered", DBUS_TYPE_BOOLEAN, &powered, NULL);
557 if (err == -EINPROGRESS)
563 static void modem_set_online_reply(struct modem_data *modem,
564 connman_bool_t success)
566 DBG("%s", modem->path);
568 if (success == TRUE) {
570 * Don't handle do anything on success here. oFono will send
571 * the change via PropertyChanged singal.
576 modem->set_online = FALSE;
579 static int modem_set_online(struct modem_data *modem)
581 DBG("%s", modem->path);
583 modem->set_online = TRUE;
585 return set_property(modem, modem->path,
586 OFONO_MODEM_INTERFACE,
587 "Online", DBUS_TYPE_BOOLEAN,
589 modem_set_online_reply);
592 static void cm_set_powered_reply(struct modem_data *modem,
593 connman_bool_t success)
595 DBG("%s", modem->path);
597 if (success == TRUE) {
599 * Don't handle do anything on success here. oFono will send
600 * the change via PropertyChanged singal.
605 modem->set_cm_powered = FALSE;
608 static int cm_set_powered(struct modem_data *modem)
610 DBG("%s", modem->path);
612 modem->set_cm_powered = TRUE;
614 return set_property(modem, modem->path,
616 "Powered", DBUS_TYPE_BOOLEAN,
617 &modem->set_cm_powered,
618 cm_set_powered_reply);
621 static int modem_set_powered(struct modem_data *modem)
623 DBG("%s", modem->path);
625 modem->set_powered = TRUE;
627 return set_property(modem, modem->path,
628 OFONO_MODEM_INTERFACE,
629 "Powered", DBUS_TYPE_BOOLEAN,
634 static int modem_set_unpowered(struct modem_data *modem)
636 DBG("%s", modem->path);
638 modem->set_powered = FALSE;
640 return set_property(modem, modem->path,
641 OFONO_MODEM_INTERFACE,
642 "Powered", DBUS_TYPE_BOOLEAN,
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;
955 static void destroy_device(struct modem_data *modem)
957 DBG("%s", modem->path);
959 connman_device_set_powered(modem->device, FALSE);
961 if (modem->network != NULL) {
962 connman_device_remove_network(modem->device, modem->network);
963 connman_network_unref(modem->network);
964 modem->network = NULL;
967 connman_device_unregister(modem->device);
968 connman_device_unref(modem->device);
970 modem->device = NULL;
973 static void add_network(struct modem_data *modem)
977 DBG("%s", modem->path);
979 if (modem->network != NULL)
982 modem->network = connman_network_create(modem->context->path,
983 CONNMAN_NETWORK_TYPE_CELLULAR);
984 if (modem->network == NULL)
987 DBG("network %p", modem->network);
989 connman_network_set_data(modem->network, modem);
991 connman_network_set_string(modem->network, "Path",
992 modem->context->path);
994 connman_network_set_index(modem->network, modem->context->index);
996 if (modem->name != NULL)
997 connman_network_set_name(modem->network, modem->name);
999 connman_network_set_name(modem->network, "");
1001 connman_network_set_strength(modem->network, modem->strength);
1003 group = get_ident(modem->context->path);
1004 connman_network_set_group(modem->network, group);
1006 connman_network_set_available(modem->network, TRUE);
1008 connman_network_set_bool(modem->network, "Roaming",
1011 if (connman_device_add_network(modem->device, modem->network) < 0) {
1012 connman_network_unref(modem->network);
1013 modem->network = NULL;
1018 static int add_cm_context(struct modem_data *modem, const char *context_path,
1019 DBusMessageIter *dict)
1021 const char *context_type;
1022 struct network_context *context = NULL;
1023 connman_bool_t active = FALSE;
1025 DBG("%s context path %s", modem->path, context_path);
1027 if (modem->context != NULL) {
1029 * We have already assigned a context to this modem
1030 * and we do only support one Internet context.
1035 context = network_context_alloc(context_path);
1036 if (context == NULL)
1039 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1040 DBusMessageIter entry, value;
1043 dbus_message_iter_recurse(dict, &entry);
1044 dbus_message_iter_get_basic(&entry, &key);
1046 dbus_message_iter_next(&entry);
1047 dbus_message_iter_recurse(&entry, &value);
1049 if (g_str_equal(key, "Type") == TRUE) {
1050 dbus_message_iter_get_basic(&value, &context_type);
1052 DBG("%s context %s type %s", modem->path,
1053 context_path, context_type);
1054 } else if (g_str_equal(key, "Settings") == TRUE) {
1055 DBG("%s Settings", modem->path);
1057 extract_ipv4_settings(&value, context);
1058 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1059 DBG("%s IPv6.Settings", modem->path);
1061 extract_ipv6_settings(&value, context);
1062 } else if (g_str_equal(key, "Active") == TRUE) {
1063 dbus_message_iter_get_basic(&value, &active);
1065 DBG("%s Active %d", modem->path, active);
1068 dbus_message_iter_next(dict);
1071 if (g_strcmp0(context_type, "internet") != 0) {
1072 network_context_free(context);
1076 modem->context = context;
1077 modem->active = active;
1079 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1084 static void remove_cm_context(struct modem_data *modem,
1085 const char *context_path)
1087 if (modem->context == NULL)
1090 g_hash_table_remove(context_hash, context_path);
1092 network_context_free(modem->context);
1093 modem->context = NULL;
1096 static gboolean context_changed(DBusConnection *connection,
1097 DBusMessage *message,
1100 const char *context_path = dbus_message_get_path(message);
1101 struct modem_data *modem = NULL;
1102 DBusMessageIter iter, value;
1105 DBG("context_path %s", context_path);
1107 modem = g_hash_table_lookup(context_hash, context_path);
1111 if (dbus_message_iter_init(message, &iter) == FALSE)
1114 dbus_message_iter_get_basic(&iter, &key);
1116 dbus_message_iter_next(&iter);
1117 dbus_message_iter_recurse(&iter, &value);
1120 * oFono guarantees the ordering of Settings and
1121 * Active. Settings will always be send before Active = True.
1122 * That means we don't have to order here.
1124 if (g_str_equal(key, "Settings") == TRUE) {
1125 DBG("%s Settings", modem->path);
1127 extract_ipv4_settings(&value, modem->context);
1128 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1129 DBG("%s IPv6.Settings", modem->path);
1131 extract_ipv6_settings(&value, modem->context);
1132 } else if (g_str_equal(key, "Active") == TRUE) {
1133 dbus_message_iter_get_basic(&value, &modem->active);
1135 DBG("%s Active %d", modem->path, modem->active);
1137 if (modem->active == TRUE)
1138 set_connected(modem);
1140 set_disconnected(modem);
1146 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1148 struct modem_data *modem = user_data;
1149 DBusMessageIter array, dict, entry, value;
1153 DBG("%s", modem->path);
1155 modem->call_get_contexts = NULL;
1157 reply = dbus_pending_call_steal_reply(call);
1159 dbus_error_init(&error);
1161 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1162 connman_error("%s", error.message);
1163 dbus_error_free(&error);
1167 if (dbus_message_iter_init(reply, &array) == FALSE)
1170 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1173 dbus_message_iter_recurse(&array, &dict);
1175 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1176 const char *context_path;
1178 dbus_message_iter_recurse(&dict, &entry);
1179 dbus_message_iter_get_basic(&entry, &context_path);
1181 dbus_message_iter_next(&entry);
1182 dbus_message_iter_recurse(&entry, &value);
1184 if (add_cm_context(modem, context_path, &value) == 0)
1187 dbus_message_iter_next(&dict);
1191 dbus_message_unref(reply);
1193 dbus_pending_call_unref(call);
1196 static int cm_get_contexts(struct modem_data *modem)
1198 DBusMessage *message;
1200 DBG("%s", modem->path);
1202 if (modem->call_get_contexts != NULL)
1205 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1206 OFONO_CM_INTERFACE, GET_CONTEXTS);
1207 if (message == NULL)
1210 if (dbus_connection_send_with_reply(connection, message,
1211 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1212 connman_error("Failed to call GetContexts()");
1213 dbus_message_unref(message);
1217 if (modem->call_get_contexts == NULL) {
1218 connman_error("D-Bus connection not available");
1219 dbus_message_unref(message);
1223 dbus_pending_call_set_notify(modem->call_get_contexts,
1224 cm_get_contexts_reply,
1227 dbus_message_unref(message);
1229 return -EINPROGRESS;
1232 static gboolean cm_context_added(DBusConnection *connection,
1233 DBusMessage *message,
1236 const char *path = dbus_message_get_path(message);
1238 struct modem_data *modem;
1239 DBusMessageIter iter, properties;
1243 modem = g_hash_table_lookup(modem_hash, context_path);
1247 if (dbus_message_iter_init(message, &iter) == FALSE)
1250 dbus_message_iter_get_basic(&iter, &context_path);
1252 dbus_message_iter_next(&iter);
1253 dbus_message_iter_recurse(&iter, &properties);
1255 if (add_cm_context(modem, context_path, &properties) != 0)
1261 static gboolean cm_context_removed(DBusConnection *connection,
1262 DBusMessage *message,
1265 const char *path = dbus_message_get_path(message);
1266 const char *context_path;
1267 struct modem_data *modem;
1268 DBusMessageIter iter;
1270 DBG("context path %s", path);
1272 if (dbus_message_iter_init(message, &iter) == FALSE)
1275 dbus_message_iter_get_basic(&iter, &context_path);
1277 modem = g_hash_table_lookup(context_hash, context_path);
1281 remove_cm_context(modem, context_path);
1286 static void netreg_update_name(struct modem_data *modem,
1287 DBusMessageIter* value)
1291 dbus_message_iter_get_basic(value, &name);
1293 DBG("%s Name %s", modem->path, name);
1295 g_free(modem->name);
1296 modem->name = g_strdup(name);
1298 if (modem->network == NULL)
1301 connman_network_set_name(modem->network, modem->name);
1302 connman_network_update(modem->network);
1305 static void netreg_update_strength(struct modem_data *modem,
1306 DBusMessageIter *value)
1308 dbus_message_iter_get_basic(value, &modem->strength);
1310 DBG("%s Strength %d", modem->path, modem->strength);
1312 if (modem->network == NULL)
1315 connman_network_set_strength(modem->network, modem->strength);
1316 connman_network_update(modem->network);
1319 static void netreg_update_roaming(struct modem_data *modem,
1320 DBusMessageIter *value)
1323 connman_bool_t roaming;
1325 dbus_message_iter_get_basic(value, &status);
1327 if (g_str_equal(status, "roaming") == TRUE)
1332 if (roaming == modem->roaming)
1335 modem->roaming = roaming;
1337 if (modem->network == NULL)
1340 connman_network_set_bool(modem->network,
1341 "Roaming", modem->roaming);
1342 connman_network_update(modem->network);
1345 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1348 const char *path = dbus_message_get_path(message);
1349 struct modem_data *modem;
1350 DBusMessageIter iter, value;
1353 modem = g_hash_table_lookup(modem_hash, path);
1357 if (modem->ignore == TRUE)
1360 if (dbus_message_iter_init(message, &iter) == FALSE)
1363 dbus_message_iter_get_basic(&iter, &key);
1365 dbus_message_iter_next(&iter);
1366 dbus_message_iter_recurse(&iter, &value);
1368 if (g_str_equal(key, "Name") == TRUE)
1369 netreg_update_name(modem, &value);
1370 else if (g_str_equal(key, "Strength") == TRUE)
1371 netreg_update_strength(modem, &value);
1372 else if (g_str_equal(key, "Status") == TRUE)
1373 netreg_update_roaming(modem, &value);
1378 static void netreg_properties_reply(struct modem_data *modem,
1379 DBusMessageIter *dict)
1381 DBG("%s", modem->path);
1383 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1384 DBusMessageIter entry, value;
1387 dbus_message_iter_recurse(dict, &entry);
1388 dbus_message_iter_get_basic(&entry, &key);
1390 dbus_message_iter_next(&entry);
1391 dbus_message_iter_recurse(&entry, &value);
1393 if (g_str_equal(key, "Name") == TRUE)
1394 netreg_update_name(modem, &value);
1395 else if (g_str_equal(key, "Strength") == TRUE)
1396 netreg_update_strength(modem, &value);
1397 else if (g_str_equal(key, "Status") == TRUE)
1398 netreg_update_roaming(modem, &value);
1400 dbus_message_iter_next(dict);
1403 if (modem->context == NULL) {
1405 * netgreg_get_properties() was issued after we got
1406 * cm_get_contexts_reply() where we create the
1407 * context. Though before we got the
1408 * netreg_properties_reply the context was removed
1409 * again. Therefore we have to skip the network
1417 if (modem->active == TRUE)
1418 set_connected(modem);
1421 static int netreg_get_properties(struct modem_data *modem)
1423 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1424 netreg_properties_reply, modem);
1427 static int cdma_netreg_get_properties(struct modem_data *modem)
1432 static void cm_update_attached(struct modem_data *modem,
1433 DBusMessageIter *value)
1435 dbus_message_iter_get_basic(value, &modem->attached);
1437 DBG("%s Attached %d", modem->path, modem->attached);
1439 if (modem->attached == FALSE)
1442 if (has_interface(modem->interfaces,
1443 OFONO_API_NETREG) == FALSE) {
1447 netreg_get_properties(modem);
1450 static void cm_update_powered(struct modem_data *modem,
1451 DBusMessageIter *value)
1453 dbus_message_iter_get_basic(value, &modem->cm_powered);
1455 DBG("%s ConnnectionManager Powered %d", modem->path,
1458 if (modem->cm_powered == TRUE)
1461 cm_set_powered(modem);
1464 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1467 const char *path = dbus_message_get_path(message);
1468 struct modem_data *modem;
1469 DBusMessageIter iter, value;
1472 modem = g_hash_table_lookup(modem_hash, path);
1476 if (modem->ignore == TRUE)
1479 if (dbus_message_iter_init(message, &iter) == FALSE)
1482 dbus_message_iter_get_basic(&iter, &key);
1484 dbus_message_iter_next(&iter);
1485 dbus_message_iter_recurse(&iter, &value);
1487 if (g_str_equal(key, "Attached") == TRUE)
1488 cm_update_attached(modem, &value);
1489 else if (g_str_equal(key, "Powered") == TRUE)
1490 cm_update_powered(modem, &value);
1495 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1497 DBG("%s", modem->path);
1499 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1500 DBusMessageIter entry, value;
1503 dbus_message_iter_recurse(dict, &entry);
1504 dbus_message_iter_get_basic(&entry, &key);
1506 dbus_message_iter_next(&entry);
1507 dbus_message_iter_recurse(&entry, &value);
1509 if (g_str_equal(key, "Attached") == TRUE)
1510 cm_update_attached(modem, &value);
1511 else if (g_str_equal(key, "Powered") == TRUE)
1512 cm_update_powered(modem, &value);
1514 dbus_message_iter_next(dict);
1518 static int cm_get_properties(struct modem_data *modem)
1520 return get_properties(modem->path, OFONO_CM_INTERFACE,
1521 cm_properties_reply, modem);
1524 static int cdma_cm_get_properties(struct modem_data *modem)
1529 static void update_sim_imsi(struct modem_data *modem,
1532 DBG("%s imsi %s", modem->path, imsi);
1534 if (g_strcmp0(modem->imsi, imsi) == 0)
1537 g_free(modem->imsi);
1538 modem->imsi = g_strdup(imsi);
1541 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1544 const char *path = dbus_message_get_path(message);
1545 struct modem_data *modem;
1546 DBusMessageIter iter, value;
1549 modem = g_hash_table_lookup(modem_hash, path);
1553 if (modem->ignore == TRUE)
1556 if (dbus_message_iter_init(message, &iter) == FALSE)
1559 dbus_message_iter_get_basic(&iter, &key);
1561 dbus_message_iter_next(&iter);
1562 dbus_message_iter_recurse(&iter, &value);
1564 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1567 dbus_message_iter_get_basic(&value, &imsi);
1569 update_sim_imsi(modem, imsi);
1571 if (modem->online == FALSE) {
1572 modem_set_online(modem);
1573 } else if (has_interface(modem->interfaces,
1574 OFONO_API_CM) == TRUE) {
1575 if (ready_to_create_device(modem) == TRUE)
1576 create_device(modem);
1577 } else if (has_interface(modem->interfaces,
1578 OFONO_API_CDMA_CM) == TRUE) {
1579 if (ready_to_create_device(modem) == TRUE)
1580 create_device(modem);
1587 static void sim_properties_reply(struct modem_data *modem,
1588 DBusMessageIter *dict)
1590 DBG("%s", modem->path);
1592 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1593 DBusMessageIter entry, value;
1596 dbus_message_iter_recurse(dict, &entry);
1597 dbus_message_iter_get_basic(&entry, &key);
1599 dbus_message_iter_next(&entry);
1600 dbus_message_iter_recurse(&entry, &value);
1602 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1605 dbus_message_iter_get_basic(&value, &imsi);
1607 update_sim_imsi(modem, imsi);
1609 if (modem->online == FALSE) {
1610 modem_set_online(modem);
1614 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1615 if (ready_to_create_device(modem) == TRUE)
1616 create_device(modem);
1617 if (modem->device != NULL) {
1618 cm_get_properties(modem);
1619 cm_get_contexts(modem);
1621 } else if (has_interface(modem->interfaces,
1622 OFONO_API_CDMA_CM) == TRUE) {
1623 if (ready_to_create_device(modem) == TRUE)
1624 create_device(modem);
1625 if (modem->device != NULL)
1626 cdma_cm_get_properties(modem);
1631 dbus_message_iter_next(dict);
1635 static int sim_get_properties(struct modem_data *modem)
1637 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1638 sim_properties_reply, modem);
1641 static gboolean modem_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, "Powered") == TRUE) {
1665 dbus_message_iter_get_basic(&value, &modem->powered);
1667 DBG("%s Powered %d", modem->path, modem->powered);
1669 if (modem->powered == FALSE)
1670 modem_set_powered(modem);
1671 } else if (g_str_equal(key, "Online") == TRUE) {
1672 dbus_message_iter_get_basic(&value, &modem->online);
1674 DBG("%s Online %d", modem->path, modem->online);
1676 if (modem->online == FALSE)
1679 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE) {
1680 if (ready_to_create_device(modem) == TRUE)
1681 create_device(modem);
1682 if (modem->device != NULL) {
1683 cm_get_properties(modem);
1684 cm_get_contexts(modem);
1686 } else if (has_interface(modem->interfaces,
1687 OFONO_API_CDMA_CM) == TRUE) {
1688 if (ready_to_create_device(modem) == TRUE)
1689 create_device(modem);
1690 if (modem->device != NULL) {
1691 cdma_cm_get_properties(modem);
1692 cdma_netreg_get_properties(modem);
1695 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1696 modem->interfaces = extract_interfaces(&value);
1698 DBG("%s Interfaces 0x%02x", modem->path,
1701 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1702 if (modem->imsi == NULL &&
1703 modem->set_powered == FALSE) {
1705 * Only use do GetProperties() when
1706 * device has not been powered up.
1708 sim_get_properties(modem);
1713 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1714 if (ready_to_create_device(modem) == TRUE)
1715 create_device(modem);
1716 if (modem->device != NULL) {
1717 cm_get_properties(modem);
1718 cm_get_contexts(modem);
1721 } else if (has_interface(modem->interfaces,
1722 OFONO_API_CDMA_CM) == TRUE) {
1723 if (ready_to_create_device(modem) == TRUE)
1724 create_device(modem);
1725 if (modem->device != NULL)
1726 cdma_cm_get_properties(modem);
1728 if (modem->context != NULL) {
1729 remove_cm_context(modem,
1730 modem->context->path);
1733 if (modem->device != NULL)
1734 destroy_device(modem);
1739 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1740 if (modem->attached == TRUE)
1741 netreg_get_properties(modem);
1742 } else if (has_interface(modem->interfaces,
1743 OFONO_API_CDMA_NETREG) == TRUE)
1744 cdma_netreg_get_properties(modem);
1745 } else if (g_str_equal(key, "Serial") == TRUE) {
1748 dbus_message_iter_get_basic(&value, &serial);
1750 g_free(modem->serial);
1751 modem->serial = g_strdup(serial);
1753 DBG("%s Serial %s", modem->path, modem->serial);
1755 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1756 if (ready_to_create_device(modem) == TRUE)
1757 create_device(modem);
1758 if (modem->device != NULL) {
1759 cm_get_properties(modem);
1760 cm_get_contexts(modem);
1762 } else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM)
1764 if (ready_to_create_device(modem) == TRUE)
1765 create_device(modem);
1766 if (modem->device != NULL)
1767 cdma_cm_get_properties(modem);
1774 static void add_modem(const char *path, DBusMessageIter *prop)
1776 struct modem_data *modem;
1780 modem = g_hash_table_lookup(modem_hash, path);
1781 if (modem != NULL) {
1783 * When oFono powers up we ask for the modems and oFono is
1784 * reporting with modem_added signal the modems. Only
1790 modem = g_try_new0(struct modem_data, 1);
1794 modem->path = g_strdup(path);
1796 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1798 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1799 DBusMessageIter entry, value;
1802 dbus_message_iter_recurse(prop, &entry);
1803 dbus_message_iter_get_basic(&entry, &key);
1805 dbus_message_iter_next(&entry);
1806 dbus_message_iter_recurse(&entry, &value);
1808 if (g_str_equal(key, "Powered") == TRUE) {
1809 dbus_message_iter_get_basic(&value, &modem->powered);
1811 DBG("%s Powered %d", modem->path, modem->powered);
1812 } else if (g_str_equal(key, "Online") == TRUE) {
1813 dbus_message_iter_get_basic(&value, &modem->online);
1815 DBG("%s Online %d", modem->path, modem->online);
1816 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1817 modem->interfaces = extract_interfaces(&value);
1819 DBG("%s Interfaces 0x%02x", modem->path,
1821 } else if (g_str_equal(key, "Serial") == TRUE) {
1824 dbus_message_iter_get_basic(&value, &serial);
1825 modem->serial = g_strdup(serial);
1827 DBG("%s Serial %s", modem->path, modem->serial);
1828 } else if (g_str_equal(key, "Type") == TRUE) {
1831 dbus_message_iter_get_basic(&value, &type);
1833 DBG("%s Type %s", modem->path, type);
1834 if (g_strcmp0(type, "hardware") != 0) {
1835 DBG("%s Ignore this modem", modem->path);
1836 modem->ignore = TRUE;
1840 dbus_message_iter_next(prop);
1843 if (modem->ignore == TRUE)
1846 if (modem->powered == FALSE) {
1847 modem_set_powered(modem);
1848 } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1849 sim_get_properties(modem);
1850 } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1851 if (ready_to_create_device(modem) == TRUE)
1852 create_device(modem);
1853 if (modem->device != NULL) {
1854 cm_get_properties(modem);
1855 cm_get_contexts(modem);
1857 } else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM)
1859 if (ready_to_create_device(modem) == TRUE)
1860 create_device(modem);
1861 if (modem->device != NULL)
1862 cdma_cm_get_properties(modem);
1866 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
1868 struct modem_data *modem = value;
1870 DBG("%s", modem->path);
1872 if (modem->ignore == TRUE)
1875 modem_set_unpowered(modem);
1878 static void remove_modem(gpointer data)
1880 struct modem_data *modem = data;
1882 DBG("%s", modem->path);
1884 if (modem->call_set_property != NULL)
1885 dbus_pending_call_cancel(modem->call_set_property);
1887 if (modem->call_get_properties != NULL)
1888 dbus_pending_call_cancel(modem->call_get_properties);
1890 if (modem->call_get_contexts != NULL)
1891 dbus_pending_call_cancel(modem->call_get_contexts);
1893 if (modem->device != NULL)
1894 destroy_device(modem);
1896 if (modem->context != NULL)
1897 remove_cm_context(modem, modem->context->path);
1899 g_free(modem->serial);
1900 g_free(modem->name);
1901 g_free(modem->imsi);
1902 g_free(modem->path);
1907 static gboolean modem_added(DBusConnection *connection,
1908 DBusMessage *message, void *user_data)
1910 DBusMessageIter iter, properties;
1915 if (dbus_message_iter_init(message, &iter) == FALSE)
1918 dbus_message_iter_get_basic(&iter, &path);
1920 dbus_message_iter_next(&iter);
1921 dbus_message_iter_recurse(&iter, &properties);
1923 add_modem(path, &properties);
1928 static gboolean modem_removed(DBusConnection *connection,
1929 DBusMessage *message, void *user_data)
1931 DBusMessageIter iter;
1936 if (dbus_message_iter_init(message, &iter) == FALSE)
1939 dbus_message_iter_get_basic(&iter, &path);
1941 g_hash_table_remove(modem_hash, path);
1946 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1950 DBusMessageIter array, dict;
1954 reply = dbus_pending_call_steal_reply(call);
1956 dbus_error_init(&error);
1958 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1959 connman_error("%s", error.message);
1960 dbus_error_free(&error);
1964 if (dbus_message_iter_init(reply, &array) == FALSE)
1967 dbus_message_iter_recurse(&array, &dict);
1969 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1970 DBusMessageIter value, properties;
1973 dbus_message_iter_recurse(&dict, &value);
1974 dbus_message_iter_get_basic(&value, &path);
1976 dbus_message_iter_next(&value);
1977 dbus_message_iter_recurse(&value, &properties);
1979 add_modem(path, &properties);
1981 dbus_message_iter_next(&dict);
1985 dbus_message_unref(reply);
1987 dbus_pending_call_unref(call);
1990 static int manager_get_modems(void)
1992 DBusMessage *message;
1993 DBusPendingCall *call;
1997 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1998 OFONO_MANAGER_INTERFACE, GET_MODEMS);
1999 if (message == NULL)
2002 if (dbus_connection_send_with_reply(connection, message,
2003 &call, TIMEOUT) == FALSE) {
2004 connman_error("Failed to call GetModems()");
2005 dbus_message_unref(message);
2010 connman_error("D-Bus connection not available");
2011 dbus_message_unref(message);
2015 dbus_pending_call_set_notify(call, manager_get_modems_reply,
2018 dbus_message_unref(message);
2020 return -EINPROGRESS;
2023 static void ofono_connect(DBusConnection *conn, void *user_data)
2027 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2028 g_free, remove_modem);
2029 if (modem_hash == NULL)
2032 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2034 if (context_hash == NULL) {
2035 g_hash_table_destroy(modem_hash);
2039 manager_get_modems();
2042 static void ofono_disconnect(DBusConnection *conn, void *user_data)
2046 if (modem_hash == NULL || context_hash == NULL)
2049 g_hash_table_destroy(modem_hash);
2052 g_hash_table_destroy(context_hash);
2053 context_hash = NULL;
2056 static int network_probe(struct connman_network *network)
2058 struct modem_data *modem = connman_network_get_data(network);
2060 DBG("%s network %p", modem->path, network);
2065 static void network_remove(struct connman_network *network)
2067 struct modem_data *modem = connman_network_get_data(network);
2069 DBG("%s network %p", modem->path, network);
2072 static int network_connect(struct connman_network *network)
2074 struct modem_data *modem = connman_network_get_data(network);
2076 DBG("%s network %p", modem->path, network);
2078 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2079 return context_set_active(modem);
2080 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2081 return cdma_cm_set_powered(modem);
2083 connman_error("Connection manager interface not available");
2088 static int network_disconnect(struct connman_network *network)
2090 struct modem_data *modem = connman_network_get_data(network);
2092 DBG("%s network %p", modem->path, network);
2094 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE)
2095 return context_set_inactive(modem);
2096 else if (has_interface(modem->interfaces, OFONO_API_CDMA_CM) == TRUE)
2097 return cdma_cm_shutdown(modem);
2099 connman_error("Connection manager interface not available");
2104 static struct connman_network_driver network_driver = {
2106 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
2107 .probe = network_probe,
2108 .remove = network_remove,
2109 .connect = network_connect,
2110 .disconnect = network_disconnect,
2113 static int modem_probe(struct connman_device *device)
2115 struct modem_data *modem = connman_device_get_data(device);
2117 DBG("%s device %p", modem->path, device);
2122 static void modem_remove(struct connman_device *device)
2124 struct modem_data *modem = connman_device_get_data(device);
2126 DBG("%s device %p", modem->path, device);
2129 static int modem_enable(struct connman_device *device)
2131 struct modem_data *modem = connman_device_get_data(device);
2133 DBG("%s device %p", modem->path, device);
2138 static int modem_disable(struct connman_device *device)
2140 struct modem_data *modem = connman_device_get_data(device);
2142 DBG("%s device %p", modem->path, device);
2147 static struct connman_device_driver modem_driver = {
2149 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2150 .probe = modem_probe,
2151 .remove = modem_remove,
2152 .enable = modem_enable,
2153 .disable = modem_disable,
2157 static guint modem_added_watch;
2158 static guint modem_removed_watch;
2159 static guint modem_watch;
2160 static guint cm_watch;
2161 static guint sim_watch;
2162 static guint context_added_watch;
2163 static guint context_removed_watch;
2164 static guint netreg_watch;
2165 static guint context_watch;
2167 static int ofono_init(void)
2173 connection = connman_dbus_get_connection();
2174 if (connection == NULL)
2177 watch = g_dbus_add_service_watch(connection,
2178 OFONO_SERVICE, ofono_connect,
2179 ofono_disconnect, NULL, NULL);
2181 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2182 OFONO_MANAGER_INTERFACE,
2187 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2188 OFONO_MANAGER_INTERFACE,
2193 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2194 OFONO_MODEM_INTERFACE,
2199 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2205 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2206 OFONO_SIM_INTERFACE,
2211 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2217 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2223 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2224 OFONO_CONTEXT_INTERFACE,
2229 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2230 OFONO_NETREG_INTERFACE,
2236 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2237 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2238 context_added_watch == 0 ||
2239 context_removed_watch == 0 ||
2240 context_watch == 0 || netreg_watch == 0) {
2245 err = connman_network_driver_register(&network_driver);
2249 err = connman_device_driver_register(&modem_driver);
2251 connman_network_driver_unregister(&network_driver);
2258 g_dbus_remove_watch(connection, netreg_watch);
2259 g_dbus_remove_watch(connection, context_watch);
2260 g_dbus_remove_watch(connection, context_removed_watch);
2261 g_dbus_remove_watch(connection, context_added_watch);
2262 g_dbus_remove_watch(connection, sim_watch);
2263 g_dbus_remove_watch(connection, cm_watch);
2264 g_dbus_remove_watch(connection, modem_watch);
2265 g_dbus_remove_watch(connection, modem_removed_watch);
2266 g_dbus_remove_watch(connection, modem_added_watch);
2267 g_dbus_remove_watch(connection, watch);
2268 dbus_connection_unref(connection);
2273 static void ofono_exit(void)
2277 if (modem_hash != NULL) {
2279 * We should propably wait for the SetProperty() reply
2280 * message, because ...
2282 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2285 * ... here we will cancel the call.
2287 g_hash_table_destroy(modem_hash);
2291 if (context_hash != NULL) {
2292 g_hash_table_destroy(context_hash);
2293 context_hash = NULL;
2296 connman_device_driver_unregister(&modem_driver);
2297 connman_network_driver_unregister(&network_driver);
2299 g_dbus_remove_watch(connection, netreg_watch);
2300 g_dbus_remove_watch(connection, context_watch);
2301 g_dbus_remove_watch(connection, context_removed_watch);
2302 g_dbus_remove_watch(connection, context_added_watch);
2303 g_dbus_remove_watch(connection, sim_watch);
2304 g_dbus_remove_watch(connection, cm_watch);
2305 g_dbus_remove_watch(connection, modem_watch);
2306 g_dbus_remove_watch(connection, modem_added_watch);
2307 g_dbus_remove_watch(connection, modem_removed_watch);
2308 g_dbus_remove_watch(connection, watch);
2310 dbus_connection_unref(connection);
2313 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2314 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)