From 5333bdf8a6686d5763bb819c28e01fa474bc8411 Mon Sep 17 00:00:00 2001 From: Pascal Gollor Date: Sun, 18 Oct 2015 10:52:17 +0200 Subject: [PATCH 1/9] update SPIFFS to version 0.3.3 --- cores/esp8266/spiffs/spiffs.h | 38 ++++++--- cores/esp8266/spiffs/spiffs_check.c | 16 ++-- cores/esp8266/spiffs/spiffs_config.h | 33 ++++++-- cores/esp8266/spiffs/spiffs_gc.c | 4 +- cores/esp8266/spiffs/spiffs_hydrogen.c | 112 ++++++++++++++++++++----- cores/esp8266/spiffs/spiffs_nucleus.c | 89 ++++++++++---------- cores/esp8266/spiffs/spiffs_nucleus.h | 14 ++-- 7 files changed, 205 insertions(+), 101 deletions(-) diff --git a/cores/esp8266/spiffs/spiffs.h b/cores/esp8266/spiffs/spiffs.h index 7b59de9ed..0a8d0220f 100644 --- a/cores/esp8266/spiffs/spiffs.h +++ b/cores/esp8266/spiffs/spiffs.h @@ -5,8 +5,6 @@ * Author: petera */ - - #ifndef SPIFFS_H_ #define SPIFFS_H_ #if defined(__cplusplus) @@ -186,6 +184,11 @@ typedef struct { // logical size of a page, must be at least // log_block_size / 8 u32_t log_page_size; + +#endif +#if SPIFFS_FILEHDL_OFFSET + // an integer offset added to each file handle + u16_t fh_ix_offset; #endif } spiffs_config; @@ -310,7 +313,7 @@ void SPIFFS_unmount(spiffs *fs); * @param path the path of the new file * @param mode ignored, for posix compliance */ -s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode); +s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode); /** * Opens/creates a file. @@ -321,7 +324,7 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode); * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT * @param mode ignored, for posix compliance */ -spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode); +spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode); /** @@ -360,13 +363,14 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len); s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len); /** - * Moves the read/write file offset + * Moves the read/write file offset. Resulting offset is returned or negative if error. + * lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset. * @param fs the file system struct * @param fh the filehandle * @param offs how much/where to move the offset * @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes * if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset - * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offset + * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative */ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence); @@ -375,7 +379,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence); * @param fs the file system struct * @param path the path of the file to remove */ -s32_t SPIFFS_remove(spiffs *fs, char *path); +s32_t SPIFFS_remove(spiffs *fs, const char *path); /** * Removes a file by filehandle @@ -390,7 +394,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh); * @param path the path of the file to stat * @param s the stat struct to populate */ -s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s); +s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s); /** * Gets file status by filehandle @@ -420,7 +424,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh); * @param old path of file to rename * @param newPath new path of file */ -s32_t SPIFFS_rename(spiffs *fs, char *old, char *newPath); +s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath); /** * Returns last error of last file operation. @@ -443,7 +447,7 @@ void SPIFFS_clearerr(spiffs *fs); * @param name the name of the directory * @param d pointer the directory stream to be populated */ -spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d); +spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d); /** * Closes a directory stream @@ -544,6 +548,20 @@ s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages); */ s32_t SPIFFS_gc(spiffs *fs, u32_t size); +/** + * Check if EOF reached. + * @param fs the file system struct + * @param fh the filehandle of the file to check + */ +s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh); + +/** + * Get position in file. + * @param fs the file system struct + * @param fh the filehandle of the file to check + */ +s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh); + #if SPIFFS_TEST_VISUALISATION /** * Prints out a visualization of the filesystem. diff --git a/cores/esp8266/spiffs/spiffs_check.c b/cores/esp8266/spiffs/spiffs_check.c index 180ce345a..0576843e3 100644 --- a/cores/esp8266/spiffs/spiffs_check.c +++ b/cores/esp8266/spiffs/spiffs_check.c @@ -440,9 +440,9 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s } static s32_t spiffs_lookup_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, int cur_entry, - u32_t user_data, void *user_p) { - (void)user_data; - (void)user_p; + const void *user_const_p, void *user_var_p) { + (void)user_const_p; + (void)user_var_p; s32_t res = SPIFFS_OK; spiffs_page_header p_hdr; spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry); @@ -873,11 +873,11 @@ static int spiffs_object_index_search(spiffs *fs, spiffs_obj_id obj_id) { } static s32_t spiffs_object_index_consistency_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, - int cur_entry, u32_t user_data, void *user_p) { - (void)user_data; + int cur_entry, const void *user_const_p, void *user_var_p) { + (void)user_const_p; s32_t res_c = SPIFFS_VIS_COUNTINUE; s32_t res = SPIFFS_OK; - u32_t *log_ix = (u32_t *)user_p; + u32_t *log_ix = (u32_t*)user_var_p; spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work; CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, @@ -977,8 +977,8 @@ s32_t spiffs_object_index_consistency_check(spiffs *fs) { memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); u32_t obj_id_log_ix = 0; CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 0, 0); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, 0, &obj_id_log_ix, - 0, 0); + res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, &obj_id_log_ix, + 0, 0, 0); if (res == SPIFFS_VIS_END) { res = SPIFFS_OK; } diff --git a/cores/esp8266/spiffs/spiffs_config.h b/cores/esp8266/spiffs/spiffs_config.h index 10497c819..2d83c261d 100644 --- a/cores/esp8266/spiffs/spiffs_config.h +++ b/cores/esp8266/spiffs/spiffs_config.h @@ -19,7 +19,7 @@ #define c_printf ets_printf #define c_memset memset -typedef signed short file_t; +typedef int16_t file_t; typedef int32_t s32_t; typedef uint32_t u32_t; typedef int16_t s16_t; @@ -46,20 +46,20 @@ typedef uint8_t u8_t; // compile time switches // Set generic spiffs debug output call. -#ifndef SPIFFS_DGB -#define SPIFFS_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_DBG +#define SPIFFS_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for garbage collecting. -#ifndef SPIFFS_GC_DGB -#define SPIFFS_GC_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_GC_DBG +#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for caching. -#ifndef SPIFFS_CACHE_DGB -#define SPIFFS_CACHE_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_CACHE_DBG +#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for system consistency checks. -#ifndef SPIFFS_CHECK_DGB -#define SPIFFS_CHECK_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_CHECK_DBG +#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__) #endif // Enable/disable API functions to determine exact number of bytes @@ -189,6 +189,21 @@ typedef uint8_t u8_t; #define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1 #endif +// Enable this if you want the HAL callbacks to be called with the spiffs struct +#ifndef SPIFFS_HAL_CALLBACK_EXTRA +#define SPIFFS_HAL_CALLBACK_EXTRA 0 +#endif + +// Enable this if you want to add an integer offset to all file handles +// (spiffs_file). This is useful if running multiple instances of spiffs on +// same target, in order to recognise to what spiffs instance a file handle +// belongs. +// NB: This adds config field fh_ix_offset in the configuration struct when +// mounting, which must be defined. +#ifndef SPIFFS_FILEHDL_OFFSET +#define SPIFFS_FILEHDL_OFFSET 0 +#endif + // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function // in the api. This function will visualize all filesystem using given printf // function. diff --git a/cores/esp8266/spiffs/spiffs_gc.c b/cores/esp8266/spiffs/spiffs_gc.c index b60d7d3f2..91d7e54a1 100644 --- a/cores/esp8266/spiffs/spiffs_gc.c +++ b/cores/esp8266/spiffs/spiffs_gc.c @@ -36,7 +36,7 @@ s32_t spiffs_gc_quick( int cur_entry = 0; spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work; - SPIFFS_GC_DBG("gc_quick: running\n", cur_block); + SPIFFS_GC_DBG("gc_quick: running\n"); #if SPIFFS_GC_STATS fs->stats_gc_runs++; #endif @@ -255,7 +255,7 @@ s32_t spiffs_gc_find_candidate( // align cand_scores on s32_t boundary #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpointer-to-int-cast" - cand_scores = (s32_t*)(((u32_t)cand_scores + sizeof(s32_t) - 1) & ~(sizeof(s32_t) - 1)); + cand_scores = (s32_t*)(((ptrdiff_t)cand_scores + sizeof(ptrdiff_t) - 1) & ~(sizeof(ptrdiff_t) - 1)); #pragma GCC diagnostic pop *block_candidates = cand_blocks; diff --git a/cores/esp8266/spiffs/spiffs_hydrogen.c b/cores/esp8266/spiffs/spiffs_hydrogen.c index 72b75d6ef..6c31da2de 100644 --- a/cores/esp8266/spiffs/spiffs_hydrogen.c +++ b/cores/esp8266/spiffs/spiffs_hydrogen.c @@ -8,6 +8,14 @@ #include "spiffs.h" #include "spiffs_nucleus.h" +#if SPIFFS_FILEHDL_OFFSET +#define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0) +#define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0) +#else +#define SPIFFS_FH_OFFS(fs, fh) (fh) +#define SPIFFS_FH_UNOFFS(fs, fh) (fh) +#endif + #if SPIFFS_CACHE == 1 static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh); #endif @@ -57,9 +65,12 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work, u8_t *fd_space, u32_t fd_space_size, void *cache, u32_t cache_size, spiffs_check_callback check_cb_f) { + void *user_data; SPIFFS_LOCK(fs); + user_data = fs->user_data; memset(fs, 0, sizeof(spiffs)); memcpy(&fs->cfg, config, sizeof(spiffs_config)); + fs->user_data = user_data; fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs); fs->work = &work[0]; fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)]; @@ -155,7 +166,7 @@ void SPIFFS_clearerr(spiffs *fs) { fs->err_code = SPIFFS_OK; } -s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { +s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) { (void)mode; SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); @@ -163,15 +174,15 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { spiffs_obj_id obj_id; s32_t res; - res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (u8_t *)path); + res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_create(fs, obj_id, (u8_t *)path, SPIFFS_TYPE_FILE, 0); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, 0); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); SPIFFS_UNLOCK(fs); return 0; } -spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode) { +spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) { (void)mode; SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); @@ -183,7 +194,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode s32_t res = spiffs_fd_find_new(fs, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); if ((flags & SPIFFS_CREAT) == 0) { if (res < SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); @@ -207,7 +218,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode spiffs_fd_return(fs, fd->file_nbr); } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, &pix); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, &pix); if (res < SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); } @@ -236,7 +247,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode SPIFFS_UNLOCK(fs); - return fd->file_nbr; + return SPIFFS_FH_OFFS(fs, fd->file_nbr); } spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) { @@ -266,7 +277,7 @@ spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_fl SPIFFS_UNLOCK(fs); - return fd->file_nbr; + return SPIFFS_FH_OFFS(fs, fd->file_nbr); } s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { @@ -277,6 +288,7 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -285,6 +297,12 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { SPIFFS_API_CHECK_RES_UNLOCK(fs, res); } + if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) { + // special case for zero sized files + res = SPIFFS_ERR_END_OF_OBJECT; + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + } + #if SPIFFS_CACHE_WR spiffs_fflush_cache(fs, fh); #endif @@ -347,6 +365,7 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { s32_t res; u32_t offset; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -464,6 +483,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES(fs, res); @@ -502,7 +522,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { return offs; } -s32_t SPIFFS_remove(spiffs *fs, char *path) { +s32_t SPIFFS_remove(spiffs *fs, const char *path) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -514,7 +534,7 @@ s32_t SPIFFS_remove(spiffs *fs, char *path) { res = spiffs_fd_find_new(fs, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t *)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); if (res != SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); } @@ -543,6 +563,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -586,7 +607,7 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi return res; } -s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { +s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -594,7 +615,7 @@ s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { s32_t res; spiffs_page_ix pix; - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); res = spiffs_stat_pix(fs, pix, 0, s); @@ -612,6 +633,7 @@ s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -669,6 +691,7 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) { s32_t res = SPIFFS_OK; #if SPIFFS_CACHE_WR SPIFFS_LOCK(fs); + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fflush_cache(fs, fh); SPIFFS_API_CHECK_RES_UNLOCK(fs,res); SPIFFS_UNLOCK(fs); @@ -684,6 +707,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) { s32_t res = SPIFFS_OK; SPIFFS_LOCK(fs); + fh = SPIFFS_FH_UNOFFS(fs, fh); #if SPIFFS_CACHE res = spiffs_fflush_cache(fs, fh); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -696,7 +720,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) { return res; } -s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { +s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *new) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -704,10 +728,10 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { spiffs_page_ix pix_old, pix_dummy; spiffs_fd *fd; - s32_t res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)old, &pix_old); + s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old, &pix_old); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)new, &pix_dummy); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new, &pix_dummy); if (res == SPIFFS_ERR_NOT_FOUND) { res = SPIFFS_OK; } else if (res == SPIFFS_OK) { @@ -724,7 +748,7 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (u8_t*)new, + res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new, 0, &pix_dummy); spiffs_fd_return(fs, fd->file_nbr); @@ -736,7 +760,7 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { return res; } -spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d) { +spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) { (void)name; if (!SPIFFS_CHECK_CFG((fs))) { @@ -760,9 +784,9 @@ static s32_t spiffs_read_dir_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { - (void)user_data; + const void *user_const_p, + void *user_var_p) { + (void)user_const_p; s32_t res; spiffs_page_object_ix_header objix_hdr; if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED || @@ -778,7 +802,7 @@ static s32_t spiffs_read_dir_v( objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags& (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - struct spiffs_dirent *e = (struct spiffs_dirent *)user_p; + struct spiffs_dirent *e = (struct spiffs_dirent*)user_var_p; e->obj_id = obj_id; strcpy((char *)e->name, (char *)objix_hdr.name); e->type = objix_hdr.type; @@ -898,6 +922,52 @@ s32_t SPIFFS_gc(spiffs *fs, u32_t size) { return 0; } +s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) { + s32_t res; + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + fh = SPIFFS_FH_UNOFFS(fs, fh); + + spiffs_fd *fd; + res = spiffs_fd_get(fs, fh, &fd); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + +#if SPIFFS_CACHE_WR + res = spiffs_fflush_cache(fs, fh); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); +#endif + + res = (fd->fdoffset == fd->size); + + SPIFFS_UNLOCK(fs); + return res; +} + +s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) { + s32_t res; + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + fh = SPIFFS_FH_UNOFFS(fs, fh); + + spiffs_fd *fd; + res = spiffs_fd_get(fs, fh, &fd); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + +#if SPIFFS_CACHE_WR + res = spiffs_fflush_cache(fs, fh); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); +#endif + + res = fd->fdoffset; + + SPIFFS_UNLOCK(fs); + return res; +} + #if SPIFFS_TEST_VISUALISATION s32_t SPIFFS_vis(spiffs *fs) { diff --git a/cores/esp8266/spiffs/spiffs_nucleus.c b/cores/esp8266/spiffs/spiffs_nucleus.c index 18712c6e1..ba55722bf 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.c +++ b/cores/esp8266/spiffs/spiffs_nucleus.c @@ -101,7 +101,7 @@ s32_t spiffs_phys_cpy( // Find object lookup entry containing given id with visitor. // Iterate over object lookup pages in each block until a given object id entry is found. -// When found, the visitor function is called with block index, entry index and user_data. +// When found, the visitor function is called with block index, entry index and user data. // If visitor returns SPIFFS_VIS_CONTINUE, the search goes on. Otherwise, the search will be // ended and visitor's return code is returned to caller. // If no visitor is given (0) the search returns on first entry with matching object id. @@ -113,8 +113,8 @@ s32_t spiffs_phys_cpy( // SPIFFS_VIS_NO_WRAP // @param obj_id argument object id // @param v visitor callback function -// @param user_data any data, passed to the callback visitor function -// @param user_p any pointer, passed to the callback visitor function +// @param user_const_p any const pointer, passed to the callback visitor function +// @param user_var_p any pointer, passed to the callback visitor function // @param block_ix reported block index where match was found // @param lu_entry reported look up index where match was found s32_t spiffs_obj_lu_find_entry_visitor( @@ -124,8 +124,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( u8_t flags, spiffs_obj_id obj_id, spiffs_visitor_f v, - u32_t user_data, - void *user_p, + const void *user_const_p, + void *user_var_p, spiffs_block_ix *block_ix, int *lu_entry) { s32_t res = SPIFFS_OK; @@ -175,8 +175,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( (flags & SPIFFS_VIS_CHECK_PH) ? obj_id : obj_lu_buf[cur_entry-entry_offset], cur_block, cur_entry, - user_data, - user_p); + user_const_p, + user_var_p); if (res == SPIFFS_VIS_COUNTINUE || res == SPIFFS_VIS_COUNTINUE_RELOAD) { if (res == SPIFFS_VIS_COUNTINUE_RELOAD) { res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, @@ -264,11 +264,11 @@ static s32_t spiffs_obj_lu_scan_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { + const void *user_const_p, + void *user_var_p) { (void)bix; - (void)user_data; - (void)user_p; + (void)user_const_p; + (void)user_var_p; if (obj_id == SPIFFS_OBJ_ID_FREE) { if (ix_entry == 0) { fs->free_blocks++; @@ -409,11 +409,11 @@ s32_t spiffs_obj_lu_find_free( fs->free_blocks--; } } - if (res == SPIFFS_VIS_END) { + if (res == SPIFFS_ERR_FULL) { SPIFFS_DBG("fs full\n"); } - return res == SPIFFS_VIS_END ? SPIFFS_ERR_FULL : res; + return res; } // Find object lookup entry containing given id @@ -439,8 +439,8 @@ static s32_t spiffs_obj_lu_find_id_and_span_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { + const void *user_const_p, + void *user_var_p) { s32_t res; spiffs_page_header ph; spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry); @@ -448,10 +448,10 @@ static s32_t spiffs_obj_lu_find_id_and_span_v( SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_header), (u8_t *)&ph); SPIFFS_CHECK_RES(res); if (ph.obj_id == obj_id && - ph.span_ix == (spiffs_span_ix)user_data && + ph.span_ix == *((spiffs_span_ix*)user_var_p) && (ph.flags & (SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED)) == SPIFFS_PH_FLAG_DELET && !((obj_id & SPIFFS_OBJ_ID_IX_FLAG) && (ph.flags & SPIFFS_PH_FLAG_IXDELE) == 0 && ph.span_ix == 0) && - (user_p == 0 || *((spiffs_page_ix *)user_p) != pix)) { + (user_const_p == 0 || *((const spiffs_page_ix*)user_const_p) != pix)) { return SPIFFS_OK; } else { return SPIFFS_VIS_COUNTINUE; @@ -476,8 +476,8 @@ s32_t spiffs_obj_lu_find_id_and_span( SPIFFS_VIS_CHECK_ID, obj_id, spiffs_obj_lu_find_id_and_span_v, - (u32_t)spix, exclusion_pix ? &exclusion_pix : 0, + &spix, &bix, &entry); @@ -515,8 +515,8 @@ s32_t spiffs_obj_lu_find_id_and_span_by_phdr( SPIFFS_VIS_CHECK_PH, obj_id, spiffs_obj_lu_find_id_and_span_v, - (u32_t)spix, exclusion_pix ? &exclusion_pix : 0, + &spix, &bix, &entry); @@ -690,7 +690,7 @@ s32_t spiffs_page_delete( s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix) { s32_t res = SPIFFS_OK; @@ -721,7 +721,7 @@ s32_t spiffs_object_create( oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED); oix_hdr.type = type; oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page - strncpy((char *)&oix_hdr.name, (char *)name, SPIFFS_OBJ_NAME_LEN); + strncpy((char*)&oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN); // update page @@ -748,7 +748,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_obj_id obj_id, spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], u32_t size, spiffs_page_ix *new_pix) { s32_t res = SPIFFS_OK; @@ -772,7 +772,7 @@ s32_t spiffs_object_update_index_hdr( // change name if (name) { - strncpy((char *)objix_hdr->name, (char *)name, SPIFFS_OBJ_NAME_LEN); + strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN); } if (size) { objix_hdr->size = size; @@ -1335,9 +1335,9 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { - (void)user_data; + const void *user_const_p, + void *user_var_p) { + (void)user_var_p; s32_t res; spiffs_page_object_ix_header objix_hdr; spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry); @@ -1351,7 +1351,7 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( if (objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - if (strcmp((char *)user_p, (char *)objix_hdr.name) == 0) { + if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) { return SPIFFS_OK; } } @@ -1362,7 +1362,7 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( // Finds object index header page by name s32_t spiffs_object_find_object_index_header_by_name( spiffs *fs, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_page_ix *pix) { s32_t res; spiffs_block_ix bix; @@ -1374,8 +1374,8 @@ s32_t spiffs_object_find_object_index_header_by_name( 0, 0, spiffs_object_find_object_index_header_by_name_v, - 0, name, + 0, &bix, &entry); @@ -1402,7 +1402,8 @@ s32_t spiffs_object_truncate( s32_t res = SPIFFS_OK; spiffs *fs = fd->fs; - res = spiffs_gc_check(fs, remove ? 0 : SPIFFS_DATA_PAGE_SIZE(fs)); + // need 2 pages if not removing: object index page + possibly chopped data page + res = spiffs_gc_check(fs, remove ? 0 : SPIFFS_DATA_PAGE_SIZE(fs) * 2); SPIFFS_CHECK_RES(res); spiffs_page_ix objix_pix = fd->objix_hdr_pix; @@ -1482,7 +1483,7 @@ s32_t spiffs_object_truncate( SPIFFS_DBG("truncate: got data pix %04x\n", data_pix); - if (cur_size - SPIFFS_DATA_PAGE_SIZE(fs) >= new_size) { + if (new_size == 0 || remove || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) { // delete full data page res = spiffs_page_data_check(fs, fd, data_pix, data_spix); if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) { @@ -1701,10 +1702,10 @@ typedef struct { } spiffs_free_obj_id_state; static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, void *user_p) { + const void *user_const_p, void *user_var_p) { if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED) { - spiffs_obj_id min_obj_id = user_data; - u8_t *conflicting_name = (u8_t *)user_p; + spiffs_obj_id min_obj_id = *((spiffs_obj_id*)user_var_p); + const u8_t *conflicting_name = (const u8_t*)user_const_p; // if conflicting name parameter is given, also check if this name is found in object index hdrs if (conflicting_name && (id & SPIFFS_OBJ_ID_IX_FLAG)) { @@ -1717,7 +1718,7 @@ static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id i if (objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - if (strcmp((char *)user_p, (char *)objix_hdr.name) == 0) { + if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) { return SPIFFS_ERR_CONFLICTING_NAME; } } @@ -1734,11 +1735,11 @@ static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id i } static s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, void *user_p) { - (void)user_data; + const void *user_const_p, void *user_var_p) { + (void)user_var_p; if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED && (id & SPIFFS_OBJ_ID_IX_FLAG)) { s32_t res; - spiffs_free_obj_id_state *state = (spiffs_free_obj_id_state *)user_p; + const spiffs_free_obj_id_state *state = (const spiffs_free_obj_id_state*)user_const_p; spiffs_page_object_ix_header objix_hdr; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, @@ -1766,10 +1767,10 @@ static s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id // Scans thru all object lookup for object index header pages. If total possible number of // object ids cannot fit into a work buffer, these are grouped. When a group containing free // object ids is found, the object lu is again scanned for object ids within group and bitmasked. -// Finally, the bitmasked is searched for a free id -s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *conflicting_name) { +// Finally, the bitmask is searched for a free id +s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) { s32_t res = SPIFFS_OK; - u32_t max_objects = (SPIFFS_CFG_PHYS_SZ(fs) / (u32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) / 2; + u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2; spiffs_free_obj_id_state state; spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE; state.min_obj_id = 1; @@ -1786,8 +1787,8 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co SPIFFS_DBG("free_obj_id: BITM min:%04x max:%04x\n", state.min_obj_id, state.max_obj_id); memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v, state.min_obj_id, - conflicting_name, 0, 0); + res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v, + conflicting_name, &state.min_obj_id, 0, 0); if (res == SPIFFS_VIS_END) res = SPIFFS_OK; SPIFFS_CHECK_RES(res); // traverse bitmask until found free obj_id @@ -1851,7 +1852,7 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co SPIFFS_DBG("free_obj_id: COMP min:%04x max:%04x compact:%i\n", state.min_obj_id, state.max_obj_id, state.compaction); memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_compact_v, 0, &state, 0, 0); + res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_compact_v, &state, 0, 0, 0); if (res == SPIFFS_VIS_END) res = SPIFFS_OK; SPIFFS_CHECK_RES(res); state.conflicting_name = 0; // searched for conflicting name once, no need to do it again diff --git a/cores/esp8266/spiffs/spiffs_nucleus.h b/cores/esp8266/spiffs/spiffs_nucleus.h index f19b24af8..b64d10cac 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.h +++ b/cores/esp8266/spiffs/spiffs_nucleus.h @@ -460,7 +460,7 @@ typedef struct __attribute(( packed )) { // callback func for object lookup visitor typedef s32_t (*spiffs_visitor_f)(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, void *user_p); + const void *user_const_p, void *user_var_p); #if SPIFFS_CACHE @@ -521,8 +521,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( u8_t flags, spiffs_obj_id obj_id, spiffs_visitor_f v, - u32_t user_data, - void *user_p, + const void *user_const_p, + void *user_var_p, spiffs_block_ix *block_ix, int *lu_entry); @@ -538,7 +538,7 @@ s32_t spiffs_obj_lu_scan( s32_t spiffs_obj_lu_find_free_obj_id( spiffs *fs, spiffs_obj_id *obj_id, - u8_t *conflicting_name); + const u8_t *conflicting_name); s32_t spiffs_obj_lu_find_free( spiffs *fs, @@ -599,7 +599,7 @@ s32_t spiffs_page_delete( s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix); @@ -609,7 +609,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_obj_id obj_id, spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], u32_t size, spiffs_page_ix *new_pix); @@ -661,7 +661,7 @@ s32_t spiffs_object_truncate( s32_t spiffs_object_find_object_index_header_by_name( spiffs *fs, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_page_ix *pix); // --------------- From 320a747890d529f14f3842d08e4427f843120cae Mon Sep 17 00:00:00 2001 From: luc Date: Thu, 22 Oct 2015 11:22:32 +0800 Subject: [PATCH 2/9] Add SPIFFS wrapper for info function this allow to display total space and used space on SPIFFS --- cores/esp8266/FS.cpp | 7 +++++++ cores/esp8266/FS.h | 1 + cores/esp8266/FSImpl.h | 1 + cores/esp8266/spiffs_api.cpp | 9 +++++++++ 4 files changed, 18 insertions(+) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index e3158713d..5691c2573 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -174,6 +174,13 @@ bool FS::format() { return _impl->format(); } +bool FS::info(uint32_t *total, uint32_t *used){ + if (!_impl) { + return false; + } + return _impl->info(total,used); +} + File FS::open(const String& path, const char* mode) { return open(path.c_str(), mode); } diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index d7a170d1d..c03e50619 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -93,6 +93,7 @@ public: bool begin(); bool format(); + bool info(uint32_t *total, uint32_t *used); File open(const char* path, const char* mode); File open(const String& path, const char* mode); diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index b95bafcfc..a1124b285 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -64,6 +64,7 @@ class FSImpl { public: virtual bool begin() = 0; virtual bool format() = 0; + virtual bool info(uint32_t *total, uint32_t *used) = 0; virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0; virtual bool exists(const char* path) = 0; virtual DirImplPtr openDir(const char* path) = 0; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 4de08c405..f60a2d094 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -76,6 +76,15 @@ public: return true; } + bool info(uint32_t *total, uint32_t *used) override{ + auto rc = SPIFFS_info(&_fs, total, used); + if (rc != SPIFFS_OK) { + DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code); + return false; + } + return true; + } + bool remove(const char* path) override { char tmpName[SPIFFS_OBJ_NAME_LEN]; strlcpy(tmpName, path, sizeof(tmpName)); From c773140a5b208450ff679b12ce3e880d59221306 Mon Sep 17 00:00:00 2001 From: Pascal Gollor Date: Thu, 22 Oct 2015 16:43:00 +0200 Subject: [PATCH 3/9] use ArduinoOTA class in OTA-mDNS.SPIFFS example --- libraries/ArduinoOTA/ArduinoOTA.cpp | 2 +- .../OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino | 155 ++++++------------ .../examples/OTA-mDNS-SPIFFS/data/cl_conf.txt | 2 +- 3 files changed, 51 insertions(+), 108 deletions(-) diff --git a/libraries/ArduinoOTA/ArduinoOTA.cpp b/libraries/ArduinoOTA/ArduinoOTA.cpp index 82b4d1411..034b13286 100644 --- a/libraries/ArduinoOTA/ArduinoOTA.cpp +++ b/libraries/ArduinoOTA/ArduinoOTA.cpp @@ -67,7 +67,7 @@ void ArduinoOTA::handle() { WiFiUDP::stopAll(); - if(!Update.begin(size)){ + if(!Update.begin(size, cmd)){ if (_serial_debug) Serial.println("Update Begin Error"); if (_error_callback) _error_callback(); diff --git a/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino b/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino index 72cb7ac9d..896c69205 100644 --- a/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino +++ b/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino @@ -2,15 +2,22 @@ * @file OTA-mDNS-SPIFFS.ino * * @author Pascal Gollor (http://www.pgollor.de/cms/) - * @data 2015-09-18 + * @date 2015-09-18 + * + * changelog: + * 2015-10-22: + * - Use new ArduinoOTA library. + * - loadConfig function can handle different line endings + * - remove mDNS studd. ArduinoOTA handle it. * */ - +// includes #include #include #include #include +#include /** @@ -18,7 +25,6 @@ * @{ */ #define HOSTNAME "ESP8266-OTA-" ///< Hostename. The setup function adds the Chip ID at the end. -#define APORT 8266 ///< Port for OTA update /// @} /** @@ -29,8 +35,11 @@ const char* ap_default_ssid = "esp8266"; ///< Default SSID. const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK. /// @} -/// OTA Update UDP server handle. -WiFiUDP OTA; +/// Uncomment the next line for verbose output over UART. +//#define SERIAL_VERBOSE + +/// OTA server handle. +ArduinoOTA ota_server; /** @@ -41,7 +50,7 @@ WiFiUDP OTA; * * The config file have to containt the WiFi SSID in the first line * and the WiFi PSK in the second line. - * Line seperator have to be \r\n (CR LF). + * Line seperator can be \r\n (CR LF) \r or \n. */ bool loadConfig(String *ssid, String *pass) { @@ -61,8 +70,21 @@ bool loadConfig(String *ssid, String *pass) content.trim(); // Check if ther is a second line available. - uint8_t pos = content.indexOf("\r\n"); - if (pos == 0) + int8_t pos = content.indexOf("\r\n"); + uint8_t le = 2; + // check for linux and mac line ending. + if (pos == -1) + { + le = 1; + pos = content.indexOf("\n"); + if (pos == -1) + { + pos = content.indexOf("\r"); + } + } + + // If there is no second line: Some information is missing. + if (pos == -1) { Serial.println("Infvalid content."); Serial.println(content); @@ -72,7 +94,18 @@ bool loadConfig(String *ssid, String *pass) // Store SSID and PSK into string vars. *ssid = content.substring(0, pos); - *pass = content.substring(pos + 2); + *pass = content.substring(pos + le); + + ssid->trim(); + pass->trim(); + +#ifdef SERIAL_VERBOSE + Serial.println("----- file content -----"); + Serial.println(content); + Serial.println("----- file content -----"); + Serial.println("ssid: " + *ssid); + Serial.println("psk: " + *pass); +#endif return true; } // loadConfig @@ -105,91 +138,6 @@ bool saveConfig(String *ssid, String *pass) } // saveConfig -/** - * @brief Handle OTA update stuff. - * - * This function comes from ESP8266 Arduino example: - * https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266mDNS/examples/DNS_SD_Arduino_OTA/DNS_SD_Arduino_OTA.ino - * - * Modification for uploading SPIFFS images from Pascal Gollor. - * - */ -static inline void ota_handle(void) -{ - bool spiffs = false; - - if (! OTA.parsePacket()) - { - return; - } - - // Get remote IP - IPAddress remote = OTA.remoteIP(); - - // Get command - int cmd = OTA.parseInt(); - Serial.print("command: "); - Serial.println(cmd); - if (cmd == U_SPIFFS) - { - spiffs = true; - Serial.println("Get SPIFFS image."); - } - - // Get remote port - int port = OTA.parseInt(); - - // Get sketch size. - int sketch_size = OTA.parseInt(); - - // Output stuff - Serial.print("Update Start: ip:"); - Serial.print(remote); - Serial.printf(", port:%d, size:%d\r\n", port, sketch_size); - - // Stop all UDP connections. - WiFiUDP::stopAll(); - - // OTA start Time - uint32_t startTime = millis(); - - // Start Updateing. - if(!Update.begin(sketch_size, cmd)) - { - Serial.println("Update Begin Error"); - return; - } - - WiFiClient client; - if (client.connect(remote, port)) - { - uint32_t written; - while(!Update.isFinished()) - { - written = Update.write(client); - if(written > 0) client.print(written, DEC); - } - Serial.setDebugOutput(false); - - if(Update.end()) - { - client.println("OK"); - Serial.printf("Update Success: %u\nRebooting...\n", (unsigned int)(millis() - startTime)); - ESP.restart(); - } - else - { - Update.printError(client); - Update.printError(Serial); - } - } - else - { - Serial.printf("Connect Failed: %u\n", (unsigned int)(millis() - startTime)); - } -} // ota_handle - - /** * @brief Arduino setup function. */ @@ -212,8 +160,8 @@ void setup() WiFi.hostname(hostname); // Print hostname. - Serial.print("hostname: "); - Serial.println(WiFi.hostname()); + Serial.println("Hostname: " + hostname); + //Serial.println(WiFi.hostname()); // Initialize file system. @@ -295,14 +243,8 @@ void setup() Serial.println(WiFi.softAPIP()); } - // Initialize mDNS service. - MDNS.begin(hostname.c_str()); - - // ... Add OTA service. - MDNS.addService("arduino", "tcp", APORT); - - // Open OTA Server. - OTA.begin(APORT); + // Start OTA server. + ota_server.setup(); } @@ -311,7 +253,8 @@ void setup() */ void loop() { - // Handle OTA update. - ota_handle(); + // Handle OTA server. + ota_server.handle(); + yield(); } diff --git a/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/data/cl_conf.txt b/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/data/cl_conf.txt index d2f1ff4ae..a14fc901c 100644 --- a/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/data/cl_conf.txt +++ b/libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/data/cl_conf.txt @@ -1,2 +1,2 @@ -YOUR_SSID +YOUR_SSID YOUR_PSK From f328e66fd740661cb967ef159428b923fd75123c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 25 Oct 2015 21:11:10 +0300 Subject: [PATCH 4/9] Add 512k (no SPIFFS) build option (#862) --- boards.txt | 23 +++++++++++-------- cores/esp8266/spiffs_api.cpp | 9 ++++++++ tools/sdk/ld/eagle.flash.512k0.ld | 18 +++++++++++++++ ...le.flash.512k.ld => eagle.flash.512k64.ld} | 0 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 tools/sdk/ld/eagle.flash.512k0.ld rename tools/sdk/ld/{eagle.flash.512k.ld => eagle.flash.512k64.ld} (100%) diff --git a/boards.txt b/boards.txt index 22a168c5b..d5ca569be 100644 --- a/boards.txt +++ b/boards.txt @@ -65,13 +65,18 @@ generic.menu.UploadSpeed.512000.upload.speed=512000 generic.menu.UploadSpeed.921600=921600 generic.menu.UploadSpeed.921600.upload.speed=921600 -generic.menu.FlashSize.512K=512K (64K SPIFFS) -generic.menu.FlashSize.512K.build.flash_size=512K -generic.menu.FlashSize.512K.build.flash_ld=eagle.flash.512k.ld -generic.menu.FlashSize.512K.build.spiffs_start=0x6B000 -generic.menu.FlashSize.512K.build.spiffs_end=0x7B000 -generic.menu.FlashSize.512K.build.spiffs_blocksize=4096 -generic.menu.FlashSize.512K.upload.maximum_size=434160 +generic.menu.FlashSize.512K64=512K (64K SPIFFS) +generic.menu.FlashSize.512K64.build.flash_size=512K +generic.menu.FlashSize.512K64.build.flash_ld=eagle.flash.512k64.ld +generic.menu.FlashSize.512K64.build.spiffs_start=0x6B000 +generic.menu.FlashSize.512K64.build.spiffs_end=0x7B000 +generic.menu.FlashSize.512K64.build.spiffs_blocksize=4096 +generic.menu.FlashSize.512K64.upload.maximum_size=434160 + +generic.menu.FlashSize.512K0=512K (no SPIFFS) +generic.menu.FlashSize.512K0.build.flash_size=512K +generic.menu.FlashSize.512K0.build.flash_ld=eagle.flash.512k0.ld +generic.menu.FlashSize.512K0.upload.maximum_size=499696 generic.menu.FlashSize.1M512=1M (512K SPIFFS) generic.menu.FlashSize.1M512.build.flash_size=1M @@ -410,7 +415,7 @@ thing.build.variant=thing thing.build.flash_mode=qio # flash chip: AT25SF041 (512 kbyte, 4Mbit) thing.build.flash_size=512K -thing.build.flash_ld=eagle.flash.512k.ld +thing.build.flash_ld=eagle.flash.512k64.ld thing.build.flash_freq=40 thing.build.spiffs_start=0x6B000 thing.build.spiffs_end=0x7B000 @@ -526,7 +531,7 @@ esp210.menu.FlashSize.4M1M.build.spiffs_pagesize=256 # wifio.build.flash_mode=qio # wifio.build.flash_size=512K # wifio.build.flash_freq=40 -# wifio.build.flash_ld=eagle.flash.512k.ld +# wifio.build.flash_ld=eagle.flash.512k64.ld # wifio.build.spiffs_start=0x6B000 # wifio.build.spiffs_end=0x7B000 # diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index f60a2d094..364b788eb 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -100,6 +100,10 @@ public: if (SPIFFS_mounted(&_fs) != 0) { return true; } + if (_size == 0) { + DEBUGV("SPIFFS size is zero"); + return false; + } if (_tryMount()) { return true; } @@ -112,6 +116,11 @@ public: } bool format() override { + if (_size == 0) { + DEBUGV("SPIFFS size is zero"); + return false; + } + bool wasMounted = (SPIFFS_mounted(&_fs) != 0); if (_tryMount()) { diff --git a/tools/sdk/ld/eagle.flash.512k0.ld b/tools/sdk/ld/eagle.flash.512k0.ld new file mode 100644 index 000000000..05e06d891 --- /dev/null +++ b/tools/sdk/ld/eagle.flash.512k0.ld @@ -0,0 +1,18 @@ +/* Flash Split for 512K chips, no SPIFFS */ +/* sketch 487KB */ +/* eeprom 20KB */ + +MEMORY +{ + dport0_0_seg : org = 0x3FF00000, len = 0x10 + dram0_0_seg : org = 0x3FFE8000, len = 0x14000 + iram1_0_seg : org = 0x40100000, len = 0x8000 + irom0_0_seg : org = 0x40201010, len = 0x79ff0 +} + +PROVIDE ( _SPIFFS_start = 0 ); +PROVIDE ( _SPIFFS_end = 0 ); +PROVIDE ( _SPIFFS_page = 0 ); +PROVIDE ( _SPIFFS_block = 0 ); + +INCLUDE "../ld/eagle.app.v6.common.ld" diff --git a/tools/sdk/ld/eagle.flash.512k.ld b/tools/sdk/ld/eagle.flash.512k64.ld similarity index 100% rename from tools/sdk/ld/eagle.flash.512k.ld rename to tools/sdk/ld/eagle.flash.512k64.ld From 405b23876cb5931e719e26cbb63ff14858d05a16 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 25 Oct 2015 23:25:03 +0300 Subject: [PATCH 5/9] Fix short unaligned writes in SPIFFS HAL (#924) --- cores/esp8266/spiffs_hal.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/esp8266/spiffs_hal.cpp b/cores/esp8266/spiffs_hal.cpp index f9bb8b42a..756896f65 100644 --- a/cores/esp8266/spiffs_hal.cpp +++ b/cores/esp8266/spiffs_hal.cpp @@ -110,6 +110,8 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) { if (addr < alignedBegin) { uint32_t nb = alignedBegin - addr; + if (nb > size) + nb = size; uint32_t tmp = 0xffffffff; memcpy(((uint8_t* )&tmp) + 4 - nb, src, nb); if (!ESP.flashWrite(alignedBegin - 4, &tmp, 4)) { From 56bd4a78b34bee7d33ef76cc011ba54412cc53ee Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Oct 2015 14:50:50 +0300 Subject: [PATCH 6/9] Fix short unaligned writes in SPIFFS HAL, 2nd attempt (#924) --- cores/esp8266/spiffs_hal.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cores/esp8266/spiffs_hal.cpp b/cores/esp8266/spiffs_hal.cpp index 756896f65..7f3245c0d 100644 --- a/cores/esp8266/spiffs_hal.cpp +++ b/cores/esp8266/spiffs_hal.cpp @@ -109,12 +109,11 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) { } if (addr < alignedBegin) { - uint32_t nb = alignedBegin - addr; - if (nb > size) - nb = size; - uint32_t tmp = 0xffffffff; - memcpy(((uint8_t* )&tmp) + 4 - nb, src, nb); - if (!ESP.flashWrite(alignedBegin - 4, &tmp, 4)) { + uint32_t ofs = alignedBegin - addr; + uint32_t nb = (size < ofs) ? size : ofs; + uint8_t tmp[4] __attribute__((aligned(4))) = {0xff, 0xff, 0xff, 0xff}; + memcpy(tmp + 4 - ofs, src, nb); + if (!ESP.flashWrite(alignedBegin - 4, (uint32_t*) tmp, 4)) { DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n", __LINE__, addr, size, alignedBegin, alignedEnd); return SPIFFS_ERR_INTERNAL; From ee0b8621f3d49ab45db53aeda054f45e7bea6af9 Mon Sep 17 00:00:00 2001 From: luc Date: Wed, 28 Oct 2015 13:58:53 +0800 Subject: [PATCH 7/9] Add header access using same method as arguments 2 based on @brianensor PR +sample and some sanity check --- .../SimpleAuthentification.ino | 126 ++++++++++++++++++ .../ESP8266WebServer/src/ESP8266WebServer.cpp | 47 +++++++ .../ESP8266WebServer/src/ESP8266WebServer.h | 9 ++ libraries/ESP8266WebServer/src/Parsing.cpp | 15 +++ 4 files changed, 197 insertions(+) create mode 100644 libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino diff --git a/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino b/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino new file mode 100644 index 000000000..19fc69c7d --- /dev/null +++ b/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino @@ -0,0 +1,126 @@ +#include +#include +#include + +const char* ssid = "........"; +const char* password = "........"; + +ESP8266WebServer server(80); + +//Check if header is present and correct +bool is_authentified(){ + Serial.println("Enter is_authentified"); + if (server.hasHeader("Cookie")){ + Serial.print("Found cookie: "); + String cookie = server.header("Cookie"); + Serial.println(cookie); + if (cookie.indexOf("ESPSESSIONID=1") != -1) { + Serial.println("Authentification Successful"); + return true; + } + } + Serial.println("Authentification Failed"); + return false; +} + +//login page, also called for disconnect +void handleLogin(){ + String msg; + if (server.hasHeader("Cookie")){ + Serial.print("Found cookie: "); + String cookie = server.header("Cookie"); + Serial.println(cookie); + } + if (server.hasArg("DISCONNECT")){ + Serial.println("Disconnection"); + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + return; + } + if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){ + if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){ + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + Serial.println("Log in Successful"); + return; + } + msg = "Wrong username/password! try again."; + Serial.println("Log in Failed"); + } + String content = "
To log in, please use : admin/admin
"; + content += "User:
"; + content += "Password:
"; + content += "
" + msg + "
"; + content += "You also can go here"; + server.send(200, "text/html", content); +} + +//root page can be accessed only if authentification is ok +void handleRoot(){ + Serial.println("Enter handleRoot"); + String header; + if (!is_authentified()){ + String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + return; + } + String content = "

