Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
27d36d8
Fix MacOS (case) filename clashes
manchoz Jul 6, 2021
357dba7
Add examples
manchoz Jul 6, 2021
c39cfee
Add helpers begin methods
manchoz Feb 1, 2021
05a1b80
Fix WebClient example
manchoz Feb 1, 2021
540e0ab
Add Static IP support
manchoz Jul 5, 2021
a5e512a
Manage Client status
manchoz Jul 5, 2021
d1501d0
Add support for Static Network configuration
manchoz Jul 5, 2021
b04124a
Update examples
manchoz Jul 5, 2021
817b47a
Add forgotten mac parameter
manchoz Jul 5, 2021
cf184fd
Add checks on socket validity before operations
manchoz Jul 6, 2021
9172c17
Manage sockets already connected
manchoz Jul 6, 2021
4aad72b
Add client socket management
manchoz Jul 6, 2021
5f802c1
WiFi: Cleanup libraries and error handling
facchinm Jul 7, 2021
4ded56c
WiFi: make library compliant with Arduino APIs
facchinm Jul 7, 2021
ec4109d
Initial: start moving WiFi generic classes into a standalone library
facchinm Jul 8, 2021
e0e8647
WiFi: port Client and Server to the new wrapper class
facchinm Jul 8, 2021
02b1239
TEMP: MbedClient: add horrible Client "constructor"
facchinm Jul 8, 2021
73cbc6b
WiFi: port UDP to new MbedUdp class
facchinm Jul 8, 2021
175d09f
Ethernet: port library to SocketWrapper
facchinm Jul 8, 2021
37edb74
MbedClient: fix copy constructor
facchinm Jul 8, 2021
974f4c2
Network: cleanup libraries and licenses
facchinm Jul 9, 2021
d4bb9d8
Network: apply clang-format on libraries
facchinm Jul 9, 2021
4340946
MbedUdp: make socket non blocking
facchinm Jul 9, 2021
123694b
SocketWrapper: fix download() API visibility
facchinm Jul 9, 2021
bc92047
MbedUDP: take into account write() errors
facchinm Jul 9, 2021
7fd7c9a
Remove protected leftovers
manchoz Jul 13, 2021
d9fa625
Add EthernetServer::available()
manchoz Jul 13, 2021
8c7962f
Add ::dnsServerIP
manchoz Jul 13, 2021
a175f35
Unlock mutexes before return
manchoz Jul 13, 2021
15ca271
Fix Typo
manchoz Jul 13, 2021
d3e8ee3
Manage copy and assignement
manchoz Jul 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ethernet: port library to SocketWrapper
  • Loading branch information
facchinm authored and manchoz committed Jul 14, 2021
commit 175d09f887dd95c8d50794098622ce62e4d9c18c
90 changes: 0 additions & 90 deletions libraries/Ethernet/src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

#define SSID_MAX_LENGTH 32

arduino::IPAddress arduino::EthernetClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
nsapi_addr_t address = socketAddress.get_addr();
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
}

SocketAddress arduino::EthernetClass::socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port) {
nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}};
return SocketAddress(convertedIP, port);
}

int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
if (eth_if == nullptr) {
//Q: What is the callback for?
Expand Down Expand Up @@ -80,91 +70,11 @@ int arduino::EthernetClass::disconnect() {
eth_if->disconnect();
}

void arduino::EthernetClass::config(arduino::IPAddress local_ip){
nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}};
_ip = SocketAddress(convertedIP);
}

void arduino::EthernetClass::config(const char *local_ip){
_ip = SocketAddress(local_ip);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server){
config(local_ip);
setDNS(dns_server);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
config(local_ip, dns_server);
nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}};
_gateway = SocketAddress(convertedGatewayIP);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
config(local_ip, dns_server, gateway);
nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}};
_netmask = SocketAddress(convertedSubnetMask);
}

void arduino::EthernetClass::setDNS(IPAddress dns_server1){
nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}};
_dnsServer1 = SocketAddress(convertedDNSServer);
}

