Skip to content

Commit f149444

Browse files
ethernet lib: set mac address, log module fix
1 parent cb3531e commit f149444

File tree

5 files changed

+246
-178
lines changed

5 files changed

+246
-178
lines changed

libraries/SocketWrapper/Ethernet.h

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,76 @@
55
#if DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
66

77
enum EthernetLinkStatus {
8-
Unknown,
9-
LinkON,
10-
LinkOFF
8+
Unknown,
9+
LinkON,
10+
LinkOFF
1111
};
1212

1313
enum EthernetHardwareStatus {
14-
EthernetNoHardware,
15-
EthernetOk
14+
EthernetNoHardware,
15+
EthernetOk
1616
};
1717

18-
class EthernetClass : public NetworkInterface {
18+
class EthernetClass: public NetworkInterface
19+
{
1920
public:
20-
EthernetClass() {
21-
}
21+
EthernetClass() {}
22+
virtual ~EthernetClass() {}
2223

23-
virtual ~EthernetClass() {
24-
}
24+
int begin(bool blocking = true, uint32_t additional_event_mask = 0) {
25+
hardwareStatus();
26+
return NetworkInterface::begin(blocking, additional_event_mask);
27+
}
2528

26-
EthernetLinkStatus linkStatus() {
27-
hardwareStatus();
28-
if (net_if_is_up(netif)) {
29-
return LinkON;
30-
} else {
31-
return LinkOFF;
32-
}
33-
}
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+
}
3436

35-
bool begin(bool blocking = true, uint32_t additional_event_mask = 0) {
36-
hardwareStatus();
37-
return NetworkInterface::begin(blocking, additional_event_mask);
38-
}
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+
}
3963

40-
bool begin(uint8_t *mac_address, int _timeout, int _response_timeout) {
41-
return begin();
42-
}
43-
44-
bool begin(uint8_t *mac_address, IPAddress _ip, IPAddress _dns, IPAddress _gateway,
45-
IPAddress _netmask, int _timeout, int _response_timeout) {
46-
return begin();
47-
}
48-
49-
EthernetHardwareStatus hardwareStatus() {
50-
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
51-
if (device_is_ready(dev)) {
52-
for (int i = 1; i < 4; i++) {
53-
auto _if = net_if_get_by_index(i);
54-
if (_if && !net_eth_type_is_wifi(_if)) {
55-
netif = _if;
56-
break;
57-
}
58-
}
59-
return EthernetOk;
60-
} else {
61-
return EthernetNoHardware;
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
6477
};
65-
6678
extern EthernetClass Ethernet;
6779

68-
#endif
80+
#endif
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)