Skip to content

Commit 2fdab09

Browse files
ethernet lib: set mac address, log module fix
1 parent e30c16f commit 2fdab09

File tree

5 files changed

+228
-163
lines changed

5 files changed

+228
-163
lines changed

libraries/SocketWrapper/Ethernet.h

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ 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+
2439
EthernetLinkStatus linkStatus() {
2540
hardwareStatus();
2641
if (net_if_is_up(netif)) {
@@ -29,20 +44,7 @@ class EthernetClass: public NetworkInterface
2944
return LinkOFF;
3045
}
3146
}
32-
33-
bool begin(bool blocking = true, uint32_t additional_event_mask = 0) {
34-
hardwareStatus();
35-
return NetworkInterface::begin(blocking, additional_event_mask);
36-
}
37-
38-
bool begin(uint8_t* mac_address, int _timeout, int _response_timeout) {
39-
return begin();
40-
}
41-
42-
bool begin(uint8_t* mac_address, IPAddress _ip, IPAddress _dns, IPAddress _gateway, IPAddress _netmask, int _timeout, int _response_timeout) {
43-
return begin();
44-
}
45-
47+
4648
EthernetHardwareStatus hardwareStatus() {
4749
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
4850
if (device_is_ready(dev)) {
@@ -57,7 +59,21 @@ class EthernetClass: public NetworkInterface
5759
} else {
5860
return EthernetNoHardware;
5961
}
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
6072
}
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
6177
};
6278
extern EthernetClass Ethernet;
6379

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,154 @@
11
#include "SocketHelpers.h"
22

