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
tcp/tcpbuf.c@79:f50e02fb5c94, 2018-11-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Nov 11 15:44:23 2018 +0000
- Revision:
- 79:f50e02fb5c94
- Parent:
- 61:aad055f1b0d1
- Child:
- 145:206bf0d073c7
Added RTO support to TCP and fixed a problem with polling of different IP versions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdbool.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | #include <stdio.h> |
andrewboyson | 61:aad055f1b0d1 | 3 | |
andrewboyson | 54:84ef2b29cf7e | 4 | #include "http.h" |
andrewboyson | 54:84ef2b29cf7e | 5 | |
andrewboyson | 79:f50e02fb5c94 | 6 | static uint32_t currentPositionInMessage; |
andrewboyson | 79:f50e02fb5c94 | 7 | static uint32_t bufferPositionInMessage; |
andrewboyson | 54:84ef2b29cf7e | 8 | static int bufferLength; |
andrewboyson | 54:84ef2b29cf7e | 9 | static char* pBuffer; |
andrewboyson | 54:84ef2b29cf7e | 10 | static char* p; |
andrewboyson | 54:84ef2b29cf7e | 11 | |
andrewboyson | 54:84ef2b29cf7e | 12 | static bool currentPositionIsInBuffer() |
andrewboyson | 54:84ef2b29cf7e | 13 | { |
andrewboyson | 54:84ef2b29cf7e | 14 | return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength; |
andrewboyson | 54:84ef2b29cf7e | 15 | } |
andrewboyson | 54:84ef2b29cf7e | 16 | |
andrewboyson | 79:f50e02fb5c94 | 17 | void TcpBufStart(uint32_t position, int mss, char *pData) |
andrewboyson | 54:84ef2b29cf7e | 18 | { |
andrewboyson | 54:84ef2b29cf7e | 19 | currentPositionInMessage = 0; |
andrewboyson | 54:84ef2b29cf7e | 20 | bufferPositionInMessage = position; |
andrewboyson | 54:84ef2b29cf7e | 21 | bufferLength = mss; |
andrewboyson | 54:84ef2b29cf7e | 22 | pBuffer = pData; |
andrewboyson | 54:84ef2b29cf7e | 23 | p = pData; |
andrewboyson | 54:84ef2b29cf7e | 24 | } |
andrewboyson | 54:84ef2b29cf7e | 25 | int TcpBufLength() |
andrewboyson | 54:84ef2b29cf7e | 26 | { |
andrewboyson | 54:84ef2b29cf7e | 27 | return p - pBuffer; |
andrewboyson | 54:84ef2b29cf7e | 28 | } |
andrewboyson | 54:84ef2b29cf7e | 29 | |
andrewboyson | 54:84ef2b29cf7e | 30 | void TcpBufAddChar(char c) |
andrewboyson | 54:84ef2b29cf7e | 31 | { |
andrewboyson | 54:84ef2b29cf7e | 32 | if (currentPositionIsInBuffer()) *p++ = c; |
andrewboyson | 54:84ef2b29cf7e | 33 | currentPositionInMessage++; |
andrewboyson | 54:84ef2b29cf7e | 34 | } |
andrewboyson | 54:84ef2b29cf7e | 35 | void TcpBufFillChar(char c, int length) |
andrewboyson | 54:84ef2b29cf7e | 36 | { |
andrewboyson | 54:84ef2b29cf7e | 37 | while (length > 0) |
andrewboyson | 54:84ef2b29cf7e | 38 | { |
andrewboyson | 54:84ef2b29cf7e | 39 | if (currentPositionIsInBuffer()) *p++ = c; |
andrewboyson | 54:84ef2b29cf7e | 40 | currentPositionInMessage++; |
andrewboyson | 54:84ef2b29cf7e | 41 | length--; |
andrewboyson | 54:84ef2b29cf7e | 42 | } |
andrewboyson | 54:84ef2b29cf7e | 43 | } |
andrewboyson | 54:84ef2b29cf7e | 44 | int TcpBufAddText(const char* text) |
andrewboyson | 54:84ef2b29cf7e | 45 | { |
andrewboyson | 54:84ef2b29cf7e | 46 | const char* start = text; |
andrewboyson | 54:84ef2b29cf7e | 47 | while (*text) |
andrewboyson | 54:84ef2b29cf7e | 48 | { |
andrewboyson | 54:84ef2b29cf7e | 49 | if (currentPositionIsInBuffer()) *p++ = *text; |
andrewboyson | 54:84ef2b29cf7e | 50 | text++; |
andrewboyson | 54:84ef2b29cf7e | 51 | currentPositionInMessage++; |
andrewboyson | 54:84ef2b29cf7e | 52 | } |
andrewboyson | 54:84ef2b29cf7e | 53 | return text - start; |
andrewboyson | 54:84ef2b29cf7e | 54 | } |
andrewboyson | 54:84ef2b29cf7e | 55 | int TcpBufAddV(char *fmt, va_list argptr) |
andrewboyson | 54:84ef2b29cf7e | 56 | { |
andrewboyson | 54:84ef2b29cf7e | 57 | int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required |
andrewboyson | 54:84ef2b29cf7e | 58 | char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 54:84ef2b29cf7e | 59 | vsprintf(text, fmt, argptr); //Fill the new buffer |
andrewboyson | 54:84ef2b29cf7e | 60 | return TcpBufAddText(text); //Add the text |
andrewboyson | 54:84ef2b29cf7e | 61 | } |
andrewboyson | 54:84ef2b29cf7e | 62 | int TcpBufAddF(char *fmt, ...) |
andrewboyson | 54:84ef2b29cf7e | 63 | { |
andrewboyson | 54:84ef2b29cf7e | 64 | va_list argptr; |
andrewboyson | 54:84ef2b29cf7e | 65 | va_start(argptr, fmt); |
andrewboyson | 54:84ef2b29cf7e | 66 | int size = TcpBufAddV(fmt, argptr); |
andrewboyson | 54:84ef2b29cf7e | 67 | va_end(argptr); |
andrewboyson | 54:84ef2b29cf7e | 68 | return size; |
andrewboyson | 54:84ef2b29cf7e | 69 | } |
andrewboyson | 54:84ef2b29cf7e | 70 | void TcpBufAddData(const char* data, int length) |
andrewboyson | 54:84ef2b29cf7e | 71 | { |
andrewboyson | 54:84ef2b29cf7e | 72 | while (length > 0) |
andrewboyson | 54:84ef2b29cf7e | 73 | { |
andrewboyson | 54:84ef2b29cf7e | 74 | if (currentPositionIsInBuffer()) *p++ = *data; |
andrewboyson | 54:84ef2b29cf7e | 75 | data++; |
andrewboyson | 54:84ef2b29cf7e | 76 | currentPositionInMessage++; |
andrewboyson | 54:84ef2b29cf7e | 77 | length--; |
andrewboyson | 54:84ef2b29cf7e | 78 | } |
andrewboyson | 54:84ef2b29cf7e | 79 | } |
andrewboyson | 54:84ef2b29cf7e | 80 | void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) |
andrewboyson | 54:84ef2b29cf7e | 81 | { |
andrewboyson | 54:84ef2b29cf7e | 82 | startFunction(); |
andrewboyson | 54:84ef2b29cf7e | 83 | while (true) |
andrewboyson | 54:84ef2b29cf7e | 84 | { |
andrewboyson | 54:84ef2b29cf7e | 85 | int c = enumerateFunction(); |
andrewboyson | 54:84ef2b29cf7e | 86 | if (c == EOF) break; |
andrewboyson | 54:84ef2b29cf7e | 87 | if (currentPositionIsInBuffer()) *p++ = c; |
andrewboyson | 54:84ef2b29cf7e | 88 | currentPositionInMessage++; |
andrewboyson | 54:84ef2b29cf7e | 89 | } |
andrewboyson | 54:84ef2b29cf7e | 90 | } |