Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Sun May 12 17:17:49 2019 +0000
Parent:
143:8cec8f08dc54
Child:
145:206bf0d073c7
Commit message:
Tidied up tcp.

Changed in this revision

tcp/http/http.c Show annotated file Show diff for this revision Revisions of this file
tcp/http/http.h Show annotated file Show diff for this revision Revisions of this file
tcp/http/httpsend.c Show annotated file Show diff for this revision Revisions of this file
tcp/https/https.c Show annotated file Show diff for this revision Revisions of this file
tcp/https/https.h Show annotated file Show diff for this revision Revisions of this file
tcp/tcb.h Show annotated file Show diff for this revision Revisions of this file
tcp/tcp.h Show annotated file Show diff for this revision Revisions of this file
tcp/tcpbuf.h Show annotated file Show diff for this revision Revisions of this file
tcp/tcprecv.c Show annotated file Show diff for this revision Revisions of this file
tcp/tcpsend.c Show annotated file Show diff for this revision Revisions of this file
--- a/tcp/http/http.c	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/http/http.c	Sun May 12 17:17:49 2019 +0000
@@ -7,14 +7,15 @@
 #include    "log.h"
 #include    "led.h"
 #include "restart.h"
+#include "mstimer.h"
 
 bool HttpTrace = false;
 
 //Plumb into these from your html server
-void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete, uint32_t* pDelayUntil);
-void (*HttpReplyFunction  )(int todo);
+void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState);
+int  (*HttpReplyFunction  )(char* pState);
 
-void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete, uint32_t* pDelayUntil)
+void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState)
 {
     int lastRestartPoint = RestartPoint;
     RestartPoint = FAULT_POINT_HttpHandleRequest;
@@ -24,18 +25,22 @@
         LogF("HTTP  <<< %d (%u)\r\n", size, positionInRequestStream);
     }
     
-    HttpRequestFunction(size, pRequestStream, positionInRequestStream, pToDo, pPostComplete, pDelayUntil);
+    HttpRequestFunction(size, pRequestStream, positionInRequestStream, pState);
     
     RestartPoint = lastRestartPoint;
 }
-void HttpSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, int todo)
+int HttpSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, char* pState)
 {
     TcpBufStart(positionInReplyStream, mss, pReplyStream);
-    HttpReplyFunction(todo);
+    int status = HttpReplyFunction(pState); //0: not started; +1: started; -1: wait
+    if (status ==  0) return TCP_APP_NOT_STARTED;
+    if (status == -1) return TCP_APP_STARTED;
+    
     *pSize = TcpBufLength();
 
     if (HttpTrace)
     {
         LogF("HTTP  >>> %d (%d)\r\n", *pSize, positionInReplyStream);
     }
+    return *pSize < mss ? TCP_APP_FINISHED : TCP_APP_STARTED;
 }
--- a/tcp/http/http.h	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/http/http.h	Sun May 12 17:17:49 2019 +0000
@@ -17,6 +17,7 @@
 extern void   HttpAddInt32AsHex (int value);
 extern void   HttpAddInt64AsHex (int64_t value);
 extern void   HttpAddTm         (struct tm* ptm);
+extern int    HttpGetLength     (void);
 
 extern void   HttpOk(const char* contentType, const char* cacheControl, const char* lastModifiedDate, const char* lastModifiedTime);
 extern char*  HttpOkCookieName;
@@ -33,12 +34,12 @@
 extern int    HttpQueryValueAsInt(char* pValue);
 extern void   HttpQueryUnencode  (char* pValue);
 
-extern void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete, uint32_t* pDelayUntil);
-extern void (*HttpReplyFunction  )(int todo);
+extern void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState);
+extern int  (*HttpReplyFunction  )(char* pState);
 
 extern bool   HttpTrace;