void arduino::EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){
setDNS(dns_server1);
nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}};
_dnsServer2 = SocketAddress(convertedDNSServer2);
}

uint8_t arduino::EthernetClass::status() {
return _currentNetworkStatus;
}

int arduino::EthernetClass::hostByName(const char* aHostname, IPAddress& aResult){
SocketAddress socketAddress = SocketAddress();
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
nsapi_addr_t address = socketAddress.get_addr();
aResult[0] = address.bytes[0];
aResult[1] = address.bytes[1];
aResult[2] = address.bytes[2];
aResult[3] = address.bytes[3];
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
}

uint8_t* arduino::EthernetClass::macAddress(uint8_t* mac) {
const char *mac_str = getNetwork()->get_mac_address();
for( int b = 0; b < 6; b++ )
{
uint32_t tmp;
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
mac[5-b] = (uint8_t)tmp ;
}
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
return mac;
}

arduino::IPAddress arduino::EthernetClass::localIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_ip_address(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::EthernetClass::subnetMask() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_netmask(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::EthernetClass::gatewayIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_gateway(&ip);
return ipAddressFromSocketAddress(ip);
}

NetworkInterface *arduino::EthernetClass::getNetwork() {
return eth_if;
}
Expand Down
35 changes: 13 additions & 22 deletions libraries/Ethernet/src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
#define ethernet_h_

#include "Arduino.h"
#include "SocketHelpers.h"
#include "api/IPAddress.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
#include "EthernetUdp.h"

#include "netsocket/NetworkInterface.h"
#include "EthernetInterface.h"
Expand All @@ -46,7 +44,7 @@ namespace arduino {

typedef void* (*voidPrtFuncPtr)(void);

class EthernetClass {
class EthernetClass : public MbedSocketClass {

public:
// Initialise the Ethernet shield to use the provided MAC address and
Expand Down Expand Up @@ -74,26 +72,13 @@ class EthernetClass {
void init(uint8_t sspin = 10);

void MACAddress(uint8_t *mac_address);
uint8_t* macAddress(uint8_t* mac);
IPAddress localIP();
IPAddress subnetMask();
IPAddress gatewayIP();
IPAddress dnsServerIP() { return ipAddressFromSocketAddress(_dnsServer1); }

void config(IPAddress local_ip);
void config(const char *local_ip);
void config(IPAddress local_ip, IPAddress dns_server);
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
void setDNS(IPAddress dns_server1);
void setDNS(IPAddress dns_server1, IPAddress dns_server2);

void setHostname(const char* name);

int disconnect(void);
void end(void);

uint8_t status();
int hostByName(const char* aHostname, IPAddress& aResult);
unsigned long getTime();

void setMACAddress(const uint8_t *mac_address);
Expand All @@ -110,15 +95,17 @@ class EthernetClass {

NetworkInterface *getNetwork();

private:

volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
EthernetInterface net;
protected:
SocketAddress _ip = nullptr;
SocketAddress _gateway = nullptr;
SocketAddress _netmask = nullptr;
SocketAddress _dnsServer1 = nullptr;
SocketAddress _dnsServer2 = nullptr;

private:

volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
EthernetInterface net;
EthernetInterface* eth_if = &net;
voidPrtFuncPtr _initializerCallback;
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
Expand All @@ -129,4 +116,8 @@ class EthernetClass {

extern arduino::EthernetClass Ethernet;

#include "EthernetClient.h"
#include "EthernetServer.h"
#include "EthernetUdp.h"

#endif
185 changes: 1 addition & 184 deletions libraries/Ethernet/src/EthernetClient.cpp
Original file line number Diff line number Diff line change
@@ -1,184 +1 @@
#include "EthernetClient.h"

#ifndef SOCKET_TIMEOUT
#define SOCKET_TIMEOUT 1000
#endif

arduino::EthernetClient::EthernetClient()
: _status(false)
{
}

uint8_t arduino::EthernetClient::status() {
return _status;
}

void arduino::EthernetClient::setSocket(Socket* _sock) {
sock = _sock;
_status = true;
}

void arduino::EthernetClient::getStatus() {
if (sock == nullptr) {
_status = false;
return;
}

uint8_t data[256];
int ret = sock->recv(data, rxBuffer.availableForStore());
for (int i = 0; i < ret; i++) {
rxBuffer.store_char(data[i]);
}
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
_status = false;
}
}

int arduino::EthernetClient::connect(SocketAddress socketAddress) {
if (sock == nullptr) {
sock = new TCPSocket();
if(static_cast<TCPSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
return 0;
}
}
//sock->sigio(mbed::callback(this, &EthernetClient::getStatus));
//sock->set_blocking(false);
address = socketAddress;
sock->set_timeout(SOCKET_TIMEOUT);
nsapi_error_t returnCode = static_cast<TCPSocket*>(sock)->connect(socketAddress);
int ret = 0;
switch (returnCode) {
case NSAPI_ERROR_IS_CONNECTED:
case NSAPI_ERROR_OK: {
ret = 1;
break;
}
}
if (ret == 1)
_status = true;
return ret;
}

int arduino::EthernetClient::connect(IPAddress ip, uint16_t port) {
return connect(Ethernet.socketAddressFromIpAddress(ip, port));
}

int arduino::EthernetClient::connect(const char *host, uint16_t port) {
SocketAddress socketAddress = SocketAddress();
socketAddress.set_port(port);
Ethernet.getNetwork()->gethostbyname(host, &socketAddress);
return connect(socketAddress);
}

int arduino::EthernetClient::connectSSL(SocketAddress socketAddress){
if (sock == nullptr) {
sock = new TLSSocket();
if(static_cast<TLSSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
return 0;
}
}
if (beforeConnect) {
beforeConnect();
}
sock->set_timeout(SOCKET_TIMEOUT);
nsapi_error_t returnCode = static_cast<TLSSocket*>(sock)->connect(socketAddress);
int ret = 0;
switch (returnCode) {
case NSAPI_ERROR_IS_CONNECTED:
case NSAPI_ERROR_OK: {
ret = 1;
break;
}
}
if (ret == 1)
_status = true;

return ret;
}

int arduino::EthernetClient::connectSSL(IPAddress ip, uint16_t port) {
return connectSSL(Ethernet.socketAddressFromIpAddress(ip, port));
}

int arduino::EthernetClient::connectSSL(const char *host, uint16_t port) {
SocketAddress socketAddress = SocketAddress();
socketAddress.set_port(port);
Ethernet.getNetwork()->gethostbyname(host, &socketAddress);
return connectSSL(socketAddress);
}

size_t arduino::EthernetClient::write(uint8_t c) {
if (sock == nullptr)
return -1;
auto ret = sock->send(&c, 1);
return ret;
}

size_t arduino::EthernetClient::write(const uint8_t *buf, size_t size) {
if (sock == nullptr)
return -1;
auto ret = sock->send(buf, size);
return ret;
}

int arduino::EthernetClient::available() {
if (rxBuffer.available() == 0) {
getStatus();
}
return rxBuffer.available();
}

int arduino::EthernetClient::read() {
if (!available()) {
return -1;
}

return rxBuffer.read_char();
}

int arduino::EthernetClient::read(uint8_t *data, size_t len) {
int avail = available();

if (!avail) {
return -1;
}

if ((int)len > avail) {
len = avail;
}

for (size_t i = 0; i < len; i++) {
data[i] = rxBuffer.read_char();
}

return len;
}

int arduino::EthernetClient::peek() {
return rxBuffer.peek();
}

void arduino::EthernetClient::flush() {

}

void arduino::EthernetClient::stop() {
if (sock != nullptr) {
sock->close();
sock = nullptr;
}
_status = false;
}

uint8_t arduino::EthernetClient::connected() {
getStatus();
return _status;
}

IPAddress arduino::EthernetClient::remoteIP() {
return Ethernet.ipAddressFromSocketAddress(address);
}

uint16_t arduino::EthernetClient::remotePort() {
return 0;
}
#include "EthernetClient.h"
Loading