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 */
118 /* Netreg Interface */
123 DBusPendingCall *call_set_property;
124 DBusPendingCall *call_get_properties;
125 DBusPendingCall *call_get_contexts;
128 static struct network_context *network_context_alloc(const char *path)
130 struct network_context *context;
132 context = g_try_new0(struct network_context, 1);
136 context->path = g_strdup(path);
139 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
140 context->ipv4_address = NULL;
141 context->ipv4_nameservers = NULL;
143 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
144 context->ipv6_address = NULL;
145 context->ipv6_nameservers = NULL;
150 static void network_context_free(struct network_context *context)
152 g_free(context->path);
154 connman_ipaddress_free(context->ipv4_address);
155 g_free(context->ipv4_nameservers);
157 connman_ipaddress_free(context->ipv6_address);
158 g_free(context->ipv6_nameservers);
163 typedef void (*set_property_cb)(struct modem_data *data,
164 connman_bool_t success);
165 typedef void (*get_properties_cb)(struct modem_data *data,
166 DBusMessageIter *dict);
168 struct property_info {
169 struct modem_data *modem;
171 const char *interface;
172 const char *property;
173 set_property_cb set_property_cb;
174 get_properties_cb get_properties_cb;
177 static void set_property_reply(DBusPendingCall *call, void *user_data)
179 struct property_info *info = user_data;
182 connman_bool_t success = TRUE;
184 DBG("%s path %s %s.%s", info->modem->path,
185 info->path, info->interface, info->property);
187 info->modem->call_set_property = NULL;
189 dbus_error_init(&error);
191 reply = dbus_pending_call_steal_reply(call);
193 if (dbus_set_error_from_message(&error, reply)) {
194 connman_error("Failed to change property: %s %s.%s: %s %s",
195 info->path, info->interface, info->property,
196 error.name, error.message);
197 dbus_error_free(&error);
201 if (info->set_property_cb != NULL)
202 (*info->set_property_cb)(info->modem, success);
204 dbus_message_unref(reply);
206 dbus_pending_call_unref(call);
209 static int set_property(struct modem_data *modem,
210 const char *path, const char *interface,
211 const char *property, int type, void *value,
212 set_property_cb notify)
214 DBusMessage *message;
215 DBusMessageIter iter;
216 struct property_info *info;
218 DBG("%s path %s %s.%s", modem->path, path, interface, property);
220 if (modem->call_set_property != NULL) {
221 connman_error("Pending SetProperty");
225 message = dbus_message_new_method_call(OFONO_SERVICE, path,
226 interface, SET_PROPERTY);
230 dbus_message_iter_init_append(message, &iter);
231 connman_dbus_property_append_basic(&iter, property, type, value);
233 if (dbus_connection_send_with_reply(connection, message,
234 &modem->call_set_property, TIMEOUT) == FALSE) {
235 connman_error("Failed to change property: %s %s.%s",
236 path, interface, property);
237 dbus_message_unref(message);
241 if (modem->call_set_property == NULL) {
242 connman_error("D-Bus connection not available");
243 dbus_message_unref(message);
247 info = g_try_new0(struct property_info, 1);
249 dbus_message_unref(message);
255 info->interface = interface;
256 info->property = property;
257 info->set_property_cb = notify;
259 dbus_pending_call_set_notify(modem->call_set_property,
260 set_property_reply, info, g_free);
262 dbus_message_unref(message);
267 static void get_properties_reply(DBusPendingCall *call, void *user_data)
269 struct property_info *info = user_data;
270 DBusMessageIter array, dict;
274 DBG("%s path %s %s", info->modem->path, info->path, info->interface);
276 info->modem->call_get_properties = NULL;
278 dbus_error_init(&error);
280 reply = dbus_pending_call_steal_reply(call);
282 if (dbus_set_error_from_message(&error, reply)) {
283 connman_error("Failed to get properties: %s %s: %s %s",
284 info->path, info->interface,
285 error.name, error.message);
286 dbus_error_free(&error);
291 if (dbus_message_iter_init(reply, &array) == FALSE)
294 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
297 dbus_message_iter_recurse(&array, &dict);
299 if (info->get_properties_cb != NULL)
300 (*info->get_properties_cb)(info->modem, &dict);
304 dbus_message_unref(reply);
306 dbus_pending_call_unref(call);
309 static int get_properties(const char *path, const char *interface,
310 get_properties_cb notify,
311 struct modem_data *modem)
313 DBusMessage *message;
314 struct property_info *info;
316 DBG("%s path %s %s", modem->path, path, interface);
318 if (modem->call_get_properties != NULL) {
319 connman_error("Pending GetProperties");
323 message = dbus_message_new_method_call(OFONO_SERVICE, path,
324 interface, GET_PROPERTIES);
328 if (dbus_connection_send_with_reply(connection, message,
329 &modem->call_get_properties, TIMEOUT) == FALSE) {
330 connman_error("Failed to call %s.GetProperties()", interface);
331 dbus_message_unref(message);
335 if (modem->call_get_properties == NULL) {
336 connman_error("D-Bus connection not available");
337 dbus_message_unref(message);
341 info = g_try_new0(struct property_info, 1);
343 dbus_message_unref(message);
349 info->interface = interface;
350 info->get_properties_cb = notify;
352 dbus_pending_call_set_notify(modem->call_get_properties,
353 get_properties_reply, info, g_free);
355 dbus_message_unref(message);
360 static void modem_set_online_reply(struct modem_data *modem,
361 connman_bool_t success)
363 DBG("%s", modem->path);
365 if (success == TRUE) {
367 * Don't handle do anything on success here. oFono will send
368 * the change via PropertyChanged singal.
373 modem->set_online = FALSE;
376 static int modem_set_online(struct modem_data *modem)
378 DBG("%s", modem->path);
380 modem->set_online = TRUE;
382 return set_property(modem, modem->path,
383 OFONO_MODEM_INTERFACE,
384 "Online", DBUS_TYPE_BOOLEAN,
386 modem_set_online_reply);
389 static void cm_set_powered_reply(struct modem_data *modem,
390 connman_bool_t success)
392 DBG("%s", modem->path);
394 if (success == TRUE) {
396 * Don't handle do anything on success here. oFono will send
397 * the change via PropertyChanged singal.
402 modem->set_cm_powered = FALSE;
405 static int cm_set_powered(struct modem_data *modem)
407 DBG("%s", modem->path);
409 modem->set_cm_powered = TRUE;
411 return set_property(modem, modem->path,
413 "Powered", DBUS_TYPE_BOOLEAN,
414 &modem->set_cm_powered,
415 cm_set_powered_reply);
418 static int modem_set_powered(struct modem_data *modem)
420 DBG("%s", modem->path);
422 modem->set_powered = TRUE;
424 return set_property(modem, modem->path,
425 OFONO_MODEM_INTERFACE,
426 "Powered", DBUS_TYPE_BOOLEAN,
431 static connman_bool_t has_interface(uint8_t interfaces,
434 if ((interfaces & api) == api)
440 static uint8_t extract_interfaces(DBusMessageIter *array)
442 DBusMessageIter entry;
443 uint8_t interfaces = 0;
445 dbus_message_iter_recurse(array, &entry);
447 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
450 dbus_message_iter_get_basic(&entry, &name);
452 if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
453 interfaces |= OFONO_API_SIM;
454 else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
455 interfaces |= OFONO_API_NETREG;
456 else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
457 interfaces |= OFONO_API_CM;
459 dbus_message_iter_next(&entry);
465 static char *extract_nameservers(DBusMessageIter *array)
467 DBusMessageIter entry;
468 char *nameservers = NULL;
471 dbus_message_iter_recurse(array, &entry);
473 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
474 const char *nameserver;
476 dbus_message_iter_get_basic(&entry, &nameserver);
478 if (nameservers == NULL) {
479 nameservers = g_strdup(nameserver);
482 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
486 dbus_message_iter_next(&entry);
492 static void extract_ipv4_settings(DBusMessageIter *array,
493 struct network_context *context)
495 DBusMessageIter dict;
496 char *address = NULL, *netmask = NULL, *gateway = NULL;
497 char *nameservers = NULL;
498 const char *interface = NULL;
501 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
504 dbus_message_iter_recurse(array, &dict);
506 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
507 DBusMessageIter entry, value;
508 const char *key, *val;
510 dbus_message_iter_recurse(&dict, &entry);
511 dbus_message_iter_get_basic(&entry, &key);
513 dbus_message_iter_next(&entry);
514 dbus_message_iter_recurse(&entry, &value);
516 if (g_str_equal(key, "Interface") == TRUE) {
517 dbus_message_iter_get_basic(&value, &interface);
519 DBG("Interface %s", interface);
521 index = connman_inet_ifindex(interface);
523 DBG("index %d", index);
524 } else if (g_str_equal(key, "Method") == TRUE) {
525 dbus_message_iter_get_basic(&value, &val);
527 DBG("Method %s", val);
529 if (g_strcmp0(val, "static") == 0) {
530 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
531 } else if (g_strcmp0(val, "dhcp") == 0) {
532 context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
535 } else if (g_str_equal(key, "Address") == TRUE) {
536 dbus_message_iter_get_basic(&value, &val);
538 address = g_strdup(val);
540 DBG("Address %s", address);
541 } else if (g_str_equal(key, "Netmask") == TRUE) {
542 dbus_message_iter_get_basic(&value, &val);
544 netmask = g_strdup(val);
546 DBG("Netmask %s", netmask);
547 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
548 nameservers = extract_nameservers(&value);
550 DBG("Nameservers %s", nameservers);
551 } else if (g_str_equal(key, "Gateway") == TRUE) {
552 dbus_message_iter_get_basic(&value, &val);
554 gateway = g_strdup(val);
556 DBG("Gateway %s", gateway);
559 dbus_message_iter_next(&dict);
565 if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
568 context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
569 if (context->ipv4_address == NULL)
572 context->index = index;
573 connman_ipaddress_set_ipv4(context->ipv4_address, address,
576 context->ipv4_nameservers = nameservers;
579 if (context->ipv4_nameservers != nameservers)
587 static void extract_ipv6_settings(DBusMessageIter *array,
588 struct network_context *context)
590 DBusMessageIter dict;
591 char *address = NULL, *gateway = NULL;
592 unsigned char prefix_length;
593 char *nameservers = NULL;
594 const char *interface = NULL;
597 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
600 dbus_message_iter_recurse(array, &dict);
602 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
603 DBusMessageIter entry, value;
604 const char *key, *val;
606 dbus_message_iter_recurse(&dict, &entry);
607 dbus_message_iter_get_basic(&entry, &key);
609 dbus_message_iter_next(&entry);
610 dbus_message_iter_recurse(&entry, &value);
612 if (g_str_equal(key, "Interface") == TRUE) {
613 dbus_message_iter_get_basic(&value, &interface);
615 DBG("Interface %s", interface);
617 index = connman_inet_ifindex(interface);
619 DBG("index %d", index);
620 } else if (g_str_equal(key, "Address") == TRUE) {
621 dbus_message_iter_get_basic(&value, &val);
623 address = g_strdup(val);
625 DBG("Address %s", address);
626 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
627 dbus_message_iter_get_basic(&value, &prefix_length);
629 DBG("prefix length %d", prefix_length);
630 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
631 nameservers = extract_nameservers(&value);
633 DBG("Nameservers %s", nameservers);
634 } else if (g_str_equal(key, "Gateway") == TRUE) {
635 dbus_message_iter_get_basic(&value, &val);
637 gateway = g_strdup(val);
639 DBG("Gateway %s", gateway);
642 dbus_message_iter_next(&dict);
648 context->ipv6_method = CONNMAN_IPCONFIG_METHOD_FIXED;
650 context->ipv6_address =
651 connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
652 if (context->ipv6_address == NULL)
655 context->index = index;
656 connman_ipaddress_set_ipv6(context->ipv6_address, address,
657 prefix_length, gateway);
659 context->ipv6_nameservers = nameservers;
662 if (context->ipv6_nameservers != nameservers)
669 static connman_bool_t ready_to_create_device(struct modem_data *modem)
672 * There are three different modem types which behave slightly
674 * - GSM modems will expose the SIM interface then the
676 * - DUN modems will expose first a unique serial number (BDADDR)
677 * and then the CM interface.
678 * - CDMA modems will expose CM first and sometime later
679 * a unique serial number.
681 * This functions tests if we have the necessary information gathered
682 * before we are able to create a device.
685 if (modem->device != NULL)
688 if (modem->imsi != NULL || modem->serial != NULL)
694 static void create_device(struct modem_data *modem)
696 struct connman_device *device;
697 char *uninitialized_var(ident);
699 DBG("%s", modem->path);
701 if (modem->imsi != NULL)
703 else if (modem->serial != NULL)
704 ident = modem->serial;
706 if (connman_dbus_validate_ident(ident) == FALSE)
707 ident = connman_dbus_encode_string(ident);
709 ident = g_strdup(ident);
711 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
715 DBG("device %p", device);
717 connman_device_set_ident(device, ident);
719 connman_device_set_string(device, "Path", modem->path);
721 connman_device_set_data(device, modem);
723 if (connman_device_register(device) < 0) {
724 connman_error("Failed to register cellular device");
725 connman_device_unref(device);
729 modem->device = device;
735 static void destroy_device(struct modem_data *modem)
737 DBG("%s", modem->path);
739 connman_device_set_powered(modem->device, FALSE);
741 connman_device_unregister(modem->device);
742 connman_device_unref(modem->device);
744 modem->device = NULL;
747 static int add_cm_context(struct modem_data *modem, const char *context_path,
748 DBusMessageIter *dict)
750 const char *context_type;
751 struct network_context *context = NULL;
752 connman_bool_t active = FALSE;
754 DBG("%s context path %s", modem->path, context_path);
756 if (modem->context != NULL) {
758 * We have already assigned a context to this modem
759 * and we do only support one Internet context.
764 context = network_context_alloc(context_path);
768 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
769 DBusMessageIter entry, value;
772 dbus_message_iter_recurse(dict, &entry);
773 dbus_message_iter_get_basic(&entry, &key);
775 dbus_message_iter_next(&entry);
776 dbus_message_iter_recurse(&entry, &value);
778 if (g_str_equal(key, "Type") == TRUE) {
779 dbus_message_iter_get_basic(&value, &context_type);
781 DBG("%s context %s type %s", modem->path,
782 context_path, context_type);
783 } else if (g_str_equal(key, "Settings") == TRUE) {
784 DBG("%s Settings", modem->path);
786 extract_ipv4_settings(&value, context);
787 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
788 DBG("%s IPv6.Settings", modem->path);
790 extract_ipv6_settings(&value, context);
791 } else if (g_str_equal(key, "Active") == TRUE) {
792 dbus_message_iter_get_basic(&value, &active);
794 DBG("%s Active %d", modem->path, active);
797 dbus_message_iter_next(dict);
800 if (g_strcmp0(context_type, "internet") != 0) {
801 network_context_free(context);
805 modem->context = context;
806 modem->active = active;
808 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
813 static void remove_cm_context(struct modem_data *modem,
814 const char *context_path)
816 if (modem->context == NULL)
819 g_hash_table_remove(context_hash, context_path);
821 network_context_free(modem->context);
822 modem->context = NULL;
825 static gboolean context_changed(DBusConnection *connection,
826 DBusMessage *message,
829 const char *context_path = dbus_message_get_path(message);
830 struct modem_data *modem = NULL;
831 DBusMessageIter iter, value;
834 DBG("context_path %s", context_path);
836 modem = g_hash_table_lookup(context_hash, context_path);
840 if (dbus_message_iter_init(message, &iter) == FALSE)
843 dbus_message_iter_get_basic(&iter, &key);
845 dbus_message_iter_next(&iter);
846 dbus_message_iter_recurse(&iter, &value);
849 * oFono guarantees the ordering of Settings and
850 * Active. Settings will always be send before Active = True.
851 * That means we don't have to order here.
853 if (g_str_equal(key, "Settings") == TRUE) {
854 DBG("%s Settings", modem->path);
856 extract_ipv4_settings(&value, modem->context);
857 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
858 DBG("%s IPv6.Settings", modem->path);
860 extract_ipv6_settings(&value, modem->context);
861 } else if (g_str_equal(key, "Active") == TRUE) {
862 dbus_message_iter_get_basic(&value, &modem->active);
864 DBG("%s Active %d", modem->path, modem->active);
870 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
872 struct modem_data *modem = user_data;
873 DBusMessageIter array, dict, entry, value;
877 DBG("%s", modem->path);
879 modem->call_get_contexts = NULL;
881 reply = dbus_pending_call_steal_reply(call);
883 dbus_error_init(&error);
885 if (dbus_set_error_from_message(&error, reply) == TRUE) {
886 connman_error("%s", error.message);
887 dbus_error_free(&error);
891 if (dbus_message_iter_init(reply, &array) == FALSE)
894 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
897 dbus_message_iter_recurse(&array, &dict);
899 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
900 const char *context_path;
902 dbus_message_iter_recurse(&dict, &entry);
903 dbus_message_iter_get_basic(&entry, &context_path);
905 dbus_message_iter_next(&entry);
906 dbus_message_iter_recurse(&entry, &value);
908 if (add_cm_context(modem, context_path, &value) == 0)
911 dbus_message_iter_next(&dict);
915 dbus_message_unref(reply);
917 dbus_pending_call_unref(call);
920 static int cm_get_contexts(struct modem_data *modem)
922 DBusMessage *message;
924 DBG("%s", modem->path);
926 if (modem->call_get_contexts != NULL)
929 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
930 OFONO_CM_INTERFACE, GET_CONTEXTS);
934 if (dbus_connection_send_with_reply(connection, message,
935 &modem->call_get_contexts, TIMEOUT) == FALSE) {
936 connman_error("Failed to call GetContexts()");
937 dbus_message_unref(message);
941 if (modem->call_get_contexts == NULL) {
942 connman_error("D-Bus connection not available");
943 dbus_message_unref(message);
947 dbus_pending_call_set_notify(modem->call_get_contexts,
948 cm_get_contexts_reply,
951 dbus_message_unref(message);
956 static gboolean cm_context_added(DBusConnection *connection,
957 DBusMessage *message,
960 const char *path = dbus_message_get_path(message);
962 struct modem_data *modem;
963 DBusMessageIter iter, properties;
967 modem = g_hash_table_lookup(modem_hash, context_path);
971 if (dbus_message_iter_init(message, &iter) == FALSE)
974 dbus_message_iter_get_basic(&iter, &context_path);
976 dbus_message_iter_next(&iter);
977 dbus_message_iter_recurse(&iter, &properties);
979 if (add_cm_context(modem, context_path, &properties) != 0)
985 static gboolean cm_context_removed(DBusConnection *connection,
986 DBusMessage *message,
989 const char *path = dbus_message_get_path(message);
990 const char *context_path;
991 struct modem_data *modem;
992 DBusMessageIter iter;
994 DBG("context path %s", path);
996 if (dbus_message_iter_init(message, &iter) == FALSE)
999 dbus_message_iter_get_basic(&iter, &context_path);
1001 modem = g_hash_table_lookup(context_hash, context_path);
1005 remove_cm_context(modem, context_path);
1010 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
1013 const char *path = dbus_message_get_path(message);
1014 struct modem_data *modem;
1015 DBusMessageIter iter, value;
1018 modem = g_hash_table_lookup(modem_hash, path);
1022 if (dbus_message_iter_init(message, &iter) == FALSE)
1025 dbus_message_iter_get_basic(&iter, &key);
1027 dbus_message_iter_next(&iter);
1028 dbus_message_iter_recurse(&iter, &value);
1030 if (g_str_equal(key, "Name") == TRUE) {
1033 dbus_message_iter_get_basic(&value, &name);
1035 DBG("%s Name %s", modem->path, name);
1037 g_free(modem->name);
1038 modem->name = g_strdup(name);
1039 } else if (g_str_equal(key, "Strength") == TRUE) {
1040 dbus_message_iter_get_basic(&value, &modem->strength);
1042 DBG("%s Strength %d", modem->path, modem->strength);
1048 static void netreg_properties_reply(struct modem_data *modem,
1049 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, "Name") == TRUE) {
1066 dbus_message_iter_get_basic(&value, &name);
1068 DBG("%s Name %s", modem->path, name);
1070 g_free(modem->name);
1071 modem->name = g_strdup(name);
1072 } else if (g_str_equal(key, "Strength") == TRUE) {
1073 dbus_message_iter_get_basic(&value, &modem->strength);
1075 DBG("%s Strength %d", modem->path,
1079 dbus_message_iter_next(dict);
1082 if (modem->context == NULL) {
1084 * netgreg_get_properties() was issued after we got
1085 * cm_get_contexts_reply() where we create the
1086 * context. Though before we got the
1087 * netreg_properties_reply the context was removed
1088 * again. Therefore we have to skip the network
1095 static int netreg_get_properties(struct modem_data *modem)
1097 return get_properties(modem->path, OFONO_NETREG_INTERFACE,
1098 netreg_properties_reply, modem);
1101 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
1104 const char *path = dbus_message_get_path(message);
1105 struct modem_data *modem;
1106 DBusMessageIter iter, value;
1109 modem = g_hash_table_lookup(modem_hash, path);
1113 if (dbus_message_iter_init(message, &iter) == FALSE)
1116 dbus_message_iter_get_basic(&iter, &key);
1118 dbus_message_iter_next(&iter);
1119 dbus_message_iter_recurse(&iter, &value);
1121 if (g_str_equal(key, "Attached") == TRUE) {
1122 dbus_message_iter_get_basic(&value, &modem->attached);
1124 DBG("%s Attached %d", modem->path, modem->attached);
1126 if (modem->attached == TRUE) {
1127 if (has_interface(modem->interfaces,
1128 OFONO_API_NETREG) == TRUE) {
1129 netreg_get_properties(modem);
1132 } else if (g_str_equal(key, "Powered") == TRUE) {
1133 dbus_message_iter_get_basic(&value, &modem->cm_powered);
1135 DBG("%s ConnnectionManager Powered %d", modem->path,
1138 if (modem->cm_powered == FALSE)
1139 cm_set_powered(modem);
1145 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
1147 DBG("%s", modem->path);
1149 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1150 DBusMessageIter entry, value;
1153 dbus_message_iter_recurse(dict, &entry);
1154 dbus_message_iter_get_basic(&entry, &key);
1156 dbus_message_iter_next(&entry);
1157 dbus_message_iter_recurse(&entry, &value);
1159 if (g_str_equal(key, "Attached") == TRUE) {
1160 dbus_message_iter_get_basic(&value, &modem->attached);
1162 DBG("%s Attached %d", modem->path,
1165 if (modem->attached == TRUE) {
1166 if (has_interface(modem->interfaces,
1167 OFONO_API_NETREG) == TRUE) {
1168 netreg_get_properties(modem);
1171 } else if (g_str_equal(key, "Powered") == TRUE) {
1172 dbus_message_iter_get_basic(&value, &modem->cm_powered);
1174 DBG("%s ConnnectionManager Powered %d", modem->path,
1177 if (modem->cm_powered == FALSE)
1178 cm_set_powered(modem);
1181 dbus_message_iter_next(dict);
1185 static int cm_get_properties(struct modem_data *modem)
1187 return get_properties(modem->path, OFONO_CM_INTERFACE,
1188 cm_properties_reply, modem);
1191 static void update_sim_imsi(struct modem_data *modem,
1194 DBG("%s imsi %s", modem->path, imsi);
1196 if (g_strcmp0(modem->imsi, imsi) == 0)
1199 g_free(modem->imsi);
1200 modem->imsi = g_strdup(imsi);
1203 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1206 const char *path = dbus_message_get_path(message);
1207 struct modem_data *modem;
1208 DBusMessageIter iter, value;
1211 modem = g_hash_table_lookup(modem_hash, path);
1215 if (dbus_message_iter_init(message, &iter) == FALSE)
1218 dbus_message_iter_get_basic(&iter, &key);
1220 dbus_message_iter_next(&iter);
1221 dbus_message_iter_recurse(&iter, &value);
1223 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1226 dbus_message_iter_get_basic(&value, &imsi);
1228 update_sim_imsi(modem, imsi);
1230 if (modem->online == FALSE) {
1231 modem_set_online(modem);
1232 } else if (has_interface(modem->interfaces,
1233 OFONO_API_CM) == TRUE) {
1234 if (ready_to_create_device(modem) == TRUE)
1235 create_device(modem);
1242 static void sim_properties_reply(struct modem_data *modem,
1243 DBusMessageIter *dict)
1245 DBG("%s", modem->path);
1247 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
1248 DBusMessageIter entry, value;
1251 dbus_message_iter_recurse(dict, &entry);
1252 dbus_message_iter_get_basic(&entry, &key);
1254 dbus_message_iter_next(&entry);
1255 dbus_message_iter_recurse(&entry, &value);
1257 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1260 dbus_message_iter_get_basic(&value, &imsi);
1262 update_sim_imsi(modem, imsi);
1264 if (modem->online == FALSE) {
1265 modem_set_online(modem);
1269 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1270 if (ready_to_create_device(modem) == TRUE)
1271 create_device(modem);
1272 if (modem->device != NULL) {
1273 cm_get_properties(modem);
1274 cm_get_contexts(modem);
1280 dbus_message_iter_next(dict);
1284 static int sim_get_properties(struct modem_data *modem)
1286 return get_properties(modem->path, OFONO_SIM_INTERFACE,
1287 sim_properties_reply, modem);
1290 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1293 const char *path = dbus_message_get_path(message);
1294 struct modem_data *modem;
1295 DBusMessageIter iter, value;
1298 modem = g_hash_table_lookup(modem_hash, path);
1302 if (dbus_message_iter_init(message, &iter) == FALSE)
1305 dbus_message_iter_get_basic(&iter, &key);
1307 dbus_message_iter_next(&iter);
1308 dbus_message_iter_recurse(&iter, &value);
1310 if (g_str_equal(key, "Powered") == TRUE) {
1311 dbus_message_iter_get_basic(&value, &modem->powered);
1313 DBG("%s Powered %d", modem->path, modem->powered);
1315 if (modem->powered == FALSE)
1316 modem_set_powered(modem);
1317 } else if (g_str_equal(key, "Online") == TRUE) {
1318 dbus_message_iter_get_basic(&value, &modem->online);
1320 DBG("%s Online %d", modem->path, modem->online);
1322 if (modem->online == FALSE)
1325 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1327 if (ready_to_create_device(modem) == TRUE)
1328 create_device(modem);
1329 if (modem->device != NULL) {
1330 cm_get_properties(modem);
1331 cm_get_contexts(modem);
1333 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1334 modem->interfaces = extract_interfaces(&value);
1336 DBG("%s Interfaces 0x%02x", modem->path,
1339 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1340 if (modem->imsi == NULL &&
1341 modem->set_powered == FALSE) {
1343 * Only use do GetProperties() when
1344 * device has not been powered up.
1346 sim_get_properties(modem);
1351 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1352 if (ready_to_create_device(modem) == TRUE)
1353 create_device(modem);
1354 if (modem->device != NULL) {
1355 cm_get_properties(modem);
1356 cm_get_contexts(modem);
1360 if (modem->context != NULL) {
1361 remove_cm_context(modem,
1362 modem->context->path);
1365 if (modem->device != NULL)
1366 destroy_device(modem);
1371 if (has_interface(modem->interfaces, OFONO_API_NETREG) == TRUE) {
1372 if (modem->attached == TRUE)
1373 netreg_get_properties(modem);
1375 } else if (g_str_equal(key, "Serial") == TRUE) {
1378 dbus_message_iter_get_basic(&value, &serial);
1380 g_free(modem->serial);
1381 modem->serial = g_strdup(serial);
1383 DBG("%s Serial %s", modem->path, modem->serial);
1385 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1386 if (ready_to_create_device(modem) == TRUE)
1387 create_device(modem);
1388 if (modem->device != NULL) {
1389 cm_get_properties(modem);
1390 cm_get_contexts(modem);
1398 static void add_modem(const char *path, DBusMessageIter *prop)
1400 struct modem_data *modem;
1404 modem = g_hash_table_lookup(modem_hash, path);
1405 if (modem != NULL) {
1407 * When oFono powers up we ask for the modems and oFono is
1408 * reporting with modem_added signal the modems. Only
1414 modem = g_try_new0(struct modem_data, 1);
1418 modem->path = g_strdup(path);
1420 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1422 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1423 DBusMessageIter entry, value;
1426 dbus_message_iter_recurse(prop, &entry);
1427 dbus_message_iter_get_basic(&entry, &key);
1429 dbus_message_iter_next(&entry);
1430 dbus_message_iter_recurse(&entry, &value);
1432 if (g_str_equal(key, "Powered") == TRUE) {
1433 dbus_message_iter_get_basic(&value, &modem->powered);
1435 DBG("%s Powered %d", modem->path, modem->powered);
1436 } else if (g_str_equal(key, "Online") == TRUE) {
1437 dbus_message_iter_get_basic(&value, &modem->online);
1439 DBG("%s Online %d", modem->path, modem->online);
1440 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1441 modem->interfaces = extract_interfaces(&value);
1443 DBG("%s Interfaces 0x%02x", modem->path,
1445 } else if (g_str_equal(key, "Serial") == TRUE) {
1448 dbus_message_iter_get_basic(&value, &serial);
1449 modem->serial = g_strdup(serial);
1451 DBG("%s Serial %s", modem->path, modem->serial);
1454 dbus_message_iter_next(prop);
1457 if (modem->powered == FALSE) {
1458 modem_set_powered(modem);
1459 } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1460 sim_get_properties(modem);
1461 } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1462 if (ready_to_create_device(modem) == TRUE)
1463 create_device(modem);
1464 if (modem->device != NULL) {
1465 cm_get_properties(modem);
1466 cm_get_contexts(modem);
1471 static void remove_modem(gpointer data)
1473 struct modem_data *modem = data;
1475 DBG("%s", modem->path);
1477 if (modem->call_set_property != NULL)
1478 dbus_pending_call_cancel(modem->call_set_property);
1480 if (modem->call_get_properties != NULL)
1481 dbus_pending_call_cancel(modem->call_get_properties);
1483 if (modem->call_get_contexts != NULL)
1484 dbus_pending_call_cancel(modem->call_get_contexts);
1486 if (modem->device != NULL)
1487 destroy_device(modem);
1489 if (modem->context != NULL)
1490 remove_cm_context(modem, modem->context->path);
1492 g_free(modem->serial);
1493 g_free(modem->name);
1494 g_free(modem->imsi);
1495 g_free(modem->path);
1500 static gboolean modem_added(DBusConnection *connection,
1501 DBusMessage *message, void *user_data)
1503 DBusMessageIter iter, properties;
1508 if (dbus_message_iter_init(message, &iter) == FALSE)
1511 dbus_message_iter_get_basic(&iter, &path);
1513 dbus_message_iter_next(&iter);
1514 dbus_message_iter_recurse(&iter, &properties);
1516 add_modem(path, &properties);
1521 static gboolean modem_removed(DBusConnection *connection,
1522 DBusMessage *message, void *user_data)
1524 DBusMessageIter iter;
1529 if (dbus_message_iter_init(message, &iter) == FALSE)
1532 dbus_message_iter_get_basic(&iter, &path);
1534 g_hash_table_remove(modem_hash, path);
1539 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1543 DBusMessageIter array, dict;
1547 reply = dbus_pending_call_steal_reply(call);
1549 dbus_error_init(&error);
1551 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1552 connman_error("%s", error.message);
1553 dbus_error_free(&error);
1557 if (dbus_message_iter_init(reply, &array) == FALSE)
1560 dbus_message_iter_recurse(&array, &dict);
1562 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1563 DBusMessageIter value, properties;
1566 dbus_message_iter_recurse(&dict, &value);
1567 dbus_message_iter_get_basic(&value, &path);
1569 dbus_message_iter_next(&value);
1570 dbus_message_iter_recurse(&value, &properties);
1572 add_modem(path, &properties);
1574 dbus_message_iter_next(&dict);
1578 dbus_message_unref(reply);
1580 dbus_pending_call_unref(call);
1583 static int manager_get_modems(void)
1585 DBusMessage *message;
1586 DBusPendingCall *call;
1590 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1591 OFONO_MANAGER_INTERFACE, GET_MODEMS);
1592 if (message == NULL)
1595 if (dbus_connection_send_with_reply(connection, message,
1596 &call, TIMEOUT) == FALSE) {
1597 connman_error("Failed to call GetModems()");
1598 dbus_message_unref(message);
1603 connman_error("D-Bus connection not available");
1604 dbus_message_unref(message);
1608 dbus_pending_call_set_notify(call, manager_get_modems_reply,
1611 dbus_message_unref(message);
1613 return -EINPROGRESS;
1616 static void ofono_connect(DBusConnection *conn, void *user_data)
1620 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1621 g_free, remove_modem);
1622 if (modem_hash == NULL)
1625 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1627 if (context_hash == NULL) {
1628 g_hash_table_destroy(modem_hash);
1632 manager_get_modems();
1635 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1639 if (modem_hash == NULL || context_hash == NULL)
1642 g_hash_table_destroy(modem_hash);
1645 g_hash_table_destroy(context_hash);
1646 context_hash = NULL;
1649 static int network_probe(struct connman_network *network)
1651 DBG("network %p", network);
1656 static void network_remove(struct connman_network *network)
1658 DBG("network %p", network);
1661 static int network_connect(struct connman_network *network)
1663 DBG("network %p", network);
1668 static int network_disconnect(struct connman_network *network)
1670 DBG("network %p", network);
1675 static struct connman_network_driver network_driver = {
1677 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
1678 .probe = network_probe,
1679 .remove = network_remove,
1680 .connect = network_connect,
1681 .disconnect = network_disconnect,
1684 static int modem_probe(struct connman_device *device)
1686 struct modem_data *modem = connman_device_get_data(device);
1688 DBG("%s device %p", modem->path, device);
1693 static void modem_remove(struct connman_device *device)
1695 struct modem_data *modem = connman_device_get_data(device);
1697 DBG("%s device %p", modem->path, device);
1700 static int modem_enable(struct connman_device *device)
1702 struct modem_data *modem = connman_device_get_data(device);
1704 DBG("%s device %p", modem->path, device);
1709 static int modem_disable(struct connman_device *device)
1711 struct modem_data *modem = connman_device_get_data(device);
1713 DBG("%s device %p", modem->path, device);
1718 static struct connman_device_driver modem_driver = {
1720 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
1721 .probe = modem_probe,
1722 .remove = modem_remove,
1723 .enable = modem_enable,
1724 .disable = modem_disable,
1728 static guint modem_added_watch;
1729 static guint modem_removed_watch;
1730 static guint modem_watch;
1731 static guint cm_watch;
1732 static guint sim_watch;
1733 static guint context_added_watch;
1734 static guint context_removed_watch;
1735 static guint netreg_watch;
1736 static guint context_watch;
1738 static int ofono_init(void)
1744 connection = connman_dbus_get_connection();
1745 if (connection == NULL)
1748 watch = g_dbus_add_service_watch(connection,
1749 OFONO_SERVICE, ofono_connect,
1750 ofono_disconnect, NULL, NULL);
1752 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1753 OFONO_MANAGER_INTERFACE,
1758 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1759 OFONO_MANAGER_INTERFACE,
1764 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1765 OFONO_MODEM_INTERFACE,
1770 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1776 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1777 OFONO_SIM_INTERFACE,
1782 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1788 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1794 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1795 OFONO_CONTEXT_INTERFACE,
1800 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1801 OFONO_NETREG_INTERFACE,
1807 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
1808 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
1809 context_added_watch == 0 ||
1810 context_removed_watch == 0 ||
1811 context_watch == 0 || netreg_watch == 0) {
1816 err = connman_network_driver_register(&network_driver);
1820 err = connman_device_driver_register(&modem_driver);
1822 connman_network_driver_unregister(&network_driver);
1829 g_dbus_remove_watch(connection, netreg_watch);
1830 g_dbus_remove_watch(connection, context_watch);
1831 g_dbus_remove_watch(connection, context_removed_watch);
1832 g_dbus_remove_watch(connection, context_added_watch);
1833 g_dbus_remove_watch(connection, sim_watch);
1834 g_dbus_remove_watch(connection, cm_watch);
1835 g_dbus_remove_watch(connection, modem_watch);
1836 g_dbus_remove_watch(connection, modem_removed_watch);
1837 g_dbus_remove_watch(connection, modem_added_watch);
1838 g_dbus_remove_watch(connection, watch);
1839 dbus_connection_unref(connection);
1844 static void ofono_exit(void)
1848 if (modem_hash != NULL) {
1849 g_hash_table_destroy(modem_hash);
1853 if (context_hash != NULL) {
1854 g_hash_table_destroy(context_hash);
1855 context_hash = NULL;
1858 connman_device_driver_unregister(&modem_driver);
1859 connman_network_driver_unregister(&network_driver);
1861 g_dbus_remove_watch(connection, netreg_watch);
1862 g_dbus_remove_watch(connection, context_watch);
1863 g_dbus_remove_watch(connection, context_removed_watch);
1864 g_dbus_remove_watch(connection, context_added_watch);
1865 g_dbus_remove_watch(connection, sim_watch);
1866 g_dbus_remove_watch(connection, cm_watch);
1867 g_dbus_remove_watch(connection, modem_watch);
1868 g_dbus_remove_watch(connection, modem_added_watch);
1869 g_dbus_remove_watch(connection, modem_removed_watch);
1870 g_dbus_remove_watch(connection, watch);
1872 dbus_connection_unref(connection);
1875 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1876 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)