Skip to content

Commit 3bb2f23

Browse files
committed
SocketWrapper: start restucturing lib
1 parent 804ccc1 commit 3bb2f23

File tree

6 files changed

+220
-122
lines changed

6 files changed

+220
-122
lines changed

Diff for: libraries/SocketWrapper/Ethernet.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#include "Ethernet.h"
22

3-
NetworkInterface Ethernet(1);
3+
#if DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
4+
EthernetClass Ethernet;
5+
#endif

Diff for: libraries/SocketWrapper/Ethernet.h

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,60 @@
1-
#define SPECIALIZE_FOR_ETHERNET
21
#include "SocketHelpers.h"
32

4-
extern NetworkInterface Ethernet;
3+
#include <zephyr/net/ethernet.h>
4+
5+
enum EthernetLinkStatus {
6+
Unknown,
7+
LinkON,
8+
LinkOFF
9+
};
10+
11+
enum EthernetHardwareStatus {
12+
EthernetNoHardware,
13+
EthernetOk
14+
};
15+
16+
class EthernetClass: public NetworkInterface
17+
{
18+
public:
19+
EthernetClass() {}
20+
virtual ~EthernetClass() {}
21+
22+
EthernetLinkStatus linkStatus() {
23+
hardwareStatus();
24+
if (net_if_is_up(netif)) {
25+
return LinkON;
26+
} else {
27+
return LinkOFF;
28+
}
29+
}
30+
31+
bool begin(bool blocking = true, uint32_t additional_event_mask = 0) {
32+
hardwareStatus();
33+
return NetworkInterface::begin(blocking, additional_event_mask);
34+
}
35+
36+
bool begin(uint8_t* mac_address, int _timeout, int _response_timeout) {
37+
return begin();
38+
}
39+
40+
bool begin(uint8_t* mac_address, IPAddress _ip, IPAddress _dns, IPAddress _gateway, IPAddress _netmask, int _timeout, int _response_timeout) {
41+
return begin();
42+
}
43+
44+
EthernetHardwareStatus hardwareStatus() {
45+
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
46+
if (device_is_ready(dev)) {
47+
for (int i = 1; i < 3; i++) {
48+
auto _if = net_if_get_by_index(i);
49+
if (!net_eth_type_is_wifi(_if)) {
50+
netif = _if;
51+
break;
52+
}
53+
}
54+
return EthernetOk;
55+
} else {
56+
return EthernetNoHardware;
57+
}
58+
}
59+
};
60+
extern EthernetClass Ethernet;

Diff for: libraries/SocketWrapper/SocketHelpers.h

+50-116
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

3-
#include "zephyr/net/dhcpv4.h"
3+
#include <zephyr/net/dhcpv4.h>
4+
#include <zephyr/net/dhcpv4_server.h>
45
#include <cstddef>
56
#include <zephyr/kernel.h>
67
#include <zephyr/linker/sections.h>
@@ -18,28 +19,11 @@
1819

1920
#undef LOG_INF
2021
#define LOG_INF(...)
21-
22-
#ifdef SPECIALIZE_FOR_ETHERNET
23-
enum EthernetLinkStatus {
24-
Unknown,
25-
LinkON,
26-
LinkOFF
27-
};
28-
29-
enum EthernetHardwareStatus {
30-
EthernetNoHardware,
31-
EthernetOk
32-
};
33-
#endif
34-
35-
#ifdef SPECIALIZE_FOR_WIFI
36-
#include "utility/wl_definitions.h"
37-
#include <zephyr/net/wifi_mgmt.h>
38-
#endif
22+
#undef LOG_ERR
23+
#define LOG_ERR(...)
3924

