..;.and now with the changed files too.

pull/30/head
Jeroen Domburg 10 years ago
parent febf58df6e
commit f977678e7f
  1. 1
      driver/uart.c
  2. BIN
      html/cat.jpeg
  3. 2
      include/driver/uart.h
  4. 207
      user/cgi.c
  5. 5
      user/cgi.h
  6. 13
      user/espfs.c
  7. 1
      user/heatshrink_decoder.c
  8. 22
      user/httpd.c
  9. 8
      user/httpdespfs.c
  10. 4
      user/io.c
  11. 12
      user/user_main.c

@ -9,6 +9,7 @@
* Modification history:
* 2014/3/12, v1.0 create this file.
*******************************************************************************/
#include "espmissingincludes.h"
#include "ets_sys.h"
#include "osapi.h"
#include "driver/uart.h"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

@ -97,5 +97,7 @@ typedef struct {
void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
void uart0_sendStr(const char *str);
#endif

@ -4,9 +4,12 @@ Some random cgi routines.
#include <string.h>
#include <osapi.h>
#include "user_interface.h"
#include "mem.h"
#include "httpd.h"
#include "cgi.h"
#include "io.h"
#include "espmissingincludes.h"
int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) {
int len;
@ -18,33 +21,215 @@ int ICACHE_FLASH_ATTR cgiLed(HttpdConnData *connData) {
}
len=httpdFindArg(connData->postBuff, "led", buff, sizeof(buff));
ioLed(atoi(buff));
if (len!=0) ioLed(atoi(buff));
httpdRedirect(connData, "led.html");
return HTTPD_CGI_DONE;
}
int ICACHE_FLASH_ATTR cgiTest(HttpdConnData *connData) {
//WiFi access point data
typedef struct {
char ssid[32];
char rssi;
char enc;
} ApData;
//Scan resolt
typedef struct {
char scanInProgress;
ApData **apData;
int noAps;
} ScanResultData;
//Static scan status storage.
ScanResultData cgiWifiAps;
void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
int n;
struct bss_info *bss_link = (struct bss_info *)arg;
os_printf("wifiScanDoneCb %d\n", status);
if (status!=OK) {
cgiWifiAps.scanInProgress=0;
wifi_station_disconnect(); //test HACK
return;
}
//Clear prev ap data if needed.
if (cgiWifiAps.apData!=NULL) {
for (n=0; n<cgiWifiAps.noAps; n++) os_free(cgiWifiAps.apData[n]);
os_free(cgiWifiAps.apData);
}
//Count amount of access points found.
n=0;
while (bss_link != NULL) {
bss_link = bss_link->next.stqe_next;
n++;
}
//Allocate memory for access point data
cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n);
cgiWifiAps.noAps=n;
//Copy access point data to the static struct
n=0;
bss_link = (struct bss_info *)arg;
while (bss_link != NULL) {
cgiWifiAps.apData[n]=(ApData *)os_malloc(sizeof(ApData));
cgiWifiAps.apData[n]->rssi=bss_link->rssi;
cgiWifiAps.apData[n]->enc=bss_link->authmode;
strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32);
bss_link = bss_link->next.stqe_next;
n++;
}
os_printf("Scan done: found %d APs\n", n);
//We're done.
cgiWifiAps.scanInProgress=0;
}
static void ICACHE_FLASH_ATTR wifiStartScan() {
int x;
cgiWifiAps.scanInProgress=1;
x=wifi_station_get_connect_status();
if (x!=STATION_GOT_IP) {
//Unit probably is trying to connect to a bogus AP. This messes up scanning. Stop that.
os_printf("STA status = %d. Disconnecting STA...\n", x);
wifi_station_disconnect();
}
wifi_station_scan(NULL, wifiScanDoneCb);
}
int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
int len;
char val1[128];
char val2[128];
int i;
char buff[1024];
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/json");
httpdEndHeaders(connData);
if (cgiWifiAps.scanInProgress==1) {
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"1\"\n }\n}\n");
espconn_sent(connData->conn, (uint8 *)buff, len);
} else {
len=os_sprintf(buff, "{\n \"result\": { \n\"inProgress\": \"0\", \n\"APs\": [\n");
espconn_sent(connData->conn, (uint8 *)buff, len);
if (cgiWifiAps.apData==NULL) cgiWifiAps.noAps=0;
for (i=0; i<cgiWifiAps.noAps; i++) {
len=os_sprintf(buff, "{\"essid\": \"%s\", \"rssi\": \"%d\", \"enc\": \"%d\"}%s\n",
cgiWifiAps.apData[i]->ssid, cgiWifiAps.apData[i]->rssi,
cgiWifiAps.apData[i]->enc, (i==cgiWifiAps.noAps-1)?"":",");
espconn_sent(connData->conn, (uint8 *)buff, len);
}
len=os_sprintf(buff, "]\n}\n}\n");
espconn_sent(connData->conn, (uint8 *)buff, len);
wifiStartScan();
}
return HTTPD_CGI_DONE;
}
static struct station_config stconf;
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
int x=wifi_station_get_connect_status();
if (x==STATION_GOT_IP) {
//Go to STA mode. This needs a reset, so do that.
wifi_set_opmode(1);
system_restart();
} else {
os_printf("Connect fail. Not going into STA-only mode.\n");
}
}
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
int x;
static ETSTimer resetTimer;
wifi_station_disconnect();
wifi_station_set_config(&stconf);
wifi_station_connect();
x=wifi_get_opmode();
if (x!=1) {
//Schedule disconnect/connect
os_timer_disarm(&resetTimer);
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
os_timer_arm(&resetTimer, 4000, 0);
}
}
int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
char essid[128];
char passwd[128];
static ETSTimer reassTimer;
if (connData->conn==NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/plain");
httpdEndHeaders(connData);
httpdFindArg(connData->postBuff, "essid", essid, sizeof(essid));
httpdFindArg(connData->postBuff, "passwd", passwd, sizeof(passwd));
os_strncpy((char*)stconf.ssid, essid, 32);
os_strncpy((char*)stconf.password, passwd, 64);
//Schedule disconnect/connect
os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, reassTimerCb, NULL);
os_timer_arm(&reassTimer, 1000, 0);
httpdRedirect(connData, "connecting.html");
return HTTPD_CGI_DONE;
}
//Cgi that reads the SPI flash. Assumes 512KByte flash.
int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) {
int *pos=(int *)&connData->cgiData;
if (connData->conn==NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
httpdFindArg(connData->postBuff, "Test1", val1, sizeof(val1));
httpdFindArg(connData->postBuff, "Test2", val2, sizeof(val2));
len=os_sprintf(buff, "Field 1: %s\nField 2: %s\n", val1, val2);
espconn_sent(connData->conn, (uint8 *)buff, len);
if (*pos==0) {
os_printf("Start flash download.\n");
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "application/bin");
httpdEndHeaders(connData);
*pos=0x40200000;
return HTTPD_CGI_MORE;
}
espconn_sent(connData->conn, (uint8 *)(*pos), 1024);
*pos+=1024;
if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE;
}
int ICACHE_FLASH_ATTR cgiTest(HttpdConnData *connData) {
return HTTPD_CGI_DONE;
}
void ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg) {
char buff[1024];
int x;
static struct station_config stconf;
if (token==NULL) return;
wifi_station_get_config(&stconf);
os_strcpy(buff, "Unknown");
if (os_strcmp(token, "WiFiMode")==0) {
x=wifi_get_opmode();
if (x==1) os_strcpy(buff, "Client");
if (x==2) os_strcpy(buff, "SoftAP");
if (x==3) os_strcpy(buff, "STA+AP");
} else if (os_strcmp(token, "currSsid")==0) {
os_strcpy(buff, (char*)stconf.ssid);
} else if (os_strcmp(token, "WiFiPasswd")==0) {
os_strcpy(buff, (char*)stconf.password);
}
espconn_sent(connData->conn, (uint8 *)buff, os_strlen(buff));
}

