Skip to content

Commit

Permalink
HAA OTA v4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RavenSystem committed Aug 2, 2020
1 parent a90682e commit 1435aab
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 22 deletions.
7 changes: 6 additions & 1 deletion devices/HAA_OTA/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#set this in local.mk
#Set this in local.mk
#PROGRAM = main

EXTRA_COMPONENTS = \
Expand Down Expand Up @@ -56,9 +56,14 @@ EXTRA_WOLFSSL_CFLAGS = \
esp-wolfssl_CFLAGS += $(EXTRA_WOLFSSL_CFLAGS)

# Max HAA OTA Main Installer firmware size is 401392 bytes

EXTRA_CFLAGS += $(EXTRA_WOLFSSL_CFLAGS)
EXTRA_CFLAGS += -DTCP_MSS=1460 -DTCP_WND=2920

EXTRA_CFLAGS += -DSPIFLASH_BASE_ADDR=0xf2000
EXTRA_CFLAGS += -DOLD_SPIFLASH_BASE_ADDR=0x8c000

EXTRA_CFLAGS += -Os

#EXTRA_CFLAGS += -DLWIP_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion devices/HAA_OTA/esp-wifi-config/content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
.nonetworks{text-align:center;margin:1em 0;}
.field{margin-bottom:0.5em;}
.field.required label::before{content:"*";color:red;}
</style></head><body><div class="container"><h1>Home Accessory Architect<br>Installer v3.2.0</h1>
</style></head><body><div class="container"><h1>Home Accessory Architect<br>Installer v4.0.0</h1>
<form action="/settings" method="post">
<div class="field required conf">
<label for="conf">JSON Config:</label>
Expand Down
34 changes: 25 additions & 9 deletions devices/HAA_OTA/esp-wifi-config/src/wifi_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ typedef enum {
} endpoint_t;

typedef struct {
char *ssid_prefix;
char *password;
char* ssid_prefix;
char* password;
void (*on_wifi_ready)();

ETSTimer sta_connect_timeout;
TaskHandle_t http_task_handle;
TaskHandle_t dns_task_handle;

uint8_t check_counter: 7;
} wifi_config_context_t;

static wifi_config_context_t *context;
Expand Down Expand Up @@ -515,6 +517,7 @@ static int wifi_config_server_on_message_complete(http_parser *parser) {
}

