DataStore.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. File file = openRead(_fs, filename);
  180. if (file) {
  181. uint8_t pad[8];
  182. // Initialize defaults for any missing fields (backward compatibility)
  183. memset(&_prefs, 0, sizeof(_prefs));
  184. node_lat = 0.0;
  185. node_lon = 0.0;
  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.read((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84
  207. file.close();
  208. }
  209. }
  210. void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_lon) {
  211. File file = openWrite(_fs, "/new_prefs");
  212. if (file) {
  213. uint8_t pad[8];
  214. memset(pad, 0, sizeof(pad));
  215. file.write((uint8_t *)&_prefs.airtime_factor, sizeof(float)); // 0
  216. file.write((uint8_t *)_prefs.node_name, sizeof(_prefs.node_name)); // 4
  217. file.write(pad, 4); // 36
  218. file.write((uint8_t *)&node_lat, sizeof(node_lat)); // 40
  219. file.write((uint8_t *)&node_lon, sizeof(node_lon)); // 48
  220. file.write((uint8_t *)&_prefs.freq, sizeof(_prefs.freq)); // 56
  221. file.write((uint8_t *)&_prefs.sf, sizeof(_prefs.sf)); // 60
  222. file.write((uint8_t *)&_prefs.cr, sizeof(_prefs.cr)); // 61
  223. file.write(pad, 1); // 62
  224. file.write((uint8_t *)&_prefs.manual_add_contacts, sizeof(_prefs.manual_add_contacts)); // 63
  225. file.write((uint8_t *)&_prefs.bw, sizeof(_prefs.bw)); // 64
  226. file.write((uint8_t *)&_prefs.tx_power_dbm, sizeof(_prefs.tx_power_dbm)); // 68
  227. file.write((uint8_t *)&_prefs.telemetry_mode_base, sizeof(_prefs.telemetry_mode_base)); // 69
  228. file.write((uint8_t *)&_prefs.telemetry_mode_loc, sizeof(_prefs.telemetry_mode_loc)); // 70
  229. file.write((uint8_t *)&_prefs.telemetry_mode_env, sizeof(_prefs.telemetry_mode_env)); // 71
  230. file.write((uint8_t *)&_prefs.rx_delay_base, sizeof(_prefs.rx_delay_base)); // 72
  231. file.write((uint8_t *)&_prefs.advert_loc_policy, sizeof(_prefs.advert_loc_policy)); // 76
  232. file.write((uint8_t *)&_prefs.multi_acks, sizeof(_prefs.multi_acks)); // 77
  233. file.write(pad, 2); // 78
  234. file.write((uint8_t *)&_prefs.ble_pin, sizeof(_prefs.ble_pin)); // 80
  235. file.write((uint8_t *)&_prefs.buzzer_quiet, sizeof(_prefs.buzzer_quiet)); // 84
  236. file.close();
  237. }
  238. }
  239. void DataStore::loadContacts(DataStoreHost* host) {
  240. File file = openRead(_getContactsChannelsFS(), "/contacts3");
  241. if (file) {
  242. bool full = false;
  243. while (!full) {
  244. ContactInfo c;
  245. uint8_t pub_key[32];
  246. uint8_t unused;
  247. bool success = (file.read(pub_key, 32) == 32);
  248. success = success && (file.read((uint8_t *)&c.name, 32) == 32);
  249. success = success && (file.read(&c.type, 1) == 1);
  250. success = success && (file.read(&c.flags, 1) == 1);
  251. success = success && (file.read(&unused, 1) == 1);
  252. success = success && (file.read((uint8_t *)&c.sync_since, 4) == 4); // was 'reserved'
  253. success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1);
  254. success = success && (file.read((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  255. success = success && (file.read(c.out_path, 64) == 64);
  256. success = success && (file.read((uint8_t *)&c.lastmod, 4) == 4);
  257. success = success && (file.read((uint8_t *)&c.gps_lat, 4) == 4);
  258. success = success && (file.read((uint8_t *)&c.gps_lon, 4) == 4);
  259. if (!success) break; // EOF
  260. c.id = mesh::Identity(pub_key);
  261. if (!host->onContactLoaded(c)) full = true;
  262. }
  263. file.close();
  264. }
  265. }
  266. void DataStore::saveContacts(DataStoreHost* host) {
  267. File file = openWrite(_getContactsChannelsFS(), "/contacts3");
  268. if (file) {
  269. uint32_t idx = 0;
  270. ContactInfo c;
  271. uint8_t unused = 0;
  272. while (host->getContactForSave(idx, c)) {
  273. bool success = (file.write(c.id.pub_key, 32) == 32);
  274. success = success && (file.write((uint8_t *)&c.name, 32) == 32);
  275. success = success && (file.write(&c.type, 1) == 1);
  276. success = success && (file.write(&c.flags, 1) == 1);
  277. success = success && (file.write(&unused, 1) == 1);
  278. success = success && (file.write((uint8_t *)&c.sync_since, 4) == 4);
  279. success = success && (file.write((uint8_t *)&c.out_path_len, 1) == 1);
  280. success = success && (file.write((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  281. success = success && (file.write(c.out_path, 64) == 64);
  282. success = success && (file.write((uint8_t *)&c.lastmod, 4) == 4);
  283. success = success && (file.write((uint8_t *)&c.gps_lat, 4) == 4);
  284. success = success && (file.write((uint8_t *)&c.gps_lon, 4) == 4);
  285. if (!success) break; // write failed
  286. idx++; // advance to next contact
  287. }
  288. file.close();
  289. }
  290. }
  291. void DataStore::loadChannels(DataStoreHost* host) {
  292. File file = openRead(_getContactsChannelsFS(), "/channels2");
  293. if (file) {
  294. bool full = false;
  295. uint8_t channel_idx = 0;
  296. while (!full) {
  297. ChannelDetails ch;
  298. uint8_t unused[4];
  299. bool success = (file.read(unused, 4) == 4);
  300. success = success && (file.read((uint8_t *)ch.name, 32) == 32);
  301. success = success && (file.read((uint8_t *)ch.channel.secret, 32) == 32);
  302. if (!success) break; // EOF
  303. if (host->onChannelLoaded(channel_idx, ch)) {
  304. channel_idx++;
  305. } else {
  306. full = true;
  307. }
  308. }
  309. file.close();
  310. }
  311. }
  312. void DataStore::saveChannels(DataStoreHost* host) {
  313. File file = openWrite(_getContactsChannelsFS(), "/channels2");
  314. if (file) {
  315. uint8_t channel_idx = 0;
  316. ChannelDetails ch;
  317. uint8_t unused[4];
  318. memset(unused, 0, 4);
  319. while (host->getChannelForSave(channel_idx, ch)) {
  320. bool success = (file.write(unused, 4) == 4);
  321. success = success && (file.write((uint8_t *)ch.name, 32) == 32);
  322. success = success && (file.write((uint8_t *)ch.channel.secret, 32) == 32);
  323. if (!success) break; // write failed
  324. channel_idx++;
  325. }
  326. file.close();
  327. }
  328. }
  329. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  330. #define MAX_ADVERT_PKT_LEN (2 + 32 + PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE)
  331. struct BlobRec {
  332. uint32_t timestamp;
  333. uint8_t key[7];
  334. uint8_t len;
  335. uint8_t data[MAX_ADVERT_PKT_LEN];
  336. };
  337. void DataStore::checkAdvBlobFile() {
  338. if (!_getContactsChannelsFS()->exists("/adv_blobs")) {
  339. File file = openWrite(_getContactsChannelsFS(), "/adv_blobs");
  340. if (file) {
  341. BlobRec zeroes;
  342. memset(&zeroes, 0, sizeof(zeroes));
  343. for (int i = 0; i < MAX_BLOBRECS; i++) { // pre-allocate to fixed size
  344. file.write((uint8_t *) &zeroes, sizeof(zeroes));
  345. }
  346. file.close();
  347. }
  348. }
  349. }
  350. void DataStore::migrateToSecondaryFS() {
  351. // migrate old adv_blobs, contacts3 and channels2 files to secondary FS if they don't already exist
  352. if (!_fsExtra->exists("/adv_blobs")) {
  353. if (_fs->exists("/adv_blobs")) {
  354. File oldAdvBlobs = openRead(_fs, "/adv_blobs");
  355. File newAdvBlobs = openWrite(_fsExtra, "/adv_blobs");
  356. if (oldAdvBlobs && newAdvBlobs) {
  357. BlobRec rec;
  358. size_t count = 0;
  359. // Copy 20 BlobRecs from old to new
  360. while (count < 20 && oldAdvBlobs.read((uint8_t *)&rec, sizeof(rec)) == sizeof(rec)) {
  361. newAdvBlobs.seek(count * sizeof(BlobRec));
  362. newAdvBlobs.write((uint8_t *)&rec, sizeof(rec));
  363. count++;
  364. }
  365. }
  366. if (oldAdvBlobs) oldAdvBlobs.close();
  367. if (newAdvBlobs) newAdvBlobs.close();
  368. _fs->remove("/adv_blobs");
  369. }
  370. }
  371. if (!_fsExtra->exists("/contacts3")) {
  372. if (_fs->exists("/contacts3")) {
  373. File oldFile = openRead(_fs, "/contacts3");
  374. File newFile = openWrite(_fsExtra, "/contacts3");
  375. if (oldFile && newFile) {
  376. uint8_t buf[64];
  377. int n;
  378. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  379. newFile.write(buf, n);
  380. }
  381. }
  382. if (oldFile) oldFile.close();
  383. if (newFile) newFile.close();
  384. _fs->remove("/contacts3");
  385. }
  386. }
  387. if (!_fsExtra->exists("/channels2")) {
  388. if (_fs->exists("/channels2")) {
  389. File oldFile = openRead(_fs, "/channels2");
  390. File newFile = openWrite(_fsExtra, "/channels2");
  391. if (oldFile && newFile) {
  392. uint8_t buf[64];
  393. int n;
  394. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  395. newFile.write(buf, n);
  396. }
  397. }
  398. if (oldFile) oldFile.close();
  399. if (newFile) newFile.close();
  400. _fs->remove("/channels2");
  401. }
  402. }
  403. // cleanup nodes which have been testing the extra fs, copy _main.id and new_prefs back to primary
  404. if (_fsExtra->exists("/_main.id")) {
  405. if (_fs->exists("/_main.id")) {_fs->remove("/_main.id");}
  406. File oldFile = openRead(_fsExtra, "/_main.id");
  407. File newFile = openWrite(_fs, "/_main.id");
  408. if (oldFile && newFile) {
  409. uint8_t buf[64];
  410. int n;
  411. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  412. newFile.write(buf, n);
  413. }
  414. }
  415. if (oldFile) oldFile.close();
  416. if (newFile) newFile.close();
  417. _fsExtra->remove("/_main.id");
  418. }
  419. if (_fsExtra->exists("/new_prefs")) {
  420. if (_fs->exists("/new_prefs")) {_fs->remove("/new_prefs");}
  421. File oldFile = openRead(_fsExtra, "/new_prefs");
  422. File newFile = openWrite(_fs, "/new_prefs");
  423. if (oldFile && newFile) {
  424. uint8_t buf[64];
  425. int n;
  426. while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
  427. newFile.write(buf, n);
  428. }
  429. }
  430. if (oldFile) oldFile.close();
  431. if (newFile) newFile.close();
  432. _fsExtra->remove("/new_prefs");
  433. }
  434. // remove files from where they should not be anymore
  435. if (_fs->exists("/adv_blobs")) {
  436. _fs->remove("/adv_blobs");
  437. }
  438. if (_fs->exists("/contacts3")) {
  439. _fs->remove("/contacts3");
  440. }
  441. if (_fs->exists("/channels2")) {
  442. _fs->remove("/channels2");
  443. }
  444. if (_fsExtra->exists("/_main.id")) {
  445. _fsExtra->remove("/_main.id");
  446. }
  447. if (_fsExtra->exists("/new_prefs")) {
  448. _fsExtra->remove("/new_prefs");
  449. }
  450. }
  451. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  452. File file = openRead(_getContactsChannelsFS(), "/adv_blobs");
  453. uint8_t len = 0; // 0 = not found
  454. if (file) {
  455. BlobRec tmp;
  456. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  457. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  458. len = tmp.len;
  459. memcpy(dest_buf, tmp.data, len);
  460. break;
  461. }
  462. }
  463. file.close();
  464. }
  465. return len;
  466. }
  467. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  468. if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
  469. checkAdvBlobFile();
  470. File file = _getContactsChannelsFS()->open("/adv_blobs", FILE_O_WRITE);
  471. if (file) {
  472. uint32_t pos = 0, found_pos = 0;
  473. uint32_t min_timestamp = 0xFFFFFFFF;
  474. // search for matching key OR evict by oldest timestmap
  475. BlobRec tmp;
  476. file.seek(0);
  477. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  478. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  479. found_pos = pos;
  480. break;
  481. }
  482. if (tmp.timestamp < min_timestamp) {
  483. min_timestamp = tmp.timestamp;
  484. found_pos = pos;
  485. }
  486. pos += sizeof(tmp);
  487. }
  488. memcpy(tmp.key, key, sizeof(tmp.key)); // just record 7 byte prefix of key
  489. memcpy(tmp.data, src_buf, len);
  490. tmp.len = len;
  491. tmp.timestamp = _clock->getCurrentTime();
  492. file.seek(found_pos);
  493. file.write((uint8_t *) &tmp, sizeof(tmp));
  494. file.close();
  495. return true;
  496. }
  497. return false; // error
  498. }
  499. #else
  500. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  501. char path[64];
  502. char fname[18];
  503. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  504. mesh::Utils::toHex(fname, key, key_len);
  505. sprintf(path, "/bl/%s", fname);
  506. if (_fs->exists(path)) {
  507. File f = openRead(_fs, path);
  508. if (f) {
  509. int len = f.read(dest_buf, 255); // currently MAX 255 byte blob len supported!!
  510. f.close();
  511. return len;
  512. }
  513. }
  514. return 0; // not found
  515. }
  516. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  517. char path[64];
  518. char fname[18];
  519. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  520. mesh::Utils::toHex(fname, key, key_len);
  521. sprintf(path, "/bl/%s", fname);
  522. File f = openWrite(_fs, path);
  523. if (f) {
  524. int n = f.write(src_buf, len);
  525. f.close();
  526. if (n == len) return true; // success!
  527. _fs->remove(path); // blob was only partially written!
  528. }
  529. return false; // error
  530. }
  531. #endif