|
2 | 2 | // TODO
|
3 | 3 | // mock AddrList with POSIX mock API
|
4 | 4 | // later: real AddrList will work with lwIP API
|
| 5 | + |
| 6 | +// mock is IPv4 only |
| 7 | + |
| 8 | +#ifndef __ADDRLISTX_H |
| 9 | +#define __ADDRLISTX_H |
| 10 | + |
| 11 | +#include <ESP8266WiFi.h> |
| 12 | + |
| 13 | +namespace esp8266 |
| 14 | +{ |
| 15 | + |
| 16 | +namespace AddressListImplementation |
| 17 | +{ |
| 18 | + |
| 19 | + |
| 20 | +struct netifWrapper |
| 21 | +{ |
| 22 | + netifWrapper (bool netif) : _netif(netif) {} |
| 23 | + netifWrapper (const netifWrapper& o) : _netif(o._netif) {} |
| 24 | + |
| 25 | + netifWrapper& operator= (const netifWrapper& o) |
| 26 | + { |
| 27 | + _netif = o._netif; |
| 28 | + return *this; |
| 29 | + } |
| 30 | + |
| 31 | + bool equal(const netifWrapper& o) |
| 32 | + { |
| 33 | + return _netif == o._netif; |
| 34 | + } |
| 35 | + |
| 36 | + // address properties |
| 37 | + IPAddress addr () const { return WiFi.localIP(); } |
| 38 | + bool isLegacy () const { return true; } |
| 39 | + bool isLocal () const { return false; } |
| 40 | + bool isV4 () const { return addr().isV4(); } |
| 41 | + bool isV6 () const { return !addr().isV4(); } |
| 42 | + String toString() const { return addr().toString(); } |
| 43 | + |
| 44 | + // related to legacy address (_num=0, ipv4) |
| 45 | + IPAddress ipv4 () const { ip_info info; wifi_get_ip_info(0, &info); return info.ip; } |
| 46 | + IPAddress netmask () const { ip_info info; wifi_get_ip_info(0, &info); return info.netmask; } |
| 47 | + IPAddress gw () const { ip_info info; wifi_get_ip_info(0, &info); return info.gw; } |
| 48 | + |
| 49 | + // common to all addresses of this interface |
| 50 | + String ifname () const { return "st"; } |
| 51 | + const char* ifhostname () const { return wifi_station_get_hostname(); } |
| 52 | + String ifmac () const { uint8_t mac[20]; WiFi.macAddress(mac); return String((char*)mac); } |
| 53 | + int ifnumber () const { return 0; } |
| 54 | + bool ifUp () const { return true; } |
| 55 | + |
| 56 | + bool _netif; |
| 57 | +}; |
| 58 | + |
| 59 | + |
| 60 | +class AddressListIterator |
| 61 | +{ |
| 62 | +public: |
| 63 | + AddressListIterator (const netifWrapper& o) : netIf(o) {} |
| 64 | + AddressListIterator (bool netif) : netIf(netif) |
| 65 | + { |
| 66 | + // This constructor is called with lwIP's global netif_list, or |
| 67 | + // nullptr. operator++() is designed to loop through _configured_ |
| 68 | + // addresses. That's why netIf's _num is initialized to -1 to allow |
| 69 | + // returning the first usable address to AddressList::begin(). |
| 70 | + (void)operator++(); |
| 71 | + } |
| 72 | + |
| 73 | + const netifWrapper& operator* () const { return netIf; } |
| 74 | + const netifWrapper* operator-> () const { return &netIf; } |
| 75 | + |
| 76 | + bool operator== (AddressListIterator& o) { return netIf.equal(*o); } |
| 77 | + bool operator!= (AddressListIterator& o) { return !netIf.equal(*o); } |
| 78 | + |
| 79 | + AddressListIterator& operator= (const AddressListIterator& o) { netIf = o.netIf; return *this; } |
| 80 | + |
| 81 | + AddressListIterator operator++ (int) |
| 82 | + { |
| 83 | + AddressListIterator ret = *this; |
| 84 | + (void)operator++(); |
| 85 | + return ret; |
| 86 | + } |
| 87 | + |
| 88 | + AddressListIterator& operator++ () |
| 89 | + { |
| 90 | + netIf._netif = !netIf._netif; |
| 91 | + return *this; |
| 92 | + } |
| 93 | + |
| 94 | + netifWrapper netIf; |
| 95 | +}; |
| 96 | + |
| 97 | + |
| 98 | +class AddressList |
| 99 | +{ |
| 100 | +public: |
| 101 | + using const_iterator = const AddressListIterator; |
| 102 | + |
| 103 | + const_iterator begin () const { return const_iterator(true); } |
| 104 | + const_iterator end () const { return const_iterator(false); } |
| 105 | + |
| 106 | +}; |
| 107 | + |
| 108 | +inline AddressList::const_iterator begin (const AddressList& a) { return a.begin(); } |
| 109 | +inline AddressList::const_iterator end (const AddressList& a) { return a.end(); } |
| 110 | + |
| 111 | + |
| 112 | +} // AddressListImplementation |
| 113 | + |
| 114 | +} // esp8266 |
| 115 | + |
| 116 | +extern esp8266::AddressListImplementation::AddressList addrList; |
| 117 | + |
| 118 | +#endif |
0 commit comments