case ENDPOINT_SETTINGS_UPDATE: {
sdk_os_timer_disarm(&context->sta_connect_timeout);
wifi_config_context_free(context);
xTaskCreate(wifi_config_server_on_settings_update_task, "on_settings_update_task", (configMINIMAL_STACK_SIZE * 2), client, (tskIDLE_PRIORITY + 0), NULL);
return 0;
Expand Down Expand Up @@ -645,7 +648,7 @@ static void dns_task(void *arg) {
serv_addr.sin_port = htons(53);
bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

const struct timeval timeout = { 2, 0 }; /* 2 second timeout */
const struct timeval timeout = { 1, 200000 }; /* 1.2 second timeout */
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

const struct ifreq ifreq1 = { "en1" };
Expand Down Expand Up @@ -786,18 +789,30 @@ static void wifi_config_sta_connect_timeout_callback(void *arg) {

if (context->on_wifi_ready) {
http_stop();
vTaskDelay(500 / portTICK_PERIOD_MS);
context->on_wifi_ready();
}

wifi_config_context_free(context);
context = NULL;
} else {
context->check_counter++;
if (context->check_counter == 40) {
context->check_counter = 0;
struct netif *netif = sdk_system_get_netif(STATION_IF);
if (netif) {
LOCK_TCPIP_CORE();
dhcp_release_and_stop(netif);
INFO("Restarting DHCP client");
dhcp_start(netif);
UNLOCK_TCPIP_CORE();
}
}
}
}

static void auto_reboot_run() {
INFO("Auto Reboot");
vTaskDelay(150 / portTICK_PERIOD_MS);
vTaskDelay(200 / portTICK_PERIOD_MS);

sdk_system_restart();
}
Expand Down Expand Up @@ -847,6 +862,7 @@ static uint8_t wifi_config_connect() {
}

sdk_wifi_station_set_config(&sta_config);
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_connect();
sdk_wifi_station_set_auto_connect(true);

Expand Down Expand Up @@ -876,11 +892,9 @@ static void wifi_config_station_connect() {
if (wifi_config_connect() == 1 && setup_mode == 0) {
INFO("\nHAA OTA - NORMAL MODE\n");
sysparam_set_int8(HAA_SETUP_MODE_SYSPARAM, 1);

wifi_config_sta_connect_timeout_callback(context);


sdk_os_timer_setfn(&context->sta_connect_timeout, wifi_config_sta_connect_timeout_callback, context);
sdk_os_timer_arm(&context->sta_connect_timeout, 1000, 1);
sdk_os_timer_arm(&context->sta_connect_timeout, 500, 1);

} else {
INFO("\nHAA OTA - SETUP MODE\n");
Expand Down Expand Up @@ -912,5 +926,7 @@ void wifi_config_init(const char *ssid_prefix, const char *password, void (*on_w

context->on_wifi_ready = on_wifi_ready;

context->check_counter = 0;

wifi_config_station_connect();
}
4 changes: 2 additions & 2 deletions devices/HAA_OTA/header.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Home Accessory Architect OTA Update
* Home Accessory Architect OTA Installer
*
* Copyright 2020 José Antonio Jiménez Campos (@RavenSystem)
*
Expand All @@ -8,7 +8,7 @@
#ifndef __HAA_OTA_HEADER_H__
#define __HAA_OTA_HEADER_H__

#define OTAVERSION "3.2.0"
#define OTAVERSION "4.0.0"

#define CUSTOM_REPO_SYSPARAM "ota_sever"
#define PORT_NUMBER_SYSPARAM "ota_port"
Expand Down
2 changes: 1 addition & 1 deletion devices/HAA_OTA/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ EXTRA_CFLAGS += -DHAABOOT
# to $(ROOT)ld/program1.ld and in the copy change this:
# irom0_0_seg : org = 0x40202010, len = (1M - 0x2010)
# to this:
# irom0_0_seg : org = 0x4028D010, len = (0xf5000 - 0x2010)
# irom0_0_seg : org = 0x40290010, len = (0xf2000 - 0x90010)
#
# note that the previous len is forgetting about the system settings area which
# is 9 sectors for esp-open-rtos, and then it is ignored as well...
Expand Down
47 changes: 45 additions & 2 deletions devices/HAA_OTA/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Home Accessory Architect OTA Update
* Home Accessory Architect OTA Installer
*
* Copyright 2020 José Antonio Jiménez Campos (@RavenSystem)
*
Expand All @@ -17,6 +17,7 @@
#include <esp8266.h>
#include <FreeRTOS.h>
#include <task.h>
#include <spiflash.h>

#include <espressif/esp_common.h>

Expand Down Expand Up @@ -46,7 +47,7 @@ void ota_task(void *arg) {
printf("\nHAA Installer Version: %s\n\n", OTAVERSION);

#ifdef HAABOOT
sysparam_set_string(USER_VERSION_SYSPARAM, "none");
sysparam_set_string(USER_VERSION_SYSPARAM, "0.0.0");
#endif // HAABOOT

sysparam_status_t status;
Expand Down Expand Up @@ -89,6 +90,43 @@ void ota_task(void *arg) {
#ifdef HAABOOT
printf("\nRunning HAABOOT\n\n");

printf("HomeKit data migration...\n");
const char magic1[] = "HAP";
char magic[sizeof(magic1)];
memset(magic, 0, sizeof(magic));

if (!spiflash_read(OLD_SPIFLASH_BASE_ADDR, (byte*) magic, sizeof(magic))) {
printf("Failed to read old sector\n");

} else if (strncmp(magic, magic1, sizeof(magic1)) == 0) {
printf("Formatting new sector 0x%x\n", SPIFLASH_BASE_ADDR);
if (!spiflash_erase_sector(SPIFLASH_BASE_ADDR)) {
printf("Failed to erase new sector\n");
} else {
printf("Reading data from 0x%x\n", OLD_SPIFLASH_BASE_ADDR);

byte data[4096];
if (!spiflash_read(OLD_SPIFLASH_BASE_ADDR, data, sizeof(data))) {
printf("Failed to read HomeKit data\n");
} else {
printf("Writting data to 0x%x\n", SPIFLASH_BASE_ADDR);

if (!spiflash_write(SPIFLASH_BASE_ADDR, data, sizeof(data))) {
printf("Failed to write HomeKit data to new sector\n");
} else {
printf("Erasing old sector 0x%x\n", OLD_SPIFLASH_BASE_ADDR);
if (!spiflash_erase_sector(OLD_SPIFLASH_BASE_ADDR)) {
printf("Failed to erase old sector\n");
} else {
printf("HomeKit data is migrated\n");
}
}
}
}
} else {
printf("Data is already migrated\n\n");
}

if (ota_get_sign(user_repo, OTAMAINFILE, signature, port, is_ssl) > 0) {
file_size = ota_get_file(user_repo, OTAMAINFILE, BOOT1SECTOR, port, is_ssl);
if (file_size > 0 && ota_verify_sign(BOOT1SECTOR, file_size, signature) == 0) {
Expand Down Expand Up @@ -160,6 +198,9 @@ void ota_task(void *arg) {

vTaskDelay(5000 / portTICK_PERIOD_MS);
}
} else {
printf("\n!!! Error readind HAAMAIN Version. Fixing...\n\n");
sysparam_set_string(USER_VERSION_SYSPARAM, "0.0.0");
}

ota_reboot();
Expand All @@ -170,8 +211,10 @@ void on_wifi_ready() {
}

void user_init(void) {
sdk_wifi_station_set_auto_connect(false);
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_disconnect();
sdk_wifi_set_sleep_type(WIFI_SLEEP_NONE);

uart_set_baud(0, 115200);

Expand Down
2 changes: 1 addition & 1 deletion devices/HAA_OTA/ota.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Home Accessory Architect OTA Update
* Home Accessory Architect OTA Installer
*
* Copyright 2020 José Antonio Jiménez Campos (@RavenSystem)
*
Expand Down
10 changes: 5 additions & 5 deletions devices/HAA_OTA/ota.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Home Accessory Architect OTA Update
* Home Accessory Architect OTA Installer
*
* Copyright 2020 José Antonio Jiménez Campos (@RavenSystem)
*
Expand Down Expand Up @@ -29,22 +29,22 @@

#define SECTORSIZE 4096
#define BOOT0SECTOR 0x02000
#define BOOT1SECTOR 0x8D000 // Must match the program1.ld value
#define BOOT1SECTOR 0x90000 // Must match the program1.ld value

#define HTTPS_PORT 443
#define FAILED "failed\n"
#define REQUESTHEAD "GET /"
#define REQUESTTAIL " HTTP/1.1\r\nHost: "
#define CRLFCRLF "\r\n\r\n"
#define RECV_BUF_LEN 1025 // current length of amazon URL 724
#define RECV_BUF_LEN 1025 // Current length of amazon URL 724
#define HOST_LEN 256
#define RANGE "\r\nRange: bytes="

#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
#define MAX_302_JUMPS 6

#define HASHSIZE 48 //SHA-384
#define SIGNSIZE 104 //ECDSA r+s in ASN1 format secP384r1
#define HASHSIZE 48 //SHA-384
#define SIGNSIZE 104 //ECDSA r+s in ASN1 format secP384r1

typedef unsigned char byte;

Expand Down

0 comments on commit 1435aab

Please sign in to comment.