DataStore.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #include <Arduino.h>
  2. #include "DataStore.h"
  3. #if defined(EXTRAFS) || defined(QSPIFLASH)
  4. #define MAX_BLOBRECS 100
  5. #else
  6. #define MAX_BLOBRECS 20
  7. #endif
  8. DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
  9. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  10. identity_store(fs, "")
  11. #elif defined(RP2040_PLATFORM)
  12. identity_store(fs, "/identity")
  13. #else
  14. identity_store(fs, "/identity")
  15. #endif
  16. {
  17. }
  18. #if defined(EXTRAFS) || defined(QSPIFLASH)
  19. DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(&fsExtra), _clock(&clock),
  20. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  21. identity_store(fs, "")
  22. #elif defined(RP2040_PLATFORM)
  23. identity_store(fs, "/identity")
  24. #else
  25. identity_store(fs, "/identity")
  26. #endif
  27. {
  28. }
  29. #endif
  30. static File openWrite(FILESYSTEM* fs, const char* filename) {
  31. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  32. fs->remove(filename);
  33. return fs->open(filename, FILE_O_WRITE);
  34. #elif defined(RP2040_PLATFORM)
  35. return fs->open(filename, "w");
  36. #else
  37. return fs->open(filename, "w", true);
  38. #endif
  39. }
  40. void DataStore::begin() {
  41. #if defined(RP2040_PLATFORM)
  42. identity_store.begin();
  43. #endif
  44. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  45. checkAdvBlobFile();
  46. #if defined(EXTRAFS) || defined(QSPIFLASH)
  47. migrateToSecondaryFS();
  48. #endif
  49. #else
  50. // init 'blob store' support
  51. _fs->mkdir("/bl");
  52. #endif
  53. }
  54. #if defined(ESP32)
  55. #include <SPIFFS.h>
  56. #elif defined(RP2040_PLATFORM)
  57. #include <LittleFS.h>
  58. #elif defined(NRF52_PLATFORM)
  59. #include <CustomLFS.h>
  60. #if defined(QSPIFLASH)
  61. #include <CustomLFS_QSPIFlash.h>
  62. #endif
  63. #elif defined(STM32_PLATFORM)
  64. #include <InternalFileSystem.h>
  65. #endif
  66. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  67. int _countLfsBlock(void *p, lfs_block_t block){
  68. lfs_size_t *size = (lfs_size_t*) p;
  69. *size += 1;
  70. return 0;
  71. }
  72. lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) {
  73. lfs_size_t size = 0;
  74. lfs_traverse(fs->_getFS(), _countLfsBlock, &size);
  75. return size;
  76. }
  77. #endif
  78. uint32_t DataStore::getStorageUsedKb() const {
  79. #if defined(ESP32)
  80. return SPIFFS.usedBytes() / 1024;
  81. #elif defined(RP2040_PLATFORM)
  82. FSInfo info;
  83. info.usedBytes = 0;
  84. _fs->info(info);
  85. return info.usedBytes / 1024;
  86. #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  87. #if defined(EXTRAFS) || defined(QSPIFLASH)
  88. const lfs_config* config = _fsExtra->_getFS()->cfg;
  89. int usedBlockCount = _getLfsUsedBlockCount(_fsExtra);
  90. #else
  91. const lfs_config* config = _fs->_getFS()->cfg;
  92. int usedBlockCount = _getLfsUsedBlockCount(_fs);
  93. #endif
  94. int usedBytes = config->block_size * usedBlockCount;
  95. return usedBytes / 1024;
  96. #else
  97. return 0;
  98. #endif
  99. }
  100. uint32_t DataStore::getStorageTotalKb() const {
  101. #if defined(ESP32)
  102. return SPIFFS.totalBytes() / 1024;
  103. #elif defined(RP2040_PLATFORM)
  104. FSInfo info;
  105. info.totalBytes = 0;
  106. _fs->info(info);
  107. return info.totalBytes / 1024;
  108. #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  109. #if defined(EXTRAFS) || defined(QSPIFLASH)
  110. const lfs_config* config = _fsExtra->_getFS()->cfg;
  111. #else
  112. const lfs_config* config = _fs->_getFS()->cfg;
  113. #endif
  114. int totalBytes = config->block_size * config->block_count;
  115. return totalBytes / 1024;
  116. #else
  117. return 0;
  118. #endif
  119. }
  120. File DataStore::openRead(const char* filename) {
  121. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  122. return _fs->open(filename, FILE_O_READ);
  123. #elif defined(RP2040_PLATFORM)
  124. return _fs->open(filename, "r");
  125. #else
  126. return _fs->open(filename, "r", false);
  127. #endif
  128. }
  129. File DataStore::openRead(FILESYSTEM* fs, const char* filename) {
  130. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  131. return fs->open(filename, FILE_O_READ);
  132. #elif defined(RP2040_PLATFORM)
  133. return fs->open(filename, "r");
  134. #else
  135. return fs->open(filename, "r", false);
  136. #endif
  137. }
  138. bool DataStore::removeFile(const char* filename) {
  139. return _fs->remove(filename);
  140. }
  141. bool DataStore::removeFile(FILESYSTEM* fs, const char* filename) {
  142. return fs->remove(filename);
  143. }
  144. bool DataStore::formatFileSystem() {
  145. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  146. #if defined(EXTRAFS) || defined(QSPIFLASH)
  147. return _fs->format() && _fsExtra->format(); // in future maybe return an error code based on which format failed?
  148. #else
  149. return _fs->format();
  150. #endif
  151. #elif defined(RP2040_PLATFORM)
  152. return LittleFS.format();
  153. #elif defined(ESP32)
  154. return ((fs::SPIFFSFS *)_fs)->format();
  155. #else
  156. #error "need to implement format()"
  157. #endif
  158. }
  159. bool DataStore::loadMainIdentity(mesh::LocalIdentity &identity) {
  160. return identity_store.load("_main", identity);
  161. }
  162. bool DataStore::saveMainIdentity(const mesh::LocalIdentity &identity) {
  163. return identity_store.save("_main", identity);
  164. }
  165. void DataStore::loadPrefs(NodePrefs& prefs, double& node_lat, double& node_lon) {
  166. if (_fs->exists("/new_prefs")) {
  167. loadPrefsInt("/new_prefs", prefs, node_lat, node_lon); // new filename
  168. } else if (_fs->exists("/node_prefs")) {
  169. loadPrefsInt("/node_prefs", prefs, node_lat, node_lon);
  170. savePrefs(prefs, node_lat, node_lon); // save to new filename
  171. _fs->remove("/node_prefs"); // remove old
  172. }
  173. }
  174. void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& node_lat, double& node_lon) {
  175. #if defined(RP2040_PLATFORM)
  176. File file = _fs->open(filename, "r");
  177. #else
  178. File file = _fs->open(filename);
  179. #endif
  180. if (file) {
  181. uint8_t pad[8];
  182. file.read((uint8_t *)&_prefs.airtime_factor, sizeof(float)); // 0
  183. file.read((uint8_t *)_prefs.node_name, sizeof(_prefs.node_name)); // 4
  184. file.read(pad, 4); // 36
  185. file.read((uint8_t *)&node_lat, sizeof(node_lat)); // 40
  186. file.read((uint8_t *)&node_lon, sizeof(node_lon)); // 48
  187. file.read((uint8_t *)&_prefs.freq, sizeof(_prefs.freq)); // 56
  188. file.read((uint8_t *)&_prefs.sf, sizeof(_prefs.sf)); // 60
  189. file.read((uint8_t *)&_prefs.cr, sizeof(_prefs.cr)); // 61
  190. file.read(pad, 1); // 62
  191. file.read((uint8_t *)&_prefs.manual_add_contacts, sizeof(_prefs.manual_add_contacts)); // 63
  192. file.read((uint8_t *)&_prefs.bw, sizeof(_prefs.bw)); // 64
  193. file.read((uint8_t *)&_prefs.tx_power_dbm, sizeof(_prefs.tx_power_dbm)); // 68
  194. file.read((uint8_t *)&_prefs.telemetry_mode_base, sizeof(_prefs.telemetry_mode_base)); // 69
  195. file.read((uint8_t *)&_prefs.telemetry_mode_loc, sizeof(_prefs.telemetry_mode_loc)); // 70
  196. file.read((uint8_t *)&_prefs.telemetry_mode_env, sizeof(_prefs.telemetry_mode_env)); // 71
  197. file.read((uint8_t *)&_prefs.rx_delay_base, sizeof(_prefs.rx_delay_base)); // 72
  198. file.read((uint8_t *)&_prefs.advert_loc_policy, sizeof(_prefs.advert_loc_policy)); // 76
  199. file.read((uint8_t *)&_prefs.multi_acks, sizeof(_prefs.multi_acks)); // 77
  200. file.read(pad, 2); // 78
  201. file.read((uint8_t *)&_prefs.ble_pin, sizeof(_prefs.ble_pin)); // 80
  202. file.close();
  203. }
  204. }
  205. void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_lon) {
  206. File file = openWrite(_fs, "/new_prefs");
  207. if (file) {
  208. uint8_t pad[8];
  209. memset(pad, 0, sizeof(pad));
  210. file.write((uint8_t *)&_prefs.airtime_factor, sizeof(float)); // 0
  211. file.write((uint8_t *)_prefs.node_name, sizeof(_prefs.node_name)); // 4
  212. file.write(pad, 4); // 36
  213. file.write((uint8_t *)&node_lat, sizeof(node_lat)); // 40
  214. file.write((uint8_t *)&node_lon, sizeof(node_lon)); // 48
  215. file.write((uint8_t *)&_prefs.freq, sizeof(_prefs.freq)); // 56
  216. file.write((uint8_t *)&_prefs.sf, sizeof(_prefs.sf)); // 60
  217. file.write((uint8_t *)&_prefs.cr, sizeof(_prefs.cr)); // 61
  218. file.write(pad, 1); // 62
  219. file.write((uint8_t *)&_prefs.manual_add_contacts, sizeof(_prefs.manual_add_contacts)); // 63
  220. file.write((uint8_t *)&_prefs.bw, sizeof(_prefs.bw)); // 64
  221. file.write((uint8_t *)&_prefs.tx_power_dbm, sizeof(_prefs.tx_power_dbm)); // 68
  222. file.write((uint8_t *)&_prefs.telemetry_mode_base, sizeof(_prefs.telemetry_mode_base)); // 69
  223. file.write((uint8_t *)&_prefs.telemetry_mode_loc, sizeof(_prefs.telemetry_mode_loc)); // 70
  224. file.write((uint8_t *)&_prefs.telemetry_mode_env, sizeof(_prefs.telemetry_mode_env)); // 71
  225. file.write((uint8_t *)&_prefs.rx_delay_base, sizeof(_prefs.rx_delay_base)); // 72
  226. file.write((uint8_t *)&_prefs.advert_loc_policy, sizeof(_prefs.advert_loc_policy)); // 76
  227. file.write((uint8_t *)&_prefs.multi_acks, sizeof(_prefs.multi_acks)); // 77
  228. file.write(pad, 2); // 78
  229. file.write((uint8_t *)&_prefs.ble_pin, sizeof(_prefs.ble_pin)); // 80
  230. file.close();
  231. }
  232. }
  233. void DataStore::loadContacts(DataStoreHost* host) {
  234. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  235. #if defined(EXTRAFS) || defined(QSPIFLASH)
  236. if (_fsExtra->exists("/contacts3")) {
  237. File file = _fsExtra->open("/contacts3");
  238. #else
  239. if (_fs->exists("/contacts3")) {
  240. File file = _fs->open("/contacts3");
  241. #endif
  242. #elif defined(RP2040_PLATFORM)
  243. if (_fs->exists("/contacts3")) {
  244. File file = _fs->open("/contacts3", "r");
  245. #else
  246. if (_fs->exists("/contacts3")) {
  247. File file = _fs->open("/contacts3", "r", false);
  248. #endif
  249. if (file) {
  250. bool full = false;
  251. while (!full) {
  252. ContactInfo c;
  253. uint8_t pub_key[32];
  254. uint8_t unused;
  255. bool success = (file.read(pub_key, 32) == 32);
  256. success = success && (file.read((uint8_t *)&c.name, 32) == 32);
  257. success = success && (file.read(&c.type, 1) == 1);
  258. success = success && (file.read(&c.flags, 1) == 1);
  259. success = success && (file.read(&unused, 1) == 1);
  260. success = success && (file.read((uint8_t *)&c.sync_since, 4) == 4); // was 'reserved'
  261. success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1);
  262. success = success && (file.read((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  263. success = success && (file.read(c.out_path, 64) == 64);
  264. success = success && (file.read((uint8_t *)&c.lastmod, 4) == 4);
  265. success = success && (file.read((uint8_t *)&c.gps_lat, 4) == 4);
  266. success = success && (file.read((uint8_t *)&c.gps_lon, 4) == 4);
  267. if (!success) break; // EOF
  268. c.id = mesh::Identity(pub_key);
  269. if (!host->onContactLoaded(c)) full = true;
  270. }
  271. file.close();
  272. }
  273. }
  274. }
  275. void DataStore::saveContacts(DataStoreHost* host) {
  276. #if defined(EXTRAFS) || defined(QSPIFLASH)
  277. File file = openWrite(_fsExtra, "/contacts3");
  278. #else
  279. File file = openWrite(_fs, "/contacts3");
  280. #endif
  281. if (file) {
  282. uint32_t idx = 0;
  283. ContactInfo c;
  284. uint8_t unused = 0;
  285. while (host->getContactForSave(idx, c)) {
  286. bool success = (file.write(c.id.pub_key, 32) == 32);
  287. success = success && (file.write((uint8_t *)&c.name, 32) == 32);
  288. success = success && (file.write(&c.type, 1) == 1);
  289. success = success && (file.write(&c.flags, 1) == 1);
  290. success = success && (file.write(&unused, 1) == 1);
  291. success = success && (file.write((uint8_t *)&c.sync_since, 4) == 4);
  292. success = success && (file.write((uint8_t *)&c.out_path_len, 1) == 1);
  293. success = success && (file.write((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  294. success = success && (file.write(c.out_path, 64) == 64);
  295. success = success && (file.write((uint8_t *)&c.lastmod, 4) == 4);
  296. success = success && (file.write((uint8_t *)&c.gps_lat, 4) == 4);
  297. success = success && (file.write((uint8_t *)&c.gps_lon, 4) == 4);
  298. if (!success) break; // write failed
  299. idx++; // advance to next contact
  300. }
  301. file.close();
  302. }
  303. }
  304. void DataStore::loadChannels(DataStoreHost* host) {
  305. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  306. #if defined(EXTRAFS) || defined(QSPIFLASH)
  307. if (_fsExtra->exists("/channels2")) {
  308. File file = _fsExtra->open("/channels2");
  309. #else
  310. if (_fs->exists("/channels2")) {
  311. File file = _fs->open("/channels2");
  312. #endif
  313. #elif defined(RP2040_PLATFORM)
  314. if (_fs->exists("/channels2")) {
  315. File file = _fs->open("/channels2", "r");
  316. #else
  317. if (_fs->exists("/channels2")) {
  318. File file = _fs->open("/channels2", "r", false);
  319. #endif
  320. if (file) {
  321. bool full = false;
  322. uint8_t channel_idx = 0;
  323. while (!full) {
  324. ChannelDetails ch;
  325. uint8_t unused[4];
  326. bool success = (file.read(unused, 4) == 4);
  327. success = success && (file.read((uint8_t *)ch.name, 32) == 32);
  328. success = success && (file.read((uint8_t *)ch.channel.secret, 32) == 32);
  329. if (!success) break; // EOF
  330. if (host->onChannelLoaded(channel_idx, ch)) {
  331. channel_idx++;
  332. } else {
  333. full = true;
  334. }
  335. }
  336. file.close();
  337. }
  338. }
  339. }
  340. void DataStore::saveChannels(DataStoreHost* host) {
  341. #if defined(EXTRAFS) || defined(QSPIFLASH)
  342. File file = openWrite(_fsExtra, "/channels2");
  343. #else
  344. File file = openWrite(_fs, "/channels2");
  345. #endif
  346. if (file) {
  347. uint8_t channel_idx = 0;
  348. ChannelDetails ch;
  349. uint8_t unused[4];
  350. memset(unused, 0, 4);
  351. while (host->getChannelForSave(channel_idx, ch)) {
  352. bool success = (file.write(unused, 4) == 4);
  353. success = success && (file.write((uint8_t *)ch.name, 32) == 32);
  354. success = success && (file.write((uint8_t *)ch.channel.secret, 32) == 32);
  355. if (!success) break; // write failed
  356. channel_idx++;
  357. }
  358. file.close();
  359. }
  360. }
  361. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  362. #define MAX_ADVERT_PKT_LEN (2 + 32 + PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE)
  363. struct BlobRec {
  364. uint32_t timestamp;
  365. uint8_t key[7];
  366. uint8_t len;
  367. uint8_t data[MAX_ADVERT_PKT_LEN];
  368. };
  369. void DataStore::checkAdvBlobFile() {
  370. #if defined(EXTRAFS) || defined(QSPIFLASH)
  371. if (!_fsExtra->exists("/adv_blobs")) {
  372. File file = openWrite(_fsExtra, "/adv_blobs");
  373. #else
  374. if (!_fs->exists("/adv_blobs")) {
  375. File file = openWrite(_fs, "/adv_blobs");
  376. #endif
  377. if (file) {
  378. BlobRec zeroes;
  379. memset(&zeroes, 0, sizeof(zeroes));
  380. for (int i = 0; i < MAX_BLOBRECS; i++) { // pre-allocate to fixed size
  381. file.write((uint8_t *) &zeroes, sizeof(zeroes));
  382. }
  383. file.close();
  384. }
  385. }
  386. }
  387. void DataStore::migrateToSecondaryFS() {
  388. // migrate old adv_blobs, contacts3 and channels2 files to secondary FS if they don't already exist
  389. if (!_fsExtra->exists("/adv_blobs")) {
  390. if (_fs->exists("/adv_blobs")) {
  391. File oldAdvBlobs = openRead(_fs, "/adv_blobs");
  392. File newAdvBlobs = openWrite(_fsExtra, "/adv_blobs");
  393. if (oldAdvBlobs && newAdvBlobs) {
  394. BlobRec rec;
  395. size_t count = 0;
  396. // Copy 20 BlobRecs from old to new
  397. while (count < 20 && oldAdvBlobs.read((uint8_t *)&rec, sizeof(rec)) == sizeof(rec)) {
  398. newAdvBlobs.seek(count * sizeof(BlobRec));
  399. newAdvBlobs.write((uint8_t *)&rec, sizeof(rec));
  400. count++;
  401. }
  402. }
  403. if (oldAdvBlobs) oldAdvBlobs.close();
  404. if (newAdvBlobs) newAdvBlobs.close();
  405. _fs->remove("/adv_blobs");
  406. }
  407. }
  408. if (!_fsExtra->exists("/contacts3")) {
  409. if (_fs->exists("/contacts3")) {
  410. File oldFile = openRead(_fs, "/contacts3");
  411. File newFile = openWrite(_fsExtra, "/contacts3");
  412. if (oldFile && newFile) {
  413. uint8_t buf[64];
  414. int n;
  415. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  416. newFile.write(buf, n);
  417. }
  418. }
  419. if (oldFile) oldFile.close();
  420. if (newFile) newFile.close();
  421. _fs->remove("/contacts3");
  422. }
  423. }
  424. if (!_fsExtra->exists("/channels2")) {
  425. if (_fs->exists("/contacts2")) {
  426. File oldFile = openRead(_fs, "/contacts2");
  427. File newFile = openWrite(_fsExtra, "/contacts2");
  428. if (oldFile && newFile) {
  429. uint8_t buf[64];
  430. int n;
  431. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  432. newFile.write(buf, n);
  433. }
  434. }
  435. if (oldFile) oldFile.close();
  436. if (newFile) newFile.close();
  437. _fs->remove("/contacts2");
  438. }
  439. }
  440. // cleanup nodes which have been testing the extra fs, copy _main.id and new_prefs back to primary
  441. if (_fsExtra->exists("/_main.id")) {
  442. if (_fs->exists("/_main.id")) {_fs->remove("/_main.id");}
  443. File oldFile = openRead(_fsExtra, "/_main.id");
  444. File newFile = openWrite(_fs, "/_main.id");
  445. if (oldFile && newFile) {
  446. uint8_t buf[64];
  447. int n;
  448. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  449. newFile.write(buf, n);
  450. }
  451. }
  452. if (oldFile) oldFile.close();
  453. if (newFile) newFile.close();
  454. _fsExtra->remove("/_main.id");
  455. }
  456. if (_fsExtra->exists("/new_prefs")) {
  457. if (_fs->exists("/new_prefs")) {_fs->remove("/new_prefs");}
  458. File oldFile = openRead(_fsExtra, "/new_prefs");
  459. File newFile = openWrite(_fs, "/new_prefs");
  460. if (oldFile && newFile) {
  461. uint8_t buf[64];
  462. int n;
  463. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  464. newFile.write(buf, n);
  465. }
  466. }
  467. if (oldFile) oldFile.close();
  468. if (newFile) newFile.close();
  469. _fsExtra->remove("/new_prefs");
  470. }
  471. // remove files from where they should not be anymore
  472. if (_fs->exists("/adv_blobs")) {
  473. _fs->remove("/adv_blobs");
  474. }
  475. if (_fs->exists("/contacts3")) {
  476. _fs->remove("/contacts3");
  477. }
  478. if (_fsExtra->exists("/_main.id")) {
  479. _fsExtra->remove("/_main.id");
  480. }
  481. if (_fsExtra->exists("/new_prefs")) {
  482. _fsExtra->remove("/new_prefs");
  483. }
  484. }
  485. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  486. #if defined(EXTRAFS) || defined(QSPIFLASH)
  487. File file = _fsExtra->open("/adv_blobs");
  488. #else
  489. File file = _fs->open("/adv_blobs");
  490. #endif
  491. uint8_t len = 0; // 0 = not found
  492. if (file) {
  493. BlobRec tmp;
  494. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  495. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  496. len = tmp.len;
  497. memcpy(dest_buf, tmp.data, len);
  498. break;
  499. }
  500. }
  501. file.close();
  502. }
  503. return len;
  504. }
  505. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  506. if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
  507. checkAdvBlobFile();
  508. #if defined(EXTRAFS) || defined(QSPIFLASH)
  509. File file = _fsExtra->open("/adv_blobs", FILE_O_WRITE);
  510. #else
  511. File file = _fs->open("/adv_blobs", FILE_O_WRITE);
  512. #endif
  513. if (file) {
  514. uint32_t pos = 0, found_pos = 0;
  515. uint32_t min_timestamp = 0xFFFFFFFF;
  516. // search for matching key OR evict by oldest timestmap
  517. BlobRec tmp;
  518. file.seek(0);
  519. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  520. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  521. found_pos = pos;
  522. break;
  523. }
  524. if (tmp.timestamp < min_timestamp) {
  525. min_timestamp = tmp.timestamp;
  526. found_pos = pos;
  527. }
  528. pos += sizeof(tmp);
  529. }
  530. memcpy(tmp.key, key, sizeof(tmp.key)); // just record 7 byte prefix of key
  531. memcpy(tmp.data, src_buf, len);
  532. tmp.len = len;
  533. tmp.timestamp = _clock->getCurrentTime();
  534. file.seek(found_pos);
  535. file.write((uint8_t *) &tmp, sizeof(tmp));
  536. file.close();
  537. return true;
  538. }
  539. return false; // error
  540. }
  541. #else
  542. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  543. char path[64];
  544. char fname[18];
  545. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  546. mesh::Utils::toHex(fname, key, key_len);
  547. sprintf(path, "/bl/%s", fname);
  548. if (_fs->exists(path)) {
  549. #if defined(RP2040_PLATFORM)
  550. File f = _fs->open(path, "r");
  551. #else
  552. File f = _fs->open(path);
  553. #endif
  554. if (f) {
  555. int len = f.read(dest_buf, 255); // currently MAX 255 byte blob len supported!!
  556. f.close();
  557. return len;
  558. }
  559. }
  560. return 0; // not found
  561. }
  562. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  563. char path[64];
  564. char fname[18];
  565. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  566. mesh::Utils::toHex(fname, key, key_len);
  567. sprintf(path, "/bl/%s", fname);
  568. File f = openWrite(_fs, path);
  569. if (f) {
  570. int n = f.write(src_buf, len);
  571. f.close();
  572. if (n == len) return true; // success!
  573. _fs->remove(path); // blob was only partially written!
  574. }
  575. return false; // error
  576. }
  577. #endif