bonds.cpp 892 B

123456789101112131415161718192021222324252627282930
  1. #include "bonds.h"
  2. #include <Adafruit_LittleFS.h>
  3. #include <InternalFileSystem.h>
  4. using namespace Adafruit_LittleFS_Namespace;
  5. Slot g_slot[NSLOT];
  6. int g_connSlot = -1;
  7. unsigned long g_connReplyMs = 0;
  8. volatile bool g_dirty = false;
  9. bool g_pairing = false;
  10. #define BOND_FILE "/bonds.bin"
  11. bool recEmpty(const uint8_t *r) { for (int i = 0; i < 24; i++) if (r[i]) return false; return true; }
  12. void saveBonds() {
  13. InternalFS.remove(BOND_FILE);
  14. File f(InternalFS);
  15. if (f.open(BOND_FILE, FILE_O_WRITE)) {
  16. for (int i = 0; i < NSLOT; i++) { uint8_t u = g_slot[i].used ? 1 : 0; f.write(&u, 1); f.write(g_slot[i].rec, 24); }
  17. f.close();
  18. }
  19. }
  20. void loadBonds() {
  21. File f(InternalFS);
  22. if (f.open(BOND_FILE, FILE_O_READ))
  23. for (int i = 0; i < NSLOT; i++) { uint8_t u = 0; if (f.read(&u, 1) == 1) { g_slot[i].used = u; f.read(g_slot[i].rec, 24); } }
  24. f.close();
  25. }