Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Wed Apr 13 09:21:02 2016 +0000
Revision:
0:09f915e6f9f6
Child:
1:94282484baae
Fixed memory allocation issues and added enumeration to log.

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 0:09f915e6f9f6 7 #define BUFFER_SIZE 1024
andrewboyson 0:09f915e6f9f6 8 #define SERVER_PORT 80
andrewboyson 0:09f915e6f9f6 9
andrewboyson 0:09f915e6f9f6 10 static char recvbuffer[BUFFER_SIZE];
andrewboyson 0:09f915e6f9f6 11 static char sendbuffer[BUFFER_SIZE];
andrewboyson 0:09f915e6f9f6 12
andrewboyson 0:09f915e6f9f6 13 void ServerInit(void) //Make sure this is only called after any other ids are reserved.
andrewboyson 0:09f915e6f9f6 14 {
andrewboyson 0:09f915e6f9f6 15 for (int id = 0; id < 4; id++)
andrewboyson 0:09f915e6f9f6 16 {
andrewboyson 0:09f915e6f9f6 17 if (!EspIpdReserved[id])
andrewboyson 0:09f915e6f9f6 18 {
andrewboyson 0:09f915e6f9f6 19 EspIpdBuffer[id] = recvbuffer;
andrewboyson 0:09f915e6f9f6 20 EspIpdBufferLen[id] = BUFFER_SIZE;
andrewboyson 0:09f915e6f9f6 21 }
andrewboyson 0:09f915e6f9f6 22 }
andrewboyson 0:09f915e6f9f6 23 }
andrewboyson 0:09f915e6f9f6 24 int ServerStatus = AT_NONE;
andrewboyson 0:09f915e6f9f6 25 int ServerMain(void)
andrewboyson 0:09f915e6f9f6 26 {
andrewboyson 0:09f915e6f9f6 27
andrewboyson 0:09f915e6f9f6 28 if (!AtBusy() && WifiStarted() && ServerStatus != AT_SUCCESS) AtStartServer(SERVER_PORT, &ServerStatus);
andrewboyson 0:09f915e6f9f6 29
andrewboyson 0:09f915e6f9f6 30 static int needToSendId = -1;
andrewboyson 0:09f915e6f9f6 31 static int needToCloseId = -1;
andrewboyson 0:09f915e6f9f6 32
andrewboyson 0:09f915e6f9f6 33 if (EspDataAvailable == ESP_AVAILABLE && !EspIpdReserved[EspIpdId])
andrewboyson 0:09f915e6f9f6 34 {
andrewboyson 0:09f915e6f9f6 35 needToSendId = EspIpdId;
andrewboyson 0:09f915e6f9f6 36 char method[10];
andrewboyson 0:09f915e6f9f6 37 char url[20];
andrewboyson 0:09f915e6f9f6 38 char file[20];
andrewboyson 0:09f915e6f9f6 39 char query[20];
andrewboyson 0:09f915e6f9f6 40 sscanf(recvbuffer, "%s %s ", method, url);
andrewboyson 0:09f915e6f9f6 41 sscanf(url, "%[^?]?%s", file, query);
andrewboyson 0:09f915e6f9f6 42 if (strcmp(file, "/favicon.ico") == 0)
andrewboyson 0:09f915e6f9f6 43 strcpy(sendbuffer, "HTTP/1.0 404 Not Found\r\n\r\n");
andrewboyson 0:09f915e6f9f6 44 else if (strcmp(file, "/log") == 0)
andrewboyson 0:09f915e6f9f6 45 {
andrewboyson 0:09f915e6f9f6 46 strcpy(sendbuffer, "HTTP/1.0 200 OK\r\n");
andrewboyson 0:09f915e6f9f6 47 strcat(sendbuffer, "\r\n");
andrewboyson 0:09f915e6f9f6 48 strcat(sendbuffer, "<html>\r\n");
andrewboyson 0:09f915e6f9f6 49 strcat(sendbuffer, "<body>\r\n");
andrewboyson 0:09f915e6f9f6 50 strcat(sendbuffer, "<code><pre>");
andrewboyson 0:09f915e6f9f6 51 LogEnumerateStart();
andrewboyson 0:09f915e6f9f6 52 char* p = sendbuffer;
andrewboyson 0:09f915e6f9f6 53 while(*p) p++; //Move to the end of string
andrewboyson 0:09f915e6f9f6 54 while (1)
andrewboyson 0:09f915e6f9f6 55 {
andrewboyson 0:09f915e6f9f6 56 int c = LogEnumerate();
andrewboyson 0:09f915e6f9f6 57 if (c == EOF) break;
andrewboyson 0:09f915e6f9f6 58 *p++ = c;
andrewboyson 0:09f915e6f9f6 59 }
andrewboyson 0:09f915e6f9f6 60 *p = 0; //Add back the end of string
andrewboyson 0:09f915e6f9f6 61 strcat(sendbuffer, "</pre></code>");
andrewboyson 0:09f915e6f9f6 62 strcat(sendbuffer, "</body>\r\n");
andrewboyson 0:09f915e6f9f6 63 strcat(sendbuffer, "</html>\r\n");
andrewboyson 0:09f915e6f9f6 64 }
andrewboyson 0:09f915e6f9f6 65 else
andrewboyson 0:09f915e6f9f6 66 {
andrewboyson 0:09f915e6f9f6 67 if (strcmp(query, "ledonoff=&led=on") == 0) Led1 = 1;
andrewboyson 0:09f915e6f9f6 68 if (strcmp(query, "ledonoff=" ) == 0) Led1 = 0;
andrewboyson 0:09f915e6f9f6 69 strcpy(sendbuffer, "HTTP/1.0 200 OK\r\n");
andrewboyson 0:09f915e6f9f6 70 strcat(sendbuffer, "\r\n");
andrewboyson 0:09f915e6f9f6 71 strcat(sendbuffer, "<html>\r\n");
andrewboyson 0:09f915e6f9f6 72 strcat(sendbuffer, "<body>\r\n");
andrewboyson 0:09f915e6f9f6 73 strcat(sendbuffer, "<form action='/' method='get'>\r\n");
andrewboyson 0:09f915e6f9f6 74 strcat(sendbuffer, "<input type='hidden' name='ledonoff'>\r\n");
andrewboyson 0:09f915e6f9f6 75 if (Led1) strcat(sendbuffer, "Led <input type='checkbox' name='led' value='on' checked='checked'>\r\n");
andrewboyson 0:09f915e6f9f6 76 else strcat(sendbuffer, "Led <input type='checkbox' name='led' value='on'>\r\n");
andrewboyson 0:09f915e6f9f6 77 strcat(sendbuffer, "<input type='submit' value='Set'><br/>\r\n");
andrewboyson 0:09f915e6f9f6 78 strcat(sendbuffer, "</form>\r\n");
andrewboyson 0:09f915e6f9f6 79 strcat(sendbuffer, "</body>\r\n");
andrewboyson 0:09f915e6f9f6 80 strcat(sendbuffer, "</html>\r\n");
andrewboyson 0:09f915e6f9f6 81 // sprintf(sendbuffer, "Received a %s request for %s and query %s\r\n", method, file, query);
andrewboyson 0:09f915e6f9f6 82 }
andrewboyson 0:09f915e6f9f6 83 }
andrewboyson 0:09f915e6f9f6 84
andrewboyson 0:09f915e6f9f6 85 if (!AtBusy() && needToSendId >= 0)
andrewboyson 0:09f915e6f9f6 86 {
andrewboyson 0:09f915e6f9f6 87 AtSendData(needToSendId, strlen(sendbuffer), sendbuffer, NULL);
andrewboyson 0:09f915e6f9f6 88 needToCloseId = needToSendId;
andrewboyson 0:09f915e6f9f6 89 needToSendId = -1;
andrewboyson 0:09f915e6f9f6 90 }
andrewboyson 0:09f915e6f9f6 91
andrewboyson 0:09f915e6f9f6 92 if (!AtBusy() && needToCloseId >= 0)
andrewboyson 0:09f915e6f9f6 93 {
andrewboyson 0:09f915e6f9f6 94 AtClose(needToCloseId, NULL);
andrewboyson 0:09f915e6f9f6 95 needToCloseId = -1;
andrewboyson 0:09f915e6f9f6 96 }
andrewboyson 0:09f915e6f9f6 97
andrewboyson 0:09f915e6f9f6 98
andrewboyson 0:09f915e6f9f6 99 return 0;
andrewboyson 0:09f915e6f9f6 100 }