*
* Connection Manager
*
- * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#endif
#include <errno.h>
+#include <stdlib.h>
#include <gdbus.h>
#include <string.h>
+#include <stdint.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
-#include <connman/element.h>
#include <connman/device.h>
#include <connman/network.h>
#include <connman/dbus.h>
-#include <connman/inet.h>
#include <connman/log.h>
+#define uninitialized_var(x) x = x
+
#define OFONO_SERVICE "org.ofono"
#define OFONO_MANAGER_INTERFACE OFONO_SERVICE ".Manager"
#define OFONO_MODEM_INTERFACE OFONO_SERVICE ".Modem"
-#define OFONO_GPRS_INTERFACE OFONO_SERVICE ".DataConnectionManager"
#define OFONO_SIM_INTERFACE OFONO_SERVICE ".SimManager"
-#define OFONO_PRI_CONTEXT_INTERFACE OFONO_SERVICE ".PrimaryDataContext"
+#define OFONO_NETREG_INTERFACE OFONO_SERVICE ".NetworkRegistration"
+#define OFONO_CM_INTERFACE OFONO_SERVICE ".ConnectionManager"
+#define OFONO_CONTEXT_INTERFACE OFONO_SERVICE ".ConnectionContext"
+#define MODEM_ADDED "ModemAdded"
+#define MODEM_REMOVED "ModemRemoved"
#define PROPERTY_CHANGED "PropertyChanged"
+#define CONTEXT_ADDED "ContextAdded"
+#define CONTEXT_REMOVED "ContextRemoved"
+
#define GET_PROPERTIES "GetProperties"
#define SET_PROPERTY "SetProperty"
+#define GET_MODEMS "GetModems"
-#define TIMEOUT 5000
+#define TIMEOUT 40000
+
+enum ofono_api {
+ OFONO_API_SIM = 0x1,
+ OFONO_API_NETREG = 0x2,
+ OFONO_API_CM = 0x4,
+};
static DBusConnection *connection;
-static GHashTable *modem_hash = NULL;
+static GHashTable *modem_hash;
struct modem_data {
char *path;
+
struct connman_device *device;
- gboolean available;
-};
-static int modem_probe(struct connman_device *device)
-{
- DBG("device %p", device);
+ /* Modem Interface */
+ char *serial;
+ connman_bool_t powered;
+ connman_bool_t online;
+ uint8_t interfaces;
- return 0;
-}
+ connman_bool_t set_powered;
+ connman_bool_t set_online;
-static void modem_remove(struct connman_device *device)
-{
- DBG("device %p", device);
-}
+ /* ConnectionManager Interface */
+ connman_bool_t attached;
+
+ /* SimManager Interface */
+ char *imsi;
-static void powered_reply(DBusPendingCall *call, void *user_data)
+ /* pending calls */
+ DBusPendingCall *call_set_property;
+ DBusPendingCall *call_get_properties;
+};
+
+typedef void (*set_property_cb)(struct modem_data *data,
+ connman_bool_t success);
+typedef void (*get_properties_cb)(struct modem_data *data,
+ DBusMessageIter *dict);
+
+struct property_info {
+ struct modem_data *modem;
+ const char *path;
+ const char *interface;
+ const char *property;
+ set_property_cb set_property_cb;
+ get_properties_cb get_properties_cb;
+};
+
+static void set_property_reply(DBusPendingCall *call, void *user_data)
{
+ struct property_info *info = user_data;
DBusMessage *reply;
DBusError error;
+ connman_bool_t success = TRUE;
- DBG("");
+ DBG("%s path %s %s.%s", info->modem->path,
+ info->path, info->interface, info->property);
+
+ info->modem->call_set_property = NULL;
dbus_error_init(&error);
reply = dbus_pending_call_steal_reply(call);
if (dbus_set_error_from_message(&error, reply)) {
- connman_error("%s", error.message);
+ connman_error("Failed to change property: %s %s.%s: %s %s",
+ info->path, info->interface, info->property,
+ error.name, error.message);
dbus_error_free(&error);
+ success = FALSE;
}
+ if (info->set_property_cb != NULL)
+ (*info->set_property_cb)(info->modem, success);
+
dbus_message_unref(reply);
dbus_pending_call_unref(call);
}
-static int gprs_change_powered(const char *path, dbus_bool_t powered)
+static int set_property(struct modem_data *modem,
+ const char *path, const char *interface,
+ const char *property, int type, void *value,
+ set_property_cb notify)
{
DBusMessage *message;
DBusMessageIter iter;
- DBusPendingCall *call;
+ struct property_info *info;
- DBG("path %s powered %d", path, powered);
+ DBG("%s path %s %s.%s", modem->path, path, interface, property);
- if (path == NULL)
- return -EINVAL;
+ if (modem->call_set_property != NULL) {
+ connman_error("Pending SetProperty");
+ return -EBUSY;
+ }
message = dbus_message_new_method_call(OFONO_SERVICE, path,
- OFONO_GPRS_INTERFACE, SET_PROPERTY);
+ interface, SET_PROPERTY);
if (message == NULL)
return -ENOMEM;
- dbus_message_set_auto_start(message, FALSE);
-
dbus_message_iter_init_append(message, &iter);
- connman_dbus_property_append_basic(&iter, "Powered",
- DBUS_TYPE_BOOLEAN, &powered);
+ connman_dbus_property_append_basic(&iter, property, type, value);
if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to change powered property");
+ &modem->call_set_property, TIMEOUT) == FALSE) {
+ connman_error("Failed to change property: %s %s.%s",
+ path, interface, property);
dbus_message_unref(message);
return -EINVAL;
}
- if (call == NULL) {
+ if (modem->call_set_property == NULL) {
connman_error("D-Bus connection not available");
dbus_message_unref(message);
return -EINVAL;
}
- dbus_pending_call_set_notify(call, powered_reply, (void *)path, NULL);
-
- dbus_message_unref(message);
-
- return -EINPROGRESS;
-}
-
-static int modem_enable(struct connman_device *device)
-{
- const char *path = connman_device_get_string(device, "Path");
-
- DBG("device %p, path, %s", device, path);
+ info = g_try_new0(struct property_info, 1);
+ if (info == NULL) {
+ dbus_message_unref(message);
+ return -ENOMEM;
+ }
- return gprs_change_powered(path, TRUE);
-}
+ info->modem = modem;
+ info->path = path;
+ info->interface = interface;
+ info->property = property;
+ info->set_property_cb = notify;
-static int modem_disable(struct connman_device *device)
-{
- const char *path = connman_device_get_string(device, "Path");
+ dbus_pending_call_set_notify(modem->call_set_property,
+ set_property_reply, info, g_free);
- DBG("device %p, path %s", device, path);
+ dbus_message_unref(message);
- return gprs_change_powered(path, FALSE);
+ return -EINPROGRESS;
}
-static struct connman_device_driver modem_driver = {
- .name = "modem",
- .type = CONNMAN_DEVICE_TYPE_CELLULAR,
- .probe = modem_probe,
- .remove = modem_remove,
- .enable = modem_enable,
- .disable = modem_disable,
-};
-
-static char *get_ident(const char *path)
+static void get_properties_reply(DBusPendingCall *call, void *user_data)
{
- char *ident, *pos;
-
- if (*path != '/')
- return NULL;
-
- ident = g_strdup(path + 1);
+ struct property_info *info = user_data;
+ DBusMessageIter array, dict;
+ DBusMessage *reply;
+ DBusError error;
- pos = ident;
+ DBG("%s path %s %s", info->modem->path, info->path, info->interface);
- while ((pos = strchr(pos, '/')) != NULL)
- *pos = '_';
+ info->modem->call_get_properties = NULL;
- return ident;
-}
+ dbus_error_init(&error);
-static void config_network_reply(DBusPendingCall *call, void *user_data)
-{
- struct connman_network *network = user_data;
- DBusMessage *reply;
- DBusMessageIter array, dict;
- gboolean internet_type = FALSE;
+ reply = dbus_pending_call_steal_reply(call);
- DBG("network %p", network);
+ if (dbus_set_error_from_message(&error, reply)) {
+ connman_error("Failed to get properties: %s %s: %s %s",
+ info->path, info->interface,
+ error.name, error.message);
+ dbus_error_free(&error);
- reply = dbus_pending_call_steal_reply(call);
+ goto done;
+ }
if (dbus_message_iter_init(reply, &array) == FALSE)
goto done;
dbus_message_iter_recurse(&array, &dict);
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
-
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
-
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
-
- if (g_str_equal(key, "Name") == TRUE) {
- const char *name;
-
- dbus_message_iter_get_basic(&value, &name);
- connman_network_set_name(network, name);
- } else if (g_str_equal(key, "Type") == TRUE) {
- const char *type;
-
- dbus_message_iter_get_basic(&value, &type);
- if (g_strcmp0(type, "internet") == 0) {
- internet_type = TRUE;
-
- connman_network_set_protocol(network,
- CONNMAN_NETWORK_PROTOCOL_IP);
- } else {
- internet_type = FALSE;
-
- connman_network_set_protocol(network,
- CONNMAN_NETWORK_PROTOCOL_UNKNOWN);
- }
- }
-
- dbus_message_iter_next(&dict);
- }
-
- if (internet_type == TRUE) {
- const char *path;
- char *group;
-
- path = connman_network_get_string(network, "Path");
-
- group = get_ident(path);
-
- connman_network_set_group(network, group);
-
- g_free(group);
- }
+ if (info->get_properties_cb != NULL)
+ (*info->get_properties_cb)(info->modem, &dict);
done:
+
dbus_message_unref(reply);
dbus_pending_call_unref(call);
}
-static void config_network(struct connman_network *network, const char *path)
+static int get_properties(const char *path, const char *interface,
+ get_properties_cb notify,
+ struct modem_data *modem)
{
DBusMessage *message;
- DBusPendingCall *call;
+ struct property_info *info;
+
+ DBG("%s path %s %s", modem->path, path, interface);
- DBG("path %s", path);
+ if (modem->call_get_properties != NULL) {
+ connman_error("Pending GetProperties");
+ return -EBUSY;
+ }
message = dbus_message_new_method_call(OFONO_SERVICE, path,
- OFONO_PRI_CONTEXT_INTERFACE, GET_PROPERTIES);
+ interface, GET_PROPERTIES);
if (message == NULL)
- return;
-
- dbus_message_set_auto_start(message, FALSE);
+ return -ENOMEM;
if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get Primary Context");
- goto done;
+ &modem->call_get_properties, TIMEOUT) == FALSE) {
+ connman_error("Failed to call %s.GetProperties()", interface);
+ dbus_message_unref(message);
+ return -EINVAL;
}
- if (call == NULL) {
+ if (modem->call_get_properties == NULL) {
connman_error("D-Bus connection not available");
- goto done;
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ info = g_try_new0(struct property_info, 1);
+ if (info == NULL) {
+ dbus_message_unref(message);
+ return -ENOMEM;
}
- dbus_pending_call_set_notify(call, config_network_reply,
- (void *)network, NULL);
+ info->modem = modem;
+ info->path = path;
+ info->interface = interface;
+ info->get_properties_cb = notify;
+
+ dbus_pending_call_set_notify(modem->call_get_properties,
+ get_properties_reply, info, g_free);
-done:
dbus_message_unref(message);
+
+ return -EINPROGRESS;
}
-static int network_probe(struct connman_network *network)
+static void modem_set_online_reply(struct modem_data *modem,
+ connman_bool_t success)
{
- const char *path;
-
- path = connman_network_get_string(network, "Path");
+ DBG("%s", modem->path);
- DBG("network %p path %s", network, path);
-
- config_network(network, path);
+ if (success == TRUE) {
+ /*
+ * Don't handle do anything on success here. oFono will send
+ * the change via PropertyChanged singal.
+ */
+ return;
+ }
- return 0;
+ modem->set_online = FALSE;
}
-static struct connman_network *pending_network;
-
-static gboolean pending_network_is_available(
- struct connman_network *pending_network)
+static int modem_set_online(struct modem_data *modem)
{
- struct connman_device *device;
- struct connman_network *network;
- const char *identifier;
- char *ident;
-
- /* Modem may be removed during waiting for active reply */
- device = connman_network_get_device(pending_network);
- if (device == NULL)
- return FALSE;
+ DBG("%s", modem->path);
- identifier = connman_network_get_identifier(pending_network);
+ modem->set_online = TRUE;
- ident = g_strdup(identifier);
+ return set_property(modem, modem->path,
+ OFONO_MODEM_INTERFACE,
+ "Online", DBUS_TYPE_BOOLEAN,
+ &modem->set_online,
+ modem_set_online_reply);
+}
- connman_network_unref(pending_network);
+static int modem_set_powered(struct modem_data *modem)
+{
+ DBG("%s", modem->path);
- /* network may be removed during waiting for active reply */
- network = connman_device_get_network(device, ident);
+ modem->set_powered = TRUE;
- g_free(ident);
+ return set_property(modem, modem->path,
+ OFONO_MODEM_INTERFACE,
+ "Powered", DBUS_TYPE_BOOLEAN,
+ &modem->set_powered,
+ NULL);
+}
- if (network == NULL)
- return FALSE;
+static connman_bool_t has_interface(uint8_t interfaces,
+ enum ofono_api api)
+{
+ if ((interfaces & api) == api)
+ return TRUE;
- return TRUE;
+ return FALSE;
}
-static void set_active_reply(DBusPendingCall *call, void *user_data)
+static uint8_t extract_interfaces(DBusMessageIter *array)
{
- DBusMessage *reply;
- DBusError error;
- struct connman_network *network = user_data;
+ DBusMessageIter entry;
+ uint8_t interfaces = 0;
- DBG("network %p", network);
+ dbus_message_iter_recurse(array, &entry);
- reply = dbus_pending_call_steal_reply(call);
+ while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
+ const char *name;
- if (pending_network_is_available(network) == FALSE)
- goto done;
+ dbus_message_iter_get_basic(&entry, &name);
- dbus_error_init(&error);
- if (dbus_set_error_from_message(&error, reply)) {
- if (connman_network_get_index(network) < 0)
- connman_network_set_error(network,
- CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
+ if (g_str_equal(name, OFONO_SIM_INTERFACE) == TRUE)
+ interfaces |= OFONO_API_SIM;
+ else if (g_str_equal(name, OFONO_NETREG_INTERFACE) == TRUE)
+ interfaces |= OFONO_API_NETREG;
+ else if (g_str_equal(name, OFONO_CM_INTERFACE) == TRUE)
+ interfaces |= OFONO_API_CM;
- pending_network = NULL;
+ dbus_message_iter_next(&entry);
+ }
- connman_error("%s", error.message);
+ return interfaces;
+}
- dbus_error_free(&error);
- } else
- pending_network = network;
+static connman_bool_t ready_to_create_device(struct modem_data *modem)
+{
+ /*
+ * There are three different modem types which behave slightly
+ * different:
+ * - GSM modems will expose the SIM interface then the
+ * CM interface.
+ * - DUN modems will expose first a unique serial number (BDADDR)
+ * and then the CM interface.
+ * - CDMA modems will expose CM first and sometime later
+ * a unique serial number.
+ *
+ * This functions tests if we have the necessary information gathered
+ * before we are able to create a device.
+ */
+
+ if (modem->device != NULL)
+ return FALSE;
-done:
- dbus_message_unref(reply);
+ if (modem->imsi != NULL || modem->serial != NULL)
+ return TRUE;
- dbus_pending_call_unref(call);
+ return FALSE;
}
-static int set_network_active(struct connman_network *network,
- dbus_bool_t active)
+static void create_device(struct modem_data *modem)
{
- DBusMessage *message;
- DBusPendingCall *call;
- DBusMessageIter iter;
+ struct connman_device *device;
+ char *uninitialized_var(ident);
- const char *path = connman_network_get_string(network, "Path");
+ DBG("%s", modem->path);
- DBG("network %p, path %s, active %d", network, path, active);
+ if (modem->imsi != NULL)
+ ident = modem->imsi;
+ else if (modem->serial != NULL)
+ ident = modem->serial;
- if (path == NULL)
- return -EINVAL;
+ if (connman_dbus_validate_ident(ident) == FALSE)
+ ident = connman_dbus_encode_string(ident);
+ else
+ ident = g_strdup(ident);
- message = dbus_message_new_method_call(OFONO_SERVICE, path,
- OFONO_PRI_CONTEXT_INTERFACE, SET_PROPERTY);
- if (message == NULL)
- return -ENOMEM;
+ device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
+ if (device == NULL)
+ goto out;
- dbus_message_set_auto_start(message, FALSE);
+ DBG("device %p", device);
- dbus_message_iter_init_append(message, &iter);
- connman_dbus_property_append_basic(&iter, "Active",
- DBUS_TYPE_BOOLEAN, &active);
+ connman_device_set_ident(device, ident);
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT * 10) == FALSE) {
- connman_error("Failed to connect service");
- dbus_message_unref(message);
- return -EINVAL;
- }
+ connman_device_set_string(device, "Path", modem->path);
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- dbus_message_unref(message);
- return -EINVAL;
+ connman_device_set_data(device, modem);
+
+ if (connman_device_register(device) < 0) {
+ connman_error("Failed to register cellular device");
+ connman_device_unref(device);
+ goto out;
}
- connman_network_ref(network);
+ modem->device = device;
- dbus_pending_call_set_notify(call, set_active_reply, network, NULL);
+out:
+ g_free(ident);
+}
- dbus_message_unref(message);
+static void destroy_device(struct modem_data *modem)
+{
+ DBG("%s", modem->path);
- if (active == TRUE)
- return -EINPROGRESS;
+ connman_device_set_powered(modem->device, FALSE);
- return 0;
+ connman_device_unregister(modem->device);
+ connman_device_unref(modem->device);
+
+ modem->device = NULL;
}
-static int network_connect(struct connman_network *network)
+static gboolean context_changed(DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
{
- if (connman_network_get_index(network) >= 0)
- return -EISCONN;
-
- return set_network_active(network, TRUE);
+ return TRUE;
}
-static int network_disconnect(struct connman_network *network)
+static gboolean cm_context_added(DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
{
- if (connman_network_get_index(network) < 0)
- return -ENOTCONN;
-
- return set_network_active(network, FALSE);
+ return TRUE;
}
-static void network_remove(struct connman_network *network)
+static gboolean cm_context_removed(DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
{
- DBG("network %p", network);
+ return TRUE;
}
-static struct connman_network_driver network_driver = {
- .name = "network",
- .type = CONNMAN_NETWORK_TYPE_CELLULAR,
- .probe = network_probe,
- .remove = network_remove,
- .connect = network_connect,
- .disconnect = network_disconnect,
-};
+static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
+{
+ return TRUE;
+}
-static void add_network(struct connman_device *device, const char *path)
+static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
{
- struct connman_network *network;
- char *ident;
+ const char *path = dbus_message_get_path(message);
+ struct modem_data *modem;
+ DBusMessageIter iter, value;
+ const char *key;
+
+ modem = g_hash_table_lookup(modem_hash, path);
+ if (modem == NULL)
+ return TRUE;
- DBG("device %p path %s", device, path);
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- network = connman_device_get_network(device, path);
- if (network != NULL)
- return;
+ dbus_message_iter_get_basic(&iter, &key);
- ident = get_ident(path);
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
- network = connman_network_create(ident,
- CONNMAN_NETWORK_TYPE_CELLULAR);
- if (network == NULL)
- return;
+ if (g_str_equal(key, "Attached") == TRUE) {
+ dbus_message_iter_get_basic(&value, &modem->attached);
- g_free(ident);
+ DBG("%s Attached %d", modem->path, modem->attached);
+ }
- connman_network_set_string(network, "Path", path);
- connman_network_set_available(network, TRUE);
- connman_network_set_index(network, -1);
- connman_device_add_network(device, network);
+ return TRUE;
}
-static void add_networks(struct connman_device *device, DBusMessageIter *array)
+static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
{
- DBusMessageIter entry;
+ DBG("%s", modem->path);
- DBG("");
+ while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter entry, value;
+ const char *key;
- dbus_message_iter_recurse(array, &entry);
+ dbus_message_iter_recurse(dict, &entry);
+ dbus_message_iter_get_basic(&entry, &key);
- while (dbus_message_iter_get_arg_type(&entry) ==
- DBUS_TYPE_OBJECT_PATH) {
- const char *path;
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
+
+ if (g_str_equal(key, "Attached") == TRUE) {
+ dbus_message_iter_get_basic(&value, &modem->attached);
- dbus_message_iter_get_basic(&entry, &path);
+ DBG("%s Attached %d", modem->path,
+ modem->attached);
- add_network(device, path);
+ return;
+ }
- dbus_message_iter_next(&entry);
+ dbus_message_iter_next(dict);
}
}
-static void check_networks_reply(DBusPendingCall *call, void *user_data)
+static int cm_get_properties(struct modem_data *modem)
{
- struct connman_device *device = user_data;
- DBusMessage *reply;
- DBusMessageIter array, dict, contexts;
- dbus_bool_t attached;
+ return get_properties(modem->path, OFONO_CM_INTERFACE,
+ cm_properties_reply, modem);
+}
- DBG("device %p", device);
+static void update_sim_imsi(struct modem_data *modem,
+ const char *imsi)
+{
+ DBG("%s imsi %s", modem->path, imsi);
- reply = dbus_pending_call_steal_reply(call);
-
- if (dbus_message_iter_init(reply, &array) == FALSE)
- goto done;
-
- if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
- goto done;
-
- dbus_message_iter_recurse(&array, &dict);
-
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
-
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
-
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
-
- DBG("key %s", key);
-
- if (g_str_equal(key, "Attached") == TRUE) {
- dbus_message_iter_get_basic(&value, &attached);
- DBG("Attached %d", attached);
- } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
- contexts = value;
- } else if (g_str_equal(key, "Status") == TRUE) {
- const char *status;
-
- dbus_message_iter_get_basic(&value, &status);
- /* FIXME: add roaming support */
- } else if (g_str_equal(key, "Powered") == TRUE) {
- dbus_bool_t powered;
-
- dbus_message_iter_get_basic(&value, &powered);
-
- connman_device_set_powered(device, powered);
- }
-
- dbus_message_iter_next(&dict);
- }
-
- if (attached == TRUE)
- add_networks(device, &contexts);
-
-done:
- dbus_message_unref(reply);
-
- dbus_pending_call_unref(call);
-}
-
-static void check_networks(struct modem_data *modem)
-{
- DBusMessage *message;
- DBusPendingCall *call;
- struct connman_device *device;
-
- DBG("modem %p", modem);
-
- if (modem == NULL)
- return;
-
- device = modem->device;
- if (device == NULL)
- return;
-
- message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
- OFONO_GPRS_INTERFACE, GET_PROPERTIES);
- if (message == NULL)
- return;
-
- dbus_message_set_auto_start(message, FALSE);
-
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get ofono GPRS");
- goto done;
- }
-
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- goto done;
- }
-
- dbus_pending_call_set_notify(call, check_networks_reply,
- (void *)device, NULL);
-
-done:
- dbus_message_unref(message);
-}
-
-static void add_device(const char *path, const char *imsi)
-{
- struct modem_data *modem;
- struct connman_device *device;
-
- DBG("path %s imsi %s", path, imsi);
-
- if (path == NULL)
- return;
-
- if (imsi == NULL)
- return;
-
- modem = g_hash_table_lookup(modem_hash, path);
- if (modem == NULL)
- return;
-
- device = connman_device_create(imsi, CONNMAN_DEVICE_TYPE_CELLULAR);
- if (device == NULL)
- return;
-
- connman_device_set_ident(device, imsi);
-
- connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
-
- connman_device_set_string(device, "Path", path);
-
- if (connman_device_register(device) < 0) {
- connman_device_unref(device);
- return;
- }
-
- modem->device = device;
-
- check_networks(modem);
-}
-
-static void sim_properties_reply(DBusPendingCall *call, void *user_data)
-{
- const char *path = user_data;
- DBusMessage *reply;
- DBusMessageIter array, dict;
-
- DBG("path %s", path);
-
- reply = dbus_pending_call_steal_reply(call);
-
- if (dbus_message_iter_init(reply, &array) == FALSE)
- goto done;
-
- if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
- goto done;
-
- dbus_message_iter_recurse(&array, &dict);
-
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key, *imsi;
-
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
-
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
-
- if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
- dbus_message_iter_get_basic(&value, &imsi);
-
- add_device(path, imsi);
- }
-
- dbus_message_iter_next(&dict);
- }
-
-done:
- dbus_message_unref(reply);
-
- dbus_pending_call_unref(call);
-}
-
-static void get_imsi(const char *path)
-{
- DBusMessage *message;
- DBusPendingCall *call;
-
- DBG("path %s", path);
-
- message = dbus_message_new_method_call(OFONO_SERVICE, path,
- OFONO_SIM_INTERFACE, GET_PROPERTIES);
- if (message == NULL)
+ if (g_strcmp0(modem->imsi, imsi) == 0)
return;
- dbus_message_set_auto_start(message, FALSE);
-
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get ofono modem sim");
- goto done;
- }
-
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- goto done;
- }
-
- dbus_pending_call_set_notify(call, sim_properties_reply,
- (void *)path, NULL);
-
-done:
- dbus_message_unref(message);
-}
-
-static int modem_change_powered(const char *path, dbus_bool_t powered)
-{
- DBusMessage *message;
- DBusMessageIter iter;
- DBusPendingCall *call;
-
- DBG("path %s powered %d", path, powered);
-
- if (path == NULL)
- return -EINVAL;
-
- message = dbus_message_new_method_call(OFONO_SERVICE, path,
- OFONO_MODEM_INTERFACE, SET_PROPERTY);
- if (message == NULL)
- return -ENOMEM;
-
- dbus_message_set_auto_start(message, FALSE);
-
- dbus_message_iter_init_append(message, &iter);
- connman_dbus_property_append_basic(&iter, "Powered",
- DBUS_TYPE_BOOLEAN, &powered);
-
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to change powered property");
- dbus_message_unref(message);
- return -EINVAL;
- }
-
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- dbus_message_unref(message);
- return -EINVAL;
- }
-
- dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
-
- dbus_message_unref(message);
-
- return -EINPROGRESS;
+ g_free(modem->imsi);
+ modem->imsi = g_strdup(imsi);
}
-static struct modem_data *add_modem(const char *path)
+static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
{
+ const char *path = dbus_message_get_path(message);
struct modem_data *modem;
-
- if (path == NULL)
- return NULL;
+ DBusMessageIter iter, value;
+ const char *key;
modem = g_hash_table_lookup(modem_hash, path);
- if (modem != NULL) {
- modem->available = TRUE;
-
- return modem;
- }
-
- modem = g_try_new0(struct modem_data, 1);
if (modem == NULL)
- return NULL;
-
- modem->path = g_strdup(path);
- modem->device = NULL;
- modem->available = TRUE;
-
- g_hash_table_insert(modem_hash, g_strdup(path), modem);
-
- return modem;
-}
-
-static gboolean modem_has_gprs(DBusMessageIter *array)
-{
- DBusMessageIter entry;
-
- dbus_message_iter_recurse(array, &entry);
-
- while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
- const char *interface;
-
- dbus_message_iter_get_basic(&entry, &interface);
-
- if (g_strcmp0(OFONO_GPRS_INTERFACE, interface) == 0)
- return TRUE;
-
- dbus_message_iter_next(&entry);
- }
-
- return FALSE;
-}
-
-static void modem_properties_reply(DBusPendingCall *call, void *user_data)
-{
- DBusMessage *reply;
- DBusMessageIter array, dict;
- const char *path = user_data;
-
- DBG("path %s", path);
-
- reply = dbus_pending_call_steal_reply(call);
-
- if (dbus_message_iter_init(reply, &array) == FALSE)
- goto done;
+ return TRUE;
- if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
- goto done;
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- dbus_message_iter_recurse(&array, &dict);
+ dbus_message_iter_get_basic(&iter, &key);
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
- dbus_bool_t powered;
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
+ if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
+ char *imsi;
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
+ dbus_message_iter_get_basic(&value, &imsi);
- if (g_str_equal(key, "Powered") == TRUE) {
- dbus_message_iter_get_basic(&value, &powered);
+ update_sim_imsi(modem, imsi);
- if (powered == FALSE) {
- modem_change_powered(path, TRUE);
- break;
- }
- } else if (g_str_equal(key, "Interface") == TRUE) {
- if (modem_has_gprs(&value) == TRUE)
- get_imsi(path);
+ if (modem->online == FALSE) {
+ modem_set_online(modem);
+ } else if (has_interface(modem->interfaces,
+ OFONO_API_CM) == TRUE) {
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
}
-
- dbus_message_iter_next(&dict);
- }
-
-done:
- dbus_message_unref(reply);
-
- dbus_pending_call_unref(call);
-}
-
-static void get_modem_properties(struct modem_data *modem)
-{
- DBusMessage *message;
- DBusPendingCall *call;
-
- DBG("path %s", modem->path);
-
- if (modem->path == NULL)
- return;
-
- message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
- OFONO_MODEM_INTERFACE, GET_PROPERTIES);
- if (message == NULL)
- return;
-
- dbus_message_set_auto_start(message, FALSE);
-
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get ofono modem");
- goto done;
- }
-
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- goto done;
- }
-
- dbus_pending_call_set_notify(call, modem_properties_reply,
- (void *)modem->path, NULL);
-
-done:
- dbus_message_unref(message);
-}
-
-static void mask_unavailable(gpointer key, gpointer value, gpointer user_data)
-{
- struct modem_data *modem = value;
-
- modem->available = FALSE;
-}
-
-static void modems_set_unavailable()
-{
- g_hash_table_foreach(modem_hash, mask_unavailable, NULL);
-}
-
-static void cleanup_modem(gpointer key, gpointer value, gpointer user_data)
-{
- struct modem_data *modem = value;
-
- if (modem->available == FALSE)
- g_hash_table_remove(modem_hash, key);
-}
-
-static void cleanup_modems()
-{
- g_hash_table_foreach(modem_hash, cleanup_modem, NULL);
-}
-
-static void update_modems(DBusMessageIter *array)
-{
- DBusMessageIter entry;
-
- dbus_message_iter_recurse(array, &entry);
-
- modems_set_unavailable();
-
- while (dbus_message_iter_get_arg_type(&entry) ==
- DBUS_TYPE_OBJECT_PATH) {
- const char *path;
- struct modem_data *modem;
-
- dbus_message_iter_get_basic(&entry, &path);
-
- modem = add_modem(path);
- if (modem != NULL)
- get_modem_properties(modem);
-
- dbus_message_iter_next(&entry);
}
- cleanup_modems();
+ return TRUE;
}
-static void manager_properties_reply(DBusPendingCall *call, void *user_data)
+static void sim_properties_reply(struct modem_data *modem,
+ DBusMessageIter *dict)
{
- DBusMessage *reply;
- DBusMessageIter array, dict;
-
- DBG("");
+ DBG("%s", modem->path);
- reply = dbus_pending_call_steal_reply(call);
-
- if (dbus_message_iter_init(reply, &array) == FALSE)
- goto done;
-
- if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
- goto done;
-
- dbus_message_iter_recurse(&array, &dict);
-
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+ while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry, value;
const char *key;
- dbus_message_iter_recurse(&dict, &entry);
+ dbus_message_iter_recurse(dict, &entry);
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
dbus_message_iter_recurse(&entry, &value);
- if (g_str_equal(key, "Modems") == TRUE) {
- update_modems(&value);
- break;
- }
-
- dbus_message_iter_next(&dict);
- }
-
-done:
- dbus_message_unref(reply);
-
- dbus_pending_call_unref(call);
-}
-
-static void modem_remove_device(struct modem_data *modem)
-{
- if (modem->device == NULL)
- return;
-
- connman_device_unregister(modem->device);
- connman_device_unref(modem->device);
-
- modem->device = NULL;
-}
-
-static void remove_modem(gpointer data)
-{
- struct modem_data *modem = data;
-
- g_free(modem->path);
-
- modem_remove_device(modem);
-
- g_free(modem);
-}
-
-static void ofono_connect(DBusConnection *connection, void *user_data)
-{
- DBusMessage *message;
- DBusPendingCall *call;
-
- DBG("connection %p", connection);
-
- modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
- g_free, remove_modem);
-
- message = dbus_message_new_method_call(OFONO_SERVICE, "/",
- OFONO_MANAGER_INTERFACE, GET_PROPERTIES);
- if (message == NULL)
- return;
-
- dbus_message_set_auto_start(message, FALSE);
+ if (g_str_equal(key, "SubscriberIdentity") == TRUE) {
+ char *imsi;
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get ofono modems");
- goto done;
- }
+ dbus_message_iter_get_basic(&value, &imsi);
- if (call == NULL) {
- connman_error("D-Bus connection not available");
- goto done;
- }
+ update_sim_imsi(modem, imsi);
- dbus_pending_call_set_notify(call, manager_properties_reply,
- NULL, NULL);
+ if (modem->online == FALSE) {
+ modem_set_online(modem);
+ break;
+ }
-done:
- dbus_message_unref(message);
+ if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
+ if (modem->device != NULL)
+ cm_get_properties(modem);
+ }
+ return;
+ }
+ dbus_message_iter_next(dict);
+ }
}
-static void ofono_disconnect(DBusConnection *connection, void *user_data)
+static int sim_get_properties(struct modem_data *modem)
{
- DBG("connection %p", connection);
-
- if (modem_hash == NULL)
- return;
-
- g_hash_table_destroy(modem_hash);
-
- modem_hash = NULL;
+ return get_properties(modem->path, OFONO_SIM_INTERFACE,
+ sim_properties_reply, modem);
}
static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
DBusMessageIter iter, value;
const char *key;
- DBG("path %s", path);
-
modem = g_hash_table_lookup(modem_hash, path);
if (modem == NULL)
return TRUE;
dbus_message_iter_recurse(&iter, &value);
if (g_str_equal(key, "Powered") == TRUE) {
- dbus_bool_t powered;
+ dbus_message_iter_get_basic(&value, &modem->powered);
+
+ DBG("%s Powered %d", modem->path, modem->powered);
+
+ if (modem->powered == FALSE)
+ modem_set_powered(modem);
+ } else if (g_str_equal(key, "Online") == TRUE) {
+ dbus_message_iter_get_basic(&value, &modem->online);
+
+ DBG("%s Online %d", modem->path, modem->online);
- dbus_message_iter_get_basic(&value, &powered);
- if (powered == TRUE)
+ if (modem->online == FALSE)
return TRUE;
- modem_remove_device(modem);
+ if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
+ return TRUE;
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
+ if (modem->device != NULL)
+ cm_get_properties(modem);
} else if (g_str_equal(key, "Interfaces") == TRUE) {
- if (modem_has_gprs(&value) == TRUE) {
- if (modem->device == NULL)
- get_imsi(modem->path);
- } else if (modem->device != NULL)
- modem_remove_device(modem);
+ modem->interfaces = extract_interfaces(&value);
+
+ DBG("%s Interfaces 0x%02x", modem->path,
+ modem->interfaces);
+
+ if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
+ if (modem->imsi == NULL &&
+ modem->set_powered == FALSE) {
+ /*
+ * Only use do GetProperties() when
+ * device has not been powered up.
+ */
+ sim_get_properties(modem);
+ return TRUE;
+ }
+ }
+
+ if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
+ if (modem->device != NULL)
+ cm_get_properties(modem);
+ } else {
+ if (modem->device != NULL)
+ destroy_device(modem);
+ }
+ } else if (g_str_equal(key, "Serial") == TRUE) {
+ char *serial;
+
+ dbus_message_iter_get_basic(&value, &serial);
+
+ g_free(modem->serial);
+ modem->serial = g_strdup(serial);
+
+ DBG("%s Serial %s", modem->path, modem->serial);
+
+ if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
+ if (modem->device != NULL)
+ cm_get_properties(modem);
+ }
}
return TRUE;
}
-static gboolean gprs_changed(DBusConnection *connection, DBusMessage *message,
- void *user_data)
+static void add_modem(const char *path, DBusMessageIter *prop)
{
- const char *path = dbus_message_get_path(message);
struct modem_data *modem;
- DBusMessageIter iter, value;
- const char *key;
- DBG("path %s", path);
+ DBG("%s", path);
modem = g_hash_table_lookup(modem_hash, path);
+ if (modem != NULL) {
+ /*
+ * When oFono powers up we ask for the modems and oFono is
+ * reporting with modem_added signal the modems. Only
+ * handle them once.
+ */
+ return;
+ }
+
+ modem = g_try_new0(struct modem_data, 1);
if (modem == NULL)
- return TRUE;
+ return;
- if (dbus_message_iter_init(message, &iter) == FALSE)
- return TRUE;
+ modem->path = g_strdup(path);
- dbus_message_iter_get_basic(&iter, &key);
+ g_hash_table_insert(modem_hash, g_strdup(path), modem);
- dbus_message_iter_next(&iter);
- dbus_message_iter_recurse(&iter, &value);
+ while (dbus_message_iter_get_arg_type(prop) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter entry, value;
+ const char *key;
- if (g_str_equal(key, "Attached") == TRUE) {
- dbus_bool_t attached;
+ dbus_message_iter_recurse(prop, &entry);
+ dbus_message_iter_get_basic(&entry, &key);
- dbus_message_iter_get_basic(&value, &attached);
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
- DBG("Attached %d", attached);
+ if (g_str_equal(key, "Powered") == TRUE) {
+ dbus_message_iter_get_basic(&value, &modem->powered);
- if (attached == TRUE)
- check_networks(modem);
- else if (modem->device != NULL)
- connman_device_remove_all_networks(modem->device);
+ DBG("%s Powered %d", modem->path, modem->powered);
+ } else if (g_str_equal(key, "Online") == TRUE) {
+ dbus_message_iter_get_basic(&value, &modem->online);
- } else if (g_str_equal(key, "Status") == TRUE) {
- const char *status;
- dbus_message_iter_get_basic(&value, &status);
+ DBG("%s Online %d", modem->path, modem->online);
+ } else if (g_str_equal(key, "Interfaces") == TRUE) {
+ modem->interfaces = extract_interfaces(&value);
- DBG("status %s", status);
+ DBG("%s Interfaces 0x%02x", modem->path,
+ modem->interfaces);
+ } else if (g_str_equal(key, "Serial") == TRUE) {
+ char *serial;
- /* FIXME: add roaming support */
- } else if (g_str_equal(key, "PrimaryContexts") == TRUE) {
- check_networks(modem);
- } else if (g_str_equal(key, "Powered") == TRUE) {
- dbus_bool_t powered;
+ dbus_message_iter_get_basic(&value, &serial);
+ modem->serial = g_strdup(serial);
- if (modem->device == NULL)
- return TRUE;
+ DBG("%s Serial %s", modem->path, modem->serial);
+ }
- dbus_message_iter_get_basic(&value, &powered);
- connman_device_set_powered(modem->device, powered);
+ dbus_message_iter_next(prop);
}
- return TRUE;
+ if (modem->powered == FALSE) {
+ modem_set_powered(modem);
+ } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
+ sim_get_properties(modem);
+ } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+ if (ready_to_create_device(modem) == TRUE)
+ create_device(modem);
+ if (modem->device != NULL)
+ cm_get_properties(modem);
+ }
}
-static gboolean manager_changed(DBusConnection *connection,
- DBusMessage *message, void *user_data)
+static void remove_modem(gpointer data)
{
- const char *path = dbus_message_get_path(message);
- DBusMessageIter iter, value;
- const char *key;
+ struct modem_data *modem = data;
- DBG("path %s", path);
+ DBG("%s", modem->path);
- if (dbus_message_iter_init(message, &iter) == FALSE)
- return TRUE;
+ if (modem->call_set_property != NULL)
+ dbus_pending_call_cancel(modem->call_set_property);
- dbus_message_iter_get_basic(&iter, &key);
+ if (modem->call_get_properties != NULL)
+ dbus_pending_call_cancel(modem->call_get_properties);
- dbus_message_iter_next(&iter);
- dbus_message_iter_recurse(&iter, &value);
+ if (modem->device != NULL)
+ destroy_device(modem);
- if (g_str_equal(key, "Modems") == TRUE)
- update_modems(&value);
+ g_free(modem->serial);
+ g_free(modem->imsi);
+ g_free(modem->path);
- return TRUE;
+ g_free(modem);
}
-static void get_dns(DBusMessageIter *array, struct connman_element *parent)
+static gboolean modem_added(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
{
- DBusMessageIter entry;
- gchar *nameserver = NULL, *nameserver_old = NULL;
+ DBusMessageIter iter, properties;
+ const char *path;
DBG("");
- dbus_message_iter_recurse(array, &entry);
-
- while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
- const char *dns;
-
- dbus_message_iter_get_basic(&entry, &dns);
-
- DBG("dns %s", dns);
-
- if (nameserver == NULL) {
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- nameserver = g_strdup(dns);
- } else {
+ dbus_message_iter_get_basic(&iter, &path);
- nameserver_old = nameserver;
- nameserver = g_strdup_printf("%s %s",
- nameserver_old, dns);
- g_free(nameserver_old);
- }
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &properties);
- dbus_message_iter_next(&entry);
- }
+ add_modem(path, &properties);
- parent->ipv4.nameserver = nameserver;
+ return TRUE;
}
-static void update_settings(DBusMessageIter *array,
- struct connman_element *parent)
+static gboolean modem_removed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
{
- DBusMessageIter dict;
- const char *interface = NULL;
+ DBusMessageIter iter;
+ const char *path;
DBG("");
- if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
- return;
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- dbus_message_iter_recurse(array, &dict);
+ dbus_message_iter_get_basic(&iter, &path);
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
+ g_hash_table_remove(modem_hash, path);
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
+ return TRUE;
+}
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
+static void manager_get_modems_reply(DBusPendingCall *call, void *user_data)
+{
+ DBusMessage *reply;
+ DBusError error;
+ DBusMessageIter array, dict;
- if (g_str_equal(key, "Interface") == TRUE) {
- int index;
+ DBG("");
- dbus_message_iter_get_basic(&value, &interface);
+ reply = dbus_pending_call_steal_reply(call);
- DBG("interface %s", interface);
+ dbus_error_init(&error);
- index = connman_inet_ifindex(interface);
- if (index >= 0) {
- connman_network_set_index(
- pending_network, index);
- } else {
- connman_error("Can not find interface %s",
- interface);
- break;
- }
- } else if (g_str_equal(key, "Method") == TRUE) {
- const char *method;
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ goto done;
+ }
- dbus_message_iter_get_basic(&value, &method);
- if (g_strcmp0(method, "static") == 0) {
+ if (dbus_message_iter_init(reply, &array) == FALSE)
+ goto done;
- parent->ipv4.method =
- CONNMAN_IPCONFIG_METHOD_FIXED;
- } else if (g_strcmp0(method, "dhcp") == 0) {
+ dbus_message_iter_recurse(&array, &dict);
- parent->ipv4.method =
- CONNMAN_IPCONFIG_METHOD_DHCP;
- break;
- }
- } else if (g_str_equal(key, "Address") == TRUE) {
- const char *address;
+ while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
+ DBusMessageIter value, properties;
+ const char *path;
- dbus_message_iter_get_basic(&value, &address);
+ dbus_message_iter_recurse(&dict, &value);
+ dbus_message_iter_get_basic(&value, &path);
- DBG("address %s", address);
+ dbus_message_iter_next(&value);
+ dbus_message_iter_recurse(&value, &properties);
- parent->ipv4.address = g_strdup(address);
- } else if (g_str_equal(key, "Netmask") == TRUE) {
- const char *netmask;
+ add_modem(path, &properties);
- dbus_message_iter_get_basic(&value, &netmask);
+ dbus_message_iter_next(&dict);
+ }
- DBG("netmask %s", netmask);
+done:
+ dbus_message_unref(reply);
- parent->ipv4.netmask = g_strdup(netmask);
- } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
+ dbus_pending_call_unref(call);
+}
- get_dns(&value, parent);
- } else if (g_str_equal(key, "Gateway") == TRUE) {
- const char *gateway;
+static int manager_get_modems(void)
+{
+ DBusMessage *message;
+ DBusPendingCall *call;
- dbus_message_iter_get_basic(&value, &gateway);
+ DBG("");
- DBG("gateway %s", gateway);
+ message = dbus_message_new_method_call(OFONO_SERVICE, "/",
+ OFONO_MANAGER_INTERFACE, GET_MODEMS);
+ if (message == NULL)
+ return -ENOMEM;
- parent->ipv4.gateway = g_strdup(gateway);
- }
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to call GetModems()");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
- dbus_message_iter_next(&dict);
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
}
- /* deactive, oFono send NULL inteface before deactive signal */
- if (interface == NULL)
- connman_network_set_index(pending_network, -1);
-}
+ dbus_pending_call_set_notify(call, manager_get_modems_reply,
+ NULL, NULL);
-static void cleanup_ipconfig(struct connman_element *parent)
-{
- g_free(parent->ipv4.address);
- parent->ipv4.address = NULL;
+ dbus_message_unref(message);
- g_free(parent->ipv4.netmask);
- parent->ipv4.netmask = NULL;
+ return -EINPROGRESS;
+}
- g_free(parent->ipv4.nameserver);
- parent->ipv4.nameserver = NULL;
+static void ofono_connect(DBusConnection *conn, void *user_data)
+{
+ DBG("");
- g_free(parent->ipv4.gateway);
- parent->ipv4.gateway = NULL;
+ modem_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, remove_modem);
+ if (modem_hash == NULL)
+ return;
- parent->ipv4.method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
+ manager_get_modems();
}
-static int static_network_set_connected(
- struct connman_network *pending_network,
- struct connman_element *parent,
- connman_bool_t connected)
+static void ofono_disconnect(DBusConnection *conn, void *user_data)
{
- if (connected == TRUE) {
- struct connman_element *element;
+ DBG("");
- if (parent->ipv4.address == NULL)
- goto failed;
+ if (modem_hash == NULL)
+ return;
- if (parent->ipv4.netmask == NULL)
- goto failed;
+ g_hash_table_destroy(modem_hash);
+ modem_hash = NULL;
+}
- element = connman_element_create(NULL);
- if (element == NULL) {
- connman_error("Can not create connman_element");
- return -ENOMEM;
- }
+static int network_probe(struct connman_network *network)
+{
+ DBG("network %p", network);
- element->type = CONNMAN_ELEMENT_TYPE_IPV4;
- element->index = parent->index;
+ return 0;
+}
- if (connman_element_register(element, parent) < 0) {
- connman_element_unref(element);
- goto failed;
- }
- } else
- cleanup_ipconfig(parent);
+static void network_remove(struct connman_network *network)
+{
+ DBG("network %p", network);
+}
- connman_network_set_connected(pending_network, connected);
+static int network_connect(struct connman_network *network)
+{
+ DBG("network %p", network);
return 0;
+}
-failed:
- connman_network_set_error(pending_network,
- CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
-
- cleanup_ipconfig(parent);
+static int network_disconnect(struct connman_network *network)
+{
+ DBG("network %p", network);
- return -EINVAL;
+ return 0;
}
-static gboolean pri_context_changed(DBusConnection *connection,
- DBusMessage *message, void *user_data)
+static struct connman_network_driver network_driver = {
+ .name = "network",
+ .type = CONNMAN_NETWORK_TYPE_CELLULAR,
+ .probe = network_probe,
+ .remove = network_remove,
+ .connect = network_connect,
+ .disconnect = network_disconnect,
+};
+
+static int modem_probe(struct connman_device *device)
{
- const char *path = dbus_message_get_path(message);
- struct connman_element *parent;
- const char *pending_path;
- DBusMessageIter iter, value;
- const char *key;
+ struct modem_data *modem = connman_device_get_data(device);
- DBG("pending_network %p, path %s", pending_network, path);
+ DBG("%s device %p", modem->path, device);
- if (pending_network == NULL)
- return TRUE;
+ return 0;
+}
- pending_path = connman_network_get_string(pending_network, "Path");
- if (g_strcmp0(pending_path, path) != 0)
- return TRUE;
+static void modem_remove(struct connman_device *device)
+{
+ struct modem_data *modem = connman_device_get_data(device);
- parent = connman_network_get_element(pending_network);
+ DBG("%s device %p", modem->path, device);
+}
- if (dbus_message_iter_init(message, &iter) == FALSE)
- return TRUE;
+static int modem_enable(struct connman_device *device)
+{
+ struct modem_data *modem = connman_device_get_data(device);
- dbus_message_iter_get_basic(&iter, &key);
+ DBG("%s device %p", modem->path, device);
- dbus_message_iter_next(&iter);
- dbus_message_iter_recurse(&iter, &value);
+ return 0;
+}
- if (g_str_equal(key, "Settings") == TRUE) {
-
- update_settings(&value, parent);
- } else if (g_str_equal(key, "Active") == TRUE) {
- dbus_bool_t active;
-
- dbus_message_iter_get_basic(&value, &active);
-
- switch (parent->ipv4.method) {
- case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
- case CONNMAN_IPCONFIG_METHOD_OFF:
- case CONNMAN_IPCONFIG_METHOD_MANUAL:
- break;
- case CONNMAN_IPCONFIG_METHOD_FIXED:
- if (static_network_set_connected(
- pending_network, parent, active) < 0)
- set_network_active(pending_network, FALSE);
- break;
- case CONNMAN_IPCONFIG_METHOD_DHCP:
- connman_network_set_method(pending_network,
- CONNMAN_IPCONFIG_METHOD_DHCP);
- connman_network_set_connected(pending_network, active);
- break;
- }
+static int modem_disable(struct connman_device *device)
+{
+ struct modem_data *modem = connman_device_get_data(device);
- pending_network = NULL;
- }
+ DBG("%s device %p", modem->path, device);
- return TRUE;
+ return 0;
}
+static struct connman_device_driver modem_driver = {
+ .name = "modem",
+ .type = CONNMAN_DEVICE_TYPE_CELLULAR,
+ .probe = modem_probe,
+ .remove = modem_remove,
+ .enable = modem_enable,
+ .disable = modem_disable,
+};
+
static guint watch;
-static guint gprs_watch;
+static guint modem_added_watch;
+static guint modem_removed_watch;
static guint modem_watch;
-static guint manager_watch;
+static guint cm_watch;
+static guint sim_watch;
+static guint context_added_watch;
+static guint context_removed_watch;
+static guint netreg_watch;
static guint context_watch;
static int ofono_init(void)
{
int err;
+ DBG("");
+
connection = connman_dbus_get_connection();
if (connection == NULL)
return -EIO;
- watch = g_dbus_add_service_watch(connection, OFONO_SERVICE,
- ofono_connect, ofono_disconnect, NULL, NULL);
+ watch = g_dbus_add_service_watch(connection,
+ OFONO_SERVICE, ofono_connect,
+ ofono_disconnect, NULL, NULL);
- gprs_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
- OFONO_GPRS_INTERFACE,
- PROPERTY_CHANGED,
- gprs_changed,
+ modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_MANAGER_INTERFACE,
+ MODEM_ADDED,
+ modem_added,
+ NULL, NULL);
+
+ modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_MANAGER_INTERFACE,
+ MODEM_REMOVED,
+ modem_removed,
NULL, NULL);
modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
modem_changed,
NULL, NULL);
- manager_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
- OFONO_MANAGER_INTERFACE,
+ cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_CM_INTERFACE,
+ PROPERTY_CHANGED,
+ cm_changed,
+ NULL, NULL);
+
+ sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_SIM_INTERFACE,
PROPERTY_CHANGED,
- manager_changed,
+ sim_changed,
+ NULL, NULL);
+
+ context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_CM_INTERFACE,
+ CONTEXT_ADDED,
+ cm_context_added,
+ NULL, NULL);
+
+ context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_CM_INTERFACE,
+ CONTEXT_REMOVED,
+ cm_context_removed,
NULL, NULL);
context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
- OFONO_PRI_CONTEXT_INTERFACE,
+ OFONO_CONTEXT_INTERFACE,
+ PROPERTY_CHANGED,
+ context_changed,
+ NULL, NULL);
+
+ netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ OFONO_NETREG_INTERFACE,
PROPERTY_CHANGED,
- pri_context_changed,
+ netreg_changed,
NULL, NULL);
- if (watch == 0 || gprs_watch == 0 || modem_watch == 0 ||
- manager_watch == 0 || context_watch == 0) {
+
+ if (watch == 0 || modem_added_watch == 0 || modem_removed_watch == 0 ||
+ modem_watch == 0 || cm_watch == 0 || sim_watch == 0 ||
+ context_added_watch == 0 ||
+ context_removed_watch == 0 ||
+ context_watch == 0 || netreg_watch == 0) {
err = -EIO;
goto remove;
}
return 0;
remove:
- g_dbus_remove_watch(connection, watch);
- g_dbus_remove_watch(connection, gprs_watch);
- g_dbus_remove_watch(connection, modem_watch);
- g_dbus_remove_watch(connection, manager_watch);
+ g_dbus_remove_watch(connection, netreg_watch);
g_dbus_remove_watch(connection, context_watch);
-
+ g_dbus_remove_watch(connection, context_removed_watch);
+ g_dbus_remove_watch(connection, context_added_watch);
+ g_dbus_remove_watch(connection, sim_watch);
+ g_dbus_remove_watch(connection, cm_watch);
+ g_dbus_remove_watch(connection, modem_watch);
+ g_dbus_remove_watch(connection, modem_removed_watch);
+ g_dbus_remove_watch(connection, modem_added_watch);
+ g_dbus_remove_watch(connection, watch);
dbus_connection_unref(connection);
return err;
static void ofono_exit(void)
{
- g_dbus_remove_watch(connection, watch);
- g_dbus_remove_watch(connection, gprs_watch);
- g_dbus_remove_watch(connection, modem_watch);
- g_dbus_remove_watch(connection, manager_watch);
- g_dbus_remove_watch(connection, context_watch);
+ DBG("");
- ofono_disconnect(connection, NULL);
+ if (modem_hash != NULL) {
+ g_hash_table_destroy(modem_hash);
+ modem_hash = NULL;
+ }
connman_device_driver_unregister(&modem_driver);
connman_network_driver_unregister(&network_driver);
+ g_dbus_remove_watch(connection, netreg_watch);
+ g_dbus_remove_watch(connection, context_watch);
+ g_dbus_remove_watch(connection, context_removed_watch);
+ g_dbus_remove_watch(connection, context_added_watch);
+ g_dbus_remove_watch(connection, sim_watch);
+ g_dbus_remove_watch(connection, cm_watch);
+ g_dbus_remove_watch(connection, modem_watch);
+ g_dbus_remove_watch(connection, modem_added_watch);
+ g_dbus_remove_watch(connection, modem_removed_watch);
+ g_dbus_remove_watch(connection, watch);
+
dbus_connection_unref(connection);
}