diff --git a/companion/src/file.cpp b/companion/src/file.cpp index 49a6fa2ea..ea73480cc 100644 --- a/companion/src/file.cpp +++ b/companion/src/file.cpp @@ -436,9 +436,11 @@ unsigned int importRlc(QByteArray & dst, QByteArray & src, unsigned int rlcVersi dst.resize(0); for( ; 1; ) { - for (int i=0; i 0) { + for (int i = 0; i < zeroes; i++) + dst.append((char) 0); + zeroes = 0; + } if (len == 0) return dst.size(); diff --git a/radio/src/CMakeLists.txt b/radio/src/CMakeLists.txt index ae23ec22c..c9e9b0567 100644 --- a/radio/src/CMakeLists.txt +++ b/radio/src/CMakeLists.txt @@ -703,7 +703,7 @@ set(GUI_SRC if(RAMBACKUP) add_definitions(-DRAMBACKUP) - set(SRC ${SRC} storage/rambackup.cpp) + set(SRC ${SRC} storage/rambackup.cpp storage/rlc.cpp) endif() if(VIRTUAL_INPUTS) diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 6ab363c4a..b44816657 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -1975,11 +1975,6 @@ uint8_t calcStickScroll( uint8_t index ) void opentxStart() { -#if defined(RAMBACKUP) && defined(SIMU) - // TODO just for tests - RamBackupWrite(); -#endif - doSplash(); #if defined(DEBUG_TRACE_BUFFER) diff --git a/radio/src/storage/rambackup.cpp b/radio/src/storage/rambackup.cpp index 6129e2910..85e0d1a03 100644 --- a/radio/src/storage/rambackup.cpp +++ b/radio/src/storage/rambackup.cpp @@ -23,7 +23,7 @@ namespace Backup { #define BACKUP #include "datastructs.h" -PACK(struct RamBackup { +PACK(struct RamBackupUncompressed { ModelData model; RadioData radio; }); @@ -32,17 +32,42 @@ PACK(struct RamBackup { #include "datacopy.cpp" -Backup::RamBackup ramBackup; +Backup::RamBackupUncompressed ramBackupUncompressed __DMA; -void RamBackupWrite() +PACK(struct RamBackup { + uint16_t size; + uint8_t data[4094]; +}); + +RamBackup ramBackup; + +void rambackupWrite() { - TRACE("RamBackup size=%d vs %d", sizeof(Backup::RamBackup), sizeof(ModelData)+sizeof(RadioData)); - copyRadioData(&ramBackup.radio, &g_eeGeneral); - copyModelData(&ramBackup.model, &g_model); + copyRadioData(&ramBackupUncompressed.radio, &g_eeGeneral); + copyModelData(&ramBackupUncompressed.model, &g_model); + ramBackup.size = compress(ramBackup.data, (const uint8_t *)&ramBackupUncompressed, sizeof(ramBackupUncompressed)); + TRACE("RamBackupWrite sdsize=%d backupsize=%d rlcsize=%d", sizeof(ModelData)+sizeof(RadioData), sizeof(Backup::RamBackupUncompressed), ramBackup.size); + +#if 0 + // TODO move this code to non regression tests + Backup::RamBackupUncompressed ramBackupRestored; + if (uncompress((uint8_t *)&ramBackupRestored, ramBackup.data, ramBackup.size) != sizeof(ramBackupUncompressed)) + TRACE("ERROR size"); + if (memcmp(&ramBackupUncompressed, &ramBackupRestored, sizeof(ramBackupUncompressed)) != 0) + TRACE("ERROR restore"); +#endif } -void RamBackupRead() +bool rambackupRestore() { - copyRadioData(&g_eeGeneral, &ramBackup.radio); - copyModelData(&g_model, &ramBackup.model); + if (ramBackup.size == 0) + return false; + + if (uncompress((uint8_t *)&ramBackupUncompressed, ramBackup.data, ramBackup.size) != sizeof(ramBackupUncompressed)) + return false; + + copyRadioData(&g_eeGeneral, &ramBackupUncompressed.radio); + copyModelData(&g_model, &ramBackupUncompressed.model); + return true; } + diff --git a/radio/src/storage/rlc.cpp b/radio/src/storage/rlc.cpp new file mode 100644 index 000000000..595d39c2a --- /dev/null +++ b/radio/src/storage/rlc.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include "debug.h" + +unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len) +{ + uint8_t * cur = dst; + bool run0 = (src[0] == 0); + uint8_t cnt = 1; + uint8_t cnt0 = 0; + + for (unsigned int i=1; 1; i++) { + bool cur0 = (src[i] == 0); + if (i==len || cur0!=run0 || cnt==0x3f || (cnt0 && cnt==0xf)) { + if (run0) { + assert(cnt0==0); + if (cnt<8 && i!=len) + cnt0 = cnt; + else + *cur++ = (cnt|0x40); + } + else { + if (cnt0) { + *cur++ = (0x80 | (cnt0<<4) | cnt); + cnt0 = 0; + } + else { + *cur++ = cnt; + } + for (int j=0; j 0) { + for (int i = 0; i < zeroes; i++) { + *cur++ = 0; + } + zeroes = 0; + } + + if (len == 0) + return cur-dst; + + for (int i=0; i>4) & 0x07; + bRlc = bRlc & 0x0f; + } + else if (bRlc & 0x40) { + zeroes = bRlc & 0x3f; + bRlc = 0; + } + } +} diff --git a/radio/src/storage/storage.h b/radio/src/storage/storage.h index c3fe87ca7..0dd292c11 100644 --- a/radio/src/storage/storage.h +++ b/radio/src/storage/storage.h @@ -58,8 +58,10 @@ void postModelLoad(bool newModel); #endif #if defined(RAMBACKUP) -void RamBackupWrite(); -void RamBackupRead(); +void rambackupWrite(); +bool rambackupRestore(); +unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len); +unsigned int uncompress(uint8_t * dst, const uint8_t * src, unsigned int len); #endif #endif // _STORAGE_H_