-extern void   HttpHandleRequest(int   size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete, uint32_t* pDelayUntil);
-extern void   HttpSendReply    (int* pSize, char* pReplyStream,   uint32_t positionInReplyStream, uint16_t mss, int todo);
+extern void   HttpHandleRequest(int   size, char* pRequestStream, uint32_t positionInRequestStream, char* pState);
+extern int    HttpSendReply    (int* pSize, char* pReplyStream,   uint32_t positionInReplyStream, uint16_t mss, char* pState);
 
 extern void   HttpDateFromDateTime(const char* date, const char *ptime, char* ptext);
 extern void   HttpDateFromNow(char* pText);
--- a/tcp/http/httpsend.c	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/http/httpsend.c	Sun May 12 17:17:49 2019 +0000
@@ -1,6 +1,7 @@
 #include <time.h>
 #include "tcpbuf.h"
 
+int  HttpGetLength() { return TcpBufLength(); }
 void HttpAddChar  (char c)                                                      {        TcpBufAddChar(c);                                 }
 void HttpFillChar (char c, int length)                                          {        TcpBufFillChar(c, length);                        }
 int  HttpAddText  (const char* text)                                            { return TcpBufAddText(text);                              }
--- a/tcp/https/https.c	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/https/https.c	Sun May 12 17:17:49 2019 +0000
@@ -6,7 +6,8 @@
 #include    "net.h"
 #include    "log.h"
 #include    "led.h"
-#include  "restart.h"
+#include "restart.h"
+#include "mstimer.h"
 
 #define TLS_CONTENT_TYPE_ChangeCipher      20
 #define TLS_CONTENT_TYPE_Alert             21
@@ -29,7 +30,7 @@
 
 #define DO_SERVER_HELLO 100
 
-static void logHandshakeType(char handshakeType)
+/*static void logHandshakeType(char handshakeType)
 {
     switch (handshakeType)
     {
@@ -60,12 +61,12 @@
         default:                            LogF("%02hX", contentType); break;
     }
 }
-
+*/
 bool HttpsTrace = true;
 
-void HttpsHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo)
+void HttpsHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, void* pData)
 {
-    
+    /*
     if (HttpsTrace)
     {
         LogF("HTTPS <<< %d (%u)\r\n", size, positionInRequestStream);
@@ -90,13 +91,16 @@
             return;
     }
     //ECDHE-RSA-AES128-GCM-SHA256
+    */
 }
-static void sendServerHello()
+/*static void sendServerHello()
 {
     Log("     sending server hello\r\n");
 }
-void HttpsSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, int todo)
+*/
+bool HttpsSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, void* pData)
 {
+    /*
     TcpBufStart(positionInReplyStream, mss, pReplyStream);
     if (todo == DO_SERVER_HELLO) sendServerHello();
     *pSize = TcpBufLength();
@@ -105,4 +109,6 @@
     {
         LogF("HTTPS >>> %d (%d)\r\n", *pSize, positionInReplyStream);
     }
+    */
+    return TCP_APP_FINISHED; //0: not started; +1: started; -1: finished
 }
--- a/tcp/https/https.h	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/https/https.h	Sun May 12 17:17:49 2019 +0000
@@ -1,5 +1,5 @@
 #include <stdint.h>
 
 extern bool   HttpsTrace;
-extern void   HttpsHandleRequest(int   size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo);
-extern void   HttpsSendReply    (int* pSize, char* pReplyStream,   uint32_t positionInReplyStream, uint16_t mss, int todo);
+extern void   HttpsHandleRequest(int   size, char* pRequestStream, uint32_t positionInRequestStream, void* pAppData);
+extern int    HttpsSendReply    (int* pSize, char* pReplyStream,   uint32_t positionInReplyStream, uint16_t mss, void* pAppData);
--- a/tcp/tcb.h	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/tcb.h	Sun May 12 17:17:49 2019 +0000
@@ -9,6 +9,8 @@
 #define TCB_ESTABLISHED    2
 #define TCB_CLOSE_FIN_WAIT 3
 
