2
0

BellUtils.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "BellUtils.h"
  2. #include <stdlib.h> // for free
  3. #include <random> // for mt19937, uniform_int_distribution, random_device
  4. #ifdef ESP_PLATFORM
  5. #include "esp_system.h"
  6. #if __has_include("esp_mac.h")
  7. #include "esp_mac.h"
  8. #endif
  9. #endif
  10. std::string bell::generateRandomUUID() {
  11. static std::random_device dev;
  12. static std::mt19937 rng(dev());
  13. std::uniform_int_distribution<int> dist(0, 15);
  14. const char* v = "0123456789abcdef";
  15. const bool dash[] = {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0};
  16. std::string res;
  17. for (int i = 0; i < 16; i++) {
  18. if (dash[i])
  19. res += "-";
  20. res += v[dist(rng)];
  21. res += v[dist(rng)];
  22. }
  23. return res;
  24. }
  25. std::string bell::getMacAddress() {
  26. #ifdef ESP_PLATFORM
  27. uint8_t mac[6];
  28. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  29. char macStr[18];
  30. sprintf(macStr, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2],
  31. mac[3], mac[4], mac[5]);
  32. return std::string(macStr);
  33. #endif
  34. return "00:00:00:00:00:00";
  35. }
  36. void bell::freeAndNull(void*& ptr) {
  37. free(ptr);
  38. ptr = nullptr;
  39. }