bonds.cpp 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. unsigned long g_connReplyMs[NSLOT] = { 0 };
  7. volatile bool g_dirty = false;
  8. bool g_pairing = false;
  9. #define BOND_FILE "/bonds.bin"
  10. bool recEmpty(const uint8_t *r)
  11. {
  12. for (int i = 0; i < 24; i++)
  13. if (r[i])
  14. return false;
  15. return true;
  16. }
  17. void saveBonds()
  18. {
  19. InternalFS.remove(BOND_FILE);
  20. File f(InternalFS);
  21. if (f.open(BOND_FILE, FILE_O_WRITE)) {
  22. for (int i = 0; i < NSLOT; i++) {
  23. uint8_t u = g_slot[i].used ? 1 : 0;
  24. f.write(&u, 1);
  25. f.write(g_slot[i].rec, 24);
  26. }
  27. f.close();
  28. }
  29. }
  30. void loadBonds()
  31. {
  32. File f(InternalFS);
  33. if (f.open(BOND_FILE, FILE_O_READ))
  34. for (int i = 0; i < NSLOT; i++) {
  35. uint8_t u = 0;
  36. if (f.read(&u, 1) == 1) {
  37. g_slot[i].used = u;
  38. f.read(g_slot[i].rec, 24);
  39. }
  40. }
  41. f.close();
  42. }
  43. int bondedSlotCount()
  44. {
  45. int n = 0;
  46. for (int i = 0; i < NSLOT; i++)
  47. if (g_slot[i].used)
  48. n++;
  49. return n;
  50. }