+#define TCB_APP_STATE_BYTES 16
+
 struct tcb
 {
     int      state;
@@ -33,16 +35,8 @@
     uint32_t bytesAckdByRem;
     bool     sentFin;
     
-    /*
-    Communicates between application (HTTP) and TCP.
-    Application usually works out what to do from the first packet and so it stores that in todo to remember what it is doing for subsequent packets.
-    Once all packets have been received the application sets postCompleted to indicate that TCP should start sending data.
-    Once all packets have been sent the application returns a data size less than MSS.
-    Note that TCP needs to keep todo in case data has to be resent after the application thinks it has finished.
-    */
-     int32_t  todo;
-     bool     postComplete;
-     uint32_t delayUntil;
+    bool     appStarted;                   //Set when data has been sent to the app.
+    char     appData[TCB_APP_STATE_BYTES]; //This is just a place for the app to hold state information - it has no function in TCP.
 };
 
 extern uint32_t    TcbGetIsn(void);
--- a/tcp/tcp.h	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/tcp.h	Sun May 12 17:17:49 2019 +0000
@@ -1,3 +1,3 @@
 #include <stdbool.h>
 
-extern bool TcpTrace;
\ No newline at end of file
+extern bool TcpTrace;
--- a/tcp/tcpbuf.h	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/tcpbuf.h	Sun May 12 17:17:49 2019 +0000
@@ -11,3 +11,8 @@
 
 extern void TcpBufStart    (uint32_t position, int mss, char *pData);
 extern int  TcpBufLength   (void);
+
+
+#define TCP_APP_NOT_STARTED 0
+#define TCP_APP_STARTED     1
+#define TCP_APP_FINISHED   -1
\ No newline at end of file
--- a/tcp/tcprecv.c	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/tcprecv.c	Sun May 12 17:17:49 2019 +0000
@@ -43,9 +43,8 @@
     pTcb->countSendsNotAcked  = 0;
     pTcb->rcvdFin             = false;
     pTcb->sentFin             = false;
-    pTcb->todo                = 0;
-    pTcb->postComplete        = false;
-    pTcb->delayUntil          = MsTimerCount;
+    pTcb->appStarted          = false;
+    for (char* p = pTcb->appData; p < pTcb->appData + TCB_APP_STATE_BYTES; p++) *p = 0;
     pTcb->remIsn              = TcpHdrSeqNum;
     pTcb->locIsn              = TcbGetIsn();
     pTcb->bytesRcvdFromRem    = 0;
@@ -60,10 +59,10 @@
     switch (pTcb->locPort)
     {
         case 80:
-            HttpHandleRequest(dataLength, pData, position, &pTcb->todo, &pTcb->postComplete, &pTcb->delayUntil);
+            HttpHandleRequest (dataLength, pData, position, pTcb->appData);
             break;
         case 443:
-            HttpsHandleRequest(dataLength, pData, position, &pTcb->todo);
+            HttpsHandleRequest(dataLength, pData, position, pTcb->appData);
             break;
         default:
             break;
@@ -97,9 +96,8 @@
     pTcb->countSendsNotAcked  = 0;
     pTcb->rcvdFin             = false;
     pTcb->sentFin             = false;
-    pTcb->todo                = 0;
-    pTcb->postComplete        = false;
-    pTcb->delayUntil          = MsTimerCount;
+    pTcb->appStarted          = false;
+    for (char* p = pTcb->appData; p < pTcb->appData + TCB_APP_STATE_BYTES; p++) *p = 0;
     pTcb->remIsn              = TcpHdrSeqNum + seqLengthRcvd; //Ack number
     pTcb->locIsn              = TcpHdrACK ? TcpHdrAckNum : 0; //Seq number
     pTcb->bytesRcvdFromRem    = 0;
@@ -294,7 +292,11 @@
             break;
             
         case TCB_ESTABLISHED:
-            if (dataLength) handleReceivedData (pPacketRx, dataLength, seqRcvdFromRem - 1, pTcb);
+            if (dataLength)
+            {
+                handleReceivedData(pPacketRx, dataLength, seqRcvdFromRem - 1, pTcb);
+                pTcb->appStarted = true;
+            }
             if (pTcb->sentFin)
             {
                 pTcb->state = pTcb->rcvdFin ? TCB_EMPTY : TCB_CLOSE_FIN_WAIT;
--- a/tcp/tcpsend.c	Thu May 09 07:47:12 2019 +0000
+++ b/tcp/tcpsend.c	Sun May 12 17:17:49 2019 +0000
@@ -6,6 +6,7 @@
 #include     "net.h"
 #include  "action.h"
 #include     "tcp.h"
+#include  "tcpbuf.h"
 #include  "tcphdr.h"
 #include     "tcb.h"
 #include     "ip4.h"
@@ -45,23 +46,15 @@
             return false;
     }    
 }