4025
class NetworkInterface {
4126
private:
42-
int iface_index = -1;
4327

4428
uint8_t ntp_server[4];
4529
static struct net_mgmt_event_callback mgmt_cb;
@@ -91,6 +75,9 @@ class NetworkInterface {
9175
net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
9276
}
9377

78+
protected:
79+
80+
struct net_if *netif = nullptr;
9481
int dhcp()
9582
{
9683
net_mgmt_init_event_callback(&mgmt_cb, event_handler, NET_EVENT_IPV4_ADDR_ADD | NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
@@ -102,23 +89,59 @@ class NetworkInterface {
10289

10390
net_dhcpv4_add_option_callback(&dhcp_cb);
10491

105-
net_dhcpv4_start(net_if_get_by_index(iface_index));
92+
net_dhcpv4_start(netif);
10693

10794
return 0;
10895
}
10996

97+
void enable_dhcpv4_server(struct net_if *netif, char* _netmask = "255.255.255.0")
98+
{
99+
static struct in_addr addr;
100+
static struct in_addr netmaskAddr;
101+
102+
if (net_addr_pton(AF_INET, String(localIP()).c_str(), &addr)) {
103+
LOG_ERR("Invalid address: %s", String(localIP()).c_str());
104+
return;
105+
}
106+
107+
if (net_addr_pton(AF_INET, _netmask, &netmaskAddr)) {
108+
LOG_ERR("Invalid netmask: %s", _netmask);
109+
return;
110+
}
111+
112+
net_if_ipv4_set_gw(netif, &addr);
113+
114+
if (net_if_ipv4_addr_add(netif, &addr, NET_ADDR_MANUAL, 0) == NULL) {
115+
LOG_ERR("unable to set IP address for AP interface");
116+
}
117+
118+
if (!net_if_ipv4_set_netmask_by_addr(netif, &addr, &netmaskAddr)) {
119+
LOG_ERR("Unable to set netmask for AP interface: %s", _netmask);
120+
}
121+
122+
addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */
123+
124+
if (net_dhcpv4_server_start(netif, &addr) != 0) {
125+
LOG_ERR("DHCP server is not started for desired IP");
126+
return;
127+
}
128+
129+
LOG_INF("DHCPv4 server started...\n");
130+
}
131+
132+
110133
public:
111-
NetworkInterface(int iface_index) : iface_index(iface_index) {}
134+
NetworkInterface() {}
112135
~NetworkInterface() {}
113136
IPAddress localIP() {
114-
return IPAddress(net_if_get_by_index(iface_index)->config.ip.ipv4->unicast[0].ipv4.address.in_addr.s_addr);
137+
return IPAddress(netif->config.ip.ipv4->unicast[0].ipv4.address.in_addr.s_addr);
115138
}
116139

117140
IPAddress subnetMask() {
118-
return IPAddress(net_if_get_by_index(iface_index)->config.ip.ipv4->unicast[0].netmask.s_addr);
141+
return IPAddress(netif->config.ip.ipv4->unicast[0].netmask.s_addr);
119142
}
120143
IPAddress gatewayIP() {
121-
return IPAddress(net_if_get_by_index(iface_index)->config.ip.ipv4->gw.s_addr);
144+
return IPAddress(netif->config.ip.ipv4->gw.s_addr);
122145
}
123146
IPAddress dnsServerIP() {
124147
return arduino::INADDR_NONE;
@@ -130,105 +153,16 @@ class NetworkInterface {
130153

131154
bool begin(bool blocking = true, uint32_t additional_event_mask = 0) {
132155
dhcp();
133-
int ret = net_mgmt_event_wait_on_iface(net_if_get_by_index(iface_index), NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
156+
int ret = net_mgmt_event_wait_on_iface(netif, NET_EVENT_IPV4_ADDR_ADD | additional_event_mask,
134157
NULL, NULL, NULL, blocking ? K_FOREVER : K_SECONDS(1));
135158
return (ret == 0);
136159
}
137160

138161
bool disconnect() {
139-
return (net_if_down(net_if_get_by_index(iface_index)) == 0);
162+
return (net_if_down(netif) == 0);
140163
}
141164

142165
// TODO: manual functions for setting IP address, subnet mask, gateway, etc.
143166
// net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
144167
// net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
145-
146-
#if defined(SPECIALIZE_FOR_ETHERNET) && DT_HAS_COMPAT_STATUS_OKAY(ethernet_phy)
147-
EthernetLinkStatus linkStatus() {
148-
if (net_if_is_up(net_if_get_by_index(iface_index))) {
149-
return LinkON;
150-
} else {
151-
return LinkOFF;
152-
}
153-
}
154-
155-
bool begin(uint8_t* mac_address, int _timeout, int _response_timeout) {
156-
return begin();
157-
}
158-
159-
bool begin(uint8_t* mac_address, IPAddress _ip, IPAddress _dns, IPAddress _gateway, IPAddress _netmask, int _timeout, int _response_timeout) {
160-
return begin();
161-
}
162-
163-
EthernetHardwareStatus hardwareStatus() {
164-
const struct device *const dev = DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(ethernet_phy));
165-
if (device_is_ready(dev)) {
166-
return EthernetOk;
167-
} else {
168-
return EthernetNoHardware;
169-
}
170-
}
171-
#endif
172-
173-
#ifdef SPECIALIZE_FOR_WIFI
174-
175-
#define NET_EVENT_WIFI_MASK \
176-
(NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
177-
NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
178-
NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED | \
179-
NET_EVENT_WIFI_SCAN_RESULT)
180-
181-
struct net_if *sta_iface;
182-
struct net_if *ap_iface;
183-
184-
struct wifi_connect_req_params ap_config;
185-
struct wifi_connect_req_params sta_config;
186-
187-
bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = false) {
188-
sta_iface = net_if_get_wifi_sta();
189-
190-
sta_config.ssid = (const uint8_t *)ssid;
191-
sta_config.ssid_length = strlen(ssid);
192-
sta_config.psk = (const uint8_t *)passphrase;
193-
sta_config.psk_length = strlen(passphrase);
194-
// TODO: change these fields with scan() results
195-
sta_config.security = WIFI_SECURITY_TYPE_PSK;
196-
sta_config.channel = WIFI_CHANNEL_ANY;
197-
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
198-
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
199-
200-
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
201-
sizeof(struct wifi_connect_req_params));
202-
if (ret) {
203-
return false;
204-
}
205-
206-
begin(false, NET_EVENT_WIFI_MASK);
207-
if (blocking) {
208-
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_AP_STA_CONNECTED, NULL, NULL, NULL, K_FOREVER);
209-
}
210-
211-
return true;
212-
}
213-
214-
int status() {
215-
struct wifi_iface_status status = { 0 };
216-
217-
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, net_if_get_by_index(iface_index), &status,
218-
sizeof(struct wifi_iface_status))) {
219-
return WL_NO_SHIELD;
220-
}
221-
222-
if (status.state >= WIFI_STATE_ASSOCIATED) {
223-
return WL_CONNECTED;
224-
} else {
225-
return WL_DISCONNECTED;
226-
}
227-
return WL_NO_SHIELD;
228-
}
229-
230-
int8_t scanNetworks() {
231-
// TODO: borrow code from mbed core for scan results handling
232-
}
233-
#endif
234168
};

Diff for: libraries/SocketWrapper/WiFi.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#include "WiFi.h"
22

3-
NetworkInterface WiFi(1);
3+
WiFiClass WiFi;

0 commit comments

Comments
 (0)