Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Tue May 31 07:35:28 2016 +0000
Revision:
6:be97d38e0b01
Parent:
5:6226f3c566ef
Child:
7:024ace6d943c
Moved to wait_us for delays below 100us and timer.read_us above. This replaced interrupts which worked but tended to be unreliable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 5:6226f3c566ef 1 #include "esp.h"
andrewboyson 5:6226f3c566ef 2 #include "at.h"
andrewboyson 5:6226f3c566ef 3 #include "io.h"
andrewboyson 5:6226f3c566ef 4 #include "log.h"
andrewboyson 5:6226f3c566ef 5 #include "time.h"
andrewboyson 5:6226f3c566ef 6 #include "wifi.h"
andrewboyson 5:6226f3c566ef 7 #include "ds18b20.h"
andrewboyson 6:be97d38e0b01 8 #include "cfg.h"
andrewboyson 6:be97d38e0b01 9 #define RECV_BUFFER_SIZE 128
andrewboyson 6:be97d38e0b01 10 #define SEND_BUFFER_SIZE 256
andrewboyson 0:09f915e6f9f6 11 #define SERVER_PORT 80
andrewboyson 0:09f915e6f9f6 12
andrewboyson 1:94282484baae 13 static char recvbuffer[RECV_BUFFER_SIZE];
andrewboyson 1:94282484baae 14 static char sendbuffer[SEND_BUFFER_SIZE];
andrewboyson 1:94282484baae 15
andrewboyson 1:94282484baae 16 #define WHAT_NOT_FOUND 0
andrewboyson 1:94282484baae 17 #define WHAT_BAD_REQUEST 1
andrewboyson 1:94282484baae 18 #define WHAT_BAD_METHOD 2
andrewboyson 1:94282484baae 19 #define WHAT_LED 3
andrewboyson 1:94282484baae 20 #define WHAT_LOG 4
andrewboyson 1:94282484baae 21 static int whatToSendToId[4];
andrewboyson 1:94282484baae 22 static int lineToSendToId[4]; //0 == do nothing; -1 == close
andrewboyson 0:09f915e6f9f6 23
andrewboyson 2:06fa34661f19 24 int ServerInit(void) //Make sure this is only called after any other ids are reserved.
andrewboyson 0:09f915e6f9f6 25 {
andrewboyson 0:09f915e6f9f6 26 for (int id = 0; id < 4; id++)
andrewboyson 0:09f915e6f9f6 27 {
andrewboyson 0:09f915e6f9f6 28 if (!EspIpdReserved[id])
andrewboyson 0:09f915e6f9f6 29 {
andrewboyson 0:09f915e6f9f6 30 EspIpdBuffer[id] = recvbuffer;
andrewboyson 1:94282484baae 31 EspIpdBufferLen[id] = RECV_BUFFER_SIZE;
andrewboyson 1:94282484baae 32 }
andrewboyson 1:94282484baae 33 whatToSendToId[id] = 0;
andrewboyson 1:94282484baae 34 lineToSendToId[id] = 0;
andrewboyson 1:94282484baae 35 }
andrewboyson 2:06fa34661f19 36 return 0;
andrewboyson 1:94282484baae 37 }
andrewboyson 5:6226f3c566ef 38 static int addChunkF(int prevlen, char *fmt, ...)
andrewboyson 5:6226f3c566ef 39 {
andrewboyson 5:6226f3c566ef 40 //Set up variable arguments
andrewboyson 5:6226f3c566ef 41 va_list argptr;
andrewboyson 5:6226f3c566ef 42 va_start(argptr, fmt);
andrewboyson 5:6226f3c566ef 43
andrewboyson 5:6226f3c566ef 44 //Fill the buffer
andrewboyson 5:6226f3c566ef 45 int room = SEND_BUFFER_SIZE - prevlen;
andrewboyson 5:6226f3c566ef 46 int sent = vsnprintf(sendbuffer + prevlen, room, fmt, argptr);
andrewboyson 5:6226f3c566ef 47 if (sent > room) sent = room;
andrewboyson 5:6226f3c566ef 48 if (sent < 0) sent = 0;
andrewboyson 5:6226f3c566ef 49
andrewboyson 5:6226f3c566ef 50 //Finish with variable arguments
andrewboyson 5:6226f3c566ef 51 va_end(argptr);
andrewboyson 5:6226f3c566ef 52
andrewboyson 5:6226f3c566ef 53 //Return length
andrewboyson 5:6226f3c566ef 54 int len = prevlen + sent;
andrewboyson 5:6226f3c566ef 55 return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
andrewboyson 5:6226f3c566ef 56
andrewboyson 5:6226f3c566ef 57 }
andrewboyson 1:94282484baae 58
andrewboyson 1:94282484baae 59 static int addChunk(int prevlen, char *text)
andrewboyson 1:94282484baae 60 {
andrewboyson 1:94282484baae 61 strncpy(sendbuffer + prevlen, text, SEND_BUFFER_SIZE - prevlen);
andrewboyson 1:94282484baae 62 int len = prevlen + strlen(text);
andrewboyson 1:94282484baae 63 return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
andrewboyson 1:94282484baae 64 }
andrewboyson 1:94282484baae 65 static int fillChunk(char * text)
andrewboyson 1:94282484baae 66 {
andrewboyson 1:94282484baae 67 strncpy(sendbuffer, text, SEND_BUFFER_SIZE);
andrewboyson 1:94282484baae 68 int len = strlen(text);
andrewboyson 1:94282484baae 69 return len < SEND_BUFFER_SIZE ? len : SEND_BUFFER_SIZE;
andrewboyson 1:94282484baae 70 }
andrewboyson 1:94282484baae 71 static int fillNotFound(int id)
andrewboyson 1:94282484baae 72 {
andrewboyson 1:94282484baae 73 int len = fillChunk("HTTP/1.0 404 Not Found\r\n\r\n");
andrewboyson 1:94282484baae 74 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 75 return len;
andrewboyson 1:94282484baae 76 }
andrewboyson 1:94282484baae 77 static int fillBadRequest(int id)
andrewboyson 1:94282484baae 78 {
andrewboyson 1:94282484baae 79 int len = fillChunk("HTTP/1.0 400 Bad Request\r\n\r\n");
andrewboyson 1:94282484baae 80 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 81 return len;
andrewboyson 1:94282484baae 82 }
andrewboyson 1:94282484baae 83 static int fillBadMethod(int id)
andrewboyson 1:94282484baae 84 {
andrewboyson 1:94282484baae 85 int len = fillChunk("HTTP/1.0 405 Method Not Allowed\r\nAllow: GET\r\n\r\n");
andrewboyson 1:94282484baae 86 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 87 return len;
andrewboyson 1:94282484baae 88 }
andrewboyson 1:94282484baae 89 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 90 {
andrewboyson 1:94282484baae 91 static int enumerationStarted = false;
andrewboyson 1:94282484baae 92 if (!enumerationStarted)
andrewboyson 1:94282484baae 93 {
andrewboyson 1:94282484baae 94 LogEnumerateStart();
andrewboyson 1:94282484baae 95 enumerationStarted = true;
andrewboyson 1:94282484baae 96 }
andrewboyson 1:94282484baae 97 char* p = sendbuffer;
andrewboyson 1:94282484baae 98 while (p < sendbuffer + SEND_BUFFER_SIZE)
andrewboyson 1:94282484baae 99 {
andrewboyson 1:94282484baae 100 int c = LogEnumerate();
andrewboyson 1:94282484baae 101 if (c == EOF)
andrewboyson 1:94282484baae 102 {
andrewboyson 1:94282484baae 103 enumerationStarted = false;
andrewboyson 1:94282484baae 104 break;
andrewboyson 0:09f915e6f9f6 105 }
andrewboyson 1:94282484baae 106 *p++ = c;
andrewboyson 1:94282484baae 107 }
andrewboyson 1:94282484baae 108 return p - sendbuffer;
andrewboyson 1:94282484baae 109 }
andrewboyson 1:94282484baae 110 static char* header =
andrewboyson 1:94282484baae 111 "HTTP/1.0 200 OK\r\n"
andrewboyson 1:94282484baae 112 "Content-Type: text/html; charset=ISO-8859-1\r\n"
andrewboyson 1:94282484baae 113 "\r\n"
andrewboyson 1:94282484baae 114 "<!DOCTYPE html>\r\n"
andrewboyson 1:94282484baae 115 "<html>\r\n"
andrewboyson 1:94282484baae 116 "<head>\r\n"
andrewboyson 1:94282484baae 117 "<title>IoT mbed</title>\r\n"
andrewboyson 1:94282484baae 118 "</head>\r\n"
andrewboyson 6:be97d38e0b01 119 "<style>\r\n"
andrewboyson 6:be97d38e0b01 120 "* { font-family: Tahoma, Geneva, sans-serif; }\r\n"
andrewboyson 6:be97d38e0b01 121 "</style>\r\n"
andrewboyson 1:94282484baae 122 "<body>\r\n";
andrewboyson 1:94282484baae 123
andrewboyson 1:94282484baae 124 static int fillLog(int id)
andrewboyson 1:94282484baae 125 {
andrewboyson 1:94282484baae 126 int len = 0;
andrewboyson 1:94282484baae 127
andrewboyson 1:94282484baae 128 switch (lineToSendToId[id])
andrewboyson 1:94282484baae 129 {
andrewboyson 1:94282484baae 130 case 1: len = fillChunk(header); lineToSendToId[id] += 1; break;
andrewboyson 1:94282484baae 131 case 2: len = fillChunk("<code><pre>"); lineToSendToId[id] += 1; break;
andrewboyson 1:94282484baae 132 case 3: len = fillLogChunk(); if (len < SEND_BUFFER_SIZE) lineToSendToId[id] += 1; break; //only increments after the last chunk
andrewboyson 1:94282484baae 133 case 4: len = fillChunk(
andrewboyson 1:94282484baae 134 "</pre></code>\r\n"
andrewboyson 1:94282484baae 135 "</body>\r\n"
andrewboyson 1:94282484baae 136 "</html>\r\n");
andrewboyson 1:94282484baae 137 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 138 break;
andrewboyson 1:94282484baae 139 }
andrewboyson 1:94282484baae 140 return len;
andrewboyson 1:94282484baae 141 }
andrewboyson 6:be97d38e0b01 142 static int addTemperature(int len, int16_t value)
andrewboyson 6:be97d38e0b01 143 {
andrewboyson 6:be97d38e0b01 144 switch (value)
andrewboyson 6:be97d38e0b01 145 {
andrewboyson 6:be97d38e0b01 146 case DS18B20_ERROR_CRC: return addChunk (len, "CRC error" );
andrewboyson 6:be97d38e0b01 147 case DS18B20_ERROR_NOT_FOUND: return addChunk (len, "ROM not found" );
andrewboyson 6:be97d38e0b01 148 case DS18B20_ERROR_TIMED_OUT: return addChunk (len, "Timed out" );
andrewboyson 6:be97d38e0b01 149 case DS18B20_ERROR_NO_DEVICE_PRESENT: return addChunk (len, "No device detected after reset");
andrewboyson 6:be97d38e0b01 150 case DS18B20_ERROR_NO_DEVICE_PARTICIPATING: return addChunk (len, "Device removed during search" );
andrewboyson 6:be97d38e0b01 151 default: return addChunkF(len, "%1.1f", value / 16.0 );
andrewboyson 6:be97d38e0b01 152 }
andrewboyson 6:be97d38e0b01 153 }
andrewboyson 1:94282484baae 154 static int fillLed(int id)
andrewboyson 1:94282484baae 155 {
andrewboyson 1:94282484baae 156 int len;
andrewboyson 1:94282484baae 157 switch (lineToSendToId[id])
andrewboyson 1:94282484baae 158 {
andrewboyson 1:94282484baae 159 case 1:
andrewboyson 1:94282484baae 160 len = fillChunk(header);
andrewboyson 1:94282484baae 161 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 162 break;
andrewboyson 1:94282484baae 163 case 2:
andrewboyson 5:6226f3c566ef 164 len = 0;
andrewboyson 6:be97d38e0b01 165 len = addChunkF(len, "Scan us: %d<br/><br/>\r\n", TimeScanUs);
andrewboyson 2:06fa34661f19 166 lineToSendToId[id] += 1;
andrewboyson 2:06fa34661f19 167 break;
andrewboyson 2:06fa34661f19 168 case 3:
andrewboyson 6:be97d38e0b01 169 len = 0;
andrewboyson 6:be97d38e0b01 170 len = addChunk (len, "Devices:<br/>\r\n");
andrewboyson 6:be97d38e0b01 171 for (int j = 0; j < DS18B20DeviceCount; j++)
andrewboyson 6:be97d38e0b01 172 {
andrewboyson 6:be97d38e0b01 173 len = addChunkF(len, "%d - ", j);
andrewboyson 6:be97d38e0b01 174 for (int i = 0; i < 8; i++) len = addChunkF(len, " %02X", DS18B20DeviceList[j*8 + i]);
andrewboyson 6:be97d38e0b01 175 len = addChunk(len, " - ");
andrewboyson 6:be97d38e0b01 176 len = addTemperature(len, DS18B20Value[j]);
andrewboyson 6:be97d38e0b01 177 len = addChunk (len, "<br/>\r\n");
andrewboyson 6:be97d38e0b01 178 }
andrewboyson 6:be97d38e0b01 179 len = addChunk (len, "<br/>\r\n");
andrewboyson 6:be97d38e0b01 180 lineToSendToId[id] += 1;
andrewboyson 6:be97d38e0b01 181 break;
andrewboyson 6:be97d38e0b01 182 case 4:
andrewboyson 6:be97d38e0b01 183 len = 0;
andrewboyson 6:be97d38e0b01 184 int16_t temp;
andrewboyson 6:be97d38e0b01 185
andrewboyson 6:be97d38e0b01 186 temp = DS18B20ValueFromRom(CfgTankRom);
andrewboyson 6:be97d38e0b01 187 len = addChunk (len, "Tank temperature: ");
andrewboyson 6:be97d38e0b01 188 len = addTemperature(len, temp);
andrewboyson 6:be97d38e0b01 189 len = addChunk (len, "<br/>\r\n");
andrewboyson 6:be97d38e0b01 190
andrewboyson 6:be97d38e0b01 191 temp = DS18B20ValueFromRom(CfgInletRom);
andrewboyson 6:be97d38e0b01 192 len = addChunk (len, "Inlet temperature: ");
andrewboyson 6:be97d38e0b01 193 len = addTemperature(len, temp);
andrewboyson 6:be97d38e0b01 194 len = addChunk (len, "<br/><br/>\r\n");
andrewboyson 6:be97d38e0b01 195
andrewboyson 6:be97d38e0b01 196 lineToSendToId[id] += 1;
andrewboyson 6:be97d38e0b01 197 break;
andrewboyson 6:be97d38e0b01 198 case 5:
andrewboyson 1:94282484baae 199 len = fillChunk(
andrewboyson 6:be97d38e0b01 200 "<br/>"
andrewboyson 1:94282484baae 201 "<form action='/' method='get'>\r\n"
andrewboyson 1:94282484baae 202 "<input type='hidden' name='ledonoff'>\r\n");
andrewboyson 1:94282484baae 203 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 204 break;
andrewboyson 6:be97d38e0b01 205 case 6:
andrewboyson 1:94282484baae 206 len = fillChunk("Led <input type='checkbox' name='led' value='on'");
andrewboyson 1:94282484baae 207 if (Led1) len = addChunk(len, " checked='checked'");
andrewboyson 1:94282484baae 208 len = addChunk(len, ">\r\n");
andrewboyson 1:94282484baae 209 lineToSendToId[id] += 1;
andrewboyson 1:94282484baae 210 break;
andrewboyson 6:be97d38e0b01 211 case 7:
andrewboyson 1:94282484baae 212 len = fillChunk(
andrewboyson 1:94282484baae 213 "<input type='submit' value='Set'><br/>\r\n"
andrewboyson 1:94282484baae 214 "</form>\r\n"
andrewboyson 1:94282484baae 215 "</body>\r\n"
andrewboyson 1:94282484baae 216 "</html>\r\n");
andrewboyson 1:94282484baae 217 lineToSendToId[id] = -1;
andrewboyson 1:94282484baae 218 break;
andrewboyson 1:94282484baae 219 }
andrewboyson 1:94282484baae 220 return len;
andrewboyson 1:94282484baae 221 }
andrewboyson 1:94282484baae 222 static int fillSendBuffer(int id)
andrewboyson 1:94282484baae 223 {
andrewboyson 1:94282484baae 224 switch(whatToSendToId[id])
andrewboyson 1:94282484baae 225 {
andrewboyson 1:94282484baae 226 case WHAT_NOT_FOUND: return fillNotFound(id);
andrewboyson 1:94282484baae 227 case WHAT_BAD_REQUEST: return fillBadRequest(id);
andrewboyson 1:94282484baae 228 case WHAT_BAD_METHOD: return fillBadMethod(id);
andrewboyson 1:94282484baae 229 case WHAT_LOG: return fillLog(id);
andrewboyson 1:94282484baae 230 case WHAT_LED: return fillLed(id);
andrewboyson 1:94282484baae 231 default:
andrewboyson 1:94282484baae 232 LogF("No such 'whatToSendToId' %s", whatToSendToId[id]);
andrewboyson 1:94282484baae 233 return -1;
andrewboyson 0:09f915e6f9f6 234 }
andrewboyson 0:09f915e6f9f6 235 }
andrewboyson 1:94282484baae 236 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 237 {
andrewboyson 1:94282484baae 238 char* p = recvbuffer;
andrewboyson 1:94282484baae 239 char* pE = recvbuffer + RECV_BUFFER_SIZE;
andrewboyson 1:94282484baae 240
andrewboyson 1:94282484baae 241 *ppMethod = NULL;
andrewboyson 1:94282484baae 242 *ppPath = NULL;
andrewboyson 1:94282484baae 243 *ppQuery = NULL;
andrewboyson 1:94282484baae 244 *pLenMethod = 0;
andrewboyson 1:94282484baae 245 *pLenPath = 0;
andrewboyson 1:94282484baae 246 *pLenQuery = 0;
andrewboyson 0:09f915e6f9f6 247
andrewboyson 1:94282484baae 248 while (*p == ' ') //Loop to non space
andrewboyson 1:94282484baae 249 {
andrewboyson 1:94282484baae 250 if (p == pE) return -1;
andrewboyson 1:94282484baae 251 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 252 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 253 p++;
andrewboyson 1:94282484baae 254 }
andrewboyson 1:94282484baae 255 *ppMethod = p; //Record the start of the method GET or POST
andrewboyson 1:94282484baae 256
andrewboyson 1:94282484baae 257 while (*p != ' ') //Loop to a space
andrewboyson 1:94282484baae 258 {
andrewboyson 1:94282484baae 259 if (p == pE) return -1;
andrewboyson 1:94282484baae 260 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 261 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 262 p++;
andrewboyson 1:94282484baae 263 }
andrewboyson 1:94282484baae 264 *pLenMethod = p - *ppMethod; //Record the length of the method GET or POST
andrewboyson 0:09f915e6f9f6 265
andrewboyson 1:94282484baae 266
andrewboyson 1:94282484baae 267 while (*p == ' ') //Loop to non space
andrewboyson 1:94282484baae 268 {
andrewboyson 1:94282484baae 269 if (p == pE) return -1;
andrewboyson 1:94282484baae 270 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 271 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 272 p++;
andrewboyson 1:94282484baae 273 }
andrewboyson 1:94282484baae 274 *ppPath = p; //Record the start of the file part of the url
andrewboyson 1:94282484baae 275
andrewboyson 1:94282484baae 276 while (*p != ' ') //Loop to space
andrewboyson 1:94282484baae 277 {
andrewboyson 1:94282484baae 278 if (p == pE) return -1;
andrewboyson 1:94282484baae 279 if (*p < ' ') return -1;
andrewboyson 1:94282484baae 280 if (*p >= 0x7f) return -1;
andrewboyson 1:94282484baae 281 if (*p == '?') *ppQuery = p + 1;
andrewboyson 1:94282484baae 282 p++;
andrewboyson 1:94282484baae 283 }
andrewboyson 1:94282484baae 284
andrewboyson 1:94282484baae 285 if (*ppQuery) //Calulate length of the file and query parts
andrewboyson 0:09f915e6f9f6 286 {
andrewboyson 1:94282484baae 287 *pLenPath = *ppQuery - *ppPath - 1;
andrewboyson 1:94282484baae 288 *pLenQuery = p - *ppQuery;
andrewboyson 1:94282484baae 289 }
andrewboyson 1:94282484baae 290 else
andrewboyson 1:94282484baae 291 {
andrewboyson 1:94282484baae 292 *pLenPath = p - *ppPath;
andrewboyson 1:94282484baae 293 *pLenQuery = 0;
andrewboyson 1:94282484baae 294 }
andrewboyson 1:94282484baae 295
andrewboyson 1:94282484baae 296 return 0;
andrewboyson 1:94282484baae 297 }
andrewboyson 1:94282484baae 298 int compare(char* mem, int len, char* str) //Returns true if same and false if not
andrewboyson 1:94282484baae 299 {
andrewboyson 1:94282484baae 300 if (strlen(str) != len) return false;
andrewboyson 1:94282484baae 301 if (memcmp(mem, str, len)) return false;
andrewboyson 1:94282484baae 302 return true;
andrewboyson 1:94282484baae 303 }
andrewboyson 1:94282484baae 304 static int recvMain()
andrewboyson 1:94282484baae 305 {
andrewboyson 6:be97d38e0b01 306 //Wait for data to be available oneshot. Buffer will normally hit limit.
andrewboyson 6:be97d38e0b01 307 if (!EspDataAvailable) return 0;
andrewboyson 1:94282484baae 308
andrewboyson 1:94282484baae 309 //Ignore ids that have been reserved elsewhere (ie NTP)
andrewboyson 1:94282484baae 310 if (EspIpdReserved[EspIpdId]) return 0;
andrewboyson 1:94282484baae 311
andrewboyson 1:94282484baae 312 //Set up the next line to send to be the first
andrewboyson 1:94282484baae 313 lineToSendToId[EspIpdId] = 1;
andrewboyson 1:94282484baae 314
andrewboyson 1:94282484baae 315 char* pMethod;
andrewboyson 1:94282484baae 316 char* pPath;
andrewboyson 1:94282484baae 317 char* pQuery;
andrewboyson 1:94282484baae 318 int lenMethod;
andrewboyson 1:94282484baae 319 int lenQuery;
andrewboyson 1:94282484baae 320 int lenPath;
andrewboyson 1:94282484baae 321 int r = splitRequest(&pMethod, &pPath, &pQuery, &lenMethod, &lenPath, &lenQuery);
andrewboyson 1:94282484baae 322 if (r)
andrewboyson 1:94282484baae 323 {
andrewboyson 1:94282484baae 324 whatToSendToId[EspIpdId] = WHAT_BAD_REQUEST;
andrewboyson 1:94282484baae 325 return 0;
andrewboyson 1:94282484baae 326 }
andrewboyson 1:94282484baae 327
andrewboyson 1:94282484baae 328 if (!compare(pMethod, lenMethod, "GET"))
andrewboyson 1:94282484baae 329 {
andrewboyson 1:94282484baae 330 whatToSendToId[EspIpdId] = WHAT_BAD_METHOD;
andrewboyson 1:94282484baae 331 return 0;
andrewboyson 1:94282484baae 332 }
andrewboyson 1:94282484baae 333
andrewboyson 1:94282484baae 334 if (compare(pPath, lenPath, "/"))
andrewboyson 1:94282484baae 335 {
andrewboyson 1:94282484baae 336 if (pQuery)
andrewboyson 0:09f915e6f9f6 337 {
andrewboyson 1:94282484baae 338 if (compare(pQuery, lenQuery, "ledonoff=&led=on")) Led1 = 1;
andrewboyson 1:94282484baae 339 if (compare(pQuery, lenQuery, "ledonoff=" )) Led1 = 0;
andrewboyson 0:09f915e6f9f6 340 }
andrewboyson 1:94282484baae 341 whatToSendToId[EspIpdId] = WHAT_LED;
andrewboyson 1:94282484baae 342 return 0;
andrewboyson 1:94282484baae 343 }
andrewboyson 1:94282484baae 344
andrewboyson 1:94282484baae 345 if (compare(pPath, lenPath, "/log"))
andrewboyson 1:94282484baae 346 {
andrewboyson 1:94282484baae 347 whatToSendToId[EspIpdId] = WHAT_LOG;
andrewboyson 1:94282484baae 348 return 0;
andrewboyson 1:94282484baae 349 }
andrewboyson 1:94282484baae 350
andrewboyson 1:94282484baae 351 whatToSendToId[EspIpdId] = WHAT_NOT_FOUND;
andrewboyson 1:94282484baae 352 return 0;
andrewboyson 1:94282484baae 353 }
andrewboyson 1:94282484baae 354 static int sendMain()
andrewboyson 1:94282484baae 355 {
andrewboyson 1:94282484baae 356 //Do nothing if a command is already in progress
andrewboyson 1:94282484baae 357 if (AtBusy()) return 0;
andrewboyson 1:94282484baae 358
andrewboyson 1:94282484baae 359 //Send data. Do each id in turn to avoid interleaved calls to fillers like LogEnumerate which are not reentrant.
andrewboyson 1:94282484baae 360 for(int id = 0; id < 4; id++)
andrewboyson 1:94282484baae 361 {
andrewboyson 1:94282484baae 362 if (lineToSendToId[id] > 0)
andrewboyson 0:09f915e6f9f6 363 {
andrewboyson 1:94282484baae 364 int length = fillSendBuffer(id);
andrewboyson 1:94282484baae 365 if (length < 0) return -1;
andrewboyson 1:94282484baae 366 if (length > 0) AtSendData(id, length, sendbuffer, NULL);
andrewboyson 1:94282484baae 367 }
andrewboyson 1:94282484baae 368 else if (lineToSendToId[id] < 0)
andrewboyson 1:94282484baae 369 {
andrewboyson 1:94282484baae 370 AtClose(id, NULL);
andrewboyson 1:94282484baae 371 lineToSendToId[id] = 0;
andrewboyson 0:09f915e6f9f6 372 }
andrewboyson 0:09f915e6f9f6 373 }
andrewboyson 0:09f915e6f9f6 374
andrewboyson 1:94282484baae 375 return 0;
andrewboyson 1:94282484baae 376 }
andrewboyson 1:94282484baae 377 int startMain()
andrewboyson 1:94282484baae 378 {
andrewboyson 1:94282484baae 379 //Do nothing if a command is already in progress
andrewboyson 1:94282484baae 380 if (AtBusy()) return 0;
andrewboyson 1:94282484baae 381
andrewboyson 1:94282484baae 382 //Do nothing until WiFi is running
andrewboyson 1:94282484baae 383 if (!WifiStarted()) return 0;
andrewboyson 0:09f915e6f9f6 384
andrewboyson 1:94282484baae 385 //Start the server if not started
andrewboyson 1:94282484baae 386 static int startServerReply = AT_NONE;
andrewboyson 1:94282484baae 387 if (startServerReply != AT_SUCCESS) AtStartServer(SERVER_PORT, &startServerReply);
andrewboyson 1:94282484baae 388 return 0;
andrewboyson 1:94282484baae 389 }
andrewboyson 1:94282484baae 390 int ServerMain()
andrewboyson 1:94282484baae 391 {
andrewboyson 1:94282484baae 392 if (startMain()) return -1;
andrewboyson 1:94282484baae 393 if ( recvMain()) return -1;
andrewboyson 1:94282484baae 394 if ( sendMain()) return -1;
andrewboyson 0:09f915e6f9f6 395
andrewboyson 0:09f915e6f9f6 396 return 0;
andrewboyson 0:09f915e6f9f6 397 }