5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/element.h>
35 #include <connman/device.h>
36 #include <connman/network.h>
37 #include <connman/dbus.h>
38 #include <connman/inet.h>
39 #include <connman/log.h>
41 #define OFONO_SERVICE "org.ofono"
43 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
45 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
46 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
47 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
48 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
50 #define PROPERTY_CHANGED "PropertyChanged"
51 #define GET_PROPERTIES "GetProperties"
52 #define SET_PROPERTY "SetProperty"
53 #define CONTEXT_ADDED "ContextAdded"
54 #define CONTEXT_REMOVED "ContextRemoved"
55 #define ADD_CONTEXT "AddContext"
56 #define GET_MODEMS "GetModems"
57 #define MODEM_ADDED "ModemAdded"
58 #define MODEM_REMOVED "ModemRemoved"
63 static DBusConnection *connection;
65 static GHashTable *modem_hash = NULL;
67 static GHashTable *network_hash;
71 struct connman_device *device;
76 gboolean pending_online;
77 gboolean requested_online;
80 uint8_t strength, has_strength;
83 static int modem_probe(struct connman_device *device)
85 DBG("device %p", device);
90 static void modem_remove(struct connman_device *device)
92 DBG("device %p", device);
95 static int call_ofono(const char *path,
96 const char *interface, const char *method,
97 DBusPendingCallNotifyFunction notify, void *user_data,
98 DBusFreeFunction free_function,
101 DBusMessage *message;
102 DBusPendingCall *call;
106 DBG("path %s %s.%s", path, interface, method);
111 message = dbus_message_new_method_call(OFONO_SERVICE, path,
116 dbus_message_set_auto_start(message, FALSE);
119 ok = dbus_message_append_args_valist(message, type, va);
125 if (dbus_connection_send_with_reply(connection, message,
126 &call, TIMEOUT) == FALSE) {
127 connman_error("Failed to call %s.%s", interface, method);
128 dbus_message_unref(message);
133 connman_error("D-Bus connection not available");
134 dbus_message_unref(message);
138 dbus_pending_call_set_notify(call, notify, user_data, free_function);
140 dbus_message_unref(message);
145 static void set_property_reply(DBusPendingCall *call, void *user_data)
149 char const *name = user_data;
153 dbus_error_init(&error);
155 reply = dbus_pending_call_steal_reply(call);
157 if (dbus_set_error_from_message(&error, reply)) {
158 connman_error("SetProperty(%s) %s %s", name,
159 error.name, error.message);
160 dbus_error_free(&error);
163 dbus_message_unref(reply);
165 dbus_pending_call_unref(call);
168 static int set_property(const char *path, const char *interface,
169 const char *property, int type, void *value,
170 DBusPendingCallNotifyFunction notify, void *user_data,
171 DBusFreeFunction free_function)
173 DBusMessage *message;
174 DBusMessageIter iter;
175 DBusPendingCall *call;
177 DBG("path %s %s.%s", path, interface, property);
179 g_assert(notify == NULL ? free_function == NULL : 1);
184 message = dbus_message_new_method_call(OFONO_SERVICE, path,
185 interface, SET_PROPERTY);
189 dbus_message_set_auto_start(message, FALSE);
191 dbus_message_iter_init_append(message, &iter);
192 connman_dbus_property_append_basic(&iter, property, type, value);
194 if (dbus_connection_send_with_reply(connection, message,
195 &call, TIMEOUT) == FALSE) {
196 connman_error("Failed to change \"%s\" property on %s",
197 property, interface);
198 dbus_message_unref(message);
203 connman_error("D-Bus connection not available");
204 dbus_message_unref(message);
208 if (notify == NULL) {
209 notify = set_property_reply;
210 user_data = (void *)property;
211 free_function = NULL;
214 dbus_pending_call_set_notify(call, notify, user_data, free_function);
216 dbus_message_unref(message);
221 static void update_modem_online(struct modem_data *modem,
222 connman_bool_t online)
224 DBG("modem %p path %s online %d", modem, modem->path, online);
226 modem->online = online;
227 modem->requested_online = online;
228 modem->pending_online = FALSE;
231 connman_device_set_powered(modem->device, online);
234 static void set_online_reply(DBusPendingCall *call, void *user_data)
236 struct modem_data *modem;
241 DBG("path %s", (char *)user_data);
243 if (modem_hash == NULL)
246 modem = g_hash_table_lookup(modem_hash, user_data);
250 reply = dbus_pending_call_steal_reply(call);
252 dbus_error_init(&error);
254 if (dbus_set_error_from_message(&error, reply)) {
255 connman_error("SetProperty(Online) %s %s",
256 error.name, error.message);
257 dbus_error_free(&error);
259 result = modem->online;
261 result = modem->requested_online;
263 if (modem->pending_online)
264 update_modem_online(modem, result);
266 dbus_message_unref(reply);
268 dbus_pending_call_unref(call);
271 static int modem_change_online(char const *path, dbus_bool_t online)
273 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
274 DBUS_TYPE_BOOLEAN, &online,
276 (void *)g_strdup(path), g_free);
279 static int modem_enable(struct connman_device *device)
281 const char *path = connman_device_get_string(device, "Path");
283 DBG("device %p, path, %s", device, path);
285 return modem_change_online(path, TRUE);
288 static int modem_disable(struct connman_device *device)
290 const char *path = connman_device_get_string(device, "Path");
292 DBG("device %p path %s", device, path);
294 return modem_change_online(path, FALSE);
297 static struct connman_device_driver modem_driver = {
299 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
300 .probe = modem_probe,
301 .remove = modem_remove,
302 .enable = modem_enable,
303 .disable = modem_disable,
306 static void modem_remove_device(struct modem_data *modem)
308 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
310 if (modem->device == NULL)
313 connman_device_remove_all_networks(modem->device);
314 connman_device_unregister(modem->device);
315 connman_device_unref(modem->device);
317 modem->device = NULL;
320 static void remove_modem(gpointer data)
322 struct modem_data *modem = data;
324 modem_remove_device(modem);
331 static void remove_network(gpointer data)
333 connman_network_unref(data);
336 static char *get_ident(const char *path)
343 pos = strrchr(path, '/');
350 static void create_service(struct connman_network *network)
357 path = connman_network_get_string(network, "Path");
359 group = get_ident(path);
361 connman_network_set_group(network, group);
364 static int network_probe(struct connman_network *network)
366 create_service(network);
370 static gboolean pending_network_is_available(struct connman_network *network)
372 /* Modem or network may be removed */
373 if (network == NULL || connman_network_get_device(network) == NULL) {
374 DBG("Modem or network was removed");
381 static void set_active_reply(DBusPendingCall *call, void *user_data)
383 char const *path = user_data;
386 struct connman_network *network;
388 network = g_hash_table_lookup(network_hash, path);
390 DBG("path %s network %p", path, network);
392 reply = dbus_pending_call_steal_reply(call);
394 if (!pending_network_is_available(network))
397 dbus_error_init(&error);
399 if (dbus_set_error_from_message(&error, reply)) {
400 connman_error("SetProperty(Active) %s %s",
401 error.name, error.message);
403 if (connman_network_get_index(network) < 0)
404 connman_network_set_error(network,
405 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
407 dbus_error_free(&error);
411 dbus_message_unref(reply);
413 dbus_pending_call_unref(call);
416 static int set_network_active(struct connman_network *network,
421 const char *path = connman_network_get_string(network, "Path");
423 DBG("network %p, path %s, active %d", network, path, active);
425 error = set_property(path, OFONO_CONTEXT_INTERFACE,
426 "Active", DBUS_TYPE_BOOLEAN, &active,
427 set_active_reply, g_strdup(path), NULL);
429 if (active == FALSE && error == -EINPROGRESS)
435 static void set_apn(struct connman_network *network)
437 const char *apn, *path;
439 apn = connman_network_get_string(network, "Cellular.APN");
443 path = connman_network_get_string(network, "Path");
447 DBG("path %s, apn %s", path, apn);
449 set_property(path, OFONO_CONTEXT_INTERFACE,
450 "AccessPointName", DBUS_TYPE_STRING, &apn,
454 static int network_connect(struct connman_network *network)
456 DBG("network %p", network);
458 if (connman_network_get_index(network) >= 0)
461 return set_network_active(network, TRUE);
464 static int network_disconnect(struct connman_network *network)
466 DBG("network %p", network);
468 if (connman_network_get_index(network) < 0)
471 return set_network_active(network, FALSE);
474 static void network_remove(struct connman_network *network)
476 char const *path = connman_network_get_string(network, "Path");
478 DBG("network %p path %s", network, path);
480 g_hash_table_remove(network_hash, path);
483 static int network_setup(struct connman_network *network, const char *key)
485 if (g_strcmp0(key, "Cellular.APN") == 0)
491 static struct connman_network_driver network_driver = {
493 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
494 .probe = network_probe,
495 .remove = network_remove,
496 .connect = network_connect,
497 .disconnect = network_disconnect,
498 .setup = network_setup,
501 static void update_settings(DBusMessageIter *array,
502 struct connman_network *network);
504 static void set_connected(struct connman_network *network,
505 connman_bool_t connected);
507 static int add_network(struct connman_device *device,
508 const char *path, DBusMessageIter *dict)
510 struct modem_data *modem = connman_device_get_data(device);
511 struct connman_network *network;
513 const char *hash_path;
514 char const *operator;
515 char const *reg_status;
516 dbus_bool_t active = FALSE;
518 DBG("modem %p device %p path %s", modem, device, path);
520 ident = get_ident(path);
522 network = connman_device_get_network(device, ident);
526 network = g_hash_table_lookup(network_hash, path);
527 if (network != NULL) {
528 DBG("path %p already exists with device %p", path,
529 connman_network_get_device(network));
530 if (connman_network_get_device(network))
532 g_hash_table_remove(network_hash, path);
535 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
539 connman_network_set_string(network, "Path", path);
540 hash_path = connman_network_get_string(network, "Path");
541 if (hash_path == NULL)
543 connman_network_ref(network);
544 g_hash_table_insert(network_hash, (char *)hash_path, network);
546 connman_network_set_available(network, TRUE);
547 connman_network_set_index(network, -1);
549 operator = connman_device_get_string(device, "Operator");
551 connman_network_set_name(network, operator);
553 if (modem->has_strength)
554 connman_network_set_strength(network, modem->strength);
556 reg_status = connman_device_get_string(device, "RegistrationStatus");
558 if (!g_strcmp0(reg_status, "roaming"))
559 connman_network_set_roaming(network, TRUE);
560 else if (!g_strcmp0(reg_status, "registered"))
561 connman_network_set_roaming(network, FALSE);
563 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
564 DBusMessageIter entry, value;
567 dbus_message_iter_recurse(dict, &entry);
568 dbus_message_iter_get_basic(&entry, &key);
570 dbus_message_iter_next(&entry);
571 dbus_message_iter_recurse(&entry, &value);
573 if (g_str_equal(key, "Type")) {
576 dbus_message_iter_get_basic(&value, &type);
577 if (g_strcmp0(type, "internet") == 0) {
578 connman_network_set_protocol(network,
579 CONNMAN_NETWORK_PROTOCOL_IP);
581 DBG("path %p type %s", path, type);
584 } else if (g_str_equal(key, "AccessPointName")) {
587 dbus_message_iter_get_basic(&value, &ap);
589 connman_network_set_string(network, "Cellular.APN", ap);
590 } else if (g_str_equal(key, "Settings"))
591 update_settings(&value, network);
592 else if (g_str_equal(key, "Active") == TRUE)
593 dbus_message_iter_get_basic(&value, &active);
595 dbus_message_iter_next(dict);
598 if (connman_device_add_network(device, network) != 0)
602 set_connected(network, active);
607 connman_network_unref(network);
608 g_hash_table_remove(network_hash, hash_path);
612 static void check_networks_reply(DBusPendingCall *call, void *user_data)
614 char *path = user_data;
615 struct modem_data *modem;
617 DBusMessageIter array, entry, value, properties;
619 DBG("path %s", path);
621 modem = g_hash_table_lookup(modem_hash, path);
624 if (modem->device == NULL)
627 reply = dbus_pending_call_steal_reply(call);
629 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
632 dbus_message_iter_init(reply, &array);
634 dbus_message_iter_recurse(&array, &entry);
636 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
637 char const *network_path;
639 dbus_message_iter_recurse(&entry, &value);
640 dbus_message_iter_get_basic(&value, &network_path);
642 dbus_message_iter_next(&value);
643 dbus_message_iter_recurse(&value, &properties);
645 add_network(modem->device, network_path, &properties);
647 dbus_message_iter_next(&entry);
651 dbus_message_unref(reply);
653 dbus_pending_call_unref(call);
656 static void check_networks(struct modem_data *modem)
658 char const *path = modem->path;
660 DBG("modem %p path %s", modem, path);
662 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
663 check_networks_reply, g_strdup(path), g_free,
667 static void modem_operator_name_changed(struct modem_data *modem,
670 struct connman_device *device = modem->device;
677 connman_device_set_string(device, "Operator", name);
679 for (g_hash_table_iter_init(&i, network_hash);
680 g_hash_table_iter_next(&i, NULL, &value);) {
681 struct connman_network *network = value;
683 if (connman_network_get_device(network) == device) {
684 connman_network_set_name(network, name);
685 connman_network_update(network);
690 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
692 struct connman_device *device = modem->device;
696 modem->strength = strength;
697 modem->has_strength = TRUE;
702 for (g_hash_table_iter_init(&i, network_hash);
703 g_hash_table_iter_next(&i, NULL, &value);) {
704 struct connman_network *network = value;
706 if (connman_network_get_device(network) == device) {
707 connman_network_set_strength(network, strength);
708 connman_network_update(network);
713 static void modem_roaming_changed(struct modem_data *modem,
716 struct connman_device *device = modem->device;
717 connman_bool_t roaming;
724 connman_device_set_string(device, "RegistrationStatus", status);
726 if (g_str_equal(status, "roaming"))
728 else if (g_str_equal(status, "registered"))
733 for (g_hash_table_iter_init(&i, network_hash);
734 g_hash_table_iter_next(&i, NULL, &value);) {
735 struct connman_network *network = value;
737 if (connman_network_get_device(network) == device) {
738 connman_network_set_roaming(network, roaming);
739 connman_network_update(network);
744 static void modem_registration_changed(struct modem_data *modem,
745 DBusMessageIter *entry)
747 DBusMessageIter iter;
752 connman_uint8_t byte;
755 dbus_message_iter_get_basic(entry, &key);
759 dbus_message_iter_next(entry);
761 dbus_message_iter_recurse(entry, &iter);
763 type = dbus_message_iter_get_arg_type(&iter);
764 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
767 dbus_message_iter_get_basic(&iter, &value);
769 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING)
770 modem_operator_name_changed(modem, value.str);
771 else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE)
772 modem_strength_changed(modem, value.byte);
773 else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING)
774 modem_roaming_changed(modem, value.str);
778 static gboolean reg_changed(DBusConnection *connection,
779 DBusMessage *message, void *user_data)
781 const char *path = dbus_message_get_path(message);
782 struct modem_data *modem;
783 DBusMessageIter iter;
785 DBG("path %s", path);
787 modem = g_hash_table_lookup(modem_hash, path);
791 if (dbus_message_iter_init(message, &iter))
792 modem_registration_changed(modem, &iter);
797 static void check_registration_reply(DBusPendingCall *call, void *user_data)
799 char const *path = user_data;
800 struct modem_data *modem;
802 DBusMessageIter array, dict, entry;
804 DBG("path %s", path);
806 modem = g_hash_table_lookup(modem_hash, path);
810 reply = dbus_pending_call_steal_reply(call);
812 if (dbus_message_iter_init(reply, &array) == FALSE)
815 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
818 dbus_message_iter_recurse(&array, &dict);
819 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
820 dbus_message_iter_recurse(&dict, &entry);
821 modem_registration_changed(modem, &entry);
822 dbus_message_iter_next(&dict);
826 dbus_message_unref(reply);
828 dbus_pending_call_unref(call);
831 static void check_registration(struct modem_data *modem)
833 char const *path = modem->path;
835 DBG("modem %p path %s", modem, path);
837 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
838 check_registration_reply, g_strdup(path), g_free,
842 static void add_device(const char *path, const char *imsi)
844 struct modem_data *modem;
845 struct connman_device *device;
847 DBG("path %s imsi %s", path, imsi);
855 modem = g_hash_table_lookup(modem_hash, path);
860 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
863 modem_remove_device(modem);
866 if (strlen(imsi) == 0)
869 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
873 connman_device_set_ident(device, imsi);
875 connman_device_set_string(device, "Path", path);
877 connman_device_set_data(device, modem);
879 if (connman_device_register(device) < 0) {
880 connman_device_unref(device);
884 modem->device = device;
887 check_registration(modem);
889 check_networks(modem);
892 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
894 const char *path = user_data;
895 const char *imsi = NULL;
897 DBusMessageIter array, dict;
899 DBG("path %s", path);
901 reply = dbus_pending_call_steal_reply(call);
903 if (dbus_message_iter_init(reply, &array) == FALSE)
906 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
909 dbus_message_iter_recurse(&array, &dict);
911 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
912 DBusMessageIter entry, value;
915 dbus_message_iter_recurse(&dict, &entry);
916 dbus_message_iter_get_basic(&entry, &key);
918 dbus_message_iter_next(&entry);
919 dbus_message_iter_recurse(&entry, &value);
921 if (g_str_equal(key, "SubscriberIdentity")) {
922 dbus_message_iter_get_basic(&value, &imsi);
923 add_device(path, imsi);
926 dbus_message_iter_next(&dict);
930 dbus_message_unref(reply);
932 dbus_pending_call_unref(call);
935 static void get_imsi(const char *path)
937 DBG("path %s", path);
939 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
940 sim_properties_reply, g_strdup(path), g_free,
944 static int gprs_change_powered(const char *path, dbus_bool_t powered)
946 DBG("path %s powered %d", path, powered);
948 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
949 DBUS_TYPE_BOOLEAN, &powered,
953 static int modem_change_powered(const char *path, dbus_bool_t powered)
955 DBG("path %s powered %d", path, powered);
957 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
958 DBUS_TYPE_BOOLEAN, &powered,
963 static gboolean modem_has_interface(DBusMessageIter *array,
964 char const *interface)
966 DBusMessageIter entry;
968 dbus_message_iter_recurse(array, &entry);
970 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
973 dbus_message_iter_get_basic(&entry, &element);
975 if (g_strcmp0(interface, element) == 0)
978 dbus_message_iter_next(&entry);
984 static gboolean modem_has_sim(DBusMessageIter *array)
986 return modem_has_interface(array, OFONO_SIM_INTERFACE);
989 static gboolean modem_has_reg(DBusMessageIter *array)
991 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
994 static gboolean modem_has_gprs(DBusMessageIter *array)
996 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
999 static void add_modem(const char *path, DBusMessageIter *properties)
1001 struct modem_data *modem;
1002 DBusMessageIter dict;
1003 dbus_bool_t powered = FALSE;
1004 dbus_bool_t online = FALSE;
1005 dbus_bool_t has_online = FALSE;
1006 gboolean has_sim = FALSE;
1007 gboolean has_reg = FALSE;
1008 gboolean has_gprs = FALSE;
1010 modem = g_hash_table_lookup(modem_hash, path);
1015 modem = g_try_new0(struct modem_data, 1);
1019 modem->path = g_strdup(path);
1020 modem->device = NULL;
1021 modem->available = TRUE;
1023 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1025 dbus_message_iter_recurse(properties, &dict);
1027 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1028 DBusMessageIter entry, value;
1031 dbus_message_iter_recurse(&dict, &entry);
1032 dbus_message_iter_get_basic(&entry, &key);
1034 dbus_message_iter_next(&entry);
1035 dbus_message_iter_recurse(&entry, &value);
1037 if (g_str_equal(key, "Powered") == TRUE)
1038 dbus_message_iter_get_basic(&value, &powered);
1039 else if (g_str_equal(key, "Online") == TRUE) {
1041 dbus_message_iter_get_basic(&value, &online);
1042 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1043 has_sim = modem_has_sim(&value);
1044 has_reg = modem_has_reg(&value);
1045 has_gprs = modem_has_gprs(&value);
1048 dbus_message_iter_next(&dict);
1052 modem_change_powered(path, TRUE);
1054 modem->has_sim = has_sim;
1055 modem->has_reg = has_reg;
1056 modem->has_gprs = has_gprs;
1058 update_modem_online(modem, online);
1064 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1068 DBusMessageIter array, dict;
1072 reply = dbus_pending_call_steal_reply(call);
1074 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1077 dbus_error_init(&error);
1079 if (dbus_set_error_from_message(&error, reply)) {
1080 connman_error("ModemManager.GetModems() %s %s",
1081 error.name, error.message);
1082 dbus_error_free(&error);
1086 if (dbus_message_iter_init(reply, &array) == FALSE)
1089 dbus_message_iter_recurse(&array, &dict);
1091 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1092 DBusMessageIter value, properties;
1093 const char *modem_path;
1095 dbus_message_iter_recurse(&dict, &value);
1096 dbus_message_iter_get_basic(&value, &modem_path);
1098 dbus_message_iter_next(&value);
1099 dbus_message_iter_recurse(&value, &properties);
1102 add_modem(modem_path, &properties);
1104 dbus_message_iter_next(&dict);
1108 dbus_message_unref(reply);
1110 dbus_pending_call_unref(call);
1113 static void ofono_connect(DBusConnection *connection, void *user_data)
1115 DBG("connection %p", connection);
1117 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1118 g_free, remove_modem);
1120 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1121 NULL, remove_network);
1123 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1124 manager_modems_reply, NULL, NULL,
1128 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1130 DBG("connection %p", connection);
1132 if (modem_hash == NULL)
1135 g_hash_table_destroy(modem_hash);
1140 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1143 const char *path = dbus_message_get_path(message);
1144 struct modem_data *modem;
1145 DBusMessageIter iter, value;
1148 DBG("path %s", path);
1150 modem = g_hash_table_lookup(modem_hash, path);
1154 if (dbus_message_iter_init(message, &iter) == FALSE)
1157 dbus_message_iter_get_basic(&iter, &key);
1159 dbus_message_iter_next(&iter);
1160 dbus_message_iter_recurse(&iter, &value);
1162 if (g_str_equal(key, "Powered") == TRUE) {
1163 dbus_bool_t powered;
1165 dbus_message_iter_get_basic(&value, &powered);
1166 if (powered == TRUE)
1169 modem->has_sim = FALSE;
1170 modem->has_reg = FALSE;
1171 modem->has_gprs = FALSE;
1173 modem_remove_device(modem);
1174 } else if (g_str_equal(key, "Online") == TRUE) {
1177 dbus_message_iter_get_basic(&value, &online);
1179 update_modem_online(modem, online);
1180 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1181 gboolean has_sim = modem_has_sim(&value);
1182 gboolean has_reg = modem_has_reg(&value);
1183 gboolean added_reg = has_reg && !modem->has_reg;
1184 gboolean has_gprs = modem_has_gprs(&value);
1185 gboolean added_gprs = has_gprs && !modem->has_gprs;
1187 modem->has_sim = has_sim;
1188 modem->has_reg = has_reg;
1189 modem->has_gprs = has_gprs;
1191 if (modem->device == NULL) {
1193 get_imsi(modem->path);
1194 } else if (!has_sim) {
1195 modem_remove_device(modem);
1198 check_registration(modem);
1200 gprs_change_powered(modem->path, TRUE);
1207 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1210 const char *path = dbus_message_get_path(message);
1211 struct modem_data *modem;
1212 DBusMessageIter iter, value;
1215 DBG("path %s", path);
1217 modem = g_hash_table_lookup(modem_hash, path);
1221 if (dbus_message_iter_init(message, &iter) == FALSE)
1224 dbus_message_iter_get_basic(&iter, &key);
1226 dbus_message_iter_next(&iter);
1227 dbus_message_iter_recurse(&iter, &value);
1229 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1232 dbus_message_iter_get_basic(&value, &imsi);
1234 add_device(path, imsi);
1235 } else if (g_str_equal(key, "Present") == TRUE) {
1236 dbus_bool_t present;
1238 dbus_message_iter_get_basic(&value, &present);
1243 if (modem->device != NULL)
1244 modem_remove_device(modem);
1246 modem->has_gprs = FALSE;
1247 modem->has_reg = FALSE;
1253 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1256 const char *path = dbus_message_get_path(message);
1257 struct modem_data *modem;
1258 DBusMessageIter iter, value;
1261 DBG("path %s", path);
1263 modem = g_hash_table_lookup(modem_hash, path);
1267 if (dbus_message_iter_init(message, &iter) == FALSE)
1270 dbus_message_iter_get_basic(&iter, &key);
1272 dbus_message_iter_next(&iter);
1273 dbus_message_iter_recurse(&iter, &value);
1275 if (g_str_equal(key, "Attached") == TRUE) {
1276 dbus_bool_t attached;
1278 dbus_message_iter_get_basic(&value, &attached);
1280 DBG("Attached %d", attached);
1282 if (attached == TRUE)
1283 check_networks(modem);
1284 else if (modem->device != NULL)
1285 connman_device_remove_all_networks(modem->device);
1292 static gboolean modem_added(DBusConnection *connection,
1293 DBusMessage *message, void *user_data)
1295 DBusMessageIter iter, properties;
1296 const char *modem_path;
1300 if (dbus_message_iter_init(message, &iter) == FALSE)
1303 dbus_message_iter_get_basic(&iter, &modem_path);
1305 dbus_message_iter_next(&iter);
1306 dbus_message_iter_recurse(&iter, &properties);
1308 add_modem(modem_path, &properties);
1313 static gboolean modem_removed(DBusConnection *connection,
1314 DBusMessage *message, void *user_data)
1316 DBusMessageIter iter;
1317 const char *modem_path;
1321 if (dbus_message_iter_init(message, &iter) == FALSE)
1324 dbus_message_iter_get_basic(&iter, &modem_path);
1326 g_hash_table_remove(modem_hash, modem_path);
1332 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1334 DBusMessageIter entry;
1335 gchar *nameserver = NULL, *nameserver_old = NULL;
1339 dbus_message_iter_recurse(array, &entry);
1341 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1344 dbus_message_iter_get_basic(&entry, &dns);
1348 if (nameserver == NULL) {
1350 nameserver = g_strdup(dns);
1353 nameserver_old = nameserver;
1354 nameserver = g_strdup_printf("%s %s",
1355 nameserver_old, dns);
1356 g_free(nameserver_old);
1359 dbus_message_iter_next(&entry);
1362 parent->ipv4.nameserver = nameserver;
1365 static void update_settings(DBusMessageIter *array,
1366 struct connman_network *network)
1368 struct connman_element *parent = connman_network_get_element(network);
1369 DBusMessageIter dict;
1370 const char *interface = NULL;
1372 DBG("network %p", network);
1374 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1377 dbus_message_iter_recurse(array, &dict);
1379 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1380 DBusMessageIter entry, value;
1383 dbus_message_iter_recurse(&dict, &entry);
1384 dbus_message_iter_get_basic(&entry, &key);
1388 dbus_message_iter_next(&entry);
1389 dbus_message_iter_recurse(&entry, &value);
1391 if (g_str_equal(key, "Interface") == TRUE) {
1394 dbus_message_iter_get_basic(&value, &interface);
1396 DBG("interface %s", interface);
1398 index = connman_inet_ifindex(interface);
1400 connman_network_set_index(network, index);
1402 connman_error("Can not find interface %s",
1406 } else if (g_str_equal(key, "Method") == TRUE) {
1409 dbus_message_iter_get_basic(&value, &method);
1410 if (g_strcmp0(method, "static") == 0) {
1412 parent->ipv4.method =
1413 CONNMAN_IPCONFIG_METHOD_FIXED;
1414 } else if (g_strcmp0(method, "dhcp") == 0) {
1416 parent->ipv4.method =
1417 CONNMAN_IPCONFIG_METHOD_DHCP;
1420 } else if (g_str_equal(key, "Address") == TRUE) {
1421 const char *address;
1423 dbus_message_iter_get_basic(&value, &address);
1425 DBG("address %s", address);
1427 parent->ipv4.address = g_strdup(address);
1428 } else if (g_str_equal(key, "Netmask") == TRUE) {
1429 const char *netmask;
1431 dbus_message_iter_get_basic(&value, &netmask);
1433 DBG("netmask %s", netmask);
1435 parent->ipv4.netmask = g_strdup(netmask);
1436 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1438 get_dns(&value, parent);
1439 } else if (g_str_equal(key, "Gateway") == TRUE) {
1440 const char *gateway;
1442 dbus_message_iter_get_basic(&value, &gateway);
1444 DBG("gateway %s", gateway);
1446 parent->ipv4.gateway = g_strdup(gateway);
1449 dbus_message_iter_next(&dict);
1452 /* deactive, oFono send NULL inteface before deactive signal */
1453 if (interface == NULL)
1454 connman_network_set_index(network, -1);
1457 static void cleanup_ipconfig(struct connman_network *network)
1459 struct connman_element *parent = connman_network_get_element(network);
1461 g_free(parent->ipv4.address);
1462 parent->ipv4.address = NULL;
1464 g_free(parent->ipv4.netmask);
1465 parent->ipv4.netmask = NULL;
1467 g_free(parent->ipv4.nameserver);
1468 parent->ipv4.nameserver = NULL;
1470 g_free(parent->ipv4.gateway);
1471 parent->ipv4.gateway = NULL;
1473 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1477 static void set_connected(struct connman_network *network,
1478 connman_bool_t connected)
1480 struct connman_element *parent = connman_network_get_element(network);
1481 enum connman_ipconfig_method method = parent->ipv4.method;
1484 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1485 case CONNMAN_IPCONFIG_METHOD_OFF:
1486 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1489 case CONNMAN_IPCONFIG_METHOD_FIXED:
1490 connman_network_set_method(network, method);
1492 if (connected == FALSE)
1493 cleanup_ipconfig(network);
1495 connman_network_set_connected(network, connected);
1498 case CONNMAN_IPCONFIG_METHOD_DHCP:
1499 connman_network_set_method(network, method);
1501 connman_network_set_connected(network, connected);
1506 static gboolean context_changed(DBusConnection *connection,
1507 DBusMessage *message, void *user_data)
1509 const char *path = dbus_message_get_path(message);
1510 struct connman_network *network;
1511 DBusMessageIter iter, value;
1514 DBG("path %s", path);
1516 network = g_hash_table_lookup(network_hash, path);
1517 if (network == NULL)
1520 if (!pending_network_is_available(network)) {
1521 remove_network(network);
1525 if (dbus_message_iter_init(message, &iter) == FALSE)
1528 dbus_message_iter_get_basic(&iter, &key);
1530 dbus_message_iter_next(&iter);
1531 dbus_message_iter_recurse(&iter, &value);
1533 if (g_str_equal(key, "Settings") == TRUE)
1534 update_settings(&value, network);
1535 else if (g_str_equal(key, "Active") == TRUE) {
1538 dbus_message_iter_get_basic(&value, &active);
1540 set_connected(network, active);
1547 static guint reg_watch;
1548 static guint sim_watch;
1549 static guint gprs_watch;
1550 static guint modem_watch;
1551 static guint modem_added_watch;
1552 static guint modem_removed_watch;
1553 static guint context_watch;
1555 static int ofono_init(void)
1559 connection = connman_dbus_get_connection();
1560 if (connection == NULL)
1563 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1564 ofono_connect, ofono_disconnect, NULL, NULL);
1566 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1567 OFONO_REGISTRATION_INTERFACE,
1572 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1573 OFONO_GPRS_INTERFACE,
1578 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1579 OFONO_MODEM_INTERFACE,
1584 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1585 OFONO_SIM_INTERFACE,
1590 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1591 OFONO_MANAGER_INTERFACE,
1596 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1597 OFONO_MANAGER_INTERFACE,
1602 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1603 OFONO_CONTEXT_INTERFACE,
1608 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1609 reg_watch == 0 || sim_watch == 0 ||
1610 modem_added_watch == 0 || modem_removed_watch == 0 ||
1611 context_watch == 0) {
1616 err = connman_network_driver_register(&network_driver);
1620 err = connman_device_driver_register(&modem_driver);
1622 connman_network_driver_unregister(&network_driver);
1629 g_dbus_remove_watch(connection, watch);
1630 g_dbus_remove_watch(connection, sim_watch);
1631 g_dbus_remove_watch(connection, reg_watch);
1632 g_dbus_remove_watch(connection, gprs_watch);
1633 g_dbus_remove_watch(connection, modem_watch);
1634 g_dbus_remove_watch(connection, modem_added_watch);
1635 g_dbus_remove_watch(connection, modem_removed_watch);
1636 g_dbus_remove_watch(connection, context_watch);
1638 dbus_connection_unref(connection);
1643 static void ofono_exit(void)
1645 g_dbus_remove_watch(connection, watch);
1646 g_dbus_remove_watch(connection, sim_watch);
1647 g_dbus_remove_watch(connection, reg_watch);
1648 g_dbus_remove_watch(connection, gprs_watch);
1649 g_dbus_remove_watch(connection, modem_watch);
1650 g_dbus_remove_watch(connection, modem_added_watch);
1651 g_dbus_remove_watch(connection, modem_removed_watch);
1652 g_dbus_remove_watch(connection, context_watch);
1654 ofono_disconnect(connection, NULL);
1656 connman_device_driver_unregister(&modem_driver);
1657 connman_network_driver_unregister(&network_driver);
1659 dbus_connection_unref(connection);
1662 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1663 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)