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

Implement filesystem operation on spare flash and UNO WiFI OTA #32

Merged
merged 5 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
204 changes: 202 additions & 2 deletions main/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,200 @@ int setAnalogWrite(const uint8_t command[], uint8_t response[])
return 6;
}

int writeFile(const uint8_t command[], uint8_t response[]) {
char filename[32 + 1];
size_t len;
size_t offset;

memcpy(&offset, &command[4], command[3]);
memcpy(&len, &command[5 + command[3]], command[4 + command[3]]);

memset(filename, 0x00, sizeof(filename));
memcpy(filename, &command[6 + command[3] + command[4 + command[3]]], command[5 + command[3] + command[4 + command[3]]]);

FILE* f = fopen(filename, "ab+");
if (f == NULL) {
return -1;
}

fseek(f, offset, SEEK_SET);
const uint8_t* data = &command[7 + command[3] + command[4 + command[3]] + command[5 + command[3] + command[4 + command[3]]]];

int ret = fwrite(data, 1, len, f);
fclose(f);

return ret;
}

int readFile(const uint8_t command[], uint8_t response[]) {
char filename[32 + 1];
size_t len;
size_t offset;

memcpy(&offset, &command[4], command[3]);
memcpy(&len, &command[5 + command[3]], command[4 + command[3]]);

memset(filename, 0x00, sizeof(filename));
memcpy(filename, &command[6 + command[3] + command[4 + command[3]]], command[5 + command[3] + command[4 + command[3]]]);

FILE* f = fopen(filename, "rb");
if (f == NULL) {
return -1;
}
fseek(f, offset, SEEK_SET);
int ret = fread(&response[4], len, 1, f);
fclose(f);

response[2] = 1; // number of parameters
response[3] = len; // parameter 1 length

return len + 5;
}

int deleteFile(const uint8_t command[], uint8_t response[]) {
char filename[32 + 1];
size_t len;
size_t offset;

memcpy(&offset, &command[4], command[3]);
memcpy(&len, &command[5 + command[3]], command[4 + command[3]]);

memset(filename, 0x00, sizeof(filename));
memcpy(filename, &command[6 + command[3] + command[4 + command[3]]], command[5 + command[3] + command[4 + command[3]]]);

int ret = -1;
struct stat st;
if (stat(filename, &st) == 0) {
// Delete it if it exists
ret = unlink(filename);
}
return 0;
}

#include <driver/uart.h>

int applyOTA(const uint8_t command[], uint8_t response[]) {
#ifdef UNO_WIFI_REV2

const char* filename = "/fs/UPDATE.BIN";
FILE* updateFile = fopen(filename, "rb");

// init uart and write update to 4809
uart_config_t uart_config;

uart_config.baud_rate = 115200;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.rx_flow_ctrl_thresh = 122;
uart_config.use_ref_tick = true;

uart_param_config(UART_NUM_1, &uart_config);

uart_set_pin(UART_NUM_1,
1, // tx
3, // rx
UART_PIN_NO_CHANGE, // rts
UART_PIN_NO_CHANGE); //cts

uart_driver_install(UART_NUM_1, 1024, 0, 20, NULL, 0);

struct stat st;
stat(filename, &st);

int retries = 0;

size_t remaining = st.st_size % 1024;
for (int i=0; i<st.st_size; i++) {
uint8_t c;
uint8_t d;

fread(&c, 1, 1, updateFile);
retries = 0;
while (retries == 0 || (c != d && retries < 100)) {
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
uart_read_bytes(UART_NUM_1, &d, 1, 10);
retries++;
}
if (retries >= 100) {
goto exit;
}
}
// send remaining bytes (to reach page size) as 0xFF
for (int i=0; i<remaining + 10; i++) {
uint8_t c = 0xFF;
uint8_t d;
retries = 0;
while (retries == 0 || (c != d && retries < 100)) {
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
uart_read_bytes(UART_NUM_1, &d, 1, 10);
retries++;
}
}

// delay a bit before restarting, in case the flashing isn't yet over
delay(200);

pinMode(19, OUTPUT);
digitalWrite(19, HIGH);
delay(200);
digitalWrite(19, LOW);
pinMode(19, INPUT);

exit:
fclose(updateFile);
unlink(filename);

return 0;
#else
return 0;
#endif
}

int existsFile(const uint8_t command[], uint8_t response[]) {
char filename[32 + 1];
size_t len;
size_t offset;

memcpy(&offset, &command[4], command[3]);
memcpy(&len, &command[5 + command[3]], command[4 + command[3]]);

memset(filename, 0x00, sizeof(filename));
memcpy(filename, &command[6 + command[3] + command[4 + command[3]]], command[5 + command[3] + command[4 + command[3]]]);

int ret = -1;

struct stat st;
ret = stat(filename, &st);
if (ret != 0) {
st.st_size = -1;
}
memcpy(&response[4], &(st.st_size), sizeof(st.st_size));

response[2] = 1; // number of parameters
response[3] = sizeof(st.st_size); // parameter 1 length

return 10;
}

