Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Fri Apr 15 09:03:54 2016 +0000
Revision:
1:94282484baae
Parent:
0:09f915e6f9f6
Child:
2:06fa34661f19
Streamed the log in chunks to allow the send buffer to be reduced. That in turn allowed the log buffer to be increased.; Added content type text/html to http responses.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:09f915e6f9f6 1 #include "esp.h"
andrewboyson 0:09f915e6f9f6 2 #include "at.h"
andrewboyson 0:09f915e6f9f6 3 #include "io.h"
andrewboyson 0:09f915e6f9f6 4 #include "log.h"
andrewboyson 0:09f915e6f9f6 5 #include "time.h"
andrewboyson 0:09f915e6f9f6 6 #include "wifi.h"
andrewboyson 1:94282484baae 7 #define RECV_BUFFER_SIZE 1024
andrewboyson 1:94282484baae 8 #define SEND_BUFFER_SIZE 256
andrewboyson 0:09f915e6f9f6 9 #define SERVER_PORT 80
andrewboyson 0:09f915e6f9f6 10
andrewboyson 1:94282484baae 11 static char recvbuffer[RECV_BUFFER_SIZE];
andrewboyson 1:94282484baae 12 static char sendbuffer[SEND_BUFFER_SIZE];
andrewboyson 1:94282484baae 13
andrewboyson 1:94282484baae 14 #define WHAT_NOT_FOUND 0
andrewboyson 1:94282484baae 15 #define WHAT_BAD_REQUEST 1
andrewboyson 1:94282484baae 16 #define WHAT_BAD_METHOD 2
andrewboyson 1:94282484baae 17 #define WHAT_LED 3
andrewboyson 1:94282484baae 18 #define WHAT_LOG 4
andrewboyson 1:94282484baae 19 static int whatToSendToId[4];
andrewboyson 1:94282484baae 20 static int lineToSendToId[4]; //0 == do nothing; -1 == close
andrewboyson 0:09f915e6f9f6 21
andrewboyson 0:09f915e6f9f6 22 void ServerInit(void) //Make sure this is only called after any other ids are reserved.
andrewboyson 0:09f915e6f9f6 23 {
andrewboyson 0:09f915e6f9f6 24 for (int id = 0; id < 4; id++)
andrewboyson 0:09f915e6f9f6 25 {
andrewboyson 0:09f915e6f9f6 26 if (!EspIpdReserved[id])
andrewboyson 0:09f915e6f9f6 27 {
andrewboyson 0:09f915e6f9f6 28 EspIpdBuffer[id] = recvbuffer;
andrewboyson 1:94282484baae 29 EspIpdBufferLen[id] = RECV_BUFFER_SIZE;
andrewboyson 1:94282484baae 30 }
andrewboyson 1:94282484baae 31 whatToSendToId[id] = 0;
andrewboyson 1:94282484baae 32 lineToSendToId[id] = 0;
andrewboyson 1:94282484baae 33 }
andrewboyson 1:94282484baae 34 }
andrewboyson 1:94282484baae 35
andrewboyson 1:94282484baae 36 static int addChunk(int prevlen, char *text)
andrewboyson 1:94282484baae 37 {
andrewboyson 1:94282484baae 38 strncpy(sendbuffer + prevlen, text, SEND_BUFFER_SIZE - prevlen);
andrewboyson 1:94282484baae 39 int len = prevlen + strlen(text);
andrewboyson 1:94282484baae 40 return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
andrewboyson 1:94282484baae 41 }
andrewboyson 1:94282484baae 42 static int fillChunk(char * text)
andrewboyson 1:94282484baae 43 {
andrewboyson 1:94282484baae 44 strncpy(sendbuffer, text, SEND_BUFFER_SIZE);
andrewboyson 1:94282484baae 45 int len = strlen(text);
andrewboyson 1:94282484baae 46 return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
andrewboyson 1:94282484baae 47 }
andrewboyson 1:94282484baae 48 static int fillNotFound(int id)
andrewboyson 1:94282484baae 49 {
andrewboyson 1:94282484baae 50 int len = fillChunk("HTTP/1.0 404 Not Found\r\n\r\n");
andrewboyson 1:94282484baae 51 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 52 return len;
andrewboyson 1:94282484baae 53 }
andrewboyson 1:94282484baae 54 static int fillBadRequest(int id)
andrewboyson 1:94282484baae 55 {
andrewboyson 1:94282484baae 56 int len = fillChunk("HTTP/1.0 400 Bad Request\r\n\r\n");
andrewboyson 1:94282484baae 57 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 58 return len;
andrewboyson 1:94282484baae 59 }
andrewboyson 1:94282484baae 60 static int fillBadMethod(int id)
andrewboyson 1:94282484baae 61 {
andrewboyson 1:94282484baae 62 int len = fillChunk("HTTP/1.0 405 Method Not Allowed\r\nAllow: GET\r\n\r\n");
andrewboyson 1:94282484baae 63 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 64 return len;
andrewboyson 1:94282484baae 65 }
andrewboyson 1:94282484baae 66 static int fillLogChunk() //Returns length. If length is less than the buffer size then that was the last chunk to send: that could mean a length of zero.
andrewboyson 1:94282484baae 67 {
andrewboyson 1:94282484baae 68 static int enumerationStarted = false;
andrewboyson 1:94282484baae 69 if (!enumerationStarted)
andrewboyson 1:94282484baae 70 {
andrewboyson 1:94282484baae 71 LogEnumerateStart();
andrewboyson 1:94282484baae 72 enumerationStarted = true;
andrewboyson 1:94282484baae 73 }
andrewboyson 1:94282484baae 74 char* p = sendbuffer;
andrewboyson 1:94282484baae 75 while (p < sendbuffer + SEND_BUFFER_SIZE)
andrewboyson 1:94282484baae 76 {
andrewboyson 1:94282484baae 77 int c = LogEnumerate();
andrewboyson 1:94282484baae 78 if (c == EOF)
andrewboyson 1:94282484baae 79 {
andrewboyson 1:94282484baae 80 enumerationStarted = false;
andrewboyson 1:94282484baae 81 break;
andrewboyson 0:09f915e6f9f6 82 }
andrewboyson 1:94282484baae 83 *p++ = c;
andrewboyson 1:94282484baae 84 }
andrewboyson 1:94282484baae 85 return p - sendbuffer;
andrewboyson 1:94282484baae 86 }
andrewboyson 1:94282484baae 87 static char* header =
andrewboyson 1:94282484baae 88 "HTTP/1.0 200 OK\r\n"
andrewboyson 1:94282484baae 89 "Content-Type: text/html; charset=ISO-8859-1\r\n"
andrewboyson 1:94282484baae 90 "\r\n"
andrewboyson 1:94282484baae 91 "<!DOCTYPE html>\r\n"
andrewboyson 1:94282484baae 92 "<html>\r\n"
andrewboyson 1:94282484baae 93 "<head>\r\n"
andrewboyson 1:94282484baae 94 "<title>IoT mbed</title>\r\n"
andrewboyson 1:94282484baae 95 "</head>\r\n"
andrewboyson 1:94282484baae 96 "<body>\r\n";
andrewboyson 1:94282484baae 97
andrewboyson 1:94282484baae 98 static int fillLog(int id)
andrewboyson 1:94282484baae 99 {
andrewboyson 1:94282484baae 100 int len = 0;
andrewboyson 1:94282484baae 101
andrewboyson 1:94282484baae 102 switch (lineToSendToId[id])
andrewboyson 1:94282484baae 103 {
andrewboyson 1:94282484baae 104 case 1: len = fillChunk(header); lineToSendToId[id] += 1; break;
andrewboyson 1:94282484baae 105 case 2: len = fillChunk("<code><pre>"); lineToSendToId[id] += 1; break;
andrewboyson 1:94282484baae 106 case 3: len = fillLogChunk(); if (len < SEND_BUFFER_SIZE) lineToSendToId[id] += 1; break; //only increments after the last chunk
andrewboyson 1:94282484baae 107 case 4: len = fillChunk(
andrewboyson 1:94282484baae 108 "</pre></code>\r\n"
andrewboyson 1:94282484baae 109 "</body>\r\n"
andrewboyson 1:94282484baae 110 "</html>\r\n");
andrewboyson 1:94282484baae 111 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 112 break;
andrewboyson 1:94282484baae 113 }
andrewboyson 1:94282484baae 114 return len;
andrewboyson 1:94282484baae 115 }
andrewboyson 1:94282484baae 116
andrewboyson 1:94282484baae 117 static int fillLed(int id)
andrewboyson 1:94282484baae 118 {
andrewboyson 1:94282484baae 119 int len;
andrewboyson 1:94282484baae 120 switch (lineToSendToId[id])
andrewboyson 1:94282484baae 121 {
andrewboyson 1:94282484baae 122 case 1:
andrewboyson 1:94282484baae 123 len = fillChunk(header);
andrewboyson 1:94282484baae 124 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 125 break;
andrewboyson 1:94282484baae 126 case 2:
andrewboyson 1:94282484baae 127 len = fillChunk(
andrewboyson 1:94282484baae 128 "<form action='/' method='get'>\r\n"
andrewboyson 1:94282484baae 129 "<input type='hidden' name='ledonoff'>\r\n");
andrewboyson 1:94282484baae 130 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 131 break;
andrewboyson 1:94282484baae 132 case 3:
andrewboyson 1:94282484baae 133 len = fillChunk("Led <input type='checkbox' name='led' value='on'");
andrewboyson 1:94282484baae 134 if (Led1) len = addChunk(len, " checked='checked'");
andrewboyson 1:94282484baae 135 len = addChunk(len, ">\r\n");
andrewboyson 1:94282484baae 136 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 137 break;
andrewboyson 1:94282484baae 138 case 4:
andrewboyson 1:94282484baae 139 len = fillChunk(
andrewboyson 1:94282484baae 140 "<input type='submit' value='Set'><br/>\r\n"
andrewboyson 1:94282484baae 141 "</form>\r\n"
andrewboyson 1:94282484baae 142 "</body>\r\n"
andrewboyson 1:94282484baae 143 "</html>\r\n");
andrewboyson 1:94282484baae 144 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 145 break;
andrewboyson 1:94282484baae 146 }
andrewboyson 1:94282484baae 147 return len;
andrewboyson 1:94282484baae 148 }
andrewboyson 1:94282484baae 149 static int fillSendBuffer(int id)
andrewboyson 1:94282484baae 150 {
andrewboyson 1:94282484baae 151 switch(whatToSendToId[id])
andrewboyson 1:94282484baae 152 {
andrewboyson 1:94282484baae 153 case WHAT_NOT_FOUND: return fillNotFound(id);
andrewboyson 1:94282484baae 154 case WHAT_BAD_REQUEST: return fillBadRequest(id);
andrewboyson 1:94282484baae 155 case WHAT_BAD_METHOD: return fillBadMethod(id);
andrewboyson 1:94282484baae 156 case WHAT_LOG: return fillLog(id);
andrewboyson 1:94282484baae 157 case WHAT_LED: return fillLed(id);
andrewboyson 1:94282484baae 158 default:
andrewboyson 1:94282484baae 159 LogF("No such 'whatToSendToId' %s", whatToSendToId[id]);
andrewboyson 1:94282484baae 160 return -1;
andrewboyson 0:09f915e6f9f6 161 }
andrewboyson 0:09f915e6f9f6 162 }
andrewboyson 1:94282484baae 163 static int splitRequest(char** ppMethod, char** ppPath, char** ppQuery, int *pLenMethod, int *pLenPath, int *pLenQuery) //returns 1 if malformed request; 0 if ok
andrewboyson 0:09f915e6f9f6 164 {
andrewboyson 1:94282484baae 165 char* p = recvbuffer;
andrewboyson 1:94282484baae 166 char* pE = recvbuffer + RECV_BUFFER_SIZE;
andrewboyson 1:94282484baae 167
andrewboyson 1:94282484baae 168 *ppMethod = NULL;
andrewboyson 1:94282484baae 169 *ppPath = NULL;
andrewboyson 1:94282484baae 170 *ppQuery = NULL;
andrewboyson 1:94282484baae 171 *pLenMethod = 0;
andrewboyson 1:94282484baae 172 *pLenPath = 0;
andrewboyson 1:94282484baae 173 *pLenQuery = 0;
andrewboyson 0:09f915e6f9f6 174
andrewboyson 1:94282484baae 175 while (*p == ' ') //Loop to non space
andrewboyson 1:94282484baae 176 {
andrewboyson 1:94282484baae 177 if (p == pE) return -1;
andrewboyson 1:94282484baae 178 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 179 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 180 p++;
andrewboyson 1:94282484baae 181 }
andrewboyson 1:94282484baae 182 *ppMethod = p; //Record the start of the method GET or POST
andrewboyson 1:94282484baae 183
andrewboyson 1:94282484baae 184 while (*p != ' ') //Loop to a space
andrewboyson 1:94282484baae 185 {
andrewboyson 1:94282484baae 186 if (p == pE) return -1;
andrewboyson 1:94282484baae 187 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 188 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 189 p++;
andrewboyson 1:94282484baae 190 }
andrewboyson 1:94282484baae 191 *pLenMethod = p - *ppMethod; //Record the length of the method GET or POST
andrewboyson 0:09f915e6f9f6 192
andrewboyson 1:94282484baae 193
andrewboyson 1:94282484baae 194 while (*p == ' ') //Loop to non space
andrewboyson 1:94282484baae 195 {
andrewboyson 1:94282484baae 196 if (p == pE) return -1;
andrewboyson 1:94282484baae 197 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 198 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 199 p++;
andrewboyson 1:94282484baae 200 }
andrewboyson 1:94282484baae 201 *ppPath = p; //Record the start of the file part of the url
andrewboyson 1:94282484baae 202
andrewboyson 1:94282484baae 203 while (*p != ' ') //Loop to space
andrewboyson 1:94282484baae 204 {
andrewboyson 1:94282484baae 205 if (p == pE) return -1;
andrewboyson 1:94282484baae 206 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 207 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 208 if (*p == '?') *ppQuery = p + 1;
andrewboyson 1:94282484baae 209 p++;
andrewboyson 1:94282484baae 210 }
andrewboyson 1:94282484baae 211
andrewboyson 1:94282484baae 212 if (*ppQuery) //Calulate length of the file and query parts
andrewboyson 0:09f915e6f9f6 213 {
andrewboyson 1:94282484baae 214 *pLenPath = *ppQuery - *ppPath - 1;
andrewboyson 1:94282484baae 215 *pLenQuery = p - *ppQuery;
andrewboyson 1:94282484baae 216 }
andrewboyson 1:94282484baae 217 else
andrewboyson 1:94282484baae 218 {
andrewboyson 1:94282484baae 219 *pLenPath = p - *ppPath;
andrewboyson 1:94282484baae 220 *pLenQuery = 0;
andrewboyson 1:94282484baae 221 }
andrewboyson 1:94282484baae 222
andrewboyson 1:94282484baae 223 return 0;
andrewboyson 1:94282484baae 224 }
andrewboyson 1:94282484baae 225 int compare(char* mem, int len, char* str) //Returns true if same and false if not
andrewboyson 1:94282484baae 226 {
andrewboyson 1:94282484baae 227 if (strlen(str) != len) return false;
andrewboyson 1:94282484baae 228 if (memcmp(mem, str, len)) return false;
andrewboyson 1:94282484baae 229 return true;
andrewboyson 1:94282484baae 230 }
andrewboyson 1:94282484baae 231 static int recvMain()
andrewboyson 1:94282484baae 232 {
andrewboyson 1:94282484baae 233 //Wait for data to be available oneshot
andrewboyson 1:94282484baae 234 if (EspDataAvailable != ESP_AVAILABLE) return 0;
andrewboyson 1:94282484baae 235
andrewboyson 1:94282484baae 236 //Ignore ids that have been reserved elsewhere (ie NTP)
andrewboyson 1:94282484baae 237 if (EspIpdReserved[EspIpdId]) return 0;
andrewboyson 1:94282484baae 238
andrewboyson 1:94282484baae 239 //Set up the next line to send to be the first
andrewboyson 1:94282484baae 240 lineToSendToId[EspIpdId] = 1;
andrewboyson 1:94282484baae 241
andrewboyson 1:94282484baae 242 char* pMethod;
andrewboyson 1:94282484baae 243 char* pPath;
andrewboyson 1:94282484baae 244 char* pQuery;
andrewboyson 1:94282484baae 245 int lenMethod;
andrewboyson 1:94282484baae 246 int lenQuery;
andrewboyson 1:94282484baae 247 int lenPath;
andrewboyson 1:94282484baae 248 int r = splitRequest(&pMethod, &pPath, &pQuery, &lenMethod, &lenPath, &lenQuery);
andrewboyson 1:94282484baae 249 if (r)
andrewboyson 1:94282484baae 250 {
andrewboyson 1:94282484baae 251 whatToSendToId[EspIpdId] = WHAT_BAD_REQUEST;
andrewboyson 1:94282484baae 252 return 0;
andrewboyson 1:94282484baae 253 }
andrewboyson 1:94282484baae 254
andrewboyson 1:94282484baae 255 if (!compare(pMethod, lenMethod, "GET"))
andrewboyson 1:94282484baae 256 {
andrewboyson 1:94282484baae 257 whatToSendToId[EspIpdId] = WHAT_BAD_METHOD;
andrewboyson 1:94282484baae 258 return 0;
andrewboyson 1:94282484baae 259 }
andrewboyson 1:94282484baae 260
andrewboyson 1:94282484baae 261 if (compare(pPath, lenPath, "/"))
andrewboyson 1:94282484baae 262 {
andrewboyson 1:94282484baae 263 if (pQuery)
andrewboyson 0:09f915e6f9f6 264 {
andrewboyson 1:94282484baae 265 if (compare(pQuery, lenQuery, "ledonoff=&led=on")) Led1 = 1;
andrewboyson 1:94282484baae 266 if (compare(pQuery, lenQuery, "ledonoff=" )) Led1 = 0;
andrewboyson 0:09f915e6f9f6 267 }
andrewboyson 1:94282484baae 268 whatToSendToId[EspIpdId] = WHAT_LED;
andrewboyson 1:94282484baae 269 return 0;
andrewboyson 1:94282484baae 270 }
andrewboyson 1:94282484baae 271
andrewboyson 1:94282484baae 272 if (compare(pPath, lenPath, "/log"))
andrewboyson 1:94282484baae 273 {
andrewboyson 1:94282484baae 274 whatToSendToId[EspIpdId] = WHAT_LOG;
andrewboyson 1:94282484baae 275 return 0;
andrewboyson 1:94282484baae 276 }
andrewboyson 1:94282484baae 277
andrewboyson 1:94282484baae 278 whatToSendToId[EspIpdId] = WHAT_NOT_FOUND;
andrewboyson 1:94282484baae 279 return 0;
andrewboyson 1:94282484baae 280 }
andrewboyson 1:94282484baae 281 static int sendMain()
andrewboyson 1:94282484baae 282 {
andrewboyson 1:94282484baae 283 //Do nothing if a command is already in progress
andrewboyson 1:94282484baae 284 if (AtBusy()) return 0;
andrewboyson 1:94282484baae 285
andrewboyson 1:94282484baae 286 //Send data. Do each id in turn to avoid interleaved calls to fillers like LogEnumerate which are not reentrant.
andrewboyson 1:94282484baae 287 for(int id = 0; id < 4; id++)
andrewboyson 1:94282484baae 288 {
andrewboyson 1:94282484baae 289 if (lineToSendToId[id] > 0)
andrewboyson 0:09f915e6f9f6 290 {
andrewboyson 1:94282484baae 291 int length = fillSendBuffer(id);
andrewboyson 1:94282484baae 292 if (length < 0) return -1;
andrewboyson 1:94282484baae 293 if (length > 0) AtSendData(id, length, sendbuffer, NULL);
andrewboyson 1:94282484baae 294 }
andrewboyson 1:94282484baae 295 else if (lineToSendToId[id] < 0)
andrewboyson 1:94282484baae 296 {
andrewboyson 1:94282484baae 297 AtClose(id, NULL);
andrewboyson 1:94282484baae 298 lineToSendToId[id] = 0;
andrewboyson 0:09f915e6f9f6 299 }
andrewboyson 0:09f915e6f9f6 300 }
andrewboyson 0:09f915e6f9f6 301
andrewboyson 1:94282484baae 302 return 0;
andrewboyson 1:94282484baae 303 }
andrewboyson 1:94282484baae 304 int startMain()
andrewboyson 1:94282484baae 305 {
andrewboyson 1:94282484baae 306 //Do nothing if a command is already in progress
andrewboyson 1:94282484baae 307 if (AtBusy()) return 0;
andrewboyson 1:94282484baae 308
andrewboyson 1:94282484baae 309 //Do nothing until WiFi is running
andrewboyson 1:94282484baae 310 if (!WifiStarted()) return 0;
andrewboyson 0:09f915e6f9f6 311
andrewboyson 1:94282484baae 312 //Start the server if not started
andrewboyson 1:94282484baae 313 static int startServerReply = AT_NONE;
andrewboyson 1:94282484baae 314 if (startServerReply != AT_SUCCESS) AtStartServer(SERVER_PORT, &startServerReply);
andrewboyson 1:94282484baae 315 return 0;
andrewboyson 1:94282484baae 316 }
andrewboyson 1:94282484baae 317 int ServerMain()
andrewboyson 1:94282484baae 318 {
andrewboyson 1:94282484baae 319 if (startMain()) return -1;
andrewboyson 1:94282484baae 320 if ( recvMain()) return -1;
andrewboyson 1:94282484baae 321 if ( sendMain()) return -1;
andrewboyson 0:09f915e6f9f6 322
andrewboyson 0:09f915e6f9f6 323 return 0;
andrewboyson 0:09f915e6f9f6 324 }