123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #pragma once
- #include "common.h"
- #include <WiFi.h>
- #include <lwip/inet.h>
- #include <esp_wifi.h>
- //
- // There are no less than 3 different types for IP addresses used
- // by different APIs. This class attempts to unify them to mask the
- // differences.
- //
- class IP4 {
- private:
- union {
- uint8_t b[4];
- uint32_t l;
- };
- public:
- constexpr IP4() : l{0} { }
- constexpr IP4(nullptr_t) : l{0} { }
- constexpr IP4(int ll) : l{(uint32_t)ll} { }
- constexpr IP4(uint32_t ll) : l{ll} { }
- constexpr IP4(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) :
- b{b0,b1,b2,b3} { }
- IP4(const IPAddress & ip) : l{ip} { }
- constexpr IP4(const ip_addr_t & ip) :
- l{ip.type == IPADDR_TYPE_V4 ? ip.u_addr.ip4.addr : 0} { }
- constexpr IP4(const esp_ip_addr_t & ip) :
- l{ip.type == IPADDR_TYPE_V4 ? ip.u_addr.ip4.addr : 0} { }
- IP4(const char *str);
- constexpr operator uint32_t () { return l; }
- operator IPAddress () { return IPAddress(l); }
- constexpr operator ip_addr_t () {
- return ip_addr_t {
- .u_addr = {.ip4 = {.addr = l}},
- .type = IPADDR_TYPE_V4
- };
- }
- constexpr operator esp_ip_addr_t () {
- return esp_ip_addr_t {
- .u_addr = {.ip4 = {.addr = l}},
- .type = IPADDR_TYPE_V4
- };
- }
- constexpr operator bool () { return l != 0; }
- constexpr bool operator ! () { return l == 0; }
- constexpr bool operator == (const IP4 &b) { return l == b.l; }
- constexpr bool operator != (const IP4 &b) { return l != b.l; }
- constexpr uint8_t operator [] (size_t n) { return b[n]; }
- uint8_t & operator [] (size_t n) { return b[n]; }
- const char *cstr();
- };
- constexpr IP4 null_ip(0);
|