Layout tweaks

This commit is contained in:
Steve Underwood 2013-01-10 22:12:19 +08:00
parent 10152cd336
commit b18490ae06
1 changed files with 17 additions and 51 deletions

View File

@ -125,7 +125,6 @@ static int t31_at_tx_handler(at_state_t *s, void *user_data, const uint8_t *buf,
return wrote; return wrote;
} }
static int t31_call_control_handler(t31_state_t *s, void *user_data, int op, const char *num) static int t31_call_control_handler(t31_state_t *s, void *user_data, int op, const char *num)
{ {
modem_t *modem = user_data; modem_t *modem = user_data;
@ -140,7 +139,6 @@ static int t31_call_control_handler(t31_state_t *s, void *user_data, int op, con
return ret; return ret;
} }
static modem_state_t modem_get_state(modem_t *modem) static modem_state_t modem_get_state(modem_t *modem)
{ {
modem_state_t state; modem_state_t state;
@ -170,7 +168,6 @@ char *modem_state2name(int state)
} }
return MODEM_STATE[state].name; return MODEM_STATE[state].name;
} }
int modem_close(modem_t *modem) int modem_close(modem_t *modem)
@ -192,7 +189,6 @@ int modem_close(modem_t *modem)
CloseHandle(modem->master); CloseHandle(modem->master);
modem->master = 0; modem->master = 0;
#endif #endif
r++; r++;
} }
@ -203,7 +199,6 @@ int modem_close(modem_t *modem)
r++; r++;
} }
if (modem->t31_state) { if (modem->t31_state) {
t31_free(modem->t31_state); t31_free(modem->t31_state);
modem->t31_state = NULL; modem->t31_state = NULL;
@ -220,7 +215,6 @@ int modem_close(modem_t *modem)
return r; return r;
} }
switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handler) switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handler)
{ {
switch_status_t status = SWITCH_STATUS_SUCCESS; switch_status_t status = SWITCH_STATUS_SUCCESS;
@ -236,7 +230,6 @@ switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handl
/* windows will have to try something like: /* windows will have to try something like:
http://com0com.cvs.sourceforge.net/viewvc/com0com/com0com/ReadMe.txt?revision=RELEASED http://com0com.cvs.sourceforge.net/viewvc/com0com/com0com/ReadMe.txt?revision=RELEASED
*/ */
#if USE_OPENPTY #if USE_OPENPTY
@ -249,7 +242,7 @@ switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handl
modem->stty = ttyname(modem->slave); modem->stty = ttyname(modem->slave);
#else #else
#ifdef WIN32 #ifdef WIN32
modem->slot = 4+globals.NEXT_ID++; /* need work here we start at COM4 for now*/ modem->slot = 4 + globals.NEXT_ID++; /* need work here we start at COM4 for now*/
snprintf(modem->devlink, sizeof(modem->devlink), "COM%d", modem->slot); snprintf(modem->devlink, sizeof(modem->devlink), "COM%d", modem->slot);
modem->master = CreateFile(modem->devlink, modem->master = CreateFile(modem->devlink,
@ -277,28 +270,22 @@ switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handl
#ifndef WIN32 #ifndef WIN32
if (modem->master < 0) { if (modem->master < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize UNIX98 master pty\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize UNIX98 master pty\n");
} }
if (grantpt(modem->master) < 0) { if (grantpt(modem->master) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to grant access to slave pty\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to grant access to slave pty\n");
} }
if (unlockpt(modem->master) < 0) { if (unlockpt(modem->master) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to unlock slave pty\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to unlock slave pty\n");
} }
modem->stty = ptsname(modem->master); modem->stty = ptsname(modem->master);
if (modem->stty == NULL) { if (modem->stty == NULL) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to obtain slave pty filename\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to obtain slave pty filename\n");
} }
modem->slave = open(modem->stty, O_RDWR); modem->slave = open(modem->stty, O_RDWR);
if (modem->slave < 0) { if (modem->slave < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to open slave pty %s\n", modem->stty); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to open slave pty %s\n", modem->stty);
} }
@ -339,7 +326,7 @@ switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handl
SetCommMask(modem->master, EV_RXCHAR); SetCommMask(modem->master, EV_RXCHAR);
if (!SetCommTimeouts(modem->master, &timeouts)){ if (!SetCommTimeouts(modem->master, &timeouts)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", modem->devlink); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", modem->devlink);
modem_close(modem); modem_close(modem);
status = SWITCH_STATUS_FALSE; status = SWITCH_STATUS_FALSE;
@ -484,7 +471,7 @@ static switch_status_t channel_on_init(switch_core_session_t *session)
modem_set_state(tech_pvt->modem, MODEM_STATE_RINGING); modem_set_state(tech_pvt->modem, MODEM_STATE_RINGING);
t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING); t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING);
while(to_ticks > 0 && switch_channel_up(channel) && modem_get_state(tech_pvt->modem) == MODEM_STATE_RINGING) { while (to_ticks > 0 && switch_channel_up(channel) && modem_get_state(tech_pvt->modem) == MODEM_STATE_RINGING) {
if (--rt <= 0) { if (--rt <= 0) {
t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING); t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING);
rt = ring_ticks; rt = ring_ticks;
@ -562,7 +549,6 @@ static switch_status_t channel_on_destroy(switch_core_session_t *session)
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
static switch_status_t channel_on_hangup(switch_core_session_t *session) static switch_status_t channel_on_hangup(switch_core_session_t *session)
{ {
switch_channel_t *channel = NULL; switch_channel_t *channel = NULL;
@ -633,7 +619,6 @@ static switch_status_t channel_on_reset(switch_core_session_t *session)
private_t *tech_pvt = (private_t *) switch_core_session_get_private(session); private_t *tech_pvt = (private_t *) switch_core_session_get_private(session);
switch_assert(tech_pvt != NULL); switch_assert(tech_pvt != NULL);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s RESET\n", switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s RESET\n",
switch_channel_get_name(switch_core_session_get_channel(session))); switch_channel_get_name(switch_core_session_get_channel(session)));
@ -642,7 +627,6 @@ static switch_status_t channel_on_reset(switch_core_session_t *session)
static switch_status_t channel_on_hibernate(switch_core_session_t *session) static switch_status_t channel_on_hibernate(switch_core_session_t *session)
{ {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s HIBERNATE\n", switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s HIBERNATE\n",
switch_channel_get_name(switch_core_session_get_channel(session))); switch_channel_get_name(switch_core_session_get_channel(session)));
@ -701,11 +685,12 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
r = t31_tx(tech_pvt->modem->t31_state, data + samples_read, samples_wanted - samples_read); r = t31_tx(tech_pvt->modem->t31_state, data + samples_read, samples_wanted - samples_read);
if (r < 0) break; if (r < 0) break;
samples_read += r; samples_read += r;
} while(samples_read < samples_wanted && r > 0); } while (samples_read < samples_wanted && r > 0);
if (r < 0) { if (r < 0) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} else if (samples_read < samples_wanted) { }
if (samples_read < samples_wanted) {
memset(data + samples_read, 0, sizeof(int16_t)*(samples_wanted - samples_read)); memset(data + samples_read, 0, sizeof(int16_t)*(samples_wanted - samples_read));
samples_read = samples_wanted; samples_read = samples_wanted;
} }
@ -765,12 +750,9 @@ static switch_status_t channel_receive_message(switch_core_session_t *session, s
break; break;
case SWITCH_MESSAGE_INDICATE_BRIDGE: case SWITCH_MESSAGE_INDICATE_BRIDGE:
mod_spandsp_indicate_data(session, SWITCH_FALSE, SWITCH_TRUE); mod_spandsp_indicate_data(session, SWITCH_FALSE, SWITCH_TRUE);
break; break;
case SWITCH_MESSAGE_INDICATE_UNBRIDGE: case SWITCH_MESSAGE_INDICATE_UNBRIDGE:
mod_spandsp_indicate_data(session, SWITCH_FALSE, SWITCH_TRUE); mod_spandsp_indicate_data(session, SWITCH_FALSE, SWITCH_TRUE);
break; break;
default: default:
break; break;
@ -788,7 +770,6 @@ static switch_status_t tech_init(private_t *tech_pvt, switch_core_session_t *ses
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
const switch_codec_implementation_t *read_impl; const switch_codec_implementation_t *read_impl;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s setup codec %s/%d/%d\n", switch_channel_get_name(channel), iananame, rate, switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s setup codec %s/%d/%d\n", switch_channel_get_name(channel), iananame, rate,
interval); interval);
@ -806,7 +787,6 @@ static switch_status_t tech_init(private_t *tech_pvt, switch_core_session_t *ses
NULL, NULL,
rate, interval, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)); rate, interval, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session));
if (status != SWITCH_STATUS_SUCCESS) { if (status != SWITCH_STATUS_SUCCESS) {
switch_core_codec_destroy(&tech_pvt->read_codec); switch_core_codec_destroy(&tech_pvt->read_codec);
goto end; goto end;
@ -825,13 +805,12 @@ static switch_status_t tech_init(private_t *tech_pvt, switch_core_session_t *ses
switch_core_timer_init(&tech_pvt->timer, "soft", switch_core_timer_init(&tech_pvt->timer, "soft",
read_impl->microseconds_per_packet / 1000, read_impl->samples_per_packet, switch_core_session_get_pool(session)); read_impl->microseconds_per_packet / 1000, read_impl->samples_per_packet, switch_core_session_get_pool(session));
switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session)); switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
switch_core_session_set_private(session, tech_pvt); switch_core_session_set_private(session, tech_pvt);
tech_pvt->session = session; tech_pvt->session = session;
tech_pvt->channel = switch_core_session_get_channel(session); tech_pvt->channel = switch_core_session_get_channel(session);
end: end:
return status; return status;
} }
@ -1018,8 +997,6 @@ static switch_status_t create_session(switch_core_session_t **new_session, modem
spandsp_globals.modem_context, spandsp_globals.modem_context,
digits); digits);
caller_profile->source = switch_core_strdup(caller_profile->pool, "mod_spandsp"); caller_profile->source = switch_core_strdup(caller_profile->pool, "mod_spandsp");
switch_channel_set_caller_profile(channel, caller_profile); switch_channel_set_caller_profile(channel, caller_profile);
tech_pvt->caller_profile = caller_profile; tech_pvt->caller_profile = caller_profile;
@ -1057,7 +1034,6 @@ static int control_handler(modem_t *modem, const char *num, int op)
op, modem_state2name(modem_get_state(modem)), modem->devlink); op, modem_state2name(modem_get_state(modem)), modem->devlink);
switch (op) { switch (op) {
case AT_MODEM_CONTROL_ANSWER: case AT_MODEM_CONTROL_ANSWER:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"Modem %s [%s] - Answering\n", modem->devlink, modem_state2name(modem_get_state(modem))); "Modem %s [%s] - Answering\n", modem->devlink, modem_state2name(modem_get_state(modem)));
@ -1096,10 +1072,8 @@ static int control_handler(modem_t *modem, const char *num, int op)
switch_clear_flag(modem, MODEM_FLAG_XOFF); switch_clear_flag(modem, MODEM_FLAG_XOFF);
wake_modem_thread(modem); wake_modem_thread(modem);
modem_set_state(modem, MODEM_STATE_HANGUP); modem_set_state(modem, MODEM_STATE_HANGUP);
if (!zstr(modem->uuid_str)) { if (!zstr(modem->uuid_str)) {
switch_core_session_t *session; switch_core_session_t *session;
@ -1114,7 +1088,6 @@ static int control_handler(modem_t *modem, const char *num, int op)
} }
} }
if (set_state) { if (set_state) {
modem_set_state(modem, MODEM_STATE_ONHOOK); modem_set_state(modem, MODEM_STATE_ONHOOK);
} }
@ -1212,7 +1185,6 @@ static int modem_wait_sock(int sock, uint32_t ms, modem_poll_t flags)
} }
return r; return r;
} }
#else #else
static int modem_wait_sock(modem_t *modem, int ms, modem_poll_t flags) static int modem_wait_sock(modem_t *modem, int ms, modem_poll_t flags)
@ -1244,7 +1216,7 @@ static int modem_wait_sock(modem_t *modem, int ms, modem_poll_t flags)
/* something went horribly wrong with WaitCommEvent(), so /* something went horribly wrong with WaitCommEvent(), so
clear all errors and try again */ clear all errors and try again */
DWORD comerrors; DWORD comerrors;
ClearCommError(modem->master,&comerrors,0); ClearCommError(modem->master, &comerrors, 0);
} else { } else {
/* IO is pending, wait for it to finish */ /* IO is pending, wait for it to finish */
dwWait = WaitForMultipleObjects(2, arHandles, FALSE, INFINITE); dwWait = WaitForMultipleObjects(2, arHandles, FALSE, INFINITE);
@ -1305,7 +1277,6 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
} }
avail = t31_at_rx_free_space(modem->t31_state); avail = t31_at_rx_free_space(modem->t31_state);
if (avail == 0) { if (avail == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Buffer Full, retrying....\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Buffer Full, retrying....\n");
switch_yield(10000); switch_yield(10000);
@ -1324,17 +1295,17 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
o.OffsetHigh = 0; o.OffsetHigh = 0;
assert(o.hEvent); assert(o.hEvent);
if (!ReadFile(modem->master, buf, avail, &readBytes, &o)) { if (!ReadFile(modem->master, buf, avail, &readBytes, &o)) {
GetOverlappedResult(modem->master,&o,&readBytes,TRUE); GetOverlappedResult(modem->master, &o, &readBytes,TRUE);
} }
CloseHandle (o.hEvent); CloseHandle (o.hEvent);
r = readBytes; r = readBytes;
#endif #endif
t31_at_rx(modem->t31_state, buf, r); t31_at_rx(modem->t31_state, buf, r);
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof(tmp));
if (!strncasecmp(buf, "AT", 2)) { if (!strncasecmp(buf, "AT", 2)) {
int x; int x;
strncpy(tmp, buf, r); strncpy(tmp, buf, r);
for (x = 0; x < r; x++) { for (x = 0; x < r; x++) {
if (tmp[x] == '\r' || tmp[x] == '\n') { if (tmp[x] == '\r' || tmp[x] == '\n') {
@ -1342,7 +1313,7 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
} }
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Command on %s [%s]\n", modem->devlink, tmp); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Command on %s [%s]\n", modem->devlink, tmp);
} }
} }
@ -1353,7 +1324,7 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
} }
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Thread ended for %s\n", modem->devlink); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Thread ended for %s\n", modem->devlink);
switch_mutex_lock(globals.mutex); switch_mutex_lock(globals.mutex);
globals.THREADCOUNT--; globals.THREADCOUNT--;
@ -1367,12 +1338,10 @@ static void launch_modem_thread(modem_t *modem)
switch_thread_t *thread; switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL; switch_threadattr_t *thd_attr = NULL;
switch_threadattr_create(&thd_attr, globals.pool); switch_threadattr_create(&thd_attr, globals.pool);
switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&thread, thd_attr, modem_thread, modem, globals.pool); switch_thread_create(&thread, thd_attr, modem_thread, modem, globals.pool);
} }
static void activate_modems(void) static void activate_modems(void)
@ -1382,16 +1351,15 @@ static void activate_modems(void)
switch_mutex_lock(globals.mutex); switch_mutex_lock(globals.mutex);
memset(globals.MODEM_POOL, 0, MAX_MODEMS); memset(globals.MODEM_POOL, 0, MAX_MODEMS);
for(x = 0; x < max; x++) { for (x = 0; x < max; x++) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Starting Modem SLOT %d\n", x); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Starting Modem SLOT %d\n", x);
launch_modem_thread(&globals.MODEM_POOL[x]); launch_modem_thread(&globals.MODEM_POOL[x]);
} }
switch_mutex_unlock(globals.mutex); switch_mutex_unlock(globals.mutex);
} }
static void deactivate_modems(void) static void deactivate_modems(void)
{ {
int max = globals.SOFT_MAX_MODEMS; int max = globals.SOFT_MAX_MODEMS;
@ -1399,8 +1367,8 @@ static void deactivate_modems(void)
switch_mutex_lock(globals.mutex); switch_mutex_lock(globals.mutex);
for(x = 0; x < max; x++) { for (x = 0; x < max; x++) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopping Modem SLOT %d\n", x); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopping Modem SLOT %d\n", x);
modem_close(&globals.MODEM_POOL[x]); modem_close(&globals.MODEM_POOL[x]);
} }
@ -1412,7 +1380,6 @@ static void deactivate_modems(void)
} }
} }
static modem_t *acquire_modem(int index) static modem_t *acquire_modem(int index)
{ {
modem_t *modem = NULL; modem_t *modem = NULL;
@ -1425,7 +1392,7 @@ static modem_t *acquire_modem(int index)
} else { } else {
int x; int x;
for(x = 0; x < globals.SOFT_MAX_MODEMS; x++) { for (x = 0; x < globals.SOFT_MAX_MODEMS; x++) {
if (globals.MODEM_POOL[x].state == MODEM_STATE_ONHOOK && (now - globals.MODEM_POOL[x].last_event) > idle_debounce) { if (globals.MODEM_POOL[x].state == MODEM_STATE_ONHOOK && (now - globals.MODEM_POOL[x].last_event) > idle_debounce) {
modem = &globals.MODEM_POOL[x]; modem = &globals.MODEM_POOL[x];
break; break;
@ -1467,7 +1434,6 @@ switch_status_t modem_global_init(switch_loadable_module_interface_t **module_in
activate_modems(); activate_modems();
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
void modem_global_shutdown(void) void modem_global_shutdown(void)