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;
612 char *ident, *mcc, *mnc;
615 DBG("device %p path %s", device, path);
617 network = connman_device_get_network(device, path);
621 ident = get_ident(path);
623 network = connman_network_create(ident,
624 CONNMAN_NETWORK_TYPE_CELLULAR);
630 connman_network_set_string(network, "Path", path);
631 connman_network_set_available(network, TRUE);
632 connman_network_set_index(network, -1);
634 mcc_mnc = connman_device_get_string(device, "MCC_MNC");
635 if (mcc_mnc != NULL) {
636 mcc = g_strndup(mcc_mnc, 3);
637 connman_network_set_string(network, "Cellular.MCC", mcc);
640 mnc = g_strdup(mcc_mnc + 3);
641 connman_network_set_string(network, "Cellular.MNC", mnc);
645 connman_device_add_network(device, network);
648 static void add_networks(struct connman_device *device, DBusMessageIter *array)
650 DBusMessageIter entry;
654 dbus_message_iter_recurse(array, &entry);
656 while (dbus_message_iter_get_arg_type(&entry) ==
657 DBUS_TYPE_OBJECT_PATH) {
660 dbus_message_iter_get_basic(&entry, &path);
662 add_network(device, path);
664 dbus_message_iter_next(&entry);
668 static void create_context_reply(DBusPendingCall *call, void *user_data)
675 dbus_error_init(&error);
677 reply = dbus_pending_call_steal_reply(call);
679 if (dbus_set_error_from_message(&error, reply)) {
680 connman_error("%s", error.message);
681 dbus_error_free(&error);
684 dbus_message_unref(reply);
686 dbus_pending_call_unref(call);
689 static void add_default_context(DBusMessageIter *array,
690 const char *path, const char *name, const char *type)
692 DBusMessageIter entry;
693 DBusMessage *message;
694 DBusPendingCall *call;
701 dbus_message_iter_recurse(array, &entry);
703 if (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_OBJECT_PATH)
706 DBG("path %s, name %s, type %s", path, name, type);
708 message = dbus_message_new_method_call(OFONO_SERVICE, path,
709 OFONO_GPRS_INTERFACE, CREATE_CONTEXT);
713 dbus_message_set_auto_start(message, FALSE);
715 dbus_message_append_args(message, DBUS_TYPE_STRING,
716 &name, DBUS_TYPE_STRING,
717 &type, DBUS_TYPE_INVALID);
719 if (dbus_connection_send_with_reply(connection, message,
720 &call, TIMEOUT) == FALSE) {
721 connman_error("Failed to create default context");
722 dbus_message_unref(message);
727 connman_error("D-Bus connection not available");
728 dbus_message_unref(message);
732 dbus_pending_call_set_notify(call, create_context_reply, NULL, NULL);
734 dbus_message_unref(message);
737 static void check_networks_reply(DBusPendingCall *call, void *user_data)
739 struct connman_device *device = user_data;
741 DBusMessageIter array, dict, contexts;
742 dbus_bool_t attached;
744 DBG("device %p", device);
746 reply = dbus_pending_call_steal_reply(call);
748 if (dbus_message_iter_init(reply, &array) == FALSE)
751 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
754 dbus_message_iter_recurse(&array, &dict);
756 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
757 DBusMessageIter entry, value;
760 dbus_message_iter_recurse(&dict, &entry);
761 dbus_message_iter_get_basic(&entry, &key);
763 dbus_message_iter_next(&entry);
764 dbus_message_iter_recurse(&entry, &value);
768 if (g_str_equal(key, "Attached") == TRUE) {
769 dbus_message_iter_get_basic(&value, &attached);
770 DBG("Attached %d", attached);
771 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
774 path = connman_device_get_string(device, "Path");
776 add_default_context(&contexts, path,
777 CONTEXT_NAME, CONTEXT_TYPE);
778 } else if (g_str_equal(key, "Status") == TRUE) {
781 dbus_message_iter_get_basic(&value, &status);
782 /* FIXME: add roaming support */
783 } else if (g_str_equal(key, "Powered") == TRUE) {
786 dbus_message_iter_get_basic(&value, &powered);
788 connman_device_set_powered(device, powered);
791 dbus_message_iter_next(&dict);
794 if (attached == TRUE)
795 add_networks(device, &contexts);
798 dbus_message_unref(reply);
800 dbus_pending_call_unref(call);
803 static void check_networks(struct modem_data *modem)
805 DBusMessage *message;
806 DBusPendingCall *call;
807 struct connman_device *device;
809 DBG("modem %p", modem);
814 device = modem->device;
818 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
819 OFONO_GPRS_INTERFACE, GET_PROPERTIES);
823 dbus_message_set_auto_start(message, FALSE);
825 if (dbus_connection_send_with_reply(connection, message,
826 &call, TIMEOUT) == FALSE) {
827 connman_error("Failed to get ofono GPRS");
832 connman_error("D-Bus connection not available");
836 dbus_pending_call_set_notify(call, check_networks_reply,
837 (void *)device, NULL);
840 dbus_message_unref(message);
843 static void add_device(const char *path, const char *imsi,
844 unsigned char mnc_length)
846 struct modem_data *modem;
847 struct connman_device *device;
850 DBG("path %s imsi %s mnc_length %d", path, imsi, mnc_length);
858 modem = g_hash_table_lookup(modem_hash, path);
862 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
866 connman_device_set_ident(device, imsi);
868 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
870 connman_device_set_string(device, "Path", path);
872 if (mnc_length == 2 || mnc_length == 3) {
873 mcc_mnc = g_strndup(imsi, mnc_length + 3);
874 connman_device_set_string(device, "MCC_MNC", mcc_mnc);
878 if (connman_device_register(device) < 0) {
879 connman_device_unref(device);
883 modem->device = device;
885 check_networks(modem);
888 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
890 const char *path = user_data;
892 /* If MobileNetworkCodeLength is not provided, mnc_length is 0 */
893 unsigned char mnc_length = 0;
895 DBusMessageIter array, dict;
897 DBG("path %s", path);
899 reply = dbus_pending_call_steal_reply(call);
901 if (dbus_message_iter_init(reply, &array) == FALSE)
904 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
907 dbus_message_iter_recurse(&array, &dict);
909 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
910 DBusMessageIter entry, value;
913 dbus_message_iter_recurse(&dict, &entry);
914 dbus_message_iter_get_basic(&entry, &key);
916 dbus_message_iter_next(&entry);
917 dbus_message_iter_recurse(&entry, &value);
919 if (g_str_equal(key, "SubscriberIdentity") == TRUE)
920 dbus_message_iter_get_basic(&value, &imsi);
921 else if (g_str_equal(key, "MobileNetworkCodeLength") == TRUE)
922 dbus_message_iter_get_basic(&value,
923 (void *) &mnc_length);
925 dbus_message_iter_next(&dict);
928 add_device(path, imsi, mnc_length);
931 dbus_message_unref(reply);
933 dbus_pending_call_unref(call);
936 static void get_imsi(const char *path)
938 DBusMessage *message;
939 DBusPendingCall *call;
941 DBG("path %s", path);
943 message = dbus_message_new_method_call(OFONO_SERVICE, path,
944 OFONO_SIM_INTERFACE, GET_PROPERTIES);
948 dbus_message_set_auto_start(message, FALSE);
950 if (dbus_connection_send_with_reply(connection, message,
951 &call, TIMEOUT) == FALSE) {
952 connman_error("Failed to get ofono modem sim");
957 connman_error("D-Bus connection not available");
961 dbus_pending_call_set_notify(call, sim_properties_reply,
965 dbus_message_unref(message);
968 static int modem_change_powered(const char *path, dbus_bool_t powered)
970 DBusMessage *message;
971 DBusMessageIter iter;
972 DBusPendingCall *call;
974 DBG("path %s powered %d", path, powered);
979 message = dbus_message_new_method_call(OFONO_SERVICE, path,
980 OFONO_MODEM_INTERFACE, SET_PROPERTY);
984 dbus_message_set_auto_start(message, FALSE);
986 dbus_message_iter_init_append(message, &iter);
987 connman_dbus_property_append_basic(&iter, "Powered",
988 DBUS_TYPE_BOOLEAN, &powered);
990 if (dbus_connection_send_with_reply(connection, message,
991 &call, TIMEOUT) == FALSE) {
992 connman_error("Failed to change powered property");
993 dbus_message_unref(message);
998 connman_error("D-Bus connection not available");
999 dbus_message_unref(message);
1003 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
1005 dbus_message_unref(message);
1007 return -EINPROGRESS;
1010 static struct modem_data *add_modem(const char *path)
1012 struct modem_data *modem;
1017 modem = g_hash_table_lookup(modem_hash, path);
1018 if (modem != NULL) {
1019 modem->available = TRUE;
1024 modem = g_try_new0(struct modem_data, 1);
1028 modem->path = g_strdup(path);
1029 modem->device = NULL;
1030 modem->available = TRUE;
1032 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1037 static gboolean modem_has_gprs(DBusMessageIter *array)
1039 DBusMessageIter entry;
1041 dbus_message_iter_recurse(array, &entry);
1043 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1044 const char *interface;
1046 dbus_message_iter_get_basic(&entry, &interface);
1048 if (g_strcmp0(OFONO_GPRS_INTERFACE, interface) == 0)
1051 dbus_message_iter_next(&entry);
1057 static void modem_properties_reply(DBusPendingCall *call, void *user_data)
1060 DBusMessageIter array, dict;
1061 const char *path = user_data;
1063 DBG("path %s", path);
1065 reply = dbus_pending_call_steal_reply(call);
1067 if (dbus_message_iter_init(reply, &array) == FALSE)
1070 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1073 dbus_message_iter_recurse(&array, &dict);
1075 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1076 DBusMessageIter entry, value;
1078 dbus_bool_t powered;
1080 dbus_message_iter_recurse(&dict, &entry);
1081 dbus_message_iter_get_basic(&entry, &key);
1083 dbus_message_iter_next(&entry);
1084 dbus_message_iter_recurse(&entry, &value);
1086 if (g_str_equal(key, "Powered") == TRUE) {
1087 dbus_message_iter_get_basic(&value, &powered);
1089 if (powered == FALSE) {
1090 modem_change_powered(path, TRUE);
1093 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1094 if (modem_has_gprs(&value) == TRUE)
1098 dbus_message_iter_next(&dict);
1102 dbus_message_unref(reply);
1104 dbus_pending_call_unref(call);
1107 static void get_modem_properties(struct modem_data *modem)
1109 DBusMessage *message;
1110 DBusPendingCall *call;
1112 DBG("path %s", modem->path);
1114 if (modem->path == NULL)
1117 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1118 OFONO_MODEM_INTERFACE, GET_PROPERTIES);
1119 if (message == NULL)
1122 dbus_message_set_auto_start(message, FALSE);
1124 if (dbus_connection_send_with_reply(connection, message,
1125 &call, TIMEOUT) == FALSE) {
1126 connman_error("Failed to get ofono modem");
1131 connman_error("D-Bus connection not available");
1135 dbus_pending_call_set_notify(call, modem_properties_reply,
1136 (void *)modem->path, NULL);
1139 dbus_message_unref(message);
1142 static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
1144 struct modem_data *modem = value;
1146 modem->available = FALSE;
1149 static void modems_set_unavailable()
1151 g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
1154 static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
1156 struct modem_data *modem = value;
1158 if (modem->available == FALSE)
1159 g_hash_table_remove(modem_hash, key);
1162 static void cleanup_modems()
1164 g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
1167 static void update_modems(DBusMessageIter *array)
1169 DBusMessageIter entry;
1171 dbus_message_iter_recurse(array, &entry);
1173 modems_set_unavailable();
1175 while (dbus_message_iter_get_arg_type(&entry) ==
1176 DBUS_TYPE_OBJECT_PATH) {
1178 struct modem_data *modem;
1180 dbus_message_iter_get_basic(&entry, &path);
1182 modem = add_modem(path);
1184 get_modem_properties(modem);
1186 dbus_message_iter_next(&entry);
1192 static void manager_properties_reply(DBusPendingCall *call, void *user_data)
1195 DBusMessageIter array, dict;
1199 reply = dbus_pending_call_steal_reply(call);
1201 if (dbus_message_iter_init(reply, &array) == FALSE)
1204 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1207 dbus_message_iter_recurse(&array, &dict);
1209 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1210 DBusMessageIter entry, value;
1213 dbus_message_iter_recurse(&dict, &entry);
1214 dbus_message_iter_get_basic(&entry, &key);
1216 dbus_message_iter_next(&entry);
1217 dbus_message_iter_recurse(&entry, &value);
1219 if (g_str_equal(key, "Modems") == TRUE) {
1220 update_modems(&value);
1224 dbus_message_iter_next(&dict);
1228 dbus_message_unref(reply);
1230 dbus_pending_call_unref(call);
1233 static void modem_remove_device(struct modem_data *modem)
1235 if (modem->device == NULL)
1238 connman_device_unregister(modem->device);
1239 connman_device_unref(modem->device);
1241 modem->device = NULL;
1244 static void remove_modem(gpointer data)
1246 struct modem_data *modem = data;
1248 g_free(modem->path);
1250 modem_remove_device(modem);
1255 static void ofono_connect(DBusConnection *connection, void *user_data)
1257 DBusMessage *message;
1258 DBusPendingCall *call;
1260 DBG("connection %p", connection);
1262 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1263 g_free, remove_modem);
1265 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1266 OFONO_MANAGER_INTERFACE, GET_PROPERTIES);
1267 if (message == NULL)
1270 dbus_message_set_auto_start(message, FALSE);
1272 if (dbus_connection_send_with_reply(connection, message,
1273 &call, TIMEOUT) == FALSE) {
1274 connman_error("Failed to get ofono modems");
1279 connman_error("D-Bus connection not available");
1283 dbus_pending_call_set_notify(call, manager_properties_reply,
1287 dbus_message_unref(message);
1291 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1293 DBG("connection %p", connection);
1295 if (modem_hash == NULL)
1298 g_hash_table_destroy(modem_hash);
1303 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1306 const char *path = dbus_message_get_path(message);
1307 struct modem_data *modem;
1308 DBusMessageIter iter, value;
1311 DBG("path %s", path);
1313 modem = g_hash_table_lookup(modem_hash, path);
1317 if (dbus_message_iter_init(message, &iter) == FALSE)
1320 dbus_message_iter_get_basic(&iter, &key);
1322 dbus_message_iter_next(&iter);
1323 dbus_message_iter_recurse(&iter, &value);
1325 if (g_str_equal(key, "Powered") == TRUE) {
1326 dbus_bool_t powered;
1328 dbus_message_iter_get_basic(&value, &powered);
1329 if (powered == TRUE)
1332 modem_remove_device(modem);
1333 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1334 if (modem_has_gprs(&value) == TRUE) {
1335 if (modem->device == NULL)
1336 get_imsi(modem->path);
1337 } else if (modem->device != NULL)
1338 modem_remove_device(modem);
1344 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1347 const char *path = dbus_message_get_path(message);
1348 struct modem_data *modem;
1349 DBusMessageIter iter, value;
1352 DBG("path %s", path);
1354 modem = g_hash_table_lookup(modem_hash, path);
1358 if (dbus_message_iter_init(message, &iter) == FALSE)
1361 dbus_message_iter_get_basic(&iter, &key);
1363 dbus_message_iter_next(&iter);
1364 dbus_message_iter_recurse(&iter, &value);
1366 if (g_str_equal(key, "Attached") == TRUE) {
1367 dbus_bool_t attached;
1369 dbus_message_iter_get_basic(&value, &attached);
1371 DBG("Attached %d", attached);
1373 if (attached == TRUE)
1374 check_networks(modem);
1375 else if (modem->device != NULL)
1376 connman_device_remove_all_networks(modem->device);
1378 } else if (g_str_equal(key, "Status") == TRUE) {
1380 dbus_message_iter_get_basic(&value, &status);
1382 DBG("status %s", status);
1384 /* FIXME: add roaming support */
1385 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
1386 check_networks(modem);
1387 } else if (g_str_equal(key, "Powered") == TRUE) {
1388 dbus_bool_t powered;
1390 if (modem->device == NULL)
1393 dbus_message_iter_get_basic(&value, &powered);
1394 connman_device_set_powered(modem->device, powered);
1400 static gboolean manager_changed(DBusConnection *connection,
1401 DBusMessage *message, void *user_data)
1403 const char *path = dbus_message_get_path(message);
1404 DBusMessageIter iter, value;
1407 DBG("path %s", path);
1409 if (dbus_message_iter_init(message, &iter) == FALSE)
1412 dbus_message_iter_get_basic(&iter, &key);
1414 dbus_message_iter_next(&iter);
1415 dbus_message_iter_recurse(&iter, &value);
1417 if (g_str_equal(key, "Modems") == TRUE)
1418 update_modems(&value);
1423 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1425 DBusMessageIter entry;
1426 gchar *nameserver = NULL, *nameserver_old = NULL;
1430 dbus_message_iter_recurse(array, &entry);
1432 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1435 dbus_message_iter_get_basic(&entry, &dns);
1439 if (nameserver == NULL) {
1441 nameserver = g_strdup(dns);
1444 nameserver_old = nameserver;
1445 nameserver = g_strdup_printf("%s %s",
1446 nameserver_old, dns);
1447 g_free(nameserver_old);
1450 dbus_message_iter_next(&entry);
1453 parent->ipv4.nameserver = nameserver;
1456 static void update_settings(DBusMessageIter *array,
1457 struct connman_element *parent)
1459 DBusMessageIter dict;
1460 const char *interface = NULL;
1464 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1467 dbus_message_iter_recurse(array, &dict);
1469 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1470 DBusMessageIter entry, value;
1473 dbus_message_iter_recurse(&dict, &entry);
1474 dbus_message_iter_get_basic(&entry, &key);
1476 dbus_message_iter_next(&entry);
1477 dbus_message_iter_recurse(&entry, &value);
1479 if (g_str_equal(key, "Interface") == TRUE) {
1482 dbus_message_iter_get_basic(&value, &interface);
1484 DBG("interface %s", interface);
1486 index = connman_inet_ifindex(interface);
1488 connman_network_set_index(
1489 pending_network, index);
1491 connman_error("Can not find interface %s",
1495 } else if (g_str_equal(key, "Method") == TRUE) {
1498 dbus_message_iter_get_basic(&value, &method);
1499 if (g_strcmp0(method, "static") == 0) {
1501 parent->ipv4.method =
1502 CONNMAN_IPCONFIG_METHOD_FIXED;
1503 } else if (g_strcmp0(method, "dhcp") == 0) {
1505 parent->ipv4.method =
1506 CONNMAN_IPCONFIG_METHOD_DHCP;
1509 } else if (g_str_equal(key, "Address") == TRUE) {
1510 const char *address;
1512 dbus_message_iter_get_basic(&value, &address);
1514 DBG("address %s", address);
1516 parent->ipv4.address = g_strdup(address);
1517 } else if (g_str_equal(key, "Netmask") == TRUE) {
1518 const char *netmask;
1520 dbus_message_iter_get_basic(&value, &netmask);
1522 DBG("netmask %s", netmask);
1524 parent->ipv4.netmask = g_strdup(netmask);
1525 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1527 get_dns(&value, parent);
1528 } else if (g_str_equal(key, "Gateway") == TRUE) {
1529 const char *gateway;
1531 dbus_message_iter_get_basic(&value, &gateway);
1533 DBG("gateway %s", gateway);
1535 parent->ipv4.gateway = g_strdup(gateway);
1538 dbus_message_iter_next(&dict);
1541 /* deactive, oFono send NULL inteface before deactive signal */
1542 if (interface == NULL)
1543 connman_network_set_index(pending_network, -1);
1546 static void cleanup_ipconfig(struct connman_element *parent)
1548 g_free(parent->ipv4.address);
1549 parent->ipv4.address = NULL;
1551 g_free(parent->ipv4.netmask);
1552 parent->ipv4.netmask = NULL;
1554 g_free(parent->ipv4.nameserver);
1555 parent->ipv4.nameserver = NULL;
1557 g_free(parent->ipv4.gateway);
1558 parent->ipv4.gateway = NULL;
1560 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1563 static int static_network_set_connected(
1564 struct connman_network *pending_network,
1565 struct connman_element *parent,
1566 connman_bool_t connected)
1568 if (connected == TRUE) {
1569 struct connman_element *element;
1571 if (parent->ipv4.address == NULL)
1574 if (parent->ipv4.netmask == NULL)
1577 element = connman_element_create(NULL);
1578 if (element == NULL) {
1579 connman_error("Can not create connman_element");
1583 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
1584 element->index = parent->index;
1586 if (connman_element_register(element, parent) < 0) {
1587 connman_element_unref(element);
1591 cleanup_ipconfig(parent);
1593 connman_network_set_connected(pending_network, connected);
1598 connman_network_set_error(pending_network,
1599 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1601 cleanup_ipconfig(parent);
1606 static gboolean pri_context_changed(DBusConnection *connection,
1607 DBusMessage *message, void *user_data)
1609 const char *path = dbus_message_get_path(message);
1610 struct connman_element *parent;
1611 const char *pending_path;
1612 DBusMessageIter iter, value;
1615 DBG("pending_network %p, path %s", pending_network, path);
1617 if (pending_network == NULL)
1620 pending_path = connman_network_get_string(pending_network, "Path");
1621 if (g_strcmp0(pending_path, path) != 0)
1624 parent = connman_network_get_element(pending_network);
1626 if (dbus_message_iter_init(message, &iter) == FALSE)
1629 dbus_message_iter_get_basic(&iter, &key);
1631 dbus_message_iter_next(&iter);
1632 dbus_message_iter_recurse(&iter, &value);
1634 if (g_str_equal(key, "Settings") == TRUE) {
1636 update_settings(&value, parent);
1637 } else if (g_str_equal(key, "Active") == TRUE) {
1640 dbus_message_iter_get_basic(&value, &active);
1642 switch (parent->ipv4.method) {
1643 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1644 case CONNMAN_IPCONFIG_METHOD_OFF:
1645 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1647 case CONNMAN_IPCONFIG_METHOD_FIXED:
1648 connman_network_set_method(pending_network,
1649 CONNMAN_IPCONFIG_METHOD_FIXED);
1651 if (static_network_set_connected(
1652 pending_network, parent, active) < 0)
1653 set_network_active(pending_network, FALSE);
1655 case CONNMAN_IPCONFIG_METHOD_DHCP:
1656 connman_network_set_method(pending_network,
1657 CONNMAN_IPCONFIG_METHOD_DHCP);
1658 connman_network_set_connected(pending_network, active);
1662 pending_network = NULL;
1669 static guint gprs_watch;
1670 static guint modem_watch;
1671 static guint manager_watch;
1672 static guint context_watch;
1674 static int ofono_init(void)
1678 connection = connman_dbus_get_connection();
1679 if (connection == NULL)
1682 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1683 ofono_connect, ofono_disconnect, NULL, NULL);
1685 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1686 OFONO_GPRS_INTERFACE,
1691 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1692 OFONO_MODEM_INTERFACE,
1697 manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1698 OFONO_MANAGER_INTERFACE,
1703 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1704 OFONO_PRI_CONTEXT_INTERFACE,
1706 pri_context_changed,
1709 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1710 manager_watch == 0 || context_watch == 0) {
1715 err = connman_network_driver_register(&network_driver);
1719 err = connman_device_driver_register(&modem_driver);
1721 connman_network_driver_unregister(&network_driver);
1728 g_dbus_remove_watch(connection, watch);
1729 g_dbus_remove_watch(connection, gprs_watch);
1730 g_dbus_remove_watch(connection, modem_watch);
1731 g_dbus_remove_watch(connection, manager_watch);
1732 g_dbus_remove_watch(connection, context_watch);
1734 dbus_connection_unref(connection);
1739 static void ofono_exit(void)
1741 g_dbus_remove_watch(connection, watch);
1742 g_dbus_remove_watch(connection, gprs_watch);
1743 g_dbus_remove_watch(connection, modem_watch);
1744 g_dbus_remove_watch(connection, manager_watch);
1745 g_dbus_remove_watch(connection, context_watch);
1747 ofono_disconnect(connection, NULL);
1749 connman_device_driver_unregister(&modem_driver);
1750 connman_network_driver_unregister(&network_driver);
1752 dbus_connection_unref(connection);
1755 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1756 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)