/
WiFi_NetX_adapter.c
355 lines (296 loc) · 11.7 KB
/
WiFi_NetX_adapter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2023-05-01
// 11:38:44
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "S7V30.h"
#include "whd.h"
#include "whd_int.h"
#include "whd_wifi_api.h"
#include "whd_resource_api.h"
#include "whd_network_types.h"
#include "cyhal_hw_types.h"
#include "WiFi_Host_Driver_Port.h"
#include "WiFi_NetX_adapter.h"
extern NX_IP *wifi_sta_ip_ptr;
extern NX_IP *wifi_ap_ip_ptr;
extern whd_interface_t g_pri_ifp;
extern whd_interface_t g_sec_ifp;
/*-----------------------------------------------------------------------------------------------------
\param ip_ptr_in
\param packet_ptr
\param destination_mac_msw
\param destination_mac_lsw
\param ethertype
-----------------------------------------------------------------------------------------------------*/
static void WHD_add_ethernet_header(NX_IP *ip_ptr_in, NX_PACKET *packet_ptr, ULONG destination_mac_msw, ULONG destination_mac_lsw, USHORT ethertype)
{
ULONG *ethernet_header;
// Make space at the front of the packet buffer for the ethernet header
packet_ptr->nx_packet_prepend_ptr = packet_ptr->nx_packet_prepend_ptr - WHD_ETHERNET_SIZE;
packet_ptr->nx_packet_length = packet_ptr->nx_packet_length + WHD_ETHERNET_SIZE;
// Ensure ethernet header writing starts with 32 bit alignment
ethernet_header = (ULONG *)(packet_ptr->nx_packet_prepend_ptr - 2);
*ethernet_header = destination_mac_msw;
*(ethernet_header + 1)= destination_mac_lsw;
*(ethernet_header + 2)=(ip_ptr_in->nx_ip_arp_physical_address_msw << 16) | (ip_ptr_in->nx_ip_arp_physical_address_lsw >> 16);
*(ethernet_header + 3)=(ip_ptr_in->nx_ip_arp_physical_address_lsw << 16) | ethertype;
NX_CHANGE_ULONG_ENDIAN(*(ethernet_header));
NX_CHANGE_ULONG_ENDIAN(*(ethernet_header+1));
NX_CHANGE_ULONG_ENDIAN(*(ethernet_header+2));
NX_CHANGE_ULONG_ENDIAN(*(ethernet_header+3));
}
/*-----------------------------------------------------------------------------------------------------
\param driver
\param mac
-----------------------------------------------------------------------------------------------------*/
static VOID WHD_get_MAC_from_driver(NX_IP_DRIVER *driver, whd_mac_t *mac)
{
mac->octet[0] = (uint8_t)((driver->nx_ip_driver_physical_address_msw & 0x0000ff00)>> 8);
mac->octet[1] = (uint8_t)((driver->nx_ip_driver_physical_address_msw & 0x000000ff)>> 0);
mac->octet[2] = (uint8_t)((driver->nx_ip_driver_physical_address_lsw & 0xff000000)>> 24);
mac->octet[3] = (uint8_t)((driver->nx_ip_driver_physical_address_lsw & 0x00ff0000)>> 16);
mac->octet[4] = (uint8_t)((driver->nx_ip_driver_physical_address_lsw & 0x0000ff00)>> 8);
mac->octet[5] = (uint8_t)((driver->nx_ip_driver_physical_address_lsw & 0x000000ff)>> 0);
}
VOID WHD_NetXDuo_driver_entry(NX_IP_DRIVER *driver, NX_IP *ip, whd_interface_t ifp)
{
NX_PACKET *packet_ptr;
whd_mac_t mac;
packet_ptr = driver->nx_ip_driver_packet;
driver->nx_ip_driver_status = NX_NOT_SUCCESSFUL;
// Process commands which are valid independent of the link state
switch (driver->nx_ip_driver_command)
{
case NX_LINK_INITIALIZE:
ip->nx_ip_driver_mtu = (ULONG) WHD_PAYLOAD_MTU;
ip->nx_ip_driver_mapping_needed = (UINT) NX_TRUE;
ip->nx_ip_arp_physical_address_msw = (ULONG)((ifp->mac_addr.octet[0] << 8)+ ifp->mac_addr.octet[1]);
ip->nx_ip_arp_physical_address_lsw = (ULONG)((ifp->mac_addr.octet[2] << 24)+(ifp->mac_addr.octet[3] << 16)+(ifp->mac_addr.octet[4] << 8)+ ifp->mac_addr.octet[5]);
ip->nx_ip_driver_link_up = NX_FALSE;
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_UNINITIALIZE:
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_ENABLE:
ip->nx_ip_driver_link_up = (UINT) NX_TRUE;
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_DISABLE:
ip->nx_ip_driver_link_up = NX_FALSE;
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_MULTICAST_JOIN:
WHD_get_MAC_from_driver(driver,&mac);
if (whd_wifi_register_multicast_address(ifp,&mac) != WHD_SUCCESS)
{
driver->nx_ip_driver_status = (UINT) NX_NOT_SUCCESSFUL;
}
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_MULTICAST_LEAVE:
WHD_get_MAC_from_driver(driver,&mac);
if (whd_wifi_unregister_multicast_address(ifp,&mac) != WHD_SUCCESS)
{
driver->nx_ip_driver_status = (UINT) NX_NOT_SUCCESSFUL;
}
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_GET_STATUS:
*(driver->nx_ip_driver_return_ptr)= (ULONG) ip->nx_ip_driver_link_up; // Signal status through return pointer
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
return;
case NX_LINK_PACKET_SEND:
case NX_LINK_ARP_RESPONSE_SEND:
case NX_LINK_ARP_SEND:
case NX_LINK_RARP_SEND:
case NX_LINK_PACKET_BROADCAST:
break;
case NX_LINK_DEFERRED_PROCESSING:
default:
driver->nx_ip_driver_status = (UINT) NX_UNHANDLED_COMMAND; // Invalid driver request
break;
}
if ((ip->nx_ip_driver_link_up == NX_TRUE) && (whd_wifi_is_ready_to_transceive(ifp) == WHD_SUCCESS))
{
switch (driver->nx_ip_driver_command)
{
case NX_LINK_PACKET_SEND:
if (packet_ptr->nx_packet_ip_version == NX_IP_VERSION_V4)
{
WHD_add_ethernet_header(ip, packet_ptr, driver->nx_ip_driver_physical_address_msw, driver->nx_ip_driver_physical_address_lsw, (USHORT) WHD_ETHERTYPE_IPv4);
}
else if (packet_ptr->nx_packet_ip_version == NX_IP_VERSION_V6)
{
WHD_add_ethernet_header(ip, packet_ptr, driver->nx_ip_driver_physical_address_msw, driver->nx_ip_driver_physical_address_lsw, (USHORT) WHD_ETHERTYPE_IPv6);
}
else
{
nx_packet_release(packet_ptr);
break;
}
whd_network_send_ethernet_data(ifp, (whd_buffer_t) packet_ptr);
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_ARP_RESPONSE_SEND:
WHD_add_ethernet_header(ip, packet_ptr, driver->nx_ip_driver_physical_address_msw, driver->nx_ip_driver_physical_address_lsw, (USHORT) WHD_ETHERTYPE_ARP);
whd_network_send_ethernet_data(ifp, (whd_buffer_t) packet_ptr);
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_ARP_SEND:
WHD_add_ethernet_header(ip, packet_ptr, (ULONG) 0xFFFF, (ULONG) 0xFFFFFFFF, (USHORT) WHD_ETHERTYPE_ARP);
whd_network_send_ethernet_data(ifp, (whd_buffer_t) packet_ptr);
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_RARP_SEND:
WHD_add_ethernet_header(ip, packet_ptr, (ULONG) 0xFFFF, (ULONG) 0xFFFFFFFF, (USHORT) WHD_ETHERTYPE_RARP);
whd_network_send_ethernet_data(ifp, (whd_buffer_t) packet_ptr);
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_PACKET_BROADCAST:
if (packet_ptr->nx_packet_ip_version == NX_IP_VERSION_V4)
{
WHD_add_ethernet_header(ip, packet_ptr, (ULONG) 0xFFFF, (ULONG) 0xFFFFFFFF, (USHORT) WHD_ETHERTYPE_IPv4);
}
else if (packet_ptr->nx_packet_ip_version == NX_IP_VERSION_V6)
{
WHD_add_ethernet_header(ip, packet_ptr, (ULONG) 0xFFFF, (ULONG) 0xFFFFFFFF, (USHORT) WHD_ETHERTYPE_IPv6);
}
else
{
nx_packet_release(packet_ptr);
break;
}
whd_network_send_ethernet_data(ifp, (whd_buffer_t) packet_ptr);
driver->nx_ip_driver_status = (UINT) NX_SUCCESS;
break;
case NX_LINK_MULTICAST_JOIN:
case NX_LINK_MULTICAST_LEAVE:
case NX_LINK_INITIALIZE:
case NX_LINK_ENABLE:
case NX_LINK_DISABLE:
case NX_LINK_GET_STATUS:
case NX_LINK_DEFERRED_PROCESSING:
default:
driver->nx_ip_driver_status = NX_UNHANDLED_COMMAND;
break;
}
}
else
{
if (packet_ptr != NULL)
{
switch (driver->nx_ip_driver_command)
{
case NX_LINK_PACKET_BROADCAST:
case NX_LINK_RARP_SEND:
case NX_LINK_ARP_SEND:
case NX_LINK_ARP_RESPONSE_SEND:
case NX_LINK_PACKET_SEND:
nx_packet_release(packet_ptr);
break;
default:
break;
}
}
}
}
/*-----------------------------------------------------------------------------------------------------
Функция отправки пакетов WiFi станции в WiFi драйвер
\param driver
-----------------------------------------------------------------------------------------------------*/
VOID WHD_STA_NetXDuo_driver_entry(NX_IP_DRIVER *driver)
{
WHD_NetXDuo_driver_entry(driver, wifi_sta_ip_ptr, g_pri_ifp);
}
/*-----------------------------------------------------------------------------------------------------
Функция отправки пакетов WiFi точки доступа в WiFi драйвер
\param driver
-----------------------------------------------------------------------------------------------------*/
VOID WHD_AP_NetXDuo_driver_entry(NX_IP_DRIVER *driver)
{
if (wifi_composition == WIFI_STA_AND_AP)
{
WHD_NetXDuo_driver_entry(driver, wifi_ap_ip_ptr, g_sec_ifp);
}
else
{
WHD_NetXDuo_driver_entry(driver, wifi_ap_ip_ptr, g_pri_ifp);
}
}
/*-----------------------------------------------------------------------------------------------------
\param ifp
\param buffer
-----------------------------------------------------------------------------------------------------*/
void WHD_network_process_ethernet_data(whd_interface_t ifp, whd_buffer_t buffer)
{
NX_IP *ip;
USHORT ethertype;
NX_PACKET *packet_ptr = (NX_PACKET *) buffer;
unsigned char *buff = packet_ptr->nx_packet_prepend_ptr;
ethertype = (USHORT)(buff[12] << 8 | buff[13]);
// Check if this is an 802.1Q VLAN tagged packet
if (ethertype == WHD_ETHERTYPE_8021Q)
{
// Need to remove the 4 octet VLAN Tag, by moving src and dest addresses 4 octets to the right, and then read the actual ethertype.
// The VLAN ID and priority fields are currently ignored.
//
uint8_t temp_buffer[12];
memcpy(temp_buffer, packet_ptr->nx_packet_prepend_ptr, 12);
memcpy(packet_ptr->nx_packet_prepend_ptr + 4, temp_buffer, 12);
packet_ptr->nx_packet_prepend_ptr = packet_ptr->nx_packet_prepend_ptr + 4;
packet_ptr->nx_packet_length = packet_ptr->nx_packet_length - 4;
buff = packet_ptr->nx_packet_prepend_ptr;
ethertype = (USHORT)(buff[12] << 8 | buff[13]);
}
if (wifi_composition == WIFI_STA_AND_AP)
{
if (ifp == g_pri_ifp)
{
ip = wifi_sta_ip_ptr;
}
else if (ifp == g_sec_ifp)
{
ip = wifi_ap_ip_ptr;
}
else
{
nx_packet_release(packet_ptr);
return;
}
}
else if (wifi_composition == WIFI_ONLY_STA)
{
// Здесь интерфейс можеть быть только pri_ifp и IP может быть только wifi_sta_ip_ptr
ip = wifi_sta_ip_ptr;
}
else if (wifi_composition == WIFI_ONLY_AP)
{
// Здесь интерфейс можеть быть только pri_ifp и IP может быть только wifi_ap_ip_ptr
ip = wifi_ap_ip_ptr;
}
else
{
nx_packet_release(packet_ptr);
return;
}
// Remove the ethernet header, so packet is ready for reading by NetX
packet_ptr->nx_packet_prepend_ptr = packet_ptr->nx_packet_prepend_ptr + WHD_ETHERNET_SIZE;
packet_ptr->nx_packet_length = packet_ptr->nx_packet_length - WHD_ETHERNET_SIZE;
if ((ethertype == WHD_ETHERTYPE_IPv4) || ethertype == WHD_ETHERTYPE_IPv6)
{
_nx_ip_packet_deferred_receive(ip, packet_ptr);
}
else if (ethertype == WHD_ETHERTYPE_ARP)
{
_nx_arp_packet_deferred_receive(ip, packet_ptr);
}
else if (ethertype == WHD_ETHERTYPE_RARP)
{
_nx_rarp_packet_deferred_receive(ip, packet_ptr);
}
else
{
nx_packet_release(packet_ptr);
}
}