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_connect(struct bbslist *bbs) { int ret; char respbuf[1024]; init_uifc(TRUE, TRUE); if(bbs->conn_type == CONN_TYPE_SERIAL) { if((com=comOpen(bbs->addr)) == COM_HANDLE_INVALID) { uifcmsg("Cannot Open Port", "`Cannot Open Port`\n\n" "Cannot open the specified serial device.\n"); conn_api.terminate=-1; return(-1); } if(bbs->bpsrate) { if(!comSetBaudRate(com, bbs->bpsrate)) { uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n" "Cannot open the specified serial device.\n"); conn_api.terminate=-1; comClose(com); return(-1); } } if(!comRaiseDTR(com)) { uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n" "comRaiseDTR() returned an error.\n"); conn_api.terminate=-1; comClose(com); return(-1); } } else { if((com=comOpen(settings.mdm.device_name)) == COM_HANDLE_INVALID) { uifcmsg("Cannot Open Modem", "`Cannot Open Modem`\n\n" "Cannot open the specified modem device.\n"); conn_api.terminate=-1; return(-1); } if(settings.mdm.com_rate) { if(!comSetBaudRate(com, settings.mdm.com_rate)) { uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n" "Cannot open the specified modem device.\n"); conn_api.terminate=-1; comClose(com); return(-1); } } if(!comRaiseDTR(com)) { uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n" "comRaiseDTR() returned an error.\n"); conn_api.terminate=-1; comClose(com); return(-1); } /* drain keyboard input to avoid accidental cancel */ while(kbhit()) getch(); uifc.pop("Initializing..."); comWriteString(com, settings.mdm.init_string); comWriteString(com, "\r"); /* Wait for "OK" */ while(1) { if((ret=modem_response(respbuf, sizeof(respbuf), 5))!=0) { modem_close(); uifc.pop(NULL); if(ret<0) uifcmsg("Modem Not Responding", "`Modem Not Responding`\n\n" "The modem did not respond to the initializtion string\n" "Check your init string and phone number.\n"); conn_api.terminate=-1; return(-1); } if(strstr(respbuf, settings.mdm.init_string)) /* Echo is on */ continue; break; } if(!strstr(respbuf, "OK")) { modem_close(); uifc.pop(NULL); uifcmsg(respbuf, "`Initialization Error`\n\n" "The modem did not respond favorably to your initialization string.\n"); conn_api.terminate=-1; return(-1); } uifc.pop(NULL); uifc.pop("Dialing..."); comWriteString(com, settings.mdm.dial_string); comWriteString(com, bbs->addr); comWriteString(com, "\r"); /* Wait for "CONNECT" */ while(1) { if((ret=modem_response(respbuf, sizeof(respbuf), 60))!=0) { modem_close(); uifc.pop(NULL); if(ret<0) uifcmsg(respbuf, "`No Answer`\n\n" "The modem did not connect within 60 seconds.\n"); conn_api.terminate=-1; return(-1); } if(strstr(respbuf, bbs->addr)) /* Dial command echoed */ continue; break; } if(!strstr(respbuf, "CONNECT")) { modem_close(); uifc.pop(NULL); uifcmsg(respbuf, "`Connection Failed`\n\n" "SyncTERM was unable to establish a connection.\n"); conn_api.terminate=-1; return(-1); } uifc.pop(NULL); uifc.pop(respbuf); SLEEP(1000); uifc.pop(NULL); } if(!create_conn_buf(&conn_inbuf, BUFFER_SIZE)) { conn_api.close(); return(-1); } if(!create_conn_buf(&conn_outbuf, BUFFER_SIZE)) { conn_api.close(); destroy_conn_buf(&conn_inbuf); return(-1); } if(!(conn_api.rd_buf=(unsigned char *)malloc(BUFFER_SIZE))) { conn_api.close(); destroy_conn_buf(&conn_inbuf); destroy_conn_buf(&conn_outbuf); return(-1); } conn_api.rd_buf_size=BUFFER_SIZE; if(!(conn_api.wr_buf=(unsigned char *)malloc(BUFFER_SIZE))) { conn_api.close(); destroy_conn_buf(&conn_inbuf); destroy_conn_buf(&conn_outbuf); FREE_AND_NULL(conn_api.rd_buf); return(-1); } conn_api.wr_buf_size=BUFFER_SIZE; if(bbs->conn_type == CONN_TYPE_SERIAL) { _beginthread(modem_output_thread, 0, (void *)-1); _beginthread(modem_input_thread, 0, (void *)-1); } else { _beginthread(modem_output_thread, 0, NULL); _beginthread(modem_input_thread, 0, NULL); } uifc.pop(NULL); return(0); }