8000 Non-blocking Ethernet begin · Astro-Alliance/ArduinoCore-mbed@38bbd7a · GitHub
[go: up one dir, main page]

Skip to content

Commit 38bbd7a

Browse files
Non-blocking Ethernet begin
1 parent dfbc626 commit 38bbd7a

File tree

2 files changed

+92
-157
lines changed

2 files changed

+92
-157
lines changed

libraries/Ethernet/src/Ethernet.cpp

Lines changed: 40 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,65 @@
11
#include "Ethernet.h"
22

3-
#define SSID_MAX_LENGTH 32
4-
5-
int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
6-
if (eth_if == nullptr) {
7-
//Q: What is the callback for?
8-
_initializerCallback();
9-
if (eth_if == nullptr) return 0;
10-
}
11-
12-
if (mac != nullptr) {
13-
eth_if->get_emac().set_hwaddr(mac);
14-
}
15-
16-
unsigned long start = millis();
17-
eth_if->set_blocking(false);
18-
eth_if->connect();
19-
20-
while ((millis() - start < timeout) && (linkStatus() != LinkON)) {
21-
delay(10);
22-
}
23-
24-
return (linkStatus() == LinkON ? 1 : 0);
3+
void arduino::EthernetClass::begin(uint8_t *mac) {
4+
if (eth_if == nullptr) return;
5+
eth_if->set_blocking(false);
6+
eth_if->connect();
257
}
268

27-
int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip) {
28-
IPAddress dns = ip;
29-
dns[3] = 1;
30-
31-
auto ret = begin(mac, ip, dns);
32-
return ret;
9+
void arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip) {
10+
IPAddress dns = ip;
11+
dns[3] = 1;
12+
begin(mac, ip, dns);
3313
}
3414

35-
int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) {
36-
IPAddress gateway = ip;
37-
gateway[3] = 1;
38-
39-
auto ret = begin(mac, ip, dns, gateway);
40-
return ret;
15+
void arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) {
16+
IPAddress gateway = ip;
17+
gateway[3] = 1;
18+
begin(mac, ip, dns, gateway);
4119
}
4220

43-
int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {
44-
IPAddress subnet(255, 255, 255, 0);
45-
auto ret = begin(mac, ip, dns, gateway, subnet);
46-
return ret;
21+
void arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {
22+
begin(mac, ip, dns, gateway, IPAddress(255, 255, 255, 0));
4723
}
4824

49-
int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
50-
config(ip, dns, gateway, subnet);
51-
52-
eth_if->set_dhcp(false);
53-
eth_if->set_network(_ip, _netmask, _gateway);
54-
eth_if->add_dns_server(_dnsServer1, nullptr);
55-
56-
auto ret = begin(mac, timeout, responseTimeout);
57-
return ret;
25+
void arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
26+
config(ip, dns, gateway, subnet);
27+
eth_if->set_dhcp(false);
28+
eth_if->set_network(_ip, _netmask, _gateway);
29+
eth_if->add_dns_server(_dnsServer1, nullptr);
30+
begin(mac);
5831
}
5932

60-
void arduino::EthernetClass::end() {
61-
disconnect();
62-
}
33+
void arduino::EthernetClass::end() { disconnect(); }
6334

6435
EthernetLinkStatus arduino::EthernetClass::linkStatus() {
65-
return (eth_if->get_connection_status() == NSAPI_STATUS_GLOBAL_UP ? LinkON : LinkOFF);
66-
}
67-
68-
EthernetHardwareStatus arduino::EthernetClass::hardwareStatus() {
69-
return EthernetMbed;
36+
switch (eth_if->get_connection_status()) {
37+
case NSAPI_STATUS_GLOBAL_UP:
38+
return LinkON;
39+
case NSAPI_STATUS_DISCONNECTED:
40+
return LinkOFF;
41+
case NSAPI_STATUS_CONNECTING:
42+
return LinkConnecting;
43+
case NSAPI_STATUS_ERROR_UNSUPPORTED:
44+
return LinkError;
45+
default:
46+
return Unknown;
47+
}
7048
}
7149

50+
EthernetHardwareStatus arduino::EthernetClass::hardwareStatus() { return EthernetMbed; }
7251

7352
int arduino::EthernetClass::disconnect() {
74-
eth_if->disconnect();
75-
return 1;
53+
eth_if->disconnect();
54+
return 1;
7655
}
7756

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

79-
uint8_t arduino::EthernetClass::status() {
80-
return _currentNetworkStatus;
81-
}
82-
83-
NetworkInterface *arduino::EthernetClass::getNetwork() {
84-
return eth_if;
85-
}
59+
NetworkInterface *arduino::EthernetClass::getNetwork() { return eth_if; }
8660

87-
unsigned long arduino::EthernetClass::getTime() {
88-
return 0;
89-
}
61+
unsigned long arduino::EthernetClass::getTime() { return 0; }
9062

91-
void arduino::EthernetClass::MACAddress(uint8_t *mac_address)
92-
{
93-
macAddress(mac_address);
94-
}
63+
void arduino::EthernetClass::MACAddress(uint8_t *mac_address) { macAddress(mac_address); }
9564

9665
arduino::EthernetClass Ethernet;

libraries/Ethernet/src/Ethernet.h

Lines changed: 52 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -23,107 +23,73 @@
2323
#define ethernet_h_
2424

2525
#include "Arduino.h"
26+
#include "EthernetInterface.h"
2627
#include "SocketHelpers.h"
2728
#include "api/IPAddress.h"
28-
2929
#include "netsocket/NetworkInterface.h"
30-
#include "EthernetInterface.h"
3130

