Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define your own host and port by command. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
25 changes: 20 additions & 5 deletions rboot-ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions rboot-ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down