From 454cfde8161df8136a5b40170e4ed24dba501e6e Mon Sep 17 00:00:00 2001 From: Andrzej Bieniek Date: Thu, 18 Sep 2014 22:58:21 +0000 Subject: [PATCH] Add sniffer application example running on Raspberry Pi --- RPi/RF24/RF24.cpp | 14 +-- RPi/RF24/examples/extra/Makefile | 2 +- RPi/RF24/examples/extra/sniffer.cpp | 133 ++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 RPi/RF24/examples/extra/sniffer.cpp diff --git a/RPi/RF24/RF24.cpp b/RPi/RF24/RF24.cpp index 6d7709bc..618212e1 100644 --- a/RPi/RF24/RF24.cpp +++ b/RPi/RF24/RF24.cpp @@ -906,13 +906,13 @@ void RF24::openReadingPipe(uint8_t child, uint64_t address) /****************************************************************************/ -void RF24::setAddressWidth(uint8_t a_width){ - - if(a_width -= 2){ - write_register(SETUP_AW,a_width%4); - addr_width = (a_width%4) + 2; - } - +void RF24::setAddressWidth(uint8_t a_width) +{ + // 2 bytes width is described as invalid in manual, but it works and is used in sniffer example application + if (a_width >= 2 && a_width <= 5){ + write_register(SETUP_AW,a_width - 2); + addr_width = a_width; + } } /****************************************************************************/ diff --git a/RPi/RF24/examples/extra/Makefile b/RPi/RF24/examples/extra/Makefile index c313b5bb..a9263af0 100644 --- a/RPi/RF24/examples/extra/Makefile +++ b/RPi/RF24/examples/extra/Makefile @@ -18,7 +18,7 @@ CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s #CCFLAGS= # define all programs -PROGRAMS = rpi-hub scanner +PROGRAMS = rpi-hub scanner sniffer SOURCES = ${PROGRAMS:=.cpp} diff --git a/RPi/RF24/examples/extra/sniffer.cpp b/RPi/RF24/examples/extra/sniffer.cpp new file mode 100644 index 00000000..fe34333d --- /dev/null +++ b/RPi/RF24/examples/extra/sniffer.cpp @@ -0,0 +1,133 @@ +/* +Copyright (C) 2014 Andrzej Bieniek + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + + +Sniffing implementation based on +http://blog.cyberexplorer.me/2014/01/sniffing-and-decoding-nrf24l01-and.html +*/ + +#include +#include +#include + + +#define RADIO_CE_PIN (RPI_V2_GPIO_P1_22) +#define RADIO_CSN_PIN (RPI_V2_GPIO_P1_24) +RF24 radio(RADIO_CE_PIN, RADIO_CSN_PIN, BCM2835_SPI_SPEED_8MHZ); + + +void setup(int channel, uint64_t rx_addr, int rx_addr_len, int payload_size, rf24_datarate_e speed) { + radio.begin(); + radio.setDataRate(speed); + radio.setAutoAck(false); + radio.setChannel(channel); + radio.disableCRC(); + radio.setPayloadSize(payload_size); + radio.setAddressWidth(rx_addr_len); + radio.openReadingPipe(1,rx_addr); + radio.stopListening(); + radio.printDetails(); +} + +void run(int rx_addr_len) { + radio.startListening(); + while(1) { + delayMicroseconds(10); + while (radio.available()) { + char buf[rx_addr_len]; + + radio.read(buf, rx_addr_len); + for(int i=0; i 128) { + printf("error: channel %d out of range (allowed values from 0 to 128)\n", channel); + abort(); + } + break; + case 'l': + rx_addr_len = atoi(optarg); + if (rx_addr_len < 2 || rx_addr_len > 5) { + printf("error: addr lenght %d out of range (allowed values from 2 to 5\n", rx_addr_len); + } + break; + case 's': + switch(atoi(optarg)) { + case 250: + speed = RF24_250KBPS; + break; + case 1000: + speed = RF24_1MBPS; + break; + case 2000: + speed = RF24_2MBPS; + break; + default: + printf("error: uknown speed %s (allowed values in kbps: 250, 1000, 2000)\n", optarg); + abort(); + break; + } + break; + case 'p': + payload_size = atoi(optarg); + if (payload_size < 1 || payload_size > 32) { + printf("error: payload %d out of range (allowed values are from 1 to 32)\n", payload_size); + abort(); + } + break; + case 'h': + case '?': + default: + printf("%s [OPTIONS]\n\n", argv[0]); + printf(" -a ADDRESS\t- hex address value, e.g.: -a 0x1234AB\n"); + printf(" -l ADDR_LEN\t- address length in bytes\n"); + printf(" -c CHANNEL\t- RF channel from 0 to 128 \n"); + printf(" -s SPEED\t- Modulation speed in kbps: 250, 1000, 2000, e.g. -s 1000\n"); + printf(" -p PAYLOAD_LEN\t- payload length in bytes, maximum is 32\n"); + abort (); + break; + } + } + if (rx_addr >> (rx_addr_len * 8)) { + printf("error: address 0x%llx is longer than %d bytes\n", rx_addr, rx_addr_len); + abort(); + } + setup(channel, rx_addr, rx_addr_len, payload_size, speed); + run(payload_size); + return 0; +}