32-
enum EthernetLinkStatus {
33-
Unknown,
34-
LinkON,
35-
LinkOFF
36-
};
31+
enum EthernetLinkStatus { Unknown, LinkON, LinkOFF, LinkConnecting, LinkError };
3732

38-
enum EthernetHardwareStatus {
39-
EthernetNoHardware,
40-
EthernetMbed = 6
41-
};
33+
enum EthernetHardwareStatus { EthernetNoHardware, EthernetMbed = 6 };
4234

4335
namespace arduino {
4436

4537
enum { // compatibility with Arduino ::maintain()
46-
DHCP_CHECK_NONE = 0,
47-
DHCP_CHECK_RENEW_FAIL = 1,
48-
DHCP_CHECK_RENEW_OK = 2,
49-
DHCP_CHECK_REBIND_FAIL = 3,
50-
DHCP_CHECK_REBIND_OK = 4
38+
DHCP_CHECK_NONE = 0,
39+
DHCP_CHECK_RENEW_FAIL = 1,
40+
DHCP_CHECK_RENEW_OK = 2,
41+
DHCP_CHECK_REBIND_FAIL = 3,
42+
DHCP_CHECK_REBIND_OK = 4
5143
};
5244

53-
typedef void *(*voidPrtFuncPtr)(void);
54-
5545
class EthernetClass : public MbedSocketClass {
46+
public:
47+
// Initialise the Ethernet shield to use the provided MAC address and
48+
// gain the rest of the configuration through DHCP.
49+
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
50+
EthernetClass(EthernetInterface *_if) : eth_if(_if){};
51+
EthernetClass(){};
52+
53+
void begin(uint8_t *mac = nullptr);
54+
EthernetLinkStatus linkStatus();
55+
EthernetHardwareStatus hardwareStatus();
56+
57+
// Manual configuration
58+
void begin(uint8_t *mac, IPAddress ip);
59+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns);
60+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);
61+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
62+
63+
void begin(IPAddress ip) { begin(nullptr, ip); }
64+
void begin(IPAddress ip, IPAddress dns) { begin(nullptr, ip, dns); }
65+
void begin(IPAddress ip, IPAddress dns, IPAddress gateway) { begin(nullptr, ip, dns, gateway); }
66+
void begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) { begin(nullptr, ip, dns, gateway, subnet); }
67+
68+
void MACAddress(uint8_t *mac_address);
69+
70+
int disconnect(void);
71+
void end(void);
72+
73+
uint8_t status();
74+
unsigned long getTime();
75+
76+
void setDnsServerIP(const IPAddress dns_server) { _dnsServer1 = socketAddressFromIpAddress(dns_server, 0); }
77+
78+
friend class EthernetClient;
79+
friend class EthernetServer;
80+
friend class EthernetUDP;
81+
82+
NetworkInterface *getNetwork();
83+
84+
constexpr static int maintain() { return DHCP_CHECK_NONE; }
5685

57-
public:
58-
// Initialise the Ethernet shield to use the provided MAC address and
59-
// gain the rest of t 5 he co 10BC0 nfiguration through DHCP.
60-
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
61-
EthernetClass(EthernetInterface *_if)
62-
: eth_if(_if){};
63-
EthernetClass(){};
64-
65-
EthernetClass(voidPrtFuncPtr _cb)
66-
: _initializerCallback(_cb){};
67-
68-
int begin(uint8_t *mac = nullptr, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
69-
EthernetLinkStatus linkStatus();
70-
EthernetHardwareStatus hardwareStatus();
71-
72-
// Manual configuration
73-
int begin(uint8_t *mac, IPAddress ip);
74-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns);
75-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);
76-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
77-
78-
int begin(IPAddress ip) {
79-
return begin(nullptr, ip);
80-
}
81-
int begin(IPAddress ip, IPAddress dns) {
82-
return begin(nullptr, ip, dns);
83-
}
84-
int begin(IPAddress ip, IPAddress dns, IPAddress gateway) {
85-
return begin(nullptr, ip, dns, gateway);
86-
}
87-
int begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
88-
return begin(nullptr, ip, dns, gateway, subnet);
89-
}
90-
void init(uint8_t sspin = 10);
91-
92-
void MACAddress(uint8_t *mac_address);
93-
94-
int disconnect(void);
95-
void end(void);
96-
97-
uint8_t status();
98-
unsigned long getTime();
99-
100-
void setMACAddress(const uint8_t *mac_address);
101-
void setLocalIP(const IPAddress local_ip);
102-
void setSubnetMask(const IPAddress subnet);
103-
void setGatewayIP(const IPAddress gateway);
104-
void setDnsServerIP(const IPAddress dns_server) {
105-
_dnsServer1 = socketAddressFromIpAddress(dns_server, 0);
106-
}
107-
void setRetransmissionTimeout(uint16_t milliseconds);
108-
void setRetransmissionCount(uint8_t num);
109-
110-
friend class EthernetClient;
111-
friend class EthernetServer;
112-
friend class EthernetUDP;
113-
114-
NetworkInterface *getNetwork();
115-
116-
constexpr static int maintain () { return DHCP_CHECK_NONE; }
117-
118-
private:
119-
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
120-
EthernetInterface net;
121-
EthernetInterface *eth_if = &net;
122-
voidPrtFuncPtr _initializerCallback;
123-
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
86+
private:
87+
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
88+
EthernetInterface net;
89+
EthernetInterface *eth_if = &net;
12490
};
12591

126-
}
92+
} // namespace arduino
12793

12894
extern arduino::EthernetClass Ethernet;
12995

0 commit comments

Comments
 (0)
0