5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/element.h>
34 #include <connman/device.h>
35 #include <connman/network.h>
36 #include <connman/dbus.h>
37 #include <connman/inet.h>
38 #include <connman/log.h>
40 #define OFONO_SERVICE "org.ofono"
42 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
43 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
44 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".DataConnectionManager"
45 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
46 #define OFONO_PRI_CONTEXT_INTERFACE OFONO_SERVICE ".PrimaryDataContext"
47 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
49 #define PROPERTY_CHANGED "PropertyChanged"
50 #define GET_PROPERTIES "GetProperties"
51 #define SET_PROPERTY "SetProperty"
52 #define CREATE_CONTEXT "CreateContext"
56 #define CONTEXT_NAME "3G Connection"
57 #define CONTEXT_TYPE "internet"
59 static DBusConnection *connection;
61 static GHashTable *modem_hash = NULL;
65 struct connman_device *device;
69 static int modem_probe(struct connman_device *device)
71 DBG("device %p", device);
76 static void modem_remove(struct connman_device *device)
78 DBG("device %p", device);
81 static void powered_reply(DBusPendingCall *call, void *user_data)
88 dbus_error_init(&error);
90 reply = dbus_pending_call_steal_reply(call);
92 if (dbus_set_error_from_message(&error, reply)) {
93 connman_error("%s", error.message);
94 dbus_error_free(&error);
97 dbus_message_unref(reply);
99 dbus_pending_call_unref(call);
102 static int gprs_change_powered(const char *path, dbus_bool_t powered)
104 DBusMessage *message;
105 DBusMessageIter iter;
106 DBusPendingCall *call;
108 DBG("path %s powered %d", path, powered);
113 message = dbus_message_new_method_call(OFONO_SERVICE, path,
114 OFONO_GPRS_INTERFACE, SET_PROPERTY);
118 dbus_message_set_auto_start(message, FALSE);
120 dbus_message_iter_init_append(message, &iter);
121 connman_dbus_property_append_basic(&iter, "Powered",
122 DBUS_TYPE_BOOLEAN, &powered);
124 if (dbus_connection_send_with_reply(connection, message,
125 &call, TIMEOUT) == FALSE) {
126 connman_error("Failed to change powered property");
127 dbus_message_unref(message);
132 connman_error("D-Bus connection not available");
133 dbus_message_unref(message);
137 dbus_pending_call_set_notify(call, powered_reply, (void *)path, NULL);
139 dbus_message_unref(message);
144 static int modem_enable(struct connman_device *device)
146 const char *path = connman_device_get_string(device, "Path");
148 DBG("device %p, path, %s", device, path);
150 return gprs_change_powered(path, TRUE);
153 static int modem_disable(struct connman_device *device)
155 const char *path = connman_device_get_string(device, "Path");
157 DBG("device %p, path %s", device, path);
159 return gprs_change_powered(path, FALSE);
162 static struct connman_device_driver modem_driver = {
164 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
165 .probe = modem_probe,
166 .remove = modem_remove,
167 .enable = modem_enable,
168 .disable = modem_disable,
171 static char *get_ident(const char *path)
178 ident = g_strdup(path + 1);
182 while ((pos = strchr(pos, '/')) != NULL)
188 static void create_service(struct connman_network *network)
195 path = connman_network_get_string(network, "Path");
197 group = get_ident(path);
199 connman_network_set_group(network, group);
204 static void set_network_name_reply(DBusPendingCall *call, void *user_data)
206 struct connman_network *network = user_data;
208 DBusMessageIter array, dict;
210 DBG("network %p", network);
212 reply = dbus_pending_call_steal_reply(call);
214 if (dbus_message_iter_init(reply, &array) == FALSE)
217 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
220 dbus_message_iter_recurse(&array, &dict);
222 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
223 DBusMessageIter entry, value;
226 dbus_message_iter_recurse(&dict, &entry);
227 dbus_message_iter_get_basic(&entry, &key);
229 dbus_message_iter_next(&entry);
230 dbus_message_iter_recurse(&entry, &value);
232 if (g_str_equal(key, "Operator") == TRUE) {
235 dbus_message_iter_get_basic(&value, &name);
236 connman_network_set_name(network, name);
237 create_service(network);
240 dbus_message_iter_next(&dict);
243 dbus_message_unref(reply);
245 dbus_pending_call_unref(call);
248 static void set_network_name(struct connman_network *network)
250 struct connman_device *device;
251 DBusMessage *message;
252 DBusPendingCall *call;
255 device = connman_network_get_device(network);
257 path = connman_device_get_string(device, "Path");
261 DBG("path %s", path);
263 message = dbus_message_new_method_call(OFONO_SERVICE, path,
264 OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES);
268 dbus_message_set_auto_start(message, FALSE);
270 if (dbus_connection_send_with_reply(connection, message,
271 &call, TIMEOUT) == FALSE) {
272 connman_error("Failed to get operator");
277 connman_error("D-Bus connection not available");
281 dbus_pending_call_set_notify(call, set_network_name_reply,
282 (void *)network, NULL);
285 dbus_message_unref(message);
288 static void config_network_reply(DBusPendingCall *call, void *user_data)
290 struct connman_network *network = user_data;
292 DBusMessageIter array, dict;
293 gboolean internet_type = FALSE;
295 DBG("network %p", network);
297 reply = dbus_pending_call_steal_reply(call);
299 if (dbus_message_iter_init(reply, &array) == FALSE)
302 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
305 dbus_message_iter_recurse(&array, &dict);
307 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
308 DBusMessageIter entry, value;
311 dbus_message_iter_recurse(&dict, &entry);
312 dbus_message_iter_get_basic(&entry, &key);
314 dbus_message_iter_next(&entry);
315 dbus_message_iter_recurse(&entry, &value);
317 if (g_str_equal(key, "Type") == TRUE) {
320 dbus_message_iter_get_basic(&value, &type);
321 if (g_strcmp0(type, "internet") == 0) {
322 internet_type = TRUE;
324 connman_network_set_protocol(network,
325 CONNMAN_NETWORK_PROTOCOL_IP);
326 set_network_name(network);
328 internet_type = FALSE;
330 connman_network_set_protocol(network,
331 CONNMAN_NETWORK_PROTOCOL_UNKNOWN);
335 dbus_message_iter_next(&dict);
339 dbus_message_unref(reply);
341 dbus_pending_call_unref(call);
344 static void config_network(struct connman_network *network, const char *path)
346 DBusMessage *message;
347 DBusPendingCall *call;
349 DBG("path %s", path);
351 message = dbus_message_new_method_call(OFONO_SERVICE, path,
352 OFONO_PRI_CONTEXT_INTERFACE, GET_PROPERTIES);
356 dbus_message_set_auto_start(message, FALSE);
358 if (dbus_connection_send_with_reply(connection, message,
359 &call, TIMEOUT) == FALSE) {
360 connman_error("Failed to get Primary Context");
365 connman_error("D-Bus connection not available");
369 dbus_pending_call_set_notify(call, config_network_reply,
370 (void *)network, NULL);
373 dbus_message_unref(message);
376 static int network_probe(struct connman_network *network)
380 path = connman_network_get_string(network, "Path");
382 DBG("network %p path %s", network, path);
384 config_network(network, path);
389 static struct connman_network *pending_network;
391 static gboolean pending_network_is_available(
392 struct connman_network *pending_network)
394 struct connman_device *device;
395 struct connman_network *network;
396 const char *identifier;
399 /* Modem may be removed during waiting for active reply */
400 device = connman_network_get_device(pending_network);
404 identifier = connman_network_get_identifier(pending_network);
406 ident = g_strdup(identifier);
408 connman_network_unref(pending_network);
410 /* network may be removed during waiting for active reply */
411 network = connman_device_get_network(device, ident);
421 static void set_active_reply(DBusPendingCall *call, void *user_data)
425 struct connman_network *network = user_data;
427 DBG("network %p", network);
429 reply = dbus_pending_call_steal_reply(call);
431 if (pending_network_is_available(network) == FALSE)
434 dbus_error_init(&error);
435 if (dbus_set_error_from_message(&error, reply)) {
436 if (connman_network_get_index(network) < 0)
437 connman_network_set_error(network,
438 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
440 pending_network = NULL;
442 connman_error("%s", error.message);
444 dbus_error_free(&error);
446 pending_network = network;
449 dbus_message_unref(reply);
451 dbus_pending_call_unref(call);
454 static int set_network_active(struct connman_network *network,
457 DBusMessage *message;
458 DBusPendingCall *call;
459 DBusMessageIter iter;
461 const char *path = connman_network_get_string(network, "Path");
463 DBG("network %p, path %s, active %d", network, path, active);
468 message = dbus_message_new_method_call(OFONO_SERVICE, path,
469 OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
473 dbus_message_set_auto_start(message, FALSE);
475 dbus_message_iter_init_append(message, &iter);
476 connman_dbus_property_append_basic(&iter, "Active",
477 DBUS_TYPE_BOOLEAN, &active);
479 if (dbus_connection_send_with_reply(connection, message,
480 &call, TIMEOUT * 10) == FALSE) {
481 connman_error("Failed to connect service");
482 dbus_message_unref(message);
487 connman_error("D-Bus connection not available");
488 dbus_message_unref(message);
492 connman_network_ref(network);
494 dbus_pending_call_set_notify(call, set_active_reply, network, NULL);
496 dbus_message_unref(message);
504 static void set_apn_reply(DBusPendingCall *call, void *user_data)
509 reply = dbus_pending_call_steal_reply(call);
511 dbus_error_init(&error);
512 if (dbus_set_error_from_message(&error, reply)) {
513 connman_error("%s", error.message);
515 dbus_error_free(&error);
518 dbus_message_unref(reply);
520 dbus_pending_call_unref(call);
523 static void set_apn(struct connman_network *network)
525 DBusMessage *message;
526 DBusPendingCall *call;
527 DBusMessageIter iter;
528 const char *apn, *path;
530 apn = connman_network_get_string(network, "Cellular.APN");
534 path = connman_network_get_string(network, "Path");
538 DBG("path %s, apn %s", path, apn);
540 message = dbus_message_new_method_call(OFONO_SERVICE, path,
541 OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
545 dbus_message_set_auto_start(message, FALSE);
547 dbus_message_iter_init_append(message, &iter);
548 connman_dbus_property_append_basic(&iter, "AccessPointName",
549 DBUS_TYPE_STRING, &apn);
551 if (dbus_connection_send_with_reply(connection, message,
552 &call, TIMEOUT) == FALSE) {
553 dbus_message_unref(message);
558 connman_error("D-Bus connection not available");
559 dbus_message_unref(message);
563 dbus_pending_call_set_notify(call, set_apn_reply, NULL, NULL);
565 dbus_message_unref(message);
568 static int network_connect(struct connman_network *network)
570 if (connman_network_get_index(network) >= 0)
573 return set_network_active(network, TRUE);
576 static int network_disconnect(struct connman_network *network)
578 if (connman_network_get_index(network) < 0)
581 return set_network_active(network, FALSE);
584 static void network_remove(struct connman_network *network)
586 DBG("network %p", network);
589 static int network_setup(struct connman_network *network, const char *key)
593 if (g_strcmp0(key, "Cellular.APN") == 0)
599 static struct connman_network_driver network_driver = {
601 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
602 .probe = network_probe,
603 .remove = network_remove,
604 .connect = network_connect,
605 .disconnect = network_disconnect,
606 .setup = network_setup,
609 static void add_network(struct connman_device *device, const char *path)
611 struct connman_network *network;
614 DBG("device %p path %s", device, path);
616 network = connman_device_get_network(device, path);
620 ident = get_ident(path);
622 network = connman_network_create(ident,
623 CONNMAN_NETWORK_TYPE_CELLULAR);
629 connman_network_set_string(network, "Path", path);
630 connman_network_set_available(network, TRUE);
631 connman_network_set_index(network, -1);
632 connman_device_add_network(device, network);
635 static void add_networks(struct connman_device *device, DBusMessageIter *array)
637 DBusMessageIter entry;
641 dbus_message_iter_recurse(array, &entry);
643 while (dbus_message_iter_get_arg_type(&entry) ==
644 DBUS_TYPE_OBJECT_PATH) {
647 dbus_message_iter_get_basic(&entry, &path);
649 add_network(device, path);
651 dbus_message_iter_next(&entry);
655 static void create_context_reply(DBusPendingCall *call, void *user_data)
662 dbus_error_init(&error);
664 reply = dbus_pending_call_steal_reply(call);
666 if (dbus_set_error_from_message(&error, reply)) {
667 connman_error("%s", error.message);
668 dbus_error_free(&error);
671 dbus_message_unref(reply);
673 dbus_pending_call_unref(call);
676 static void add_default_context(DBusMessageIter *array,
677 const char *path, const char *name, const char *type)
679 DBusMessageIter entry;
680 DBusMessage *message;
681 DBusPendingCall *call;
688 dbus_message_iter_recurse(array, &entry);
690 if (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_OBJECT_PATH)
693 DBG("path %s, name %s, type %s", path, name, type);
695 message = dbus_message_new_method_call(OFONO_SERVICE, path,
696 OFONO_GPRS_INTERFACE, CREATE_CONTEXT);
700 dbus_message_set_auto_start(message, FALSE);
702 dbus_message_append_args(message, DBUS_TYPE_STRING,
703 &name, DBUS_TYPE_STRING,
704 &type, DBUS_TYPE_INVALID);
706 if (dbus_connection_send_with_reply(connection, message,
707 &call, TIMEOUT) == FALSE) {
708 connman_error("Failed to create default context");
709 dbus_message_unref(message);
714 connman_error("D-Bus connection not available");
715 dbus_message_unref(message);
719 dbus_pending_call_set_notify(call, create_context_reply, NULL, NULL);
721 dbus_message_unref(message);
724 static void check_networks_reply(DBusPendingCall *call, void *user_data)
726 struct connman_device *device = user_data;
728 DBusMessageIter array, dict, contexts;
729 dbus_bool_t attached;
731 DBG("device %p", device);
733 reply = dbus_pending_call_steal_reply(call);
735 if (dbus_message_iter_init(reply, &array) == FALSE)
738 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
741 dbus_message_iter_recurse(&array, &dict);
743 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
744 DBusMessageIter entry, value;
747 dbus_message_iter_recurse(&dict, &entry);
748 dbus_message_iter_get_basic(&entry, &key);
750 dbus_message_iter_next(&entry);
751 dbus_message_iter_recurse(&entry, &value);
755 if (g_str_equal(key, "Attached") == TRUE) {
756 dbus_message_iter_get_basic(&value, &attached);
757 DBG("Attached %d", attached);
758 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
761 path = connman_device_get_string(device, "Path");
763 add_default_context(&contexts, path,
764 CONTEXT_NAME, CONTEXT_TYPE);
765 } else if (g_str_equal(key, "Status") == TRUE) {
768 dbus_message_iter_get_basic(&value, &status);
769 /* FIXME: add roaming support */
770 } else if (g_str_equal(key, "Powered") == TRUE) {
773 dbus_message_iter_get_basic(&value, &powered);
775 connman_device_set_powered(device, powered);
778 dbus_message_iter_next(&dict);
781 if (attached == TRUE)
782 add_networks(device, &contexts);
785 dbus_message_unref(reply);
787 dbus_pending_call_unref(call);
790 static void check_networks(struct modem_data *modem)
792 DBusMessage *message;
793 DBusPendingCall *call;
794 struct connman_device *device;
796 DBG("modem %p", modem);
801 device = modem->device;
805 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
806 OFONO_GPRS_INTERFACE, GET_PROPERTIES);
810 dbus_message_set_auto_start(message, FALSE);
812 if (dbus_connection_send_with_reply(connection, message,
813 &call, TIMEOUT) == FALSE) {
814 connman_error("Failed to get ofono GPRS");
819 connman_error("D-Bus connection not available");
823 dbus_pending_call_set_notify(call, check_networks_reply,
824 (void *)device, NULL);
827 dbus_message_unref(message);
830 static void add_device(const char *path, const char *imsi)
832 struct modem_data *modem;
833 struct connman_device *device;
835 DBG("path %s imsi %s", path, imsi);
843 modem = g_hash_table_lookup(modem_hash, path);
847 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
851 connman_device_set_ident(device, imsi);
853 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
855 connman_device_set_string(device, "Path", path);
857 if (connman_device_register(device) < 0) {
858 connman_device_unref(device);
862 modem->device = device;
864 check_networks(modem);
867 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
869 const char *path = user_data;
871 DBusMessageIter array, dict;
873 DBG("path %s", path);
875 reply = dbus_pending_call_steal_reply(call);
877 if (dbus_message_iter_init(reply, &array) == FALSE)
880 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
883 dbus_message_iter_recurse(&array, &dict);
885 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
886 DBusMessageIter entry, value;
887 const char *key, *imsi;
889 dbus_message_iter_recurse(&dict, &entry);
890 dbus_message_iter_get_basic(&entry, &key);
892 dbus_message_iter_next(&entry);
893 dbus_message_iter_recurse(&entry, &value);
895 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
896 dbus_message_iter_get_basic(&value, &imsi);
898 add_device(path, imsi);
901 dbus_message_iter_next(&dict);
905 dbus_message_unref(reply);
907 dbus_pending_call_unref(call);
910 static void get_imsi(const char *path)
912 DBusMessage *message;
913 DBusPendingCall *call;
915 DBG("path %s", path);
917 message = dbus_message_new_method_call(OFONO_SERVICE, path,
918 OFONO_SIM_INTERFACE, GET_PROPERTIES);
922 dbus_message_set_auto_start(message, FALSE);
924 if (dbus_connection_send_with_reply(connection, message,
925 &call, TIMEOUT) == FALSE) {
926 connman_error("Failed to get ofono modem sim");
931 connman_error("D-Bus connection not available");
935 dbus_pending_call_set_notify(call, sim_properties_reply,
939 dbus_message_unref(message);
942 static int modem_change_powered(const char *path, dbus_bool_t powered)
944 DBusMessage *message;
945 DBusMessageIter iter;
946 DBusPendingCall *call;
948 DBG("path %s powered %d", path, powered);
953 message = dbus_message_new_method_call(OFONO_SERVICE, path,
954 OFONO_MODEM_INTERFACE, SET_PROPERTY);
958 dbus_message_set_auto_start(message, FALSE);
960 dbus_message_iter_init_append(message, &iter);
961 connman_dbus_property_append_basic(&iter, "Powered",
962 DBUS_TYPE_BOOLEAN, &powered);
964 if (dbus_connection_send_with_reply(connection, message,
965 &call, TIMEOUT) == FALSE) {
966 connman_error("Failed to change powered property");
967 dbus_message_unref(message);
972 connman_error("D-Bus connection not available");
973 dbus_message_unref(message);
977 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
979 dbus_message_unref(message);
984 static struct modem_data *add_modem(const char *path)
986 struct modem_data *modem;
991 modem = g_hash_table_lookup(modem_hash, path);
993 modem->available = TRUE;
998 modem = g_try_new0(struct modem_data, 1);
1002 modem->path = g_strdup(path);
1003 modem->device = NULL;
1004 modem->available = TRUE;
1006 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1011 static gboolean modem_has_gprs(DBusMessageIter *array)
1013 DBusMessageIter entry;
1015 dbus_message_iter_recurse(array, &entry);
1017 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1018 const char *interface;
1020 dbus_message_iter_get_basic(&entry, &interface);
1022 if (g_strcmp0(OFONO_GPRS_INTERFACE, interface) == 0)
1025 dbus_message_iter_next(&entry);
1031 static void modem_properties_reply(DBusPendingCall *call, void *user_data)
1034 DBusMessageIter array, dict;
1035 const char *path = user_data;
1037 DBG("path %s", path);
1039 reply = dbus_pending_call_steal_reply(call);
1041 if (dbus_message_iter_init(reply, &array) == FALSE)
1044 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1047 dbus_message_iter_recurse(&array, &dict);
1049 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1050 DBusMessageIter entry, value;
1052 dbus_bool_t powered;
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, "Powered") == TRUE) {
1061 dbus_message_iter_get_basic(&value, &powered);
1063 if (powered == FALSE) {
1064 modem_change_powered(path, TRUE);
1067 } else if (g_str_equal(key, "Interface") == TRUE) {
1068 if (modem_has_gprs(&value) == TRUE)
1072 dbus_message_iter_next(&dict);
1076 dbus_message_unref(reply);
1078 dbus_pending_call_unref(call);
1081 static void get_modem_properties(struct modem_data *modem)
1083 DBusMessage *message;
1084 DBusPendingCall *call;
1086 DBG("path %s", modem->path);
1088 if (modem->path == NULL)
1091 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1092 OFONO_MODEM_INTERFACE, GET_PROPERTIES);
1093 if (message == NULL)
1096 dbus_message_set_auto_start(message, FALSE);
1098 if (dbus_connection_send_with_reply(connection, message,
1099 &call, TIMEOUT) == FALSE) {
1100 connman_error("Failed to get ofono modem");
1105 connman_error("D-Bus connection not available");
1109 dbus_pending_call_set_notify(call, modem_properties_reply,
1110 (void *)modem->path, NULL);
1113 dbus_message_unref(message);
1116 static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
1118 struct modem_data *modem = value;
1120 modem->available = FALSE;
1123 static void modems_set_unavailable()
1125 g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
1128 static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
1130 struct modem_data *modem = value;
1132 if (modem->available == FALSE)
1133 g_hash_table_remove(modem_hash, key);
1136 static void cleanup_modems()
1138 g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
1141 static void update_modems(DBusMessageIter *array)
1143 DBusMessageIter entry;
1145 dbus_message_iter_recurse(array, &entry);
1147 modems_set_unavailable();
1149 while (dbus_message_iter_get_arg_type(&entry) ==
1150 DBUS_TYPE_OBJECT_PATH) {
1152 struct modem_data *modem;
1154 dbus_message_iter_get_basic(&entry, &path);
1156 modem = add_modem(path);
1158 get_modem_properties(modem);
1160 dbus_message_iter_next(&entry);
1166 static void manager_properties_reply(DBusPendingCall *call, void *user_data)
1169 DBusMessageIter array, dict;
1173 reply = dbus_pending_call_steal_reply(call);
1175 if (dbus_message_iter_init(reply, &array) == FALSE)
1178 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1181 dbus_message_iter_recurse(&array, &dict);
1183 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1184 DBusMessageIter entry, value;
1187 dbus_message_iter_recurse(&dict, &entry);
1188 dbus_message_iter_get_basic(&entry, &key);
1190 dbus_message_iter_next(&entry);
1191 dbus_message_iter_recurse(&entry, &value);
1193 if (g_str_equal(key, "Modems") == TRUE) {
1194 update_modems(&value);
1198 dbus_message_iter_next(&dict);
1202 dbus_message_unref(reply);
1204 dbus_pending_call_unref(call);
1207 static void modem_remove_device(struct modem_data *modem)
1209 if (modem->device == NULL)
1212 connman_device_unregister(modem->device);
1213 connman_device_unref(modem->device);
1215 modem->device = NULL;
1218 static void remove_modem(gpointer data)
1220 struct modem_data *modem = data;
1222 g_free(modem->path);
1224 modem_remove_device(modem);
1229 static void ofono_connect(DBusConnection *connection, void *user_data)
1231 DBusMessage *message;
1232 DBusPendingCall *call;
1234 DBG("connection %p", connection);
1236 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1237 g_free, remove_modem);
1239 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1240 OFONO_MANAGER_INTERFACE, GET_PROPERTIES);
1241 if (message == NULL)
1244 dbus_message_set_auto_start(message, FALSE);
1246 if (dbus_connection_send_with_reply(connection, message,
1247 &call, TIMEOUT) == FALSE) {
1248 connman_error("Failed to get ofono modems");
1253 connman_error("D-Bus connection not available");
1257 dbus_pending_call_set_notify(call, manager_properties_reply,
1261 dbus_message_unref(message);
1265 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1267 DBG("connection %p", connection);
1269 if (modem_hash == NULL)
1272 g_hash_table_destroy(modem_hash);
1277 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1280 const char *path = dbus_message_get_path(message);
1281 struct modem_data *modem;
1282 DBusMessageIter iter, value;
1285 DBG("path %s", path);
1287 modem = g_hash_table_lookup(modem_hash, path);
1291 if (dbus_message_iter_init(message, &iter) == FALSE)
1294 dbus_message_iter_get_basic(&iter, &key);
1296 dbus_message_iter_next(&iter);
1297 dbus_message_iter_recurse(&iter, &value);
1299 if (g_str_equal(key, "Powered") == TRUE) {
1300 dbus_bool_t powered;
1302 dbus_message_iter_get_basic(&value, &powered);
1303 if (powered == TRUE)
1306 modem_remove_device(modem);
1307 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1308 if (modem_has_gprs(&value) == TRUE) {
1309 if (modem->device == NULL)
1310 get_imsi(modem->path);
1311 } else if (modem->device != NULL)
1312 modem_remove_device(modem);
1318 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1321 const char *path = dbus_message_get_path(message);
1322 struct modem_data *modem;
1323 DBusMessageIter iter, value;
1326 DBG("path %s", path);
1328 modem = g_hash_table_lookup(modem_hash, path);
1332 if (dbus_message_iter_init(message, &iter) == FALSE)
1335 dbus_message_iter_get_basic(&iter, &key);
1337 dbus_message_iter_next(&iter);
1338 dbus_message_iter_recurse(&iter, &value);
1340 if (g_str_equal(key, "Attached") == TRUE) {
1341 dbus_bool_t attached;
1343 dbus_message_iter_get_basic(&value, &attached);
1345 DBG("Attached %d", attached);
1347 if (attached == TRUE)
1348 check_networks(modem);
1349 else if (modem->device != NULL)
1350 connman_device_remove_all_networks(modem->device);
1352 } else if (g_str_equal(key, "Status") == TRUE) {
1354 dbus_message_iter_get_basic(&value, &status);
1356 DBG("status %s", status);
1358 /* FIXME: add roaming support */
1359 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
1360 check_networks(modem);
1361 } else if (g_str_equal(key, "Powered") == TRUE) {
1362 dbus_bool_t powered;
1364 if (modem->device == NULL)
1367 dbus_message_iter_get_basic(&value, &powered);
1368 connman_device_set_powered(modem->device, powered);
1374 static gboolean manager_changed(DBusConnection *connection,
1375 DBusMessage *message, void *user_data)
1377 const char *path = dbus_message_get_path(message);
1378 DBusMessageIter iter, value;
1381 DBG("path %s", path);
1383 if (dbus_message_iter_init(message, &iter) == FALSE)
1386 dbus_message_iter_get_basic(&iter, &key);
1388 dbus_message_iter_next(&iter);
1389 dbus_message_iter_recurse(&iter, &value);
1391 if (g_str_equal(key, "Modems") == TRUE)
1392 update_modems(&value);
1397 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1399 DBusMessageIter entry;
1400 gchar *nameserver = NULL, *nameserver_old = NULL;
1404 dbus_message_iter_recurse(array, &entry);
1406 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1409 dbus_message_iter_get_basic(&entry, &dns);
1413 if (nameserver == NULL) {
1415 nameserver = g_strdup(dns);
1418 nameserver_old = nameserver;
1419 nameserver = g_strdup_printf("%s %s",
1420 nameserver_old, dns);
1421 g_free(nameserver_old);
1424 dbus_message_iter_next(&entry);
1427 parent->ipv4.nameserver = nameserver;
1430 static void update_settings(DBusMessageIter *array,
1431 struct connman_element *parent)
1433 DBusMessageIter dict;
1434 const char *interface = NULL;
1438 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1441 dbus_message_iter_recurse(array, &dict);
1443 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1444 DBusMessageIter entry, value;
1447 dbus_message_iter_recurse(&dict, &entry);
1448 dbus_message_iter_get_basic(&entry, &key);
1450 dbus_message_iter_next(&entry);
1451 dbus_message_iter_recurse(&entry, &value);
1453 if (g_str_equal(key, "Interface") == TRUE) {
1456 dbus_message_iter_get_basic(&value, &interface);
1458 DBG("interface %s", interface);
1460 index = connman_inet_ifindex(interface);
1462 connman_network_set_index(
1463 pending_network, index);
1465 connman_error("Can not find interface %s",
1469 } else if (g_str_equal(key, "Method") == TRUE) {
1472 dbus_message_iter_get_basic(&value, &method);
1473 if (g_strcmp0(method, "static") == 0) {
1475 parent->ipv4.method =
1476 CONNMAN_IPCONFIG_METHOD_FIXED;
1477 } else if (g_strcmp0(method, "dhcp") == 0) {
1479 parent->ipv4.method =
1480 CONNMAN_IPCONFIG_METHOD_DHCP;
1483 } else if (g_str_equal(key, "Address") == TRUE) {
1484 const char *address;
1486 dbus_message_iter_get_basic(&value, &address);
1488 DBG("address %s", address);
1490 parent->ipv4.address = g_strdup(address);
1491 } else if (g_str_equal(key, "Netmask") == TRUE) {
1492 const char *netmask;
1494 dbus_message_iter_get_basic(&value, &netmask);
1496 DBG("netmask %s", netmask);
1498 parent->ipv4.netmask = g_strdup(netmask);
1499 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1501 get_dns(&value, parent);
1502 } else if (g_str_equal(key, "Gateway") == TRUE) {
1503 const char *gateway;
1505 dbus_message_iter_get_basic(&value, &gateway);
1507 DBG("gateway %s", gateway);
1509 parent->ipv4.gateway = g_strdup(gateway);
1512 dbus_message_iter_next(&dict);
1515 /* deactive, oFono send NULL inteface before deactive signal */
1516 if (interface == NULL)
1517 connman_network_set_index(pending_network, -1);
1520 static void cleanup_ipconfig(struct connman_element *parent)
1522 g_free(parent->ipv4.address);
1523 parent->ipv4.address = NULL;
1525 g_free(parent->ipv4.netmask);
1526 parent->ipv4.netmask = NULL;
1528 g_free(parent->ipv4.nameserver);
1529 parent->ipv4.nameserver = NULL;
1531 g_free(parent->ipv4.gateway);
1532 parent->ipv4.gateway = NULL;
1534 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1537 static int static_network_set_connected(
1538 struct connman_network *pending_network,
1539 struct connman_element *parent,
1540 connman_bool_t connected)
1542 if (connected == TRUE) {
1543 struct connman_element *element;
1545 if (parent->ipv4.address == NULL)
1548 if (parent->ipv4.netmask == NULL)
1551 element = connman_element_create(NULL);
1552 if (element == NULL) {
1553 connman_error("Can not create connman_element");
1557 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
1558 element->index = parent->index;
1560 if (connman_element_register(element, parent) < 0) {
1561 connman_element_unref(element);
1565 cleanup_ipconfig(parent);
1567 connman_network_set_connected(pending_network, connected);
1572 connman_network_set_error(pending_network,
1573 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1575 cleanup_ipconfig(parent);
1580 static gboolean pri_context_changed(DBusConnection *connection,
1581 DBusMessage *message, void *user_data)
1583 const char *path = dbus_message_get_path(message);
1584 struct connman_element *parent;
1585 const char *pending_path;
1586 DBusMessageIter iter, value;
1589 DBG("pending_network %p, path %s", pending_network, path);
1591 if (pending_network == NULL)
1594 pending_path = connman_network_get_string(pending_network, "Path");
1595 if (g_strcmp0(pending_path, path) != 0)
1598 parent = connman_network_get_element(pending_network);
1600 if (dbus_message_iter_init(message, &iter) == FALSE)
1603 dbus_message_iter_get_basic(&iter, &key);
1605 dbus_message_iter_next(&iter);
1606 dbus_message_iter_recurse(&iter, &value);
1608 if (g_str_equal(key, "Settings") == TRUE) {
1610 update_settings(&value, parent);
1611 } else if (g_str_equal(key, "Active") == TRUE) {
1614 dbus_message_iter_get_basic(&value, &active);
1616 switch (parent->ipv4.method) {
1617 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1618 case CONNMAN_IPCONFIG_METHOD_OFF:
1619 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1621 case CONNMAN_IPCONFIG_METHOD_FIXED:
1622 if (static_network_set_connected(
1623 pending_network, parent, active) < 0)
1624 set_network_active(pending_network, FALSE);
1626 case CONNMAN_IPCONFIG_METHOD_DHCP:
1627 connman_network_set_method(pending_network,
1628 CONNMAN_IPCONFIG_METHOD_DHCP);
1629 connman_network_set_connected(pending_network, active);
1633 pending_network = NULL;
1640 static guint gprs_watch;
1641 static guint modem_watch;
1642 static guint manager_watch;
1643 static guint context_watch;
1645 static int ofono_init(void)
1649 connection = connman_dbus_get_connection();
1650 if (connection == NULL)
1653 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1654 ofono_connect, ofono_disconnect, NULL, NULL);
1656 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1657 OFONO_GPRS_INTERFACE,
1662 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1663 OFONO_MODEM_INTERFACE,
1668 manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1669 OFONO_MANAGER_INTERFACE,
1674 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1675 OFONO_PRI_CONTEXT_INTERFACE,
1677 pri_context_changed,
1680 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1681 manager_watch == 0 || context_watch == 0) {
1686 err = connman_network_driver_register(&network_driver);
1690 err = connman_device_driver_register(&modem_driver);
1692 connman_network_driver_unregister(&network_driver);
1699 g_dbus_remove_watch(connection, watch);
1700 g_dbus_remove_watch(connection, gprs_watch);
1701 g_dbus_remove_watch(connection, modem_watch);
1702 g_dbus_remove_watch(connection, manager_watch);
1703 g_dbus_remove_watch(connection, context_watch);
1705 dbus_connection_unref(connection);
1710 static void ofono_exit(void)
1712 g_dbus_remove_watch(connection, watch);
1713 g_dbus_remove_watch(connection, gprs_watch);
1714 g_dbus_remove_watch(connection, modem_watch);
1715 g_dbus_remove_watch(connection, manager_watch);
1716 g_dbus_remove_watch(connection, context_watch);
1718 ofono_disconnect(connection, NULL);
1720 connman_device_driver_unregister(&modem_driver);
1721 connman_network_driver_unregister(&network_driver);
1723 dbus_connection_unref(connection);
1726 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1727 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)