migrate from binary config to json config

This commit is contained in:
sharandac
2020-08-11 14:32:27 +02:00
parent 77703b42b0
commit 33fe453f5e
24 changed files with 557 additions and 226 deletions

View File

@@ -20,31 +20,11 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "ArduinoJson.h"
#include "HTTPClient.h"
#include "update_check_version.h"
// arduinoJson allocator for external PSRAM
// see: https://arduinojson.org/v6/how-to/use-external-ram-on-esp32/
struct UpdateSpiRamAllocator {
void* allocate( size_t size ) {
void *psram = ps_calloc( size, 1 );
if ( psram ) {
log_i("allocate %dbytes(%p) json psram", size, psram );
return( psram );
}
else {
log_e("allocate %dbytes(%p) json psram failed", size, psram );
while(1);
}
}
void deallocate( void* pointer ) {
log_i("deallocate (%p) json psram", pointer );
free( pointer );
}
};
using SpiRamJsonDocument = BasicJsonDocument<UpdateSpiRamAllocator>;
#include "hardware/json_config_psram_allocator.h"
uint64_t update_check_new_version( void ) {
char url[512]="";

View File

@@ -29,6 +29,8 @@
#include "gui/statusbar.h"
#include "gui/keyboard.h"
#include "hardware/json_config_psram_allocator.h"
update_config_t update_config;
lv_obj_t *update_setup_tile = NULL;
@@ -88,7 +90,7 @@ void update_setup_tile_setup( uint32_t tile_num ) {
lv_label_set_text( update_check_autosync_label, "check for updates");
lv_obj_align( update_check_autosync_label, update_check_autosync_cont, LV_ALIGN_IN_LEFT_MID, 5, 0 );
if ( update_config.autosync)
if ( update_config.autosync )
lv_switch_on( update_check_autosync_onoff, LV_ANIM_OFF);
else
lv_switch_off( update_check_autosync_onoff, LV_ANIM_OFF);
@@ -112,33 +114,71 @@ static void exit_update_check_setup_event_cb( lv_obj_t * obj, lv_event_t event )
}
void update_save_config( void ) {
fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_WRITE );
if ( SPIFFS.exists( UPDATE_CONFIG_FILE ) ) {
SPIFFS.remove( UPDATE_CONFIG_FILE );
log_i("remove old binary update config");
}
if ( !file ) {
log_e("Can't save file: %s", UPDATE_CONFIG_FILE );
}
else {
file.write( (uint8_t *)&update_config, sizeof( update_config ) );
fs::File file = SPIFFS.open( UPDATE_JSON_CONFIG_FILE, FILE_WRITE );
if (!file) {
log_e("Can't open file: %s!", UPDATE_JSON_CONFIG_FILE );
}
else {
SpiRamJsonDocument doc( 1000 );
doc["autosync"] = update_config.autosync;
if ( serializeJsonPretty( doc, file ) == 0) {
log_e("Failed to write config file");
}
doc.clear();
}
file.close();
}
}
void update_read_config( void ) {
fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_READ );
if ( SPIFFS.exists( UPDATE_JSON_CONFIG_FILE ) ) {
fs::File file = SPIFFS.open( UPDATE_JSON_CONFIG_FILE, FILE_READ );
if (!file) {
log_e("Can't open file: %s!", UPDATE_JSON_CONFIG_FILE );
}
else {
int filesize = file.size();
SpiRamJsonDocument doc( filesize * 2 );
if (!file) {
log_e("Can't open file: %s!", UPDATE_CONFIG_FILE );
}
else {
int filesize = file.size();
if ( filesize > sizeof( update_config ) ) {
log_e("Failed to read configfile. Wrong filesize!" );
DeserializationError error = deserializeJson( doc, file );
if ( error ) {
log_e("update check deserializeJson() failed: %s", error.c_str() );
}
else {
update_config.autosync = doc["autosync"].as<bool>();
}
doc.clear();
}
file.close();
}
else {
file.read( (uint8_t *)&update_config, filesize );
log_i("no json config exists, read from binary");
fs::File file = SPIFFS.open( UPDATE_CONFIG_FILE, FILE_READ );
if (!file) {
log_e("Can't open file: %s!", UPDATE_CONFIG_FILE );
}
else {
int filesize = file.size();
if ( filesize > sizeof( update_config ) ) {
log_e("Failed to read configfile. Wrong filesize!" );
}
else {
file.read( (uint8_t *)&update_config, filesize );
file.close();
update_save_config();
return;
}
file.close();
}
}
file.close();
}
}
bool update_setup_get_autosync( void ) {

View File

@@ -25,6 +25,7 @@
#include <TTGO.h>
#define UPDATE_CONFIG_FILE "/update.cfg"
#define UPDATE_JSON_CONFIG_FILE "/update.json"
typedef struct {
bool autosync = true;