BOOL modem_command(COM_HANDLE com_handle, const char* cmd) { char resp[128]; int i; for(i=0;i<=mdm_cmdretry;i++) { if(i) { lprintf(LOG_WARNING,"Retry #%u: sending modem command (%s) on %s", i, cmd, com_dev); lprintf(LOG_DEBUG,"Dropping DTR on %s", com_dev); if(!comLowerDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u lowering DTR on %s", COM_ERROR_VALUE, com_dev); SLEEP(dtr_delay); lprintf(LOG_DEBUG,"Raising DTR on %s", com_dev); if(!comRaiseDTR(com_handle)) lprintf(LOG_ERR,"ERROR %u raising DTR on %s", COM_ERROR_VALUE, com_dev); } if(!modem_send(com_handle, cmd)) { lprintf(LOG_ERR,"ERROR %u sending modem command (%s) on %s" ,COM_ERROR_VALUE, cmd, com_dev); continue; } if(modem_response(com_handle, resp, sizeof(resp))) break; } if(i<=mdm_cmdretry) { lprintf(LOG_INFO,"Modem Response: %s", resp); return TRUE; } lprintf(LOG_ERR,"Modem command (%s) failure on %s (%u attempts)", cmd, com_dev, i); return FALSE; }
int modem_close(void) { time_t start; conn_api.terminate=1; if((comGetModemStatus(com)&COM_DCD)==0) /* DCD already low */ goto CLOSEIT; /* TODO: We need a drain function */ SLEEP(500); if(!comLowerDTR(com)) goto CLOSEIT; start=time(NULL); while(time(NULL)-start <= 10) { if((comGetModemStatus(com)&COM_DCD) == 0) goto CLOSEIT; SLEEP(1000); } CLOSEIT: while(conn_api.input_thread_running || conn_api.output_thread_running) SLEEP(1); comClose(com); destroy_conn_buf(&conn_inbuf); destroy_conn_buf(&conn_outbuf); FREE_AND_NULL(conn_api.rd_buf); FREE_AND_NULL(conn_api.wr_buf); return(0); }
BOOL hangup_call(COM_HANDLE com_handle) { time_t start; int attempt; int mdm_status; if(!carrier_detect(com_handle))/* DCD already low */ return TRUE; lprintf(LOG_DEBUG,"Waiting for transmit buffer to empty"); SLEEP(dtr_delay); for(attempt=0; attempt<hangup_attempts; attempt++) { lprintf(LOG_INFO,"Dropping DTR (attempt #%d)", attempt+1); if(!comLowerDTR(com_handle)) { lprintf(LOG_ERR,"ERROR %u lowering DTR", COM_ERROR); continue; } lprintf(LOG_DEBUG,"Waiting for loss of Carrier Detect (DCD)"); start=time(NULL); while(time(NULL)-start <= dcd_timeout) { if(((mdm_status=modem_status(com_handle))&COM_DCD)==0) return TRUE; SLEEP(1000); } lprintf(LOG_ERR,"TIMEOUT waiting for DCD to drop (attempt #%d of %d)" ,attempt+1, hangup_attempts); lprintf(LOG_DEBUG,"Modem status: 0x%lX", mdm_status); } return FALSE; }
void modem_input_thread(void *args) { int rd; int buffered; size_t buffer; BOOL monitor_dsr=TRUE; SetThreadName("Modem Input"); conn_api.input_thread_running=1; if(args != NULL) { if((comGetModemStatus(com)&COM_DSR) == 0) monitor_dsr=FALSE; } while(com != COM_HANDLE_INVALID && !conn_api.terminate) { rd=comReadBuf(com, conn_api.rd_buf, conn_api.rd_buf_size, NULL, 100); buffered=0; while(buffered < rd) { pthread_mutex_lock(&(conn_inbuf.mutex)); buffer=conn_buf_wait_free(&conn_inbuf, rd-buffered, 100); buffered+=conn_buf_put(&conn_inbuf, conn_api.rd_buf+buffered, buffer); pthread_mutex_unlock(&(conn_inbuf.mutex)); } if(args==NULL) { if((comGetModemStatus(com)&COM_DCD) == 0) break; } else if(monitor_dsr) { if((comGetModemStatus(com)&COM_DSR) == 0) break; } } if(args != NULL) comLowerDTR(com); conn_api.input_thread_running=0; }