Skip to content

Commit

Permalink
#15: Improve sniffer performance with binary mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Jul 14, 2017
1 parent 7425b7a commit 3fa3da2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
4 changes: 4 additions & 0 deletions inc/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ bool cfgReadFP32list(ConfigField field, float list[], uint8_t length);

bool cfgWriteFP32list(ConfigField field, float list[], uint8_t length);

void cfgSetBinaryMode(bool enable);

bool cfgIsBinaryMode();

#endif
3 changes: 1 addition & 2 deletions lib/freertos/src/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -2758,7 +2758,7 @@ BaseType_t xSwitchRequired = pdFALSE;
#endif /* configUSE_APPLICATION_TASK_TAG */
/*-----------------------------------------------------------*/

void vTaskSwitchContext( void )
__attribute__((used)) void vTaskSwitchContext( void )
{
if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
{
Expand Down Expand Up @@ -4804,4 +4804,3 @@ const TickType_t xConstTickCount = xTickCount;
#ifdef FREERTOS_MODULE_TEST
#include "tasks_test_access_functions.h"
#endif

12 changes: 12 additions & 0 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,15 @@ bool cfgWriteFP32list(ConfigField field, float list[], uint8_t length) {
readData();
return true;
}

static bool binaryMode = false;

void cfgSetBinaryMode(bool enable)
{
binaryMode = enable;
}

bool cfgIsBinaryMode()
{
return binaryMode;
}
8 changes: 7 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static void main_task(void *pvParameters) {
if (lps25hTestConnection()) {
printf("[OK]\r\n");
lps25hSetEnabled(true);
} else {
} else {
printf("[FAIL] (%u)\r\n", (unsigned int)hi2c1.ErrorCode);
selftestPasses = false;
}
Expand Down Expand Up @@ -233,6 +233,10 @@ static void handleSerialInput(char ch) {
help();
configChanged = false;
break;
case 'b':
cfgSetBinaryMode(true);
configChanged = false;
break;
case '#':
productionTestsRun();
printf("System halted, reset to continue\r\n");
Expand Down Expand Up @@ -355,6 +359,8 @@ static void help() {
printf("m - List and change mode\r\n");
printf("d - reset configuration\r\n");
printf("h - This help\r\n");
printf("---- For machine only\r\n");
printf("b - Switch to binary mode (sniffer only)\r\n");
}

static StaticTask_t xMainTask;
Expand Down
28 changes: 20 additions & 8 deletions src/uwb_sniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <string.h>
#include <stdio.h>
#include <unistd.h>

#include "cfg.h"
#include "led.h"
Expand All @@ -45,21 +46,32 @@ static uint32_t twrAnchorOnEvent(dwDevice_t *dev, uwbEvent_t event)

if (event == eventPacketReceived) {
int dataLength = dwGetDataLength(dev);
dwGetReceiveTimestamp(dev, &arrival);
dwGetRawReceiveTimestamp(dev, &arrival);
dwGetData(dev, (uint8_t*)&rxPacket, dataLength);

dwNewReceive(dev);
dwSetDefaults(dev);
dwStartReceive(dev);

printf("From %02x to %02x @%02x%08x: ", rxPacket.sourceAddress[0],
rxPacket.destAddress[0],
(unsigned int) arrival.high8,
(unsigned int) arrival.low32);
for (int i=0; i<(dataLength - MAC802154_HEADER_LENGTH); i++) {
printf("%02x", rxPacket.payload[i]);
if (cfgIsBinaryMode()) {
write(STDOUT_FILENO, "\xbc", 1);
write(STDOUT_FILENO, &arrival.full, 5);
write(STDOUT_FILENO, &rxPacket.sourceAddress[0], 1);
write(STDOUT_FILENO, &rxPacket.destAddress[0], 1);
dataLength -= MAC802154_HEADER_LENGTH;
write(STDOUT_FILENO, &dataLength, 2);
write(STDOUT_FILENO, rxPacket.payload, dataLength);
write(STDOUT_FILENO, &dataLength, 2); // Length repeated for sync detection
} else {
printf("From %02x to %02x @%02x%08x: ", rxPacket.sourceAddress[0],
rxPacket.destAddress[0],
(unsigned int) arrival.high8,
(unsigned int) arrival.low32);
for (int i=0; i<(dataLength - MAC802154_HEADER_LENGTH); i++) {
printf("%02x", rxPacket.payload[i]);
}
printf("\r\n");
}
printf("\r\n");

} else {
dwNewReceive(dev);
Expand Down
39 changes: 39 additions & 0 deletions tools/sniffer/sniffer_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# UWB binary sniffer decoder
#
# The LPS node should be in sniffer mode already, it will be switched to
# binary mode by this script.

import sys
import struct
import serial
import binascii

if len(sys.argv) != 2:
print("usage: {} <sniffer serial port>".format(sys.argv[0]))
sys.exit(1)

ser = serial.Serial('/dev/ttyACM0')

# Switch to binary mode
ser.write(b'b')

while True:
c = ser.read(1)

if c == b'\xbc':
ts = ser.read(5)
ts += b'\0\0\0'
ts = struct.unpack('<Q', ts)[0]
addrFrom, addrTo = struct.unpack('<BB', ser.read(2))
length = struct.unpack('<H', ser.read(2))[0]
if length > 1024:
continue
data = ser.read(length)
l2 = struct.unpack('<H', ser.read(2))[0]
if length == l2:
print("@{:010x} from {} to {}: {}".format(ts, addrFrom, addrTo,
binascii.hexlify(data)
.decode('utf8')))
else:
print("Out of sync!")

0 comments on commit 3fa3da2

Please sign in to comment.