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

Committer:
andrewboyson
Date:
Mon Nov 13 08:06:55 2017 +0000
Revision:
54:84ef2b29cf7e
Tidied HTTP files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 54:84ef2b29cf7e 1 #include "mbed.h"
andrewboyson 54:84ef2b29cf7e 2 #include "http.h"
andrewboyson 54:84ef2b29cf7e 3 #include "io.h"
andrewboyson 54:84ef2b29cf7e 4
andrewboyson 54:84ef2b29cf7e 5 static int currentPositionInMessage;
andrewboyson 54:84ef2b29cf7e 6 static int bufferPositionInMessage;
andrewboyson 54:84ef2b29cf7e 7 static int bufferLength;
andrewboyson 54:84ef2b29cf7e 8 static char* pBuffer;
andrewboyson 54:84ef2b29cf7e 9 static char* p;
andrewboyson 54:84ef2b29cf7e 10
andrewboyson 54:84ef2b29cf7e 11 static bool currentPositionIsInBuffer()
andrewboyson 54:84ef2b29cf7e 12 {
andrewboyson 54:84ef2b29cf7e 13 return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength;
andrewboyson 54:84ef2b29cf7e 14 }
andrewboyson 54:84ef2b29cf7e 15
andrewboyson 54:84ef2b29cf7e 16 void TcpBufStart(int position, int mss, char *pData)
andrewboyson 54:84ef2b29cf7e 17 {
andrewboyson 54:84ef2b29cf7e 18 currentPositionInMessage = 0;
andrewboyson 54:84ef2b29cf7e 19 bufferPositionInMessage = position;
andrewboyson 54:84ef2b29cf7e 20 bufferLength = mss;
andrewboyson 54:84ef2b29cf7e 21 pBuffer = pData;
andrewboyson 54:84ef2b29cf7e 22 p = pData;
andrewboyson 54:84ef2b29cf7e 23 }
andrewboyson 54:84ef2b29cf7e 24 int TcpBufLength()
andrewboyson 54:84ef2b29cf7e 25 {
andrewboyson 54:84ef2b29cf7e 26 return p - pBuffer;
andrewboyson 54:84ef2b29cf7e 27 }
andrewboyson 54:84ef2b29cf7e 28
andrewboyson 54:84ef2b29cf7e 29 void TcpBufAddChar(char c)
andrewboyson 54:84ef2b29cf7e 30 {
andrewboyson 54:84ef2b29cf7e 31 if (currentPositionIsInBuffer()) *p++ = c;
andrewboyson 54:84ef2b29cf7e 32 currentPositionInMessage++;
andrewboyson 54:84ef2b29cf7e 33 }
andrewboyson 54:84ef2b29cf7e 34 void TcpBufFillChar(char c, int length)
andrewboyson 54:84ef2b29cf7e 35 {
andrewboyson 54:84ef2b29cf7e 36 while (length > 0)
andrewboyson 54:84ef2b29cf7e 37 {
andrewboyson 54:84ef2b29cf7e 38 if (currentPositionIsInBuffer()) *p++ = c;
andrewboyson 54:84ef2b29cf7e 39 currentPositionInMessage++;
andrewboyson 54:84ef2b29cf7e 40 length--;
andrewboyson 54:84ef2b29cf7e 41 }
andrewboyson 54:84ef2b29cf7e 42 }
andrewboyson 54:84ef2b29cf7e 43 int TcpBufAddText(const char* text)
andrewboyson 54:84ef2b29cf7e 44 {
andrewboyson 54:84ef2b29cf7e 45 const char* start = text;
andrewboyson 54:84ef2b29cf7e 46 while (*text)
andrewboyson 54:84ef2b29cf7e 47 {
andrewboyson 54:84ef2b29cf7e 48 if (currentPositionIsInBuffer()) *p++ = *text;
andrewboyson 54:84ef2b29cf7e 49 text++;
andrewboyson 54:84ef2b29cf7e 50 currentPositionInMessage++;
andrewboyson 54:84ef2b29cf7e 51 }
andrewboyson 54:84ef2b29cf7e 52 return text - start;
andrewboyson 54:84ef2b29cf7e 53 }
andrewboyson 54:84ef2b29cf7e 54 int TcpBufAddV(char *fmt, va_list argptr)
andrewboyson 54:84ef2b29cf7e 55 {
andrewboyson 54:84ef2b29cf7e 56 int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
andrewboyson 54:84ef2b29cf7e 57 char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
andrewboyson 54:84ef2b29cf7e 58 vsprintf(text, fmt, argptr); //Fill the new buffer
andrewboyson 54:84ef2b29cf7e 59 return TcpBufAddText(text); //Add the text
andrewboyson 54:84ef2b29cf7e 60 }
andrewboyson 54:84ef2b29cf7e 61 int TcpBufAddF(char *fmt, ...)
andrewboyson 54:84ef2b29cf7e 62 {
andrewboyson 54:84ef2b29cf7e 63 va_list argptr;
andrewboyson 54:84ef2b29cf7e 64 va_start(argptr, fmt);
andrewboyson 54:84ef2b29cf7e 65 int size = TcpBufAddV(fmt, argptr);
andrewboyson 54:84ef2b29cf7e 66 va_end(argptr);
andrewboyson 54:84ef2b29cf7e 67 return size;
andrewboyson 54:84ef2b29cf7e 68 }
andrewboyson 54:84ef2b29cf7e 69 void TcpBufAddData(const char* data, int length)
andrewboyson 54:84ef2b29cf7e 70 {
andrewboyson 54:84ef2b29cf7e 71 while (length > 0)
andrewboyson 54:84ef2b29cf7e 72 {
andrewboyson 54:84ef2b29cf7e 73 if (currentPositionIsInBuffer()) *p++ = *data;
andrewboyson 54:84ef2b29cf7e 74 data++;
andrewboyson 54:84ef2b29cf7e 75 currentPositionInMessage++;
andrewboyson 54:84ef2b29cf7e 76 length--;
andrewboyson 54:84ef2b29cf7e 77 }
andrewboyson 54:84ef2b29cf7e 78 }
andrewboyson 54:84ef2b29cf7e 79 void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void))
andrewboyson 54:84ef2b29cf7e 80 {
andrewboyson 54:84ef2b29cf7e 81 startFunction();
andrewboyson 54:84ef2b29cf7e 82 while (true)
andrewboyson 54:84ef2b29cf7e 83 {
andrewboyson 54:84ef2b29cf7e 84 int c = enumerateFunction();
andrewboyson 54:84ef2b29cf7e 85 if (c == EOF) break;
andrewboyson 54:84ef2b29cf7e 86 if (currentPositionIsInBuffer()) *p++ = c;
andrewboyson 54:84ef2b29cf7e 87 currentPositionInMessage++;
andrewboyson 54:84ef2b29cf7e 88 }
andrewboyson 54:84ef2b29cf7e 89 }