From ea4510dfe03f891340985594c4de15134e548853 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 17 Nov 2015 12:54:57 +0000 Subject: recvr --- polycom_recv/Makefile | 298 ++++++ polycom_recv/gpio.c | 115 ++ polycom_recv/main.c | 23 + polycom_recv/mdns.c | 57 + polycom_recv/msg.c | 101 ++ polycom_recv/platform.c | 1498 ++++++++++++++++++++++++++ polycom_recv/project.h | 43 + polycom_recv/prototypes.h | 27 + polycom_recv/reset.c | 20 + polycom_recv/uart.c | 61 ++ polycom_recv/uart_register.h | 160 +++ polycom_recv/upgrade.c | 83 ++ polycom_recv/util.c | 55 + polycom_recv/webserver.c | 2442 ++++++++++++++++++++++++++++++++++++++++++ polycom_recv/webserver.h | 45 + polycom_recv/wifi.c | 21 + 16 files changed, 5049 insertions(+) create mode 100644 polycom_recv/Makefile create mode 100644 polycom_recv/gpio.c create mode 100644 polycom_recv/main.c create mode 100644 polycom_recv/mdns.c create mode 100644 polycom_recv/msg.c create mode 100644 polycom_recv/platform.c create mode 100644 polycom_recv/project.h create mode 100644 polycom_recv/prototypes.h create mode 100644 polycom_recv/reset.c create mode 100644 polycom_recv/uart.c create mode 100644 polycom_recv/uart_register.h create mode 100644 polycom_recv/upgrade.c create mode 100644 polycom_recv/util.c create mode 100644 polycom_recv/webserver.c create mode 100644 polycom_recv/webserver.h create mode 100644 polycom_recv/wifi.c diff --git a/polycom_recv/Makefile b/polycom_recv/Makefile new file mode 100644 index 0000000..5103815 --- /dev/null +++ b/polycom_recv/Makefile @@ -0,0 +1,298 @@ +OPT=-Os -g -O2 + +WARN=-Wpointer-arith -Wundef -Werror +DEFS=-DICACHE_FLASH +INCLUDES=-Idummy + +GCCFLAGS=-fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -ffunction-sections -fdata-sections + +PORT=/dev/ttyUSB0 +#BAUD=-b 921600 +#BAUD=-b 460800 +#BAUD=-b 200000 +#BAUD=-b 9600 + +#BOOT=none +BOOT=new +SPI_SPEED=40 +SPI_MODE=QIO +SPI_SIZE_MAP=512 + + +#esptool.py write_flash 0x00000 ../bin/eagle.flash.bin 0x40000 ../bin/eagle.irom0text.bin || exit 1 + +PROG=polycom_xmit +PROG0=${PROG}.0 +PROG1=${PROG}.1 +PROG2=${PROG}.2 + +SRC=main.c webserver.c util.c reset.c wifi.c uart.c upgrade.c gpio.c msg.c mdns.c + +UART_BAUD=115200 + +default: flash.stamp + sympathy -b ${UART_BAUD} -d ${PORT} -t -L log + +view: + sympathy -b ${UART_BAUD} -d ${PORT} -t -L log + + +flash:flash.stamp + sympathy -b ${UART_BAUD} -d ${PORT} -t -L log + +flash.stamp: user0.bin + ${EPSTOOL} ${BAUD} --port ${PORT} write_flash 0x00000 user0.bin 0x40000 user0/eagle.app.v6.irom0text.bin + touch $@ + +flash1:flash1.stamp + sympathy -b 115200 -d ${PORT} -t -L log + +flash1.stamp: user1.bin ${BLANK_BIN} ${BOOT_BIN} + ${EPSTOOL} ${BAUD} --port ${PORT} write_flash 0x00000 "${BOOT_BIN}" 0x01000 user1.bin 0x7c000 ${BLANK_BIN} + touch $@ + +put: put.stamp + +put.stamp: user1.bin user2.bin + scp user1.bin user2.bin global:/var/www/esp8266/ + + +clean: + /bin/rm -f ${OBJ} ${PROG0} ${PROG1} ${PROG2} out.stamp flash.stamp put.stamp user0.bin user1.bin user2.bin flash1.stamp *~ + /bin/rm -rf user0 user1 user2 + +protos: + echo -n > prototypes.h + cproto -v -E "${CC} -E" ${CPPFLAGS} ${SRC} > prototypes.h.tmp + mv -f prototypes.h.tmp prototypes.h + +tidy: + indent -i2 -ts0 ${SRC} + + + +########################################################################################33 +SDK=/usr/xtensa-lx106-elf/esp-8266-sdk + +LDDIR=${SDK}/ld/ +BOOT_BIN=${SDK}/bin/boot_v1.4(b1).bin +BLANK_BIN=${SDK}/bin/blank.bin + + +CROSS=xtensa-lx106-elf- + +EPSTOOL=esptool.py +CC=${CROSS}cc +AR=${CROSS}ar +OBJDUMP=${CROSS}objdump +OBJCOPY=${CROSS}objcopy +GENAPPBIN=${CROSS}gen_appbin.py +CPROTO=cproto + +BOOT?=none +SPI_SPEED?=40 +SPI_MODE?=QIO +SPI_SIZE_MAP?=0 + +ifeq ($(BOOT), new) + boot = new +else + ifeq ($(BOOT), old) + boot = old + else + boot = none + endif +endif + + +ifeq ($(SPI_SPEED), 26.7) + freqdiv = 1 +else + ifeq ($(SPI_SPEED), 20) + freqdiv = 2 + else + ifeq ($(SPI_SPEED), 80) + freqdiv = 15 + else + freqdiv = 0 + endif + endif +endif + + +ifeq ($(SPI_MODE), QOUT) + mode = 1 +else + ifeq ($(SPI_MODE), DIO) + mode = 2 + else + ifeq ($(SPI_MODE), DOUT) + mode = 3 + else + mode = 0 + endif + endif +endif + +addr0 = 0x01000 +addr1 = 0x01000 + +ifeq ($(SPI_SIZE_MAP), 1) + size_map = 1 + flash = 256 +else + ifeq ($(SPI_SIZE_MAP), 2) + size_map = 2 + flash = 1024 + addr2 = 0x81000 + else + ifeq ($(SPI_SIZE_MAP), 3) + size_map = 3 + flash = 2048 + addr2 = 0x81000 + else + ifeq ($(SPI_SIZE_MAP), 4) + size_map = 4 + flash = 4096 + addr2 = 0x81000 + else + ifeq ($(SPI_SIZE_MAP), 5) + size_map = 5 + flash = 2048 + addr2 = 0x101000 + else + ifeq ($(SPI_SIZE_MAP), 6) + size_map = 6 + flash = 4096 + addr2 = 0x101000 + else + size_map = 0 + flash = 512 + addr2 = 0x41000 + endif + endif + endif + endif + endif +endif + +flash_b=$(shell echo $(flash) \* 1024 | bc ) +hflash_b=$(shell echo $(flash) \* 512 | bc ) + +LD_FILE0 = $(LDDIR)/eagle.app.v6.ld +LD_FILE1 = i-dont-exist +LD_FILE2 = i-dont-exist + +ifneq ($(boot), none) + ifeq ($(size_map), 6) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).2048.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).2048.ld + else + ifeq ($(size_map), 5) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).2048.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).2048.ld + else + ifeq ($(size_map), 4) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).1024.app1.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).1024.app2.ld + else + ifeq ($(size_map), 3) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).1024.app1.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).1024.app2.ld + else + ifeq ($(size_map), 2) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).1024.app1.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).1024.app2.ld + else + ifeq ($(size_map), 0) + LD_FILE1 = $(LDDIR)/eagle.app.v6.$(boot).512.app1.ld + LD_FILE2 = $(LDDIR)/eagle.app.v6.$(boot).512.app2.ld + endif + endif + endif + endif + endif +endif +endif + +user0.bin:${PROG0} + rm -rf user0 + mkdir -p user0 + $(OBJDUMP) -x -s $< > user0/eagle.dump + $(OBJDUMP) -S $< > user0/eagle.S + + $(OBJCOPY) --only-section .text -O binary $< user0/eagle.app.v6.text.bin + $(OBJCOPY) --only-section .data -O binary $< user0/eagle.app.v6.data.bin + $(OBJCOPY) --only-section .rodata -O binary $< user0/eagle.app.v6.rodata.bin + $(OBJCOPY) --only-section .irom0.text -O binary $< user0/eagle.app.v6.irom0text.bin + + (cd user0 && ${GENAPPBIN} ../$< 0 $(mode) $(freqdiv) $(size_map) ) + + cp user0/eagle.app.flash.bin user0.bin + + @echo "** user0.bin uses $$(stat -c '%s' $@) bytes of" $(flash_b) "available"0 + if [ $$(stat -c '%s' $@) -gt $$(( $(flash_b) )) ]; then echo "$@ too big!"; false; fi + + +user1.bin:${PROG1} + rm -rf user1 + mkdir -p user1 + $(OBJDUMP) -x -s $< > user1/eagle.dump + $(OBJDUMP) -S $< > user1/eagle.S + + $(OBJCOPY) --only-section .text -O binary $< user1/eagle.app.v6.text.bin + $(OBJCOPY) --only-section .data -O binary $< user1/eagle.app.v6.data.bin + $(OBJCOPY) --only-section .rodata -O binary $< user1/eagle.app.v6.rodata.bin + $(OBJCOPY) --only-section .irom0.text -O binary $< user1/eagle.app.v6.irom0text.bin + + (cd user1 && ${GENAPPBIN} ../$< 1 $(mode) $(freqdiv) $(size_map) ) + + cp user1/eagle.app.flash.bin user1.bin + + @echo "** user1.bin uses $$(stat -c '%s' $@) bytes of" $(hflash_b) "available"0 + if [ $$(stat -c '%s' $@) -gt $$(( $(hflash_b) )) ]; then echo "$@ too big!"; false; fi + + +user2.bin:${PROG2} + rm -rf user2 + mkdir -p user2 + $(OBJDUMP) -x -s $< > user2/eagle.dump + $(OBJDUMP) -S $< > user2/eagle.S + + $(OBJCOPY) --only-section .text -O binary $< user2/eagle.app.v6.text.bin + $(OBJCOPY) --only-section .data -O binary $< user2/eagle.app.v6.data.bin + $(OBJCOPY) --only-section .rodata -O binary $< user2/eagle.app.v6.rodata.bin + $(OBJCOPY) --only-section .irom0.text -O binary $< user2/eagle.app.v6.irom0text.bin + + (cd user2 && ${GENAPPBIN} ../$< 2 $(mode) $(freqdiv) $(size_map) ) + + cp user2/eagle.app.flash.bin user2.bin + if [ $$(stat -c '%s' $@) -gt $$(( $(hflash_b) )) ]; then echo "$@ too big!"; false; fi + + + + +CFLAGS=${GCCFLAGS} ${WARN} ${OPT} ${CPPFLAGS} +CPPFLAGS=${DEFS} ${INCLUDES} + +OBJ=${SRC:%.c=%.o} + + +LDFLAGS= -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,-EL + +LIBS=-Wl,--start-group -lc -lgcc -lhal -lphy -lpp -lnet80211 -llwip -lwpa -lmain -ljson -lupgrade -lssl -lpwm -lsmartconfig ${OBJ} -Wl,--end-group + +%.o:%.c + ${CC} ${CFLAGS} ${CPPFLAGS} -c -o $@ $< + +${PROG0}: ${OBJ} ${LD_FILE0} + ${CC} -o ${PROG0} -T${LD_FILE0} ${CFLAGS} ${LDFLAGS} ${LIBS} + +${PROG1}: ${OBJ} ${LD_FILE1} + ${CC} -o ${PROG1} -T${LD_FILE1} ${CFLAGS} ${LDFLAGS} ${LIBS} + +${PROG2}: ${OBJ} ${LD_FILE2} + ${CC} -o ${PROG2} -T${LD_FILE2} ${CFLAGS} ${LDFLAGS} ${LIBS} + + + diff --git a/polycom_recv/gpio.c b/polycom_recv/gpio.c new file mode 100644 index 0000000..33b3cda --- /dev/null +++ b/polycom_recv/gpio.c @@ -0,0 +1,115 @@ +#include "project.h" + +static os_timer_t gpio_timer; + + +static struct gpio +{ + int id; + uint32_t pin; + uint32_t func; + uint32_t bit; + int pull; + int state; +} gpios[] = +{ + { + 14, PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14, BIT14, 0}, + { + 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, BIT12, 0}, + { + 13, PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13, BIT13, 0}, + { + 0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0, BIT0, 0} +}; + +#define N_GPIOS (sizeof(gpios)/sizeof(gpios[0])) + +static uint32_t mask; + +static os_timer_t gpio_timer; +static os_timer_t gpio_intr_timer; + +static int flash; + + +uint32_t ICACHE_FLASH_ATTR +gpio_read (void) +{ + return gpio_input_get () & mask; +} + + + +static void ICACHE_FLASH_ATTR +gpio_dispatch (void) +{ + uint32_t v = 0; + int i; + + for (i = 0; i < N_GPIOS; ++i) + { + if ((gpios[i].state == 1 && flash) || (gpios[i].state == 2)) + v |= gpios[i].bit; + } + gpio_output_set (v, (~v) & mask, mask, 0); +} + + + + +static void ICACHE_FLASH_ATTR +gpio_timer_cb (void *arg) +{ + flash = !flash; + + gpio_dispatch (); +} + + +void ICACHE_FLASH_ATTR +gpio_write (int a, int b, int c, int d) +{ + uint32_t v = 0; + + gpios[0].state = a; + gpios[1].state = b; + gpios[2].state = c; + gpios[3].state = d; + + gpio_dispatch (); +} + + +void ICACHE_FLASH_ATTR +gpio_init (void) +{ + int i; + + for (i = 0; i < N_GPIOS; ++i) + { + PIN_FUNC_SELECT (gpios[i].pin, gpios[i].func); + if (gpios[i].pull) + PIN_PULLUP_EN (gpios[i].pin); + else + PIN_PULLUP_DIS (gpios[i].pin); + +/* disable drivers */ + gpio_output_set (0, 0, gpios[i].bit, 0); + + gpio_register_set (GPIO_PIN_ADDR (gpios[i].id), + GPIO_PIN_INT_TYPE_SET (GPIO_PIN_INTR_DISABLE) | + GPIO_PIN_PAD_DRIVER_SET (GPIO_PAD_DRIVER_ENABLE) | + GPIO_PIN_SOURCE_SET (GPIO_AS_PIN_SOURCE)); + + GPIO_REG_WRITE (GPIO_STATUS_W1TC_ADDRESS, gpios[i].bit); + + mask |= gpios[i].bit; + } + + os_timer_disarm (&gpio_timer); + os_timer_setfn (&gpio_timer, (os_timer_func_t *) gpio_timer_cb, NULL); + os_timer_arm (&gpio_timer, 500, 1); + + +} diff --git a/polycom_recv/main.c b/polycom_recv/main.c new file mode 100644 index 0000000..5739799 --- /dev/null +++ b/polycom_recv/main.c @@ -0,0 +1,23 @@ +#include "project.h" + +void +user_rf_pre_init (void) +{ +} + +void +user_init (void) +{ + uart_init (); + + os_printf ("SDK version:%s\n", system_get_sdk_version ()); + os_printf ("Hello world\n"); + + reset_init (); + wifi_init (); + msg_init (); + gpio_init (); + mdns_start (); + + webserver_init (); +} diff --git a/polycom_recv/mdns.c b/polycom_recv/mdns.c new file mode 100644 index 0000000..51d1b48 --- /dev/null +++ b/polycom_recv/mdns.c @@ -0,0 +1,57 @@ +#include "project.h" + +static os_timer_t mdns_timer; + +static struct mdns_info *info; + + + +static void ICACHE_FLASH_ATTR +check_connexion (void *arg) +{ + struct ip_info ipconfig; + static ip_addr_t last_ip; + + wifi_get_ip_info (STATION_IF, &ipconfig); + + if (wifi_station_get_connect_status () != STATION_GOT_IP) + return; + + if (ipconfig.ip.addr == last_ip.addr) + return; + + last_ip.addr = ipconfig.ip.addr; + + + + if (info) + { + espconn_mdns_close (); + os_free (info); + } + + info = (struct mdns_info *) os_zalloc (sizeof (struct mdns_info)); + + if (!info) + { + last_ip.addr = 0; + return; + } + + + info->host_name = "lights"; + info->ipAddr = last_ip.addr; + info->server_name = "lights"; + info->server_port = 80; + info->txt_data[0] = "version = 1.0.1"; + espconn_mdns_init (info); + +} + +void ICACHE_FLASH_ATTR +mdns_start (void) +{ + os_timer_disarm (&mdns_timer); + os_timer_setfn (&mdns_timer, (os_timer_func_t *) check_connexion, NULL); + os_timer_arm (&mdns_timer, 10000, 1); +} diff --git a/polycom_recv/msg.c b/polycom_recv/msg.c new file mode 100644 index 0000000..7671fd6 --- /dev/null +++ b/polycom_recv/msg.c @@ -0,0 +1,101 @@ +#include "project.h" + +static struct espconn udp; + +static int a, b, c, d; + + +static void ICACHE_FLASH_ATTR +parse_field (char *str, int *v) +{ + if (!strcmp (str, "0")) + *v = 0; + if (!strcmp (str, "1")) + *v = 1; + if (!strcmp (str, "2")) + *v = 2; +} + +static void ICACHE_FLASH_ATTR +parse_comma (char **pdata, size_t * pdata_len, char *obuf, size_t obuf_len) +{ + char buf[64]; + char *data = *pdata; + size_t data_len = *pdata_len; + size_t buf_len = sizeof (buf) - 1; + char *ptr; + + memset (obuf, 0, obuf_len); + + buf[0] = 0; + + for (ptr = buf; (*data != ',') && data_len; ptr++, data++, data_len--) + { + if (buf_len) + { + *ptr = *data; + *(ptr + 1) = 0; + buf_len--; + } + } + + if (*data == ',') + { + data++; + data_len--; + } + + strncpy (obuf, buf, obuf_len - 1); + + *pdata_len = data_len; + *pdata = data; +} + + + +static void ICACHE_FLASH_ATTR +msg_recv (void *arg, char *data, unsigned short _data_len) +{ + char sa[6], sb[6], sc[6], sd[6], se[6]; + size_t data_len = data_len; + + if (!data_len) + return; + + + parse_comma (&data, &data_len, sa, sizeof (sa)); + parse_comma (&data, &data_len, sb, sizeof (sb)); + parse_comma (&data, &data_len, sc, sizeof (sc)); + parse_comma (&data, &data_len, sd, sizeof (sd)); + parse_comma (&data, &data_len, se, sizeof (se)); + + + os_printf("A->%s B->%s C->%s D->%s E->%s\n",sa,sb,sc,sd,se); + + + if (*sb) + parse_field (sa, &a); + if (*sc) + parse_field (sb, &b); + if (*sd) + parse_field (sc, &c); + if (*se) + parse_field (sd, &d); + + os_printf("a->%d b->%d c->%d d->%d\n",a,b,c,d); + + gpio_write (a, b, c, d); + + +} + + +void ICACHE_FLASH_ATTR +msg_init (void) +{ + udp.type = ESPCONN_UDP; + udp.proto.udp = (esp_udp *) os_zalloc (sizeof (esp_udp)); + udp.proto.udp->local_port = 29153; + espconn_create (&udp); + espconn_regist_recvcb (&udp, msg_recv); +} diff --git a/polycom_recv/platform.c b/polycom_recv/platform.c new file mode 100644 index 0000000..ff275dc --- /dev/null +++ b/polycom_recv/platform.c @@ -0,0 +1,1498 @@ +#include "project.h" + + + +void ICACHE_FLASH_ATTR +user_esp_platform_init(void) +{ + + os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ + IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); + os_printf("IOT VERSION = %s\n",iot_version); + + system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); + + struct rst_info *rtc_info = system_get_rst_info(); + + os_printf("reset reason: %x\n", rtc_info->reason); + + if (rtc_info->reason == REASON_WDT_RST || + rtc_info->reason == REASON_EXCEPTION_RST || + rtc_info->reason == REASON_SOFT_WDT_RST) { + if (rtc_info->reason == REASON_EXCEPTION_RST) { + os_printf("Fatal exception (%d):\n", rtc_info->exccause); + } + os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", + rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); + } + + /***add by tzx for saving ip_info to avoid dhcp_client start****/ + struct dhcp_client_info dhcp_info; + struct ip_info sta_info; + system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); + if(dhcp_info.flag == 0x01 ) { + if (true == wifi_station_dhcpc_status()) + { + wifi_station_dhcpc_stop(); + } + sta_info.ip = dhcp_info.ip_addr; + sta_info.gw = dhcp_info.gw; + sta_info.netmask = dhcp_info.netmask; + if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { + os_printf("set default ip wrong\n"); + } + } + os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); + system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); + + +#if AP_CACHE + wifi_station_ap_number_set(AP_CACHE_NUMBER); +#endif + +#if 0 + { + char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; + char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; + struct ip_info info; + + wifi_set_macaddr(SOFTAP_IF, sofap_mac); + wifi_set_macaddr(STATION_IF, sta_mac); + + IP4_ADDR(&info.ip, 192, 168, 3, 200); + IP4_ADDR(&info.gw, 192, 168, 3, 1); + IP4_ADDR(&info.netmask, 255, 255, 255, 0); + wifi_set_ip_info(STATION_IF, &info); + + IP4_ADDR(&info.ip, 10, 10, 10, 1); + IP4_ADDR(&info.gw, 10, 10, 10, 1); + IP4_ADDR(&info.netmask, 255, 255, 255, 0); + wifi_set_ip_info(SOFTAP_IF, &info); + } +#endif + + if (esp_param.activeflag != 1) { +#ifdef SOFTAP_ENCRYPT + struct softap_config config; + char password[33]; + char macaddr[6]; + + wifi_softap_get_config(&config); + wifi_get_macaddr(SOFTAP_IF, macaddr); + + os_memset(config.password, 0, sizeof(config.password)); + os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); + os_memcpy(config.password, password, os_strlen(password)); + config.authmode = AUTH_WPA_WPA2_PSK; + + wifi_softap_set_config(&config); +#endif + + wifi_set_opmode(STATIONAP_MODE); + } + +#if PLUG_DEVICE + user_plug_init(); +#elif LIGHT_DEVICE + user_light_init(); +#elif SENSOR_DEVICE + user_sensor_init(esp_param.activeflag); +#endif + + if (wifi_get_opmode() != SOFTAP_MODE) { + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); + os_timer_arm(&client_timer, 100, 0); + } +} + + + + + + +#if 0 +/****************************************************************************** + * Copyright 2013-2014 Espressif Systems (Wuxi) + * + * FileName: esp_platform.c + * + * Description: The client mode configration. + * Check your hardware connection with the host while use this mode. + * + * Modification history: + * 2014/5/09, v1.0 create this file. +*******************************************************************************/ +#include "ets_sys.h" +#include "os_type.h" +#include "mem.h" +#include "osapi.h" +#include "user_interface.h" + +#include "espconn.h" +#include "esp_platform.h" +#include "iot_version.h" +#include "upgrade.h" + +#if ESP_PLATFORM + +#define ESP_DEBUG + +#ifdef ESP_DEBUG +#define ESP_DBG os_printf +#else +#define ESP_DBG +#endif + +#define ACTIVE_FRAME "{\"nonce\": %d,\"path\": \"/v1/device/activate/\", \"method\": \"POST\", \"body\": {\"encrypt_method\": \"PLAIN\", \"token\": \"%s\", \"bssid\": \""MACSTR"\",\"rom_version\":\"%s\"}, \"meta\": {\"Authorization\": \"token %s\"}}\n" + +#if PLUG_DEVICE +#include "plug.h" + +#define RESPONSE_FRAME "{\"status\": 200, \"datapoint\": {\"x\": %d}, \"nonce\": %d, \"deliver_to_device\": true}\n" +#define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n" + +#elif LIGHT_DEVICE +#include "light.h" + +#define RESPONSE_FRAME "{\"status\": 200,\"nonce\": %d, \"datapoint\": {\"x\": %d,\"y\": %d,\"z\": %d,\"k\": %d,\"l\": %d},\"deliver_to_device\":true}\n" +#define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n" + +#elif SENSOR_DEVICE +#include "sensor.h" + +#if HUMITURE_SUB_DEVICE +#define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/tem_hum/datapoint/\", \"method\": \"POST\", \ +\"body\": {\"datapoint\": {\"x\": %s%d.%02d,\"y\": %d.%02d}}, \"meta\": {\"Authorization\": \"token %s\"}}\n" +#elif FLAMMABLE_GAS_SUB_DEVICE +#define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/flammable_gas/datapoint/\", \"method\": \"POST\", \ +\"body\": {\"datapoint\": {\"x\": %d.%03d}}, \"meta\": {\"Authorization\": \"token %s\"}}\n" +#endif + +LOCAL uint32 count = 0; +#endif + +#define UPGRADE_FRAME "{\"path\": \"/v1/messages/\", \"method\": \"POST\", \"meta\": {\"Authorization\": \"token %s\"},\ +\"get\":{\"action\":\"%s\"},\"body\":{\"pre_rom_version\":\"%s\",\"rom_version\":\"%s\"}}\n" + +#if PLUG_DEVICE || LIGHT_DEVICE +#define BEACON_FRAME "{\"path\": \"/v1/ping/\", \"method\": \"POST\",\"meta\": {\"Authorization\": \"token %s\"}}\n" +#define RPC_RESPONSE_FRAME "{\"status\": 200, \"nonce\": %d, \"deliver_to_device\": true}\n" +#define TIMER_FRAME "{\"body\": {}, \"get\":{\"is_humanize_format_simple\":\"true\"},\"meta\": {\"Authorization\": \"Token %s\"},\"path\": \"/v1/device/timers/\",\"post\":{},\"method\": \"GET\"}\n" +#define pheadbuffer "Connection: keep-alive\r\n\ +Cache-Control: no-cache\r\n\ +User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 \r\n\ +Accept: */*\r\n\ +Authorization: token %s\r\n\ +Accept-Encoding: gzip,deflate,sdch\r\n\ +Accept-Language: zh-CN,zh;q=0.8\r\n\r\n" + +LOCAL uint8 ping_status; +LOCAL os_timer_t beacon_timer; +#endif + +#ifdef USE_DNS +ip_addr_t esp_server_ip; +#endif + +LOCAL struct espconn user_conn; +LOCAL struct _esp_tcp user_tcp; +LOCAL os_timer_t client_timer; + struct esp_platform_saved_param esp_param; +LOCAL uint8 device_status; +LOCAL uint8 device_recon_count = 0; +LOCAL uint32 active_nonce = 0; +LOCAL uint8 iot_version[20] = {0}; +struct rst_info rtc_info; +void user_esp_platform_check_ip(uint8 reset_flag); + +/****************************************************************************** + * FunctionName : user_esp_platform_get_token + * Description : get the espressif's device token + * Parameters : token -- the parame point which write the flash + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_get_token(uint8_t *token) +{ + if (token == NULL) { + return; + } + + os_memcpy(token, esp_param.token, sizeof(esp_param.token)); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_set_token + * Description : save the token for the espressif's device + * Parameters : token -- the parame point which write the flash + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_set_token(uint8_t *token) +{ + if (token == NULL) { + return; + } + + esp_param.activeflag = 0; + os_memcpy(esp_param.token, token, os_strlen(token)); + + system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param)); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_set_active + * Description : set active flag + * Parameters : activeflag -- 0 or 1 + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_set_active(uint8 activeflag) +{ + esp_param.activeflag = activeflag; + + system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param)); +} + +void ICACHE_FLASH_ATTR +user_esp_platform_set_connect_status(uint8 status) +{ + device_status = status; +} + +/****************************************************************************** + * FunctionName : user_esp_platform_get_connect_status + * Description : get each connection step's status + * Parameters : none + * Returns : status +*******************************************************************************/ +uint8 ICACHE_FLASH_ATTR +user_esp_platform_get_connect_status(void) +{ + uint8 status = wifi_station_get_connect_status(); + + if (status == STATION_GOT_IP) { + status = (device_status == 0) ? DEVICE_CONNECTING : device_status; + } + + ESP_DBG("status %d\n", status); + return status; +} + +/****************************************************************************** + * FunctionName : user_esp_platform_parse_nonce + * Description : parse the device nonce + * Parameters : pbuffer -- the recivce data point + * Returns : the nonce +*******************************************************************************/ +int ICACHE_FLASH_ATTR +user_esp_platform_parse_nonce(char *pbuffer) +{ + char *pstr = NULL; + char *pparse = NULL; + char noncestr[11] = {0}; + int nonce = 0; + pstr = (char *)os_strstr(pbuffer, "\"nonce\": "); + + if (pstr != NULL) { + pstr += 9; + pparse = (char *)os_strstr(pstr, ","); + + if (pparse != NULL) { + os_memcpy(noncestr, pstr, pparse - pstr); + } else { + pparse = (char *)os_strstr(pstr, "}"); + + if (pparse != NULL) { + os_memcpy(noncestr, pstr, pparse - pstr); + } else { + pparse = (char *)os_strstr(pstr, "]"); + + if (pparse != NULL) { + os_memcpy(noncestr, pstr, pparse - pstr); + } else { + return 0; + } + } + } + + nonce = atoi(noncestr); + } + + return nonce; +} + +/****************************************************************************** + * FunctionName : user_esp_platform_get_info + * Description : get and update the espressif's device status + * Parameters : pespconn -- the espconn used to connect with host + * pbuffer -- prossing the data point + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_get_info(struct espconn *pconn, uint8 *pbuffer) +{ + char *pbuf = NULL; + int nonce = 0; + + pbuf = (char *)os_zalloc(packet_size); + + nonce = user_esp_platform_parse_nonce(pbuffer); + + if (pbuf != NULL) { +#if PLUG_DEVICE + os_sprintf(pbuf, RESPONSE_FRAME, user_plug_get_status(), nonce); +#elif LIGHT_DEVICE + uint32 white_val; + white_val = (PWM_CHANNEL>LIGHT_COLD_WHITE?user_light_get_duty(LIGHT_COLD_WHITE):0); + os_sprintf(pbuf, RESPONSE_FRAME, nonce, user_light_get_period(), + user_light_get_duty(LIGHT_RED), user_light_get_duty(LIGHT_GREEN), + user_light_get_duty(LIGHT_BLUE),white_val );//50); +#endif + + ESP_DBG("%s\n", pbuf); +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pconn, pbuf, os_strlen(pbuf)); +#endif + os_free(pbuf); + pbuf = NULL; + } +} + +/****************************************************************************** + * FunctionName : user_esp_platform_set_info + * Description : prossing the data and controling the espressif's device + * Parameters : pespconn -- the espconn used to connect with host + * pbuffer -- prossing the data point + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_set_info(struct espconn *pconn, uint8 *pbuffer) +{ +#if PLUG_DEVICE + char *pstr = NULL; + pstr = (char *)os_strstr(pbuffer, "plug-status"); + + if (pstr != NULL) { + pstr = (char *)os_strstr(pbuffer, "body"); + + if (pstr != NULL) { + + if (os_strncmp(pstr + 27, "1", 1) == 0) { + user_plug_set_status(0x01); + } else if (os_strncmp(pstr + 27, "0", 1) == 0) { + user_plug_set_status(0x00); + } + } + } + +#elif LIGHT_DEVICE + char *pstr = NULL; + char *pdata = NULL; + char *pbuf = NULL; + char recvbuf[10]; + uint16 length = 0; + uint32 data = 0; + static uint32 rr,gg,bb,cw,ww,period; + ww=0; + cw=0; + extern uint8 light_sleep_flg; + pstr = (char *)os_strstr(pbuffer, "\"path\": \"/v1/datastreams/light/datapoint/\""); + + if (pstr != NULL) { + pstr = (char *)os_strstr(pbuffer, "{\"datapoint\": "); + + if (pstr != NULL) { + pbuf = (char *)os_strstr(pbuffer, "}}"); + length = pbuf - pstr; + length += 2; + pdata = (char *)os_zalloc(length + 1); + os_memcpy(pdata, pstr, length); + + pstr = (char *)os_strchr(pdata, 'x'); + + if (pstr != NULL) { + pstr += 4; + pbuf = (char *)os_strchr(pstr, ','); + + if (pbuf != NULL) { + length = pbuf - pstr; + os_memset(recvbuf, 0, 10); + os_memcpy(recvbuf, pstr, length); + data = atoi(recvbuf); + period = data; + //user_light_set_period(data); + } + } + + pstr = (char *)os_strchr(pdata, 'y'); + + if (pstr != NULL) { + pstr += 4; + pbuf = (char *)os_strchr(pstr, ','); + + if (pbuf != NULL) { + length = pbuf - pstr; + os_memset(recvbuf, 0, 10); + os_memcpy(recvbuf, pstr, length); + data = atoi(recvbuf); + rr=data; + os_printf("r: %d\r\n",rr); + //user_light_set_duty(data, 0); + } + } + + pstr = (char *)os_strchr(pdata, 'z'); + + if (pstr != NULL) { + pstr += 4; + pbuf = (char *)os_strchr(pstr, ','); + + if (pbuf != NULL) { + length = pbuf - pstr; + os_memset(recvbuf, 0, 10); + os_memcpy(recvbuf, pstr, length); + data = atoi(recvbuf); + gg=data; + os_printf("g: %d\r\n",gg); + //user_light_set_duty(data, 1); + } + } + + pstr = (char *)os_strchr(pdata, 'k'); + + if (pstr != NULL) { + pstr += 4;; + pbuf = (char *)os_strchr(pstr, ','); + + if (pbuf != NULL) { + length = pbuf - pstr; + os_memset(recvbuf, 0, 10); + os_memcpy(recvbuf, pstr, length); + data = atoi(recvbuf); + bb=data; + os_printf("b: %d\r\n",bb); + //user_light_set_duty(data, 2); + } + } + + pstr = (char *)os_strchr(pdata, 'l'); + + if (pstr != NULL) { + pstr += 4;; + pbuf = (char *)os_strchr(pstr, ','); + + if (pbuf != NULL) { + length = pbuf - pstr; + os_memset(recvbuf, 0, 10); + os_memcpy(recvbuf, pstr, length); + data = atoi(recvbuf); + cw=data; + ww=data; + os_printf("cw: %d\r\n",cw); + os_printf("ww:%d\r\n",ww); //chg + //user_light_set_duty(data, 2); + } + } + + os_free(pdata); + } + } + + if((rr|gg|bb|cw|ww) == 0){ + if(light_sleep_flg==0){ + + } + + }else{ + if(light_sleep_flg==1){ + os_printf("modem sleep en\r\n"); + wifi_set_sleep_type(MODEM_SLEEP_T); + light_sleep_flg =0; + } + } + + light_set_aim(rr,gg,bb,cw,ww,period); + //user_light_restart(); + +#endif + + user_esp_platform_get_info(pconn, pbuffer); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_reconnect + * Description : reconnect with host after get ip + * Parameters : pespconn -- the espconn used to reconnect with host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_reconnect(struct espconn *pespconn) +{ + ESP_DBG("user_esp_platform_reconnect\n"); + + user_esp_platform_check_ip(0); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_discon_cb + * Description : disconnect successfully with the host + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_discon_cb(void *arg) +{ + struct espconn *pespconn = arg; + struct ip_info ipconfig; + struct dhcp_client_info dhcp_info; + ESP_DBG("user_esp_platform_discon_cb\n"); + +#if (PLUG_DEVICE || LIGHT_DEVICE) + os_timer_disarm(&beacon_timer); +#endif + + if (pespconn == NULL) { + return; + } + + pespconn->proto.tcp->local_port = espconn_port(); + +#if (PLUG_DEVICE || SENSOR_DEVICE) + user_link_led_output(1); +#endif + +#if SENSOR_DEVICE +#ifdef SENSOR_DEEP_SLEEP + + if (wifi_get_opmode() == STATION_MODE) { + /***add by tzx for saving ip_info to avoid dhcp_client start****/ + wifi_get_ip_info(STATION_IF, &ipconfig); + + dhcp_info.ip_addr = ipconfig.ip; + dhcp_info.netmask = ipconfig.netmask; + dhcp_info.gw = ipconfig.gw ; + dhcp_info.flag = 0x01; + os_printf("dhcp_info.ip_addr = %d\n",dhcp_info.ip_addr); + system_rtc_mem_write(64,&dhcp_info,sizeof(struct dhcp_client_info)); + user_sensor_deep_sleep_enter(); + } else { + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn); + os_timer_arm(&client_timer, SENSOR_DEEP_SLEEP_TIME / 1000, 0); + } + +#else + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn); + os_timer_arm(&client_timer, 1000, 0); +#endif +#else + user_esp_platform_reconnect(pespconn); +#endif +} + +/****************************************************************************** + * FunctionName : user_esp_platform_discon + * Description : A new incoming connection has been disconnected. + * Parameters : espconn -- the espconn used to disconnect with host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_discon(struct espconn *pespconn) +{ + ESP_DBG("user_esp_platform_discon\n"); + +#if (PLUG_DEVICE || SENSOR_DEVICE) + user_link_led_output(1); +#endif + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_disconnect(pespconn); +#else + espconn_disconnect(pespconn); +#endif +} + +/****************************************************************************** + * FunctionName : user_esp_platform_sent_cb + * Description : Data has been sent successfully and acknowledged by the remote host. + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_sent_cb(void *arg) +{ + struct espconn *pespconn = arg; + + ESP_DBG("user_esp_platform_sent_cb\n"); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_sent + * Description : Processing the application data and sending it to the host + * Parameters : pespconn -- the espconn used to connetion with the host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_sent(struct espconn *pespconn) +{ + uint8 devkey[token_size] = {0}; + uint32 nonce; + char *pbuf = (char *)os_zalloc(packet_size); + + os_memcpy(devkey, esp_param.devkey, 40); + + if (esp_param.activeflag == 0xFF) { + esp_param.activeflag = 0; + } + + if (pbuf != NULL) { + if (esp_param.activeflag == 0) { + uint8 token[token_size] = {0}; + uint8 bssid[6]; + active_nonce = os_random(); + + os_memcpy(token, esp_param.token, 40); + + wifi_get_macaddr(STATION_IF, bssid); + + os_sprintf(pbuf, ACTIVE_FRAME, active_nonce, token, MAC2STR(bssid),iot_version, devkey); + } + +#if SENSOR_DEVICE +#if HUMITURE_SUB_DEVICE + else { +#if 0 + uint16 tp, rh; + uint8 data[4]; + + if (user_mvh3004_read_th(data)) { + rh = data[0] << 8 | data[1]; + tp = data[2] << 8 | data[3]; + } + +#else + uint16 tp, rh; + uint8 *data; + uint32 tp_t, rh_t; + data = (uint8 *)user_mvh3004_get_poweron_th(); + + rh = data[0] << 8 | data[1]; + tp = data[2] << 8 | data[3]; +#endif + tp_t = (tp >> 2) * 165 * 100 / (16384 - 1); + rh_t = (rh & 0x3fff) * 100 * 100 / (16384 - 1); + + if (tp_t >= 4000) { + os_sprintf(pbuf, UPLOAD_FRAME, count, "", tp_t / 100 - 40, tp_t % 100, rh_t / 100, rh_t % 100, devkey); + } else { + tp_t = 4000 - tp_t; + os_sprintf(pbuf, UPLOAD_FRAME, count, "-", tp_t / 100, tp_t % 100, rh_t / 100, rh_t % 100, devkey); + } + } + +#elif FLAMMABLE_GAS_SUB_DEVICE + else { + uint32 adc_value = system_adc_read(); + + os_sprintf(pbuf, UPLOAD_FRAME, count, adc_value / 1024, adc_value * 1000 / 1024, devkey); + } + +#endif +#else + else { + nonce = os_random(); + os_sprintf(pbuf, FIRST_FRAME, nonce , devkey); + } + +#endif + ESP_DBG("%s\n", pbuf); + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + + os_free(pbuf); + } +} + +#if PLUG_DEVICE || LIGHT_DEVICE +/****************************************************************************** + * FunctionName : user_esp_platform_sent_beacon + * Description : sent beacon frame for connection with the host is activate + * Parameters : pespconn -- the espconn used to connetion with the host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_sent_beacon(struct espconn *pespconn) +{ + if (pespconn == NULL) { + return; + } + + if (pespconn->state == ESPCONN_CONNECT) { + if (esp_param.activeflag == 0) { + ESP_DBG("please check device is activated.\n"); + user_esp_platform_sent(pespconn); + } else { + uint8 devkey[token_size] = {0}; + os_memcpy(devkey, esp_param.devkey, 40); + + ESP_DBG("user_esp_platform_sent_beacon %u\n", system_get_time()); + + if (ping_status == 0) { + ESP_DBG("user_esp_platform_sent_beacon sent fail!\n"); + user_esp_platform_discon(pespconn); + } else { + char *pbuf = (char *)os_zalloc(packet_size); + + if (pbuf != NULL) { + os_sprintf(pbuf, BEACON_FRAME, devkey); + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + + ping_status = 0; + os_timer_arm(&beacon_timer, BEACON_TIME, 0); + os_free(pbuf); + } + } + } + } else { + ESP_DBG("user_esp_platform_sent_beacon sent fail!\n"); + user_esp_platform_discon(pespconn); + } +} + +/****************************************************************************** + * FunctionName : user_platform_rpc_set_rsp + * Description : response the message to server to show setting info is received + * Parameters : pespconn -- the espconn used to connetion with the host + * nonce -- mark the message received from server + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_platform_rpc_set_rsp(struct espconn *pespconn, int nonce) +{ + char *pbuf = (char *)os_zalloc(packet_size); + + if (pespconn == NULL) { + return; + } + + os_sprintf(pbuf, RPC_RESPONSE_FRAME, nonce); + ESP_DBG("%s\n", pbuf); +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + os_free(pbuf); +} + +/****************************************************************************** + * FunctionName : user_platform_timer_get + * Description : get the timers from server + * Parameters : pespconn -- the espconn used to connetion with the host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_platform_timer_get(struct espconn *pespconn) +{ + uint8 devkey[token_size] = {0}; + char *pbuf = (char *)os_zalloc(packet_size); + os_memcpy(devkey, esp_param.devkey, 40); + + if (pespconn == NULL) { + return; + } + + os_sprintf(pbuf, TIMER_FRAME, devkey); + ESP_DBG("%s\n", pbuf); +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + os_free(pbuf); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_upgrade_cb + * Description : Processing the downloaded data from the server + * Parameters : pespconn -- the espconn used to connetion with the host + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_upgrade_rsp(void *arg) +{ + struct upgrade_server_info *server = arg; + struct espconn *pespconn = server->pespconn; + uint8 devkey[41] = {0}; + uint8 *pbuf = NULL; + char *action = NULL; + + os_memcpy(devkey, esp_param.devkey, 40); + pbuf = (char *)os_zalloc(packet_size); + + if (server->upgrade_flag == true) { + ESP_DBG("user_esp_platform_upgarde_successfully\n"); + action = "device_upgrade_success"; + os_sprintf(pbuf, UPGRADE_FRAME, devkey, action, server->pre_version, server->upgrade_version); + ESP_DBG("%s\n",pbuf); + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + + if (pbuf != NULL) { + os_free(pbuf); + pbuf = NULL; + } + } else { + ESP_DBG("user_esp_platform_upgrade_failed\n"); + action = "device_upgrade_failed"; + os_sprintf(pbuf, UPGRADE_FRAME, devkey, action,server->pre_version, server->upgrade_version); + ESP_DBG("%s\n",pbuf); + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf)); +#else + espconn_sent(pespconn, pbuf, os_strlen(pbuf)); +#endif + + if (pbuf != NULL) { + os_free(pbuf); + pbuf = NULL; + } + } + + os_free(server->url); + server->url = NULL; + os_free(server); + server = NULL; +} + +/****************************************************************************** + * FunctionName : user_esp_platform_upgrade_begin + * Description : Processing the received data from the server + * Parameters : pespconn -- the espconn used to connetion with the host + * server -- upgrade param + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_upgrade_begin(struct espconn *pespconn, struct upgrade_server_info *server) +{ + uint8 user_bin[9] = {0}; + uint8 devkey[41] = {0}; + + server->pespconn = pespconn; + + os_memcpy(devkey, esp_param.devkey, 40); + os_memcpy(server->ip, pespconn->proto.tcp->remote_ip, 4); + +#ifdef UPGRADE_SSL_ENABLE + server->port = 443; +#else + server->port = 80; +#endif + + server->check_cb = user_esp_platform_upgrade_rsp; + server->check_times = 120000; + + if (server->url == NULL) { + server->url = (uint8 *)os_zalloc(512); + } + + if (system_upgrade_userbin_check() == UPGRADE_FW_BIN1) { + os_memcpy(user_bin, "user2.bin", 10); + } else if (system_upgrade_userbin_check() == UPGRADE_FW_BIN2) { + os_memcpy(user_bin, "user1.bin", 10); + } + + os_sprintf(server->url, "GET /v1/device/rom/?action=download_rom&version=%s&filename=%s HTTP/1.0\r\nHost: "IPSTR":%d\r\n"pheadbuffer"", + server->upgrade_version, user_bin, IP2STR(server->ip), + server->port, devkey); + ESP_DBG("%s\n",server->url); + +#ifdef UPGRADE_SSL_ENABLE + + if (system_upgrade_start_ssl(server) == false) { +#else + + if (system_upgrade_start(server) == false) { +#endif + ESP_DBG("upgrade is already started\n"); + } +} +#endif + +/****************************************************************************** + * FunctionName : user_esp_platform_recv_cb + * Description : Processing the received data from the server + * Parameters : arg -- Additional argument to pass to the callback function + * pusrdata -- The received data (or NULL when the connection has been closed!) + * length -- The length of received data + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_recv_cb(void *arg, char *pusrdata, unsigned short length) +{ + char *pstr = NULL; + LOCAL char pbuffer[1024 * 2] = {0}; + struct espconn *pespconn = arg; + + ESP_DBG("user_esp_platform_recv_cb %s\n", pusrdata); + +#if (PLUG_DEVICE || LIGHT_DEVICE) + os_timer_disarm(&beacon_timer); +#endif + + if (length == 1460) { + os_memcpy(pbuffer, pusrdata, length); + } else { + struct espconn *pespconn = (struct espconn *)arg; + + os_memcpy(pbuffer + os_strlen(pbuffer), pusrdata, length); + + if ((pstr = (char *)os_strstr(pbuffer, "\"activate_status\": ")) != NULL && + user_esp_platform_parse_nonce(pbuffer) == active_nonce) { + if (os_strncmp(pstr + 19, "1", 1) == 0) { + ESP_DBG("device activates successful.\n"); + + device_status = DEVICE_ACTIVE_DONE; + esp_param.activeflag = 1; + system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param)); + user_esp_platform_sent(pespconn); + if(LIGHT_DEVICE){ + system_restart(); + } + } else { + ESP_DBG("device activates failed.\n"); + device_status = DEVICE_ACTIVE_FAIL; + } + } + +#if (PLUG_DEVICE || LIGHT_DEVICE) + else if ((pstr = (char *)os_strstr(pbuffer, "\"action\": \"sys_upgrade\"")) != NULL) { + if ((pstr = (char *)os_strstr(pbuffer, "\"version\":")) != NULL) { + struct upgrade_server_info *server = NULL; + int nonce = user_esp_platform_parse_nonce(pbuffer); + user_platform_rpc_set_rsp(pespconn, nonce); + + server = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info)); + os_memcpy(server->upgrade_version, pstr + 12, 16); + server->upgrade_version[15] = '\0'; + os_sprintf(server->pre_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ + IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); + user_esp_platform_upgrade_begin(pespconn, server); + } + } else if ((pstr = (char *)os_strstr(pbuffer, "\"action\": \"sys_reboot\"")) != NULL) { + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)system_upgrade_reboot, NULL); + os_timer_arm(&client_timer, 1000, 0); + } else if ((pstr = (char *)os_strstr(pbuffer, "/v1/device/timers/")) != NULL) { + int nonce = user_esp_platform_parse_nonce(pbuffer); + user_platform_rpc_set_rsp(pespconn, nonce); + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_platform_timer_get, pespconn); + os_timer_arm(&client_timer, 2000, 0); + } else if ((pstr = (char *)os_strstr(pbuffer, "\"method\": ")) != NULL) { + if (os_strncmp(pstr + 11, "GET", 3) == 0) { + user_esp_platform_get_info(pespconn, pbuffer); + } else if (os_strncmp(pstr + 11, "POST", 4) == 0) { + user_esp_platform_set_info(pespconn, pbuffer); + } + } else if ((pstr = (char *)os_strstr(pbuffer, "ping success")) != NULL) { + ESP_DBG("ping success\n"); + ping_status = 1; + } else if ((pstr = (char *)os_strstr(pbuffer, "send message success")) != NULL) { + } else if ((pstr = (char *)os_strstr(pbuffer, "timers")) != NULL) { + user_platform_timer_start(pusrdata , pespconn); + } + +#elif SENSOR_DEVICE + else if ((pstr = (char *)os_strstr(pbuffer, "\"status\":")) != NULL) { + if (os_strncmp(pstr + 10, "200", 3) != 0) { + ESP_DBG("message upload failed.\n"); + } else { + count++; + ESP_DBG("message upload sucessful.\n"); + } + + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_discon, pespconn); + os_timer_arm(&client_timer, 10, 0); + } + +#endif + else if ((pstr = (char *)os_strstr(pbuffer, "device")) != NULL) { +#if PLUG_DEVICE || LIGHT_DEVICE + user_platform_timer_get(pespconn); +#elif SENSOR_DEVICE + +#endif + } + + os_memset(pbuffer, 0, sizeof(pbuffer)); + } + +#if (PLUG_DEVICE || LIGHT_DEVICE) + os_timer_arm(&beacon_timer, BEACON_TIME, 0); +#endif +} + +#if AP_CACHE +/****************************************************************************** + * FunctionName : user_esp_platform_ap_change + * Description : add the user interface for changing to next ap ID. + * Parameters : + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_ap_change(void) +{ + uint8 current_id; + uint8 i = 0; + ESP_DBG("user_esp_platform_ap_is_changing\n"); + + current_id = wifi_station_get_current_ap_id(); + ESP_DBG("current ap id =%d\n", current_id); + + if (current_id == AP_CACHE_NUMBER - 1) { + i = 0; + } else { + i = current_id + 1; + } + while (wifi_station_ap_change(i) != true) { + i++; + if (i == AP_CACHE_NUMBER - 1) { + i = 0; + } + } + + /* just need to re-check ip while change AP */ + device_recon_count = 0; + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL); + os_timer_arm(&client_timer, 100, 0); +} +#endif + +LOCAL bool ICACHE_FLASH_ATTR +user_esp_platform_reset_mode(void) +{ + if (wifi_get_opmode() == STATION_MODE) { + wifi_set_opmode(STATIONAP_MODE); + } + +#if AP_CACHE + /* delay 5s to change AP */ + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_ap_change, NULL); + os_timer_arm(&client_timer, 5000, 0); + + return true; +#endif + + return false; +} + +/****************************************************************************** + * FunctionName : user_esp_platform_recon_cb + * Description : The connection had an error and is already deallocated. + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_recon_cb(void *arg, sint8 err) +{ + struct espconn *pespconn = (struct espconn *)arg; + + ESP_DBG("user_esp_platform_recon_cb\n"); + +#if (PLUG_DEVICE || LIGHT_DEVICE) + os_timer_disarm(&beacon_timer); +#endif + +#if (PLUG_DEVICE || SENSOR_DEVICE) + user_link_led_output(1); +#endif + + if (++device_recon_count == 5) { + device_status = DEVICE_CONNECT_SERVER_FAIL; + + if (user_esp_platform_reset_mode()) { + return; + } + } + +#if SENSOR_DEVICE +#ifdef SENSOR_DEEP_SLEEP + + if (wifi_get_opmode() == STATION_MODE) { + user_esp_platform_reset_mode(); + + //user_sensor_deep_sleep_enter(); + } else { + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn); + os_timer_arm(&client_timer, 1000, 0); + } + +#else + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn); + os_timer_arm(&client_timer, 1000, 0); +#endif +#else + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn); + os_timer_arm(&client_timer, 1000, 0); +#endif +} + +/****************************************************************************** + * FunctionName : user_esp_platform_connect_cb + * Description : A new incoming connection has been connected. + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_connect_cb(void *arg) +{ + struct espconn *pespconn = arg; + + ESP_DBG("user_esp_platform_connect_cb\n"); + if (wifi_get_opmode() == STATIONAP_MODE ) { + wifi_set_opmode(STATION_MODE); + } + +#if (PLUG_DEVICE || SENSOR_DEVICE) + user_link_led_timer_done(); +#endif + device_recon_count = 0; + espconn_regist_recvcb(pespconn, user_esp_platform_recv_cb); + espconn_regist_sentcb(pespconn, user_esp_platform_sent_cb); + user_esp_platform_sent(pespconn); +} + +/****************************************************************************** + * FunctionName : user_esp_platform_connect + * Description : The function given as the connect with the host + * Parameters : espconn -- the espconn used to connect the connection + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_connect(struct espconn *pespconn) +{ + ESP_DBG("user_esp_platform_connect\n"); + +#ifdef CLIENT_SSL_ENABLE + espconn_secure_connect(pespconn); +#else + espconn_connect(pespconn); +#endif +} + +#ifdef USE_DNS +/****************************************************************************** + * FunctionName : user_esp_platform_dns_found + * Description : dns found callback + * Parameters : name -- pointer to the name that was looked up. + * ipaddr -- pointer to an ip_addr_t containing the IP address of + * the hostname, or NULL if the name could not be found (or on any + * other error). + * callback_arg -- a user-specified callback argument passed to + * dns_gethostbyname + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) +{ + struct espconn *pespconn = (struct espconn *)arg; + + if (ipaddr == NULL) { + ESP_DBG("user_esp_platform_dns_found NULL\n"); + + if (++device_recon_count == 5) { + device_status = DEVICE_CONNECT_SERVER_FAIL; + + user_esp_platform_reset_mode(); + } + + return; + } + + ESP_DBG("user_esp_platform_dns_found %d.%d.%d.%d\n", + *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1), + *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3)); + + if (esp_server_ip.addr == 0 && ipaddr->addr != 0) { + os_timer_disarm(&client_timer); + esp_server_ip.addr = ipaddr->addr; + os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4); + + pespconn->proto.tcp->local_port = espconn_port(); + +#ifdef CLIENT_SSL_ENABLE + pespconn->proto.tcp->remote_port = 8443; +#else + pespconn->proto.tcp->remote_port = 8000; +#endif + +#if (PLUG_DEVICE || LIGHT_DEVICE) + ping_status = 1; +#endif + + espconn_regist_connectcb(pespconn, user_esp_platform_connect_cb); + espconn_regist_disconcb(pespconn, user_esp_platform_discon_cb); + espconn_regist_reconcb(pespconn, user_esp_platform_recon_cb); + user_esp_platform_connect(pespconn); + } +} + +/****************************************************************************** + * FunctionName : user_esp_platform_dns_check_cb + * Description : 1s time callback to check dns found + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_dns_check_cb(void *arg) +{ + struct espconn *pespconn = arg; + + ESP_DBG("user_esp_platform_dns_check_cb\n"); + + espconn_gethostbyname(pespconn, ESP_DOMAIN, &esp_server_ip, user_esp_platform_dns_found); + + os_timer_arm(&client_timer, 1000, 0); +} + +LOCAL void ICACHE_FLASH_ATTR +user_esp_platform_start_dns(struct espconn *pespconn) +{ + esp_server_ip.addr = 0; + espconn_gethostbyname(pespconn, ESP_DOMAIN, &esp_server_ip, user_esp_platform_dns_found); + + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_dns_check_cb, pespconn); + os_timer_arm(&client_timer, 1000, 0); +} +#endif + + +#if LIGHT_DEVICE +void user_mdns_conf() +{ + +struct ip_info ipconfig; +wifi_get_ip_info(STATION_IF, &ipconfig); + +struct mdns_info *info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info)); +info->host_name = "espressif_light_demo"; +info->ipAddr= ipconfig.ip.addr; //sation ip +info->server_name = "espLight"; +info->server_port = 80; +info->txt_data[0] = "version = 1.0.1"; +espconn_mdns_init(info); + + +} +#endif + +/****************************************************************************** + * FunctionName : user_esp_platform_check_ip + * Description : espconn struct parame init when get ip addr + * Parameters : none + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_check_ip(uint8 reset_flag) +{ + struct ip_info ipconfig; + + os_timer_disarm(&client_timer); + + wifi_get_ip_info(STATION_IF, &ipconfig); + + if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) { +#if (PLUG_DEVICE || SENSOR_DEVICE) + user_link_led_timer_init(); +#endif + +//*************************** +#if LIGHT_DEVICE + user_mdns_conf(); +#endif +//*************************** + user_conn.proto.tcp = &user_tcp; + user_conn.type = ESPCONN_TCP; + user_conn.state = ESPCONN_NONE; + + device_status = DEVICE_CONNECTING; + + if (reset_flag) { + device_recon_count = 0; + } + +#if (PLUG_DEVICE || LIGHT_DEVICE) + os_timer_disarm(&beacon_timer); + os_timer_setfn(&beacon_timer, (os_timer_func_t *)user_esp_platform_sent_beacon, &user_conn); +#endif + +#ifdef USE_DNS + user_esp_platform_start_dns(&user_conn); +#else + const char esp_server_ip[4] = {114, 215, 177, 97}; + + os_memcpy(user_conn.proto.tcp->remote_ip, esp_server_ip, 4); + user_conn.proto.tcp->local_port = espconn_port(); + +#ifdef CLIENT_SSL_ENABLE + user_conn.proto.tcp->remote_port = 8443; +#else + user_conn.proto.tcp->remote_port = 8000; +#endif + + espconn_regist_connectcb(&user_conn, user_esp_platform_connect_cb); + espconn_regist_reconcb(&user_conn, user_esp_platform_recon_cb); + user_esp_platform_connect(&user_conn); +#endif + } else { + /* if there are wrong while connecting to some AP, then reset mode */ + if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD || + wifi_station_get_connect_status() == STATION_NO_AP_FOUND || + wifi_station_get_connect_status() == STATION_CONNECT_FAIL)) { + user_esp_platform_reset_mode(); + } else { + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL); + os_timer_arm(&client_timer, 100, 0); + } + } +} + +/****************************************************************************** + * FunctionName : user_esp_platform_init + * Description : device parame init based on espressif platform + * Parameters : none + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR +user_esp_platform_init(void) +{ + + os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ + IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); + os_printf("IOT VERSION = %s\n",iot_version); + + system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); + + struct rst_info *rtc_info = system_get_rst_info(); + + os_printf("reset reason: %x\n", rtc_info->reason); + + if (rtc_info->reason == REASON_WDT_RST || + rtc_info->reason == REASON_EXCEPTION_RST || + rtc_info->reason == REASON_SOFT_WDT_RST) { + if (rtc_info->reason == REASON_EXCEPTION_RST) { + os_printf("Fatal exception (%d):\n", rtc_info->exccause); + } + os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", + rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); + } + + /***add by tzx for saving ip_info to avoid dhcp_client start****/ + struct dhcp_client_info dhcp_info; + struct ip_info sta_info; + system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); + if(dhcp_info.flag == 0x01 ) { + if (true == wifi_station_dhcpc_status()) + { + wifi_station_dhcpc_stop(); + } + sta_info.ip = dhcp_info.ip_addr; + sta_info.gw = dhcp_info.gw; + sta_info.netmask = dhcp_info.netmask; + if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { + os_printf("set default ip wrong\n"); + } + } + os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); + system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); + + +#if AP_CACHE + wifi_station_ap_number_set(AP_CACHE_NUMBER); +#endif + +#if 0 + { + char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; + char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; + struct ip_info info; + + wifi_set_macaddr(SOFTAP_IF, sofap_mac); + wifi_set_macaddr(STATION_IF, sta_mac); + + IP4_ADDR(&info.ip, 192, 168, 3, 200); + IP4_ADDR(&info.gw, 192, 168, 3, 1); + IP4_ADDR(&info.netmask, 255, 255, 255, 0); + wifi_set_ip_info(STATION_IF, &info); + + IP4_ADDR(&info.ip, 10, 10, 10, 1); + IP4_ADDR(&info.gw, 10, 10, 10, 1); + IP4_ADDR(&info.netmask, 255, 255, 255, 0); + wifi_set_ip_info(SOFTAP_IF, &info); + } +#endif + + if (esp_param.activeflag != 1) { +#ifdef SOFTAP_ENCRYPT + struct softap_config config; + char password[33]; + char macaddr[6]; + + wifi_softap_get_config(&config); + wifi_get_macaddr(SOFTAP_IF, macaddr); + + os_memset(config.password, 0, sizeof(config.password)); + os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); + os_memcpy(config.password, password, os_strlen(password)); + config.authmode = AUTH_WPA_WPA2_PSK; + + wifi_softap_set_config(&config); +#endif + + wifi_set_opmode(STATIONAP_MODE); + } + +#if PLUG_DEVICE + user_plug_init(); +#elif LIGHT_DEVICE + user_light_init(); +#elif SENSOR_DEVICE + user_sensor_init(esp_param.activeflag); +#endif + + if (wifi_get_opmode() != SOFTAP_MODE) { + os_timer_disarm(&client_timer); + os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); + os_timer_arm(&client_timer, 100, 0); + } +} + +#endif +#endif diff --git a/polycom_recv/project.h b/polycom_recv/project.h new file mode 100644 index 0000000..08b275a --- /dev/null +++ b/polycom_recv/project.h @@ -0,0 +1,43 @@ +#ifndef __PROJECT_H__ +#define __PROJECT_H__ + +#define USE_OPTIMIZE_PRINTF +#define USE_US_TIMER + +#if 1 +#define ESP_DBG os_printf +#else +#define ESP_DBG +#endif + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "uart_register.h" + + +#define ESP_DOMAIN "iot.panaceas.org" + +#define SOFTAP_PSK "fishsoup" + +#define BEACON_TIMEOUT 150000000 +#define BEACON_TIME 50000 + +#define AP_CACHE 1 +#define AP_CACHE_NUMBER 5 + +#undef HTTPS + + +#define FOO do { os_printf("%s:%d\n",__FILE__,__LINE__); } while (0) + + +#endif + diff --git a/polycom_recv/prototypes.h b/polycom_recv/prototypes.h new file mode 100644 index 0000000..b8063ec --- /dev/null +++ b/polycom_recv/prototypes.h @@ -0,0 +1,27 @@ +/* main.c */ +void user_rf_pre_init(void); +void user_init(void); +/* webserver.c */ +void webserver_send_reply(struct espconn *conn, int status, char *type, char *body, size_t body_len); +void webserver_init(void); +/* util.c */ +char *bounded_strstr(char *haystack, uint32_t len, char *needle); +bool util_isspace(char c); +void crash(void); +/* reset.c */ +void reset_init(void); +/* wifi.c */ +void wifi_init(void); +/* uart.c */ +STATUS uart0_tx_one_char(uint8 TxChar); +void uart_init(void); +/* upgrade.c */ +void upgrade(void); +/* gpio.c */ +uint32_t gpio_read(void); +void gpio_write(int a, int b, int c, int d); +void gpio_init(void); +/* msg.c */ +void msg_init(void); +/* mdns.c */ +void mdns_start(void); diff --git a/polycom_recv/reset.c b/polycom_recv/reset.c new file mode 100644 index 0000000..0068d07 --- /dev/null +++ b/polycom_recv/reset.c @@ -0,0 +1,20 @@ +#include "project.h" +void ICACHE_FLASH_ATTR +reset_init (void) +{ + struct rst_info *rtc_info = system_get_rst_info (); + os_printf ("reset reason: %x\n", rtc_info->reason); + if (rtc_info->reason == REASON_WDT_RST + || rtc_info->reason == REASON_EXCEPTION_RST + || rtc_info->reason == REASON_SOFT_WDT_RST) + { + if (rtc_info->reason == REASON_EXCEPTION_RST) + { + os_printf ("Fatal exception (%d):\n", rtc_info->exccause); + } + os_printf + ("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", + rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, + rtc_info->depc); + } +} diff --git a/polycom_recv/uart.c b/polycom_recv/uart.c new file mode 100644 index 0000000..5d7b5d3 --- /dev/null +++ b/polycom_recv/uart.c @@ -0,0 +1,61 @@ +#include "project.h" + + + +LOCAL void ICACHE_FLASH_ATTR +uart0_config (uint32_t baud) +{ + + +// ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff)); + + PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U); + PIN_FUNC_SELECT (PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); + + + + uart_div_modify (UART0, UART_CLK_FREQ / baud); + + SET_PERI_REG_MASK (UART_CONF0 (UART0), UART_RXFIFO_RST | UART_TXFIFO_RST); //RESET FIFO + CLEAR_PERI_REG_MASK (UART_CONF0 (UART0), UART_RXFIFO_RST | UART_TXFIFO_RST); + + WRITE_PERI_REG (UART_CONF1 (UART0), + ((100 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) + | (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | + UART_RX_TOUT_EN | ((0x10 & UART_TXFIFO_EMPTY_THRHD) << + UART_TXFIFO_EMPTY_THRHD_S)); + SET_PERI_REG_MASK (UART_INT_ENA (UART0), + UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA); + + + + WRITE_PERI_REG (UART_INT_CLR (UART0), 0xffff); + //enable rx_interrupt + //SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_OVF_INT_ENA); +} + + + +STATUS +uart0_tx_one_char (uint8 TxChar) +{ + while (true) + { + uint32 fifo_cnt = + READ_PERI_REG (UART_STATUS (UART0)) & (UART_TXFIFO_CNT << + UART_TXFIFO_CNT_S); + if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) + { + break; + } + } + WRITE_PERI_REG (UART_FIFO (UART0), TxChar); + return OK; +} + + +void ICACHE_FLASH_ATTR +uart_init (void) +{ + uart0_config (115200); +} diff --git a/polycom_recv/uart_register.h b/polycom_recv/uart_register.h new file mode 100644 index 0000000..98def20 --- /dev/null +++ b/polycom_recv/uart_register.h @@ -0,0 +1,160 @@ +/* + * File : uart_register.h + * Copyright (C) 2013 - 2016, Espressif Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of version 3 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +/* + * Copyright (c) 2010 - 2011 Espressif System + * + */ + +#ifndef UART_REGISTER_H_ +#define UART_REGISTER_H_ + +#define UART0 0 +#define UART1 1 + + +#define REG_UART_BASE(i) (0x60000000 + (i)*0xf00) +//version value:32'h062000 + +#define UART_FIFO(i) (REG_UART_BASE(i) + 0x0) +#define UART_RXFIFO_RD_BYTE 0x000000FF +#define UART_RXFIFO_RD_BYTE_S 0 + +#define UART_INT_RAW(i) (REG_UART_BASE(i) + 0x4) +#define UART_RXFIFO_TOUT_INT_RAW (BIT(8)) +#define UART_BRK_DET_INT_RAW (BIT(7)) +#define UART_CTS_CHG_INT_RAW (BIT(6)) +#define UART_DSR_CHG_INT_RAW (BIT(5)) +#define UART_RXFIFO_OVF_INT_RAW (BIT(4)) +#define UART_FRM_ERR_INT_RAW (BIT(3)) +#define UART_PARITY_ERR_INT_RAW (BIT(2)) +#define UART_TXFIFO_EMPTY_INT_RAW (BIT(1)) +#define UART_RXFIFO_FULL_INT_RAW (BIT(0)) + +#define UART_INT_ST(i) (REG_UART_BASE(i) + 0x8) +#define UART_RXFIFO_TOUT_INT_ST (BIT(8)) +#define UART_BRK_DET_INT_ST (BIT(7)) +#define UART_CTS_CHG_INT_ST (BIT(6)) +#define UART_DSR_CHG_INT_ST (BIT(5)) +#define UART_RXFIFO_OVF_INT_ST (BIT(4)) +#define UART_FRM_ERR_INT_ST (BIT(3)) +#define UART_PARITY_ERR_INT_ST (BIT(2)) +#define UART_TXFIFO_EMPTY_INT_ST (BIT(1)) +#define UART_RXFIFO_FULL_INT_ST (BIT(0)) + +#define UART_INT_ENA(i) (REG_UART_BASE(i) + 0xC) +#define UART_RXFIFO_TOUT_INT_ENA (BIT(8)) +#define UART_BRK_DET_INT_ENA (BIT(7)) +#define UART_CTS_CHG_INT_ENA (BIT(6)) +#define UART_DSR_CHG_INT_ENA (BIT(5)) +#define UART_RXFIFO_OVF_INT_ENA (BIT(4)) +#define UART_FRM_ERR_INT_ENA (BIT(3)) +#define UART_PARITY_ERR_INT_ENA (BIT(2)) +#define UART_TXFIFO_EMPTY_INT_ENA (BIT(1)) +#define UART_RXFIFO_FULL_INT_ENA (BIT(0)) + +#define UART_INT_CLR(i) (REG_UART_BASE(i) + 0x10) +#define UART_RXFIFO_TOUT_INT_CLR (BIT(8)) +#define UART_BRK_DET_INT_CLR (BIT(7)) +#define UART_CTS_CHG_INT_CLR (BIT(6)) +#define UART_DSR_CHG_INT_CLR (BIT(5)) +#define UART_RXFIFO_OVF_INT_CLR (BIT(4)) +#define UART_FRM_ERR_INT_CLR (BIT(3)) +#define UART_PARITY_ERR_INT_CLR (BIT(2)) +#define UART_TXFIFO_EMPTY_INT_CLR (BIT(1)) +#define UART_RXFIFO_FULL_INT_CLR (BIT(0)) + +#define UART_CLKDIV(i) (REG_UART_BASE(i) + 0x14) +#define UART_CLKDIV_CNT 0x000FFFFF +#define UART_CLKDIV_S 0 + +#define UART_AUTOBAUD(i) (REG_UART_BASE(i) + 0x18) +#define UART_GLITCH_FILT 0x000000FF +#define UART_GLITCH_FILT_S 8 +#define UART_AUTOBAUD_EN (BIT(0)) + +#define UART_STATUS(i) (REG_UART_BASE(i) + 0x1C) +#define UART_TXD (BIT(31)) +#define UART_RTSN (BIT(30)) +#define UART_DTRN (BIT(29)) +#define UART_TXFIFO_CNT 0x000000FF +#define UART_TXFIFO_CNT_S 16 +#define UART_RXD (BIT(15)) +#define UART_CTSN (BIT(14)) +#define UART_DSRN (BIT(13)) +#define UART_RXFIFO_CNT 0x000000FF +#define UART_RXFIFO_CNT_S 0 + +#define UART_CONF0(i) (REG_UART_BASE(i) + 0x20) +#define UART_DTR_INV (BIT(24)) +#define UART_RTS_INV (BIT(23)) +#define UART_TXD_INV (BIT(22)) +#define UART_DSR_INV (BIT(21)) +#define UART_CTS_INV (BIT(20)) +#define UART_RXD_INV (BIT(19)) +#define UART_TXFIFO_RST (BIT(18)) +#define UART_RXFIFO_RST (BIT(17)) +#define UART_IRDA_EN (BIT(16)) +#define UART_TX_FLOW_EN (BIT(15)) +#define UART_LOOPBACK (BIT(14)) +#define UART_IRDA_RX_INV (BIT(13)) +#define UART_IRDA_TX_INV (BIT(12)) +#define UART_IRDA_WCTL (BIT(11)) +#define UART_IRDA_TX_EN (BIT(10)) +#define UART_IRDA_DPLX (BIT(9)) +#define UART_TXD_BRK (BIT(8)) +#define UART_SW_DTR (BIT(7)) +#define UART_SW_RTS (BIT(6)) +#define UART_STOP_BIT_NUM 0x00000003 +#define UART_STOP_BIT_NUM_S 4 +#define UART_BIT_NUM 0x00000003 +#define UART_BIT_NUM_S 2 +#define UART_PARITY_EN (BIT(1)) +#define UART_PARITY_EN_M 0x00000001 +#define UART_PARITY_EN_S 1 +#define UART_PARITY (BIT(0)) +#define UART_PARITY_M 0x00000001 +#define UART_PARITY_S 0 + +#define UART_CONF1(i) (REG_UART_BASE(i) + 0x24) +#define UART_RX_TOUT_EN (BIT(31)) +#define UART_RX_TOUT_THRHD 0x0000007F +#define UART_RX_TOUT_THRHD_S 24 +#define UART_RX_FLOW_EN (BIT(23)) +#define UART_RX_FLOW_THRHD 0x0000007F +#define UART_RX_FLOW_THRHD_S 16 +#define UART_TXFIFO_EMPTY_THRHD 0x0000007F +#define UART_TXFIFO_EMPTY_THRHD_S 8 +#define UART_RXFIFO_FULL_THRHD 0x0000007F +#define UART_RXFIFO_FULL_THRHD_S 0 + +#define UART_LOWPULSE(i) (REG_UART_BASE(i) + 0x28) +#define UART_LOWPULSE_MIN_CNT 0x000FFFFF +#define UART_LOWPULSE_MIN_CNT_S 0 + +#define UART_HIGHPULSE(i) (REG_UART_BASE(i) + 0x2C) +#define UART_HIGHPULSE_MIN_CNT 0x000FFFFF +#define UART_HIGHPULSE_MIN_CNT_S 0 + +#define UART_PULSE_NUM(i) (REG_UART_BASE(i) + 0x30) +#define UART_PULSE_NUM_CNT 0x0003FF +#define UART_PULSE_NUM_CNT_S 0 + +#define UART_DATE(i) (REG_UART_BASE(i) + 0x78) +#define UART_ID(i) (REG_UART_BASE(i) + 0x7C) + +#endif // UART_REGISTER_H_INCLUDED + diff --git a/polycom_recv/upgrade.c b/polycom_recv/upgrade.c new file mode 100644 index 0000000..2bf0c99 --- /dev/null +++ b/polycom_recv/upgrade.c @@ -0,0 +1,83 @@ +#include "project.h" + + +#define pheadbuffer "Connection: keep-alive\r\n\ +Cache-Control: no-cache\r\n\ +User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 \r\n\ +Accept: */*\r\n\ +\r\n\r\n" + + +static void ICACHE_FLASH_ATTR +upgrade_check_cb (void *arg) +{ + struct upgrade_server_info *server = arg; + + if (server->upgrade_flag == true) + { + os_printf ("upgrade finished sucesfully\n"); + } + else + { + os_printf ("upgrade finished un-sucessfully\n"); + } + + os_free (server->url); + server->url = NULL; + os_free (server); + server = NULL; +} + + +void ICACHE_FLASH_ATTR +upgrade (void) +{ + struct upgrade_server_info *server = NULL; + char user_bin[10]; + + server = (struct upgrade_server_info *) os_zalloc (sizeof (*server)); + + if (!server) + return; + + + server->ip[0] = 82; + server->ip[1] = 68; + server->ip[2] = 70; + server->ip[3] = 173; + + server->port = 80; + + server->check_cb = upgrade_check_cb; + server->check_times = 120000; + + server->url = (uint8 *) os_zalloc (512); + + if (system_upgrade_userbin_check () == UPGRADE_FW_BIN1) + { + os_strcpy (user_bin, "user2.bin"); + } + else if (system_upgrade_userbin_check () == UPGRADE_FW_BIN2) + { + os_strcpy (user_bin, "user1.bin"); + } + + + os_sprintf (server->url, + "GET /esp8266/%s HTTP/1.1\r\nHost: " IPSTR ":%d\r\n" pheadbuffer + "", user_bin, IP2STR (server->ip), server->port); + + +#if 0 + if (system_upgrade_start_ssl (server) == false) +#else + if (system_upgrade_start (server) == false) +#endif + { + os_printf ("Upgrade start failed\n"); + } + else + { + os_printf ("Upgrade start ok\n"); + } +} diff --git a/polycom_recv/util.c b/polycom_recv/util.c new file mode 100644 index 0000000..1d44f3d --- /dev/null +++ b/polycom_recv/util.c @@ -0,0 +1,55 @@ +#include "project.h" + + +ICACHE_FLASH_ATTR char * +bounded_strstr (char *haystack, uint32_t len, char *needle) +{ + char *end = haystack + len; + char *hptr, *nptr; + + if (!*needle) + return NULL; + + for (; (*haystack) && (haystack < end); ++haystack) + { + + hptr = haystack; + nptr = needle; + + while ((*hptr) == (*nptr)) + { + nptr++; + hptr++; + if (!*nptr) + return haystack; + if (hptr >= end) + return NULL; + } + } + + return NULL; +} + + +bool +util_isspace (char c) +{ + switch (c) + { + case ' ': + case '\r': + case '\t': + case '\n': + return true; + default: + return false; + } +} + +ICACHE_FLASH_ATTR void +crash (void) +{ + char *c = NULL; + + os_printf ("%d", *c); +} diff --git a/polycom_recv/webserver.c b/polycom_recv/webserver.c new file mode 100644 index 0000000..2089383 --- /dev/null +++ b/polycom_recv/webserver.c @@ -0,0 +1,2442 @@ +#include "project.h" + +#define URL_SIZE 256 + +#define TYPE_UNKNOWN 0 +#define TYPE_GET 1 +#define TYPE_POST 2 + +#define MAX_RECV_BUFFER_SIZE 1024 + +typedef struct http_state_struct +{ + bool header_parsed_ok; + bool error; + bool overflow; + uint8_t type; + + uint32_t bytes_recvd; + + uint32_t header_length; + uint32_t content_length; + uint32_t expected_length; + + char url[URL_SIZE]; + char *recv_buffer; + uint32_t recv_buffer_len; + uint32_t recv_buffer_size; + +} http_state; + +static http_state static_state = { 0 }; + + +#define LENGTH_ERR ((uint32_t) -1) + + + +void ICACHE_FLASH_ATTR +webserver_send_reply (struct espconn *conn, int status, char *type, + char *body, size_t body_len) +{ + char header[256] = { 0 }; + char *ptr = header; + + if (body && !body_len) + body_len = os_strlen (body); + + switch (status) + { + case 200: + ptr += + os_sprintf (ptr, + "HTTP/1.1 %d OK\r\nContent-Length: %d\r\nServer: InternetOfThingsThing/1.0\r\n", + status, body ? body_len : 0); + + break; + default: + ptr += + os_sprintf (ptr, + "HTTP/1.1 %d BadRequest\r\nContent-Length: %d\r\nServer: InternetOfThingsThing/1.0\r\n", + status, 0); + break; + } + if (body && body_len) + { + + if (type) + ptr += + os_sprintf (ptr, + "Content-type: %s\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n", + type); + } + + ptr += os_sprintf (ptr, "Connection: close\r\n\r\n"); + +#ifdef SERVER_SSL_ENABLE + espconn_secure_sent (conn, header, (uint32_t) ptr - header); + if (body && body_len) + espconn_secure_sent (conn, body, body_len); +#else + espconn_sent (conn, header, (uint32_t) (ptr - header)); + if (body && body_len) + espconn_sent (conn, body, body_len); +#endif + +} + + +static bool ICACHE_FLASH_ATTR +webserver_get_header (char *buf, uint32_t length, char *match, char *ret, + uint32_t ret_len) +{ + uint32_t match_len = os_strlen (match); + char *line_ptr; + char *eol_ptr; + uint32_t line_length; + + + line_ptr = (char *) bounded_strstr (buf, length, match); + + if (!line_ptr) + return false; + + line_length = (buf + length) - line_ptr; + + if (line_length <= match_len) + return false; + + line_ptr += match_len; + line_length -= match_len; + + eol_ptr = (char *) bounded_strstr (line_ptr, line_length, "\r\n"); + + if (!eol_ptr) + return false; + + line_length = eol_ptr - line_ptr; + + if (line_length >= ret_len) + return false; + + os_memcpy (ret, line_ptr, ret_len); + ret[ret_len] = 0; + + return true; +} + + +static bool ICACHE_FLASH_ATTR +webserver_parse_request (http_state * s, char *buf) +{ + char *ptr = buf; + char *eol_ptr; + uint32_t line_length; + + eol_ptr = (char *) bounded_strstr (buf, s->header_length, "\r\n"); + + if (!eol_ptr) + return false; + + line_length = eol_ptr - buf; + + + if (line_length < 5) + return false; + + if (!strncmp (ptr, "GET ", 4)) + { + s->type = TYPE_GET; + ptr += 4; + line_length -= 4; + + } + else if (!strncmp (ptr, "POST ", 5)) + { + s->type = TYPE_POST; + ptr += 5; + line_length -= 5; + } + + + while (util_isspace (*ptr)) + { + ptr++; + line_length--; + if (!line_length) + return false; + } + + + eol_ptr = ptr; + + /*Skip the protocol version */ + while (!util_isspace (*eol_ptr)) + { + eol_ptr++; + line_length--; + if (!line_length) + return false; + } + + line_length = eol_ptr - ptr; + + if (line_length >= URL_SIZE) + return false; + + os_memcpy (s->url, ptr, line_length); + + s->url[line_length] = 0; + + return true; +} + + + +static bool ICACHE_FLASH_ATTR +webserver_parse_header (http_state * s, char *buf, uint32_t length) +{ + char *eoh_ptr; + char tmp_buf[256]; + char *ptr; + + s->header_parsed_ok = false; + + + if (s->error) + return false; + + + eoh_ptr = (char *) bounded_strstr (buf, length, "\r\n\r\n"); + + if (!eoh_ptr) + return false; + + eoh_ptr += 4; + + + s->header_length = eoh_ptr - buf; + + if (!webserver_get_header + (buf, s->header_length, "Content-Length: ", tmp_buf, sizeof (tmp_buf))) + { + s->content_length = 0; + + } + else + { + s->content_length = atoi (tmp_buf); + if (s->content_length < 0) + return false; + } + + + if (!webserver_parse_request (s, buf)) + { + s->error++; + return false; + } + + + s->header_parsed_ok = true; + + s->expected_length = s->content_length + s->header_length; + + + os_printf ("wph:url:%s\n", s->url); + os_printf ("wph:head:%u,data:%u,tot:%u\n", s->header_length, + s->content_length, s->content_length + s->header_length); + return true; +} + +static void ICACHE_FLASH_ATTR +webserver_state_reset (http_state * s) +{ + os_printf ("wsr:\n"); + + if (s->recv_buffer) + os_free (s->recv_buffer); + + memset (s, 0, sizeof (*s)); +} + + +static bool ICACHE_FLASH_ATTR +webserver_append_data (http_state * s, char *data, uint32_t len) +{ + uint32_t space; + + s->bytes_recvd += len; + + if (s->error) + return false; + + if (!s->recv_buffer) + { + + if (s->recv_buffer_size) + { + if (s->recv_buffer_size > MAX_RECV_BUFFER_SIZE) + s->recv_buffer_size = MAX_RECV_BUFFER_SIZE; + } + else + { + s->recv_buffer_size = MAX_RECV_BUFFER_SIZE; + } + + s->recv_buffer_len = 0; + + s->recv_buffer = (char *) os_zalloc (s->recv_buffer_size); + + if (!s->recv_buffer) + { + s->error++; + return false; + } + + os_printf ("wad:bs:%u,len:%u,add:%u\n", s->recv_buffer_size, + s->recv_buffer_len, len); + + } + + space = s->recv_buffer_size - s->recv_buffer_len; + + if (len > space) + { + s->overflow++; + space = len; + } + + os_memcpy (s->recv_buffer, data, space); + s->recv_buffer_len += space; + + return true; +} + + + +static void ICACHE_FLASH_ATTR +webserver_header (http_state * s) +{ + + + +} + +static void ICACHE_FLASH_ATTR +webserver_req (struct espconn *conn, http_state * s) +{ + os_printf ("Webserver req: %s\n", s->url); + if (!os_strcmp (s->url, "/crash.html")) + { + webserver_send_reply (conn, 200, "text/html", + "crashed into monitor", + 0); + crash (); + } + else if (!os_strcmp (s->url, "/upgrade.html")) + { + webserver_send_reply (conn, 200, "text/html", + "triggered upgrade", + 0); + os_printf ("Upgrading\n"); + upgrade (); + } +#if 0 + else if (!os_strcmp (s->url, "/gpio.html")) + { + gpio_page (conn); + } + else if (!os_strcmp (s->url, "/")) + { + gpio_colour_page (conn); + } +#endif + else + { + webserver_send_reply (conn, 404, "text/html", + "Not Found", + 0); + } + + + espconn_disconnect (conn); + +} + + + + +static void ICACHE_FLASH_ATTR +webserver_recv (void *arg, char *pusrdata, unsigned short data_len) +{ + struct espconn *conn = arg; + uint32_t len; + http_state *s = &static_state; +#if 0 + if (doing_upgrade) + { + upgrade_recv (ptrespconn, pusrdata, data_len); + free_recvbuffer (); + return; + } +#endif + + os_printf ("len:%u\n", data_len); + if (!s->header_parsed_ok) + { + if (!s->recv_buffer) + { + if (webserver_parse_header (s, pusrdata, data_len)) + { + webserver_header (s); + s->recv_buffer_size = s->expected_length; + } + + webserver_append_data (s, pusrdata, data_len); + } + else + { + webserver_append_data (s, pusrdata, data_len); + webserver_parse_header (s, s->recv_buffer, s->recv_buffer_len); + } + } + else + { + webserver_append_data (s, pusrdata, data_len); + } + + + if (s->error) + return; + if (s->bytes_recvd != s->expected_length) + return; + webserver_req (conn, s); +} + + +static ICACHE_FLASH_ATTR void +webserver_recon (void *arg, sint8 err) +{ + struct espconn *pesp_conn = arg; + os_printf + ("webserver's %d.%d.%d.%d:%d err %d reconnect\n", + pesp_conn->proto.tcp->remote_ip[0], + pesp_conn->proto.tcp->remote_ip[1], + pesp_conn->proto.tcp->remote_ip[2], + pesp_conn->proto.tcp->remote_ip[3], + pesp_conn->proto.tcp->remote_port, err); +} + +static ICACHE_FLASH_ATTR void +webserver_discon (void *arg) +{ + struct espconn *pesp_conn = arg; + os_printf ("webserver's %d.%d.%d.%d:%d disconnect\n", + pesp_conn->proto.tcp->remote_ip[0], + pesp_conn->proto.tcp->remote_ip[1], + pesp_conn->proto.tcp->remote_ip[2], + pesp_conn->proto.tcp->remote_ip[3], + pesp_conn->proto.tcp->remote_port); +} + + + + +static void ICACHE_FLASH_ATTR +webserver_listen (void *arg) +{ + struct espconn *pesp_conn = arg; + webserver_state_reset (&static_state); + espconn_regist_recvcb (pesp_conn, webserver_recv); + espconn_regist_reconcb (pesp_conn, webserver_recon); + espconn_regist_disconcb (pesp_conn, webserver_discon); +} + + +void ICACHE_FLASH_ATTR +webserver_init (void) +{ + static struct espconn esp_conn; + static esp_tcp esptcp; + esp_conn.type = ESPCONN_TCP; + esp_conn.state = ESPCONN_NONE; + esp_conn.proto.tcp = &esptcp; +#ifdef HTTPS + esp_conn.proto.tcp->local_port = 443; +#else + esp_conn.proto.tcp->local_port = 80; +#endif + espconn_regist_connectcb (&esp_conn, webserver_listen); +#ifdef HTTPS + espconn_secure_accept (&esp_conn); +#else + espconn_accept (&esp_conn); +#endif +} + + + + +#if 0 +/****************************************************************************** + * Copyright 2013-2014 Espressif Systems (Wuxi) + * + * FileName: webserver.c + * + * Description: The web server mode configration. + * Check your hardware connection with the host while use this mode. + * Modification history: + * 2014/3/12, v1.0 create this file. +*******************************************************************************/ +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" + +#include "iot_version.h" +#include "espconn.h" +#include "json.h" +#include "webserver.h" + +#include "upgrade.h" +#if ESP_PLATFORM +#include "esp_platform.h" +#endif + +#if LIGHT_DEVICE +#include "light.h" +#endif + +static struct station_config *sta_conf; +static struct softap_config *ap_conf; +//static struct secrty_server_info *sec_server; +//static struct upgrade_server_info *server; +//struct lewei_login_info *login_info; +static scaninfo *pscaninfo; +extern u16 scannum; +static uint32 PostCmdNeeRsp = 1; +uint8 upgrade_lock = 0; +static os_timer_t app_upgrade_10s; +static os_timer_t upgrade_check_timer; +/****************************************************************************** + * FunctionName : device_get + * Description : set up the device information parmer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +device_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + if (os_strncmp (path, "manufacture", 11) == 0) + { + jsontree_write_string (js_ctx, "Espressif Systems"); + } + else if (os_strncmp (path, "product", 7) == 0) + { +#if SENSOR_DEVICE +#if HUMITURE_SUB_DEVICE + jsontree_write_string (js_ctx, "Humiture"); +#elif FLAMMABLE_GAS_SUB_DEVICE + jsontree_write_string (js_ctx, "Flammable Gas"); +#endif +#endif +#if PLUG_DEVICE + jsontree_write_string (js_ctx, "Plug"); +#endif +#if LIGHT_DEVICE + jsontree_write_string (js_ctx, "Light"); +#endif + } + + return 0; +} + +static struct jsontree_callback device_callback = +JSONTREE_CALLBACK (device_get, NULL); +/****************************************************************************** + * FunctionName : userbin_get + * Description : get up the user bin paramer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +userbin_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + char string[32]; + if (os_strncmp (path, "status", 8) == 0) + { + os_sprintf (string, "200"); + } + else if (os_strncmp (path, "user_bin", 8) == 0) + { + if (system_upgrade_userbin_check () == 0x00) + { + os_sprintf (string, "user1.bin"); + } + else if (system_upgrade_userbin_check () == 0x01) + { + os_sprintf (string, "user2.bin"); + } + else + { + return 0; + } + } + + jsontree_write_string (js_ctx, string); + return 0; +} + +static struct jsontree_callback userbin_callback = +JSONTREE_CALLBACK (userbin_get, NULL); +JSONTREE_OBJECT (userbin_tree, + JSONTREE_PAIR ("status", + &userbin_callback), + JSONTREE_PAIR ("user_bin", &userbin_callback)); +JSONTREE_OBJECT (userinfo_tree, JSONTREE_PAIR ("user_info", &userbin_tree)); +/****************************************************************************** + * FunctionName : version_get + * Description : set up the device version paramer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +version_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + char string[32]; + if (os_strncmp (path, "hardware", 8) == 0) + { +#if SENSOR_DEVICE + os_sprintf (string, "0.3"); +#else + os_sprintf (string, "0.1"); +#endif + } + else if (os_strncmp (path, "sdk_version", 11) == 0) + { + os_sprintf (string, "%s", system_get_sdk_version ()); + } + else if (os_strncmp (path, "iot_version", 11) == 0) + { + os_sprintf (string, "%s%d.%d.%dt%d(%s)", VERSION_TYPE, + IOT_VERSION_MAJOR, IOT_VERSION_MINOR, + IOT_VERSION_REVISION, device_type, UPGRADE_FALG); + } + + jsontree_write_string (js_ctx, string); + return 0; +} + +static struct jsontree_callback version_callback = +JSONTREE_CALLBACK (version_get, NULL); +JSONTREE_OBJECT (device_tree, + JSONTREE_PAIR ("product", + &device_callback), + JSONTREE_PAIR ("manufacturer", &device_callback)); +JSONTREE_OBJECT (version_tree, + JSONTREE_PAIR ("hardware", + &version_callback), + JSONTREE_PAIR ("sdk_version", + &version_callback), + JSONTREE_PAIR ("iot_version", &version_callback),); +JSONTREE_OBJECT (info_tree, + JSONTREE_PAIR ("Version", + &version_tree), + JSONTREE_PAIR ("Device", &device_tree)); +JSONTREE_OBJECT (INFOTree, JSONTREE_PAIR ("info", &info_tree)); +static int ICACHE_FLASH_ATTR +connect_status_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + if (os_strncmp (path, "status", 8) == 0) + { + jsontree_write_int (js_ctx, user_esp_platform_get_connect_status ()); + } + + return 0; +} + +static struct jsontree_callback connect_status_callback = +JSONTREE_CALLBACK (connect_status_get, NULL); +JSONTREE_OBJECT (status_sub_tree, + JSONTREE_PAIR ("status", &connect_status_callback)); +JSONTREE_OBJECT (connect_status_tree, + JSONTREE_PAIR ("Status", &status_sub_tree)); +JSONTREE_OBJECT (con_status_tree, + JSONTREE_PAIR ("info", &connect_status_tree)); +#if PLUG_DEVICE +/****************************************************************************** + * FunctionName : status_get + * Description : set up the device status as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +status_get (struct jsontree_context *js_ctx) +{ + if (user_plug_get_status () == 1) + { + jsontree_write_int (js_ctx, 1); + } + else + { + jsontree_write_int (js_ctx, 0); + } + + return 0; +} + +/****************************************************************************** + * FunctionName : status_set + * Description : parse the device status parmer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * parser -- A pointer to a JSON parser state + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +status_set (struct jsontree_context *js_ctx, struct jsonparse_state *parser) +{ + int type; + while ((type = jsonparse_next (parser)) != 0) + { + if (type == JSON_TYPE_PAIR_NAME) + { + if (jsonparse_strcmp_value (parser, "status") == 0) + { + uint8 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + user_plug_set_status (status); + } + } + } + + return 0; +} + +static struct jsontree_callback status_callback = +JSONTREE_CALLBACK (status_get, status_set); +JSONTREE_OBJECT (status_tree, JSONTREE_PAIR ("status", &status_callback)); +JSONTREE_OBJECT (response_tree, JSONTREE_PAIR ("Response", &status_tree)); +JSONTREE_OBJECT (StatusTree, JSONTREE_PAIR ("switch", &response_tree)); +#endif +#if LIGHT_DEVICE +static int ICACHE_FLASH_ATTR +light_status_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + if (os_strncmp (path, "red", 3) == 0) + { + jsontree_write_int (js_ctx, user_light_get_duty (LIGHT_RED)); + } + else if (os_strncmp (path, "green", 5) == 0) + { + jsontree_write_int (js_ctx, user_light_get_duty (LIGHT_GREEN)); + } + else if (os_strncmp (path, "blue", 4) == 0) + { + jsontree_write_int (js_ctx, user_light_get_duty (LIGHT_BLUE)); + } + else if (os_strncmp (path, "wwhite", 6) == 0) + { + if (PWM_CHANNEL > LIGHT_WARM_WHITE) + { + jsontree_write_int (js_ctx, user_light_get_duty (LIGHT_WARM_WHITE)); + } + else + { + jsontree_write_int (js_ctx, 0); + } + } + else if (os_strncmp (path, "cwhite", 6) == 0) + { + if (PWM_CHANNEL > LIGHT_COLD_WHITE) + { + jsontree_write_int (js_ctx, user_light_get_duty (LIGHT_COLD_WHITE)); + } + else + { + jsontree_write_int (js_ctx, 0); + } + } + else if (os_strncmp (path, "period", 6) == 0) + { + jsontree_write_int (js_ctx, user_light_get_period ()); + } + + return 0; +} + +static int ICACHE_FLASH_ATTR +light_status_set (struct jsontree_context *js_ctx, + struct jsonparse_state *parser) +{ + int type; + static uint32 r, g, b, cw, ww, period; + period = 1000; + cw = 0; + ww = 0; + extern uint8 light_sleep_flg; + while ((type = jsonparse_next (parser)) != 0) + { + if (type == JSON_TYPE_PAIR_NAME) + { + if (jsonparse_strcmp_value (parser, "red") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + r = status; + os_printf ("R: %d \n", status); + //user_light_set_duty(status, LIGHT_RED); + //light_set_aim_r( r); + } + else if (jsonparse_strcmp_value (parser, "green") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + g = status; + os_printf ("G: %d \n", status); + //user_light_set_duty(status, LIGHT_GREEN); + //light_set_aim_g( g); + } + else if (jsonparse_strcmp_value (parser, "blue") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + b = status; + os_printf ("B: %d \n", status); + //user_light_set_duty(status, LIGHT_BLUE); + //set_aim_b( b); + } + else if (jsonparse_strcmp_value (parser, "cwhite") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + cw = status; + os_printf ("CW: %d \n", status); + //user_light_set_duty(status, LIGHT_BLUE); + //set_aim_b( b); + } + else if (jsonparse_strcmp_value (parser, "wwhite") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + ww = status; + os_printf ("WW: %d \n", status); + //user_light_set_duty(status, LIGHT_BLUE); + //set_aim_b( b); + } + else if (jsonparse_strcmp_value (parser, "period") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + os_printf ("PERIOD: %d \n", status); + period = status; + //user_light_set_period(status); + } + else if (jsonparse_strcmp_value (parser, "response") == 0) + { + uint32 status; + jsonparse_next (parser); + jsonparse_next (parser); + status = jsonparse_get_value_as_int (parser); + os_printf ("rspneed: %d \n", status); + PostCmdNeeRsp = status; + } + } + } + + if ((r | g | b | ww | cw) == 0) + { + if (light_sleep_flg == 0) + { + + } + + } + else + { + if (light_sleep_flg == 1) + { + os_printf ("modem sleep en\r\n"); + wifi_set_sleep_type (MODEM_SLEEP_T); + light_sleep_flg = 0; + } + } + light_set_aim (r, g, b, cw, ww, period); + return 0; +} + +static struct jsontree_callback light_callback = +JSONTREE_CALLBACK (light_status_get, light_status_set); +JSONTREE_OBJECT (rgb_tree, + JSONTREE_PAIR ("red", &light_callback), + JSONTREE_PAIR ("green", + &light_callback), + JSONTREE_PAIR ("blue", &light_callback), + JSONTREE_PAIR ("cwhite", + &light_callback), + JSONTREE_PAIR ("wwhite", &light_callback),); +JSONTREE_OBJECT (sta_tree, + JSONTREE_PAIR ("period", + &light_callback), + JSONTREE_PAIR ("rgb", &rgb_tree)); +JSONTREE_OBJECT (PwmTree, JSONTREE_PAIR ("light", &sta_tree)); +#endif +/****************************************************************************** + * FunctionName : wifi_station_get + * Description : set up the station paramer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +wifi_station_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + struct ip_info ipconfig; + uint8 buf[20]; + os_bzero (buf, sizeof (buf)); + wifi_station_get_config (sta_conf); + wifi_get_ip_info (STATION_IF, &ipconfig); + if (os_strncmp (path, "ssid", 4) == 0) + { + jsontree_write_string (js_ctx, sta_conf->ssid); + } + else if (os_strncmp (path, "password", 8) == 0) + { + jsontree_write_string (js_ctx, sta_conf->password); + } + else if (os_strncmp (path, "ip", 2) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.ip)); + jsontree_write_string (js_ctx, buf); + } + else if (os_strncmp (path, "mask", 4) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.netmask)); + jsontree_write_string (js_ctx, buf); + } + else if (os_strncmp (path, "gw", 2) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.gw)); + jsontree_write_string (js_ctx, buf); + } + + return 0; +} + +/****************************************************************************** + * FunctionName : wifi_station_set + * Description : parse the station parmer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * parser -- A pointer to a JSON parser state + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +wifi_station_set (struct jsontree_context *js_ctx, + struct jsonparse_state *parser) +{ + int type; + uint8 station_tree; + while ((type = jsonparse_next (parser)) != 0) + { + if (type == JSON_TYPE_PAIR_NAME) + { + char buffer[64]; + os_bzero (buffer, 64); + if (jsonparse_strcmp_value (parser, "Station") == 0) + { + station_tree = 1; + } + else if (jsonparse_strcmp_value (parser, "Softap") == 0) + { + station_tree = 0; + } + + if (station_tree) + { + if (jsonparse_strcmp_value (parser, "ssid") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + os_memcpy (sta_conf->ssid, buffer, os_strlen (buffer)); + } + else if (jsonparse_strcmp_value (parser, "password") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + os_memcpy (sta_conf->password, buffer, os_strlen (buffer)); + } + +#if ESP_PLATFORM + + else if (jsonparse_strcmp_value (parser, "token") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + user_esp_platform_set_token (buffer); + } + +#endif + } + } + } + + return 0; +} + +static struct jsontree_callback wifi_station_callback = +JSONTREE_CALLBACK (wifi_station_get, wifi_station_set); +JSONTREE_OBJECT (get_station_config_tree, + JSONTREE_PAIR ("ssid", + &wifi_station_callback), + JSONTREE_PAIR ("password", &wifi_station_callback)); +JSONTREE_OBJECT (set_station_config_tree, + JSONTREE_PAIR ("ssid", + &wifi_station_callback), + JSONTREE_PAIR ("password", + &wifi_station_callback), + JSONTREE_PAIR ("token", &wifi_station_callback)); +JSONTREE_OBJECT (ip_tree, + JSONTREE_PAIR ("ip", + &wifi_station_callback), + JSONTREE_PAIR ("mask", + &wifi_station_callback), + JSONTREE_PAIR ("gw", &wifi_station_callback)); +JSONTREE_OBJECT (get_station_tree, + JSONTREE_PAIR ("Connect_Station", + &get_station_config_tree), + JSONTREE_PAIR ("Ipinfo_Station", &ip_tree)); +JSONTREE_OBJECT (set_station_tree, + JSONTREE_PAIR ("Connect_Station", &set_station_config_tree)); +//JSONTREE_OBJECT(get_wifi_station_info_tree, +// JSONTREE_PAIR("Station", &get_station_tree)); +//JSONTREE_OBJECT(set_wifi_station_info_tree, +// JSONTREE_PAIR("station", &set_station_tree)); +/****************************************************************************** + * FunctionName : wifi_softap_get + * Description : set up the softap paramer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +wifi_softap_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + struct ip_info ipconfig; + uint8 buf[20]; + os_bzero (buf, sizeof (buf)); + wifi_softap_get_config (ap_conf); + wifi_get_ip_info (SOFTAP_IF, &ipconfig); + if (os_strncmp (path, "ssid", 4) == 0) + { + jsontree_write_string (js_ctx, ap_conf->ssid); + } + else if (os_strncmp (path, "password", 8) == 0) + { + jsontree_write_string (js_ctx, ap_conf->password); + } + else if (os_strncmp (path, "channel", 7) == 0) + { + jsontree_write_int (js_ctx, ap_conf->channel); + } + else if (os_strncmp (path, "authmode", 8) == 0) + { + switch (ap_conf->authmode) + { + case AUTH_OPEN: + jsontree_write_string (js_ctx, "OPEN"); + break; + case AUTH_WEP: + jsontree_write_string (js_ctx, "WEP"); + break; + case AUTH_WPA_PSK: + jsontree_write_string (js_ctx, "WPAPSK"); + break; + case AUTH_WPA2_PSK: + jsontree_write_string (js_ctx, "WPA2PSK"); + break; + case AUTH_WPA_WPA2_PSK: + jsontree_write_string (js_ctx, "WPAPSK/WPA2PSK"); + break; + default: + jsontree_write_int (js_ctx, ap_conf->authmode); + break; + } + } + else if (os_strncmp (path, "ip", 2) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.ip)); + jsontree_write_string (js_ctx, buf); + } + else if (os_strncmp (path, "mask", 4) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.netmask)); + jsontree_write_string (js_ctx, buf); + } + else if (os_strncmp (path, "gw", 2) == 0) + { + os_sprintf (buf, IPSTR, IP2STR (&ipconfig.gw)); + jsontree_write_string (js_ctx, buf); + } + + return 0; +} + +/****************************************************************************** + * FunctionName : wifi_softap_set + * Description : parse the softap parmer as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * parser -- A pointer to a JSON parser state + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +wifi_softap_set (struct jsontree_context *js_ctx, + struct jsonparse_state *parser) +{ + int type; + uint8 softap_tree; + while ((type = jsonparse_next (parser)) != 0) + { + if (type == JSON_TYPE_PAIR_NAME) + { + char buffer[64]; + os_bzero (buffer, 64); + if (jsonparse_strcmp_value (parser, "Station") == 0) + { + softap_tree = 0; + } + else if (jsonparse_strcmp_value (parser, "Softap") == 0) + { + softap_tree = 1; + } + + if (softap_tree) + { + if (jsonparse_strcmp_value (parser, "authmode") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + // other mode will be supported later... + if (os_strcmp (buffer, "OPEN") == 0) + { + ap_conf->authmode = AUTH_OPEN; + } + else if (os_strcmp (buffer, "WPAPSK") == 0) + { + ap_conf->authmode = AUTH_WPA_PSK; + os_printf ("%d %s\n", ap_conf->authmode, buffer); + } + else if (os_strcmp (buffer, "WPA2PSK") == 0) + { + ap_conf->authmode = AUTH_WPA2_PSK; + } + else if (os_strcmp (buffer, "WPAPSK/WPA2PSK") == 0) + { + ap_conf->authmode = AUTH_WPA_WPA2_PSK; + } + else + { + ap_conf->authmode = AUTH_OPEN; + return 0; + } + } + + if (jsonparse_strcmp_value (parser, "channel") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + ap_conf->channel = jsonparse_get_value_as_int (parser); + } + else if (jsonparse_strcmp_value (parser, "ssid") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + os_memcpy (ap_conf->ssid, buffer, os_strlen (buffer)); + } + else if (jsonparse_strcmp_value (parser, "password") == 0) + { + jsonparse_next (parser); + jsonparse_next (parser); + jsonparse_copy_value (parser, buffer, sizeof (buffer)); + os_memcpy (ap_conf->password, buffer, os_strlen (buffer)); + } + } + } + } + + return 0; +} + +static struct jsontree_callback wifi_softap_callback = +JSONTREE_CALLBACK (wifi_softap_get, wifi_softap_set); +JSONTREE_OBJECT (softap_config_tree, + JSONTREE_PAIR ("authmode", + &wifi_softap_callback), + JSONTREE_PAIR ("channel", + &wifi_softap_callback), + JSONTREE_PAIR ("ssid", + &wifi_softap_callback), + JSONTREE_PAIR ("password", &wifi_softap_callback)); +JSONTREE_OBJECT (softap_ip_tree, + JSONTREE_PAIR ("ip", + &wifi_softap_callback), + JSONTREE_PAIR ("mask", + &wifi_softap_callback), + JSONTREE_PAIR ("gw", &wifi_softap_callback)); +JSONTREE_OBJECT (get_softap_tree, + JSONTREE_PAIR ("Connect_Softap", + &softap_config_tree), + JSONTREE_PAIR ("Ipinfo_Softap", &softap_ip_tree)); +JSONTREE_OBJECT (set_softap_tree, + JSONTREE_PAIR ("Ipinfo_Softap", &softap_config_tree)); +JSONTREE_OBJECT (get_wifi_tree, + JSONTREE_PAIR ("Station", + &get_station_tree), + JSONTREE_PAIR ("Softap", &get_softap_tree)); +JSONTREE_OBJECT (set_wifi_tree, + JSONTREE_PAIR ("Station", + &set_station_tree), + JSONTREE_PAIR ("Softap", &set_softap_tree)); +JSONTREE_OBJECT (wifi_response_tree, + JSONTREE_PAIR ("Response", &get_wifi_tree)); +JSONTREE_OBJECT (wifi_request_tree, + JSONTREE_PAIR ("Request", &set_wifi_tree)); +JSONTREE_OBJECT (wifi_info_tree, JSONTREE_PAIR ("wifi", &wifi_response_tree)); +JSONTREE_OBJECT (wifi_req_tree, JSONTREE_PAIR ("wifi", &wifi_request_tree)); +/****************************************************************************** + * FunctionName : scan_get + * Description : set up the scan data as a JSON format + * Parameters : js_ctx -- A pointer to a JSON set up + * Returns : result +*******************************************************************************/ +static int ICACHE_FLASH_ATTR +scan_get (struct jsontree_context *js_ctx) +{ + const char *path = jsontree_path_name (js_ctx, js_ctx->depth - 1); + // STAILQ_HEAD(, bss_info) *pbss = scanarg; + static struct bss_info *bss; + if (os_strncmp (path, "TotalPage", 9) == 0) + { + jsontree_write_int (js_ctx, pscaninfo->totalpage); + } + else if (os_strncmp (path, "PageNum", 7) == 0) + { + jsontree_write_int (js_ctx, pscaninfo->pagenum); + } + else if (os_strncmp (path, "bssid", 5) == 0) + { + bss = STAILQ_FIRST (pscaninfo->pbss); + u8 buffer[32]; + //if (bss != NULL){ + os_memset (buffer, 0, sizeof (buffer)); + os_sprintf (buffer, MACSTR, MAC2STR (bss->bssid)); + jsontree_write_string (js_ctx, buffer); + //} + } + else if (os_strncmp (path, "ssid", 4) == 0) + { + //if (bss != NULL) + jsontree_write_string (js_ctx, bss->ssid); + } + else if (os_strncmp (path, "rssi", 4) == 0) + { + //if (bss != NULL) + jsontree_write_int (js_ctx, -(bss->rssi)); + } + else if (os_strncmp (path, "channel", 7) == 0) + { + //if (bss != NULL) + jsontree_write_int (js_ctx, bss->channel); + } + else if (os_strncmp (path, "authmode", 8) == 0) + { + //if (bss != NULL){ + switch (bss->authmode) + { + case AUTH_OPEN: + jsontree_write_string (js_ctx, "OPEN"); + break; + case AUTH_WEP: + jsontree_write_string (js_ctx, "WEP"); + break; + case AUTH_WPA_PSK: + jsontree_write_string (js_ctx, "WPAPSK"); + break; + case AUTH_WPA2_PSK: + jsontree_write_string (js_ctx, "WPA2PSK"); + break; + case AUTH_WPA_WPA2_PSK: + jsontree_write_string (js_ctx, "WPAPSK/WPA2PSK"); + break; + default: + jsontree_write_int (js_ctx, bss->authmode); + break; + } + + STAILQ_REMOVE_HEAD (pscaninfo->pbss, next); + os_free (bss); + //} + } + + return 0; +} + +static struct jsontree_callback scan_callback = +JSONTREE_CALLBACK (scan_get, NULL); +JSONTREE_OBJECT (scaninfo_tree, + JSONTREE_PAIR ("bssid", &scan_callback), + JSONTREE_PAIR ("ssid", &scan_callback), + JSONTREE_PAIR ("rssi", &scan_callback), + JSONTREE_PAIR ("channel", + &scan_callback), + JSONTREE_PAIR ("authmode", &scan_callback)); +JSONTREE_ARRAY (scanrslt_tree, + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree), + JSONTREE_PAIR_ARRAY (&scaninfo_tree)); +JSONTREE_OBJECT (scantree, + JSONTREE_PAIR ("TotalPage", + &scan_callback), + JSONTREE_PAIR ("PageNum", + &scan_callback), + JSONTREE_PAIR ("ScanResult", &scanrslt_tree)); +JSONTREE_OBJECT (scanres_tree, JSONTREE_PAIR ("Response", &scantree)); +JSONTREE_OBJECT (scan_tree, JSONTREE_PAIR ("scan", &scanres_tree)); +/****************************************************************************** + * FunctionName : parse_url + * Description : parse the received data from the server + * Parameters : precv -- the received data + * purl_frame -- the result of parsing the url + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +parse_url (char *precv, URL_Frame * purl_frame) +{ + char *str = NULL; + uint8 length = 0; + char *pbuffer = NULL; + char *pbufer = NULL; + if (purl_frame == NULL || precv == NULL) + { + return; + } + + pbuffer = (char *) os_strstr (precv, "Host:"); + if (pbuffer != NULL) + { + length = pbuffer - precv; + pbufer = (char *) os_zalloc (length + 1); + pbuffer = pbufer; + os_memcpy (pbuffer, precv, length); + os_memset (purl_frame->pSelect, 0, URLSize); + os_memset (purl_frame->pCommand, 0, URLSize); + os_memset (purl_frame->pFilename, 0, URLSize); + if (os_strncmp (pbuffer, "GET ", 4) == 0) + { + purl_frame->Type = GET; + pbuffer += 4; + } + else if (os_strncmp (pbuffer, "POST ", 5) == 0) + { + purl_frame->Type = POST; + pbuffer += 5; + } + + pbuffer++; + str = (char *) os_strstr (pbuffer, "?"); + if (str != NULL) + { + length = str - pbuffer; + os_memcpy (purl_frame->pSelect, pbuffer, length); + str++; + pbuffer = (char *) os_strstr (str, "="); + if (pbuffer != NULL) + { + length = pbuffer - str; + os_memcpy (purl_frame->pCommand, str, length); + pbuffer++; + str = (char *) os_strstr (pbuffer, "&"); + if (str != NULL) + { + length = str - pbuffer; + os_memcpy (purl_frame->pFilename, pbuffer, length); + } + else + { + str = (char *) os_strstr (pbuffer, " HTTP"); + if (str != NULL) + { + length = str - pbuffer; + os_memcpy (purl_frame->pFilename, pbuffer, length); + } + } + } + } + + os_free (pbufer); + } + else + { + return; + } +} + +static char *precvbuffer; +static uint32 dat_sumlength = 0; +static bool ICACHE_FLASH_ATTR +save_data (char *precv, uint16 length) +{ + bool flag = false; + char length_buf[10] = { + 0 + }; + char *ptemp = NULL; + char *pdata = NULL; + uint16 headlength = 0; + static uint32 totallength = 0; + ptemp = (char *) os_strstr (precv, "\r\n\r\n"); + if (ptemp != NULL) + { + length -= ptemp - precv; + length -= 4; + totallength += length; + headlength = ptemp - precv + 4; + pdata = (char *) os_strstr (precv, "Content-Length: "); + if (pdata != NULL) + { + pdata += 16; + precvbuffer = (char *) os_strstr (pdata, "\r\n"); + if (precvbuffer != NULL) + { + os_memcpy (length_buf, pdata, precvbuffer - pdata); + dat_sumlength = atoi (length_buf); + } + } + else + { + if (totallength != 0x00) + { + totallength = 0; + dat_sumlength = 0; + return false; + } + } + if ((dat_sumlength + headlength) >= 1024) + { + precvbuffer = (char *) os_zalloc (headlength + 1); + os_memcpy (precvbuffer, precv, headlength + 1); + } + else + { + precvbuffer = (char *) os_zalloc (dat_sumlength + headlength + 1); + os_memcpy (precvbuffer, precv, os_strlen (precv)); + } + } + else + { + if (precvbuffer != NULL) + { + totallength += length; + os_memcpy (precvbuffer + os_strlen (precvbuffer), precv, length); + } + else + { + totallength = 0; + dat_sumlength = 0; + return false; + } + } + + if (totallength == dat_sumlength) + { + totallength = 0; + dat_sumlength = 0; + return true; + } + else + { + return false; + } +} + +static bool ICACHE_FLASH_ATTR +check_data (char *precv, uint16 length) +{ + //bool flag = true; + char length_buf[10] = { + 0 + }; + char *ptemp = NULL; + char *pdata = NULL; + char *tmp_precvbuffer; + uint16 tmp_length = length; + uint32 tmp_totallength = 0; + ptemp = (char *) os_strstr (precv, "\r\n\r\n"); + if (ptemp != NULL) + { + tmp_length -= ptemp - precv; + tmp_length -= 4; + tmp_totallength += tmp_length; + pdata = (char *) os_strstr (precv, "Content-Length: "); + if (pdata != NULL) + { + pdata += 16; + tmp_precvbuffer = (char *) os_strstr (pdata, "\r\n"); + if (tmp_precvbuffer != NULL) + { + os_memcpy (length_buf, pdata, tmp_precvbuffer - pdata); + dat_sumlength = atoi (length_buf); + os_printf ("A_dat:%u,tot:%u,lenght:%u\n", dat_sumlength, + tmp_totallength, tmp_length); + if (dat_sumlength != tmp_totallength) + { + return false; + } + } + } + } + return true; +} + +static os_timer_t *restart_10ms; +static rst_parm *rstparm; +/****************************************************************************** + * FunctionName : restart_10ms_cb + * Description : system restart or wifi reconnected after a certain time. + * Parameters : arg -- Additional argument to pass to the function + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +restart_10ms_cb (void *arg) +{ + if (rstparm != NULL && rstparm->pespconn != NULL) + { + switch (rstparm->parmtype) + { + case WIFI: + //if (rstparm->pespconn->state == ESPCONN_CLOSE) { + if (sta_conf->ssid[0] != 0x00) + { + wifi_station_set_config (sta_conf); + wifi_station_disconnect (); + wifi_station_connect (); + user_esp_platform_check_ip (1); + } + + if (ap_conf->ssid[0] != 0x00) + { + wifi_softap_set_config (ap_conf); + system_restart (); + } + + os_free (ap_conf); + ap_conf = NULL; + os_free (sta_conf); + sta_conf = NULL; + os_free (rstparm); + rstparm = NULL; + os_free (restart_10ms); + restart_10ms = NULL; + //} else { + // os_timer_arm(restart_10ms, 10, 0); + //} + break; + case DEEP_SLEEP: + case REBOOT: + if (rstparm->pespconn->state == ESPCONN_CLOSE) + { + wifi_set_opmode (STATION_MODE); + if (rstparm->parmtype == DEEP_SLEEP) + { +#if SENSOR_DEVICE + system_deep_sleep (SENSOR_DEEP_SLEEP_TIME); +#endif + } + } + else + { + os_timer_arm (restart_10ms, 10, 0); + } + + break; + default: + break; + } + } +} + +/****************************************************************************** + * FunctionName : data_send + * Description : processing the data as http format and send to the client or server + * Parameters : arg -- argument to set for client or server + * responseOK -- true or false + * psend -- The send data + * Returns : +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +data_send (void *arg, bool responseOK, char *psend) +{ + uint16 length = 0; + char *pbuf = NULL; + char httphead[256]; + struct espconn *ptrespconn = arg; + os_memset (httphead, 0, 256); + if (responseOK) + { + os_sprintf (httphead, + "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nServer: lwIP/1.4.0\r\n", + psend ? os_strlen (psend) : 0); + if (psend) + { + os_sprintf (httphead + os_strlen (httphead), + "Content-type: application/json\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n\r\n"); + length = os_strlen (httphead) + os_strlen (psend); + pbuf = (char *) os_zalloc (length + 1); + os_memcpy (pbuf, httphead, os_strlen (httphead)); + os_memcpy (pbuf + os_strlen (httphead), psend, os_strlen (psend)); + } + else + { + os_sprintf (httphead + os_strlen (httphead), "\n"); + length = os_strlen (httphead); + } + } + else + { + os_sprintf (httphead, "HTTP/1.0 400 BadRequest\r\n\ +Content-Length: 0\r\nServer: lwIP/1.4.0\r\n\n"); + length = os_strlen (httphead); + } + + if (psend) + { +#ifdef SERVER_SSL_ENABLE + espconn_secure_sent (ptrespconn, pbuf, length); +#else + espconn_sent (ptrespconn, pbuf, length); +#endif + } + else + { +#ifdef SERVER_SSL_ENABLE + espconn_secure_sent (ptrespconn, httphead, length); +#else + espconn_sent (ptrespconn, httphead, length); +#endif + } + + if (pbuf) + { + os_free (pbuf); + pbuf = NULL; + } +} + +/****************************************************************************** + * FunctionName : json_send + * Description : processing the data as json format and send to the client or server + * Parameters : arg -- argument to set for client or server + * ParmType -- json format type + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +json_send (void *arg, ParmType ParmType) +{ + char *pbuf = NULL; + pbuf = (char *) os_zalloc (jsonSize); + struct espconn *ptrespconn = arg; + switch (ParmType) + { +#if LIGHT_DEVICE + + case LIGHT_STATUS: + json_ws_send ((struct jsontree_value *) &PwmTree, "light", pbuf); + break; +#endif +#if PLUG_DEVICE + case SWITCH_STATUS: + json_ws_send ((struct jsontree_value *) &StatusTree, "switch", pbuf); + break; +#endif + case INFOMATION: + json_ws_send ((struct jsontree_value *) &INFOTree, "info", pbuf); + break; + case WIFI: + json_ws_send ((struct jsontree_value *) &wifi_info_tree, "wifi", pbuf); + break; + case CONNECT_STATUS: + json_ws_send ((struct jsontree_value *) &con_status_tree, "info", pbuf); + break; + case USER_BIN: + json_ws_send ((struct jsontree_value *) &userinfo_tree, "user_info", + pbuf); + break; + case SCAN: + { + u8 i = 0; + u8 scancount = 0; + struct bss_info *bss = NULL; + bss = STAILQ_FIRST (pscaninfo->pbss); + if (bss == NULL) + { + os_free (pscaninfo); + pscaninfo = NULL; + os_sprintf (pbuf, "{\n\"successful\": false,\n\"data\": null\n}"); + } + else + { + do + { + if (pscaninfo->page_sn == pscaninfo->pagenum) + { + pscaninfo->page_sn = 0; + os_sprintf (pbuf, + "{\n\"successful\": false,\n\"meessage\": \"repeated page\"\n}"); + break; + } + + scancount = scannum - (pscaninfo->pagenum - 1) * 8; + if (scancount >= 8) + { + pscaninfo->data_cnt += 8; + pscaninfo->page_sn = pscaninfo->pagenum; + if (pscaninfo->data_cnt > scannum) + { + pscaninfo->data_cnt -= 8; + os_sprintf (pbuf, + "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}"); + break; + } + + json_ws_send ((struct jsontree_value *) &scan_tree, + "scan", pbuf); + } + else + { + pscaninfo->data_cnt += scancount; + pscaninfo->page_sn = pscaninfo->pagenum; + if (pscaninfo->data_cnt > scannum) + { + pscaninfo->data_cnt -= scancount; + os_sprintf (pbuf, + "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}"); + break; + } + + char *ptrscanbuf = (char *) os_zalloc (jsonSize); + char *pscanbuf = ptrscanbuf; + os_sprintf (pscanbuf, ",\n\"ScanResult\": [\n"); + pscanbuf += os_strlen (pscanbuf); + for (i = 0; i < scancount; i++) + { + JSONTREE_OBJECT (page_tree, + JSONTREE_PAIR ("page", + &scaninfo_tree)); + json_ws_send ((struct jsontree_value *) &page_tree, + "page", pscanbuf); + os_sprintf (pscanbuf + os_strlen (pscanbuf), ",\n"); + pscanbuf += os_strlen (pscanbuf); + } + + os_sprintf (pscanbuf - 2, "]\n"); + JSONTREE_OBJECT (scantree, + JSONTREE_PAIR ("TotalPage", + &scan_callback), + JSONTREE_PAIR ("PageNum", + &scan_callback)); + JSONTREE_OBJECT (scanres_tree, + JSONTREE_PAIR ("Response", &scantree)); + JSONTREE_OBJECT (scan_tree, + JSONTREE_PAIR ("scan", &scanres_tree)); + json_ws_send ((struct jsontree_value *) &scan_tree, + "scan", pbuf); + os_memcpy (pbuf + os_strlen (pbuf) - 4, ptrscanbuf, + os_strlen (ptrscanbuf)); + os_sprintf (pbuf + os_strlen (pbuf), "}\n}"); + os_free (ptrscanbuf); + } + } + while (0); + } + + break; + } + + default: + break; + } + + data_send (ptrespconn, true, pbuf); + os_free (pbuf); + pbuf = NULL; +} + +/****************************************************************************** + * FunctionName : response_send + * Description : processing the send result + * Parameters : arg -- argument to set for client or server + * responseOK -- true or false + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +response_send (void *arg, bool responseOK) +{ + struct espconn *ptrespconn = arg; + data_send (ptrespconn, responseOK, NULL); +} + +/****************************************************************************** + * FunctionName : json_scan_cb + * Description : processing the scan result + * Parameters : arg -- Additional argument to pass to the callback function + * status -- scan status + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +json_scan_cb (void *arg, STATUS status) +{ + pscaninfo->pbss = arg; + if (scannum % 8 == 0) + { + pscaninfo->totalpage = scannum / 8; + } + else + { + pscaninfo->totalpage = scannum / 8 + 1; + } + + JSONTREE_OBJECT (totaltree, JSONTREE_PAIR ("TotalPage", &scan_callback)); + JSONTREE_OBJECT (totalres_tree, JSONTREE_PAIR ("Response", &totaltree)); + JSONTREE_OBJECT (total_tree, JSONTREE_PAIR ("total", &totalres_tree)); + char *pbuf = NULL; + pbuf = (char *) os_zalloc (jsonSize); + json_ws_send ((struct jsontree_value *) &total_tree, "total", pbuf); + data_send (pscaninfo->pespconn, true, pbuf); + os_free (pbuf); +} + +void ICACHE_FLASH_ATTR +upgrade_check_func (void *arg) +{ + struct espconn *ptrespconn = arg; + os_timer_disarm (&upgrade_check_timer); + if (system_upgrade_flag_check () == UPGRADE_FLAG_START) + { + response_send (ptrespconn, false); + system_upgrade_deinit (); + system_upgrade_flag_set (UPGRADE_FLAG_IDLE); + upgrade_lock = 0; + os_printf ("local upgrade failed\n"); + } + else if (system_upgrade_flag_check () == UPGRADE_FLAG_FINISH) + { + os_printf ("local upgrade success\n"); + response_send (ptrespconn, true); + upgrade_lock = 0; + } + else + { + + } + + +} + +/****************************************************************************** + * FunctionName : upgrade_deinit + * Description : disconnect the connection with the host + * Parameters : bin -- server number + * Returns : none +*******************************************************************************/ +void ICACHE_FLASH_ATTR static +local_upgrade_deinit (void) +{ + if (system_upgrade_flag_check () != UPGRADE_FLAG_START) + { + os_printf ("system upgrade deinit\n"); + system_upgrade_deinit (); + } +} + + +/****************************************************************************** + * FunctionName : upgrade_download + * Description : Processing the upgrade data from the host + * Parameters : bin -- server number + * pusrdata -- The upgrade data (or NULL when the connection has been closed!) + * length -- The length of upgrade data + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +local_upgrade_download (void *arg, char *pusrdata, unsigned short length) +{ + char *ptr = NULL; + char *ptmp2 = NULL; + char lengthbuffer[32]; + static uint32 totallength = 0; + static uint32 sumlength = 0; + char A_buf[2] = { + 0xE9, 0x03 + }; + char B_buf[2] = { + 0xEA, 0x04 + }; + struct espconn *pespconn = arg; + if (totallength == 0 + && (ptr = + (char *) os_strstr (pusrdata, + "\r\n\r\n")) != NULL + && (ptr = (char *) os_strstr (pusrdata, "Content-Length")) != NULL) + { + ptr = (char *) os_strstr (pusrdata, "Content-Length: "); + if (ptr != NULL) + { + ptr += 16; + ptmp2 = (char *) os_strstr (ptr, "\r\n"); + if (ptmp2 != NULL) + { + os_memset (lengthbuffer, 0, sizeof (lengthbuffer)); + os_memcpy (lengthbuffer, ptr, ptmp2 - ptr); + sumlength = atoi (lengthbuffer); + } + else + { + os_printf ("sumlength failed\n"); + } + } + else + { + os_printf ("Content-Length: failed\n"); + } + if (sumlength != 0) + { + system_upgrade_erase_flash (sumlength); + } + ptr = (char *) os_strstr (pusrdata, "\r\n\r\n"); + length -= ptr - pusrdata; + length -= 4; + totallength += length; + os_printf ("upgrade file download start.\n"); + system_upgrade (ptr + 4, length); + } + else + { + totallength += length; + system_upgrade (pusrdata, length); + } + + if (totallength == sumlength) + { + os_printf ("upgrade file download finished.\n"); + system_upgrade_flag_set (UPGRADE_FLAG_FINISH); + totallength = 0; + sumlength = 0; + upgrade_check_func (pespconn); + os_timer_disarm (&app_upgrade_10s); + os_timer_setfn (&app_upgrade_10s, + (os_timer_func_t *) local_upgrade_deinit, NULL); + os_timer_arm (&app_upgrade_10s, 10, 0); + } +} + +/****************************************************************************** + * FunctionName : webserver_recv + * Description : Processing the received data from the server + * Parameters : arg -- Additional argument to pass to the callback function + * pusrdata -- The received data (or NULL when the connection has been closed!) + * length -- The length of received data + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +webserver_recv (void *arg, char *pusrdata, unsigned short length) +{ + URL_Frame *pURL_Frame = NULL; + char *pParseBuffer = NULL; + bool parse_flag = false; + struct espconn *ptrespconn = arg; + if (upgrade_lock == 0) + { + + os_printf ("len:%u\n", length); + if (check_data (pusrdata, length) == false) + { + os_printf ("goto\n"); + goto _temp_exit; + } + + parse_flag = save_data (pusrdata, length); + if (parse_flag == false) + { + response_send (ptrespconn, false); + } + +// os_printf(precvbuffer); + pURL_Frame = (URL_Frame *) os_zalloc (sizeof (URL_Frame)); + parse_url (precvbuffer, pURL_Frame); + switch (pURL_Frame->Type) + { + case GET: + os_printf ("We have a GET request.\n"); + if (os_strcmp (pURL_Frame->pSelect, "client") == 0 && + os_strcmp (pURL_Frame->pCommand, "command") == 0) + { + if (os_strcmp (pURL_Frame->pFilename, "info") == 0) + { + json_send (ptrespconn, INFOMATION); + } + + if (os_strcmp (pURL_Frame->pFilename, "status") == 0) + { + json_send (ptrespconn, CONNECT_STATUS); + } + else if (os_strcmp (pURL_Frame->pFilename, "scan") == 0) + { + char *strstr = NULL; + strstr = (char *) os_strstr (pusrdata, "&"); + if (strstr == NULL) + { + if (pscaninfo == NULL) + { + pscaninfo = + (scaninfo *) os_zalloc (sizeof (scaninfo)); + } + + pscaninfo->pespconn = ptrespconn; + pscaninfo->pagenum = 0; + pscaninfo->page_sn = 0; + pscaninfo->data_cnt = 0; + wifi_station_scan (NULL, json_scan_cb); + } + else + { + strstr++; + if (os_strncmp (strstr, "page", 4) == 0) + { + if (pscaninfo != NULL) + { + pscaninfo->pagenum = *(strstr + 5); + pscaninfo->pagenum -= 0x30; + if (pscaninfo->pagenum > pscaninfo->totalpage + || pscaninfo->pagenum == 0) + { + response_send (ptrespconn, false); + } + else + { + json_send (ptrespconn, SCAN); + } + } + else + { + response_send (ptrespconn, false); + } + } + else + { + response_send (ptrespconn, false); + } + } + } + else + { + response_send (ptrespconn, false); + } + } + else + if (os_strcmp (pURL_Frame->pSelect, "config") == 0 && + os_strcmp (pURL_Frame->pCommand, "command") == 0) + { + if (os_strcmp (pURL_Frame->pFilename, "wifi") == 0) + { + ap_conf = + (struct softap_config *) + os_zalloc (sizeof (struct softap_config)); + sta_conf = + (struct station_config *) + os_zalloc (sizeof (struct station_config)); + json_send (ptrespconn, WIFI); + os_free (sta_conf); + os_free (ap_conf); + sta_conf = NULL; + ap_conf = NULL; + } + +#if PLUG_DEVICE + else if (os_strcmp (pURL_Frame->pFilename, "switch") == 0) + { + json_send (ptrespconn, SWITCH_STATUS); + } + +#endif + +#if LIGHT_DEVICE + else if (os_strcmp (pURL_Frame->pFilename, "light") == 0) + { + json_send (ptrespconn, LIGHT_STATUS); + } + + +#endif + + else if (os_strcmp (pURL_Frame->pFilename, "reboot") == 0) + { + json_send (ptrespconn, REBOOT); + } + else + { + response_send (ptrespconn, false); + } + } + else + if (os_strcmp (pURL_Frame->pSelect, "upgrade") == 0 && + os_strcmp (pURL_Frame->pCommand, "command") == 0) + { + if (os_strcmp (pURL_Frame->pFilename, "getuser") == 0) + { + json_send (ptrespconn, USER_BIN); + } + } + else + { + response_send (ptrespconn, false); + } + + break; + case POST: + os_printf ("We have a POST request.\n"); + pParseBuffer = (char *) os_strstr (precvbuffer, "\r\n\r\n"); + if (pParseBuffer == NULL) + { + break; + } + + pParseBuffer += 4; + if (os_strcmp (pURL_Frame->pSelect, "config") == 0 && + os_strcmp (pURL_Frame->pCommand, "command") == 0) + { +#if SENSOR_DEVICE + + if (os_strcmp (pURL_Frame->pFilename, "sleep") == 0) + { +#else + + if (os_strcmp (pURL_Frame->pFilename, "reboot") == 0) + { +#endif + + if (pParseBuffer != NULL) + { + if (restart_10ms != NULL) + { + os_timer_disarm (restart_10ms); + } + + if (rstparm == NULL) + { + rstparm = + (rst_parm *) os_zalloc (sizeof (rst_parm)); + } + + rstparm->pespconn = ptrespconn; +#if SENSOR_DEVICE + rstparm->parmtype = DEEP_SLEEP; +#else + rstparm->parmtype = REBOOT; +#endif + if (restart_10ms == NULL) + { + restart_10ms = + (os_timer_t *) os_malloc (sizeof (os_timer_t)); + } + + os_timer_setfn (restart_10ms, + (os_timer_func_t *) restart_10ms_cb, + NULL); + os_timer_arm (restart_10ms, 10, 0); // delay 10ms, then do + response_send (ptrespconn, true); + } + else + { + response_send (ptrespconn, false); + } + } + else if (os_strcmp (pURL_Frame->pFilename, "wifi") == 0) + { + if (pParseBuffer != NULL) + { + struct jsontree_context js; + user_esp_platform_set_connect_status + (DEVICE_CONNECTING); + if (restart_10ms != NULL) + { + os_timer_disarm (restart_10ms); + } + + if (ap_conf == NULL) + { + ap_conf = + (struct softap_config *) + os_zalloc (sizeof (struct softap_config)); + } + + if (sta_conf == NULL) + { + sta_conf = + (struct station_config *) + os_zalloc (sizeof (struct station_config)); + } + + jsontree_setup (&js, + (struct jsontree_value *) + &wifi_req_tree, json_putchar); + json_parse (&js, pParseBuffer); + if (rstparm == NULL) + { + rstparm = + (rst_parm *) os_zalloc (sizeof (rst_parm)); + } + + rstparm->pespconn = ptrespconn; + rstparm->parmtype = WIFI; + if (sta_conf->ssid[0] != 0x00 + || ap_conf->ssid[0] != 0x00) + { + ap_conf->ssid_hidden = 0; + ap_conf->max_connection = 4; + if (restart_10ms == NULL) + { + restart_10ms = + (os_timer_t *) + os_malloc (sizeof (os_timer_t)); + } + + os_timer_disarm (restart_10ms); + os_timer_setfn (restart_10ms, + (os_timer_func_t *) restart_10ms_cb, + NULL); + os_timer_arm (restart_10ms, 10, 0); // delay 10ms, then do + } + else + { + os_free (ap_conf); + os_free (sta_conf); + os_free (rstparm); + sta_conf = NULL; + ap_conf = NULL; + rstparm = NULL; + } + + response_send (ptrespconn, true); + } + else + { + response_send (ptrespconn, false); + } + } + +#if PLUG_DEVICE + else if (os_strcmp (pURL_Frame->pFilename, "switch") == 0) + { + if (pParseBuffer != NULL) + { + struct jsontree_context js; + jsontree_setup (&js, + (struct jsontree_value *) &StatusTree, + json_putchar); + json_parse (&js, pParseBuffer); + response_send (ptrespconn, true); + } + else + { + response_send (ptrespconn, false); + } + } + +#endif + +#if LIGHT_DEVICE + else if (os_strcmp (pURL_Frame->pFilename, "light") == 0) + { + if (pParseBuffer != NULL) + { + struct jsontree_context js; + jsontree_setup (&js, (struct jsontree_value *) &PwmTree, + json_putchar); + json_parse (&js, pParseBuffer); + os_printf ("rsp1:%u\n", PostCmdNeeRsp); + if (PostCmdNeeRsp == 0) + PostCmdNeeRsp = 1; + else + response_send (ptrespconn, true); + } + else + { + response_send (ptrespconn, false); + } + } + else if (os_strcmp (pURL_Frame->pFilename, "reset") == 0) + { + response_send (ptrespconn, true); + extern struct esp_platform_saved_param esp_param; + esp_param.activeflag = 0; + system_param_save_with_protect (ESP_PARAM_START_SEC, + &esp_param, + sizeof (esp_param)); + system_restore (); + system_restart (); + } + +#endif + else + { + response_send (ptrespconn, false); + } + } + else + if (os_strcmp (pURL_Frame->pSelect, "upgrade") == 0 && + os_strcmp (pURL_Frame->pCommand, "command") == 0) + { + if (os_strcmp (pURL_Frame->pFilename, "start") == 0) + { + response_send (ptrespconn, true); + os_printf ("local upgrade start\n"); + upgrade_lock = 1; + system_upgrade_init (); + system_upgrade_flag_set (UPGRADE_FLAG_START); + os_timer_disarm (&upgrade_check_timer); + os_timer_setfn (&upgrade_check_timer, + (os_timer_func_t *) upgrade_check_func, + NULL); + os_timer_arm (&upgrade_check_timer, 120000, 0); + } + else if (os_strcmp (pURL_Frame->pFilename, "reset") == 0) + { + + response_send (ptrespconn, true); + os_printf ("local upgrade restart\n"); + system_upgrade_reboot (); + } + else + { + response_send (ptrespconn, false); + } + } + else + { + response_send (ptrespconn, false); + } + break; + } + + if (precvbuffer != NULL) + { + os_free (precvbuffer); + precvbuffer = NULL; + } + os_free (pURL_Frame); + pURL_Frame = NULL; + _temp_exit: + ; + } + else if (upgrade_lock == 1) + { + local_upgrade_download (ptrespconn, pusrdata, length); + if (precvbuffer != NULL) + { + os_free (precvbuffer); + precvbuffer = NULL; + } + os_free (pURL_Frame); + pURL_Frame = NULL; + } +} + +/****************************************************************************** + * FunctionName : webserver_recon + * Description : the connection has been err, reconnection + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +static ICACHE_FLASH_ATTR void +webserver_recon (void *arg, sint8 err) +{ + struct espconn *pesp_conn = arg; + os_printf + ("webserver's %d.%d.%d.%d:%d err %d reconnect\n", + pesp_conn->proto.tcp->remote_ip[0], + pesp_conn->proto.tcp->remote_ip[1], + pesp_conn->proto.tcp->remote_ip[2], + pesp_conn->proto.tcp->remote_ip[3], + pesp_conn->proto.tcp->remote_port, err); +} + +/****************************************************************************** + * FunctionName : webserver_recon + * Description : the connection has been err, reconnection + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +static ICACHE_FLASH_ATTR void +webserver_discon (void *arg) +{ + struct espconn *pesp_conn = arg; + os_printf ("webserver's %d.%d.%d.%d:%d disconnect\n", + pesp_conn->proto.tcp->remote_ip[0], + pesp_conn->proto.tcp->remote_ip[1], + pesp_conn->proto.tcp->remote_ip[2], + pesp_conn->proto.tcp->remote_ip[3], + pesp_conn->proto.tcp->remote_port); +} + +/****************************************************************************** + * FunctionName : user_accept_listen + * Description : server listened a connection successfully + * Parameters : arg -- Additional argument to pass to the callback function + * Returns : none +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +webserver_listen (void *arg) +{ + struct espconn *pesp_conn = arg; + espconn_regist_recvcb (pesp_conn, webserver_recv); + espconn_regist_reconcb (pesp_conn, webserver_recon); + espconn_regist_disconcb (pesp_conn, webserver_discon); +} + +/****************************************************************************** + * FunctionName : user_webserver_init + * Description : parameter initialize as a server + * Parameters : port -- server port + * Returns : none +*******************************************************************************/ + +#endif diff --git a/polycom_recv/webserver.h b/polycom_recv/webserver.h new file mode 100644 index 0000000..3db88fa --- /dev/null +++ b/polycom_recv/webserver.h @@ -0,0 +1,45 @@ +#ifndef __USER_WEBSERVER_H__ +#define __USER_WEBSERVER_H__ + +#define SERVER_PORT 80 +#define SERVER_SSL_PORT 443 + +#define URLSize 10 + +typedef enum Result_Resp { + RespFail = 0, + RespSuc, +} Result_Resp; + +typedef enum ProtocolType { + GET = 0, + POST, +} ProtocolType; + +typedef enum _ParmType { + SWITCH_STATUS = 0, + INFOMATION, + WIFI, + SCAN, + REBOOT, + DEEP_SLEEP, + LIGHT_STATUS, + CONNECT_STATUS, + USER_BIN +} ParmType; + +typedef struct URL_Frame { + enum ProtocolType Type; + char pSelect[URLSize]; + char pCommand[URLSize]; + char pFilename[URLSize]; +} URL_Frame; + +typedef struct _rst_parm { + ParmType parmtype; + struct espconn *pespconn; +} rst_parm; + +void user_webserver_init(uint32 port); + +#endif diff --git a/polycom_recv/wifi.c b/polycom_recv/wifi.c new file mode 100644 index 0000000..9cb31ca --- /dev/null +++ b/polycom_recv/wifi.c @@ -0,0 +1,21 @@ +#include "project.h" +void ICACHE_FLASH_ATTR +wifi_init (void) +{ + struct station_config config = { 0 }; + + os_strcpy (config.ssid, "medaka-bgn"); + os_strcpy (config.password, "fishsoup"); + + wifi_station_set_config_current (&config); + + wifi_set_opmode_current (STATION_MODE); + wifi_set_phy_mode (PHY_MODE_11N); + + wifi_station_set_reconnect_policy (true); + + wifi_station_set_auto_connect (true); + + //wifi_station_connect(); + //wifi_station_dhcpc_start (); +} -- cgit v1.2.3