diff --git a/libs/freetdm/src/include/zap_types.h b/libs/freetdm/src/include/zap_types.h index 41a45af4a7..23bfaf9170 100644 --- a/libs/freetdm/src/include/zap_types.h +++ b/libs/freetdm/src/include/zap_types.h @@ -69,9 +69,11 @@ typedef enum { ZAP_TRUNK_T1, ZAP_TRUNK_J1, ZAP_TRUNK_BRI, + ZAP_TRUNK_FXO, + ZAP_TRUNK_FXS, ZAP_TRUNK_NONE } zap_trunk_type_t; -#define TRUNK_STRINGS "E1", "T1", "J1", "BRI", "NONE" +#define TRUNK_STRINGS "E1", "T1", "J1", "BRI", "FXO", "FXS", "NONE" ZAP_STR2ENUM_P(zap_str2zap_trunk_type, zap_trunk_type2str, zap_trunk_type_t) typedef enum { diff --git a/libs/freetdm/src/testanalog.c b/libs/freetdm/src/testanalog.c index c61cf5138a..9670564348 100644 --- a/libs/freetdm/src/testanalog.c +++ b/libs/freetdm/src/testanalog.c @@ -9,9 +9,9 @@ static void *test_call(zap_thread_t *me, void *obj) zap_size_t len; - sleep(5); + sleep(10); - printf("answer call\n"); + zap_log(ZAP_LOG_DEBUG, "answer call and start echo test\n"); zap_set_state_locked(chan, ZAP_CHANNEL_STATE_UP); @@ -24,7 +24,7 @@ static void *test_call(zap_thread_t *me, void *obj) len = sizeof(frame); if (flags & ZAP_READ) { if (zap_channel_read(chan, frame, &len) == ZAP_SUCCESS) { - //printf("WRITE %d\n", len); + //zap_log(ZAP_LOG_DEBUG, "WRITE %d\n", len); zap_channel_write(chan, frame, &len); } else { break; @@ -36,18 +36,18 @@ static void *test_call(zap_thread_t *me, void *obj) zap_set_state_locked(chan, ZAP_CHANNEL_STATE_BUSY); } - printf("call over\n"); + zap_log(ZAP_LOG_DEBUG, "call over\n"); } static ZIO_SIGNAL_CB_FUNCTION(on_signal) { - printf("got sig [%s]\n", zap_signal_event2str(sigmsg->event_id)); + zap_log(ZAP_LOG_DEBUG, "got sig [%s]\n", zap_signal_event2str(sigmsg->event_id)); switch(sigmsg->event_id) { case ZAP_SIGEVENT_START: zap_set_state_locked(sigmsg->channel, ZAP_CHANNEL_STATE_RING); - printf("launching thread and indicating ring\n"); + zap_log(ZAP_LOG_DEBUG, "launching thread and indicating ring\n"); zap_thread_create_detached(test_call, sigmsg->channel); break; default: @@ -64,17 +64,16 @@ int main(int argc, char *argv[]) zap_global_set_default_logger(ZAP_LOG_LEVEL_DEBUG); if (zap_global_init() != ZAP_SUCCESS) { - fprintf(stderr, "Error loading OpenZAP\n"); + zap_log(ZAP_LOG_ERROR, "Error loading OpenZAP\n"); exit(-1); } - printf("OpenZAP loaded\n"); + zap_log(ZAP_LOG_DEBUG, "OpenZAP loaded\n"); if (zap_span_find("wanpipe", 1, &span) != ZAP_SUCCESS) { - fprintf(stderr, "Error finding OpenZAP span\n"); + zap_log(ZAP_LOG_ERROR, "Error finding OpenZAP span\n"); } - zap_analog_configure_span(span, "us", 2000, 11, on_signal); zap_analog_start(span); diff --git a/libs/freetdm/src/zap_analog.c b/libs/freetdm/src/zap_analog.c index 5a68aabd96..d4f80a1ff8 100644 --- a/libs/freetdm/src/zap_analog.c +++ b/libs/freetdm/src/zap_analog.c @@ -110,18 +110,10 @@ static void *zap_analog_channel_run(zap_thread_t *me, void *obj) } zap_set_flag_locked(chan, ZAP_CHANNEL_INTHREAD); - - - teletone_init_session(&ts, 0, teletone_handler, dt_buffer); -#if 0 - ts.debug = 1; - ts.debug_stream = stdout; -#endif ts.rate = 8000; zap_channel_command(chan, ZAP_COMMAND_GET_CODEC, &old_codec); zap_channel_command(chan, ZAP_COMMAND_GET_INTERVAL, &interval); - zap_buffer_set_loops(dt_buffer, -1); while (zap_test_flag(chan, ZAP_CHANNEL_INTHREAD)) { @@ -132,7 +124,7 @@ static void *zap_analog_channel_run(zap_thread_t *me, void *obj) elapsed += interval; state_counter += interval; - XX printf("WTF %s %s\n", zap_channel_state2str(chan->state), zap_channel_state2str(chan->last_state)); + if (!zap_test_flag(chan, ZAP_CHANNEL_STATE_CHANGE)) { switch(chan->state) { case ZAP_CHANNEL_STATE_DIALTONE: @@ -152,7 +144,8 @@ static void *zap_analog_channel_run(zap_thread_t *me, void *obj) case ZAP_CHANNEL_STATE_UP: case ZAP_CHANNEL_STATE_IDLE: { - zap_sleep(interval * 1000); + zap_sleep(interval); + continue; } break; default: diff --git a/libs/freetdm/src/zap_io.c b/libs/freetdm/src/zap_io.c index 5f2ec84db2..a1f6a0bcba 100644 --- a/libs/freetdm/src/zap_io.c +++ b/libs/freetdm/src/zap_io.c @@ -212,6 +212,7 @@ zap_status_t zap_span_create(zap_io_interface_t *zio, zap_span_t **span) zap_copy_string(new_span->tone_map[ZAP_TONEMAP_RING], "%(2000,4000,440,480)", ZAP_TONEMAP_LEN); zap_copy_string(new_span->tone_map[ZAP_TONEMAP_BUSY], "%(500,500,480,620)", ZAP_TONEMAP_LEN); zap_copy_string(new_span->tone_map[ZAP_TONEMAP_ATTN], "%(100,100,1400,2060,2450,2600)", ZAP_TONEMAP_LEN); + new_span->trunk_type = ZAP_TRUNK_NONE; *span = new_span; return ZAP_SUCCESS; } @@ -363,9 +364,9 @@ zap_status_t zap_span_next_event(zap_span_t *span, zap_event_t **event) zap_status_t zap_channel_set_event_callback(zap_channel_t *zchan, zio_event_cb_t event_callback) { - zap_mutex_lock(zchan->span->mutex); + zap_mutex_lock(zchan->mutex); zchan->event_callback = event_callback; - zap_mutex_unlock(zchan->span->mutex); + zap_mutex_unlock(zchan->mutex); return ZAP_SUCCESS; } @@ -504,15 +505,17 @@ static zap_status_t zap_channel_reset(zap_channel_t *zchan) zap_status_t zap_channel_open_chan(zap_channel_t *zchan) { zap_status_t status = ZAP_FAIL; - - zap_mutex_lock(zchan->span->mutex); + + if ((status = zap_mutex_trylock(zchan->mutex)) != ZAP_SUCCESS) { + return status; + } if (zap_test_flag(zchan, ZAP_CHANNEL_READY) && ! zap_test_flag(zchan, ZAP_CHANNEL_OPEN)) { status = zchan->span->zio->open(zchan); if (status == ZAP_SUCCESS) { zap_set_flag(zchan, ZAP_CHANNEL_OPEN); } } - zap_mutex_unlock(zchan->span->mutex); + zap_mutex_unlock(zchan->mutex); return status; } @@ -528,9 +531,12 @@ zap_status_t zap_channel_open(const char *name, uint32_t span_id, uint32_t chan_ if (span_id < ZAP_MAX_SPANS_INTERFACE && chan_id < ZAP_MAX_CHANNELS_SPAN && zio) { zap_channel_t *check; - zap_mutex_lock(zio->spans[span_id].mutex); + check = &zio->spans[span_id].channels[chan_id]; - + if ((status = zap_mutex_trylock(check->mutex)) != ZAP_SUCCESS) { + return status; + } + if (zap_test_flag(check, ZAP_CHANNEL_READY) && ! zap_test_flag(check, ZAP_CHANNEL_OPEN)) { status = check->zio->open(check); if (status == ZAP_SUCCESS) { @@ -538,7 +544,7 @@ zap_status_t zap_channel_open(const char *name, uint32_t span_id, uint32_t chan_ *zchan = check; } } - zap_mutex_unlock(zio->spans[span_id].mutex); + zap_mutex_unlock(check->mutex); } return status; @@ -554,7 +560,7 @@ zap_status_t zap_channel_close(zap_channel_t **zchan) assert(check != NULL); *zchan = NULL; - zap_mutex_lock(check->span->mutex); + zap_mutex_lock(check->mutex); if (zap_test_flag(check, ZAP_CHANNEL_OPEN)) { status = check->zio->close(check); if (status == ZAP_SUCCESS) { @@ -563,7 +569,7 @@ zap_status_t zap_channel_close(zap_channel_t **zchan) } } - zap_mutex_unlock(check->span->mutex); + zap_mutex_unlock(check->mutex); return status; } @@ -613,7 +619,7 @@ zap_status_t zap_channel_command(zap_channel_t *zchan, zap_command_t command, vo return ZAP_FAIL; } - zap_mutex_lock(zchan->span->mutex); + zap_mutex_lock(zchan->mutex); switch(command) { case ZAP_COMMAND_SET_INTERVAL: @@ -774,7 +780,7 @@ zap_status_t zap_channel_command(zap_channel_t *zchan, zap_command_t command, vo status = zchan->zio->command(zchan, command, obj); done: - zap_mutex_unlock(zchan->span->mutex); + zap_mutex_unlock(zchan->mutex); return status; } diff --git a/libs/freetdm/src/zap_wanpipe.c b/libs/freetdm/src/zap_wanpipe.c index 81aeef82df..175cdc50da 100644 --- a/libs/freetdm/src/zap_wanpipe.c +++ b/libs/freetdm/src/zap_wanpipe.c @@ -228,9 +228,25 @@ static ZIO_CONFIGURE_FUNCTION(wanpipe_configure) span->trunk_type = zap_str2zap_trunk_type(val); zap_log(ZAP_LOG_DEBUG, "setting trunk type to '%s'\n", zap_trunk_type2str(span->trunk_type)); } else if (!strcasecmp(var, "fxo-channel")) { - configured += wp_configure_channel(&cfg, val, span, ZAP_CHAN_TYPE_FXO); + if (span->trunk_type == ZAP_TRUNK_NONE) { + span->trunk_type = ZAP_TRUNK_FXO; + zap_log(ZAP_LOG_DEBUG, "setting trunk type to '%s'\n", zap_trunk_type2str(span->trunk_type)); + } + if (span->trunk_type == ZAP_TRUNK_FXO) { + configured += wp_configure_channel(&cfg, val, span, ZAP_CHAN_TYPE_FXO); + } else { + zap_log(ZAP_LOG_WARNING, "Cannot add FXO channels to an FXS trunk!\n"); + } } else if (!strcasecmp(var, "fxs-channel")) { - configured += wp_configure_channel(&cfg, val, span, ZAP_CHAN_TYPE_FXS); + if (span->trunk_type == ZAP_TRUNK_NONE) { + span->trunk_type = ZAP_TRUNK_FXS; + zap_log(ZAP_LOG_DEBUG, "setting trunk type to '%s'\n", zap_trunk_type2str(span->trunk_type)); + } + if (span->trunk_type == ZAP_TRUNK_FXS) { + configured += wp_configure_channel(&cfg, val, span, ZAP_CHAN_TYPE_FXS); + } else { + zap_log(ZAP_LOG_WARNING, "Cannot add FXS channels to an FXO trunk!\n"); + } } else if (!strcasecmp(var, "b-channel")) { configured += wp_configure_channel(&cfg, val, span, ZAP_CHAN_TYPE_B); } else if (!strcasecmp(var, "d-channel")) {