ofono/src/gprs.c

1018 lines
25 KiB
C
Raw Normal View History

/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 Intel Corporation. All rights reserved.
*
* 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
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <glib.h>
#include <gdbus.h>
#include "ofono.h"
#include "common.h"
#define DATA_CONNECTION_MANAGER_INTERFACE "org.ofono.DataConnectionManager"
#define DATA_CONTEXT_INTERFACE "org.ofono.PrimaryDataContext"
#define DATA_CONNECTION_FLAG_ATTACHING 0x1
static GSList *g_drivers = NULL;
2009-10-22 22:20:50 +00:00
enum gprs_context_type {
GPRS_CONTEXT_TYPE_INTERNET = 1,
GPRS_CONTEXT_TYPE_MMS,
GPRS_CONTEXT_TYPE_WAP,
};
2009-10-20 18:09:54 +00:00
struct ofono_gprs {
GSList *contexts;
ofono_bool_t attached;
ofono_bool_t roaming_allowed;
ofono_bool_t powered;
int status;
int location;
int cellid;
int technology;
int flags;
int next_context_id;
2009-10-22 22:39:57 +00:00
int cid_min;
int cid_max;
DBusMessage *pending;
2009-10-20 18:09:54 +00:00
const struct ofono_gprs_driver *driver;
void *driver_data;
struct ofono_atom *atom;
};
struct pri_context {
ofono_bool_t active;
enum gprs_context_type type;
char *name;
char *path;
struct ofono_gprs_primary_context context;
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs;
};
2009-10-20 18:09:54 +00:00
static void gprs_netreg_update(struct ofono_gprs *gprs);
2009-10-22 22:20:50 +00:00
static inline const char *gprs_context_type_to_string(int type)
{
switch (type) {
2009-10-22 22:20:50 +00:00
case GPRS_CONTEXT_TYPE_INTERNET:
return "internet";
2009-10-22 22:20:50 +00:00
case GPRS_CONTEXT_TYPE_MMS:
return "mms";
2009-10-22 22:20:50 +00:00
case GPRS_CONTEXT_TYPE_WAP:
return "wap";
}
return NULL;
}
2009-10-22 22:42:01 +00:00
static struct pri_context *gprs_context_by_path(struct ofono_gprs *gprs,
2009-10-20 18:09:54 +00:00
const char *ctx_path)
{
GSList *l;
2009-10-20 18:09:54 +00:00
for (l = gprs->contexts; l; l = l->next) {
2009-10-22 22:42:01 +00:00
struct pri_context *ctx = l->data;
2009-10-22 22:42:01 +00:00
if (g_str_equal(ctx_path, ctx->path))
return ctx;
}
return NULL;
}
2009-10-20 18:09:54 +00:00
static DBusMessage *pri_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct pri_context *ctx = data;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
dbus_bool_t value;
2009-10-22 22:20:50 +00:00
const char *type = gprs_context_type_to_string(ctx->type);
const char *name = ctx->name ? ctx->name : "";
const char *strvalue;
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
2009-10-22 22:42:40 +00:00
ofono_dbus_dict_append(&dict, "Name", DBUS_TYPE_STRING, &name);
value = ctx->active;
ofono_dbus_dict_append(&dict, "Active", DBUS_TYPE_BOOLEAN, &value);
2009-10-22 22:42:40 +00:00
ofono_dbus_dict_append(&dict, "Type", DBUS_TYPE_STRING, &type);
2009-10-22 22:42:40 +00:00
strvalue = ctx->context.apn;
ofono_dbus_dict_append(&dict, "AccessPointName", DBUS_TYPE_STRING,
&strvalue);
2009-10-22 22:42:40 +00:00
strvalue = ctx->context.username;
ofono_dbus_dict_append(&dict, "Username", DBUS_TYPE_STRING,
&strvalue);
2009-10-22 22:42:40 +00:00
strvalue = ctx->context.password;
ofono_dbus_dict_append(&dict, "Passwod", DBUS_TYPE_STRING,
&strvalue);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static DBusMessage *pri_set_apn(struct pri_context *ctx, DBusConnection *conn,
DBusMessage *msg, const char *apn)
{
if (strlen(apn) > OFONO_GPRS_MAX_APN_LENGTH)
return __ofono_error_invalid_format(msg);
if (g_str_equal(apn, ctx->context.apn))
return dbus_message_new_method_return(msg);
strcpy(ctx->context.apn, apn);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, ctx->path,
DATA_CONTEXT_INTERFACE,
"AccessPointName",
DBUS_TYPE_STRING, &apn);
return NULL;
}
static DBusMessage *pri_set_username(struct pri_context *ctx,
DBusConnection *conn, DBusMessage *msg,
const char *username)
{
if (strlen(username) > OFONO_GPRS_MAX_USERNAME_LENGTH)
return __ofono_error_invalid_format(msg);
if (g_str_equal(username, ctx->context.username))
return dbus_message_new_method_return(msg);
strcpy(ctx->context.username, username);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, ctx->path,
DATA_CONTEXT_INTERFACE,
"Username",
DBUS_TYPE_STRING, &username);
return NULL;
}
static DBusMessage *pri_set_password(struct pri_context *ctx,
DBusConnection *conn, DBusMessage *msg,
const char *password)
{
if (strlen(password) > OFONO_GPRS_MAX_PASSWORD_LENGTH)
return __ofono_error_invalid_format(msg);
if (g_str_equal(password, ctx->context.password))
return dbus_message_new_method_return(msg);
strcpy(ctx->context.password, password);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, ctx->path,
DATA_CONTEXT_INTERFACE,
"Password",
DBUS_TYPE_STRING, &password);
return NULL;
}
static DBusMessage *pri_set_type(struct pri_context *ctx, DBusConnection *conn,
DBusMessage *msg, const char *type)
{
enum gprs_context_type context_type;
if (g_str_equal(type, "internet"))
context_type = GPRS_CONTEXT_TYPE_INTERNET;
else if (g_str_equal(type, "wap"))
context_type = GPRS_CONTEXT_TYPE_WAP;
else if (g_str_equal(type, "mms"))
context_type = GPRS_CONTEXT_TYPE_MMS;
else
return __ofono_error_invalid_args(msg);
if (ctx->type == context_type)
return dbus_message_new_method_return(msg);
ctx->type = context_type;
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, ctx->path,
DATA_CONTEXT_INTERFACE, "Type",
DBUS_TYPE_STRING, &type);
return NULL;
}
static DBusMessage *pri_set_name(struct pri_context *ctx, DBusConnection *conn,
DBusMessage *msg, const char *name)
{
int context_type;
if (ctx->name && g_str_equal(ctx->name, name))
return dbus_message_new_method_return(msg);
if (ctx->name)
g_free(ctx->name);
ctx->name = g_strdup(name);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, ctx->path,
DATA_CONTEXT_INTERFACE, "Name",
DBUS_TYPE_STRING, &name);
return NULL;
}
2009-10-20 18:09:54 +00:00
static DBusMessage *pri_set_property(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct pri_context *ctx = data;
DBusMessageIter iter;
DBusMessageIter var;
const char *property;
dbus_bool_t value;
const char *str;
const char *path;
if (!dbus_message_iter_init(msg, &iter))
return __ofono_error_invalid_args(msg);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&iter, &property);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
return __ofono_error_invalid_args(msg);
dbus_message_iter_recurse(&iter, &var);
if (g_str_equal(property, "Active")) {
if (ctx->gprs->pending)
return __ofono_error_busy(msg);
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
if (ctx->active == (ofono_bool_t) value)
return dbus_message_new_method_return(msg);
2009-10-20 18:09:54 +00:00
if (ctx->gprs->flags & DATA_CONNECTION_FLAG_ATTACHING)
return __ofono_error_busy(msg);
2009-10-20 18:09:54 +00:00
if (value && !ctx->gprs->attached)
return __ofono_error_failed(msg);
return __ofono_error_not_implemented(msg);
}
/* All other properties are read-only when context is active */
if (ctx->active == TRUE)
return __ofono_error_in_use(msg);
if (!strcmp(property, "AccessPointName")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &str);
return pri_set_apn(ctx, conn, msg, str);
} else if (!strcmp(property, "Type")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &str);
return pri_set_type(ctx, conn, msg, str);
} else if (!strcmp(property, "Username")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &str);
return pri_set_username(ctx, conn, msg, str);
} else if (!strcmp(property, "Password")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &str);
return pri_set_password(ctx, conn, msg, str);
} else if (!strcmp(property, "Name")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &str);
return pri_set_name(ctx, conn, msg, str);
}
return __ofono_error_invalid_args(msg);
}
static GDBusMethodTable context_methods[] = {
2009-10-20 18:09:54 +00:00
{ "GetProperties", "", "a{sv}", pri_get_properties },
{ "SetProperty", "sv", "", pri_set_property,
G_DBUS_METHOD_FLAG_ASYNC },
{ }
};
static GDBusSignalTable context_signals[] = {
{ "PropertyChanged", "sv" },
{ }
};
static struct pri_context *pri_context_create(struct ofono_gprs *gprs)
{
struct pri_context *context = g_try_new0(struct pri_context, 1);
if (!context)
return NULL;
2009-10-20 18:09:54 +00:00
context->gprs = gprs;
return context;
}
static void pri_context_destroy(gpointer userdata)
{
struct pri_context *ctx = userdata;
if (ctx->name)
g_free(ctx->name);
if (ctx->path)
g_free(ctx->path);
g_free(ctx);
}
static gboolean context_dbus_register(struct pri_context *ctx)
{
DBusConnection *conn = ofono_dbus_get_connection();
char path[256];
unsigned int id = ctx->gprs->next_context_id;
snprintf(path, sizeof(path), "%s/primarycontext%u",
__ofono_atom_get_path(ctx->gprs->atom), id);
if (!g_dbus_register_interface(conn, path, DATA_CONTEXT_INTERFACE,
context_methods, context_signals,
NULL, ctx, pri_context_destroy)) {
ofono_error("Could not register PrimaryContext %s", path);
pri_context_destroy(ctx);
return FALSE;
}
ctx->path = g_strdup(path);
ctx->gprs->next_context_id += 1;
return TRUE;
}
static gboolean context_dbus_unregister(struct pri_context *ctx)
{
DBusConnection *conn = ofono_dbus_get_connection();
char path[256];
strcpy(path, ctx->path);
return g_dbus_unregister_interface(conn, path,
DATA_CONTEXT_INTERFACE);
}
2009-10-22 22:46:10 +00:00
static char **gprs_contexts_path_list(GSList *context_list)
{
GSList *l;
char **i;
struct pri_context *ctx;
char **objlist = g_new0(char *, g_slist_length(context_list) + 1);
if (!objlist)
return NULL;
for (i = objlist, l = context_list; l; l = l->next) {
ctx = l->data;
2009-10-22 22:46:10 +00:00
*i++ = g_strdup(ctx->path);
}
return objlist;
}
2009-10-20 18:09:54 +00:00
static void gprs_generic_callback(const struct ofono_error *error, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_debug("command failed with error: %s",
telephony_error_to_str(error));
2009-10-20 18:09:54 +00:00
if (!gprs->pending)
return;
if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
2009-10-20 18:09:54 +00:00
reply = dbus_message_new_method_return(gprs->pending);
else
2009-10-20 18:09:54 +00:00
reply = __ofono_error_failed(gprs->pending);
2009-10-20 18:09:54 +00:00
__ofono_dbus_pending_reply(&gprs->pending, reply);
}
2009-10-20 18:09:54 +00:00
static void gprs_attach_callback(const struct ofono_error *error, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path;
dbus_bool_t value;
if (error->type == OFONO_ERROR_TYPE_NO_ERROR &&
2009-10-20 18:09:54 +00:00
(gprs->flags & DATA_CONNECTION_FLAG_ATTACHING)) {
gprs->attached = !gprs->attached;
2009-10-20 18:09:54 +00:00
path = __ofono_atom_get_path(gprs->atom);
value = gprs->attached;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Attached", DBUS_TYPE_BOOLEAN, &value);
}
2009-10-20 18:09:54 +00:00
gprs->flags &= ~DATA_CONNECTION_FLAG_ATTACHING;
2009-10-20 18:09:54 +00:00
gprs_netreg_update(gprs);
}
2009-10-20 18:09:54 +00:00
static void gprs_netreg_update(struct ofono_gprs *gprs)
{
DBusConnection *conn = ofono_dbus_get_connection();
int attach;
int operator_ok;
const char *path;
dbus_bool_t value = 0;
2009-10-20 18:09:54 +00:00
operator_ok = gprs->roaming_allowed ||
(gprs->status != NETWORK_REGISTRATION_STATUS_ROAMING);
2009-10-20 18:09:54 +00:00
attach = gprs->powered && operator_ok;
2009-10-20 18:09:54 +00:00
if (gprs->attached != attach &&
!(gprs->flags & DATA_CONNECTION_FLAG_ATTACHING)) {
gprs->flags |= DATA_CONNECTION_FLAG_ATTACHING;
2009-10-20 18:09:54 +00:00
gprs->driver->set_attached(gprs, attach, gprs_attach_callback,
gprs);
/* Prevent further attempts to attach */
2009-10-20 18:09:54 +00:00
if (!attach && gprs->powered) {
gprs->powered = 0;
2009-10-20 18:09:54 +00:00
path = __ofono_atom_get_path(gprs->atom);
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Powered", DBUS_TYPE_BOOLEAN, &value);
}
}
}
2009-10-20 18:09:54 +00:00
static DBusMessage *gprs_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
char **objpath_list;
dbus_bool_t value;
2009-10-20 18:09:54 +00:00
const char *status = registration_status_to_string(gprs->status);
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
2009-10-22 22:46:10 +00:00
objpath_list = gprs_contexts_path_list(gprs->contexts);
if (!objpath_list)
return NULL;
ofono_dbus_dict_append_array(&dict, "PrimaryContexts",
DBUS_TYPE_OBJECT_PATH, &objpath_list);
g_strfreev(objpath_list);
2009-10-20 18:09:54 +00:00
value = gprs->attached;
ofono_dbus_dict_append(&dict, "Attached", DBUS_TYPE_BOOLEAN, &value);
2009-10-20 18:09:54 +00:00
value = gprs->roaming_allowed;
ofono_dbus_dict_append(&dict, "RoamingAllowed",
DBUS_TYPE_BOOLEAN, &value);
2009-10-20 18:09:54 +00:00
value = gprs->powered;
ofono_dbus_dict_append(&dict, "Powered", DBUS_TYPE_BOOLEAN, &value);
ofono_dbus_dict_append(&dict, "Status", DBUS_TYPE_STRING, &status);
2009-10-20 18:09:54 +00:00
if (gprs->location != -1) {
dbus_uint16_t location = gprs->location;
ofono_dbus_dict_append(&dict, "LocationAreaCode",
DBUS_TYPE_UINT16, &location);
}
2009-10-20 18:09:54 +00:00
if (gprs->cellid != -1) {
dbus_uint32_t cellid = gprs->cellid;
ofono_dbus_dict_append(&dict, "CellId",
DBUS_TYPE_UINT32, &cellid);
}
2009-10-20 18:09:54 +00:00
if (gprs->technology != -1) {
const char *technology =
2009-10-20 18:09:54 +00:00
registration_tech_to_string(gprs->technology);
ofono_dbus_dict_append(&dict, "Technology", DBUS_TYPE_STRING,
&technology);
}
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
2009-10-20 18:09:54 +00:00
static DBusMessage *gprs_set_property(DBusConnection *conn,
DBusMessage *msg, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
DBusMessageIter iter;
DBusMessageIter var;
const char *property;
dbus_bool_t value;
const char *path;
2009-10-20 18:09:54 +00:00
if (gprs->pending)
return __ofono_error_busy(msg);
if (!dbus_message_iter_init(msg, &iter))
return __ofono_error_invalid_args(msg);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&iter, &property);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
return __ofono_error_invalid_args(msg);
dbus_message_iter_recurse(&iter, &var);
if (!strcmp(property, "RoamingAllowed")) {
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
2009-10-20 18:09:54 +00:00
gprs->roaming_allowed = value;
gprs_netreg_update(gprs);
} else if (!strcmp(property, "Powered")) {
2009-10-20 18:09:54 +00:00
if (!gprs->driver->set_attached)
return __ofono_error_not_implemented(msg);
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
2009-10-20 18:09:54 +00:00
gprs->powered = value;
gprs_netreg_update(gprs);
} else
return __ofono_error_invalid_args(msg);
2009-10-20 18:09:54 +00:00
path = __ofono_atom_get_path(gprs->atom);
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
property, DBUS_TYPE_BOOLEAN, &value);
return dbus_message_new_method_return(msg);
}
2009-10-20 18:09:54 +00:00
static DBusMessage *gprs_create_context(DBusConnection *conn,
DBusMessage *msg, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
2009-10-22 22:48:39 +00:00
struct pri_context *context;
const char *path;
char **objpath_list;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID))
return __ofono_error_invalid_args(msg);
2009-10-22 22:48:39 +00:00
context = pri_context_create(gprs);
2009-10-22 22:48:39 +00:00
if (!context) {
ofono_error("Unable to allocate context struct");
return __ofono_error_failed(msg);
}
2009-10-22 22:48:39 +00:00
ofono_debug("Registering new context");
2009-10-22 22:48:39 +00:00
if (!context_dbus_register(context)) {
ofono_error("Unable to register primary context");
return __ofono_error_failed(msg);
}
2009-10-22 22:48:39 +00:00
gprs->contexts = g_slist_append(gprs->contexts, context);
2009-10-22 22:48:39 +00:00
objpath_list = gprs_contexts_path_list(gprs->contexts);
2009-10-22 22:48:39 +00:00
if (objpath_list) {
path = __ofono_atom_get_path(gprs->atom);
ofono_dbus_signal_array_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"PrimaryContexts",
DBUS_TYPE_OBJECT_PATH, &objpath_list);
2009-10-22 22:48:39 +00:00
g_strfreev(objpath_list);
}
2009-10-22 22:48:39 +00:00
path = context->path;
return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
}
2009-10-20 18:09:54 +00:00
static DBusMessage *gprs_remove_context(DBusConnection *conn,
DBusMessage *msg, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
struct pri_context *ctx;
const char *path;
2009-10-22 22:49:22 +00:00
char **objpath_list;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID))
return __ofono_error_invalid_args(msg);
if (path[0] == '\0')
return __ofono_error_invalid_format(msg);
2009-10-20 18:09:54 +00:00
ctx = gprs_context_by_path(gprs, path);
if (!ctx)
return __ofono_error_not_found(msg);
2009-10-22 22:49:22 +00:00
if (ctx->active)
return __ofono_error_failed(msg);
2009-10-22 22:49:22 +00:00
ofono_debug("Unregistering context: %s\n", ctx->path);
2009-10-22 22:49:22 +00:00
context_dbus_unregister(ctx);
gprs->contexts = g_slist_remove(gprs->contexts, ctx);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
2009-10-22 22:49:22 +00:00
objpath_list = gprs_contexts_path_list(gprs->contexts);
if (objpath_list) {
path = __ofono_atom_get_path(gprs->atom);
ofono_dbus_signal_array_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"PrimaryContexts",
DBUS_TYPE_OBJECT_PATH, &objpath_list);
g_strfreev(objpath_list);
}
return NULL;
}
2009-10-20 18:09:54 +00:00
static DBusMessage *gprs_deactivate_all(DBusConnection *conn,
DBusMessage *msg, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = data;
2009-10-20 18:09:54 +00:00
if (gprs->pending)
return __ofono_error_busy(msg);
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID))
return __ofono_error_invalid_args(msg);
return __ofono_error_not_implemented(msg);
}
static GDBusMethodTable manager_methods[] = {
2009-10-20 18:09:54 +00:00
{ "GetProperties", "", "a{sv}", gprs_get_properties },
{ "SetProperty", "sv", "", gprs_set_property },
2009-10-22 22:48:39 +00:00
{ "CreateContext", "", "o", gprs_create_context },
2009-10-20 18:09:54 +00:00
{ "RemoveContext", "o", "", gprs_remove_context,
G_DBUS_METHOD_FLAG_ASYNC },
2009-10-20 18:09:54 +00:00
{ "DeactivateAll", "", "", gprs_deactivate_all,
G_DBUS_METHOD_FLAG_ASYNC },
{ }
};
static GDBusSignalTable manager_signals[] = {
{ "PropertyChanged", "sv" },
{ }
};
2009-10-22 22:50:20 +00:00
void ofono_gprs_attach_notify(struct ofono_gprs *gprs, int attached)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path;
dbus_bool_t value = 0;
2009-10-20 18:09:54 +00:00
if (gprs->attached && !(gprs->flags & DATA_CONNECTION_FLAG_ATTACHING)) {
gprs->attached = 0;
2009-10-20 18:09:54 +00:00
path = __ofono_atom_get_path(gprs->atom);
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Attached", DBUS_TYPE_BOOLEAN, &value);
2009-10-20 18:09:54 +00:00
gprs_netreg_update(gprs);
}
}
2009-10-22 22:50:30 +00:00
static void set_registration_status(struct ofono_gprs *gprs, int status)
{
const char *str_status = registration_status_to_string(status);
2009-10-20 18:09:54 +00:00
const char *path = __ofono_atom_get_path(gprs->atom);
DBusConnection *conn = ofono_dbus_get_connection();
dbus_bool_t attached;
2009-10-20 18:09:54 +00:00
gprs->status = status;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Status", DBUS_TYPE_STRING,
&str_status);
attached = (status != NETWORK_REGISTRATION_STATUS_REGISTERED &&
status != NETWORK_REGISTRATION_STATUS_ROAMING);
2009-10-20 18:09:54 +00:00
if (gprs->attached != (int) attached &&
!(gprs->flags & DATA_CONNECTION_FLAG_ATTACHING)) {
gprs->attached = (int) attached;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Attached", DBUS_TYPE_BOOLEAN,
&attached);
2009-10-20 18:09:54 +00:00
gprs_netreg_update(gprs);
}
}
2009-10-20 18:09:54 +00:00
static void set_registration_location(struct ofono_gprs *gprs,
int lac)
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-10-20 18:09:54 +00:00
const char *path = __ofono_atom_get_path(gprs->atom);
dbus_uint16_t dbus_lac = lac;
if (lac > 0xffff)
return;
2009-10-20 18:09:54 +00:00
gprs->location = lac;
2009-10-20 18:09:54 +00:00
if (gprs->location == -1)
return;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"LocationAreaCode",
DBUS_TYPE_UINT16, &dbus_lac);
}
2009-10-20 18:09:54 +00:00
static void set_registration_cellid(struct ofono_gprs *gprs, int ci)
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-10-20 18:09:54 +00:00
const char *path = __ofono_atom_get_path(gprs->atom);
dbus_uint32_t dbus_ci = ci;
2009-10-20 18:09:54 +00:00
gprs->cellid = ci;
2009-10-20 18:09:54 +00:00
if (gprs->cellid == -1)
return;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"CellId", DBUS_TYPE_UINT32,
&dbus_ci);
}
2009-10-20 18:09:54 +00:00
static void set_registration_technology(struct ofono_gprs *gprs,
int tech)
{
const char *tech_str = registration_tech_to_string(tech);
DBusConnection *conn = ofono_dbus_get_connection();
2009-10-20 18:09:54 +00:00
const char *path = __ofono_atom_get_path(gprs->atom);
2009-10-20 18:09:54 +00:00
gprs->technology = tech;
2009-10-20 18:09:54 +00:00
if (gprs->technology == -1)
return;
ofono_dbus_signal_property_changed(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
"Technology", DBUS_TYPE_STRING,
&tech_str);
}
2009-10-20 18:09:54 +00:00
void ofono_gprs_status_notify(struct ofono_gprs *gprs,
int status, int lac, int ci, int tech)
{
2009-10-20 18:09:54 +00:00
if (gprs->status != status)
set_registration_status(gprs, status);
2009-10-20 18:09:54 +00:00
if (gprs->location != lac)
set_registration_location(gprs, lac);
2009-10-20 18:09:54 +00:00
if (gprs->cellid != ci)
set_registration_cellid(gprs, ci);
2009-10-20 18:09:54 +00:00
if (gprs->technology != tech)
set_registration_technology(gprs, tech);
}
2009-10-22 22:39:57 +00:00
void ofono_gprs_set_cid_range(struct ofono_gprs *gprs, int min, int max)
{
if (gprs == NULL)
return;
gprs->cid_min = min;
gprs->cid_max = max;
}
int ofono_gprs_driver_register(const struct ofono_gprs_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
if (d->probe == NULL)
return -EINVAL;
g_drivers = g_slist_prepend(g_drivers, (void *)d);
return 0;
}
2009-10-20 21:29:10 +00:00
void ofono_gprs_driver_unregister(const struct ofono_gprs_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
g_drivers = g_slist_remove(g_drivers, (void *)d);
}
2009-10-20 21:29:10 +00:00
static void gprs_unregister(struct ofono_atom *atom)
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = __ofono_atom_get_data(atom);
struct ofono_modem *modem = __ofono_atom_get_modem(atom);
const char *path = __ofono_atom_get_path(atom);
2009-10-20 18:09:54 +00:00
g_slist_free(gprs->contexts);
ofono_modem_remove_interface(modem, DATA_CONNECTION_MANAGER_INTERFACE);
g_dbus_unregister_interface(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE);
}
2009-10-20 21:29:10 +00:00
static void gprs_remove(struct ofono_atom *atom)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = __ofono_atom_get_data(atom);
DBG("atom: %p", atom);
2009-10-20 18:09:54 +00:00
if (gprs == NULL)
return;
2009-10-20 18:09:54 +00:00
if (gprs->driver && gprs->driver->remove)
gprs->driver->remove(gprs);
2009-10-20 18:09:54 +00:00
g_free(gprs);
}
2009-10-20 21:29:10 +00:00
struct ofono_gprs *ofono_gprs_create(struct ofono_modem *modem,
unsigned int vendor,
const char *driver, void *data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs;
GSList *l;
if (driver == NULL)
return NULL;
2009-10-20 18:09:54 +00:00
gprs = g_try_new0(struct ofono_gprs, 1);
2009-10-20 18:09:54 +00:00
if (gprs == NULL)
return NULL;
2009-10-20 21:29:10 +00:00
gprs->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_GPRS,
gprs_remove, gprs);
for (l = g_drivers; l; l = l->next) {
2009-10-20 18:09:54 +00:00
const struct ofono_gprs_driver *drv = l->data;
if (g_strcmp0(drv->name, driver))
continue;
2009-10-20 18:09:54 +00:00
if (drv->probe(gprs, vendor, data) < 0)
continue;
2009-10-20 18:09:54 +00:00
gprs->driver = drv;
break;
}
2009-10-20 18:09:54 +00:00
gprs->technology = -1;
gprs->cellid = -1;
gprs->location = -1;
gprs->next_context_id = 1;
2009-10-20 18:09:54 +00:00
return gprs;
}
2009-10-20 18:09:54 +00:00
void ofono_gprs_register(struct ofono_gprs *gprs)
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-10-20 18:09:54 +00:00
struct ofono_modem *modem = __ofono_atom_get_modem(gprs->atom);
const char *path = __ofono_atom_get_path(gprs->atom);
if (!g_dbus_register_interface(conn, path,
DATA_CONNECTION_MANAGER_INTERFACE,
manager_methods, manager_signals, NULL,
2009-10-20 18:09:54 +00:00
gprs, NULL)) {
ofono_error("Could not create %s interface",
DATA_CONNECTION_MANAGER_INTERFACE);
return;
}
ofono_modem_add_interface(modem, DATA_CONNECTION_MANAGER_INTERFACE);
2009-10-20 21:29:10 +00:00
__ofono_atom_register(gprs->atom, gprs_unregister);
}
2009-10-20 18:09:54 +00:00
void ofono_gprs_remove(struct ofono_gprs *gprs)
{
2009-10-20 18:09:54 +00:00
__ofono_atom_free(gprs->atom);
}
2009-10-22 22:50:30 +00:00
void ofono_gprs_set_data(struct ofono_gprs *gprs, void *data)
{
2009-10-20 18:09:54 +00:00
gprs->driver_data = data;
}
2009-10-20 18:09:54 +00:00
void *ofono_gprs_get_data(struct ofono_gprs *gprs)
{
2009-10-20 18:09:54 +00:00
return gprs->driver_data;
}