DataStore.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include <Arduino.h>
  2. #include "DataStore.h"
  3. DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
  4. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  5. identity_store(fs, "")
  6. #elif defined(RP2040_PLATFORM)
  7. identity_store(fs, "/identity")
  8. #else
  9. identity_store(fs, "/identity")
  10. #endif
  11. {
  12. }
  13. static File openWrite(FILESYSTEM* _fs, const char* filename) {
  14. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  15. _fs->remove(filename);
  16. return _fs->open(filename, FILE_O_WRITE);
  17. #elif defined(RP2040_PLATFORM)
  18. return _fs->open(filename, "w");
  19. #else
  20. return _fs->open(filename, "w", true);
  21. #endif
  22. }
  23. void DataStore::begin() {
  24. #if defined(RP2040_PLATFORM)
  25. identity_store.begin();
  26. #endif
  27. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  28. checkAdvBlobFile();
  29. #else
  30. // init 'blob store' support
  31. _fs->mkdir("/bl");
  32. #endif
  33. }
  34. #if defined(ESP32)
  35. #include <SPIFFS.h>
  36. #elif defined(RP2040_PLATFORM)
  37. #include <LittleFS.h>
  38. #endif
  39. File DataStore::openRead(const char* filename) {
  40. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  41. return _fs->open(filename, FILE_O_READ);
  42. #elif defined(RP2040_PLATFORM)
  43. return _fs->open(filename, "r");
  44. #else
  45. return _fs->open(filename, "r", false);
  46. #endif
  47. }
  48. bool DataStore::removeFile(const char* filename) {
  49. return _fs->remove(filename);
  50. }
  51. bool DataStore::formatFileSystem() {
  52. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  53. return _fs->format();
  54. #elif defined(RP2040_PLATFORM)
  55. return LittleFS.format();
  56. #elif defined(ESP32)
  57. return ((fs::SPIFFSFS *)_fs)->format();
  58. #else
  59. #error "need to implement format()"
  60. #endif
  61. }
  62. bool DataStore::loadMainIdentity(mesh::LocalIdentity &identity) {
  63. return identity_store.load("_main", identity);
  64. }
  65. bool DataStore::saveMainIdentity(const mesh::LocalIdentity &identity) {
  66. return identity_store.save("_main", identity);
  67. }
  68. void DataStore::loadPrefs(NodePrefs& prefs, double& node_lat, double& node_lon) {
  69. if (_fs->exists("/new_prefs")) {
  70. loadPrefsInt("/new_prefs", prefs, node_lat, node_lon); // new filename
  71. } else if (_fs->exists("/node_prefs")) {
  72. loadPrefsInt("/node_prefs", prefs, node_lat, node_lon);
  73. savePrefs(prefs, node_lat, node_lon); // save to new filename
  74. _fs->remove("/node_prefs"); // remove old
  75. }
  76. }
  77. void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& node_lat, double& node_lon) {
  78. #if defined(RP2040_PLATFORM)
  79. File file = _fs->open(filename, "r");
  80. #else
  81. File file = _fs->open(filename);
  82. #endif
  83. if (file) {
  84. uint8_t pad[8];
  85. file.read((uint8_t *)&_prefs.airtime_factor, sizeof(float)); // 0
  86. file.read((uint8_t *)_prefs.node_name, sizeof(_prefs.node_name)); // 4
  87. file.read(pad, 4); // 36
  88. file.read((uint8_t *)&node_lat, sizeof(node_lat)); // 40
  89. file.read((uint8_t *)&node_lon, sizeof(node_lon)); // 48
  90. file.read((uint8_t *)&_prefs.freq, sizeof(_prefs.freq)); // 56
  91. file.read((uint8_t *)&_prefs.sf, sizeof(_prefs.sf)); // 60
  92. file.read((uint8_t *)&_prefs.cr, sizeof(_prefs.cr)); // 61
  93. file.read((uint8_t *)&_prefs.reserved1, sizeof(_prefs.reserved1)); // 62
  94. file.read((uint8_t *)&_prefs.manual_add_contacts, sizeof(_prefs.manual_add_contacts)); // 63
  95. file.read((uint8_t *)&_prefs.bw, sizeof(_prefs.bw)); // 64
  96. file.read((uint8_t *)&_prefs.tx_power_dbm, sizeof(_prefs.tx_power_dbm)); // 68
  97. file.read((uint8_t *)&_prefs.telemetry_mode_base, sizeof(_prefs.telemetry_mode_base)); // 69
  98. file.read((uint8_t *)&_prefs.telemetry_mode_loc, sizeof(_prefs.telemetry_mode_loc)); // 70
  99. file.read((uint8_t *)&_prefs.telemetry_mode_env, sizeof(_prefs.telemetry_mode_env)); // 71
  100. file.read((uint8_t *)&_prefs.rx_delay_base, sizeof(_prefs.rx_delay_base)); // 72
  101. file.read(pad, 4); // 76
  102. file.read((uint8_t *)&_prefs.ble_pin, sizeof(_prefs.ble_pin)); // 80
  103. file.close();
  104. }
  105. }
  106. void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_lon) {
  107. File file = openWrite(_fs, "/new_prefs");
  108. if (file) {
  109. uint8_t pad[8];
  110. memset(pad, 0, sizeof(pad));
  111. file.write((uint8_t *)&_prefs.airtime_factor, sizeof(float)); // 0
  112. file.write((uint8_t *)_prefs.node_name, sizeof(_prefs.node_name)); // 4
  113. file.write(pad, 4); // 36
  114. file.write((uint8_t *)&node_lat, sizeof(node_lat)); // 40
  115. file.write((uint8_t *)&node_lon, sizeof(node_lon)); // 48
  116. file.write((uint8_t *)&_prefs.freq, sizeof(_prefs.freq)); // 56
  117. file.write((uint8_t *)&_prefs.sf, sizeof(_prefs.sf)); // 60
  118. file.write((uint8_t *)&_prefs.cr, sizeof(_prefs.cr)); // 61
  119. file.write((uint8_t *)&_prefs.reserved1, sizeof(_prefs.reserved1)); // 62
  120. file.write((uint8_t *)&_prefs.manual_add_contacts, sizeof(_prefs.manual_add_contacts)); // 63
  121. file.write((uint8_t *)&_prefs.bw, sizeof(_prefs.bw)); // 64
  122. file.write((uint8_t *)&_prefs.tx_power_dbm, sizeof(_prefs.tx_power_dbm)); // 68
  123. file.write((uint8_t *)&_prefs.telemetry_mode_base, sizeof(_prefs.telemetry_mode_base)); // 69
  124. file.write((uint8_t *)&_prefs.telemetry_mode_loc, sizeof(_prefs.telemetry_mode_loc)); // 70
  125. file.write((uint8_t *)&_prefs.telemetry_mode_env, sizeof(_prefs.telemetry_mode_env)); // 71
  126. file.write((uint8_t *)&_prefs.rx_delay_base, sizeof(_prefs.rx_delay_base)); // 72
  127. file.write(pad, 4); // 76
  128. file.write((uint8_t *)&_prefs.ble_pin, sizeof(_prefs.ble_pin)); // 80
  129. file.close();
  130. }
  131. }
  132. void DataStore::loadContacts(DataStoreHost* host) {
  133. if (_fs->exists("/contacts3")) {
  134. #if defined(RP2040_PLATFORM)
  135. File file = _fs->open("/contacts3", "r");
  136. #else
  137. File file = _fs->open("/contacts3");
  138. #endif
  139. if (file) {
  140. bool full = false;
  141. while (!full) {
  142. ContactInfo c;
  143. uint8_t pub_key[32];
  144. uint8_t unused;
  145. bool success = (file.read(pub_key, 32) == 32);
  146. success = success && (file.read((uint8_t *)&c.name, 32) == 32);
  147. success = success && (file.read(&c.type, 1) == 1);
  148. success = success && (file.read(&c.flags, 1) == 1);
  149. success = success && (file.read(&unused, 1) == 1);
  150. success = success && (file.read((uint8_t *)&c.sync_since, 4) == 4); // was 'reserved'
  151. success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1);
  152. success = success && (file.read((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  153. success = success && (file.read(c.out_path, 64) == 64);
  154. success = success && (file.read((uint8_t *)&c.lastmod, 4) == 4);
  155. success = success && (file.read((uint8_t *)&c.gps_lat, 4) == 4);
  156. success = success && (file.read((uint8_t *)&c.gps_lon, 4) == 4);
  157. if (!success) break; // EOF
  158. c.id = mesh::Identity(pub_key);
  159. if (!host->onContactLoaded(c)) full = true;
  160. }
  161. file.close();
  162. }
  163. }
  164. }
  165. void DataStore::saveContacts(DataStoreHost* host) {
  166. File file = openWrite(_fs, "/contacts3");
  167. if (file) {
  168. uint32_t idx = 0;
  169. ContactInfo c;
  170. uint8_t unused = 0;
  171. while (host->getContactForSave(idx, c)) {
  172. bool success = (file.write(c.id.pub_key, 32) == 32);
  173. success = success && (file.write((uint8_t *)&c.name, 32) == 32);
  174. success = success && (file.write(&c.type, 1) == 1);
  175. success = success && (file.write(&c.flags, 1) == 1);
  176. success = success && (file.write(&unused, 1) == 1);
  177. success = success && (file.write((uint8_t *)&c.sync_since, 4) == 4);
  178. success = success && (file.write((uint8_t *)&c.out_path_len, 1) == 1);
  179. success = success && (file.write((uint8_t *)&c.last_advert_timestamp, 4) == 4);
  180. success = success && (file.write(c.out_path, 64) == 64);
  181. success = success && (file.write((uint8_t *)&c.lastmod, 4) == 4);
  182. success = success && (file.write((uint8_t *)&c.gps_lat, 4) == 4);
  183. success = success && (file.write((uint8_t *)&c.gps_lon, 4) == 4);
  184. if (!success) break; // write failed
  185. idx++; // advance to next contact
  186. }
  187. file.close();
  188. }
  189. }
  190. void DataStore::loadChannels(DataStoreHost* host) {
  191. if (_fs->exists("/channels2")) {
  192. #if defined(RP2040_PLATFORM)
  193. File file = _fs->open("/channels2", "r");
  194. #else
  195. File file = _fs->open("/channels2");
  196. #endif
  197. if (file) {
  198. bool full = false;
  199. uint8_t channel_idx = 0;
  200. while (!full) {
  201. ChannelDetails ch;
  202. uint8_t unused[4];
  203. bool success = (file.read(unused, 4) == 4);
  204. success = success && (file.read((uint8_t *)ch.name, 32) == 32);
  205. success = success && (file.read((uint8_t *)ch.channel.secret, 32) == 32);
  206. if (!success) break; // EOF
  207. if (host->onChannelLoaded(channel_idx, ch)) {
  208. channel_idx++;
  209. } else {
  210. full = true;
  211. }
  212. }
  213. file.close();
  214. }
  215. }
  216. }
  217. void DataStore::saveChannels(DataStoreHost* host) {
  218. File file = openWrite(_fs, "/channels2");
  219. if (file) {
  220. uint8_t channel_idx = 0;
  221. ChannelDetails ch;
  222. uint8_t unused[4];
  223. memset(unused, 0, 4);
  224. while (host->getChannelForSave(channel_idx, ch)) {
  225. bool success = (file.write(unused, 4) == 4);
  226. success = success && (file.write((uint8_t *)ch.name, 32) == 32);
  227. success = success && (file.write((uint8_t *)ch.channel.secret, 32) == 32);
  228. if (!success) break; // write failed
  229. channel_idx++;
  230. }
  231. file.close();
  232. }
  233. }
  234. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  235. #define MAX_ADVERT_PKT_LEN (2 + 32 + PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE)
  236. struct BlobRec {
  237. uint32_t timestamp;
  238. uint8_t key[7];
  239. uint8_t len;
  240. uint8_t data[MAX_ADVERT_PKT_LEN];
  241. };
  242. void DataStore::checkAdvBlobFile() {
  243. if (!_fs->exists("/adv_blobs")) {
  244. File file = openWrite(_fs, "/adv_blobs");
  245. if (file) {
  246. BlobRec zeroes;
  247. memset(&zeroes, 0, sizeof(zeroes));
  248. for (int i = 0; i < 20; i++) { // pre-allocate to fixed size
  249. file.write((uint8_t *) &zeroes, sizeof(zeroes));
  250. }
  251. file.close();
  252. }
  253. }
  254. }
  255. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  256. File file = _fs->open("/adv_blobs");
  257. uint8_t len = 0; // 0 = not found
  258. if (file) {
  259. BlobRec tmp;
  260. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  261. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  262. len = tmp.len;
  263. memcpy(dest_buf, tmp.data, len);
  264. break;
  265. }
  266. }
  267. file.close();
  268. }
  269. return len;
  270. }
  271. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  272. if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
  273. checkAdvBlobFile();
  274. File file = _fs->open("/adv_blobs", FILE_O_WRITE);
  275. if (file) {
  276. uint32_t pos = 0, found_pos = 0;
  277. uint32_t min_timestamp = 0xFFFFFFFF;
  278. // search for matching key OR evict by oldest timestmap
  279. BlobRec tmp;
  280. file.seek(0);
  281. while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
  282. if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
  283. found_pos = pos;
  284. break;
  285. }
  286. if (tmp.timestamp < min_timestamp) {
  287. min_timestamp = tmp.timestamp;
  288. found_pos = pos;
  289. }
  290. pos += sizeof(tmp);
  291. }
  292. memcpy(tmp.key, key, sizeof(tmp.key)); // just record 7 byte prefix of key
  293. memcpy(tmp.data, src_buf, len);
  294. tmp.len = len;
  295. tmp.timestamp = _clock->getCurrentTime();
  296. file.seek(found_pos);
  297. file.write((uint8_t *) &tmp, sizeof(tmp));
  298. file.close();
  299. return true;
  300. }
  301. return false; // error
  302. }
  303. #else
  304. uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
  305. char path[64];
  306. char fname[18];
  307. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  308. mesh::Utils::toHex(fname, key, key_len);
  309. sprintf(path, "/bl/%s", fname);
  310. if (_fs->exists(path)) {
  311. #if defined(RP2040_PLATFORM)
  312. File f = _fs->open(path, "r");
  313. #else
  314. File f = _fs->open(path);
  315. #endif
  316. if (f) {
  317. int len = f.read(dest_buf, 255); // currently MAX 255 byte blob len supported!!
  318. f.close();
  319. return len;
  320. }
  321. }
  322. return 0; // not found
  323. }
  324. bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
  325. char path[64];
  326. char fname[18];
  327. if (key_len > 8) key_len = 8; // just use first 8 bytes (prefix)
  328. mesh::Utils::toHex(fname, key, key_len);
  329. sprintf(path, "/bl/%s", fname);
  330. File f = openWrite(_fs, path);
  331. if (f) {
  332. int n = f.write(src_buf, len);
  333. f.close();
  334. if (n == len) return true; // success!
  335. _fs->remove(path); // blob was only partially written!
  336. }
  337. return false; // error
  338. }
  339. #endif