8000 Ethernet: port library to SocketWrapper · OS-Q/Arduino_Core_mbed@175d09f · GitHub
[go: up one dir, main page]

Skip to content

Commit 175d09f

Browse files
facchinmmanchoz
authored andcommitted
Ethernet: port library to SocketWrapper
1 parent 73cbc6b commit 175d09f

File tree

8 files changed

+43
-682
lines changed

8 files changed

+43
-682
lines changed

libraries/Ethernet/src/Ethernet.cpp

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
#define SSID_MAX_LENGTH 32
44

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-
155
int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
166
if (eth_if == nullptr) {
177
//Q: What is the callback for?
@@ -80,91 +70,11 @@ int arduino::EthernetClass::disconnect() {
8070
eth_if->disconnect();
8171
}
8272

83-
void arduino::EthernetClass::config(arduino::IPAddress local_ip){
84-
nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}};
85-
_ip = SocketAddress(convertedIP);
86-
}
87-
88-
void arduino::EthernetClass::config(const char *local_ip){
89-
_ip = SocketAddress(local_ip);
90-
}
91-
92-
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server){
93-
config(local_ip);
94-
setDNS(dns_server);
95-
}
96-
97-
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
98-
config(local_ip, dns_server);
99-
nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}};
100-
_gateway = SocketAddress(convertedGatewayIP);
101-
}
102-
103-
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
104-
config(local_ip, dns_server, gateway);
105-
nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}};
106-
_netmask = SocketAddress(convertedSubnetMask);
107-
}
108-
109-
void arduino::EthernetClass::setDNS(IPAddress dns_server1){
110-
nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}};
111-
_dnsServer1 = SocketAddress(convertedDNSServer);
112-
}
113-
114-
void arduino::EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){
115-
setDNS(dns_server1);
116-
nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}};
117-
_dnsServer2 = SocketAddress(convertedDNSServer2);
118-
}
11973

12074
uint8_t arduino::EthernetClass::status() {
12175
return _currentNetworkStatus;
12276
}
12377

124-
int arduino::EthernetClass::hostByName(const char* aHostname, IPAddress& aResult){
125-
SocketAddress socketAddress = SocketAddress();
126-
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
127-
nsapi_addr_t address = socketAddress.get_addr();
128-
aResult[0] = address.bytes[0];
129-
aResult[1] = address.bytes[1];
130-
aResult[2] = address.bytes[2];
131-
aResult[3] = address.bytes[3];
132-
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
133-
}
134-
135-
uint8_t* arduino::EthernetClass::macAddress(uint8_t* mac) {
136-
const char *mac_str = getNetwork()->get_mac_address();
137-
for( int b = 0; b < 6; b++ )
138-
{
139-
uint32_t tmp;
140-
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
141-
mac[5-b] = (uint8_t)tmp ;
142-
}
143-
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
144-
return mac;
145-
}
146-
147-
arduino::IPAddress arduino::EthernetClass::localIP() {
148-
SocketAddress ip;
149-
NetworkInterface *interface = getNetwork();
150-
interface->get_ip_address(&ip);
151-
return ipAddressFromSocketAddress(ip);
152-
}
153-
154-
arduino::IPAddress arduino::EthernetClass::subnetMask() {
155-
SocketAddress ip;
156-
NetworkInterface *interface = getNetwork();
157-
interface->get_netmask(&ip);
158-
return ipAddressFromSocketAddress(ip);
159-
}
160-
161-
arduino::IPAddress arduino::EthernetClass::gatewayIP() {
162-
SocketAddress ip;
163-
NetworkInterface *interface = getNetwork();
164-
interface->get_gateway(&ip);
165-
return ipAddressFromSocketAddress(ip);
166-
}
167-
16878
NetworkInterface *arduino::EthernetClass::getNetwork() {
16979
return eth_if;
17080
}

libraries/Ethernet/src/Ethernet.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
#define ethernet_h_
2424

2525
#include "Arduino.h"
26+
#include "SocketHelpers.h"
2627
#include "api/IPAddress.h"
27-
#include "EthernetClient.h"
28-
#include "EthernetServer.h"
29-
#include "EthernetUdp.h"
3028

