DataStore.cpp 20 KB

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