FS-3761 --resolve

This commit is contained in:
Marc Olivier Chouinard 2011-12-17 16:39:04 -05:00
parent 2b34887698
commit ee4145c72a
2 changed files with 92 additions and 80 deletions

View File

@ -165,6 +165,7 @@ char *modem_state2name(int state)
int modem_close(modem_t *modem) int modem_close(modem_t *modem)
{ {
int r = 0; int r = 0;
switch_status_t was_running = switch_test_flag(modem, MODEM_FLAG_RUNNING);
switch_clear_flag(modem, MODEM_FLAG_RUNNING); switch_clear_flag(modem, MODEM_FLAG_RUNNING);
@ -199,16 +200,19 @@ int modem_close(modem_t *modem)
unlink(modem->devlink); unlink(modem->devlink);
if (was_running) {
switch_mutex_lock(globals.mutex); switch_mutex_lock(globals.mutex);
globals.REF_COUNT--; globals.REF_COUNT--;
switch_mutex_unlock(globals.mutex); switch_mutex_unlock(globals.mutex);
}
return r; return r;
} }
int 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;
#ifdef WIN32 #ifdef WIN32
COMMTIMEOUTS timeouts={0}; COMMTIMEOUTS timeouts={0};
#endif #endif
@ -228,7 +232,8 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
if (modem->master < 0) { if (modem->master < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize pty\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize pty\n");
return -1; status = SWITCH_STATUS_FALSE;
goto end;
} }
modem->stty = ttyname(modem->slave); modem->stty = ttyname(modem->slave);
@ -245,12 +250,13 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED,
0); 0);
if(modem->master==INVALID_HANDLE_VALUE) { if(modem->master==INVALID_HANDLE_VALUE) {
status = SWITCH_STATUS_FALSE;
if(GetLastError()==ERROR_FILE_NOT_FOUND) { if(GetLastError()==ERROR_FILE_NOT_FOUND) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port does not exist\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port does not exist\n");
return -1; goto end;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port open error\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port open error\n");
return -1; goto end;
} }
#elif !defined(HAVE_POSIX_OPENPT) #elif !defined(HAVE_POSIX_OPENPT)
modem->master = open("/dev/ptmx", O_RDWR); modem->master = open("/dev/ptmx", O_RDWR);
@ -303,13 +309,15 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
if (symlink(modem->stty, modem->devlink)) { if (symlink(modem->stty, modem->devlink)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to create %s symbolic link\n", modem->devlink); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to create %s symbolic link\n", modem->devlink);
modem_close(modem); modem_close(modem);
return -1; status = SWITCH_STATUS_FALSE;
goto end;
} }
if (fcntl(modem->master, F_SETFL, fcntl(modem->master, F_GETFL, 0) | O_NONBLOCK)) { if (fcntl(modem->master, F_SETFL, fcntl(modem->master, F_GETFL, 0) | O_NONBLOCK)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", ttyname(modem->master)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", ttyname(modem->master));
modem_close(modem); modem_close(modem);
return -1; status = SWITCH_STATUS_FALSE;
goto end;
} }
#else #else
timeouts.ReadIntervalTimeout=50; timeouts.ReadIntervalTimeout=50;
@ -324,7 +332,8 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
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);
return -1; status = SWITCH_STATUS_FALSE;
goto end;
} }
modem->threadAbort = CreateEvent(NULL, TRUE, FALSE, NULL); modem->threadAbort = CreateEvent(NULL, TRUE, FALSE, NULL);
#endif #endif
@ -332,8 +341,8 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
if (!(modem->t31_state = t31_init(NULL, t31_at_tx_handler, modem, t31_call_control_handler, modem, NULL, NULL))) { if (!(modem->t31_state = t31_init(NULL, t31_at_tx_handler, modem, t31_call_control_handler, modem, NULL, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot initialize the T.31 modem\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot initialize the T.31 modem\n");
modem_close(modem); modem_close(modem);
return -1; status = SWITCH_STATUS_FALSE;
goto end;
} }
if (spandsp_globals.modem_verbose) { if (spandsp_globals.modem_verbose) {
@ -364,7 +373,8 @@ int modem_init(modem_t *modem, modem_control_handler_t control_handler)
globals.REF_COUNT++; globals.REF_COUNT++;
switch_mutex_unlock(globals.mutex); switch_mutex_unlock(globals.mutex);
return 0; end:
return status;
} }
static switch_endpoint_interface_t *modem_endpoint_interface = NULL; static switch_endpoint_interface_t *modem_endpoint_interface = NULL;
@ -1241,6 +1251,7 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
globals.THREADCOUNT++; globals.THREADCOUNT++;
switch_mutex_unlock(globals.mutex); switch_mutex_unlock(globals.mutex);
if (switch_test_flag(modem, MODEM_FLAG_RUNNING)) {
switch_mutex_lock(modem->cond_mutex); switch_mutex_lock(modem->cond_mutex);
while (switch_test_flag(modem, MODEM_FLAG_RUNNING)) { while (switch_test_flag(modem, MODEM_FLAG_RUNNING)) {
@ -1314,6 +1325,7 @@ static void *SWITCH_THREAD_FUNC modem_thread(switch_thread_t *thread, void *obj)
if (switch_test_flag(modem, MODEM_FLAG_RUNNING)) { if (switch_test_flag(modem, MODEM_FLAG_RUNNING)) {
modem_close(modem); modem_close(modem);
} }
}
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);

View File

@ -146,7 +146,7 @@ typedef struct modem modem_t;
char *modem_state2name(int state); char *modem_state2name(int state);
int modem_close(struct modem *fm); int modem_close(struct modem *fm);
int modem_init(struct modem *fm, modem_control_handler_t control_handler); switch_status_t modem_init(struct modem *fm, modem_control_handler_t control_handler);
#endif //MODEM_SUPPORT #endif //MODEM_SUPPORT