├── .gitignore ├── .gitmodules ├── 40B_ESP_Smart_Plug__User_Guide__EN_v1.1.pdf ├── License ├── Makefile ├── README.md ├── bin ├── blank.bin ├── boot_v1.6.bin └── esp_init_data_default.bin ├── driver ├── Makefile ├── gpio.c ├── i2c_master.c ├── key.c └── uart.c ├── gen_misc.bat ├── gen_misc.sh ├── html_light ├── 140medley.min.js ├── index.html ├── picker.js └── wifi │ ├── 140medley.min.js │ ├── connecting.html │ ├── icons.png │ ├── style.css │ └── wifi.tpl ├── html_plug ├── 140medley.min.js ├── index.html ├── plug_off.png ├── plug_on.png └── wifi │ ├── 140medley.min.js │ ├── connecting.html │ ├── icons.png │ ├── style.css │ └── wifi.tpl ├── include ├── driver │ ├── gpio.h │ ├── i2c_master.h │ ├── key.h │ ├── uart.h │ └── uart_register.h ├── ssl │ ├── cert.h │ └── private_key.h ├── user_cgi.h ├── user_config.h ├── user_configstore.h ├── user_devicefind.h ├── user_esp_platform.h ├── user_esp_platform_timer.h ├── user_iot_version.h ├── user_light.h ├── user_light_action.h ├── user_light_adj.h ├── user_plug.h ├── user_sensor.h ├── user_switch.h └── user_webserver.h ├── libesphttpd ├── Makefile ├── core │ ├── auth.c │ ├── base64.c │ ├── base64.h │ ├── httpd.c │ └── httpdespfs.c ├── espfs │ ├── espfs.c │ ├── espfsformat.h │ ├── espfstest │ │ ├── Makefile │ │ └── main.c │ ├── heatshrink_config_custom.h │ ├── heatshrink_decoder.c │ └── mkespfsimage │ │ ├── Makefile │ │ ├── heatshrink_encoder.c │ │ └── main.c ├── include │ ├── auth.h │ ├── captdns.h │ ├── cgiflash.h │ ├── cgiwifi.h │ ├── esp8266.h │ ├── espfs.h │ ├── espmissingincludes.h │ ├── httpd.h │ ├── httpdespfs.h │ ├── user_config.h │ └── webpages-espfs.h ├── lib │ └── heatshrink │ │ ├── .travis.yml │ │ ├── LICENSE │ │ ├── Makefile │ │ ├── README.md │ │ ├── dec_sm.dot │ │ ├── enc_sm.dot │ │ ├── greatest.h │ │ ├── heatshrink.c │ │ ├── heatshrink_common.h │ │ ├── heatshrink_config.h │ │ ├── heatshrink_decoder.c │ │ ├── heatshrink_decoder.h │ │ ├── heatshrink_encoder.c │ │ ├── heatshrink_encoder.h │ │ ├── test_heatshrink_dynamic.c │ │ ├── test_heatshrink_dynamic_theft.c │ │ └── test_heatshrink_static.c ├── util │ ├── captdns.c │ ├── cgiflash.c │ └── cgiwifi.c └── webpages.espfs.ld ├── upgrade ├── Makefile ├── upgrade.c ├── upgrade_crc32.c └── upgrade_lib.c └── user ├── Makefile ├── user_cgi.c ├── user_configstore.c ├── user_devicefind.c ├── user_esp_platform.c ├── user_esp_platform_timer.c ├── user_light.c ├── user_light_adj.c ├── user_main.c ├── user_plug.c ├── user_sensor.c └── user_webserver.c /.gitignore: -------------------------------------------------------------------------------- 1 | # TO Delete if release 2 | bin/upgrade 3 | 4 | # Compile intermediate file 5 | .output 6 | *.d 7 | *.o 8 | 9 | # Config file 10 | .settings 11 | .project 12 | .cproject 13 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "ESP8266_RTOS_SDK"] 2 | path = ESP8266_RTOS_SDK 3 | url = https://github.com/espressif/ESP8266_RTOS_SDK.git 4 | -------------------------------------------------------------------------------- /40B_ESP_Smart_Plug__User_Guide__EN_v1.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/40B_ESP_Smart_Plug__User_Guide__EN_v1.1.pdf -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/License -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ############################################################# 2 | # Required variables for each makefile 3 | # Discard this section from all parent makefiles 4 | # Expected variables (with automatic defaults): 5 | # CSRCS (all "C" files in the dir) 6 | # SUBDIRS (all subdirs with a Makefile) 7 | # GEN_LIBS - list of libs to be generated () 8 | # GEN_IMAGES - list of object file images to be generated () 9 | # GEN_BINS - list of binaries to be generated () 10 | # COMPONENTS_xxx - a list of libs/objs in the form 11 | # subdir/lib to be extracted and rolled up into 12 | # a generated lib/image xxx.a () 13 | # 14 | TARGET = eagle 15 | #FLAVOR = release 16 | FLAVOR = debug 17 | 18 | #EXTRA_CCFLAGS += -u 19 | 20 | ifndef PDIR # { 21 | GEN_IMAGES= eagle.app.v6.out 22 | GEN_BINS= eagle.app.v6.bin 23 | SPECIAL_MKTARGETS=$(APP_MKTARGETS) 24 | SUBDIRS= \ 25 | user \ 26 | driver \ 27 | upgrade 28 | 29 | endif # } PDIR 30 | 31 | LDDIR = $(SDK_PATH)/ld 32 | 33 | CCFLAGS += -Os 34 | 35 | TARGET_LDFLAGS = \ 36 | -nostdlib \ 37 | -Wl,-EL \ 38 | --longcalls \ 39 | --text-section-literals 40 | 41 | ifeq ($(FLAVOR),debug) 42 | TARGET_LDFLAGS += -g -O2 43 | endif 44 | 45 | ifeq ($(FLAVOR),release) 46 | TARGET_LDFLAGS += -g -O0 47 | endif 48 | 49 | dummy: all 50 | 51 | libesphttpd/libesphttpd.a: libesphttpd/Makefile 52 | make -C libesphttpd libesphttpd.a 53 | 54 | libesphttpd/libwebpages-espfs.a: libesphttpd/Makefile 55 | make -C libesphttpd libwebpages-espfs.a 56 | 57 | COMPONENTS_eagle.app.v6 = \ 58 | user/libuser.a \ 59 | driver/libdriver.a \ 60 | upgrade/libupgrade.a 61 | 62 | LINKFLAGS_eagle.app.v6 = \ 63 | -L$(SDK_PATH)/lib \ 64 | -Wl,--gc-sections \ 65 | -nostdlib \ 66 | -T$(LD_FILE) \ 67 | -Wl,--no-check-sections \ 68 | -u call_user_start \ 69 | -Wl,-static \ 70 | -Wl,--start-group \ 71 | -lcirom \ 72 | -lmirom \ 73 | -lgcc \ 74 | -lhal \ 75 | -lphy \ 76 | -lpp \ 77 | -lnet80211 \ 78 | -lcrypto \ 79 | -lwpa \ 80 | -lmain \ 81 | -lfreertos \ 82 | -llwip \ 83 | -lssl \ 84 | -ljson \ 85 | -lsmartconfig \ 86 | -lpwm \ 87 | -L./libesphttpd \ 88 | -lesphttpd \ 89 | -lwebpages-espfs \ 90 | $(DEP_LIBS_eagle.app.v6) \ 91 | -Wl,--end-group 92 | 93 | 94 | # -lcirom \ 95 | -lmirom \ 96 | 97 | DEPENDS_eagle.app.v6 = \ 98 | $(LD_FILE) \ 99 | $(LDDIR)/eagle.rom.addr.v6.ld \ 100 | libesphttpd/libesphttpd.a \ 101 | libesphttpd/libwebpages-espfs.a 102 | 103 | ############################################################# 104 | # Configuration i.e. compile options etc. 105 | # Target specific stuff (defines etc.) goes in here! 106 | # Generally values applying to a tree are captured in the 107 | # makefile at its root level - these are then overridden 108 | # for a subtree within the makefile rooted therein 109 | # 110 | 111 | #UNIVERSAL_TARGET_DEFINES = \ 112 | 113 | # Other potential configuration flags include: 114 | # -DTXRX_TXBUF_DEBUG 115 | # -DTXRX_RXBUF_DEBUG 116 | # -DWLAN_CONFIG_CCX 117 | CONFIGURATION_DEFINES = -DICACHE_FLASH 118 | 119 | DEFINES += \ 120 | $(UNIVERSAL_TARGET_DEFINES) \ 121 | $(CONFIGURATION_DEFINES) 122 | 123 | DDEFINES += \ 124 | $(UNIVERSAL_TARGET_DEFINES) \ 125 | $(CONFIGURATION_DEFINES) 126 | 127 | 128 | ############################################################# 129 | # Recursion Magic - Don't touch this!! 130 | # 131 | # Each subtree potentially has an include directory 132 | # corresponding to the common APIs applicable to modules 133 | # rooted at that subtree. Accordingly, the INCLUDE PATH 134 | # of a module can only contain the include directories up 135 | # its parent path, and not its siblings 136 | # 137 | # Required for each makefile to inherit from the parent 138 | # 139 | 140 | INCLUDES := $(INCLUDES) -I $(PDIR)include -I $(PDIR)libesphttpd/include -I $(PDIR)libesphttpd/espfs 141 | sinclude $(SDK_PATH)/Makefile 142 | 143 | .PHONY: FORCE 144 | FORCE: 145 | 146 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266 IOT Platform # 2 | 3 | ---------- 4 | 5 | ESP8266 SDK provides users with a simple, fast and efficient development platform for Internet of Things products. The ESP8266 IOT Platform is based on the FreeRTOS ESP8266 SDK (https://github.com/espressif/esp_iot_rtos_sdk) and adds on to it some commonly used functionalities, in an example application of a smart plug. This application uses the ESP-TOUCH protocol to realise smart configuration of the device. The communication protocols used are JSON and HTTP REST. An Android mobile APK (https://github.com/EspressifApp/IOT-Espressif-Android) is also included as a basic template for the users. 6 | 7 | ## Code Structure ## 8 | 9 | ## usr directory ## 10 | 11 | user_main.c: The entry point for the main program. 12 | 13 | user_webserver.c: Creates the TCP webserver, using JSON packets and REST architecture. 14 | 15 | user_devicefind.c: Creates a UDP service, which recieves special finder message on port 1025 and allows the user to discover devices on the network. 16 | 17 | user_esp_platform.c: provides the Espressif Smart Configuration API (ESP-TOUCH) example; communicates with the Espressif Cloud servers (customize this to connect to your own servers); maintains the network status and data transmission to server. 18 | 19 | user_plug.c: implements the functionality of a smart plug in this example. 20 | 21 | user_esp_platform_timer.c: implements the timer functionalities. 22 | 23 | user_light.c: could be used to output PWM signals that can be used for smart lighting. 24 | 25 | user_cgi.c: implents an adapter between the HTTP webserver and the SDK. 26 | 27 | ## upgrade directory ## 28 | 29 | upgrade.c: firmware upgrade example. 30 | 31 | upgrade_lib.c: operations on FLASH devices pertaining the upgrade of firmware. 32 | 33 | ## include directory ## 34 | 35 | The include directory includes the relevant headers needed for the project. Of interest, is "user_config.h", which can be used to configure or select the examples. By setting the MACROs, we can enable the relevant functionality, e.g. PLUG_DEVICE and LIGHT_DEVICE. 36 | 37 | Please note that you have to adjust these parameters based on your flash map. For more details, please refer to "2A-ESP8266 __IOT_SDK_User_Manuel" 38 | 39 | user_esp_platform.h: #define ESP_PARAM_START_SEC 0x7D 40 | 41 | user_light.h: #define PRIV_PARAM_START_SEC 0x7C 42 | 43 | user_plug.h: #define PRIV_PARAM_START_SEC 0x7C 44 | 45 | ## Driver Directory ## 46 | 47 | This contains the GPIO interface. 48 | 49 | ## libesphttpd Directory ## 50 | 51 | This directory implements a small HTTP server. It is compatible with most web browsers. Core contains the parser implementing the HTTP protocol and a simple file system. ESPFS is a file system with simple compression capabilites built in. util contains the interface with WiFi and DNS related codes. 52 | 53 | ## html_light and html_plug Directories ## 54 | 55 | These directories contain the JavaScript and HTML pages and user interface resources. 56 | 57 | ## Usage ## 58 | 59 | ## Configuration ## 60 | 61 | Target device can be configured through defining user_config.h macro. This application default configuration is a smart power plug (or smart power socket) (#define PLUG_DEVICE 1), and supports the HTTP server function (#define HTTPD_SERVER 1). 62 | 63 | ## Compiling the Code ## 64 | 65 | Run the compilation script ./gen_misc.sh; you will prompted for some configuration parameters. User the firmware download tool to flash the device with the bins generated. For my version of FreeRTOS ESP8266 SDK 1.2.0.3, I have used the following parameters in the upload: 66 | 67 | boot_v1.4(b1).bin, downloads to flash 0x00000 68 | 69 | user1.2048.new.3.bin, downloads to flash 0x10000 70 | 71 | esp_init_data_default.bin, downloads to 0x1fc000 72 | 73 | blank.bin, downloads to flash 0x1fe000 -------------------------------------------------------------------------------- /bin/blank.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/bin/blank.bin -------------------------------------------------------------------------------- /bin/boot_v1.6.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/bin/boot_v1.6.bin -------------------------------------------------------------------------------- /bin/esp_init_data_default.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/bin/esp_init_data_default.bin -------------------------------------------------------------------------------- /driver/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ############################################################# 3 | # Required variables for each makefile 4 | # Discard this section from all parent makefiles 5 | # Expected variables (with automatic defaults): 6 | # CSRCS (all "C" files in the dir) 7 | # SUBDIRS (all subdirs with a Makefile) 8 | # GEN_LIBS - list of libs to be generated () 9 | # GEN_IMAGES - list of images to be generated () 10 | # COMPONENTS_xxx - a list of libs/objs in the form 11 | # subdir/lib to be extracted and rolled up into 12 | # a generated lib/image xxx.a () 13 | # 14 | ifndef PDIR 15 | GEN_LIBS = libdriver.a 16 | endif 17 | 18 | 19 | ############################################################# 20 | # Configuration i.e. compile options etc. 21 | # Target specific stuff (defines etc.) goes in here! 22 | # Generally values applying to a tree are captured in the 23 | # makefile at its root level - these are then overridden 24 | # for a subtree within the makefile rooted therein 25 | # 26 | #DEFINES += 27 | 28 | ############################################################# 29 | # Recursion Magic - Don't touch this!! 30 | # 31 | # Each subtree potentially has an include directory 32 | # corresponding to the common APIs applicable to modules 33 | # rooted at that subtree. Accordingly, the INCLUDE PATH 34 | # of a module can only contain the include directories up 35 | # its parent path, and not its siblings 36 | # 37 | # Required for each makefile to inherit from the parent 38 | # 39 | 40 | INCLUDES := $(INCLUDES) -I $(PDIR)include 41 | INCLUDES += -I ./ 42 | PDIR := ../$(PDIR) 43 | sinclude $(PDIR)Makefile 44 | 45 | -------------------------------------------------------------------------------- /driver/gpio.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | * FileName: gpio.c 5 | * 6 | * Description: GPIO related operation 7 | * 8 | * Modification history: 9 | * 2015/7/3, v1.0 create this file. 10 | *******************************************************************************/ 11 | #include "espressif/esp_common.h" 12 | #include "freertos/portmacro.h" 13 | 14 | #include "driver/gpio.h" 15 | 16 | void 17 | gpio_config(GPIO_ConfigTypeDef *pGPIOConfig) 18 | { 19 | uint16 gpio_pin_mask = pGPIOConfig->GPIO_Pin; 20 | uint32 io_reg; 21 | uint8 io_num = 0; 22 | uint32 pin_reg; 23 | 24 | if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Input) { 25 | GPIO_AS_INPUT(gpio_pin_mask); 26 | } else if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Output) { 27 | GPIO_AS_OUTPUT(gpio_pin_mask); 28 | } 29 | 30 | do { 31 | if ((gpio_pin_mask >> io_num) & 0x1) { 32 | io_reg = GPIO_PIN_REG(io_num); 33 | 34 | if ((0x1 << io_num) & (GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_5)) { 35 | PIN_FUNC_SELECT(io_reg, 0); 36 | } else { 37 | PIN_FUNC_SELECT(io_reg, 3); 38 | } 39 | 40 | if (pGPIOConfig->GPIO_Pullup) { 41 | PIN_PULLUP_EN(io_reg); 42 | } else { 43 | PIN_PULLUP_DIS(io_reg); 44 | } 45 | 46 | if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Out_OD) { 47 | portENTER_CRITICAL(); 48 | 49 | pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(io_num)); 50 | pin_reg &= (~GPIO_PIN_DRIVER_MASK); 51 | pin_reg |= (GPIO_PAD_DRIVER_ENABLE << GPIO_PIN_DRIVER_LSB); 52 | GPIO_REG_WRITE(GPIO_PIN_ADDR(io_num), pin_reg); 53 | 54 | portEXIT_CRITICAL(); 55 | } else if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Sigma_Delta) { 56 | portENTER_CRITICAL(); 57 | 58 | pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(io_num)); 59 | pin_reg &= (~GPIO_PIN_SOURCE_MASK); 60 | pin_reg |= (0x1 << GPIO_PIN_SOURCE_LSB); 61 | GPIO_REG_WRITE(GPIO_PIN_ADDR(io_num), pin_reg); 62 | GPIO_REG_WRITE(GPIO_SIGMA_DELTA_ADDRESS, SIGMA_DELTA_ENABLE); 63 | 64 | portEXIT_CRITICAL(); 65 | } 66 | 67 | gpio_pin_intr_state_set(io_num, pGPIOConfig->GPIO_IntrType); 68 | } 69 | 70 | io_num++; 71 | } while (io_num < 16); 72 | } 73 | 74 | 75 | /* 76 | * Change GPIO pin output by setting, clearing, or disabling pins. 77 | * In general, it is expected that a bit will be set in at most one 78 | * of these masks. If a bit is clear in all masks, the output state 79 | * remains unchanged. 80 | * 81 | * There is no particular ordering guaranteed; so if the order of 82 | * writes is significant, calling code should divide a single call 83 | * into multiple calls. 84 | */ 85 | void 86 | gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask) 87 | { 88 | GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, set_mask); 89 | GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clear_mask); 90 | GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, enable_mask); 91 | GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, disable_mask); 92 | } 93 | 94 | /* 95 | * Sample the value of GPIO input pins and returns a bitmask. 96 | */ 97 | uint32 98 | gpio_input_get(void) 99 | { 100 | return GPIO_REG_READ(GPIO_IN_ADDRESS); 101 | } 102 | 103 | /* 104 | * Register an application-specific interrupt handler for GPIO pin 105 | * interrupts. Once the interrupt handler is called, it will not 106 | * be called again until after a call to gpio_intr_ack. Any GPIO 107 | * interrupts that occur during the interim are masked. 108 | * 109 | * The application-specific handler is called with a mask of 110 | * pending GPIO interrupts. After processing pin interrupts, the 111 | * application-specific handler may wish to use gpio_intr_pending 112 | * to check for any additional pending interrupts before it returns. 113 | */ 114 | void 115 | gpio_intr_handler_register(void *fn, void *arg) 116 | { 117 | _xt_isr_attach(ETS_GPIO_INUM, fn, arg); 118 | } 119 | 120 | /* 121 | only highlevel and lowlevel intr can use for wakeup 122 | */ 123 | void 124 | gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state) 125 | { 126 | uint32 pin_reg; 127 | 128 | if ((intr_state == GPIO_PIN_INTR_LOLEVEL) || (intr_state == GPIO_PIN_INTR_HILEVEL)) { 129 | portENTER_CRITICAL(); 130 | 131 | pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i)); 132 | pin_reg &= (~GPIO_PIN_INT_TYPE_MASK); 133 | pin_reg |= (intr_state << GPIO_PIN_INT_TYPE_LSB); 134 | pin_reg |= GPIO_PIN_WAKEUP_ENABLE_SET(GPIO_WAKEUP_ENABLE); 135 | GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg); 136 | 137 | portEXIT_CRITICAL(); 138 | } 139 | } 140 | 141 | void 142 | gpio_pin_wakeup_disable(void) 143 | { 144 | uint8 i; 145 | uint32 pin_reg; 146 | 147 | for (i = 0; i < GPIO_PIN_COUNT; i++) { 148 | pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i)); 149 | 150 | if (pin_reg & GPIO_PIN_WAKEUP_ENABLE_MASK) { 151 | pin_reg &= (~GPIO_PIN_INT_TYPE_MASK); 152 | pin_reg |= (GPIO_PIN_INTR_DISABLE << GPIO_PIN_INT_TYPE_LSB); 153 | pin_reg &= ~(GPIO_PIN_WAKEUP_ENABLE_SET(GPIO_WAKEUP_ENABLE)); 154 | GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg); 155 | } 156 | } 157 | } 158 | 159 | void 160 | gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state) 161 | { 162 | uint32 pin_reg; 163 | 164 | portENTER_CRITICAL(); 165 | 166 | pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i)); 167 | pin_reg &= (~GPIO_PIN_INT_TYPE_MASK); 168 | pin_reg |= (intr_state << GPIO_PIN_INT_TYPE_LSB); 169 | GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg); 170 | 171 | portEXIT_CRITICAL(); 172 | } 173 | 174 | void 175 | gpio16_output_conf(void) 176 | { 177 | WRITE_PERI_REG(PAD_XPD_DCDC_CONF, 178 | (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC to output rtc_gpio0 179 | 180 | WRITE_PERI_REG(RTC_GPIO_CONF, 181 | (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable 182 | 183 | WRITE_PERI_REG(RTC_GPIO_ENABLE, 184 | (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); //out enable 185 | } 186 | 187 | void 188 | gpio16_output_set(uint8 value) 189 | { 190 | WRITE_PERI_REG(RTC_GPIO_OUT, 191 | (READ_PERI_REG(RTC_GPIO_OUT) & (uint32)0xfffffffe) | (uint32)(value & 1)); 192 | } 193 | 194 | void 195 | gpio16_input_conf(void) 196 | { 197 | WRITE_PERI_REG(PAD_XPD_DCDC_CONF, 198 | (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC and rtc_gpio0 connection 199 | 200 | WRITE_PERI_REG(RTC_GPIO_CONF, 201 | (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable 202 | 203 | WRITE_PERI_REG(RTC_GPIO_ENABLE, 204 | READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe); //out disable 205 | } 206 | 207 | uint8 208 | gpio16_input_get(void) 209 | { 210 | return (uint8)(READ_PERI_REG(RTC_GPIO_IN_DATA) & 1); 211 | } 212 | 213 | -------------------------------------------------------------------------------- /driver/key.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | * FileName: key.c 5 | * 6 | * Description: key driver, now can use different gpio and install different function 7 | * 8 | * Modification history: 9 | * 2015/7/1, v1.0 create this file. 10 | *******************************************************************************/ 11 | #include "esp_common.h" 12 | #include "driver/gpio.h" 13 | #include "driver/key.h" 14 | 15 | #define LONG_PRESS_TIME 5000 //ms 16 | 17 | LOCAL void key_intr_handler(struct keys_param *keys); 18 | 19 | /****************************************************************************** 20 | * FunctionName : get_key_status 21 | * Description : get single key status 22 | * Parameters : single_key - single key pointer parameter 23 | * Returns : none 24 | *******************************************************************************/ 25 | BOOL 26 | get_key_status(struct single_key_param *single_key) 27 | { 28 | return GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id)); 29 | } 30 | 31 | /****************************************************************************** 32 | * FunctionName : key_init_single 33 | * Description : init single key's gpio and register function 34 | * Parameters : uint8 gpio_id - which gpio to use 35 | * uint32 gpio_name - gpio mux name 36 | * uint32 gpio_func - gpio function 37 | * key_function long_press - long press function, needed to install 38 | * key_function short_press - short press function, needed to install 39 | * Returns : single_key_param - single key parameter, needed by key init 40 | *******************************************************************************/ 41 | struct single_key_param * 42 | key_init_single(uint8 gpio_id, uint32 gpio_name, uint8 gpio_func, key_function long_press, key_function short_press) 43 | { 44 | struct single_key_param *single_key = (struct single_key_param *)zalloc(sizeof(struct single_key_param)); 45 | 46 | single_key->gpio_id = gpio_id; 47 | single_key->gpio_name = gpio_name; 48 | single_key->gpio_func = gpio_func; 49 | single_key->long_press = long_press; 50 | single_key->short_press = short_press; 51 | 52 | return single_key; 53 | } 54 | 55 | /****************************************************************************** 56 | * FunctionName : key_init 57 | * Description : init keys 58 | * Parameters : key_param *keys - keys parameter, which inited by key_init_single 59 | * Returns : none 60 | *******************************************************************************/ 61 | void 62 | key_init(struct keys_param *keys) 63 | { 64 | u32 i; 65 | GPIO_ConfigTypeDef *pGPIOConfig; 66 | 67 | pGPIOConfig = (GPIO_ConfigTypeDef*)zalloc(sizeof(GPIO_ConfigTypeDef)); 68 | gpio_intr_handler_register(key_intr_handler,keys); 69 | 70 | for (i = 0; i < keys->key_num; i++) { 71 | keys->single_key[i]->key_level = 1; 72 | pGPIOConfig->GPIO_IntrType = GPIO_PIN_INTR_NEGEDGE; 73 | pGPIOConfig->GPIO_Pullup = GPIO_PullUp_EN; 74 | pGPIOConfig->GPIO_Mode = GPIO_Mode_Input; 75 | pGPIOConfig->GPIO_Pin = (1 << keys->single_key[i]->gpio_id);//this is GPIO_Pin_13 for switch 76 | gpio_config(pGPIOConfig); 77 | } 78 | //enable gpio iterrupt 79 | _xt_isr_unmask(1<key_5s); 92 | //check this gpio pin state 93 | if (0 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) { 94 | //this gpio has been in low state for 5s, then call long_press function 95 | if (single_key->long_press) { 96 | single_key->long_press(); 97 | } 98 | } 99 | } 100 | 101 | /****************************************************************************** 102 | * FunctionName : key_50ms_cb 103 | * Description : 50ms timer callback to check it's a real key push 104 | * Parameters : single_key_param *single_key - single key parameter 105 | * Returns : none 106 | *******************************************************************************/ 107 | LOCAL void 108 | key_50ms_cb(struct single_key_param *single_key) 109 | { 110 | os_timer_disarm(&single_key->key_50ms); 111 | //check this gpio pin state 112 | if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) { 113 | os_timer_disarm(&single_key->key_5s); 114 | single_key->key_level = 1; 115 | gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_NEGEDGE); 116 | //this gpio has been in low state no more than 5s, then call short_press function 117 | if (single_key->short_press) { 118 | single_key->short_press(); 119 | } 120 | } else { 121 | gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_POSEDGE); 122 | } 123 | } 124 | 125 | /****************************************************************************** 126 | * FunctionName : key_intr_handler 127 | * Description : key interrupt handler 128 | * Parameters : key_param *keys - keys parameter, which inited by key_init_single 129 | * Returns : none 130 | *******************************************************************************/ 131 | 132 | LOCAL void 133 | key_intr_handler(struct keys_param *keys) 134 | { 135 | uint8 i; 136 | uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS); 137 | 138 | for (i = 0; i < keys->key_num; i++) { 139 | if (gpio_status & BIT(keys->single_key[i]->gpio_id)) { 140 | 141 | //disable this gpio pin interrupt 142 | gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_DISABLE); 143 | //clear interrupt status 144 | GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(keys->single_key[i]->gpio_id)); 145 | 146 | if (keys->single_key[i]->key_level == 1) { 147 | // 5s, restart & enter softap mode 148 | os_timer_disarm(&keys->single_key[i]->key_5s); 149 | os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]); 150 | os_timer_arm(&keys->single_key[i]->key_5s, LONG_PRESS_TIME, 0); 151 | keys->single_key[i]->key_level = 0; 152 | 153 | //enable this gpio pin interrupt 154 | gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE); 155 | } else { 156 | 157 | // 50ms, check if this is a real key up 158 | os_timer_disarm(&keys->single_key[i]->key_50ms); 159 | os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]); 160 | os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0); 161 | } 162 | } 163 | } 164 | } 165 | 166 | -------------------------------------------------------------------------------- /gen_misc.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | Rem ******NOTICE****** 4 | Rem MUST set SDK_PATH & BIN_PATH firstly!!! 5 | Rem example: 6 | Rem set SDK_PATH=/c/esp_iot_sdk_freertos 7 | Rem set BIN_PATH=/c/esp8266_bin 8 | 9 | set SDK_PATH="" 10 | set BIN_PATH="" 11 | 12 | echo gen_misc.bat version 20150911 13 | echo . 14 | 15 | if not %SDK_PATH% == "" ( 16 | echo SDK_PATH: %SDK_PATH% 17 | ) else ( 18 | echo ERROR: Please set SDK_PATH in gen_misc.bat firstly, exit!!! 19 | goto end 20 | ) 21 | 22 | if not %BIN_PATH% == "" ( 23 | echo BIN_PATH: %BIN_PATH% 24 | ) else ( 25 | echo ERROR: Please set BIN_PATH in gen_misc.bat firstly, exit!!! 26 | goto end 27 | ) 28 | 29 | echo . 30 | echo Please check SDK_PATH/BIN_PATH, enter (Y/y) to continue: 31 | set input=default 32 | set /p input= 33 | 34 | if not %input% == Y ( 35 | if not %input% == y ( 36 | goto end 37 | ) 38 | ) 39 | 40 | echo . 41 | echo Please follow below steps(1-5) to generate specific bin(s): 42 | echo STEP 1: use boot_v1.2+ by default 43 | set boot=new 44 | 45 | echo boot mode: %boot% 46 | echo. 47 | 48 | echo STEP 2: choose bin generate(0=eagle.flash.bin+eagle.irom0text.bin, 1=user1.bin, 2=user2.bin) 49 | set input=default 50 | set /p input=enter (0/1/2, default 0): 51 | 52 | if %input% equ 1 ( 53 | if %boot% equ none ( 54 | set app=0 55 | echo choose no boot before 56 | echo generate bin: eagle.flash.bin+eagle.irom0text.bin 57 | ) else ( 58 | set app=1 59 | echo generate bin: user1.bin 60 | ) 61 | ) else ( 62 | if %input% equ 2 ( 63 | if %boot% equ none ( 64 | set app=0 65 | echo choose no boot before 66 | echo generate bin: eagle.flash.bin+eagle.irom0text.bin 67 | ) else ( 68 | set app=2 69 | echo generate bin: user2.bin 70 | ) 71 | ) else ( 72 | if %boot% neq none ( 73 | set boot=none 74 | echo ignore boot 75 | ) 76 | set app=0 77 | echo generate bin: eagle.flash.bin+eagle.irom0text.bin 78 | )) 79 | 80 | echo. 81 | 82 | echo STEP 3: choose spi speed(0=20MHz, 1=26.7MHz, 2=40MHz, 3=80MHz) 83 | set input=default 84 | set /p input=enter (0/1/2/3, default 2): 85 | 86 | if %input% equ 0 ( 87 | set spi_speed=20 88 | ) else ( 89 | if %input% equ 1 ( 90 | set spi_speed=26.7 91 | ) else ( 92 | if %input% equ 3 ( 93 | set spi_speed=80 94 | ) else ( 95 | set spi_speed=40 96 | ))) 97 | 98 | echo spi speed: %spi_speed% MHz 99 | echo. 100 | 101 | echo STEP 4: choose spi mode(0=QIO, 1=QOUT, 2=DIO, 3=DOUT) 102 | set input=default 103 | set /p input=enter (0/1/2/3, default 0): 104 | 105 | if %input% equ 1 ( 106 | set spi_mode=QOUT 107 | ) else ( 108 | if %input% equ 2 ( 109 | set spi_mode=DIO 110 | ) else ( 111 | if %input% equ 3 ( 112 | set spi_mode=DOUT 113 | ) else ( 114 | set spi_mode=QIO 115 | ))) 116 | 117 | echo spi mode: %spi_mode% 118 | echo. 119 | 120 | echo STEP 5: choose flash size and map 121 | echo 0= 512KB( 256KB+ 256KB) 122 | echo 2=1024KB( 512KB+ 512KB) 123 | echo 3=2048KB( 512KB+ 512KB) 124 | echo 4=4096KB( 512KB+ 512KB) 125 | echo 5=2048KB(1024KB+1024KB) 126 | echo 6=4096KB(1024KB+1024KB) 127 | set input=default 128 | set /p input=enter (0/1/2/3/4/5/6, default 0): 129 | 130 | if %input% equ 2 ( 131 | set spi_size_map=2 132 | echo spi size: 1024KB 133 | echo spi ota map: 512KB + 512KB 134 | ) else ( 135 | if %input% equ 3 ( 136 | set spi_size_map=3 137 | echo spi size: 2048KB 138 | echo spi ota map: 512KB + 512KB 139 | ) else ( 140 | if %input% equ 4 ( 141 | set spi_size_map=4 142 | echo spi size: 4096KB 143 | echo spi ota map: 512KB + 512KB 144 | ) else ( 145 | if %input% equ 5 ( 146 | set spi_size_map=5 147 | echo spi size: 2048KB 148 | echo spi ota map: 1024KB + 1024KB 149 | ) else ( 150 | if %input% equ 6 ( 151 | set spi_size_map=6 152 | echo spi size: 4096KB 153 | echo spi ota map: 1024KB + 1024KB 154 | ) else ( 155 | set spi_size_map=0 156 | echo spi size: 512KB 157 | echo spi ota map: 256KB + 256KB 158 | ) 159 | ) 160 | ) 161 | ) 162 | ) 163 | 164 | touch user/user_main.c 165 | 166 | echo. 167 | echo start... 168 | echo. 169 | 170 | make clean 171 | 172 | make COMPILE=xcc BOOT=%boot% APP=%app% SPI_SPEED=%spi_speed% SPI_MODE=%spi_mode% SPI_SIZE_MAP=%spi_size_map% 173 | 174 | :end -------------------------------------------------------------------------------- /gen_misc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | :<ESP8266 light 2 | 3 | 4 | 46 | 47 | 50 |