@ -4,6 +4,11 @@
#include "httpd.h"
int cgiLed(HttpdConnData *connData);
int cgiReadFlash(HttpdConnData *connData);
int cgiTest(HttpdConnData *connData);
int cgiWiFiScan(HttpdConnData *connData);
void ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg);
int ICACHE_FLASH_ATTR cgiWiFi(HttpdConnData *connData);
int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData);
#endif

@ -12,6 +12,7 @@ It's written for use with httpd, but doesn't need to be used as such.
#include "espconn.h"
#include "mem.h"
#include "osapi.h"
#include "espmissingincludes.h"
#else
//Test build
#include <stdio.h>
@ -109,7 +110,8 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
}
p+=sizeof(EspFsHeader);
os_memcpy(namebuf, p, sizeof(namebuf));
os_printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n", namebuf, h.nameLen,h.fileLenComp,h.compression,h.flags);
os_printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n",
namebuf, (unsigned int)h.nameLen, (unsigned int)h.fileLenComp, h.compression, h.flags);
if (os_strcmp(namebuf, fileName)==0) {
p+=h.nameLen;
r=(EspFsFile *)os_malloc(sizeof(EspFsFile));
@ -155,7 +157,7 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
int toRead;
toRead=flen-(fh->posComp-fh->posStart);
if (len>toRead) len=toRead;
os_printf("Reading %d bytes from %x\n", len, fh->posComp);
os_printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp);
memcpyAligned(buff, fh->posComp, len);
fh->posDecomp+=len;
fh->posComp+=len;
@ -164,23 +166,24 @@ int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
#ifdef EFS_HEATSHRINK
} else if (fh->decompressor==COMPRESS_HEATSHRINK) {
int decoded=0;
int elen, rlen, r;
unsigned int elen, rlen;
char ebuff[16];
heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData;
while(decoded<len) {
//Feed data into the decompressor
//ToDo: Check ret val of heatshrink fns for errors
elen=flen-(fh->posComp - fh->posStart);
if (elen==0) return decoded; //file is read
if (elen>0) {
memcpyAligned(ebuff, fh->posComp, 16);
r=heatshrink_decoder_sink(dec, ebuff, (elen>16)?16:elen, &rlen);
heatshrink_decoder_sink(dec, (uint8_t *)ebuff, (elen>16)?16:elen, &rlen);
fh->posComp+=rlen;
if (rlen==elen) {
heatshrink_decoder_finish(dec);
}
}
//Grab decompressed data and put into buff
r=heatshrink_decoder_poll(dec, buff, len-decoded, &rlen);
heatshrink_decoder_poll(dec, (uint8_t *)buff, len-decoded, &rlen);
fh->posDecomp+=rlen;
buff+=rlen;
decoded+=rlen;

@ -8,6 +8,7 @@
#define _STDDEF_H
#define _STDINT_H
#include "espmissingincludes.h"
#include "c_types.h"
#include "mem.h"
#include "osapi.h"

@ -1,3 +1,4 @@
#include "espmissingincludes.h"
#include "driver/uart.h"
#include "c_types.h"
#include "user_interface.h"
@ -43,6 +44,7 @@ typedef struct {
static const MimeMap mimeTypes[]={
{"htm", "text/htm"},
{"html", "text/html"},
{"js", "text/javascript"},
{"txt", "text/plain"},
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
@ -83,6 +85,7 @@ static int httpdHexVal(char c) {
if (c>='0' && c<='9') return c-'0';
if (c>='A' && c<='F') return c-'A'+10;
if (c>='a' && c<='f') return c-'a'+10;
return 0;
}
//Decode a percent-encoded value
@ -107,13 +110,13 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen) {
s++;
}
if (d<retLen) ret[d]=0;
return d;
}
//Find a specific arg in a string of get- or post-data.
//Returns len of arg or -1 if not found.
int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen) {
char *p, *e;
int len;
if (line==NULL) return 0;
p=line;
while(p!=NULL && *p!='\n' && *p!='\r' && *p!=0) {
@ -138,7 +141,7 @@ void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) {
char buff[128];
int l;
l=os_sprintf(buff, "HTTP/1.0 %d OK\r\nServer: esp8266-httpd/0.1\r\n", code);
espconn_sent(conn->conn, buff, l);
espconn_sent(conn->conn, (uint8 *)buff, l);
}
@ -146,11 +149,11 @@ void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const
char buff[256];
int l;
l=os_sprintf(buff, "%s: %s\r\n", field, val);
espconn_sent(conn->conn, buff, l);
espconn_sent(conn->conn, (uint8 *)buff, l);
}
void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn) {
espconn_sent(conn->conn, "\r\n", 2);
espconn_sent(conn->conn, (uint8 *)"\r\n", 2);
}
//ToDo: sprintf->snprintf everywhere
@ -158,13 +161,10 @@ void ICACHE_FLASH_ATTR httpdRedirect(HttpdConnData *conn, char *newUrl) {
char buff[1024];
int l;
l=os_sprintf(buff, "HTTP/1.1 302 Found\r\nLocation: %s\r\n\r\nMoved to %s\r\n", newUrl, newUrl);
espconn_sent(conn->conn, buff, l);
espconn_sent(conn->conn, (uint8 *)buff, l);
}
int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) {
int len;
char buff[1024];
if (connData->conn==NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
@ -198,7 +198,7 @@ static void ICACHE_FLASH_ATTR httpdSendResp(HttpdConnData *conn) {
int r;
//See if the url is somewhere in our internal url table.
while (builtInUrls[i].url!=NULL && conn->url!=NULL) {
os_printf("%s == %s?\n", builtInUrls[i].url, conn->url);
// os_printf("%s == %s?\n", builtInUrls[i].url, conn->url);
if (os_strcmp(builtInUrls[i].url, conn->url)==0 || builtInUrls[i].url[0]=='*') {
os_printf("Is url index %d\n", i);
conn->cgiData=NULL;
@ -237,7 +237,6 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
os_printf("URL = %s\n", conn->url);
conn->getArgs=(char*)os_strstr(conn->url, "?");
if (conn->getArgs!=0) {
int x,l;
*conn->getArgs=0;
conn->getArgs++;
os_printf("GET args = %s\n", conn->getArgs);
@ -256,7 +255,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
}
static void ICACHE_FLASH_ATTR httpdRecvCb(void *arg, char *data, unsigned short len) {
int x, l;
int x;
char *p, *e;
HttpdConnData *conn=httpdFindConnData(arg);
if (conn==NULL) return;
@ -336,7 +335,6 @@ static void ICACHE_FLASH_ATTR httpdDisconCb(void *arg) {
static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) {
struct espconn *conn=arg;
HttpdConnData *conndata;
int i;
//Find empty conndata in pool
for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break;

@ -2,6 +2,7 @@
Connector to let httpd use the espfs filesystem to serve the files in that.
*/
#include "espmissingincludes.h"
#include <string.h>
#include <osapi.h>
#include "c_types.h"
@ -58,14 +59,13 @@ typedef struct {
int tokenPos;
} TplData;
typedef int (* TplCallback)(HttpdConnData *connData, char *token, void **arg);
typedef void (* TplCallback)(HttpdConnData *connData, char *token, void **arg);
int ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData) {
TplData *tpd=connData->cgiData;
int len;
int x, sp;
char *p, *e;
int sentTo;
int x, sp=0;
char *e=NULL;
char buff[1025];
if (connData->conn==NULL) {

@ -1,3 +1,4 @@
#include "espmissingincludes.h"
#include "c_types.h"
#include "user_interface.h"
#include "espconn.h"
@ -20,12 +21,13 @@ void ICACHE_FLASH_ATTR ioLed(int ena) {
}
}
static void ICACHE_FLASH_ATTR resetBtnTimerCb(void) {
static void ICACHE_FLASH_ATTR resetBtnTimerCb(void *arg) {
static int resetCnt=0;
if (!GPIO_INPUT_GET(BTNGPIO)) {
resetCnt++;
} else {
if (resetCnt>=6) { //3 sec pressed
wifi_station_disconnect();
wifi_set_opmode(0x3); //reset to AP+STA mode
os_printf("Reset to AP mode. Restarting system...\n");
system_restart();

@ -1,3 +1,4 @@
#include "espmissingincludes.h"
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
@ -7,9 +8,16 @@
#include "cgi.h"
HttpdBuiltInUrl builtInUrls[]={
// {"/", cgiLiteral, "Lalala etc"},
{"/", cgiRedirect, "/test2.html"},
{"/flash.bin", cgiReadFlash, NULL},
{"/led.cgi", cgiLed, NULL},
{"/test.cgi", cgiTest, NULL},
{"/wifi", cgiRedirect, "/wifi/wifi.tpl"},
{"/wifi/", cgiRedirect, "/wifi/wifi.tpl"},
{"/wifi/wifiscan.cgi", cgiWiFiScan, NULL},
{"/wifi/wifi.tpl", cgiEspFsTemplate, tplWlan},
{"/wifi/connect.cgi", cgiWiFiConnect},
{"*", cgiEspFsHook, NULL},
{NULL, NULL, NULL}
};

Loading…
Cancel
Save