-static int addApplicationData(void* pPacket, uint16_t port, uint32_t start, int mss, int todo)
-{
-    int dataLength = 0;
+static int addApplicationData(int *pDataLength, void* pPacket, uint16_t port, uint32_t start, int mss, char* pAppData)
+{    
     char* pData = (char*)pPacket + TcpHdrSizeGet();
     switch (port)
     {
-        case 80:
-            HttpSendReply(&dataLength, pData, start, mss, todo);
-            break;
-        case 443:
-            HttpsSendReply(&dataLength, pData, start, mss, todo);
-            break;
-        default:
-            break;
-    }    
-        
-    return dataLength;
+        case  80: return HttpSendReply (pDataLength, pData, start, mss, pAppData);
+        case 443: return HttpsSendReply(pDataLength, pData, start, mss, pAppData);
+        default:  return 0; //0: not started; +1: started; -1: finished
+    }
 }
 static int preparePacket(void* pPacket, struct tcb* pTcb, int dataLength, int* pSize)
 {
@@ -103,22 +96,12 @@
             {
                 if (pTcb->bytesSentToRem - pTcb->bytesAckdByRem < pTcb->window)
                 {
-                    if (pTcb->todo && pTcb->postComplete && MsTimerAbsolute(pTcb->delayUntil)) //don't send response until any post has completed
+                    //0: not started; +1: started; -1: finished
+                    int state = addApplicationData(&dataLength, pPacket, pTcb->locPort, pTcb->bytesSentToRem - 1, pTcb->remMss, pTcb->appData);
+                    if (state == TCP_APP_FINISHED || state == TCP_APP_NOT_STARTED && pTcb->rcvdFin)
                     {
-                        dataLength = addApplicationData(pPacket, pTcb->locPort, pTcb->bytesSentToRem - 1, pTcb->remMss, pTcb->todo);
-                        if (dataLength < pTcb->remMss)
-                        {
-                            TcpHdrFIN     = true;
-                            pTcb->sentFin = true;
-                        }
-                    }
-                    else
-                    {
-                        if (pTcb->rcvdFin)
-                        {
-                            TcpHdrFIN     = true;
-                            pTcb->sentFin = true;
-                        }
+                        TcpHdrFIN     = true;
+                        pTcb->sentFin = true;
                     }
                 }
             }
@@ -162,10 +145,10 @@
             
         case TCB_ESTABLISHED:
         case TCB_CLOSE_FIN_WAIT:
-            if (pTcb->todo)
+            if (pTcb->appStarted)
             {
-                dataLength = addApplicationData(pPacket, pTcb->locPort, seqNum - 1, pTcb->remMss, pTcb->todo);
-                if (dataLength < pTcb->remMss)
+                bool added = addApplicationData(&dataLength, pPacket, pTcb->locPort, seqNum - 1, pTcb->remMss, pTcb->appData);
+                if (added && dataLength < pTcb->remMss)
                 {
                     TcpHdrFIN     = true;
                     pTcb->sentFin = true;