int downloadFile(const uint8_t command[], uint8_t response[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing a piece in arduinoOTA but is this needed? From what I remember the arduinoOTA binary POSTs to a web server ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that method to allow a board to perform OTA even if it's behind a NAT (mostly for Create 🙂 ). I used it also for local deployment
to reduce the amount of code that must be changed to adapt WiFi101OTA for UNO WiFi r2

char url[64 + 1];
char filename[64 + 1];

memset(url, 0x00, sizeof(url));
memset(filename, 0x00, sizeof(filename));

memcpy(url, &command[4], command[3]);
memcpy(filename, "/fs/", strlen("/fs/"));
memcpy(&filename[strlen("/fs/")], &command[5 + command[3]], command[4 + command[3]]);

FILE* f = fopen(filename, "w");
downloadAndSaveFile(url, filename, f);
fclose(f);

return 0;
}

typedef int (*CommandHandlerType)(const uint8_t command[], uint8_t response[]);

Expand All @@ -1009,7 +1203,10 @@ const CommandHandlerType commandHandlers[] = {
NULL, NULL, NULL, NULL, sendDataTcp, getDataBufTcp, insertDataBuf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,

// 0x50 -> 0x5f
setPinMode, setDigitalWrite, setAnalogWrite,
setPinMode, setDigitalWrite, setAnalogWrite, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,

// 0x60 -> 0x6f
writeFile, readFile, deleteFile, existsFile, downloadFile, applyOTA, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};

#define NUM_COMMAND_HANDLERS (sizeof(commandHandlers) / sizeof(commandHandlers[0]))
Expand All @@ -1034,6 +1231,9 @@ void CommandHandlerClass::begin()
xTaskCreatePinnedToCore(CommandHandlerClass::gpio0Updater, "gpio0Updater", 8192, NULL, 1, NULL, 1);
}

#define UDIV_UP(a, b) (((a) + (b) - 1) / (b))
#define ALIGN_UP(a, b) (UDIV_UP(a, b) * (b))

int CommandHandlerClass::handle(const uint8_t command[], uint8_t response[])
{
int responseLength = 0;
Expand All @@ -1060,7 +1260,7 @@ int CommandHandlerClass::handle(const uint8_t command[], uint8_t response[])

xSemaphoreGive(_updateGpio0PinSemaphore);

return responseLength;
return ALIGN_UP(responseLength, 4);
}

void CommandHandlerClass::gpio0Updater(void*)
Expand Down
2 changes: 2 additions & 0 deletions main/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ class CommandHandlerClass {

extern CommandHandlerClass CommandHandler;

extern "C" int downloadAndSaveFile(char* url, char* filename, FILE* f);

#endif
65 changes: 65 additions & 0 deletions main/http_client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// esp_http_client.c
#include <dirent.h>
#include <sys/stat.h>
#include "esp_log.h"
#include <stdio.h>

#include "esp_http_client.h"

#define MAX_HTTP_RECV_BUFFER 128

static const char* TAG = "HTTP_HANDLER";

static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
switch(evt->event_id) {
case HTTP_EVENT_ERROR:
case HTTP_EVENT_ON_CONNECTED:
case HTTP_EVENT_HEADER_SENT:
case HTTP_EVENT_ON_FINISH:
case HTTP_EVENT_DISCONNECTED:
case HTTP_EVENT_ON_HEADER:
break;
case HTTP_EVENT_ON_DATA:
if (!esp_http_client_is_chunked_response(evt->client)) {
//fwrite((char*)evt->data, sizeof(uint8_t), evt->data_len, (FILE*)evt->user_data);
}
break;
}
return ESP_OK;
}

int downloadAndSaveFile(char* url, char* filename, FILE* f) {

char *buffer = (char*)malloc(MAX_HTTP_RECV_BUFFER);
if (buffer == NULL) {
return -1;
}
esp_http_client_config_t config = {
.url = url,
.event_handler = _http_event_handler,
.user_data = f,
};

esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err;
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
free(buffer);
return -1;
}
int content_length = esp_http_client_fetch_headers(client);
int total_read_len = 0, read_len;
while (total_read_len < content_length) {
read_len = esp_http_client_read(client, buffer, MAX_HTTP_RECV_BUFFER);
fwrite(buffer, sizeof(uint8_t), read_len, f);
if (read_len <= 0) {
break;
}
total_read_len += read_len;
}
esp_http_client_close(client);
esp_http_client_cleanup(client);
free(buffer);

return 0;
}
17 changes: 17 additions & 0 deletions main/sketch.ino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@

extern "C" {
#include <driver/periph_ctrl.h>

#include <driver/uart.h>
#include <esp_bt.h>

#include "esp_spiffs.h"
#include "esp_log.h"
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include "esp_partition.h"
}

#include <Arduino.h>
Expand Down Expand Up @@ -135,6 +143,15 @@ void setupWiFi() {
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
SPIS.begin();

esp_vfs_spiffs_conf_t conf = {
.base_path = "/fs",
.partition_label = "storage",
.max_files = 20,
.format_if_mount_failed = true
};

esp_err_t ret = esp_vfs_spiffs_register(&conf);

if (WiFi.status() == WL_NO_SHIELD) {
while (1); // no shield
}
Expand Down
1 change: 1 addition & 0 deletions partitions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ nvs, data, nvs, 0x9000, 0x6000
phy_init, data, phy, 0xf000, 0x1000
certs, data, 0x04, 0x10000, 0x20000
factory, app, factory, 0x30000, 0x180000
storage, data, spiffs, 0x1B0000,0x40000