1
1
#pragma once
2
2
3
- #include " zephyr/net/dhcpv4.h"
3
+ #include < zephyr/net/dhcpv4.h>
4
+ #include < zephyr/net/dhcpv4_server.h>
4
5
#include < cstddef>
5
6
#include < zephyr/kernel.h>
6
7
#include < zephyr/linker/sections.h>
18
19
19
20
#undef LOG_INF
20
21
#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 (...)
39
24
40
25
class NetworkInterface {
41
26
private:
42
- int iface_index = -1 ;
43
27
44
28
uint8_t ntp_server[4 ];
45
29
static struct net_mgmt_event_callback mgmt_cb;
@@ -91,6 +75,9 @@ class NetworkInterface {
91
75
net_addr_ntop (AF_INET, cb->data , buf, sizeof (buf)));
92
76
}
93
77
78
+ protected:
79
+
80
+ struct net_if *netif = nullptr ;
94
81
int dhcp ()
95
82
{
96
83
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 {
102
89
103
90
net_dhcpv4_add_option_callback (&dhcp_cb);
104
91
105
- net_dhcpv4_start (net_if_get_by_index (iface_index) );
92
+ net_dhcpv4_start (netif );
106
93
107
94
return 0 ;
108
95
}
109
96
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
+
110
133
public:
111
- NetworkInterface (int iface_index) : iface_index(iface_index ) {}
134
+ NetworkInterface () {}
112
135
~NetworkInterface () {}
113
136
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 );
115
138
}
116
139
117
140
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 );
119
142
}
120
143
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 );
122
145
}
123
146
IPAddress dnsServerIP () {
124
147
return arduino::INADDR_NONE;
@@ -130,105 +153,16 @@ class NetworkInterface {
130
153
131
154
bool begin (bool blocking = true , uint32_t additional_event_mask = 0 ) {
132
155
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,
134
157
NULL , NULL , NULL , blocking ? K_FOREVER : K_SECONDS (1 ));
135
158
return (ret == 0 );
136
159
}
137
160
138
161
bool disconnect () {
139
- return (net_if_down (net_if_get_by_index (iface_index) ) == 0 );
162
+ return (net_if_down (netif ) == 0 );
140
163
}
141
164
142
165
// TODO: manual functions for setting IP address, subnet mask, gateway, etc.
143
166
// net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
144
167
// 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
234
168
};
0 commit comments