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
33 #define CONNMAN_API_SUBJECT_TO_CHANGE
34 #include <connman/plugin.h>
35 #include <connman/element.h>
36 #include <connman/device.h>
37 #include <connman/network.h>
38 #include <connman/dbus.h>
39 #include <connman/inet.h>
40 #include <connman/technology.h>
41 #include <connman/log.h>
45 #define OFONO_SERVICE "org.ofono"
47 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
48 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
49 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
50 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
51 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
52 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
54 #define PROPERTY_CHANGED "PropertyChanged"
55 #define GET_PROPERTIES "GetProperties"
56 #define SET_PROPERTY "SetProperty"
57 #define CONTEXT_ADDED "ContextAdded"
58 #define CONTEXT_REMOVED "ContextRemoved"
59 #define ADD_CONTEXT "AddContext"
60 #define GET_MODEMS "GetModems"
61 #define MODEM_ADDED "ModemAdded"
62 #define MODEM_REMOVED "ModemRemoved"
67 static DBusConnection *connection;
69 static GHashTable *modem_hash = NULL;
71 static GHashTable *network_hash;
75 struct connman_device *device;
80 gboolean pending_online;
81 gboolean requested_online;
84 uint8_t strength, has_strength;
87 static int modem_probe(struct connman_device *device)
89 DBG("device %p", device);
94 static void modem_remove(struct connman_device *device)
96 DBG("device %p", device);
99 static int call_ofono(const char *path,
100 const char *interface, const char *method,
101 DBusPendingCallNotifyFunction notify, void *user_data,
102 DBusFreeFunction free_function,
105 DBusMessage *message;
106 DBusPendingCall *call;
110 DBG("path %s %s.%s", path, interface, method);
115 message = dbus_message_new_method_call(OFONO_SERVICE, path,
120 dbus_message_set_auto_start(message, FALSE);
123 ok = dbus_message_append_args_valist(message, type, va);
129 if (dbus_connection_send_with_reply(connection, message,
130 &call, TIMEOUT) == FALSE) {
131 connman_error("Failed to call %s.%s", interface, method);
132 dbus_message_unref(message);
137 connman_error("D-Bus connection not available");
138 dbus_message_unref(message);
142 dbus_pending_call_set_notify(call, notify, user_data, free_function);
144 dbus_message_unref(message);
149 static void set_property_reply(DBusPendingCall *call, void *user_data)
153 char const *name = user_data;
157 dbus_error_init(&error);
159 reply = dbus_pending_call_steal_reply(call);
161 if (dbus_set_error_from_message(&error, reply)) {
162 connman_error("SetProperty(%s) %s %s", name,
163 error.name, error.message);
164 dbus_error_free(&error);
167 dbus_message_unref(reply);
169 dbus_pending_call_unref(call);
172 static int set_property(const char *path, const char *interface,
173 const char *property, int type, void *value,
174 DBusPendingCallNotifyFunction notify, void *user_data,
175 DBusFreeFunction free_function)
177 DBusMessage *message;
178 DBusMessageIter iter;
179 DBusPendingCall *call;
181 DBG("path %s %s.%s", path, interface, property);
183 g_assert(notify == NULL ? free_function == NULL : 1);
188 message = dbus_message_new_method_call(OFONO_SERVICE, path,
189 interface, SET_PROPERTY);
193 dbus_message_set_auto_start(message, FALSE);
195 dbus_message_iter_init_append(message, &iter);
196 connman_dbus_property_append_basic(&iter, property, type, value);
198 if (dbus_connection_send_with_reply(connection, message,
199 &call, TIMEOUT) == FALSE) {
200 connman_error("Failed to change \"%s\" property on %s",
201 property, interface);
202 dbus_message_unref(message);
207 connman_error("D-Bus connection not available");
208 dbus_message_unref(message);
212 if (notify == NULL) {
213 notify = set_property_reply;
214 user_data = (void *)property;
215 free_function = NULL;
218 dbus_pending_call_set_notify(call, notify, user_data, free_function);
220 dbus_message_unref(message);
225 static void update_modem_online(struct modem_data *modem,
226 connman_bool_t online)
228 DBG("modem %p path %s online %d", modem, modem->path, online);
230 modem->online = online;
231 modem->requested_online = online;
232 modem->pending_online = FALSE;
235 connman_device_set_powered(modem->device, online);
238 static void set_online_reply(DBusPendingCall *call, void *user_data)
240 struct modem_data *modem;
245 DBG("path %s", (char *)user_data);
247 if (modem_hash == NULL)
250 modem = g_hash_table_lookup(modem_hash, user_data);
254 reply = dbus_pending_call_steal_reply(call);
256 dbus_error_init(&error);
258 if (dbus_set_error_from_message(&error, reply)) {
259 connman_error("SetProperty(Online) %s %s",
260 error.name, error.message);
261 dbus_error_free(&error);
263 result = modem->online;
265 result = modem->requested_online;
267 if (modem->pending_online)
268 update_modem_online(modem, result);
270 dbus_message_unref(reply);
272 dbus_pending_call_unref(call);
275 static int modem_change_online(char const *path, dbus_bool_t online)
277 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
278 DBUS_TYPE_BOOLEAN, &online,
280 (void *)g_strdup(path), g_free);
283 static int modem_enable(struct connman_device *device)
285 const char *path = connman_device_get_string(device, "Path");
287 DBG("device %p, path, %s", device, path);
289 return modem_change_online(path, TRUE);
292 static int modem_disable(struct connman_device *device)
294 const char *path = connman_device_get_string(device, "Path");
296 DBG("device %p path %s", device, path);
298 return modem_change_online(path, FALSE);
301 static struct connman_device_driver modem_driver = {
303 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
304 .probe = modem_probe,
305 .remove = modem_remove,
306 .enable = modem_enable,
307 .disable = modem_disable,
310 static void modem_remove_device(struct modem_data *modem)
312 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
314 if (modem->device == NULL)
317 connman_device_remove_all_networks(modem->device);
318 connman_device_unregister(modem->device);
319 connman_device_unref(modem->device);
321 modem->device = NULL;
324 static void remove_modem(gpointer data)
326 struct modem_data *modem = data;
328 modem_remove_device(modem);
335 static void remove_network(gpointer data)
337 connman_network_unref(data);
340 static char *get_ident(const char *path)
347 pos = strrchr(path, '/');
354 static void create_service(struct connman_network *network)
361 path = connman_network_get_string(network, "Path");
363 group = get_ident(path);
365 connman_network_set_group(network, group);
368 static int network_probe(struct connman_network *network)
373 static gboolean pending_network_is_available(struct connman_network *network)
375 /* Modem or network may be removed */
376 if (network == NULL || connman_network_get_device(network) == NULL) {
377 DBG("Modem or network was removed");
384 static void set_active_reply(DBusPendingCall *call, void *user_data)
386 char const *path = user_data;
389 struct connman_network *network;
391 network = g_hash_table_lookup(network_hash, path);
393 DBG("path %s network %p", path, network);
395 reply = dbus_pending_call_steal_reply(call);
397 if (!pending_network_is_available(network))
400 dbus_error_init(&error);
402 if (dbus_set_error_from_message(&error, reply)) {
403 connman_error("SetProperty(Active) %s %s",
404 error.name, error.message);
406 if (connman_network_get_index(network) < 0)
407 connman_network_set_error(network,
408 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
410 dbus_error_free(&error);
414 dbus_message_unref(reply);
416 dbus_pending_call_unref(call);
419 static int set_network_active(struct connman_network *network,
424 const char *path = connman_network_get_string(network, "Path");
426 DBG("network %p, path %s, active %d", network, path, active);
428 error = set_property(path, OFONO_CONTEXT_INTERFACE,
429 "Active", DBUS_TYPE_BOOLEAN, &active,
430 set_active_reply, g_strdup(path), NULL);
432 if (active == FALSE && error == -EINPROGRESS)
438 static void set_apn(struct connman_network *network)
440 const char *apn, *path;
442 apn = connman_network_get_string(network, "Cellular.APN");
446 path = connman_network_get_string(network, "Path");
450 DBG("path %s, apn %s", path, apn);
452 set_property(path, OFONO_CONTEXT_INTERFACE,
453 "AccessPointName", DBUS_TYPE_STRING, &apn,
457 static int network_connect(struct connman_network *network)
459 DBG("network %p", network);
461 if (connman_network_get_index(network) >= 0)
464 return set_network_active(network, TRUE);
467 static int network_disconnect(struct connman_network *network)
469 DBG("network %p", network);
471 if (connman_network_get_index(network) < 0)
474 return set_network_active(network, FALSE);
477 static void network_remove(struct connman_network *network)
479 char const *path = connman_network_get_string(network, "Path");
481 DBG("network %p path %s", network, path);
483 g_hash_table_remove(network_hash, path);
486 static int network_setup(struct connman_network *network, const char *key)
488 if (g_strcmp0(key, "Cellular.APN") == 0)
494 static struct connman_network_driver network_driver = {
496 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
497 .probe = network_probe,
498 .remove = network_remove,
499 .connect = network_connect,
500 .disconnect = network_disconnect,
501 .setup = network_setup,
504 static void update_settings(DBusMessageIter *array,
505 struct connman_network *network);
507 static void set_connected(struct connman_network *network,
508 connman_bool_t connected);
510 static int add_network(struct connman_device *device,
511 const char *path, DBusMessageIter *dict)
513 struct modem_data *modem = connman_device_get_data(device);
514 struct connman_network *network;
516 const char *hash_path;
517 char const *operator;
518 char const *reg_status;
519 dbus_bool_t active = FALSE;
521 DBG("modem %p device %p path %s", modem, device, path);
523 ident = get_ident(path);
525 network = connman_device_get_network(device, ident);
529 network = g_hash_table_lookup(network_hash, path);
530 if (network != NULL) {
531 DBG("path %p already exists with device %p", path,
532 connman_network_get_device(network));
533 if (connman_network_get_device(network))
535 g_hash_table_remove(network_hash, path);
538 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
542 connman_network_set_string(network, "Path", path);
543 hash_path = connman_network_get_string(network, "Path");
544 if (hash_path == NULL)
547 create_service(network);
549 connman_network_ref(network);
550 g_hash_table_insert(network_hash, (char *)hash_path, network);
552 connman_network_set_available(network, TRUE);
553 connman_network_set_index(network, -1);
555 operator = connman_device_get_string(device, "Operator");
557 connman_network_set_name(network, operator);
559 if (modem->has_strength)
560 connman_network_set_strength(network, modem->strength);
562 reg_status = connman_device_get_string(device, "RegistrationStatus");
564 if (!g_strcmp0(reg_status, "roaming"))
565 connman_network_set_roaming(network, TRUE);
566 else if (!g_strcmp0(reg_status, "registered"))
567 connman_network_set_roaming(network, FALSE);
569 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
570 DBusMessageIter entry, value;
573 dbus_message_iter_recurse(dict, &entry);
574 dbus_message_iter_get_basic(&entry, &key);
576 dbus_message_iter_next(&entry);
577 dbus_message_iter_recurse(&entry, &value);
579 if (g_str_equal(key, "Type")) {
582 dbus_message_iter_get_basic(&value, &type);
583 if (g_strcmp0(type, "internet") != 0) {
584 DBG("path %p type %s", path, type);
587 } else if (g_str_equal(key, "AccessPointName")) {
590 dbus_message_iter_get_basic(&value, &ap);
592 connman_network_set_string(network, "Cellular.APN", ap);
593 } else if (g_str_equal(key, "Settings"))
594 update_settings(&value, network);
595 else if (g_str_equal(key, "Active") == TRUE)
596 dbus_message_iter_get_basic(&value, &active);
598 dbus_message_iter_next(dict);
601 if (connman_device_add_network(device, network) != 0)
605 set_connected(network, active);
610 connman_network_unref(network);
611 g_hash_table_remove(network_hash, hash_path);
615 static void check_networks_reply(DBusPendingCall *call, void *user_data)
617 char *path = user_data;
618 struct modem_data *modem;
620 DBusMessageIter array, entry, value, properties;
622 DBG("path %s", path);
624 modem = g_hash_table_lookup(modem_hash, path);
627 if (modem->device == NULL)
630 reply = dbus_pending_call_steal_reply(call);
632 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
635 dbus_message_iter_init(reply, &array);
637 dbus_message_iter_recurse(&array, &entry);
639 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
640 char const *network_path;
642 dbus_message_iter_recurse(&entry, &value);
643 dbus_message_iter_get_basic(&value, &network_path);
645 dbus_message_iter_next(&value);
646 dbus_message_iter_recurse(&value, &properties);
648 add_network(modem->device, network_path, &properties);
650 dbus_message_iter_next(&entry);
654 dbus_message_unref(reply);
656 dbus_pending_call_unref(call);
659 static void check_networks(struct modem_data *modem)
661 char const *path = modem->path;
663 DBG("modem %p path %s", modem, path);
665 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
666 check_networks_reply, g_strdup(path), g_free,
670 static void modem_operator_name_changed(struct modem_data *modem,
673 struct connman_device *device = modem->device;
680 connman_device_set_string(device, "Operator", name);
682 for (g_hash_table_iter_init(&i, network_hash);
683 g_hash_table_iter_next(&i, NULL, &value);) {
684 struct connman_network *network = value;
686 if (connman_network_get_device(network) == device) {
687 connman_network_set_name(network, name);
688 connman_network_update(network);
693 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
695 struct connman_device *device = modem->device;
699 modem->strength = strength;
700 modem->has_strength = TRUE;
705 for (g_hash_table_iter_init(&i, network_hash);
706 g_hash_table_iter_next(&i, NULL, &value);) {
707 struct connman_network *network = value;
709 if (connman_network_get_device(network) == device) {
710 connman_network_set_strength(network, strength);
711 connman_network_update(network);
716 static void modem_roaming_changed(struct modem_data *modem,
719 struct connman_device *device = modem->device;
720 connman_bool_t roaming;
727 connman_device_set_string(device, "RegistrationStatus", status);
729 if (g_str_equal(status, "roaming"))
731 else if (g_str_equal(status, "registered"))
736 for (g_hash_table_iter_init(&i, network_hash);
737 g_hash_table_iter_next(&i, NULL, &value);) {
738 struct connman_network *network = value;
740 if (connman_network_get_device(network) == device) {
741 connman_network_set_roaming(network, roaming);
742 connman_network_update(network);
747 static void modem_registration_changed(struct modem_data *modem,
748 DBusMessageIter *entry)
750 DBusMessageIter iter;
753 connman_uint8_t strength;
754 char const *name, *status, *mcc_s;
756 dbus_message_iter_get_basic(entry, &key);
760 dbus_message_iter_next(entry);
762 dbus_message_iter_recurse(entry, &iter);
764 type = dbus_message_iter_get_arg_type(&iter);
765 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
768 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
769 dbus_message_iter_get_basic(&iter, &name);
770 modem_operator_name_changed(modem, name);
771 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
772 dbus_message_iter_get_basic(&iter, &strength);
773 modem_strength_changed(modem, strength);
774 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
775 dbus_message_iter_get_basic(&iter, &status);
776 modem_roaming_changed(modem, status);
777 } else if (g_str_equal(key, "MobileCountryCode") &&
778 type == DBUS_TYPE_STRING) {
782 dbus_message_iter_get_basic(&iter, &mcc_s);
788 alpha2 = mcc_country_codes[mcc - 200];
789 connman_technology_set_regdom(alpha2);
794 static gboolean reg_changed(DBusConnection *connection,
795 DBusMessage *message, void *user_data)
797 const char *path = dbus_message_get_path(message);
798 struct modem_data *modem;
799 DBusMessageIter iter;
801 DBG("path %s", path);
803 modem = g_hash_table_lookup(modem_hash, path);
807 if (dbus_message_iter_init(message, &iter))
808 modem_registration_changed(modem, &iter);
813 static void check_registration_reply(DBusPendingCall *call, void *user_data)
815 char const *path = user_data;
816 struct modem_data *modem;
818 DBusMessageIter array, dict, entry;
820 DBG("path %s", path);
822 modem = g_hash_table_lookup(modem_hash, path);
826 reply = dbus_pending_call_steal_reply(call);
828 if (dbus_message_iter_init(reply, &array) == FALSE)
831 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
834 dbus_message_iter_recurse(&array, &dict);
835 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
836 dbus_message_iter_recurse(&dict, &entry);
837 modem_registration_changed(modem, &entry);
838 dbus_message_iter_next(&dict);
842 dbus_message_unref(reply);
844 dbus_pending_call_unref(call);
847 static void check_registration(struct modem_data *modem)
849 char const *path = modem->path;
851 DBG("modem %p path %s", modem, path);
853 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
854 check_registration_reply, g_strdup(path), g_free,
858 static void add_device(const char *path, const char *imsi)
860 struct modem_data *modem;
861 struct connman_device *device;
863 DBG("path %s imsi %s", path, imsi);
871 modem = g_hash_table_lookup(modem_hash, path);
876 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
879 modem_remove_device(modem);
882 if (strlen(imsi) == 0)
885 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
889 connman_device_set_ident(device, imsi);
891 connman_device_set_string(device, "Path", path);
893 connman_device_set_data(device, modem);
895 if (connman_device_register(device) < 0) {
896 connman_device_unref(device);
900 modem->device = device;
903 check_registration(modem);
905 check_networks(modem);
908 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
910 const char *path = user_data;
911 const char *imsi = NULL;
913 DBusMessageIter array, dict;
915 DBG("path %s", path);
917 reply = dbus_pending_call_steal_reply(call);
919 if (dbus_message_iter_init(reply, &array) == FALSE)
922 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
925 dbus_message_iter_recurse(&array, &dict);
927 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
928 DBusMessageIter entry, value;
931 dbus_message_iter_recurse(&dict, &entry);
932 dbus_message_iter_get_basic(&entry, &key);
934 dbus_message_iter_next(&entry);
935 dbus_message_iter_recurse(&entry, &value);
937 if (g_str_equal(key, "SubscriberIdentity")) {
938 dbus_message_iter_get_basic(&value, &imsi);
939 add_device(path, imsi);
942 dbus_message_iter_next(&dict);
946 dbus_message_unref(reply);
948 dbus_pending_call_unref(call);
951 static void get_imsi(const char *path)
953 DBG("path %s", path);
955 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
956 sim_properties_reply, g_strdup(path), g_free,
960 static int gprs_change_powered(const char *path, dbus_bool_t powered)
962 DBG("path %s powered %d", path, powered);
964 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
965 DBUS_TYPE_BOOLEAN, &powered,
969 static int modem_change_powered(const char *path, dbus_bool_t powered)
971 DBG("path %s powered %d", path, powered);
973 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
974 DBUS_TYPE_BOOLEAN, &powered,
979 static gboolean modem_has_interface(DBusMessageIter *array,
980 char const *interface)
982 DBusMessageIter entry;
984 dbus_message_iter_recurse(array, &entry);
986 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
989 dbus_message_iter_get_basic(&entry, &element);
991 if (g_strcmp0(interface, element) == 0)
994 dbus_message_iter_next(&entry);
1000 static gboolean modem_has_sim(DBusMessageIter *array)
1002 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1005 static gboolean modem_has_reg(DBusMessageIter *array)
1007 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1010 static gboolean modem_has_gprs(DBusMessageIter *array)
1012 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1015 static void add_modem(const char *path, DBusMessageIter *prop)
1017 struct modem_data *modem;
1018 dbus_bool_t powered = FALSE;
1019 dbus_bool_t online = FALSE;
1020 dbus_bool_t has_online = FALSE;
1021 dbus_bool_t locked = FALSE;
1022 gboolean has_sim = FALSE;
1023 gboolean has_reg = FALSE;
1024 gboolean has_gprs = FALSE;
1026 modem = g_hash_table_lookup(modem_hash, path);
1031 modem = g_try_new0(struct modem_data, 1);
1035 modem->path = g_strdup(path);
1036 modem->device = NULL;
1037 modem->available = TRUE;
1039 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1041 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1042 DBusMessageIter entry, value;
1045 dbus_message_iter_recurse(prop, &entry);
1046 dbus_message_iter_get_basic(&entry, &key);
1048 dbus_message_iter_next(&entry);
1049 dbus_message_iter_recurse(&entry, &value);
1051 if (g_str_equal(key, "Powered") == TRUE)
1052 dbus_message_iter_get_basic(&value, &powered);
1053 else if (g_str_equal(key, "Lockdown") == TRUE)
1054 dbus_message_iter_get_basic(&value, &locked);
1055 else if (g_str_equal(key, "Online") == TRUE) {
1057 dbus_message_iter_get_basic(&value, &online);
1058 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1059 has_sim = modem_has_sim(&value);
1060 has_reg = modem_has_reg(&value);
1061 has_gprs = modem_has_gprs(&value);
1064 dbus_message_iter_next(prop);
1071 modem_change_powered(path, TRUE);
1073 modem->has_sim = has_sim;
1074 modem->has_reg = has_reg;
1075 modem->has_gprs = has_gprs;
1077 update_modem_online(modem, online);
1083 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1087 DBusMessageIter array, dict;
1091 reply = dbus_pending_call_steal_reply(call);
1093 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1096 dbus_error_init(&error);
1098 if (dbus_set_error_from_message(&error, reply)) {
1099 connman_error("ModemManager.GetModems() %s %s",
1100 error.name, error.message);
1101 dbus_error_free(&error);
1105 if (dbus_message_iter_init(reply, &array) == FALSE)
1108 dbus_message_iter_recurse(&array, &dict);
1110 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1111 DBusMessageIter value, properties;
1112 const char *modem_path;
1114 dbus_message_iter_recurse(&dict, &value);
1115 dbus_message_iter_get_basic(&value, &modem_path);
1117 dbus_message_iter_next(&value);
1118 dbus_message_iter_recurse(&value, &properties);
1121 add_modem(modem_path, &properties);
1123 dbus_message_iter_next(&dict);
1127 dbus_message_unref(reply);
1129 dbus_pending_call_unref(call);
1132 static void ofono_connect(DBusConnection *connection, void *user_data)
1134 DBG("connection %p", connection);
1136 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1137 g_free, remove_modem);
1139 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1140 NULL, remove_network);
1142 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1143 manager_modems_reply, NULL, NULL,
1147 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1149 DBG("connection %p", connection);
1151 if (modem_hash == NULL)
1154 g_hash_table_destroy(modem_hash);
1159 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1162 const char *path = dbus_message_get_path(message);
1163 struct modem_data *modem;
1164 DBusMessageIter iter, value;
1167 DBG("path %s", path);
1169 modem = g_hash_table_lookup(modem_hash, path);
1173 if (dbus_message_iter_init(message, &iter) == FALSE)
1176 dbus_message_iter_get_basic(&iter, &key);
1178 dbus_message_iter_next(&iter);
1179 dbus_message_iter_recurse(&iter, &value);
1181 if (g_str_equal(key, "Powered") == TRUE) {
1182 dbus_bool_t powered;
1184 dbus_message_iter_get_basic(&value, &powered);
1185 if (powered == TRUE)
1188 modem->has_sim = FALSE;
1189 modem->has_reg = FALSE;
1190 modem->has_gprs = FALSE;
1192 modem_remove_device(modem);
1193 } else if (g_str_equal(key, "Online") == TRUE) {
1196 dbus_message_iter_get_basic(&value, &online);
1198 update_modem_online(modem, online);
1199 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1202 dbus_message_iter_get_basic(&value, &locked);
1205 modem_change_powered(path, TRUE);
1207 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1208 gboolean has_sim = modem_has_sim(&value);
1209 gboolean has_reg = modem_has_reg(&value);
1210 gboolean added_reg = has_reg && !modem->has_reg;
1211 gboolean has_gprs = modem_has_gprs(&value);
1212 gboolean added_gprs = has_gprs && !modem->has_gprs;
1214 modem->has_sim = has_sim;
1215 modem->has_reg = has_reg;
1216 modem->has_gprs = has_gprs;
1218 if (modem->device == NULL) {
1220 get_imsi(modem->path);
1221 } else if (!has_sim) {
1222 modem_remove_device(modem);
1225 check_registration(modem);
1227 gprs_change_powered(modem->path, TRUE);
1234 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1237 const char *path = dbus_message_get_path(message);
1238 struct modem_data *modem;
1239 DBusMessageIter iter, value;
1242 DBG("path %s", path);
1244 modem = g_hash_table_lookup(modem_hash, path);
1248 if (dbus_message_iter_init(message, &iter) == FALSE)
1251 dbus_message_iter_get_basic(&iter, &key);
1253 dbus_message_iter_next(&iter);
1254 dbus_message_iter_recurse(&iter, &value);
1256 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1259 dbus_message_iter_get_basic(&value, &imsi);
1261 add_device(path, imsi);
1262 } else if (g_str_equal(key, "Present") == TRUE) {
1263 dbus_bool_t present;
1265 dbus_message_iter_get_basic(&value, &present);
1270 if (modem->device != NULL)
1271 modem_remove_device(modem);
1273 modem->has_gprs = FALSE;
1274 modem->has_reg = FALSE;
1280 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1283 const char *path = dbus_message_get_path(message);
1284 struct modem_data *modem;
1285 DBusMessageIter iter, value;
1288 DBG("path %s", path);
1290 modem = g_hash_table_lookup(modem_hash, path);
1294 if (dbus_message_iter_init(message, &iter) == FALSE)
1297 dbus_message_iter_get_basic(&iter, &key);
1299 dbus_message_iter_next(&iter);
1300 dbus_message_iter_recurse(&iter, &value);
1302 if (g_str_equal(key, "Attached") == TRUE) {
1303 dbus_bool_t attached;
1305 dbus_message_iter_get_basic(&value, &attached);
1307 DBG("Attached %d", attached);
1309 if (attached == TRUE)
1310 check_networks(modem);
1311 else if (modem->device != NULL)
1312 connman_device_remove_all_networks(modem->device);
1319 static gboolean modem_added(DBusConnection *connection,
1320 DBusMessage *message, void *user_data)
1322 DBusMessageIter iter, properties;
1323 const char *modem_path;
1327 if (dbus_message_iter_init(message, &iter) == FALSE)
1330 dbus_message_iter_get_basic(&iter, &modem_path);
1332 dbus_message_iter_next(&iter);
1333 dbus_message_iter_recurse(&iter, &properties);
1335 add_modem(modem_path, &properties);
1340 static gboolean modem_removed(DBusConnection *connection,
1341 DBusMessage *message, void *user_data)
1343 DBusMessageIter iter;
1344 const char *modem_path;
1348 if (dbus_message_iter_init(message, &iter) == FALSE)
1351 dbus_message_iter_get_basic(&iter, &modem_path);
1353 g_hash_table_remove(modem_hash, modem_path);
1359 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1361 DBusMessageIter entry;
1362 gchar *nameserver = NULL, *nameserver_old = NULL;
1366 dbus_message_iter_recurse(array, &entry);
1368 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1371 dbus_message_iter_get_basic(&entry, &dns);
1375 if (nameserver == NULL) {
1377 nameserver = g_strdup(dns);
1380 nameserver_old = nameserver;
1381 nameserver = g_strdup_printf("%s %s",
1382 nameserver_old, dns);
1383 g_free(nameserver_old);
1386 dbus_message_iter_next(&entry);
1389 parent->ipv4.nameserver = nameserver;
1392 static void update_settings(DBusMessageIter *array,
1393 struct connman_network *network)
1395 struct connman_element *parent = connman_network_get_element(network);
1396 DBusMessageIter dict;
1397 const char *interface = NULL;
1399 DBG("network %p", network);
1401 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1404 dbus_message_iter_recurse(array, &dict);
1406 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1407 DBusMessageIter entry, value;
1410 dbus_message_iter_recurse(&dict, &entry);
1411 dbus_message_iter_get_basic(&entry, &key);
1415 dbus_message_iter_next(&entry);
1416 dbus_message_iter_recurse(&entry, &value);
1418 if (g_str_equal(key, "Interface") == TRUE) {
1421 dbus_message_iter_get_basic(&value, &interface);
1423 DBG("interface %s", interface);
1425 index = connman_inet_ifindex(interface);
1427 connman_network_set_index(network, index);
1429 connman_error("Can not find interface %s",
1433 } else if (g_str_equal(key, "Method") == TRUE) {
1436 dbus_message_iter_get_basic(&value, &method);
1437 if (g_strcmp0(method, "static") == 0) {
1439 parent->ipv4.method =
1440 CONNMAN_IPCONFIG_METHOD_FIXED;
1441 } else if (g_strcmp0(method, "dhcp") == 0) {
1443 parent->ipv4.method =
1444 CONNMAN_IPCONFIG_METHOD_DHCP;
1447 } else if (g_str_equal(key, "Address") == TRUE) {
1448 const char *address;
1450 dbus_message_iter_get_basic(&value, &address);
1452 DBG("address %s", address);
1454 parent->ipv4.address = g_strdup(address);
1455 } else if (g_str_equal(key, "Netmask") == TRUE) {
1456 const char *netmask;
1458 dbus_message_iter_get_basic(&value, &netmask);
1460 DBG("netmask %s", netmask);
1462 parent->ipv4.netmask = g_strdup(netmask);
1463 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1465 get_dns(&value, parent);
1466 } else if (g_str_equal(key, "Gateway") == TRUE) {
1467 const char *gateway;
1469 dbus_message_iter_get_basic(&value, &gateway);
1471 DBG("gateway %s", gateway);
1473 parent->ipv4.gateway = g_strdup(gateway);
1476 dbus_message_iter_next(&dict);
1479 /* deactive, oFono send NULL inteface before deactive signal */
1480 if (interface == NULL)
1481 connman_network_set_index(network, -1);
1484 static void cleanup_ipconfig(struct connman_network *network)
1486 struct connman_element *parent = connman_network_get_element(network);
1488 g_free(parent->ipv4.address);
1489 parent->ipv4.address = NULL;
1491 g_free(parent->ipv4.netmask);
1492 parent->ipv4.netmask = NULL;
1494 g_free(parent->ipv4.nameserver);
1495 parent->ipv4.nameserver = NULL;
1497 g_free(parent->ipv4.gateway);
1498 parent->ipv4.gateway = NULL;
1500 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1504 static void set_connected(struct connman_network *network,
1505 connman_bool_t connected)
1507 struct connman_element *parent = connman_network_get_element(network);
1508 enum connman_ipconfig_method method = parent->ipv4.method;
1511 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1512 case CONNMAN_IPCONFIG_METHOD_OFF:
1513 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1516 case CONNMAN_IPCONFIG_METHOD_FIXED:
1517 connman_network_set_ipv4_method(network, method);
1519 connman_network_set_connected(network, connected);
1521 if (connected == FALSE)
1522 cleanup_ipconfig(network);
1526 case CONNMAN_IPCONFIG_METHOD_DHCP:
1527 connman_network_set_ipv4_method(network, method);
1529 connman_network_set_connected(network, connected);
1534 static gboolean context_changed(DBusConnection *connection,
1535 DBusMessage *message, void *user_data)
1537 const char *path = dbus_message_get_path(message);
1538 struct connman_network *network;
1539 DBusMessageIter iter, value;
1542 DBG("path %s", path);
1544 network = g_hash_table_lookup(network_hash, path);
1545 if (network == NULL)
1548 if (!pending_network_is_available(network)) {
1549 remove_network(network);
1553 if (dbus_message_iter_init(message, &iter) == FALSE)
1556 dbus_message_iter_get_basic(&iter, &key);
1558 dbus_message_iter_next(&iter);
1559 dbus_message_iter_recurse(&iter, &value);
1561 if (g_str_equal(key, "Settings") == TRUE)
1562 update_settings(&value, network);
1563 else if (g_str_equal(key, "Active") == TRUE) {
1566 dbus_message_iter_get_basic(&value, &active);
1568 set_connected(network, active);
1575 static guint reg_watch;
1576 static guint sim_watch;
1577 static guint gprs_watch;
1578 static guint modem_watch;
1579 static guint modem_added_watch;
1580 static guint modem_removed_watch;
1581 static guint context_watch;
1583 static int ofono_init(void)
1587 connection = connman_dbus_get_connection();
1588 if (connection == NULL)
1591 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1592 ofono_connect, ofono_disconnect, NULL, NULL);
1594 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1595 OFONO_REGISTRATION_INTERFACE,
1600 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1601 OFONO_GPRS_INTERFACE,
1606 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1607 OFONO_MODEM_INTERFACE,
1612 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1613 OFONO_SIM_INTERFACE,
1618 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1619 OFONO_MANAGER_INTERFACE,
1624 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1625 OFONO_MANAGER_INTERFACE,
1630 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1631 OFONO_CONTEXT_INTERFACE,
1636 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1637 reg_watch == 0 || sim_watch == 0 ||
1638 modem_added_watch == 0 || modem_removed_watch == 0 ||
1639 context_watch == 0) {
1644 err = connman_network_driver_register(&network_driver);
1648 err = connman_device_driver_register(&modem_driver);
1650 connman_network_driver_unregister(&network_driver);
1657 g_dbus_remove_watch(connection, watch);
1658 g_dbus_remove_watch(connection, sim_watch);
1659 g_dbus_remove_watch(connection, reg_watch);
1660 g_dbus_remove_watch(connection, gprs_watch);
1661 g_dbus_remove_watch(connection, modem_watch);
1662 g_dbus_remove_watch(connection, modem_added_watch);
1663 g_dbus_remove_watch(connection, modem_removed_watch);
1664 g_dbus_remove_watch(connection, context_watch);
1666 dbus_connection_unref(connection);
1671 static void ofono_exit(void)
1673 g_dbus_remove_watch(connection, watch);
1674 g_dbus_remove_watch(connection, sim_watch);
1675 g_dbus_remove_watch(connection, reg_watch);
1676 g_dbus_remove_watch(connection, gprs_watch);
1677 g_dbus_remove_watch(connection, modem_watch);
1678 g_dbus_remove_watch(connection, modem_added_watch);
1679 g_dbus_remove_watch(connection, modem_removed_watch);
1680 g_dbus_remove_watch(connection, context_watch);
1682 ofono_disconnect(connection, NULL);
1684 connman_device_driver_unregister(&modem_driver);
1685 connman_network_driver_unregister(&network_driver);
1687 dbus_connection_unref(connection);
1690 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1691 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)