/*! * This method reads and empties the input buffer of `_dataStream`. * It continues until it finds the specified prompt or until * the specified amount of time has elapsed. * The source data is converted from HEX and copied to the * buffer supplied. * The first letter of the prompt cannot be a valid Hex char. * It attempts to output the data it reads to `_diagStream`. * @param buffer The buffer to copy the data into. * @param size The size of `buffer`. * @param bytesStored The number of bytes copied is written to this parameter. * @param prompt The prompt to read until. * @param timeMS The time limit in milliseconds. * @return `true` if it found the specified prompt within the time * limit, otherwise `false`. */ bool Sodaq_WifiBee::readHexTillPrompt(uint8_t* buffer, const size_t size, size_t& bytesStored, const char* prompt, const uint32_t timeMS) { if (!_dataStream) { return false; } bool result = false; uint32_t startTS = millis(); size_t promptIndex = 0; size_t promptLen = strlen(prompt); size_t bufferIndex = 0; size_t streamCount = 0; bool even = false; while (!timedOut32(startTS, timeMS)) { if (available()) { startTS = millis(); char c = read(); diagPrint(c); streamCount++; if (bufferIndex < size) { buffer[bufferIndex] = c; bufferIndex++; } if (c == prompt[promptIndex]) { promptIndex++; if (promptIndex == promptLen) { result = true; bufferIndex = ((size - 1) < ((streamCount - promptLen) / 2)) ? (size - 1) : (streamCount - promptLen) / 2; break; } } else { promptIndex = 0; if (even) { _buffer[bufferIndex - 2] = HEX2BYTE(_buffer[bufferIndex - 2], _buffer[bufferIndex - 1]); bufferIndex--; } } even = !even; } else { _delay(10); } } bytesStored = bufferIndex; return result; }
unsigned int DecodeHex(const char* pData,BYTE* pOut,int inlen) { unsigned int decodesize = 0; if(inlen==0) inlen = (int)::strlen(pData); char *pBinPtr = const_cast<char*>(pData); char *end = pBinPtr+inlen; BYTE *pWrite = pOut; for(pBinPtr; pBinPtr < end; pBinPtr+=2) { if(!IsHexChar(*pBinPtr) || !IsHexChar(*(pBinPtr+1))) break; ++decodesize; *(pWrite++) = HEX2BYTE(*pBinPtr,*(pBinPtr+1)); } return decodesize; }
static unsigned int decodeHex(const char* pData,char* outbuf,const int buflen){ unsigned int decodesize = 0; int inlen = strlen(pData); char* nspbuf = new char[inlen]; memset(nspbuf, 0, inlen); for (int i = 0, j = 0; i < inlen; ++ i){ // no space if ( ' ' == pData[i] ){ ++ j; continue; } nspbuf[i-j] = pData[i]; } char chr1 = 0, chr2 = 0; char *pBinPtr = const_cast<char*>(nspbuf); char *end = pBinPtr+inlen; for (pBinPtr; pBinPtr < end && decodesize < buflen; pBinPtr += 2, ++ decodesize){ chr1 = getDimChar(*pBinPtr); chr2 = getDimChar(*(pBinPtr+1)); if(!chr1 || !chr2) break; *(outbuf++) = HEX2BYTE(chr1,chr2); } if(nspbuf) delete nspbuf; return decodesize; }