3129
#include "netsocket/NetworkInterface.h"
3230
#include "EthernetInterface.h"
@@ -46,7 +44,7 @@ namespace arduino {
4644

4745
typedef void* (*voidPrtFuncPtr)(void);
4846

49-
class EthernetClass {
47+
class EthernetClass : public MbedSocketClass {
5048

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

7674
void MACAddress(uint8_t *mac_address);
77-
uint8_t* macAddress(uint8_t* mac);
78-
IPAddress localIP();
79-
IPAddress subnetMask();
80-
IPAddress gatewayIP();
81-
IPAddress dnsServerIP() { return ipAddressFromSocketAddress(_dnsServer1); }
82-
83-
void config(IPAddress local_ip);
84-
void config(const char *local_ip);
85-
void config(IPAddress local_ip, IPAddress dns_server);
86-
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
87-
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
88-
void setDNS(IPAddress dns_server1);
89-
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
75+
9076
void setHostname(const char* name);
9177

9278
int disconnect(void);
9379
void end(void);
9480

9581
uint8_t status();
96-
int hostByName(const char* aHostname, IPAddress& aResult);
9782
unsigned long getTime();
9883

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

11196
NetworkInterface *getNetwork();
11297

113-
private:
114-
115-
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
116-
EthernetInterface net;
98+
protected:
11799
SocketAddress _ip = nullptr;
118100
SocketAddress _gateway = nullptr;
119101
SocketAddress _netmask = nullptr;
120102
SocketAddress _dnsServer1 = nullptr;
121103
SocketAddress _dnsServer2 = nullptr;
104+
105+
private:
106+
107+
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
108+
EthernetInterface net;
122109
EthernetInterface* eth_if = &net;
123110
voidPrtFuncPtr _initializerCallback;
124111
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
@@ -129,4 +116,8 @@ class EthernetClass {
129116

130117
extern arduino::EthernetClass Ethernet;
131118

119+
#include "EthernetClient.h"
120+
#include "EthernetServer.h"
121+
#include "EthernetUdp.h"
122+
132123
#endif
Lines changed: 1 addition & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1 @@
1-
#include "EthernetClient.h"
2-
3-
#ifndef SOCKET_TIMEOUT
4-
#define SOCKET_TIMEOUT 1000
5-
#endif
6-
7-
arduino::EthernetClient::EthernetClient()
8-
: _status(false)
9-
{
10-
}
11-
12-
uint8_t arduino::EthernetClient::status() {
13-
return _status;
14-
}
15-
16-
void arduino::EthernetClient::setSocket(Socket* _sock) {
17-
sock = _sock;
18-
_status = true;
19-
}
20-
21-
void arduino::EthernetClient::getStatus() {
22-
if (sock == nullptr) {
23-
_status = false;
24-
return;
25-
}
26-
27-
uint8_t data[256];
28-
int ret = sock->recv(data, rxBuffer.availableForStore());
29-
for (int i = 0; i < ret; i++) {
30-
rxBuffer.store_char(data[i]);
31-
}
32-
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
33-
_status = false;
34-
}
35-
}
36-
37-
int arduino::EthernetClient::connect(SocketAddress socketAddress) {
38-
if (sock == nullptr) {
39-
sock = new TCPSocket();
40-
if(static_cast<TCPSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
41-
return 0;
42-
}
43-
}
44-
//sock->sigio(mbed::callback(this, &EthernetClient::getStatus));
45-
//sock->set_blocking(false);
46-
address = socketAddress;
47-
sock->set_timeout(SOCKET_TIMEOUT);
48-
nsapi_error_t returnCode = static_cast<TCPSocket*>(sock)->connect(socketAddress);
49-
int ret = 0;
50-
switch (returnCode) {
51-
case NSAPI_ERROR_IS_CONNECTED:
52-
case NSAPI_ERROR_OK: {
53-
ret = 1;
54-
break;
55-
}
56-
}
57-
if (ret == 1)
58-
_status = true;
59-
return ret;
60-
}
61-
62-
int arduino::EthernetClient::connect(IPAddress ip, uint16_t port) {
63-
return connect(Ethernet.socketAddressFromIpAddress(ip, port));
64-
}
65-
66-
int arduino::EthernetClient::connect(const char *host, uint16_t port) {
67-
SocketAddress socketAddress = SocketAddress();
68-
socketAddress.set_port(port);
69-
Ethernet.getNetwork()->gethostbyname(host, &socketAddress);
70-
return connect(socketAddress);
71-
}
72-
73-
int arduino::EthernetClient::connectSSL(SocketAddress socketAddress){
74-
if (sock == nullptr) {
75-
sock = new TLSSocket();
76-
if(static_cast<TLSSocket*>(sock)->open(Ethernet.getNetwork()) != NSAPI_ERROR_OK){
77-
return 0;
78-
}
79-
}
80-
if (beforeConnect) {
81-
beforeConnect();
82-
}
83-
sock->set_timeout(SOCKET_TIMEOUT);
84-
nsapi_error_t returnCode = static_cast<TLSSocket*>(sock)->connect(socketAddress);
85-
int ret = 0;
86-
switch (returnCode) {
87-
case NSAPI_ERROR_IS_CONNECTED:
88-
case NSAPI_ERROR_OK: {
89-
ret = 1;
90-
break;
91-
}
92-
}
93-
if (ret == 1)
94-
_status = true;
95-
96-
return ret;
97-
}
98-
99-
int arduino::EthernetClient::connectSSL(IPAddress ip, uint16_t port) {
100-
return connectSSL(Ethernet.socketAddressFromIpAddress(ip, port));
101-
}
102-
103-
int arduino::EthernetClient::connectSSL(const char *host, uint16_t port) {
104-
SocketAddress socketAddress = SocketAddress();
105-
socketAddress.set_port(port);
106-
Ethernet.getNetwork()->gethostbyname(host, &socketAddress);
107-
return connectSSL(socketAddress);
108-
}
109-
110-
size_t arduino::EthernetClient::write(uint8_t c) {
111-
if (sock == nullptr)
112-
return -1;
113-
auto ret = sock->send(&c, 1);
114-
return ret;
115-
}
116-
117-
size_t arduino::EthernetClient::write(const uint8_t *buf, size_t size) {
118-
if (sock == nullptr)
119-
return -1;
120-
auto ret = sock->send(buf, size);
121-
return ret;
122-
}
123-
124-
int arduino::EthernetClient::available() {
125-
if (rxBuffer.available() == 0) {
126-
getStatus();
127-
}
128-
return rxBuffer.available();
129-
}
130-
131-
int arduino::EthernetClient::read() {
132-
if (!available()) {
133-
return -1;
134-
}
135-
136-
return rxBuffer.read_char();
137-
}
138-
139- 10000
int arduino::EthernetClient::read(uint8_t *data, size_t len) {
140-
int avail = available();
141-
142-
if (!avail) {
143-
return -1;
144-
}
145-
146-
if ((int)len > avail) {
147-
len = avail;
148-
}
149-
150-
for (size_t i = 0; i < len; i++) {
151-
data[i] = rxBuffer.read_char();
152-
}
153-
154-
return len;
155-
}
156-
157-
int arduino::EthernetClient::peek() {
158-
return rxBuffer.peek();
159-
}
160-
161-
void arduino::EthernetClient::flush() {
162-
163-
}
164-
165-
void arduino::EthernetClient::stop() {
166-
if (sock != nullptr) {
167-
sock->close();
168-
sock = nullptr;
169-
}
170-
_status = false;
171-
}
172-
173-
uint8_t arduino::EthernetClient::connected() {
174-
getStatus();
175-
return _status;
176-
}
177-
178-
IPAddress arduino::EthernetClient::remoteIP() {
179-
return Ethernet.ipAddressFromSocketAddress(address);
180-
}
181-
182-
uint16_t arduino::EthernetClient::remotePort() {
183-
return 0;
184-
}
1+
#include "EthernetClient.h"

0 commit comments

Comments
 (0)
0