mirror of https://github.com/jeelabs/esp-link.git
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.
134 lines
4.3 KiB
134 lines
4.3 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.
|
|
* ----------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
#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 "uart.h"
|
|
#include "serbridge.h"
|
|
#include "status.h"
|
|
#include "console.h"
|
|
#define MCU_RESET 12
|
|
#define MCU_ISP 13
|
|
#include <gpio.h>
|
|
|
|
//#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;
|
|
}
|
|
|
|
|
|
/*
|
|
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[]={
|
|
{"/", cgiRedirect, "/index.tpl"},
|
|
{"/flash/download", cgiReadFlash, NULL},
|
|
{"/flash/next", cgiGetFirmwareNext, NULL},
|
|
{"/flash/upload", cgiUploadFirmware, NULL},
|
|
{"/flash/reboot", cgiRebootFirmware, NULL},
|
|
{"/led.tpl", cgiEspFsTemplate, tplLed},
|
|
{"/index.tpl", cgiEspFsTemplate, tplCounter},
|
|
{"/led.cgi", cgiLed, NULL},
|
|
{"/console.tpl", cgiEspFsTemplate, tplConsole},
|
|
|
|
//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.
|
|
// {"/wifi/*", authBasic, myPassFn},
|
|
|
|
{"/wifi", cgiRedirect, "/wifi/wifi.tpl"},
|
|
{"/wifi/", cgiRedirect, "/wifi/wifi.tpl"},
|
|
{"/wifi/wifiscan.cgi", cgiWiFiScan, NULL},
|
|
{"/wifi/wifi.tpl", cgiEspFsTemplate, tplWlan},
|
|
{"/wifi/connect.cgi", cgiWiFiConnect, NULL},
|
|
{"/wifi/connstatus.cgi", cgiWiFiConnStatus, NULL},
|
|
{"/wifi/setmode.cgi", cgiWiFiSetMode, NULL},
|
|
|
|
{"*", cgiEspFsHook, NULL}, //Catch-all cgi function for the filesystem
|
|
{NULL, NULL, NULL}
|
|
};
|
|
|
|
|
|
#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
|
|
|
|
void user_rf_pre_init(void) {
|
|
}
|
|
|
|
// address of espfs binary blob
|
|
extern uint32_t _binary_espfs_img_start;
|
|
|
|
//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
|
|
void user_init(void) {
|
|
// init gpio pins used to reset&reprogram attached microcontrollers
|
|
gpio_init();
|
|
GPIO_OUTPUT_SET(MCU_ISP, 1);
|
|
GPIO_OUTPUT_SET(MCU_RESET, 0);
|
|
// init UART
|
|
uart_init(BIT_RATE_115200, BIT_RATE_115200);
|
|
// say hello (leave some time to cause break in TX after boot loader's msg
|
|
os_delay_us(10000L);
|
|
os_printf("\n\nInitializing esp-link\n");
|
|
// Status LEDs
|
|
statusInit();
|
|
// Wifi
|
|
wifiInit();
|
|
// init the flash filesystem with the html stuff
|
|
EspFsInitResult res = espFsInit(&_binary_espfs_img_start);
|
|
os_printf("espFsInit(0x%08lx) returned %d\n", (uint32_t)&_binary_espfs_img_start, res);
|
|
// mount the http handlers
|
|
httpdInit(builtInUrls, 80);
|
|
// init the wifi-serial transparent bridge (port 23)
|
|
serbridgeInit(23);
|
|
uart_add_recv_cb(&serbridgeUartCb);
|
|
#ifdef SHOW_HEAP_USE
|
|
os_timer_disarm(&prHeapTimer);
|
|
os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
|
|
os_timer_arm(&prHeapTimer, 3000, 1);
|
|
#endif
|
|
os_printf("** esp-link ready\n");
|
|
consoleInit();
|
|
}
|
|
|