DataStore.cpp 21 KB

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