3+
#include <zephyr/logging/log.h>
4+
LOG_MODULE_DECLARE(sketch, CONFIG_LOG_DEFAULT_LEVEL);
5+
36
struct net_mgmt_event_callback NetworkInterface::mgmt_cb;
47
struct net_dhcpv4_option_callback NetworkInterface::dhcp_cb;
8+
9+
void NetworkInterface::event_handler(struct net_mgmt_event_callback *cb,
10+
uint64_t mgmt_event,
11+
struct net_if *iface)
12+
{
13+
int i = 0;
14+
15+
if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
16+
return;
17+
}
18+
19+
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
20+
char buf[NET_IPV4_ADDR_LEN];
21+
22+
if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type !=
23+
NET_ADDR_DHCP) {
24+
continue;
25+
}
26+
27+
LOG_INF(" Address[%d]: %s", net_if_get_by_iface(iface),
28+
net_addr_ntop(AF_INET,
29+
&iface->config.ip.ipv4->unicast[i].ipv4.address.in_addr,
30+
buf, sizeof(buf)));
31+
LOG_INF(" Subnet[%d]: %s", net_if_get_by_iface(iface),
32+
net_addr_ntop(AF_INET,
33+
&iface->config.ip.ipv4->unicast[i].netmask,
34+
buf, sizeof(buf)));
35+
LOG_INF(" Router[%d]: %s", net_if_get_by_iface(iface),
36+
net_addr_ntop(AF_INET,
37+
&iface->config.ip.ipv4->gw,
38+
buf, sizeof(buf)));
39+
LOG_INF("Lease time[%d]: %u seconds", net_if_get_by_iface(iface),
40+
iface->config.dhcpv4.lease_time);
41+
}
42+
}
43+
44+
void NetworkInterface::option_handler(struct net_dhcpv4_option_callback *cb,
45+
size_t length,
46+
enum net_dhcpv4_msg_type msg_type,
47+
struct net_if *iface)
48+
{
49+
char buf[NET_IPV4_ADDR_LEN];
50+
51+
LOG_INF("DHCP Option %d: %s", cb->option,
52+
net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
53+
}
54+
55+
int NetworkInterface::dhcp()
56+
{
57+
net_mgmt_init_event_callback(&mgmt_cb, event_handler, NET_EVENT_IPV4_ADDR_ADD | NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
58+
net_mgmt_add_event_callback(&mgmt_cb);
59+
60+
net_dhcpv4_init_option_callback(&dhcp_cb, option_handler,
61+
DHCP_OPTION_NTP, ntp_server,
62+
sizeof(ntp_server));
63+
64+
net_dhcpv4_add_option_callback(&dhcp_cb);
65+
66+
net_dhcpv4_start(netif);
67+
68+
LOG_INF("DHCPv4 started...\n");
69+
70+
return 0;
71+
}
72+
73+
void NetworkInterface::enable_dhcpv4_server(struct net_if *netif, char* _netmask)
74+
{
75+
static struct in_addr addr;
76+
static struct in_addr netmaskAddr;
77+
78+
if (net_addr_pton(AF_INET, String(localIP()).c_str(), &addr)) {
79+
LOG_ERR("Invalid address: %s", String(localIP()).c_str());
80+
return;
81+
}
82+
83+
if (net_addr_pton(AF_INET, _netmask, &netmaskAddr)) {
84+
LOG_ERR("Invalid netmask: %s", _netmask);
85+
return;
86+
}
87+
88+
net_if_ipv4_set_gw(netif, &addr);
89+
90+
if (net_if_ipv4_addr_add(netif, &addr, NET_ADDR_MANUAL, 0) == NULL) {
91+
LOG_ERR("unable to set IP address for AP interface");
92+
}
93+
94+
if (!net_if_ipv4_set_netmask_by_addr(netif, &addr, &netmaskAddr)) {
95+
LOG_ERR("Unable to set netmask for AP interface: %s", _netmask);
96+
}
97+
98+
addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */
99+
100+
if (net_dhcpv4_server_start(netif, &addr) != 0) {
101+
LOG_ERR("DHCP server is not started for desired IP");
102+
return;
103+
}
104+
105+
LOG_INF("DHCPv4 server started...\n");
106+
}
107+
108+
IPAddress NetworkInterface::localIP() {
109+
return IPAddress(netif->config.ip.ipv4->unicast[0].ipv4.address.in_addr.s_addr);
110+
}
111+
112+
IPAddress NetworkInterface::subnetMask() {
113+
return IPAddress(netif->config.ip.ipv4->unicast[0].netmask.s_addr);
114+
}
115+
IPAddress NetworkInterface::gatewayIP() {
116+
return IPAddress(netif->config.ip.ipv4->gw.s_addr);
117+
}
118+
IPAddress NetworkInterface::dnsServerIP() {
119+
return arduino::INADDR_NONE;
120+
}
121+
122+
IPAddress NetworkInterface::dnsIP(int n) {
123+
//TODO
124+
}
125+
126+
void NetworkInterface::setMACAddress(const uint8_t* mac) {
127+
struct net_eth_addr new_mac;
128+
struct ethernet_req_params params = { 0 };
129+
130+
memcpy(&params.mac_address, &new_mac, sizeof(struct net_eth_addr));
131+
132+
net_if_down(netif); // Ensure the interface is down before changing the MAC address
133+
134+
int ret = net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, netif,
135+
&params, sizeof(params));
136+
if (ret != 0) {
137+
LOG_ERR("Failed to set MAC address via net_mgmt, ret=%d", ret);
138+
} else {
139+
LOG_INF("MAC address set successfully via net_mgmt");
140+
}
141+
142+
net_if_up(netif); // Bring the interface back up after changing the MAC address
143+
}
144+
145+
int NetworkInterface::begin(bool blocking, uint32_t additional_event_mask) {
146+
dhcp();
147+
int ret = net_mgmt_event_wait_on_iface(netif, NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
148+
NULL, NULL, NULL, blocking ? K_FOREVER : K_SECONDS(1));
149+
return (ret == 0) ? 1 : 0;
150+
}
151+
152+
bool NetworkInterface::disconnect() {
153+
return (net_if_down(netif) == 0);
154+
}

0 commit comments

Comments
 (0)