diff --git a/src/WiFiEsp.h b/src/WiFiEsp.h index 23f77bf..338943a 100644 --- a/src/WiFiEsp.h +++ b/src/WiFiEsp.h @@ -28,7 +28,7 @@ along with The Arduino WiFiEsp library. If not, see #include "WiFiEspClient.h" #include "WiFiEspServer.h" #include "utility/EspDrv.h" -#include "utility/RingBuffer.h" +#include "utility/WifiEspRingBuffer.h" #include "utility/debug.h" diff --git a/src/utility/EspDrv.cpp b/src/utility/EspDrv.cpp index 670a055..1889e3e 100644 --- a/src/utility/EspDrv.cpp +++ b/src/utility/EspDrv.cpp @@ -46,7 +46,7 @@ typedef enum Stream *EspDrv::espSerial; -RingBuffer EspDrv::ringBuf(32); +WifiEspRingBuffer EspDrv::ringBuf(32); // Array of data to cache the information related to the networks discovered char EspDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; @@ -797,12 +797,18 @@ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, co if(idx==NUMESPTAGS) { // end tag found - ringBuf.getStr(outStr, strlen(endTag)); + if( ringBuf.getLength() - strlen(endTag) <= outStrLen ) { + ringBuf.getStr(outStr, strlen(endTag)); - // read the remaining part of the response - readUntil(2000); + // read the remaining part of the response + readUntil(2000); - ret = true; + ret = true; + } else { + LOGERROR(F("Buffer overflow in sendCmdGet")); + } + + } else { @@ -871,7 +877,7 @@ int EspDrv::sendCmd(const __FlashStringHelper* cmd, int timeout, ...) va_list args; va_start (args, timeout); - vsnprintf_P (cmdBuf, CMD_BUFFER_SIZE, (char*)cmd, args); + vsnprintf (cmdBuf, CMD_BUFFER_SIZE, (char*)cmd, args); va_end (args); espEmptyBuf(); diff --git a/src/utility/EspDrv.h b/src/utility/EspDrv.h index cfb6408..41efedb 100644 --- a/src/utility/EspDrv.h +++ b/src/utility/EspDrv.h @@ -23,7 +23,7 @@ along with The Arduino WiFiEsp library. If not, see #include "IPAddress.h" -#include "RingBuffer.h" +#include "WifiEspRingBuffer.h" @@ -304,7 +304,7 @@ class EspDrv // the ring buffer is used to search the tags in the stream - static RingBuffer ringBuf; + static WifiEspRingBuffer ringBuf; //static int sendCmd(const char* cmd, int timeout=1000); diff --git a/src/utility/RingBuffer.cpp b/src/utility/WifiEspRingBuffer.cpp similarity index 80% rename from src/utility/RingBuffer.cpp rename to src/utility/WifiEspRingBuffer.cpp index 1d5b05b..2aa2540 100644 --- a/src/utility/RingBuffer.cpp +++ b/src/utility/WifiEspRingBuffer.cpp @@ -16,11 +16,11 @@ along with The Arduino WiFiEsp library. If not, see . --------------------------------------------------------------------*/ -#include "RingBuffer.h" +#include "WifiEspRingBuffer.h" #include -RingBuffer::RingBuffer(unsigned int size) +WifiEspRingBuffer::WifiEspRingBuffer(unsigned int size) { _size = size; // add one char to terminate the string @@ -29,20 +29,20 @@ RingBuffer::RingBuffer(unsigned int size) init(); } -RingBuffer::~RingBuffer() {} +WifiEspRingBuffer::~WifiEspRingBuffer() {} -void RingBuffer::reset() +void WifiEspRingBuffer::reset() { ringBufP = ringBuf; } -void RingBuffer::init() +void WifiEspRingBuffer::init() { ringBufP = ringBuf; memset(ringBuf, 0, _size+1); } -void RingBuffer::push(char c) +void WifiEspRingBuffer::push(char c) { *ringBufP = c; ringBufP++; @@ -52,7 +52,7 @@ void RingBuffer::push(char c) -bool RingBuffer::endsWith(const char* str) +bool WifiEspRingBuffer::endsWith(const char* str) { int findStrLen = strlen(str); @@ -77,9 +77,11 @@ bool RingBuffer::endsWith(const char* str) return true; } +unsigned int WifiEspRingBuffer::getLength() { + return ringBufP - ringBuf; +} - -void RingBuffer::getStr(char * destination, unsigned int skipChars) +void WifiEspRingBuffer::getStr(char * destination, unsigned int skipChars) { int len = ringBufP-ringBuf-skipChars; diff --git a/src/utility/RingBuffer.h b/src/utility/WifiEspRingBuffer.h similarity index 85% rename from src/utility/RingBuffer.h rename to src/utility/WifiEspRingBuffer.h index 76de9ca..613ceb3 100644 --- a/src/utility/RingBuffer.h +++ b/src/utility/WifiEspRingBuffer.h @@ -16,15 +16,15 @@ along with The Arduino WiFiEsp library. If not, see . --------------------------------------------------------------------*/ -#ifndef RingBuffer_h -#define RingBuffer_h +#ifndef WifiEspWifiEspRingBuffer_h +#define WifiEspWifiEspRingBuffer_h -class RingBuffer +class WifiEspRingBuffer { public: - RingBuffer(unsigned int size); - ~RingBuffer(); + WifiEspRingBuffer(unsigned int size); + ~WifiEspRingBuffer(); void reset(); void init(); @@ -32,6 +32,7 @@ class RingBuffer int getPos(); bool endsWith(const char* str); void getStr(char * destination, unsigned int skipChars); + unsigned int getLength(); private: diff --git a/src/utility/debug.h b/src/utility/debug.h index 9557a13..c2aeea4 100644 --- a/src/utility/debug.h +++ b/src/utility/debug.h @@ -28,7 +28,7 @@ along with The Arduino WiFiEsp library. If not, see // 3: INFO: errors, warnings and informational (default) // 4: DEBUG: errors, warnings, informational and debug -#define _ESPLOGLEVEL_ 3 +#define _ESPLOGLEVEL_ 4 #define LOGERROR(x) if(_ESPLOGLEVEL_>0) { Serial.print("[WiFiEsp] "); Serial.println(x); }