From 7fe866ed3c89091ec4cfe9483d8858d9a6187367 Mon Sep 17 00:00:00 2001 From: fvpalha Date: Tue, 26 Jul 2016 10:54:09 -0300 Subject: [PATCH] Define your own host and port by command. --- Makefile | 21 ++++++++++++++++++++- main.c | 2 ++ rboot-ota.c | 25 ++++++++++++++++++++----- rboot-ota.h | 6 ++++-- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 620ccd5..abd6992 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,15 @@ SDK_BASE ?= /opt/esp-open-sdk/sdk SDK_LIBDIR = lib SDK_INCDIR = include -ESPTOOL2 ?= ../esptool2/esptool2 +ESPTOOL ?= /opt/esp-open-sdk/esptool/esptool.py +ESPTOOL2 ?= /opt/esp-open-sdk/esptool2/esptool2 +RBOOT ?= /opt/esp-open-sdk/rboot/firmware/rboot.bin FW_SECTS = .text .data .rodata FW_USER_ARGS = -quiet -bin -boot2 +ESPPORT ?= /dev/ttyUSB0 +ESPBAUD ?= 115200 + ifndef XTENSA_BINDIR CC := xtensa-lx106-elf-gcc LD := xtensa-lx106-elf-gcc @@ -70,6 +75,20 @@ $(BUILD_DIR): $(FIRMW_DIR): @mkdir -p $@ +flash-rboot: + $(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) 0x00000 $(RBOOT) + +flash-sampleproject: + $(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) 0x02000 $(FIRMW_DIR)/rom0.bin + $(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) 0x82000 $(FIRMW_DIR)/rom1.bin + +flash-all: all flash-rboot flash-sampleproject flash-init + +flash-init: + $(ESPTOOL) -p $(ESPPORT) -b $(ESPBAUD) write_flash $(flashimageoptions) \ + 0x3fc000 $(SDK_BASE)/bin/esp_init_data_default.bin \ + 0xfc000 blank4.bin + clean: @echo "RM $(BUILD_DIR) $(FIRMW_DIR)" @rm -rf $(BUILD_DIR) diff --git a/main.c b/main.c index 5624d9f..350d19e 100644 --- a/main.c +++ b/main.c @@ -125,6 +125,8 @@ static void ICACHE_FLASH_ATTR OtaUpdate_CallBack(bool result, uint8 rom_slot) { static void ICACHE_FLASH_ATTR OtaUpdate() { + setOtaHostIp ("192.168.1.2"); + setOtaHostPort (80); // start the upgrade process if (rboot_ota_start((ota_callback)OtaUpdate_CallBack)) { uart0_send("Updating...\r\n"); diff --git a/rboot-ota.c b/rboot-ota.c index 351098a..249322e 100644 --- a/rboot-ota.c +++ b/rboot-ota.c @@ -34,6 +34,21 @@ typedef struct { static upgrade_status *upgrade; static os_timer_t ota_timer; +//static const char ota_host_ip[32]; +char *ota_host_ip; +int ota_host_port; + +void ICACHE_FLASH_ATTR setOtaHostIp (char *value) { + ota_host_ip = value; +} + +char* ICACHE_FLASH_ATTR getOtaHostIp (void) { + return ota_host_ip; +} + +void ICACHE_FLASH_ATTR setOtaHostPort (int value) { + ota_host_port = value; +} // clean up at the end of the update // will call the user call back to indicate completion @@ -178,8 +193,8 @@ static void ICACHE_FLASH_ATTR upgrade_connect_cb(void *arg) { return; } os_sprintf((char*)request, - "GET /%s HTTP/1.1\r\nHost: " OTA_HOST "\r\n" HTTP_HEADER, - (upgrade->rom_slot == FLASH_BY_ADDR ? OTA_FILE : (upgrade->rom_slot == 0 ? OTA_ROM0 : OTA_ROM1))); + "GET /%s HTTP/1.1\r\nHost: %s\r\n" HTTP_HEADER, + (upgrade->rom_slot == FLASH_BY_ADDR ? OTA_FILE : (upgrade->rom_slot == 0 ? OTA_ROM0 : OTA_ROM1)), ota_host_ip); // send the http request, with timeout for reply os_timer_setfn(&ota_timer, (os_timer_func_t *)rboot_ota_deinit, 0); @@ -238,7 +253,7 @@ static void ICACHE_FLASH_ATTR upgrade_resolved(const char *name, ip_addr_t *ip, if (ip == 0) { uart0_send("DNS lookup failed for: "); - uart0_send(OTA_HOST); + uart0_send(ota_host_ip); uart0_send("\r\n"); // not connected so don't call disconnect on the connection // but call our own disconnect callback to do the cleanup @@ -250,7 +265,7 @@ static void ICACHE_FLASH_ATTR upgrade_resolved(const char *name, ip_addr_t *ip, upgrade->conn->type = ESPCONN_TCP; upgrade->conn->state = ESPCONN_NONE; upgrade->conn->proto.tcp->local_port = espconn_port(); - upgrade->conn->proto.tcp->remote_port = OTA_PORT; + upgrade->conn->proto.tcp->remote_port = ota_host_port; *(ip_addr_t*)upgrade->conn->proto.tcp->remote_ip = *ip; // set connection call backs espconn_regist_connectcb(upgrade->conn, upgrade_connect_cb); @@ -320,7 +335,7 @@ bool ICACHE_FLASH_ATTR rboot_ota_start(ota_callback callback) { system_upgrade_flag_set(UPGRADE_FLAG_START); // dns lookup - result = espconn_gethostbyname(upgrade->conn, OTA_HOST, &upgrade->ip, upgrade_resolved); + result = espconn_gethostbyname(upgrade->conn, ota_host_ip, &upgrade->ip, upgrade_resolved); if (result == ESPCONN_OK) { // hostname is already cached or is actually a dotted decimal ip address upgrade_resolved(0, &upgrade->ip, upgrade->conn); diff --git a/rboot-ota.h b/rboot-ota.h index 1f9a51b..ff82560 100644 --- a/rboot-ota.h +++ b/rboot-ota.h @@ -16,8 +16,6 @@ extern "C" { #endif // ota server details -#define OTA_HOST "192.168.7.5" -#define OTA_PORT 80 #define OTA_ROM0 "rom0.bin" #define OTA_ROM1 "rom1.bin" // OTA_FILE is not required, but is part of the example @@ -40,6 +38,10 @@ Accept: */*\r\n\r\n" // callback method should take this format typedef void (*ota_callback)(bool result, uint8 rom_slot); +void ICACHE_FLASH_ATTR setOtaHostIp (char *value); +char* ICACHE_FLASH_ATTR getOtaHostIp (void); +void ICACHE_FLASH_ATTR setOtaHostPort (int value); + // function to perform the ota update bool ICACHE_FLASH_ATTR rboot_ota_start(ota_callback callback);