Skip to content

Commit c411d38

Browse files
kapbhnordicjm
authored andcommitted
samples: raw_tx_packet: Fix beacon frame length calculation
Replace size of beacon frame with actual beacon frame length. So, It will avoid padding and pass original payload. Signed-off-by: Kapil Bhatt <[email protected]>
1 parent b757b4c commit c411d38

File tree

1 file changed

+61
-5
lines changed
  • samples/wifi/raw_tx_packet/src

1 file changed

+61
-5
lines changed

samples/wifi/raw_tx_packet/src/main.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ LOG_MODULE_REGISTER(raw_tx_packet, CONFIG_LOG_DEFAULT_LEVEL);
2525
#include "wifi_connection.h"
2626

2727
#define BEACON_PAYLOAD_LENGTH 256
28+
#define PADDING_ZERO_THRESHOLD 10
2829
#define CONTINUOUS_MODE_TRANSMISSION 0
2930
#define FIXED_MODE_TRANSMISSION 1
3031

@@ -201,12 +202,66 @@ static int setup_raw_pkt_socket(int *sockfd, struct sockaddr_ll *sa)
201202
return 0;
202203
}
203204

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+
204259
static void fill_raw_tx_pkt_hdr(struct raw_tx_pkt_header *raw_tx_pkt)
205260
{
206261
/* Raw Tx Packet header */
207262
raw_tx_pkt->magic_num = NRF_WIFI_MAGIC_NUM_RAWTX;
208263
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);
210265
raw_tx_pkt->tx_mode = CONFIG_RAW_TX_PKT_SAMPLE_RATE_FLAGS;
211266
raw_tx_pkt->queue = CONFIG_RAW_TX_PKT_SAMPLE_QUEUE_NUM;
212267
/* The byte is reserved and used by the driver */
@@ -256,6 +311,7 @@ static void wifi_send_raw_tx_packets(void)
256311
struct raw_tx_pkt_header packet;
257312
char *test_frame = NULL;
258313
unsigned int buf_length, num_pkts, transmission_mode, num_failures = 0;
314+
unsigned short beacon_frame_length = calculate_beacon_frame_length(&test_beacon_frame);
259315

260316
ret = setup_raw_pkt_socket(&sockfd, &sa);
261317
if (ret < 0) {
@@ -271,18 +327,18 @@ static void wifi_send_raw_tx_packets(void)
271327
return;
272328
}
273329

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);
275331
if (!test_frame) {
276332
LOG_ERR("Malloc failed for send buffer %d", errno);
277333
return;
278334
}
279335

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;
281337
memcpy(test_frame, &packet, sizeof(struct raw_tx_pkt_header));
282338

283339
if (num_pkts == 1) {
284340
memcpy(test_frame + sizeof(struct raw_tx_pkt_header),
285-
&test_beacon_frame, sizeof(test_beacon_frame));
341+
&test_beacon_frame, beacon_frame_length);
286342

287343
ret = wifi_send_raw_tx_pkt(sockfd, test_frame, buf_length, &sa);
288344
if (ret < 0) {
@@ -294,7 +350,7 @@ static void wifi_send_raw_tx_packets(void)
294350
} else {
295351
for (int i = 0; i < num_pkts; i++) {
296352
memcpy(test_frame + sizeof(struct raw_tx_pkt_header),
297-
&test_beacon_frame, sizeof(test_beacon_frame));
353+
&test_beacon_frame, beacon_frame_length);
298354

299355
ret = sendto(sockfd, test_frame, buf_length, 0,
300356
(struct sockaddr *)&sa, sizeof(sa));

0 commit comments

Comments
 (0)