Desktop
/* $Id: modem.c,v 1.54.2.3 2011/01/22 15:37:59 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 <stdarg.h>
#include <string.h>
#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 */
/* 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_request_type */
int modem_request_type(Modem * modem, ModemRequestType type, ...)
{
va_list ap;
ModemRequest request;
va_start(ap, type);
memset(&request, 0, sizeof(request));
switch((request.type = type))
{
/* no arguments */
case MODEM_REQUEST_CALL_HANGUP:
break;
case MODEM_REQUEST_AUTHENTICATE:
request.authenticate.username = va_arg(ap,
char const *);
request.authenticate.password = va_arg(ap,
char const *);
break;
case MODEM_REQUEST_CALL:
request.call.number = va_arg(ap, char const *);
break;
case MODEM_REQUEST_CALL_PRESENTATION:
request.call_presentation.enabled = va_arg(ap, int);
break;
case MODEM_REQUEST_CONTACT:
request.contact.id = va_arg(ap, unsigned int);
break;
case MODEM_REQUEST_MESSAGE:
request.message.id = va_arg(ap, unsigned int);
break;
default: /* XXX unknown request type */
va_end(ap);
return -1;
}
va_end(ap);
return modem_request(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;
}