You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							193 lines
						
					
					
						
							6.1 KiB
						
					
					
				
			
		
		
	
	
							193 lines
						
					
					
						
							6.1 KiB
						
					
					
				/*
 | 
						|
 * ----------------------------------------------------------------------------
 | 
						|
 * "THE BEER-WARE LICENSE" (Revision 42):
 | 
						|
 * Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain 
 | 
						|
 * this notice you can do whatever you want with this stuff. If we meet some day, 
 | 
						|
 * and you think this stuff is worth it, you can buy me a beer in return. 
 | 
						|
 * ----------------------------------------------------------------------------
 | 
						|
 */
 | 
						|
 | 
						|
/*
 | 
						|
This is example code for the esphttpd library. It's a small-ish demo showing off 
 | 
						|
the server, including WiFi connection management capabilities, some IO and
 | 
						|
some pictures of cats.
 | 
						|
*/
 | 
						|
 | 
						|
#include <esp8266.h>
 | 
						|
#include "httpd.h"
 | 
						|
#include "io.h"
 | 
						|
#include "httpdespfs.h"
 | 
						|
#include "cgi.h"
 | 
						|
#include "cgiwifi.h"
 | 
						|
#include "cgiflash.h"
 | 
						|
#include "stdout.h"
 | 
						|
#include "auth.h"
 | 
						|
#include "espfs.h"
 | 
						|
#include "captdns.h"
 | 
						|
#include "webpages-espfs.h"
 | 
						|
#include "cgiwebsocket.h"
 | 
						|
#include "cgi-test.h"
 | 
						|
 | 
						|
//The example can print out the heap use every 3 seconds. You can use this to catch memory leaks.
 | 
						|
//#define SHOW_HEAP_USE
 | 
						|
 | 
						|
//Function that tells the authentication system what users/passwords live on the system.
 | 
						|
//This is disabled in the default build; if you want to try it, enable the authBasic line in
 | 
						|
//the builtInUrls below.
 | 
						|
