+ bool success = true;
+
+ 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("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)
+ (*info->set_property_cb)(info->modem, info->context,
+ success);
+
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+static int set_property(struct modem_data *modem,
+ struct network_context *context,
+ const char *path, const char *interface,
+ const char *property, int type, void *value,
+ set_property_cb notify)
+{
+ DBusMessage *message;
+ DBusMessageIter iter;
+ struct property_info *info;
+
+ DBG("%s path %s %s.%s", modem->path, path, interface, property);
+
+ if (modem->call_set_property) {
+ DBG("Cancel pending SetProperty");
+ dbus_pending_call_cancel(modem->call_set_property);
+ dbus_pending_call_unref(modem->call_set_property);
+ modem->call_set_property = NULL;
+ }
+
+ message = dbus_message_new_method_call(OFONO_SERVICE, path,
+ interface, SET_PROPERTY);
+ if (!message)
+ return -ENOMEM;
+
+ dbus_message_iter_init_append(message, &iter);
+ connman_dbus_property_append_basic(&iter, property, type, value);
+
+ if (!dbus_connection_send_with_reply(connection, message,
+ &modem->call_set_property, TIMEOUT)) {
+ connman_error("Failed to change property: %s %s.%s",
+ path, interface, property);
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ if (!modem->call_set_property) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ info = g_try_new0(struct property_info, 1);
+ if (!info) {
+ dbus_message_unref(message);
+ return -ENOMEM;
+ }
+
+ info->modem = modem;
+ info->context = context;
+ info->path = path;
+ info->interface = interface;
+ info->property = property;
+ info->set_property_cb = notify;
+
+ if (info->context)
+ network_context_ref(info->context);
+
+ dbus_pending_call_set_notify(modem->call_set_property,
+ set_property_reply, info, free_property_info);
+
+ dbus_message_unref(message);
+
+ return -EINPROGRESS;
+}
+
+static void get_properties_reply(DBusPendingCall *call, void *user_data)
+{
+ struct property_info *info = user_data;
+ DBusMessageIter array, dict;
+ DBusMessage *reply;
+ DBusError error;
+
+ DBG("%s path %s %s", info->modem->path, info->path, info->interface);
+
+ info->modem->call_get_properties = NULL;
+
+ dbus_error_init(&error);
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ 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);
+
+ goto done;
+ }
+
+ if (!dbus_message_iter_init(reply, &array))
+ goto done;
+
+ if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
+ goto done;
+
+ dbus_message_iter_recurse(&array, &dict);
+
+ if (info->get_properties_cb)
+ (*info->get_properties_cb)(info->modem, &dict);
+
+done:
+
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+static int get_properties(const char *path, const char *interface,
+ get_properties_cb notify,
+ struct modem_data *modem)
+{
+ DBusMessage *message;
+ struct property_info *info;
+
+ DBG("%s path %s %s", modem->path, path, interface);
+
+ if (modem->call_get_properties) {
+ connman_error("Pending GetProperties");
+ return -EBUSY;
+ }
+
+ message = dbus_message_new_method_call(OFONO_SERVICE, path,
+ interface, GET_PROPERTIES);
+ if (!message)
+ return -ENOMEM;
+
+ if (!dbus_connection_send_with_reply(connection, message,
+ &modem->call_get_properties, TIMEOUT)) {
+ connman_error("Failed to call %s.GetProperties()", interface);
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ if (!modem->call_get_properties) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ info = g_try_new0(struct property_info, 1);
+ if (!info) {
+ dbus_message_unref(message);
+ return -ENOMEM;
+ }
+
+ 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);
+
+ dbus_message_unref(message);
+
+ return -EINPROGRESS;
+}
+
+static void context_set_active_reply(struct modem_data *modem,
+ struct network_context *context, bool success)
+{
+ DBG("%s", context->path);
+
+ if (success) {
+ /*
+ * Don't handle do anything on success here. oFono will send
+ * the change via PropertyChanged singal.
+ */
+ return;
+ }
+
+ /*
+ * Active = True might fail due a timeout. That means oFono
+ * still tries to go online. If we retry to set Active = True,
+ * we just get a InProgress error message. Should we power
+ * cycle the modem in such cases?
+ */
+
+ if (!context->network) {
+ /*
+ * In the case where we power down the device
+ * we don't wait for the reply, therefore the network
+ * might already be gone.
+ */
+ return;
+ }
+
+ connman_network_set_error(context->network,
+ CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
+}
+
+static int context_set_active(struct modem_data *modem,
+ struct network_context *context,
+ dbus_bool_t active)
+{
+ int err;
+
+ DBG("%s active %d", modem->path, active);
+
+ err = set_property(modem, context, context->path,
+ OFONO_CONTEXT_INTERFACE,
+ "Active", DBUS_TYPE_BOOLEAN,
+ &active,
+ context_set_active_reply);
+
+ if (!active && err == -EINPROGRESS)
+ return 0;
+
+ return err;
+}
+
+static void cdma_cm_set_powered_reply(struct modem_data *modem,
+ struct network_context *context, bool success)
+{
+ DBG("%s", context->path);
+
+ if (success) {
+ /*
+ * Don't handle do anything on success here. oFono will send
+ * the change via PropertyChanged singal.
+ */
+ return;
+ }
+
+ /*
+ * Powered = True might fail due a timeout. That means oFono
+ * still tries to go online. If we retry to set Powered = True,
+ * we just get a InProgress error message. Should we power
+ * cycle the modem in such cases?
+ */
+
+ if (!context->network) {
+ /*
+ * In the case where we power down the device
+ * we don't wait for the reply, therefore the network
+ * might already be gone.
+ */
+ return;
+ }
+
+ connman_network_set_error(context->network,
+ CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
+}
+
+static int cdma_cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
+{
+ int err;
+ struct network_context *context = NULL;
+
+ if (!modem->context_list)
+ return -1;
+
+ DBG("%s powered %d", modem->path, powered);
+
+ /* In case of CDMA, there is only one context */
+ context = modem->context_list->data;
+ err = set_property(modem, context, modem->path,
+ OFONO_CDMA_CM_INTERFACE,
+ "Powered", DBUS_TYPE_BOOLEAN,
+ &powered,
+ cdma_cm_set_powered_reply);
+
+ if (!powered && err == -EINPROGRESS)
+ return 0;
+
+ return err;
+}
+
+static int modem_set_online(struct modem_data *modem, dbus_bool_t online)
+{
+ DBG("%s online %d", modem->path, online);
+
+ return set_property(modem, NULL, modem->path,
+ OFONO_MODEM_INTERFACE,
+ "Online", DBUS_TYPE_BOOLEAN,
+ &online,
+ NULL);
+}
+
+static int cm_set_powered(struct modem_data *modem, dbus_bool_t powered)
+{
+ int err;
+
+ DBG("%s powered %d", modem->path, powered);
+
+ err = set_property(modem, NULL, modem->path,
+ OFONO_CM_INTERFACE,
+ "Powered", DBUS_TYPE_BOOLEAN,
+ &powered,
+ NULL);
+
+ if (!powered && err == -EINPROGRESS)
+ return 0;
+
+ return err;
+}
+
+static int modem_set_powered(struct modem_data *modem, dbus_bool_t powered)
+{
+ int err;
+
+ DBG("%s powered %d", modem->path, powered);
+
+ modem->set_powered = powered;
+
+ err = set_property(modem, NULL, modem->path,
+ OFONO_MODEM_INTERFACE,
+ "Powered", DBUS_TYPE_BOOLEAN,
+ &powered,
+ NULL);
+
+ if (!powered && err == -EINPROGRESS)
+ return 0;
+
+ return err;
+}
+
+static bool has_interface(uint8_t interfaces,
+ enum ofono_api api)
+{
+ if ((interfaces & api) == api)
+ return true;
+
+ return false;
+}
+
+static uint8_t extract_interfaces(DBusMessageIter *array)
+{
+ DBusMessageIter entry;
+ uint8_t interfaces = 0;
+
+ dbus_message_iter_recurse(array, &entry);
+
+ while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
+ const char *name;
+
+ dbus_message_iter_get_basic(&entry, &name);
+
+ if (g_str_equal(name, OFONO_SIM_INTERFACE))
+ interfaces |= OFONO_API_SIM;
+ else if (g_str_equal(name, OFONO_NETREG_INTERFACE))
+ interfaces |= OFONO_API_NETREG;
+ else if (g_str_equal(name, OFONO_CM_INTERFACE))
+ interfaces |= OFONO_API_CM;
+ else if (g_str_equal(name, OFONO_CDMA_CM_INTERFACE))
+ interfaces |= OFONO_API_CDMA_CM;
+ else if (g_str_equal(name, OFONO_CDMA_NETREG_INTERFACE))
+ interfaces |= OFONO_API_CDMA_NETREG;
+
+ dbus_message_iter_next(&entry);
+ }
+
+ return interfaces;
+}
+
+static char *extract_nameservers(DBusMessageIter *array)
+{
+ DBusMessageIter entry;
+ char *nameservers = NULL;
+ char *tmp;
+
+ dbus_message_iter_recurse(array, &entry);
+
+ while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
+ const char *nameserver;
+
+ dbus_message_iter_get_basic(&entry, &nameserver);
+
+ if (!nameservers) {
+ nameservers = g_strdup(nameserver);
+ } else {
+ tmp = nameservers;
+ nameservers = g_strdup_printf("%s %s", tmp, nameserver);
+ g_free(tmp);
+ }
+
+ dbus_message_iter_next(&entry);
+ }
+
+ return nameservers;
+}
+
+static void extract_ipv4_settings(DBusMessageIter *array,
+ struct network_context *context)
+{
+ DBusMessageIter dict;
+ char *address = NULL, *netmask = NULL, *gateway = NULL;
+ char *nameservers = NULL;
+ const char *interface = NULL;
+ int index = -1;
+
+ connman_ipaddress_free(context->ipv4_address);
+ context->ipv4_address = NULL;
+
+ if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
+ return;
+
+ dbus_message_iter_recurse(array, &dict);
+
+ while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter entry, value;
+ const char *key, *val;
+
+ 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, "Interface")) {
+ dbus_message_iter_get_basic(&value, &interface);
+
+ DBG("Interface %s", interface);
+
+ index = connman_inet_ifindex(interface);
+
+ DBG("index %d", index);
+ } else if (g_str_equal(key, "Method")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ DBG("Method %s", val);
+
+ if (g_strcmp0(val, "static") == 0)
+ context->ipv4_method = CONNMAN_IPCONFIG_METHOD_FIXED;
+ else if (g_strcmp0(val, "dhcp") == 0)
+ context->ipv4_method = CONNMAN_IPCONFIG_METHOD_DHCP;
+
+ } else if (g_str_equal(key, "Address")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ address = g_strdup(val);
+
+ DBG("Address %s", address);
+ } else if (g_str_equal(key, "Netmask")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ netmask = g_strdup(val);
+
+ DBG("Netmask %s", netmask);
+ } else if (g_str_equal(key, "DomainNameServers")) {
+ nameservers = extract_nameservers(&value);
+
+ DBG("Nameservers %s", nameservers);
+ } else if (g_str_equal(key, "Gateway")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ gateway = g_strdup(val);
+
+ DBG("Gateway %s", gateway);
+ }
+
+ dbus_message_iter_next(&dict);
+ }
+
+ if (index < 0)
+ goto out;
+
+ context->index = index;
+
+ if (context->ipv4_method != CONNMAN_IPCONFIG_METHOD_FIXED)
+ goto out;
+
+ context->ipv4_address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
+ if (!context->ipv4_address) {
+ context->index = -1;
+ goto out;
+ }
+
+ connman_ipaddress_set_ipv4(context->ipv4_address, address,
+ netmask, gateway);
+
+ g_free(context->ipv4_nameservers);
+ context->ipv4_nameservers = nameservers;
+
+out:
+ if (context->ipv4_nameservers != nameservers)
+ g_free(nameservers);
+
+ g_free(address);
+ g_free(netmask);
+ g_free(gateway);
+}
+
+static void extract_ipv6_settings(DBusMessageIter *array,
+ struct network_context *context)
+{
+ DBusMessageIter dict;
+ char *address = NULL, *gateway = NULL;
+ unsigned char prefix_length = 0;
+ char *nameservers = NULL;
+ const char *interface = NULL;
+ int index = -1;
+
+ connman_ipaddress_free(context->ipv6_address);
+ context->ipv6_address = NULL;
+
+ if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
+ return;
+
+ dbus_message_iter_recurse(array, &dict);
+
+ while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter entry, value;
+ const char *key, *val;
+
+ 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, "Interface")) {
+ dbus_message_iter_get_basic(&value, &interface);
+
+ DBG("Interface %s", interface);
+
+ index = connman_inet_ifindex(interface);
+
+ DBG("index %d", index);
+ } else if (g_str_equal(key, "Address")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ address = g_strdup(val);
+
+ DBG("Address %s", address);
+ } else if (g_str_equal(key, "PrefixLength")) {
+ dbus_message_iter_get_basic(&value, &prefix_length);
+
+ DBG("prefix length %d", prefix_length);
+ } else if (g_str_equal(key, "DomainNameServers")) {
+ nameservers = extract_nameservers(&value);
+
+ DBG("Nameservers %s", nameservers);
+ } else if (g_str_equal(key, "Gateway")) {
+ dbus_message_iter_get_basic(&value, &val);
+
+ gateway = g_strdup(val);
+
+ DBG("Gateway %s", gateway);
+ }
+
+ dbus_message_iter_next(&dict);
+ }
+
+ if (index < 0)
+ goto out;
+
+ context->ipv6_method = CONNMAN_IPCONFIG_METHOD_AUTO;
+
+ context->ipv6_address =
+ connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV6);
+ if (!context->ipv6_address)
+ goto out;
+
+ context->index = index;
+ connman_ipaddress_set_ipv6(context->ipv6_address, address,
+ prefix_length, gateway);
+
+ g_free(context->ipv6_nameservers);
+ context->ipv6_nameservers = nameservers;
+
+out:
+ if (context->ipv6_nameservers != nameservers)
+ g_free(nameservers);
+
+ g_free(address);
+ g_free(gateway);
+}
+
+static bool 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.
+ * - 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)
+ return false;
+
+ if (modem->imsi || modem->serial)
+ return true;
+
+ return false;
+}
+
+static void create_device(struct modem_data *modem)
+{
+ struct connman_device *device;
+ char *ident = NULL;
+
+ DBG("%s", modem->path);
+
+ if (modem->imsi)
+ ident = modem->imsi;
+ else if (modem->serial)
+ ident = modem->serial;
+
+ if (!connman_dbus_validate_ident(ident))
+ ident = connman_dbus_encode_string(ident);
+ else
+ ident = g_strdup(ident);
+
+ device = connman_device_create("ofono", CONNMAN_DEVICE_TYPE_CELLULAR);
+ if (!device)
+ goto out;
+
+ DBG("device %p", device);
+
+ connman_device_set_ident(device, ident);
+
+ connman_device_set_string(device, "Path", modem->path);
+
+ 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;
+ }
+
+ modem->device = device;
+
+ connman_device_set_powered(modem->device, modem->online);
+out:
+ g_free(ident);
+}
+
+static void destroy_device(struct modem_data *modem)
+{
+ DBG("%s", modem->path);
+
+ connman_device_set_powered(modem->device, false);
+
+ connman_device_unregister(modem->device);
+ connman_device_unref(modem->device);
+
+ modem->device = NULL;
+}
+
+static void add_network(struct modem_data *modem,
+ struct network_context *context)
+{
+ const char *group;
+
+ DBG("%s", modem->path);
+
+ if (context->network)
+ return;
+
+ context->network = connman_network_create(context->path,
+ CONNMAN_NETWORK_TYPE_CELLULAR);
+ if (!context->network)
+ return;
+
+ DBG("network %p", context->network);
+
+ connman_network_set_data(context->network, modem);
+
+ connman_network_set_string(context->network, "Path",
+ context->path);
+
+ if (modem->name)
+ connman_network_set_name(context->network, modem->name);
+ else
+ connman_network_set_name(context->network, "");
+
+ connman_network_set_strength(context->network, modem->strength);
+
+ group = get_ident(context->path);
+ connman_network_set_group(context->network, group);
+
+ connman_network_set_bool(context->network, "Roaming",
+ modem->roaming);
+
+ if (connman_device_add_network(modem->device, context->network) < 0) {
+ connman_network_unref(context->network);
+ context->network = NULL;
+ return;
+ }
+}
+
+static void remove_network(struct modem_data *modem,
+ struct network_context *context)
+{
+ DBG("%s", modem->path);
+
+ if (!context || !context->network)
+ return;
+
+ DBG("network %p", context->network);
+
+ if (modem->device)
+ connman_device_remove_network(modem->device, context->network);
+ connman_network_unref(context->network);
+ context->network = NULL;
+}
+
+static int set_context_ipconfig(struct network_context *context,
+ const char *protocol)
+{
+ DBG("context %p protocol %s", context, protocol);
+
+ if (!context || !protocol)
+ return -EINVAL;
+
+ if (g_str_equal(protocol, "ip")) {
+ if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
+ context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
+
+ context->ipv6_method = CONNMAN_IPCONFIG_METHOD_OFF;
+
+ connman_ipaddress_free(context->ipv6_address);
+ context->ipv6_address = NULL;
+
+ } else if (g_str_equal(protocol, "ipv6")) {
+ if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
+ context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
+
+ context->ipv4_method = CONNMAN_IPCONFIG_METHOD_OFF;
+
+ connman_ipaddress_free(context->ipv4_address);
+ context->ipv4_address = NULL;
+
+ } else if (g_str_equal(protocol, "dual")) {
+ if (context->ipv4_method == CONNMAN_IPCONFIG_METHOD_OFF)
+ context->ipv4_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
+
+ if (context->ipv6_method == CONNMAN_IPCONFIG_METHOD_OFF)
+ context->ipv6_method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
+ }
+
+ DBG("ipv4 method %d ipv6 method %d", context->ipv4_method,
+ context->ipv6_method);
+
+ return 0;
+}
+
+static int add_cm_context(struct modem_data *modem, const char *context_path,
+ DBusMessageIter *dict)
+{
+ const char *context_type = NULL;
+ struct network_context *context = NULL;
+ dbus_bool_t active = FALSE;
+ const char *ip_protocol = NULL;
+
+ DBG("%s context path %s", modem->path, context_path);
+
+ context = network_context_alloc(context_path);
+ if (!context)
+ return -ENOMEM;
+
+ 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, "Type")) {
+ dbus_message_iter_get_basic(&value, &context_type);
+
+ DBG("%s context %s type %s", modem->path,
+ context_path, context_type);
+ } else if (g_str_equal(key, "Settings")) {
+ DBG("%s Settings", modem->path);
+
+ extract_ipv4_settings(&value, context);
+ } else if (g_str_equal(key, "IPv6.Settings")) {
+ DBG("%s IPv6.Settings", modem->path);
+
+ extract_ipv6_settings(&value, context);
+ } else if (g_str_equal(key, "Active")) {
+ dbus_message_iter_get_basic(&value, &active);
+
+ DBG("%s Active %d", modem->path, active);
+ } else if (g_str_equal(key, "AccessPointName")) {
+ const char *apn;
+
+ dbus_message_iter_get_basic(&value, &apn);
+ if (apn && strlen(apn) > 0)
+ context->valid_apn = true;
+ else
+ context->valid_apn = false;
+
+ DBG("%s AccessPointName '%s'", modem->path, apn);
+ } else if (g_str_equal(key, "Protocol") &&
+ dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
+
+ dbus_message_iter_get_basic(&value, &ip_protocol);
+
+ DBG("%s Protocol %s", modem->path, ip_protocol);
+ }
+
+ dbus_message_iter_next(dict);
+ }
+
+ if (g_strcmp0(context_type, "internet") != 0) {
+ network_context_unref(context);
+ return -EINVAL;
+ }
+
+ if (ip_protocol)
+ set_context_ipconfig(context, ip_protocol);
+
+ context->active = active;
+
+ modem->context_list = g_slist_prepend(modem->context_list, context);
+ g_hash_table_replace(context_hash, g_strdup(context_path), modem);
+
+ if (context->valid_apn && modem->attached &&
+ has_interface(modem->interfaces, OFONO_API_NETREG))
+ add_network(modem, context);
+
+ return 0;
+}
+
+static void remove_cm_context(struct modem_data *modem,
+ struct network_context *context)
+{
+ if (!modem->context_list)
+ return;
+ if (!context)
+ return;
+
+ g_hash_table_remove(context_hash, context->path);
+
+ if (context->network)
+ remove_network(modem, context);
+ modem->context_list = g_slist_remove(modem->context_list, context);
+
+ network_context_unref(context);
+ context = NULL;
+}
+
+static void remove_all_contexts(struct modem_data *modem)
+{
+ GSList *list = NULL;
+
+ DBG("");
+
+ if (modem->context_list == NULL)
+ return;
+
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ remove_cm_context(modem, context);
+ }
+ g_slist_free(modem->context_list);
+ modem->context_list = NULL;
+}
+
+static void remove_all_networks(struct modem_data *modem)
+{
+ GSList *list;
+
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ remove_network(modem, context);
+ }
+}
+
+static gboolean context_changed(DBusConnection *conn,
+ DBusMessage *message,
+ void *user_data)
+{
+ struct network_context *context = NULL;
+ const char *context_path = dbus_message_get_path(message);
+ struct modem_data *modem = NULL;
+ DBusMessageIter iter, value;
+ const char *key;
+
+ DBG("context_path %s", context_path);
+
+ modem = g_hash_table_lookup(context_hash, context_path);
+ if (!modem)
+ return TRUE;
+
+ context = get_context_with_path(modem->context_list, context_path);
+ if (!context)
+ return TRUE;
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ /*
+ * oFono guarantees the ordering of Settings and
+ * Active. Settings will always be send before Active = True.
+ * That means we don't have to order here.
+ */
+ if (g_str_equal(key, "Settings")) {
+ DBG("%s Settings", modem->path);
+
+ extract_ipv4_settings(&value, context);
+ } else if (g_str_equal(key, "IPv6.Settings")) {
+ DBG("%s IPv6.Settings", modem->path);
+
+ extract_ipv6_settings(&value, context);
+ } else if (g_str_equal(key, "Active")) {
+ dbus_bool_t active;
+
+ dbus_message_iter_get_basic(&value, &active);
+ context->active = active;
+
+ DBG("%s Active %d", modem->path, context->active);
+
+ if (context->active)
+ set_connected(modem, context);
+ else
+ set_disconnected(context);
+ } else if (g_str_equal(key, "AccessPointName")) {
+ const char *apn;
+
+ dbus_message_iter_get_basic(&value, &apn);
+
+ DBG("%s AccessPointName %s", modem->path, apn);
+
+ if (apn && strlen(apn) > 0) {
+ context->valid_apn = true;
+
+ if (context->network)
+ return TRUE;
+
+ if (!modem->attached)
+ return TRUE;
+
+ if (!has_interface(modem->interfaces,
+ OFONO_API_NETREG))
+ return TRUE;
+
+ add_network(modem, context);
+
+ if (context->active)
+ set_connected(modem, context);
+ } else {
+ context->valid_apn = false;
+
+ if (!context->network)
+ return TRUE;
+
+ remove_network(modem, context);
+ }
+
+ } else if (g_str_equal(key, "Protocol") &&
+ dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING ) {
+ const char *ip_protocol;
+
+ dbus_message_iter_get_basic(&value, &ip_protocol);
+
+ set_context_ipconfig(context, ip_protocol);
+ }
+
+ return TRUE;
+}
+
+static void cm_get_contexts_reply(DBusPendingCall *call, void *user_data)
+{
+ struct modem_data *modem = user_data;
+ DBusMessageIter array, dict, entry, value;
+ DBusMessage *reply;
+ DBusError error;
+
+ DBG("%s", modem->path);
+
+ modem->call_get_contexts = NULL;
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ dbus_error_init(&error);
+
+ if (dbus_set_error_from_message(&error, reply)) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ goto done;
+ }
+
+ if (!dbus_message_iter_init(reply, &array))
+ 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_STRUCT) {
+ const char *context_path;
+
+ dbus_message_iter_recurse(&dict, &entry);
+ dbus_message_iter_get_basic(&entry, &context_path);
+
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
+
+ if (add_cm_context(modem, context_path, &value))
+ break;
+
+ dbus_message_iter_next(&dict);
+ }
+
+done:
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+static int cm_get_contexts(struct modem_data *modem)
+{
+ DBusMessage *message;
+
+ DBG("%s", modem->path);
+
+ if (modem->call_get_contexts)
+ return -EBUSY;
+
+ message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
+ OFONO_CM_INTERFACE, GET_CONTEXTS);
+ if (!message)
+ return -ENOMEM;
+
+ if (!dbus_connection_send_with_reply(connection, message,
+ &modem->call_get_contexts, TIMEOUT)) {
+ connman_error("Failed to call GetContexts()");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ if (!modem->call_get_contexts) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ dbus_pending_call_set_notify(modem->call_get_contexts,
+ cm_get_contexts_reply,
+ modem, NULL);
+
+ dbus_message_unref(message);
+
+ return -EINPROGRESS;
+}
+
+static gboolean cm_context_added(DBusConnection *conn,
+ DBusMessage *message,
+ void *user_data)
+{
+ const char *path = dbus_message_get_path(message);
+ char *context_path;
+ struct modem_data *modem;
+ DBusMessageIter iter, properties, dict;
+
+ DBG("%s", path);
+
+ modem = g_hash_table_lookup(modem_hash, path);
+ if (!modem)
+ return TRUE;
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &context_path);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &properties);
+
+ /* Sometimes, we get an array instead of dict */
+ if (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_ARRAY) {
+ /* Must recurse again */
+ dbus_message_iter_recurse(&properties, &dict);
+ if (add_cm_context(modem, context_path, &dict) != 0)
+ return TRUE;
+ }
+ if (add_cm_context(modem, context_path, &properties) != 0)
+ return TRUE;
+
+ return TRUE;
+}
+
+static gboolean cm_context_removed(DBusConnection *conn,
+ DBusMessage *message,
+ void *user_data)
+{
+ const char *path = dbus_message_get_path(message);
+ const char *context_path;
+ struct modem_data *modem;
+ struct network_context *context;
+ DBusMessageIter iter;
+
+ DBG("context path %s", path);
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &context_path);
+
+ modem = g_hash_table_lookup(context_hash, context_path);
+ if (!modem)
+ return TRUE;
+
+ context = get_context_with_path(modem->context_list, context_path);
+ remove_cm_context(modem, context);
+
+ return TRUE;
+}
+
+static void netreg_update_name(struct modem_data *modem,
+ DBusMessageIter* value)
+{
+ char *name;
+ GSList *list;
+
+ dbus_message_iter_get_basic(value, &name);
+
+ DBG("%s Name %s", modem->path, name);
+
+ g_free(modem->name);
+ modem->name = g_strdup(name);
+
+ if (!modem->context_list)
+ return;
+
+ /* For all the context */
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ if (context->network) {
+ connman_network_set_name(context->network, modem->name);
+ connman_network_update(context->network);
+ }
+ }
+}
+
+static void netreg_update_strength(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ GSList *list;
+
+ dbus_message_iter_get_basic(value, &modem->strength);
+
+ DBG("%s Strength %d", modem->path, modem->strength);
+
+ if (!modem->context_list)
+ return;
+
+ /*
+ * GSM:
+ * We don't have 2 signal notifications we always report the strength
+ * signal. data_strength is always equal to 0.
+ *
+ * CDMA:
+ * In the case we have a data_strength signal (from 1xEVDO network)
+ * we don't need to update the value with strength signal (from 1xCDMA)
+ * because the modem is registered to 1xEVDO network for data call.
+ * In case we have no data_strength signal (not registered to 1xEVDO
+ * network), we must report the strength signal (registered to 1xCDMA
+ * network e.g slow mode).
+ */
+ if (modem->data_strength != 0)
+ return;
+
+ /* For all the context */
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ if (context->network) {
+ connman_network_set_strength(context->network,
+ modem->strength);
+ connman_network_update(context->network);
+ }
+ }
+}
+
+/* Retrieve 1xEVDO Data Strength signal */
+static void netreg_update_datastrength(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ GSList *list;
+
+ dbus_message_iter_get_basic(value, &modem->data_strength);
+
+ DBG("%s Data Strength %d", modem->path, modem->data_strength);
+
+ if (!modem->context_list)
+ return;
+
+ /*
+ * CDMA modem is not registered to 1xEVDO network, let
+ * update_signal_strength() reporting the value on the Strength signal
+ * notification.
+ */
+ if (modem->data_strength == 0)
+ return;
+
+ /* For all the context */
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ if (context->network) {
+ connman_network_set_strength(context->network,
+ modem->data_strength);
+ connman_network_update(context->network);
+ }
+ }
+}
+
+static void netreg_update_status(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ char *status;
+ bool roaming;
+ GSList *list;
+
+ dbus_message_iter_get_basic(value, &status);
+
+ roaming = g_str_equal(status, "roaming");
+ modem->registered = roaming || g_str_equal(status, "registered");
+
+ if (roaming == modem->roaming)
+ return;
+
+ modem->roaming = roaming;
+
+ if (!modem->context_list)
+ return;
+
+ /* For all the context */
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ if (context->network) {
+ connman_network_set_bool(context->network,
+ "Roaming", modem->roaming);
+ connman_network_update(context->network);
+ }
+ }
+}
+
+static void netreg_update_regdom(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ char *mobile_country_code;
+ char *alpha2;
+ int mcc;
+
+ dbus_message_iter_get_basic(value, &mobile_country_code);
+
+ DBG("%s MobileContryCode %s", modem->path, mobile_country_code);
+
+
+ mcc = atoi(mobile_country_code);
+ if (mcc > 799 || mcc < 200)
+ return;
+
+ alpha2 = mcc_country_codes[mcc - 200];
+ if (alpha2)
+ connman_technology_set_regdom(alpha2);
+}
+
+static gboolean netreg_changed(DBusConnection *conn, DBusMessage *message,
+ void *user_data)
+{
+ 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)
+ return TRUE;
+
+ if (modem->ignore)
+ return TRUE;
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ if (g_str_equal(key, "Name"))
+ netreg_update_name(modem, &value);
+ else if (g_str_equal(key, "Strength"))
+ netreg_update_strength(modem, &value);
+ else if (g_str_equal(key, "Status"))
+ netreg_update_status(modem, &value);
+ else if (g_str_equal(key, "MobileCountryCode"))
+ netreg_update_regdom(modem, &value);
+
+ return TRUE;
+}
+
+static void netreg_properties_reply(struct modem_data *modem,
+ DBusMessageIter *dict)
+{
+ GSList *list = NULL;
+
+ DBG("%s", modem->path);
+
+ 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"))
+ netreg_update_name(modem, &value);
+ else if (g_str_equal(key, "Strength"))
+ netreg_update_strength(modem, &value);
+ else if (g_str_equal(key, "Status"))
+ netreg_update_status(modem, &value);
+ else if (g_str_equal(key, "MobileCountryCode"))
+ netreg_update_regdom(modem, &value);
+
+ dbus_message_iter_next(dict);
+ }
+
+ if (!modem->context_list) {
+ /*
+ * netgreg_get_properties() was issued after we got
+ * cm_get_contexts_reply() where we create the
+ * context. Though before we got the
+ * netreg_properties_reply the context was removed
+ * again. Therefore we have to skip the network
+ * creation.
+ */
+ return;
+ }
+ /* Check for all contexts if they are valids and/or actives */
+ for (list = modem->context_list; list; list = list->next) {
+ struct network_context *context = list->data;
+
+ if (context->valid_apn)
+ add_network(modem, context);
+ if (context->active)
+ set_connected(modem, context);
+ }
+}
+
+static int netreg_get_properties(struct modem_data *modem)
+{
+ return get_properties(modem->path, OFONO_NETREG_INTERFACE,
+ netreg_properties_reply, modem);
+}
+
+static void add_cdma_network(struct modem_data *modem)
+{
+ struct network_context *context = NULL;
+ /* Be sure that device is created before adding CDMA network */
+ if (!modem->device)
+ return;
+
+ /*
+ * CDMA modems don't need contexts for data call, however the current
+ * add_network() logic needs one, so we create one to proceed.
+ */
+ if (!modem->context_list) {
+ context = network_context_alloc(modem->path);
+ modem->context_list = g_slist_prepend(modem->context_list,
+ context);
+ } else
+ context = modem->context_list->data;
+
+ if (!modem->name)
+ modem->name = g_strdup("CDMA Network");
+
+ add_network(modem, context);
+
+ if (modem->cdma_cm_powered)
+ set_connected(modem, context);
+}
+
+static gboolean cdma_netreg_changed(DBusConnection *conn,
+ DBusMessage *message,
+ void *user_data)
+{
+ const char *path = dbus_message_get_path(message);
+ struct modem_data *modem;
+ DBusMessageIter iter, value;
+ const char *key;
+
+ DBG("");
+
+ modem = g_hash_table_lookup(modem_hash, path);
+ if (!modem)
+ return TRUE;
+
+ if (modem->ignore)
+ return TRUE;
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ if (g_str_equal(key, "Name"))
+ netreg_update_name(modem, &value);
+ else if (g_str_equal(key, "Strength"))
+ netreg_update_strength(modem, &value);
+ else if (g_str_equal(key, "DataStrength"))
+ netreg_update_datastrength(modem, &value);
+ else if (g_str_equal(key, "Status"))
+ netreg_update_status(modem, &value);
+
+ if (modem->registered)
+ add_cdma_network(modem);
+ else
+ remove_all_networks(modem);
+
+ return TRUE;
+}
+
+static void cdma_netreg_properties_reply(struct modem_data *modem,
+ DBusMessageIter *dict)
+{
+ DBG("%s", modem->path);
+
+ 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"))
+ netreg_update_name(modem, &value);
+ else if (g_str_equal(key, "Strength"))
+ netreg_update_strength(modem, &value);
+ else if (g_str_equal(key, "DataStrength"))
+ netreg_update_datastrength(modem, &value);
+ else if (g_str_equal(key, "Status"))
+ netreg_update_status(modem, &value);
+
+ dbus_message_iter_next(dict);
+ }
+
+ if (modem->registered)
+ add_cdma_network(modem);
+ else
+ remove_all_networks(modem);
+}
+
+static int cdma_netreg_get_properties(struct modem_data *modem)
+{
+ return get_properties(modem->path, OFONO_CDMA_NETREG_INTERFACE,
+ cdma_netreg_properties_reply, modem);
+}
+
+static void cm_update_attached(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ dbus_bool_t attached;
+
+ dbus_message_iter_get_basic(value, &attached);
+ modem->attached = attached;
+
+ DBG("%s Attached %d", modem->path, modem->attached);
+
+ if (!modem->attached) {
+ remove_all_networks(modem);
+ return;
+ }
+
+ if (!has_interface(modem->interfaces, OFONO_API_NETREG))
+ return;
+
+ netreg_get_properties(modem);
+}
+
+static void cm_update_powered(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ dbus_bool_t cm_powered;
+
+ dbus_message_iter_get_basic(value, &cm_powered);
+ modem->cm_powered = cm_powered;
+
+ DBG("%s ConnnectionManager Powered %d", modem->path,
+ modem->cm_powered);
+
+ if (modem->cm_powered)
+ return;
+
+ cm_set_powered(modem, TRUE);
+}
+
+static gboolean cm_changed(DBusConnection *conn, DBusMessage *message,
+ void *user_data)
+{
+ 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)
+ return TRUE;
+
+ if (modem->ignore)
+ return TRUE;
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ if (g_str_equal(key, "Attached"))
+ cm_update_attached(modem, &value);
+ else if (g_str_equal(key, "Powered"))
+ cm_update_powered(modem, &value);
+
+ return TRUE;
+}
+
+static void cdma_cm_update_powered(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ struct network_context *context = NULL;
+ dbus_bool_t cdma_cm_powered;
+
+ dbus_message_iter_get_basic(value, &cdma_cm_powered);
+ modem->cdma_cm_powered = cdma_cm_powered;
+
+ DBG("%s CDMA cm Powered %d", modem->path, modem->cdma_cm_powered);
+
+ if (!modem->context_list)
+ return;
+
+ /* In case of CDMA, there is only one context */
+ context = modem->context_list->data;
+ if (modem->cdma_cm_powered)
+ set_connected(modem, context);
+ else
+ set_disconnected(context);
+}
+
+static void cdma_cm_update_settings(struct modem_data *modem,
+ DBusMessageIter *value)
+{
+ DBG("%s Settings", modem->path);
+
+ extract_ipv4_settings(value, modem->context_list->data);
+}
+
+static gboolean cdma_cm_changed(DBusConnection *conn,
+ DBusMessage *message, void *user_data)
+{
+ 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)
+ return TRUE;
+
+ if (modem->online && !modem->context_list)
+ cdma_netreg_get_properties(modem);
+
+ if (!dbus_message_iter_init(message, &iter))
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ if (g_str_equal(key, "Powered"))
+ cdma_cm_update_powered(modem, &value);
+ if (g_str_equal(key, "Settings"))
+ cdma_cm_update_settings(modem, &value);