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 dbus_bool_t 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 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
282 if (modem->online == online)
285 modem->requested_online = online;
287 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
288 DBUS_TYPE_BOOLEAN, &online,
290 (void *)g_strdup(path), g_free);
293 static int modem_enable(struct connman_device *device)
295 const char *path = connman_device_get_string(device, "Path");
297 DBG("device %p, path, %s", device, path);
299 return modem_change_online(path, TRUE);
302 static int modem_disable(struct connman_device *device)
304 const char *path = connman_device_get_string(device, "Path");
306 DBG("device %p path %s", device, path);
308 return modem_change_online(path, FALSE);
311 static struct connman_device_driver modem_driver = {
313 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
314 .probe = modem_probe,
315 .remove = modem_remove,
316 .enable = modem_enable,
317 .disable = modem_disable,
320 static void modem_remove_device(struct modem_data *modem)
322 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
324 if (modem->device == NULL)
327 connman_device_remove_all_networks(modem->device);
328 connman_device_unregister(modem->device);
329 connman_device_unref(modem->device);
331 modem->device = NULL;
334 static void remove_modem(gpointer data)
336 struct modem_data *modem = data;
338 modem_remove_device(modem);
345 static void remove_network(gpointer data)
347 connman_network_unref(data);
350 static char *get_ident(const char *path)
357 pos = strrchr(path, '/');
364 static void create_service(struct connman_network *network)
371 path = connman_network_get_string(network, "Path");
373 group = get_ident(path);
375 connman_network_set_group(network, group);
378 static int network_probe(struct connman_network *network)
383 static gboolean pending_network_is_available(struct connman_network *network)
385 /* Modem or network may be removed */
386 if (network == NULL || connman_network_get_device(network) == NULL) {
387 DBG("Modem or network was removed");
394 static void set_active_reply(DBusPendingCall *call, void *user_data)
396 char const *path = user_data;
399 struct connman_network *network;
401 network = g_hash_table_lookup(network_hash, path);
403 DBG("path %s network %p", path, network);
405 reply = dbus_pending_call_steal_reply(call);
407 if (!pending_network_is_available(network))
410 dbus_error_init(&error);
412 if (dbus_set_error_from_message(&error, reply)) {
413 connman_error("SetProperty(Active) %s %s",
414 error.name, error.message);
416 if (connman_network_get_index(network) < 0)
417 connman_network_set_error(network,
418 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
420 dbus_error_free(&error);
424 dbus_message_unref(reply);
426 dbus_pending_call_unref(call);
429 static int set_network_active(struct connman_network *network,
434 const char *path = connman_network_get_string(network, "Path");
436 DBG("network %p, path %s, active %d", network, path, active);
438 error = set_property(path, OFONO_CONTEXT_INTERFACE,
439 "Active", DBUS_TYPE_BOOLEAN, &active,
440 set_active_reply, g_strdup(path), NULL);
442 if (active == FALSE && error == -EINPROGRESS)
448 static void set_apn(struct connman_network *network)
450 const char *apn, *path;
452 apn = connman_network_get_string(network, "Cellular.APN");
456 path = connman_network_get_string(network, "Path");
460 DBG("path %s, apn %s", path, apn);
462 set_property(path, OFONO_CONTEXT_INTERFACE,
463 "AccessPointName", DBUS_TYPE_STRING, &apn,
467 static int network_connect(struct connman_network *network)
469 DBG("network %p", network);
471 if (connman_network_get_index(network) >= 0)
474 return set_network_active(network, TRUE);
477 static int network_disconnect(struct connman_network *network)
479 DBG("network %p", network);
481 if (connman_network_get_index(network) < 0)
484 return set_network_active(network, FALSE);
487 static void network_remove(struct connman_network *network)
489 char const *path = connman_network_get_string(network, "Path");
491 DBG("network %p path %s", network, path);
493 g_hash_table_remove(network_hash, path);
496 static int network_setup(struct connman_network *network, const char *key)
498 if (g_strcmp0(key, "Cellular.APN") == 0)
504 static struct connman_network_driver network_driver = {
506 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
507 .probe = network_probe,
508 .remove = network_remove,
509 .connect = network_connect,
510 .disconnect = network_disconnect,
511 .setup = network_setup,
514 static void update_settings(DBusMessageIter *array,
515 struct connman_network *network);
517 static void set_connected(struct connman_network *network,
518 connman_bool_t connected);
520 static int add_network(struct connman_device *device,
521 const char *path, DBusMessageIter *dict)
523 struct modem_data *modem = connman_device_get_data(device);
524 struct connman_network *network;
526 const char *hash_path;
527 char const *operator;
528 char const *reg_status;
529 dbus_bool_t active = FALSE;
531 DBG("modem %p device %p path %s", modem, device, path);
533 ident = get_ident(path);
535 network = connman_device_get_network(device, ident);
539 network = g_hash_table_lookup(network_hash, path);
540 if (network != NULL) {
541 DBG("path %p already exists with device %p", path,
542 connman_network_get_device(network));
543 if (connman_network_get_device(network))
545 g_hash_table_remove(network_hash, path);
548 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
552 connman_network_set_string(network, "Path", path);
553 hash_path = connman_network_get_string(network, "Path");
554 if (hash_path == NULL)
557 create_service(network);
559 connman_network_ref(network);
560 g_hash_table_insert(network_hash, (char *)hash_path, network);
562 connman_network_set_available(network, TRUE);
563 connman_network_set_index(network, -1);
565 operator = connman_device_get_string(device, "Operator");
567 connman_network_set_name(network, operator);
569 if (modem->has_strength)
570 connman_network_set_strength(network, modem->strength);
572 reg_status = connman_device_get_string(device, "RegistrationStatus");
574 if (!g_strcmp0(reg_status, "roaming"))
575 connman_network_set_roaming(network, TRUE);
576 else if (!g_strcmp0(reg_status, "registered"))
577 connman_network_set_roaming(network, FALSE);
579 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
580 DBusMessageIter entry, value;
583 dbus_message_iter_recurse(dict, &entry);
584 dbus_message_iter_get_basic(&entry, &key);
586 dbus_message_iter_next(&entry);
587 dbus_message_iter_recurse(&entry, &value);
589 if (g_str_equal(key, "Type")) {
592 dbus_message_iter_get_basic(&value, &type);
593 if (g_strcmp0(type, "internet") != 0) {
594 DBG("path %p type %s", path, type);
597 } else if (g_str_equal(key, "AccessPointName")) {
600 dbus_message_iter_get_basic(&value, &ap);
602 connman_network_set_string(network, "Cellular.APN", ap);
603 } else if (g_str_equal(key, "Settings"))
604 update_settings(&value, network);
605 else if (g_str_equal(key, "Active") == TRUE)
606 dbus_message_iter_get_basic(&value, &active);
608 dbus_message_iter_next(dict);
611 if (connman_device_add_network(device, network) != 0)
615 set_connected(network, active);
620 connman_network_unref(network);
621 g_hash_table_remove(network_hash, hash_path);
625 static void check_networks_reply(DBusPendingCall *call, void *user_data)
627 char *path = user_data;
628 struct modem_data *modem;
630 DBusMessageIter array, entry, value, properties;
632 DBG("path %s", path);
634 modem = g_hash_table_lookup(modem_hash, path);
637 if (modem->device == NULL)
640 reply = dbus_pending_call_steal_reply(call);
642 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
645 dbus_message_iter_init(reply, &array);
647 dbus_message_iter_recurse(&array, &entry);
649 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
650 char const *network_path;
652 dbus_message_iter_recurse(&entry, &value);
653 dbus_message_iter_get_basic(&value, &network_path);
655 dbus_message_iter_next(&value);
656 dbus_message_iter_recurse(&value, &properties);
658 add_network(modem->device, network_path, &properties);
660 dbus_message_iter_next(&entry);
664 dbus_message_unref(reply);
666 dbus_pending_call_unref(call);
669 static void check_networks(struct modem_data *modem)
671 char const *path = modem->path;
673 DBG("modem %p path %s", modem, path);
675 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
676 check_networks_reply, g_strdup(path), g_free,
680 static void modem_operator_name_changed(struct modem_data *modem,
683 struct connman_device *device = modem->device;
690 connman_device_set_string(device, "Operator", name);
692 for (g_hash_table_iter_init(&i, network_hash);
693 g_hash_table_iter_next(&i, NULL, &value);) {
694 struct connman_network *network = value;
696 if (connman_network_get_device(network) == device) {
697 connman_network_set_name(network, name);
698 connman_network_update(network);
703 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
705 struct connman_device *device = modem->device;
709 modem->strength = strength;
710 modem->has_strength = TRUE;
715 for (g_hash_table_iter_init(&i, network_hash);
716 g_hash_table_iter_next(&i, NULL, &value);) {
717 struct connman_network *network = value;
719 if (connman_network_get_device(network) == device) {
720 connman_network_set_strength(network, strength);
721 connman_network_update(network);
726 static void modem_roaming_changed(struct modem_data *modem,
729 struct connman_device *device = modem->device;
730 connman_bool_t roaming;
737 connman_device_set_string(device, "RegistrationStatus", status);
739 if (g_str_equal(status, "roaming"))
741 else if (g_str_equal(status, "registered"))
746 for (g_hash_table_iter_init(&i, network_hash);
747 g_hash_table_iter_next(&i, NULL, &value);) {
748 struct connman_network *network = value;
750 if (connman_network_get_device(network) == device) {
751 connman_network_set_roaming(network, roaming);
752 connman_network_update(network);
757 static void modem_registration_changed(struct modem_data *modem,
758 DBusMessageIter *entry)
760 DBusMessageIter iter;
763 connman_uint8_t strength;
764 char const *name, *status, *mcc_s;
766 dbus_message_iter_get_basic(entry, &key);
770 dbus_message_iter_next(entry);
772 dbus_message_iter_recurse(entry, &iter);
774 type = dbus_message_iter_get_arg_type(&iter);
775 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
778 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
779 dbus_message_iter_get_basic(&iter, &name);
780 modem_operator_name_changed(modem, name);
781 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
782 dbus_message_iter_get_basic(&iter, &strength);
783 modem_strength_changed(modem, strength);
784 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
785 dbus_message_iter_get_basic(&iter, &status);
786 modem_roaming_changed(modem, status);
787 } else if (g_str_equal(key, "MobileCountryCode") &&
788 type == DBUS_TYPE_STRING) {
792 dbus_message_iter_get_basic(&iter, &mcc_s);
798 alpha2 = mcc_country_codes[mcc - 200];
799 connman_technology_set_regdom(alpha2);
804 static gboolean reg_changed(DBusConnection *connection,
805 DBusMessage *message, void *user_data)
807 const char *path = dbus_message_get_path(message);
808 struct modem_data *modem;
809 DBusMessageIter iter;
811 DBG("path %s", path);
813 modem = g_hash_table_lookup(modem_hash, path);
817 if (dbus_message_iter_init(message, &iter))
818 modem_registration_changed(modem, &iter);
823 static void check_registration_reply(DBusPendingCall *call, void *user_data)
825 char const *path = user_data;
826 struct modem_data *modem;
828 DBusMessageIter array, dict, entry;
830 DBG("path %s", path);
832 modem = g_hash_table_lookup(modem_hash, path);
836 reply = dbus_pending_call_steal_reply(call);
838 if (dbus_message_iter_init(reply, &array) == FALSE)
841 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
844 dbus_message_iter_recurse(&array, &dict);
845 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
846 dbus_message_iter_recurse(&dict, &entry);
847 modem_registration_changed(modem, &entry);
848 dbus_message_iter_next(&dict);
852 dbus_message_unref(reply);
854 dbus_pending_call_unref(call);
857 static void check_registration(struct modem_data *modem)
859 char const *path = modem->path;
861 DBG("modem %p path %s", modem, path);
863 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
864 check_registration_reply, g_strdup(path), g_free,
868 static void add_device(const char *path, const char *imsi)
870 struct modem_data *modem;
871 struct connman_device *device;
873 DBG("path %s imsi %s", path, imsi);
881 modem = g_hash_table_lookup(modem_hash, path);
886 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
889 modem_remove_device(modem);
892 if (strlen(imsi) == 0)
895 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
899 connman_device_set_ident(device, imsi);
901 connman_device_set_string(device, "Path", path);
903 connman_device_set_data(device, modem);
905 if (connman_device_register(device) < 0) {
906 connman_device_unref(device);
910 modem->device = device;
913 check_registration(modem);
915 check_networks(modem);
918 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
920 const char *path = user_data;
921 const char *imsi = NULL;
923 DBusMessageIter array, dict;
925 DBG("path %s", path);
927 reply = dbus_pending_call_steal_reply(call);
929 if (dbus_message_iter_init(reply, &array) == FALSE)
932 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
935 dbus_message_iter_recurse(&array, &dict);
937 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
938 DBusMessageIter entry, value;
941 dbus_message_iter_recurse(&dict, &entry);
942 dbus_message_iter_get_basic(&entry, &key);
944 dbus_message_iter_next(&entry);
945 dbus_message_iter_recurse(&entry, &value);
947 if (g_str_equal(key, "SubscriberIdentity")) {
948 dbus_message_iter_get_basic(&value, &imsi);
949 add_device(path, imsi);
952 dbus_message_iter_next(&dict);
956 dbus_message_unref(reply);
958 dbus_pending_call_unref(call);
961 static void get_imsi(const char *path)
963 DBG("path %s", path);
965 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
966 sim_properties_reply, g_strdup(path), g_free,
970 static int gprs_change_powered(const char *path, dbus_bool_t powered)
972 DBG("path %s powered %d", path, powered);
974 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
975 DBUS_TYPE_BOOLEAN, &powered,
979 static int modem_change_powered(const char *path, dbus_bool_t powered)
981 DBG("path %s powered %d", path, powered);
983 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
984 DBUS_TYPE_BOOLEAN, &powered,
989 static gboolean modem_has_interface(DBusMessageIter *array,
990 char const *interface)
992 DBusMessageIter entry;
994 dbus_message_iter_recurse(array, &entry);
996 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
999 dbus_message_iter_get_basic(&entry, &element);
1001 if (g_strcmp0(interface, element) == 0)
1004 dbus_message_iter_next(&entry);
1010 static gboolean modem_has_sim(DBusMessageIter *array)
1012 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1015 static gboolean modem_has_reg(DBusMessageIter *array)
1017 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1020 static gboolean modem_has_gprs(DBusMessageIter *array)
1022 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1025 static void add_modem(const char *path, DBusMessageIter *prop)
1027 struct modem_data *modem;
1028 dbus_bool_t powered = FALSE;
1029 dbus_bool_t online = FALSE;
1030 dbus_bool_t has_online = FALSE;
1031 dbus_bool_t locked = FALSE;
1032 gboolean has_sim = FALSE;
1033 gboolean has_reg = FALSE;
1034 gboolean has_gprs = FALSE;
1036 modem = g_hash_table_lookup(modem_hash, path);
1041 modem = g_try_new0(struct modem_data, 1);
1045 modem->path = g_strdup(path);
1046 modem->device = NULL;
1047 modem->available = TRUE;
1049 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1051 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1052 DBusMessageIter entry, value;
1055 dbus_message_iter_recurse(prop, &entry);
1056 dbus_message_iter_get_basic(&entry, &key);
1058 dbus_message_iter_next(&entry);
1059 dbus_message_iter_recurse(&entry, &value);
1061 if (g_str_equal(key, "Powered") == TRUE)
1062 dbus_message_iter_get_basic(&value, &powered);
1063 else if (g_str_equal(key, "Lockdown") == TRUE)
1064 dbus_message_iter_get_basic(&value, &locked);
1065 else if (g_str_equal(key, "Online") == TRUE) {
1067 dbus_message_iter_get_basic(&value, &online);
1068 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1069 has_sim = modem_has_sim(&value);
1070 has_reg = modem_has_reg(&value);
1071 has_gprs = modem_has_gprs(&value);
1074 dbus_message_iter_next(prop);
1081 modem_change_powered(path, TRUE);
1083 modem->has_sim = has_sim;
1084 modem->has_reg = has_reg;
1085 modem->has_gprs = has_gprs;
1087 update_modem_online(modem, online);
1093 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1097 DBusMessageIter array, dict;
1101 reply = dbus_pending_call_steal_reply(call);
1103 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1106 dbus_error_init(&error);
1108 if (dbus_set_error_from_message(&error, reply)) {
1109 connman_error("ModemManager.GetModems() %s %s",
1110 error.name, error.message);
1111 dbus_error_free(&error);
1115 if (dbus_message_iter_init(reply, &array) == FALSE)
1118 dbus_message_iter_recurse(&array, &dict);
1120 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1121 DBusMessageIter value, properties;
1122 const char *modem_path;
1124 dbus_message_iter_recurse(&dict, &value);
1125 dbus_message_iter_get_basic(&value, &modem_path);
1127 dbus_message_iter_next(&value);
1128 dbus_message_iter_recurse(&value, &properties);
1131 add_modem(modem_path, &properties);
1133 dbus_message_iter_next(&dict);
1137 dbus_message_unref(reply);
1139 dbus_pending_call_unref(call);
1142 static void ofono_connect(DBusConnection *connection, void *user_data)
1144 DBG("connection %p", connection);
1146 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1147 g_free, remove_modem);
1149 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1150 NULL, remove_network);
1152 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1153 manager_modems_reply, NULL, NULL,
1157 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1159 DBG("connection %p", connection);
1161 if (modem_hash == NULL)
1164 g_hash_table_destroy(modem_hash);
1169 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1172 const char *path = dbus_message_get_path(message);
1173 struct modem_data *modem;
1174 DBusMessageIter iter, value;
1177 DBG("path %s", path);
1179 modem = g_hash_table_lookup(modem_hash, path);
1183 if (dbus_message_iter_init(message, &iter) == FALSE)
1186 dbus_message_iter_get_basic(&iter, &key);
1188 dbus_message_iter_next(&iter);
1189 dbus_message_iter_recurse(&iter, &value);
1191 if (g_str_equal(key, "Powered") == TRUE) {
1192 dbus_bool_t powered;
1194 dbus_message_iter_get_basic(&value, &powered);
1195 if (powered == TRUE)
1198 modem->has_sim = FALSE;
1199 modem->has_reg = FALSE;
1200 modem->has_gprs = FALSE;
1202 modem_remove_device(modem);
1203 } else if (g_str_equal(key, "Online") == TRUE) {
1206 dbus_message_iter_get_basic(&value, &online);
1208 update_modem_online(modem, online);
1209 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1212 dbus_message_iter_get_basic(&value, &locked);
1215 modem_change_powered(path, TRUE);
1217 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1218 gboolean has_sim = modem_has_sim(&value);
1219 gboolean has_reg = modem_has_reg(&value);
1220 gboolean added_reg = has_reg && !modem->has_reg;
1221 gboolean has_gprs = modem_has_gprs(&value);
1222 gboolean added_gprs = has_gprs && !modem->has_gprs;
1224 modem->has_sim = has_sim;
1225 modem->has_reg = has_reg;
1226 modem->has_gprs = has_gprs;
1228 if (modem->device == NULL) {
1230 get_imsi(modem->path);
1231 } else if (!has_sim) {
1232 modem_remove_device(modem);
1235 check_registration(modem);
1237 gprs_change_powered(modem->path, TRUE);
1244 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1247 const char *path = dbus_message_get_path(message);
1248 struct modem_data *modem;
1249 DBusMessageIter iter, value;
1252 DBG("path %s", path);
1254 modem = g_hash_table_lookup(modem_hash, path);
1258 if (dbus_message_iter_init(message, &iter) == FALSE)
1261 dbus_message_iter_get_basic(&iter, &key);
1263 dbus_message_iter_next(&iter);
1264 dbus_message_iter_recurse(&iter, &value);
1266 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1269 dbus_message_iter_get_basic(&value, &imsi);
1271 add_device(path, imsi);
1272 } else if (g_str_equal(key, "Present") == TRUE) {
1273 dbus_bool_t present;
1275 dbus_message_iter_get_basic(&value, &present);
1280 if (modem->device != NULL)
1281 modem_remove_device(modem);
1283 modem->has_gprs = FALSE;
1284 modem->has_reg = FALSE;
1290 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1293 const char *path = dbus_message_get_path(message);
1294 struct modem_data *modem;
1295 DBusMessageIter iter, value;
1298 DBG("path %s", path);
1300 modem = g_hash_table_lookup(modem_hash, path);
1304 if (dbus_message_iter_init(message, &iter) == FALSE)
1307 dbus_message_iter_get_basic(&iter, &key);
1309 dbus_message_iter_next(&iter);
1310 dbus_message_iter_recurse(&iter, &value);
1312 if (g_str_equal(key, "Attached") == TRUE) {
1313 dbus_bool_t attached;
1315 dbus_message_iter_get_basic(&value, &attached);
1317 DBG("Attached %d", attached);
1319 if (attached == TRUE)
1320 check_networks(modem);
1321 else if (modem->device != NULL)
1322 connman_device_remove_all_networks(modem->device);
1329 static gboolean modem_added(DBusConnection *connection,
1330 DBusMessage *message, void *user_data)
1332 DBusMessageIter iter, properties;
1333 const char *modem_path;
1337 if (dbus_message_iter_init(message, &iter) == FALSE)
1340 dbus_message_iter_get_basic(&iter, &modem_path);
1342 dbus_message_iter_next(&iter);
1343 dbus_message_iter_recurse(&iter, &properties);
1345 add_modem(modem_path, &properties);
1350 static gboolean modem_removed(DBusConnection *connection,
1351 DBusMessage *message, void *user_data)
1353 DBusMessageIter iter;
1354 const char *modem_path;
1358 if (dbus_message_iter_init(message, &iter) == FALSE)
1361 dbus_message_iter_get_basic(&iter, &modem_path);
1363 g_hash_table_remove(modem_hash, modem_path);
1369 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1371 DBusMessageIter entry;
1372 gchar *nameserver = NULL, *nameserver_old = NULL;
1376 dbus_message_iter_recurse(array, &entry);
1378 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1381 dbus_message_iter_get_basic(&entry, &dns);
1385 if (nameserver == NULL) {
1387 nameserver = g_strdup(dns);
1390 nameserver_old = nameserver;
1391 nameserver = g_strdup_printf("%s %s",
1392 nameserver_old, dns);
1393 g_free(nameserver_old);
1396 dbus_message_iter_next(&entry);
1399 parent->ipv4.nameserver = nameserver;
1402 static void update_settings(DBusMessageIter *array,
1403 struct connman_network *network)
1405 struct connman_element *parent = connman_network_get_element(network);
1406 DBusMessageIter dict;
1407 const char *interface = NULL;
1409 DBG("network %p", network);
1411 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1414 dbus_message_iter_recurse(array, &dict);
1416 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1417 DBusMessageIter entry, value;
1420 dbus_message_iter_recurse(&dict, &entry);
1421 dbus_message_iter_get_basic(&entry, &key);
1425 dbus_message_iter_next(&entry);
1426 dbus_message_iter_recurse(&entry, &value);
1428 if (g_str_equal(key, "Interface") == TRUE) {
1431 dbus_message_iter_get_basic(&value, &interface);
1433 DBG("interface %s", interface);
1435 index = connman_inet_ifindex(interface);
1437 connman_network_set_index(network, index);
1439 connman_error("Can not find interface %s",
1443 } else if (g_str_equal(key, "Method") == TRUE) {
1446 dbus_message_iter_get_basic(&value, &method);
1447 if (g_strcmp0(method, "static") == 0) {
1449 parent->ipv4.method =
1450 CONNMAN_IPCONFIG_METHOD_FIXED;
1451 } else if (g_strcmp0(method, "dhcp") == 0) {
1453 parent->ipv4.method =
1454 CONNMAN_IPCONFIG_METHOD_DHCP;
1457 } else if (g_str_equal(key, "Address") == TRUE) {
1458 const char *address;
1460 dbus_message_iter_get_basic(&value, &address);
1462 DBG("address %s", address);
1464 parent->ipv4.address = g_strdup(address);
1465 } else if (g_str_equal(key, "Netmask") == TRUE) {
1466 const char *netmask;
1468 dbus_message_iter_get_basic(&value, &netmask);
1470 DBG("netmask %s", netmask);
1472 parent->ipv4.netmask = g_strdup(netmask);
1473 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1475 get_dns(&value, parent);
1476 } else if (g_str_equal(key, "Gateway") == TRUE) {
1477 const char *gateway;
1479 dbus_message_iter_get_basic(&value, &gateway);
1481 DBG("gateway %s", gateway);
1483 parent->ipv4.gateway = g_strdup(gateway);
1486 dbus_message_iter_next(&dict);
1489 /* deactive, oFono send NULL inteface before deactive signal */
1490 if (interface == NULL)
1491 connman_network_set_index(network, -1);
1494 static void cleanup_ipconfig(struct connman_network *network)
1496 struct connman_element *parent = connman_network_get_element(network);
1498 g_free(parent->ipv4.address);
1499 parent->ipv4.address = NULL;
1501 g_free(parent->ipv4.netmask);
1502 parent->ipv4.netmask = NULL;
1504 g_free(parent->ipv4.nameserver);
1505 parent->ipv4.nameserver = NULL;
1507 g_free(parent->ipv4.gateway);
1508 parent->ipv4.gateway = NULL;
1510 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1514 static void set_connected(struct connman_network *network,
1515 connman_bool_t connected)
1517 struct connman_element *parent = connman_network_get_element(network);
1518 enum connman_ipconfig_method method = parent->ipv4.method;
1521 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1522 case CONNMAN_IPCONFIG_METHOD_OFF:
1523 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1524 case CONNMAN_IPCONFIG_METHOD_AUTO:
1527 case CONNMAN_IPCONFIG_METHOD_FIXED:
1528 connman_network_set_ipv4_method(network, method);
1530 connman_network_set_connected(network, connected);
1532 if (connected == FALSE)
1533 cleanup_ipconfig(network);
1537 case CONNMAN_IPCONFIG_METHOD_DHCP:
1538 connman_network_set_ipv4_method(network, method);
1540 connman_network_set_connected(network, connected);
1545 static gboolean context_changed(DBusConnection *connection,
1546 DBusMessage *message, void *user_data)
1548 const char *path = dbus_message_get_path(message);
1549 struct connman_network *network;
1550 DBusMessageIter iter, value;
1553 DBG("path %s", path);
1555 network = g_hash_table_lookup(network_hash, path);
1556 if (network == NULL)
1559 if (!pending_network_is_available(network)) {
1560 remove_network(network);
1564 if (dbus_message_iter_init(message, &iter) == FALSE)
1567 dbus_message_iter_get_basic(&iter, &key);
1569 dbus_message_iter_next(&iter);
1570 dbus_message_iter_recurse(&iter, &value);
1572 if (g_str_equal(key, "Settings") == TRUE)
1573 update_settings(&value, network);
1574 else if (g_str_equal(key, "Active") == TRUE) {
1577 dbus_message_iter_get_basic(&value, &active);
1579 set_connected(network, active);
1586 static guint reg_watch;
1587 static guint sim_watch;
1588 static guint gprs_watch;
1589 static guint modem_watch;
1590 static guint modem_added_watch;
1591 static guint modem_removed_watch;
1592 static guint context_watch;
1594 static int ofono_init(void)
1598 connection = connman_dbus_get_connection();
1599 if (connection == NULL)
1602 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1603 ofono_connect, ofono_disconnect, NULL, NULL);
1605 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1606 OFONO_REGISTRATION_INTERFACE,
1611 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1612 OFONO_GPRS_INTERFACE,
1617 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1618 OFONO_MODEM_INTERFACE,
1623 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1624 OFONO_SIM_INTERFACE,
1629 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1630 OFONO_MANAGER_INTERFACE,
1635 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1636 OFONO_MANAGER_INTERFACE,
1641 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1642 OFONO_CONTEXT_INTERFACE,
1647 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1648 reg_watch == 0 || sim_watch == 0 ||
1649 modem_added_watch == 0 || modem_removed_watch == 0 ||
1650 context_watch == 0) {
1655 err = connman_network_driver_register(&network_driver);
1659 err = connman_device_driver_register(&modem_driver);
1661 connman_network_driver_unregister(&network_driver);
1668 g_dbus_remove_watch(connection, watch);
1669 g_dbus_remove_watch(connection, sim_watch);
1670 g_dbus_remove_watch(connection, reg_watch);
1671 g_dbus_remove_watch(connection, gprs_watch);
1672 g_dbus_remove_watch(connection, modem_watch);
1673 g_dbus_remove_watch(connection, modem_added_watch);
1674 g_dbus_remove_watch(connection, modem_removed_watch);
1675 g_dbus_remove_watch(connection, context_watch);
1677 dbus_connection_unref(connection);
1682 static void ofono_exit(void)
1684 g_dbus_remove_watch(connection, watch);
1685 g_dbus_remove_watch(connection, sim_watch);
1686 g_dbus_remove_watch(connection, reg_watch);
1687 g_dbus_remove_watch(connection, gprs_watch);
1688 g_dbus_remove_watch(connection, modem_watch);
1689 g_dbus_remove_watch(connection, modem_added_watch);
1690 g_dbus_remove_watch(connection, modem_removed_watch);
1691 g_dbus_remove_watch(connection, context_watch);
1693 ofono_disconnect(connection, NULL);
1695 connman_device_driver_unregister(&modem_driver);
1696 connman_network_driver_unregister(&network_driver);
1698 dbus_connection_unref(connection);
1701 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1702 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)