diff --git a/cores/esp8266/core_esp8266_phy.c b/cores/esp8266/core_esp8266_phy.c index 2391ffa374..67604174e5 100644 --- a/cores/esp8266/core_esp8266_phy.c +++ b/cores/esp8266/core_esp8266_phy.c @@ -25,6 +25,8 @@ #include #include #include "c_types.h" + #include "ets_sys.h" + #include "spi_flash.h" static const uint8_t ICACHE_FLASH_ATTR phy_init_data[128] = { @@ -228,7 +230,7 @@ static const uint8_t ICACHE_FLASH_ATTR phy_init_data[128] = // 3: auto measure frequency offset and correct it, bbpll is 160M, it only can correct + frequency offset. // 5: use 113 byte force_freq_offset to correct frequency offset, bbpll is 168M, it can correct + and - frequency offset. // 7: use 113 byte force_freq_offset to correct frequency offset, bbpll is 160M , it only can correct + frequency offset. - [112] = 3, + [112] = 0, // force_freq_offset // signed, unit is 8kHz @@ -250,14 +252,20 @@ static const uint8_t ICACHE_FLASH_ATTR phy_init_data[128] = #define __get_rf_mode _Z13__get_rf_modev #define __run_user_rf_pre_init _Z22__run_user_rf_pre_initv -extern int __real_register_chipv6_phy(uint8_t* init_data); -extern int __wrap_register_chipv6_phy(uint8_t* init_data) +static bool spoof_init_data = false; + +extern int __real_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size); +extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size); + +extern int ICACHE_RAM_ATTR __wrap_spi_flash_read(uint32_t addr, uint32_t* dst, size_t size) { - if (init_data != NULL) { - memcpy(init_data, phy_init_data, sizeof(phy_init_data)); - init_data[107] = __get_adc_mode(); + if (!spoof_init_data || size != 128) { + return __real_spi_flash_read(addr, dst, size); } - return __real_register_chipv6_phy(init_data); + + memcpy(dst, phy_init_data, sizeof(phy_init_data)); + ((uint8_t*)dst)[107] = __get_adc_mode(); + return 0; } extern int __get_rf_mode(void) __attribute__((weak)); @@ -278,10 +286,16 @@ extern void __run_user_rf_pre_init(void) return; // default do noting } +uint32_t user_rf_cal_sector_set(void) +{ + spoof_init_data = true; + return flashchip->chip_size/SPI_FLASH_SEC_SIZE - 4; +} + void user_rf_pre_init() { // *((volatile uint32_t*) 0x60000710) = 0; - + spoof_init_data = false; volatile uint32_t* rtc_reg = (volatile uint32_t*) 0x60001000; if((rtc_reg[24] >> 16) > 4) { rtc_reg[24] &= 0xFFFF; @@ -295,3 +309,7 @@ void user_rf_pre_init() } __run_user_rf_pre_init(); } + + +void ICACHE_RAM_ATTR user_spi_flash_dio_to_qio_pre_init() {} + diff --git a/platform.txt b/platform.txt index ea8094077a..f8f79de6c9 100644 --- a/platform.txt +++ b/platform.txt @@ -31,7 +31,7 @@ compiler.c.flags=-c {compiler.warning_flags} -Os -g -Wpointer-arith -Wno-implici compiler.S.cmd=xtensa-lx106-elf-gcc compiler.S.flags=-c -g -x assembler-with-cpp -MMD -mlongcalls -compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u call_user_start -u _printf_float -u _scanf_float -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy +compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u call_user_start -u _printf_float -u _scanf_float -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read compiler.c.elf.cmd=xtensa-lx106-elf-gcc compiler.c.elf.libs=-lhal -lphy -lpp -lnet80211 {build.lwip_lib} -lwpa -lcrypto -lmain -lwps -laxtls -lespnow -lsmartconfig -lmesh -lwpa2 -lstdc++ -lm -lc -lgcc diff --git a/tools/sdk/changelog.txt b/tools/sdk/changelog.txt index 14fb2bc3ae..1766ea6140 100644 --- a/tools/sdk/changelog.txt +++ b/tools/sdk/changelog.txt @@ -1,3 +1,30 @@ +ESP8266_NONOS_SDK_V2.0.0_16_07_19 Release Note +---------------------------------------------- +1. Updated libphy.a to 1055, fixed an issue of large current in Light-sleep. +2. Updated AT+ to 1.3.0: + 2.1 Added Light-sleep wakeup command, AT+WAKEUPGPIO; + 2.2 Fixed abnormal AT+CWDHCPS IP allocation; + 2.3 Added at_sdio demo under example directory. +3. Fixed probable system default when calling cur and def interfaces. +4. Fixed the issue of high current in Deep-sleep mode without disabling SPI first. +5. Fixed an issue where the SDK would crash when switching from AP to Station mode by calling wifi_set_phy_mode. +6. Updated secondary boot loader to v1.6: + 6.1 Supports AT + based on SDIO; + 6.2 Supports entering testing mode through GPIO. +7. Added support for MXIC Flash QIO mode. +8. Fixed exception caused during TCP handshake and retransmission. +9. Fixed issues in ESP-NOW. +10. Added ESP-PAIR (Simple-Pair) feature, for APIs please refer to ESP8266 SDK API Guide. +11. wpa2-enterprise time function derivation and time check disable can be set by users. +12. Support for PEAP/TTLS in wpa2-enterprise, for APIs please refer to ESP8266 SDK API Guide. +13. Added mqtt demos under examples directory. +14. Other issue fixes. +15. Resolved an issue that RF_Init_data sector may be broken in stress test. Provided a function user_rf_cal_sector_set which has to be added in application by software developer. More details about user_rf_cal_sector_set refer to documentation "2C-ESP8266__SDK__API Guide". (Resolved in ESP8266_NONOS_SDK_V1.5.4.1) +16. Fix a potential risk that will cause rf not work after external reset. (Resolved in ESP8266_NONOS_SDK_V1.5.4.1) +17. Add SDIO AT support. (Resolved in ESP8266_NONOS_SDK_V1.5.4.1) +18. Fix a potential bug in espconn. (Resolved in ESP8266_NONOS_SDK_V1.5.4.1) + + ESP8266_NONOS_SDK_V1.5.4_16_05_20 Release Note ---------------------------------------------- Optimization: diff --git a/tools/sdk/include/eagle_soc.h b/tools/sdk/include/eagle_soc.h index 04e033067a..2a77e188b0 100644 --- a/tools/sdk/include/eagle_soc.h +++ b/tools/sdk/include/eagle_soc.h @@ -173,6 +173,11 @@ //RTC reg {{ #define REG_RTC_BASE PERIPHS_RTC_BASEADDR +#define RTC_STORE0 (REG_RTC_BASE + 0x030) +#define RTC_STORE1 (REG_RTC_BASE + 0x034) +#define RTC_STORE2 (REG_RTC_BASE + 0x038) +#define RTC_STORE3 (REG_RTC_BASE + 0x03C) + #define RTC_GPIO_OUT (REG_RTC_BASE + 0x068) #define RTC_GPIO_ENABLE (REG_RTC_BASE + 0x074) #define RTC_GPIO_IN_DATA (REG_RTC_BASE + 0x08C) diff --git a/tools/sdk/include/espnow.h b/tools/sdk/include/espnow.h index 3bb6f83394..bd0ebc42bf 100644 --- a/tools/sdk/include/espnow.h +++ b/tools/sdk/include/espnow.h @@ -10,6 +10,7 @@ enum esp_now_role { ESP_NOW_ROLE_IDLE = 0, ESP_NOW_ROLE_CONTROLLER, ESP_NOW_ROLE_SLAVE, + ESP_NOW_ROLE_COMBO, ESP_NOW_ROLE_MAX, }; diff --git a/tools/sdk/include/simple_pair.h b/tools/sdk/include/simple_pair.h new file mode 100644 index 0000000000..ccab95bced --- /dev/null +++ b/tools/sdk/include/simple_pair.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 -2018 Espressif System + * + */ + +#ifndef __SIMPLE_PAIR_H__ +#define __SIMPLE_PAIR_H__ + +typedef enum { + SP_ST_STA_FINISH = 0, + SP_ST_AP_FINISH = 0, + SP_ST_AP_RECV_NEG, + SP_ST_STA_AP_REFUSE_NEG, + /* all following is err */ + SP_ST_WAIT_TIMEOUT, + SP_ST_SEND_ERROR, + SP_ST_KEY_INSTALL_ERR, + SP_ST_KEY_OVERLAP_ERR, //means the same macaddr has two different keys + SP_ST_OP_ERROR, + SP_ST_UNKNOWN_ERROR, + SP_ST_MAX, +} SP_ST_t; + + +typedef void (*simple_pair_status_cb_t)(u8 *sa, u8 status); + +int register_simple_pair_status_cb(simple_pair_status_cb_t cb); +void unregister_simple_pair_status_cb(void); + +int simple_pair_init(void); +void simple_pair_deinit(void); + +int simple_pair_state_reset(void); +int simple_pair_ap_enter_announce_mode(void); +int simple_pair_sta_enter_scan_mode(void); + +int simple_pair_sta_start_negotiate(void); +int simple_pair_ap_start_negotiate(void); +int simple_pair_ap_refuse_negotiate(void); + +int simple_pair_set_peer_ref(u8 *peer_mac, u8 *tmp_key, u8 *ex_key); +int simple_pair_get_peer_ref(u8 *peer_mac, u8 *tmp_key, u8 *ex_key); + + +#endif /* __SIMPLE_PAIR_H__ */ diff --git a/tools/sdk/include/user_interface.h b/tools/sdk/include/user_interface.h index f9195829c9..1dcf7c2f46 100644 --- a/tools/sdk/include/user_interface.h +++ b/tools/sdk/include/user_interface.h @@ -16,6 +16,7 @@ #include "queue.h" #include "user_config.h" #include "spi_flash.h" +#include "gpio.h" #ifndef MAC2STR #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] @@ -178,6 +179,7 @@ typedef struct bss_info { sint16 freq_offset; sint16 freqcal_val; uint8 *esp_mesh_ie; + uint8 simple_pair; } bss_info_t; typedef struct _scaninfo { @@ -606,4 +608,7 @@ bool wifi_set_user_ie(bool enable, uint8 *m_oui, uint8 type, uint8 *user_ie, uin int wifi_register_user_ie_manufacturer_recv_cb(user_ie_manufacturer_recv_cb_t cb); void wifi_unregister_user_ie_manufacturer_recv_cb(void); +void wifi_enable_gpio_wakeup(uint32 i, GPIO_INT_TYPE intr_status); +void wifi_disable_gpio_wakeup(void); + #endif diff --git a/tools/sdk/include/wpa2_enterprise.h b/tools/sdk/include/wpa2_enterprise.h new file mode 100644 index 0000000000..f83b8db7a5 --- /dev/null +++ b/tools/sdk/include/wpa2_enterprise.h @@ -0,0 +1,38 @@ +#ifndef __WPA2_ENTERPRISE_H__ +#define __WPA2_ENTERPRISE_H__ + +typedef long os_time_t; + +struct os_time { + os_time_t sec; + os_time_t usec; +}; + +typedef int (* get_time_func_t)(struct os_time *t); + +int wifi_station_set_wpa2_enterprise_auth(int enable); + +int wifi_station_set_enterprise_cert_key(u8 *client_cert, int client_cert_len, + u8 *private_key, int private_key_len, + u8 *private_key_passwd, int private_key_passwd_len); +void wifi_station_clear_enterprise_cert_key(void); + +int wifi_station_set_enterprise_ca_cert(u8 *ca_cert, int ca_cert_len); +void wifi_station_clear_enterprise_ca_cert(void); + +int wifi_station_set_enterprise_username(u8 *username, int len); +void wifi_station_clear_enterprise_username(void); + +int wifi_station_set_enterprise_password(u8 *password, int len); +void wifi_station_clear_enterprise_password(void); + +int wifi_station_set_enterprise_new_password(u8 *new_password, int len); +void wifi_station_clear_enterprise_new_password(void); + +void wifi_station_set_enterprise_disable_time_check(bool disable); +bool wifi_station_get_enterprise_disable_time_check(void); + +void wpa2_enterprise_set_user_get_time(get_time_func_t cb); + + +#endif /* __WPA2_ENTERPRISE_H__ */ diff --git a/tools/sdk/ld/eagle.app.v6.common.ld b/tools/sdk/ld/eagle.app.v6.common.ld index 2ed1b887b6..a2e082c4a9 100644 --- a/tools/sdk/ld/eagle.app.v6.common.ld +++ b/tools/sdk/ld/eagle.app.v6.common.ld @@ -163,6 +163,8 @@ SECTIONS *libgcc.a:_udivdi3.o(.literal .text) *libsmartconfig.a:(.literal .text .literal.* .text.*) *libstdc++.a:(.literal .text .literal.* .text.*) + *liblwip_gcc.a:(.literal .text .literal.* .text.*) + *liblwip_src.a:(.literal .text .literal.* .text.*) *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text .irom.text.*) _irom0_text_end = ABSOLUTE(.); _flash_code_end = ABSOLUTE(.); diff --git a/tools/sdk/ld/eagle.rom.addr.v6.ld b/tools/sdk/ld/eagle.rom.addr.v6.ld index 19576cb73d..9269fa2de5 100644 --- a/tools/sdk/ld/eagle.rom.addr.v6.ld +++ b/tools/sdk/ld/eagle.rom.addr.v6.ld @@ -20,6 +20,7 @@ PROVIDE ( SPI_read_status = 0x400043c8 ); PROVIDE ( SPI_write_status = 0x40004400 ); PROVIDE ( SPI_write_enable = 0x4000443c ); PROVIDE ( Wait_SPI_Idle = 0x4000448c ); +PROVIDE ( Enable_QMode = 0x400044c0 ); PROVIDE ( SPIEraseArea = 0x40004b44 ); PROVIDE ( SPIEraseBlock = 0x400049b4 ); PROVIDE ( SPIEraseChip = 0x40004984 ); diff --git a/tools/sdk/lib/libat.a b/tools/sdk/lib/libat.a index 36fed2efb1..7dbb6ae146 100644 Binary files a/tools/sdk/lib/libat.a and b/tools/sdk/lib/libat.a differ diff --git a/tools/sdk/lib/libcrypto.a b/tools/sdk/lib/libcrypto.a index 17dcc8ab40..56245a3d58 100644 Binary files a/tools/sdk/lib/libcrypto.a and b/tools/sdk/lib/libcrypto.a differ diff --git a/tools/sdk/lib/libdriver.a b/tools/sdk/lib/libdriver.a new file mode 100644 index 0000000000..68d314c326 Binary files /dev/null and b/tools/sdk/lib/libdriver.a differ diff --git a/tools/sdk/lib/libespnow.a b/tools/sdk/lib/libespnow.a index 964236e83d..ac9b407fae 100644 Binary files a/tools/sdk/lib/libespnow.a and b/tools/sdk/lib/libespnow.a differ diff --git a/tools/sdk/lib/libgcc.a b/tools/sdk/lib/libgcc.a new file mode 100644 index 0000000000..beb9274848 Binary files /dev/null and b/tools/sdk/lib/libgcc.a differ diff --git a/tools/sdk/lib/libjson.a b/tools/sdk/lib/libjson.a index 4f4eefc8b3..f85ce36b4c 100644 Binary files a/tools/sdk/lib/libjson.a and b/tools/sdk/lib/libjson.a differ diff --git a/tools/sdk/lib/liblwip.a b/tools/sdk/lib/liblwip.a index 2e6ff96bda..829db331b5 100644 Binary files a/tools/sdk/lib/liblwip.a and b/tools/sdk/lib/liblwip.a differ diff --git a/tools/sdk/lib/liblwip_536.a b/tools/sdk/lib/liblwip_536.a new file mode 100644 index 0000000000..19b12d93f4 Binary files /dev/null and b/tools/sdk/lib/liblwip_536.a differ diff --git a/tools/sdk/lib/liblwip_gcc.a b/tools/sdk/lib/liblwip_gcc.a index f1cfed0d34..4758c769a3 100644 Binary files a/tools/sdk/lib/liblwip_gcc.a and b/tools/sdk/lib/liblwip_gcc.a differ diff --git a/tools/sdk/lib/libmain.a b/tools/sdk/lib/libmain.a index d9b9ed6756..fdc93e024b 100644 Binary files a/tools/sdk/lib/libmain.a and b/tools/sdk/lib/libmain.a differ diff --git a/tools/sdk/lib/libmesh.a b/tools/sdk/lib/libmesh.a index 8c9b237f71..277ece582a 100644 Binary files a/tools/sdk/lib/libmesh.a and b/tools/sdk/lib/libmesh.a differ diff --git a/tools/sdk/lib/libnet80211.a b/tools/sdk/lib/libnet80211.a index 34cac64a86..5081dd3671 100644 Binary files a/tools/sdk/lib/libnet80211.a and b/tools/sdk/lib/libnet80211.a differ diff --git a/tools/sdk/lib/libphy.a b/tools/sdk/lib/libphy.a index 273baae695..05ab062970 100644 Binary files a/tools/sdk/lib/libphy.a and b/tools/sdk/lib/libphy.a differ diff --git a/tools/sdk/lib/libpp.a b/tools/sdk/lib/libpp.a index 128628e852..bb93cbebbc 100644 Binary files a/tools/sdk/lib/libpp.a and b/tools/sdk/lib/libpp.a differ diff --git a/tools/sdk/lib/libsmartconfig.a b/tools/sdk/lib/libsmartconfig.a index c0d4e27bd6..b85460ccea 100644 Binary files a/tools/sdk/lib/libsmartconfig.a and b/tools/sdk/lib/libsmartconfig.a differ diff --git a/tools/sdk/lib/libssl.a b/tools/sdk/lib/libssl.a index 3565e03c24..4b6beab413 100644 Binary files a/tools/sdk/lib/libssl.a and b/tools/sdk/lib/libssl.a differ diff --git a/tools/sdk/lib/libupgrade.a b/tools/sdk/lib/libupgrade.a index 4ff86489dc..fa6560bdb9 100644 Binary files a/tools/sdk/lib/libupgrade.a and b/tools/sdk/lib/libupgrade.a differ diff --git a/tools/sdk/lib/libwpa.a b/tools/sdk/lib/libwpa.a index 78305ff2ab..18d966d3b5 100644 Binary files a/tools/sdk/lib/libwpa.a and b/tools/sdk/lib/libwpa.a differ diff --git a/tools/sdk/lib/libwpa2.a b/tools/sdk/lib/libwpa2.a index e3ef9db225..22eb333793 100644 Binary files a/tools/sdk/lib/libwpa2.a and b/tools/sdk/lib/libwpa2.a differ diff --git a/tools/sdk/lib/libwps.a b/tools/sdk/lib/libwps.a index 184fb4c0c4..a1c00c09fa 100644 Binary files a/tools/sdk/lib/libwps.a and b/tools/sdk/lib/libwps.a differ diff --git a/tools/sdk/lwip/include/arch/cc.h b/tools/sdk/lwip/include/arch/cc.h index ff03b30709..735e700af5 100644 --- a/tools/sdk/lwip/include/arch/cc.h +++ b/tools/sdk/lwip/include/arch/cc.h @@ -40,6 +40,9 @@ #include "osapi.h" #define EFAULT 14 +#undef ICACHE_FLASH_ATTR +#define ICACHE_FLASH_ATTR + //#define LWIP_PROVIDE_ERRNO #if (1) diff --git a/tools/sdk/lwip/include/lwip/app/espconn.h b/tools/sdk/lwip/include/lwip/app/espconn.h index 9738b5e221..5167ce1fc5 100644 --- a/tools/sdk/lwip/include/lwip/app/espconn.h +++ b/tools/sdk/lwip/include/lwip/app/espconn.h @@ -3,6 +3,7 @@ #include "lwip/dns.h" #include "os_type.h" +#include "lwip/app/espconn_buf.h" #if 0 #define espconn_printf(fmt, args...) os_printf(fmt,## args) @@ -32,6 +33,8 @@ typedef void (* espconn_reconnect_callback)(void *arg, sint8 err); #define ESPCONN_ARG -12 /* Illegal argument. */ #define ESPCONN_IF -14 /* Low_level error */ #define ESPCONN_ISCONN -15 /* Already connected. */ +#define ESPCONN_TIME -16 /* Sync Time error */ +#define ESPCONN_NODATA -17 /* No data can be read */ #define ESPCONN_HANDSHAKE -28 /* ssl handshake failed */ #define ESPCONN_RESP_TIMEOUT -29 /* ssl handshake no response*/ @@ -186,6 +189,8 @@ typedef struct _espconn_msg{ //***********Code for WIFI_BLOCK from upper************** uint8 recv_hold_flag; uint16 recv_holded_buf_Len; +//******************************************************* + ringbuf *readbuf; }espconn_msg; #ifndef _MDNS_INFO diff --git a/tools/sdk/lwip/include/lwip/app/espconn_buf.h b/tools/sdk/lwip/include/lwip/app/espconn_buf.h new file mode 100644 index 0000000000..8bdfaa12ab --- /dev/null +++ b/tools/sdk/lwip/include/lwip/app/espconn_buf.h @@ -0,0 +1,60 @@ +/* + * ringbuf.h + * + * Created on: Apr 22, 2016 + * Author: liuhan + */ + +#ifndef _ESPCONN_BUF_H_ +#define _ESPCONN_BUF_H_ + +/* + * ringbuffer.c + * + * Created on: Apr 22, 2016 + * Author: liuhan + */ +#include "c_types.h" + +#include "ets_sys.h" +#include "os_type.h" + +typedef struct ringbuf_t { + uint8_t *buf; + uint8_t *head, *tail; + size_t size; +} ringbuf, *ringbuf_t; + +ringbuf_t ringbuf_new(size_t capacity); + +size_t ringbuf_buffer_size(const struct ringbuf_t *rb); + +void ringbuf_reset(ringbuf_t rb); + +void ringbuf_free(ringbuf_t *rb); + +size_t ringbuf_capacity(const struct ringbuf_t *rb); + +size_t ringbuf_bytes_free(const struct ringbuf_t *rb); + +size_t ringbuf_bytes_used(const struct ringbuf_t *rb); + +int ringbuf_is_full(const struct ringbuf_t *rb); + +int ringbuf_is_empty(const struct ringbuf_t *rb); + +const void* ringbuf_tail(const struct ringbuf_t *rb); + +const void* ringbuf_head(const struct ringbuf_t *rb); + +static uint8_t *ringbuf_nextp(ringbuf_t rb, const uint8_t *p); + +size_t ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset); + +size_t ringbuf_memset(ringbuf_t dst, int c, size_t len); + +void *ringbuf_memcpy_into(ringbuf_t dst, const void *src, size_t count); + +void *ringbuf_memcpy_from(void *dst, ringbuf_t src, size_t count); + +#endif /* RINGBUF_H_ */ diff --git a/tools/sdk/lwip/include/lwip/app/time.h b/tools/sdk/lwip/include/lwip/app/time.h new file mode 100644 index 0000000000..189d7d1f0d --- /dev/null +++ b/tools/sdk/lwip/include/lwip/app/time.h @@ -0,0 +1,53 @@ +/* + * time.h + * + * Created on: May 31, 2016 + * Author: liuhan + */ + +#ifndef TIME_H_ +#define TIME_H_ +#include "osapi.h" +#include "os_type.h" +#include "lwip/sntp.h" + +struct timeval { + unsigned long tv_sec; /* seconds */ + unsigned long tv_usec; /* and microseconds */ +}; + +/***************************RTC TIME OPTION***************************************/ +// daylight settings +// Base calculated with value obtained from NTP server (64 bits) +#define sntp_base (*((uint64_t*)RTC_STORE0)) +// Timer value when base was obtained +#define TIM_REF_SET(value) WRITE_PERI_REG(RTC_STORE2, value) +#define TIM_REF_GET() READ_PERI_REG(RTC_STORE2) + +// Setters and getters for CAL, TZ and DST. +#define RTC_CAL_SET(val) do {uint32 value = READ_PERI_REG(RTC_STORE3);\ + value |= ((val) & 0x0000FFFF);\ + WRITE_PERI_REG(RTC_STORE3, value);\ + }while(0) +#define RTC_DST_SET(val) do {uint32 value = READ_PERI_REG(RTC_STORE3);\ + value |= (((val)<<16) & 0x00010000);\ + WRITE_PERI_REG(RTC_STORE3, value);\ + }while(0) +#define RTC_TZ_SET(val) do {uint32 value = READ_PERI_REG(RTC_STORE3);\ + value |= (((val)<<24) & 0xFF000000);\ + WRITE_PERI_REG(RTC_STORE3, value);\ + }while(0) + +#define RTC_CAL_GET() (READ_PERI_REG(RTC_STORE3) & 0x0000FFFF) +#define RTC_DST_GET() ((READ_PERI_REG(RTC_STORE3) & 0x00010000)>>16) +#define RTC_TZ_GET() ((((int)READ_PERI_REG(RTC_STORE3)) & ((int)0xFF000000))>>24) +void system_update_rtc(time_t t, uint32_t us); +time_t sntp_get_rtc_time(sint32_t *us); + +int gettimeofday(struct timeval* t, void* timezone); +void updateTime(uint32 ms); +bool configTime(int timezone, int daylightOffset, char *server1, char *server2, char *server3, bool enable); +time_t time(time_t *t); +unsigned long millis(void); +unsigned long micros(void); +#endif /* TIME_H_ */ diff --git a/tools/sdk/lwip/include/lwip/sntp.h b/tools/sdk/lwip/include/lwip/sntp.h index 14e802e15a..e0d523be1e 100644 --- a/tools/sdk/lwip/include/lwip/sntp.h +++ b/tools/sdk/lwip/include/lwip/sntp.h @@ -8,6 +8,8 @@ extern "C" { #endif +typedef long time_t; + /** The maximum number of SNTP servers that can be set */ #ifndef SNTP_MAX_SERVERS #define SNTP_MAX_SERVERS 3 @@ -24,6 +26,8 @@ extern "C" { #define SNTP_SERVER_DNS 1 #endif +bool sntp_get_timetype(void); +void sntp_set_receive_time_size(void); /** One server address/name can be defined as default if SNTP_SERVER_DNS == 1: * #define SNTP_SERVER_ADDRESS "pool.ntp.org" */ diff --git a/tools/sdk/lwip/src/app/dhcpserver.c b/tools/sdk/lwip/src/app/dhcpserver.c index 916b46398b..60736b0cae 100644 --- a/tools/sdk/lwip/src/app/dhcpserver.c +++ b/tools/sdk/lwip/src/app/dhcpserver.c @@ -92,10 +92,12 @@ void ICACHE_FLASH_ATTR node_remove_from_list(list_node **phead, list_node* pdele } else { if (plist == pdelete){ *phead = plist->pnext; + pdelete->pnext = NULL; } else { while (plist != NULL) { if (plist->pnext == pdelete){ plist->pnext = pdelete->pnext; + pdelete->pnext = NULL; } plist = plist->pnext; } @@ -269,7 +271,10 @@ static void ICACHE_FLASH_ATTR create_msg(struct dhcps_msg *m) os_memset((char *) m->file, 0, sizeof(m->file)); os_memset((char *) m->options, 0, sizeof(m->options)); - os_memcpy((char *) m->options, &magic_cookie, sizeof(magic_cookie)); + +//For xiaomi crash bug + uint32 magic_cookie1 = magic_cookie; + os_memcpy((char *) m->options, &magic_cookie1, sizeof(magic_cookie1)); } /////////////////////////////////////////////////////////////////////////////////// /* @@ -760,11 +765,18 @@ void ICACHE_FLASH_ATTR dhcps_stop(void) //udp_remove(pcb_dhcps); list_node *pnode = NULL; list_node *pback_node = NULL; + struct dhcps_pool* dhcp_node = NULL; + struct ip_addr ip_zero; + + os_memset(&ip_zero,0x0,sizeof(ip_zero)); pnode = plist; while (pnode != NULL) { pback_node = pnode; pnode = pback_node->pnext; node_remove_from_list(&plist, pback_node); + dhcp_node = (struct dhcps_pool*)pback_node->pnode; + //wifi_softap_dhcps_client_leave(dhcp_node->mac,&dhcp_node->ip,TRUE); // force to delete + wifi_softap_set_station_info(dhcp_node->mac, &ip_zero); os_free(pback_node->pnode); pback_node->pnode = NULL; os_free(pback_node); @@ -1077,10 +1089,10 @@ uint32 ICACHE_FLASH_ATTR wifi_softap_dhcps_client_update(u8 *bssid, struct ip_ad return IPADDR_ANY; } } else { - if (start_ip == end_ip) { + if (start_ip > end_ip) { return IPADDR_ANY; } - start_ip = htonl((ntohl(start_ip) + 1)); + //start_ip = htonl((ntohl(start_ip) + 1)); flag = TRUE; } } @@ -1096,6 +1108,8 @@ uint32 ICACHE_FLASH_ATTR wifi_softap_dhcps_client_update(u8 *bssid, struct ip_ad // mac exists and ip exists in other node,delete mac node_remove_from_list(&plist,pmac_node); + os_free(pmac_node->pnode); + pmac_node->pnode = NULL; os_free(pmac_node); pmac_node = pip_node; os_memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)); @@ -1144,6 +1158,10 @@ uint32 ICACHE_FLASH_ATTR wifi_softap_dhcps_client_update(u8 *bssid, struct ip_ad os_free(pdhcps_pool); return IPADDR_ANY; } + if (pdhcps_pool->ip.addr > end_ip) { + os_free(pdhcps_pool); + return IPADDR_ANY; + } os_memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)); pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER; pdhcps_pool->type = type; diff --git a/tools/sdk/lwip/src/app/espconn.c b/tools/sdk/lwip/src/app/espconn.c index 8d82e4cd68..93ce6fa4c7 100644 --- a/tools/sdk/lwip/src/app/espconn.c +++ b/tools/sdk/lwip/src/app/espconn.c @@ -35,6 +35,8 @@ remot_info premot[linkMax]; struct espconn_packet pktinfo[2]; +void espconn_init(void); + static uint8 espconn_tcp_get_buf_count(espconn_buf *pesp_buf); /****************************************************************************** * FunctionName : espconn_copy_partial @@ -266,6 +268,8 @@ espconn_connect(struct espconn *espconn) espconn_msg *plist = NULL; remot_info *pinfo = NULL; + volatile int tmp = (int) espconn_init; + if (espconn == NULL) { return ESPCONN_ARG; } else if (espconn ->type != ESPCONN_TCP) @@ -336,6 +340,8 @@ espconn_create(struct espconn *espconn) sint8 value = ESPCONN_OK; espconn_msg *plist = NULL; + volatile int tmp = (int) espconn_init; + if (espconn == NULL) { return ESPCONN_ARG; } else if (espconn ->type != ESPCONN_UDP){ @@ -436,6 +442,39 @@ espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length) return ESPCONN_ARG; } +sint16 ICACHE_FLASH_ATTR espconn_recv(struct espconn *espconn, void *mem, size_t len) +{ + espconn_msg *pnode = NULL; + bool value = false; + int bytes_used = 0; + if (espconn == NULL || mem == NULL || len == 0) + return ESPCONN_ARG; + + /*Find the node depend on the espconn message*/ + value = espconn_find_connection(espconn, &pnode); + if (value && espconn->type == ESPCONN_TCP){ + if (pnode->readbuf != NULL){ + bytes_used = ringbuf_bytes_used(pnode->readbuf); + if (bytes_used != 0) { + if (len > bytes_used) { + len = bytes_used; + } + ringbuf_memcpy_from(mem, pnode->readbuf, len); + espconn_recv_unhold(pnode->pespconn); + return len; + } else { + return ESPCONN_OK; + } + } else{ + return ESPCONN_OK; + } + } else{ + return ESPCONN_ARG; + } + + return ESPCONN_ARG; +} + /****************************************************************************** * FunctionName : espconn_sendto * Description : send data for UDP @@ -877,6 +916,8 @@ espconn_accept(struct espconn *espconn) sint8 value = ESPCONN_OK; espconn_msg *plist = NULL; + volatile int tmp = (int) espconn_init; + if (espconn == NULL) { return ESPCONN_ARG; } else if (espconn ->type != ESPCONN_TCP) diff --git a/tools/sdk/lwip/src/app/espconn_buf.c b/tools/sdk/lwip/src/app/espconn_buf.c new file mode 100644 index 0000000000..f91d1d23ea --- /dev/null +++ b/tools/sdk/lwip/src/app/espconn_buf.c @@ -0,0 +1,205 @@ +/* + * espconn_buf.c + * + * Created on: May 25, 2016 + * Author: liuhan + */ + +#include "lwip/memp.h" +#include "lwip/def.h" +#include "ets_sys.h" +#include "os_type.h" +#include "lwip/app/espconn_buf.h" + + +#ifdef MEMLEAK_DEBUG +static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; +#endif + +#if (!defined(lwIP_unlikely)) +#define lwIP_unlikely(Expression) !!(Expression) +#endif + +#define lwIP_ASSERT(Expression) do{if(!(Expression)) {os_printf("%s %d\n", __func__, __LINE__);return;}}while(0) + +ringbuf_t ringbuf_new(size_t capacity) +{ + ringbuf_t rb = (ringbuf_t)os_zalloc(sizeof(struct ringbuf_t)); + if (rb){ + rb->size = capacity + 1; + rb->buf = (uint8*)os_zalloc(rb->size); + if (rb->buf){ + ringbuf_reset(rb); + }else{ + os_free(rb); + return NULL; + } + } + return rb; +} + +size_t ringbuf_buffer_size(const struct ringbuf_t *rb) +{ + return rb->size; +} + +void ringbuf_reset(ringbuf_t rb) +{ + rb ->head = rb->tail = rb->buf; +} + +void ringbuf_free(ringbuf_t *rb) +{ + lwIP_ASSERT(rb && *rb); + os_free((*rb)->buf); + os_free(*rb); + *rb = NULL; +} + +size_t ringbuf_capacity(const struct ringbuf_t *rb) +{ + return ringbuf_buffer_size(rb) - 1; +} + +static const uint8_t* ringbuf_end(const struct ringbuf_t *rb) +{ + return rb->buf + ringbuf_buffer_size(rb); +} + +size_t ringbuf_bytes_free(const struct ringbuf_t *rb) +{ + if (rb->head >= rb->tail){ + return ringbuf_capacity(rb) - (rb->head - rb->tail); + }else{ + return rb->tail - rb->head -1; + } +} + +size_t ringbuf_bytes_used(const struct ringbuf_t *rb) +{ + return ringbuf_capacity(rb) - ringbuf_bytes_free(rb); +} + +int ringbuf_is_full(const struct ringbuf_t *rb) +{ + return ringbuf_bytes_free(rb) == 0; +} + +int ringbuf_is_empty(const struct ringbuf_t *rb) +{ + return ringbuf_bytes_free(rb) == ringbuf_capacity(rb); +} + +const void* ringbuf_tail(const struct ringbuf_t *rb) +{ + return rb->tail; +} +const void* ringbuf_head(const struct ringbuf_t *rb) +{ + return rb->head; +} + +static uint8_t *ringbuf_nextp(ringbuf_t rb, const uint8_t *p) +{ + lwIP_ASSERT((p >= rb->buf) && (p < ringbuf_end(rb))); + return rb->buf + ((++p -rb->buf) % ringbuf_buffer_size(rb)); +} + +size_t ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset) +{ + const uint8_t *bufend = ringbuf_end(rb); + size_t bytes_used = ringbuf_bytes_used(rb); + if (offset >= bytes_used) + return bytes_used; + + const uint8_t *start = rb ->buf + (((rb->tail - rb->buf) + offset) % ringbuf_buffer_size(rb)); + lwIP_ASSERT(bufend > start); + size_t n = LWIP_MIN(bufend - start, bytes_used - offset); + const uint8_t *found = (const uint8_t *)memchr(start, c, n); + if (found) + return offset + (found - start); + else + return ringbuf_findchr(rb, c, offset + n); +} + +size_t ringbuf_memset(ringbuf_t dst, int c, size_t len) +{ + const uint8_t *bufend = ringbuf_end(dst); + size_t nwritten = 0; + size_t count = LWIP_MIN(len, ringbuf_buffer_size(dst)); + int overflow = count > ringbuf_bytes_free(dst); + + while (nwritten != count){ + + lwIP_ASSERT(bufend > dst->head); + size_t n = LWIP_MIN(bufend - dst->head, count - nwritten); + os_memset(dst->head, c, n); + dst->head += n; + nwritten += n; + + if (dst->head == bufend) + dst->head = dst->buf; + } + + if (overflow){ + dst->tail = ringbuf_nextp(dst, dst->head); + lwIP_ASSERT(ringbuf_is_full(dst)); + } + + return nwritten; +} + +void *ringbuf_memcpy_into(ringbuf_t dst,const void *src, size_t count) +{ + const uint8_t *u8src = src; + const uint8_t *bufend = ringbuf_end(dst); + int overflow = count > ringbuf_bytes_free(dst); + size_t nread = 0; + + while (nread != count){ + lwIP_ASSERT(bufend > dst->head); + size_t n = LWIP_MIN(bufend - dst->head, count - nread); + os_memcpy(dst->head, u8src + nread, n); + dst->head += n; + nread += n; + + if (dst->head == bufend) + dst->head = dst->buf; + } + + if (overflow) { + dst->tail = ringbuf_nextp(dst, dst->head); + lwIP_ASSERT(ringbuf_is_full(dst)); + } + + return dst->head; +} + +void *ringbuf_memcpy_from(void *dst,ringbuf_t src, size_t count) +{ + size_t bytes_used = ringbuf_bytes_used(src); + + if (count > bytes_used) + return NULL; + + const uint8_t *u8dst = dst; + const uint8_t *bufend = ringbuf_end(src); + size_t nwritten = 0; + + while (nwritten != count){ + lwIP_ASSERT(bufend > src->tail); + size_t n = LWIP_MIN(bufend - src->tail, count - nwritten); + os_memcpy((uint8_t*)u8dst + nwritten, src->tail, n); + src->tail += n; + nwritten += n; + + if (src->tail == bufend) + src->tail = src->buf; + } + + lwIP_ASSERT(count + ringbuf_bytes_used(src) == bytes_used); + return src->tail; +} + + + diff --git a/tools/sdk/lwip/src/app/espconn_tcp.c b/tools/sdk/lwip/src/app/espconn_tcp.c index 9be0ac453b..69c86c228e 100644 --- a/tools/sdk/lwip/src/app/espconn_tcp.c +++ b/tools/sdk/lwip/src/app/espconn_tcp.c @@ -239,6 +239,10 @@ void ICACHE_FLASH_ATTR espconn_tcp_memp_free(espconn_msg *pmemp) os_free(pmemp->pespconn); pmemp->pespconn = NULL; } + + if (pmemp->readbuf != NULL){ + ringbuf_free(&pmemp->readbuf); + } os_free(pmemp); pmemp = NULL; } @@ -397,34 +401,46 @@ espconn_tcp_disconnect_successful(void *arg) static void ICACHE_FLASH_ATTR espconn_Task(os_event_t *events) { + espconn_msg *plist = NULL; + bool active_flag = false; espconn_msg *task_msg = NULL; struct espconn *pespconn = NULL; task_msg = (espconn_msg *) events->par; - switch (events->sig) { - case SIG_ESPCONN_WRITE: { - pespconn = task_msg->pespconn; - if (pespconn == NULL) { - return; - } + /*find the active connection node*/ + for (plist = plink_active; plist != NULL; plist = plist->pnext){ + if (task_msg == plist) { + active_flag = true; + break; + } + } + + if (active_flag){ + switch (events->sig) { + case SIG_ESPCONN_WRITE: { + pespconn = task_msg->pespconn; + if (pespconn == NULL) { + return; + } - if (pespconn->proto.tcp->write_finish_fn != NULL) { - pespconn->proto.tcp->write_finish_fn(pespconn); + if (pespconn->proto.tcp->write_finish_fn != NULL) { + pespconn->proto.tcp->write_finish_fn(pespconn); + } } + break; + case SIG_ESPCONN_ERRER: + /*remove the node from the client's active connection list*/ + espconn_list_delete(&plink_active, task_msg); + espconn_tcp_reconnect(task_msg); + break; + case SIG_ESPCONN_CLOSE: + /*remove the node from the client's active connection list*/ + espconn_list_delete(&plink_active, task_msg); + espconn_tcp_disconnect_successful(task_msg); + break; + default: + break; } - break; - case SIG_ESPCONN_ERRER: - /*remove the node from the client's active connection list*/ - espconn_list_delete(&plink_active, task_msg); - espconn_tcp_reconnect(task_msg); - break; - case SIG_ESPCONN_CLOSE: - /*remove the node from the client's active connection list*/ - espconn_list_delete(&plink_active, task_msg); - espconn_tcp_disconnect_successful(task_msg); - break; - default: - break; } } @@ -478,9 +494,13 @@ espconn_tcp_sent(void *arg, uint8 *psent, uint16 length) err = tcp_write(pcb, psent, len, 0); if (err == ERR_MEM) { - len /= 2; + if(len < 3) + len--; + else + len /= 2; } - } while (err == ERR_MEM && len > 1); + + } while (err == ERR_MEM && len > 0); /*Find out what we can send and send it, offset the buffer point for next send*/ if (err == ERR_OK) { @@ -615,7 +635,37 @@ espconn_recv_unhold(struct espconn *pespconn) } //***********Code for WIFI_BLOCK from upper************** +sint8 ICACHE_FLASH_ATTR +espconn_lock_recv(espconn_msg *plockmsg) +{ + if (plockmsg == NULL || plockmsg->pespconn == NULL) { + return ESPCONN_ARG; + } + + if (plockmsg->pespconn->recv_callback == NULL){ + if (plockmsg->readbuf == NULL){ + plockmsg->readbuf = ringbuf_new(TCP_WND); + if (plockmsg->readbuf == NULL) + return ESPCONN_MEM; + } + return espconn_recv_hold(plockmsg->pespconn); + } + return ESPCONN_OK; +} + +sint8 ICACHE_FLASH_ATTR +espconn_unlock_recv(espconn_msg *punlockmsg) +{ + if (punlockmsg == NULL || punlockmsg->pespconn == NULL) { + return ESPCONN_ARG; + } + + if (punlockmsg->pespconn->recv_callback != NULL) + return espconn_recv_unhold(punlockmsg->pespconn); + + return ESPCONN_OK; +} /****************************************************************************** * FunctionName : espconn_client_recv * Description : Data has been received on this pcb. @@ -631,6 +681,8 @@ espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) espconn_msg *precv_cb = arg; tcp_arg(pcb, arg); + /*lock the window because of application layer don't need the data*/ + espconn_lock_recv(precv_cb); if (p != NULL) { /*To update and advertise a larger window*/ @@ -640,30 +692,38 @@ espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) precv_cb->recv_holded_buf_Len += p->tot_len; } - if (err == ERR_OK && p != NULL) { - char *pdata = NULL; - u16_t length = 0; - /*Copy the contents of a packet buffer to an application buffer. - *to prevent memory leaks, ensure that each allocated is deleted*/ - pdata = (char *)os_zalloc(p ->tot_len + 1); - length = pbuf_copy_partial(p, pdata, p ->tot_len, 0); - pbuf_free(p); - - if (length != 0) { - /*switch the state of espconn for application process*/ - precv_cb->pespconn ->state = ESPCONN_READ; - precv_cb->pcommon.pcb = pcb; - if (precv_cb->pespconn->recv_callback != NULL) { - precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length); - } - /*switch the state of espconn for next packet copy*/ - if (pcb->state == ESTABLISHED) - precv_cb->pespconn ->state = ESPCONN_CONNECT; - } + if (precv_cb->pespconn->recv_callback != NULL){ + if (err == ERR_OK && p != NULL) { + char *pdata = NULL; + u16_t length = 0; + /*Copy the contents of a packet buffer to an application buffer. + *to prevent memory leaks, ensure that each allocated is deleted*/ + pdata = (char *)os_zalloc(p ->tot_len + 1); + length = pbuf_copy_partial(p, pdata, p ->tot_len, 0); + pbuf_free(p); + + if (length != 0) { + /*switch the state of espconn for application process*/ + precv_cb->pespconn ->state = ESPCONN_READ; + precv_cb->pcommon.pcb = pcb; + precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length); + + /*switch the state of espconn for next packet copy*/ + if (pcb->state == ESTABLISHED) + precv_cb->pespconn ->state = ESPCONN_CONNECT; + } - /*to prevent memory leaks, ensure that each allocated is deleted*/ - os_free(pdata); - pdata = NULL; + /*to prevent memory leaks, ensure that each allocated is deleted*/ + os_free(pdata); + pdata = NULL; + } + } else{ + /*unregister receive function*/ + struct pbuf *pthis = NULL; + for (pthis = p; pthis != NULL; pthis = pthis->next) { + ringbuf_memcpy_into(precv_cb->readbuf, pthis->payload, pthis->len); + pbuf_free(pthis); + } } if (err == ERR_OK && p == NULL) { @@ -877,6 +937,8 @@ espconn_client_connect(void *arg, struct tcp_pcb *tpcb, err_t err) if (espconn_keepalive_disabled(pcon)) espconn_keepalive_enable(tpcb); +// /*lock the window because of application layer don't need the data*/ +// espconn_lock_recv(pcon); } else{ os_printf("err in host connected (%s)\n",lwip_strerr(err)); } @@ -1015,6 +1077,9 @@ espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) tcp_arg(pcb, arg); espconn_printf("server has application data received: %d\n", system_get_free_heap_size()); + /*lock the window because of application layer don't need the data*/ + espconn_lock_recv(precv_cb); + if (p != NULL) { /*To update and advertise a larger window*/ if(precv_cb->recv_hold_flag == 0) @@ -1023,42 +1088,47 @@ espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) precv_cb->recv_holded_buf_Len += p->tot_len; } - if (err == ERR_OK && p != NULL) { - u8_t *data_ptr = NULL; - u32_t data_cntr = 0; - /*clear the count for connection timeout*/ - precv_cb->pcommon.recv_check = 0; - /*Copy the contents of a packet buffer to an application buffer. - *to prevent memory leaks, ensure that each allocated is deleted*/ - data_ptr = (u8_t *)os_zalloc(p ->tot_len + 1); - data_cntr = pbuf_copy_partial(p, data_ptr, p ->tot_len, 0); - pbuf_free(p); - - if (data_cntr != 0) { - /*switch the state of espconn for application process*/ - precv_cb->pespconn ->state = ESPCONN_READ; - precv_cb->pcommon.pcb = pcb; - if (precv_cb->pespconn->recv_callback != NULL) { - precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr); - } - - /*switch the state of espconn for next packet copy*/ - if (pcb->state == ESTABLISHED) - precv_cb->pespconn ->state = ESPCONN_CONNECT; - } - - /*to prevent memory leaks, ensure that each allocated is deleted*/ - os_free(data_ptr); - data_ptr = NULL; - espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size()); - } else { - if (p != NULL) { - pbuf_free(p); - } + /*register receive function*/ + if (precv_cb->pespconn->recv_callback != NULL) { + if (err == ERR_OK && p != NULL) { + u8_t *data_ptr = NULL; + u32_t data_cntr = 0; + /*clear the count for connection timeout*/ + precv_cb->pcommon.recv_check = 0; + /*Copy the contents of a packet buffer to an application buffer. + *to prevent memory leaks, ensure that each allocated is deleted*/ + data_ptr = (u8_t *) os_zalloc(p ->tot_len + 1); + data_cntr = pbuf_copy_partial(p, data_ptr, p->tot_len, 0); + pbuf_free(p); + + if (data_cntr != 0) { + /*switch the state of espconn for application process*/ + precv_cb->pespconn->state = ESPCONN_READ; + precv_cb->pcommon.pcb = pcb; + precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr); + + /*switch the state of espconn for next packet copy*/ + if (pcb->state == ESTABLISHED) + precv_cb->pespconn->state = ESPCONN_CONNECT; + } - espconn_server_close(precv_cb, pcb,0); - } + /*to prevent memory leaks, ensure that each allocated is deleted*/ + os_free(data_ptr); + data_ptr = NULL; + espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size()); + } + } else { + /*unregister receive function*/ + struct pbuf *pthis = NULL; + for (pthis = p; pthis != NULL; pthis = pthis->next) { + ringbuf_memcpy_into(precv_cb->readbuf, pthis->payload, pthis->len); + pbuf_free(pthis); + } + } + if (err == ERR_OK && p == NULL) { + espconn_server_close(precv_cb, pcb, 0); + } return ERR_OK; } @@ -1292,6 +1362,8 @@ espconn_tcp_accept(void *arg, struct tcp_pcb *pcb, err_t err) if (espconn_keepalive_disabled(paccept)) espconn_keepalive_enable(pcb); +// /*lock the window because of application layer don't need the data*/ +// espconn_lock_recv(paccept); return ERR_OK; } diff --git a/tools/sdk/lwip/src/app/espconn_udp.c b/tools/sdk/lwip/src/app/espconn_udp.c index 6e6b0ab4e8..e3ac8e60f2 100644 --- a/tools/sdk/lwip/src/app/espconn_udp.c +++ b/tools/sdk/lwip/src/app/espconn_udp.c @@ -94,8 +94,8 @@ espconn_udp_sent(void *arg, uint8 *psent, uint16 length) return ESPCONN_ARG; } - if (1470 < length) { - datalen = 1470; + if ((IP_FRAG_MAX_MTU - 20 - 8) < length) { + datalen = IP_FRAG_MAX_MTU - 20 - 8; } else { datalen = length; } @@ -200,8 +200,8 @@ espconn_udp_sendto(void *arg, uint8 *psent, uint16 length) return ESPCONN_ARG; } - if (1470 < length) { - datalen = 1470; + if ((IP_FRAG_MAX_MTU - 20 - 8) < length) { + datalen = IP_FRAG_MAX_MTU - 20 - 8; } else { datalen = length; } diff --git a/tools/sdk/lwip/src/core/dhcp.c b/tools/sdk/lwip/src/core/dhcp.c old mode 100644 new mode 100755 index 342543ea0a..17b2a47e10 --- a/tools/sdk/lwip/src/core/dhcp.c +++ b/tools/sdk/lwip/src/core/dhcp.c @@ -1096,12 +1096,12 @@ dhcp_renew(struct netif *netif) } #endif /* LWIP_NETIF_HOSTNAME */ -#if 0 +#if 1 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr)); #endif -#if 0 +#if 1 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4); dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr)); #endif @@ -1159,7 +1159,7 @@ dhcp_rebind(struct netif *netif) } #endif /* LWIP_NETIF_HOSTNAME */ -#if 0 +#if 1 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr)); diff --git a/tools/sdk/lwip/src/core/dns.c b/tools/sdk/lwip/src/core/dns.c old mode 100644 new mode 100755 index a98095d0a5..5e75f6d5af --- a/tools/sdk/lwip/src/core/dns.c +++ b/tools/sdk/lwip/src/core/dns.c @@ -227,7 +227,7 @@ static ip_addr_t dns_servers[DNS_MAX_SERVERS]; /** Contiguous buffer for processing responses */ //static u8_t dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)]; static u8_t* dns_payload; -static u8_t dns_random; +static u16_t dns_random; /** * Initialize the resolver: set up the UDP pcb and configure the default server * (DNS_SERVER_ADDRESS). @@ -570,7 +570,7 @@ dns_send(u8_t numdns, const char* name, u8_t id) char *query, *nptr; const char *pHostname; u8_t n; - dns_random = os_random()%250; + dns_random = os_random()>>16; LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n", (u16_t)(numdns), name)); LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS); @@ -698,7 +698,7 @@ dns_check_entry(u8_t i) case DNS_STATE_DONE: { /* if the time to live is nul */ - if (--pEntry->ttl == 0) { + if ((pEntry->ttl == 0) || (--pEntry->ttl == 0)) { LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name)); /* flush this entry */ pEntry->state = DNS_STATE_UNUSED; @@ -825,6 +825,13 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t if (pEntry->found) { (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg); } + if (pEntry->ttl == 0) { + /* RFC 883, page 29: "Zero values are + interpreted to mean that the RR can only be used for the + transaction in progress, and should not be cached." + -> flush this entry now */ + goto flushentry; + } /* deallocate memory and return */ goto memerr; } else { @@ -847,6 +854,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t if (pEntry->found) { (*pEntry->found)(pEntry->name, NULL, pEntry->arg); } +flushentry: /* flush this entry */ pEntry->state = DNS_STATE_UNUSED; pEntry->found = NULL; diff --git a/tools/sdk/lwip/src/core/ipv4/igmp.c b/tools/sdk/lwip/src/core/ipv4/igmp.c index 90c82a6319..9c07fd6bc3 100644 --- a/tools/sdk/lwip/src/core/ipv4/igmp.c +++ b/tools/sdk/lwip/src/core/ipv4/igmp.c @@ -149,7 +149,7 @@ static err_t igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, static void igmp_send(struct igmp_group *group, u8_t type)ICACHE_FLASH_ATTR; -static struct igmp_group* igmp_group_list; +static struct igmp_group* igmp_group_list = NULL; static ip_addr_t allsystems; static ip_addr_t allrouters; diff --git a/tools/sdk/lwip/src/core/netif.c b/tools/sdk/lwip/src/core/netif.c index 6913b408f6..cb6b6a84e6 100644 --- a/tools/sdk/lwip/src/core/netif.c +++ b/tools/sdk/lwip/src/core/netif.c @@ -486,6 +486,10 @@ void netif_set_up(struct netif *netif) */ void netif_set_down(struct netif *netif) { + if (netif == NULL) { + return; + } + if (netif->flags & NETIF_FLAG_UP) { netif->flags &= ~NETIF_FLAG_UP; #if LWIP_SNMP diff --git a/tools/sdk/lwip/src/core/sntp.c b/tools/sdk/lwip/src/core/sntp.c old mode 100644 new mode 100755 index 745ba399c0..0aa26b91ce --- a/tools/sdk/lwip/src/core/sntp.c +++ b/tools/sdk/lwip/src/core/sntp.c @@ -51,7 +51,7 @@ #include "lwip/dns.h" #include "lwip/ip_addr.h" #include "lwip/pbuf.h" - +#include "lwip/app/time.h" //#include #if LWIP_UDP @@ -148,14 +148,18 @@ #endif /** SNTP macro to change system time including microseconds */ -#ifdef SNTP_SET_SYSTEM_TIME_US -#define SNTP_CALC_TIME_US 1 -#define SNTP_RECEIVE_TIME_SIZE 2 -#else -#define SNTP_SET_SYSTEM_TIME_US(sec, us) -#define SNTP_CALC_TIME_US 0 -#define SNTP_RECEIVE_TIME_SIZE 1 -#endif +uint8 sntp_receive_time_size = 1; +#define SNTP_RECEIVE_TIME_SIZE sntp_receive_time_size +#define SNTP_SET_SYSTEM_TIME_US(sec, us) sntp_update_rtc(sec, us) +//#ifdef SNTP_SET_SYSTEM_TIME_US +//#define SNTP_SET_SYSTEM_TIME_US(sec, us) sntp_update_rtc(sec, us) +//#define SNTP_CALC_TIME_US 1 +//#define SNTP_RECEIVE_TIME_SIZE 2 +//#else +//#define SNTP_SET_SYSTEM_TIME_US(sec, us) +//#define SNTP_CALC_TIME_US 0 +//#define SNTP_RECEIVE_TIME_SIZE sntp_receive_time_size +//#endif /** SNTP macro to get system time, used with SNTP_CHECK_RESPONSE >= 2 * to send in request and compare in response. @@ -295,10 +299,12 @@ static ip_addr_t sntp_last_server_address; * to compare against in response */ static u32_t sntp_last_timestamp_sent[2]; #endif /* SNTP_CHECK_RESPONSE >= 2 */ -typedef long time_t; + //uint32 current_stamp_1 = 0; //uint32 current_stamp_2 = 0; -uint32 realtime_stamp = 0; +static bool sntp_time_flag = false; +static uint32 sntp_update_delay = SNTP_UPDATE_DELAY; +static uint32 realtime_stamp = 0; LOCAL os_timer_t sntp_timer; /*****************************************/ #define SECSPERMIN 60L @@ -643,13 +649,24 @@ bool ICACHE_FLASH_ATTR sntp_set_timezone(sint8 timezone) { if(timezone >= -11 || timezone <= 13) { - time_zone = timezone; + if (sntp_get_timetype()){ + RTC_TZ_SET(time_zone); + } else + time_zone = timezone; return true; } else { return false; } } + +void ICACHE_FLASH_ATTR sntp_set_daylight(int daylight) +{ + if (sntp_get_timetype()){ + RTC_DST_SET(daylight); + } +} + void ICACHE_FLASH_ATTR sntp_time_inc(void) { @@ -665,7 +682,22 @@ sntp_process(u32_t *receive_timestamp) * @todo: if MSB is 1, SNTP time is 2036-based! */ time_t t = (ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970); - + if (sntp_get_timetype()){ + u32_t us = ntohl(receive_timestamp[1]) / 4295; + SNTP_SET_SYSTEM_TIME_US(t, us); + /* display local time from GMT time */ + LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %"U32_F" us", ctime(&t), us)); + } else{ + /* change system time and/or the update the RTC clock */ + SNTP_SET_SYSTEM_TIME(t); + /* display local time from GMT time */ + t += time_zone * 60 * 60;// format GMT + time_zone TIME ZONE + realtime_stamp = t; + os_timer_disarm(&sntp_timer); + os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL); + os_timer_arm(&sntp_timer, 1000, 1); + } +#if 0 #if SNTP_CALC_TIME_US u32_t us = ntohl(receive_timestamp[1]) / 4295; SNTP_SET_SYSTEM_TIME_US(t, us); @@ -682,10 +714,8 @@ sntp_process(u32_t *receive_timestamp) os_timer_disarm(&sntp_timer); os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL); os_timer_arm(&sntp_timer, 1000, 1); - os_printf("%s\n",sntp_asctime(sntp_localtime (&t))); -// os_printf("%s\n",ctime(&t)); -// LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s", ctime(&t))); #endif /* SNTP_CALC_TIME_US */ +#endif } /** @@ -856,9 +886,9 @@ sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t sntp_process(receive_timestamp); /* Set up timeout for next request */ - sys_timeout((u32_t)SNTP_UPDATE_DELAY, sntp_request, NULL); + sys_timeout((u32_t)sntp_update_delay, sntp_request, NULL); LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n", - (u32_t)SNTP_UPDATE_DELAY)); + (u32_t)sntp_update_delay)); } else if (err == SNTP_ERR_KOD) { /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */ sntp_try_next_server(NULL); @@ -1125,4 +1155,31 @@ sntp_getservername(u8_t idx) } #endif /* SNTP_SERVER_DNS */ +void ICACHE_FLASH_ATTR +sntp_set_update_delay(uint32 ms) +{ + sntp_update_delay = ms > 15000?ms:15000; +} + +void ICACHE_FLASH_ATTR +sntp_set_timetype(bool type) +{ + // sntp_time_flag = type; +} + +bool sntp_get_timetype(void) +{ + return sntp_time_flag; +} + +void ICACHE_FLASH_ATTR +sntp_set_receive_time_size(void) +{ + if (sntp_get_timetype()){ + sntp_receive_time_size = 2; + } else{ + sntp_receive_time_size = 1; + } +} + #endif /* LWIP_UDP */ diff --git a/tools/sdk/version b/tools/sdk/version index 82c7dcf1da..70665c512c 100644 --- a/tools/sdk/version +++ b/tools/sdk/version @@ -1 +1 @@ -1.5.4_16_05_20 \ No newline at end of file +2.0.0_16_08_09 \ No newline at end of file