From 4268bf84b06a66c98803e4998116390ea5d24da7 Mon Sep 17 00:00:00 2001 From: Moises Silva Date: Fri, 24 Dec 2010 15:58:04 -0500 Subject: [PATCH] freetdm: more core state refactoring, still untested --- libs/freetdm/Makefile.am | 1 + libs/freetdm/src/ftdm_state.c | 458 ++++++++++++++++++ libs/freetdm/src/include/freetdm.h | 3 +- libs/freetdm/src/include/private/ftdm_core.h | 53 +- libs/freetdm/src/include/private/ftdm_state.h | 218 +++++++++ libs/freetdm/src/include/private/ftdm_types.h | 63 +-- 6 files changed, 685 insertions(+), 111 deletions(-) create mode 100644 libs/freetdm/src/ftdm_state.c create mode 100644 libs/freetdm/src/include/private/ftdm_state.h diff --git a/libs/freetdm/Makefile.am b/libs/freetdm/Makefile.am index 5e804b7505..2ab5c29e18 100644 --- a/libs/freetdm/Makefile.am +++ b/libs/freetdm/Makefile.am @@ -73,6 +73,7 @@ libfreetdm_la_SOURCES = \ $(SRC)/hashtable.c \ $(SRC)/hashtable_itr.c \ $(SRC)/ftdm_io.c \ + $(SRC)/ftdm_state.c \ $(SRC)/ftdm_queue.c \ $(SRC)/ftdm_sched.c \ $(SRC)/ftdm_call_utils.c \ diff --git a/libs/freetdm/src/ftdm_state.c b/libs/freetdm/src/ftdm_state.c new file mode 100644 index 0000000000..2b544756ec --- /dev/null +++ b/libs/freetdm/src/ftdm_state.c @@ -0,0 +1,458 @@ +/* + * Copyright (c) 2010, Sangoma Technologies + * Moises Silva + * 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 "private/ftdm_core.h" + +FTDM_ENUM_NAMES(CHANNEL_STATE_NAMES, CHANNEL_STATE_STRINGS) +FTDM_STR2ENUM(ftdm_str2ftdm_channel_state, ftdm_channel_state2str, ftdm_channel_state_t, CHANNEL_STATE_NAMES, FTDM_CHANNEL_STATE_INVALID) + +/* This function is only needed for boost and we should get rid of it at the next refactoring */ +FT_DECLARE(ftdm_status_t) ftdm_channel_init(ftdm_channel_t *fchan) +{ + ftdm_channel_lock(fchan); + + if (fchan->init_state != FTDM_CHANNEL_STATE_DOWN) { + ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, fchan, fchan->init_state, 1); + fchan->init_state = FTDM_CHANNEL_STATE_DOWN; + } + + ftdm_channel_unlock(fchan); + return FTDM_SUCCESS; +} + +FT_DECLARE(ftdm_status_t) _ftdm_channel_complete_state(const char *file, const char *func, int line, ftdm_channel_t *fchan) +{ + uint8_t hindex = 0; + ftdm_channel_state_t state = fchan->state; + + if (fchan->state_status == FTDM_STATE_STATUS_COMPLETED) { + return FTDM_SUCCESS; + } + + if (state == FTDM_CHANNEL_STATE_PROGRESS) { + ftdm_set_flag(fchan, FTDM_CHANNEL_PROGRESS); + } else if (state == FTDM_CHANNEL_STATE_UP) { + ftdm_set_flag(fchan, FTDM_CHANNEL_PROGRESS); + ftdm_set_flag(fchan, FTDM_CHANNEL_MEDIA); + ftdm_set_flag(fchan, FTDM_CHANNEL_ANSWERED); + } else if (state == FTDM_CHANNEL_STATE_PROGRESS_MEDIA) { + ftdm_set_flag(fchan, FTDM_CHANNEL_PROGRESS); + ftdm_set_flag(fchan, FTDM_CHANNEL_MEDIA); + } + + /* if there is a pending ack for an indication */ + if (ftdm_test_flag(fchan, FTDM_CHANNEL_IND_ACK_PENDING)) { + ftdm_ack_indication(fchan, fchan->indication, FTDM_SUCCESS); + ftdm_clear_flag(fchan, FTDM_CHANNEL_IND_ACK_PENDING); + } + + ftdm_log_chan_ex(fchan, file, func, line, FTDM_LOG_LEVEL_DEBUG, "Completed state change from %s to %s\n", ftdm_channel_state2str(fchan->state), ftdm_channel_state2str(state)); + hindex = (fchan->hindex == 0) ? (ftdm_array_len(fchan->history) - 1) : (hindex - 1); + + ftdm_assert(!fchan->history[hindex].end_time, "End time should be zero!\n"); + + fchan->history[hindex].end_time = ftdm_current_time_in_ms(); + + /* FIXME: broadcast condition to wake up anyone waiting on state completion if the channel + * is blocking (FTDM_CHANNEL_NONBLOCK is not set) */ + + return FTDM_SUCCESS; +} + +FT_DECLARE(ftdm_status_t) _ftdm_set_state(const char *file, const char *func, int line, + ftdm_channel_t *fchan, ftdm_channel_state_t state) +{ + if (fchan->state_status == FTDM_STATE_STATUS_NEW) { + /* the current state is new, setting a new state from a signaling module + when the current state is new is equivalent to implicitly acknowledging + the current state */ + _ftdm_channel_complete_state(file, func, line, fchan); + } + return ftdm_channel_set_state(file, func, line, fchan, state, 0); +} + +static int ftdm_parse_state_map(ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, ftdm_state_map_t *state_map) +{ + int x = 0, ok = 0; + ftdm_state_direction_t direction = ftdm_test_flag(ftdmchan, FTDM_CHANNEL_OUTBOUND) ? ZSD_OUTBOUND : ZSD_INBOUND; + + for(x = 0; x < FTDM_MAP_NODE_SIZE; x++) { + int i = 0, proceed = 0; + if (!state_map->nodes[x].type) { + break; + } + + if (state_map->nodes[x].direction != direction) { + continue; + } + + if (state_map->nodes[x].check_states[0] == FTDM_ANY_STATE) { + proceed = 1; + } else { + for(i = 0; i < FTDM_MAP_MAX; i++) { + if (state_map->nodes[x].check_states[i] == ftdmchan->state) { + proceed = 1; + break; + } + } + } + + if (!proceed) { + continue; + } + + for(i = 0; i < FTDM_MAP_MAX; i++) { + ok = (state_map->nodes[x].type == ZSM_ACCEPTABLE); + if (state_map->nodes[x].states[i] == FTDM_END) { + break; + } + if (state_map->nodes[x].states[i] == state) { + ok = !ok; + goto end; + } + } + } + end: + + return ok; +} + +/* this function MUST be called with the channel lock held. If waitrq == 1, the channel will be unlocked/locked (never call it with waitrq == 1 with an lock recursivity > 1) */ +#define DEFAULT_WAIT_TIME 1000 +FT_DECLARE(ftdm_status_t) ftdm_channel_set_state(const char *file, const char *func, int line, ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, int waitrq) +{ + int ok = 1; + int waitms = DEFAULT_WAIT_TIME; + + if (!ftdm_test_flag(ftdmchan, FTDM_CHANNEL_READY)) { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_ERROR, "Ignored state change request from %s to %s, the channel is not ready\n", + ftdm_channel_state2str(ftdmchan->state), ftdm_channel_state2str(state)); + return FTDM_FAIL; + } + + if (ftdm_test_flag(ftdmchan, FTDM_CHANNEL_STATE_CHANGE)) { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_ERROR, "Ignored state change request from %s to %s, the previous state change has not been processed yet\n", + ftdm_channel_state2str(ftdmchan->state), ftdm_channel_state2str(state)); + return FTDM_FAIL; + } + + if (ftdmchan->state == state) { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_WARNING, "Why bother changing state from %s to %s\n", ftdm_channel_state2str(ftdmchan->state), ftdm_channel_state2str(state)); + return FTDM_FAIL; + } + + if (ftdmchan->span->state_map) { + ok = ftdm_parse_state_map(ftdmchan, state, ftdmchan->span->state_map); + goto end; + } + + /* basic core state validation (by-passed if the signaling module provides a state_map) */ + switch(ftdmchan->state) { + case FTDM_CHANNEL_STATE_HANGUP: + case FTDM_CHANNEL_STATE_TERMINATING: + { + ok = 0; + switch(state) { + case FTDM_CHANNEL_STATE_DOWN: + case FTDM_CHANNEL_STATE_BUSY: + case FTDM_CHANNEL_STATE_RESTART: + ok = 1; + break; + default: + break; + } + } + break; + case FTDM_CHANNEL_STATE_UP: + { + ok = 1; + switch(state) { + case FTDM_CHANNEL_STATE_PROGRESS: + case FTDM_CHANNEL_STATE_PROGRESS_MEDIA: + case FTDM_CHANNEL_STATE_RING: + ok = 0; + break; + default: + break; + } + } + break; + case FTDM_CHANNEL_STATE_DOWN: + { + ok = 0; + + switch(state) { + case FTDM_CHANNEL_STATE_DIALTONE: + case FTDM_CHANNEL_STATE_COLLECT: + case FTDM_CHANNEL_STATE_DIALING: + case FTDM_CHANNEL_STATE_RING: + case FTDM_CHANNEL_STATE_PROGRESS_MEDIA: + case FTDM_CHANNEL_STATE_PROGRESS: + case FTDM_CHANNEL_STATE_IDLE: + case FTDM_CHANNEL_STATE_GET_CALLERID: + case FTDM_CHANNEL_STATE_GENRING: + ok = 1; + break; + default: + break; + } + } + break; + case FTDM_CHANNEL_STATE_BUSY: + { + switch(state) { + case FTDM_CHANNEL_STATE_UP: + ok = 0; + break; + default: + break; + } + } + break; + case FTDM_CHANNEL_STATE_RING: + { + switch(state) { + case FTDM_CHANNEL_STATE_UP: + ok = 1; + break; + default: + break; + } + } + break; + default: + break; + } + +end: + + if (ok) { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_DEBUG, "Changed state from %s to %s\n", ftdm_channel_state2str(ftdmchan->state), ftdm_channel_state2str(state)); + ftdmchan->last_state = ftdmchan->state; + ftdmchan->state = state; + ftdmchan->state_status = FTDM_STATE_STATUS_NEW; + ftdmchan->history[ftdmchan->hindex].file = file; + ftdmchan->history[ftdmchan->hindex].func = func; + ftdmchan->history[ftdmchan->hindex].line = line; + ftdmchan->history[ftdmchan->hindex].state = ftdmchan->state; + ftdmchan->history[ftdmchan->hindex].last_state = ftdmchan->last_state; + ftdmchan->history[ftdmchan->hindex].time = ftdm_current_time_in_ms(); + ftdmchan->history[ftdmchan->hindex].end_time = 0; + ftdmchan->hindex++; + if (ftdmchan->hindex == ftdm_array_len(ftdmchan->history)) { + ftdmchan->hindex = 0; + } + ftdm_set_flag(ftdmchan, FTDM_CHANNEL_STATE_CHANGE); + + ftdm_mutex_lock(ftdmchan->span->mutex); + ftdm_set_flag(ftdmchan->span, FTDM_SPAN_STATE_CHANGE); + if (ftdmchan->span->pendingchans) { + ftdm_queue_enqueue(ftdmchan->span->pendingchans, ftdmchan); + } + ftdm_mutex_unlock(ftdmchan->span->mutex); + } else { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_WARNING, "VETO state change from %s to %s\n", ftdm_channel_state2str(ftdmchan->state), ftdm_channel_state2str(state)); + goto done; + } + + if (ftdm_test_flag(ftdmchan, FTDM_CHANNEL_NONBLOCK)) { + /* the channel should not block waiting for state processing */ + goto done; + } + + /* there is an inherent race here between set and check of the change flag but we do not care because + * the flag should never last raised for more than a few ms for any state change */ + while (waitrq && waitms > 0) { + /* give a chance to the signaling stack to process it */ + ftdm_mutex_unlock(ftdmchan->mutex); + + ftdm_sleep(10); + waitms -= 10; + + ftdm_mutex_lock(ftdmchan->mutex); + + /* if the flag is no longer set, the state change was processed (or is being processed) */ + if (!ftdm_test_flag(ftdmchan, FTDM_CHANNEL_STATE_CHANGE)) { + break; + } + + /* if the state is no longer what we set, the state change was + * obviously processed (and the current state change flag is for other state change) */ + if (ftdmchan->state != state) { + break; + } + } + + if (waitms <= 0) { + ftdm_log_chan_ex(ftdmchan, file, func, line, FTDM_LOG_LEVEL_WARNING, "state change from %s to %s was most likely not processed after aprox %dms\n", + ftdm_channel_state2str(ftdmchan->last_state), ftdm_channel_state2str(state), DEFAULT_WAIT_TIME); + ok = 0; + } +done: + return ok ? FTDM_SUCCESS : FTDM_FAIL; +} + +FT_DECLARE(int) ftdm_channel_get_state(const ftdm_channel_t *ftdmchan) +{ + int state; + ftdm_channel_lock(ftdmchan); + state = ftdmchan->state; + ftdm_channel_unlock(ftdmchan); + return state; +} + +FT_DECLARE(const char *) ftdm_channel_get_state_str(const ftdm_channel_t *ftdmchan) +{ + const char *state; + ftdm_channel_lock(ftdmchan); + state = ftdm_channel_state2str(ftdmchan->state); + ftdm_channel_unlock(ftdmchan); + return state; +} + +FT_DECLARE(int) ftdm_channel_get_last_state(const ftdm_channel_t *ftdmchan) +{ + int last_state; + ftdm_channel_lock(ftdmchan); + last_state = ftdmchan->last_state; + ftdm_channel_unlock(ftdmchan); + return last_state; +} + +FT_DECLARE(const char *) ftdm_channel_get_last_state_str(const ftdm_channel_t *ftdmchan) +{ + const char *state; + ftdm_channel_lock(ftdmchan); + state = ftdm_channel_state2str(ftdmchan->last_state); + ftdm_channel_unlock(ftdmchan); + return state; +} + +static void write_history_entry(const ftdm_channel_t *fchan, ftdm_stream_handle_t *stream, int i, ftdm_time_t *prevtime) +{ + char func[255]; + char line[255]; + char states[255]; + const char *filename = NULL; + snprintf(states, sizeof(states), "%-5.15s => %-5.15s", ftdm_channel_state2str(fchan->history[i].last_state), ftdm_channel_state2str(fchan->history[i].state)); + snprintf(func, sizeof(func), "[%s]", fchan->history[i].func); + filename = strrchr(fchan->history[i].file, *FTDM_PATH_SEPARATOR); + if (!filename) { + filename = fchan->history[i].file; + } else { + filename++; + } + if (!(*prevtime)) { + *prevtime = fchan->history[i].time; + } + snprintf(line, sizeof(func), "[%s:%d]", filename, fchan->history[i].line); + stream->write_function(stream, "%-30.30s %-30.30s %-30.30s %lums\n", states, func, line, (fchan->history[i].time - *prevtime)); + *prevtime = fchan->history[i].time; +} + +FT_DECLARE(char *) ftdm_channel_get_history_str(const ftdm_channel_t *fchan) +{ + uint8_t i = 0; + ftdm_time_t currtime = 0; + ftdm_time_t prevtime = 0; + + ftdm_stream_handle_t stream = { 0 }; + FTDM_STANDARD_STREAM(stream); + if (!fchan->history[0].file) { + stream.write_function(&stream, "-- No state history --\n"); + return stream.data; + } + + stream.write_function(&stream, "%-30.30s %-30.30s %-30.30s %s", + "-- States --", "-- Function --", "-- Location --", "-- Time Offset --\n"); + + for (i = fchan->hindex; i < ftdm_array_len(fchan->history); i++) { + if (!fchan->history[i].file) { + break; + } + write_history_entry(fchan, &stream, i, &prevtime); + } + + for (i = 0; i < fchan->hindex; i++) { + write_history_entry(fchan, &stream, i, &prevtime); + } + + currtime = ftdm_current_time_in_ms(); + + stream.write_function(&stream, "\nTime since last state change: %lums\n", (currtime - prevtime)); + + return stream.data; +} + +FT_DECLARE(ftdm_status_t) ftdm_channel_advance_states(ftdm_channel_t *fchan, ftdm_channel_state_processor_t state_processor) +{ + ftdm_channel_state_t state; + + ftdm_channel_lock(fchan); + while (fchan->state_status == FTDM_STATE_STATUS_NEW) { + state = fchan->state; + state_processor(fchan); + if (state == fchan->state) { + /* if the state did not change, the state status must go to PROCESSED + * otherwise we don't touch it since is a new state and the old state was + * already completed implicitly by the state_processor() function via some internal + * call to ftdm_set_state() */ + fchan->state_status = FTDM_STATE_STATUS_PROCESSED; + } + } + ftdm_channel_unlock(fchan); + return FTDM_SUCCESS; +} + +FT_DECLARE(int) ftdm_check_state_all(ftdm_span_t *span, ftdm_channel_state_t state) +{ + uint32_t j; + for(j = 1; j <= span->chan_count; j++) { + if (span->channels[j]->state != state || ftdm_test_flag(span->channels[j], FTDM_CHANNEL_STATE_CHANGE)) { + return 0; + } + } + return 1; +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/libs/freetdm/src/include/freetdm.h b/libs/freetdm/src/include/freetdm.h index 68c9fdc7c4..30fe6ca4f5 100644 --- a/libs/freetdm/src/include/freetdm.h +++ b/libs/freetdm/src/include/freetdm.h @@ -440,6 +440,7 @@ typedef struct { * This is used during incoming calls when you want to request the signaling stack * to notify about indications occurring locally. See ftdm_channel_call_indicate for more info */ typedef enum { + FTDM_CHANNEL_INDICATE_NONE, FTDM_CHANNEL_INDICATE_RINGING, FTDM_CHANNEL_INDICATE_PROCEED, FTDM_CHANNEL_INDICATE_PROGRESS, @@ -449,7 +450,7 @@ typedef enum { FTDM_CHANNEL_INDICATE_ANSWER, FTDM_CHANNEL_INDICATE_INVALID, } ftdm_channel_indication_t; -#define INDICATION_STRINGS "RINGING", "PROCEED", "PROGRESS", "PROGRESS_MEDIA", "BUSY", "ANSWER", "INVALID" +#define INDICATION_STRINGS "NONE", "RINGING", "PROCEED", "PROGRESS", "PROGRESS_MEDIA", "BUSY", "ANSWER", "INVALID" /*! \brief Move from string to ftdm_channel_indication_t and viceversa */ FTDM_STR2ENUM_P(ftdm_str2channel_indication, ftdm_channel_indication2str, ftdm_channel_indication_t) diff --git a/libs/freetdm/src/include/private/ftdm_core.h b/libs/freetdm/src/include/private/ftdm_core.h index dd70dd10ed..8f97456fb7 100644 --- a/libs/freetdm/src/include/private/ftdm_core.h +++ b/libs/freetdm/src/include/private/ftdm_core.h @@ -192,17 +192,6 @@ extern "C" { #define ftdm_clear_sflag_locked(obj, flag) assert(obj->mutex != NULL); ftdm_mutex_lock(obj->mutex); (obj)->sflags &= ~(flag); ftdm_mutex_unlock(obj->mutex); -#define ftdm_set_state(obj, s) ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s, 0); \ - -#define ftdm_set_state_locked(obj, s) \ - do { \ - ftdm_channel_lock(obj); \ - ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s, 0); \ - ftdm_channel_unlock(obj); \ - } while(0); - -#define ftdm_set_state_r(obj, s, r) r = ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s, 0); - #ifdef _MSC_VER /* The while(0) below throws a conditional expression is constant warning */ #pragma warning(disable:4127) @@ -363,15 +352,6 @@ typedef struct { ftdm_mutex_t *mutex; } ftdm_dtmf_debug_t; -typedef struct { - const char *file; - const char *func; - int line; - ftdm_channel_state_t state; - ftdm_channel_state_t last_state; - ftdm_time_t time; -} ftdm_channel_history_entry_t; - typedef enum { FTDM_IOSTATS_ERROR_CRC = (1 << 0), FTDM_IOSTATS_ERROR_FRAME = (1 << 1), @@ -424,9 +404,11 @@ struct ftdm_channel { uint32_t native_interval; uint32_t packet_len; ftdm_channel_state_t state; + ftdm_state_status_t state_status; ftdm_channel_state_t last_state; ftdm_channel_state_t init_state; - ftdm_channel_history_entry_t history[10]; + ftdm_channel_indication_t indication; + ftdm_state_history_entry_t history[10]; uint8_t hindex; ftdm_mutex_t *mutex; teletone_dtmf_detect_state_t dtmf_detect; @@ -572,9 +554,6 @@ FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_checksum(ftdm_fsk_data_state_t *stat FT_DECLARE(ftdm_status_t) ftdm_fsk_data_add_sdmf(ftdm_fsk_data_state_t *state, const char *date, char *number); FT_DECLARE(ftdm_status_t) ftdm_channel_send_fsk_data(ftdm_channel_t *ftdmchan, ftdm_fsk_data_state_t *fsk_data, float db_level); -FT_DECLARE(ftdm_status_t) ftdm_channel_set_state(const char *file, const char *func, int line, - ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, int wait); - FT_DECLARE(ftdm_status_t) ftdm_span_load_tones(ftdm_span_t *span, const char *mapname); FT_DECLARE(ftdm_time_t) ftdm_current_time_in_ms(void); @@ -589,8 +568,6 @@ FT_DECLARE(void) print_hex_bytes(uint8_t *data, ftdm_size_t dlen, char *buf, ftd FT_DECLARE_NONSTD(int) ftdm_hash_equalkeys(void *k1, void *k2); FT_DECLARE_NONSTD(uint32_t) ftdm_hash_hashfromstring(void *ky); -FT_DECLARE(ftdm_status_t) ftdm_channel_complete_state(ftdm_channel_t *ftdmchan); - FT_DECLARE(int) ftdm_load_modules(void); FT_DECLARE(ftdm_status_t) ftdm_unload_modules(void); @@ -707,30 +684,6 @@ static __inline__ void ftdm_abort(void) #endif } -static __inline__ void ftdm_set_state_all(ftdm_span_t *span, ftdm_channel_state_t state) -{ - uint32_t j; - ftdm_mutex_lock(span->mutex); - for(j = 1; j <= span->chan_count; j++) { - if (!FTDM_IS_DCHAN(span->channels[j])) { - ftdm_set_state_locked((span->channels[j]), state); - } - } - ftdm_mutex_unlock(span->mutex); -} - -static __inline__ int ftdm_check_state_all(ftdm_span_t *span, ftdm_channel_state_t state) -{ - uint32_t j; - for(j = 1; j <= span->chan_count; j++) { - if (span->channels[j]->state != state || ftdm_test_flag(span->channels[j], FTDM_CHANNEL_STATE_CHANGE)) { - return 0; - } - } - - return 1; -} - static __inline__ int16_t ftdm_saturated_add(int16_t sample1, int16_t sample2) { int addres; diff --git a/libs/freetdm/src/include/private/ftdm_state.h b/libs/freetdm/src/include/private/ftdm_state.h new file mode 100644 index 0000000000..79a3df07e9 --- /dev/null +++ b/libs/freetdm/src/include/private/ftdm_state.h @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2010, Sangoma Technologies + * Moises Silva + * 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. + */ + +#ifndef __FTDM_STATE_H__ +#define __FTDM_STATE_H__ + +/*! \file + * \brief State handling definitions + * \note Most, if not all of the state handling functions assume you have a lock acquired. Touching the channel + * state is a sensitive matter that requires checks and careful thought and is typically a process that + * is not encapsulated within a single function, therefore the lock must be explicitly acquired by the + * caller (most of the time, signaling modules), process states, set a new state and process it, and + * finally unlock the channel. See docs/locking.txt fore more info + */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + FTDM_CHANNEL_STATE_DOWN, + FTDM_CHANNEL_STATE_HOLD, + FTDM_CHANNEL_STATE_SUSPENDED, + FTDM_CHANNEL_STATE_DIALTONE, + FTDM_CHANNEL_STATE_COLLECT, + FTDM_CHANNEL_STATE_RING, + FTDM_CHANNEL_STATE_RINGING, + FTDM_CHANNEL_STATE_BUSY, + FTDM_CHANNEL_STATE_ATTN, + FTDM_CHANNEL_STATE_GENRING, + FTDM_CHANNEL_STATE_DIALING, + FTDM_CHANNEL_STATE_GET_CALLERID, + FTDM_CHANNEL_STATE_CALLWAITING, + FTDM_CHANNEL_STATE_RESTART, + FTDM_CHANNEL_STATE_PROCEED, + FTDM_CHANNEL_STATE_PROGRESS, + FTDM_CHANNEL_STATE_PROGRESS_MEDIA, + FTDM_CHANNEL_STATE_UP, + FTDM_CHANNEL_STATE_IDLE, + FTDM_CHANNEL_STATE_TERMINATING, + FTDM_CHANNEL_STATE_CANCEL, + FTDM_CHANNEL_STATE_HANGUP, + FTDM_CHANNEL_STATE_HANGUP_COMPLETE, + FTDM_CHANNEL_STATE_IN_LOOP, + FTDM_CHANNEL_STATE_RESET, + FTDM_CHANNEL_STATE_INVALID +} ftdm_channel_state_t; +#define CHANNEL_STATE_STRINGS "DOWN", "HOLD", "SUSPENDED", "DIALTONE", "COLLECT", \ + "RING", "RINGING", "BUSY", "ATTN", "GENRING", "DIALING", "GET_CALLERID", "CALLWAITING", \ + "RESTART", "PROCEED", "PROGRESS", "PROGRESS_MEDIA", "UP", "IDLE", "TERMINATING", "CANCEL", \ + "HANGUP", "HANGUP_COMPLETE", "IN_LOOP", "RESET", "INVALID" +FTDM_STR2ENUM_P(ftdm_str2ftdm_channel_state, ftdm_channel_state2str, ftdm_channel_state_t) + +typedef struct { + const char *file; + const char *func; + int line; + ftdm_channel_state_t state; + ftdm_channel_state_t last_state; + ftdm_time_t time; + ftdm_time_t end_time; +} ftdm_state_history_entry_t; + +typedef ftdm_status_t (*ftdm_channel_state_processor_t)(ftdm_channel_t *fchan); + +FT_DECLARE(ftdm_status_t) ftdm_channel_advance_states(ftdm_channel_t *fchan, ftdm_channel_state_processor_t processor); +FT_DECLARE(ftdm_status_t) _ftdm_channel_complete_state(const char *file, const char *function, int line, ftdm_channel_t *fchan); +#define ftdm_channel_complete_state(obj) _ftdm_channel_complete_state(__FILE__, __FUNCTION__, __LINE__, obj) +FT_DECLARE(int) ftdm_check_state_all(ftdm_span_t *span, ftdm_channel_state_t state); + +/*! + * \brief Status of the current channel state + * \note A given state goes thru several status (yes, states for the state!) + * The order is always FTDM_STATE_STATUS_NEW -> FTDM_STATE_STATUS_PROCESSED -> FTDM_STATUS_COMPLETED + * However, is possible to go from NEW -> COMPLETED directly when the signaling module explicitly changes + * the state of the channel in the middle of processing the current state by calling the ftdm_set_state() API + * + * FTDM_STATE_STATUS_NEW - + * Someone just set the state of the channel, either the signaling module or the user (implicitly through a call API). + * This is accomplished by calling ftdm_channel_set_state() which changes the 'state' and 'last_state' memebers of + * the ftdm_channel_t structure. + * + * FTDM_STATE_STATUS_PROCESSED - + * The signaling module did something based on the new state. + * + * This is accomplished via ftdm_channel_advance_states() + * + * When ftdm_channel_advance_states(), at the very least, if the channel has its state in FTDM_STATE_STATUS_NEW, it + * will move to FTDM_STATE_STATUS_PROCESSED, depending on what the signaling module does during the processing + * the state may move to FTDM_STATE_STATUS_COMPLETED right after or wait for a signaling specific event to complete it. + * It is also possible that more state transitions occur during the execution of ftdm_channel_advance_states() if one + * state processing/completion leads to another state change, the function will not return until the chain of events + * lead to a state that is not in FTDM_STATE_STATUS_NEW + * + * FTDM_STATE_STATUS_COMPLETED - + * The signaling module completed the processing of the state and there is nothing further to be done for this state. + * + * This is accomplished either explicitly by the signaling module by calling ftdm_channel_complete_state() or by + * the signaling module implicitly by trying to set the state of the channel to a new state via ftdm_set_state() + * + * When working with blocking channels (FTDM_CHANNEL_NONBLOCK flag not set), the user thread is signaled and unblocked + * so it can continue. + * + * When a state moves to this status is also possible for a signal FTDM_SIGEVENT_INDICATION_COMPLETED to be delivered + * by the core if the state change was associated to an indication requested by the user, + */ +typedef enum { + FTDM_STATE_STATUS_NEW, + FTDM_STATE_STATUS_PROCESSED, + FTDM_STATE_STATUS_COMPLETED +} ftdm_state_status_t; + +typedef enum { + ZSM_NONE, + ZSM_UNACCEPTABLE, + ZSM_ACCEPTABLE +} ftdm_state_map_type_t; + +typedef enum { + ZSD_INBOUND, + ZSD_OUTBOUND, +} ftdm_state_direction_t; + +#define FTDM_MAP_NODE_SIZE 512 +#define FTDM_MAP_MAX FTDM_CHANNEL_STATE_INVALID+2 + +struct ftdm_state_map_node { + ftdm_state_direction_t direction; + ftdm_state_map_type_t type; + ftdm_channel_state_t check_states[FTDM_MAP_MAX]; + ftdm_channel_state_t states[FTDM_MAP_MAX]; +}; +typedef struct ftdm_state_map_node ftdm_state_map_node_t; + +struct ftdm_state_map { + ftdm_state_map_node_t nodes[FTDM_MAP_NODE_SIZE]; +}; +typedef struct ftdm_state_map ftdm_state_map_t; + +FT_DECLARE(ftdm_status_t) ftdm_channel_set_state(const char *file, const char *func, int line, + ftdm_channel_t *ftdmchan, ftdm_channel_state_t state, int wait); + +/*!\brief Set the state of a channel immediately and implicitly complete the previous state */ +FT_DECLARE(ftdm_status_t) _ftdm_set_state(const char *file, const char *func, int line, + ftdm_channel_t *fchan, ftdm_channel_state_t state); +#define ftdm_set_state(obj, s) _ftdm_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s); \ + +/*!\brief This macro is deprecated, signaling modules should always lock the channel themselves anyways since they must + * process first the user pending state changes then set a new state before releasing the lock + */ +#define ftdm_set_state_locked(obj, s) \ + do { \ + ftdm_channel_lock(obj); \ + ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s, 0); \ + ftdm_channel_unlock(obj); \ + } while(0); + +#define ftdm_set_state_r(obj, s, r) r = ftdm_channel_set_state(__FILE__, __FUNCTION__, __LINE__, obj, s, 0); + +#define ftdm_set_state_all(span, state) \ + do { \ + uint32_t _j; \ + ftdm_mutex_lock((span)->mutex); \ + for(_j = 1; _j <= (span)->chan_count; _j++) { \ + if (!FTDM_IS_DCHAN(span->channels[_j])) { \ + ftdm_set_state_locked((span->channels[_j]), state); \ + } \ + } \ + ftdm_mutex_unlock((span)->mutex); \ + } while (0); + +#ifdef __cplusplus +} +#endif + +#endif + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/libs/freetdm/src/include/private/ftdm_types.h b/libs/freetdm/src/include/private/ftdm_types.h index d70bc69b3a..c4d48a893d 100644 --- a/libs/freetdm/src/include/private/ftdm_types.h +++ b/libs/freetdm/src/include/private/ftdm_types.h @@ -204,40 +204,6 @@ typedef enum { FTDM_CHANNEL_FEATURE_IO_STATS = (1<<9), /*!< Channel supports IO statistics (HDLC channels only) */ } ftdm_channel_feature_t; -typedef enum { - FTDM_CHANNEL_STATE_DOWN, - FTDM_CHANNEL_STATE_HOLD, - FTDM_CHANNEL_STATE_SUSPENDED, - FTDM_CHANNEL_STATE_DIALTONE, - FTDM_CHANNEL_STATE_COLLECT, - FTDM_CHANNEL_STATE_RING, - FTDM_CHANNEL_STATE_RINGING, - FTDM_CHANNEL_STATE_BUSY, - FTDM_CHANNEL_STATE_ATTN, - FTDM_CHANNEL_STATE_GENRING, - FTDM_CHANNEL_STATE_DIALING, - FTDM_CHANNEL_STATE_GET_CALLERID, - FTDM_CHANNEL_STATE_CALLWAITING, - FTDM_CHANNEL_STATE_RESTART, - FTDM_CHANNEL_STATE_PROCEED, - FTDM_CHANNEL_STATE_PROGRESS, - FTDM_CHANNEL_STATE_PROGRESS_MEDIA, - FTDM_CHANNEL_STATE_UP, - FTDM_CHANNEL_STATE_IDLE, - FTDM_CHANNEL_STATE_TERMINATING, - FTDM_CHANNEL_STATE_CANCEL, - FTDM_CHANNEL_STATE_HANGUP, - FTDM_CHANNEL_STATE_HANGUP_COMPLETE, - FTDM_CHANNEL_STATE_IN_LOOP, - FTDM_CHANNEL_STATE_RESET, - FTDM_CHANNEL_STATE_INVALID -} ftdm_channel_state_t; -#define CHANNEL_STATE_STRINGS "DOWN", "HOLD", "SUSPENDED", "DIALTONE", "COLLECT", \ - "RING", "RINGING", "BUSY", "ATTN", "GENRING", "DIALING", "GET_CALLERID", "CALLWAITING", \ - "RESTART", "PROCEED", "PROGRESS", "PROGRESS_MEDIA", "UP", "IDLE", "TERMINATING", "CANCEL", \ - "HANGUP", "HANGUP_COMPLETE", "IN_LOOP", "RESET", "INVALID" -FTDM_STR2ENUM_P(ftdm_str2ftdm_channel_state, ftdm_channel_state2str, ftdm_channel_state_t) - /*!< Channel flags. This used to be an enum but we reached the 32bit limit for enums, is safer this way */ #define FTDM_CHANNEL_CONFIGURED (1ULL << 0) #define FTDM_CHANNEL_READY (1ULL << 1) @@ -282,33 +248,10 @@ FTDM_STR2ENUM_P(ftdm_str2ftdm_channel_state, ftdm_channel_state2str, ftdm_channe #define FTDM_CHANNEL_CALL_STARTED (1ULL << 32) /*!< The user wants non-blocking operations in the channel */ #define FTDM_CHANNEL_NONBLOCK (1ULL << 33) +/*!< There is a pending acknowledge for an indication */ +#define FTDM_CHANNEL_IND_ACK_PENDING (1ULL << 34) -typedef enum { - ZSM_NONE, - ZSM_UNACCEPTABLE, - ZSM_ACCEPTABLE -} ftdm_state_map_type_t; - -typedef enum { - ZSD_INBOUND, - ZSD_OUTBOUND, -} ftdm_state_direction_t; - -#define FTDM_MAP_NODE_SIZE 512 -#define FTDM_MAP_MAX FTDM_CHANNEL_STATE_INVALID+2 - -struct ftdm_state_map_node { - ftdm_state_direction_t direction; - ftdm_state_map_type_t type; - ftdm_channel_state_t check_states[FTDM_MAP_MAX]; - ftdm_channel_state_t states[FTDM_MAP_MAX]; -}; -typedef struct ftdm_state_map_node ftdm_state_map_node_t; - -struct ftdm_state_map { - ftdm_state_map_node_t nodes[FTDM_MAP_NODE_SIZE]; -}; -typedef struct ftdm_state_map ftdm_state_map_t; +#include "ftdm_state.h" typedef enum ftdm_channel_hw_link_status { FTDM_HW_LINK_DISCONNECTED = 0,