|
| 1 | +#include "Ethernet.h" |
| 2 | + |
| 3 | +#define SSID_MAX_LENGTH 32 |
| 4 | + |
| 5 | +arduino::IPAddress arduino::EthernetClass::ipAddressFromSocketAddress(SocketAddress socketAddress) { |
| 6 | + nsapi_addr_t address = socketAddress.get_addr(); |
| 7 | + return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]); |
| 8 | +} |
| 9 | + |
| 10 | +SocketAddress arduino::EthernetClass::socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port) { |
| 11 | + nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}}; |
| 12 | + return SocketAddress(convertedIP, port); |
| 13 | +} |
| 14 | + |
| 15 | +int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { |
| 16 | + if (eth_if == nullptr) { |
| 17 | + //Q: What is the callback for? |
| 18 | + _initializerCallback(); |
| 19 | + if(eth_if == nullptr) return 0; |
| 20 | + } |
| 21 | + |
| 22 | + unsigned long start = millis(); |
| 23 | + eth_if->set_blocking(false); |
| 24 | + nsapi_error_t result = eth_if->connect(); |
| 25 | + |
| 26 | + while ((millis() - start < timeout) && (linkStatus() != LinkON)) { |
| 27 | + delay(10); |
| 28 | + } |
| 29 | + |
| 30 | + return (linkStatus() != LinkON ? 1 : 0); |
| 31 | +} |
| 32 | + |
| 33 | +void arduino::EthernetClass::end() { |
| 34 | + disconnect(); |
| 35 | +} |
| 36 | + |
| 37 | +EthernetLinkStatus arduino::EthernetClass::linkStatus() { |
| 38 | + return (eth_if->get_connection_status() == NSAPI_STATUS_GLOBAL_UP ? LinkON : LinkOFF); |
| 39 | +} |
| 40 | + |
| 41 | +EthernetHardwareStatus arduino::EthernetClass::hardwareStatus() { |
| 42 | + return EthernetMbed; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +int arduino::EthernetClass::disconnect() { |
| 47 | + eth_if->disconnect(); |
| 48 | +} |
| 49 | + |
| 50 | +void arduino::EthernetClass::config(arduino::IPAddress local_ip){ |
| 51 | + nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}}; |
| 52 | + _ip = SocketAddress(convertedIP); |
| 53 | +} |
| 54 | + |
| 55 | +void arduino::EthernetClass::config(const char *local_ip){ |
| 56 | + _ip = SocketAddress(local_ip); |
| 57 | +} |
| 58 | + |
| 59 | +void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server){ |
| 60 | + config(local_ip); |
| 61 | + setDNS(dns_server); |
| 62 | +} |
| 63 | + |
| 64 | +void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){ |
| 65 | + config(local_ip, dns_server); |
| 66 | + nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}}; |
| 67 | + _gateway = SocketAddress(convertedGatewayIP); |
| 68 | +} |
| 69 | + |
| 70 | +void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){ |
| 71 | + config(local_ip, dns_server, gateway); |
| 72 | + nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}}; |
| 73 | + _netmask = SocketAddress(convertedSubnetMask); |
| 74 | +} |
| 75 | + |
| 76 | +void arduino::EthernetClass::setDNS(IPAddress dns_server1){ |
| 77 | + nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}}; |
| 78 | + _dnsServer1 = SocketAddress(convertedDNSServer); |
| 79 | +} |
| 80 | + |
| 81 | +void arduino::EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){ |
| 82 | + setDNS(dns_server1); |
| 83 | + nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}}; |
| 84 | + _dnsServer2 = SocketAddress(convertedDNSServer2); |
| 85 | +} |
| 86 | + |
| 87 | +uint8_t arduino::EthernetClass::status() { |
| 88 | + return _currentNetworkStatus; |
| 89 | +} |
| 90 | + |
| 91 | +int arduino::EthernetClass::hostByName(const char* aHostname, IPAddress& aResult){ |
| 92 | + SocketAddress socketAddress = SocketAddress(); |
| 93 | + nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress); |
| 94 | + nsapi_addr_t address = socketAddress.get_addr(); |
| 95 | + aResult[0] = address.bytes[0]; |
| 96 | + aResult[1] = address.bytes[1]; |
| 97 | + aResult[2] = address.bytes[2]; |
| 98 | + aResult[3] = address.bytes[3]; |
| 99 | + return returnCode == NSAPI_ERROR_OK ? 1 : 0; |
| 100 | +} |
| 101 | + |
| 102 | +uint8_t* arduino::EthernetClass::macAddress(uint8_t* mac) { |
| 103 | + const char *mac_str = getNetwork()->get_mac_address(); |
| 104 | + for( int b = 0; b < 6; b++ ) |
| 105 | + { |
| 106 | + uint32_t tmp; |
| 107 | + sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ; |
| 108 | + mac[5-b] = (uint8_t)tmp ; |
| 109 | + } |
| 110 | + //sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]); |
| 111 | + return mac; |
| 112 | +} |
| 113 | + |
| 114 | +arduino::IPAddress arduino::EthernetClass::localIP() { |
| 115 | + SocketAddress ip; |
| 116 | + NetworkInterface *interface = getNetwork(); |
| 117 | + interface->get_ip_address(&ip); |
| 118 | + return ipAddressFromSocketAddress(ip); |
| 119 | +} |
| 120 | + |
| 121 | +arduino::IPAddress arduino::EthernetClass::subnetMask() { |
| 122 | + SocketAddress ip; |
| 123 | + NetworkInterface *interface = getNetwork(); |
| 124 | + interface->get_netmask(&ip); |
| 125 | + return ipAddressFromSocketAddress(ip); |
| 126 | +} |
| 127 | + |
| 128 | +arduino::IPAddress arduino::EthernetClass::gatewayIP() { |
| 129 | + SocketAddress ip; |
| 130 | + NetworkInterface *interface = getNetwork(); |
| 131 | + interface->get_gateway(&ip); |
| 132 | + return ipAddressFromSocketAddress(ip); |
| 133 | +} |
| 134 | + |
| 135 | +NetworkInterface *arduino::EthernetClass::getNetwork() { |
| 136 | + return eth_if; |
| 137 | +} |
| 138 | + |
| 139 | +unsigned long arduino::EthernetClass::getTime() { |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +arduino::EthernetClass Ethernet; |
0 commit comments