2007-05-22 22:07:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Anthony Minessale II
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the original author; nor the names of any contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
|
|
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openzap.h"
|
2007-05-24 03:42:40 +00:00
|
|
|
#include "zap_analog.h"
|
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
zap_status_t zap_analog_configure_span(zap_span_t *span, char *tonemap, uint32_t digit_timeout, uint32_t max_dialstr, zio_signal_cb_t sig_cb)
|
2007-05-24 03:42:40 +00:00
|
|
|
{
|
2007-05-24 16:42:38 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
assert(sig_cb != NULL);
|
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
if (span->signal_type) {
|
|
|
|
snprintf(span->last_error, sizeof(span->last_error), "Span is already configured for signalling.");
|
|
|
|
return ZAP_FAIL;
|
|
|
|
}
|
2007-05-24 16:42:38 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
if (digit_timeout < 2000 || digit_timeout > 10000) {
|
|
|
|
digit_timeout = 2000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_dialstr < 2 || max_dialstr > 20) {
|
|
|
|
max_dialstr = 11;
|
|
|
|
}
|
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
span->analog_data = malloc(sizeof(*span->analog_data));
|
|
|
|
memset(span->analog_data, 0, sizeof(*span->analog_data));
|
2007-05-24 21:57:03 +00:00
|
|
|
assert(span->analog_data != NULL);
|
|
|
|
|
|
|
|
span->analog_data->digit_timeout = digit_timeout;
|
|
|
|
span->analog_data->max_dialstr = max_dialstr;
|
|
|
|
span->analog_data->sig_cb = sig_cb;
|
2007-05-24 03:42:40 +00:00
|
|
|
span->signal_type = ZAP_SIGTYPE_ANALOG;
|
2007-05-24 16:42:38 +00:00
|
|
|
zap_span_load_tones(span, tonemap);
|
2007-05-24 21:57:03 +00:00
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
return ZAP_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
|
|
|
|
{
|
|
|
|
zap_buffer_t *dt_buffer = ts->user_data;
|
|
|
|
int wrote;
|
|
|
|
|
|
|
|
if (!dt_buffer) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wrote = teletone_mux_tones(ts, map);
|
|
|
|
zap_buffer_write(dt_buffer, ts->buffer, wrote * 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *zap_analog_channel_run(zap_thread_t *me, void *obj)
|
|
|
|
{
|
|
|
|
zap_channel_t *chan = (zap_channel_t *) obj;
|
|
|
|
zap_buffer_t *dt_buffer = NULL;
|
|
|
|
teletone_generation_session_t ts;
|
|
|
|
uint8_t frame[1024];
|
|
|
|
zap_size_t len, rlen;
|
|
|
|
zap_tone_type_t tt = ZAP_TONE_DTMF;
|
2007-05-24 21:57:03 +00:00
|
|
|
char dtmf[128];
|
2007-05-27 04:42:10 +00:00
|
|
|
zap_size_t dtmf_offset = 0;
|
2007-05-24 21:57:03 +00:00
|
|
|
zap_analog_data_t *data = chan->span->analog_data;
|
|
|
|
zap_channel_t *closed_chan;
|
|
|
|
uint32_t state_counter = 0, elapsed = 0, interval = 0, last_digit = 0, indicate = 0;
|
|
|
|
zap_sigmsg_t sig;
|
2007-05-26 03:27:31 +00:00
|
|
|
zap_status_t status;
|
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
zap_log(ZAP_LOG_DEBUG, "ANALOG CHANNEL thread starting.\n");
|
|
|
|
|
2007-05-27 04:42:10 +00:00
|
|
|
ts.buffer = NULL;
|
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
if (zap_channel_open_chan(chan) != ZAP_SUCCESS) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zap_buffer_create(&dt_buffer, 1024, 3192, 0) != ZAP_SUCCESS) {
|
2007-05-26 03:27:31 +00:00
|
|
|
snprintf(chan->last_error, sizeof(chan->last_error), "memory error!");
|
2007-05-24 03:42:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zap_channel_command(chan, ZAP_COMMAND_ENABLE_TONE_DETECT, &tt) != ZAP_SUCCESS) {
|
2007-05-26 03:27:31 +00:00
|
|
|
snprintf(chan->last_error, sizeof(chan->last_error), "error initilizing tone detector!");
|
2007-05-24 03:42:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
zap_set_flag_locked(chan, ZAP_CHANNEL_INTHREAD);
|
|
|
|
teletone_init_session(&ts, 0, teletone_handler, dt_buffer);
|
2007-05-24 16:42:38 +00:00
|
|
|
ts.rate = 8000;
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
zap_channel_command(chan, ZAP_COMMAND_GET_INTERVAL, &interval);
|
|
|
|
zap_buffer_set_loops(dt_buffer, -1);
|
2007-05-24 03:42:40 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
while (zap_test_flag(chan, ZAP_CHANNEL_INTHREAD)) {
|
2007-05-24 03:42:40 +00:00
|
|
|
zap_wait_flag_t flags = ZAP_READ;
|
|
|
|
zap_size_t dlen = 0;
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
len = sizeof(frame);
|
2007-05-24 21:57:03 +00:00
|
|
|
|
|
|
|
elapsed += interval;
|
|
|
|
state_counter += interval;
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
if (!zap_test_flag(chan, ZAP_CHANNEL_STATE_CHANGE)) {
|
|
|
|
switch(chan->state) {
|
|
|
|
case ZAP_CHANNEL_STATE_DIALTONE:
|
|
|
|
{
|
|
|
|
if (state_counter > 10000) {
|
|
|
|
zap_set_state_locked(chan, ZAP_CHANNEL_STATE_BUSY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_BUSY:
|
|
|
|
{
|
|
|
|
if (state_counter > 20000) {
|
|
|
|
zap_set_state_locked(chan, ZAP_CHANNEL_STATE_ATTN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_UP:
|
|
|
|
case ZAP_CHANNEL_STATE_IDLE:
|
|
|
|
{
|
2007-05-25 14:42:34 +00:00
|
|
|
zap_sleep(interval);
|
|
|
|
continue;
|
2007-05-24 21:57:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
zap_clear_flag_locked(chan, ZAP_CHANNEL_STATE_CHANGE);
|
2007-05-26 03:27:31 +00:00
|
|
|
indicate = 0;
|
|
|
|
state_counter = 0;
|
2007-05-24 21:57:03 +00:00
|
|
|
switch(chan->state) {
|
|
|
|
case ZAP_CHANNEL_STATE_UP:
|
|
|
|
{
|
2007-05-25 23:39:01 +00:00
|
|
|
sig.event_id = ZAP_SIGEVENT_UP;
|
2007-05-24 21:57:03 +00:00
|
|
|
data->sig_cb(&sig);
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_IDLE:
|
|
|
|
{
|
|
|
|
memset(&sig, 0, sizeof(sig));
|
|
|
|
sig.event_id = ZAP_SIGEVENT_START;
|
|
|
|
sig.chan_id = chan->chan_id;
|
|
|
|
sig.span_id = chan->span_id;
|
|
|
|
sig.channel = chan;
|
|
|
|
sig.span = chan->span;
|
|
|
|
zap_copy_string(sig.dnis, dtmf, sizeof(sig.dnis));
|
|
|
|
data->sig_cb(&sig);
|
2007-05-25 23:39:01 +00:00
|
|
|
continue;
|
2007-05-24 21:57:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_DOWN:
|
|
|
|
{
|
|
|
|
sig.event_id = ZAP_SIGEVENT_STOP;
|
|
|
|
data->sig_cb(&sig);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_DIALTONE:
|
|
|
|
{
|
|
|
|
zap_buffer_zero(dt_buffer);
|
|
|
|
teletone_run(&ts, chan->span->tone_map[ZAP_TONEMAP_DIAL]);
|
|
|
|
indicate = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_RING:
|
|
|
|
{
|
|
|
|
zap_buffer_zero(dt_buffer);
|
|
|
|
teletone_run(&ts, chan->span->tone_map[ZAP_TONEMAP_RING]);
|
|
|
|
indicate = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_BUSY:
|
|
|
|
{
|
|
|
|
zap_buffer_zero(dt_buffer);
|
|
|
|
teletone_run(&ts, chan->span->tone_map[ZAP_TONEMAP_BUSY]);
|
|
|
|
indicate = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_CHANNEL_STATE_ATTN:
|
|
|
|
{
|
|
|
|
zap_buffer_zero(dt_buffer);
|
|
|
|
teletone_run(&ts, chan->span->tone_map[ZAP_TONEMAP_ATTN]);
|
|
|
|
indicate = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
if ((dlen = zap_channel_dequeue_dtmf(chan, dtmf + dtmf_offset, sizeof(dtmf) - strlen(dtmf)))) {
|
|
|
|
if (chan->state == ZAP_CHANNEL_STATE_DIALTONE || chan->state == ZAP_CHANNEL_STATE_COLLECT) {
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "DTMF %s\n", dtmf + dtmf_offset);
|
|
|
|
if (chan->state == ZAP_CHANNEL_STATE_DIALTONE) {
|
|
|
|
zap_set_state_locked(chan, ZAP_CHANNEL_STATE_COLLECT);
|
|
|
|
}
|
|
|
|
dtmf_offset = strlen(dtmf);
|
|
|
|
last_digit = elapsed;
|
|
|
|
}
|
2007-05-24 03:42:40 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
if (last_digit && ((elapsed - last_digit > data->digit_timeout) || strlen(dtmf) > data->max_dialstr)) {
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "Number obtained [%s]\n", dtmf);
|
|
|
|
zap_set_state_locked(chan, ZAP_CHANNEL_STATE_IDLE);
|
|
|
|
last_digit = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-26 03:27:31 +00:00
|
|
|
if (zap_channel_wait(chan, &flags, interval * 2) == ZAP_FAIL) {
|
2007-05-24 03:42:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2007-05-24 21:57:03 +00:00
|
|
|
|
2007-05-26 03:27:31 +00:00
|
|
|
if (!(flags & ZAP_READ)) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-25 23:39:01 +00:00
|
|
|
|
2007-05-26 03:27:31 +00:00
|
|
|
if (zap_channel_read(chan, frame, &len) != ZAP_SUCCESS) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!indicate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chan->effective_codec != ZAP_CODEC_SLIN) {
|
|
|
|
len *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlen = zap_buffer_read_loop(dt_buffer, frame, len);
|
|
|
|
|
|
|
|
if (chan->effective_codec != ZAP_CODEC_SLIN) {
|
|
|
|
zio_codec_t codec_func = NULL;
|
|
|
|
|
|
|
|
if (chan->native_codec == ZAP_CODEC_ULAW) {
|
|
|
|
codec_func = zio_slin2ulaw;
|
|
|
|
} else if (chan->native_codec == ZAP_CODEC_ALAW) {
|
|
|
|
codec_func = zio_slin2alaw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codec_func) {
|
|
|
|
status = codec_func(frame, sizeof(frame), &rlen);
|
2007-05-24 03:42:40 +00:00
|
|
|
} else {
|
2007-05-26 03:27:31 +00:00
|
|
|
snprintf(chan->last_error, sizeof(chan->last_error), "codec error!");
|
2007-05-24 03:42:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2007-05-26 03:27:31 +00:00
|
|
|
|
|
|
|
zap_channel_write(chan, frame, &rlen);
|
2007-05-24 03:42:40 +00:00
|
|
|
}
|
2007-05-24 21:57:03 +00:00
|
|
|
|
2007-05-26 03:27:31 +00:00
|
|
|
done:
|
2007-05-24 21:57:03 +00:00
|
|
|
|
|
|
|
closed_chan = chan;
|
|
|
|
zap_channel_close(&chan);
|
2007-05-26 03:27:31 +00:00
|
|
|
|
2007-05-25 23:39:01 +00:00
|
|
|
zap_channel_command(closed_chan, ZAP_COMMAND_SET_NATIVE_CODEC, NULL);
|
2007-05-24 21:57:03 +00:00
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
if (ts.buffer) {
|
|
|
|
teletone_destroy_session(&ts);
|
|
|
|
}
|
2007-05-26 03:27:31 +00:00
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
if (dt_buffer) {
|
|
|
|
zap_buffer_destroy(&dt_buffer);
|
|
|
|
}
|
|
|
|
|
2007-05-24 21:57:03 +00:00
|
|
|
zap_clear_flag(closed_chan, ZAP_CHANNEL_INTHREAD);
|
|
|
|
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "ANALOG CHANNEL thread ended.\n");
|
2007-05-26 03:27:31 +00:00
|
|
|
|
2007-05-24 03:42:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zap_status_t process_event(zap_span_t *span, zap_event_t *event)
|
|
|
|
{
|
2007-05-24 16:42:38 +00:00
|
|
|
zap_log(ZAP_LOG_DEBUG, "EVENT [%s][%d:%d]\n", zap_oob_event2str(event->enum_id), event->channel->span_id, event->channel->chan_id);
|
2007-05-24 03:42:40 +00:00
|
|
|
|
|
|
|
switch(event->enum_id) {
|
|
|
|
case ZAP_OOB_ONHOOK:
|
|
|
|
{
|
|
|
|
zap_set_state_locked(event->channel, ZAP_CHANNEL_STATE_DOWN);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_OOB_OFFHOOK:
|
|
|
|
{
|
|
|
|
if (!zap_test_flag(event->channel, ZAP_CHANNEL_INTHREAD)) {
|
2007-05-24 21:57:03 +00:00
|
|
|
zap_set_state_locked(event->channel, ZAP_CHANNEL_STATE_DIALTONE);
|
2007-05-24 03:42:40 +00:00
|
|
|
zap_thread_create_detached(zap_analog_channel_run, event->channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ZAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *zap_analog_run(zap_thread_t *me, void *obj)
|
|
|
|
{
|
|
|
|
zap_span_t *span = (zap_span_t *) obj;
|
|
|
|
zap_analog_data_t *data = span->analog_data;
|
|
|
|
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "ANALOG thread starting.\n");
|
|
|
|
|
|
|
|
while(zap_test_flag(data, ZAP_ANALOG_RUNNING)) {
|
|
|
|
int waitms = 10;
|
|
|
|
zap_status_t status;
|
|
|
|
|
|
|
|
status = zap_span_poll_event(span, waitms);
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case ZAP_SUCCESS:
|
|
|
|
{
|
|
|
|
zap_event_t *event;
|
|
|
|
while (zap_span_next_event(span, &event) == ZAP_SUCCESS) {
|
|
|
|
if (process_event(span, event) != ZAP_SUCCESS) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZAP_FAIL:
|
|
|
|
{
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "Failure!\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
zap_clear_flag(data, ZAP_ANALOG_RUNNING);
|
|
|
|
|
|
|
|
zap_log(ZAP_LOG_DEBUG, "ANALOG thread ending.\n");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zap_status_t zap_analog_start(zap_span_t *span)
|
|
|
|
{
|
|
|
|
zap_set_flag(span->analog_data, ZAP_ANALOG_RUNNING);
|
|
|
|
return zap_thread_create_detached(zap_analog_run, span);
|
|
|
|
}
|
|
|
|
|