5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/element.h>
35 #include <connman/device.h>
36 #include <connman/network.h>
37 #include <connman/dbus.h>
38 #include <connman/inet.h>
39 #include <connman/log.h>
41 #define OFONO_SERVICE "org.ofono"
43 #define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
44 #define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
45 #define OFONO_GPRS_INTERFACE OFONO_SERVICE ".ConnectionManager"
46 #define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
47 #define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
48 #define OFONO_REGISTRATION_INTERFACE OFONO_SERVICE ".NetworkRegistration"
50 #define PROPERTY_CHANGED "PropertyChanged"
51 #define GET_PROPERTIES "GetProperties"
52 #define SET_PROPERTY "SetProperty"
53 #define CONTEXT_ADDED "ContextAdded"
54 #define CONTEXT_REMOVED "ContextRemoved"
55 #define ADD_CONTEXT "AddContext"
56 #define GET_MODEMS "GetModems"
57 #define MODEM_ADDED "ModemAdded"
58 #define MODEM_REMOVED "ModemRemoved"
63 static DBusConnection *connection;
65 static GHashTable *modem_hash = NULL;
67 static GHashTable *network_hash;
71 struct connman_device *device;
76 gboolean pending_online;
77 gboolean requested_online;
80 uint8_t strength, has_strength;
83 static int modem_probe(struct connman_device *device)
85 DBG("device %p", device);
90 static void modem_remove(struct connman_device *device)
92 DBG("device %p", device);
95 static int call_ofono(const char *path,
96 const char *interface, const char *method,
97 DBusPendingCallNotifyFunction notify, void *user_data,
98 DBusFreeFunction free_function,
101 DBusMessage *message;
102 DBusPendingCall *call;
106 DBG("path %s %s.%s", path, interface, method);
111 message = dbus_message_new_method_call(OFONO_SERVICE, path,
116 dbus_message_set_auto_start(message, FALSE);
119 ok = dbus_message_append_args_valist(message, type, va);
125 if (dbus_connection_send_with_reply(connection, message,
126 &call, TIMEOUT) == FALSE) {
127 connman_error("Failed to call %s.%s", interface, method);
128 dbus_message_unref(message);
133 connman_error("D-Bus connection not available");
134 dbus_message_unref(message);
138 dbus_pending_call_set_notify(call, notify, user_data, free_function);
140 dbus_message_unref(message);
145 static void set_property_reply(DBusPendingCall *call, void *user_data)
149 char const *name = user_data;
153 dbus_error_init(&error);
155 reply = dbus_pending_call_steal_reply(call);
157 if (dbus_set_error_from_message(&error, reply)) {
158 connman_error("SetProperty(%s) %s %s", name,
159 error.name, error.message);
160 dbus_error_free(&error);
163 dbus_message_unref(reply);
165 dbus_pending_call_unref(call);
168 static int set_property(const char *path, const char *interface,
169 const char *property, int type, void *value,
170 DBusPendingCallNotifyFunction notify, void *user_data,
171 DBusFreeFunction free_function)
173 DBusMessage *message;
174 DBusMessageIter iter;
175 DBusPendingCall *call;
177 DBG("path %s %s.%s", path, interface, property);
179 g_assert(notify == NULL ? free_function == NULL : 1);
184 message = dbus_message_new_method_call(OFONO_SERVICE, path,
185 interface, SET_PROPERTY);
189 dbus_message_set_auto_start(message, FALSE);
191 dbus_message_iter_init_append(message, &iter);
192 connman_dbus_property_append_basic(&iter, property, type, value);
194 if (dbus_connection_send_with_reply(connection, message,
195 &call, TIMEOUT) == FALSE) {
196 connman_error("Failed to change \"%s\" property on %s",
197 property, interface);
198 dbus_message_unref(message);
203 connman_error("D-Bus connection not available");
204 dbus_message_unref(message);
208 if (notify == NULL) {
209 notify = set_property_reply;
210 user_data = (void *)property;
211 free_function = NULL;
214 dbus_pending_call_set_notify(call, notify, user_data, free_function);
216 dbus_message_unref(message);
221 static void update_modem_online(struct modem_data *modem,
222 connman_bool_t online)
224 DBG("modem %p path %s online %d", modem, modem->path, online);
226 modem->online = online;
227 modem->requested_online = online;
228 modem->pending_online = FALSE;
231 connman_device_set_powered(modem->device, online);
234 static void set_online_reply(DBusPendingCall *call, void *user_data)
236 struct modem_data *modem;
241 DBG("path %s", (char *)user_data);
243 if (modem_hash == NULL)
246 modem = g_hash_table_lookup(modem_hash, user_data);
250 reply = dbus_pending_call_steal_reply(call);
252 dbus_error_init(&error);
254 if (dbus_set_error_from_message(&error, reply)) {
255 connman_error("SetProperty(Online) %s %s",
256 error.name, error.message);
257 dbus_error_free(&error);
259 result = modem->online;
261 result = modem->requested_online;
263 if (modem->pending_online)
264 update_modem_online(modem, result);
266 dbus_message_unref(reply);
268 dbus_pending_call_unref(call);
271 static int modem_change_online(char const *path, dbus_bool_t online)
273 return set_property(path, OFONO_MODEM_INTERFACE, "Online",
274 DBUS_TYPE_BOOLEAN, &online,
276 (void *)g_strdup(path), g_free);
279 static int modem_enable(struct connman_device *device)
281 const char *path = connman_device_get_string(device, "Path");
283 DBG("device %p, path, %s", device, path);
285 return modem_change_online(path, TRUE);
288 static int modem_disable(struct connman_device *device)
290 const char *path = connman_device_get_string(device, "Path");
292 DBG("device %p path %s", device, path);
294 return modem_change_online(path, FALSE);
297 static struct connman_device_driver modem_driver = {
299 .type = CONNMAN_DEVICE_TYPE_CELLULAR,
300 .probe = modem_probe,
301 .remove = modem_remove,
302 .enable = modem_enable,
303 .disable = modem_disable,
306 static void modem_remove_device(struct modem_data *modem)
308 DBG("modem %p path %s device %p", modem, modem->path, modem->device);
310 if (modem->device == NULL)
313 connman_device_remove_all_networks(modem->device);
314 connman_device_unregister(modem->device);
315 connman_device_unref(modem->device);
317 modem->device = NULL;
320 static void remove_modem(gpointer data)
322 struct modem_data *modem = data;
324 modem_remove_device(modem);
331 static void remove_network(gpointer data)
333 connman_network_unref(data);
336 static char *get_ident(const char *path)
343 pos = strrchr(path, '/');
350 static void create_service(struct connman_network *network)
357 path = connman_network_get_string(network, "Path");
359 group = get_ident(path);
361 connman_network_set_group(network, group);
364 static int network_probe(struct connman_network *network)
366 create_service(network);
370 static gboolean pending_network_is_available(struct connman_network *network)
372 /* Modem or network may be removed */
373 if (network == NULL || connman_network_get_device(network) == NULL) {
374 DBG("Modem or network was removed");
381 static void set_active_reply(DBusPendingCall *call, void *user_data)
383 char const *path = user_data;
386 struct connman_network *network;
388 network = g_hash_table_lookup(network_hash, path);
390 DBG("path %s network %p", path, network);
392 reply = dbus_pending_call_steal_reply(call);
394 if (!pending_network_is_available(network))
397 dbus_error_init(&error);
399 if (dbus_set_error_from_message(&error, reply)) {
400 connman_error("SetProperty(Active) %s %s",
401 error.name, error.message);
403 if (connman_network_get_index(network) < 0)
404 connman_network_set_error(network,
405 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
407 dbus_error_free(&error);
411 dbus_message_unref(reply);
413 dbus_pending_call_unref(call);
416 static int set_network_active(struct connman_network *network,
421 const char *path = connman_network_get_string(network, "Path");
423 DBG("network %p, path %s, active %d", network, path, active);
425 error = set_property(path, OFONO_CONTEXT_INTERFACE,
426 "Active", DBUS_TYPE_BOOLEAN, &active,
427 set_active_reply, g_strdup(path), NULL);
429 if (active == FALSE && error == -EINPROGRESS)
435 static void set_apn(struct connman_network *network)
437 const char *apn, *path;
439 apn = connman_network_get_string(network, "Cellular.APN");
443 path = connman_network_get_string(network, "Path");
447 DBG("path %s, apn %s", path, apn);
449 set_property(path, OFONO_CONTEXT_INTERFACE,
450 "AccessPointName", DBUS_TYPE_STRING, &apn,
454 static int network_connect(struct connman_network *network)
456 DBG("network %p", network);
458 if (connman_network_get_index(network) >= 0)
461 return set_network_active(network, TRUE);
464 static int network_disconnect(struct connman_network *network)
466 DBG("network %p", network);
468 if (connman_network_get_index(network) < 0)
471 return set_network_active(network, FALSE);
474 static void network_remove(struct connman_network *network)
476 char const *path = connman_network_get_string(network, "Path");
478 DBG("network %p path %s", network, path);
480 g_hash_table_remove(network_hash, path);
483 static int network_setup(struct connman_network *network, const char *key)
485 if (g_strcmp0(key, "Cellular.APN") == 0)
491 static struct connman_network_driver network_driver = {
493 .type = CONNMAN_NETWORK_TYPE_CELLULAR,
494 .probe = network_probe,
495 .remove = network_remove,
496 .connect = network_connect,
497 .disconnect = network_disconnect,
498 .setup = network_setup,
501 static void update_settings(DBusMessageIter *array,
502 struct connman_network *network);
504 static void set_connected(struct connman_network *network,
505 connman_bool_t connected);
507 static int add_network(struct connman_device *device,
508 const char *path, DBusMessageIter *dict)
510 struct modem_data *modem = connman_device_get_data(device);
511 struct connman_network *network;
513 const char *hash_path;
514 char const *operator;
515 char const *reg_status;
516 dbus_bool_t active = FALSE;
518 DBG("modem %p device %p path %s", modem, device, path);
520 ident = get_ident(path);
522 network = connman_device_get_network(device, ident);
526 network = g_hash_table_lookup(network_hash, path);
527 if (network != NULL) {
528 DBG("path %p already exists with device %p", path,
529 connman_network_get_device(network));
530 if (connman_network_get_device(network))
532 g_hash_table_remove(network_hash, path);
535 network = connman_network_create(ident, CONNMAN_NETWORK_TYPE_CELLULAR);
539 connman_network_set_string(network, "Path", path);
540 hash_path = connman_network_get_string(network, "Path");
541 if (hash_path == NULL)
543 connman_network_ref(network);
544 g_hash_table_insert(network_hash, (char *)hash_path, network);
546 connman_network_set_available(network, TRUE);
547 connman_network_set_index(network, -1);
549 operator = connman_device_get_string(device, "Operator");
551 connman_network_set_name(network, operator);
553 if (modem->has_strength)
554 connman_network_set_strength(network, modem->strength);
556 reg_status = connman_device_get_string(device, "RegistrationStatus");
558 if (!g_strcmp0(reg_status, "roaming"))
559 connman_network_set_roaming(network, TRUE);
560 else if (!g_strcmp0(reg_status, "registered"))
561 connman_network_set_roaming(network, FALSE);
563 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
564 DBusMessageIter entry, value;
567 dbus_message_iter_recurse(dict, &entry);
568 dbus_message_iter_get_basic(&entry, &key);
570 dbus_message_iter_next(&entry);
571 dbus_message_iter_recurse(&entry, &value);
573 if (g_str_equal(key, "Type")) {
576 dbus_message_iter_get_basic(&value, &type);
577 if (g_strcmp0(type, "internet") == 0) {
578 connman_network_set_protocol(network,
579 CONNMAN_NETWORK_PROTOCOL_IP);
581 DBG("path %p type %s", path, type);
584 } else if (g_str_equal(key, "AccessPointName")) {
587 dbus_message_iter_get_basic(&value, &ap);
589 connman_network_set_string(network, "Cellular.APN", ap);
590 } else if (g_str_equal(key, "Settings"))
591 update_settings(&value, network);
592 else if (g_str_equal(key, "Active") == TRUE)
593 dbus_message_iter_get_basic(&value, &active);
595 dbus_message_iter_next(dict);
598 if (connman_device_add_network(device, network) != 0)
602 set_connected(network, active);
607 connman_network_unref(network);
608 g_hash_table_remove(network_hash, hash_path);
612 static void check_networks_reply(DBusPendingCall *call, void *user_data)
614 char *path = user_data;
615 struct modem_data *modem;
617 DBusMessageIter array, entry, value, properties;
619 DBG("path %s", path);
621 modem = g_hash_table_lookup(modem_hash, path);
624 if (modem->device == NULL)
627 reply = dbus_pending_call_steal_reply(call);
629 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
632 dbus_message_iter_init(reply, &array);
634 dbus_message_iter_recurse(&array, &entry);
636 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
637 char const *network_path;
639 dbus_message_iter_recurse(&entry, &value);
640 dbus_message_iter_get_basic(&value, &network_path);
642 dbus_message_iter_next(&value);
643 dbus_message_iter_recurse(&value, &properties);
645 add_network(modem->device, network_path, &properties);
647 dbus_message_iter_next(&entry);
651 dbus_message_unref(reply);
653 dbus_pending_call_unref(call);
656 static void check_networks(struct modem_data *modem)
658 char const *path = modem->path;
660 DBG("modem %p path %s", modem, path);
662 call_ofono(path, OFONO_GPRS_INTERFACE, "GetContexts",
663 check_networks_reply, g_strdup(path), g_free,
667 static void modem_operator_name_changed(struct modem_data *modem,
670 struct connman_device *device = modem->device;
677 connman_device_set_string(device, "Operator", name);
679 for (g_hash_table_iter_init(&i, network_hash);
680 g_hash_table_iter_next(&i, NULL, &value);) {
681 struct connman_network *network = value;
683 if (connman_network_get_device(network) == device) {
684 connman_network_set_name(network, name);
685 connman_network_update(network);
690 static void modem_strength_changed(struct modem_data *modem, uint8_t strength)
692 struct connman_device *device = modem->device;
696 modem->strength = strength;
697 modem->has_strength = TRUE;
702 for (g_hash_table_iter_init(&i, network_hash);
703 g_hash_table_iter_next(&i, NULL, &value);) {
704 struct connman_network *network = value;
706 if (connman_network_get_device(network) == device) {
707 connman_network_set_strength(network, strength);
708 connman_network_update(network);
713 static void modem_roaming_changed(struct modem_data *modem,
716 struct connman_device *device = modem->device;
717 connman_bool_t roaming;
724 connman_device_set_string(device, "RegistrationStatus", status);
726 if (g_str_equal(status, "roaming"))
728 else if (g_str_equal(status, "registered"))
733 for (g_hash_table_iter_init(&i, network_hash);
734 g_hash_table_iter_next(&i, NULL, &value);) {
735 struct connman_network *network = value;
737 if (connman_network_get_device(network) == device) {
738 connman_network_set_roaming(network, roaming);
739 connman_network_update(network);
744 static void modem_registration_changed(struct modem_data *modem,
745 DBusMessageIter *entry)
747 DBusMessageIter iter;
752 connman_uint8_t byte;
755 dbus_message_iter_get_basic(entry, &key);
759 dbus_message_iter_next(entry);
761 dbus_message_iter_recurse(entry, &iter);
763 type = dbus_message_iter_get_arg_type(&iter);
764 if (type != DBUS_TYPE_BYTE && type != DBUS_TYPE_STRING)
767 dbus_message_iter_get_basic(&iter, &value);
769 if (g_str_equal(key, "Name") && type == DBUS_TYPE_STRING)
770 modem_operator_name_changed(modem, value.str);
771 else if (g_str_equal(key, "Strength") && type == DBUS_TYPE_BYTE)
772 modem_strength_changed(modem, value.byte);
773 else if (g_str_equal(key, "Status") && type == DBUS_TYPE_STRING)
774 modem_roaming_changed(modem, value.str);
778 static gboolean reg_changed(DBusConnection *connection,
779 DBusMessage *message, void *user_data)
781 const char *path = dbus_message_get_path(message);
782 struct modem_data *modem;
783 DBusMessageIter iter;
785 DBG("path %s", path);
787 modem = g_hash_table_lookup(modem_hash, path);
791 if (dbus_message_iter_init(message, &iter))
792 modem_registration_changed(modem, &iter);
797 static void check_registration_reply(DBusPendingCall *call, void *user_data)
799 char const *path = user_data;
800 struct modem_data *modem;
802 DBusMessageIter array, dict, entry;
804 DBG("path %s", path);
806 modem = g_hash_table_lookup(modem_hash, path);
810 reply = dbus_pending_call_steal_reply(call);
812 if (dbus_message_iter_init(reply, &array) == FALSE)
815 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
818 dbus_message_iter_recurse(&array, &dict);
819 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
820 dbus_message_iter_recurse(&dict, &entry);
821 modem_registration_changed(modem, &entry);
822 dbus_message_iter_next(&dict);
826 dbus_message_unref(reply);
828 dbus_pending_call_unref(call);
831 static void check_registration(struct modem_data *modem)
833 char const *path = modem->path;
835 DBG("modem %p path %s", modem, path);
837 call_ofono(path, OFONO_REGISTRATION_INTERFACE, GET_PROPERTIES,
838 check_registration_reply, g_strdup(path), g_free,
842 static void add_device(const char *path, const char *imsi)
844 struct modem_data *modem;
845 struct connman_device *device;
847 DBG("path %s imsi %s", path, imsi);
855 modem = g_hash_table_lookup(modem_hash, path);
860 if (!g_strcmp0(imsi, connman_device_get_ident(modem->device)))
863 modem_remove_device(modem);
866 if (strlen(imsi) == 0)
869 device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
873 connman_device_set_ident(device, imsi);
875 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
877 connman_device_set_string(device, "Path", path);
879 connman_device_set_data(device, modem);
881 if (connman_device_register(device) < 0) {
882 connman_device_unref(device);
886 modem->device = device;
889 check_registration(modem);
891 check_networks(modem);
894 static void sim_properties_reply(DBusPendingCall *call, void *user_data)
896 const char *path = user_data;
897 const char *imsi = NULL;
899 DBusMessageIter array, dict;
901 DBG("path %s", path);
903 reply = dbus_pending_call_steal_reply(call);
905 if (dbus_message_iter_init(reply, &array) == FALSE)
908 if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
911 dbus_message_iter_recurse(&array, &dict);
913 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
914 DBusMessageIter entry, value;
917 dbus_message_iter_recurse(&dict, &entry);
918 dbus_message_iter_get_basic(&entry, &key);
920 dbus_message_iter_next(&entry);
921 dbus_message_iter_recurse(&entry, &value);
923 if (g_str_equal(key, "SubscriberIdentity")) {
924 dbus_message_iter_get_basic(&value, &imsi);
925 add_device(path, imsi);
928 dbus_message_iter_next(&dict);
932 dbus_message_unref(reply);
934 dbus_pending_call_unref(call);
937 static void get_imsi(const char *path)
939 DBG("path %s", path);
941 call_ofono(path, OFONO_SIM_INTERFACE, GET_PROPERTIES,
942 sim_properties_reply, g_strdup(path), g_free,
946 static int gprs_change_powered(const char *path, dbus_bool_t powered)
948 DBG("path %s powered %d", path, powered);
950 return set_property(path, OFONO_GPRS_INTERFACE, "Powered",
951 DBUS_TYPE_BOOLEAN, &powered,
955 static int modem_change_powered(const char *path, dbus_bool_t powered)
957 DBG("path %s powered %d", path, powered);
959 return set_property(path, OFONO_MODEM_INTERFACE, "Powered",
960 DBUS_TYPE_BOOLEAN, &powered,
965 static gboolean modem_has_interface(DBusMessageIter *array,
966 char const *interface)
968 DBusMessageIter entry;
970 dbus_message_iter_recurse(array, &entry);
972 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
975 dbus_message_iter_get_basic(&entry, &element);
977 if (g_strcmp0(interface, element) == 0)
980 dbus_message_iter_next(&entry);
986 static gboolean modem_has_sim(DBusMessageIter *array)
988 return modem_has_interface(array, OFONO_SIM_INTERFACE);
991 static gboolean modem_has_reg(DBusMessageIter *array)
993 return modem_has_interface(array, OFONO_REGISTRATION_INTERFACE);
996 static gboolean modem_has_gprs(DBusMessageIter *array)
998 return modem_has_interface(array, OFONO_GPRS_INTERFACE);
1001 static void add_modem(const char *path, DBusMessageIter *properties)
1003 struct modem_data *modem;
1004 DBusMessageIter dict;
1005 dbus_bool_t powered = FALSE;
1006 dbus_bool_t online = FALSE;
1007 dbus_bool_t has_online = FALSE;
1008 gboolean has_sim = FALSE;
1009 gboolean has_reg = FALSE;
1010 gboolean has_gprs = FALSE;
1012 modem = g_hash_table_lookup(modem_hash, path);
1017 modem = g_try_new0(struct modem_data, 1);
1021 modem->path = g_strdup(path);
1022 modem->device = NULL;
1023 modem->available = TRUE;
1025 g_hash_table_insert(modem_hash, g_strdup(path), modem);
1027 dbus_message_iter_recurse(properties, &dict);
1029 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1030 DBusMessageIter entry, value;
1033 dbus_message_iter_recurse(&dict, &entry);
1034 dbus_message_iter_get_basic(&entry, &key);
1036 dbus_message_iter_next(&entry);
1037 dbus_message_iter_recurse(&entry, &value);
1039 if (g_str_equal(key, "Powered") == TRUE)
1040 dbus_message_iter_get_basic(&value, &powered);
1041 else if (g_str_equal(key, "Online") == TRUE) {
1043 dbus_message_iter_get_basic(&value, &online);
1044 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1045 has_sim = modem_has_sim(&value);
1046 has_reg = modem_has_reg(&value);
1047 has_gprs = modem_has_gprs(&value);
1050 dbus_message_iter_next(&dict);
1054 modem_change_powered(path, TRUE);
1056 modem->has_sim = has_sim;
1057 modem->has_reg = has_reg;
1058 modem->has_gprs = has_gprs;
1060 update_modem_online(modem, online);
1066 static void manager_modems_reply(DBusPendingCall *call, void *user_data)
1070 DBusMessageIter array, dict;
1074 reply = dbus_pending_call_steal_reply(call);
1076 if (dbus_message_has_signature(reply, "a(oa{sv})") == FALSE)
1079 dbus_error_init(&error);
1081 if (dbus_set_error_from_message(&error, reply)) {
1082 connman_error("ModemManager.GetModems() %s %s",
1083 error.name, error.message);
1084 dbus_error_free(&error);
1088 if (dbus_message_iter_init(reply, &array) == FALSE)
1091 dbus_message_iter_recurse(&array, &dict);
1093 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
1094 DBusMessageIter value, properties;
1095 const char *modem_path;
1097 dbus_message_iter_recurse(&dict, &value);
1098 dbus_message_iter_get_basic(&value, &modem_path);
1100 dbus_message_iter_next(&value);
1101 dbus_message_iter_recurse(&value, &properties);
1104 add_modem(modem_path, &properties);
1106 dbus_message_iter_next(&dict);
1110 dbus_message_unref(reply);
1112 dbus_pending_call_unref(call);
1115 static void ofono_connect(DBusConnection *connection, void *user_data)
1117 DBG("connection %p", connection);
1119 modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1120 g_free, remove_modem);
1122 network_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1123 NULL, remove_network);
1125 call_ofono("/", OFONO_MANAGER_INTERFACE, GET_MODEMS,
1126 manager_modems_reply, NULL, NULL,
1130 static void ofono_disconnect(DBusConnection *connection, void *user_data)
1132 DBG("connection %p", connection);
1134 if (modem_hash == NULL)
1137 g_hash_table_destroy(modem_hash);
1142 static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
1145 const char *path = dbus_message_get_path(message);
1146 struct modem_data *modem;
1147 DBusMessageIter iter, value;
1150 DBG("path %s", path);
1152 modem = g_hash_table_lookup(modem_hash, path);
1156 if (dbus_message_iter_init(message, &iter) == FALSE)
1159 dbus_message_iter_get_basic(&iter, &key);
1161 dbus_message_iter_next(&iter);
1162 dbus_message_iter_recurse(&iter, &value);
1164 if (g_str_equal(key, "Powered") == TRUE) {
1165 dbus_bool_t powered;
1167 dbus_message_iter_get_basic(&value, &powered);
1168 if (powered == TRUE)
1171 modem->has_sim = FALSE;
1172 modem->has_reg = FALSE;
1173 modem->has_gprs = FALSE;
1175 modem_remove_device(modem);
1176 } else if (g_str_equal(key, "Online") == TRUE) {
1179 dbus_message_iter_get_basic(&value, &online);
1181 update_modem_online(modem, online);
1182 } else if (g_str_equal(key, "Interfaces") == TRUE) {
1183 gboolean has_sim = modem_has_sim(&value);
1184 gboolean has_reg = modem_has_reg(&value);
1185 gboolean added_reg = has_reg && !modem->has_reg;
1186 gboolean has_gprs = modem_has_gprs(&value);
1187 gboolean added_gprs = has_gprs && !modem->has_gprs;
1189 modem->has_sim = has_sim;
1190 modem->has_reg = has_reg;
1191 modem->has_gprs = has_gprs;
1193 if (modem->device == NULL) {
1195 get_imsi(modem->path);
1196 } else if (!has_sim) {
1197 modem_remove_device(modem);
1200 check_registration(modem);
1202 gprs_change_powered(modem->path, TRUE);
1209 static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
1212 const char *path = dbus_message_get_path(message);
1213 struct modem_data *modem;
1214 DBusMessageIter iter, value;
1217 DBG("path %s", path);
1219 modem = g_hash_table_lookup(modem_hash, path);
1223 if (dbus_message_iter_init(message, &iter) == FALSE)
1226 dbus_message_iter_get_basic(&iter, &key);
1228 dbus_message_iter_next(&iter);
1229 dbus_message_iter_recurse(&iter, &value);
1231 if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
1234 dbus_message_iter_get_basic(&value, &imsi);
1236 add_device(path, imsi);
1237 } else if (g_str_equal(key, "Present") == TRUE) {
1238 dbus_bool_t present;
1240 dbus_message_iter_get_basic(&value, &present);
1245 if (modem->device != NULL)
1246 modem_remove_device(modem);
1248 modem->has_gprs = FALSE;
1249 modem->has_reg = FALSE;
1255 static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
1258 const char *path = dbus_message_get_path(message);
1259 struct modem_data *modem;
1260 DBusMessageIter iter, value;
1263 DBG("path %s", path);
1265 modem = g_hash_table_lookup(modem_hash, path);
1269 if (dbus_message_iter_init(message, &iter) == FALSE)
1272 dbus_message_iter_get_basic(&iter, &key);
1274 dbus_message_iter_next(&iter);
1275 dbus_message_iter_recurse(&iter, &value);
1277 if (g_str_equal(key, "Attached") == TRUE) {
1278 dbus_bool_t attached;
1280 dbus_message_iter_get_basic(&value, &attached);
1282 DBG("Attached %d", attached);
1284 if (attached == TRUE)
1285 check_networks(modem);
1286 else if (modem->device != NULL)
1287 connman_device_remove_all_networks(modem->device);
1294 static gboolean modem_added(DBusConnection *connection,
1295 DBusMessage *message, void *user_data)
1297 DBusMessageIter iter, properties;
1298 const char *modem_path;
1302 if (dbus_message_iter_init(message, &iter) == FALSE)
1305 dbus_message_iter_get_basic(&iter, &modem_path);
1307 dbus_message_iter_next(&iter);
1308 dbus_message_iter_recurse(&iter, &properties);
1310 add_modem(modem_path, &properties);
1315 static gboolean modem_removed(DBusConnection *connection,
1316 DBusMessage *message, void *user_data)
1318 DBusMessageIter iter;
1319 const char *modem_path;
1323 if (dbus_message_iter_init(message, &iter) == FALSE)
1326 dbus_message_iter_get_basic(&iter, &modem_path);
1328 g_hash_table_remove(modem_hash, modem_path);
1334 static void get_dns(DBusMessageIter *array, struct connman_element *parent)
1336 DBusMessageIter entry;
1337 gchar *nameserver = NULL, *nameserver_old = NULL;
1341 dbus_message_iter_recurse(array, &entry);
1343 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1346 dbus_message_iter_get_basic(&entry, &dns);
1350 if (nameserver == NULL) {
1352 nameserver = g_strdup(dns);
1355 nameserver_old = nameserver;
1356 nameserver = g_strdup_printf("%s %s",
1357 nameserver_old, dns);
1358 g_free(nameserver_old);
1361 dbus_message_iter_next(&entry);
1364 parent->ipv4.nameserver = nameserver;
1367 static void update_settings(DBusMessageIter *array,
1368 struct connman_network *network)
1370 struct connman_element *parent = connman_network_get_element(network);
1371 DBusMessageIter dict;
1372 const char *interface = NULL;
1374 DBG("network %p", network);
1376 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1379 dbus_message_iter_recurse(array, &dict);
1381 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1382 DBusMessageIter entry, value;
1385 dbus_message_iter_recurse(&dict, &entry);
1386 dbus_message_iter_get_basic(&entry, &key);
1390 dbus_message_iter_next(&entry);
1391 dbus_message_iter_recurse(&entry, &value);
1393 if (g_str_equal(key, "Interface") == TRUE) {
1396 dbus_message_iter_get_basic(&value, &interface);
1398 DBG("interface %s", interface);
1400 index = connman_inet_ifindex(interface);
1402 connman_network_set_index(network, index);
1404 connman_error("Can not find interface %s",
1408 } else if (g_str_equal(key, "Method") == TRUE) {
1411 dbus_message_iter_get_basic(&value, &method);
1412 if (g_strcmp0(method, "static") == 0) {
1414 parent->ipv4.method =
1415 CONNMAN_IPCONFIG_METHOD_FIXED;
1416 } else if (g_strcmp0(method, "dhcp") == 0) {
1418 parent->ipv4.method =
1419 CONNMAN_IPCONFIG_METHOD_DHCP;
1422 } else if (g_str_equal(key, "Address") == TRUE) {
1423 const char *address;
1425 dbus_message_iter_get_basic(&value, &address);
1427 DBG("address %s", address);
1429 parent->ipv4.address = g_strdup(address);
1430 } else if (g_str_equal(key, "Netmask") == TRUE) {
1431 const char *netmask;
1433 dbus_message_iter_get_basic(&value, &netmask);
1435 DBG("netmask %s", netmask);
1437 parent->ipv4.netmask = g_strdup(netmask);
1438 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
1440 get_dns(&value, parent);
1441 } else if (g_str_equal(key, "Gateway") == TRUE) {
1442 const char *gateway;
1444 dbus_message_iter_get_basic(&value, &gateway);
1446 DBG("gateway %s", gateway);
1448 parent->ipv4.gateway = g_strdup(gateway);
1451 dbus_message_iter_next(&dict);
1454 /* deactive, oFono send NULL inteface before deactive signal */
1455 if (interface == NULL)
1456 connman_network_set_index(network, -1);
1459 static void cleanup_ipconfig(struct connman_network *network)
1461 struct connman_element *parent = connman_network_get_element(network);
1463 g_free(parent->ipv4.address);
1464 parent->ipv4.address = NULL;
1466 g_free(parent->ipv4.netmask);
1467 parent->ipv4.netmask = NULL;
1469 g_free(parent->ipv4.nameserver);
1470 parent->ipv4.nameserver = NULL;
1472 g_free(parent->ipv4.gateway);
1473 parent->ipv4.gateway = NULL;
1475 parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1479 static void set_connected(struct connman_network *network,
1480 connman_bool_t connected)
1482 struct connman_element *parent = connman_network_get_element(network);
1483 enum connman_ipconfig_method method = parent->ipv4.method;
1486 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1487 case CONNMAN_IPCONFIG_METHOD_OFF:
1488 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1491 case CONNMAN_IPCONFIG_METHOD_FIXED:
1492 connman_network_set_method(network, method);
1494 if (connected == FALSE)
1495 cleanup_ipconfig(network);
1497 connman_network_set_connected(network, connected);
1500 case CONNMAN_IPCONFIG_METHOD_DHCP:
1501 connman_network_set_method(network, method);
1503 connman_network_set_connected(network, connected);
1508 static gboolean context_changed(DBusConnection *connection,
1509 DBusMessage *message, void *user_data)
1511 const char *path = dbus_message_get_path(message);
1512 struct connman_network *network;
1513 DBusMessageIter iter, value;
1516 DBG("path %s", path);
1518 network = g_hash_table_lookup(network_hash, path);
1519 if (network == NULL)
1522 if (!pending_network_is_available(network)) {
1523 remove_network(network);
1527 if (dbus_message_iter_init(message, &iter) == FALSE)
1530 dbus_message_iter_get_basic(&iter, &key);
1532 dbus_message_iter_next(&iter);
1533 dbus_message_iter_recurse(&iter, &value);
1535 if (g_str_equal(key, "Settings") == TRUE)
1536 update_settings(&value, network);
1537 else if (g_str_equal(key, "Active") == TRUE) {
1540 dbus_message_iter_get_basic(&value, &active);
1542 set_connected(network, active);
1549 static guint reg_watch;
1550 static guint sim_watch;
1551 static guint gprs_watch;
1552 static guint modem_watch;
1553 static guint modem_added_watch;
1554 static guint modem_removed_watch;
1555 static guint context_watch;
1557 static int ofono_init(void)
1561 connection = connman_dbus_get_connection();
1562 if (connection == NULL)
1565 watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
1566 ofono_connect, ofono_disconnect, NULL, NULL);
1568 reg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1569 OFONO_REGISTRATION_INTERFACE,
1574 gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1575 OFONO_GPRS_INTERFACE,
1580 modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1581 OFONO_MODEM_INTERFACE,
1586 sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1587 OFONO_SIM_INTERFACE,
1592 modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1593 OFONO_MANAGER_INTERFACE,
1598 modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1599 OFONO_MANAGER_INTERFACE,
1604 context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
1605 OFONO_CONTEXT_INTERFACE,
1610 if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
1611 reg_watch == 0 || sim_watch == 0 ||
1612 modem_added_watch == 0 || modem_removed_watch == 0 ||
1613 context_watch == 0) {
1618 err = connman_network_driver_register(&network_driver);
1622 err = connman_device_driver_register(&modem_driver);
1624 connman_network_driver_unregister(&network_driver);
1631 g_dbus_remove_watch(connection, watch);
1632 g_dbus_remove_watch(connection, sim_watch);
1633 g_dbus_remove_watch(connection, reg_watch);
1634 g_dbus_remove_watch(connection, gprs_watch);
1635 g_dbus_remove_watch(connection, modem_watch);
1636 g_dbus_remove_watch(connection, modem_added_watch);
1637 g_dbus_remove_watch(connection, modem_removed_watch);
1638 g_dbus_remove_watch(connection, context_watch);
1640 dbus_connection_unref(connection);
1645 static void ofono_exit(void)
1647 g_dbus_remove_watch(connection, watch);
1648 g_dbus_remove_watch(connection, sim_watch);
1649 g_dbus_remove_watch(connection, reg_watch);
1650 g_dbus_remove_watch(connection, gprs_watch);
1651 g_dbus_remove_watch(connection, modem_watch);
1652 g_dbus_remove_watch(connection, modem_added_watch);
1653 g_dbus_remove_watch(connection, modem_removed_watch);
1654 g_dbus_remove_watch(connection, context_watch);
1656 ofono_disconnect(connection, NULL);
1658 connman_device_driver_unregister(&modem_driver);
1659 connman_network_driver_unregister(&network_driver);
1661 dbus_connection_unref(connection);
1664 CONNMAN_PLUGIN_DEFINE(ofono, "oFono telephony plugin", VERSION,
1665 CONNMAN_PLUGIN_PRIORITY_DEFAULT, ofono_init, ofono_exit)