@@ -25,6 +25,7 @@ LOG_MODULE_REGISTER(raw_tx_packet, CONFIG_LOG_DEFAULT_LEVEL);
25
25
#include "wifi_connection.h"
26
26
27
27
#define BEACON_PAYLOAD_LENGTH 256
28
+ #define PADDING_ZERO_THRESHOLD 10
28
29
#define CONTINUOUS_MODE_TRANSMISSION 0
29
30
#define FIXED_MODE_TRANSMISSION 1
30
31
@@ -201,12 +202,66 @@ static int setup_raw_pkt_socket(int *sockfd, struct sockaddr_ll *sa)
201
202
return 0 ;
202
203
}
203
204
205
+ static unsigned short calculate_beacon_payload_length (const uint8_t * payload ,
206
+ unsigned short max_length )
207
+ {
208
+ /* Skip fixed parameters: timestamp(8) + beacon_interval(2) + capability(2) */
209
+ unsigned short pos = 12 ;
210
+ unsigned short actual_length = pos ;
211
+
212
+ while (pos < max_length ) {
213
+ if ((pos + 1 < max_length ) && (payload [pos ] == 0 ) && (payload [pos + 1 ] == 0 )) {
214
+ unsigned short zero_count = 0 ;
215
+ unsigned short check_pos = pos ;
216
+
217
+ while (check_pos < max_length && payload [check_pos ] == 0 ) {
218
+ zero_count ++ ;
219
+ check_pos ++ ;
220
+ }
221
+
222
+ if (zero_count >= PADDING_ZERO_THRESHOLD ) {
223
+ break ;
224
+ }
225
+ }
226
+
227
+ if (pos + 1 >= max_length ) {
228
+ break ;
229
+ }
230
+
231
+ uint8_t length = payload [pos + 1 ];
232
+
233
+ actual_length = pos + 2 + length ;
234
+
235
+ pos += 2 + length ;
236
+
237
+ if (pos >= max_length ) {
238
+ break ;
239
+ }
240
+ }
241
+ return actual_length ;
242
+ }
243
+
244
+ static unsigned short calculate_beacon_frame_length (const struct beacon * beacon_frame )
245
+ {
246
+ unsigned short header_length = sizeof (beacon_frame -> frame_control ) +
247
+ sizeof (beacon_frame -> duration ) +
248
+ sizeof (beacon_frame -> da ) +
249
+ sizeof (beacon_frame -> sa ) +
250
+ sizeof (beacon_frame -> bssid ) +
251
+ sizeof (beacon_frame -> seq_ctrl );
252
+
253
+ unsigned short payload_length = calculate_beacon_payload_length (beacon_frame -> payload ,
254
+ BEACON_PAYLOAD_LENGTH );
255
+
256
+ return header_length + payload_length ;
257
+ }
258
+
204
259
static void fill_raw_tx_pkt_hdr (struct raw_tx_pkt_header * raw_tx_pkt )
205
260
{
206
261
/* Raw Tx Packet header */
207
262
raw_tx_pkt -> magic_num = NRF_WIFI_MAGIC_NUM_RAWTX ;
208
263
raw_tx_pkt -> data_rate = CONFIG_RAW_TX_PKT_SAMPLE_RATE_VALUE ;
209
- raw_tx_pkt -> packet_length = sizeof ( test_beacon_frame );
264
+ raw_tx_pkt -> packet_length = calculate_beacon_frame_length ( & test_beacon_frame );
210
265
raw_tx_pkt -> tx_mode = CONFIG_RAW_TX_PKT_SAMPLE_RATE_FLAGS ;
211
266
raw_tx_pkt -> queue = CONFIG_RAW_TX_PKT_SAMPLE_QUEUE_NUM ;
212
267
/* The byte is reserved and used by the driver */
@@ -256,6 +311,7 @@ static void wifi_send_raw_tx_packets(void)
256
311
struct raw_tx_pkt_header packet ;
257
312
char * test_frame = NULL ;
258
313
unsigned int buf_length , num_pkts , transmission_mode , num_failures = 0 ;
314
+ unsigned short beacon_frame_length = calculate_beacon_frame_length (& test_beacon_frame );
259
315
260
316
ret = setup_raw_pkt_socket (& sockfd , & sa );
261
317
if (ret < 0 ) {
@@ -271,18 +327,18 @@ static void wifi_send_raw_tx_packets(void)
271
327
return ;
272
328
}
273
329
274
- test_frame = malloc (sizeof (struct raw_tx_pkt_header ) + sizeof ( test_beacon_frame ) );
330
+ test_frame = malloc (sizeof (struct raw_tx_pkt_header ) + beacon_frame_length );
275
331
if (!test_frame ) {
276
332
LOG_ERR ("Malloc failed for send buffer %d" , errno );
277
333
return ;
278
334
}
279
335
280
- buf_length = sizeof (struct raw_tx_pkt_header ) + sizeof ( test_beacon_frame ) ;
336
+ buf_length = sizeof (struct raw_tx_pkt_header ) + beacon_frame_length ;
281
337
memcpy (test_frame , & packet , sizeof (struct raw_tx_pkt_header ));
282
338
283
339
if (num_pkts == 1 ) {
284
340
memcpy (test_frame + sizeof (struct raw_tx_pkt_header ),
285
- & test_beacon_frame , sizeof ( test_beacon_frame ) );
341
+ & test_beacon_frame , beacon_frame_length );
286
342
287
343
ret = wifi_send_raw_tx_pkt (sockfd , test_frame , buf_length , & sa );
288
344
if (ret < 0 ) {
@@ -294,7 +350,7 @@ static void wifi_send_raw_tx_packets(void)
294
350
} else {
295
351
for (int i = 0 ; i < num_pkts ; i ++ ) {
296
352
memcpy (test_frame + sizeof (struct raw_tx_pkt_header ),
297
- & test_beacon_frame , sizeof ( test_beacon_frame ) );
353
+ & test_beacon_frame , beacon_frame_length );
298
354
299
355
ret = sendto (sockfd , test_frame , buf_length , 0 ,
300
356
(struct sockaddr * )& sa , sizeof (sa ));
0 commit comments