uint8_t ESP_ReadCharsUntil(uint8_t *buf, size_t bufSize, uint8_t sentinelChar, uint16_t timeoutMs) { uint8_t ch; uint8_t res = ERR_OK; if (bufSize<=1) { return ERR_OVERRUN; /* buffer to small */ } buf[0] = '\0'; buf[bufSize-1] = '\0'; /* always terminate */ bufSize--; for(;;) { /* breaks */ if (bufSize==0) { res = ERR_OVERRUN; break; } if (AS2_GetCharsInRxBuf()>0) { (void)AS2_RecvChar(&ch); *buf = ch; buf++; bufSize--; if (ch==sentinelChar) { *buf = '\0'; /* terminate string */ break; /* sentinel found */ } } else { if (timeoutMs>10) { WAIT1_WaitOSms(5); timeoutMs -= 5; } else { res = ERR_NOTAVAIL; /* timeout */ break; } } } return res; }
static uint8_t ReadIntoIPDBuffer(uint8_t *buf, size_t bufSize, uint8_t *p, uint16_t msgSize, uint16_t msTimeout, const CLS1_StdIOType *io) { uint8_t ch; size_t nofInBuf; int timeout; nofInBuf = p-buf; bufSize -= nofInBuf; /* take into account what we already have in buffer */ timeout = msTimeout; while (msgSize>0 && bufSize>0) { if (AS2_GetCharsInRxBuf()>0) { (void)AS2_RecvChar(&ch); *p = ch; if (io!=NULL) { /* copy on console */ io->stdOut(ch); } p++; *p = '\0'; /* terminate */ nofInBuf++; msgSize--; bufSize--; } else { /* check in case we recveive less characters than expected, happens for POST? */ if (nofInBuf>6 && UTIL1_strncmp(&p[-6], "\r\nOK\r\n", sizeof("\r\nOK\r\n")-1)==0) { break; } else { timeout -= 10; WAIT1_WaitOSms(10); if (timeout<0) { return ERR_BUSY; } } } } return ERR_OK; }
static uint8_t RxResponse(unsigned char *rxBuf, size_t rxBufLength, unsigned char *expectedTail, uint16_t msTimeout) { unsigned char ch; uint8_t res = ERR_OK; unsigned char *p; if (rxBufLength < sizeof("x\r\n")) { return ERR_OVERFLOW; /* not enough space in buffer */ } p = rxBuf; p[0] = '\0'; for(;;) { /* breaks */ if (msTimeout == 0) { break; /* will decide outside of loop if it is a timeout. */ } else if (rxBufLength == 0) { res = ERR_OVERFLOW; /* not enough space in buffer */ break; } else if (AS2_GetCharsInRxBuf() > 0) { #if 0 if (AS2_RecvChar(&ch) != ERR_OK) { res = ERR_RXEMPTY; break; } #else /* might get an overrun OVERRUN_ERR error here? Ignoring error for now */ (void)AS2_RecvChar(&ch); #endif *p++ = ch; *p = '\0'; /* always terminate */ rxBufLength--; } else if (expectedTail!=NULL && expectedTail[0]!='\0' && UTIL1_strtailcmp(rxBuf, expectedTail) == 0) { break; /* finished */ } else { WAIT1_WaitOSms(1); msTimeout--; } } /* for */ if (msTimeout==0) { /* timeout! */ if (expectedTail[0] != '\0' /* timeout, and we expected something: an error for sure */ || rxBuf[0] == '\0' /* timeout, did not know what to expect, but received nothing? There has to be a response. */ ) { res = ERR_FAULT; } } return res; }
uint8_t ESP_SendStr(const uint8_t *str, CLS1_ConstStdIOType *io) { uint8_t buf[32]; uint8_t rxBuf[48]; uint8_t res; uint16_t timeoutMs; #define RX_TIMEOUT_MS 3000 AS2_TComData ch; UTIL1_strcpy(buf, sizeof(buf), str); UTIL1_strcat(buf, sizeof(buf), "\r\n"); res = ESP_SendATCommand(buf, rxBuf, sizeof(rxBuf), NULL, ESP_DEFAULT_TIMEOUT_MS, io); timeoutMs = 0; while(timeoutMs<RX_TIMEOUT_MS) { WAIT1_WaitOSms(100); timeoutMs += 100; while (AS2_GetCharsInRxBuf()>0) { (void)AS2_RecvChar(&ch); CLS1_SendChar(ch); } } return ERR_OK; }
/* ** =================================================================== ** Method : AS2_RecvBlock (component AsynchroSerial) ** Description : ** If any data is received, this method returns the block of ** the data and its length (and incidental error), otherwise it ** returns an error code (it does not wait for data). ** This method is available only if non-zero length of the ** input buffer is defined and the receiver property is enabled. ** If less than requested number of characters is received only ** the available data is copied from the receive buffer to the ** user specified destination. The value ERR_EXEMPTY is ** returned and the value of variable pointed by the Rcv ** parameter is set to the number of received characters. ** Parameters : ** NAME - DESCRIPTION ** * Ptr - Pointer to the block of received data ** Size - Size of the block ** * Rcv - Pointer to real number of the received data ** Returns : ** --- - Error code, possible codes: ** ERR_OK - OK ** ERR_SPEED - This device does not work in ** the active speed mode ** ERR_RXEMPTY - The receive buffer didn't ** contain the requested number of data. Only ** available data has been returned. ** ERR_COMMON - common error occurred (the ** GetError method can be used for error ** specification) ** =================================================================== */ byte AS2_RecvBlock(AS2_TComData *Ptr, word Size, word *Rcv) { register word count; /* Number of received chars */ register byte result = ERR_OK; /* Last error */ for (count = 0x00U; count < Size; count++) { switch (AS2_RecvChar(Ptr++)) { /* Receive data and test the return value*/ case ERR_RXEMPTY: /* No data in the buffer */ if (result == ERR_OK) { /* If no receiver error reported */ result = ERR_RXEMPTY; /* Return info that requested number of data is not available */ } *Rcv = count; /* Return number of received chars */ return result; case ERR_COMMON: /* Receiver error reported */ result = ERR_COMMON; /* Return info that an error was detected */ break; default: break; } } *Rcv = count; /* Return number of received chars */ return result; /* OK */ }