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
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/element.h>
35 #include <connman/device.h>
36 #include <connman/network.h>
37 #include <connman/dbus.h>
38 #include <connman/inet.h>
39 #include <connman/log.h>
41 #define OFONO_SERVICE "org.ofono"
43 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
45 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
46 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
47 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
48 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
50 #define PROPERTY_CHANGED "PropertyChanged"
51 #define GET_PROPERTIES "GetProperties"
52 #define SET_PROPERTY "SetProperty"
53 #define CONTEXT_ADDED "ContextAdded"
54 #define CONTEXT_REMOVED "ContextRemoved"
55 #define ADD_CONTEXT "AddContext"
59 #define CONTEXT_NAME "3G Connection"
60 #define CONTEXT_TYPE "internet"
62 static DBusConnection *connection;
64 static GHashTable *modem_hash = NULL;
66 static GHashTable *network_hash;
70 struct connman_device *device;
75 gboolean pending_online;
76 gboolean requested_online;
79 uint8_t strength, has_strength;
82 static int modem_probe(struct connman_device *device)
84 DBG("device %p", device);
89 static void modem_remove(struct connman_device *device)
91 DBG("device %p", device);
94 static int call_ofono(const char *path,
95 const char *interface, const char *method,
96 DBusPendingCallNotifyFunction notify, void *user_data,
97 DBusFreeFunction free_function,
100 DBusMessage *message;
101 DBusPendingCall *call;
105 DBG("path %s %s.%s", path, interface, method);
110 message = dbus_message_new_method_call(OFONO_SERVICE, path,
115 dbus_message_set_auto_start(message, FALSE);
118 ok = dbus_message_append_args_valist(message, type, va);
124 if (dbus_connection_send_with_reply(connection, message,
125 &call, TIMEOUT) == FALSE) {
126 connman_error("Failed to call %s.%s", interface, method);
127 dbus_message_unref(message);
132 connman_error("D-Bus connection not available");
133 dbus_message_unref(message);
137 dbus_pending_call_set_notify(call, notify, user_data, free_function);
139 dbus_message_unref(message);
144 static void set_property_reply(DBusPendingCall *call, void *user_data)
148 char const *name = user_data;
152 dbus_error_init(&error);
154 reply = dbus_pending_call_steal_reply(call);
156 if (dbus_set_error_from_message(&error, reply)) {
157 connman_error("SetProperty(%s) %s %s", name,
158 error.name, error.message);
159 dbus_error_free(&error);
162 dbus_message_unref(reply);
164 dbus_pending_call_unref(call);
167 static int set_property(const char *path, const char *interface,
168 const char *property, int type, void *value,
169 DBusPendingCallNotifyFunction notify, void *user_data,
170 DBusFreeFunction free_function)
172 DBusMessage *message;
173 DBusMessageIter iter;
174 DBusPendingCall *call;
176 DBG("path %s %s.%s", path, interface, property);
178 g_assert(notify == NULL ? free_function == NULL : 1);
183 message = dbus_message_new_method_call(OFONO_SERVICE, path,
184 interface, SET_PROPERTY);
188 dbus_message_set_auto_start(message, FALSE);
190 dbus_message_iter_init_append(message, &iter);
191 connman_dbus_property_append_basic(&iter, property, type, value);
193 if (dbus_connection_send_with_reply(connection, message,
194 &call, TIMEOUT) == FALSE) {
195 connman_error("Failed to change \"%s\" property on %s",
196 property, interface);
197 dbus_message_unref(message);
202 connman_error("D-Bus connection not available");
203 dbus_message_unref(message);
207 if (notify == NULL) {
208 notify = set_property_reply;
209 user_data = (void *)property;
210 free_function = NULL;
213 dbus_pending_call_set_notify(call, notify, user_data, free_function);
215 dbus_message_unref(message);
220 static void update_modem_online(struct modem_data *modem,
221 connman_bool_t online)
223 DBG("modem %p path %s online %d", modem, modem->path, online);
225 modem->online = online;
226 modem->requested_online = online;
227 modem->pending_online = FALSE;
230 connman_device_set_powered(modem->device, online);
233 static void set_online_reply(DBusPendingCall *call, void *user_data)
235 struct modem_data *modem;
240 DBG("path %s", (char *)user_data);
242 if (modem_hash == NULL)
245 modem = g_hash_table_lookup(modem_hash, user_data);
249 reply = dbus_pending_call_steal_reply(call);
251 dbus_error_init(&error);
253 if (dbus_set_error_from_message(&error, reply)) {
254 connman_error("SetProperty(Online) %s %s",
255 error.name, error.message);
256 dbus_error_free(&error);
258 result = modem->online;
260 result = modem->requested_online;
262 if (modem->pending_online)
263 update_modem_online(modem, result);
265 dbus_message_unref(reply);
267 dbus_pending_call_unref(call);
270 static int modem_change_online(char const *path, dbus_bool_t online)
272 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
273 DBUS_TYPE_BOOLEAN, &online,
275 (void *)g_strdup(path), g_free);
278 static int modem_enable(struct connman_device *device)
280 const char *path = connman_device_get_string(device, "Path");
282 DBG("device %p, path, %s", device, path);
284 return modem_change_online(path, TRUE);
287 static int modem_disable(struct connman_device *device)
289 const char *path = connman_device_get_string(device, "Path");
291 DBG("device %p path %s", device, path);
293 return modem_change_online(path, FALSE);
296 static struct connman_device_driver modem_driver = {
298 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
299 .probe = modem_probe,
300 .remove = modem_remove,
301 .enable = modem_enable,
302 .disable = modem_disable,
305 static void modem_remove_device(struct modem_data *modem)
307 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
309 if (modem->device == NULL)
312 connman_device_remove_all_networks(modem->device);
313 connman_device_unregister(modem->device);
314 connman_device_unref(modem->device);
316 modem->device = NULL;
319 static void remove_modem(gpointer data)
321 struct modem_data *modem = data;
323 modem_remove_device(modem);
330 static void remove_network(gpointer data)
332 connman_network_unref(data);
335 static char *get_ident(const char *path)
342 pos = strrchr(path, '/');
349 static void create_service(struct connman_network *network)
356 path = connman_network_get_string(network, "Path");
358 group = get_ident(path);
360 connman_network_set_group(network, group);
363 static int network_probe(struct connman_network *network)
365 create_service(network);
369 static gboolean pending_network_is_available(struct connman_network *network)
371 /* Modem or network may be removed */
372 if (network == NULL || connman_network_get_device(network) == NULL) {
373 DBG("Modem or network was removed");
380 static void set_active_reply(DBusPendingCall *call, void *user_data)
382 char const *path = user_data;
385 struct connman_network *network;
387 network = g_hash_table_lookup(network_hash, path);
389 DBG("path %s network %p", path, network);
391 reply = dbus_pending_call_steal_reply(call);
393 if (!pending_network_is_available(network))
396 dbus_error_init(&error);
398 if (dbus_set_error_from_message(&error, reply)) {
399 connman_error("SetProperty(Active) %s %s",
400 error.name, error.message);
402 if (connman_network_get_index(network) < 0)
403 connman_network_set_error(network,
404 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
406 dbus_error_free(&error);
410 dbus_message_unref(reply);
412 dbus_pending_call_unref(call);
415 static int set_network_active(struct connman_network *network,
420 const char *path = connman_network_get_string(network, "Path");
422 DBG("network %p, path %s, active %d", network, path, active);
424 error = set_property(path, OFONO_CONTEXT_INTERFACE,
425 "Active", DBUS_TYPE_BOOLEAN, &active,
426 set_active_reply, g_strdup(path), NULL);
428 if (active == FALSE && error == -EINPROGRESS)
434 static void set_apn(struct connman_network *network)
436 const char *apn, *path;
438 apn = connman_network_get_string(network, "Cellular.APN");
442 path = connman_network_get_string(network, "Path");
446 DBG("path %s, apn %s", path, apn);
448 set_property(path, OFONO_CONTEXT_INTERFACE,
449 "AccessPointName", DBUS_TYPE_STRING, &apn,
453 static int network_connect(struct connman_network *network)
455 DBG("network %p", network);
457 if (connman_network_get_index(network) >= 0)
460 return set_network_active(network, TRUE);
463 static int network_disconnect(struct connman_network *network)
465 DBG("network %p", network);
467 if (connman_network_get_index(network) < 0)
470 return set_network_active(network, FALSE);
473 static void network_remove(struct connman_network *network)
475 char const *path = connman_network_get_string(network, "Path");
477 DBG("network %p path %s", network, path);
479 g_hash_table_remove(network_hash, path);
482 static int network_setup(struct connman_network *network, const char *key)
484 if (g_strcmp0(key, "Cellular.APN") == 0)
490 static struct connman_network_driver network_driver = {
492 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
493 .probe = network_probe,
494 .remove = network_remove,
495 .connect = network_connect,
496 .disconnect = network_disconnect,
497 .setup = network_setup,
500 static void update_settings(DBusMessageIter *array,
501 struct connman_network *network);
503 static void set_connected(struct connman_network *network,
504 connman_bool_t connected);
506 static int add_network(struct connman_device *device,
507 const char *path, DBusMessageIter *dict)
509 struct modem_data *modem = connman_device_get_data(device);
510 struct connman_network *network;
512 const char *hash_path;
513 char const *operator;
514 char const *reg_status;
515 dbus_bool_t active = FALSE;
517 DBG("modem %p device %p path %s", modem, device, path);
519 ident = get_ident(path);
521 network = connman_device_get_network(device, ident);
525 network = g_hash_table_lookup(network_hash, path);
526 if (network != NULL) {
527 DBG("path %p already exists with device %p", path,
528 connman_network_get_device(network));
529 if (connman_network_get_device(network))
531 g_hash_table_remove(network_hash, path);
534 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
538 connman_network_set_string(network, "Path", path);
539 hash_path = connman_network_get_string(network, "Path");
540 if (hash_path == NULL)
542 connman_network_ref(network);
543 g_hash_table_insert(network_hash, (char *)hash_path, network);
545 connman_network_set_available(network, TRUE);
546 connman_network_set_index(network, -1);
548 operator = connman_device_get_string(device, "Operator");
550 connman_network_set_name(network, operator);
552 if (modem->has_strength)
553 connman_network_set_strength(network, modem->strength);
555 reg_status = connman_device_get_string(device, "RegistrationStatus");
557 if (!g_strcmp0(reg_status, "roaming"))
558 connman_network_set_roaming(network, TRUE);
559 else if (!g_strcmp0(reg_status, "registered"))
560 connman_network_set_roaming(network, FALSE);
562 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
563 DBusMessageIter entry, value;
566 dbus_message_iter_recurse(dict, &entry);
567 dbus_message_iter_get_basic(&entry, &key);
569 dbus_message_iter_next(&entry);
570 dbus_message_iter_recurse(&entry, &value);
572 if (g_str_equal(key, "Type")) {
575 dbus_message_iter_get_basic(&value, &type);
576 if (g_strcmp0(type, "internet") == 0) {
577 connman_network_set_protocol(network,
578 CONNMAN_NETWORK_PROTOCOL_IP);
580 DBG("path %p type %s", path, type);
583 } else if (g_str_equal(key, "AccessPointName")) {
586 dbus_message_iter_get_basic(&value, &ap);
588 connman_network_set_string(network, "Cellular.APN", ap);
589 } else if (g_str_equal(key, "Settings"))
590 update_settings(&value, network);
591 else if (g_str_equal(key, "Active") == TRUE)
592 dbus_message_iter_get_basic(&value, &active);
594 dbus_message_iter_next(dict);
597 if (connman_device_add_network(device, network) != 0)
601 set_connected(network, active);
606 connman_network_unref(network);
607 g_hash_table_remove(network_hash, hash_path);
611 static void check_networks_reply(DBusPendingCall *call, void *user_data)
613 char *path = user_data;
614 struct modem_data *modem;
616 DBusMessageIter array, entry, value, properties;
618 DBG("path %s", path);
620 modem = g_hash_table_lookup(modem_hash, path);
623 if (modem->device == NULL)
626 reply = dbus_pending_call_steal_reply(call);
628 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
631 dbus_message_iter_init(reply, &array);
633 dbus_message_iter_recurse(&array, &entry);
635 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
636 char const *network_path;
638 dbus_message_iter_recurse(&entry, &value);
639 dbus_message_iter_get_basic(&value, &network_path);
641 dbus_message_iter_next(&value);
642 dbus_message_iter_recurse(&value, &properties);
644 add_network(modem->device, network_path, &properties);
646 dbus_message_iter_next(&entry);
650 dbus_message_unref(reply);
652 dbus_pending_call_unref(call);
655 static void check_networks(struct modem_data *modem)
657 char const *path = modem->path;
659 DBG("modem %p path %s", modem, path);
661 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
662 check_networks_reply, g_strdup(path), g_free,
666 static void modem_operator_name_changed(struct modem_data *modem,
669 struct connman_device *device = modem->device;
676 connman_device_set_string(device, "Operator", name);
678 for (g_hash_table_iter_init(&i, network_hash);
679 g_hash_table_iter_next(&i, NULL, &value);) {
680 struct connman_network *network = value;
682 if (connman_network_get_device(network) == device) {
683 connman_network_set_name(network, name);
684 connman_network_update(network);
689 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
691 struct connman_device *device = modem->device;
695 modem->strength = strength;
696 modem->has_strength = TRUE;
701 for (g_hash_table_iter_init(&i, network_hash);
702 g_hash_table_iter_next(&i, NULL, &value);) {
703 struct connman_network *network = value;
705 if (connman_network_get_device(network) == device) {
706 connman_network_set_strength(network, strength);
707 connman_network_update(network);
712 static void modem_roaming_changed(struct modem_data *modem,
715 struct connman_device *device = modem->device;
716 connman_bool_t roaming;
720 connman_device_set_string(device, "RegistrationStatus", status);
722 if (g_str_equal(status, "roaming"))
724 else if (g_str_equal(status, "registered"))
732 for (g_hash_table_iter_init(&i, network_hash);
733 g_hash_table_iter_next(&i, NULL, &value);) {
734 struct connman_network *network = value;
736 if (connman_network_get_device(network) == device) {
737 connman_network_set_roaming(network, roaming);
738 connman_network_update(network);
743 static void modem_registration_changed(struct modem_data *modem,
744 DBusMessageIter *entry)
746 DBusMessageIter iter;
751 connman_uint8_t byte;
754 dbus_message_iter_get_basic(entry, &key);
758 dbus_message_iter_next(entry);
760 dbus_message_iter_recurse(entry, &iter);
762 type = dbus_message_iter_get_arg_type(&iter);
763 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
766 dbus_message_iter_get_basic(&iter, &value);
768 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING)
769 modem_operator_name_changed(modem, value.str);
770 else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE)
771 modem_strength_changed(modem, value.byte);
772 else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING)
773 modem_roaming_changed(modem, value.str);
777 static gboolean reg_changed(DBusConnection *connection,
778 DBusMessage *message, void *user_data)
780 const char *path = dbus_message_get_path(message);
781 struct modem_data *modem;
782 DBusMessageIter iter;
784 DBG("path %s", path);
786 modem = g_hash_table_lookup(modem_hash, path);
790 if (dbus_message_iter_init(message, &iter))
791 modem_registration_changed(modem, &iter);
796 static void check_registration_reply(DBusPendingCall *call, void *user_data)
798 char const *path = user_data;
799 struct modem_data *modem;
801 DBusMessageIter array, dict, entry;
803 DBG("path %s", path);
805 modem = g_hash_table_lookup(modem_hash, path);
809 reply = dbus_pending_call_steal_reply(call);
811 if (dbus_message_iter_init(reply, &array) == FALSE)
814 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
817 dbus_message_iter_recurse(&array, &dict);
818 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
819 dbus_message_iter_recurse(&dict, &entry);
820 modem_registration_changed(modem, &entry);
821 dbus_message_iter_next(&dict);
825 dbus_message_unref(reply);
827 dbus_pending_call_unref(call);
830 static void check_registration(struct modem_data *modem)
832 char const *path = modem->path;
834 DBG("modem %p path %s", modem, path);
836 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
837 check_registration_reply, g_strdup(path), g_free,
841 static void add_device(const char *path, const char *imsi)
843 struct modem_data *modem;
844 struct connman_device *device;
846 DBG("path %s imsi %s", path, imsi);
854 modem = g_hash_table_lookup(modem_hash, path);
859 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
862 modem_remove_device(modem);
865 if (strlen(imsi) == 0)
868 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
872 connman_device_set_ident(device, imsi);
874 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
876 connman_device_set_string(device, "Path", path);
878 connman_device_set_data(device, modem);
880 if (connman_device_register(device) < 0) {
881 connman_device_unref(device);
885 modem->device = device;
888 check_registration(modem);
890 check_networks(modem);
893 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
895 const char *path = user_data;
896 const char *imsi = NULL;
898 DBusMessageIter array, dict;
900 DBG("path %s", path);
902 reply = dbus_pending_call_steal_reply(call);
904 if (dbus_message_iter_init(reply, &array) == FALSE)
907 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
910 dbus_message_iter_recurse(&array, &dict);
912 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
913 DBusMessageIter entry, value;
916 dbus_message_iter_recurse(&dict, &entry);
917 dbus_message_iter_get_basic(&entry, &key);
919 dbus_message_iter_next(&entry);
920 dbus_message_iter_recurse(&entry, &value);
922 if (g_str_equal(key, "SubscriberIdentity")) {
923 dbus_message_iter_get_basic(&value, &imsi);
924 add_device(path, imsi);
927 dbus_message_iter_next(&dict);
931 dbus_message_unref(reply);
933 dbus_pending_call_unref(call);
936 static void get_imsi(const char *path)
938 DBG("path %s", path);
940 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
941 sim_properties_reply, g_strdup(path), g_free,
945 static int gprs_change_powered(const char *path, dbus_bool_t powered)
947 DBG("path %s powered %d", path, powered);
949 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
950 DBUS_TYPE_BOOLEAN, &powered,
954 static int modem_change_powered(const char *path, dbus_bool_t powered)
956 DBG("path %s powered %d", path, powered);
958 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
959 DBUS_TYPE_BOOLEAN, &powered,
963 static struct modem_data *add_modem(const char *path)
965 struct modem_data *modem;
967 DBG("path %s", path);
972 modem = g_hash_table_lookup(modem_hash, path);
974 modem->available = TRUE;
979 modem = g_try_new0(struct modem_data, 1);
983 modem->path = g_strdup(path);
984 modem->device = NULL;
985 modem->available = TRUE;
987 g_hash_table_insert(modem_hash, g_strdup(path), modem);
992 static gboolean modem_has_interface(DBusMessageIter *array,
993 char const *interface)
995 DBusMessageIter entry;
997 dbus_message_iter_recurse(array, &entry);
999 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1000 const char *element;
1002 dbus_message_iter_get_basic(&entry, &element);
1004 if (g_strcmp0(interface, element) == 0)
1007 dbus_message_iter_next(&entry);
1013 static gboolean modem_has_sim(DBusMessageIter *array)
1015 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1018 static gboolean modem_has_reg(DBusMessageIter *array)
1020 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1023 static gboolean modem_has_gprs(DBusMessageIter *array)
1025 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1028 static void modem_properties_reply(DBusPendingCall *call, void *user_data)
1032 DBusMessageIter array, dict;
1033 const char *path = user_data;
1034 dbus_bool_t powered = FALSE;
1035 dbus_bool_t online = FALSE;
1036 dbus_bool_t has_online = FALSE;
1037 gboolean has_sim = FALSE;
1038 gboolean has_reg = FALSE;
1039 gboolean has_gprs = FALSE;
1040 struct modem_data *new_modem;
1042 DBG("path %s", path);
1044 reply = dbus_pending_call_steal_reply(call);
1046 if (g_hash_table_lookup(modem_hash, path))
1049 dbus_error_init(&error);
1051 if (dbus_set_error_from_message(&error, reply)) {
1052 connman_error("Modem.GetProperties(%s) %s %s",
1053 path, error.name, error.message);
1054 dbus_error_free(&error);
1058 if (dbus_message_iter_init(reply, &array) == FALSE)
1061 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1064 dbus_message_iter_recurse(&array, &dict);
1066 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1067 DBusMessageIter entry, value;
1070 dbus_message_iter_recurse(&dict, &entry);
1071 dbus_message_iter_get_basic(&entry, &key);
1073 dbus_message_iter_next(&entry);
1074 dbus_message_iter_recurse(&entry, &value);
1076 if (g_str_equal(key, "Powered") == TRUE)
1077 dbus_message_iter_get_basic(&value, &powered);
1078 else if (g_str_equal(key, "Online") == TRUE) {
1080 dbus_message_iter_get_basic(&value, &online);
1081 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1082 has_sim = modem_has_sim(&value);
1083 has_reg = modem_has_reg(&value);
1084 has_gprs = modem_has_gprs(&value);
1087 dbus_message_iter_next(&dict);
1090 new_modem = add_modem(path);
1091 if (new_modem == NULL)
1095 modem_change_powered(path, TRUE);
1097 new_modem->has_sim = has_sim;
1098 new_modem->has_reg = has_reg;
1099 new_modem->has_gprs = has_gprs;
1101 update_modem_online(new_modem, online);
1107 dbus_message_unref(reply);
1109 dbus_pending_call_unref(call);
1112 static void get_modem_properties(const char *path)
1114 DBG("path %s", path);
1116 call_ofono(path, OFONO_MODEM_INTERFACE, GET_PROPERTIES,
1117 modem_properties_reply, g_strdup(path), g_free,
1121 static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
1123 struct modem_data *modem = value;
1125 modem->available = FALSE;
1128 static void modems_set_unavailable()
1130 g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
1133 static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
1135 struct modem_data *modem = value;
1137 if (modem->available == FALSE)
1138 g_hash_table_remove(modem_hash, key);
1141 static void cleanup_modems()
1143 g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
1146 static void update_modems(DBusMessageIter *array)
1148 DBusMessageIter entry;
1152 dbus_message_iter_recurse(array, &entry);
1154 modems_set_unavailable();
1156 while (dbus_message_iter_get_arg_type(&entry) ==
1157 DBUS_TYPE_OBJECT_PATH) {
1159 struct modem_data *modem;
1161 dbus_message_iter_get_basic(&entry, &path);
1163 modem = g_hash_table_lookup(modem_hash, path);
1166 modem->available = TRUE;
1168 get_modem_properties(path);
1170 dbus_message_iter_next(&entry);
1176 static void manager_properties_reply(DBusPendingCall *call, void *user_data)
1180 DBusMessageIter array, dict;
1184 reply = dbus_pending_call_steal_reply(call);
1186 dbus_error_init(&error);
1188 if (dbus_set_error_from_message(&error, reply)) {
1189 connman_error("ModemManager.GetProperties() %s %s",
1190 error.name, error.message);
1191 dbus_error_free(&error);
1195 if (dbus_message_iter_init(reply, &array) == FALSE)
1198 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1201 dbus_message_iter_recurse(&array, &dict);
1203 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1204 DBusMessageIter entry, value;
1207 dbus_message_iter_recurse(&dict, &entry);
1208 dbus_message_iter_get_basic(&entry, &key);
1210 dbus_message_iter_next(&entry);
1211 dbus_message_iter_recurse(&entry, &value);
1213 if (g_str_equal(key, "Modems") == TRUE) {
1214 update_modems(&value);
1218 dbus_message_iter_next(&dict);
1222 dbus_message_unref(reply);
1224 dbus_pending_call_unref(call);
1227 static void ofono_connect(DBusConnection *connection, void *user_data)
1229 DBG("connection %p", connection);
1231 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1232 g_free, remove_modem);
1234 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1235 NULL, remove_network);
1237 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_PROPERTIES,
1238 manager_properties_reply, NULL, NULL,
1242 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1244 DBG("connection %p", connection);
1246 if (modem_hash == NULL)
1249 g_hash_table_destroy(modem_hash);
1254 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1257 const char *path = dbus_message_get_path(message);
1258 struct modem_data *modem;
1259 DBusMessageIter iter, value;
1262 DBG("path %s", path);
1264 modem = g_hash_table_lookup(modem_hash, path);
1268 if (dbus_message_iter_init(message, &iter) == FALSE)
1271 dbus_message_iter_get_basic(&iter, &key);
1273 dbus_message_iter_next(&iter);
1274 dbus_message_iter_recurse(&iter, &value);
1276 if (g_str_equal(key, "Powered") == TRUE) {
1277 dbus_bool_t powered;
1279 dbus_message_iter_get_basic(&value, &powered);
1280 if (powered == TRUE)
1283 modem->has_sim = FALSE;
1284 modem->has_reg = FALSE;
1285 modem->has_gprs = FALSE;
1287 modem_remove_device(modem);
1288 } else if (g_str_equal(key, "Online") == TRUE) {
1291 dbus_message_iter_get_basic(&value, &online);
1293 update_modem_online(modem, online);
1294 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1295 gboolean has_sim = modem_has_sim(&value);
1296 gboolean has_reg = modem_has_reg(&value);
1297 gboolean added_reg = has_reg && !modem->has_reg;
1298 gboolean has_gprs = modem_has_gprs(&value);
1299 gboolean added_gprs = has_gprs && !modem->has_gprs;
1301 modem->has_sim = has_sim;
1302 modem->has_reg = has_reg;
1303 modem->has_gprs = has_gprs;
1305 if (modem->device == NULL) {
1307 get_imsi(modem->path);
1308 } else if (!has_sim) {
1309 modem_remove_device(modem);
1312 check_registration(modem);
1314 gprs_change_powered(modem->path, TRUE);
1321 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1324 const char *path = dbus_message_get_path(message);
1325 struct modem_data *modem;
1326 DBusMessageIter iter, value;
1329 DBG("path %s", path);
1331 modem = g_hash_table_lookup(modem_hash, path);
1335 if (dbus_message_iter_init(message, &iter) == FALSE)
1338 dbus_message_iter_get_basic(&iter, &key);
1340 dbus_message_iter_next(&iter);
1341 dbus_message_iter_recurse(&iter, &value);
1343 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1346 dbus_message_iter_get_basic(&value, &imsi);
1348 add_device(path, imsi);
1349 } else if (g_str_equal(key, "Present") == TRUE) {
1350 dbus_bool_t present;
1352 dbus_message_iter_get_basic(&value, &present);
1357 if (modem->device != NULL)
1358 modem_remove_device(modem);
1360 modem->has_gprs = FALSE;
1361 modem->has_reg = FALSE;
1367 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1370 const char *path = dbus_message_get_path(message);
1371 struct modem_data *modem;
1372 DBusMessageIter iter, value;
1375 DBG("path %s", path);
1377 modem = g_hash_table_lookup(modem_hash, path);
1381 if (dbus_message_iter_init(message, &iter) == FALSE)
1384 dbus_message_iter_get_basic(&iter, &key);
1386 dbus_message_iter_next(&iter);
1387 dbus_message_iter_recurse(&iter, &value);
1389 if (g_str_equal(key, "Attached") == TRUE) {
1390 dbus_bool_t attached;
1392 dbus_message_iter_get_basic(&value, &attached);
1394 DBG("Attached %d", attached);
1396 if (attached == TRUE)
1397 check_networks(modem);
1398 else if (modem->device != NULL)
1399 connman_device_remove_all_networks(modem->device);
1406 static gboolean manager_changed(DBusConnection *connection,
1407 DBusMessage *message, void *user_data)
1409 const char *path = dbus_message_get_path(message);
1410 DBusMessageIter iter, value;
1413 DBG("path %s", path);
1415 if (dbus_message_iter_init(message, &iter) == FALSE)
1418 dbus_message_iter_get_basic(&iter, &key);
1420 dbus_message_iter_next(&iter);
1421 dbus_message_iter_recurse(&iter, &value);
1423 if (g_str_equal(key, "Modems") == TRUE)
1424 update_modems(&value);
1429 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1431 DBusMessageIter entry;
1432 gchar *nameserver = NULL, *nameserver_old = NULL;
1436 dbus_message_iter_recurse(array, &entry);
1438 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1441 dbus_message_iter_get_basic(&entry, &dns);
1445 if (nameserver == NULL) {
1447 nameserver = g_strdup(dns);
1450 nameserver_old = nameserver;
1451 nameserver = g_strdup_printf("%s %s",
1452 nameserver_old, dns);
1453 g_free(nameserver_old);
1456 dbus_message_iter_next(&entry);
1459 parent->ipv4.nameserver = nameserver;
1462 static void update_settings(DBusMessageIter *array,
1463 struct connman_network *network)
1465 struct connman_element *parent = connman_network_get_element(network);
1466 DBusMessageIter dict;
1467 const char *interface = NULL;
1469 DBG("network %p", network);
1471 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1474 dbus_message_iter_recurse(array, &dict);
1476 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1477 DBusMessageIter entry, value;
1480 dbus_message_iter_recurse(&dict, &entry);
1481 dbus_message_iter_get_basic(&entry, &key);
1485 dbus_message_iter_next(&entry);
1486 dbus_message_iter_recurse(&entry, &value);
1488 if (g_str_equal(key, "Interface") == TRUE) {
1491 dbus_message_iter_get_basic(&value, &interface);
1493 DBG("interface %s", interface);
1495 index = connman_inet_ifindex(interface);
1497 connman_network_set_index(network, index);
1499 connman_error("Can not find interface %s",
1503 } else if (g_str_equal(key, "Method") == TRUE) {
1506 dbus_message_iter_get_basic(&value, &method);
1507 if (g_strcmp0(method, "static") == 0) {
1509 parent->ipv4.method =
1510 CONNMAN_IPCONFIG_METHOD_FIXED;
1511 } else if (g_strcmp0(method, "dhcp") == 0) {
1513 parent->ipv4.method =
1514 CONNMAN_IPCONFIG_METHOD_DHCP;
1517 } else if (g_str_equal(key, "Address") == TRUE) {
1518 const char *address;
1520 dbus_message_iter_get_basic(&value, &address);
1522 DBG("address %s", address);
1524 parent->ipv4.address = g_strdup(address);
1525 } else if (g_str_equal(key, "Netmask") == TRUE) {
1526 const char *netmask;
1528 dbus_message_iter_get_basic(&value, &netmask);
1530 DBG("netmask %s", netmask);
1532 parent->ipv4.netmask = g_strdup(netmask);
1533 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1535 get_dns(&value, parent);
1536 } else if (g_str_equal(key, "Gateway") == TRUE) {
1537 const char *gateway;
1539 dbus_message_iter_get_basic(&value, &gateway);
1541 DBG("gateway %s", gateway);
1543 parent->ipv4.gateway = g_strdup(gateway);
1546 dbus_message_iter_next(&dict);
1549 /* deactive, oFono send NULL inteface before deactive signal */
1550 if (interface == NULL)
1551 connman_network_set_index(network, -1);
1554 static void cleanup_ipconfig(struct connman_network *network)
1556 struct connman_element *parent = connman_network_get_element(network);
1558 g_free(parent->ipv4.address);
1559 parent->ipv4.address = NULL;
1561 g_free(parent->ipv4.netmask);
1562 parent->ipv4.netmask = NULL;
1564 g_free(parent->ipv4.nameserver);
1565 parent->ipv4.nameserver = NULL;
1567 g_free(parent->ipv4.gateway);
1568 parent->ipv4.gateway = NULL;
1570 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1574 static void set_connected(struct connman_network *network,
1575 connman_bool_t connected)
1577 struct connman_element *parent = connman_network_get_element(network);
1578 enum connman_ipconfig_method method = parent->ipv4.method;
1581 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1582 case CONNMAN_IPCONFIG_METHOD_OFF:
1583 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1586 case CONNMAN_IPCONFIG_METHOD_FIXED:
1587 connman_network_set_method(network, method);
1589 if (connected == FALSE)
1590 cleanup_ipconfig(network);
1592 connman_network_set_connected(network, connected);
1595 case CONNMAN_IPCONFIG_METHOD_DHCP:
1596 connman_network_set_method(network, method);
1598 connman_network_set_connected(network, connected);
1603 static gboolean context_changed(DBusConnection *connection,
1604 DBusMessage *message, void *user_data)
1606 const char *path = dbus_message_get_path(message);
1607 struct connman_network *network;
1608 DBusMessageIter iter, value;
1611 DBG("path %s", path);
1613 network = g_hash_table_lookup(network_hash, path);
1614 if (network == NULL)
1617 if (!pending_network_is_available(network)) {
1618 remove_network(network);
1622 if (dbus_message_iter_init(message, &iter) == FALSE)
1625 dbus_message_iter_get_basic(&iter, &key);
1627 dbus_message_iter_next(&iter);
1628 dbus_message_iter_recurse(&iter, &value);
1630 if (g_str_equal(key, "Settings") == TRUE)
1631 update_settings(&value, network);
1632 else if (g_str_equal(key, "Active") == TRUE) {
1635 dbus_message_iter_get_basic(&value, &active);
1637 set_connected(network, active);
1644 static guint reg_watch;
1645 static guint sim_watch;
1646 static guint gprs_watch;
1647 static guint modem_watch;
1648 static guint manager_watch;
1649 static guint context_watch;
1651 static int ofono_init(void)
1655 connection = connman_dbus_get_connection();
1656 if (connection == NULL)
1659 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1660 ofono_connect, ofono_disconnect, NULL, NULL);
1662 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1663 OFONO_REGISTRATION_INTERFACE,
1668 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1669 OFONO_GPRS_INTERFACE,
1674 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1675 OFONO_MODEM_INTERFACE,
1680 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1681 OFONO_SIM_INTERFACE,
1686 manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1687 OFONO_MANAGER_INTERFACE,
1692 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1693 OFONO_CONTEXT_INTERFACE,
1698 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1699 reg_watch == 0 || sim_watch == 0 ||
1700 manager_watch == 0 || context_watch == 0) {
1705 err = connman_network_driver_register(&network_driver);
1709 err = connman_device_driver_register(&modem_driver);
1711 connman_network_driver_unregister(&network_driver);
1718 g_dbus_remove_watch(connection, watch);
1719 g_dbus_remove_watch(connection, sim_watch);
1720 g_dbus_remove_watch(connection, reg_watch);
1721 g_dbus_remove_watch(connection, gprs_watch);
1722 g_dbus_remove_watch(connection, modem_watch);
1723 g_dbus_remove_watch(connection, manager_watch);
1724 g_dbus_remove_watch(connection, context_watch);
1726 dbus_connection_unref(connection);
1731 static void ofono_exit(void)
1733 g_dbus_remove_watch(connection, watch);
1734 g_dbus_remove_watch(connection, sim_watch);
1735 g_dbus_remove_watch(connection, reg_watch);
1736 g_dbus_remove_watch(connection, gprs_watch);
1737 g_dbus_remove_watch(connection, modem_watch);
1738 g_dbus_remove_watch(connection, manager_watch);
1739 g_dbus_remove_watch(connection, context_watch);
1741 ofono_disconnect(connection, NULL);
1743 connman_device_driver_unregister(&modem_driver);
1744 connman_network_driver_unregister(&network_driver);
1746 dbus_connection_unref(connection);
1749 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1750 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)