Backing up an unused program in case of future need

Dependencies:   mbed

Revision:
2:06fa34661f19
Parent:
0:09f915e6f9f6
Child:
3:accba7e07a0d
--- a/esp.cpp	Fri Apr 15 09:03:54 2016 +0000
+++ b/esp.cpp	Fri Apr 22 09:23:57 2016 +0000
@@ -1,7 +1,8 @@
 #include "mbed.h"
-#include "log.h"
-#include "esp.h"
-#include "io.h"
+#include  "log.h"
+#include  "esp.h"
+#include   "io.h"
+#include  "cfg.h"
 #include <stdarg.h>
 
 
@@ -22,30 +23,49 @@
 #define LINE_LENGTH 256
 int   EspLineAvailable; //Initialised in init; can be one of the values defined in esp.h
 char  EspLine[LINE_LENGTH];
-static char * pLineNext; //Initialised in init to EspLine
-int addToLine(char c)
+static char* pLineNext; //Initialised in init to EspLine
+
+#define RESP_LENGTH 256
+char EspResp[RESP_LENGTH];
+static char* pRespNext; //Initialised at each request to EspResp
+static int addRawCharToBuffer(char* pBuff, char** ppNext, int len, char c)
 {
-    if (pLineNext >= EspLine + LINE_LENGTH) return -1;
+    
+    // if *ppNext is at the last position in pBuff (pBuff+len-1) then we are full and should stop
+    if (*ppNext >= pBuff + len - 1) return -1;
+    
+    // Put the char into *ppNext and NUL into *ppNext + 1.
+    **ppNext = c; 
+    ++*ppNext;
+    **ppNext = '\0';
+    
+    return 0;
+}
+static int addCharToBuffer(char* pBuff, char** ppNext, int len, char c, int includeCrLf)
+{
+    if (!pBuff) return -1;
     switch (c)
     {
-        case 0:
-            *pLineNext = '\\';
-            pLineNext++;
-            *pLineNext = '0';
-            pLineNext++;
+        case '\0':
+            if (addRawCharToBuffer(pBuff, ppNext, len, '\\')) return -1;
+            if (addRawCharToBuffer(pBuff, ppNext, len, '0' )) return -1; //This is the character zero '0' not NUL '\0'
             break;
         case '\r':
         case '\n':
+            if (includeCrLf && addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
             break;
         default:
-            *pLineNext = c;
-            pLineNext++;
+            if (addRawCharToBuffer(pBuff, ppNext, len, c)) return -1;
             break;
     }
-    *pLineNext = 0;
     return 0;
 }
-
+static int addChar(char c)
+{
+    int r = addCharToBuffer(EspLine, &pLineNext, LINE_LENGTH, c, false);
+            addCharToBuffer(EspResp, &pRespNext, RESP_LENGTH, c, true );
+    return r;
+}
 //Unsolicited ntp or http requests
 //================================
 void *EspIpdBuffer[4];
@@ -66,10 +86,10 @@
 static char* pSendPush; //Initialised in init
 static char* pSendPull; //Initialised in init
 
-static void incrementPushPullPointer(char** pp, char* buffer, int bufferLength)
+static void incrementPushPullPointer(char** pp, char* pbuffer, int bufferlength)
 {
-    (*pp)++; //increment the pointer by one
-    if (*pp == buffer + bufferLength) *pp = buffer; //if the pointer is now beyond the end then point it back to the start
+    ++*pp; //increment the pointer by one
+    if (*pp == pbuffer + bufferlength) *pp = pbuffer; //if the pointer is now beyond the end then point it back to the start
 }
 static void recvpush(void) //Called by the esp data received interrupt
 {
@@ -117,6 +137,8 @@
 }
 void EspSendString(char* p)
 {
+    pRespNext = EspResp;
+    *pRespNext = '\0';
     while(*p) sendpush(*p++);
 }
 void EspSendData(int length, const void * snd)
@@ -139,7 +161,9 @@
     pSendPush = sendbuffer;
     pSendPull = sendbuffer;
     pLineNext = EspLine;
-    *pLineNext = 0;
+    *pLineNext = '\0';
+    pRespNext = EspResp;
+    *pRespNext = '\0';
     EspLengthToSend = 0;
     EspDataToSend = NULL;
     state = IDLE;
@@ -169,17 +193,18 @@
     }
 }
 //General commands
-void EspInit()
+int EspInit()
 {
     EspResetAndStop();
     esp.attach(&recvpush, Serial::RxIrq);
-    esp.baud(BAUD);
+    esp.baud(CfgBaud);
     for (int i = 0; i < 4; i++)
     {
         EspIpdBuffer[i] = NULL;
         EspIpdBufferLen[i] = 0;
         EspIpdReserved[i] = 0;
     }
+    return 0;
 }
 void EspBaud(int baud)
 {
@@ -212,7 +237,7 @@
             {
                 pLineNext = EspLine;
                 *pLineNext = 0;           
-                int r = addToLine(c);
+                int r = addChar(c);
                 if (r)
                 {
                     EspLineAvailable = ESP_OVERFLOW;
@@ -232,7 +257,7 @@
             }
             else
             {
-                int r = addToLine(c);
+                int r = addChar(c);
                 if (r)
                 {
                     EspLineAvailable = ESP_OVERFLOW;
@@ -244,8 +269,8 @@
             if (pipdHeader == ipdHeader && c != 'I') //If the first character after the '+' is not 'I' then start a line instead
             {
                 pLineNext = EspLine;
-                addToLine('+'); 
-                addToLine(c);
+                addChar('+'); 
+                addChar(c);
                 state = IN_LINE;               
             }
             else