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 modem_set_online_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.
526 modem->set_online = FALSE;
529 static int modem_set_online(struct modem_data *modem)
531 DBG("%s", modem->path);
533 modem->set_online = TRUE;
535 return set_property(modem, modem->path,
536 OFONO_MODEM_INTERFACE,
537 "Online", DBUS_TYPE_BOOLEAN,
539 modem_set_online_reply);
542 static void cm_set_powered_reply(struct modem_data *modem,
543 connman_bool_t success)
545 DBG("%s", modem->path);
547 if (success == TRUE) {
549 * Don't handle do anything on success here. oFono will send
550 * the change via PropertyChanged singal.
555 modem->set_cm_powered = FALSE;
558 static int cm_set_powered(struct modem_data *modem)
560 DBG("%s", modem->path);
562 modem->set_cm_powered = TRUE;
564 return set_property(modem, modem->path,
566 "Powered", DBUS_TYPE_BOOLEAN,
567 &modem->set_cm_powered,
568 cm_set_powered_reply);
571 static int modem_set_powered(struct modem_data *modem)
573 DBG("%s", modem->path);
575 modem->set_powered = TRUE;
577 return set_property(modem, modem->path,
578 OFONO_MODEM_INTERFACE,
579 "Powered", DBUS_TYPE_BOOLEAN,
584 static int modem_set_unpowered(struct modem_data *modem)
586 DBG("%s", modem->path);
588 modem->set_powered = FALSE;
590 return set_property(modem, modem->path,
591 OFONO_MODEM_INTERFACE,
592 "Powered", DBUS_TYPE_BOOLEAN,
597 static connman_bool_t has_interface(uint8_t interfaces,
600 if ((interfaces & api) == api)
606 static uint8_t extract_interfaces(DBusMessageIter *array)
608 DBusMessageIter entry;
609 uint8_t interfaces = 0;
611 dbus_message_iter_recurse(array, &entry);
613 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
616 dbus_message_iter_get_basic(&entry, &name);
618 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
619 interfaces |= OFONO_API_SIM;
620 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
621 interfaces |= OFONO_API_NETREG;
622 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
623 interfaces |= OFONO_API_CM;
624 else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE) == TRUE)
625 interfaces |= OFONO_API_CDMA_CM;
626 else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE) == TRUE)
627 interfaces |= OFONO_API_CDMA_NETREG;
629 dbus_message_iter_next(&entry);
635 static char *extract_nameservers(DBusMessageIter *array)
637 DBusMessageIter entry;
638 char *nameservers = NULL;
641 dbus_message_iter_recurse(array, &entry);
643 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
644 const char *nameserver;
646 dbus_message_iter_get_basic(&entry, &nameserver);
648 if (nameservers == NULL) {
649 nameservers = g_strdup(nameserver);
652 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
656 dbus_message_iter_next(&entry);
662 static void extract_ipv4_settings(DBusMessageIter *array,
663 struct network_context *context)
665 DBusMessageIter dict;
666 char *address = NULL, *netmask = NULL, *gateway = NULL;
667 char *nameservers = NULL;
668 const char *interface = NULL;
671 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
674 dbus_message_iter_recurse(array, &dict);
676 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
677 DBusMessageIter entry, value;
678 const char *key, *val;
680 dbus_message_iter_recurse(&dict, &entry);
681 dbus_message_iter_get_basic(&entry, &key);
683 dbus_message_iter_next(&entry);
684 dbus_message_iter_recurse(&entry, &value);
686 if (g_str_equal(key, "Interface") == TRUE) {
687 dbus_message_iter_get_basic(&value, &interface);
689 DBG("Interface %s", interface);
691 index = connman_inet_ifindex(interface);
693 DBG("index %d", index);
694 } else if (g_str_equal(key, "Method") == TRUE) {
695 dbus_message_iter_get_basic(&value, &val);
697 DBG("Method %s", val);
699 if (g_strcmp0(val, "static") == 0) {
700 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
701 } else if (g_strcmp0(val, "dhcp") == 0) {
702 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
705 } else if (g_str_equal(key, "Address") == TRUE) {
706 dbus_message_iter_get_basic(&value, &val);
708 address = g_strdup(val);
710 DBG("Address %s", address);
711 } else if (g_str_equal(key, "Netmask") == TRUE) {
712 dbus_message_iter_get_basic(&value, &val);
714 netmask = g_strdup(val);
716 DBG("Netmask %s", netmask);
717 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
718 nameservers = extract_nameservers(&value);
720 DBG("Nameservers %s", nameservers);
721 } else if (g_str_equal(key, "Gateway") == TRUE) {
722 dbus_message_iter_get_basic(&value, &val);
724 gateway = g_strdup(val);
726 DBG("Gateway %s", gateway);
729 dbus_message_iter_next(&dict);
735 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
738 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
739 if (context->ipv4_address == NULL)
742 context->index = index;
743 connman_ipaddress_set_ipv4(context->ipv4_address, address,
746 context->ipv4_nameservers = nameservers;
749 if (context->ipv4_nameservers != nameservers)
757 static void extract_ipv6_settings(DBusMessageIter *array,
758 struct network_context *context)
760 DBusMessageIter dict;
761 char *address = NULL, *gateway = NULL;
762 unsigned char prefix_length;
763 char *nameservers = NULL;
764 const char *interface = NULL;
767 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
770 dbus_message_iter_recurse(array, &dict);
772 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
773 DBusMessageIter entry, value;
774 const char *key, *val;
776 dbus_message_iter_recurse(&dict, &entry);
777 dbus_message_iter_get_basic(&entry, &key);
779 dbus_message_iter_next(&entry);
780 dbus_message_iter_recurse(&entry, &value);
782 if (g_str_equal(key, "Interface") == TRUE) {
783 dbus_message_iter_get_basic(&value, &interface);
785 DBG("Interface %s", interface);
787 index = connman_inet_ifindex(interface);
789 DBG("index %d", index);
790 } else if (g_str_equal(key, "Address") == TRUE) {
791 dbus_message_iter_get_basic(&value, &val);
793 address = g_strdup(val);
795 DBG("Address %s", address);
796 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
797 dbus_message_iter_get_basic(&value, &prefix_length);
799 DBG("prefix length %d", prefix_length);
800 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
801 nameservers = extract_nameservers(&value);
803 DBG("Nameservers %s", nameservers);
804 } else if (g_str_equal(key, "Gateway") == TRUE) {
805 dbus_message_iter_get_basic(&value, &val);
807 gateway = g_strdup(val);
809 DBG("Gateway %s", gateway);
812 dbus_message_iter_next(&dict);
818 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
820 context->ipv6_address =
821 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
822 if (context->ipv6_address == NULL)
825 context->index = index;
826 connman_ipaddress_set_ipv6(context->ipv6_address, address,
827 prefix_length, gateway);
829 context->ipv6_nameservers = nameservers;
832 if (context->ipv6_nameservers != nameservers)
839 static connman_bool_t ready_to_create_device(struct modem_data *modem)
842 * There are three different modem types which behave slightly
844 * - GSM modems will expose the SIM interface then the
846 * - DUN modems will expose first a unique serial number (BDADDR)
847 * and then the CM interface.
848 * - CDMA modems will expose CM first and sometime later
849 * a unique serial number.
851 * This functions tests if we have the necessary information gathered
852 * before we are able to create a device.
855 if (modem->device != NULL)
858 if (modem->imsi != NULL || modem->serial != NULL)
864 static void create_device(struct modem_data *modem)
866 struct connman_device *device;
867 char *uninitialized_var(ident);
869 DBG("%s", modem->path);
871 if (modem->imsi != NULL)
873 else if (modem->serial != NULL)
874 ident = modem->serial;
876 if (connman_dbus_validate_ident(ident) == FALSE)
877 ident = connman_dbus_encode_string(ident);
879 ident = g_strdup(ident);
881 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
885 DBG("device %p", device);
887 connman_device_set_ident(device, ident);
889 connman_device_set_string(device, "Path", modem->path);
891 connman_device_set_data(device, modem);
893 if (connman_device_register(device) < 0) {
894 connman_error("Failed to register cellular device");
895 connman_device_unref(device);
899 modem->device = device;
905 static void destroy_device(struct modem_data *modem)
907 DBG("%s", modem->path);
909 connman_device_set_powered(modem->device, FALSE);
911 if (modem->network != NULL) {
912 connman_device_remove_network(modem->device, modem->network);
913 connman_network_unref(modem->network);
914 modem->network = NULL;
917 connman_device_unregister(modem->device);
918 connman_device_unref(modem->device);
920 modem->device = NULL;
923 static void add_network(struct modem_data *modem)
927 DBG("%s", modem->path);
929 if (modem->network != NULL)
932 modem->network = connman_network_create(modem->context->path,
933 CONNMAN_NETWORK_TYPE_CELLULAR);
934 if (modem->network == NULL)
937 DBG("network %p", modem->network);
939 connman_network_set_data(modem->network, modem);
941 connman_network_set_string(modem->network, "Path",
942 modem->context->path);
944 connman_network_set_index(modem->network, modem->context->index);
946 if (modem->name != NULL)
947 connman_network_set_name(modem->network, modem->name);
949 connman_network_set_name(modem->network, "");
951 connman_network_set_strength(modem->network, modem->strength);
953 group = get_ident(modem->context->path);
954 connman_network_set_group(modem->network, group);
956 connman_network_set_available(modem->network, TRUE);
958 connman_network_set_bool(modem->network, "Roaming",
961 if (connman_device_add_network(modem->device, modem->network) < 0) {
962 connman_network_unref(modem->network);
963 modem->network = NULL;
968 static int add_cm_context(struct modem_data *modem, const char *context_path,
969 DBusMessageIter *dict)
971 const char *context_type;
972 struct network_context *context = NULL;
973 connman_bool_t active = FALSE;
975 DBG("%s context path %s", modem->path, context_path);
977 if (modem->context != NULL) {
979 * We have already assigned a context to this modem
980 * and we do only support one Internet context.
985 context = network_context_alloc(context_path);
989 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
990 DBusMessageIter entry, value;
993 dbus_message_iter_recurse(dict, &entry);
994 dbus_message_iter_get_basic(&entry, &key);
996 dbus_message_iter_next(&entry);
997 dbus_message_iter_recurse(&entry, &value);
999 if (g_str_equal(key, "Type") == TRUE) {
1000 dbus_message_iter_get_basic(&value, &context_type);
1002 DBG("%s context %s type %s", modem->path,
1003 context_path, context_type);
1004 } else if (g_str_equal(key, "Settings") == TRUE) {
1005 DBG("%s Settings", modem->path);
1007 extract_ipv4_settings(&value, context);
1008 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1009 DBG("%s IPv6.Settings", modem->path);
1011 extract_ipv6_settings(&value, context);
1012 } else if (g_str_equal(key, "Active") == TRUE) {
1013 dbus_message_iter_get_basic(&value, &active);
1015 DBG("%s Active %d", modem->path, active);
1018 dbus_message_iter_next(dict);
1021 if (g_strcmp0(context_type, "internet") != 0) {
1022 network_context_free(context);
1026 modem->context = context;
1027 modem->active = active;
1029 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
1034 static void remove_cm_context(struct modem_data *modem,
1035 const char *context_path)
1037 if (modem->context == NULL)
1040 g_hash_table_remove(context_hash, context_path);
1042 network_context_free(modem->context);
1043 modem->context = NULL;
1046 static gboolean context_changed(DBusConnection *connection,
1047 DBusMessage *message,
1050 const char *context_path = dbus_message_get_path(message);
1051 struct modem_data *modem = NULL;
1052 DBusMessageIter iter, value;
1055 DBG("context_path %s", context_path);
1057 modem = g_hash_table_lookup(context_hash, context_path);
1061 if (dbus_message_iter_init(message, &iter) == FALSE)
1064 dbus_message_iter_get_basic(&iter, &key);
1066 dbus_message_iter_next(&iter);
1067 dbus_message_iter_recurse(&iter, &value);
1070 * oFono guarantees the ordering of Settings and
1071 * Active. Settings will always be send before Active = True.
1072 * That means we don't have to order here.
1074 if (g_str_equal(key, "Settings") == TRUE) {
1075 DBG("%s Settings", modem->path);
1077 extract_ipv4_settings(&value, modem->context);
1078 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
1079 DBG("%s IPv6.Settings", modem->path);
1081 extract_ipv6_settings(&value, modem->context);
1082 } else if (g_str_equal(key, "Active") == TRUE) {
1083 dbus_message_iter_get_basic(&value, &modem->active);
1085 DBG("%s Active %d", modem->path, modem->active);
1087 if (modem->active == TRUE)
1088 set_connected(modem);
1090 set_disconnected(modem);
1096 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
1098 struct modem_data *modem = user_data;
1099 DBusMessageIter array, dict, entry, value;
1103 DBG("%s", modem->path);
1105 modem->call_get_contexts = NULL;
1107 reply = dbus_pending_call_steal_reply(call);
1109 dbus_error_init(&error);
1111 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1112 connman_error("%s", error.message);
1113 dbus_error_free(&error);
1117 if (dbus_message_iter_init(reply, &array) == FALSE)
1120 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1123 dbus_message_iter_recurse(&array, &dict);
1125 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1126 const char *context_path;
1128 dbus_message_iter_recurse(&dict, &entry);
1129 dbus_message_iter_get_basic(&entry, &context_path);
1131 dbus_message_iter_next(&entry);
1132 dbus_message_iter_recurse(&entry, &value);
1134 if (add_cm_context(modem, context_path, &value) == 0)
1137 dbus_message_iter_next(&dict);
1141 dbus_message_unref(reply);
1143 dbus_pending_call_unref(call);
1146 static int cm_get_contexts(struct modem_data *modem)
1148 DBusMessage *message;
1150 DBG("%s", modem->path);
1152 if (modem->call_get_contexts != NULL)
1155 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1156 OFONO_CM_INTERFACE, GET_CONTEXTS);
1157 if (message == NULL)
1160 if (dbus_connection_send_with_reply(connection, message,
1161 &modem->call_get_contexts, TIMEOUT) == FALSE) {
1162 connman_error("Failed to call GetContexts()");
1163 dbus_message_unref(message);
1167 if (modem->call_get_contexts == NULL) {
1168 connman_error("D-Bus connection not available");
1169 dbus_message_unref(message);
1173 dbus_pending_call_set_notify(modem->call_get_contexts,
1174 cm_get_contexts_reply,
1177 dbus_message_unref(message);
1179 return -EINPROGRESS;
1182 static gboolean cm_context_added(DBusConnection *connection,
1183 DBusMessage *message,
1186 const char *path = dbus_message_get_path(message);
1188 struct modem_data *modem;
1189 DBusMessageIter iter, properties;
1193 modem = g_hash_table_lookup(modem_hash, context_path);
1197 if (dbus_message_iter_init(message, &iter) == FALSE)
1200 dbus_message_iter_get_basic(&iter, &context_path);
1202 dbus_message_iter_next(&iter);
1203 dbus_message_iter_recurse(&iter, &properties);
1205 if (add_cm_context(modem, context_path, &properties) != 0)
1211 static gboolean cm_context_removed(DBusConnection *connection,
1212 DBusMessage *message,
1215 const char *path = dbus_message_get_path(message);
1216 const char *context_path;
1217 struct modem_data *modem;
1218 DBusMessageIter iter;
1220 DBG("context path %s", path);
1222 if (dbus_message_iter_init(message, &iter) == FALSE)
1225 dbus_message_iter_get_basic(&iter, &context_path);
1227 modem = g_hash_table_lookup(context_hash, context_path);
1231 remove_cm_context(modem, context_path);
1236 static void netreg_update_name(struct modem_data *modem,
1237 DBusMessageIter* value)
1241 dbus_message_iter_get_basic(value, &name);
1243 DBG("%s Name %s", modem->path, name);
1245 g_free(modem->name);
1246 modem->name = g_strdup(name);
1248 if (modem->network == NULL)
1251 connman_network_set_name(modem->network, modem->name);
1252 connman_network_update(modem->network);
1255 static void netreg_update_strength(struct modem_data *modem,
1256 DBusMessageIter *value)
1258 dbus_message_iter_get_basic(value, &modem->strength);
1260 DBG("%s Strength %d", modem->path, modem->strength);
1262 if (modem->network == NULL)
1265 connman_network_set_strength(modem->network, modem->strength);
1266 connman_network_update(modem->network);
1269 static void netreg_update_roaming(struct modem_data *modem,
1270 DBusMessageIter *value)
1273 connman_bool_t roaming;
1275 dbus_message_iter_get_basic(value, &status);
1277 if (g_str_equal(status, "roaming") == TRUE)
1282 if (roaming == modem->roaming)
1285 modem->roaming = roaming;
1287 if (modem->network == NULL)
1290 connman_network_set_bool(modem->network,
1291 "Roaming", modem->roaming);
1292 connman_network_update(modem->network);
1295 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1298 const char *path = dbus_message_get_path(message);
1299 struct modem_data *modem;
1300 DBusMessageIter iter, value;
1303 modem = g_hash_table_lookup(modem_hash, path);
1307 if (modem->ignore == TRUE)
1310 if (dbus_message_iter_init(message, &iter) == FALSE)
1313 dbus_message_iter_get_basic(&iter, &key);
1315 dbus_message_iter_next(&iter);
1316 dbus_message_iter_recurse(&iter, &value);
1318 if (g_str_equal(key, "Name") == TRUE)
1319 netreg_update_name(modem, &value);
1320 else if (g_str_equal(key, "Strength") == TRUE)
1321 netreg_update_strength(modem, &value);
1322 else if (g_str_equal(key, "Status") == TRUE)
1323 netreg_update_roaming(modem, &value);
1328 static void netreg_properties_reply(struct modem_data *modem,
1329 DBusMessageIter *dict)
1331 DBG("%s", modem->path);
1333 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1334 DBusMessageIter entry, value;
1337 dbus_message_iter_recurse(dict, &entry);
1338 dbus_message_iter_get_basic(&entry, &key);
1340 dbus_message_iter_next(&entry);
1341 dbus_message_iter_recurse(&entry, &value);
1343 if (g_str_equal(key, "Name") == TRUE)
1344 netreg_update_name(modem, &value);
1345 else if (g_str_equal(key, "Strength") == TRUE)
1346 netreg_update_strength(modem, &value);
1347 else if (g_str_equal(key, "Status") == TRUE)
1348 netreg_update_roaming(modem, &value);
1350 dbus_message_iter_next(dict);
1353 if (modem->context == NULL) {
1355 * netgreg_get_properties() was issued after we got
1356 * cm_get_contexts_reply() where we create the
1357 * context. Though before we got the
1358 * netreg_properties_reply the context was removed
1359 * again. Therefore we have to skip the network
1367 if (modem->active == TRUE)
1368 set_connected(modem);
1371 static int netreg_get_properties(struct modem_data *modem)
1373 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1374 netreg_properties_reply, modem);
1377 static void cm_update_attached(struct modem_data *modem,
1378 DBusMessageIter *value)
1380 dbus_message_iter_get_basic(value, &modem->attached);
1382 DBG("%s Attached %d", modem->path, modem->attached);
1384 if (modem->attached == FALSE)
1387 if (has_interface(modem->interfaces,
1388 OFONO_API_NETREG) == FALSE) {
1392 netreg_get_properties(modem);
1395 static void cm_update_powered(struct modem_data *modem,
1396 DBusMessageIter *value)
1398 dbus_message_iter_get_basic(value, &modem->cm_powered);
1400 DBG("%s ConnnectionManager Powered %d", modem->path,
1403 if (modem->cm_powered == TRUE)
1406 cm_set_powered(modem);
1409 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1412 const char *path = dbus_message_get_path(message);
1413 struct modem_data *modem;
1414 DBusMessageIter iter, value;
1417 modem = g_hash_table_lookup(modem_hash, path);
1421 if (modem->ignore == TRUE)
1424 if (dbus_message_iter_init(message, &iter) == FALSE)
1427 dbus_message_iter_get_basic(&iter, &key);
1429 dbus_message_iter_next(&iter);
1430 dbus_message_iter_recurse(&iter, &value);
1432 if (g_str_equal(key, "Attached") == TRUE)
1433 cm_update_attached(modem, &value);
1434 else if (g_str_equal(key, "Powered") == TRUE)
1435 cm_update_powered(modem, &value);
1440 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1442 DBG("%s", modem->path);
1444 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1445 DBusMessageIter entry, value;
1448 dbus_message_iter_recurse(dict, &entry);
1449 dbus_message_iter_get_basic(&entry, &key);
1451 dbus_message_iter_next(&entry);
1452 dbus_message_iter_recurse(&entry, &value);
1454 if (g_str_equal(key, "Attached") == TRUE)
1455 cm_update_attached(modem, &value);
1456 else if (g_str_equal(key, "Powered") == TRUE)
1457 cm_update_powered(modem, &value);
1459 dbus_message_iter_next(dict);
1463 static int cm_get_properties(struct modem_data *modem)
1465 return get_properties(modem->path, OFONO_CM_INTERFACE,
1466 cm_properties_reply, modem);
1469 static void update_sim_imsi(struct modem_data *modem,
1472 DBG("%s imsi %s", modem->path, imsi);
1474 if (g_strcmp0(modem->imsi, imsi) == 0)
1477 g_free(modem->imsi);
1478 modem->imsi = g_strdup(imsi);
1481 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1484 const char *path = dbus_message_get_path(message);
1485 struct modem_data *modem;
1486 DBusMessageIter iter, value;
1489 modem = g_hash_table_lookup(modem_hash, path);
1493 if (modem->ignore == TRUE)
1496 if (dbus_message_iter_init(message, &iter) == FALSE)
1499 dbus_message_iter_get_basic(&iter, &key);
1501 dbus_message_iter_next(&iter);
1502 dbus_message_iter_recurse(&iter, &value);
1504 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1507 dbus_message_iter_get_basic(&value, &imsi);
1509 update_sim_imsi(modem, imsi);
1511 if (modem->online == FALSE) {
1512 modem_set_online(modem);
1513 } else if (has_interface(modem->interfaces,
1514 OFONO_API_CM) == TRUE) {
1515 if (ready_to_create_device(modem) == TRUE)
1516 create_device(modem);
1523 static void sim_properties_reply(struct modem_data *modem,
1524 DBusMessageIter *dict)
1526 DBG("%s", modem->path);
1528 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1529 DBusMessageIter entry, value;
1532 dbus_message_iter_recurse(dict, &entry);
1533 dbus_message_iter_get_basic(&entry, &key);
1535 dbus_message_iter_next(&entry);
1536 dbus_message_iter_recurse(&entry, &value);
1538 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1541 dbus_message_iter_get_basic(&value, &imsi);
1543 update_sim_imsi(modem, imsi);
1545 if (modem->online == FALSE) {
1546 modem_set_online(modem);
1550 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1551 if (ready_to_create_device(modem) == TRUE)
1552 create_device(modem);
1553 if (modem->device != NULL) {
1554 cm_get_properties(modem);
1555 cm_get_contexts(modem);
1561 dbus_message_iter_next(dict);
1565 static int sim_get_properties(struct modem_data *modem)
1567 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1568 sim_properties_reply, modem);
1571 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1574 const char *path = dbus_message_get_path(message);
1575 struct modem_data *modem;
1576 DBusMessageIter iter, value;
1579 modem = g_hash_table_lookup(modem_hash, path);
1583 if (modem->ignore == TRUE)
1586 if (dbus_message_iter_init(message, &iter) == FALSE)
1589 dbus_message_iter_get_basic(&iter, &key);
1591 dbus_message_iter_next(&iter);
1592 dbus_message_iter_recurse(&iter, &value);
1594 if (g_str_equal(key, "Powered") == TRUE) {
1595 dbus_message_iter_get_basic(&value, &modem->powered);
1597 DBG("%s Powered %d", modem->path, modem->powered);
1599 if (modem->powered == FALSE)
1600 modem_set_powered(modem);
1601 } else if (g_str_equal(key, "Online") == TRUE) {
1602 dbus_message_iter_get_basic(&value, &modem->online);
1604 DBG("%s Online %d", modem->path, modem->online);
1606 if (modem->online == FALSE)
1609 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1611 if (ready_to_create_device(modem) == TRUE)
1612 create_device(modem);
1613 if (modem->device != NULL) {
1614 cm_get_properties(modem);
1615 cm_get_contexts(modem);
1617 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1618 modem->interfaces = extract_interfaces(&value);
1620 DBG("%s Interfaces 0x%02x", modem->path,
1623 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1624 if (modem->imsi == NULL &&
1625 modem->set_powered == FALSE) {
1627 * Only use do GetProperties() when
1628 * device has not been powered up.
1630 sim_get_properties(modem);
1635 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1636 if (ready_to_create_device(modem) == TRUE)
1637 create_device(modem);
1638 if (modem->device != NULL) {
1639 cm_get_properties(modem);
1640 cm_get_contexts(modem);
1644 if (modem->context != NULL) {
1645 remove_cm_context(modem,
1646 modem->context->path);
1649 if (modem->device != NULL)
1650 destroy_device(modem);
1655 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1656 if (modem->attached == TRUE)
1657 netreg_get_properties(modem);
1659 } else if (g_str_equal(key, "Serial") == TRUE) {
1662 dbus_message_iter_get_basic(&value, &serial);
1664 g_free(modem->serial);
1665 modem->serial = g_strdup(serial);
1667 DBG("%s Serial %s", modem->path, modem->serial);
1669 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1670 if (ready_to_create_device(modem) == TRUE)
1671 create_device(modem);
1672 if (modem->device != NULL) {
1673 cm_get_properties(modem);
1674 cm_get_contexts(modem);
1682 static void add_modem(const char *path, DBusMessageIter *prop)
1684 struct modem_data *modem;
1688 modem = g_hash_table_lookup(modem_hash, path);
1689 if (modem != NULL) {
1691 * When oFono powers up we ask for the modems and oFono is
1692 * reporting with modem_added signal the modems. Only
1698 modem = g_try_new0(struct modem_data, 1);
1702 modem->path = g_strdup(path);
1704 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1706 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1707 DBusMessageIter entry, value;
1710 dbus_message_iter_recurse(prop, &entry);
1711 dbus_message_iter_get_basic(&entry, &key);
1713 dbus_message_iter_next(&entry);
1714 dbus_message_iter_recurse(&entry, &value);
1716 if (g_str_equal(key, "Powered") == TRUE) {
1717 dbus_message_iter_get_basic(&value, &modem->powered);
1719 DBG("%s Powered %d", modem->path, modem->powered);
1720 } else if (g_str_equal(key, "Online") == TRUE) {
1721 dbus_message_iter_get_basic(&value, &modem->online);
1723 DBG("%s Online %d", modem->path, modem->online);
1724 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1725 modem->interfaces = extract_interfaces(&value);
1727 DBG("%s Interfaces 0x%02x", modem->path,
1729 } else if (g_str_equal(key, "Serial") == TRUE) {
1732 dbus_message_iter_get_basic(&value, &serial);
1733 modem->serial = g_strdup(serial);
1735 DBG("%s Serial %s", modem->path, modem->serial);
1736 } else if (g_str_equal(key, "Type") == TRUE) {
1739 dbus_message_iter_get_basic(&value, &type);
1741 DBG("%s Type %s", modem->path, type);
1742 if (g_strcmp0(type, "hardware") != 0) {
1743 DBG("%s Ignore this modem", modem->path);
1744 modem->ignore = TRUE;
1748 dbus_message_iter_next(prop);
1751 if (modem->ignore == TRUE)
1754 if (modem->powered == FALSE) {
1755 modem_set_powered(modem);
1756 } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1757 sim_get_properties(modem);
1758 } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1759 if (ready_to_create_device(modem) == TRUE)
1760 create_device(modem);
1761 if (modem->device != NULL) {
1762 cm_get_properties(modem);
1763 cm_get_contexts(modem);
1768 static void modem_power_down(gpointer key, gpointer value, gpointer user_data)
1770 struct modem_data *modem = value;
1772 DBG("%s", modem->path);
1774 if (modem->ignore == TRUE)
1777 modem_set_unpowered(modem);
1780 static void remove_modem(gpointer data)
1782 struct modem_data *modem = data;
1784 DBG("%s", modem->path);
1786 if (modem->call_set_property != NULL)
1787 dbus_pending_call_cancel(modem->call_set_property);
1789 if (modem->call_get_properties != NULL)
1790 dbus_pending_call_cancel(modem->call_get_properties);
1792 if (modem->call_get_contexts != NULL)
1793 dbus_pending_call_cancel(modem->call_get_contexts);
1795 if (modem->device != NULL)
1796 destroy_device(modem);
1798 if (modem->context != NULL)
1799 remove_cm_context(modem, modem->context->path);
1801 g_free(modem->serial);
1802 g_free(modem->name);
1803 g_free(modem->imsi);
1804 g_free(modem->path);
1809 static gboolean modem_added(DBusConnection *connection,
1810 DBusMessage *message, void *user_data)
1812 DBusMessageIter iter, properties;
1817 if (dbus_message_iter_init(message, &iter) == FALSE)
1820 dbus_message_iter_get_basic(&iter, &path);
1822 dbus_message_iter_next(&iter);
1823 dbus_message_iter_recurse(&iter, &properties);
1825 add_modem(path, &properties);
1830 static gboolean modem_removed(DBusConnection *connection,
1831 DBusMessage *message, void *user_data)
1833 DBusMessageIter iter;
1838 if (dbus_message_iter_init(message, &iter) == FALSE)
1841 dbus_message_iter_get_basic(&iter, &path);
1843 g_hash_table_remove(modem_hash, path);
1848 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1852 DBusMessageIter array, dict;
1856 reply = dbus_pending_call_steal_reply(call);
1858 dbus_error_init(&error);
1860 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1861 connman_error("%s", error.message);
1862 dbus_error_free(&error);
1866 if (dbus_message_iter_init(reply, &array) == FALSE)
1869 dbus_message_iter_recurse(&array, &dict);
1871 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1872 DBusMessageIter value, properties;
1875 dbus_message_iter_recurse(&dict, &value);
1876 dbus_message_iter_get_basic(&value, &path);
1878 dbus_message_iter_next(&value);
1879 dbus_message_iter_recurse(&value, &properties);
1881 add_modem(path, &properties);
1883 dbus_message_iter_next(&dict);
1887 dbus_message_unref(reply);
1889 dbus_pending_call_unref(call);
1892 static int manager_get_modems(void)
1894 DBusMessage *message;
1895 DBusPendingCall *call;
1899 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1900 OFONO_MANAGER_INTERFACE, GET_MODEMS);
1901 if (message == NULL)
1904 if (dbus_connection_send_with_reply(connection, message,
1905 &call, TIMEOUT) == FALSE) {
1906 connman_error("Failed to call GetModems()");
1907 dbus_message_unref(message);
1912 connman_error("D-Bus connection not available");
1913 dbus_message_unref(message);
1917 dbus_pending_call_set_notify(call, manager_get_modems_reply,
1920 dbus_message_unref(message);
1922 return -EINPROGRESS;
1925 static void ofono_connect(DBusConnection *conn, void *user_data)
1929 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1930 g_free, remove_modem);
1931 if (modem_hash == NULL)
1934 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1936 if (context_hash == NULL) {
1937 g_hash_table_destroy(modem_hash);
1941 manager_get_modems();
1944 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1948 if (modem_hash == NULL || context_hash == NULL)
1951 g_hash_table_destroy(modem_hash);
1954 g_hash_table_destroy(context_hash);
1955 context_hash = NULL;
1958 static int network_probe(struct connman_network *network)
1960 struct modem_data *modem = connman_network_get_data(network);
1962 DBG("%s network %p", modem->path, network);
1967 static void network_remove(struct connman_network *network)
1969 struct modem_data *modem = connman_network_get_data(network);
1971 DBG("%s network %p", modem->path, network);
1974 static int network_connect(struct connman_network *network)
1976 struct modem_data *modem = connman_network_get_data(network);
1978 DBG("%s network %p", modem->path, network);
1980 return context_set_active(modem);
1983 static int network_disconnect(struct connman_network *network)
1985 struct modem_data *modem = connman_network_get_data(network);
1987 DBG("%s network %p", modem->path, network);
1989 return context_set_inactive(modem);
1992 static struct connman_network_driver network_driver = {
1994 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
1995 .probe = network_probe,
1996 .remove = network_remove,
1997 .connect = network_connect,
1998 .disconnect = network_disconnect,
2001 static int modem_probe(struct connman_device *device)
2003 struct modem_data *modem = connman_device_get_data(device);
2005 DBG("%s device %p", modem->path, device);
2010 static void modem_remove(struct connman_device *device)
2012 struct modem_data *modem = connman_device_get_data(device);
2014 DBG("%s device %p", modem->path, device);
2017 static int modem_enable(struct connman_device *device)
2019 struct modem_data *modem = connman_device_get_data(device);
2021 DBG("%s device %p", modem->path, device);
2026 static int modem_disable(struct connman_device *device)
2028 struct modem_data *modem = connman_device_get_data(device);
2030 DBG("%s device %p", modem->path, device);
2035 static struct connman_device_driver modem_driver = {
2037 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
2038 .probe = modem_probe,
2039 .remove = modem_remove,
2040 .enable = modem_enable,
2041 .disable = modem_disable,
2045 static guint modem_added_watch;
2046 static guint modem_removed_watch;
2047 static guint modem_watch;
2048 static guint cm_watch;
2049 static guint sim_watch;
2050 static guint context_added_watch;
2051 static guint context_removed_watch;
2052 static guint netreg_watch;
2053 static guint context_watch;
2055 static int ofono_init(void)
2061 connection = connman_dbus_get_connection();
2062 if (connection == NULL)
2065 watch = g_dbus_add_service_watch(connection,
2066 OFONO_SERVICE, ofono_connect,
2067 ofono_disconnect, NULL, NULL);
2069 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2070 OFONO_MANAGER_INTERFACE,
2075 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2076 OFONO_MANAGER_INTERFACE,
2081 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2082 OFONO_MODEM_INTERFACE,
2087 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2093 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2094 OFONO_SIM_INTERFACE,
2099 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2105 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2111 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2112 OFONO_CONTEXT_INTERFACE,
2117 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
2118 OFONO_NETREG_INTERFACE,
2124 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
2125 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
2126 context_added_watch == 0 ||
2127 context_removed_watch == 0 ||
2128 context_watch == 0 || netreg_watch == 0) {
2133 err = connman_network_driver_register(&network_driver);
2137 err = connman_device_driver_register(&modem_driver);
2139 connman_network_driver_unregister(&network_driver);
2146 g_dbus_remove_watch(connection, netreg_watch);
2147 g_dbus_remove_watch(connection, context_watch);
2148 g_dbus_remove_watch(connection, context_removed_watch);
2149 g_dbus_remove_watch(connection, context_added_watch);
2150 g_dbus_remove_watch(connection, sim_watch);
2151 g_dbus_remove_watch(connection, cm_watch);
2152 g_dbus_remove_watch(connection, modem_watch);
2153 g_dbus_remove_watch(connection, modem_removed_watch);
2154 g_dbus_remove_watch(connection, modem_added_watch);
2155 g_dbus_remove_watch(connection, watch);
2156 dbus_connection_unref(connection);
2161 static void ofono_exit(void)
2165 if (modem_hash != NULL) {
2167 * We should propably wait for the SetProperty() reply
2168 * message, because ...
2170 g_hash_table_foreach(modem_hash, modem_power_down, NULL);
2173 * ... here we will cancel the call.
2175 g_hash_table_destroy(modem_hash);
2179 if (context_hash != NULL) {
2180 g_hash_table_destroy(context_hash);
2181 context_hash = NULL;
2184 connman_device_driver_unregister(&modem_driver);
2185 connman_network_driver_unregister(&network_driver);
2187 g_dbus_remove_watch(connection, netreg_watch);
2188 g_dbus_remove_watch(connection, context_watch);
2189 g_dbus_remove_watch(connection, context_removed_watch);
2190 g_dbus_remove_watch(connection, context_added_watch);
2191 g_dbus_remove_watch(connection, sim_watch);
2192 g_dbus_remove_watch(connection, cm_watch);
2193 g_dbus_remove_watch(connection, modem_watch);
2194 g_dbus_remove_watch(connection, modem_added_watch);
2195 g_dbus_remove_watch(connection, modem_removed_watch);
2196 g_dbus_remove_watch(connection, watch);
2198 dbus_connection_unref(connection);
2201 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
2202 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)