Backing up an unused program in case of future need

Dependencies:   mbed

server.cpp

Committer:
andrewboyson
Date:
2016-04-13
Revision:
0:09f915e6f9f6
Child:
1:94282484baae

File content as of revision 0:09f915e6f9f6:

#include  "esp.h"
#include   "at.h"
#include   "io.h"
#include  "log.h"
#include "time.h"
#include "wifi.h"
#define BUFFER_SIZE 1024
#define SERVER_PORT 80

static char recvbuffer[BUFFER_SIZE];
static char sendbuffer[BUFFER_SIZE];

void ServerInit(void) //Make sure this is only called after any other ids are reserved.
{
    for (int id = 0; id < 4; id++)
    {
        if (!EspIpdReserved[id])
        {
            EspIpdBuffer[id] = recvbuffer;
            EspIpdBufferLen[id] = BUFFER_SIZE;
        }
    }
}
int ServerStatus = AT_NONE;
int ServerMain(void)
{

    if (!AtBusy() && WifiStarted() && ServerStatus != AT_SUCCESS) AtStartServer(SERVER_PORT, &ServerStatus);
        
    static int needToSendId = -1;
    static int needToCloseId  = -1;
    
    if (EspDataAvailable == ESP_AVAILABLE && !EspIpdReserved[EspIpdId])
    {
        needToSendId = EspIpdId;
        char method[10];
        char url[20];
        char file[20];
        char query[20];
        sscanf(recvbuffer, "%s %s ", method, url);
        sscanf(url, "%[^?]?%s", file, query);
        if (strcmp(file, "/favicon.ico") == 0)
            strcpy(sendbuffer, "HTTP/1.0 404 Not Found\r\n\r\n");
        else if (strcmp(file, "/log") == 0)
        {
            strcpy(sendbuffer, "HTTP/1.0 200 OK\r\n");
            strcat(sendbuffer, "\r\n");
            strcat(sendbuffer, "<html>\r\n");
            strcat(sendbuffer, "<body>\r\n");
            strcat(sendbuffer, "<code><pre>");
            LogEnumerateStart();
            char* p = sendbuffer;
            while(*p) p++; //Move to the end of string
            while (1)
            {
                int c = LogEnumerate();
                if (c == EOF) break;
                *p++ = c;
            }
            *p = 0; //Add back the end of string
            strcat(sendbuffer, "</pre></code>");
            strcat(sendbuffer, "</body>\r\n");
            strcat(sendbuffer, "</html>\r\n");
        }
        else
        {
            if (strcmp(query, "ledonoff=&led=on") == 0) Led1 = 1;
            if (strcmp(query, "ledonoff="       ) == 0) Led1 = 0;
            strcpy(sendbuffer, "HTTP/1.0 200 OK\r\n");
            strcat(sendbuffer, "\r\n");
            strcat(sendbuffer, "<html>\r\n");
            strcat(sendbuffer, "<body>\r\n");
            strcat(sendbuffer, "<form action='/' method='get'>\r\n");
            strcat(sendbuffer, "<input type='hidden' name='ledonoff'>\r\n");
            if (Led1) strcat(sendbuffer, "Led <input type='checkbox' name='led' value='on' checked='checked'>\r\n");
            else      strcat(sendbuffer, "Led <input type='checkbox' name='led' value='on'>\r\n");
            strcat(sendbuffer, "<input type='submit' value='Set'><br/>\r\n");
            strcat(sendbuffer, "</form>\r\n");
            strcat(sendbuffer, "</body>\r\n");
            strcat(sendbuffer, "</html>\r\n");
//            sprintf(sendbuffer, "Received a %s request for %s and query %s\r\n", method, file, query);
        }
    }

    if (!AtBusy() && needToSendId >= 0)
    {
        AtSendData(needToSendId, strlen(sendbuffer), sendbuffer, NULL);
        needToCloseId = needToSendId;
        needToSendId = -1;
    }
    
    if (!AtBusy() && needToCloseId >= 0)
    {
        AtClose(needToCloseId, NULL);
        needToCloseId = -1;
    }
    
    
    return 0;
}