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 uint8_t strength, has_strength;
92 static int modem_probe(struct connman_device *device)
94 DBG("device %p", device);
99 static void modem_remove(struct connman_device *device)
101 DBG("device %p", device);
104 static int call_ofono(const char *path,
105 const char *interface, const char *method,
106 DBusPendingCallNotifyFunction notify, void *user_data,
107 DBusFreeFunction free_function,
110 DBusMessage *message;
111 DBusPendingCall *call;
115 DBG("path %s %s.%s", path, interface, method);
120 message = dbus_message_new_method_call(OFONO_SERVICE, path,
125 dbus_message_set_auto_start(message, FALSE);
128 ok = dbus_message_append_args_valist(message, type, va);
134 if (dbus_connection_send_with_reply(connection, message,
135 &call, TIMEOUT) == FALSE) {
136 connman_error("Failed to call %s.%s", interface, method);
137 dbus_message_unref(message);
142 connman_error("D-Bus connection not available");
143 dbus_message_unref(message);
147 dbus_pending_call_set_notify(call, notify, user_data, free_function);
149 dbus_message_unref(message);
154 static void set_property_reply(DBusPendingCall *call, void *user_data)
158 char const *name = user_data;
162 dbus_error_init(&error);
164 reply = dbus_pending_call_steal_reply(call);
166 if (dbus_set_error_from_message(&error, reply)) {
167 connman_error("SetProperty(%s) %s %s", name,
168 error.name, error.message);
169 dbus_error_free(&error);
172 dbus_message_unref(reply);
174 dbus_pending_call_unref(call);
177 static int set_property(const char *path, const char *interface,
178 const char *property, int type, void *value,
179 DBusPendingCallNotifyFunction notify, void *user_data,
180 DBusFreeFunction free_function)
182 DBusMessage *message;
183 DBusMessageIter iter;
184 DBusPendingCall *call;
186 DBG("path %s %s.%s", path, interface, property);
188 g_assert(notify == NULL ? free_function == NULL : 1);
193 message = dbus_message_new_method_call(OFONO_SERVICE, path,
194 interface, SET_PROPERTY);
198 dbus_message_set_auto_start(message, FALSE);
200 dbus_message_iter_init_append(message, &iter);
201 connman_dbus_property_append_basic(&iter, property, type, value);
203 if (dbus_connection_send_with_reply(connection, message,
204 &call, TIMEOUT) == FALSE) {
205 connman_error("Failed to change \"%s\" property on %s",
206 property, interface);
207 dbus_message_unref(message);
212 connman_error("D-Bus connection not available");
213 dbus_message_unref(message);
217 if (notify == NULL) {
218 notify = set_property_reply;
219 user_data = (void *)property;
220 free_function = NULL;
223 dbus_pending_call_set_notify(call, notify, user_data, free_function);
225 dbus_message_unref(message);
230 static void update_modem_online(struct modem_data *modem,
231 connman_bool_t online)
233 DBG("modem %p path %s online %d", modem, modem->path, online);
235 modem->online = online;
236 modem->requested_online = online;
237 modem->pending_online = FALSE;
240 connman_device_set_powered(modem->device, online);
243 static void set_online_reply(DBusPendingCall *call, void *user_data)
245 struct modem_data *modem;
250 DBG("path %s", (char *)user_data);
252 if (modem_hash == NULL)
255 modem = g_hash_table_lookup(modem_hash, user_data);
259 reply = dbus_pending_call_steal_reply(call);
261 dbus_error_init(&error);
263 if (dbus_set_error_from_message(&error, reply)) {
264 connman_error("SetProperty(Online) %s %s",
265 error.name, error.message);
266 dbus_error_free(&error);
268 result = modem->online;
270 result = modem->requested_online;
272 if (modem->pending_online)
273 update_modem_online(modem, result);
275 dbus_message_unref(reply);
277 dbus_pending_call_unref(call);
280 static int modem_change_online(char const *path, dbus_bool_t online)
282 struct modem_data *modem = g_hash_table_lookup(modem_hash, path);
287 if (modem->online == online)
290 modem->requested_online = online;
292 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
293 DBUS_TYPE_BOOLEAN, &online,
295 (void *)g_strdup(path), g_free);
298 static int modem_enable(struct connman_device *device)
300 const char *path = connman_device_get_string(device, "Path");
302 DBG("device %p, path, %s", device, path);
304 return modem_change_online(path, TRUE);
307 static int modem_disable(struct connman_device *device)
309 const char *path = connman_device_get_string(device, "Path");
311 DBG("device %p path %s", device, path);
313 return modem_change_online(path, FALSE);
316 static struct connman_device_driver modem_driver = {
318 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
319 .probe = modem_probe,
320 .remove = modem_remove,
321 .enable = modem_enable,
322 .disable = modem_disable,
325 static void modem_remove_device(struct modem_data *modem)
327 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
329 if (modem->device == NULL)
332 connman_device_remove_all_networks(modem->device);
333 connman_device_unregister(modem->device);
334 connman_device_unref(modem->device);
336 modem->device = NULL;
339 static void remove_modem(gpointer data)
341 struct modem_data *modem = data;
343 modem_remove_device(modem);
350 static void remove_network(gpointer data)
352 connman_network_unref(data);
355 static char *get_ident(const char *path)
362 pos = strrchr(path, '/');
369 static void create_service(struct connman_network *network)
376 path = connman_network_get_string(network, "Path");
378 group = get_ident(path);
380 connman_network_set_group(network, group);
383 static int network_probe(struct connman_network *network)
388 static gboolean pending_network_is_available(struct connman_network *network)
390 /* Modem or network may be removed */
391 if (network == NULL || connman_network_get_device(network) == NULL) {
392 DBG("Modem or network was removed");
399 static void set_active_reply(DBusPendingCall *call, void *user_data)
401 char const *path = user_data;
404 struct connman_network *network;
406 network = g_hash_table_lookup(network_hash, path);
408 DBG("path %s network %p", path, network);
410 reply = dbus_pending_call_steal_reply(call);
412 if (!pending_network_is_available(network))
415 dbus_error_init(&error);
417 if (dbus_set_error_from_message(&error, reply)) {
418 connman_error("SetProperty(Active) %s %s",
419 error.name, error.message);
421 if (connman_network_get_index(network) < 0)
422 connman_network_set_error(network,
423 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
425 dbus_error_free(&error);
429 dbus_message_unref(reply);
431 dbus_pending_call_unref(call);
434 static int set_network_active(struct connman_network *network,
439 const char *path = connman_network_get_string(network, "Path");
441 DBG("network %p, path %s, active %d", network, path, active);
443 error = set_property(path, OFONO_CONTEXT_INTERFACE,
444 "Active", DBUS_TYPE_BOOLEAN, &active,
445 set_active_reply, g_strdup(path), NULL);
447 if (active == FALSE && error == -EINPROGRESS)
453 static void set_apn(struct connman_network *network)
455 const char *apn, *path;
457 apn = connman_network_get_string(network, "Cellular.APN");
461 path = connman_network_get_string(network, "Path");
465 DBG("path %s, apn %s", path, apn);
467 set_property(path, OFONO_CONTEXT_INTERFACE,
468 "AccessPointName", DBUS_TYPE_STRING, &apn,
472 static int network_connect(struct connman_network *network)
474 DBG("network %p", network);
476 if (connman_network_get_index(network) >= 0)
479 return set_network_active(network, TRUE);
482 static int network_disconnect(struct connman_network *network)
484 DBG("network %p", network);
486 if (connman_network_get_index(network) < 0)
489 return set_network_active(network, FALSE);
492 static void network_remove(struct connman_network *network)
494 char const *path = connman_network_get_string(network, "Path");
496 DBG("network %p path %s", network, path);
498 g_hash_table_remove(network_hash, path);
501 static int network_setup(struct connman_network *network, const char *key)
503 if (g_strcmp0(key, "Cellular.APN") == 0)
509 static struct connman_network_driver network_driver = {
511 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
512 .probe = network_probe,
513 .remove = network_remove,
514 .connect = network_connect,
515 .disconnect = network_disconnect,
516 .setup = network_setup,
519 static void update_settings(DBusMessageIter *array,
520 struct connman_network *network);
522 static void set_connected(struct connman_network *network,
523 connman_bool_t connected);
525 static int add_network(struct connman_device *device,
526 const char *path, DBusMessageIter *dict)
528 struct modem_data *modem = connman_device_get_data(device);
529 struct connman_network *network;
531 const char *hash_path;
532 char const *operator;
533 char const *reg_status;
534 dbus_bool_t active = FALSE;
536 DBG("modem %p device %p path %s", modem, device, path);
538 ident = get_ident(path);
540 network = connman_device_get_network(device, ident);
544 network = g_hash_table_lookup(network_hash, path);
545 if (network != NULL) {
546 DBG("path %p already exists with device %p", path,
547 connman_network_get_device(network));
548 if (connman_network_get_device(network))
550 g_hash_table_remove(network_hash, path);
553 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
557 connman_network_set_string(network, "Path", path);
558 hash_path = connman_network_get_string(network, "Path");
559 if (hash_path == NULL)
562 create_service(network);
564 connman_network_ref(network);
565 g_hash_table_insert(network_hash, (char *)hash_path, network);
567 connman_network_set_available(network, TRUE);
568 connman_network_set_index(network, -1);
570 operator = connman_device_get_string(device, "Operator");
572 connman_network_set_name(network, operator);
574 if (modem->has_strength)
575 connman_network_set_strength(network, modem->strength);
577 reg_status = connman_device_get_string(device, "RegistrationStatus");
579 if (!g_strcmp0(reg_status, "roaming"))
580 connman_network_set_roaming(network, TRUE);
581 else if (!g_strcmp0(reg_status, "registered"))
582 connman_network_set_roaming(network, FALSE);
584 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
585 DBusMessageIter entry, value;
588 dbus_message_iter_recurse(dict, &entry);
589 dbus_message_iter_get_basic(&entry, &key);
591 dbus_message_iter_next(&entry);
592 dbus_message_iter_recurse(&entry, &value);
594 if (g_str_equal(key, "Type")) {
597 dbus_message_iter_get_basic(&value, &type);
598 if (g_strcmp0(type, "internet") != 0) {
599 DBG("path %p type %s", path, type);
602 } else if (g_str_equal(key, "AccessPointName")) {
605 dbus_message_iter_get_basic(&value, &ap);
607 connman_network_set_string(network, "Cellular.APN", ap);
608 } else if (g_str_equal(key, "Settings"))
609 update_settings(&value, network);
610 else if (g_str_equal(key, "Active") == TRUE)
611 dbus_message_iter_get_basic(&value, &active);
613 dbus_message_iter_next(dict);
616 if (connman_device_add_network(device, network) != 0)
620 set_connected(network, active);
625 connman_network_unref(network);
626 g_hash_table_remove(network_hash, hash_path);
630 static void check_networks_reply(DBusPendingCall *call, void *user_data)
632 char *path = user_data;
633 struct modem_data *modem;
635 DBusMessageIter array, entry, value, properties;
637 DBG("path %s", path);
639 modem = g_hash_table_lookup(modem_hash, path);
642 if (modem->device == NULL)
645 reply = dbus_pending_call_steal_reply(call);
647 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
650 dbus_message_iter_init(reply, &array);
652 dbus_message_iter_recurse(&array, &entry);
654 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
655 char const *network_path;
657 dbus_message_iter_recurse(&entry, &value);
658 dbus_message_iter_get_basic(&value, &network_path);
660 dbus_message_iter_next(&value);
661 dbus_message_iter_recurse(&value, &properties);
663 add_network(modem->device, network_path, &properties);
665 dbus_message_iter_next(&entry);
669 dbus_message_unref(reply);
671 dbus_pending_call_unref(call);
674 static void check_networks(struct modem_data *modem)
676 char const *path = modem->path;
678 DBG("modem %p path %s", modem, path);
680 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
681 check_networks_reply, g_strdup(path), g_free,
685 static void modem_clear_network_errors(struct modem_data *modem)
687 struct connman_device *device = modem->device;
694 g_hash_table_iter_init(&i, network_hash);
696 while (g_hash_table_iter_next(&i, NULL, &value)) {
697 struct connman_network *network = value;
699 if (connman_network_get_device(network) == device)
700 connman_network_clear_error(network);
704 static void modem_operator_name_changed(struct modem_data *modem,
707 struct connman_device *device = modem->device;
714 connman_device_set_string(device, "Operator", name);
716 for (g_hash_table_iter_init(&i, network_hash);
717 g_hash_table_iter_next(&i, NULL, &value);) {
718 struct connman_network *network = value;
720 if (connman_network_get_device(network) == device) {
721 connman_network_set_name(network, name);
722 connman_network_update(network);
727 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
729 struct connman_device *device = modem->device;
733 modem->strength = strength;
734 modem->has_strength = TRUE;
739 for (g_hash_table_iter_init(&i, network_hash);
740 g_hash_table_iter_next(&i, NULL, &value);) {
741 struct connman_network *network = value;
743 if (connman_network_get_device(network) == device) {
744 connman_network_set_strength(network, strength);
745 connman_network_update(network);
750 static void modem_roaming_changed(struct modem_data *modem,
753 struct connman_device *device = modem->device;
754 connman_bool_t roaming;
761 connman_device_set_string(device, "RegistrationStatus", status);
763 if (g_str_equal(status, "roaming"))
765 else if (g_str_equal(status, "registered"))
770 for (g_hash_table_iter_init(&i, network_hash);
771 g_hash_table_iter_next(&i, NULL, &value);) {
772 struct connman_network *network = value;
774 if (connman_network_get_device(network) == device) {
775 connman_network_set_roaming(network, roaming);
776 connman_network_update(network);
781 static void modem_registration_changed(struct modem_data *modem,
782 DBusMessageIter *entry)
784 DBusMessageIter iter;
787 connman_uint8_t strength;
788 char const *name, *status, *mcc_s;
790 dbus_message_iter_get_basic(entry, &key);
794 dbus_message_iter_next(entry);
796 dbus_message_iter_recurse(entry, &iter);
798 type = dbus_message_iter_get_arg_type(&iter);
799 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
802 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING) {
803 dbus_message_iter_get_basic(&iter, &name);
804 modem_operator_name_changed(modem, name);
805 } else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE) {
806 dbus_message_iter_get_basic(&iter, &strength);
807 modem_strength_changed(modem, strength);
808 } else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING) {
809 dbus_message_iter_get_basic(&iter, &status);
810 modem_roaming_changed(modem, status);
811 } else if (g_str_equal(key, "MobileCountryCode") &&
812 type == DBUS_TYPE_STRING) {
816 dbus_message_iter_get_basic(&iter, &mcc_s);
822 alpha2 = mcc_country_codes[mcc - 200];
823 connman_technology_set_regdom(alpha2);
828 static gboolean reg_changed(DBusConnection *connection,
829 DBusMessage *message, void *user_data)
831 const char *path = dbus_message_get_path(message);
832 struct modem_data *modem;
833 DBusMessageIter iter;
835 DBG("path %s", path);
837 modem = g_hash_table_lookup(modem_hash, path);
841 if (dbus_message_iter_init(message, &iter))
842 modem_registration_changed(modem, &iter);
847 static void check_registration_reply(DBusPendingCall *call, void *user_data)
849 char const *path = user_data;
850 struct modem_data *modem;
852 DBusMessageIter array, dict, entry;
854 DBG("path %s", path);
856 modem = g_hash_table_lookup(modem_hash, path);
860 reply = dbus_pending_call_steal_reply(call);
862 if (dbus_message_iter_init(reply, &array) == FALSE)
865 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
868 dbus_message_iter_recurse(&array, &dict);
869 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
870 dbus_message_iter_recurse(&dict, &entry);
871 modem_registration_changed(modem, &entry);
872 dbus_message_iter_next(&dict);
876 dbus_message_unref(reply);
878 dbus_pending_call_unref(call);
881 static void check_registration(struct modem_data *modem)
883 char const *path = modem->path;
885 DBG("modem %p path %s", modem, path);
887 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
888 check_registration_reply, g_strdup(path), g_free,
892 static void modem_gprs_changed(struct modem_data *modem,
893 DBusMessageIter *entry)
895 DBusMessageIter iter;
900 dbus_message_iter_get_basic(entry, &key);
904 dbus_message_iter_next(entry);
906 dbus_message_iter_recurse(entry, &iter);
908 type = dbus_message_iter_get_arg_type(&iter);
910 if (type != DBUS_TYPE_BOOLEAN)
913 dbus_message_iter_get_basic(&iter, &value);
915 if (g_str_equal(key, "Attached") == TRUE) {
916 DBG("Attached %d", value);
918 modem->attached = value;
921 modem_clear_network_errors(modem);
922 } else if (g_str_equal(key, "Powered") == TRUE) {
923 DBG("Powered %d", value);
925 modem->powered = value;
926 } else if (g_str_equal(key, "RoamingAllowed") == TRUE) {
927 DBG("RoamingAllowed %d", value);
929 modem->roaming_allowed = value;
933 static void check_gprs_reply(DBusPendingCall *call, void *user_data)
935 char const *path = user_data;
936 struct modem_data *modem;
938 DBusMessageIter array, dict, entry;
940 DBG("path %s", path);
942 modem = g_hash_table_lookup(modem_hash, path);
946 reply = dbus_pending_call_steal_reply(call);
948 if (dbus_message_iter_init(reply, &array) == FALSE)
951 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
954 dbus_message_iter_recurse(&array, &dict);
955 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
956 dbus_message_iter_recurse(&dict, &entry);
957 modem_gprs_changed(modem, &entry);
958 dbus_message_iter_next(&dict);
962 dbus_message_unref(reply);
964 dbus_pending_call_unref(call);
966 check_networks(modem);
969 static void check_gprs(struct modem_data *modem)
971 char const *path = modem->path;
973 DBG("modem %p path %s", modem, path);
975 call_ofono(path, OFONO_GPRS_INTERFACE, GET_PROPERTIES,
976 check_gprs_reply, g_strdup(path), g_free,
980 static void add_device(const char *path, const char *imsi)
982 struct modem_data *modem;
983 struct connman_device *device;
985 DBG("path %s imsi %s", path, imsi);
993 modem = g_hash_table_lookup(modem_hash, path);
998 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
1001 modem_remove_device(modem);
1004 if (strlen(imsi) == 0)
1007 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
1011 connman_device_set_ident(device, imsi);
1013 connman_device_set_string(device, "Path", path);
1015 connman_device_set_data(device, modem);
1017 if (connman_device_register(device) < 0) {
1018 connman_device_unref(device);
1022 modem->device = device;
1025 check_registration(modem);
1027 if (modem->has_gprs)
1031 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
1033 const char *path = user_data;
1034 const char *imsi = NULL;
1036 DBusMessageIter array, dict;
1038 DBG("path %s", path);
1040 reply = dbus_pending_call_steal_reply(call);
1042 if (dbus_message_iter_init(reply, &array) == FALSE)
1045 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1048 dbus_message_iter_recurse(&array, &dict);
1050 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1051 DBusMessageIter entry, value;
1054 dbus_message_iter_recurse(&dict, &entry);
1055 dbus_message_iter_get_basic(&entry, &key);
1057 dbus_message_iter_next(&entry);
1058 dbus_message_iter_recurse(&entry, &value);
1060 if (g_str_equal(key, "SubscriberIdentity")) {
1061 dbus_message_iter_get_basic(&value, &imsi);
1062 add_device(path, imsi);
1065 dbus_message_iter_next(&dict);
1069 dbus_message_unref(reply);
1071 dbus_pending_call_unref(call);
1074 static void get_imsi(const char *path)
1076 DBG("path %s", path);
1078 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
1079 sim_properties_reply, g_strdup(path), g_free,
1083 static int gprs_change_powered(const char *path, dbus_bool_t powered)
1085 DBG("path %s powered %d", path, powered);
1087 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
1088 DBUS_TYPE_BOOLEAN, &powered,
1092 static int modem_change_powered(const char *path, dbus_bool_t powered)
1094 DBG("path %s powered %d", path, powered);
1096 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
1097 DBUS_TYPE_BOOLEAN, &powered,
1102 static gboolean modem_has_interface(DBusMessageIter *array,
1103 char const *interface)
1105 DBusMessageIter entry;
1107 dbus_message_iter_recurse(array, &entry);
1109 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1110 const char *element;
1112 dbus_message_iter_get_basic(&entry, &element);
1114 if (g_strcmp0(interface, element) == 0)
1117 dbus_message_iter_next(&entry);
1123 static gboolean modem_has_sim(DBusMessageIter *array)
1125 return modem_has_interface(array, OFONO_SIM_INTERFACE);
1128 static gboolean modem_has_reg(DBusMessageIter *array)
1130 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
1133 static gboolean modem_has_gprs(DBusMessageIter *array)
1135 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1138 static void add_modem(const char *path, DBusMessageIter *prop)
1140 struct modem_data *modem;
1141 dbus_bool_t powered = FALSE;
1142 dbus_bool_t online = FALSE;
1143 dbus_bool_t has_online = FALSE;
1144 dbus_bool_t locked = FALSE;
1145 gboolean has_sim = FALSE;
1146 gboolean has_reg = FALSE;
1147 gboolean has_gprs = FALSE;
1149 modem = g_hash_table_lookup(modem_hash, path);
1154 modem = g_try_new0(struct modem_data, 1);
1158 modem->path = g_strdup(path);
1159 modem->device = NULL;
1160 modem->available = TRUE;
1162 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1164 while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
1165 DBusMessageIter entry, value;
1168 dbus_message_iter_recurse(prop, &entry);
1169 dbus_message_iter_get_basic(&entry, &key);
1171 dbus_message_iter_next(&entry);
1172 dbus_message_iter_recurse(&entry, &value);
1174 if (g_str_equal(key, "Powered") == TRUE)
1175 dbus_message_iter_get_basic(&value, &powered);
1176 else if (g_str_equal(key, "Lockdown") == TRUE)
1177 dbus_message_iter_get_basic(&value, &locked);
1178 else if (g_str_equal(key, "Online") == TRUE) {
1180 dbus_message_iter_get_basic(&value, &online);
1181 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1182 has_sim = modem_has_sim(&value);
1183 has_reg = modem_has_reg(&value);
1184 has_gprs = modem_has_gprs(&value);
1187 dbus_message_iter_next(prop);
1194 modem_change_powered(path, TRUE);
1196 modem->has_sim = has_sim;
1197 modem->has_reg = has_reg;
1198 modem->has_gprs = has_gprs;
1200 update_modem_online(modem, online);
1206 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1210 DBusMessageIter array, dict;
1214 reply = dbus_pending_call_steal_reply(call);
1216 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1219 dbus_error_init(&error);
1221 if (dbus_set_error_from_message(&error, reply)) {
1222 connman_error("ModemManager.GetModems() %s %s",
1223 error.name, error.message);
1224 dbus_error_free(&error);
1228 if (dbus_message_iter_init(reply, &array) == FALSE)
1231 dbus_message_iter_recurse(&array, &dict);
1233 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1234 DBusMessageIter value, properties;
1235 const char *modem_path;
1237 dbus_message_iter_recurse(&dict, &value);
1238 dbus_message_iter_get_basic(&value, &modem_path);
1240 dbus_message_iter_next(&value);
1241 dbus_message_iter_recurse(&value, &properties);
1244 add_modem(modem_path, &properties);
1246 dbus_message_iter_next(&dict);
1250 dbus_message_unref(reply);
1252 dbus_pending_call_unref(call);
1255 static void ofono_connect(DBusConnection *connection, void *user_data)
1257 DBG("connection %p", connection);
1259 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1260 g_free, remove_modem);
1262 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1263 NULL, remove_network);
1265 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1266 manager_modems_reply, NULL, NULL,
1270 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1272 DBG("connection %p", connection);
1274 if (modem_hash == NULL)
1277 g_hash_table_destroy(modem_hash);
1282 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1285 const char *path = dbus_message_get_path(message);
1286 struct modem_data *modem;
1287 DBusMessageIter iter, value;
1290 DBG("path %s", path);
1292 modem = g_hash_table_lookup(modem_hash, path);
1296 if (dbus_message_iter_init(message, &iter) == FALSE)
1299 dbus_message_iter_get_basic(&iter, &key);
1301 dbus_message_iter_next(&iter);
1302 dbus_message_iter_recurse(&iter, &value);
1304 if (g_str_equal(key, "Powered") == TRUE) {
1305 dbus_bool_t powered;
1307 dbus_message_iter_get_basic(&value, &powered);
1308 if (powered == TRUE)
1311 modem->has_sim = FALSE;
1312 modem->has_reg = FALSE;
1313 modem->has_gprs = FALSE;
1315 modem_remove_device(modem);
1316 } else if (g_str_equal(key, "Online") == TRUE) {
1319 dbus_message_iter_get_basic(&value, &online);
1321 update_modem_online(modem, online);
1322 } else if (g_str_equal(key, "Lockdown") == TRUE) {
1325 dbus_message_iter_get_basic(&value, &locked);
1328 modem_change_powered(path, TRUE);
1330 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1331 gboolean has_sim = modem_has_sim(&value);
1332 gboolean has_reg = modem_has_reg(&value);
1333 gboolean added_reg = has_reg && !modem->has_reg;
1334 gboolean has_gprs = modem_has_gprs(&value);
1335 gboolean added_gprs = has_gprs && !modem->has_gprs;
1337 modem->has_sim = has_sim;
1338 modem->has_reg = has_reg;
1339 modem->has_gprs = has_gprs;
1341 if (modem->device == NULL) {
1343 get_imsi(modem->path);
1344 } else if (!has_sim) {
1345 modem_remove_device(modem);
1348 check_registration(modem);
1351 gprs_change_powered(modem->path, TRUE);
1360 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1363 const char *path = dbus_message_get_path(message);
1364 struct modem_data *modem;
1365 DBusMessageIter iter, value;
1368 DBG("path %s", path);
1370 modem = g_hash_table_lookup(modem_hash, path);
1374 if (dbus_message_iter_init(message, &iter) == FALSE)
1377 dbus_message_iter_get_basic(&iter, &key);
1379 dbus_message_iter_next(&iter);
1380 dbus_message_iter_recurse(&iter, &value);
1382 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1385 dbus_message_iter_get_basic(&value, &imsi);
1387 add_device(path, imsi);
1388 } else if (g_str_equal(key, "Present") == TRUE) {
1389 dbus_bool_t present;
1391 dbus_message_iter_get_basic(&value, &present);
1396 if (modem->device != NULL)
1397 modem_remove_device(modem);
1399 modem->has_gprs = FALSE;
1400 modem->has_reg = FALSE;
1406 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1409 const char *path = dbus_message_get_path(message);
1410 struct modem_data *modem;
1411 DBusMessageIter iter;
1413 DBG("path %s", path);
1415 modem = g_hash_table_lookup(modem_hash, path);
1419 if (dbus_message_iter_init(message, &iter))
1420 modem_gprs_changed(modem, &iter);
1425 static gboolean modem_added(DBusConnection *connection,
1426 DBusMessage *message, void *user_data)
1428 DBusMessageIter iter, properties;
1429 const char *modem_path;
1433 if (dbus_message_iter_init(message, &iter) == FALSE)
1436 dbus_message_iter_get_basic(&iter, &modem_path);
1438 dbus_message_iter_next(&iter);
1439 dbus_message_iter_recurse(&iter, &properties);
1441 add_modem(modem_path, &properties);
1446 static gboolean modem_removed(DBusConnection *connection,
1447 DBusMessage *message, void *user_data)
1449 DBusMessageIter iter;
1450 const char *modem_path;
1454 if (dbus_message_iter_init(message, &iter) == FALSE)
1457 dbus_message_iter_get_basic(&iter, &modem_path);
1459 g_hash_table_remove(modem_hash, modem_path);
1465 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1467 DBusMessageIter entry;
1468 gchar *nameserver = NULL, *nameserver_old = NULL;
1472 dbus_message_iter_recurse(array, &entry);
1474 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1477 dbus_message_iter_get_basic(&entry, &dns);
1481 if (nameserver == NULL) {
1483 nameserver = g_strdup(dns);
1486 nameserver_old = nameserver;
1487 nameserver = g_strdup_printf("%s %s",
1488 nameserver_old, dns);
1489 g_free(nameserver_old);
1492 dbus_message_iter_next(&entry);
1495 parent->ipv4.nameserver = nameserver;
1498 static void update_settings(DBusMessageIter *array,
1499 struct connman_network *network)
1501 struct connman_element *parent = connman_network_get_element(network);
1502 DBusMessageIter dict;
1503 const char *interface = NULL;
1505 DBG("network %p", network);
1507 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1510 dbus_message_iter_recurse(array, &dict);
1512 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1513 DBusMessageIter entry, value;
1516 dbus_message_iter_recurse(&dict, &entry);
1517 dbus_message_iter_get_basic(&entry, &key);
1521 dbus_message_iter_next(&entry);
1522 dbus_message_iter_recurse(&entry, &value);
1524 if (g_str_equal(key, "Interface") == TRUE) {
1527 dbus_message_iter_get_basic(&value, &interface);
1529 DBG("interface %s", interface);
1531 index = connman_inet_ifindex(interface);
1533 connman_network_set_index(network, index);
1535 connman_error("Can not find interface %s",
1539 } else if (g_str_equal(key, "Method") == TRUE) {
1542 dbus_message_iter_get_basic(&value, &method);
1543 if (g_strcmp0(method, "static") == 0) {
1545 parent->ipv4.method =
1546 CONNMAN_IPCONFIG_METHOD_FIXED;
1547 } else if (g_strcmp0(method, "dhcp") == 0) {
1549 parent->ipv4.method =
1550 CONNMAN_IPCONFIG_METHOD_DHCP;
1553 } else if (g_str_equal(key, "Address") == TRUE) {
1554 const char *address;
1556 dbus_message_iter_get_basic(&value, &address);
1558 DBG("address %s", address);
1560 parent->ipv4.address = g_strdup(address);
1561 } else if (g_str_equal(key, "Netmask") == TRUE) {
1562 const char *netmask;
1564 dbus_message_iter_get_basic(&value, &netmask);
1566 DBG("netmask %s", netmask);
1568 parent->ipv4.netmask = g_strdup(netmask);
1569 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1571 get_dns(&value, parent);
1572 } else if (g_str_equal(key, "Gateway") == TRUE) {
1573 const char *gateway;
1575 dbus_message_iter_get_basic(&value, &gateway);
1577 DBG("gateway %s", gateway);
1579 parent->ipv4.gateway = g_strdup(gateway);
1582 dbus_message_iter_next(&dict);
1585 /* deactive, oFono send NULL inteface before deactive signal */
1586 if (interface == NULL)
1587 connman_network_set_index(network, -1);
1590 static void cleanup_ipconfig(struct connman_network *network)
1592 struct connman_element *parent = connman_network_get_element(network);
1594 g_free(parent->ipv4.address);
1595 parent->ipv4.address = NULL;
1597 g_free(parent->ipv4.netmask);
1598 parent->ipv4.netmask = NULL;
1600 g_free(parent->ipv4.nameserver);
1601 parent->ipv4.nameserver = NULL;
1603 g_free(parent->ipv4.gateway);
1604 parent->ipv4.gateway = NULL;
1606 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1610 static void set_connected(struct connman_network *network,
1611 connman_bool_t connected)
1613 struct connman_element *parent = connman_network_get_element(network);
1614 enum connman_ipconfig_method method = parent->ipv4.method;
1617 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1618 case CONNMAN_IPCONFIG_METHOD_OFF:
1619 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1620 case CONNMAN_IPCONFIG_METHOD_AUTO:
1623 case CONNMAN_IPCONFIG_METHOD_FIXED:
1624 connman_network_set_ipv4_method(network, method);
1626 connman_network_set_connected(network, connected);
1628 if (connected == FALSE)
1629 cleanup_ipconfig(network);
1633 case CONNMAN_IPCONFIG_METHOD_DHCP:
1634 connman_network_set_ipv4_method(network, method);
1636 connman_network_set_connected(network, connected);
1641 static gboolean context_changed(DBusConnection *connection,
1642 DBusMessage *message, void *user_data)
1644 const char *path = dbus_message_get_path(message);
1645 struct connman_network *network;
1646 DBusMessageIter iter, value;
1649 DBG("path %s", path);
1651 network = g_hash_table_lookup(network_hash, path);
1652 if (network == NULL)
1655 if (!pending_network_is_available(network)) {
1656 remove_network(network);
1660 if (dbus_message_iter_init(message, &iter) == FALSE)
1663 dbus_message_iter_get_basic(&iter, &key);
1665 dbus_message_iter_next(&iter);
1666 dbus_message_iter_recurse(&iter, &value);
1668 if (g_str_equal(key, "Settings") == TRUE)
1669 update_settings(&value, network);
1670 else if (g_str_equal(key, "Active") == TRUE) {
1673 dbus_message_iter_get_basic(&value, &active);
1675 set_connected(network, active);
1682 static guint reg_watch;
1683 static guint sim_watch;
1684 static guint gprs_watch;
1685 static guint modem_watch;
1686 static guint modem_added_watch;
1687 static guint modem_removed_watch;
1688 static guint context_watch;
1690 static int ofono_init(void)
1694 connection = connman_dbus_get_connection();
1695 if (connection == NULL)
1698 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1699 ofono_connect, ofono_disconnect, NULL, NULL);
1701 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1702 OFONO_REGISTRATION_INTERFACE,
1707 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1708 OFONO_GPRS_INTERFACE,
1713 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1714 OFONO_MODEM_INTERFACE,
1719 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1720 OFONO_SIM_INTERFACE,
1725 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1726 OFONO_MANAGER_INTERFACE,
1731 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1732 OFONO_MANAGER_INTERFACE,
1737 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1738 OFONO_CONTEXT_INTERFACE,
1743 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1744 reg_watch == 0 || sim_watch == 0 ||
1745 modem_added_watch == 0 || modem_removed_watch == 0 ||
1746 context_watch == 0) {
1751 err = connman_network_driver_register(&network_driver);
1755 err = connman_device_driver_register(&modem_driver);
1757 connman_network_driver_unregister(&network_driver);
1764 g_dbus_remove_watch(connection, watch);
1765 g_dbus_remove_watch(connection, sim_watch);
1766 g_dbus_remove_watch(connection, reg_watch);
1767 g_dbus_remove_watch(connection, gprs_watch);
1768 g_dbus_remove_watch(connection, modem_watch);
1769 g_dbus_remove_watch(connection, modem_added_watch);
1770 g_dbus_remove_watch(connection, modem_removed_watch);
1771 g_dbus_remove_watch(connection, context_watch);
1773 dbus_connection_unref(connection);
1778 static void ofono_exit(void)
1780 g_dbus_remove_watch(connection, watch);
1781 g_dbus_remove_watch(connection, sim_watch);
1782 g_dbus_remove_watch(connection, reg_watch);
1783 g_dbus_remove_watch(connection, gprs_watch);
1784 g_dbus_remove_watch(connection, modem_watch);
1785 g_dbus_remove_watch(connection, modem_added_watch);
1786 g_dbus_remove_watch(connection, modem_removed_watch);
1787 g_dbus_remove_watch(connection, context_watch);
1789 ofono_disconnect(connection, NULL);
1791 connman_device_driver_unregister(&modem_driver);
1792 connman_network_driver_unregister(&network_driver);
1794 dbus_connection_unref(connection);
1797 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1798 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)