<commit_before>/* $Id$ */
/* Copyright (c) 2014 <NAME> <<EMAIL>> */
/* 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 <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>


/* private */
/* types */
struct _Phone
{
	Config * config;
	PhonePluginHelper helper;
	PhonePluginDefinition * plugind;
	PhonePlugin * plugin;
	int fd;
};


/* prototypes */
static int _phone_init(Phone * phone, PhonePluginDefinition * plugind);
static void _phone_destroy(Phone * phone);

/* helpers */
static char const * _helper_config_get(Phone * phone, char const * section,
		char const * variable);
static int _helper_config_set(Phone * phone, char const * section,
		char const * variable, char const * value);
static int _helper_error(Phone * phone, char const * message, int ret);
static int _helper_request(Phone * phone, ModemRequest * request);
static int _helper_trigger(Phone * phone, ModemEventType event);


/* functions */
/* phone_init */
static int _phone_init(Phone * phone, PhonePluginDefinition * plugind)
{
	if((phone->config = config_new()) == NULL)
		return -1;
	memset(&phone->helper, 0, sizeof(phone->helper));
	phone->helper.phone = phone;
	phone->helper.config_get = _helper_config_get;
	phone->helper.config_set = _helper_config_set;
	phone->helper.error = _helper_error;
	phone->helper.request = _helper_request;
	phone->helper.trigger = _helper_trigger;
	phone->plugind = plugind;
	phone->plugin = NULL;
	phone->fd = -1;
	return 0;
}


/* phone_destroy */
static void _phone_destroy(Phone * phone)
{
	if(phone->fd >= 0)
		close(phone->fd);
}


/* helpers */
/* helper_config_get */
static char const * _helper_config_get(Phone * phone, char const * section,
		char const * variable)
{
	return config_get(phone->config, section, variable);
}


/* helper_config_set */
static int _helper_config_set(Phone * phone, char const * section,
		char const * variable, char const * value)
{
	return config_set(phone->config, section, variable, value);
}


/* helper_error */
static int _helper_error(Phone * phone, char const * message, int ret)
{
	fprintf(stderr, "%s: %s\n", PROGNAME, message);
	return ret;
}


/* helper_request */
static int _request_call(Phone * phone, ModemRequest * request);
static int _request_authenticate(Phone * phone, ModemRequest * request);

static int _helper_request(Phone * phone, ModemRequest * request)
{
	switch(request->type)
	{
		case MODEM_REQUEST_AUTHENTICATE:
			return _request_authenticate(phone, request);
		case MODEM_REQUEST_CALL:
			return _request_call(phone, request);
		default:
			/* FIXME implement more */
			return -error_set_code(1, "Not implemented");
	}
}

static int _request_authenticate(Phone * phone, ModemRequest * request)
{
	if(request->authenticate.name == NULL)
		return -error_set_code(1, "Unknown authentication");
	if(strcmp(request->authenticate.name, "APN") == 0
			|| strcmp(request->authenticate.name, "GPRS") == 0)
		/* FIXME really implement */
		return 0;
	return -error_set_code(1, "Unknown authentication");
}

static int _request_call(Phone * phone, ModemRequest * request)
{
	if(request->call.call_type != MODEM_CALL_TYPE_DATA)
		return -error_set_code(1, "Unknown call type");
	/* FIXME really implement */
	return 0;
}


/* helper_trigger */
static int _trigger_connection(Phone * phone, ModemEventType type);
#if defined(SIOCGIFDATA) || defined(SIOCGIFFLAGS)
static int _trigger_connection_interface(Phone * phone, PhoneEvent * event,
		char const * interface);
#endif

