BellUtils.cpp 996 B

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