5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/device.h>
37 #include <connman/network.h>
38 #include <connman/inet.h>
39 #include <connman/dbus.h>
40 #include <connman/log.h>
42 #define uninitialized_var(x) x = x
44 #define OFONO_SERVICE "org.ofono"
46 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
47 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
48 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
49 #define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
50 #define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
51 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
53 #define MODEM_ADDED "ModemAdded"
54 #define MODEM_REMOVED "ModemRemoved"
55 #define PROPERTY_CHANGED "PropertyChanged"
56 #define CONTEXT_ADDED "ContextAdded"
57 #define CONTEXT_REMOVED "ContextRemoved"
59 #define GET_PROPERTIES "GetProperties"
60 #define SET_PROPERTY "SetProperty"
61 #define GET_MODEMS "GetModems"
62 #define GET_CONTEXTS "GetContexts"
68 OFONO_API_NETREG = 0x2,
72 static DBusConnection *connection;
74 static GHashTable *modem_hash;
75 static GHashTable *context_hash;
77 struct network_context {
81 enum connman_ipconfig_method ipv4_method;
82 struct connman_ipaddress *ipv4_address;
83 char *ipv4_nameservers;
85 enum connman_ipconfig_method ipv6_method;
86 struct connman_ipaddress *ipv6_address;
87 char *ipv6_nameservers;
93 struct connman_device *device;
95 struct network_context *context;
99 connman_bool_t powered;
100 connman_bool_t online;
103 connman_bool_t set_powered;
104 connman_bool_t set_online;
106 /* ConnectionManager Interface */
107 connman_bool_t attached;
108 connman_bool_t cm_powered;
110 connman_bool_t set_cm_powered;
112 /* ConnectionContext Interface */
113 connman_bool_t active;
115 /* SimManager Interface */
119 DBusPendingCall *call_set_property;
120 DBusPendingCall *call_get_properties;
121 DBusPendingCall *call_get_contexts;
124 static struct network_context *network_context_alloc(const char *path)
126 struct network_context *context;
128 context = g_try_new0(struct network_context, 1);
132 context->path = g_strdup(path);
135 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
136 context->ipv4_address = NULL;
137 context->ipv4_nameservers = NULL;
139 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
140 context->ipv6_address = NULL;
141 context->ipv6_nameservers = NULL;
146 static void network_context_free(struct network_context *context)
148 g_free(context->path);
150 connman_ipaddress_free(context->ipv4_address);
151 g_free(context->ipv4_nameservers);
153 connman_ipaddress_free(context->ipv6_address);
154 g_free(context->ipv6_nameservers);
159 typedef void (*set_property_cb)(struct modem_data *data,
160 connman_bool_t success);
161 typedef void (*get_properties_cb)(struct modem_data *data,
162 DBusMessageIter *dict);
164 struct property_info {
165 struct modem_data *modem;
167 const char *interface;
168 const char *property;
169 set_property_cb set_property_cb;
170 get_properties_cb get_properties_cb;
173 static void set_property_reply(DBusPendingCall *call, void *user_data)
175 struct property_info *info = user_data;
178 connman_bool_t success = TRUE;
180 DBG("%s path %s %s.%s", info->modem->path,
181 info->path, info->interface, info->property);
183 info->modem->call_set_property = NULL;
185 dbus_error_init(&error);
187 reply = dbus_pending_call_steal_reply(call);
189 if (dbus_set_error_from_message(&error, reply)) {
190 connman_error("Failed to change property: %s %s.%s: %s %s",
191 info->path, info->interface, info->property,
192 error.name, error.message);
193 dbus_error_free(&error);
197 if (info->set_property_cb != NULL)
198 (*info->set_property_cb)(info->modem, success);
200 dbus_message_unref(reply);
202 dbus_pending_call_unref(call);
205 static int set_property(struct modem_data *modem,
206 const char *path, const char *interface,
207 const char *property, int type, void *value,
208 set_property_cb notify)
210 DBusMessage *message;
211 DBusMessageIter iter;
212 struct property_info *info;
214 DBG("%s path %s %s.%s", modem->path, path, interface, property);
216 if (modem->call_set_property != NULL) {
217 connman_error("Pending SetProperty");
221 message = dbus_message_new_method_call(OFONO_SERVICE, path,
222 interface, SET_PROPERTY);
226 dbus_message_iter_init_append(message, &iter);
227 connman_dbus_property_append_basic(&iter, property, type, value);
229 if (dbus_connection_send_with_reply(connection, message,
230 &modem->call_set_property, TIMEOUT) == FALSE) {
231 connman_error("Failed to change property: %s %s.%s",
232 path, interface, property);
233 dbus_message_unref(message);
237 if (modem->call_set_property == NULL) {
238 connman_error("D-Bus connection not available");
239 dbus_message_unref(message);
243 info = g_try_new0(struct property_info, 1);
245 dbus_message_unref(message);
251 info->interface = interface;
252 info->property = property;
253 info->set_property_cb = notify;
255 dbus_pending_call_set_notify(modem->call_set_property,
256 set_property_reply, info, g_free);
258 dbus_message_unref(message);
263 static void get_properties_reply(DBusPendingCall *call, void *user_data)
265 struct property_info *info = user_data;
266 DBusMessageIter array, dict;
270 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
272 info->modem->call_get_properties = NULL;
274 dbus_error_init(&error);
276 reply = dbus_pending_call_steal_reply(call);
278 if (dbus_set_error_from_message(&error, reply)) {
279 connman_error("Failed to get properties: %s %s: %s %s",
280 info->path, info->interface,
281 error.name, error.message);
282 dbus_error_free(&error);
287 if (dbus_message_iter_init(reply, &array) == FALSE)
290 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
293 dbus_message_iter_recurse(&array, &dict);
295 if (info->get_properties_cb != NULL)
296 (*info->get_properties_cb)(info->modem, &dict);
300 dbus_message_unref(reply);
302 dbus_pending_call_unref(call);
305 static int get_properties(const char *path, const char *interface,
306 get_properties_cb notify,
307 struct modem_data *modem)
309 DBusMessage *message;
310 struct property_info *info;
312 DBG("%s path %s %s", modem->path, path, interface);
314 if (modem->call_get_properties != NULL) {
315 connman_error("Pending GetProperties");
319 message = dbus_message_new_method_call(OFONO_SERVICE, path,
320 interface, GET_PROPERTIES);
324 if (dbus_connection_send_with_reply(connection, message,
325 &modem->call_get_properties, TIMEOUT) == FALSE) {
326 connman_error("Failed to call %s.GetProperties()", interface);
327 dbus_message_unref(message);
331 if (modem->call_get_properties == NULL) {
332 connman_error("D-Bus connection not available");
333 dbus_message_unref(message);
337 info = g_try_new0(struct property_info, 1);
339 dbus_message_unref(message);
345 info->interface = interface;
346 info->get_properties_cb = notify;
348 dbus_pending_call_set_notify(modem->call_get_properties,
349 get_properties_reply, info, g_free);
351 dbus_message_unref(message);
356 static void modem_set_online_reply(struct modem_data *modem,
357 connman_bool_t success)
359 DBG("%s", modem->path);
361 if (success == TRUE) {
363 * Don't handle do anything on success here. oFono will send
364 * the change via PropertyChanged singal.
369 modem->set_online = FALSE;
372 static int modem_set_online(struct modem_data *modem)
374 DBG("%s", modem->path);
376 modem->set_online = TRUE;
378 return set_property(modem, modem->path,
379 OFONO_MODEM_INTERFACE,
380 "Online", DBUS_TYPE_BOOLEAN,
382 modem_set_online_reply);
385 static void cm_set_powered_reply(struct modem_data *modem,
386 connman_bool_t success)
388 DBG("%s", modem->path);
390 if (success == TRUE) {
392 * Don't handle do anything on success here. oFono will send
393 * the change via PropertyChanged singal.
398 modem->set_cm_powered = FALSE;
401 static int cm_set_powered(struct modem_data *modem)
403 DBG("%s", modem->path);
405 modem->set_cm_powered = TRUE;
407 return set_property(modem, modem->path,
409 "Powered", DBUS_TYPE_BOOLEAN,
410 &modem->set_cm_powered,
411 cm_set_powered_reply);
414 static int modem_set_powered(struct modem_data *modem)
416 DBG("%s", modem->path);
418 modem->set_powered = TRUE;
420 return set_property(modem, modem->path,
421 OFONO_MODEM_INTERFACE,
422 "Powered", DBUS_TYPE_BOOLEAN,
427 static connman_bool_t has_interface(uint8_t interfaces,
430 if ((interfaces & api) == api)
436 static uint8_t extract_interfaces(DBusMessageIter *array)
438 DBusMessageIter entry;
439 uint8_t interfaces = 0;
441 dbus_message_iter_recurse(array, &entry);
443 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
446 dbus_message_iter_get_basic(&entry, &name);
448 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
449 interfaces |= OFONO_API_SIM;
450 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
451 interfaces |= OFONO_API_NETREG;
452 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
453 interfaces |= OFONO_API_CM;
455 dbus_message_iter_next(&entry);
461 static char *extract_nameservers(DBusMessageIter *array)
463 DBusMessageIter entry;
464 char *nameservers = NULL;
467 dbus_message_iter_recurse(array, &entry);
469 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
470 const char *nameserver;
472 dbus_message_iter_get_basic(&entry, &nameserver);
474 if (nameservers == NULL) {
475 nameservers = g_strdup(nameserver);
478 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
482 dbus_message_iter_next(&entry);
488 static void extract_ipv4_settings(DBusMessageIter *array,
489 struct network_context *context)
491 DBusMessageIter dict;
492 char *address = NULL, *netmask = NULL, *gateway = NULL;
493 char *nameservers = NULL;
494 const char *interface = NULL;
497 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
500 dbus_message_iter_recurse(array, &dict);
502 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
503 DBusMessageIter entry, value;
504 const char *key, *val;
506 dbus_message_iter_recurse(&dict, &entry);
507 dbus_message_iter_get_basic(&entry, &key);
509 dbus_message_iter_next(&entry);
510 dbus_message_iter_recurse(&entry, &value);
512 if (g_str_equal(key, "Interface") == TRUE) {
513 dbus_message_iter_get_basic(&value, &interface);
515 DBG("Interface %s", interface);
517 index = connman_inet_ifindex(interface);
519 DBG("index %d", index);
520 } else if (g_str_equal(key, "Method") == TRUE) {
521 dbus_message_iter_get_basic(&value, &val);
523 DBG("Method %s", val);
525 if (g_strcmp0(val, "static") == 0) {
526 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
527 } else if (g_strcmp0(val, "dhcp") == 0) {
528 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
531 } else if (g_str_equal(key, "Address") == TRUE) {
532 dbus_message_iter_get_basic(&value, &val);
534 address = g_strdup(val);
536 DBG("Address %s", address);
537 } else if (g_str_equal(key, "Netmask") == TRUE) {
538 dbus_message_iter_get_basic(&value, &val);
540 netmask = g_strdup(val);
542 DBG("Netmask %s", netmask);
543 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
544 nameservers = extract_nameservers(&value);
546 DBG("Nameservers %s", nameservers);
547 } else if (g_str_equal(key, "Gateway") == TRUE) {
548 dbus_message_iter_get_basic(&value, &val);
550 gateway = g_strdup(val);
552 DBG("Gateway %s", gateway);
555 dbus_message_iter_next(&dict);
561 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
564 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
565 if (context->ipv4_address == NULL)
568 context->index = index;
569 connman_ipaddress_set_ipv4(context->ipv4_address, address,
572 context->ipv4_nameservers = nameservers;
575 if (context->ipv4_nameservers != nameservers)
583 static void extract_ipv6_settings(DBusMessageIter *array,
584 struct network_context *context)
586 DBusMessageIter dict;
587 char *address = NULL, *gateway = NULL;
588 unsigned char prefix_length;
589 char *nameservers = NULL;
590 const char *interface = NULL;
593 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
596 dbus_message_iter_recurse(array, &dict);
598 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
599 DBusMessageIter entry, value;
600 const char *key, *val;
602 dbus_message_iter_recurse(&dict, &entry);
603 dbus_message_iter_get_basic(&entry, &key);
605 dbus_message_iter_next(&entry);
606 dbus_message_iter_recurse(&entry, &value);
608 if (g_str_equal(key, "Interface") == TRUE) {
609 dbus_message_iter_get_basic(&value, &interface);
611 DBG("Interface %s", interface);
613 index = connman_inet_ifindex(interface);
615 DBG("index %d", index);
616 } else if (g_str_equal(key, "Address") == TRUE) {
617 dbus_message_iter_get_basic(&value, &val);
619 address = g_strdup(val);
621 DBG("Address %s", address);
622 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
623 dbus_message_iter_get_basic(&value, &prefix_length);
625 DBG("prefix length %d", prefix_length);
626 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
627 nameservers = extract_nameservers(&value);
629 DBG("Nameservers %s", nameservers);
630 } else if (g_str_equal(key, "Gateway") == TRUE) {
631 dbus_message_iter_get_basic(&value, &val);
633 gateway = g_strdup(val);
635 DBG("Gateway %s", gateway);
638 dbus_message_iter_next(&dict);
644 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
646 context->ipv6_address =
647 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
648 if (context->ipv6_address == NULL)
651 context->index = index;
652 connman_ipaddress_set_ipv6(context->ipv6_address, address,
653 prefix_length, gateway);
655 context->ipv6_nameservers = nameservers;
658 if (context->ipv6_nameservers != nameservers)
665 static connman_bool_t ready_to_create_device(struct modem_data *modem)
668 * There are three different modem types which behave slightly
670 * - GSM modems will expose the SIM interface then the
672 * - DUN modems will expose first a unique serial number (BDADDR)
673 * and then the CM interface.
674 * - CDMA modems will expose CM first and sometime later
675 * a unique serial number.
677 * This functions tests if we have the necessary information gathered
678 * before we are able to create a device.
681 if (modem->device != NULL)
684 if (modem->imsi != NULL || modem->serial != NULL)
690 static void create_device(struct modem_data *modem)
692 struct connman_device *device;
693 char *uninitialized_var(ident);
695 DBG("%s", modem->path);
697 if (modem->imsi != NULL)
699 else if (modem->serial != NULL)
700 ident = modem->serial;
702 if (connman_dbus_validate_ident(ident) == FALSE)
703 ident = connman_dbus_encode_string(ident);
705 ident = g_strdup(ident);
707 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
711 DBG("device %p", device);
713 connman_device_set_ident(device, ident);
715 connman_device_set_string(device, "Path", modem->path);
717 connman_device_set_data(device, modem);
719 if (connman_device_register(device) < 0) {
720 connman_error("Failed to register cellular device");
721 connman_device_unref(device);
725 modem->device = device;
731 static void destroy_device(struct modem_data *modem)
733 DBG("%s", modem->path);
735 connman_device_set_powered(modem->device, FALSE);
737 connman_device_unregister(modem->device);
738 connman_device_unref(modem->device);
740 modem->device = NULL;
743 static int add_cm_context(struct modem_data *modem, const char *context_path,
744 DBusMessageIter *dict)
746 const char *context_type;
747 struct network_context *context = NULL;
748 connman_bool_t active = FALSE;
750 DBG("%s context path %s", modem->path, context_path);
752 if (modem->context != NULL) {
754 * We have already assigned a context to this modem
755 * and we do only support one Internet context.
760 context = network_context_alloc(context_path);
764 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
765 DBusMessageIter entry, value;
768 dbus_message_iter_recurse(dict, &entry);
769 dbus_message_iter_get_basic(&entry, &key);
771 dbus_message_iter_next(&entry);
772 dbus_message_iter_recurse(&entry, &value);
774 if (g_str_equal(key, "Type") == TRUE) {
775 dbus_message_iter_get_basic(&value, &context_type);
777 DBG("%s context %s type %s", modem->path,
778 context_path, context_type);
779 } else if (g_str_equal(key, "Settings") == TRUE) {
780 DBG("%s Settings", modem->path);
782 extract_ipv4_settings(&value, context);
783 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
784 DBG("%s IPv6.Settings", modem->path);
786 extract_ipv6_settings(&value, context);
787 } else if (g_str_equal(key, "Active") == TRUE) {
788 dbus_message_iter_get_basic(&value, &active);
790 DBG("%s Active %d", modem->path, active);
793 dbus_message_iter_next(dict);
796 if (g_strcmp0(context_type, "internet") != 0) {
797 network_context_free(context);
801 modem->context = context;
802 modem->active = active;
804 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
809 static void remove_cm_context(struct modem_data *modem,
810 const char *context_path)
812 if (modem->context == NULL)
815 g_hash_table_remove(context_hash, context_path);
817 network_context_free(modem->context);
818 modem->context = NULL;
821 static gboolean context_changed(DBusConnection *connection,
822 DBusMessage *message,
825 const char *context_path = dbus_message_get_path(message);
826 struct modem_data *modem = NULL;
827 DBusMessageIter iter, value;
830 DBG("context_path %s", context_path);
832 modem = g_hash_table_lookup(context_hash, context_path);
836 if (dbus_message_iter_init(message, &iter) == FALSE)
839 dbus_message_iter_get_basic(&iter, &key);
841 dbus_message_iter_next(&iter);
842 dbus_message_iter_recurse(&iter, &value);
845 * oFono guarantees the ordering of Settings and
846 * Active. Settings will always be send before Active = True.
847 * That means we don't have to order here.
849 if (g_str_equal(key, "Settings") == TRUE) {
850 DBG("%s Settings", modem->path);
852 extract_ipv4_settings(&value, modem->context);
853 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
854 DBG("%s IPv6.Settings", modem->path);
856 extract_ipv6_settings(&value, modem->context);
857 } else if (g_str_equal(key, "Active") == TRUE) {
858 dbus_message_iter_get_basic(&value, &modem->active);
860 DBG("%s Active %d", modem->path, modem->active);
866 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
868 struct modem_data *modem = user_data;
869 DBusMessageIter array, dict, entry, value;
873 DBG("%s", modem->path);
875 modem->call_get_contexts = NULL;
877 reply = dbus_pending_call_steal_reply(call);
879 dbus_error_init(&error);
881 if (dbus_set_error_from_message(&error, reply) == TRUE) {
882 connman_error("%s", error.message);
883 dbus_error_free(&error);
887 if (dbus_message_iter_init(reply, &array) == FALSE)
890 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
893 dbus_message_iter_recurse(&array, &dict);
895 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
896 const char *context_path;
898 dbus_message_iter_recurse(&dict, &entry);
899 dbus_message_iter_get_basic(&entry, &context_path);
901 dbus_message_iter_next(&entry);
902 dbus_message_iter_recurse(&entry, &value);
904 if (add_cm_context(modem, context_path, &value) == 0)
907 dbus_message_iter_next(&dict);
911 dbus_message_unref(reply);
913 dbus_pending_call_unref(call);
916 static int cm_get_contexts(struct modem_data *modem)
918 DBusMessage *message;
920 DBG("%s", modem->path);
922 if (modem->call_get_contexts != NULL)
925 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
926 OFONO_CM_INTERFACE, GET_CONTEXTS);
930 if (dbus_connection_send_with_reply(connection, message,
931 &modem->call_get_contexts, TIMEOUT) == FALSE) {
932 connman_error("Failed to call GetContexts()");
933 dbus_message_unref(message);
937 if (modem->call_get_contexts == NULL) {
938 connman_error("D-Bus connection not available");
939 dbus_message_unref(message);
943 dbus_pending_call_set_notify(modem->call_get_contexts,
944 cm_get_contexts_reply,
947 dbus_message_unref(message);
952 static gboolean cm_context_added(DBusConnection *connection,
953 DBusMessage *message,
956 const char *path = dbus_message_get_path(message);
958 struct modem_data *modem;
959 DBusMessageIter iter, properties;
963 modem = g_hash_table_lookup(modem_hash, context_path);
967 if (dbus_message_iter_init(message, &iter) == FALSE)
970 dbus_message_iter_get_basic(&iter, &context_path);
972 dbus_message_iter_next(&iter);
973 dbus_message_iter_recurse(&iter, &properties);
975 if (add_cm_context(modem, context_path, &properties) != 0)
981 static gboolean cm_context_removed(DBusConnection *connection,
982 DBusMessage *message,
985 const char *path = dbus_message_get_path(message);
986 const char *context_path;
987 struct modem_data *modem;
988 DBusMessageIter iter;
990 DBG("context path %s", path);
992 if (dbus_message_iter_init(message, &iter) == FALSE)
995 dbus_message_iter_get_basic(&iter, &context_path);
997 modem = g_hash_table_lookup(context_hash, context_path);
1001 remove_cm_context(modem, context_path);
1006 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1012 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1015 const char *path = dbus_message_get_path(message);
1016 struct modem_data *modem;
1017 DBusMessageIter iter, value;
1020 modem = g_hash_table_lookup(modem_hash, path);
1024 if (dbus_message_iter_init(message, &iter) == FALSE)
1027 dbus_message_iter_get_basic(&iter, &key);
1029 dbus_message_iter_next(&iter);
1030 dbus_message_iter_recurse(&iter, &value);
1032 if (g_str_equal(key, "Attached") == TRUE) {
1033 dbus_message_iter_get_basic(&value, &modem->attached);
1035 DBG("%s Attached %d", modem->path, modem->attached);
1036 } else if (g_str_equal(key, "Powered") == TRUE) {
1037 dbus_message_iter_get_basic(&value, &modem->cm_powered);
1039 DBG("%s ConnnectionManager Powered %d", modem->path,
1042 if (modem->cm_powered == FALSE)
1043 cm_set_powered(modem);
1049 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1051 DBG("%s", modem->path);
1053 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1054 DBusMessageIter entry, value;
1057 dbus_message_iter_recurse(dict, &entry);
1058 dbus_message_iter_get_basic(&entry, &key);
1060 dbus_message_iter_next(&entry);
1061 dbus_message_iter_recurse(&entry, &value);
1063 if (g_str_equal(key, "Attached") == TRUE) {
1064 dbus_message_iter_get_basic(&value, &modem->attached);
1066 DBG("%s Attached %d", modem->path,
1068 } else if (g_str_equal(key, "Powered") == TRUE) {
1069 dbus_message_iter_get_basic(&value, &modem->cm_powered);
1071 DBG("%s ConnnectionManager Powered %d", modem->path,
1074 if (modem->cm_powered == FALSE)
1075 cm_set_powered(modem);
1078 dbus_message_iter_next(dict);
1082 static int cm_get_properties(struct modem_data *modem)
1084 return get_properties(modem->path, OFONO_CM_INTERFACE,
1085 cm_properties_reply, modem);
1088 static void update_sim_imsi(struct modem_data *modem,
1091 DBG("%s imsi %s", modem->path, imsi);
1093 if (g_strcmp0(modem->imsi, imsi) == 0)
1096 g_free(modem->imsi);
1097 modem->imsi = g_strdup(imsi);
1100 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1103 const char *path = dbus_message_get_path(message);
1104 struct modem_data *modem;
1105 DBusMessageIter iter, value;
1108 modem = g_hash_table_lookup(modem_hash, path);
1112 if (dbus_message_iter_init(message, &iter) == FALSE)
1115 dbus_message_iter_get_basic(&iter, &key);
1117 dbus_message_iter_next(&iter);
1118 dbus_message_iter_recurse(&iter, &value);
1120 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1123 dbus_message_iter_get_basic(&value, &imsi);
1125 update_sim_imsi(modem, imsi);
1127 if (modem->online == FALSE) {
1128 modem_set_online(modem);
1129 } else if (has_interface(modem->interfaces,
1130 OFONO_API_CM) == TRUE) {
1131 if (ready_to_create_device(modem) == TRUE)
1132 create_device(modem);
1139 static void sim_properties_reply(struct modem_data *modem,
1140 DBusMessageIter *dict)
1142 DBG("%s", modem->path);
1144 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1145 DBusMessageIter entry, value;
1148 dbus_message_iter_recurse(dict, &entry);
1149 dbus_message_iter_get_basic(&entry, &key);
1151 dbus_message_iter_next(&entry);
1152 dbus_message_iter_recurse(&entry, &value);
1154 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1157 dbus_message_iter_get_basic(&value, &imsi);
1159 update_sim_imsi(modem, imsi);
1161 if (modem->online == FALSE) {
1162 modem_set_online(modem);
1166 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1167 if (ready_to_create_device(modem) == TRUE)
1168 create_device(modem);
1169 if (modem->device != NULL) {
1170 cm_get_properties(modem);
1171 cm_get_contexts(modem);
1177 dbus_message_iter_next(dict);
1181 static int sim_get_properties(struct modem_data *modem)
1183 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1184 sim_properties_reply, modem);
1187 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1190 const char *path = dbus_message_get_path(message);
1191 struct modem_data *modem;
1192 DBusMessageIter iter, value;
1195 modem = g_hash_table_lookup(modem_hash, path);
1199 if (dbus_message_iter_init(message, &iter) == FALSE)
1202 dbus_message_iter_get_basic(&iter, &key);
1204 dbus_message_iter_next(&iter);
1205 dbus_message_iter_recurse(&iter, &value);
1207 if (g_str_equal(key, "Powered") == TRUE) {
1208 dbus_message_iter_get_basic(&value, &modem->powered);
1210 DBG("%s Powered %d", modem->path, modem->powered);
1212 if (modem->powered == FALSE)
1213 modem_set_powered(modem);
1214 } else if (g_str_equal(key, "Online") == TRUE) {
1215 dbus_message_iter_get_basic(&value, &modem->online);
1217 DBG("%s Online %d", modem->path, modem->online);
1219 if (modem->online == FALSE)
1222 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1224 if (ready_to_create_device(modem) == TRUE)
1225 create_device(modem);
1226 if (modem->device != NULL) {
1227 cm_get_properties(modem);
1228 cm_get_contexts(modem);
1230 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1231 modem->interfaces = extract_interfaces(&value);
1233 DBG("%s Interfaces 0x%02x", modem->path,
1236 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1237 if (modem->imsi == NULL &&
1238 modem->set_powered == FALSE) {
1240 * Only use do GetProperties() when
1241 * device has not been powered up.
1243 sim_get_properties(modem);
1248 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1249 if (ready_to_create_device(modem) == TRUE)
1250 create_device(modem);
1251 if (modem->device != NULL) {
1252 cm_get_properties(modem);
1253 cm_get_contexts(modem);
1256 if (modem->context != NULL) {
1257 remove_cm_context(modem,
1258 modem->context->path);
1261 if (modem->device != NULL)
1262 destroy_device(modem);
1264 } else if (g_str_equal(key, "Serial") == TRUE) {
1267 dbus_message_iter_get_basic(&value, &serial);
1269 g_free(modem->serial);
1270 modem->serial = g_strdup(serial);
1272 DBG("%s Serial %s", modem->path, modem->serial);
1274 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1275 if (ready_to_create_device(modem) == TRUE)
1276 create_device(modem);
1277 if (modem->device != NULL) {
1278 cm_get_properties(modem);
1279 cm_get_contexts(modem);
1287 static void add_modem(const char *path, DBusMessageIter *prop)
1289 struct modem_data *modem;
1293 modem = g_hash_table_lookup(modem_hash, path);
1294 if (modem != NULL) {
1296 * When oFono powers up we ask for the modems and oFono is
1297 * reporting with modem_added signal the modems. Only
1303 modem = g_try_new0(struct modem_data, 1);
1307 modem->path = g_strdup(path);
1309 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1311 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1312 DBusMessageIter entry, value;
1315 dbus_message_iter_recurse(prop, &entry);
1316 dbus_message_iter_get_basic(&entry, &key);
1318 dbus_message_iter_next(&entry);
1319 dbus_message_iter_recurse(&entry, &value);
1321 if (g_str_equal(key, "Powered") == TRUE) {
1322 dbus_message_iter_get_basic(&value, &modem->powered);
1324 DBG("%s Powered %d", modem->path, modem->powered);
1325 } else if (g_str_equal(key, "Online") == TRUE) {
1326 dbus_message_iter_get_basic(&value, &modem->online);
1328 DBG("%s Online %d", modem->path, modem->online);
1329 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1330 modem->interfaces = extract_interfaces(&value);
1332 DBG("%s Interfaces 0x%02x", modem->path,
1334 } else if (g_str_equal(key, "Serial") == TRUE) {
1337 dbus_message_iter_get_basic(&value, &serial);
1338 modem->serial = g_strdup(serial);
1340 DBG("%s Serial %s", modem->path, modem->serial);
1343 dbus_message_iter_next(prop);
1346 if (modem->powered == FALSE) {
1347 modem_set_powered(modem);
1348 } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1349 sim_get_properties(modem);
1350 } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1351 if (ready_to_create_device(modem) == TRUE)
1352 create_device(modem);
1353 if (modem->device != NULL) {
1354 cm_get_properties(modem);
1355 cm_get_contexts(modem);
1360 static void remove_modem(gpointer data)
1362 struct modem_data *modem = data;
1364 DBG("%s", modem->path);
1366 if (modem->call_set_property != NULL)
1367 dbus_pending_call_cancel(modem->call_set_property);
1369 if (modem->call_get_properties != NULL)
1370 dbus_pending_call_cancel(modem->call_get_properties);
1372 if (modem->call_get_contexts != NULL)
1373 dbus_pending_call_cancel(modem->call_get_contexts);
1375 if (modem->device != NULL)
1376 destroy_device(modem);
1378 if (modem->context != NULL)
1379 remove_cm_context(modem, modem->context->path);
1381 g_free(modem->serial);
1382 g_free(modem->imsi);
1383 g_free(modem->path);
1388 static gboolean modem_added(DBusConnection *connection,
1389 DBusMessage *message, void *user_data)
1391 DBusMessageIter iter, properties;
1396 if (dbus_message_iter_init(message, &iter) == FALSE)
1399 dbus_message_iter_get_basic(&iter, &path);
1401 dbus_message_iter_next(&iter);
1402 dbus_message_iter_recurse(&iter, &properties);
1404 add_modem(path, &properties);
1409 static gboolean modem_removed(DBusConnection *connection,
1410 DBusMessage *message, void *user_data)
1412 DBusMessageIter iter;
1417 if (dbus_message_iter_init(message, &iter) == FALSE)
1420 dbus_message_iter_get_basic(&iter, &path);
1422 g_hash_table_remove(modem_hash, path);
1427 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1431 DBusMessageIter array, dict;
1435 reply = dbus_pending_call_steal_reply(call);
1437 dbus_error_init(&error);
1439 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1440 connman_error("%s", error.message);
1441 dbus_error_free(&error);
1445 if (dbus_message_iter_init(reply, &array) == FALSE)
1448 dbus_message_iter_recurse(&array, &dict);
1450 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1451 DBusMessageIter value, properties;
1454 dbus_message_iter_recurse(&dict, &value);
1455 dbus_message_iter_get_basic(&value, &path);
1457 dbus_message_iter_next(&value);
1458 dbus_message_iter_recurse(&value, &properties);
1460 add_modem(path, &properties);
1462 dbus_message_iter_next(&dict);
1466 dbus_message_unref(reply);
1468 dbus_pending_call_unref(call);
1471 static int manager_get_modems(void)
1473 DBusMessage *message;
1474 DBusPendingCall *call;
1478 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1479 OFONO_MANAGER_INTERFACE, GET_MODEMS);
1480 if (message == NULL)
1483 if (dbus_connection_send_with_reply(connection, message,
1484 &call, TIMEOUT) == FALSE) {
1485 connman_error("Failed to call GetModems()");
1486 dbus_message_unref(message);
1491 connman_error("D-Bus connection not available");
1492 dbus_message_unref(message);
1496 dbus_pending_call_set_notify(call, manager_get_modems_reply,
1499 dbus_message_unref(message);
1501 return -EINPROGRESS;
1504 static void ofono_connect(DBusConnection *conn, void *user_data)
1508 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1509 g_free, remove_modem);
1510 if (modem_hash == NULL)
1513 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1515 if (context_hash == NULL) {
1516 g_hash_table_destroy(modem_hash);
1520 manager_get_modems();
1523 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1527 if (modem_hash == NULL || context_hash == NULL)
1530 g_hash_table_destroy(modem_hash);
1533 g_hash_table_destroy(context_hash);
1534 context_hash = NULL;
1537 static int network_probe(struct connman_network *network)
1539 DBG("network %p", network);
1544 static void network_remove(struct connman_network *network)
1546 DBG("network %p", network);
1549 static int network_connect(struct connman_network *network)
1551 DBG("network %p", network);
1556 static int network_disconnect(struct connman_network *network)
1558 DBG("network %p", network);
1563 static struct connman_network_driver network_driver = {
1565 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
1566 .probe = network_probe,
1567 .remove = network_remove,
1568 .connect = network_connect,
1569 .disconnect = network_disconnect,
1572 static int modem_probe(struct connman_device *device)
1574 struct modem_data *modem = connman_device_get_data(device);
1576 DBG("%s device %p", modem->path, device);
1581 static void modem_remove(struct connman_device *device)
1583 struct modem_data *modem = connman_device_get_data(device);
1585 DBG("%s device %p", modem->path, device);
1588 static int modem_enable(struct connman_device *device)
1590 struct modem_data *modem = connman_device_get_data(device);
1592 DBG("%s device %p", modem->path, device);
1597 static int modem_disable(struct connman_device *device)
1599 struct modem_data *modem = connman_device_get_data(device);
1601 DBG("%s device %p", modem->path, device);
1606 static struct connman_device_driver modem_driver = {
1608 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
1609 .probe = modem_probe,
1610 .remove = modem_remove,
1611 .enable = modem_enable,
1612 .disable = modem_disable,
1616 static guint modem_added_watch;
1617 static guint modem_removed_watch;
1618 static guint modem_watch;
1619 static guint cm_watch;
1620 static guint sim_watch;
1621 static guint context_added_watch;
1622 static guint context_removed_watch;
1623 static guint netreg_watch;
1624 static guint context_watch;
1626 static int ofono_init(void)
1632 connection = connman_dbus_get_connection();
1633 if (connection == NULL)
1636 watch = g_dbus_add_service_watch(connection,
1637 OFONO_SERVICE, ofono_connect,
1638 ofono_disconnect, NULL, NULL);
1640 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1641 OFONO_MANAGER_INTERFACE,
1646 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1647 OFONO_MANAGER_INTERFACE,
1652 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1653 OFONO_MODEM_INTERFACE,
1658 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1664 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1665 OFONO_SIM_INTERFACE,
1670 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1676 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1682 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1683 OFONO_CONTEXT_INTERFACE,
1688 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1689 OFONO_NETREG_INTERFACE,
1695 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
1696 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
1697 context_added_watch == 0 ||
1698 context_removed_watch == 0 ||
1699 context_watch == 0 || netreg_watch == 0) {
1704 err = connman_network_driver_register(&network_driver);
1708 err = connman_device_driver_register(&modem_driver);
1710 connman_network_driver_unregister(&network_driver);
1717 g_dbus_remove_watch(connection, netreg_watch);
1718 g_dbus_remove_watch(connection, context_watch);
1719 g_dbus_remove_watch(connection, context_removed_watch);
1720 g_dbus_remove_watch(connection, context_added_watch);
1721 g_dbus_remove_watch(connection, sim_watch);
1722 g_dbus_remove_watch(connection, cm_watch);
1723 g_dbus_remove_watch(connection, modem_watch);
1724 g_dbus_remove_watch(connection, modem_removed_watch);
1725 g_dbus_remove_watch(connection, modem_added_watch);
1726 g_dbus_remove_watch(connection, watch);
1727 dbus_connection_unref(connection);
1732 static void ofono_exit(void)
1736 if (modem_hash != NULL) {
1737 g_hash_table_destroy(modem_hash);
1741 if (context_hash != NULL) {
1742 g_hash_table_destroy(context_hash);
1743 context_hash = NULL;
1746 connman_device_driver_unregister(&modem_driver);
1747 connman_network_driver_unregister(&network_driver);
1749 g_dbus_remove_watch(connection, netreg_watch);
1750 g_dbus_remove_watch(connection, context_watch);
1751 g_dbus_remove_watch(connection, context_removed_watch);
1752 g_dbus_remove_watch(connection, context_added_watch);
1753 g_dbus_remove_watch(connection, sim_watch);
1754 g_dbus_remove_watch(connection, cm_watch);
1755 g_dbus_remove_watch(connection, modem_watch);
1756 g_dbus_remove_watch(connection, modem_added_watch);
1757 g_dbus_remove_watch(connection, modem_removed_watch);
1758 g_dbus_remove_watch(connection, watch);
1760 dbus_connection_unref(connection);
1763 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1764 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)