ofono/src/modem.c

1070 lines
22 KiB
C
Raw Normal View History

2009-05-06 04:13:14 +00:00
/*
*
* 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>
2009-05-06 04:13:14 +00:00
#include <glib.h>
#include <gdbus.h>
#include "ofono.h"
#include "common.h"
2009-05-06 04:13:14 +00:00
static GSList *g_devinfo_drivers = NULL;
2009-08-22 00:11:32 +00:00
static GSList *g_driver_list = NULL;
2009-07-29 21:32:10 +00:00
static GSList *g_modem_list = NULL;
2009-08-20 21:58:01 +00:00
struct ofono_modem {
char *path;
GSList *atoms;
GSList *atom_watches;
int next_atom_watch_id;
GSList *interface_list;
unsigned int call_ids;
DBusMessage *pending;
guint interface_update;
2009-08-22 00:11:32 +00:00
ofono_bool_t powered;
ofono_bool_t powered_pending;
ofono_bool_t powered_persistent;
guint timeout;
const struct ofono_modem_driver *driver;
2009-08-20 21:58:01 +00:00
void *driver_data;
2009-08-22 00:11:32 +00:00
char *driver_type;
2009-05-06 04:13:14 +00:00
};
struct ofono_devinfo {
char *manufacturer;
char *model;
char *revision;
char *serial;
const struct ofono_devinfo_driver *driver;
void *driver_data;
struct ofono_atom *atom;
};
2009-08-12 19:46:59 +00:00
struct ofono_atom {
enum ofono_atom_type type;
void (*destruct)(struct ofono_atom *atom);
void (*unregister)(struct ofono_atom *atom);
void *data;
2009-08-14 20:30:54 +00:00
struct ofono_modem *modem;
2009-08-12 19:46:59 +00:00
};
2009-08-14 19:51:35 +00:00
struct ofono_atom_watch {
enum ofono_atom_type type;
int id;
ofono_atom_watch_func notify;
ofono_destroy_func destroy;
void *notify_data;
};
unsigned int __ofono_modem_alloc_callid(struct ofono_modem *modem)
2009-05-06 04:13:14 +00:00
{
unsigned int i;
2009-08-20 21:58:01 +00:00
for (i = 1; i < sizeof(modem->call_ids) * 8; i++) {
if (modem->call_ids & (0x1 << i))
2009-05-06 04:13:14 +00:00
continue;
2009-08-20 21:58:01 +00:00
modem->call_ids |= (0x1 << i);
2009-05-06 04:13:14 +00:00
return i;
}
return 0;
}
void __ofono_modem_release_callid(struct ofono_modem *modem, int id)
2009-05-06 04:13:14 +00:00
{
2009-08-20 21:58:01 +00:00
modem->call_ids &= ~(0x1 << id);
2009-05-06 04:13:14 +00:00
}
void ofono_modem_set_data(struct ofono_modem *modem, void *data)
2009-05-06 04:13:14 +00:00
{
2009-08-20 21:58:01 +00:00
if (modem == NULL)
return;
modem->driver_data = data;
2009-05-06 04:13:14 +00:00
}
void *ofono_modem_get_data(struct ofono_modem *modem)
2009-05-06 04:13:14 +00:00
{
2009-08-20 21:58:01 +00:00
if (modem == NULL)
return NULL;
2009-05-06 04:13:14 +00:00
2009-08-20 21:58:01 +00:00
return modem->driver_data;
2009-05-06 04:13:14 +00:00
}
2009-07-29 19:26:45 +00:00
const char *ofono_modem_get_path(struct ofono_modem *modem)
{
if (modem)
return modem->path;
return NULL;
}
2009-08-12 19:46:59 +00:00
struct ofono_atom *__ofono_modem_add_atom(struct ofono_modem *modem,
enum ofono_atom_type type,
void (*destruct)(struct ofono_atom *),
void *data)
{
struct ofono_atom *atom;
if (modem == NULL)
return NULL;
atom = g_new0(struct ofono_atom, 1);
atom->type = type;
atom->destruct = destruct;
atom->data = data;
2009-08-14 20:30:54 +00:00
atom->modem = modem;
2009-08-12 19:46:59 +00:00
modem->atoms = g_slist_prepend(modem->atoms, atom);
return atom;
}
void *__ofono_atom_get_data(struct ofono_atom *atom)
{
return atom->data;
}
2009-08-14 20:30:54 +00:00
const char *__ofono_atom_get_path(struct ofono_atom *atom)
{
return atom->modem->path;
}
struct ofono_modem *__ofono_atom_get_modem(struct ofono_atom *atom)
{
return atom->modem;
}
2009-08-14 20:13:58 +00:00
static void call_watches(struct ofono_atom *atom,
enum ofono_atom_watch_condition cond)
{
struct ofono_modem *modem = atom->modem;
GSList *l;
struct ofono_atom_watch *watch;
for (l = modem->atom_watches; l; l = l->next) {
watch = l->data;
if (watch->type != atom->type)
continue;
watch->notify(atom, cond, watch->notify_data);
}
}
2009-08-12 19:46:59 +00:00
void __ofono_atom_register(struct ofono_atom *atom,
void (*unregister)(struct ofono_atom *))
{
if (unregister == NULL)
return;
2009-08-12 19:46:59 +00:00
atom->unregister = unregister;
2009-08-14 21:21:23 +00:00
call_watches(atom, OFONO_ATOM_WATCH_CONDITION_REGISTERED);
2009-08-12 19:46:59 +00:00
}
void __ofono_atom_unregister(struct ofono_atom *atom)
{
if (atom->unregister == NULL)
return;
2009-08-14 21:21:23 +00:00
call_watches(atom, OFONO_ATOM_WATCH_CONDITION_UNREGISTERED);
atom->unregister(atom);
2009-08-12 19:46:59 +00:00
}
2009-08-14 23:47:12 +00:00
gboolean __ofono_atom_get_registered(struct ofono_atom *atom)
{
return atom->unregister ? TRUE : FALSE;
}
2009-08-14 19:51:35 +00:00
int __ofono_modem_add_atom_watch(struct ofono_modem *modem,
enum ofono_atom_type type,
ofono_atom_watch_func notify,
void *data, ofono_destroy_func destroy)
2009-08-14 19:51:35 +00:00
{
struct ofono_atom_watch *watch;
2009-08-18 14:43:30 +00:00
2009-08-14 19:51:35 +00:00
if (notify == NULL)
return 0;
2009-08-18 14:43:30 +00:00
2009-08-14 19:51:35 +00:00
watch = g_new0(struct ofono_atom_watch, 1);
watch->type = type;
watch->id = ++modem->next_atom_watch_id;
2009-08-14 19:51:35 +00:00
watch->notify = notify;
watch->destroy = destroy;
watch->notify_data = data;
modem->atom_watches = g_slist_prepend(modem->atom_watches, watch);
return watch->id;
}
gboolean __ofono_modem_remove_atom_watch(struct ofono_modem *modem, int id)
{
struct ofono_atom_watch *watch;
GSList *p;
GSList *c;
p = NULL;
c = modem->atom_watches;
while (c) {
watch = c->data;
if (watch->id != id) {
p = c;
c = c->next;
continue;
}
if (p)
p->next = c->next;
else
modem->atom_watches = c->next;
if (watch->destroy)
watch->destroy(watch->notify_data);
g_free(watch);
g_slist_free_1(c);
return TRUE;
}
return FALSE;
}
static void remove_all_watches(struct ofono_modem *modem)
{
struct ofono_atom_watch *watch;
GSList *l;
for (l = modem->atom_watches; l; l = l->next) {
watch = l->data;
if (watch->destroy)
watch->destroy(watch->notify_data);
g_free(watch);
}
g_slist_free(modem->atom_watches);
modem->atom_watches = NULL;
}
2009-08-12 19:46:59 +00:00
struct ofono_atom *__ofono_modem_find_atom(struct ofono_modem *modem,
enum ofono_atom_type type)
{
GSList *l;
struct ofono_atom *atom;
if (modem == NULL)
return NULL;
for (l = modem->atoms; l; l = l->next) {
atom = l->data;
if (atom->type == type)
return atom;
}
return NULL;
}
void __ofono_modem_foreach_atom(struct ofono_modem *modem,
enum ofono_atom_type type,
ofono_atom_func callback, void *data)
{
GSList *l;
struct ofono_atom *atom;
if (modem == NULL)
return;
for (l = modem->atoms; l; l = l->next) {
atom = l->data;
if (atom->type != type)
continue;
callback(atom, data);
}
}
void __ofono_atom_free(struct ofono_atom *atom)
2009-08-12 19:46:59 +00:00
{
struct ofono_modem *modem = atom->modem;
2009-08-12 19:46:59 +00:00
modem->atoms = g_slist_remove(modem->atoms, atom);
__ofono_atom_unregister(atom);
if (atom->destruct)
atom->destruct(atom);
g_free(atom);
}
static void remove_all_atoms(struct ofono_modem *modem)
{
GSList *l;
struct ofono_atom *atom;
if (modem == NULL)
return;
for (l = modem->atoms; l; l = l->next) {
atom = l->data;
__ofono_atom_unregister(atom);
if (atom->destruct)
atom->destruct(atom);
g_free(atom);
}
g_slist_free(modem->atoms);
modem->atoms = NULL;
}
static DBusMessage *modem_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
2009-05-06 04:13:14 +00:00
{
struct ofono_modem *modem = data;
2009-05-06 04:13:14 +00:00
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
char **interfaces;
int i;
GSList *l;
struct ofono_atom *devinfo_atom;
2009-05-06 04:13:14 +00:00
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-05-06 04:13:14 +00:00
2009-08-22 00:11:32 +00:00
ofono_dbus_dict_append(&dict, "Powered", DBUS_TYPE_BOOLEAN,
&modem->powered);
devinfo_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_DEVINFO);
2009-05-06 04:13:14 +00:00
/* We cheat a little here and don't check the registered status */
if (devinfo_atom) {
struct ofono_devinfo *info;
2009-05-06 04:13:14 +00:00
info = __ofono_atom_get_data(devinfo_atom);
2009-05-06 04:13:14 +00:00
if (info->manufacturer)
ofono_dbus_dict_append(&dict, "Manufacturer",
DBUS_TYPE_STRING,
&info->manufacturer);
2009-05-06 04:13:14 +00:00
if (info->model)
ofono_dbus_dict_append(&dict, "Model", DBUS_TYPE_STRING,
&info->model);
if (info->revision)
ofono_dbus_dict_append(&dict, "Revision",
DBUS_TYPE_STRING,
&info->revision);
if (info->serial)
ofono_dbus_dict_append(&dict, "Serial",
DBUS_TYPE_STRING,
&info->serial);
}
2009-08-20 21:58:01 +00:00
interfaces = g_new0(char *, g_slist_length(modem->interface_list) + 1);
for (i = 0, l = modem->interface_list; l; l = l->next, i++)
2009-05-06 04:13:14 +00:00
interfaces[i] = l->data;
2009-07-29 15:29:52 +00:00
ofono_dbus_dict_append_array(&dict, "Interfaces", DBUS_TYPE_STRING,
2009-05-06 04:13:14 +00:00
&interfaces);
g_free(interfaces);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
2009-08-22 00:11:32 +00:00
static int set_powered(struct ofono_modem *modem, ofono_bool_t powered)
{
const struct ofono_modem_driver *driver = modem->driver;
int err = -EINVAL;
if (driver == NULL)
return -EINVAL;
if (modem->powered_pending == powered)
return -EALREADY;
modem->powered_pending = powered;
if (powered == TRUE) {
if (driver->enable)
err = driver->enable(modem);
} else {
if (driver->disable)
err = driver->disable(modem);
}
return err;
}
static gboolean set_powered_timeout(gpointer user)
{
struct ofono_modem *modem = user;
DBG("modem: %p", modem);
modem->timeout = 0;
if (modem->pending != NULL) {
DBusMessage *reply;
reply = __ofono_error_timed_out(modem->pending);
__ofono_dbus_pending_reply(&modem->pending, reply);
}
return FALSE;
}
static DBusMessage *modem_set_property(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
DBusMessageIter iter, var;
const char *name;
if (dbus_message_iter_init(msg, &iter) == FALSE)
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, &name);
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(name, "Powered") == TRUE) {
ofono_bool_t powered;
int err;
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &powered);
if (modem->pending != NULL)
return __ofono_error_busy(msg);
if (modem->powered == powered)
return dbus_message_new_method_return(msg);
err = set_powered(modem, powered);
if (err < 0) {
if (err != -EINPROGRESS)
return __ofono_error_failed(msg);
modem->pending = dbus_message_ref(msg);
modem->timeout = g_timeout_add_seconds(20,
set_powered_timeout, modem);
return NULL;
}
modem->powered = powered;
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
ofono_dbus_signal_property_changed(conn, modem->path,
OFONO_MODEM_INTERFACE,
"Powered", DBUS_TYPE_BOOLEAN,
&powered);
if (powered) {
if (modem->driver->populate)
modem->driver->populate(modem);
2009-08-26 22:08:40 +00:00
__ofono_history_probe_drivers(modem);
2009-08-22 00:11:32 +00:00
} else {
remove_all_atoms(modem);
}
return NULL;
}
return __ofono_error_invalid_args(msg);
}
2009-05-06 04:13:14 +00:00
static GDBusMethodTable modem_methods[] = {
{ "GetProperties", "", "a{sv}", modem_get_properties },
2009-08-22 00:11:32 +00:00
{ "SetProperty", "sv", "", modem_set_property,
G_DBUS_METHOD_FLAG_ASYNC },
2009-05-06 04:13:14 +00:00
{ }
};
static GDBusSignalTable modem_signals[] = {
{ "PropertyChanged", "sv" },
{ }
};
2009-08-22 00:11:32 +00:00
void ofono_modem_set_powered(struct ofono_modem *modem, ofono_bool_t powered)
{
DBusConnection *conn = ofono_dbus_get_connection();
dbus_bool_t dbus_powered;
if (modem->timeout > 0) {
g_source_remove(modem->timeout);
modem->timeout = 0;
}
if (modem->pending != NULL) {
DBusMessage *reply;
if (powered == modem->powered_pending)
reply = dbus_message_new_method_return(modem->pending);
else
reply = __ofono_error_failed(modem->pending);
__ofono_dbus_pending_reply(&modem->pending, reply);
}
if (modem->powered == powered)
return;
modem->powered = powered;
modem->powered_pending = powered;
if (modem->driver == NULL)
return;
dbus_powered = powered;
ofono_dbus_signal_property_changed(conn, modem->path,
OFONO_MODEM_INTERFACE,
"Powered", DBUS_TYPE_BOOLEAN,
&dbus_powered);
if (powered) {
if (modem->driver->populate)
modem->driver->populate(modem);
2009-08-26 22:08:40 +00:00
__ofono_history_probe_drivers(modem);
2009-08-22 00:11:32 +00:00
} else {
remove_all_atoms(modem);
}
}
2009-05-06 04:13:14 +00:00
static gboolean trigger_interface_update(void *data)
{
struct ofono_modem *modem = data;
DBusConnection *conn = ofono_dbus_get_connection();
2009-05-06 04:13:14 +00:00
char **interfaces;
GSList *l;
int i;
2009-08-20 21:58:01 +00:00
interfaces = g_new0(char *, g_slist_length(modem->interface_list) + 1);
for (i = 0, l = modem->interface_list; l; l = l->next, i++)
2009-05-06 04:13:14 +00:00
interfaces[i] = l->data;
ofono_dbus_signal_array_property_changed(conn, modem->path,
2009-07-29 21:09:12 +00:00
OFONO_MODEM_INTERFACE,
2009-05-06 04:13:14 +00:00
"Interfaces", DBUS_TYPE_STRING,
&interfaces);
g_free(interfaces);
2009-08-20 21:58:01 +00:00
modem->interface_update = 0;
2009-05-06 04:13:14 +00:00
return FALSE;
}
void ofono_modem_add_interface(struct ofono_modem *modem,
const char *interface)
2009-05-06 04:13:14 +00:00
{
2009-08-20 21:58:01 +00:00
modem->interface_list =
g_slist_prepend(modem->interface_list, g_strdup(interface));
2009-05-06 04:13:14 +00:00
2009-08-20 21:58:01 +00:00
if (modem->interface_update != 0)
return;
2009-05-06 04:13:14 +00:00
2009-08-20 21:58:01 +00:00
modem->interface_update = g_idle_add(trigger_interface_update, modem);
2009-05-06 04:13:14 +00:00
}
void ofono_modem_remove_interface(struct ofono_modem *modem,
const char *interface)
2009-05-06 04:13:14 +00:00
{
2009-08-20 21:58:01 +00:00
GSList *found = g_slist_find_custom(modem->interface_list, interface,
2009-05-06 04:13:14 +00:00
(GCompareFunc) strcmp);
if (!found) {
ofono_error("Interface %s not found on the interface_list",
interface);
return;
}
g_free(found->data);
2009-08-20 21:58:01 +00:00
modem->interface_list = g_slist_remove(modem->interface_list,
found->data);
2009-05-06 04:13:14 +00:00
2009-08-20 21:58:01 +00:00
if (modem->interface_update != 0)
return;
modem->interface_update = g_idle_add(trigger_interface_update, modem);
2009-05-06 04:13:14 +00:00
}
static void query_serial_cb(const struct ofono_error *error,
const char *serial, void *user)
2009-05-06 04:13:14 +00:00
{
struct ofono_devinfo *info = user;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(info->atom);
2009-05-06 04:13:14 +00:00
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2009-05-06 04:13:14 +00:00
return;
info->serial = g_strdup(serial);
2009-05-06 04:13:14 +00:00
ofono_dbus_signal_property_changed(conn, path,
OFONO_MODEM_INTERFACE,
"Serial", DBUS_TYPE_STRING,
&info->serial);
2009-05-06 04:13:14 +00:00
}
static void query_serial(struct ofono_devinfo *info)
2009-05-06 04:13:14 +00:00
{
if (!info->driver->query_serial)
2009-08-07 22:45:48 +00:00
return;
2009-05-06 04:13:14 +00:00
info->driver->query_serial(info, query_serial_cb, info);
2009-05-06 04:13:14 +00:00
}
static void query_revision_cb(const struct ofono_error *error,
const char *revision, void *user)
{
struct ofono_devinfo *info = user;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(info->atom);
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
goto out;
2009-05-06 04:13:14 +00:00
info->revision = g_strdup(revision);
2009-05-06 04:13:14 +00:00
ofono_dbus_signal_property_changed(conn, path,
OFONO_MODEM_INTERFACE,
"Revision", DBUS_TYPE_STRING,
&info->revision);
out:
query_serial(info);
2009-05-06 04:13:14 +00:00
}
static void query_revision(struct ofono_devinfo *info)
2009-05-06 04:13:14 +00:00
{
if (!info->driver->query_revision) {
query_serial(info);
2009-08-07 22:45:48 +00:00
return;
2009-05-06 04:13:14 +00:00
}
info->driver->query_revision(info, query_revision_cb, info);
2009-05-06 04:13:14 +00:00
}
static void query_model_cb(const struct ofono_error *error,
const char *model, void *user)
{
struct ofono_devinfo *info = user;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(info->atom);
2009-05-06 04:13:14 +00:00
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
goto out;
2009-05-06 04:13:14 +00:00
info->model = g_strdup(model);
ofono_dbus_signal_property_changed(conn, path,
OFONO_MODEM_INTERFACE,
"Model", DBUS_TYPE_STRING,
&info->model);
out:
query_revision(info);
2009-05-06 04:13:14 +00:00
}
static void query_model(struct ofono_devinfo *info)
2009-05-06 04:13:14 +00:00
{
if (!info->driver->query_model) {
2009-05-06 04:13:14 +00:00
/* If model is not supported, don't bother querying revision */
query_serial(info);
2009-05-06 04:13:14 +00:00
}
info->driver->query_model(info, query_model_cb, info);
2009-05-06 04:13:14 +00:00
}
static void query_manufacturer_cb(const struct ofono_error *error,
const char *manufacturer, void *user)
{
struct ofono_devinfo *info = user;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(info->atom);
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
goto out;
2009-05-06 04:13:14 +00:00
info->manufacturer = g_strdup(manufacturer);
2009-05-06 04:13:14 +00:00
ofono_dbus_signal_property_changed(conn, path,
OFONO_MODEM_INTERFACE,
"Serial", DBUS_TYPE_STRING,
&info->manufacturer);
out:
query_model(info);
2009-05-06 04:13:14 +00:00
}
static gboolean query_manufacturer(gpointer user)
{
struct ofono_devinfo *info = user;
2009-05-06 04:13:14 +00:00
if (!info->driver->query_manufacturer) {
query_model(info);
2009-05-06 04:13:14 +00:00
return FALSE;
}
info->driver->query_manufacturer(info, query_manufacturer_cb, info);
2009-05-06 04:13:14 +00:00
return FALSE;
}
int ofono_devinfo_driver_register(const struct ofono_devinfo_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
if (d->probe == NULL)
return -EINVAL;
g_devinfo_drivers = g_slist_prepend(g_devinfo_drivers, (void *)d);
return 0;
}
void ofono_devinfo_driver_unregister(const struct ofono_devinfo_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
g_devinfo_drivers = g_slist_remove(g_devinfo_drivers, (void *)d);
}
static void devinfo_remove(struct ofono_atom *atom)
{
struct ofono_devinfo *info = __ofono_atom_get_data(atom);
DBG("atom: %p", atom);
if (info == NULL)
return;
if (info->driver == NULL)
return;
if (info->driver->remove)
info->driver->remove(info);
g_free(info->manufacturer);
g_free(info->model);
g_free(info->revision);
g_free(info->serial);
g_free(info);
}
struct ofono_devinfo *ofono_devinfo_create(struct ofono_modem *modem,
const char *driver,
void *data)
{
struct ofono_devinfo *info;
GSList *l;
info = g_new0(struct ofono_devinfo, 1);
info->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_DEVINFO,
devinfo_remove, info);
info->driver_data = data;
for (l = g_devinfo_drivers; l; l = l->next) {
const struct ofono_devinfo_driver *drv = l->data;
if (g_strcmp0(drv->name, driver))
continue;
if (drv->probe(info) < 0)
continue;
info->driver = drv;
break;
}
return info;
}
void ofono_devinfo_register(struct ofono_devinfo *info)
{
query_manufacturer(info);
}
void ofono_devinfo_remove(struct ofono_devinfo *info)
{
__ofono_atom_free(info->atom);
}
void ofono_devinfo_set_data(struct ofono_devinfo *info, void *data)
{
info->driver_data = data;
}
void *ofono_devinfo_get_data(struct ofono_devinfo *info)
{
return info->driver_data;
}
2009-08-22 00:11:32 +00:00
/* Clients only need to free *modems */
const char **__ofono_modem_get_list()
2009-05-06 04:13:14 +00:00
{
2009-08-22 00:11:32 +00:00
GSList *l;
int i;
2009-05-06 04:13:14 +00:00
struct ofono_modem *modem;
2009-08-22 00:11:32 +00:00
const char **modems;
modems = g_new0(const char *, g_slist_length(g_modem_list) + 1);
for (l = g_modem_list, i = 0; l; l = l->next, i++) {
modem = l->data;
if (modem->driver == NULL)
continue;
modems[i] = modem->path;
}
return modems;
}
struct ofono_modem *ofono_modem_create(const char *node, const char *type)
{
struct ofono_modem *modem;
char path[128];
2009-08-30 18:28:52 +00:00
DBG("%s, %s", node, type);
2009-08-22 00:11:32 +00:00
if (strlen(node) > 16)
return NULL;
2009-05-06 04:13:14 +00:00
modem = g_try_new0(struct ofono_modem, 1);
2009-08-22 00:11:32 +00:00
2009-05-06 04:13:14 +00:00
if (modem == NULL)
return modem;
2009-08-22 00:11:32 +00:00
snprintf(path, sizeof(path), "/%s", node);
2009-05-06 04:13:14 +00:00
modem->path = g_strdup(path);
2009-08-22 00:11:32 +00:00
modem->driver_type = g_strdup(type);
2009-05-06 04:13:14 +00:00
2009-08-22 00:11:32 +00:00
g_modem_list = g_slist_prepend(g_modem_list, modem);
2009-05-06 04:13:14 +00:00
return modem;
}
2009-08-22 00:11:32 +00:00
static void emit_modems()
2009-05-06 04:13:14 +00:00
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-08-22 00:11:32 +00:00
const char **modems = __ofono_modem_get_list();
2009-05-06 04:13:14 +00:00
2009-08-22 00:11:32 +00:00
if (modems == NULL)
return;
2009-08-12 19:46:59 +00:00
2009-08-22 00:11:32 +00:00
ofono_dbus_signal_array_property_changed(conn,
OFONO_MANAGER_PATH,
OFONO_MANAGER_INTERFACE, "Modems",
DBUS_TYPE_OBJECT_PATH, &modems);
2009-05-06 04:13:14 +00:00
2009-08-22 00:11:32 +00:00
g_free(modems);
2009-05-06 04:13:14 +00:00
}
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
int ofono_modem_register(struct ofono_modem *modem)
2009-07-29 21:32:10 +00:00
{
2009-08-22 00:11:32 +00:00
DBusConnection *conn = ofono_dbus_get_connection();
2009-07-29 21:32:10 +00:00
GSList *l;
2009-08-22 00:11:32 +00:00
if (modem == NULL)
return -EINVAL;
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
if (modem->driver != NULL)
return -EALREADY;
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
for (l = g_driver_list; l; l = l->next) {
const struct ofono_modem_driver *drv = l->data;
if (g_strcmp0(drv->name, modem->driver_type))
continue;
if (drv->probe(modem) < 0)
continue;
modem->driver = drv;
break;
2009-07-29 21:32:10 +00:00
}
2009-08-22 00:11:32 +00:00
if (modem->driver == NULL)
return -ENODEV;
if (!g_dbus_register_interface(conn, modem->path, OFONO_MODEM_INTERFACE,
modem_methods, modem_signals, NULL,
modem, NULL)) {
ofono_error("Modem register failed on path %s", modem->path);
if (modem->driver->remove)
modem->driver->remove(modem);
modem->driver = NULL;
return -EIO;
}
g_free(modem->driver_type);
modem->driver_type = NULL;
emit_modems();
/* TODO: Read powered property from store */
if (modem->powered_persistent)
set_powered(modem, TRUE);
2009-08-26 22:08:40 +00:00
if (modem->powered == TRUE && modem->driver->populate) {
2009-08-22 00:11:32 +00:00
modem->driver->populate(modem);
2009-08-26 22:08:40 +00:00
__ofono_history_probe_drivers(modem);
}
2009-08-22 00:11:32 +00:00
return 0;
2009-07-29 21:32:10 +00:00
}
2009-08-22 00:11:32 +00:00
static void modem_unregister(struct ofono_modem *modem)
2009-07-29 21:32:10 +00:00
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-08-22 00:11:32 +00:00
if (modem->driver == NULL)
return;
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
remove_all_atoms(modem);
remove_all_watches(modem);
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
g_slist_foreach(modem->interface_list, (GFunc)g_free, NULL);
g_slist_free(modem->interface_list);
modem->interface_list = NULL;
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
if (modem->timeout) {
g_source_remove(modem->timeout);
modem->timeout = 0;
}
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
if (modem->pending) {
dbus_message_unref(modem->pending);
modem->pending = NULL;
}
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
if (modem->interface_update) {
g_source_remove(modem->interface_update);
modem->interface_update = 0;
2009-07-29 21:32:10 +00:00
}
2009-08-22 00:11:32 +00:00
g_dbus_unregister_interface(conn, modem->path, OFONO_MODEM_INTERFACE);
emit_modems();
if (modem->powered == TRUE)
set_powered(modem, FALSE);
if (modem->driver->remove)
modem->driver->remove(modem);
modem->driver = NULL;
2009-07-29 21:32:10 +00:00
}
2009-08-22 00:11:32 +00:00
void ofono_modem_remove(struct ofono_modem *modem)
2009-07-29 21:32:10 +00:00
{
2009-08-22 00:11:32 +00:00
DBG("%p", modem);
2009-07-29 21:32:10 +00:00
if (modem == NULL)
2009-08-22 00:11:32 +00:00
return;
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
if (modem->driver)
modem_unregister(modem);
2009-07-29 21:32:10 +00:00
g_modem_list = g_slist_remove(g_modem_list, modem);
2009-08-22 00:11:32 +00:00
if (modem->driver_type)
g_free(modem->driver_type);
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
g_free(modem->path);
g_free(modem);
}
2009-07-29 21:32:10 +00:00
2009-08-22 00:11:32 +00:00
int ofono_modem_driver_register(const struct ofono_modem_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
if (d->probe == NULL)
return -EINVAL;
g_driver_list = g_slist_prepend(g_driver_list, (void *)d);
2009-07-29 21:32:10 +00:00
return 0;
}
2009-08-22 00:11:32 +00:00
void ofono_modem_driver_unregister(const struct ofono_modem_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
g_driver_list = g_slist_remove(g_driver_list, (void *)d);
}