Skip to content

Commit 60f110c

Browse files
Ethernet lib: move Ethernet functions implementation in .cpp
1 parent f149444 commit 60f110c

File tree

2 files changed

+93
-53
lines changed

2 files changed

+93
-53
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
#include "Ethernet.h"
22

33
#if DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
4+
5+
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
6+
setMACAddress(mac);
7+
return NetworkInterface::begin(true, 0);
8+
}
9+
10+
int EthernetClass::begin(uint8_t *mac, IPAddress ip) {
11+
IPAddress dns = ip;
12+
dns[3] = 1;
13+
14+
auto ret = begin(mac, ip, dns);
15+
return ret;
16+
}
17+
18+
int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) {
19+
IPAddress gateway = ip;
20+
gateway[3] = 1;
21+
22+
auto ret = begin(mac, ip, dns, gateway);
23+
return ret;
24+
}
25+
26+
int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {
27+
IPAddress subnet(255, 255, 255, 0);
28+
auto ret = begin(mac, ip, dns, gateway, subnet);
29+
return ret;
30+
}
31+
32+
int EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout, unsigned long responseTimeout) {
33+
// TODO: Config the network interface with the provided IP, DNS, gateway, and subnet
34+
35+
return begin(mac, timeout, responseTimeout);
36+
}
37+
38+
EthernetLinkStatus EthernetClass::linkStatus() {
39+
if ((hardwareStatus() == EthernetOk) && net_if_is_up(netif)) {
40+
return LinkON;
41+
}
42+
return LinkOFF;
43+
}
44+
45+
EthernetHardwareStatus EthernetClass::hardwareStatus() {
46+
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
47+
if (device_is_ready(dev)) {
48+
for (int i = 1; i < 3; i++) {
49+
auto _if = net_if_get_by_index(i);
50+
if (!net_eth_type_is_wifi(_if)) {
51+
netif = _if;
52+
break;
53+
}
54+
}
55+
return EthernetOk;
56+
} else {
57+
return EthernetNoHardware;
58+
}
59+
}
60+
61+
void EthernetClass::setMACAddress(const uint8_t *mac_address) {
62+
if (mac_address != nullptr) {
63+
NetworkInterface::setMACAddress(mac_address);
64+
}
65+
}
66+
67+
IPAddress EthernetClass::localIP() {
68+
return NetworkInterface::localIP();
69+
}
70+
471
EthernetClass Ethernet;
572
#endif

libraries/SocketWrapper/Ethernet.h

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,33 @@ class EthernetClass: public NetworkInterface
2121
EthernetClass() {}
2222
virtual ~EthernetClass() {}
2323

24-
int begin(bool blocking = true, uint32_t additional_event_mask = 0) {
25-
hardwareStatus();
26-
return NetworkInterface::begin(blocking, additional_event_mask);
27-
}
28-
29-
int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000) {
30-
hardwareStatus();
31-
if (mac != nullptr) {
32-
NetworkInterface::setMACAddress(mac);
33-
}
34-
return NetworkInterface::begin(true, 0);
35-
}
36-
37-
int maintain(); //TODO
38-
39-
EthernetLinkStatus linkStatus() {
40-
hardwareStatus();
41-
if (net_if_is_up(netif)) {
42-
return LinkON;
43-
} else {
44-
return LinkOFF;
45-
}
46-
}
47-
48-
EthernetHardwareStatus hardwareStatus() {
49-
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
50-
if (device_is_ready(dev)) {
51-
for (int i = 1; i < 3; i++) {
52-
auto _if = net_if_get_by_index(i);
53-
if (!net_eth_type_is_wifi(_if)) {
54-
netif = _if;
55-
break;
56-
}
57-
}
58-
return EthernetOk;
59-
} else {
60-
return EthernetNoHardware;
61-
}
62-
}
63-
64-
int begin(uint8_t *mac, IPAddress ip) {
65-
return begin(); //TODO
66-
}
67-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns) {
68-
return begin(); //TODO
69-
}
70-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {
71-
return begin(); //TODO
72-
}
73-
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
74-
return begin(); //TODO
75-
}
76-
void init(uint8_t sspin = 10); //TODO
24+
int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
25+
int maintain();
26+
EthernetLinkStatus linkStatus();
27+
EthernetHardwareStatus hardwareStatus();
28+
29+
// Manual configuration
30+
int begin(uint8_t *mac, IPAddress ip);
31+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns);
32+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);
33+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
34+
void init(uint8_t sspin = 10);
35+
36+
void MACAddress(uint8_t *mac_address);
37+
IPAddress localIP();
38+
IPAddress subnetMask();
39+
IPAddress gatewayIP();
40+
IPAddress dnsServerIP();
41+
42+
void setMACAddress(const uint8_t *mac_address);
43+
void setLocalIP(const IPAddress local_ip);
44+
void setSubnetMask(const IPAddress subnet);
45+
void setGatewayIP(const IPAddress gateway);
46+
void setDnsServerIP(const IPAddress dns_server);
47+
void setRetransmissionTimeout(uint16_t milliseconds);
48+
void setRetransmissionCount(uint8_t num);
7749
};
50+
7851
extern EthernetClass Ethernet;
7952

8053
#endif

0 commit comments

Comments
 (0)