static int _helper_trigger(Phone * phone, ModemEventType event)
{
	switch(event)
	{
		case MODEM_EVENT_TYPE_CONNECTION:
			return _trigger_connection(phone, event);
		default:
			/* FIXME implement more */<commit_msg>Implemented hanging up ppp connections (not fully functional)<commit_after>/* $Id$ */
/* Copyright (c) 2014 <NAME> <<EMAIL>> */
/* 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 <sys/ioctl.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <net/if.h>


/* private */
/* types */
struct _Phone
{
	Config * config;
	PhonePluginHelper helper;
	PhonePluginDefinition * plugind;
	PhonePlugin * plugin;
	int fd;
};


/* prototypes */
static int _phone_init(Phone * phone, PhonePluginDefinition * plugind);
static void _phone_destroy(Phone * phone);

/* helpers */
static char const * _helper_config_get(Phone * phone, char const * section,
		char const * variable);
static int _helper_config_set(Phone * phone, char const * section,
		char const * variable, char const * value);
static int _helper_error(Phone * phone, char const * message, int ret);
static int _helper_request(Phone * phone, ModemRequest * request);
static int _helper_trigger(Phone * phone, ModemEventType event);


/* functions */
/* phone_init */
static int _phone_init(Phone * phone, PhonePluginDefinition * plugind)
{
	if((phone->config = config_new()) == NULL)
		return -1;
	memset(&phone->helper, 0, sizeof(phone->helper));
	phone->helper.phone = phone;
	phone->helper.config_get = _helper_config_get;
	phone->helper.config_set = _helper_config_set;
	phone->helper.error = _helper_error;
	phone->helper.request = _helper_request;
	phone->helper.trigger = _helper_trigger;
	phone->plugind = plugind;
	phone->plugin = NULL;
	phone->fd = -1;
	return 0;
}


/* phone_destroy */
static void _phone_destroy(Phone * phone)
{
	if(phone->fd >= 0)
		close(phone->fd);
}


/* helpers */
/* helper_config_get */
static char const * _helper_config_get(Phone * phone, char const * section,
		char const * variable)
{
	return config_get(phone->config, section, variable);
}


/* helper_config_set */
static int _helper_config_set(Phone * phone, char const * section,
		char const * variable, char const * value)
{
	return config_set(phone->config, section, variable, value);
}


/* helper_error */
static int _helper_error(Phone * phone, char const * message, int ret)
{
	fprintf(stderr, "%s: %s\n", PROGNAME, message);
	return ret;
}


/* helper_request */
static int _request_call(Phone * phone, ModemRequest * request);
static int _request_call_hangup(Phone * phone, ModemRequest * request);
static int _request_authenticate(Phone * phone, ModemRequest * request);

static int _helper_request(Phone * phone, ModemRequest * request)
{
	switch(request->type)
	{
		case MODEM_REQUEST_AUTHENTICATE:
			return _request_authenticate(phone, request);
		case MODEM_REQUEST_CALL:
			return _request_call(phone, request);
		case MODEM_REQUEST_CALL_HANGUP:
			return _request_call_hangup(phone, request);
		default:
			/* FIXME implement more */
			return -error_set_code(1, "Not implemented");
	}
}

static int _request_authenticate(Phone * phone, ModemRequest * request)
{
	if(request->authenticate.name == NULL)
		return -error_set_code(1, "Unknown authentication");
	if(strcmp(request->authenticate.name, "APN") == 0
			|| strcmp(request->authenticate.name, "GPRS") == 0)
		/* FIXME really implement */
		return 0;
	return -error_set_code(1, "Unknown authentication");
}

static int _request_call(Phone * phone, ModemRequest * request)
{
	if(request->call.call_type != MODEM_CALL_TYPE_DATA)
		return -error_set_code(1, "Unknown call type");
	/* FIXME really implement */
	return 0;
}

static int _request_call_hangup(Phone * phone, ModemRequest * request)
{
	int ret = 0;
	char const * interface;
	String * path;
	FILE * fp;
	char buf[16];
	pid_t pid;

	if((interface = config_get(phone->config, "gprs", "interface")) == NULL)
		return -error_set_code(1, "Unknown interface");
	if((path = string_new_append("/var/run/", interface, ".pid", NULL))
			== NULL)
		return -1;
	/* FIXME likely to not be readable */
	if((fp = fopen(path, "r")) == NULL)
		ret = -error_set_code(1, "%s: %s", path, strerror(errno));
	else if(fread(buf, sizeof(*buf), sizeof(buf), fp) == 0)
		ret = -error_set_code(1, "%s: %s", path, strerror(errno));
	else
	{
		buf[sizeof(buf) - 1] = '\0';
		if(sscanf(buf, "%u", &pid) != 1)
			ret = -error_set_code(1, "%s", strerror(errno));
		else if(kill(pid, SIGHUP) != 0)
			ret = -error_set_code(1, "%u: %s", pid,
					strerror(errno));
	}
	if(fp != NULL)
		fclose(fp);
	string_delete(path);
	return ret;
}


/* helper_trigger */
static int _trigger_connection(Phone * phone, ModemEventType type);
#if defined(SIOCGIFDATA) || defined(SIOCGIFFLAGS)
static int _trigger_connection_interface(Phone * phone, PhoneEvent * event,
		char const * interface);
#endif

static int _helper_trigger(Phone * phone, ModemEventType event)
{
	switch(event)
	{
		case MODEM_EVENT_TYPE_CONNECTION:
			return _trigger_connection(phone, event);
		default:
			/* FIXME implement more */
{'fraction_non_alphanumeric': 0.12506401720782545, 'fraction_numerical': 0.0051213766260370785, 'mean_word_length': 4.630911188004614, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572205', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3112, 'n_words': 915}