Pick a color:

51 |
52 | 53 | 54 | -------------------------------------------------------------------------------- /html_light/wifi/140medley.min.js: -------------------------------------------------------------------------------- 1 | var t=function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,f){return Function("x","with(x)return "+f).call(c,d||b||{})})}},s=function(a,b){return b?{get:function(c){return a[c]&&b.parse(a[c])},set:function(c,d){a[c]=b.stringify(d)}}:{}}(this.localStorage||{},JSON),p=function(a,b,c,d){c=c||document;d=c[b="on"+b];a=c[b]=function(e){d=d&&d(e=e||c.event);return(a=a&&b(e))?b:d};c=this},m=function(a,b,c){b=document;c=b.createElement("p");c.innerHTML=a;for(a=b.createDocumentFragment();b= 2 | c.firstChild;)a.appendChild(b);return a},$=function(a,b){a=a.match(/^(\W)?(.*)/);return(b||document)["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2])},j=function(a){for(a=0;a<4;a++)try{return a?new ActiveXObject([,"Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP"):new XMLHttpRequest}catch(b){}}; 3 | -------------------------------------------------------------------------------- /html_light/wifi/connecting.html: -------------------------------------------------------------------------------- 1 | Connecting... 2 | 3 | 4 | 34 | 35 | 36 |
37 |

Connecting to AP...

38 |

Status:
39 |

...
40 |

41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /html_light/wifi/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/html_light/wifi/icons.png -------------------------------------------------------------------------------- /html_light/wifi/style.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #404040; 4 | font-family: sans-serif; 5 | } 6 | 7 | #main { 8 | background-color: #d0d0FF; 9 | -moz-border-radius: 5px; 10 | -webkit-border-radius: 5px; 11 | border-radius: 5px; 12 | border: 2px solid #000000; 13 | width: 800px; 14 | margin: 0 auto; 15 | padding: 20px 16 | } 17 | 18 | .icon { 19 | background-image: url("icons.png"); 20 | background-color: transparent; 21 | width: 32px; 22 | height: 32px; 23 | display: inline-block; 24 | } -------------------------------------------------------------------------------- /html_light/wifi/wifi.tpl: -------------------------------------------------------------------------------- 1 | WiFi connection 2 | 3 | 4 | 74 | 75 | 76 |
77 |

78 | Current WiFi mode: %WiFiMode% 79 |

80 |

81 | Note: %WiFiapwarn% 82 |

83 |
84 |

85 | To connect to a WiFi network, please select one of the detected networks...
86 |

Scanning...
87 |
88 | WiFi password, if applicable:
89 |
90 | 91 |

92 |
93 | 94 | 95 | -------------------------------------------------------------------------------- /html_plug/140medley.min.js: -------------------------------------------------------------------------------- 1 | var t=function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,f){return Function("x","with(x)return "+f).call(c,d||b||{})})}},s=function(a,b){return b?{get:function(c){return a[c]&&b.parse(a[c])},set:function(c,d){a[c]=b.stringify(d)}}:{}}(this.localStorage||{},JSON),p=function(a,b,c,d){c=c||document;d=c[b="on"+b];a=c[b]=function(e){d=d&&d(e=e||c.event);return(a=a&&b(e))?b:d};c=this},m=function(a,b,c){b=document;c=b.createElement("p");c.innerHTML=a;for(a=b.createDocumentFragment();b= 2 | c.firstChild;)a.appendChild(b);return a},$=function(a,b){a=a.match(/^(\W)?(.*)/);return(b||document)["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2])},j=function(a){for(a=0;a<4;a++)try{return a?new ActiveXObject([,"Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP"):new XMLHttpRequest}catch(b){}}; 3 | -------------------------------------------------------------------------------- /html_plug/index.html: -------------------------------------------------------------------------------- 1 | ESP8266 PLUG 2 | 3 | 64 | 65 | 68 | 69 |
70 | 71 |
72 | 73 | 74 | -------------------------------------------------------------------------------- /html_plug/plug_off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/html_plug/plug_off.png -------------------------------------------------------------------------------- /html_plug/plug_on.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/html_plug/plug_on.png -------------------------------------------------------------------------------- /html_plug/wifi/140medley.min.js: -------------------------------------------------------------------------------- 1 | var t=function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,f){return Function("x","with(x)return "+f).call(c,d||b||{})})}},s=function(a,b){return b?{get:function(c){return a[c]&&b.parse(a[c])},set:function(c,d){a[c]=b.stringify(d)}}:{}}(this.localStorage||{},JSON),p=function(a,b,c,d){c=c||document;d=c[b="on"+b];a=c[b]=function(e){d=d&&d(e=e||c.event);return(a=a&&b(e))?b:d};c=this},m=function(a,b,c){b=document;c=b.createElement("p");c.innerHTML=a;for(a=b.createDocumentFragment();b= 2 | c.firstChild;)a.appendChild(b);return a},$=function(a,b){a=a.match(/^(\W)?(.*)/);return(b||document)["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2])},j=function(a){for(a=0;a<4;a++)try{return a?new ActiveXObject([,"Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP"):new XMLHttpRequest}catch(b){}}; 3 | -------------------------------------------------------------------------------- /html_plug/wifi/connecting.html: -------------------------------------------------------------------------------- 1 | Connecting... 2 | 3 | 4 | 34 | 35 | 36 |
37 |

Connecting to AP...

38 |

Status:
39 |

...
40 |

41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /html_plug/wifi/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wujiangang/ESP8266_IOT_PLATFORM/7a3da236a68dec48364f76b2fa00ce019c769459/html_plug/wifi/icons.png -------------------------------------------------------------------------------- /html_plug/wifi/style.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | background-color: #404040; 4 | font-family: sans-serif; 5 | } 6 | 7 | #main { 8 | background-color: #d0d0FF; 9 | -moz-border-radius: 5px; 10 | -webkit-border-radius: 5px; 11 | border-radius: 5px; 12 | border: 2px solid #000000; 13 | width: 800px; 14 | margin: 0 auto; 15 | padding: 20px 16 | } 17 | 18 | .icon { 19 | background-image: url("icons.png"); 20 | background-color: transparent; 21 | width: 32px; 22 | height: 32px; 23 | display: inline-block; 24 | } -------------------------------------------------------------------------------- /html_plug/wifi/wifi.tpl: -------------------------------------------------------------------------------- 1 | WiFi connection 2 | 3 | 4 | 74 | 75 | 76 |
77 |

78 | Current WiFi mode: %WiFiMode% 79 |

80 |

81 | Note: %WiFiapwarn% 82 |

83 | 84 |

85 | To connect to a WiFi network, please select one of the detected networks...
86 |

Scanning...
87 |
88 | WiFi password, if applicable:
89 |
90 | 91 |

92 |
93 | 94 | 95 | -------------------------------------------------------------------------------- /include/driver/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | */ 5 | 6 | #ifndef __GPIO_H__ 7 | #define __GPIO_H__ 8 | 9 | #include "esp8266/gpio_register.h" 10 | 11 | #define ETS_GPIO_INTR_ENABLE() _xt_isr_unmask(1 << ETS_GPIO_INUM) 12 | #define ETS_GPIO_INTR_DISABLE() _xt_isr_mask(1 << ETS_GPIO_INUM) 13 | 14 | #define GPIO_Pin_0 (BIT(0)) /* Pin 0 selected */ 15 | #define GPIO_Pin_1 (BIT(1)) /* Pin 1 selected */ 16 | #define GPIO_Pin_2 (BIT(2)) /* Pin 2 selected */ 17 | #define GPIO_Pin_3 (BIT(3)) /* Pin 3 selected */ 18 | #define GPIO_Pin_4 (BIT(4)) /* Pin 4 selected */ 19 | #define GPIO_Pin_5 (BIT(5)) /* Pin 5 selected */ 20 | #define GPIO_Pin_6 (BIT(6)) /* Pin 6 selected */ 21 | #define GPIO_Pin_7 (BIT(7)) /* Pin 7 selected */ 22 | #define GPIO_Pin_8 (BIT(8)) /* Pin 8 selected */ 23 | #define GPIO_Pin_9 (BIT(9)) /* Pin 9 selected */ 24 | #define GPIO_Pin_10 (BIT(10)) /* Pin 10 selected */ 25 | #define GPIO_Pin_11 (BIT(11)) /* Pin 11 selected */ 26 | #define GPIO_Pin_12 (BIT(12)) /* Pin 12 selected */ 27 | #define GPIO_Pin_13 (BIT(13)) /* Pin 13 selected */ 28 | #define GPIO_Pin_14 (BIT(14)) /* Pin 14 selected */ 29 | #define GPIO_Pin_15 (BIT(15)) /* Pin 15 selected */ 30 | #define GPIO_Pin_All (0xFFFF) /* All pins selected */ 31 | 32 | #define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U 33 | #define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U 34 | #define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U 35 | #define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U 36 | #define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U 37 | #define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U 38 | #define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U 39 | #define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U 40 | #define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U 41 | #define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U 42 | #define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U 43 | #define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U 44 | #define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U 45 | #define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U 46 | #define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U 47 | #define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U 48 | 49 | #define GPIO_PIN_REG(i) \ 50 | (i==0) ? GPIO_PIN_REG_0: \ 51 | (i==1) ? GPIO_PIN_REG_1: \ 52 | (i==2) ? GPIO_PIN_REG_2: \ 53 | (i==3) ? GPIO_PIN_REG_3: \ 54 | (i==4) ? GPIO_PIN_REG_4: \ 55 | (i==5) ? GPIO_PIN_REG_5: \ 56 | (i==6) ? GPIO_PIN_REG_6: \ 57 | (i==7) ? GPIO_PIN_REG_7: \ 58 | (i==8) ? GPIO_PIN_REG_8: \ 59 | (i==9) ? GPIO_PIN_REG_9: \ 60 | (i==10)? GPIO_PIN_REG_10: \ 61 | (i==11)? GPIO_PIN_REG_11: \ 62 | (i==12)? GPIO_PIN_REG_12: \ 63 | (i==13)? GPIO_PIN_REG_13: \ 64 | (i==14)? GPIO_PIN_REG_14: \ 65 | GPIO_PIN_REG_15 66 | 67 | #define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4) 68 | 69 | #define GPIO_ID_IS_PIN_REGISTER(reg_id) \ 70 | ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1))) 71 | 72 | #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0) 73 | 74 | typedef enum { 75 | GPIO_PIN_INTR_DISABLE = 0, 76 | GPIO_PIN_INTR_POSEDGE = 1, 77 | GPIO_PIN_INTR_NEGEDGE = 2, 78 | GPIO_PIN_INTR_ANYEGDE = 3, 79 | GPIO_PIN_INTR_LOLEVEL = 4, 80 | GPIO_PIN_INTR_HILEVEL = 5 81 | } GPIO_INT_TYPE; 82 | 83 | typedef enum { 84 | GPIO_Mode_Input = 0x0, 85 | GPIO_Mode_Out_OD, 86 | GPIO_Mode_Output , 87 | GPIO_Mode_Sigma_Delta , 88 | } GPIOMode_TypeDef; 89 | 90 | typedef enum { 91 | GPIO_PullUp_DIS = 0x0, 92 | GPIO_PullUp_EN = 0x1, 93 | } GPIO_Pullup_IF; 94 | 95 | typedef struct { 96 | uint16 GPIO_Pin; 97 | GPIOMode_TypeDef GPIO_Mode; 98 | GPIO_Pullup_IF GPIO_Pullup; 99 | GPIO_INT_TYPE GPIO_IntrType; 100 | } GPIO_ConfigTypeDef; 101 | 102 | #define GPIO_OUTPUT_SET(gpio_no, bit_value) \ 103 | gpio_output_conf(bit_value<>gpio_no)&BIT0) 113 | 114 | void gpio16_output_conf(void); 115 | void gpio16_output_set(uint8 value); 116 | void gpio16_input_conf(void); 117 | uint8 gpio16_input_get(void); 118 | 119 | void gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask); 120 | void gpio_intr_handler_register(void *fn, void *arg); 121 | void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state); 122 | void gpio_pin_wakeup_disable(); 123 | void gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state); 124 | uint32 gpio_input_get(void); 125 | 126 | #endif 127 | -------------------------------------------------------------------------------- /include/driver/i2c_master.h: -------------------------------------------------------------------------------- 1 | #ifndef __I2C_MASTER_H__ 2 | #define __I2C_MASTER_H__ 3 | 4 | #include "esp8266/pin_mux_register.h" 5 | #define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_GPIO2_U 6 | #define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_GPIO4_U 7 | #define I2C_MASTER_SDA_GPIO 2 8 | #define I2C_MASTER_SCL_GPIO 4 9 | #define I2C_MASTER_SDA_FUNC FUNC_GPIO2 10 | #define I2C_MASTER_SCL_FUNC FUNC_GPIO4 11 | 12 | //#define I2C_MASTER_SDA_MUX PERIPHS_IO_MUX_GPIO2_U 13 | //#define I2C_MASTER_SCL_MUX PERIPHS_IO_MUX_GPIO0_U 14 | //#define I2C_MASTER_SDA_GPIO 2 15 | //#define I2C_MASTER_SCL_GPIO 0 16 | //#define I2C_MASTER_SDA_FUNC FUNC_GPIO2 17 | //#define I2C_MASTER_SCL_FUNC FUNC_GPIO0 18 | 19 | #if 0 20 | #define I2C_MASTER_GPIO_SET(pin) \ 21 | gpio_output_set(1<. 16 | */ 17 | /* 18 | * Copyright (c) 2010 - 2011 Espressif System 19 | * 20 | */ 21 | 22 | #ifndef UART_REGISTER_H_ 23 | #define UART_REGISTER_H_ 24 | 25 | #define REG_UART_BASE(i) (0x60000000 + (i)*0xf00) 26 | //version value:32'h062000 27 | 28 | #define UART_FIFO(i) (REG_UART_BASE(i) + 0x0) 29 | #define UART_RXFIFO_RD_BYTE 0x000000FF 30 | #define UART_RXFIFO_RD_BYTE_S 0 31 | 32 | #define UART_INT_RAW(i) (REG_UART_BASE(i) + 0x4) 33 | #define UART_RXFIFO_TOUT_INT_RAW (BIT(8)) 34 | #define UART_BRK_DET_INT_RAW (BIT(7)) 35 | #define UART_CTS_CHG_INT_RAW (BIT(6)) 36 | #define UART_DSR_CHG_INT_RAW (BIT(5)) 37 | #define UART_RXFIFO_OVF_INT_RAW (BIT(4)) 38 | #define UART_FRM_ERR_INT_RAW (BIT(3)) 39 | #define UART_PARITY_ERR_INT_RAW (BIT(2)) 40 | #define UART_TXFIFO_EMPTY_INT_RAW (BIT(1)) 41 | #define UART_RXFIFO_FULL_INT_RAW (BIT(0)) 42 | 43 | #define UART_INT_ST(i) (REG_UART_BASE(i) + 0x8) 44 | #define UART_RXFIFO_TOUT_INT_ST (BIT(8)) 45 | #define UART_BRK_DET_INT_ST (BIT(7)) 46 | #define UART_CTS_CHG_INT_ST (BIT(6)) 47 | #define UART_DSR_CHG_INT_ST (BIT(5)) 48 | #define UART_RXFIFO_OVF_INT_ST (BIT(4)) 49 | #define UART_FRM_ERR_INT_ST (BIT(3)) 50 | #define UART_PARITY_ERR_INT_ST (BIT(2)) 51 | #define UART_TXFIFO_EMPTY_INT_ST (BIT(1)) 52 | #define UART_RXFIFO_FULL_INT_ST (BIT(0)) 53 | 54 | #define UART_INT_ENA(i) (REG_UART_BASE(i) + 0xC) 55 | #define UART_RXFIFO_TOUT_INT_ENA (BIT(8)) 56 | #define UART_BRK_DET_INT_ENA (BIT(7)) 57 | #define UART_CTS_CHG_INT_ENA (BIT(6)) 58 | #define UART_DSR_CHG_INT_ENA (BIT(5)) 59 | #define UART_RXFIFO_OVF_INT_ENA (BIT(4)) 60 | #define UART_FRM_ERR_INT_ENA (BIT(3)) 61 | #define UART_PARITY_ERR_INT_ENA (BIT(2)) 62 | #define UART_TXFIFO_EMPTY_INT_ENA (BIT(1)) 63 | #define UART_RXFIFO_FULL_INT_ENA (BIT(0)) 64 | 65 | #define UART_INT_CLR(i) (REG_UART_BASE(i) + 0x10) 66 | #define UART_RXFIFO_TOUT_INT_CLR (BIT(8)) 67 | #define UART_BRK_DET_INT_CLR (BIT(7)) 68 | #define UART_CTS_CHG_INT_CLR (BIT(6)) 69 | #define UART_DSR_CHG_INT_CLR (BIT(5)) 70 | #define UART_RXFIFO_OVF_INT_CLR (BIT(4)) 71 | #define UART_FRM_ERR_INT_CLR (BIT(3)) 72 | #define UART_PARITY_ERR_INT_CLR (BIT(2)) 73 | #define UART_TXFIFO_EMPTY_INT_CLR (BIT(1)) 74 | #define UART_RXFIFO_FULL_INT_CLR (BIT(0)) 75 | 76 | #define UART_CLKDIV(i) (REG_UART_BASE(i) + 0x14) 77 | #define UART_CLKDIV_CNT 0x000FFFFF 78 | #define UART_CLKDIV_S 0 79 | 80 | #define UART_AUTOBAUD(i) (REG_UART_BASE(i) + 0x18) 81 | #define UART_GLITCH_FILT 0x000000FF 82 | #define UART_GLITCH_FILT_S 8 83 | #define UART_AUTOBAUD_EN (BIT(0)) 84 | 85 | #define UART_STATUS(i) (REG_UART_BASE(i) + 0x1C) 86 | #define UART_TXD (BIT(31)) 87 | #define UART_RTSN (BIT(30)) 88 | #define UART_DTRN (BIT(29)) 89 | #define UART_TXFIFO_CNT 0x000000FF 90 | #define UART_TXFIFO_CNT_S 16 91 | #define UART_RXD (BIT(15)) 92 | #define UART_CTSN (BIT(14)) 93 | #define UART_DSRN (BIT(13)) 94 | #define UART_RXFIFO_CNT 0x000000FF 95 | #define UART_RXFIFO_CNT_S 0 96 | 97 | #define UART_CONF0(i) (REG_UART_BASE(i) + 0x20) 98 | #define UART_DTR_INV (BIT(24)) 99 | #define UART_RTS_INV (BIT(23)) 100 | #define UART_TXD_INV (BIT(22)) 101 | #define UART_DSR_INV (BIT(21)) 102 | #define UART_CTS_INV (BIT(20)) 103 | #define UART_RXD_INV (BIT(19)) 104 | #define UART_TXFIFO_RST (BIT(18)) 105 | #define UART_RXFIFO_RST (BIT(17)) 106 | #define UART_IRDA_EN (BIT(16)) 107 | #define UART_TX_FLOW_EN (BIT(15)) 108 | #define UART_LOOPBACK (BIT(14)) 109 | #define UART_IRDA_RX_INV (BIT(13)) 110 | #define UART_IRDA_TX_INV (BIT(12)) 111 | #define UART_IRDA_WCTL (BIT(11)) 112 | #define UART_IRDA_TX_EN (BIT(10)) 113 | #define UART_IRDA_DPLX (BIT(9)) 114 | #define UART_TXD_BRK (BIT(8)) 115 | #define UART_SW_DTR (BIT(7)) 116 | #define UART_SW_RTS (BIT(6)) 117 | #define UART_STOP_BIT_NUM 0x00000003 118 | #define UART_STOP_BIT_NUM_S 4 119 | #define UART_BIT_NUM 0x00000003 120 | #define UART_BIT_NUM_S 2 121 | #define UART_PARITY_EN (BIT(1)) 122 | #define UART_PARITY_EN_M 0x00000001 123 | #define UART_PARITY_EN_S 1 124 | #define UART_PARITY (BIT(0)) 125 | #define UART_PARITY_M 0x00000001 126 | #define UART_PARITY_S 0 127 | 128 | #define UART_CONF1(i) (REG_UART_BASE(i) + 0x24) 129 | #define UART_RX_TOUT_EN (BIT(31)) 130 | #define UART_RX_TOUT_THRHD 0x0000007F 131 | #define UART_RX_TOUT_THRHD_S 24 132 | #define UART_RX_FLOW_EN (BIT(23)) 133 | #define UART_RX_FLOW_THRHD 0x0000007F 134 | #define UART_RX_FLOW_THRHD_S 16 135 | #define UART_TXFIFO_EMPTY_THRHD 0x0000007F 136 | #define UART_TXFIFO_EMPTY_THRHD_S 8 137 | #define UART_RXFIFO_FULL_THRHD 0x0000007F 138 | #define UART_RXFIFO_FULL_THRHD_S 0 139 | 140 | #define UART_LOWPULSE(i) (REG_UART_BASE(i) + 0x28) 141 | #define UART_LOWPULSE_MIN_CNT 0x000FFFFF 142 | #define UART_LOWPULSE_MIN_CNT_S 0 143 | 144 | #define UART_HIGHPULSE(i) (REG_UART_BASE(i) + 0x2C) 145 | #define UART_HIGHPULSE_MIN_CNT 0x000FFFFF 146 | #define UART_HIGHPULSE_MIN_CNT_S 0 147 | 148 | #define UART_PULSE_NUM(i) (REG_UART_BASE(i) + 0x30) 149 | #define UART_PULSE_NUM_CNT 0x0003FF 150 | #define UART_PULSE_NUM_CNT_S 0 151 | 152 | #define UART_DATE(i) (REG_UART_BASE(i) + 0x78) 153 | #define UART_ID(i) (REG_UART_BASE(i) + 0x7C) 154 | 155 | #endif // UART_REGISTER_H_INCLUDED 156 | 157 | -------------------------------------------------------------------------------- /include/ssl/cert.h: -------------------------------------------------------------------------------- 1 | unsigned char default_certificate[] = { 2 | 0x30, 0x82, 0x01, 0x82, 0x30, 0x81, 0xec, 0x02, 0x09, 0x00, 0x88, 0xf2, 3 | 0x5f, 0x46, 0x12, 0x2e, 0x3d, 0x3a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 4 | 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x1c, 0x31, 5 | 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x11, 0x77, 0x77, 6 | 0x77, 0x2e, 0x65, 0x73, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x66, 0x2e, 7 | 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x36, 0x32, 8 | 0x34, 0x31, 0x30, 0x32, 0x32, 0x33, 0x33, 0x5a, 0x17, 0x0d, 0x32, 0x38, 9 | 0x30, 0x33, 0x30, 0x32, 0x31, 0x30, 0x32, 0x32, 0x33, 0x33, 0x5a, 0x30, 10 | 0x34, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x09, 11 | 0x65, 0x73, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, 0x66, 0x31, 0x1e, 0x30, 12 | 0x1c, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x15, 0x65, 0x73, 0x70, 0x72, 13 | 0x65, 0x73, 0x73, 0x69, 0x66, 0x20, 0x49, 0x6f, 0x54, 0x20, 0x70, 0x72, 14 | 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 15 | 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 16 | 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, 0xb9, 0x83, 0x30, 0xca, 0xfb, 0xec, 17 | 0x11, 0x9e, 0x94, 0xb7, 0x89, 0xf2, 0x84, 0x2c, 0xda, 0xe1, 0x9a, 0x53, 18 | 0x3a, 0x1b, 0x6e, 0xc9, 0x85, 0x81, 0xf9, 0xa3, 0x41, 0xdb, 0xe2, 0x82, 19 | 0x3b, 0xfa, 0x80, 0x22, 0x3b, 0x81, 0x6d, 0x25, 0x73, 0x7e, 0xf6, 0x49, 20 | 0xcc, 0x69, 0x3c, 0x6c, 0xd8, 0x05, 0xfb, 0x92, 0x02, 0xcf, 0x19, 0x2a, 21 | 0x10, 0x7d, 0x69, 0x7a, 0xd8, 0x9d, 0xd3, 0xcf, 0x6c, 0xef, 0x02, 0x03, 22 | 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 23 | 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x2d, 0x63, 24 | 0x58, 0x21, 0xe3, 0x8b, 0x37, 0x0d, 0x28, 0x68, 0x11, 0x0e, 0x4d, 0xdd, 25 | 0xf3, 0xea, 0xdb, 0xec, 0xd7, 0x09, 0x47, 0x2c, 0xa1, 0xd8, 0xd1, 0x71, 26 | 0x83, 0x11, 0xb4, 0x17, 0xbc, 0x83, 0xea, 0x5a, 0xd6, 0x73, 0x02, 0x25, 27 | 0x87, 0x01, 0x76, 0xfc, 0x59, 0x1a, 0xcf, 0xd9, 0x49, 0xc9, 0xf9, 0x1f, 28 | 0x5c, 0x3b, 0x24, 0x6a, 0x5c, 0xa5, 0xca, 0xe6, 0x5d, 0x34, 0x5b, 0x5f, 29 | 0xcf, 0x56, 0x9c, 0x71, 0xd2, 0x6b, 0xdd, 0x1f, 0x15, 0xae, 0x4d, 0xf1, 30 | 0xca, 0x35, 0xc8, 0xdd, 0x93, 0x1b, 0x58, 0x1e, 0x94, 0x08, 0xcf, 0xa0, 31 | 0x20, 0xb9, 0x75, 0xa5, 0x4c, 0x77, 0xf5, 0x7f, 0xed, 0xd5, 0xcd, 0x53, 32 | 0xaa, 0x87, 0xa6, 0x3c, 0xf5, 0x72, 0xd8, 0xd2, 0xb0, 0xf7, 0x11, 0xb0, 33 | 0x0e, 0xe9, 0x41, 0xd6, 0x8e, 0xd9, 0x07, 0xf8, 0xed, 0xf8, 0x67, 0x7f, 34 | 0x28, 0x18, 0xf0, 0x1b, 0x29, 0x11 35 | }; 36 | unsigned int default_certificate_len = 390; 37 | -------------------------------------------------------------------------------- /include/ssl/private_key.h: -------------------------------------------------------------------------------- 1 | unsigned char default_private_key[] = { 2 | 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xb9, 0x83, 3 | 0x30, 0xca, 0xfb, 0xec, 0x11, 0x9e, 0x94, 0xb7, 0x89, 0xf2, 0x84, 0x2c, 4 | 0xda, 0xe1, 0x9a, 0x53, 0x3a, 0x1b, 0x6e, 0xc9, 0x85, 0x81, 0xf9, 0xa3, 5 | 0x41, 0xdb, 0xe2, 0x82, 0x3b, 0xfa, 0x80, 0x22, 0x3b, 0x81, 0x6d, 0x25, 6 | 0x73, 0x7e, 0xf6, 0x49, 0xcc, 0x69, 0x3c, 0x6c, 0xd8, 0x05, 0xfb, 0x92, 7 | 0x02, 0xcf, 0x19, 0x2a, 0x10, 0x7d, 0x69, 0x7a, 0xd8, 0x9d, 0xd3, 0xcf, 8 | 0x6c, 0xef, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x1d, 0x13, 0x92, 9 | 0xf2, 0x3d, 0xca, 0x22, 0x78, 0xd8, 0x96, 0x6b, 0xe8, 0xb7, 0x0e, 0xd0, 10 | 0xbf, 0xcb, 0x90, 0x7f, 0xeb, 0x0c, 0xd2, 0x49, 0x3a, 0xb6, 0x06, 0x00, 11 | 0xac, 0x96, 0x34, 0x13, 0x72, 0x4b, 0x8c, 0xd2, 0xb9, 0x35, 0xf5, 0x64, 12 | 0x18, 0xb2, 0x47, 0x5b, 0x9f, 0xbb, 0xf2, 0x5b, 0x2f, 0x66, 0x78, 0x2d, 13 | 0x0a, 0x76, 0x44, 0xc5, 0x4f, 0xdb, 0x7d, 0x13, 0xcf, 0xa5, 0x08, 0xdc, 14 | 0x01, 0x02, 0x21, 0x00, 0xdf, 0x9a, 0x89, 0xd0, 0xef, 0x23, 0xcf, 0x12, 15 | 0xac, 0x8a, 0x63, 0x1a, 0x8c, 0xc0, 0x3f, 0xf4, 0x38, 0x52, 0x3c, 0x9f, 16 | 0x19, 0x0a, 0x37, 0xd2, 0xcb, 0x5d, 0xeb, 0xb6, 0x2a, 0x33, 0xb0, 0x91, 17 | 0x02, 0x21, 0x00, 0xd4, 0x63, 0xd9, 0x6a, 0x18, 0x5b, 0xe8, 0xa8, 0x57, 18 | 0x4d, 0xd1, 0x9a, 0xa8, 0xd7, 0xe1, 0x65, 0x75, 0xb3, 0xb9, 0x5c, 0x94, 19 | 0x14, 0xca, 0x98, 0x41, 0x47, 0x9c, 0x0a, 0x22, 0x38, 0x05, 0x7f, 0x02, 20 | 0x20, 0x6a, 0xce, 0xfd, 0xef, 0xe0, 0x9b, 0x61, 0x49, 0x91, 0x43, 0x95, 21 | 0x6d, 0x54, 0x38, 0x6d, 0x14, 0x32, 0x67, 0x0d, 0xf0, 0x0d, 0x5c, 0xf5, 22 | 0x27, 0x6a, 0xdf, 0x55, 0x3d, 0xb1, 0xd0, 0xf9, 0x11, 0x02, 0x21, 0x00, 23 | 0xba, 0x94, 0xa0, 0xf9, 0xb0, 0x3e, 0x85, 0x8b, 0xe5, 0x6e, 0x4a, 0x95, 24 | 0x88, 0x80, 0x65, 0xd5, 0x00, 0xea, 0x8b, 0x0b, 0x46, 0x57, 0x61, 0x87, 25 | 0x11, 0xc9, 0xfb, 0xcd, 0x77, 0x34, 0x29, 0xb7, 0x02, 0x20, 0x06, 0x8d, 26 | 0x41, 0x11, 0x47, 0x93, 0xcb, 0xad, 0xda, 0x5d, 0xe1, 0x9d, 0x49, 0x8d, 27 | 0xe0, 0xab, 0x48, 0xe6, 0x18, 0x28, 0x4a, 0x94, 0xae, 0xf9, 0xad, 0xc5, 28 | 0x5b, 0x0b, 0x15, 0xc6, 0x73, 0x17 29 | }; 30 | unsigned int default_private_key_len = 318; 31 | -------------------------------------------------------------------------------- /include/user_cgi.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_CGI_H__ 2 | #define __USER_CGI_H__ 3 | 4 | #include "httpd.h" 5 | 6 | typedef enum _ParmType { 7 | SWITCH_STATUS = 0, 8 | INFOMATION, 9 | WIFI, 10 | SCAN, 11 | REBOOT, 12 | DEEP_SLEEP, 13 | LIGHT_STATUS, 14 | CONNECT_STATUS, 15 | USER_BIN 16 | } ParmType; 17 | 18 | 19 | typedef struct _rst_parm { 20 | ParmType parmtype; 21 | } rst_parm; 22 | 23 | 24 | 25 | 26 | int cgiEspApi(HttpdConnData *connData); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/user_config.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_CONFIG_H__ 2 | #define __USER_CONFIG_H__ 3 | 4 | /*support one platform at the same project*/ 5 | #define ESP_PLATFORM 1 6 | #define LEWEI_PLATFORM 0 7 | 8 | /*support one server at the same project*/ 9 | #define HTTPD_SERVER 1 10 | #define WEB_SERVICE 0 11 | 12 | /*support one device at the same project*/ 13 | #define PLUG_DEVICE 1 14 | #define LIGHT_DEVICE 0 15 | #define SENSOR_DEVICE 0 //TBD 16 | 17 | 18 | #if LIGHT_DEVICE 19 | #define USE_US_TIMER 20 | #endif 21 | 22 | #define RESTORE_KEEP_TIMER 0 23 | 24 | #if ESP_PLATFORM 25 | 26 | #if PLUG_DEVICE || LIGHT_DEVICE 27 | #define BEACON_TIMEOUT 150000000 28 | #define BEACON_TIME 50000 29 | #endif 30 | 31 | #if SENSOR_DEVICE 32 | #define HUMITURE_SUB_DEVICE 1 33 | #define FLAMMABLE_GAS_SUB_DEVICE 0 34 | #endif 35 | 36 | #if SENSOR_DEVICE 37 | #define SENSOR_DEEP_SLEEP 38 | 39 | #if HUMITURE_SUB_DEVICE 40 | #define SENSOR_DEEP_SLEEP_TIME 30000000 41 | #elif FLAMMABLE_GAS_SUB_DEVICE 42 | #define SENSOR_DEEP_SLEEP_TIME 60000000 43 | #endif 44 | #endif 45 | 46 | #define AP_CACHE 47 | #ifdef AP_CACHE 48 | #define AP_CACHE_NUMBER 5 49 | #endif 50 | 51 | 52 | //#define SOFTAP_ENCRYPT 53 | #ifdef SOFTAP_ENCRYPT 54 | #define PASSWORD "v*%W>L<@i&Nxe!" 55 | #endif 56 | 57 | #define USE_DNS 58 | #define ESP_DOMAIN "iot.espressif.cn" 59 | 60 | /*SSL not Ready*/ 61 | //#define SERVER_SSL_ENABLE 62 | //#define CLIENT_SSL_ENABLE 63 | //#define UPGRADE_SSL_ENABLE 64 | 65 | #elif LEWEI_PLATFORM 66 | 67 | #endif 68 | 69 | #endif 70 | 71 | -------------------------------------------------------------------------------- /include/user_configstore.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_CONFIGSTORE_H__ 2 | #define __USER_CONFIGSTORE_H__ 3 | 4 | #include "user_light_action.h" 5 | #include "c_types.h" 6 | 7 | #define CONFIG_MAC_CNT 16 8 | 9 | typedef struct { 10 | uint32_t r; 11 | uint32_t g; 12 | uint32_t b; 13 | uint32_t cw; 14 | uint32_t ww; 15 | } LedCol; 16 | 17 | 18 | typedef struct { 19 | uint8_t chsum; 20 | uint8_t seq; 21 | uint8_t pad; 22 | // uint8_t wifiChan; 23 | LedCol bval[5]; 24 | // LampMacType macData[CONFIG_MAC_CNT]; 25 | int wlanChannel[CONFIG_MAC_CNT]; 26 | } MyConfig; 27 | 28 | extern MyConfig myConfig; 29 | void configLoad(); 30 | void configSave(); 31 | 32 | #endif -------------------------------------------------------------------------------- /include/user_devicefind.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_DEVICEFIND_H__ 2 | #define __USER_DEVICEFIND_H__ 3 | 4 | void user_devicefind_start(void); 5 | sint8 user_devicefind_stop(void); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /include/user_esp_platform.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_DEVICE_H__ 2 | #define __USER_DEVICE_H__ 3 | 4 | #ifdef CLIENT_SSL_ENABLE 5 | #include "ssl/ssl_ssl.h" 6 | #endif 7 | 8 | 9 | /* NOTICE---this is for 512KB spi flash. 10 | * you can change to other sector if you use other size spi flash. */ 11 | #define ESP_PARAM_START_SEC 0x7D 12 | 13 | #define packet_size (2 * 1024) 14 | 15 | #define token_size 41 16 | 17 | struct esp_platform_saved_param { 18 | uint8 devkey[40]; 19 | uint8 token[40]; 20 | uint8 activeflag; 21 | uint8 tokenrdy; 22 | uint8 pad[2]; 23 | }; 24 | 25 | enum { 26 | DEVICE_GOT_IP=39, 27 | DEVICE_CONNECTING, 28 | DEVICE_ACTIVE_DONE, 29 | DEVICE_ACTIVE_FAIL, 30 | DEVICE_CONNECT_SERVER_FAIL 31 | }; 32 | 33 | struct dhcp_client_info { 34 | ip_addr_t ip_addr; 35 | ip_addr_t netmask; 36 | ip_addr_t gw; 37 | uint8 flag; 38 | uint8 pad[3]; 39 | }; 40 | 41 | enum{ 42 | AP_DISCONNECTED = 0, 43 | AP_CONNECTED, 44 | DNS_SUCESSES, 45 | DNS_FAIL, 46 | }; 47 | 48 | struct client_conn_param { 49 | int32 sock_fd; 50 | #ifdef CLIENT_SSL_ENABLE 51 | SSL *ssl; 52 | SSL_CTX *ssl_ctx; 53 | #endif 54 | }; 55 | 56 | void user_esp_platform_init(void); 57 | sint8 user_esp_platform_deinit(void); 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /include/user_esp_platform_timer.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_DEVICEFIND_H__ 2 | #define __USER_DEVICEFIND_H__ 3 | 4 | #define TIMER_NUMBER 10 5 | 6 | struct esp_platform_wait_timer_param { 7 | int wait_time_second; 8 | uint8 wait_time_param[12]; 9 | uint8 wait_action[16]; 10 | }; 11 | 12 | struct wait_param { 13 | uint32 min_time_backup; 14 | uint16 action_number; 15 | uint16 count; 16 | uint8 action[TIMER_NUMBER][15]; 17 | }; 18 | 19 | struct timer_bkup_param{ 20 | u32 timer_recoup; 21 | u32 timer_start_time; 22 | u32 buffer_size; 23 | int timestamp; 24 | u16 magic; 25 | char *split_buffer; 26 | char pad; 27 | }; 28 | 29 | 30 | void user_platform_timer_start(char* pbuffer); 31 | void user_platform_timer_restore(void); 32 | void user_platform_timer_bkup(void); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/user_iot_version.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_IOT_VERSION_H__ 2 | #define __USER_IOT_VERSION_H__ 3 | 4 | #include "user_config.h" 5 | 6 | #define IOT_VERSION_MAJOR 1U 7 | #define IOT_VERSION_MINOR 0U 8 | #define IOT_VERSION_REVISION 5U 9 | 10 | #define VERSION_NUM (IOT_VERSION_MAJOR * 1000 + IOT_VERSION_MINOR * 100 + IOT_VERSION_REVISION) 11 | 12 | //#define VERSION_TYPE "b" 13 | #define VERSION_TYPE "v" 14 | 15 | #if LIGHT_DEVICE 16 | #define device_type 45772 17 | #elif PLUG_DEVICE 18 | #define device_type 23701 19 | #elif SENSOR_DEVICE 20 | #define device_type 12335 21 | #endif 22 | 23 | 24 | #define ONLINE_UPGRADE 0 25 | #define LOCAL_UPGRADE 0 26 | #define ALL_UPGRADE 1 27 | #define NONE_UPGRADE 0 28 | 29 | #if ONLINE_UPGRADE 30 | #define UPGRADE_FALG "O" 31 | #elif LOCAL_UPGRADE 32 | #define UPGRADE_FALG "l" 33 | #elif ALL_UPGRADE 34 | #define UPGRADE_FALG "a" 35 | #elif NONE_UPGRADE 36 | #define UPGRADE_FALG "n" 37 | #endif 38 | 39 | #define IOT_VERSION 40 | 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /include/user_light.h: -------------------------------------------------------------------------------- 1 | #include "esp_common.h" 2 | 3 | #if LIGHT_DEVICE 4 | 5 | #ifndef __USER_LIGHT_H__ 6 | #define __USER_LIGHT_H__ 7 | /*pwm.h: function and macro definition of PWM API , driver level */ 8 | /*user_light.h: user interface for light API, user level*/ 9 | /*user_light_adj: API for color changing and lighting effects, user level*/ 10 | #include "driver/key.h" 11 | #include "pwm.h" 12 | 13 | 14 | /* NOTICE !!! ---this is for 512KB spi flash.*/ 15 | /* You can change to other sector if you use other size spi flash. */ 16 | /* Refer to the documentation about OTA support and flash mapping*/ 17 | #define PRIV_PARAM_START_SEC 0x7C 18 | #define PRIV_PARAM_SAVE 0 19 | 20 | /*note this pin may be used by PWM_3_OUT */ 21 | #define PLUG_KEY_NUM 1 22 | #define PLUG_KEY_0_IO_MUX PERIPHS_IO_MUX_MTCK_U 23 | #define PLUG_KEY_0_IO_NUM 13 24 | #define PLUG_KEY_0_IO_FUNC FUNC_GPIO13 25 | 26 | #define PWM_CHANNEL 3 27 | 28 | /*max duty value sent from APK*/ 29 | #define APP_MAX_PWM 22222 30 | 31 | 32 | enum { 33 | LIGHT_RED = 0, 34 | LIGHT_GREEN, 35 | LIGHT_BLUE, 36 | LIGHT_COLD_WHITE, 37 | LIGHT_WARM_WHITE, 38 | }; 39 | 40 | enum { 41 | LED_OFF = 0, 42 | LED_ON = 1, 43 | LED_1HZ, 44 | LED_5HZ, 45 | LED_20HZ, 46 | }; 47 | 48 | /*Definition of GPIO PIN params, for GPIO initialization*/ 49 | #define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U 50 | #define PWM_0_OUT_IO_NUM 12 51 | #define PWM_0_OUT_IO_FUNC FUNC_GPIO12 52 | 53 | #define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U 54 | #define PWM_1_OUT_IO_NUM 15 55 | #define PWM_1_OUT_IO_FUNC FUNC_GPIO15 56 | 57 | #define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTMS_U 58 | #define PWM_2_OUT_IO_NUM 14 59 | #define PWM_2_OUT_IO_FUNC FUNC_GPIO14 60 | 61 | #define PWM_3_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U 62 | #define PWM_3_OUT_IO_NUM 13 63 | #define PWM_3_OUT_IO_FUNC FUNC_GPIO13 64 | 65 | #define PWM_4_OUT_IO_MUX PERIPHS_IO_MUX_GPIO4_U 66 | #define PWM_4_OUT_IO_NUM 4 67 | #define PWM_4_OUT_IO_FUNC FUNC_GPIO4 68 | 69 | struct light_saved_param { 70 | uint32 pwm_period; 71 | uint32 pwm_duty[PWM_CHANNEL]; 72 | }; 73 | 74 | void user_light_init(void); 75 | uint32 user_light_get_duty(uint8 channel); 76 | void user_light_set_duty(uint32 duty, uint8 channel); 77 | uint32 user_light_get_period(void); 78 | void user_light_set_period(uint32 period); 79 | 80 | #endif 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /include/user_light_action.h: -------------------------------------------------------------------------------- 1 | #ifndef _USER_LIGHT_ACTION_H 2 | #define _USER_LIGHT_ACTION_H 3 | 4 | #include "esp_common.h" 5 | #include "user_config.h" 6 | 7 | 8 | //Light state. Bitfield. 9 | typedef enum { 10 | LST_ACTIVE=(1<<0), 11 | LST_ACKED=(1<<1) 12 | } LightStatusEnum; 13 | 14 | 15 | typedef struct { 16 | uint8_t mac[6]; 17 | uint16_t status; 18 | } LampMacType; 19 | 20 | 21 | 22 | #define CMD_NUM 10 23 | #define LIGHT_DEV_NUM 10 24 | 25 | 26 | #if LIGHT_DEVICE 27 | void light_EspnowInit(); 28 | void light_EspnowDeinit(); 29 | /* 30 | #elif LIGHT_SWITCH 31 | void switch_EspnowInit(); 32 | void switch_EspnowSendLightCmd(uint8 idx, uint32 channelNum, uint32* duty, uint32 period); 33 | void switch_EspnowSendCmdByChnl(uint8 chn,uint32 channelNum, uint32* duty, uint32 period); 34 | void switch_EspnowSendChnSync(uint8 channel); 35 | void switch_EspnowDeinit(); 36 | */ 37 | #endif 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/user_light_adj.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_LIGHT_ADJ_H__ 2 | #define __USER_LIGHT_ADJ_H__ 3 | /*pwm.h: function and macro definition of PWM API , driver level */ 4 | /*user_light.h: user interface for light setting, user level*/ 5 | /*user_light_adj: API for color changing and lighting effects, user level*/ 6 | 7 | 8 | 9 | 10 | /*save RGB params to flash when calling light_set_aim*/ 11 | #define SAVE_LIGHT_PARAM 0 //set to 0: do not save color params 12 | 13 | /*check current consumption and limit the total current for LED driver IC*/ 14 | /*NOTE: YOU SHOULD REPLACE WIHT THE LIMIT CURRENT OF YOUR OWN APPLICATION*/ 15 | #define LIGHT_CURRENT_LIMIT 0 //set to 0: do not limit total current 16 | #if LIGHT_CURRENT_LIMIT 17 | #define LIGHT_TOTAL_CURRENT_MAX (450*1000) //450000/1000 MA AT MOST 18 | #define LIGHT_CURRENT_MARGIN (80*1000) //80000/1000 MA CURRENT RAISES WHILE TEMPERATURE INCREASING 19 | #define LIGHT_CURRENT_MARGIN_L2 (110*1000) //110000/1000 MA 20 | #define LIGHT_CURRENT_MARGIN_L3 (140*1000) //140000/1000 MA 21 | #endif 22 | 23 | 24 | /*set target duty for PWM channels, change each channel duty gradually */ 25 | void light_set_aim(uint32 r,uint32 g,uint32 b,uint32 cw,uint32 ww,uint32 period);//'white' channel is not used in default demo 26 | void light_set_aim_r(uint32 r); 27 | void light_set_aim_g(uint32 g); 28 | void light_set_aim_b(uint32 b); 29 | void light_set_aim_cw(uint32 cw); 30 | void light_set_aim_ww(uint32 ww); 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /include/user_plug.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_ESPSWITCH_H__ 2 | #define __USER_ESPSWITCH_H__ 3 | 4 | #include "driver/key.h" 5 | 6 | /* NOTICE---this is for 512KB spi flash. 7 | * you can change to other sector if you use other size spi flash. */ 8 | #define PRIV_PARAM_START_SEC 0x7C 9 | 10 | #define PRIV_PARAM_SAVE 0 11 | 12 | #define PLUG_KEY_NUM 1 13 | 14 | #define PLUG_KEY_0_IO_MUX PERIPHS_IO_MUX_MTCK_U 15 | #define PLUG_KEY_0_IO_NUM 13 16 | #define PLUG_KEY_0_IO_FUNC FUNC_GPIO13 17 | 18 | #define PLUG_WIFI_LED_IO_MUX PERIPHS_IO_MUX_GPIO0_U 19 | #define PLUG_WIFI_LED_IO_NUM 0 20 | #define PLUG_WIFI_LED_IO_FUNC FUNC_GPIO0 21 | 22 | #define PLUG_LINK_LED_IO_MUX PERIPHS_IO_MUX_MTDI_U 23 | #define PLUG_LINK_LED_IO_NUM 12 24 | #define PLUG_LINK_LED_IO_FUNC FUNC_GPIO12 25 | 26 | #define PLUG_RELAY_LED_IO_MUX PERIPHS_IO_MUX_MTDO_U 27 | #define PLUG_RELAY_LED_IO_NUM 15 28 | #define PLUG_RELAY_LED_IO_FUNC FUNC_GPIO15 29 | 30 | #define PLUG_STATUS_OUTPUT(pin, on) GPIO_OUTPUT_SET(pin, on) 31 | 32 | enum { 33 | LED_OFF = 0, 34 | LED_ON = 1, 35 | LED_1HZ, 36 | LED_5HZ, 37 | LED_20HZ, 38 | }; 39 | 40 | struct plug_saved_param { 41 | uint8_t status; 42 | uint8_t pad[3]; 43 | }; 44 | 45 | void user_plug_init(void); 46 | uint8 user_plug_get_status(void); 47 | void user_plug_set_status(bool status); 48 | BOOL user_get_key_status(void); 49 | 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /include/user_sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef __USER_SENSOR_H__ 2 | #define __USER_SENSOR_H__ 3 | 4 | #include "user_config.h" 5 | #include "driver/key.h" 6 | 7 | #define SENSOR_KEY_NUM 1 8 | 9 | #define SENSOR_KEY_IO_MUX PERIPHS_IO_MUX_MTCK_U 10 | #define SENSOR_KEY_IO_NUM 13 11 | #define SENSOR_KEY_IO_FUNC FUNC_GPIO13 12 | 13 | #define SENSOR_WIFI_LED_IO_MUX PERIPHS_IO_MUX_GPIO0_U 14 | #define SENSOR_WIFI_LED_IO_NUM 0 15 | #define SENSOR_WIFI_LED_IO_FUNC FUNC_GPIO0 16 | 17 | #define SENSOR_LINK_LED_IO_MUX PERIPHS_IO_MUX_MTDI_U 18 | #define SENSOR_LINK_LED_IO_NUM 12 19 | #define SENSOR_LINK_LED_IO_FUNC FUNC_GPIO12 20 | 21 | #define SENSOR_UNUSED_LED_IO_MUX PERIPHS_IO_MUX_MTDO_U 22 | #define SENSOR_UNUSED_LED_IO_NUM 15 23 | #define SENSOR_UNUSED_LED_IO_FUNC FUNC_GPIO15 24 | 25 | enum { 26 | LED_OFF = 0, 27 | LED_ON = 1, 28 | LED_1HZ, 29 | LED_5HZ, 30 | LED_20HZ, 31 | }; 32 | 33 | #if HUMITURE_SUB_DEVICE 34 | bool user_mvh3004_read_th(uint8 *data); 35 | void user_mvh3004_init(void); 36 | #endif 37 | 38 | void user_sensor_init(uint8 active); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/user_switch.h: -------------------------------------------------------------------------------- 1 | #ifndef _USER_SWITCH_H 2 | #define _USER_SWITCH_H 3 | #include "os_type.h" 4 | #include "ets_sys.h" 5 | #include "osapi.h" 6 | #include "c_types.h" 7 | #include "gpio.h" 8 | 9 | #define SWITCH_HOLD_MUX PERIPHS_IO_MUX_MTMS_U 10 | #define SWITCH_HOLD_NUM 14 11 | #define SWITCH_HOLD_FUNC FUNC_GPIO14 12 | #define SWITCH_HOLD_PIN GPIO_Pin_14 13 | 14 | #define _SWITCH_GPIO_HOLD() GPIO_OUTPUT_SET(SWITCH_HOLD_NUM,0x1); 15 | #define _SWITCH_GPIO_RELEASE() GPIO_OUTPUT_SET(SWITCH_HOLD_NUM,0x0); 16 | 17 | 18 | #define SWITCH_INPUT_CHANNLE_NUM 4 19 | 20 | 21 | #define SWITCH_INPUT_01_IO_MUX PERIPHS_IO_MUX_MTDI_U 22 | #define SWITCH_INPUT_01_IO_NUM 12 23 | #define SWITCH_INPUT_01_IO_FUNC FUNC_GPIO12 24 | #define SWITCH_INPUT_01_IO_PIN GPIO_Pin_12 25 | 26 | #define SWITCH_INPUT_02_IO_MUX PERIPHS_IO_MUX_MTCK_U 27 | #define SWITCH_INPUT_02_IO_NUM 13 28 | #define SWITCH_INPUT_02_IO_FUNC FUNC_GPIO13 29 | #define SWITCH_INPUT_02_IO_PIN GPIO_Pin_13 30 | 31 | #define SWITCH_INPUT_03_IO_MUX PERIPHS_IO_MUX_GPIO4_U 32 | #define SWITCH_INPUT_03_IO_NUM 4 33 | #define SWITCH_INPUT_03_IO_FUNC FUNC_GPIO4 34 | #define SWITCH_INPUT_03_IO_PIN GPIO_Pin_4 35 | 36 | #define SWITCH_INPUT_04_IO_MUX PERIPHS_IO_MUX_GPIO5_U 37 | #define SWITCH_INPUT_04_IO_NUM 5 38 | #define SWITCH_INPUT_04_IO_FUNC FUNC_GPIO5 39 | #define SWITCH_INPUT_04_IO_PIN GPIO_Pin_5 40 | 41 | 42 | 43 | 44 | #define GPIO_Pin_0 (BIT(0)) /* Pin 0 selected */ 45 | #define GPIO_Pin_1 (BIT(1)) /* Pin 1 selected */ 46 | #define GPIO_Pin_2 (BIT(2)) /* Pin 2 selected */ 47 | #define GPIO_Pin_3 (BIT(3)) /* Pin 3 selected */ 48 | #define GPIO_Pin_4 (BIT(4)) /* Pin 4 selected */ 49 | #define GPIO_Pin_5 (BIT(5)) /* Pin 5 selected */ 50 | #define GPIO_Pin_6 (BIT(6)) /* Pin 6 selected */ 51 | #define GPIO_Pin_7 (BIT(7)) /* Pin 7 selected */ 52 | #define GPIO_Pin_8 (BIT(8)) /* Pin 8 selected */ 53 | #define GPIO_Pin_9 (BIT(9)) /* Pin 9 selected */ 54 | #define GPIO_Pin_10 (BIT(10)) /* Pin 10 selected */ 55 | #define GPIO_Pin_11 (BIT(11)) /* Pin 11 selected */ 56 | #define GPIO_Pin_12 (BIT(12)) /* Pin 12 selected */ 57 | #define GPIO_Pin_13 (BIT(13)) /* Pin 13 selected */ 58 | #define GPIO_Pin_14 (BIT(14)) /* Pin 14 selected */ 59 | #define GPIO_Pin_15 (BIT(15)) /* Pin 15 selected */ 60 | #define GPIO_Pin_All (0xFFFF) /* All pins selected */ 61 | 62 | #define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U 63 | #define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U 64 | #define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U 65 | #define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U 66 | #define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U 67 | #define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U 68 | #define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U 69 | #define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U 70 | #define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U 71 | #define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U 72 | #define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U 73 | #define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U 74 | #define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U 75 | #define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U 76 | #define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U 77 | #define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U 78 | 79 | #define GPIO_PIN_REG(i) \ 80 | (i==0) ? GPIO_PIN_REG_0: \ 81 | (i==1) ? GPIO_PIN_REG_1: \ 82 | (i==2) ? GPIO_PIN_REG_2: \ 83 | (i==3) ? GPIO_PIN_REG_3: \ 84 | (i==4) ? GPIO_PIN_REG_4: \ 85 | (i==5) ? GPIO_PIN_REG_5: \ 86 | (i==6) ? GPIO_PIN_REG_6: \ 87 | (i==7) ? GPIO_PIN_REG_7: \ 88 | (i==8) ? GPIO_PIN_REG_8: \ 89 | (i==9) ? GPIO_PIN_REG_9: \ 90 | (i==10)? GPIO_PIN_REG_10: \ 91 | (i==11)? GPIO_PIN_REG_11: \ 92 | (i==12)? GPIO_PIN_REG_12: \ 93 | (i==13)? GPIO_PIN_REG_13: \ 94 | (i==14)? GPIO_PIN_REG_14: \ 95 | GPIO_PIN_REG_15 96 | 97 | #define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4) 98 | 99 | #define GPIO_ID_IS_PIN_REGISTER(reg_id) \ 100 | ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1))) 101 | 102 | #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0) 103 | #if 0 104 | typedef enum { 105 | GPIO_PIN_INTR_DISABLE = 0, 106 | GPIO_PIN_INTR_POSEDGE = 1, 107 | GPIO_PIN_INTR_NEGEDGE = 2, 108 | GPIO_PIN_INTR_ANYEDGE = 3, 109 | GPIO_PIN_INTR_LOLEVEL = 4, 110 | GPIO_PIN_INTR_HILEVEL = 5 111 | } GPIO_INT_TYPE; 112 | #endif 113 | 114 | typedef enum { 115 | GPIO_Mode_Input = 0x0, 116 | GPIO_Mode_Out_OD, 117 | GPIO_Mode_Output , 118 | GPIO_Mode_Sigma_Delta , 119 | } GPIOMode_TypeDef; 120 | 121 | typedef enum { 122 | GPIO_PullUp_DIS = 0x0, 123 | GPIO_PullUp_EN = 0x1, 124 | } GPIO_Pullup_IF; 125 | 126 | typedef struct { 127 | uint16 GPIO_Pin; 128 | GPIOMode_TypeDef GPIO_Mode; 129 | GPIO_Pullup_IF GPIO_Pullup; 130 | GPIO_INT_TYPE GPIO_IntrType; 131 | } GPIO_ConfigTypeDef; 132 | 133 | #define GPIO_PIN_DRIVER_LSB 2 134 | #define GPIO_PIN_DRIVER_MASK (0x00000001<(b)?(a):(b)) /**< Find the maximum of 2 numbers. */ 82 | 83 | void user_webserver_start(void); 84 | sint8 user_webserver_stop(void); 85 | 86 | #endif 87 | 88 | #endif 89 | 90 | -------------------------------------------------------------------------------- /libesphttpd/Makefile: -------------------------------------------------------------------------------- 1 | 2 | # Directory the Makefile is in. Please don't include other Makefiles before this. 3 | THISDIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) 4 | 5 | #Include httpd config from lower level, if it exists 6 | -include ../esphttpdconfig.mk 7 | 8 | 9 | #Default options. If you want to change them, please create ../esphttpdconfig.mk with the options you want in it. 10 | GZIP_COMPRESSION ?= no 11 | COMPRESS_W_YUI ?= no 12 | YUI-COMPRESSOR ?= /usr/bin/yui-compressor 13 | USE_HEATSHRINK ?= yes 14 | 15 | 16 | 17 | # Output directors to store intermediate compiled files 18 | # relative to the project directory 19 | BUILD_BASE = build 20 | 21 | # Base directory for the compiler. Needs a / at the end; if not set it'll use the tools that are in 22 | # the PATH. 23 | XTENSA_TOOLS_ROOT ?= 24 | 25 | # base directory of the ESP8266 SDK package, absolute 26 | #SDK_BASE ?= /opt/Espressif/ESP8266_SDK 27 | SDK_BASE = $(SDK_PATH) 28 | HTMLDIR = ../html_plug 29 | # name for the target project 30 | LIB = libesphttpd.a libwebpages-espfs.a 31 | 32 | # which modules (subdirectories) of the project to include in compiling 33 | MODULES = espfs core util 34 | EXTRA_INCDIR = ./include \ 35 | . \ 36 | lib/heatshrink/ 37 | 38 | 39 | # compiler flags using during compilation of source files 40 | CFLAGS = -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ 41 | -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH \ 42 | -Wno-address -ffunction-sections -fdata-sections 43 | 44 | # various paths from the SDK used in this project 45 | SDK_LIBDIR = lib 46 | SDK_LDDIR = ld 47 | 48 | SDK_INCDIR = include 49 | SDK_ESP_INCDIR = include/espressif 50 | SDK_OS_INCDIR = include/freertos 51 | SDK_JSON_INCDIR = include/json 52 | SDK_LWIP_INCDIR = include/lwip 53 | SDK_LWIP4_INCDIR = include/lwip/ipv4 54 | SDK_LWIP6_INCDIR = include/lwip/ipv6 55 | SDK_EXTRA_INCDIR = extra_include 56 | SDK_XTENSA_INCDIR = extra_include/xtensa 57 | 58 | # select which tools to use as compiler, librarian and linker 59 | CC := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-gcc 60 | AR := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-ar 61 | LD := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-gcc 62 | OBJCOPY := $(XTENSA_TOOLS_ROOT)xtensa-lx106-elf-objcopy 63 | 64 | #### 65 | #### no user configurable options below here 66 | #### 67 | SRC_DIR := $(MODULES) 68 | BUILD_DIR := $(addprefix $(BUILD_BASE)/,$(MODULES)) 69 | 70 | SDK_INCDIR := $(addprefix -I$(SDK_BASE)/,$(SDK_INCDIR)) 71 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_ESP_INCDIR)) 72 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_OS_INCDIR)) 73 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_JSON_INCDIR)) 74 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_LWIP_INCDIR)) 75 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_LWIP4_INCDIR)) 76 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_LWIP6_INCDIR)) 77 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_EXTRA_INCDIR)) 78 | SDK_INCDIR += $(addprefix -I$(SDK_BASE)/,$(SDK_XTENSA_INCDIR)) 79 | 80 | 81 | SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c)) 82 | OBJ := $(patsubst %.c,$(BUILD_BASE)/%.o,$(SRC)) 83 | 84 | INCDIR := $(addprefix -I,$(SRC_DIR)) 85 | EXTRA_INCDIR := $(addprefix -I,$(EXTRA_INCDIR)) 86 | MODULE_INCDIR := $(addsuffix /include,$(INCDIR)) 87 | 88 | V ?= $(VERBOSE) 89 | ifeq ("$(V)","1") 90 | Q := 91 | vecho := @true 92 | else 93 | Q := @ 94 | vecho := @echo 95 | endif 96 | 97 | ifeq ("$(GZIP_COMPRESSION)","yes") 98 | CFLAGS += -DGZIP_COMPRESSION 99 | endif 100 | 101 | ifeq ("$(USE_HEATSHRINK)","yes") 102 | CFLAGS += -DESPFS_HEATSHRINK 103 | endif 104 | 105 | vpath %.c $(SRC_DIR) 106 | 107 | define compile-objects 108 | $1/%.o: %.c 109 | $(vecho) "CC $$<" 110 | $(Q) $(CC) $(INCDIR) $(MODULE_INCDIR) $(EXTRA_INCDIR) $(SDK_INCDIR) $(CFLAGS) -c $$< -o $$@ 111 | endef 112 | 113 | .PHONY: all checkdirs clean webpages.espfs submodules 114 | 115 | all: checkdirs $(LIB) webpages.espfs 116 | 117 | submodules: lib/heatshrink/Makefile 118 | lib/heatshrink/Makefile: 119 | $(Q) echo "Heatshrink isn't found. Checking out submodules to fetch it." 120 | $(Q) git submodule init 121 | $(Q) git submodule update 122 | 123 | 124 | $(LIB): $(BUILD_DIR) submodules $(OBJ) 125 | $(vecho) "AR $@" 126 | $(Q) $(AR) cru $@ $(OBJ) 127 | 128 | checkdirs: $(BUILD_DIR) 129 | 130 | $(BUILD_DIR): 131 | $(Q) mkdir -p $@ 132 | 133 | 134 | webpages.espfs: $(HTMLDIR) espfs/mkespfsimage/mkespfsimage 135 | ifeq ("$(COMPRESS_W_YUI)","yes") 136 | $(Q) rm -rf html_compressed; 137 | $(Q) cp -r ../html html_compressed; 138 | $(Q) echo "Compression assets with yui-compressor. This may take a while..." 139 | $(Q) for file in `find html_compressed -type f -name "*.js"`; do $(YUI-COMPRESSOR) --type js $$file -o $$file; done 140 | $(Q) for file in `find html_compressed -type f -name "*.css"`; do $(YUI-COMPRESSOR) --type css $$file -o $$file; done 141 | $(Q) awk "BEGIN {printf \"YUI compression ratio was: %.2f%%\\n\", (`du -b -s html_compressed/ | sed 's/\([0-9]*\).*/\1/'`/`du -b -s ../html/ | sed 's/\([0-9]*\).*/\1/'`)*100}" 142 | # mkespfsimage will compress html, css and js files with gzip by default if enabled 143 | # override with -g cmdline parameter 144 | $(Q) cd html_compressed; find . | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd ..; 145 | else 146 | $(Q) cd $(HTMLDIR); find . | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd .. 147 | endif 148 | 149 | libwebpages-espfs.a: webpages.espfs 150 | $(Q) $(OBJCOPY) -I binary -O elf32-xtensa-le -B xtensa --rename-section .data=.irom0.literal \ 151 | webpages.espfs build/webpages.espfs.o.tmp 152 | $(Q) $(LD) -nostdlib -Wl,-r build/webpages.espfs.o.tmp -o build/webpages.espfs.o -Wl,-T webpages.espfs.ld 153 | $(Q) $(AR) cru $@ build/webpages.espfs.o 154 | 155 | 156 | espfs/mkespfsimage/mkespfsimage: espfs/mkespfsimage/ 157 | $(Q) $(MAKE) -C espfs/mkespfsimage USE_HEATSHRINK="$(USE_HEATSHRINK)" GZIP_COMPRESSION="$(GZIP_COMPRESSION)" 158 | 159 | clean: 160 | $(Q) rm -f $(LIB) 161 | $(Q) find $(BUILD_BASE) -type f | xargs rm -f 162 | $(Q) make -C espfs/mkespfsimage/ clean 163 | $(Q) rm -rf $(FW_BASE) 164 | $(Q) rm -f webpages.espfs libwebpages-espfs.a 165 | ifeq ("$(COMPRESS_W_YUI)","yes") 166 | $(Q) rm -rf html_compressed 167 | endif 168 | 169 | $(foreach bdir,$(BUILD_DIR),$(eval $(call compile-objects,$(bdir)))) -------------------------------------------------------------------------------- /libesphttpd/core/auth.c: -------------------------------------------------------------------------------- 1 | /* 2 | HTTP auth implementation. Only does basic authentication for now. 3 | */ 4 | 5 | /* 6 | * ---------------------------------------------------------------------------- 7 | * "THE BEER-WARE LICENSE" (Revision 42): 8 | * Jeroen Domburg wrote this file. As long as you retain 9 | * this notice you can do whatever you want with this stuff. If we meet some day, 10 | * and you think this stuff is worth it, you can buy me a beer in return. 11 | * ---------------------------------------------------------------------------- 12 | */ 13 | 14 | 15 | #include 16 | #include "auth.h" 17 | #include "base64.h" 18 | 19 | int authBasic(HttpdConnData *connData) { 20 | const char *forbidden="401 Forbidden."; 21 | int no=0; 22 | int r; 23 | char hdr[(AUTH_MAX_USER_LEN+AUTH_MAX_PASS_LEN+2)*10]; 24 | char userpass[AUTH_MAX_USER_LEN+AUTH_MAX_PASS_LEN+2]; 25 | char user[AUTH_MAX_USER_LEN]; 26 | char pass[AUTH_MAX_PASS_LEN]; 27 | if (connData->conn==NULL) { 28 | //Connection aborted. Clean up. 29 | return HTTPD_CGI_DONE; 30 | } 31 | 32 | r=httpdGetHeader(connData, "Authorization", hdr, sizeof(hdr)); 33 | if (r && strncmp(hdr, "Basic", 5)==0) { 34 | r=base64_decode(strlen(hdr)-6, hdr+6, sizeof(userpass), (unsigned char *)userpass); 35 | if (r<0) r=0; //just clean out string on decode error 36 | userpass[r]=0; //zero-terminate user:pass string 37 | // printf("Auth: %s\n", userpass); 38 | while (((AuthGetUserPw)(connData->cgiArg))(connData, no, 39 | user, AUTH_MAX_USER_LEN, pass, AUTH_MAX_PASS_LEN)) { 40 | //Check user/pass against auth header 41 | if (strlen(userpass)==strlen(user)+strlen(pass)+1 && 42 | strncmp(userpass, user, strlen(user))==0 && 43 | userpass[strlen(user)]==':' && 44 | strcmp(userpass+strlen(user)+1, pass)==0) { 45 | //Authenticated. Yay! 46 | return HTTPD_CGI_AUTHENTICATED; 47 | } 48 | no++; //Not authenticated with this user/pass. Check next user/pass combo. 49 | } 50 | } 51 | 52 | //Not authenticated. Go bug user with login screen. 53 | httpdStartResponse(connData, 401); 54 | httpdHeader(connData, "Content-Type", "text/plain"); 55 | httpdHeader(connData, "WWW-Authenticate", "Basic realm=\""HTTP_AUTH_REALM"\""); 56 | httpdEndHeaders(connData); 57 | httpdSend(connData, forbidden, -1); 58 | //Okay, all done. 59 | return HTTPD_CGI_DONE; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /libesphttpd/core/base64.c: -------------------------------------------------------------------------------- 1 | /* base64.c : base-64 / MIME encode/decode */ 2 | /* PUBLIC DOMAIN - Jon Mayo - November 13, 2003 */ 3 | #include 4 | #include "base64.h" 5 | 6 | static const uint8_t base64dec_tab[256]= { 7 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 8 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 9 | 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, 10 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, 11 | 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 12 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, 13 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 14 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, 15 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 16 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 17 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 18 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 19 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 20 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 21 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 22 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 23 | }; 24 | 25 | #if 0 26 | static int base64decode(const char in[4], char out[3]) { 27 | uint8_t v[4]; 28 | 29 | v[0]=base64dec_tab[(unsigned)in[0]]; 30 | v[1]=base64dec_tab[(unsigned)in[1]]; 31 | v[2]=base64dec_tab[(unsigned)in[2]]; 32 | v[3]=base64dec_tab[(unsigned)in[3]]; 33 | 34 | out[0]=(v[0]<<2)|(v[1]>>4); 35 | out[1]=(v[1]<<4)|(v[2]>>2); 36 | out[2]=(v[2]<<6)|(v[3]); 37 | return (v[0]|v[1]|v[2]|v[3])!=255 ? in[3]=='=' ? in[2]=='=' ? 1 : 2 : 3 : 0; 38 | } 39 | #endif 40 | 41 | /* decode a base64 string in one shot */ 42 | int base64_decode(size_t in_len, const char *in, size_t out_len, unsigned char *out) { 43 | unsigned int ii, io; 44 | uint32_t v; 45 | unsigned int rem; 46 | 47 | for(io=0,ii=0,v=0,rem=0;ii=8) { 56 | rem-=8; 57 | if(io>=out_len) return -1; /* truncation is failure */ 58 | out[io++]=(v>>rem)&255; 59 | } 60 | } 61 | if(rem>=8) { 62 | rem-=8; 63 | if(io>=out_len) return -1; /* truncation is failure */ 64 | out[io++]=(v>>rem)&255; 65 | } 66 | return io; 67 | } 68 | 69 | //Only need decode functions for now. 70 | #if 0 71 | 72 | static const uint8_t base64enc_tab[64]= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 73 | 74 | void base64encode(const unsigned char in[3], unsigned char out[4], int count) { 75 | out[0]=base64enc_tab[(in[0]>>2)]; 76 | out[1]=base64enc_tab[((in[0]&3)<<4)|(in[1]>>4)]; 77 | out[2]=count<2 ? '=' : base64enc_tab[((in[1]&15)<<2)|(in[2]>>6)]; 78 | out[3]=count<3 ? '=' : base64enc_tab[(in[2]&63)]; 79 | } 80 | 81 | 82 | int base64_encode(size_t in_len, const unsigned char *in, size_t out_len, char *out) { 83 | unsigned ii, io; 84 | uint_least32_t v; 85 | unsigned rem; 86 | 87 | for(io=0,ii=0,v=0,rem=0;ii=6) { 93 | rem-=6; 94 | if(io>=out_len) return -1; /* truncation is failure */ 95 | out[io++]=base64enc_tab[(v>>rem)&63]; 96 | } 97 | } 98 | if(rem) { 99 | v<<=(6-rem); 100 | if(io>=out_len) return -1; /* truncation is failure */ 101 | out[io++]=base64enc_tab[v&63]; 102 | } 103 | while(io&3) { 104 | if(io>=out_len) return -1; /* truncation is failure */ 105 | out[io++]='='; 106 | } 107 | if(io>=out_len) return -1; /* no room for null terminator */ 108 | out[io]=0; 109 | return io; 110 | } 111 | 112 | #endif -------------------------------------------------------------------------------- /libesphttpd/core/base64.h: -------------------------------------------------------------------------------- 1 | #ifndef BASE64_H 2 | #define BASE64_H 3 | 4 | int base64_decode(size_t in_len, const char *in, size_t out_len, unsigned char *out); 5 | 6 | #endif -------------------------------------------------------------------------------- /libesphttpd/core/httpdespfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | Connector to let httpd use the espfs filesystem to serve the files in it. 3 | */ 4 | 5 | /* 6 | * ---------------------------------------------------------------------------- 7 | * "THE BEER-WARE LICENSE" (Revision 42): 8 | * Jeroen Domburg wrote this file. As long as you retain 9 | * this notice you can do whatever you want with this stuff. If we meet some day, 10 | * and you think this stuff is worth it, you can buy me a beer in return. 11 | * ---------------------------------------------------------------------------- 12 | */ 13 | 14 | #include 15 | 16 | #include "lwip/sockets.h" 17 | 18 | #include "httpdespfs.h" 19 | #include "espfs.h" 20 | #include "espfsformat.h" 21 | 22 | // The static files marked with FLAG_GZIP are compressed and will be served with GZIP compression. 23 | // If the client does not advertise that he accepts GZIP send following warning message (telnet users for e.g.) 24 | static const char *gzipNonSupportedMessage = "HTTP/1.0 501 Not implemented\r\nServer: esp8266-httpd/"HTTPDVER"\r\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 52\r\n\r\nYour browser does not accept gzip-compressed data.\r\n"; 25 | 26 | 27 | //This is a catch-all cgi function. It takes the url passed to it, looks up the corresponding 28 | //path in the filesystem and if it exists, passes the file through. This simulates what a normal 29 | //webserver would do with static files. 30 | int cgiEspFsHook(HttpdConnData *connData) { 31 | EspFsFile *file=connData->cgiData; 32 | int len; 33 | char buff[1024]; 34 | char acceptEncodingBuffer[64]; 35 | int isGzip; 36 | 37 | if (connData->conn==NULL) { 38 | //Connection aborted. Clean up. 39 | if (file!=NULL)espFsClose(file); 40 | return HTTPD_CGI_DONE; 41 | } 42 | 43 | if (file==NULL) { 44 | //First call to this cgi. Open the file so we can read it. 45 | file=espFsOpen(connData->url); 46 | if (file==NULL) { 47 | return HTTPD_CGI_NOTFOUND; 48 | } 49 | 50 | // The gzip checking code is intentionally without #ifdefs because checking 51 | // for FLAG_GZIP (which indicates gzip compressed file) is very easy, doesn't 52 | // mean additional overhead and is actually safer to be on at all times. 53 | // If there are no gzipped files in the image, the code bellow will not cause any harm. 54 | 55 | // Check if requested file was GZIP compressed 56 | isGzip = espFsFlags(file) & FLAG_GZIP; 57 | if (isGzip) { 58 | // Check the browser's "Accept-Encoding" header. If the client does not 59 | // advertise that he accepts GZIP send a warning message (telnet users for e.g.) 60 | httpdGetHeader(connData, "Accept-Encoding", acceptEncodingBuffer, 64); 61 | if (strstr(acceptEncodingBuffer, "gzip") == NULL) { 62 | //No Accept-Encoding: gzip header present 63 | httpdSend(connData, gzipNonSupportedMessage, -1); 64 | espFsClose(file); 65 | return HTTPD_CGI_DONE; 66 | } 67 | } 68 | 69 | connData->cgiData=file; 70 | httpdStartResponse(connData, 200); 71 | httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url)); 72 | if (isGzip) { 73 | httpdHeader(connData, "Content-Encoding", "gzip"); 74 | } 75 | httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate"); 76 | httpdEndHeaders(connData); 77 | return HTTPD_CGI_MORE; 78 | } 79 | 80 | len=espFsRead(file, buff, 1024); 81 | if (len>0) write(connData->conn->sockfd, (uint8 *)buff, len); 82 | if (len!=1024) { 83 | //We're done. 84 | espFsClose(file); 85 | return HTTPD_CGI_DONE; 86 | } else { 87 | //Ok, till next time. 88 | return HTTPD_CGI_MORE; 89 | } 90 | } 91 | 92 | 93 | //cgiEspFsTemplate can be used as a template. 94 | 95 | typedef struct { 96 | EspFsFile *file; 97 | void *tplArg; 98 | char token[64]; 99 | int tokenPos; 100 | } TplData; 101 | 102 | typedef void (* TplCallback)(HttpdConnData *connData, char *token, void **arg); 103 | 104 | int cgiEspFsTemplate(HttpdConnData *connData) { 105 | TplData *tpd=connData->cgiData; 106 | int len; 107 | int x, sp=0; 108 | char *e=NULL; 109 | char buff[1025]; 110 | 111 | if (connData->conn==NULL) { 112 | //Connection aborted. Clean up. 113 | ((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg); 114 | espFsClose(tpd->file); 115 | free(tpd); 116 | return HTTPD_CGI_DONE; 117 | } 118 | 119 | if (tpd==NULL) { 120 | //First call to this cgi. Open the file so we can read it. 121 | 122 | tpd=(TplData *)malloc(sizeof(TplData)); 123 | tpd->file=espFsOpen(connData->url); 124 | tpd->tplArg=NULL; 125 | tpd->tokenPos=-1; 126 | if (tpd->file==NULL) { 127 | espFsClose(tpd->file); 128 | free(tpd); 129 | return HTTPD_CGI_NOTFOUND; 130 | } 131 | if (espFsFlags(tpd->file) & FLAG_GZIP) { 132 | espFsClose(tpd->file); 133 | free(tpd); 134 | return HTTPD_CGI_NOTFOUND; 135 | } 136 | connData->cgiData=tpd; 137 | httpdStartResponse(connData, 200); 138 | httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url)); 139 | httpdEndHeaders(connData); 140 | return HTTPD_CGI_MORE; 141 | } 142 | 143 | len=espFsRead(tpd->file, buff, 1024); 144 | if (len>0) { 145 | sp=0; 146 | e=buff; 147 | for (x=0; xtokenPos==-1) { 149 | //Inside ordinary text. 150 | if (buff[x]=='%') { 151 | //Send raw data up to now 152 | if (sp!=0) httpdSend(connData, e, sp); 153 | sp=0; 154 | //Go collect token chars. 155 | tpd->tokenPos=0; 156 | } else { 157 | sp++; 158 | } 159 | } else { 160 | if (buff[x]=='%') { 161 | if (tpd->tokenPos==0) { 162 | //This is the second % of a %% escape string. 163 | //Send a single % and resume with the normal program flow. 164 | httpdSend(connData, "%", 1); 165 | } else { 166 | //This is an actual token. 167 | tpd->token[tpd->tokenPos++]=0; //zero-terminate token 168 | ((TplCallback)(connData->cgiArg))(connData, tpd->token, &tpd->tplArg); 169 | } 170 | //Go collect normal chars again. 171 | e=&buff[x+1]; 172 | tpd->tokenPos=-1; 173 | } else { 174 | if (tpd->tokenPos<(sizeof(tpd->token)-1)) tpd->token[tpd->tokenPos++]=buff[x]; 175 | } 176 | } 177 | } 178 | } 179 | //Send remaining bit. 180 | 181 | if (sp!=0) httpdSend(connData, e, sp); 182 | if (len!=1024) { 183 | //We're done. 184 | ((TplCallback)(connData->cgiArg))(connData, NULL, &tpd->tplArg); 185 | espFsClose(tpd->file); 186 | free(tpd); 187 | return HTTPD_CGI_DONE; 188 | } else { 189 | //Ok, till next time. 190 | return HTTPD_CGI_MORE; 191 | } 192 | } 193 | 194 | -------------------------------------------------------------------------------- /libesphttpd/espfs/espfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | This is a simple read-only implementation of a file system. It uses a block of data coming from the 3 | mkespfsimg tool, and can use that block to do abstracted operations on the files that are in there. 4 | It's written for use with httpd, but doesn't need to be used as such. 5 | */ 6 | 7 | /* 8 | * ---------------------------------------------------------------------------- 9 | * "THE BEER-WARE LICENSE" (Revision 42): 10 | * Jeroen Domburg wrote this file. As long as you retain 11 | * this notice you can do whatever you want with this stuff. If we meet some day, 12 | * and you think this stuff is worth it, you can buy me a beer in return. 13 | * ---------------------------------------------------------------------------- 14 | */ 15 | 16 | 17 | //These routines can also be tested by comping them in with the espfstest tool. This 18 | //simplifies debugging, but needs some slightly different headers. The #ifdef takes 19 | //care of that. 20 | 21 | #ifdef __ets__ 22 | //esp build 23 | #include 24 | #else 25 | //Test build 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define 32 | #endif 33 | 34 | #include "espfsformat.h" 35 | #include "espfs.h" 36 | 37 | #ifdef ESPFS_HEATSHRINK 38 | #include "heatshrink_config_custom.h" 39 | #include "heatshrink_decoder.h" 40 | #endif 41 | 42 | static char* espFsData = NULL; 43 | 44 | 45 | struct EspFsFile { 46 | EspFsHeader *header; 47 | char decompressor; 48 | int32_t posDecomp; 49 | char *posStart; 50 | char *posComp; 51 | void *decompData; 52 | }; 53 | 54 | /* 55 | Available locations, at least in my flash, with boundaries partially guessed. This 56 | is using 0.9.1/0.9.2 SDK on a not-too-new module. 57 | 0x00000 (0x10000): Code/data (RAM data?) 58 | 0x10000 (0x02000): Gets erased by something? 59 | 0x12000 (0x2E000): Free (filled with zeroes) (parts used by ESPCloud and maybe SSL) 60 | 0x40000 (0x20000): Code/data (ROM data?) 61 | 0x60000 (0x1C000): Free 62 | 0x7c000 (0x04000): Param store 63 | 0x80000 - end of flash 64 | 65 | Accessing the flash through the mem emulation at 0x40200000 is a bit hairy: All accesses 66 | *must* be aligned 32-bit accesses. Reading a short, byte or unaligned word will result in 67 | a memory exception, crashing the program. 68 | */ 69 | 70 | EspFsInitResult espFsInit(void *flashAddress) { 71 | // base address must be aligned to 4 bytes 72 | if (((int)flashAddress & 3) != 0) { 73 | printf("error: not 4 bytes aligned addr:%p", flashAddress); 74 | return ESPFS_INIT_RESULT_BAD_ALIGN; 75 | } 76 | 77 | // check if there is valid header at address 78 | EspFsHeader testHeader; 79 | printf("espFsInit test 0x%x\n",(unsigned int)&testHeader); 80 | memcpy(&testHeader, flashAddress, sizeof(EspFsHeader)); 81 | if (testHeader.magic != ESPFS_MAGIC) { 82 | return ESPFS_INIT_RESULT_NO_IMAGE; 83 | } 84 | 85 | espFsData = (char *)flashAddress; 86 | return ESPFS_INIT_RESULT_OK; 87 | } 88 | 89 | //Copies len bytes over from dst to src, but does it using *only* 90 | //aligned 32-bit reads. Yes, it's no too optimized but it's short and sweet and it works. 91 | 92 | //ToDo: perhaps os_memcpy also does unaligned accesses? 93 | #ifdef __ets__ 94 | void memcpyAligned(char *dst, char *src, int len) { 95 | int x; 96 | int w, b; 97 | for (x=0; x>0); 101 | if (b==1) *dst=(w>>8); 102 | if (b==2) *dst=(w>>16); 103 | if (b==3) *dst=(w>>24); 104 | dst++; src++; 105 | } 106 | } 107 | #else 108 | #define memcpyAligned memcpy 109 | #endif 110 | 111 | // Returns flags of opened file. 112 | int espFsFlags(EspFsFile *fh) { 113 | if (fh == NULL) { 114 | printf("File handle not ready\n"); 115 | return -1; 116 | } 117 | 118 | int8_t flags; 119 | memcpyAligned((char*)&flags, (char*)&fh->header->flags, 1); 120 | return (int)flags; 121 | } 122 | 123 | //Open a file and return a pointer to the file desc struct. 124 | EspFsFile *espFsOpen(char *fileName) { 125 | if (espFsData == NULL) { 126 | printf("should call espfsint first\n"); 127 | return NULL; 128 | } 129 | char *p=espFsData; 130 | char *hpos; 131 | char namebuf[256]; 132 | EspFsHeader h; 133 | EspFsFile *r; 134 | //Strip initial slashes 135 | while(fileName[0]=='/') fileName++; 136 | //Go find that file! 137 | while(1) { 138 | hpos=p; 139 | //Grab the next file header. 140 | printf("espFsOpen 0x%x, p = 0x%x \n",(int)&h, (int)p); 141 | memcpy(&h, p, sizeof(EspFsHeader)); 142 | if (h.magic!=ESPFS_MAGIC) { 143 | printf("Magic mismatch. EspFS image broken.\n"); 144 | return NULL; 145 | } 146 | if (h.flags&FLAG_LASTFILE) { 147 | printf("End of image.\n"); 148 | return NULL; 149 | } 150 | //Grab the name of the file. 151 | p+=sizeof(EspFsHeader); 152 | memcpy(namebuf, p, sizeof(namebuf)); 153 | // printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n", 154 | // namebuf, (unsigned int)h.nameLen, (unsigned int)h.fileLenComp, h.compression, h.flags); 155 | if (strcmp(namebuf, fileName)==0) { 156 | //Yay, this is the file we need! 157 | p+=h.nameLen; //Skip to content. 158 | r=(EspFsFile *)malloc(sizeof(EspFsFile)); //Alloc file desc mem 159 | // printf("Alloc %p\n", r); 160 | if (r==NULL) return NULL; 161 | r->header=(EspFsHeader *)hpos; 162 | r->decompressor=h.compression; 163 | r->posComp=p; 164 | r->posStart=p; 165 | r->posDecomp=0; 166 | if (h.compression==COMPRESS_NONE) { 167 | r->decompData=NULL; 168 | #ifdef ESPFS_HEATSHRINK 169 | } else if (h.compression==COMPRESS_HEATSHRINK) { 170 | //File is compressed with Heatshrink. 171 | char parm; 172 | heatshrink_decoder *dec; 173 | //Decoder params are stored in 1st byte. 174 | memcpyAligned(&parm, r->posComp, 1); 175 | r->posComp++; 176 | printf("Heatshrink compressed file; decode parms = %x\n", parm); 177 | dec=heatshrink_decoder_alloc(16, (parm>>4)&0xf, parm&0xf); 178 | r->decompData=dec; 179 | #endif 180 | } else { 181 | printf("Invalid compression: %d\n", h.compression); 182 | return NULL; 183 | } 184 | return r; 185 | } 186 | //We don't need this file. Skip name and file 187 | p+=h.nameLen+h.fileLenComp; 188 | if ((int)p&3) p+=4-((int)p&3); //align to next 32bit val 189 | } 190 | } 191 | 192 | //Read len bytes from the given file into buff. Returns the actual amount of bytes read. 193 | int espFsRead(EspFsFile *fh, char *buff, int len) { 194 | int flen, fdlen; 195 | if (fh==NULL) return 0; 196 | //Cache file length. 197 | memcpyAligned((char*)&flen, (char*)&fh->header->fileLenComp, 4); 198 | memcpyAligned((char*)&fdlen, (char*)&fh->header->fileLenDecomp, 4); 199 | //Do stuff depending on the way the file is compressed. 200 | if (fh->decompressor==COMPRESS_NONE) { 201 | int toRead; 202 | toRead=flen-(fh->posComp-fh->posStart); 203 | if (len>toRead) len=toRead; 204 | // printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp); 205 | memcpyAligned(buff, fh->posComp, len); 206 | fh->posDecomp+=len; 207 | fh->posComp+=len; 208 | // printf("Done reading %d bytes, pos=%x\n", len, fh->posComp); 209 | return len; 210 | #ifdef ESPFS_HEATSHRINK 211 | } else if (fh->decompressor==COMPRESS_HEATSHRINK) { 212 | int decoded=0; 213 | size_t elen, rlen; 214 | char ebuff[16]; 215 | heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData; 216 | // printf("Alloc %p\n", dec); 217 | if (fh->posDecomp == fdlen) { 218 | return 0; 219 | } 220 | 221 | // We must ensure that whole file is decompressed and written to output buffer. 222 | // This means even when there is no input data (elen==0) try to poll decoder until 223 | // posDecomp equals decompressed file length 224 | 225 | while(decodedposComp - fh->posStart); 229 | if (elen>0) { 230 | memcpyAligned(ebuff, fh->posComp, 16); 231 | heatshrink_decoder_sink(dec, (uint8_t *)ebuff, (elen>16)?16:elen, &rlen); 232 | fh->posComp+=rlen; 233 | } 234 | //Grab decompressed data and put into buff 235 | heatshrink_decoder_poll(dec, (uint8_t *)buff, len-decoded, &rlen); 236 | fh->posDecomp+=rlen; 237 | buff+=rlen; 238 | decoded+=rlen; 239 | 240 | // printf("Elen %d rlen %d d %d pd %ld fdl %d\n",elen,rlen,decoded, fh->posDecomp, fdlen); 241 | 242 | if (elen == 0) { 243 | if (fh->posDecomp == fdlen) { 244 | // printf("Decoder finish\n"); 245 | heatshrink_decoder_finish(dec); 246 | } 247 | return decoded; 248 | } 249 | } 250 | return len; 251 | #endif 252 | } 253 | return 0; 254 | } 255 | 256 | //Close the file. 257 | void espFsClose(EspFsFile *fh) { 258 | if (fh==NULL) return; 259 | #ifdef ESPFS_HEATSHRINK 260 | if (fh->decompressor==COMPRESS_HEATSHRINK) { 261 | heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData; 262 | heatshrink_decoder_free(dec); 263 | // printf("Freed %p\n", dec); 264 | } 265 | #endif 266 | printf("espFsClose %p\n", fh); 267 | free(fh); 268 | } 269 | 270 | 271 | 272 | -------------------------------------------------------------------------------- /libesphttpd/espfs/espfsformat.h: -------------------------------------------------------------------------------- 1 | #ifndef ESPROFSFORMAT_H 2 | #define ESPROFSFORMAT_H 3 | 4 | /* 5 | Stupid cpio-like tool to make read-only 'filesystems' that live on the flash SPI chip of the module. 6 | Can (will) use lzf compression (when I come around to it) to make shit quicker. Aligns names, files, 7 | headers on 4-byte boundaries so the SPI abstraction hardware in the ESP8266 doesn't crap on itself 8 | when trying to do a <4byte or unaligned read. 9 | */ 10 | 11 | /* 12 | The idea 'borrows' from cpio: it's basically a concatenation of {header, filename, file} data. 13 | Header, filename and file data is 32-bit aligned. The last file is indicated by data-less header 14 | with the FLAG_LASTFILE flag set. 15 | */ 16 | 17 | 18 | #define FLAG_LASTFILE (1<<0) 19 | #define FLAG_GZIP (1<<1) 20 | #define COMPRESS_NONE 0 21 | #define COMPRESS_HEATSHRINK 1 22 | #define ESPFS_MAGIC 0x73665345 23 | 24 | typedef struct { 25 | int32_t magic; 26 | int8_t flags; 27 | int8_t compression; 28 | int16_t nameLen; 29 | int32_t fileLenComp; 30 | int32_t fileLenDecomp; 31 | } __attribute__((packed)) EspFsHeader; 32 | 33 | #endif -------------------------------------------------------------------------------- /libesphttpd/espfs/espfstest/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-I../../lib/heatshrink -I.. -std=gnu99 -DESPFS_HEATSHRINK 2 | 3 | espfstest: main.o espfs.o heatshrink_decoder.o 4 | $(CC) -o $@ $^ 5 | 6 | espfs.o: ../espfs.c 7 | $(CC) $(CFLAGS) -c $^ -o $@ 8 | 9 | heatshrink_decoder.o: ../heatshrink_decoder.c 10 | $(CC) $(CFLAGS) -c $^ -o $@ 11 | 12 | clean: 13 | rm -f *.o espfstest 14 | -------------------------------------------------------------------------------- /libesphttpd/espfs/espfstest/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Simple and stupid file decompressor for an espfs image. Mostly used as a testbed for espfs.c and 3 | the decompressors: code compiled natively is way easier to debug using gdb et all :) 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include "espfs.h" 16 | 17 | char *espFsData; 18 | 19 | int main(int argc, char **argv) { 20 | int f, out; 21 | int len; 22 | char buff[128]; 23 | EspFsFile *ef; 24 | off_t size; 25 | EspFsInitResult ir; 26 | 27 | if (argc!=3) { 28 | printf("Usage: %s espfs-image file\nExpands file from the espfs-image archive.\n", argv[0]); 29 | exit(0); 30 | } 31 | 32 | f=open(argv[1], O_RDONLY); 33 | if (f<=0) { 34 | perror(argv[1]); 35 | exit(1); 36 | } 37 | size=lseek(f, 0, SEEK_END); 38 | espFsData=mmap(NULL, size, PROT_READ, MAP_SHARED, f, 0); 39 | if (espFsData==MAP_FAILED) { 40 | perror("mmap"); 41 | exit(1); 42 | } 43 | 44 | ir=espFsInit(espFsData); 45 | if (ir != ESPFS_INIT_RESULT_OK) { 46 | printf("Couldn't init espfs filesystem (code %d)\n", ir); 47 | exit(1); 48 | } 49 | 50 | ef=espFsOpen(argv[2]); 51 | if (ef==NULL) { 52 | printf("Couldn't find %s in image.\n", argv[2]); 53 | exit(1); 54 | } 55 | 56 | out=open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644); 57 | if (out<=0) { 58 | perror(argv[2]); 59 | exit(1); 60 | } 61 | 62 | while ((len=espFsRead(ef, buff, 128))!=0) { 63 | write(out, buff, len); 64 | } 65 | espFsClose(ef); 66 | //munmap, close, ... I can't be bothered. 67 | } 68 | -------------------------------------------------------------------------------- /libesphttpd/espfs/heatshrink_config_custom.h: -------------------------------------------------------------------------------- 1 | //Heatshrink config for the decompressor. 2 | #ifndef HEATSHRINK_CONFIG_H 3 | #define HEATSHRINK_CONFIG_H 4 | 5 | /* Should functionality assuming dynamic allocation be used? */ 6 | #define HEATSHRINK_DYNAMIC_ALLOC 1 7 | 8 | #if HEATSHRINK_DYNAMIC_ALLOC 9 | /* Optional replacement of malloc/free */ 10 | #ifdef __ets__ 11 | #define HEATSHRINK_MALLOC(SZ) malloc(SZ) 12 | #define HEATSHRINK_FREE(P, SZ) free(P) 13 | #else 14 | #define HEATSHRINK_MALLOC(SZ) malloc(SZ) 15 | #define HEATSHRINK_FREE(P, SZ) free(P) 16 | #endif 17 | #else 18 | /* Required parameters for static configuration */ 19 | #define HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 32 20 | #define HEATSHRINK_STATIC_WINDOW_BITS 8 21 | #define HEATSHRINK_STATIC_LOOKAHEAD_BITS 4 22 | #endif 23 | 24 | /* Turn on logging for debugging. */ 25 | #define HEATSHRINK_DEBUGGING_LOGS 0 26 | 27 | /* Use indexing for faster compression. (This requires additional space.) */ 28 | #define HEATSHRINK_USE_INDEX 1 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /libesphttpd/espfs/heatshrink_decoder.c: -------------------------------------------------------------------------------- 1 | #include "espfs.h" 2 | #ifdef ESPFS_HEATSHRINK 3 | //Stupid wrapper so we don't have to move c-files around 4 | //Also loads httpd-specific config. 5 | 6 | #ifdef __ets__ 7 | //esp build 8 | 9 | #include 10 | //#include "esp_common.h" 11 | 12 | #define memset(x,y,z) memset(x,y,z) 13 | #define memcpy(x,y,z) memcpy(x,y,z) 14 | #endif 15 | 16 | #include "heatshrink_config_custom.h" 17 | #include "../lib/heatshrink/heatshrink_decoder.c" 18 | 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /libesphttpd/espfs/mkespfsimage/Makefile: -------------------------------------------------------------------------------- 1 | GZIP_COMPRESSION ?= no 2 | USE_HEATSHRINK ?= yes 3 | 4 | CFLAGS=-I../../lib/heatshrink -I../../include -I.. -std=gnu99 5 | ifeq ("$(GZIP_COMPRESSION)","yes") 6 | CFLAGS += -DESPFS_GZIP 7 | endif 8 | 9 | ifeq ("$(USE_HEATSHRINK)","yes") 10 | CFLAGS += -DESPFS_HEATSHRINK 11 | endif 12 | 13 | OBJS=main.o heatshrink_encoder.o 14 | TARGET=mkespfsimage 15 | 16 | $(TARGET): $(OBJS) 17 | ifeq ("$(GZIP_COMPRESSION)","yes") 18 | $(CC) -o $@ $^ -lz 19 | else 20 | $(CC) -o $@ $^ 21 | endif 22 | 23 | clean: 24 | rm -f $(TARGET) $(OBJS) -------------------------------------------------------------------------------- /libesphttpd/espfs/mkespfsimage/heatshrink_encoder.c: -------------------------------------------------------------------------------- 1 | //Stupid wraparound include to make sure object file doesn't end up in heatshrink dir 2 | #ifdef ESPFS_HEATSHRINK 3 | #include "../lib/heatshrink/heatshrink_encoder.c" 4 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/auth.h: -------------------------------------------------------------------------------- 1 | #ifndef AUTH_H 2 | #define AUTH_H 3 | 4 | #include "httpd.h" 5 | 6 | #ifndef HTTP_AUTH_REALM 7 | #define HTTP_AUTH_REALM "Protected" 8 | #endif 9 | 10 | #define HTTPD_AUTH_SINGLE 0 11 | #define HTTPD_AUTH_CALLBACK 1 12 | 13 | #define AUTH_MAX_USER_LEN 32 14 | #define AUTH_MAX_PASS_LEN 32 15 | 16 | //Parameter given to authWhatever functions. This callback returns the usernames/passwords the device 17 | //has. 18 | typedef int (* AuthGetUserPw)(HttpdConnData *connData, int no, char *user, int userLen, char *pass, int passLen); 19 | 20 | int authBasic(HttpdConnData *connData); 21 | 22 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/captdns.h: -------------------------------------------------------------------------------- 1 | #ifndef CAPTDNS_H 2 | #define CAPTDNS_H 3 | void captdnsInit(void); 4 | 5 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/cgiflash.h: -------------------------------------------------------------------------------- 1 | #ifndef CGIFLASH_H 2 | #define CGIFLASH_H 3 | 4 | #include "httpd.h" 5 | 6 | #define CGIFLASH_TYPE_FW 0 7 | #define CGIFLASH_TYPE_ESPFS 1 8 | 9 | typedef struct { 10 | int type; 11 | int fw1Pos; 12 | int fw2Pos; 13 | int fwSize; 14 | } CgiUploadFlashDef; 15 | 16 | int cgiReadFlash(HttpdConnData *connData); 17 | int cgiGetFirmwareNext(HttpdConnData *connData); 18 | int cgiUploadFirmware(HttpdConnData *connData); 19 | int cgiRebootFirmware(HttpdConnData *connData); 20 | 21 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/cgiwifi.h: -------------------------------------------------------------------------------- 1 | #ifndef CGIWIFI_H 2 | #define CGIWIFI_H 3 | 4 | #include "httpd.h" 5 | 6 | int cgiWiFiScan(HttpdConnData *connData); 7 | int tplWlan(HttpdConnData *connData, char *token, void **arg); 8 | int cgiWiFi(HttpdConnData *connData); 9 | int cgiWiFiConnect(HttpdConnData *connData); 10 | int cgiWiFiSetMode(HttpdConnData *connData); 11 | int cgiWiFiConnStatus(HttpdConnData *connData); 12 | 13 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/esp8266.h: -------------------------------------------------------------------------------- 1 | // Combined include file for esp8266 2 | 3 | #include "esp_common.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //#include 10 | //#include 11 | //#include 12 | //#include 13 | //#include 14 | //#include 15 | //#include 16 | //#include "espmissingincludes.h" 17 | 18 | -------------------------------------------------------------------------------- /libesphttpd/include/espfs.h: -------------------------------------------------------------------------------- 1 | #ifndef ESPFS_H 2 | #define ESPFS_H 3 | 4 | // This define is done in Makefile. If you do not use default Makefile, uncomment 5 | // to be able to use Heatshrink-compressed espfs images. 6 | //#define ESPFS_HEATSHRINK 7 | 8 | typedef enum { 9 | ESPFS_INIT_RESULT_OK, 10 | ESPFS_INIT_RESULT_NO_IMAGE, 11 | ESPFS_INIT_RESULT_BAD_ALIGN, 12 | } EspFsInitResult; 13 | 14 | typedef struct EspFsFile EspFsFile; 15 | 16 | EspFsInitResult espFsInit(void *flashAddress); 17 | EspFsFile *espFsOpen(char *fileName); 18 | int espFsFlags(EspFsFile *fh); 19 | int espFsRead(EspFsFile *fh, char *buff, int len); 20 | void espFsClose(EspFsFile *fh); 21 | 22 | 23 | #endif -------------------------------------------------------------------------------- /libesphttpd/include/espmissingincludes.h: -------------------------------------------------------------------------------- 1 | #ifndef ESPMISSINGINCLUDES_H 2 | #define ESPMISSINGINCLUDES_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //Missing function prototypes in include folders. Gcc will warn on these if we don't define 'em anywhere. 10 | //MOST OF THESE ARE GUESSED! but they seem to swork and shut up the compiler. 11 | typedef struct espconn espconn; 12 | 13 | int atoi(const char *nptr); 14 | void ets_install_putc1(void *routine); 15 | void ets_isr_attach(int intr, void *handler, void *arg); 16 | void ets_isr_mask(unsigned intr); 17 | void ets_isr_unmask(unsigned intr); 18 | int ets_memcmp(const void *s1, const void *s2, size_t n); 19 | void *ets_memcpy(void *dest, const void *src, size_t n); 20 | void *ets_memset(void *s, int c, size_t n); 21 | int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); 22 | int ets_str2macaddr(void *, void *); 23 | int ets_strcmp(const char *s1, const char *s2); 24 | char *ets_strcpy(char *dest, const char *src); 25 | size_t ets_strlen(const char *s); 26 | int ets_strncmp(const char *s1, const char *s2, int len); 27 | char *ets_strncpy(char *dest, const char *src, size_t n); 28 | char *ets_strstr(const char *haystack, const char *needle); 29 | void ets_timer_arm_new(ETSTimer *a, int b, int c, int isMstimer); 30 | void ets_timer_disarm(ETSTimer *a); 31 | void ets_timer_setfn(ETSTimer *t, ETSTimerFunc *fn, void *parg); 32 | void ets_update_cpu_frequency(int freqmhz); 33 | int os_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); 34 | int os_snprintf(char *str, size_t size, const char *format, ...) __attribute__ ((format (printf, 3, 4))); 35 | int os_printf_plus(const char *format, ...) __attribute__ ((format (printf, 1, 2))); 36 | void pvPortFree(void *ptr); 37 | void *pvPortMalloc(size_t xWantedSize); 38 | void *pvPortZalloc(size_t); 39 | void uart_div_modify(int no, unsigned int freq); 40 | void vPortFree(void *ptr); 41 | void *vPortMalloc(size_t xWantedSize); 42 | uint8 wifi_get_opmode(void); 43 | uint32 system_get_time(); 44 | int rand(void); 45 | void ets_bzero(void *s, size_t n); 46 | void ets_delay_us(int ms); 47 | 48 | 49 | //Standard PIN_FUNC_SELECT gives a warning. Replace by a non-warning one. 50 | #ifdef PIN_FUNC_SELECT 51 | #undef PIN_FUNC_SELECT 52 | #define PIN_FUNC_SELECT(PIN_NAME, FUNC) do { \ 53 | WRITE_PERI_REG(PIN_NAME, \ 54 | (READ_PERI_REG(PIN_NAME) \ 55 | & (~(PERIPHS_IO_MUX_FUNC< 2 | All rights reserved. 3 | 4 | Permission to use, copy, modify, and/or distribute this software for any 5 | purpose with or without fee is hereby granted, provided that the above 6 | copyright notice and this permission notice appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = heatshrink 2 | #OPTIMIZE = -O0 3 | #OPTIMIZE = -Os 4 | OPTIMIZE = -O3 5 | WARN = -Wall -Wextra -pedantic #-Werror 6 | CFLAGS += -std=c99 -g ${WARN} ${OPTIMIZE} 7 | CFLAGS += -Wmissing-prototypes 8 | CFLAGS += -Wstrict-prototypes 9 | CFLAGS += -Wmissing-declarations 10 | 11 | # If libtheft is available, build additional property-based tests. 12 | # Uncomment these to use it in test_heatshrink_dynamic. 13 | #CFLAGS += -DHEATSHRINK_HAS_THEFT 14 | #LDFLAGS += -ltheft 15 | 16 | all: 17 | @echo "For tests, make test_heatshrink_dynamic (default) or change the" 18 | @echo "config.h to disable static memory and build test_heatshrink_static." 19 | @echo "For the standalone command-line tool, make heatshrink." 20 | 21 | ${PROJECT}: heatshrink.c 22 | 23 | OBJS= heatshrink_encoder.o \ 24 | heatshrink_decoder.o \ 25 | 26 | heatshrink: ${OBJS} 27 | test_heatshrink_dynamic: ${OBJS} test_heatshrink_dynamic_theft.o 28 | test_heatshrink_static: ${OBJS} 29 | 30 | *.o: Makefile heatshrink_config.h 31 | 32 | heatshrink_decoder.o: heatshrink_decoder.h heatshrink_common.h 33 | heatshrink_encoder.o: heatshrink_encoder.h heatshrink_common.h 34 | 35 | tags: TAGS 36 | 37 | TAGS: 38 | etags *.[ch] 39 | 40 | diagrams: dec_sm.png enc_sm.png 41 | 42 | dec_sm.png: dec_sm.dot 43 | dot -o $@ -Tpng $< 44 | 45 | enc_sm.png: enc_sm.dot 46 | dot -o $@ -Tpng $< 47 | 48 | clean: 49 | rm -f ${PROJECT} test_heatshrink_{dynamic,static} *.o *.core {dec,enc}_sm.png TAGS 50 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/README.md: -------------------------------------------------------------------------------- 1 | # heatshrink 2 | 3 | A data compression/decompression library for embedded/real-time systems. 4 | 5 | ## Key Features: 6 | 7 | - **Low memory usage (as low as 50 bytes)** 8 | It is useful for some cases with less than 50 bytes, and useful 9 | for many general cases with < 300 bytes. 10 | - **Incremental, bounded CPU use** 11 | You can chew on input data in arbitrarily tiny bites. 12 | This is a useful property in hard real-time environments. 13 | - **Can use either static or dynamic memory allocation** 14 | The library doesn't impose any constraints on memory management. 15 | - **ISC license** 16 | You can use it freely, even for commercial purposes. 17 | 18 | ## Getting Started: 19 | 20 | There is a standalone command-line program, `heatshrink`, but the 21 | encoder and decoder can also be used as libraries, independent of each 22 | other. To do so, copy `heatshrink_common.h`, `heatshrink_config.h`, and 23 | either `heatshrink_encoder.c` or `heatshrink_decoder.c` (and their 24 | respective header) into your project. 25 | 26 | Dynamic allocation is used by default, but in an embedded context, you 27 | probably want to statically allocate the encoder/decoder. Set 28 | `HEATSHRINK_DYNAMIC_ALLOC` to 0 in `heatshrink_config.h`. 29 | 30 | ## More Information and Benchmarks: 31 | 32 | heatshrink is based on [LZSS], since it's particularly suitable for 33 | compression in small amounts of memory. It can use an optional, small 34 | [index] to make compression significantly faster, but otherwise can run 35 | in under 100 bytes of memory. The index currently adds 2^(window size+1) 36 | bytes to memory usage for compression, and temporarily allocates 512 37 | bytes on the stack during index construction. 38 | 39 | For more information, see the [blog post] for an overview, and the 40 | `heatshrink_encoder.h` / `heatshrink_decoder.h` header files for API 41 | documentation. 42 | 43 | [blog post]: http://spin.atomicobject.com/2013/03/14/heatshrink-embedded-data-compression/ 44 | [index]: http://spin.atomicobject.com/2014/01/13/lightweight-indexing-for-embedded-systems/ 45 | [LZSS]: http://en.wikipedia.org/wiki/Lempel-Ziv-Storer-Szymanski 46 | 47 | ## Build Status 48 | 49 | [![Build Status](https://travis-ci.org/atomicobject/heatshrink.png)](http://travis-ci.org/atomicobject/heatshrink) 50 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/dec_sm.dot: -------------------------------------------------------------------------------- 1 | digraph { 2 | graph [label="Decoder state machine", labelloc="t"] 3 | Start [style="invis", shape="point"] 4 | empty 5 | input_available 6 | yield_literal 7 | backref_index_msb 8 | backref_index_lsb 9 | backref_count_msb 10 | backref_count_lsb 11 | yield_backref 12 | check_for_more_input 13 | done [peripheries=2] 14 | 15 | empty->input_available [label="sink()", color="blue", weight=10] 16 | Start->empty 17 | 18 | input_available->yield_literal [label="pop 1-bit"] 19 | input_available->backref_index_msb [label="pop 0-bit", weight=10] 20 | input_available->backref_index_lsb [label="pop 0-bit, index <8 bits", weight=10] 21 | 22 | yield_literal->yield_literal [label="sink()", color="blue"] 23 | yield_literal->yield_literal [label="poll()", color="red"] 24 | yield_literal->check_for_more_input [label="poll(), done", color="red"] 25 | 26 | backref_index_msb->backref_index_msb [label="sink()", color="blue"] 27 | backref_index_msb->backref_index_lsb [label="pop index, upper bits", weight=10] 28 | backref_index_msb->done [label="finish()", color="blue"] 29 | 30 | backref_index_lsb->backref_index_lsb [label="sink()", color="blue"] 31 | backref_index_lsb->backref_count_msb [label="pop index, lower bits", weight=10] 32 | backref_index_lsb->backref_count_lsb [label="pop index, count <=8 bits", weight=10] 33 | backref_index_lsb->done [label="finish()", color="blue"] 34 | 35 | backref_count_msb->backref_count_msb [label="sink()", color="blue"] 36 | backref_count_msb->backref_count_lsb [label="pop count, upper bits", weight=10] 37 | backref_count_msb->done [label="finish()", color="blue"] 38 | 39 | backref_count_lsb->backref_count_lsb [label="sink()", color="blue"] 40 | backref_count_lsb->yield_backref [label="pop count, lower bits", weight=10] 41 | backref_count_lsb->done [label="finish()", color="blue"] 42 | 43 | yield_backref->yield_backref [label="sink()", color="blue"] 44 | yield_backref->yield_backref [label="poll()", color="red"] 45 | yield_backref->check_for_more_input [label="poll(), done", 46 | color="red", weight=10] 47 | 48 | check_for_more_input->empty [label="no"] 49 | check_for_more_input->input_available [label="yes"] 50 | 51 | empty->done [label="finish()", color="blue"] 52 | } 53 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/enc_sm.dot: -------------------------------------------------------------------------------- 1 | digraph { 2 | graph [label="Encoder state machine", labelloc="t"] 3 | start [style="invis", shape="point"] 4 | not_full 5 | filled 6 | search 7 | yield_tag_bit 8 | yield_literal 9 | yield_br_length 10 | yield_br_index 11 | save_backlog 12 | flush_bits 13 | done [peripheries=2] 14 | 15 | start->not_full [label="start"] 16 | 17 | not_full->not_full [label="sink(), not full", color="blue"] 18 | not_full->filled [label="sink(), buffer is full", color="blue"] 19 | not_full->filled [label="finish(), set is_finished", color="blue"] 20 | 21 | filled->search [label="indexing (if any)"] 22 | 23 | search->search [label="step"] 24 | search->yield_tag_bit [label="literal"] 25 | search->yield_tag_bit [label="match found"] 26 | search->save_backlog [label="input exhausted"] 27 | 28 | yield_tag_bit->yield_tag_bit [label="poll(), full buf", color="red"] 29 | yield_tag_bit->yield_literal [label="poll(), literal", color="red"] 30 | yield_tag_bit->yield_br_index [label="poll(), no literal", color="red"] 31 | yield_tag_bit->flush_bits [label="finishing, no literal"] 32 | 33 | yield_literal->yield_literal [label="poll(), full buf", color="red"] 34 | yield_literal->search [label="poll(), no match", color="red"] 35 | yield_literal->yield_tag_bit [label="poll(), match", color="red"] 36 | yield_literal->flush_bits [label="poll(), final literal", color="red"] 37 | 38 | yield_br_index->yield_br_index [label="poll(), full buf", color="red"] 39 | yield_br_index->yield_br_length [label="poll()", color="red"] 40 | 41 | yield_br_length->yield_br_length [label="poll(), full buf", color="red"] 42 | yield_br_length->search [label="done"] 43 | 44 | save_backlog->flush_bits [label="finishing, no literal"] 45 | save_backlog->yield_tag_bit [label="finishing, literal"] 46 | save_backlog->not_full [label="expect more input"] 47 | 48 | flush_bits->flush_bits [label="poll(), full buf", color="red"] 49 | flush_bits->done [label="poll(), flushed", color="red"] 50 | flush_bits->done [label="no more output"] 51 | } 52 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/heatshrink_common.h: -------------------------------------------------------------------------------- 1 | #ifndef HEATSHRINK_H 2 | #define HEATSHRINK_H 3 | 4 | #define HEATSHRINK_AUTHOR "Scott Vokes " 5 | #define HEATSHRINK_URL "https://github.com/atomicobject/heatshrink" 6 | 7 | /* Version 0.3.1 */ 8 | #define HEATSHRINK_VERSION_MAJOR 0 9 | #define HEATSHRINK_VERSION_MINOR 3 10 | #define HEATSHRINK_VERSION_PATCH 1 11 | 12 | #define HEATSHRINK_MIN_WINDOW_BITS 4 13 | #define HEATSHRINK_MAX_WINDOW_BITS 15 14 | 15 | #define HEATSHRINK_MIN_LOOKAHEAD_BITS 2 16 | 17 | #define HEATSHRINK_LITERAL_MARKER 0x01 18 | #define HEATSHRINK_BACKREF_MARKER 0x00 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/heatshrink_config.h: -------------------------------------------------------------------------------- 1 | #ifndef HEATSHRINK_CONFIG_H 2 | #define HEATSHRINK_CONFIG_H 3 | 4 | /* Should functionality assuming dynamic allocation be used? */ 5 | #define HEATSHRINK_DYNAMIC_ALLOC 1 6 | 7 | #if HEATSHRINK_DYNAMIC_ALLOC 8 | /* Optional replacement of malloc/free */ 9 | #define HEATSHRINK_MALLOC(SZ) malloc(SZ) 10 | #define HEATSHRINK_FREE(P, SZ) free(P) 11 | #else 12 | /* Required parameters for static configuration */ 13 | #define HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 32 14 | #define HEATSHRINK_STATIC_WINDOW_BITS 8 15 | #define HEATSHRINK_STATIC_LOOKAHEAD_BITS 4 16 | #endif 17 | 18 | /* Turn on logging for debugging. */ 19 | #define HEATSHRINK_DEBUGGING_LOGS 0 20 | 21 | /* Use indexing for faster compression. (This requires additional space.) */ 22 | #define HEATSHRINK_USE_INDEX 1 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/heatshrink_decoder.h: -------------------------------------------------------------------------------- 1 | #ifndef HEATSHRINK_DECODER_H 2 | #define HEATSHRINK_DECODER_H 3 | 4 | #include 5 | #include 6 | #include "heatshrink_common.h" 7 | #include "heatshrink_config.h" 8 | 9 | typedef enum { 10 | HSDR_SINK_OK, /* data sunk, ready to poll */ 11 | HSDR_SINK_FULL, /* out of space in internal buffer */ 12 | HSDR_SINK_ERROR_NULL=-1, /* NULL argument */ 13 | } HSD_sink_res; 14 | 15 | typedef enum { 16 | HSDR_POLL_EMPTY, /* input exhausted */ 17 | HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */ 18 | HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */ 19 | HSDR_POLL_ERROR_UNKNOWN=-2, 20 | } HSD_poll_res; 21 | 22 | typedef enum { 23 | HSDR_FINISH_DONE, /* output is done */ 24 | HSDR_FINISH_MORE, /* more output remains */ 25 | HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */ 26 | } HSD_finish_res; 27 | 28 | #if HEATSHRINK_DYNAMIC_ALLOC 29 | #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \ 30 | ((BUF)->input_buffer_size) 31 | #define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \ 32 | ((BUF)->window_sz2) 33 | #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \ 34 | ((BUF)->lookahead_sz2) 35 | #else 36 | #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \ 37 | HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 38 | #define HEATSHRINK_DECODER_WINDOW_BITS(_) \ 39 | (HEATSHRINK_STATIC_WINDOW_BITS) 40 | #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \ 41 | (HEATSHRINK_STATIC_LOOKAHEAD_BITS) 42 | #endif 43 | 44 | typedef struct { 45 | uint16_t input_size; /* bytes in input buffer */ 46 | uint16_t input_index; /* offset to next unprocessed input byte */ 47 | uint16_t output_count; /* how many bytes to output */ 48 | uint16_t output_index; /* index for bytes to output */ 49 | uint16_t head_index; /* head of window buffer */ 50 | uint16_t bit_accumulator; 51 | uint8_t state; /* current state machine node */ 52 | uint8_t current_byte; /* current byte of input */ 53 | uint8_t bit_index; /* current bit index */ 54 | 55 | #if HEATSHRINK_DYNAMIC_ALLOC 56 | /* Fields that are only used if dynamically allocated. */ 57 | uint8_t window_sz2; /* window buffer bits */ 58 | uint8_t lookahead_sz2; /* lookahead bits */ 59 | uint16_t input_buffer_size; /* input buffer size */ 60 | 61 | /* Input buffer, then expansion window buffer */ 62 | uint8_t buffers[]; 63 | #else 64 | /* Input buffer, then expansion window buffer */ 65 | uint8_t buffers[(1 << HEATSHRINK_DECODER_WINDOW_BITS(_)) 66 | + HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)]; 67 | #endif 68 | } heatshrink_decoder; 69 | 70 | #if HEATSHRINK_DYNAMIC_ALLOC 71 | /* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes, 72 | * an expansion buffer size of 2^WINDOW_SZ2, and a lookahead 73 | * size of 2^lookahead_sz2. (The window buffer and lookahead sizes 74 | * must match the settings used when the data was compressed.) 75 | * Returns NULL on error. */ 76 | heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size, 77 | uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2); 78 | 79 | /* Free a decoder. */ 80 | void heatshrink_decoder_free(heatshrink_decoder *hsd); 81 | #endif 82 | 83 | /* Reset a decoder. */ 84 | void heatshrink_decoder_reset(heatshrink_decoder *hsd); 85 | 86 | /* Sink at most SIZE bytes from IN_BUF into the decoder. *INPUT_SIZE is set to 87 | * indicate how many bytes were actually sunk (in case a buffer was filled). */ 88 | HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, 89 | uint8_t *in_buf, size_t size, size_t *input_size); 90 | 91 | /* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into 92 | * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */ 93 | HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, 94 | uint8_t *out_buf, size_t out_buf_size, size_t *output_size); 95 | 96 | /* Notify the dencoder that the input stream is finished. 97 | * If the return value is HSDR_FINISH_MORE, there is still more output, so 98 | * call heatshrink_decoder_poll and repeat. */ 99 | HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd); 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/heatshrink_encoder.h: -------------------------------------------------------------------------------- 1 | #ifndef HEATSHRINK_ENCODER_H 2 | #define HEATSHRINK_ENCODER_H 3 | 4 | #include 5 | #include 6 | #include "heatshrink_common.h" 7 | #include "heatshrink_config.h" 8 | 9 | typedef enum { 10 | HSER_SINK_OK, /* data sunk into input buffer */ 11 | HSER_SINK_ERROR_NULL=-1, /* NULL argument */ 12 | HSER_SINK_ERROR_MISUSE=-2, /* API misuse */ 13 | } HSE_sink_res; 14 | 15 | typedef enum { 16 | HSER_POLL_EMPTY, /* input exhausted */ 17 | HSER_POLL_MORE, /* poll again for more output */ 18 | HSER_POLL_ERROR_NULL=-1, /* NULL argument */ 19 | HSER_POLL_ERROR_MISUSE=-2, /* API misuse */ 20 | } HSE_poll_res; 21 | 22 | typedef enum { 23 | HSER_FINISH_DONE, /* encoding is complete */ 24 | HSER_FINISH_MORE, /* more output remaining; use poll */ 25 | HSER_FINISH_ERROR_NULL=-1, /* NULL argument */ 26 | } HSE_finish_res; 27 | 28 | #if HEATSHRINK_DYNAMIC_ALLOC 29 | #define HEATSHRINK_ENCODER_WINDOW_BITS(HSE) \ 30 | ((HSE)->window_sz2) 31 | #define HEATSHRINK_ENCODER_LOOKAHEAD_BITS(HSE) \ 32 | ((HSE)->lookahead_sz2) 33 | #define HEATSHRINK_ENCODER_INDEX(HSE) \ 34 | ((HSE)->search_index) 35 | struct hs_index { 36 | uint16_t size; 37 | int16_t index[]; 38 | }; 39 | #else 40 | #define HEATSHRINK_ENCODER_WINDOW_BITS(_) \ 41 | (HEATSHRINK_STATIC_WINDOW_BITS) 42 | #define HEATSHRINK_ENCODER_LOOKAHEAD_BITS(_) \ 43 | (HEATSHRINK_STATIC_LOOKAHEAD_BITS) 44 | #define HEATSHRINK_ENCODER_INDEX(HSE) \ 45 | (&(HSE)->search_index) 46 | struct hs_index { 47 | uint16_t size; 48 | int16_t index[2 << HEATSHRINK_STATIC_WINDOW_BITS]; 49 | }; 50 | #endif 51 | 52 | typedef struct { 53 | uint16_t input_size; /* bytes in input buffer */ 54 | uint16_t match_scan_index; 55 | uint16_t match_length; 56 | uint16_t match_pos; 57 | uint16_t outgoing_bits; /* enqueued outgoing bits */ 58 | uint8_t outgoing_bits_count; 59 | uint8_t flags; 60 | uint8_t state; /* current state machine node */ 61 | uint8_t current_byte; /* current byte of output */ 62 | uint8_t bit_index; /* current bit index */ 63 | #if HEATSHRINK_DYNAMIC_ALLOC 64 | uint8_t window_sz2; /* 2^n size of window */ 65 | uint8_t lookahead_sz2; /* 2^n size of lookahead */ 66 | #if HEATSHRINK_USE_INDEX 67 | struct hs_index *search_index; 68 | #endif 69 | /* input buffer and / sliding window for expansion */ 70 | uint8_t buffer[]; 71 | #else 72 | #if HEATSHRINK_USE_INDEX 73 | struct hs_index search_index; 74 | #endif 75 | /* input buffer and / sliding window for expansion */ 76 | uint8_t buffer[2 << HEATSHRINK_ENCODER_WINDOW_BITS(_)]; 77 | #endif 78 | } heatshrink_encoder; 79 | 80 | #if HEATSHRINK_DYNAMIC_ALLOC 81 | /* Allocate a new encoder struct and its buffers. 82 | * Returns NULL on error. */ 83 | heatshrink_encoder *heatshrink_encoder_alloc(uint8_t window_sz2, 84 | uint8_t lookahead_sz2); 85 | 86 | /* Free an encoder. */ 87 | void heatshrink_encoder_free(heatshrink_encoder *hse); 88 | #endif 89 | 90 | /* Reset an encoder. */ 91 | void heatshrink_encoder_reset(heatshrink_encoder *hse); 92 | 93 | /* Sink up to SIZE bytes from IN_BUF into the encoder. 94 | * INPUT_SIZE is set to the number of bytes actually sunk (in case a 95 | * buffer was filled.). */ 96 | HSE_sink_res heatshrink_encoder_sink(heatshrink_encoder *hse, 97 | uint8_t *in_buf, size_t size, size_t *input_size); 98 | 99 | /* Poll for output from the encoder, copying at most OUT_BUF_SIZE bytes into 100 | * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */ 101 | HSE_poll_res heatshrink_encoder_poll(heatshrink_encoder *hse, 102 | uint8_t *out_buf, size_t out_buf_size, size_t *output_size); 103 | 104 | /* Notify the encoder that the input stream is finished. 105 | * If the return value is HSER_FINISH_MORE, there is still more output, so 106 | * call heatshrink_encoder_poll and repeat. */ 107 | HSE_finish_res heatshrink_encoder_finish(heatshrink_encoder *hse); 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /libesphttpd/lib/heatshrink/test_heatshrink_static.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "heatshrink_encoder.h" 5 | #include "heatshrink_decoder.h" 6 | #include "greatest.h" 7 | 8 | #if HEATSHRINK_DYNAMIC_ALLOC 9 | #error HEATSHRINK_DYNAMIC_ALLOC must be false for static allocation test suite. 10 | #endif 11 | 12 | SUITE(integration); 13 | 14 | /* The majority of the tests are in test_heatshrink_dynamic, because that allows 15 | * instantiating encoders/decoders with different settings at run-time. */ 16 | 17 | static heatshrink_encoder hse; 18 | static heatshrink_decoder hsd; 19 | 20 | static void fill_with_pseudorandom_letters(uint8_t *buf, uint16_t size, uint32_t seed) { 21 | uint64_t rn = 9223372036854775783; /* prime under 2^64 */ 22 | for (int i=0; i 1) { 50 | printf("\n^^ COMPRESSING\n"); 51 | dump_buf("input", input, input_size); 52 | } 53 | 54 | uint32_t sunk = 0; 55 | uint32_t polled = 0; 56 | while (sunk < input_size) { 57 | ASSERT(heatshrink_encoder_sink(&hse, &input[sunk], input_size - sunk, &count) >= 0); 58 | sunk += count; 59 | if (log_lvl > 1) printf("^^ sunk %zd\n", count); 60 | if (sunk == input_size) { 61 | ASSERT_EQ(HSER_FINISH_MORE, heatshrink_encoder_finish(&hse)); 62 | } 63 | 64 | HSE_poll_res pres; 65 | do { /* "turn the crank" */ 66 | pres = heatshrink_encoder_poll(&hse, &comp[polled], comp_sz - polled, &count); 67 | ASSERT(pres >= 0); 68 | polled += count; 69 | if (log_lvl > 1) printf("^^ polled %zd\n", count); 70 | } while (pres == HSER_POLL_MORE); 71 | ASSERT_EQ(HSER_POLL_EMPTY, pres); 72 | if (polled >= comp_sz) FAILm("compression should never expand that much"); 73 | if (sunk == input_size) { 74 | ASSERT_EQ(HSER_FINISH_DONE, heatshrink_encoder_finish(&hse)); 75 | } 76 | } 77 | if (log_lvl > 0) printf("in: %u compressed: %u ", input_size, polled); 78 | uint32_t compressed_size = polled; 79 | sunk = 0; 80 | polled = 0; 81 | 82 | if (log_lvl > 1) { 83 | printf("\n^^ DECOMPRESSING\n"); 84 | dump_buf("comp", comp, compressed_size); 85 | } 86 | while (sunk < compressed_size) { 87 | ASSERT(heatshrink_decoder_sink(&hsd, &comp[sunk], compressed_size - sunk, &count) >= 0); 88 | sunk += count; 89 | if (log_lvl > 1) printf("^^ sunk %zd\n", count); 90 | if (sunk == compressed_size) { 91 | ASSERT_EQ(HSDR_FINISH_MORE, heatshrink_decoder_finish(&hsd)); 92 | } 93 | 94 | HSD_poll_res pres; 95 | do { 96 | pres = heatshrink_decoder_poll(&hsd, &decomp[polled], 97 | decomp_sz - polled, &count); 98 | ASSERT(pres >= 0); 99 | polled += count; 100 | if (log_lvl > 1) printf("^^ polled %zd\n", count); 101 | } while (pres == HSDR_POLL_MORE); 102 | ASSERT_EQ(HSDR_POLL_EMPTY, pres); 103 | if (sunk == compressed_size) { 104 | HSD_finish_res fres = heatshrink_decoder_finish(&hsd); 105 | ASSERT_EQ(HSDR_FINISH_DONE, fres); 106 | } 107 | 108 | if (polled > input_size) { 109 | FAILm("Decompressed data is larger than original input"); 110 | } 111 | } 112 | if (log_lvl > 0) printf("decompressed: %u\n", polled); 113 | if (polled != input_size) { 114 | FAILm("Decompressed length does not match original input length"); 115 | } 116 | 117 | if (log_lvl > 1) dump_buf("decomp", decomp, polled); 118 | for (size_t i=0; i out[%zd] == 0x%02x ('%c')\n", 124 | j, input[j], isprint(input[j]) ? input[j] : '.', 125 | j, decomp[j], isprint(decomp[j]) ? decomp[j] : '.'); 126 | } 127 | } 128 | } 129 | ASSERT_EQ(input[i], decomp[i]); 130 | } 131 | free(comp); 132 | free(decomp); 133 | PASS(); 134 | } 135 | 136 | TEST pseudorandom_data_should_match(uint32_t size, uint32_t seed) { 137 | uint8_t input[size]; 138 | fill_with_pseudorandom_letters(input, size, seed); 139 | return compress_and_expand_and_check(input, size, 0); 140 | } 141 | 142 | SUITE(integration) { 143 | #if __STDC_VERSION__ >= 19901L 144 | for (uint32_t size=1; size < 64*1024; size <<= 1) { 145 | if (GREATEST_IS_VERBOSE()) printf(" -- size %u\n", size); 146 | for (uint32_t seed=1; seed<=100; seed++) { 147 | if (GREATEST_IS_VERBOSE()) printf(" -- seed %u\n", seed); 148 | RUN_TESTp(pseudorandom_data_should_match, size, seed); 149 | } 150 | } 151 | #endif 152 | } 153 | 154 | /* Add all the definitions that need to be in the test runner's main file. */ 155 | GREATEST_MAIN_DEFS(); 156 | 157 | int main(int argc, char **argv) { 158 | GREATEST_MAIN_BEGIN(); /* command-line arguments, initialization. */ 159 | printf("INPUT_BUFFER_SIZE: %u\n", HEATSHRINK_STATIC_INPUT_BUFFER_SIZE); 160 | printf("WINDOW_BITS: %u\n", HEATSHRINK_STATIC_WINDOW_BITS); 161 | printf("LOOKAHEAD_BITS: %u\n", HEATSHRINK_STATIC_LOOKAHEAD_BITS); 162 | 163 | printf("sizeof(heatshrink_encoder): %zd\n", sizeof(heatshrink_encoder)); 164 | printf("sizeof(heatshrink_decoder): %zd\n", sizeof(heatshrink_decoder)); 165 | RUN_SUITE(integration); 166 | GREATEST_MAIN_END(); /* display results */ 167 | } 168 | -------------------------------------------------------------------------------- /libesphttpd/util/cgiflash.c: -------------------------------------------------------------------------------- 1 | /* 2 | Some flash handling cgi routines. Used for reading the existing flash and updating the ESPFS image. 3 | */ 4 | 5 | /* 6 | * ---------------------------------------------------------------------------- 7 | * "THE BEER-WARE LICENSE" (Revision 42): 8 | * Jeroen Domburg wrote this file. As long as you retain 9 | * this notice you can do whatever you want with this stuff. If we meet some day, 10 | * and you think this stuff is worth it, you can buy me a beer in return. 11 | * ---------------------------------------------------------------------------- 12 | */ 13 | #include 14 | #include "cgiflash.h" 15 | #include "espfs.h" 16 | 17 | #include "lwip/sockets.h" 18 | 19 | #include "upgrade.h" 20 | 21 | #define ESPFS_SIZE 0 22 | #define FIRMWARE_SIZE 0 23 | 24 | 25 | // Check that the header of the firmware blob looks like actual firmware... 26 | static char* checkBinHeader(void *buf) { 27 | uint8_t *cd = (uint8_t *)buf; 28 | if (cd[0] != 0xEA) return "IROM magic missing"; 29 | if (cd[1] != 4 || cd[2] > 3 || cd[3] > 0x40) return "bad flash header"; 30 | if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr"; 31 | if (((uint32_t *)buf)[2] != 0) return "Invalid start offset"; 32 | return NULL; 33 | } 34 | 35 | static char* checkEspfsHeader(void *buf) { 36 | if (memcmp(buf, "ESfs", 4)!=0) return "Bad ESPfs header"; 37 | return NULL; 38 | } 39 | 40 | 41 | // Cgi to query which firmware needs to be uploaded next 42 | int cgiGetFirmwareNext(HttpdConnData *connData) { 43 | if (connData->conn==NULL) { 44 | //Connection aborted. Clean up. 45 | return HTTPD_CGI_DONE; 46 | } 47 | uint8 id = system_upgrade_userbin_check(); 48 | httpdStartResponse(connData, 200); 49 | httpdHeader(connData, "Content-Type", "text/plain"); 50 | httpdHeader(connData, "Content-Length", "9"); 51 | httpdEndHeaders(connData); 52 | char *next = id == 1 ? "user1.bin" : "user2.bin"; 53 | httpdSend(connData, next, -1); 54 | printf("Next firmware: %s (got %d)\n", next, id); 55 | return HTTPD_CGI_DONE; 56 | } 57 | 58 | 59 | //Cgi that reads the SPI flash. Assumes 512KByte flash. 60 | //ToDo: Figure out real flash size somehow? 61 | int cgiReadFlash(HttpdConnData *connData) { 62 | int *pos=(int *)&connData->cgiData; 63 | if (connData->conn==NULL) { 64 | //Connection aborted. Clean up. 65 | return HTTPD_CGI_DONE; 66 | } 67 | 68 | if (*pos==0) { 69 | printf("Start flash download.\n"); 70 | httpdStartResponse(connData, 200); 71 | httpdHeader(connData, "Content-Type", "application/bin"); 72 | httpdEndHeaders(connData); 73 | *pos=0x40200000; 74 | return HTTPD_CGI_MORE; 75 | } 76 | //Send 1K of flash per call. We will get called again if we haven't sent 512K yet. 77 | write(connData->conn->sockfd, (uint8 *)(*pos), 1024); 78 | *pos+=1024; 79 | if (*pos>=0x40200000+(512*1024)) return HTTPD_CGI_DONE; else return HTTPD_CGI_MORE; 80 | } 81 | 82 | 83 | //Cgi that allows the firmware to be replaced via http POST 84 | int cgiUploadFirmware(HttpdConnData *connData) { 85 | CgiUploadFlashDef *def=(CgiUploadFlashDef*)connData->cgiArg; 86 | uint32_t address; 87 | if (connData->conn==NULL) { 88 | //Connection aborted. Clean up. 89 | return HTTPD_CGI_DONE; 90 | } 91 | 92 | int offset = connData->post->received - connData->post->buffLen; 93 | if (offset == 0) { 94 | connData->cgiPrivData = NULL; 95 | } else if (connData->cgiPrivData != NULL) { 96 | // we have an error condition, do nothing 97 | return HTTPD_CGI_DONE; 98 | } 99 | 100 | // assume no error yet... 101 | char *err = NULL; 102 | int code = 400; 103 | 104 | if (connData->post==NULL) err="No POST request."; 105 | if (def==NULL) err="Flash def = NULL ?"; 106 | 107 | // check overall size 108 | os_printf("Max img sz 0x%X, post len 0x%X\n", def->fwSize, connData->post->len); 109 | if (err==NULL && connData->post->len > def->fwSize) err = "Firmware image too large"; 110 | 111 | // check that data starts with an appropriate header 112 | if (err == NULL && offset == 0 && def->type==CGIFLASH_TYPE_FW) err = checkBinHeader(connData->post->buff); 113 | if (err == NULL && offset == 0 && def->type==CGIFLASH_TYPE_ESPFS) err = checkEspfsHeader(connData->post->buff); 114 | 115 | // make sure we're buffering in 1024 byte chunks 116 | if (err == NULL && offset % 1024 != 0) { 117 | err = "Buffering problem"; 118 | code = 500; 119 | } 120 | 121 | // return an error if there is one 122 | if (err != NULL) { 123 | os_printf("Error %d: %s\n", code, err); 124 | httpdStartResponse(connData, code); 125 | httpdHeader(connData, "Content-Type", "text/plain"); 126 | httpdEndHeaders(connData); 127 | httpdSend(connData, "Firmware image error:\r\n", -1); 128 | httpdSend(connData, err, -1); 129 | httpdSend(connData, "\r\n", -1); 130 | connData->cgiPrivData = (void *)1; 131 | return HTTPD_CGI_DONE; 132 | } 133 | 134 | // let's see which partition we need to flash and what flash address that puts us at 135 | int id=system_upgrade_userbin_check(); 136 | if (id==1) address=def->fw1Pos; else address=def->fw2Pos; 137 | address += offset; 138 | // erase next flash block if necessary 139 | if (address % SPI_FLASH_SEC_SIZE == 0){ 140 | // We need to erase this block 141 | printf("Erasing flash at 0x%05x (id=%d)\n", (unsigned int)address, 2-id); 142 | spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE); 143 | } 144 | 145 | // Write the data 146 | printf("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, (unsigned int)address, 147 | connData->post->received, connData->post->len); 148 | spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen); 149 | 150 | if (connData->post->received == connData->post->len){ 151 | httpdStartResponse(connData, 200); 152 | httpdEndHeaders(connData); 153 | return HTTPD_CGI_DONE; 154 | } else { 155 | return HTTPD_CGI_MORE; 156 | } 157 | } 158 | 159 | //static ETSTimer flash_reboot_timer; 160 | 161 | // Handle request to reboot into the new firmware 162 | int cgiRebootFirmware(HttpdConnData *connData) { 163 | if (connData->conn==NULL) { 164 | //Connection aborted. Clean up. 165 | return HTTPD_CGI_DONE; 166 | } 167 | 168 | // TODO: sanity-check that the 'next' partition actually contains something that looks like 169 | // valid firmware 170 | 171 | // This should probably be forked into a separate task that waits a second to let the 172 | // current HTTP request finish... 173 | system_upgrade_flag_set(UPGRADE_FLAG_FINISH); 174 | system_upgrade_reboot();//Jerremy, wait a minit ? 175 | httpdStartResponse(connData, 200); 176 | httpdEndHeaders(connData); 177 | return HTTPD_CGI_DONE; 178 | } 179 | 180 | -------------------------------------------------------------------------------- /libesphttpd/webpages.espfs.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-xtensa-le") 2 | 3 | 4 | SECTIONS 5 | { 6 | .irom0.literal : ALIGN(4) SUBALIGN(4) { 7 | webpages_espfs_start = .; 8 | *(*) 9 | webpages_espfs_end = .; 10 | webpages_espfs_size = webpages_espfs_end - webpages_espfs_start; 11 | } 12 | } -------------------------------------------------------------------------------- /upgrade/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ############################################################# 3 | # Required variables for each makefile 4 | # Discard this section from all parent makefiles 5 | # Expected variables (with automatic defaults): 6 | # CSRCS (all "C" files in the dir) 7 | # SUBDIRS (all subdirs with a Makefile) 8 | # GEN_LIBS - list of libs to be generated () 9 | # GEN_IMAGES - list of images to be generated () 10 | # COMPONENTS_xxx - a list of libs/objs in the form 11 | # subdir/lib to be extracted and rolled up into 12 | # a generated lib/image xxx.a () 13 | # 14 | ifndef PDIR 15 | 16 | GEN_LIBS = libupgrade.a 17 | 18 | endif 19 | 20 | 21 | ############################################################# 22 | # Configuration i.e. compile options etc. 23 | # Target specific stuff (defines etc.) goes in here! 24 | # Generally values applying to a tree are captured in the 25 | # makefile at its root level - these are then overridden 26 | # for a subtree within the makefile rooted therein 27 | # 28 | #DEFINES += 29 | 30 | ############################################################# 31 | # Recursion Magic - Don't touch this!! 32 | # 33 | # Each subtree potentially has an include directory 34 | # corresponding to the common APIs applicable to modules 35 | # rooted at that subtree. Accordingly, the INCLUDE PATH 36 | # of a module can only contain the include directories up 37 | # its parent path, and not its siblings 38 | # 39 | # Required for each makefile to inherit from the parent 40 | # 41 | 42 | INCLUDES := $(INCLUDES) -I $(PDIR)include 43 | INCLUDES += -I ./ 44 | PDIR := ../$(PDIR) 45 | sinclude $(PDIR)Makefile 46 | 47 | -------------------------------------------------------------------------------- /upgrade/upgrade_crc32.c: -------------------------------------------------------------------------------- 1 | /***************************************************** 2 | ** Name : crc32.c 3 | ** Author : tianzx 4 | ** Version : 1.0 5 | ** Date : 2016-1 6 | ** Description : CRC32 Checking 7 | ******************************************************/ 8 | #include "esp_common.h" 9 | 10 | #include "lwip/err.h" 11 | #include "lwip/ip_addr.h" 12 | #include "lwip/mem.h" 13 | #include 14 | 15 | #define BUFSIZE 512 16 | #define CRC_BLOCK_SIZE 512 17 | uint16 start_sec; 18 | static unsigned int *crc_table; 19 | 20 | #ifdef MEMLEAK_DEBUG 21 | static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; 22 | #endif 23 | 24 | static int init_crc_table(void); 25 | static unsigned int crc32(unsigned int crc, unsigned char * buffer, unsigned int size); 26 | 27 | static int ICACHE_FLASH_ATTR 28 | init_crc_table(void) 29 | { 30 | unsigned int c; 31 | unsigned int i, j; 32 | 33 | crc_table = (unsigned int*)zalloc(256 * 4); 34 | if(crc_table == NULL){ 35 | os_printf("malloc crc table failed\n"); 36 | return -1; 37 | } 38 | for (i = 0; i < 256; i++) { 39 | c = (unsigned int)i; 40 | for (j = 0; j < 8; j++) { 41 | if (c & 1) 42 | c = 0xedb88320L ^ (c >> 1); 43 | else 44 | c = c >> 1; 45 | } 46 | crc_table[i] = c; 47 | } 48 | return 0; 49 | } 50 | 51 | static unsigned int ICACHE_FLASH_ATTR 52 | crc32(unsigned int crc,unsigned char *buffer, unsigned int size) 53 | { 54 | unsigned int i; 55 | for (i = 0; i < size; i++) { 56 | crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8); 57 | } 58 | return crc ; 59 | } 60 | 61 | 62 | static int ICACHE_FLASH_ATTR 63 | calc_img_crc(unsigned int sumlength,unsigned int *img_crc) 64 | { 65 | int fd; 66 | int ret; 67 | int i = 0; 68 | uint8 error = 0; 69 | unsigned char *buf = (char *)zalloc(BUFSIZE); 70 | if(buf == NULL){ 71 | os_printf("malloc crc buf failed\n"); 72 | free(crc_table); 73 | return -1; 74 | } 75 | 76 | unsigned int crc = 0xffffffff; 77 | 78 | uint16 sec_block = sumlength / CRC_BLOCK_SIZE ; 79 | uint32 sec_last = sumlength % CRC_BLOCK_SIZE; 80 | for (i = 0; i < sec_block; i++) { 81 | if ( 0 != (error = spi_flash_read(start_sec * SPI_FLASH_SEC_SIZE + i * CRC_BLOCK_SIZE ,(uint32 *)buf, BUFSIZE))){ 82 | free(crc_table); 83 | free(buf); 84 | os_printf("spi_flash_read error %d\n",error); 85 | return -1; 86 | } 87 | crc = crc32(crc, buf, BUFSIZE); 88 | } 89 | if(sec_last > 0 ) { 90 | if (0 != (error = spi_flash_read(start_sec * SPI_FLASH_SEC_SIZE + i * CRC_BLOCK_SIZE, (uint32 *)buf, sec_last))){ 91 | free(crc_table); 92 | free(buf); 93 | os_printf("spi_flash_read error %d\n",error); 94 | return -1; 95 | } 96 | crc = crc32(crc, buf, sec_last); 97 | } 98 | *img_crc = crc; 99 | free(crc_table); 100 | free(buf); 101 | return 0; 102 | } 103 | 104 | int ICACHE_FLASH_ATTR 105 | upgrade_crc_check(uint16 fw_bin_sec ,unsigned int sumlength) 106 | { 107 | int ret; 108 | unsigned int img_crc; 109 | unsigned int flash_crc = 0xFF; 110 | start_sec = fw_bin_sec; 111 | if ( 0 != init_crc_table()) { 112 | return false; 113 | } 114 | ret = calc_img_crc(sumlength - 4,&img_crc); 115 | if (ret < 0) { 116 | return false; 117 | } 118 | os_printf("img_crc = %u\n",img_crc); 119 | spi_flash_read(start_sec * SPI_FLASH_SEC_SIZE + sumlength - 4,&flash_crc, 4); 120 | os_printf("flash_crc = %u\n",flash_crc); 121 | if(img_crc == flash_crc) { 122 | return 0; 123 | } else { 124 | return -1; 125 | } 126 | } 127 | 128 | -------------------------------------------------------------------------------- /upgrade/upgrade_lib.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | * FileName: upgrade_lib.c 5 | * 6 | * Description: entry file of user application 7 | * 8 | * Modification history: 9 | * 2015/7/1, v1.0 create this file. 10 | *******************************************************************************/ 11 | #include "esp_common.h" 12 | #include "lwip/mem.h" 13 | #include "upgrade.h" 14 | 15 | struct upgrade_param { 16 | uint32 fw_bin_addr; 17 | uint16 fw_bin_sec; 18 | uint16 fw_bin_sec_num; 19 | uint16 fw_bin_sec_earse; 20 | uint8 extra; 21 | uint8 save[4]; 22 | uint8 *buffer; 23 | }; 24 | 25 | LOCAL struct upgrade_param *upgrade; 26 | 27 | //extern SpiFlashChip *flashchip; 28 | 29 | LOCAL bool OUT_OF_RANGE(uint16 erase_sec) 30 | { 31 | uint8 spi_size_map = system_get_flash_size_map(); 32 | uint16 sec_num = 0; 33 | uint16 start_sec = 0; 34 | 35 | 36 | if (spi_size_map == FLASH_SIZE_8M_MAP_512_512 || 37 | spi_size_map ==FLASH_SIZE_16M_MAP_512_512 || 38 | spi_size_map ==FLASH_SIZE_32M_MAP_512_512){ 39 | start_sec = (system_upgrade_userbin_check() == USER_BIN2)? 1:129; 40 | sec_num = 122; 41 | } else if(spi_size_map == FLASH_SIZE_16M_MAP_1024_1024 || 42 | spi_size_map == FLASH_SIZE_32M_MAP_1024_1024){ 43 | start_sec = (system_upgrade_userbin_check() == USER_BIN2)? 1:257; 44 | sec_num = 250; 45 | } else { 46 | start_sec = (system_upgrade_userbin_check() == USER_BIN2)? 1:65; 47 | sec_num = 58; 48 | } 49 | if((erase_sec >= start_sec) &&(erase_sec <= (start_sec + sec_num))) 50 | { 51 | return false; 52 | } else { 53 | return true; 54 | } 55 | 56 | } 57 | 58 | 59 | 60 | 61 | /****************************************************************************** 62 | * FunctionName : user_upgrade_internal 63 | * Description : a 64 | * Parameters : 65 | * Returns : 66 | *******************************************************************************/ 67 | LOCAL bool 68 | system_upgrade_internal(struct upgrade_param *upgrade, uint8 *data, u32 len) 69 | { 70 | bool ret = false; 71 | uint16 secnm=0; 72 | if(data == NULL || len == 0) 73 | { 74 | return true; 75 | } 76 | 77 | /*got the sumlngth,erase all upgrade sector*/ 78 | if(len > SPI_FLASH_SEC_SIZE ) { 79 | upgrade->fw_bin_sec_earse=upgrade->fw_bin_sec; 80 | 81 | secnm=((upgrade->fw_bin_addr + len)>>12) + (len&0xfff?1:0); 82 | while(upgrade->fw_bin_sec_earse != secnm) { 83 | taskENTER_CRITICAL(); 84 | if( OUT_OF_RANGE( upgrade->fw_bin_sec_earse) ) 85 | { 86 | os_printf("fw_bin_sec_earse:%d, Out of range\n",upgrade->fw_bin_sec_earse); 87 | break; 88 | 89 | } 90 | else 91 | { 92 | spi_flash_erase_sector(upgrade->fw_bin_sec_earse); 93 | upgrade->fw_bin_sec_earse++; 94 | } 95 | taskEXIT_CRITICAL(); 96 | vTaskDelay(10 / portTICK_RATE_MS); 97 | } 98 | os_printf("flash erase over\n"); 99 | return true; 100 | } 101 | 102 | upgrade->buffer = (uint8 *)zalloc(len + upgrade->extra); 103 | 104 | memcpy(upgrade->buffer, upgrade->save, upgrade->extra); 105 | memcpy(upgrade->buffer + upgrade->extra, data, len); 106 | 107 | len += upgrade->extra; 108 | upgrade->extra = len & 0x03; 109 | len -= upgrade->extra; 110 | 111 | if(upgrade->extra<=4) 112 | memcpy(upgrade->save, upgrade->buffer + len, upgrade->extra); 113 | else 114 | os_printf("ERR3:arr_overflow,%u,%d\n",__LINE__,upgrade->extra); 115 | 116 | do { 117 | if (upgrade->fw_bin_addr + len >= (upgrade->fw_bin_sec + upgrade->fw_bin_sec_num) * SPI_FLASH_SEC_SIZE) { 118 | break; 119 | } 120 | 121 | if (spi_flash_write(upgrade->fw_bin_addr, (uint32 *)upgrade->buffer, len) != SPI_FLASH_RESULT_OK) { 122 | break; 123 | } 124 | 125 | ret = true; 126 | upgrade->fw_bin_addr += len; 127 | } while (0); 128 | 129 | free(upgrade->buffer); 130 | upgrade->buffer = NULL; 131 | return ret; 132 | } 133 | 134 | /****************************************************************************** 135 | * FunctionName : system_get_fw_start_sec 136 | * Description : a 137 | * Parameters : 138 | * Returns : 139 | *******************************************************************************/ 140 | uint16 system_get_fw_start_sec() 141 | { 142 | if(upgrade != NULL) { 143 | return upgrade->fw_bin_sec; 144 | } else { 145 | return 0; 146 | } 147 | } 148 | 149 | /****************************************************************************** 150 | * FunctionName : user_upgrade 151 | * Description : a 152 | * Parameters : 153 | * Returns : 154 | *******************************************************************************/ 155 | bool system_upgrade(uint8 *data, uint32 len) 156 | { 157 | bool ret; 158 | 159 | /* for connect data debug 160 | if(len < 1460){ 161 | char *precv_buf = (char*)malloc(1480); 162 | memcpy(precv_buf, data,len); 163 | memcpy(precv_buf+len,"\0\r\n",3); 164 | printf("%s\n",precv_buf); 165 | free(precv_buf); 166 | } 167 | */ 168 | ret = system_upgrade_internal(upgrade, data, len); 169 | 170 | return ret; 171 | } 172 | 173 | /****************************************************************************** 174 | * FunctionName : system_upgrade_init 175 | * Description : a 176 | * Parameters : 177 | * Returns : 178 | *******************************************************************************/ 179 | void 180 | system_upgrade_init(void) 181 | { 182 | uint32 user_bin2_start,user_bin1_start; 183 | uint8 spi_size_map = system_get_flash_size_map(); 184 | 185 | if (upgrade == NULL) { 186 | upgrade = (struct upgrade_param *)zalloc(sizeof(struct upgrade_param)); 187 | } 188 | 189 | user_bin1_start = 1; 190 | 191 | if (spi_size_map == FLASH_SIZE_8M_MAP_512_512 || 192 | spi_size_map ==FLASH_SIZE_16M_MAP_512_512 || 193 | spi_size_map ==FLASH_SIZE_32M_MAP_512_512){ 194 | user_bin2_start = 129; 195 | upgrade->fw_bin_sec_num = 122; 196 | } else if(spi_size_map == FLASH_SIZE_16M_MAP_1024_1024 || 197 | spi_size_map == FLASH_SIZE_32M_MAP_1024_1024){ 198 | user_bin2_start = 257; 199 | upgrade->fw_bin_sec_num = 250; 200 | } else { 201 | user_bin2_start = 65; 202 | upgrade->fw_bin_sec_num = 58; 203 | } 204 | 205 | upgrade->fw_bin_sec = (system_upgrade_userbin_check() == USER_BIN1) ? user_bin2_start : user_bin1_start; 206 | 207 | upgrade->fw_bin_addr = upgrade->fw_bin_sec * SPI_FLASH_SEC_SIZE; 208 | 209 | upgrade->fw_bin_sec_earse = upgrade->fw_bin_sec; 210 | } 211 | 212 | /****************************************************************************** 213 | * FunctionName : system_upgrade_deinit 214 | * Description : a 215 | * Parameters : 216 | * Returns : 217 | *******************************************************************************/ 218 | void 219 | system_upgrade_deinit(void) 220 | { 221 | if (upgrade != NULL) { 222 | free(upgrade); 223 | upgrade = NULL; 224 | }else { 225 | return; 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /user/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ############################################################# 3 | # Required variables for each makefile 4 | # Discard this section from all parent makefiles 5 | # Expected variables (with automatic defaults): 6 | # CSRCS (all "C" files in the dir) 7 | # SUBDIRS (all subdirs with a Makefile) 8 | # GEN_LIBS - list of libs to be generated () 9 | # GEN_IMAGES - list of images to be generated () 10 | # COMPONENTS_xxx - a list of libs/objs in the form 11 | # subdir/lib to be extracted and rolled up into 12 | # a generated lib/image xxx.a () 13 | # 14 | ifndef PDIR 15 | GEN_LIBS = libuser.a 16 | endif 17 | 18 | 19 | ############################################################# 20 | # Configuration i.e. compile options etc. 21 | # Target specific stuff (defines etc.) goes in here! 22 | # Generally values applying to a tree are captured in the 23 | # makefile at its root level - these are then overridden 24 | # for a subtree within the makefile rooted therein 25 | # 26 | #DEFINES += 27 | 28 | ############################################################# 29 | # Recursion Magic - Don't touch this!! 30 | # 31 | # Each subtree potentially has an include directory 32 | # corresponding to the common APIs applicable to modules 33 | # rooted at that subtree. Accordingly, the INCLUDE PATH 34 | # of a module can only contain the include directories up 35 | # its parent path, and not its siblings 36 | # 37 | # Required for each makefile to inherit from the parent 38 | # 39 | 40 | INCLUDES := $(INCLUDES) -I $(PDIR)include 41 | INCLUDES += -I ./ 42 | PDIR := ../$(PDIR) 43 | sinclude $(PDIR)Makefile 44 | 45 | -------------------------------------------------------------------------------- /user/user_configstore.c: -------------------------------------------------------------------------------- 1 | #include "user_configstore.h" 2 | #include "spi_flash.h" 3 | #include 4 | #include "user_light_action.h" 5 | 6 | MyConfig myConfig; 7 | static int confLoc=0; 8 | 9 | //Simple additive 8-bit checksum 10 | static char calcChsum(MyConfig *conf) { 11 | char *p=(char*)conf; 12 | int x; 13 | char r=0x5a; 14 | for (x=1; x10000 || light_param.pwm_period <1000){ 166 | light_param.pwm_period = 1000; 167 | light_param.pwm_duty[0]= APP_MAX_PWM; 168 | light_param.pwm_duty[1]= APP_MAX_PWM; 169 | light_param.pwm_duty[2]= APP_MAX_PWM; 170 | light_param.pwm_duty[3]= APP_MAX_PWM; 171 | light_param.pwm_duty[4]= APP_MAX_PWM; 172 | } 173 | printf("LIGHT P:%d",light_param.pwm_period); 174 | printf(" R:%d",light_param.pwm_duty[LIGHT_RED]); 175 | printf(" G:%d",light_param.pwm_duty[LIGHT_GREEN]); 176 | printf(" B:%d",light_param.pwm_duty[LIGHT_BLUE]); 177 | if(PWM_CHANNEL>LIGHT_COLD_WHITE){ 178 | printf(" CW:%d",light_param.pwm_duty[LIGHT_COLD_WHITE]); 179 | printf(" WW:%d\r\n",light_param.pwm_duty[LIGHT_WARM_WHITE]); 180 | }else{ 181 | printf("\r\n"); 182 | } 183 | 184 | light_set_aim(light_param.pwm_duty[LIGHT_RED], 185 | light_param.pwm_duty[LIGHT_GREEN], 186 | light_param.pwm_duty[LIGHT_BLUE], 187 | light_param.pwm_duty[LIGHT_COLD_WHITE], 188 | light_param.pwm_duty[LIGHT_WARM_WHITE], 189 | light_param.pwm_period); 190 | 191 | } 192 | #endif 193 | 194 | -------------------------------------------------------------------------------- /user/user_main.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | * FileName: user_main.c 5 | * 6 | * Description: get and config the device timer 7 | * 8 | * Modification history: 9 | * 2015/7/1, v1.0 create this file. 10 | *******************************************************************************/ 11 | 12 | #include "esp_common.h" 13 | 14 | #include "freertos/FreeRTOS.h" 15 | #include "freertos/task.h" 16 | 17 | #include "lwip/sockets.h" 18 | #include "lwip/dns.h" 19 | #include "lwip/netdb.h" 20 | 21 | #include "user_config.h" 22 | #include "user_devicefind.h" 23 | #include "user_webserver.h" 24 | #ifdef ESP_PLATFORM 25 | #include "user_esp_platform.h" 26 | #endif 27 | 28 | #if HTTPD_SERVER 29 | #include "espfsformat.h" 30 | #include "espfs.h" 31 | #include "captdns.h" 32 | #include "httpd.h" 33 | #include "cgiwifi.h" 34 | #include "httpdespfs.h" 35 | #include "user_cgi.h" 36 | #include "webpages-espfs.h" 37 | 38 | #ifdef ESPFS_HEATSHRINK 39 | #include "heatshrink_config_custom.h" 40 | #include "heatshrink_decoder.h" 41 | #endif 42 | 43 | #endif 44 | 45 | #ifdef SERVER_SSL_ENABLE 46 | #include "ssl/cert.h" 47 | #include "ssl/private_key.h" 48 | #else 49 | 50 | #ifdef CLIENT_SSL_ENABLE 51 | unsigned char *default_certificate; 52 | unsigned int default_certificate_len = 0; 53 | unsigned char *default_private_key; 54 | unsigned int default_private_key_len = 0; 55 | #endif 56 | #endif 57 | 58 | #if HTTPD_SERVER 59 | HttpdBuiltInUrl builtInUrls[]={ 60 | {"*", cgiRedirectApClientToHostname, "esp.nonet"}, 61 | {"/", cgiRedirect, "/index.html"}, 62 | {"/wifi", cgiRedirect, "/wifi/wifi.tpl"}, 63 | {"/wifi/wifiscan.cgi", cgiWiFiScan, NULL}, 64 | {"/wifi/wifi.tpl", cgiEspFsTemplate, tplWlan}, 65 | {"/wifi/connect.cgi", cgiWiFiConnect, NULL}, 66 | {"/wifi/connstatus.cgi", cgiWiFiConnStatus, NULL}, 67 | {"/wifi/setmode.cgi", cgiWiFiSetMode, NULL}, 68 | 69 | {"/config", cgiEspApi, NULL}, 70 | {"/client", cgiEspApi, NULL}, 71 | {"/upgrade", cgiEspApi, NULL}, 72 | 73 | {"*", cgiEspFsHook, NULL}, //Catch-all cgi function for the filesystem 74 | {NULL, NULL, NULL} //end marker 75 | }; 76 | #endif 77 | 78 | /****************************************************************************** 79 | * FunctionName : user_rf_cal_sector_set 80 | * Description : SDK just reversed 4 sectors, used for rf init data and paramters. 81 | * We add this function to force users to set rf cal sector, since 82 | * we don't know which sector is free in user's application. 83 | * sector map for last several sectors : ABCCC 84 | * A : rf cal 85 | * B : rf init data 86 | * C : sdk parameters 87 | * Parameters : none 88 | * Returns : rf cal sector 89 | *******************************************************************************/ 90 | uint32 user_rf_cal_sector_set(void) 91 | { 92 | flash_size_map size_map = system_get_flash_size_map(); 93 | uint32 rf_cal_sec = 0; 94 | 95 | switch (size_map) { 96 | case FLASH_SIZE_4M_MAP_256_256: 97 | rf_cal_sec = 128 - 5; 98 | break; 99 | 100 | case FLASH_SIZE_8M_MAP_512_512: 101 | rf_cal_sec = 256 - 5; 102 | break; 103 | 104 | case FLASH_SIZE_16M_MAP_512_512: 105 | case FLASH_SIZE_16M_MAP_1024_1024: 106 | rf_cal_sec = 512 - 5; 107 | break; 108 | 109 | case FLASH_SIZE_32M_MAP_512_512: 110 | case FLASH_SIZE_32M_MAP_1024_1024: 111 | rf_cal_sec = 1024 - 5; 112 | break; 113 | 114 | default: 115 | rf_cal_sec = 0; 116 | break; 117 | } 118 | 119 | return rf_cal_sec; 120 | } 121 | 122 | /****************************************************************************** 123 | * FunctionName : user_init 124 | * Description : entry of user application, init user function here 125 | * Parameters : none 126 | * Returns : none 127 | *******************************************************************************/ 128 | void user_init(void) 129 | { 130 | printf("SDK version:%s,%u\n", system_get_sdk_version(),__LINE__ ); 131 | wifi_set_opmode(STATIONAP_MODE); 132 | 133 | #if ESP_PLATFORM 134 | /*Initialization of the peripheral drivers*/ 135 | /*For light demo , it is user_light_init();*/ 136 | /* Also check whether assigned ip addr by the router.If so, connect to ESP-server */ 137 | user_esp_platform_init(); 138 | #endif 139 | 140 | /*Establish a udp socket to receive local device detect info.*/ 141 | /*Listen to the port 1025, as well as udp broadcast. 142 | /*If receive a string of device_find_request, it rely its IP address and MAC.*/ 143 | user_devicefind_start(); 144 | 145 | #if WEB_SERVICE 146 | /*Establish a TCP server for http(with JSON) POST or GET command to communicate with the device.*/ 147 | /*You can find the command in "2B-SDK-Espressif IoT Demo.pdf" to see the details.*/ 148 | user_webserver_start(); 149 | 150 | #elif HTTPD_SERVER 151 | /*Initialize DNS server for captive portal*/ 152 | captdnsInit(); 153 | 154 | /*Initialize espfs containing static webpages*/ 155 | espFsInit((void*)(webpages_espfs_start)); 156 | 157 | /*Initialize webserver*/ 158 | httpdInit(builtInUrls, 80); 159 | #endif 160 | 161 | } 162 | 163 | 164 | -------------------------------------------------------------------------------- /user/user_plug.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright (C) 2014 -2016 Espressif System 3 | * 4 | * FileName: user_plug.c 5 | * 6 | * Description: plug demo's function realization 7 | * 8 | * Modification history: 9 | * 2015/7/1, v1.0 create this file. 10 | *******************************************************************************/ 11 | #include "esp_common.h" 12 | #include "user_config.h" 13 | #if PLUG_DEVICE 14 | #include "user_plug.h" 15 | 16 | LOCAL struct plug_saved_param plug_param; 17 | LOCAL struct keys_param keys; 18 | LOCAL struct single_key_param *single_key[PLUG_KEY_NUM]; 19 | LOCAL os_timer_t link_led_timer; 20 | LOCAL uint8 link_led_level = 0; 21 | 22 | /****************************************************************************** 23 | * FunctionName : user_plug_get_status 24 | * Description : get plug's status, 0x00 or 0x01 25 | * Parameters : none 26 | * Returns : uint8 - plug's status 27 | *******************************************************************************/ 28 | uint8 29 | user_plug_get_status(void) 30 | { 31 | return plug_param.status; 32 | } 33 | 34 | /****************************************************************************** 35 | * FunctionName : user_plug_set_status 36 | * Description : set plug's status, 0x00 or 0x01 37 | * Parameters : uint8 - status 38 | * Returns : none 39 | *******************************************************************************/ 40 | void 41 | user_plug_set_status(bool status) 42 | { 43 | if (status != plug_param.status) { 44 | if (status > 1) { 45 | printf("error status input!\n"); 46 | return; 47 | } 48 | printf("status input! %d\n", status); 49 | 50 | plug_param.status = status; 51 | PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, status); 52 | } 53 | } 54 | 55 | /****************************************************************************** 56 | * FunctionName : user_plug_short_press 57 | * Description : key's short press function, needed to be installed 58 | * Parameters : none 59 | * Returns : none 60 | *******************************************************************************/ 61 | LOCAL void 62 | user_plug_short_press(void) 63 | { 64 | user_plug_set_status((~plug_param.status) & 0x01); 65 | spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE); 66 | spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE, 67 | (uint32 *)&plug_param, sizeof(struct plug_saved_param)); 68 | } 69 | 70 | /****************************************************************************** 71 | * FunctionName : user_plug_long_press 72 | * Description : key's long press function, needed to be installed 73 | * Parameters : none 74 | * Returns : none 75 | *******************************************************************************/ 76 | LOCAL void 77 | user_plug_long_press(void) 78 | { 79 | int boot_flag=12345; 80 | user_esp_platform_set_active(0); 81 | system_restore(); 82 | 83 | system_rtc_mem_write(70, &boot_flag, sizeof(boot_flag)); 84 | printf("long_press boot_flag %d \n",boot_flag); 85 | system_rtc_mem_read(70, &boot_flag, sizeof(boot_flag)); 86 | printf("long_press boot_flag %d \n",boot_flag); 87 | 88 | #if RESTORE_KEEP_TIMER 89 | user_platform_timer_bkup(); 90 | #endif 91 | 92 | system_restart(); 93 | } 94 | 95 | /****************************************************************************** 96 | * FunctionName : user_link_led_init 97 | * Description : int led gpio 98 | * Parameters : none 99 | * Returns : none 100 | *******************************************************************************/ 101 | LOCAL void 102 | user_link_led_init(void) 103 | { 104 | PIN_FUNC_SELECT(PLUG_LINK_LED_IO_MUX, PLUG_LINK_LED_IO_FUNC); 105 | } 106 | 107 | LOCAL void 108 | user_link_led_timer_cb(void) 109 | { 110 | link_led_level = (~link_led_level) & 0x01; 111 | GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), link_led_level); 112 | } 113 | 114 | void 115 | user_link_led_timer_init(int time) 116 | { 117 | os_timer_disarm(&link_led_timer); 118 | os_timer_setfn(&link_led_timer, (os_timer_func_t *)user_link_led_timer_cb, NULL); 119 | os_timer_arm(&link_led_timer, time, 1); 120 | link_led_level = 0; 121 | GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), link_led_level); 122 | } 123 | /* 124 | void 125 | user_link_led_timer_done(void) 126 | { 127 | os_timer_disarm(&link_led_timer); 128 | 129 | GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), 1); 130 | } 131 | */ 132 | /****************************************************************************** 133 | * FunctionName : user_link_led_output 134 | * Description : led flash mode 135 | * Parameters : mode, on/off/xhz 136 | * Returns : none 137 | *******************************************************************************/ 138 | void 139 | user_link_led_output(uint8 mode) 140 | { 141 | 142 | switch (mode) { 143 | case LED_OFF: 144 | os_timer_disarm(&link_led_timer); 145 | GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), 1); 146 | break; 147 | 148 | case LED_ON: 149 | os_timer_disarm(&link_led_timer); 150 | GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), 0); 151 | break; 152 | 153 | case LED_1HZ: 154 | user_link_led_timer_init(1000); 155 | break; 156 | 157 | case LED_5HZ: 158 | user_link_led_timer_init(200); 159 | break; 160 | 161 | case LED_20HZ: 162 | user_link_led_timer_init(50); 163 | break; 164 | 165 | default: 166 | printf("ERROR:LED MODE WRONG!\n"); 167 | break; 168 | } 169 | 170 | } 171 | 172 | /****************************************************************************** 173 | * FunctionName : user_get_key_status 174 | * Description : a 175 | * Parameters : none 176 | * Returns : none 177 | *******************************************************************************/ 178 | BOOL 179 | user_get_key_status(void) 180 | { 181 | return get_key_status(single_key[0]); 182 | } 183 | 184 | /****************************************************************************** 185 | * FunctionName : user_plug_init 186 | * Description : init plug's key function and relay output 187 | * Parameters : none 188 | * Returns : none 189 | *******************************************************************************/ 190 | void 191 | user_plug_init(void) 192 | { 193 | printf("user_plug_init start!\n"); 194 | 195 | user_link_led_init(); 196 | 197 | wifi_status_led_install(PLUG_WIFI_LED_IO_NUM, PLUG_WIFI_LED_IO_MUX, PLUG_WIFI_LED_IO_FUNC); 198 | 199 | single_key[0] = key_init_single(PLUG_KEY_0_IO_NUM, PLUG_KEY_0_IO_MUX, PLUG_KEY_0_IO_FUNC, 200 | user_plug_long_press, user_plug_short_press); 201 | 202 | keys.key_num = PLUG_KEY_NUM; 203 | keys.single_key = single_key; 204 | 205 | key_init(&keys); 206 | 207 | spi_flash_read((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE, 208 | (uint32 *)&plug_param, sizeof(struct plug_saved_param)); 209 | 210 | PIN_FUNC_SELECT(PLUG_RELAY_LED_IO_MUX, PLUG_RELAY_LED_IO_FUNC); 211 | 212 | // default to be off, for safety. 213 | if (plug_param.status == 0xff) { 214 | plug_param.status = 0; 215 | } 216 | 217 | PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, plug_param.status); 218 | } 219 | #endif 220 | 221 | --------------------------------------------------------------------------------