Desktop

/* $Id: modem.c,v 1.54.2.2 2011/01/20 01:37:58 khorben Exp $ */
/* Copyright (c) 2011 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Phone */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>. */
#include <System.h>
#include "Phone/phone.h"
#include "modem.h"
#include "../config.h"
#ifndef PREFIX
# define PREFIX "/usr/local"
#endif
#ifndef LIBDIR
# define LIBDIR PREFIX "/lib"
#endif
/* Modem */
/* private */
/* types */
struct _Modem
{
Plugin * plugin;
ModemPluginHelper helper;
ModemPlugin * modem;
ModemEventCallback callback;
int active;
void * priv;
};
/* prototypes */
static void _modem_event_callback(Modem * modem, ModemEvent * event);
static char const * _modem_config_get(Modem * modem, char const * section,
char const * variable);
static int _modem_error(Modem * modem, char const * message, int ret);
/* public */
/* functions */
/* modem_new */
Modem * modem_new(char const * plugin, ModemEventCallback callback, void * priv)
{
Modem * modem;
if((modem = object_new(sizeof(*modem))) == NULL)
return NULL;
modem->modem = NULL;
modem->callback = callback;
modem->active = 0;
modem->priv = priv;
if((modem->plugin = plugin_new(LIBDIR, PACKAGE, "modem", plugin))
!= NULL)
modem->modem = plugin_lookup(modem->plugin, "plugin");
if(modem->modem == NULL)
{
modem_delete(modem);
return NULL;
}
modem->helper.modem = modem;
modem->helper.event = _modem_event_callback;
modem->helper.config_get = _modem_config_get;
modem->helper.error = _modem_error;
modem->modem->helper = &modem->helper;
if(modem->modem->init != NULL && modem->modem->init(modem->modem) != 0)
{
modem->modem = NULL;
modem_delete(modem);
return NULL;
}
return modem;
}
/* modem_delete */
void modem_delete(Modem * modem)
{
if(modem->modem != NULL)
modem_stop(modem);
if(modem->plugin != NULL)
plugin_delete(modem->plugin);
object_delete(modem);
}
/* accessors */
#if 1
/* modem_get */
int modem_get(Modem * modem, ModemEventType event, ...)
{
return modem_trigger(modem, event);
}
/* modem_set */
int modem_set(Modem * modem, ModemRequestType request, ...)
{
/* FIXME implement workaround */
return -1;
}
#endif
/* modem_set_callback */
void modem_set_callback(Modem * modem, ModemEventCallback callback, void * priv)
{
modem->callback = callback;
modem->priv = priv;
}
/* useful */
/* modem_request */
int modem_request(Modem * modem, ModemRequest * request)
{
if(modem->modem->request == NULL)
return -1;
return modem->modem->request(modem->modem, request);
}
/* modem_start */
int modem_start(Modem * modem)
{
int ret = 0;
if(modem->active != 0)
return 0;
if(modem->modem->start != NULL)
ret = modem->modem->start(modem->modem);
if(ret == 0)
modem->active = 1;
return ret;
}
/* modem_stop */
int modem_stop(Modem * modem)
{
if(modem->active == 0)
return 0;
if(modem->modem->stop == NULL)
return 0;
return modem->modem->stop(modem->modem);
}
/* modem_trigger */
int modem_trigger(Modem * modem, ModemEventType event)
{
if(modem->active == 0)
return -1; /* XXX report error */
if(modem->modem->trigger == NULL)
return -1;
return modem->modem->trigger(modem->modem, event);
}
/* private */
/* functions */
/* modem_event_callback */
static void _modem_event_callback(Modem * modem, ModemEvent * event)
{
if(modem->callback == NULL)
return;
modem->callback(modem->priv, event);
}
/* modem_config_get */
static char const * _modem_config_get(Modem * modem, char const * section,
char const * variable)
{
/* FIXME implement */
return NULL;
}
/* modem_error */
static int _modem_error(Modem * modem, char const * message, int ret)
{
ModemEvent event;
if(modem == NULL || modem->callback == NULL)
return error_set_print(PACKAGE, ret, "%s", message);
event.type = MODEM_EVENT_TYPE_ERROR;
event.error.message = message;
modem->callback(modem->priv, &event);
return ret;
}