hello, you successfully connected to esp8266!


"; + if (server.hasHeader("User-Agent")){ + content += "the user agent used is : " + server.header("User-Agent") + "

"; + } + content += "You can access this page until you disconnect"; + server.send(200, "text/html", content); +} + +//no need authentification +void handleNotFound(){ + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += (server.method() == HTTP_GET)?"GET":"POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + for (uint8_t i=0; i 0)) + return true; + } + return false; +} + String ESP8266WebServer::hostHeader() { return _hostHeader; } diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 8a640a935..40216f482 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -83,6 +83,12 @@ public: String argName(int i); // get request argument name by number int args(); // get arguments count bool hasArg(const char* name); // check if argument exists + void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect + String header(const char* name); // get request header value by name + String header(int i); // get request header value by number + String headerName(int i); // get request header name by number + int headers(); // get header count + bool hasHeader(const char* name); // check if header exists String hostHeader(); // get request host header if available or empty String if not @@ -124,6 +130,7 @@ protected: void _uploadWriteByte(uint8_t b); uint8_t _uploadReadByte(WiFiClient& client); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); + bool _collectHeader(const char* headerName, const char* headerValue); struct RequestArgument { String key; @@ -140,6 +147,8 @@ protected: RequestArgument* _currentArgs; HTTPUpload _currentUpload; + RequestArgument* _currentHeaders; + size_t _headerKeysCount; size_t _contentLength; String _responseHeaders; diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index 216f20c3e..0458a9980 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -31,6 +31,10 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); + //reset header value + for (int i = 0; i < _headerKeysCount; ++i) { + _currentHeaders[i].value =String(); + } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces @@ -96,6 +100,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); + _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG DEBUG_OUTPUT.print("headerName: "); @@ -161,6 +166,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); + _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG DEBUG_OUTPUT.print("headerName: "); @@ -187,6 +193,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { return true; } +bool ESP8266WebServer::_collectHeader(const char* headerName, const char* headerValue) { + for (size_t i = 0; i < _headerKeysCount; i++) { + if (_currentHeaders[i].key==headerName) { + _currentHeaders[i].value=headerValue; + return true; + } + } + return false; +} void ESP8266WebServer::_parseArguments(String data) { #ifdef DEBUG From 219cb19ece7b84ed94854007d89a3406e66b1282 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 28 Oct 2015 16:45:08 +0300 Subject: [PATCH 8/9] Update SPIFFS wrapper for 0.3.3 --- cores/esp8266/spiffs_api.cpp | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 364b788eb..2d0d90b83 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -63,11 +63,7 @@ public: DirImplPtr openDir(const char* path) override; bool rename(const char* pathFrom, const char* pathTo) override { - char tmpNameFrom[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpNameFrom, pathFrom, sizeof(tmpNameFrom)); - char tmpNameTo[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpNameTo, pathTo, sizeof(tmpNameTo)); - auto rc = SPIFFS_rename(&_fs, tmpNameFrom, tmpNameTo); + auto rc = SPIFFS_rename(&_fs, pathFrom, pathTo); if (rc != SPIFFS_OK) { DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc, pathFrom, pathTo); @@ -86,9 +82,7 @@ public: } bool remove(const char* path) override { - char tmpName[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpName, path, sizeof(tmpName)); - auto rc = SPIFFS_remove(&_fs, tmpName); + auto rc = SPIFFS_remove(&_fs, path); if (rc != SPIFFS_OK) { DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path); return false; @@ -411,9 +405,7 @@ protected: FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) { int mode = getSpiffsMode(openMode, accessMode); - char tmpName[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpName, path, sizeof(tmpName)); - int fd = SPIFFS_open(&_fs, tmpName, mode, 0); + int fd = SPIFFS_open(&_fs, path, mode, 0); if (fd < 0) { DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n", fd, path, openMode, accessMode, _fs.err_code); @@ -423,18 +415,14 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc } bool SPIFFSImpl::exists(const char* path) { - char tmpName[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpName, path, sizeof(tmpName)); spiffs_stat stat; - int rc = SPIFFS_stat(&_fs, tmpName, &stat); + int rc = SPIFFS_stat(&_fs, path, &stat); return rc == SPIFFS_OK; } DirImplPtr SPIFFSImpl::openDir(const char* path) { spiffs_DIR dir; - char tmpName[SPIFFS_OBJ_NAME_LEN]; - strlcpy(tmpName, path, sizeof(tmpName)); - spiffs_DIR* result = SPIFFS_opendir(&_fs, tmpName, &dir); + spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir); if (!result) { DEBUGV("SPIFFSImpl::openDir: path=`%s` err=%d\r\n", path, _fs.err_code); return DirImplPtr(); From b13742cd252fa5fb4467d54a5abffd46366eaa27 Mon Sep 17 00:00:00 2001 From: Carl Pacey Date: Wed, 28 Oct 2015 12:16:06 -0400 Subject: [PATCH 9/9] Support Sketch > Export compiled binary --- platform.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/platform.txt b/platform.txt index 30c58cf4e..5440f55b3 100644 --- a/platform.txt +++ b/platform.txt @@ -77,6 +77,10 @@ recipe.objcopy.eep.pattern= recipe.objcopy.hex.pattern="{runtime.tools.esptool.path}/{compiler.esptool.cmd}" -eo "{runtime.platform.path}/bootloaders/eboot/eboot.elf" -bo "{build.path}/{build.project_name}.bin" -bm {build.flash_mode} -bf {build.flash_freq} -bz {build.flash_size} -bs .text -bp 4096 -ec -eo "{build.path}/{build.project_name}.elf" -bs .irom0.text -bs .text -bs .data -bs .rodata -bc -ec +## Save hex +recipe.output.tmp_file={build.project_name}.bin +recipe.output.save_file={build.project_name}.{build.variant}.bin + ## Compute size recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf" recipe.size.regex=^(?:\.irom0\.text|\.text|\.data|\.rodata|)\s+([0-9]+).*