bonds.cpp 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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)
  12. {
  13. for (int i = 0; i < 24; i++)
  14. if (r[i])
  15. return false;
  16. return true;
  17. }
  18. void saveBonds()
  19. {
  20. InternalFS.remove(BOND_FILE);
  21. File f(InternalFS);
  22. if (f.open(BOND_FILE, FILE_O_WRITE)) {
  23. for (int i = 0; i < NSLOT; i++) {
  24. uint8_t u = g_slot[i].used ? 1 : 0;
  25. f.write(&u, 1);
  26. f.write(g_slot[i].rec, 24);
  27. }
  28. f.close();
  29. }
  30. }
  31. void loadBonds()
  32. {
  33. File f(InternalFS);
  34. if (f.open(BOND_FILE, FILE_O_READ))
  35. for (int i = 0; i < NSLOT; i++) {
  36. uint8_t u = 0;
  37. if (f.read(&u, 1) == 1) {
  38. g_slot[i].used = u;
  39. f.read(g_slot[i].rec, 24);
  40. }
  41. }
  42. f.close();
  43. }