A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
Diff: tcp/tcpbuf.c
- Revision:
- 145:206bf0d073c7
- Parent:
- 79:f50e02fb5c94
- Child:
- 146:0fc66d610fd6
--- a/tcp/tcpbuf.c Sun May 12 17:17:49 2019 +0000 +++ b/tcp/tcpbuf.c Tue May 14 15:09:39 2019 +0000 @@ -9,11 +9,6 @@ static char* pBuffer; static char* p; -static bool currentPositionIsInBuffer() -{ - return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength; -} - void TcpBufStart(uint32_t position, int mss, char *pData) { currentPositionInMessage = 0; @@ -26,65 +21,16 @@ { return p - pBuffer; } +bool TcpBufFilled() +{ + return p - pBuffer >= bufferLength; +} void TcpBufAddChar(char c) { - if (currentPositionIsInBuffer()) *p++ = c; + //Add character if the current position is within the buffer + if (currentPositionInMessage >= bufferPositionInMessage && + currentPositionInMessage < bufferPositionInMessage + bufferLength) *p++ = c; + currentPositionInMessage++; } -void TcpBufFillChar(char c, int length) -{ - while (length > 0) - { - if (currentPositionIsInBuffer()) *p++ = c; - currentPositionInMessage++; - length--; - } -} -int TcpBufAddText(const char* text) -{ - const char* start = text; - while (*text) - { - if (currentPositionIsInBuffer()) *p++ = *text; - text++; - currentPositionInMessage++; - } - return text - start; -} -int TcpBufAddV(char *fmt, va_list argptr) -{ - int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required - char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null - vsprintf(text, fmt, argptr); //Fill the new buffer - return TcpBufAddText(text); //Add the text -} -int TcpBufAddF(char *fmt, ...) -{ - va_list argptr; - va_start(argptr, fmt); - int size = TcpBufAddV(fmt, argptr); - va_end(argptr); - return size; -} -void TcpBufAddData(const char* data, int length) -{ - while (length > 0) - { - if (currentPositionIsInBuffer()) *p++ = *data; - data++; - currentPositionInMessage++; - length--; - } -} -void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) -{ - startFunction(); - while (true) - { - int c = enumerateFunction(); - if (c == EOF) break; - if (currentPositionIsInBuffer()) *p++ = c; - currentPositionInMessage++; - } -}