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 /* org.ofono.ConnectionManager properties */
87 dbus_bool_t roaming_allowed;
89 connman_bool_t registered;
90 connman_bool_t roaming;
91 uint8_t strength, has_strength;
94 static int modem_probe(struct connman_device *device)
96 DBG("device %p", device);
101 static void modem_remove(struct connman_device *device)
103 DBG("device %p", device);
106 static int call_ofono(const char *path,
107 const char *interface, const char *method,
108 DBusPendingCallNotifyFunction notify, void *user_data,
109 DBusFreeFunction free_function,
112 DBusMessage *message;
113 DBusPendingCall *call;
117 DBG("path %s %s.%s", path, interface, method);
122 message = dbus_message_new_method_call(OFONO_SERVICE, path,
127 dbus_message_set_auto_start(message, FALSE);
130 ok = dbus_message_append_args_valist(message, type, va);
136 if (dbus_connection_send_with_reply(connection, message,
137 &call, TIMEOUT) == FALSE) {
138 connman_error("Failed to call %s.%s", interface, method);
139 dbus_message_unref(message);
144 connman_error("D-Bus connection not available");
145 dbus_message_unref(message);
149 dbus_pending_call_set_notify(call, notify, user_data, free_function);
151 dbus_message_unref(message);
156 static void set_property_reply(DBusPendingCall *call, void *user_data)
160 char const *name = user_data;
164 dbus_error_init(&error);
166 reply = dbus_pending_call_steal_reply(call);
168 if (dbus_set_error_from_message(&error, reply)) {
169 connman_error("SetProperty(%s) %s %s", name,
170 error.name, error.message);
171 dbus_error_free(&error);
174 dbus_message_unref(reply);
176 dbus_pending_call_unref(call);
179 static int set_property(const char *path, const char *interface,
180 const char *property, int type, void *value,
181 DBusPendingCallNotifyFunction notify, void *user_data,
182 DBusFreeFunction free_function)
184 DBusMessage *message;
185 DBusMessageIter iter;
186 DBusPendingCall *call;
188 DBG("path %s %s.%s", path, interface, property);
190 g_assert(notify == NULL ? free_function == NULL : 1);
195 message = dbus_message_new_method_call(OFONO_SERVICE, path,
196 interface, SET_PROPERTY);
200 dbus_message_set_auto_start(message, FALSE);
202 dbus_message_iter_init_append(message, &iter);
203 connman_dbus_property_append_basic(&iter, property, type, value);
205 if (dbus_connection_send_with_reply(connection, message,
206 &call, TIMEOUT) == FALSE) {
207 connman_error("Failed to change \"%s\" property on %s",
208 property, interface);
209 dbus_message_unref(message);
214 connman_error("D-Bus connection not available");
215 dbus_message_unref(message);
219 if (notify == NULL) {
220 notify = set_property_reply;
221 user_data = (void *)property;
222 free_function = NULL;
225 dbus_pending_call_set_notify(call, notify, user_data, free_function);
227 dbus_message_unref(message);
232 static void update_modem_online(struct modem_data *modem,
233 connman_bool_t online)
235 DBG("modem %p path %s online %d", modem, modem->path, online);
237 modem->online = online;
238 modem->requested_online = online;
239 modem->pending_online = FALSE;
242 connman_device_set_powered(modem->device, online);
245 static void set_online_reply(DBusPendingCall *call, void *user_data)
247 struct modem_data *modem;
252 DBG("path %s", (char *)user_data);
254 if (modem_hash == NULL)
257 modem = g_hash_table_lookup(modem_hash, user_data);
261 reply = dbus_pending_call_steal_reply(call);
263 dbus_error_init(&error);
265 if (dbus_set_error_from_message(&error, reply)) {
266 connman_error("SetProperty(Online) %s %s",
267 error.name, error.message);
268 dbus_error_free(&error);
270 result = modem->online;
272 result = modem->requested_online;
274 if (modem->pending_online)
275 update_modem_online(modem, result);
277 dbus_message_unref(reply);
279 dbus_pending_call_unref(call);
282 static int modem_change_online(char const *path, dbus_bool_t online)
284 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
289 if (modem->online == online)
292 modem->requested_online = online;
294 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
295 DBUS_TYPE_BOOLEAN, &online,
297 (void *)g_strdup(path), g_free);
300 static int modem_enable(struct connman_device *device)
302 const char *path = connman_device_get_string(device, "Path");
304 DBG("device %p, path, %s", device, path);
306 return modem_change_online(path, TRUE);
309 static int modem_disable(struct connman_device *device)
311 const char *path = connman_device_get_string(device, "Path");
313 DBG("device %p path %s", device, path);
315 return modem_change_online(path, FALSE);
318 static struct connman_device_driver modem_driver = {
320 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
321 .probe = modem_probe,
322 .remove = modem_remove,
323 .enable = modem_enable,
324 .disable = modem_disable,
327 static void modem_remove_device(struct modem_data *modem)
329 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
331 if (modem->device == NULL)
334 connman_device_remove_all_networks(modem->device);
335 connman_device_unregister(modem->device);
336 connman_device_unref(modem->device);
338 modem->device = NULL;
341 static void remove_modem(gpointer data)
343 struct modem_data *modem = data;
345 modem_remove_device(modem);
352 static void remove_network(gpointer data)
354 connman_network_unref(data);
357 static char *get_ident(const char *path)
364 pos = strrchr(path, '/');
371 static void create_service(struct connman_network *network)
378 path = connman_network_get_string(network, "Path");
380 group = get_ident(path);
382 connman_network_set_group(network, group);
385 static int network_probe(struct connman_network *network)
390 static gboolean pending_network_is_available(struct connman_network *network)
392 /* Modem or network may be removed */
393 if (network == NULL || connman_network_get_device(network) == NULL) {
394 DBG("Modem or network was removed");
401 static void set_active_reply(DBusPendingCall *call, void *user_data)
403 char const *path = user_data;
406 struct connman_network *network;
408 network = g_hash_table_lookup(network_hash, path);
410 DBG("path %s network %p", path, network);
412 reply = dbus_pending_call_steal_reply(call);
414 if (!pending_network_is_available(network))
417 dbus_error_init(&error);
419 if (dbus_set_error_from_message(&error, reply)) {
420 connman_error("SetProperty(Active) %s %s",
421 error.name, error.message);
423 if (connman_network_get_index(network) < 0)
424 connman_network_set_error(network,
425 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
427 dbus_error_free(&error);
431 dbus_message_unref(reply);
433 dbus_pending_call_unref(call);
436 static int set_network_active(struct connman_network *network,
441 const char *path = connman_network_get_string(network, "Path");
443 DBG("network %p, path %s, active %d", network, path, active);
445 error = set_property(path, OFONO_CONTEXT_INTERFACE,
446 "Active", DBUS_TYPE_BOOLEAN, &active,
447 set_active_reply, g_strdup(path), NULL);
449 if (active == FALSE && error == -EINPROGRESS)
455 static void set_apn(struct connman_network *network)
457 const char *apn, *path;
459 apn = connman_network_get_string(network, "Cellular.APN");
463 path = connman_network_get_string(network, "Path");
467 DBG("path %s, apn %s", path, apn);
469 set_property(path, OFONO_CONTEXT_INTERFACE,
470 "AccessPointName", DBUS_TYPE_STRING, &apn,
474 static int network_connect(struct connman_network *network)
476 struct connman_device *device;
477 struct modem_data *modem;
479 DBG("network %p", network);
481 if (connman_network_get_index(network) >= 0)
484 device = connman_network_get_device(network);
488 modem = connman_device_get_data(device);
492 if (modem->registered == FALSE)
495 if (modem->powered == FALSE)
498 if (modem->roaming_allowed == FALSE && modem->roaming == TRUE)
501 return set_network_active(network, TRUE);
504 static int network_disconnect(struct connman_network *network)
506 DBG("network %p", network);
508 if (connman_network_get_index(network) < 0)
511 return set_network_active(network, FALSE);
514 static void network_remove(struct connman_network *network)
516 char const *path = connman_network_get_string(network, "Path");
518 DBG("network %p path %s", network, path);
520 g_hash_table_remove(network_hash, path);
523 static int network_setup(struct connman_network *network, const char *key)
525 if (g_strcmp0(key, "Cellular.APN") == 0)
531 static struct connman_network_driver network_driver = {
533 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
534 .probe = network_probe,
535 .remove = network_remove,
536 .connect = network_connect,
537 .disconnect = network_disconnect,
538 .setup = network_setup,
541 static void update_settings(DBusMessageIter *array,
542 struct connman_network *network);
544 static void set_connected(struct connman_network *network,
545 connman_bool_t connected);
547 static int add_network(struct connman_device *device,
548 const char *path, DBusMessageIter *dict)
550 struct modem_data *modem = connman_device_get_data(device);
551 struct connman_network *network;
553 const char *hash_path;
554 char const *operator;
555 dbus_bool_t active = FALSE;
557 DBG("modem %p device %p path %s", modem, device, path);
559 ident = get_ident(path);
561 network = connman_device_get_network(device, ident);
565 network = g_hash_table_lookup(network_hash, path);
566 if (network != NULL) {
567 DBG("path %p already exists with device %p", path,
568 connman_network_get_device(network));
569 if (connman_network_get_device(network))
571 g_hash_table_remove(network_hash, path);
574 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
578 connman_network_set_string(network, "Path", path);
579 hash_path = connman_network_get_string(network, "Path");
580 if (hash_path == NULL)
583 create_service(network);
585 connman_network_ref(network);
586 g_hash_table_insert(network_hash, (char *)hash_path, network);
588 connman_network_set_available(network, TRUE);
589 connman_network_set_index(network, -1);
591 operator = connman_device_get_string(device, "Operator");
593 connman_network_set_name(network, operator);
595 if (modem->has_strength)
596 connman_network_set_strength(network, modem->strength);
598 connman_network_set_roaming(network, modem->roaming);
600 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
601 DBusMessageIter entry, value;
604 dbus_message_iter_recurse(dict, &entry);
605 dbus_message_iter_get_basic(&entry, &key);
607 dbus_message_iter_next(&entry);
608 dbus_message_iter_recurse(&entry, &value);
610 if (g_str_equal(key, "Type")) {
613 dbus_message_iter_get_basic(&value, &type);
614 if (g_strcmp0(type, "internet") != 0) {
615 DBG("path %p type %s", path, type);
618 } else if (g_str_equal(key, "AccessPointName")) {
621 dbus_message_iter_get_basic(&value, &ap);
623 connman_network_set_string(network, "Cellular.APN", ap);
624 } else if (g_str_equal(key, "Settings"))
625 update_settings(&value, network);
626 else if (g_str_equal(key, "Active") == TRUE)
627 dbus_message_iter_get_basic(&value, &active);
629 dbus_message_iter_next(dict);
632 if (connman_device_add_network(device, network) != 0)
636 set_connected(network, active);
641 connman_network_unref(network);
642 g_hash_table_remove(network_hash, hash_path);
646 static void check_networks_reply(DBusPendingCall *call, void *user_data)
648 char *path = user_data;
649 struct modem_data *modem;
651 DBusMessageIter array, entry, value, properties;
653 DBG("path %s", path);
655 modem = g_hash_table_lookup(modem_hash, path);
658 if (modem->device == NULL)
661 reply = dbus_pending_call_steal_reply(call);
663 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
666 dbus_message_iter_init(reply, &array);
668 dbus_message_iter_recurse(&array, &entry);
670 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
671 char const *network_path;
673 dbus_message_iter_recurse(&entry, &value);
674 dbus_message_iter_get_basic(&value, &network_path);
676 dbus_message_iter_next(&value);
677 dbus_message_iter_recurse(&value, &properties);
679 add_network(modem->device, network_path, &properties);
681 dbus_message_iter_next(&entry);
685 dbus_message_unref(reply);
687 dbus_pending_call_unref(call);
690 static void check_networks(struct modem_data *modem)
692 char const *path = modem->path;
694 DBG("modem %p path %s", modem, path);
696 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
697 check_networks_reply, g_strdup(path), g_free,
701 static void modem_clear_network_errors(struct modem_data *modem)
703 struct connman_device *device = modem->device;
710 g_hash_table_iter_init(&i, network_hash);
712 while (g_hash_table_iter_next(&i, NULL, &value)) {
713 struct connman_network *network = value;
715 if (connman_network_get_device(network) == device)
716 connman_network_clear_error(network);
720 static void modem_operator_name_changed(struct modem_data *modem,
723 struct connman_device *device = modem->device;
730 connman_device_set_string(device, "Operator", name);
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_name(network, name);
738 connman_network_update(network);
743 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
745 struct connman_device *device = modem->device;
749 modem->strength = strength;
750 modem->has_strength = TRUE;
755 for (g_hash_table_iter_init(&i, network_hash);
756 g_hash_table_iter_next(&i, NULL, &value);) {
757 struct connman_network *network = value;
759 if (connman_network_get_device(network) == device) {
760 connman_network_set_strength(network, strength);
761 connman_network_update(network);
766 static void modem_roaming_changed(struct modem_data *modem,
769 struct connman_device *device = modem->device;
770 connman_bool_t roaming = FALSE;
771 connman_bool_t registered = FALSE;
772 connman_bool_t was_roaming = modem->roaming;
776 if (g_str_equal(status, "roaming"))
778 else if (g_str_equal(status, "registered"))
781 registered = registered || roaming;
783 if (modem->roaming == roaming && modem->registered == registered)
786 modem->registered = registered;
787 modem->roaming = roaming;
789 if (roaming == was_roaming)
795 g_hash_table_iter_init(&i, network_hash);
797 while (g_hash_table_iter_next(&i, NULL, &value)) {
798 struct connman_network *network = value;
800 if (connman_network_get_device(network) == device) {
801 connman_network_set_roaming(network, roaming);
802 connman_network_update(network);
807 static void modem_registration_removed(struct modem_data *modem)
809 modem->registered = FALSE;
810 modem->roaming = FALSE;
813 static void modem_registration_changed(struct modem_data *modem,
814 DBusMessageIter *entry)
816 DBusMessageIter iter;
819 connman_uint8_t strength;
820 char const *name, *status, *mcc_s;
822 dbus_message_iter_get_basic(entry, &key);
826 dbus_message_iter_next(entry);
828 dbus_message_iter_recurse(entry, &iter);
830 type = dbus_message_iter_get_arg_type(&iter);
831 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
834 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
835 dbus_message_iter_get_basic(&iter, &name);
836 modem_operator_name_changed(modem, name);
837 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
838 dbus_message_iter_get_basic(&iter, &strength);
839 modem_strength_changed(modem, strength);
840 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
841 dbus_message_iter_get_basic(&iter, &status);
842 modem_roaming_changed(modem, status);
843 } else if (g_str_equal(key, "MobileCountryCode") &&
844 type == DBUS_TYPE_STRING) {
848 dbus_message_iter_get_basic(&iter, &mcc_s);
854 alpha2 = mcc_country_codes[mcc - 200];
855 connman_technology_set_regdom(alpha2);
860 static gboolean reg_changed(DBusConnection *connection,
861 DBusMessage *message, void *user_data)
863 const char *path = dbus_message_get_path(message);
864 struct modem_data *modem;
865 DBusMessageIter iter;
867 DBG("path %s", path);
869 modem = g_hash_table_lookup(modem_hash, path);
873 if (dbus_message_iter_init(message, &iter))
874 modem_registration_changed(modem, &iter);
879 static void check_registration_reply(DBusPendingCall *call, void *user_data)
881 char const *path = user_data;
882 struct modem_data *modem;
884 DBusMessageIter array, dict, entry;
886 DBG("path %s", path);
888 modem = g_hash_table_lookup(modem_hash, path);
892 reply = dbus_pending_call_steal_reply(call);
894 if (dbus_message_iter_init(reply, &array) == FALSE)
897 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
900 dbus_message_iter_recurse(&array, &dict);
901 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
902 dbus_message_iter_recurse(&dict, &entry);
903 modem_registration_changed(modem, &entry);
904 dbus_message_iter_next(&dict);
908 dbus_message_unref(reply);
910 dbus_pending_call_unref(call);
913 static void check_registration(struct modem_data *modem)
915 char const *path = modem->path;
917 DBG("modem %p path %s", modem, path);
919 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
920 check_registration_reply, g_strdup(path), g_free,
924 static void modem_gprs_changed(struct modem_data *modem,
925 DBusMessageIter *entry)
927 DBusMessageIter iter;
932 dbus_message_iter_get_basic(entry, &key);
936 dbus_message_iter_next(entry);
938 dbus_message_iter_recurse(entry, &iter);
940 type = dbus_message_iter_get_arg_type(&iter);
942 if (type != DBUS_TYPE_BOOLEAN)
945 dbus_message_iter_get_basic(&iter, &value);
947 if (g_str_equal(key, "Attached") == TRUE) {
948 DBG("Attached %d", value);
950 modem->attached = value;
953 modem_clear_network_errors(modem);
954 } else if (g_str_equal(key, "Powered") == TRUE) {
955 DBG("Powered %d", value);
957 modem->powered = value;
958 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
959 DBG("RoamingAllowed %d", value);
961 modem->roaming_allowed = value;
965 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
967 char const *path = user_data;
968 struct modem_data *modem;
970 DBusMessageIter array, dict, entry;
972 DBG("path %s", path);
974 modem = g_hash_table_lookup(modem_hash, path);
978 reply = dbus_pending_call_steal_reply(call);
980 if (dbus_message_iter_init(reply, &array) == FALSE)
983 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
986 dbus_message_iter_recurse(&array, &dict);
987 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
988 dbus_message_iter_recurse(&dict, &entry);
989 modem_gprs_changed(modem, &entry);
990 dbus_message_iter_next(&dict);
994 dbus_message_unref(reply);
996 dbus_pending_call_unref(call);
998 check_networks(modem);
1001 static void check_gprs(struct modem_data *modem)
1003 char const *path = modem->path;
1005 DBG("modem %p path %s", modem, path);
1007 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
1008 check_gprs_reply, g_strdup(path), g_free,
1012 static void add_device(const char *path, const char *imsi)
1014 struct modem_data *modem;
1015 struct connman_device *device;
1017 DBG("path %s imsi %s", path, imsi);
1025 modem = g_hash_table_lookup(modem_hash, path);
1029 if (modem->device) {
1030 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1033 modem_remove_device(modem);
1036 if (strlen(imsi) == 0)
1039 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1043 connman_device_set_ident(device, imsi);
1045 connman_device_set_string(device, "Path", path);
1047 connman_device_set_data(device, modem);
1049 if (connman_device_register(device) < 0) {
1050 connman_device_unref(device);
1054 modem->device = device;
1057 check_registration(modem);
1059 if (modem->has_gprs)
1063 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1065 const char *path = user_data;
1066 const char *imsi = NULL;
1068 DBusMessageIter array, dict;
1070 DBG("path %s", path);
1072 reply = dbus_pending_call_steal_reply(call);
1074 if (dbus_message_iter_init(reply, &array) == FALSE)
1077 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1080 dbus_message_iter_recurse(&array, &dict);
1082 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1083 DBusMessageIter entry, value;
1086 dbus_message_iter_recurse(&dict, &entry);
1087 dbus_message_iter_get_basic(&entry, &key);
1089 dbus_message_iter_next(&entry);
1090 dbus_message_iter_recurse(&entry, &value);
1092 if (g_str_equal(key, "SubscriberIdentity")) {
1093 dbus_message_iter_get_basic(&value, &imsi);
1094 add_device(path, imsi);
1097 dbus_message_iter_next(&dict);
1101 dbus_message_unref(reply);
1103 dbus_pending_call_unref(call);
1106 static void get_imsi(const char *path)
1108 DBG("path %s", path);
1110 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1111 sim_properties_reply, g_strdup(path), g_free,
1115 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1117 DBG("path %s powered %d", path, powered);
1119 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1120 DBUS_TYPE_BOOLEAN, &powered,
1124 static int modem_change_powered(const char *path, dbus_bool_t powered)
1126 DBG("path %s powered %d", path, powered);
1128 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1129 DBUS_TYPE_BOOLEAN, &powered,
1134 static gboolean modem_has_interface(DBusMessageIter *array,
1135 char const *interface)
1137 DBusMessageIter entry;
1139 dbus_message_iter_recurse(array, &entry);
1141 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1142 const char *element;
1144 dbus_message_iter_get_basic(&entry, &element);
1146 if (g_strcmp0(interface, element) == 0)
1149 dbus_message_iter_next(&entry);
1155 static gboolean modem_has_sim(DBusMessageIter *array)
1157 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1160 static gboolean modem_has_reg(DBusMessageIter *array)
1162 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1165 static gboolean modem_has_gprs(DBusMessageIter *array)
1167 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1170 static void add_modem(const char *path, DBusMessageIter *prop)
1172 struct modem_data *modem;
1173 dbus_bool_t powered = FALSE;
1174 dbus_bool_t online = FALSE;
1175 dbus_bool_t has_online = FALSE;
1176 dbus_bool_t locked = FALSE;
1177 gboolean has_sim = FALSE;
1178 gboolean has_reg = FALSE;
1179 gboolean has_gprs = FALSE;
1181 modem = g_hash_table_lookup(modem_hash, path);
1186 modem = g_try_new0(struct modem_data, 1);
1190 modem->path = g_strdup(path);
1191 modem->device = NULL;
1192 modem->available = TRUE;
1194 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1196 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1197 DBusMessageIter entry, value;
1200 dbus_message_iter_recurse(prop, &entry);
1201 dbus_message_iter_get_basic(&entry, &key);
1203 dbus_message_iter_next(&entry);
1204 dbus_message_iter_recurse(&entry, &value);
1206 if (g_str_equal(key, "Powered") == TRUE)
1207 dbus_message_iter_get_basic(&value, &powered);
1208 else if (g_str_equal(key, "Lockdown") == TRUE)
1209 dbus_message_iter_get_basic(&value, &locked);
1210 else if (g_str_equal(key, "Online") == TRUE) {
1212 dbus_message_iter_get_basic(&value, &online);
1213 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1214 has_sim = modem_has_sim(&value);
1215 has_reg = modem_has_reg(&value);
1216 has_gprs = modem_has_gprs(&value);
1219 dbus_message_iter_next(prop);
1226 modem_change_powered(path, TRUE);
1228 modem->has_sim = has_sim;
1229 modem->has_reg = has_reg;
1230 modem->has_gprs = has_gprs;
1232 update_modem_online(modem, online);
1238 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1242 DBusMessageIter array, dict;
1246 reply = dbus_pending_call_steal_reply(call);
1248 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1251 dbus_error_init(&error);
1253 if (dbus_set_error_from_message(&error, reply)) {
1254 connman_error("ModemManager.GetModems() %s %s",
1255 error.name, error.message);
1256 dbus_error_free(&error);
1260 if (dbus_message_iter_init(reply, &array) == FALSE)
1263 dbus_message_iter_recurse(&array, &dict);
1265 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1266 DBusMessageIter value, properties;
1267 const char *modem_path;
1269 dbus_message_iter_recurse(&dict, &value);
1270 dbus_message_iter_get_basic(&value, &modem_path);
1272 dbus_message_iter_next(&value);
1273 dbus_message_iter_recurse(&value, &properties);
1276 add_modem(modem_path, &properties);
1278 dbus_message_iter_next(&dict);
1282 dbus_message_unref(reply);
1284 dbus_pending_call_unref(call);
1287 static void ofono_connect(DBusConnection *connection, void *user_data)
1289 DBG("connection %p", connection);
1291 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1292 g_free, remove_modem);
1294 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1295 NULL, remove_network);
1297 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1298 manager_modems_reply, NULL, NULL,
1302 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1304 DBG("connection %p", connection);
1306 if (modem_hash == NULL)
1309 g_hash_table_destroy(modem_hash);
1314 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1317 const char *path = dbus_message_get_path(message);
1318 struct modem_data *modem;
1319 DBusMessageIter iter, value;
1322 DBG("path %s", path);
1324 modem = g_hash_table_lookup(modem_hash, path);
1328 if (dbus_message_iter_init(message, &iter) == FALSE)
1331 dbus_message_iter_get_basic(&iter, &key);
1333 dbus_message_iter_next(&iter);
1334 dbus_message_iter_recurse(&iter, &value);
1336 if (g_str_equal(key, "Powered") == TRUE) {
1337 dbus_bool_t powered;
1339 dbus_message_iter_get_basic(&value, &powered);
1340 if (powered == TRUE)
1343 modem->has_sim = FALSE;
1344 modem->has_reg = FALSE;
1345 modem->has_gprs = FALSE;
1347 modem_remove_device(modem);
1348 } else if (g_str_equal(key, "Online") == TRUE) {
1351 dbus_message_iter_get_basic(&value, &online);
1353 update_modem_online(modem, online);
1354 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1357 dbus_message_iter_get_basic(&value, &locked);
1360 modem_change_powered(path, TRUE);
1362 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1363 gboolean has_sim = modem_has_sim(&value);
1364 gboolean has_reg = modem_has_reg(&value);
1365 gboolean had_reg = modem->has_reg;
1366 gboolean has_gprs = modem_has_gprs(&value);
1367 gboolean had_gprs = modem->has_gprs;
1369 modem->has_sim = has_sim;
1370 modem->has_reg = has_reg;
1371 modem->has_gprs = has_gprs;
1373 if (modem->device == NULL) {
1375 get_imsi(modem->path);
1376 } else if (!has_sim) {
1377 modem_remove_device(modem);
1379 if (has_reg && !had_reg)
1380 check_registration(modem);
1381 else if (had_reg && !has_reg)
1382 modem_registration_removed(modem);
1384 if (has_gprs && !had_gprs) {
1385 gprs_change_powered(modem->path, TRUE);
1394 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1397 const char *path = dbus_message_get_path(message);
1398 struct modem_data *modem;
1399 DBusMessageIter iter, value;
1402 DBG("path %s", path);
1404 modem = g_hash_table_lookup(modem_hash, path);
1408 if (dbus_message_iter_init(message, &iter) == FALSE)
1411 dbus_message_iter_get_basic(&iter, &key);
1413 dbus_message_iter_next(&iter);
1414 dbus_message_iter_recurse(&iter, &value);
1416 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1419 dbus_message_iter_get_basic(&value, &imsi);
1421 add_device(path, imsi);
1422 } else if (g_str_equal(key, "Present") == TRUE) {
1423 dbus_bool_t present;
1425 dbus_message_iter_get_basic(&value, &present);
1430 if (modem->device != NULL)
1431 modem_remove_device(modem);
1433 modem->has_gprs = FALSE;
1434 modem->has_reg = FALSE;
1440 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1443 const char *path = dbus_message_get_path(message);
1444 struct modem_data *modem;
1445 DBusMessageIter iter;
1447 DBG("path %s", path);
1449 modem = g_hash_table_lookup(modem_hash, path);
1453 if (dbus_message_iter_init(message, &iter))
1454 modem_gprs_changed(modem, &iter);
1459 static gboolean modem_added(DBusConnection *connection,
1460 DBusMessage *message, void *user_data)
1462 DBusMessageIter iter, properties;
1463 const char *modem_path;
1467 if (dbus_message_iter_init(message, &iter) == FALSE)
1470 dbus_message_iter_get_basic(&iter, &modem_path);
1472 dbus_message_iter_next(&iter);
1473 dbus_message_iter_recurse(&iter, &properties);
1475 add_modem(modem_path, &properties);
1480 static gboolean modem_removed(DBusConnection *connection,
1481 DBusMessage *message, void *user_data)
1483 DBusMessageIter iter;
1484 const char *modem_path;
1488 if (dbus_message_iter_init(message, &iter) == FALSE)
1491 dbus_message_iter_get_basic(&iter, &modem_path);
1493 g_hash_table_remove(modem_hash, modem_path);
1499 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1501 DBusMessageIter entry;
1502 gchar *nameserver = NULL, *nameserver_old = NULL;
1506 dbus_message_iter_recurse(array, &entry);
1508 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1511 dbus_message_iter_get_basic(&entry, &dns);
1515 if (nameserver == NULL) {
1517 nameserver = g_strdup(dns);
1520 nameserver_old = nameserver;
1521 nameserver = g_strdup_printf("%s %s",
1522 nameserver_old, dns);
1523 g_free(nameserver_old);
1526 dbus_message_iter_next(&entry);
1529 parent->ipv4.nameserver = nameserver;
1532 static void update_settings(DBusMessageIter *array,
1533 struct connman_network *network)
1535 struct connman_element *parent = connman_network_get_element(network);
1536 DBusMessageIter dict;
1537 const char *interface = NULL;
1539 DBG("network %p", network);
1541 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1544 dbus_message_iter_recurse(array, &dict);
1546 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1547 DBusMessageIter entry, value;
1550 dbus_message_iter_recurse(&dict, &entry);
1551 dbus_message_iter_get_basic(&entry, &key);
1555 dbus_message_iter_next(&entry);
1556 dbus_message_iter_recurse(&entry, &value);
1558 if (g_str_equal(key, "Interface") == TRUE) {
1561 dbus_message_iter_get_basic(&value, &interface);
1563 DBG("interface %s", interface);
1565 index = connman_inet_ifindex(interface);
1567 connman_network_set_index(network, index);
1569 connman_error("Can not find interface %s",
1573 } else if (g_str_equal(key, "Method") == TRUE) {
1576 dbus_message_iter_get_basic(&value, &method);
1577 if (g_strcmp0(method, "static") == 0) {
1579 parent->ipv4.method =
1580 CONNMAN_IPCONFIG_METHOD_FIXED;
1581 } else if (g_strcmp0(method, "dhcp") == 0) {
1583 parent->ipv4.method =
1584 CONNMAN_IPCONFIG_METHOD_DHCP;
1587 } else if (g_str_equal(key, "Address") == TRUE) {
1588 const char *address;
1590 dbus_message_iter_get_basic(&value, &address);
1592 DBG("address %s", address);
1594 parent->ipv4.address = g_strdup(address);
1595 } else if (g_str_equal(key, "Netmask") == TRUE) {
1596 const char *netmask;
1598 dbus_message_iter_get_basic(&value, &netmask);
1600 DBG("netmask %s", netmask);
1602 parent->ipv4.netmask = g_strdup(netmask);
1603 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1605 get_dns(&value, parent);
1606 } else if (g_str_equal(key, "Gateway") == TRUE) {
1607 const char *gateway;
1609 dbus_message_iter_get_basic(&value, &gateway);
1611 DBG("gateway %s", gateway);
1613 parent->ipv4.gateway = g_strdup(gateway);
1616 dbus_message_iter_next(&dict);
1619 /* deactive, oFono send NULL inteface before deactive signal */
1620 if (interface == NULL)
1621 connman_network_set_index(network, -1);
1624 static void cleanup_ipconfig(struct connman_network *network)
1626 struct connman_element *parent = connman_network_get_element(network);
1628 g_free(parent->ipv4.address);
1629 parent->ipv4.address = NULL;
1631 g_free(parent->ipv4.netmask);
1632 parent->ipv4.netmask = NULL;
1634 g_free(parent->ipv4.nameserver);
1635 parent->ipv4.nameserver = NULL;
1637 g_free(parent->ipv4.gateway);
1638 parent->ipv4.gateway = NULL;
1640 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1644 static void set_connected(struct connman_network *network,
1645 connman_bool_t connected)
1647 struct connman_element *parent = connman_network_get_element(network);
1648 enum connman_ipconfig_method method = parent->ipv4.method;
1651 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1652 case CONNMAN_IPCONFIG_METHOD_OFF:
1653 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1654 case CONNMAN_IPCONFIG_METHOD_AUTO:
1657 case CONNMAN_IPCONFIG_METHOD_FIXED:
1658 connman_network_set_ipv4_method(network, method);
1660 connman_network_set_connected(network, connected);
1662 if (connected == FALSE)
1663 cleanup_ipconfig(network);
1667 case CONNMAN_IPCONFIG_METHOD_DHCP:
1668 connman_network_set_ipv4_method(network, method);
1670 connman_network_set_connected(network, connected);
1675 static gboolean context_changed(DBusConnection *connection,
1676 DBusMessage *message, void *user_data)
1678 const char *path = dbus_message_get_path(message);
1679 struct connman_network *network;
1680 DBusMessageIter iter, value;
1683 DBG("path %s", path);
1685 network = g_hash_table_lookup(network_hash, path);
1686 if (network == NULL)
1689 if (!pending_network_is_available(network)) {
1690 remove_network(network);
1694 if (dbus_message_iter_init(message, &iter) == FALSE)
1697 dbus_message_iter_get_basic(&iter, &key);
1699 dbus_message_iter_next(&iter);
1700 dbus_message_iter_recurse(&iter, &value);
1702 if (g_str_equal(key, "Settings") == TRUE)
1703 update_settings(&value, network);
1704 else if (g_str_equal(key, "Active") == TRUE) {
1707 dbus_message_iter_get_basic(&value, &active);
1709 set_connected(network, active);
1716 static guint reg_watch;
1717 static guint sim_watch;
1718 static guint gprs_watch;
1719 static guint modem_watch;
1720 static guint modem_added_watch;
1721 static guint modem_removed_watch;
1722 static guint context_watch;
1724 static int ofono_init(void)
1728 connection = connman_dbus_get_connection();
1729 if (connection == NULL)
1732 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1733 ofono_connect, ofono_disconnect, NULL, NULL);
1735 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1736 OFONO_REGISTRATION_INTERFACE,
1741 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1742 OFONO_GPRS_INTERFACE,
1747 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1748 OFONO_MODEM_INTERFACE,
1753 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1754 OFONO_SIM_INTERFACE,
1759 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1760 OFONO_MANAGER_INTERFACE,
1765 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1766 OFONO_MANAGER_INTERFACE,
1771 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1772 OFONO_CONTEXT_INTERFACE,
1777 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1778 reg_watch == 0 || sim_watch == 0 ||
1779 modem_added_watch == 0 || modem_removed_watch == 0 ||
1780 context_watch == 0) {
1785 err = connman_network_driver_register(&network_driver);
1789 err = connman_device_driver_register(&modem_driver);
1791 connman_network_driver_unregister(&network_driver);
1798 g_dbus_remove_watch(connection, watch);
1799 g_dbus_remove_watch(connection, sim_watch);
1800 g_dbus_remove_watch(connection, reg_watch);
1801 g_dbus_remove_watch(connection, gprs_watch);
1802 g_dbus_remove_watch(connection, modem_watch);
1803 g_dbus_remove_watch(connection, modem_added_watch);
1804 g_dbus_remove_watch(connection, modem_removed_watch);
1805 g_dbus_remove_watch(connection, context_watch);
1807 dbus_connection_unref(connection);
1812 static void ofono_exit(void)
1814 g_dbus_remove_watch(connection, watch);
1815 g_dbus_remove_watch(connection, sim_watch);
1816 g_dbus_remove_watch(connection, reg_watch);
1817 g_dbus_remove_watch(connection, gprs_watch);
1818 g_dbus_remove_watch(connection, modem_watch);
1819 g_dbus_remove_watch(connection, modem_added_watch);
1820 g_dbus_remove_watch(connection, modem_removed_watch);
1821 g_dbus_remove_watch(connection, context_watch);
1823 ofono_disconnect(connection, NULL);
1825 connman_device_driver_unregister(&modem_driver);
1826 connman_network_driver_unregister(&network_driver);
1828 dbus_connection_unref(connection);
1831 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1832 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)