12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include "kbf/net.h"
- #include <sstream>
- #include <iomanip>
- #include <arpa/inet.h>
- #include <esp_system.h>
- #include <esp_log.h>
- #include "kbf/macros.h"
- using std::string;
- using namespace kbf;
- string net::MAC::str() const {
- using std::setw;
- std::stringstream ss;
- ss << std::hex << std::setfill('0') << setw(2) << std::right << std::uppercase
- << (uint32_t) this->addr[0] << ":" << setw(2)
- << (uint32_t) this->addr[1] << ":" << setw(2)
- << (uint32_t) this->addr[2] << ":" << setw(2)
- << (uint32_t) this->addr[3] << ":" << setw(2)
- << (uint32_t) this->addr[4] << ":" << setw(2)
- << (uint32_t) this->addr[5];
- return ss.str();
- }
- net::MAC::MAC(const uint8_t addr[6]) {
- // TODO look up some (syntax) magic for array initialization
- for (int i = 0; i < 6; i++) {
- this->addr[i] = addr[i];
- }
- }
- void net::setMac(const MAC &mac) {
- ESP_LOGI(TAG, "setting base MAC address to %s", mac.str().c_str());
- CHECK(esp_base_mac_addr_set(mac.addr));
- }
- net::MAC net::getMac() {
- uint8_t addr[6]{};
- CHECK(esp_base_mac_addr_get(addr));
- auto mac = MAC(addr);
- ESP_LOGI(TAG, "read base MAC address: %s", mac.str().c_str());
- return mac;
- }
- string net::IP::str() const {
- return string(inet_ntoa(addr));
- }
|