BellUtils.cpp 834 B

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