net.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "kbf/net.h"
  2. #include <sstream>
  3. #include <iomanip>
  4. #include <arpa/inet.h>
  5. #include <esp_system.h>
  6. #include <esp_log.h>
  7. #include "kbf/macros.h"
  8. using std::string;
  9. using namespace kbf;
  10. string net::MAC::str() const {
  11. using std::setw;
  12. std::stringstream ss;
  13. ss << std::hex << std::setfill('0') << setw(2) << std::right << std::uppercase
  14. << (uint32_t) this->addr[0] << ":" << setw(2)
  15. << (uint32_t) this->addr[1] << ":" << setw(2)
  16. << (uint32_t) this->addr[2] << ":" << setw(2)
  17. << (uint32_t) this->addr[3] << ":" << setw(2)
  18. << (uint32_t) this->addr[4] << ":" << setw(2)
  19. << (uint32_t) this->addr[5];
  20. return ss.str();
  21. }
  22. net::MAC::MAC(const uint8_t addr[6]) {
  23. // TODO look up some (syntax) magic for array initialization
  24. for (int i = 0; i < 6; i++) {
  25. this->addr[i] = addr[i];
  26. }
  27. }
  28. void net::setMac(const MAC &mac) {
  29. ESP_LOGI(TAG, "setting base MAC address to %s", mac.str().c_str());
  30. CHECK(esp_base_mac_addr_set(mac.addr));
  31. }
  32. net::MAC net::getMac() {
  33. uint8_t addr[6]{};
  34. CHECK(esp_base_mac_addr_get(addr));
  35. auto mac = MAC(addr);
  36. ESP_LOGI(TAG, "read base MAC address: %s", mac.str().c_str());
  37. return mac;
  38. }
  39. string net::IP::str() const {
  40. return string(inet_ntoa(addr));
  41. }