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 pos = strrchr(path, '/');
182 return g_strdup(pos + 1);
185 static void create_service(struct connman_network *network)
192 path = connman_network_get_string(network, "Path");
194 group = get_ident(path);
196 connman_network_set_group(network, group);
201 static void set_network_name_reply(DBusPendingCall *call, void *user_data)
203 struct connman_network *network = user_data;
205 DBusMessageIter array, dict;
207 DBG("network %p", network);
209 reply = dbus_pending_call_steal_reply(call);
211 if (dbus_message_iter_init(reply, &array) == FALSE)
214 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
217 dbus_message_iter_recurse(&array, &dict);
219 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
220 DBusMessageIter entry, value;
223 dbus_message_iter_recurse(&dict, &entry);
224 dbus_message_iter_get_basic(&entry, &key);
226 dbus_message_iter_next(&entry);
227 dbus_message_iter_recurse(&entry, &value);
230 * 'Operator' is deprecated since version 0.20, but
231 * keep it here for backward compatibility reasons.
233 if (g_str_equal(key, "Operator") == TRUE ||
234 g_str_equal(key, "Name") == TRUE) {
237 dbus_message_iter_get_basic(&value, &name);
238 connman_network_set_name(network, name);
239 create_service(network);
242 dbus_message_iter_next(&dict);
245 dbus_message_unref(reply);
247 dbus_pending_call_unref(call);
250 static void set_network_name(struct connman_network *network)
252 struct connman_device *device;
253 DBusMessage *message;
254 DBusPendingCall *call;
257 device = connman_network_get_device(network);
259 path = connman_device_get_string(device, "Path");
263 DBG("path %s", path);
265 message = dbus_message_new_method_call(OFONO_SERVICE, path,
266 OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES);
270 dbus_message_set_auto_start(message, FALSE);
272 if (dbus_connection_send_with_reply(connection, message,
273 &call, TIMEOUT) == FALSE) {
274 connman_error("Failed to get operator");
279 connman_error("D-Bus connection not available");
283 dbus_pending_call_set_notify(call, set_network_name_reply,
284 (void *)network, NULL);
287 dbus_message_unref(message);
290 static void config_network_reply(DBusPendingCall *call, void *user_data)
292 struct connman_network *network = user_data;
294 DBusMessageIter array, dict;
295 gboolean internet_type = FALSE;
297 DBG("network %p", network);
299 reply = dbus_pending_call_steal_reply(call);
301 if (dbus_message_iter_init(reply, &array) == FALSE)
304 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
307 dbus_message_iter_recurse(&array, &dict);
309 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
310 DBusMessageIter entry, value;
313 dbus_message_iter_recurse(&dict, &entry);
314 dbus_message_iter_get_basic(&entry, &key);
316 dbus_message_iter_next(&entry);
317 dbus_message_iter_recurse(&entry, &value);
319 if (g_str_equal(key, "Type") == TRUE) {
322 dbus_message_iter_get_basic(&value, &type);
323 if (g_strcmp0(type, "internet") == 0) {
324 internet_type = TRUE;
326 connman_network_set_protocol(network,
327 CONNMAN_NETWORK_PROTOCOL_IP);
328 set_network_name(network);
330 internet_type = FALSE;
332 connman_network_set_protocol(network,
333 CONNMAN_NETWORK_PROTOCOL_UNKNOWN);
337 dbus_message_iter_next(&dict);
341 dbus_message_unref(reply);
343 dbus_pending_call_unref(call);
346 static void config_network(struct connman_network *network, const char *path)
348 DBusMessage *message;
349 DBusPendingCall *call;
351 DBG("path %s", path);
353 message = dbus_message_new_method_call(OFONO_SERVICE, path,
354 OFONO_PRI_CONTEXT_INTERFACE, GET_PROPERTIES);
358 dbus_message_set_auto_start(message, FALSE);
360 if (dbus_connection_send_with_reply(connection, message,
361 &call, TIMEOUT) == FALSE) {
362 connman_error("Failed to get Primary Context");
367 connman_error("D-Bus connection not available");
371 dbus_pending_call_set_notify(call, config_network_reply,
372 (void *)network, NULL);
375 dbus_message_unref(message);
378 static gboolean registration_changed(DBusConnection *connection,
379 DBusMessage *message, void *user_data)
381 const char *path = dbus_message_get_path(message);
382 struct connman_network *network = user_data;
383 DBusMessageIter iter, value;
386 DBG("path %s", path);
388 if (dbus_message_iter_init(message, &iter) == FALSE)
391 dbus_message_iter_get_basic(&iter, &key);
395 dbus_message_iter_next(&iter);
396 dbus_message_iter_recurse(&iter, &value);
398 if (g_strcmp0(key, "Name") == 0 ||
399 g_strcmp0(key, "Operator") == 0) {
402 dbus_message_iter_get_basic(&value, &name);
403 DBG("name %s", name);
404 connman_network_set_name(network, name);
405 create_service(network);
406 } else if (g_strcmp0(key, "Strength") == 0) {
407 connman_uint8_t strength;
409 dbus_message_iter_get_basic(&value, &strength);
410 connman_network_set_strength(network, strength);
411 connman_network_update(network);
417 static int network_probe(struct connman_network *network)
422 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
423 OFONO_REGISTRATION_INTERFACE,
425 registration_changed,
430 connman_network_set_data(network, GUINT_TO_POINTER(reg_watch));
432 path = connman_network_get_string(network, "Path");
434 DBG("network %p path %s", network, path);
436 config_network(network, path);
441 static struct connman_network *pending_network;
443 static gboolean pending_network_is_available(
444 struct connman_network *pending_network)
446 struct connman_device *device;
447 struct connman_network *network;
448 const char *identifier;
451 /* Modem may be removed during waiting for active reply */
452 device = connman_network_get_device(pending_network);
453 if (device == NULL) {
454 DBG("Modem is removed");
458 identifier = connman_network_get_identifier(pending_network);
460 ident = g_strdup(identifier);
462 connman_network_unref(pending_network);
464 /* network may be removed during waiting for active reply */
465 network = connman_device_get_network(device, ident);
475 static void set_active_reply(DBusPendingCall *call, void *user_data)
479 struct connman_network *network = user_data;
481 DBG("network %p", network);
483 reply = dbus_pending_call_steal_reply(call);
485 if (pending_network_is_available(network) == FALSE)
488 dbus_error_init(&error);
489 if (dbus_set_error_from_message(&error, reply)) {
490 if (connman_network_get_index(network) < 0)
491 connman_network_set_error(network,
492 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
494 pending_network = NULL;
496 connman_error("%s", error.message);
498 dbus_error_free(&error);
500 pending_network = network;
503 dbus_message_unref(reply);
505 dbus_pending_call_unref(call);
508 static int set_network_active(struct connman_network *network,
511 DBusMessage *message;
512 DBusPendingCall *call;
513 DBusMessageIter iter;
515 const char *path = connman_network_get_string(network, "Path");
517 DBG("network %p, path %s, active %d", network, path, active);
522 message = dbus_message_new_method_call(OFONO_SERVICE, path,
523 OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
527 dbus_message_set_auto_start(message, FALSE);
529 dbus_message_iter_init_append(message, &iter);
530 connman_dbus_property_append_basic(&iter, "Active",
531 DBUS_TYPE_BOOLEAN, &active);
533 if (dbus_connection_send_with_reply(connection, message,
534 &call, TIMEOUT * 10) == FALSE) {
535 connman_error("Failed to connect service");
536 dbus_message_unref(message);
541 connman_error("D-Bus connection not available");
542 dbus_message_unref(message);
546 connman_network_ref(network);
548 dbus_pending_call_set_notify(call, set_active_reply, network, NULL);
550 dbus_message_unref(message);
558 static void set_apn_reply(DBusPendingCall *call, void *user_data)
563 reply = dbus_pending_call_steal_reply(call);
565 dbus_error_init(&error);
566 if (dbus_set_error_from_message(&error, reply)) {
567 connman_error("%s", error.message);
569 dbus_error_free(&error);
572 dbus_message_unref(reply);
574 dbus_pending_call_unref(call);
577 static void set_apn(struct connman_network *network)
579 DBusMessage *message;
580 DBusPendingCall *call;
581 DBusMessageIter iter;
582 const char *apn, *path;
584 apn = connman_network_get_string(network, "Cellular.APN");
588 path = connman_network_get_string(network, "Path");
592 DBG("path %s, apn %s", path, apn);
594 message = dbus_message_new_method_call(OFONO_SERVICE, path,
595 OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
599 dbus_message_set_auto_start(message, FALSE);
601 dbus_message_iter_init_append(message, &iter);
602 connman_dbus_property_append_basic(&iter, "AccessPointName",
603 DBUS_TYPE_STRING, &apn);
605 if (dbus_connection_send_with_reply(connection, message,
606 &call, TIMEOUT) == FALSE) {
607 dbus_message_unref(message);
612 connman_error("D-Bus connection not available");
613 dbus_message_unref(message);
617 dbus_pending_call_set_notify(call, set_apn_reply, NULL, NULL);
619 dbus_message_unref(message);
622 static int network_connect(struct connman_network *network)
624 if (connman_network_get_index(network) >= 0)
627 return set_network_active(network, TRUE);
630 static int network_disconnect(struct connman_network *network)
632 if (connman_network_get_index(network) < 0)
635 return set_network_active(network, FALSE);
638 static void network_remove(struct connman_network *network)
642 DBG("network %p", network);
644 reg_watch = GPOINTER_TO_UINT(connman_network_get_data(network));
645 g_dbus_remove_watch(connection, reg_watch);
648 static int network_setup(struct connman_network *network, const char *key)
652 if (g_strcmp0(key, "Cellular.APN") == 0)
658 static struct connman_network_driver network_driver = {
660 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
661 .probe = network_probe,
662 .remove = network_remove,
663 .connect = network_connect,
664 .disconnect = network_disconnect,
665 .setup = network_setup,
668 static void add_network(struct connman_device *device, const char *path)
670 struct connman_network *network;
675 DBG("device %p path %s", device, path);
677 network = connman_device_get_network(device, path);
681 ident = get_ident(path);
683 network = connman_network_create(ident,
684 CONNMAN_NETWORK_TYPE_CELLULAR);
690 connman_network_set_string(network, "Path", path);
691 connman_network_set_available(network, TRUE);
692 connman_network_set_index(network, -1);
694 mcc = connman_device_get_string(device, "MCC");
696 connman_network_set_string(network, "Cellular.MCC", mcc);
698 mnc = connman_device_get_string(device, "MNC");
700 connman_network_set_string(network, "Cellular.MNC", mnc);
702 connman_device_add_network(device, network);
705 static void add_networks(struct connman_device *device, DBusMessageIter *array)
707 DBusMessageIter entry;
711 dbus_message_iter_recurse(array, &entry);
713 while (dbus_message_iter_get_arg_type(&entry) ==
714 DBUS_TYPE_OBJECT_PATH) {
717 dbus_message_iter_get_basic(&entry, &path);
719 add_network(device, path);
721 dbus_message_iter_next(&entry);
725 static void create_context_reply(DBusPendingCall *call, void *user_data)
732 dbus_error_init(&error);
734 reply = dbus_pending_call_steal_reply(call);
736 if (dbus_set_error_from_message(&error, reply)) {
737 connman_error("%s", error.message);
738 dbus_error_free(&error);
741 dbus_message_unref(reply);
743 dbus_pending_call_unref(call);
746 static void add_default_context(DBusMessageIter *array,
747 const char *path, const char *name, const char *type)
749 DBusMessageIter entry;
750 DBusMessage *message;
751 DBusPendingCall *call;
758 dbus_message_iter_recurse(array, &entry);
760 if (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_OBJECT_PATH)
763 DBG("path %s, name %s, type %s", path, name, type);
765 message = dbus_message_new_method_call(OFONO_SERVICE, path,
766 OFONO_GPRS_INTERFACE, CREATE_CONTEXT);
770 dbus_message_set_auto_start(message, FALSE);
772 dbus_message_append_args(message, DBUS_TYPE_STRING,
773 &name, DBUS_TYPE_STRING,
774 &type, DBUS_TYPE_INVALID);
776 if (dbus_connection_send_with_reply(connection, message,
777 &call, TIMEOUT) == FALSE) {
778 connman_error("Failed to create default context");
779 dbus_message_unref(message);
784 connman_error("D-Bus connection not available");
785 dbus_message_unref(message);
789 dbus_pending_call_set_notify(call, create_context_reply, NULL, NULL);
791 dbus_message_unref(message);
794 static void check_networks_reply(DBusPendingCall *call, void *user_data)
796 struct connman_device *device = user_data;
798 DBusMessageIter array, dict, contexts;
799 dbus_bool_t attached;
801 DBG("device %p", device);
803 reply = dbus_pending_call_steal_reply(call);
805 if (dbus_message_iter_init(reply, &array) == FALSE)
808 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
811 dbus_message_iter_recurse(&array, &dict);
813 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
814 DBusMessageIter entry, value;
817 dbus_message_iter_recurse(&dict, &entry);
818 dbus_message_iter_get_basic(&entry, &key);
820 dbus_message_iter_next(&entry);
821 dbus_message_iter_recurse(&entry, &value);
825 if (g_str_equal(key, "Attached") == TRUE) {
826 dbus_message_iter_get_basic(&value, &attached);
827 DBG("Attached %d", attached);
828 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
831 path = connman_device_get_string(device, "Path");
833 add_default_context(&contexts, path,
834 CONTEXT_NAME, CONTEXT_TYPE);
835 } else if (g_str_equal(key, "Powered") == TRUE) {
838 dbus_message_iter_get_basic(&value, &powered);
840 connman_device_set_powered(device, powered);
843 dbus_message_iter_next(&dict);
846 if (attached == TRUE)
847 add_networks(device, &contexts);
850 dbus_message_unref(reply);
852 dbus_pending_call_unref(call);
855 static void check_networks(struct modem_data *modem)
857 DBusMessage *message;
858 DBusPendingCall *call;
859 struct connman_device *device;
861 DBG("modem %p", modem);
866 device = modem->device;
870 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
871 OFONO_GPRS_INTERFACE, GET_PROPERTIES);
875 dbus_message_set_auto_start(message, FALSE);
877 if (dbus_connection_send_with_reply(connection, message,
878 &call, TIMEOUT) == FALSE) {
879 connman_error("Failed to get ofono GPRS");
884 connman_error("D-Bus connection not available");
888 dbus_pending_call_set_notify(call, check_networks_reply,
889 (void *)device, NULL);
892 dbus_message_unref(message);
895 static void add_device(const char *path, const char *imsi,
896 const char *mcc, const char *mnc)
898 struct modem_data *modem;
899 struct connman_device *device;
901 DBG("path %s imsi %s", path, imsi);
909 modem = g_hash_table_lookup(modem_hash, path);
913 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
917 connman_device_set_ident(device, imsi);
919 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
921 connman_device_set_string(device, "Path", path);
923 connman_device_set_string(device, "MCC", mcc);
925 connman_device_set_string(device, "MNC", mnc);
927 if (connman_device_register(device) < 0) {
928 connman_device_unref(device);
932 modem->device = device;
934 check_networks(modem);
937 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
939 const char *path = user_data;
943 /* If MobileNetworkCodeLength is not provided, mnc_length is 0 */
944 unsigned char mnc_length = 0;
946 DBusMessageIter array, dict;
948 DBG("path %s", path);
950 reply = dbus_pending_call_steal_reply(call);
952 if (dbus_message_iter_init(reply, &array) == FALSE)
955 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
958 dbus_message_iter_recurse(&array, &dict);
960 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
961 DBusMessageIter entry, value;
964 dbus_message_iter_recurse(&dict, &entry);
965 dbus_message_iter_get_basic(&entry, &key);
967 dbus_message_iter_next(&entry);
968 dbus_message_iter_recurse(&entry, &value);
970 if (g_str_equal(key, "SubscriberIdentity") == TRUE)
971 dbus_message_iter_get_basic(&value, &imsi);
973 * 'MobileNetworkCodeLength' is deprecated since version 0.20, but
974 * keep it here for backward compatibility reasons.
976 else if (g_str_equal(key, "MobileNetworkCodeLength") == TRUE)
977 dbus_message_iter_get_basic(&value,
978 (void *) &mnc_length);
979 else if (g_str_equal(key, "MobileCountryCode") == TRUE)
980 dbus_message_iter_get_basic(&value,
982 else if (g_str_equal(key, "MobileNetworkCode") == TRUE)
983 dbus_message_iter_get_basic(&value,
986 dbus_message_iter_next(&dict);
989 if (mnc_length == 2 || mnc_length == 3) {
990 mcc = g_strndup(imsi, 3);
991 mnc = g_strndup(imsi + 3, mnc_length);
994 add_device(path, imsi, mcc, mnc);
996 if (mnc_length == 2 || mnc_length == 3) {
1002 dbus_message_unref(reply);
1004 dbus_pending_call_unref(call);
1007 static void get_imsi(const char *path)
1009 DBusMessage *message;
1010 DBusPendingCall *call;
1012 DBG("path %s", path);
1014 message = dbus_message_new_method_call(OFONO_SERVICE, path,
1015 OFONO_SIM_INTERFACE, GET_PROPERTIES);
1016 if (message == NULL)
1019 dbus_message_set_auto_start(message, FALSE);
1021 if (dbus_connection_send_with_reply(connection, message,
1022 &call, TIMEOUT) == FALSE) {
1023 connman_error("Failed to get ofono modem sim");
1028 connman_error("D-Bus connection not available");
1032 dbus_pending_call_set_notify(call, sim_properties_reply,
1033 (void *)path, NULL);
1036 dbus_message_unref(message);
1039 static int modem_change_powered(const char *path, dbus_bool_t powered)
1041 DBusMessage *message;
1042 DBusMessageIter iter;
1043 DBusPendingCall *call;
1045 DBG("path %s powered %d", path, powered);
1050 message = dbus_message_new_method_call(OFONO_SERVICE, path,
1051 OFONO_MODEM_INTERFACE, SET_PROPERTY);
1052 if (message == NULL)
1055 dbus_message_set_auto_start(message, FALSE);
1057 dbus_message_iter_init_append(message, &iter);
1058 connman_dbus_property_append_basic(&iter, "Powered",
1059 DBUS_TYPE_BOOLEAN, &powered);
1061 if (dbus_connection_send_with_reply(connection, message,
1062 &call, TIMEOUT) == FALSE) {
1063 connman_error("Failed to change powered property");
1064 dbus_message_unref(message);
1069 connman_error("D-Bus connection not available");
1070 dbus_message_unref(message);
1074 dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
1076 dbus_message_unref(message);
1078 return -EINPROGRESS;
1081 static struct modem_data *add_modem(const char *path)
1083 struct modem_data *modem;
1088 modem = g_hash_table_lookup(modem_hash, path);
1089 if (modem != NULL) {
1090 modem->available = TRUE;
1095 modem = g_try_new0(struct modem_data, 1);
1099 modem->path = g_strdup(path);
1100 modem->device = NULL;
1101 modem->available = TRUE;
1103 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1108 static gboolean modem_has_gprs(DBusMessageIter *array)
1110 DBusMessageIter entry;
1112 dbus_message_iter_recurse(array, &entry);
1114 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1115 const char *interface;
1117 dbus_message_iter_get_basic(&entry, &interface);
1119 if (g_strcmp0(OFONO_GPRS_INTERFACE, interface) == 0)
1122 dbus_message_iter_next(&entry);
1128 static void modem_properties_reply(DBusPendingCall *call, void *user_data)
1131 DBusMessageIter array, dict;
1132 const char *path = user_data;
1134 DBG("path %s", path);
1136 reply = dbus_pending_call_steal_reply(call);
1138 if (dbus_message_iter_init(reply, &array) == FALSE)
1141 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1144 dbus_message_iter_recurse(&array, &dict);
1146 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1147 DBusMessageIter entry, value;
1149 dbus_bool_t powered;
1151 dbus_message_iter_recurse(&dict, &entry);
1152 dbus_message_iter_get_basic(&entry, &key);
1154 dbus_message_iter_next(&entry);
1155 dbus_message_iter_recurse(&entry, &value);
1157 if (g_str_equal(key, "Powered") == TRUE) {
1158 dbus_message_iter_get_basic(&value, &powered);
1160 if (powered == FALSE) {
1161 modem_change_powered(path, TRUE);
1164 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1165 if (modem_has_gprs(&value) == TRUE)
1169 dbus_message_iter_next(&dict);
1173 dbus_message_unref(reply);
1175 dbus_pending_call_unref(call);
1178 static void get_modem_properties(struct modem_data *modem)
1180 DBusMessage *message;
1181 DBusPendingCall *call;
1183 DBG("path %s", modem->path);
1185 if (modem->path == NULL)
1188 message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
1189 OFONO_MODEM_INTERFACE, GET_PROPERTIES);
1190 if (message == NULL)
1193 dbus_message_set_auto_start(message, FALSE);
1195 if (dbus_connection_send_with_reply(connection, message,
1196 &call, TIMEOUT) == FALSE) {
1197 connman_error("Failed to get ofono modem");
1202 connman_error("D-Bus connection not available");
1206 dbus_pending_call_set_notify(call, modem_properties_reply,
1207 (void *)modem->path, NULL);
1210 dbus_message_unref(message);
1213 static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
1215 struct modem_data *modem = value;
1217 modem->available = FALSE;
1220 static void modems_set_unavailable()
1222 g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
1225 static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
1227 struct modem_data *modem = value;
1229 if (modem->available == FALSE)
1230 g_hash_table_remove(modem_hash, key);
1233 static void cleanup_modems()
1235 g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
1238 static void update_modems(DBusMessageIter *array)
1240 DBusMessageIter entry;
1242 dbus_message_iter_recurse(array, &entry);
1244 modems_set_unavailable();
1246 while (dbus_message_iter_get_arg_type(&entry) ==
1247 DBUS_TYPE_OBJECT_PATH) {
1249 struct modem_data *modem;
1251 dbus_message_iter_get_basic(&entry, &path);
1253 modem = add_modem(path);
1255 get_modem_properties(modem);
1257 dbus_message_iter_next(&entry);
1263 static void manager_properties_reply(DBusPendingCall *call, void *user_data)
1266 DBusMessageIter array, dict;
1270 reply = dbus_pending_call_steal_reply(call);
1272 if (dbus_message_iter_init(reply, &array) == FALSE)
1275 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
1278 dbus_message_iter_recurse(&array, &dict);
1280 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1281 DBusMessageIter entry, value;
1284 dbus_message_iter_recurse(&dict, &entry);
1285 dbus_message_iter_get_basic(&entry, &key);
1287 dbus_message_iter_next(&entry);
1288 dbus_message_iter_recurse(&entry, &value);
1290 if (g_str_equal(key, "Modems") == TRUE) {
1291 update_modems(&value);
1295 dbus_message_iter_next(&dict);
1299 dbus_message_unref(reply);
1301 dbus_pending_call_unref(call);
1304 static void modem_remove_device(struct modem_data *modem)
1306 if (modem->device == NULL)
1309 connman_device_unregister(modem->device);
1310 connman_device_unref(modem->device);
1312 modem->device = NULL;
1315 static void remove_modem(gpointer data)
1317 struct modem_data *modem = data;
1319 g_free(modem->path);
1321 modem_remove_device(modem);
1326 static void ofono_connect(DBusConnection *connection, void *user_data)
1328 DBusMessage *message;
1329 DBusPendingCall *call;
1331 DBG("connection %p", connection);
1333 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1334 g_free, remove_modem);
1336 message = dbus_message_new_method_call(OFONO_SERVICE, "/",
1337 OFONO_MANAGER_INTERFACE, GET_PROPERTIES);
1338 if (message == NULL)
1341 dbus_message_set_auto_start(message, FALSE);
1343 if (dbus_connection_send_with_reply(connection, message,
1344 &call, TIMEOUT) == FALSE) {
1345 connman_error("Failed to get ofono modems");
1350 connman_error("D-Bus connection not available");
1354 dbus_pending_call_set_notify(call, manager_properties_reply,
1358 dbus_message_unref(message);
1362 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1364 DBG("connection %p", connection);
1366 if (modem_hash == NULL)
1369 g_hash_table_destroy(modem_hash);
1374 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1377 const char *path = dbus_message_get_path(message);
1378 struct modem_data *modem;
1379 DBusMessageIter iter, value;
1382 DBG("path %s", path);
1384 modem = g_hash_table_lookup(modem_hash, path);
1388 if (dbus_message_iter_init(message, &iter) == FALSE)
1391 dbus_message_iter_get_basic(&iter, &key);
1393 dbus_message_iter_next(&iter);
1394 dbus_message_iter_recurse(&iter, &value);
1396 if (g_str_equal(key, "Powered") == TRUE) {
1397 dbus_bool_t powered;
1399 dbus_message_iter_get_basic(&value, &powered);
1400 if (powered == TRUE)
1403 modem_remove_device(modem);
1404 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1405 if (modem_has_gprs(&value) == TRUE) {
1406 if (modem->device == NULL)
1407 get_imsi(modem->path);
1408 } else if (modem->device != NULL)
1409 modem_remove_device(modem);
1415 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1418 const char *path = dbus_message_get_path(message);
1419 struct modem_data *modem;
1420 DBusMessageIter iter, value;
1423 DBG("path %s", path);
1425 modem = g_hash_table_lookup(modem_hash, path);
1429 if (dbus_message_iter_init(message, &iter) == FALSE)
1432 dbus_message_iter_get_basic(&iter, &key);
1434 dbus_message_iter_next(&iter);
1435 dbus_message_iter_recurse(&iter, &value);
1437 if (g_str_equal(key, "Attached") == TRUE) {
1438 dbus_bool_t attached;
1440 dbus_message_iter_get_basic(&value, &attached);
1442 DBG("Attached %d", attached);
1444 if (attached == TRUE)
1445 check_networks(modem);
1446 else if (modem->device != NULL)
1447 connman_device_remove_all_networks(modem->device);
1449 } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
1450 check_networks(modem);
1451 } else if (g_str_equal(key, "Powered") == TRUE) {
1452 dbus_bool_t powered;
1454 if (modem->device == NULL)
1457 dbus_message_iter_get_basic(&value, &powered);
1458 connman_device_set_powered(modem->device, powered);
1464 static gboolean manager_changed(DBusConnection *connection,
1465 DBusMessage *message, void *user_data)
1467 const char *path = dbus_message_get_path(message);
1468 DBusMessageIter iter, value;
1471 DBG("path %s", path);
1473 if (dbus_message_iter_init(message, &iter) == FALSE)
1476 dbus_message_iter_get_basic(&iter, &key);
1478 dbus_message_iter_next(&iter);
1479 dbus_message_iter_recurse(&iter, &value);
1481 if (g_str_equal(key, "Modems") == TRUE)
1482 update_modems(&value);
1487 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1489 DBusMessageIter entry;
1490 gchar *nameserver = NULL, *nameserver_old = NULL;
1494 dbus_message_iter_recurse(array, &entry);
1496 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1499 dbus_message_iter_get_basic(&entry, &dns);
1503 if (nameserver == NULL) {
1505 nameserver = g_strdup(dns);
1508 nameserver_old = nameserver;
1509 nameserver = g_strdup_printf("%s %s",
1510 nameserver_old, dns);
1511 g_free(nameserver_old);
1514 dbus_message_iter_next(&entry);
1517 parent->ipv4.nameserver = nameserver;
1520 static void update_settings(DBusMessageIter *array,
1521 struct connman_element *parent)
1523 DBusMessageIter dict;
1524 const char *interface = NULL;
1528 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1531 dbus_message_iter_recurse(array, &dict);
1533 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1534 DBusMessageIter entry, value;
1537 dbus_message_iter_recurse(&dict, &entry);
1538 dbus_message_iter_get_basic(&entry, &key);
1540 dbus_message_iter_next(&entry);
1541 dbus_message_iter_recurse(&entry, &value);
1543 if (g_str_equal(key, "Interface") == TRUE) {
1546 dbus_message_iter_get_basic(&value, &interface);
1548 DBG("interface %s", interface);
1550 index = connman_inet_ifindex(interface);
1552 connman_network_set_index(
1553 pending_network, index);
1555 connman_error("Can not find interface %s",
1559 } else if (g_str_equal(key, "Method") == TRUE) {
1562 dbus_message_iter_get_basic(&value, &method);
1563 if (g_strcmp0(method, "static") == 0) {
1565 parent->ipv4.method =
1566 CONNMAN_IPCONFIG_METHOD_FIXED;
1567 } else if (g_strcmp0(method, "dhcp") == 0) {
1569 parent->ipv4.method =
1570 CONNMAN_IPCONFIG_METHOD_DHCP;
1573 } else if (g_str_equal(key, "Address") == TRUE) {
1574 const char *address;
1576 dbus_message_iter_get_basic(&value, &address);
1578 DBG("address %s", address);
1580 parent->ipv4.address = g_strdup(address);
1581 } else if (g_str_equal(key, "Netmask") == TRUE) {
1582 const char *netmask;
1584 dbus_message_iter_get_basic(&value, &netmask);
1586 DBG("netmask %s", netmask);
1588 parent->ipv4.netmask = g_strdup(netmask);
1589 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1591 get_dns(&value, parent);
1592 } else if (g_str_equal(key, "Gateway") == TRUE) {
1593 const char *gateway;
1595 dbus_message_iter_get_basic(&value, &gateway);
1597 DBG("gateway %s", gateway);
1599 parent->ipv4.gateway = g_strdup(gateway);
1602 dbus_message_iter_next(&dict);
1605 /* deactive, oFono send NULL inteface before deactive signal */
1606 if (interface == NULL)
1607 connman_network_set_index(pending_network, -1);
1610 static void cleanup_ipconfig(struct connman_element *parent)
1612 g_free(parent->ipv4.address);
1613 parent->ipv4.address = NULL;
1615 g_free(parent->ipv4.netmask);
1616 parent->ipv4.netmask = NULL;
1618 g_free(parent->ipv4.nameserver);
1619 parent->ipv4.nameserver = NULL;
1621 g_free(parent->ipv4.gateway);
1622 parent->ipv4.gateway = NULL;
1624 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1627 static int static_network_set_connected(
1628 struct connman_network *pending_network,
1629 struct connman_element *parent,
1630 connman_bool_t connected)
1632 if (connected == FALSE)
1633 cleanup_ipconfig(parent);
1635 connman_network_set_connected(pending_network, connected);
1640 static gboolean pri_context_changed(DBusConnection *connection,
1641 DBusMessage *message, void *user_data)
1643 const char *path = dbus_message_get_path(message);
1644 struct connman_element *parent;
1645 const char *pending_path;
1646 DBusMessageIter iter, value;
1649 DBG("pending_network %p, path %s", pending_network, path);
1651 if (pending_network == NULL)
1654 pending_path = connman_network_get_string(pending_network, "Path");
1655 if (g_strcmp0(pending_path, path) != 0)
1658 parent = connman_network_get_element(pending_network);
1660 if (dbus_message_iter_init(message, &iter) == FALSE)
1663 dbus_message_iter_get_basic(&iter, &key);
1665 dbus_message_iter_next(&iter);
1666 dbus_message_iter_recurse(&iter, &value);
1668 if (g_str_equal(key, "Settings") == TRUE) {
1670 update_settings(&value, parent);
1671 } else if (g_str_equal(key, "Active") == TRUE) {
1674 dbus_message_iter_get_basic(&value, &active);
1676 switch (parent->ipv4.method) {
1677 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1678 case CONNMAN_IPCONFIG_METHOD_OFF:
1679 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1681 case CONNMAN_IPCONFIG_METHOD_FIXED:
1682 connman_network_set_method(pending_network,
1683 CONNMAN_IPCONFIG_METHOD_FIXED);
1685 if (static_network_set_connected(
1686 pending_network, parent, active) < 0)
1687 set_network_active(pending_network, FALSE);
1689 case CONNMAN_IPCONFIG_METHOD_DHCP:
1690 connman_network_set_method(pending_network,
1691 CONNMAN_IPCONFIG_METHOD_DHCP);
1692 connman_network_set_connected(pending_network, active);
1696 pending_network = NULL;
1703 static guint gprs_watch;
1704 static guint modem_watch;
1705 static guint manager_watch;
1706 static guint context_watch;
1708 static int ofono_init(void)
1712 connection = connman_dbus_get_connection();
1713 if (connection == NULL)
1716 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1717 ofono_connect, ofono_disconnect, NULL, NULL);
1719 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1720 OFONO_GPRS_INTERFACE,
1725 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1726 OFONO_MODEM_INTERFACE,
1731 manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1732 OFONO_MANAGER_INTERFACE,
1737 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1738 OFONO_PRI_CONTEXT_INTERFACE,
1740 pri_context_changed,
1743 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1744 manager_watch == 0 || context_watch == 0) {
1749 err = connman_network_driver_register(&network_driver);
1753 err = connman_device_driver_register(&modem_driver);
1755 connman_network_driver_unregister(&network_driver);
1762 g_dbus_remove_watch(connection, watch);
1763 g_dbus_remove_watch(connection, gprs_watch);
1764 g_dbus_remove_watch(connection, modem_watch);
1765 g_dbus_remove_watch(connection, manager_watch);
1766 g_dbus_remove_watch(connection, context_watch);
1768 dbus_connection_unref(connection);
1773 static void ofono_exit(void)
1775 g_dbus_remove_watch(connection, watch);
1776 g_dbus_remove_watch(connection, gprs_watch);
1777 g_dbus_remove_watch(connection, modem_watch);
1778 g_dbus_remove_watch(connection, manager_watch);
1779 g_dbus_remove_watch(connection, context_watch);
1781 ofono_disconnect(connection, NULL);
1783 connman_device_driver_unregister(&modem_driver);
1784 connman_network_driver_unregister(&network_driver);
1786 dbus_connection_unref(connection);
1789 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1790 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)