DataStore.cpp 20 KB

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