|
| 1 | +/* |
| 2 | + This file is part of the EthernetEspAT library for Arduino |
| 3 | + https://github.com/Networking-for-Arduino/EthernetEspAT |
| 4 | + Copyright 2024 Juraj Andrassy |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "Ethernet.h" |
| 22 | +#include <utility/EspAtDrv.h> |
| 23 | + |
| 24 | +// these statics are removed by compiler, if not used |
| 25 | +char EthernetClass::fwVersion[15] = {0}; |
| 26 | +char EthernetClass::name[33] = {0}; // hostname |
| 27 | + |
| 28 | +EthernetClass::EthernetClass() { |
| 29 | +} |
| 30 | + |
| 31 | +bool EthernetClass::init(Stream& serial, int8_t resetPin) { |
| 32 | + bool ok = EspAtDrv.init(&serial, resetPin); |
| 33 | + if (ok) { |
| 34 | + hwStatus = EthernetHardwareFound; |
| 35 | + } |
| 36 | + return ok; |
| 37 | +} |
| 38 | + |
| 39 | +bool EthernetClass::setPersistent(bool persistent) { |
| 40 | + return EspAtDrv.sysPersistent(persistent); |
| 41 | +} |
| 42 | + |
| 43 | +int EthernetClass::begin(uint8_t *mac, unsigned long timeout) { |
| 44 | + if (mac != nullptr && !EspAtDrv.ethSetMac(mac)) |
| 45 | + return 0; |
| 46 | + if (!EspAtDrv.ethEnableDHCP()) |
| 47 | + return false; |
| 48 | + if (timeout) { |
| 49 | + const unsigned long start = millis(); |
| 50 | + while ((millis() - start) < timeout) { |
| 51 | + if (localIP() != INADDR_NONE) |
| 52 | + return true; |
| 53 | + delay(200); |
| 54 | + } |
| 55 | + } |
| 56 | + return (localIP() != INADDR_NONE); |
| 57 | +} |
| 58 | + |
| 59 | +bool EthernetClass::begin(uint8_t *mac, IPAddress localIP, IPAddress dnsIP, IPAddress gatewayIP, IPAddress netmask) { |
| 60 | + (void) mac; |
| 61 | + if (dnsIP == INADDR_NONE) { |
| 62 | + dnsIP = localIP; |
| 63 | + dnsIP[3] = 1; |
| 64 | + } |
| 65 | + return EspAtDrv.ethStaticIp(localIP, gatewayIP, netmask) && setDNS(dnsIP); |
| 66 | +} |
| 67 | + |
| 68 | +int EthernetClass::begin(unsigned long timeout) { |
| 69 | + return begin(nullptr, timeout); |
| 70 | +} |
| 71 | + |
| 72 | +bool EthernetClass::begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) { |
| 73 | + return begin(nullptr, ip, dns, gateway, subnet); |
| 74 | +} |
| 75 | + |
| 76 | +int EthernetClass::maintain() { |
| 77 | + EspAtDrv.maintain(); |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +void EthernetClass::end() { |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +EthernetLinkStatus EthernetClass::linkStatus() { |
| 86 | + return EspAtDrv.ethStatus() ? LinkON : LinkOFF; |
| 87 | +} |
| 88 | + |
| 89 | +EthernetHardwareStatus EthernetClass::hardwareStatus() { |
| 90 | + return hwStatus; |
| 91 | +} |
| 92 | + |
| 93 | +void EthernetClass::MACAddress(uint8_t *mac) { |
| 94 | + macAddress(mac); |
| 95 | +} |
| 96 | + |
| 97 | +IPAddress EthernetClass::dnsServerIP() { |
| 98 | + return dnsIP(); |
| 99 | +} |
| 100 | + |
| 101 | +void EthernetClass::setDnsServerIP(const IPAddress dns) { |
| 102 | + setDNS(dns); |
| 103 | +} |
| 104 | + |
| 105 | +bool EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2) { |
| 106 | + return EspAtDrv.setDNS(dns_server1, dns_server2); |
| 107 | +} |
| 108 | + |
| 109 | +bool EthernetClass::setHostname(const char* name) { |
| 110 | + return EspAtDrv.setEthHostname(name); |
| 111 | +} |
| 112 | + |
| 113 | +const char* EthernetClass::hostname(char* buffer) { |
| 114 | + if (!EspAtDrv.ethHostnameQuery(buffer)) { |
| 115 | + buffer[0] = 0; |
| 116 | + } |
| 117 | + return buffer; |
| 118 | +} |
| 119 | + |
| 120 | +uint8_t* EthernetClass::macAddress(uint8_t* mac) { |
| 121 | + if (!EspAtDrv.ethMacQuery(mac)) |
| 122 | + return nullptr; |
| 123 | + return mac; |
| 124 | +} |
| 125 | + |
| 126 | +IPAddress EthernetClass::localIP() { |
| 127 | + IPAddress ip; |
| 128 | + IPAddress gw; |
| 129 | + IPAddress mask; |
| 130 | + EspAtDrv.ethIpQuery(ip, gw, mask); |
| 131 | + return ip; |
| 132 | +} |
| 133 | + |
| 134 | +IPAddress EthernetClass::gatewayIP() { |
| 135 | + IPAddress ip; |
| 136 | + IPAddress gw; |
| 137 | + IPAddress mask; |
| 138 | + EspAtDrv.ethIpQuery(ip, gw, mask); |
| 139 | + return gw; |
| 140 | +} |
| 141 | + |
| 142 | +IPAddress EthernetClass::subnetMask() { |
| 143 | + IPAddress ip; |
| 144 | + IPAddress gw; |
| 145 | + IPAddress mask; |
| 146 | + EspAtDrv.ethIpQuery(ip, gw, mask); |
| 147 | + return mask; |
| 148 | +} |
| 149 | + |
| 150 | +IPAddress EthernetClass::dnsIP(int n) { |
| 151 | + IPAddress dns1; |
| 152 | + IPAddress dns2; |
| 153 | + EspAtDrv.dnsQuery(dns1, dns2); |
| 154 | + switch (n) { |
| 155 | + case 0: |
| 156 | + return dns1; |
| 157 | + case 1: |
| 158 | + return dns2; |
| 159 | + } |
| 160 | + return IPAddress(0, 0, 0, 0); |
| 161 | +} |
| 162 | + |
| 163 | +bool EthernetClass::dhcpIsEnabled() { |
| 164 | + bool sta; |
| 165 | + bool ap; |
| 166 | + bool eth; |
| 167 | + if (!EspAtDrv.dhcpStateQuery(sta, ap, eth)) |
| 168 | + return false; |
| 169 | + return eth; |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +bool EthernetClass::startMDNS(const char* hostname, const char* serverName, uint16_t serverPort) { |
| 174 | + return EspAtDrv.mDNS(hostname, serverName, serverPort); |
| 175 | +} |
| 176 | + |
| 177 | +bool EthernetClass::hostByName(const char* hostname, IPAddress& result) { |
| 178 | + return EspAtDrv.resolve(hostname, result); |
| 179 | +} |
| 180 | + |
| 181 | +bool EthernetClass::ping(const char* hostname) { |
| 182 | + return EspAtDrv.ping(hostname); |
| 183 | +} |
| 184 | + |
| 185 | +bool EthernetClass::ping(IPAddress ip) { |
| 186 | + char s[16]; |
| 187 | + EspAtDrv.ip2str(ip, s); |
| 188 | + return ping(s); |
| 189 | +} |
| 190 | + |
| 191 | +bool EthernetClass::sntp(const char* server1, const char* server2) { |
| 192 | + return EspAtDrv.sntpCfg(server1, server2); |
| 193 | +} |
| 194 | + |
| 195 | +unsigned long EthernetClass::getTime() { |
| 196 | + return EspAtDrv.sntpTime(); |
| 197 | +} |
| 198 | + |
| 199 | +const char* EthernetClass::firmwareVersion(char* buffer) { |
| 200 | + EspAtDrv.firmwareVersion(buffer); |
| 201 | + return buffer; |
| 202 | +} |
| 203 | + |
| 204 | +EspAtDrvError EthernetClass::getLastDriverError() { |
| 205 | + return EspAtDrv.getLastErrorCode(); |
| 206 | +} |
| 207 | + |
| 208 | +bool EthernetClass::sleepMode(EspAtSleepMode mode) { |
| 209 | + return EspAtDrv.sleepMode(mode); |
| 210 | +} |
| 211 | + |
| 212 | +bool EthernetClass::deepSleep() { |
| 213 | + return EspAtDrv.deepSleep(); |
| 214 | +} |
| 215 | + |
| 216 | +bool EthernetClass::reset(uint8_t resetPin) { |
| 217 | + return EspAtDrv.reset(resetPin); |
| 218 | +} |
| 219 | + |
| 220 | +bool EthernetClass::wifiOff(bool save) { |
| 221 | + return EspAtDrv.wifiOff(save); |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | +EthernetClass Ethernet; |
0 commit comments