5 * Copyright (C) 2007-2009 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"
48 #define PROPERTY_CHANGED "PropertyChanged"
49 #define GET_PROPERTIES "GetProperties"
50 #define SET_PROPERTY "SetProperty"
54 static DBusConnection *connection;
56 static GHashTable *modem_hash = NULL;
60 struct connman_device *device;
64 static int modem_probe(struct connman_device *device)
66 DBG("device %p", device);
71 static void modem_remove(struct connman_device *device)
73 DBG("device %p", device);
76 static void powered_reply(DBusPendingCall *call, void *user_data)
83 dbus_error_init(&error);
85 reply = dbus_pending_call_steal_reply(call);
87 if (dbus_set_error_from_message(&error, reply)) {
88 connman_error("%s", error.message);
89 dbus_error_free(&error);
92 dbus_message_unref(reply);
95 static int gprs_change_powered(const char *path, dbus_bool_t powered)
99 DBusPendingCall *call;
101 DBG("path %s powered %d", path, powered);
106 message = dbus_message_new_method_call(OFONO_SERVICE, path,
107 OFONO_GPRS_INTERFACE, SET_PROPERTY);
111 dbus_message_set_auto_start(message, FALSE);
113 dbus_message_iter_init_append(message, &iter);
114 connman_dbus_property_append_basic(&iter, "Powered",
115 DBUS_TYPE_BOOLEAN, &powered);
117 if (dbus_connection_send_with_reply(connection, message,
118 &call, TIMEOUT) == FALSE) {
119 connman_error("Failed to change powered property");
120 dbus_message_unref(message);
125 connman_error("D-Bus connection not available");
126 dbus_message_unref(message);
130 dbus_pending_call_set_notify(call, powered_reply, (void *)path, NULL);
132 dbus_message_unref(message);
137 static int modem_enable(struct connman_device *device)
139 const char *path = connman_device_get_string(device, "Path");
141 DBG("device %p, path, %s", device, path);
143 return gprs_change_powered(path, TRUE);
146 static int modem_disable(struct connman_device *device)
148 const char *path = connman_device_get_string(device, "Path");
150 DBG("device %p, path %s", device, path);
152 return gprs_change_powered(path, FALSE);
155 static struct connman_device_driver modem_driver = {
157 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
158 .probe = modem_probe,
159 .remove = modem_remove,
160 .enable = modem_enable,
161 .disable = modem_disable,
164 static char *get_ident(const char *path)
171 ident = g_strdup(path + 1);
175 while ((pos = strchr(pos, '/')) != NULL)
181 static void config_network_reply(DBusPendingCall *call, void *user_data)
183 struct connman_network *network = user_data;
185 DBusMessageIter array, dict;
186 gboolean internet_type = FALSE;
188 DBG("network %p", network);
190 reply = dbus_pending_call_steal_reply(call);
194 if (dbus_message_iter_init(reply, &array) == FALSE)
197 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
200 dbus_message_iter_recurse(&array, &dict);
202 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
203 DBusMessageIter entry, value;
206 dbus_message_iter_recurse(&dict, &entry);
207 dbus_message_iter_get_basic(&entry, &key);
209 dbus_message_iter_next(&entry);
210 dbus_message_iter_recurse(&entry, &value);
212 if (g_str_equal(key, "Name") == TRUE) {
215 dbus_message_iter_get_basic(&value, &name);
216 connman_network_set_name(network, name);
217 } else if (g_str_equal(key, "Type") == TRUE) {
220 dbus_message_iter_get_basic(&value, &type);
221 if (g_strcmp0(type, "internet") == 0) {
222 internet_type = TRUE;
224 connman_network_set_protocol(network,
225 CONNMAN_NETWORK_PROTOCOL_IP);
227 internet_type = FALSE;
229 connman_network_set_protocol(network,
230 CONNMAN_NETWORK_PROTOCOL_UNKNOWN);
234 dbus_message_iter_next(&dict);
237 if (internet_type == TRUE) {
241 path = connman_network_get_string(network, "Path");
243 group = get_ident(path);
245 connman_network_set_group(network, group);
251 dbus_message_unref(reply);
254 static void config_network(struct connman_network *network, const char *path)
256 DBusMessage *message;
257 DBusPendingCall *call;
259 DBG("path %s", path);
261 message = dbus_message_new_method_call(OFONO_SERVICE, path,
262 OFONO_PRI_CONTEXT_INTERFACE, GET_PROPERTIES);
266 dbus_message_set_auto_start(message, FALSE);
268 if (dbus_connection_send_with_reply(connection, message,
269 &call, TIMEOUT) == FALSE) {
270 connman_error("Failed to get Primary Context");
275 connman_error("D-Bus connection not available");
279 dbus_pending_call_set_notify(call, config_network_reply,
280 (void *)network, NULL);
283 dbus_message_unref(message);
286 static int network_probe(struct connman_network *network)
290 path = connman_network_get_string(network, "Path");
292 DBG("network %p path %s", network, path);
294 config_network(network, path);
299 static struct connman_network *pending_network;
301 static gboolean pending_network_is_available(
302 struct connman_network *pending_network)
304 struct connman_device *device;
305 struct connman_network *network;
306 const char *identifier;
309 /* Modem may be removed during waiting for active reply */
310 device = connman_network_get_device(pending_network);
314 identifier = connman_network_get_identifier(pending_network);
316 ident = g_strdup(identifier);
318 connman_network_unref(pending_network);
320 /* network may be removed during waiting for active reply */
321 network = connman_device_get_network(device, ident);
331 static void set_active_reply(DBusPendingCall *call, void *user_data)
335 struct connman_network *network = user_data;
337 DBG("network %p", network);
339 if (pending_network_is_available(network) == FALSE)
342 reply = dbus_pending_call_steal_reply(call);
346 dbus_error_init(&error);
347 if (dbus_set_error_from_message(&error, reply)) {
348 if (connman_network_get_index(network) < 0)
349 connman_network_set_error(network,
350 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
352 pending_network = NULL;
354 connman_error("%s", error.message);
356 dbus_error_free(&error);
358 pending_network = network;
360 dbus_message_unref(reply);
363 static int set_network_active(struct connman_network *network,
366 DBusMessage *message;
367 DBusPendingCall *call;
368 DBusMessageIter iter;
370 const char *path = connman_network_get_string(network, "Path");
372 DBG("network %p, path %s, active %d", network, path, active);
377 message = dbus_message_new_method_call(OFONO_SERVICE, path,
378 OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
382 dbus_message_set_auto_start(message, FALSE);
384 dbus_message_iter_init_append(message, &iter);
385 connman_dbus_property_append_basic(&iter, "Active",
386 DBUS_TYPE_BOOLEAN, &active);
388 if (dbus_connection_send_with_reply(connection, message,
389 &call, TIMEOUT * 10) == FALSE) {
390 connman_error("Failed to connect service");
391 dbus_message_unref(message);
396 connman_error("D-Bus connection not available");
397 dbus_message_unref(message);
401 connman_network_ref(network);
403 dbus_pending_call_set_notify(call, set_active_reply, network, NULL);
405 dbus_message_unref(message);
413 static int network_connect(struct connman_network *network)
415 if (connman_network_get_index(network) >= 0)
418 return set_network_active(network, TRUE);
421 static int network_disconnect(struct connman_network *network)
423 if (connman_network_get_index(network) < 0)
426 return set_network_active(network, FALSE);
429 static void network_remove(struct connman_network *network)
431 DBG("network %p", network);
434 static struct connman_network_driver network_driver = {
436 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
437 .probe = network_probe,
438 .remove = network_remove,
439 .connect = network_connect,
440 .disconnect = network_disconnect,
443 static void add_network(struct connman_device *device, const char *path)
445 struct connman_network *network;
448 DBG("device %p path %s", device, path);
450 network = connman_device_get_network(device, path);
454 ident = get_ident(path);
456 network = connman_network_create(ident,
457 CONNMAN_NETWORK_TYPE_CELLULAR);
463 connman_network_set_string(network, "Path", path);
464 connman_network_set_available(network, TRUE);
465 connman_network_set_index(network, -1);
466 connman_device_add_network(device, network);
469 static void add_networks(struct connman_device *device, DBusMessageIter *array)
471 DBusMessageIter entry;
475 dbus_message_iter_recurse(array, &entry);
477 while (dbus_message_iter_get_arg_type(&entry) ==
478 DBUS_TYPE_OBJECT_PATH) {
481 dbus_message_iter_get_basic(&entry, &path);
483 add_network(device, path);
485 dbus_message_iter_next(&entry);
489 static void check_networks_reply(DBusPendingCall *call, void *user_data)
491 struct connman_device *device = user_data;
493 DBusMessageIter array, dict, contexts;
494 dbus_bool_t attached;
496 DBG("device %p", device);
498 reply = dbus_pending_call_steal_reply(call);
502 if (dbus_message_iter_init(reply, &array) == FALSE)
505 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
508 dbus_message_iter_recurse(&array, &dict);
510 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
511 DBusMessageIter entry, value;
514 dbus_message_iter_recurse(&dict, &entry);
515 dbus_message_iter_get_basic(&entry, &key);
517 dbus_message_iter_next(&entry);
518 dbus_message_iter_recurse(&entry, &value);
522 if (g_str_equal(key, "Attached") == TRUE) {
523 dbus_message_iter_get_basic(&value, &attached);
524 DBG("Attached %d", attached);
525 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
527 } else if (g_str_equal(key, "Status") == TRUE) {
530 dbus_message_iter_get_basic(&value, &status);
531 /* FIXME: add roaming support */
532 } else if (g_str_equal(key, "Powered") == TRUE) {
535 dbus_message_iter_get_basic(&value, &powered);
537 connman_device_set_powered(device, powered);
540 dbus_message_iter_next(&dict);
543 if (attached == TRUE)
544 add_networks(device, &contexts);
547 dbus_message_unref(reply);
550 static void check_networks(struct modem_data *modem)
552 DBusMessage *message;
553 DBusPendingCall *call;
554 struct connman_device *device;
556 DBG("modem %p", modem);
561 device = modem->device;
565 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
566 OFONO_GPRS_INTERFACE, GET_PROPERTIES);
570 dbus_message_set_auto_start(message, FALSE);
572 if (dbus_connection_send_with_reply(connection, message,
573 &call, TIMEOUT) == FALSE) {
574 connman_error("Failed to get ofono GPRS");
579 connman_error("D-Bus connection not available");
583 dbus_pending_call_set_notify(call, check_networks_reply,
584 (void *)device, NULL);
587 dbus_message_unref(message);
590 static void add_device(const char *path, const char *imsi)
592 struct modem_data *modem;
593 struct connman_device *device;
595 DBG("path %s imsi %s", path, imsi);
603 modem = g_hash_table_lookup(modem_hash, path);
607 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
611 connman_device_set_ident(device, imsi);
613 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
615 connman_device_set_string(device, "Path", path);
617 if (connman_device_register(device) < 0) {
618 connman_device_unref(device);
622 modem->device = device;
624 check_networks(modem);
627 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
629 const char *path = user_data;
631 DBusMessageIter array, dict;
633 DBG("path %s", path);
635 reply = dbus_pending_call_steal_reply(call);
639 if (dbus_message_iter_init(reply, &array) == FALSE)
642 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
645 dbus_message_iter_recurse(&array, &dict);
647 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
648 DBusMessageIter entry, value;
649 const char *key, *imsi;
651 dbus_message_iter_recurse(&dict, &entry);
652 dbus_message_iter_get_basic(&entry, &key);
654 dbus_message_iter_next(&entry);
655 dbus_message_iter_recurse(&entry, &value);
657 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
658 dbus_message_iter_get_basic(&value, &imsi);
660 add_device(path, imsi);
663 dbus_message_iter_next(&dict);
667 dbus_message_unref(reply);
670 static void get_imsi(const char *path)
672 DBusMessage *message;
673 DBusPendingCall *call;
675 DBG("path %s", path);
677 message = dbus_message_new_method_call(OFONO_SERVICE, path,
678 OFONO_SIM_INTERFACE, GET_PROPERTIES);
682 dbus_message_set_auto_start(message, FALSE);
684 if (dbus_connection_send_with_reply(connection, message,
685 &call, TIMEOUT) == FALSE) {
686 connman_error("Failed to get ofono modem sim");
691 connman_error("D-Bus connection not available");
695 dbus_pending_call_set_notify(call, sim_properties_reply,
699 dbus_message_unref(message);
702 static int modem_change_powered(const char *path, dbus_bool_t powered)
704 DBusMessage *message;
705 DBusMessageIter iter;
706 DBusPendingCall *call;
708 DBG("path %s powered %d", path, powered);
713 message = dbus_message_new_method_call(OFONO_SERVICE, path,
714 OFONO_MODEM_INTERFACE, SET_PROPERTY);
718 dbus_message_set_auto_start(message, FALSE);
720 dbus_message_iter_init_append(message, &iter);
721 connman_dbus_property_append_basic(&iter, "Powered",
722 DBUS_TYPE_BOOLEAN, &powered);
724 if (dbus_connection_send_with_reply(connection, message,
725 &call, TIMEOUT) == FALSE) {
726 connman_error("Failed to change powered property");
727 dbus_message_unref(message);
732 connman_error("D-Bus connection not available");
733 dbus_message_unref(message);
737 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
739 dbus_message_unref(message);
744 static struct modem_data *add_modem(const char *path)
746 struct modem_data *modem;
751 modem = g_hash_table_lookup(modem_hash, path);
753 modem->available = TRUE;
758 modem = g_try_new0(struct modem_data, 1);
762 modem->path = g_strdup(path);
763 modem->device = NULL;
764 modem->available = TRUE;
766 g_hash_table_insert(modem_hash, g_strdup(path), modem);
771 static gboolean modem_has_gprs(DBusMessageIter *array)
773 DBusMessageIter entry;
775 dbus_message_iter_recurse(array, &entry);
777 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
778 const char *interface;
780 dbus_message_iter_get_basic(&entry, &interface);
782 if (g_strcmp0(OFONO_GPRS_INTERFACE, interface) == 0)
785 dbus_message_iter_next(&entry);
791 static void modem_properties_reply(DBusPendingCall *call, void *user_data)
794 DBusMessageIter array, dict;
795 const char *path = user_data;
797 DBG("path %s", path);
799 reply = dbus_pending_call_steal_reply(call);
803 if (dbus_message_iter_init(reply, &array) == FALSE)
806 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
809 dbus_message_iter_recurse(&array, &dict);
811 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
812 DBusMessageIter entry, value;
816 dbus_message_iter_recurse(&dict, &entry);
817 dbus_message_iter_get_basic(&entry, &key);
819 dbus_message_iter_next(&entry);
820 dbus_message_iter_recurse(&entry, &value);
822 if (g_str_equal(key, "Powered") == TRUE) {
823 dbus_message_iter_get_basic(&value, &powered);
825 if (powered == FALSE) {
826 modem_change_powered(path, TRUE);
829 } else if (g_str_equal(key, "Interface") == TRUE) {
830 if (modem_has_gprs(&value) == TRUE)
834 dbus_message_iter_next(&dict);
838 dbus_message_unref(reply);
841 static void get_modem_properties(struct modem_data *modem)
843 DBusMessage *message;
844 DBusPendingCall *call;
846 DBG("path %s", modem->path);
848 if (modem->path == NULL)
851 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
852 OFONO_MODEM_INTERFACE, GET_PROPERTIES);
856 dbus_message_set_auto_start(message, FALSE);
858 if (dbus_connection_send_with_reply(connection, message,
859 &call, TIMEOUT) == FALSE) {
860 connman_error("Failed to get ofono modem");
865 connman_error("D-Bus connection not available");
869 dbus_pending_call_set_notify(call, modem_properties_reply,
870 (void *)modem->path, NULL);
873 dbus_message_unref(message);
876 static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
878 struct modem_data *modem = value;
880 modem->available = FALSE;
883 static void modems_set_unavailable()
885 g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
888 static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
890 struct modem_data *modem = value;
892 if (modem->available == FALSE)
893 g_hash_table_remove(modem_hash, key);
896 static void cleanup_modems()
898 g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
901 static void update_modems(DBusMessageIter *array)
903 DBusMessageIter entry;
905 dbus_message_iter_recurse(array, &entry);
907 modems_set_unavailable();
909 while (dbus_message_iter_get_arg_type(&entry) ==
910 DBUS_TYPE_OBJECT_PATH) {
912 struct modem_data *modem;
914 dbus_message_iter_get_basic(&entry, &path);
916 modem = add_modem(path);
918 get_modem_properties(modem);
920 dbus_message_iter_next(&entry);
926 static void manager_properties_reply(DBusPendingCall *call, void *user_data)
929 DBusMessageIter array, dict;
933 reply = dbus_pending_call_steal_reply(call);
937 if (dbus_message_iter_init(reply, &array) == FALSE)
940 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
943 dbus_message_iter_recurse(&array, &dict);
945 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
946 DBusMessageIter entry, value;
949 dbus_message_iter_recurse(&dict, &entry);
950 dbus_message_iter_get_basic(&entry, &key);
952 dbus_message_iter_next(&entry);
953 dbus_message_iter_recurse(&entry, &value);
955 if (g_str_equal(key, "Modems") == TRUE) {
956 update_modems(&value);
960 dbus_message_iter_next(&dict);
964 dbus_message_unref(reply);
967 static void modem_remove_device(struct modem_data *modem)
969 if (modem->device == NULL)
972 connman_device_unregister(modem->device);
973 connman_device_unref(modem->device);
975 modem->device = NULL;
978 static void remove_modem(gpointer data)
980 struct modem_data *modem = data;
984 modem_remove_device(modem);
989 static void ofono_connect(DBusConnection *connection, void *user_data)
991 DBusMessage *message;
992 DBusPendingCall *call;
994 DBG("connection %p", connection);
996 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
997 g_free, remove_modem);
999 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1000 OFONO_MANAGER_INTERFACE, GET_PROPERTIES);
1001 if (message == NULL)
1004 dbus_message_set_auto_start(message, FALSE);
1006 if (dbus_connection_send_with_reply(connection, message,
1007 &call, TIMEOUT) == FALSE) {
1008 connman_error("Failed to get ofono modems");
1013 connman_error("D-Bus connection not available");
1017 dbus_pending_call_set_notify(call, manager_properties_reply,
1021 dbus_message_unref(message);
1025 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1027 DBG("connection %p", connection);
1029 if (modem_hash == NULL)
1032 g_hash_table_destroy(modem_hash);
1037 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1040 const char *path = dbus_message_get_path(message);
1041 struct modem_data *modem;
1042 DBusMessageIter iter, value;
1045 DBG("path %s", path);
1047 modem = g_hash_table_lookup(modem_hash, path);
1051 if (dbus_message_iter_init(message, &iter) == FALSE)
1054 dbus_message_iter_get_basic(&iter, &key);
1056 dbus_message_iter_next(&iter);
1057 dbus_message_iter_recurse(&iter, &value);
1059 if (g_str_equal(key, "Powered") == TRUE) {
1060 dbus_bool_t powered;
1062 dbus_message_iter_get_basic(&value, &powered);
1063 if (powered == TRUE)
1066 modem_remove_device(modem);
1067 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1068 if (modem_has_gprs(&value) == TRUE) {
1069 if (modem->device == NULL)
1070 get_imsi(modem->path);
1071 } else if (modem->device != NULL)
1072 modem_remove_device(modem);
1078 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1081 const char *path = dbus_message_get_path(message);
1082 struct modem_data *modem;
1083 DBusMessageIter iter, value;
1086 DBG("path %s", path);
1088 modem = g_hash_table_lookup(modem_hash, path);
1092 if (dbus_message_iter_init(message, &iter) == FALSE)
1095 dbus_message_iter_get_basic(&iter, &key);
1097 dbus_message_iter_next(&iter);
1098 dbus_message_iter_recurse(&iter, &value);
1100 if (g_str_equal(key, "Attached") == TRUE) {
1101 dbus_bool_t attached;
1103 dbus_message_iter_get_basic(&value, &attached);
1105 DBG("Attached %d", attached);
1107 if (attached == TRUE)
1108 check_networks(modem);
1109 else if (modem->device != NULL)
1110 connman_device_remove_all_networks(modem->device);
1112 } else if (g_str_equal(key, "Status") == TRUE) {
1114 dbus_message_iter_get_basic(&value, &status);
1116 DBG("status %s", status);
1118 /* FIXME: add roaming support */
1119 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
1120 check_networks(modem);
1121 } else if (g_str_equal(key, "Powered") == TRUE) {
1122 dbus_bool_t powered;
1124 if (modem->device == NULL)
1127 dbus_message_iter_get_basic(&value, &powered);
1128 connman_device_set_powered(modem->device, powered);
1134 static gboolean manager_changed(DBusConnection *connection,
1135 DBusMessage *message, void *user_data)
1137 const char *path = dbus_message_get_path(message);
1138 DBusMessageIter iter, value;
1141 DBG("path %s", path);
1143 if (dbus_message_iter_init(message, &iter) == FALSE)
1146 dbus_message_iter_get_basic(&iter, &key);
1148 dbus_message_iter_next(&iter);
1149 dbus_message_iter_recurse(&iter, &value);
1151 if (g_str_equal(key, "Modems") == TRUE)
1152 update_modems(&value);
1157 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1159 DBusMessageIter entry;
1160 gchar *nameserver = NULL, *nameserver_old = NULL;
1164 dbus_message_iter_recurse(array, &entry);
1166 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1169 dbus_message_iter_get_basic(&entry, &dns);
1173 if (nameserver == NULL) {
1175 nameserver = g_strdup(dns);
1178 nameserver_old = nameserver;
1179 nameserver = g_strdup_printf("%s %s",
1180 nameserver_old, dns);
1181 g_free(nameserver_old);
1184 dbus_message_iter_next(&entry);
1187 parent->ipv4.nameserver = nameserver;
1190 static void update_settings(DBusMessageIter *array,
1191 struct connman_element *parent)
1193 DBusMessageIter dict;
1194 const char *interface = NULL;
1198 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1201 dbus_message_iter_recurse(array, &dict);
1203 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1204 DBusMessageIter entry, value;
1207 dbus_message_iter_recurse(&dict, &entry);
1208 dbus_message_iter_get_basic(&entry, &key);
1210 dbus_message_iter_next(&entry);
1211 dbus_message_iter_recurse(&entry, &value);
1213 if (g_str_equal(key, "Interface") == TRUE) {
1216 dbus_message_iter_get_basic(&value, &interface);
1218 DBG("interface %s", interface);
1220 index = connman_inet_ifindex(interface);
1222 connman_network_set_index(
1223 pending_network, index);
1225 connman_error("Can not find interface %s",
1229 } else if (g_str_equal(key, "Method") == TRUE) {
1232 dbus_message_iter_get_basic(&value, &method);
1233 if (g_strcmp0(method, "static") == 0) {
1235 parent->ipv4.method =
1236 CONNMAN_IPCONFIG_METHOD_FIXED;
1237 } else if (g_strcmp0(method, "dhcp") == 0) {
1239 parent->ipv4.method =
1240 CONNMAN_IPCONFIG_METHOD_DHCP;
1243 } else if (g_str_equal(key, "Address") == TRUE) {
1244 const char *address;
1246 dbus_message_iter_get_basic(&value, &address);
1248 DBG("address %s", address);
1250 parent->ipv4.address = g_strdup(address);
1251 } else if (g_str_equal(key, "Netmask") == TRUE) {
1252 const char *netmask;
1254 dbus_message_iter_get_basic(&value, &netmask);
1256 DBG("netmask %s", netmask);
1258 parent->ipv4.netmask = g_strdup(netmask);
1259 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1261 get_dns(&value, parent);
1262 } else if (g_str_equal(key, "Gateway") == TRUE) {
1263 const char *gateway;
1265 dbus_message_iter_get_basic(&value, &gateway);
1267 DBG("gateway %s", gateway);
1269 parent->ipv4.gateway = g_strdup(gateway);
1272 dbus_message_iter_next(&dict);
1275 /* deactive, oFono send NULL inteface before deactive signal */
1276 if (interface == NULL)
1277 connman_network_set_index(pending_network, -1);
1280 static void cleanup_ipconfig(struct connman_element *parent)
1282 g_free(parent->ipv4.address);
1283 parent->ipv4.address = NULL;
1285 g_free(parent->ipv4.netmask);
1286 parent->ipv4.netmask = NULL;
1288 g_free(parent->ipv4.nameserver);
1289 parent->ipv4.nameserver = NULL;
1291 g_free(parent->ipv4.gateway);
1292 parent->ipv4.gateway = NULL;
1294 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1297 static int static_network_set_connected(
1298 struct connman_network *pending_network,
1299 struct connman_element *parent,
1300 connman_bool_t connected)
1302 if (connected == TRUE) {
1303 struct connman_element *element;
1305 if (parent->ipv4.address == NULL)
1308 if (parent->ipv4.netmask == NULL)
1311 element = connman_element_create(NULL);
1312 if (element == NULL) {
1313 connman_error("Can not create connman_element");
1317 element->type = CONNMAN_ELEMENT_TYPE_IPV4;
1318 element->index = parent->index;
1320 if (connman_element_register(element, parent) < 0) {
1321 connman_element_unref(element);
1325 cleanup_ipconfig(parent);
1327 connman_network_set_connected(pending_network, connected);
1332 connman_network_set_error(pending_network,
1333 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1335 cleanup_ipconfig(parent);
1340 static gboolean pri_context_changed(DBusConnection *connection,
1341 DBusMessage *message, void *user_data)
1343 const char *path = dbus_message_get_path(message);
1344 struct connman_element *parent;
1345 const char *pending_path;
1346 DBusMessageIter iter, value;
1349 DBG("pending_network %p, path %s", pending_network, path);
1351 if (pending_network == NULL)
1354 pending_path = connman_network_get_string(pending_network, "Path");
1355 if (g_strcmp0(pending_path, path) != 0)
1358 parent = connman_network_get_element(pending_network);
1360 if (dbus_message_iter_init(message, &iter) == FALSE)
1363 dbus_message_iter_get_basic(&iter, &key);
1365 dbus_message_iter_next(&iter);
1366 dbus_message_iter_recurse(&iter, &value);
1368 if (g_str_equal(key, "Settings") == TRUE) {
1370 update_settings(&value, parent);
1371 } else if (g_str_equal(key, "Active") == TRUE) {
1374 dbus_message_iter_get_basic(&value, &active);
1376 switch (parent->ipv4.method) {
1377 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1378 case CONNMAN_IPCONFIG_METHOD_OFF:
1379 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1381 case CONNMAN_IPCONFIG_METHOD_FIXED:
1382 if (static_network_set_connected(
1383 pending_network, parent, active) < 0)
1384 set_network_active(pending_network, FALSE);
1386 case CONNMAN_IPCONFIG_METHOD_DHCP:
1387 connman_network_set_method(pending_network,
1388 CONNMAN_IPCONFIG_METHOD_DHCP);
1389 connman_network_set_connected(pending_network, active);
1393 pending_network = NULL;
1400 static guint gprs_watch;
1401 static guint modem_watch;
1402 static guint manager_watch;
1403 static guint context_watch;
1405 static int ofono_init(void)
1409 connection = connman_dbus_get_connection();
1410 if (connection == NULL)
1413 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1414 ofono_connect, ofono_disconnect, NULL, NULL);
1416 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1417 OFONO_GPRS_INTERFACE,
1422 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1423 OFONO_MODEM_INTERFACE,
1428 manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1429 OFONO_MANAGER_INTERFACE,
1434 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1435 OFONO_PRI_CONTEXT_INTERFACE,
1437 pri_context_changed,
1440 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1441 manager_watch == 0 || context_watch == 0) {
1446 err = connman_network_driver_register(&network_driver);
1450 err = connman_device_driver_register(&modem_driver);
1452 connman_network_driver_unregister(&network_driver);
1459 g_dbus_remove_watch(connection, watch);
1460 g_dbus_remove_watch(connection, gprs_watch);
1461 g_dbus_remove_watch(connection, modem_watch);
1462 g_dbus_remove_watch(connection, manager_watch);
1463 g_dbus_remove_watch(connection, context_watch);
1465 dbus_connection_unref(connection);
1470 static void ofono_exit(void)
1472 g_dbus_remove_watch(connection, watch);
1473 g_dbus_remove_watch(connection, gprs_watch);
1474 g_dbus_remove_watch(connection, modem_watch);
1475 g_dbus_remove_watch(connection, manager_watch);
1476 g_dbus_remove_watch(connection, context_watch);
1478 ofono_disconnect(connection, NULL);
1480 connman_device_driver_unregister(&modem_driver);
1481 connman_network_driver_unregister(&network_driver);
1483 dbus_connection_unref(connection);
1486 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1487 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)