ZigBee/802.15.4 Sniffer

This is a simple ZigBee sniffer application which can dump raw data from any zigbee transmitter in 2.4 GHz and will work in the default channel set while compiling. The purpose is to demonstrate how to use sniffers.

Platform tested
MicaZ and TelosB ( Should work with Imote also)
Interfaces needed
Boot : To boot up the device
Leds : To indicate data capture
Receive : To capture the packet (provided by CC2420ReceiveC)
SplitControl : To control the radio
CC2420PacketBody : For reading the packet header
To run the program, assuming that you are using telosb , type in the shell/command prompt
java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSB0:telosb


SniffC.nc
----------------
#include "printf.h"
module SniffC
{
uses
{
interface Boot;
interface Leds;
interface Receive as Rx;
interface SplitControl as RadioControl;
interface CC2420PacketBody as CPacketBody;
}
}
implementation
{
event void Boot.booted()
{
call RadioControl.start();
}
event void RadioControl.startDone(error_t err)
{
}
event void RadioControl.stopDone(error_t err)
{
}
event message_t* Rx.receive(message_t*msg,void*payload,uint8_t len)
{
uint8_t i;
cc2420_header_t *h=call CPacketBody.getHeader(msg);
for(i=0;ilength;i++)
printf("%u",msg->data[i]);
printf("\n");
printfflush();
call Leds.led1Toggle();
return msg;
}
}


SniffAppC.nc
------------------
configuration SniffAppC
{
}
implementation
{
components CC2420ReceiveC as CCC, LedsC, MainC, SniffC, CC2420CsmaC as CCS,CC2420PacketC as CCP;
SniffC.Boot->MainC;
SniffC.Leds->LedsC;
SniffC.Rx->CCC;
SniffC.CPacketBody->CCP;
SniffC.RadioControl-> CCS;
}


Makefile
COMPONENT=SniffAppC
CFLAGS+= -I$(TOSDIR)/lib/printf
CFLAGS += -DCC2420_NO_ACKNOWLEDGEMENTS
CFLAGS += -DCC2420_NO_ADDRESS_RECOGNITION
CFLAGS += -DENABLE_SPI0_DMA
include $(MAKERULES)

Debugging NesC made easy with Printf library


Put watch , breakpoints ... all sort of such powerful debugging we miss when we do tinyos and nesc. I had a good struggle to find out what went wrong in a code till I figured out serialforwarder and the libraries to print values in between. This again needs many routine calls and stuffs.
To make things easier, there is a library "tinyos printf". Anyone who had used printf in C, same syntax will work. This post is on how to use tinyos printf library.
Before we begin, I assume that you are familiar with serialforwarder interface and simple blink application. Again the screenshots are done with original mote. Avrora users can do the same with micaz compilation, then follow the post using serialforwarder interface with avrora.

This is simple application which generates random numbers with Random interface and prints the value to the screen. All you need is
  1. Include printf path in Makefile
  2. Include printf.h in your application
  3. Invoke the Serialforwarder
  4. Type java net.tinyos.tools.PrintfClient and see the output.
The screenshots are given below.

The corresponding files can be found here

Related posts
Running tinyos programs using avrora
Using Serialforwarder with Avrora