int myPassFn(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen) {
 | 
						|
	if (no==0) {
 | 
						|
		os_strcpy(user, "admin");
 | 
						|
		os_strcpy(pass, "s3cr3t");
 | 
						|
		return 1;
 | 
						|
//Add more users this way. Check against incrementing no for each user added.
 | 
						|
//	} else if (no==1) {
 | 
						|
//		os_strcpy(user, "user1");
 | 
						|
//		os_strcpy(pass, "something");
 | 
						|
//		return 1;
 | 
						|
	}
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
static ETSTimer websockTimer;
 | 
						|
 | 
						|
//Broadcast the uptime in seconds every second over connected websockets
 | 
						|
static void ICACHE_FLASH_ATTR websockTimerCb(void *arg) {
 | 
						|
	static int ctr=0;
 | 
						|
	char buff[128];
 | 
						|
	ctr++;
 | 
						|
	os_sprintf(buff, "Up for %d minutes %d seconds!\n", ctr/60, ctr%60);
 | 
						|
	cgiWebsockBroadcast("/websocket/ws.cgi", buff, os_strlen(buff), WEBSOCK_FLAG_NONE);
 | 
						|
}
 | 
						|
 | 
						|
//On reception of a message, send "You sent: " plus whatever the other side sent
 | 
						|
void myWebsocketRecv(Websock *ws, char *data, int len, int flags) {
 | 
						|
	int i;
 | 
						|
	char buff[128];
 | 
						|
	os_sprintf(buff, "You sent: ");
 | 
						|
	for (i=0; i<len; i++) buff[i+10]=data[i];
 | 
						|
	buff[i+10]=0;
 | 
						|
	cgiWebsocketSend(ws, buff, os_strlen(buff), WEBSOCK_FLAG_NONE);
 | 
						|
}
 | 
						|
 | 
						|
//Websocket connected. Install reception handler and send welcome message.
 | 
						|
void myWebsocketConnect(Websock *ws) {
 | 
						|
	ws->recvCb=myWebsocketRecv;
 | 
						|
	cgiWebsocketSend(ws, "Hi, Websocket!", 14, WEBSOCK_FLAG_NONE);
 | 
						|
}
 | 
						|
 | 
						|
//On reception of a message, echo it back verbatim
 | 
						|
void myEchoWebsocketRecv(Websock *ws, char *data, int len, int flags) {
 | 
						|
	os_printf("EchoWs: echo, len=%d\n", len);
 | 
						|
	cgiWebsocketSend(ws, data, len, flags);
 | 
						|
}
 | 
						|
 | 
						|
//Echo websocket connected. Install reception handler.
 | 
						|
void myEchoWebsocketConnect(Websock *ws) {
 | 
						|
	os_printf("EchoWs: connect\n");
 | 
						|
	ws->recvCb=myEchoWebsocketRecv;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
#ifdef ESPFS_POS
 | 
						|
CgiUploadFlashDef uploadParams={
 | 
						|
	.type=CGIFLASH_TYPE_ESPFS,
 | 
						|
	.fw1Pos=ESPFS_POS,
 | 
						|
	.fw2Pos=0,
 | 
						|
	.fwSize=ESPFS_SIZE,
 | 
						|
};
 | 
						|
#define INCLUDE_FLASH_FNS
 | 
						|
#endif
 | 
						|
#ifdef OTA_FLASH_SIZE_K
 | 
						|
CgiUploadFlashDef uploadParams={
 | 
						|
	.type=CGIFLASH_TYPE_FW,
 | 
						|
	.fw1Pos=0x1000,
 | 
						|
	.fw2Pos=((OTA_FLASH_SIZE_K*1024)/2)+0x1000,
 | 
						|
	.fwSize=((OTA_FLASH_SIZE_K*1024)/2)-0x1000,
 | 
						|
	.tagName=OTA_TAGNAME
 | 
						|
};
 | 
						|
#define INCLUDE_FLASH_FNS
 | 
						|
#endif
 | 
						|
 | 
						|
/*
 | 
						|
This is the main url->function dispatching data struct.
 | 
						|
In short, it's a struct with various URLs plus their handlers. The handlers can
 | 
						|
be 'standard' CGI functions you wrote, or 'special' CGIs requiring an argument.
 | 
						|
They can also be auth-functions. An asterisk will match any url starting with
 | 
						|
everything before the asterisks; "*" matches everything. The list will be
 | 
						|
handled top-down, so make sure to put more specific rules above the more
 | 
						|
general ones. Authorization things (like authBasic) act as a 'barrier' and
 | 
						|
should be placed above the URLs they protect.
 | 
						|
*/
 | 
						|
HttpdBuiltInUrl builtInUrls[]={
 | 
						|
	ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp8266.nonet"), // redirect func for the captive portal
 | 
						|
	ROUTE_REDIRECT("/", "/index.tpl"),
 | 
						|
	ROUTE_TPL("/led.tpl", tplLed),
 | 
						|
	ROUTE_TPL("/index.tpl", tplCounter),
 | 
						|
	ROUTE_CGI("/led.cgi", cgiLed),
 | 
						|
#ifdef INCLUDE_FLASH_FNS
 | 
						|
	ROUTE_CGI_ARG("/flash/next", cgiGetFirmwareNext, &uploadParams),
 | 
						|
	ROUTE_CGI_ARG("/flash/upload", cgiUploadFirmware, &uploadParams),
 | 
						|
#endif
 | 
						|
	ROUTE_CGI("/flash/reboot", cgiRebootFirmware),
 | 
						|
 | 
						|
	//Routines to make the /wifi URL and everything beneath it work.
 | 
						|
 | 
						|
//Enable the line below to protect the WiFi configuration with an username/password combo.
 | 
						|
//	ROUTE_AUTH("/wifi/*", myPassFn),
 | 
						|
 | 
						|
	ROUTE_REDIRECT("/wifi", "/wifi/wifi.tpl"),
 | 
						|
	ROUTE_REDIRECT("/wifi/", "/wifi/wifi.tpl"),
 | 
						|
	ROUTE_CGI("/wifi/wifiscan.cgi", cgiWiFiScan),
 | 
						|
	ROUTE_TPL("/wifi/wifi.tpl", tplWlan),
 | 
						|
	ROUTE_CGI("/wifi/connect.cgi", cgiWiFiConnect),
 | 
						|
	ROUTE_CGI("/wifi/connstatus.cgi", cgiWiFiConnStatus),
 | 
						|
	ROUTE_CGI("/wifi/setmode.cgi", cgiWiFiSetMode),
 | 
						|
 | 
						|
	ROUTE_WS("/websocket/ws.cgi", myWebsocketConnect),
 | 
						|
	ROUTE_WS("/websocket/echo.cgi", myEchoWebsocketConnect),
 | 
						|
 | 
						|
	ROUTE_REDIRECT("/test", "/test/index.html"),
 | 
						|
	ROUTE_REDIRECT("/test/", "/test/index.html"),
 | 
						|
	ROUTE_CGI("/test/test.cgi", cgiTestbed),
 | 
						|
 | 
						|
	ROUTE_FILESYSTEM(),
 | 
						|
	ROUTE_END(),
 | 
						|
};
 | 
						|
 | 
						|
 | 
						|
#ifdef SHOW_HEAP_USE
 | 
						|
static ETSTimer prHeapTimer;
 | 
						|
 | 
						|
static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) {
 | 
						|
	os_printf("Heap: %ld\n", (unsigned long)system_get_free_heap_size());
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
 | 
						|
void user_init(void) {
 | 
						|
	stdoutInit();
 | 
						|
	ioInit();
 | 
						|
	captdnsInit();
 | 
						|
 | 
						|
	// 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position
 | 
						|
	// where image is written in flash that is defined in Makefile.
 | 
						|
#ifdef ESPFS_POS
 | 
						|
	espFsInit((void*)(0x40200000 + ESPFS_POS));
 | 
						|
#else
 | 
						|
	espFsInit((void*)(webpages_espfs_start));
 | 
						|
#endif
 | 
						|
	httpdInit(builtInUrls, 80);
 | 
						|
#ifdef SHOW_HEAP_USE
 | 
						|
	os_timer_disarm(&prHeapTimer);
 | 
						|
	os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
 | 
						|
	os_timer_arm(&prHeapTimer, 3000, 1);
 | 
						|
#endif
 | 
						|
	os_timer_disarm(&websockTimer);
 | 
						|
	os_timer_setfn(&websockTimer, websockTimerCb, NULL);
 | 
						|
	os_timer_arm(&websockTimer, 1000, 1);
 | 
						|
	os_printf("\nReady\n");
 | 
						|
}
 | 
						|
 | 
						|
void user_rf_pre_init() {
 | 
						|
	//Not needed, but some SDK versions want this defined.
 | 
						|
}
 | 
						|
 |