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 connman_bool_t ready_to_create_device(struct modem_data *modem)
464 * There are three different modem types which behave slightly
466 * - GSM modems will expose the SIM interface then the
468 * - DUN modems will expose first a unique serial number (BDADDR)
469 * and then the CM interface.
470 * - CDMA modems will expose CM first and sometime later
471 * a unique serial number.
473 * This functions tests if we have the necessary information gathered
474 * before we are able to create a device.
477 if (modem->device != NULL)
480 if (modem->imsi != NULL || modem->serial != NULL)
486 static void create_device(struct modem_data *modem)
488 struct connman_device *device;
489 char *uninitialized_var(ident);
491 DBG("%s", modem->path);
493 if (modem->imsi != NULL)
495 else if (modem->serial != NULL)
496 ident = modem->serial;
498 if (connman_dbus_validate_ident(ident) == FALSE)
499 ident = connman_dbus_encode_string(ident);
501 ident = g_strdup(ident);
503 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
507 DBG("device %p", device);
509 connman_device_set_ident(device, ident);
511 connman_device_set_string(device, "Path", modem->path);
513 connman_device_set_data(device, modem);
515 if (connman_device_register(device) < 0) {
516 connman_error("Failed to register cellular device");
517 connman_device_unref(device);
521 modem->device = device;
527 static void destroy_device(struct modem_data *modem)
529 DBG("%s", modem->path);
531 connman_device_set_powered(modem->device, FALSE);
533 connman_device_unregister(modem->device);
534 connman_device_unref(modem->device);
536 modem->device = NULL;
539 static int add_cm_context(struct modem_data *modem, const char *context_path,
540 DBusMessageIter *dict)
542 const char *context_type;
543 struct network_context *context = NULL;
544 connman_bool_t active = FALSE;
546 DBG("%s context path %s", modem->path, context_path);
548 if (modem->context != NULL) {
550 * We have already assigned a context to this modem
551 * and we do only support one Internet context.
556 context = network_context_alloc(context_path);
560 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
561 DBusMessageIter entry, value;
564 dbus_message_iter_recurse(dict, &entry);
565 dbus_message_iter_get_basic(&entry, &key);
567 dbus_message_iter_next(&entry);
568 dbus_message_iter_recurse(&entry, &value);
570 if (g_str_equal(key, "Type") == TRUE) {
571 dbus_message_iter_get_basic(&value, &context_type);
573 DBG("%s context %s type %s", modem->path,
574 context_path, context_type);
575 } else if (g_str_equal(key, "Settings") == TRUE) {
576 DBG("%s Settings", modem->path);
577 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
578 DBG("%s IPv6.Settings", modem->path);
579 } else if (g_str_equal(key, "Active") == TRUE) {
580 dbus_message_iter_get_basic(&value, &active);
582 DBG("%s Active %d", modem->path, active);
585 dbus_message_iter_next(dict);
588 if (g_strcmp0(context_type, "internet") != 0) {
589 network_context_free(context);
593 modem->context = context;
594 modem->active = active;
596 g_hash_table_replace(context_hash, g_strdup(context_path), modem);
601 static void remove_cm_context(struct modem_data *modem,
602 const char *context_path)
604 if (modem->context == NULL)
607 g_hash_table_remove(context_hash, context_path);
609 network_context_free(modem->context);
610 modem->context = NULL;
613 static gboolean context_changed(DBusConnection *connection,
614 DBusMessage *message,
617 const char *context_path = dbus_message_get_path(message);
618 struct modem_data *modem = NULL;
619 DBusMessageIter iter, value;
622 DBG("context_path %s", context_path);
624 modem = g_hash_table_lookup(context_hash, context_path);
628 if (dbus_message_iter_init(message, &iter) == FALSE)
631 dbus_message_iter_get_basic(&iter, &key);
633 dbus_message_iter_next(&iter);
634 dbus_message_iter_recurse(&iter, &value);
637 * oFono guarantees the ordering of Settings and
638 * Active. Settings will always be send before Active = True.
639 * That means we don't have to order here.
641 if (g_str_equal(key, "Settings") == TRUE) {
642 DBG("%s Settings", modem->path);
643 } else if (g_str_equal(key, "IPv6.Settings") == TRUE) {
644 DBG("%s IPv6.Settings", modem->path);
645 } else if (g_str_equal(key, "Active") == TRUE) {
646 dbus_message_iter_get_basic(&value, &modem->active);
648 DBG("%s Active %d", modem->path, modem->active);
654 static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
656 struct modem_data *modem = user_data;
657 DBusMessageIter array, dict, entry, value;
661 DBG("%s", modem->path);
663 modem->call_get_contexts = NULL;
665 reply = dbus_pending_call_steal_reply(call);
667 dbus_error_init(&error);
669 if (dbus_set_error_from_message(&error, reply) == TRUE) {
670 connman_error("%s", error.message);
671 dbus_error_free(&error);
675 if (dbus_message_iter_init(reply, &array) == FALSE)
678 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
681 dbus_message_iter_recurse(&array, &dict);
683 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
684 const char *context_path;
686 dbus_message_iter_recurse(&dict, &entry);
687 dbus_message_iter_get_basic(&entry, &context_path);
689 dbus_message_iter_next(&entry);
690 dbus_message_iter_recurse(&entry, &value);
692 if (add_cm_context(modem, context_path, &value) == 0)
695 dbus_message_iter_next(&dict);
699 dbus_message_unref(reply);
701 dbus_pending_call_unref(call);
704 static int cm_get_contexts(struct modem_data *modem)
706 DBusMessage *message;
708 DBG("%s", modem->path);
710 if (modem->call_get_contexts != NULL)
713 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
714 OFONO_CM_INTERFACE, GET_CONTEXTS);
718 if (dbus_connection_send_with_reply(connection, message,
719 &modem->call_get_contexts, TIMEOUT) == FALSE) {
720 connman_error("Failed to call GetContexts()");
721 dbus_message_unref(message);
725 if (modem->call_get_contexts == NULL) {
726 connman_error("D-Bus connection not available");
727 dbus_message_unref(message);
731 dbus_pending_call_set_notify(modem->call_get_contexts,
732 cm_get_contexts_reply,
735 dbus_message_unref(message);
740 static gboolean cm_context_added(DBusConnection *connection,
741 DBusMessage *message,
744 const char *path = dbus_message_get_path(message);
746 struct modem_data *modem;
747 DBusMessageIter iter, properties;
751 modem = g_hash_table_lookup(modem_hash, context_path);
755 if (dbus_message_iter_init(message, &iter) == FALSE)
758 dbus_message_iter_get_basic(&iter, &context_path);
760 dbus_message_iter_next(&iter);
761 dbus_message_iter_recurse(&iter, &properties);
763 if (add_cm_context(modem, context_path, &properties) != 0)
769 static gboolean cm_context_removed(DBusConnection *connection,
770 DBusMessage *message,
773 const char *path = dbus_message_get_path(message);
774 const char *context_path;
775 struct modem_data *modem;
776 DBusMessageIter iter;
778 DBG("context path %s", path);
780 if (dbus_message_iter_init(message, &iter) == FALSE)
783 dbus_message_iter_get_basic(&iter, &context_path);
785 modem = g_hash_table_lookup(context_hash, context_path);
789 remove_cm_context(modem, context_path);
794 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
800 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
803 const char *path = dbus_message_get_path(message);
804 struct modem_data *modem;
805 DBusMessageIter iter, value;
808 modem = g_hash_table_lookup(modem_hash, path);
812 if (dbus_message_iter_init(message, &iter) == FALSE)
815 dbus_message_iter_get_basic(&iter, &key);
817 dbus_message_iter_next(&iter);
818 dbus_message_iter_recurse(&iter, &value);
820 if (g_str_equal(key, "Attached") == TRUE) {
821 dbus_message_iter_get_basic(&value, &modem->attached);
823 DBG("%s Attached %d", modem->path, modem->attached);
824 } else if (g_str_equal(key, "Powered") == TRUE) {
825 dbus_message_iter_get_basic(&value, &modem->cm_powered);
827 DBG("%s ConnnectionManager Powered %d", modem->path,
830 if (modem->cm_powered == FALSE)
831 cm_set_powered(modem);
837 static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
839 DBG("%s", modem->path);
841 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
842 DBusMessageIter entry, value;
845 dbus_message_iter_recurse(dict, &entry);
846 dbus_message_iter_get_basic(&entry, &key);
848 dbus_message_iter_next(&entry);
849 dbus_message_iter_recurse(&entry, &value);
851 if (g_str_equal(key, "Attached") == TRUE) {
852 dbus_message_iter_get_basic(&value, &modem->attached);
854 DBG("%s Attached %d", modem->path,
856 } else if (g_str_equal(key, "Powered") == TRUE) {
857 dbus_message_iter_get_basic(&value, &modem->cm_powered);
859 DBG("%s ConnnectionManager Powered %d", modem->path,
862 if (modem->cm_powered == FALSE)
863 cm_set_powered(modem);
866 dbus_message_iter_next(dict);
870 static int cm_get_properties(struct modem_data *modem)
872 return get_properties(modem->path, OFONO_CM_INTERFACE,
873 cm_properties_reply, modem);
876 static void update_sim_imsi(struct modem_data *modem,
879 DBG("%s imsi %s", modem->path, imsi);
881 if (g_strcmp0(modem->imsi, imsi) == 0)
885 modem->imsi = g_strdup(imsi);
888 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
891 const char *path = dbus_message_get_path(message);
892 struct modem_data *modem;
893 DBusMessageIter iter, value;
896 modem = g_hash_table_lookup(modem_hash, path);
900 if (dbus_message_iter_init(message, &iter) == FALSE)
903 dbus_message_iter_get_basic(&iter, &key);
905 dbus_message_iter_next(&iter);
906 dbus_message_iter_recurse(&iter, &value);
908 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
911 dbus_message_iter_get_basic(&value, &imsi);
913 update_sim_imsi(modem, imsi);
915 if (modem->online == FALSE) {
916 modem_set_online(modem);
917 } else if (has_interface(modem->interfaces,
918 OFONO_API_CM) == TRUE) {
919 if (ready_to_create_device(modem) == TRUE)
920 create_device(modem);
927 static void sim_properties_reply(struct modem_data *modem,
928 DBusMessageIter *dict)
930 DBG("%s", modem->path);
932 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
933 DBusMessageIter entry, value;
936 dbus_message_iter_recurse(dict, &entry);
937 dbus_message_iter_get_basic(&entry, &key);
939 dbus_message_iter_next(&entry);
940 dbus_message_iter_recurse(&entry, &value);
942 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
945 dbus_message_iter_get_basic(&value, &imsi);
947 update_sim_imsi(modem, imsi);
949 if (modem->online == FALSE) {
950 modem_set_online(modem);
954 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
955 if (ready_to_create_device(modem) == TRUE)
956 create_device(modem);
957 if (modem->device != NULL) {
958 cm_get_properties(modem);
959 cm_get_contexts(modem);
965 dbus_message_iter_next(dict);
969 static int sim_get_properties(struct modem_data *modem)
971 return get_properties(modem->path, OFONO_SIM_INTERFACE,
972 sim_properties_reply, modem);
975 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
978 const char *path = dbus_message_get_path(message);
979 struct modem_data *modem;
980 DBusMessageIter iter, value;
983 modem = g_hash_table_lookup(modem_hash, path);
987 if (dbus_message_iter_init(message, &iter) == FALSE)
990 dbus_message_iter_get_basic(&iter, &key);
992 dbus_message_iter_next(&iter);
993 dbus_message_iter_recurse(&iter, &value);
995 if (g_str_equal(key, "Powered") == TRUE) {
996 dbus_message_iter_get_basic(&value, &modem->powered);
998 DBG("%s Powered %d", modem->path, modem->powered);
1000 if (modem->powered == FALSE)
1001 modem_set_powered(modem);
1002 } else if (g_str_equal(key, "Online") == TRUE) {
1003 dbus_message_iter_get_basic(&value, &modem->online);
1005 DBG("%s Online %d", modem->path, modem->online);
1007 if (modem->online == FALSE)
1010 if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
1012 if (ready_to_create_device(modem) == TRUE)
1013 create_device(modem);
1014 if (modem->device != NULL) {
1015 cm_get_properties(modem);
1016 cm_get_contexts(modem);
1018 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1019 modem->interfaces = extract_interfaces(&value);
1021 DBG("%s Interfaces 0x%02x", modem->path,
1024 if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1025 if (modem->imsi == NULL &&
1026 modem->set_powered == FALSE) {
1028 * Only use do GetProperties() when
1029 * device has not been powered up.
1031 sim_get_properties(modem);
1036 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1037 if (ready_to_create_device(modem) == TRUE)
1038 create_device(modem);
1039 if (modem->device != NULL) {
1040 cm_get_properties(modem);
1041 cm_get_contexts(modem);
1044 if (modem->context != NULL) {
1045 remove_cm_context(modem,
1046 modem->context->path);
1049 if (modem->device != NULL)
1050 destroy_device(modem);
1052 } else if (g_str_equal(key, "Serial") == TRUE) {
1055 dbus_message_iter_get_basic(&value, &serial);
1057 g_free(modem->serial);
1058 modem->serial = g_strdup(serial);
1060 DBG("%s Serial %s", modem->path, modem->serial);
1062 if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1063 if (ready_to_create_device(modem) == TRUE)
1064 create_device(modem);
1065 if (modem->device != NULL) {
1066 cm_get_properties(modem);
1067 cm_get_contexts(modem);
1075 static void add_modem(const char *path, DBusMessageIter *prop)
1077 struct modem_data *modem;
1081 modem = g_hash_table_lookup(modem_hash, path);
1082 if (modem != NULL) {
1084 * When oFono powers up we ask for the modems and oFono is
1085 * reporting with modem_added signal the modems. Only
1091 modem = g_try_new0(struct modem_data, 1);
1095 modem->path = g_strdup(path);
1097 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1099 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1100 DBusMessageIter entry, value;
1103 dbus_message_iter_recurse(prop, &entry);
1104 dbus_message_iter_get_basic(&entry, &key);
1106 dbus_message_iter_next(&entry);
1107 dbus_message_iter_recurse(&entry, &value);
1109 if (g_str_equal(key, "Powered") == TRUE) {
1110 dbus_message_iter_get_basic(&value, &modem->powered);
1112 DBG("%s Powered %d", modem->path, modem->powered);
1113 } else if (g_str_equal(key, "Online") == TRUE) {
1114 dbus_message_iter_get_basic(&value, &modem->online);
1116 DBG("%s Online %d", modem->path, modem->online);
1117 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1118 modem->interfaces = extract_interfaces(&value);
1120 DBG("%s Interfaces 0x%02x", modem->path,
1122 } else if (g_str_equal(key, "Serial") == TRUE) {
1125 dbus_message_iter_get_basic(&value, &serial);
1126 modem->serial = g_strdup(serial);
1128 DBG("%s Serial %s", modem->path, modem->serial);
1131 dbus_message_iter_next(prop);
1134 if (modem->powered == FALSE) {
1135 modem_set_powered(modem);
1136 } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
1137 sim_get_properties(modem);
1138 } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
1139 if (ready_to_create_device(modem) == TRUE)
1140 create_device(modem);
1141 if (modem->device != NULL) {
1142 cm_get_properties(modem);
1143 cm_get_contexts(modem);
1148 static void remove_modem(gpointer data)
1150 struct modem_data *modem = data;
1152 DBG("%s", modem->path);
1154 if (modem->call_set_property != NULL)
1155 dbus_pending_call_cancel(modem->call_set_property);
1157 if (modem->call_get_properties != NULL)
1158 dbus_pending_call_cancel(modem->call_get_properties);
1160 if (modem->call_get_contexts != NULL)
1161 dbus_pending_call_cancel(modem->call_get_contexts);
1163 if (modem->device != NULL)
1164 destroy_device(modem);
1166 if (modem->context != NULL)
1167 remove_cm_context(modem, modem->context->path);
1169 g_free(modem->serial);
1170 g_free(modem->imsi);
1171 g_free(modem->path);
1176 static gboolean modem_added(DBusConnection *connection,
1177 DBusMessage *message, void *user_data)
1179 DBusMessageIter iter, properties;
1184 if (dbus_message_iter_init(message, &iter) == FALSE)
1187 dbus_message_iter_get_basic(&iter, &path);
1189 dbus_message_iter_next(&iter);
1190 dbus_message_iter_recurse(&iter, &properties);
1192 add_modem(path, &properties);
1197 static gboolean modem_removed(DBusConnection *connection,
1198 DBusMessage *message, void *user_data)
1200 DBusMessageIter iter;
1205 if (dbus_message_iter_init(message, &iter) == FALSE)
1208 dbus_message_iter_get_basic(&iter, &path);
1210 g_hash_table_remove(modem_hash, path);
1215 static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
1219 DBusMessageIter array, dict;
1223 reply = dbus_pending_call_steal_reply(call);
1225 dbus_error_init(&error);
1227 if (dbus_set_error_from_message(&error, reply) == TRUE) {
1228 connman_error("%s", error.message);
1229 dbus_error_free(&error);
1233 if (dbus_message_iter_init(reply, &array) == FALSE)
1236 dbus_message_iter_recurse(&array, &dict);
1238 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1239 DBusMessageIter value, properties;
1242 dbus_message_iter_recurse(&dict, &value);
1243 dbus_message_iter_get_basic(&value, &path);
1245 dbus_message_iter_next(&value);
1246 dbus_message_iter_recurse(&value, &properties);
1248 add_modem(path, &properties);
1250 dbus_message_iter_next(&dict);
1254 dbus_message_unref(reply);
1256 dbus_pending_call_unref(call);
1259 static int manager_get_modems(void)
1261 DBusMessage *message;
1262 DBusPendingCall *call;
1266 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1267 OFONO_MANAGER_INTERFACE, GET_MODEMS);
1268 if (message == NULL)
1271 if (dbus_connection_send_with_reply(connection, message,
1272 &call, TIMEOUT) == FALSE) {
1273 connman_error("Failed to call GetModems()");
1274 dbus_message_unref(message);
1279 connman_error("D-Bus connection not available");
1280 dbus_message_unref(message);
1284 dbus_pending_call_set_notify(call, manager_get_modems_reply,
1287 dbus_message_unref(message);
1289 return -EINPROGRESS;
1292 static void ofono_connect(DBusConnection *conn, void *user_data)
1296 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1297 g_free, remove_modem);
1298 if (modem_hash == NULL)
1301 context_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1303 if (context_hash == NULL) {
1304 g_hash_table_destroy(modem_hash);
1308 manager_get_modems();
1311 static void ofono_disconnect(DBusConnection *conn, void *user_data)
1315 if (modem_hash == NULL || context_hash == NULL)
1318 g_hash_table_destroy(modem_hash);
1321 g_hash_table_destroy(context_hash);
1322 context_hash = NULL;
1325 static int network_probe(struct connman_network *network)
1327 DBG("network %p", network);
1332 static void network_remove(struct connman_network *network)
1334 DBG("network %p", network);
1337 static int network_connect(struct connman_network *network)
1339 DBG("network %p", network);
1344 static int network_disconnect(struct connman_network *network)
1346 DBG("network %p", network);
1351 static struct connman_network_driver network_driver = {
1353 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
1354 .probe = network_probe,
1355 .remove = network_remove,
1356 .connect = network_connect,
1357 .disconnect = network_disconnect,
1360 static int modem_probe(struct connman_device *device)
1362 struct modem_data *modem = connman_device_get_data(device);
1364 DBG("%s device %p", modem->path, device);
1369 static void modem_remove(struct connman_device *device)
1371 struct modem_data *modem = connman_device_get_data(device);
1373 DBG("%s device %p", modem->path, device);
1376 static int modem_enable(struct connman_device *device)
1378 struct modem_data *modem = connman_device_get_data(device);
1380 DBG("%s device %p", modem->path, device);
1385 static int modem_disable(struct connman_device *device)
1387 struct modem_data *modem = connman_device_get_data(device);
1389 DBG("%s device %p", modem->path, device);
1394 static struct connman_device_driver modem_driver = {
1396 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
1397 .probe = modem_probe,
1398 .remove = modem_remove,
1399 .enable = modem_enable,
1400 .disable = modem_disable,
1404 static guint modem_added_watch;
1405 static guint modem_removed_watch;
1406 static guint modem_watch;
1407 static guint cm_watch;
1408 static guint sim_watch;
1409 static guint context_added_watch;
1410 static guint context_removed_watch;
1411 static guint netreg_watch;
1412 static guint context_watch;
1414 static int ofono_init(void)
1420 connection = connman_dbus_get_connection();
1421 if (connection == NULL)
1424 watch = g_dbus_add_service_watch(connection,
1425 OFONO_SERVICE, ofono_connect,
1426 ofono_disconnect, NULL, NULL);
1428 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1429 OFONO_MANAGER_INTERFACE,
1434 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1435 OFONO_MANAGER_INTERFACE,
1440 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1441 OFONO_MODEM_INTERFACE,
1446 cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1452 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1453 OFONO_SIM_INTERFACE,
1458 context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1464 context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1470 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1471 OFONO_CONTEXT_INTERFACE,
1476 netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1477 OFONO_NETREG_INTERFACE,
1483 if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
1484 modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
1485 context_added_watch == 0 ||
1486 context_removed_watch == 0 ||
1487 context_watch == 0 || netreg_watch == 0) {
1492 err = connman_network_driver_register(&network_driver);
1496 err = connman_device_driver_register(&modem_driver);
1498 connman_network_driver_unregister(&network_driver);
1505 g_dbus_remove_watch(connection, netreg_watch);
1506 g_dbus_remove_watch(connection, context_watch);
1507 g_dbus_remove_watch(connection, context_removed_watch);
1508 g_dbus_remove_watch(connection, context_added_watch);
1509 g_dbus_remove_watch(connection, sim_watch);
1510 g_dbus_remove_watch(connection, cm_watch);
1511 g_dbus_remove_watch(connection, modem_watch);
1512 g_dbus_remove_watch(connection, modem_removed_watch);
1513 g_dbus_remove_watch(connection, modem_added_watch);
1514 g_dbus_remove_watch(connection, watch);
1515 dbus_connection_unref(connection);
1520 static void ofono_exit(void)
1524 if (modem_hash != NULL) {
1525 g_hash_table_destroy(modem_hash);
1529 if (context_hash != NULL) {
1530 g_hash_table_destroy(context_hash);
1531 context_hash = NULL;
1534 connman_device_driver_unregister(&modem_driver);
1535 connman_network_driver_unregister(&network_driver);
1537 g_dbus_remove_watch(connection, netreg_watch);
1538 g_dbus_remove_watch(connection, context_watch);
1539 g_dbus_remove_watch(connection, context_removed_watch);
1540 g_dbus_remove_watch(connection, context_added_watch);
1541 g_dbus_remove_watch(connection, sim_watch);
1542 g_dbus_remove_watch(connection, cm_watch);
1543 g_dbus_remove_watch(connection, modem_watch);
1544 g_dbus_remove_watch(connection, modem_added_watch);
1545 g_dbus_remove_watch(connection, modem_removed_watch);
1546 g_dbus_remove_watch(connection, watch);
1548 dbus_connection_unref(connection);
1551 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1552 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)