Created
April 9, 2024 19:29
-
-
Save alessandrocarminati/0c89243c8d3620b9d3c1d1c107a66306 to your computer and use it in GitHub Desktop.
polarity.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Asterisk -- An open source telephony toolkit. | |
* | |
* Copyright (C) 2013, Alessandro. | |
* | |
* Alessandro Carminati <alessandro.carminati[at]gmail.com> | |
* | |
* This program is free software, distributed under the terms of | |
* the GNU General Public License Version 2. See the LICENSE file | |
* at the top of the source tree. | |
*/ | |
/*! \file | |
* | |
* \brief App to polarity2 reverse a DAHDI trunk | |
* | |
* \author Alessandro Carminati <alessandro.carminati[at]gmail.com> | |
* | |
* \ingroup applications | |
*/ | |
/*** MODULEINFO | |
<depend>dahdi</depend> | |
<support_level>core</support_level> | |
***/ | |
#include "asterisk.h" | |
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 357721 $") | |
#include <dahdi/user.h> | |
#include "asterisk/lock.h" | |
#include "asterisk/file.h" | |
#include "asterisk/channel.h" | |
#include "asterisk/pbx.h" | |
#include "asterisk/module.h" | |
#include "asterisk/translate.h" | |
#include "asterisk/image.h" | |
#include "asterisk/app.h" | |
/* Polarity states */ | |
#define POLARITY_IDLE 0 | |
#define POLARITY_REV 1 | |
/*** DOCUMENTATION | |
<application name="Polarity2" language="en_US"> | |
<synopsis> | |
Reverse polarity of a given DAHDI Trunk for a given num of ms. | |
</synopsis> | |
<syntax> | |
<parameter name="Channel" required="true"> | |
<para>channel whose polarity have to be reversed.</para> | |
</parameter> | |
<parameter name="Duration" required="true"> | |
<para>Duration im ms of reversed polarity.</para> | |
</parameter> | |
</syntax> | |
<description> | |
<para>Performs a polarity reversal on a given DAHDI trunk. This can be used to | |
send billing signal to old payphones.</para> | |
</description> | |
<see-also> | |
<ref type="application">Polarity</ref> | |
</see-also> | |
</application> | |
***/ | |
static char *app = "Polarity2"; | |
static inline int dahdi_wait_event(int fd) | |
{ | |
/* Avoid the silly dahdi_waitevent which ignores a bunch of events */ | |
int i,j=0; | |
i = DAHDI_IOMUX_SIGEVENT; | |
if (ioctl(fd, DAHDI_IOMUX, &i) == -1) return -1; | |
if (ioctl(fd, DAHDI_GETEVENT, &j) == -1) return -1; | |
return j; | |
} | |
static int polarity2_exec(struct ast_channel *chan, const char *data) | |
{ | |
int res = -1; | |
int x, mseconds; | |
struct dahdi_params dahdip; | |
struct ast_channel *tempchan; | |
char *info, *peers; | |
AST_DECLARE_APP_ARGS(args, | |
AST_APP_ARG(reqchans); | |
AST_APP_ARG(delay); | |
); | |
if (ast_strlen_zero(data)) { | |
ast_log(LOG_WARNING, "Polarity2 requires two arguments (dahdi/num, milliseconds)\n"); | |
return -1; | |
} | |
info = ast_strdupa(data); | |
AST_STANDARD_APP_ARGS(args, info); | |
peers = args.reqchans; | |
if (sscanf(args.delay, "%d", &mseconds) == 0) { | |
ast_log(LOG_WARNING, "Wrong format for milliseconds\n"); | |
return -1; | |
} | |
ast_verb(3, "Polarity2 has been issued on %s for %d ms\n",peers, mseconds); | |
if ((tempchan = ast_channel_get_by_name(peers))) { | |
memset(&dahdip, 0, sizeof(dahdip)); | |
res = ioctl(ast_channel_fd(tempchan, 0), DAHDI_GET_PARAMS, &dahdip); | |
ast_verb(3, "Polarity2 Begin Chan=%s : %s\n",ast_channel_name(tempchan),strerror(errno)); | |
if (!res) { | |
if (dahdip.sigtype & __DAHDI_SIG_FXO) { | |
ast_verb(3, "Polarity2 INVERT sent\n"); | |
x = POLARITY_REV; | |
res = ioctl(ast_channel_fd(tempchan, 0), DAHDI_SETPOLARITY, &x); | |
if (!res || (errno == EINPROGRESS)) { | |
if (res) { | |
/* Wait for the event to finish */ | |
dahdi_wait_event(ast_channel_fd(chan, 0)); | |
} | |
res = ast_safe_sleep(chan, mseconds); | |
ast_verb(3, "INVERTED channel %s\n", ast_channel_name(tempchan)); | |
} else ast_log(LOG_WARNING, "Unable to INVERT channel %s: %s\n", ast_channel_name(tempchan), strerror(errno)); | |
ast_verb(3, "Polarity2 REVERT sent\n"); | |
x = POLARITY_IDLE; | |
res = ioctl(ast_channel_fd(tempchan, 0), DAHDI_SETPOLARITY, &x); | |
if (!res || (errno == EINPROGRESS)) { | |
if (res) { | |
/* Wait for the event to finish */ | |
dahdi_wait_event(ast_channel_fd(chan, 0)); | |
} | |
res = ast_safe_sleep(chan, 500); | |
ast_verb(3, "REVERTED channel %s\n", ast_channel_name(tempchan)); | |
} else ast_log(LOG_WARNING, "Unable to REVERT channel %s: %s\n", ast_channel_name(tempchan), strerror(errno)); | |
ast_verb(3, "Polarity reversed completed.\n"); | |
} else { | |
ast_log(LOG_WARNING, "%s is not an FXS Channel\n", ast_channel_name(chan)); | |
return -1; | |
} | |
} else { | |
ast_log(LOG_WARNING, "Unable to get parameters of %s: %s\n", ast_channel_name(tempchan), strerror(errno)); | |
return -1; | |
} | |
return res; | |
} else { | |
ast_log(LOG_WARNING, "%s is not usable\n", peers); | |
return -1; | |
} | |
} | |
static int unload_module(void) | |
{ | |
return ast_unregister_application(app); | |
} | |
static int load_module(void) | |
{ | |
return ast_register_application_xml(app, polarity2_exec); | |
} | |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Polarity reverse channel application"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment