📄 smodlib.c
字号:
// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), allbuf, strlen(allbuf));// write( GetPortFD(), buf, sizeof(buf));// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); CHECK_ABORT_0; SET_ALARM(MsgExchTimeOut); usleep(24000000/conf->PortSpeed); do { ExchComplete = CheckBuffer( MsgExpect ); if (MState.AbortCommand) { RESET_ALARM; return 0; } // sleep long enough to receive at least 2 bytes. usleep(24000000/conf->PortSpeed); } while (!ExchComplete && !iTimedOut); if (iTimedOut) { RESET_ALARM; syslog(LOG_ERR,"Time out in MsgExchange!\n"); return -1; } RESET_ALARM; if (ExchComplete == -1) { syslog(LOG_ERR, "Error received! Could be incompatible modem!"); return 0; }; return 1;}int VModem::CheckBuffer( char* MsgStr, char* SavTo, int* Num, int DelOnNF ){ int retval = 0; int matchlen; char srchMsk[512]; char *ptr = MsgStr; char *ptrto, *found;// sigset_t usrmask, oldmask; ReadFromPort(); if (Num != NULL) *Num = 0;// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); Data[inData] = '\0'; // if (!DelOnNF && inData > 2)// {// syslog(LOG_DEBUG,"In buffer of Check Buffer:");// syslog(LOG_DEBUG,Data);// } matchlen = 7; // Sizeof --vvvvvvvvv if ((found = memstr(Data, ERROR_CMD, inData)) == NULL) { while ( *ptr != '\0' ) { // Extract the new line to search... ptrto = srchMsk; while (*ptr != '\0' && *ptr != '|') { *ptrto = *ptr; ptrto++; ptr++; } // Append cr lf to the end of mask... *ptrto++ = '\015'; *ptrto++ = '\012'; *ptrto = '\0'; if (*ptr == '|') ptr++; matchlen = strlen(srchMsk); if ((found = memstr(Data, srchMsk, inData)) != NULL) { retval = 1; break; } } } else retval = -1; // Save all info up to the found string if (SavTo != NULL) { int size; if (found == NULL) { size = ( inData > 100 )?inData - 100: 0; } else { size = (found - Data); } for(*Num = 0; (*Num) < size; (*Num)++) { SavTo[*Num] = Data[*Num]; } } // Remove all info in the buffer up to this message if ( found != NULL ) { int size = matchlen + ( found - Data ); if ( size > 1 ) { Data[size-2] = '\0'; syslog(LOG_DEBUG,"Received:"); syslog(LOG_DEBUG,found); // Surgical operation, removing the message: // If you are brave, you can use strncpy ;-) for (int i = 0; i < (inData - size); i++) { Data[i] = Data[i+size]; } inData -= size; } else { syslog(LOG_ERR,"Detect line length is not correct!"); } } else { if ( Num != NULL && *Num > 0 && DelOnNF) { for (int i = 0; i < (inData - (*Num)); i++) { Data[i] = Data[i + *Num]; } inData -= (*Num); } } // if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// {// syslog(LOG_ERR, "Unable to restore a signal mask!");// } return retval;};// Returns 0 - some fatal error// -1 - time out// 1 - success// -2 - aborted (by DTMF signal, etc.)int VModem::GrabAFile( char* FName, char* MsgSnd, char* MsgExpect, char* MsgUntil ){ PREP_ALARM;// sigset_t usrmask, oldmask; char datbuf[BUFFER_SIZE+1]; char newline[3] = {'\015','\012','\0'}; int ExchComplete = 0; char allbuf[256]; int sfp; MState.StopTransmit = 0; MState.Recording = 1; sfp = creat(FName, 0644); if ( sfp == -1 ) { syslog(LOG_ERR, "Unable to create the file:" ); syslog(LOG_ERR, FName ); return 0; } strcpy(allbuf, MsgSnd); strcat(allbuf, newline); iTimedOut = 0; CHECK_ABORT_0; if (MState.StopTransmit) return -2; FlushRecBuffer(); syslog(LOG_DEBUG,"Sending:"); syslog(LOG_DEBUG,MsgSnd);// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), allbuf, strlen(allbuf));// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); CHECK_ABORT_0; if (MState.StopTransmit) return -2; SET_ALARM(MsgExchTimeOut); usleep(24000000/conf->PortSpeed); do { ExchComplete = CheckBuffer( MsgExpect ); if (MState.AbortCommand || MState.StopTransmit) { RESET_ALARM; close( sfp ); return -2; } // sleep long enough to receive at least 2 bytes. usleep(24000000/conf->PortSpeed); } while (!ExchComplete && !iTimedOut); if (iTimedOut) { RESET_ALARM; syslog(LOG_ERR,"Time out in MsgExchange!\n"); close( sfp ); return -1; } RESET_ALARM; if (ExchComplete == -1) { syslog(LOG_ERR, "Error received! Could be incompatible modem!"); close( sfp ); return 0; } // We should have received CONNECT by now. Get all the data from the // modem and write to the file. int AbortTransfer = 0; int AbortSent = 0; do { int bytesRec; ExchComplete = CheckBuffer( MsgUntil, datbuf, &bytesRec ); if (bytesRec != 0) {// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// sigaddset(&usrmask, SIGURG );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( sfp, datbuf, bytesRec); if ((MState.AbortCommand || MState.StopTransmit) && !AbortSent) { char done[2] = { DLE, ETX }; AbortSent = 1; write( GetPortFD(), done, 2); }// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); } if (inDTMF) { AbortTransfer = ProcessDTMF(); } // sleep long enough to receive at least 2 bytes. usleep(24000000/conf->PortSpeed); } while (!ExchComplete && !AbortTransfer); close( sfp ); return 1;}int VModem::ProcessDTMF(){// sigset_t usrmask, oldmask; char localDTMF[DTMF_SIZE]; int inlocalDTMF; int retval = 0;// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); inlocalDTMF = inDTMF; memcpy(localDTMF, DTMF, inDTMF); inDTMF = 0;// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); for (int i = 0; i < inlocalDTMF; i++) { retval = ProcessDTMFChar( localDTMF[i] ); if (retval) break; } return retval;}int VModem::ProcessDTMFChar(char DTM){// sigset_t usrmask, oldmask; char buf[256]; int retval = 0; sprintf( buf, "Received DTMF signal:'%c'",DTM ); syslog(LOG_DEBUG,buf); switch (DTM) { case 'q': case 's': // sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), "!", 1); // Just abort reception :-)// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); if (inPubDTMF >= 0 && inPubDTMF < PUBDTMFSIZE) PubDTMF[inPubDTMF++] = DTM; break; case ETX: retval = 1; break; case '#': MState.StopTransmit = 1; case '*': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'A': case 'B': case 'C': case 'D': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h': if (inPubDTMF >= 0 && inPubDTMF < PUBDTMFSIZE) PubDTMF[inPubDTMF++] = DTM; if (!MState.Recording && !MState.IgnoreDTMF) { MState.StopTransmit = 1; } break; default: break; } return retval;}int VModem::GetPubDTMF(char* ARRAY){ ProcessDTMF(); /* Grab additional DTMF characters */ for(int i=0; i<inPubDTMF; i++) ARRAY[i]=PubDTMF[i]; return inPubDTMF;}//// Double all DLE's in the buffer and remove frames damaged by// USR bugs.//void ProcessBuffer(unsigned char* datbuf, int& bytesRec, int LastRec, unsigned char* sndbuf, int& bytesSnd, int CheckGSM){ int frameptr = 0; int srcptr = 0; int dstptr = 0; while ( frameptr + 37 < bytesRec ) { if (CheckGSM) { // Check completeness of GSM Frame while(!((datbuf[frameptr+36] == 0xA5 && datbuf[frameptr+37] == 0xA5) && (datbuf[frameptr] == 0xB6 && datbuf[frameptr+1] == 0xB6 || datbuf[frameptr] == 0xFE && datbuf[frameptr+1] == 0xFE))) { frameptr++; // Just check that we did not go over board if ( frameptr + 38 >= bytesRec ) break; } if ( frameptr + 38 >= bytesRec ) break; } // Transfer all the data... for ( srcptr = frameptr; srcptr < frameptr + 38; srcptr++ ) { sndbuf[dstptr++] = datbuf[srcptr]; if ( datbuf[srcptr] == DLE ) { sndbuf[dstptr++] = DLE; } } frameptr += 38; } if (srcptr < bytesRec) memmove(datbuf, &(datbuf[srcptr]), bytesRec - srcptr); bytesRec = bytesRec - srcptr; bytesSnd = dstptr; if ( LastRec == 0 ) bytesRec = 0;};// Returns 0 - some fatal error// -1 - time out// 1 - success// -2 - aborted (by DTMF signal, etc.)int VModem::SendAFile( char* FName, char* MsgSnd, char* MsgExpect, char* MsgUntil ){ PREP_ALARM;// sigset_t usrmask, oldmask; char datbuf[BUFFER_SIZE/4]; char sndbuf[BUFFER_SIZE/2]; char newline[3] = {'\015','\012','\0'}; int ExchComplete = 0; char allbuf[256]; int sfp; MState.StopTransmit = 0; MState.Recording = 0; sfp = open(FName, O_RDONLY); if ( sfp == -1 ) { syslog(LOG_ERR, "Unable to open the file:" ); syslog(LOG_ERR, FName ); return 0; } strcpy(allbuf, MsgSnd); strcat(allbuf, newline); iTimedOut = 0; FlushRecBuffer(); syslog(LOG_DEBUG,"Sending:"); syslog(LOG_DEBUG,MsgSnd); ABORT_CLOSE;// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), allbuf, strlen(allbuf));// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); ABORT_CLOSE; SET_ALARM(MsgExchTimeOut); usleep(24000000/conf->PortSpeed); do { ExchComplete = CheckBuffer( MsgExpect ); if (MState.AbortCommand || MState.StopTransmit) { RESET_ALARM; close(sfp); return 0; } // sleep long enough to receive at least 2 bytes. usleep(24000000/conf->PortSpeed); } while (!ExchComplete && !iTimedOut); if (iTimedOut) { RESET_ALARM; syslog(LOG_ERR,"Time out in MsgExchange!\n"); close( sfp ); return -1; } RESET_ALARM; ABORT_CLOSE; if (ExchComplete == -1) { syslog(LOG_ERR, "Error received! Could be incompatible modem!"); close( sfp ); return 0; } // We should have received CONNECT by now. Get all the data from the // file and write to the modem. int AbortTransfer = 0; int bytesRec = 0; int bytesSnd; int inBuffer = 0; int complete = 0; do { if ( inBuffer > 38 ) inBuffer = 0; // Flush overfilled buffer... bytesRec = read(sfp, &(datbuf[inBuffer]), sizeof(datbuf) - inBuffer - 1); inBuffer += bytesRec; ProcessBuffer((unsigned char*)datbuf, inBuffer, bytesRec, (unsigned char*)sndbuf, bytesSnd, conf->CheckGSM); if (bytesSnd != 0 && !(MState.AbortCommand || MState.StopTransmit)) {// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// sigaddset(&usrmask, SIGURG );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), sndbuf, bytesSnd);// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); } else { if ( ! complete ) { char done[2] = { DLE, ETX };// sigemptyset( &usrmask );// sigaddset(&usrmask, SIGUSR1 );// sigaddset(&usrmask, SIGURG );// if (sigprocmask( SIG_BLOCK,&usrmask, &oldmask) < 0)// syslog(LOG_ERR, "Unable to set a signal mask!"); write( GetPortFD(), done, 2);// if (sigprocmask( SIG_SETMASK,&oldmask, NULL) < 0)// syslog(LOG_ERR, "Unable to restore a signal mask!"); complete = 1; } } ExchComplete = CheckBuffer( MsgUntil ); if (inDTMF) { AbortTransfer = ProcessDTMF(); } } while ( !ExchComplete && !AbortTransfer); close( sfp ); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -