net.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef KBF_NET_H
  2. #define KBF_NET_H
  3. #include <string>
  4. #include <esp_netif.h>
  5. namespace kbf::net {
  6. /** @brief Tag used for logging. */
  7. static constexpr const char *TAG = "kbf::net";
  8. /**
  9. * @brief Stores a MAC address.
  10. */
  11. class MAC {
  12. public:
  13. MAC() : addr{0, 0, 0, 0, 0, 0} {};
  14. explicit MAC(const uint8_t addr[6]);
  15. /** @brief MAC address as uint8_t[6] */
  16. uint8_t addr[6]{};
  17. /**
  18. * @brief Returns the MAC address as std::string.
  19. *
  20. * @return the address in "aa:bb:cc:dd:ee:ff" format
  21. */
  22. [[nodiscard]] std::string str() const;
  23. };
  24. /**
  25. * @brief Sets the base MAC address of the device.
  26. *
  27. * @note This function must be called before the WiFi driver is initialized,
  28. * i.e. before kbf::wifi::init().
  29. *
  30. * @param MAC address to use
  31. */
  32. void setMac(const MAC &mac);
  33. /**
  34. * @brief Gets the base MAC address of the device.
  35. *
  36. * @return MAC address in use
  37. */
  38. MAC getMac();
  39. /**
  40. * @brief Stores an IPv4 address.
  41. */
  42. class IP {
  43. public:
  44. IP() : addr{0} {};
  45. /**
  46. * @brief Instantiate from esp_ip4_addr.
  47. *
  48. * @param addr esp_ip4_addr
  49. */
  50. explicit IP(esp_ip4_addr addr) : addr(addr.addr) {};
  51. /**
  52. * @brief Returns the IPv4 address as a std::string.
  53. *
  54. * @return the address in "123.123.123.123" format
  55. */
  56. [[nodiscard]] std::string str() const;
  57. private:
  58. uint32_t addr;
  59. };
  60. }
  61. #endif //KBF_NET_H