Hi
I have a project:
1. a client sensor broadcast package
2. there are 5 rooms, each room has 2 sensors
3. the sensors receive package, embed the sensor Id
4. send the new package to server through multihop
5. server get the new package through Delta.
How should I change the following nc code to do it?
Code:
#include "CountMsg.h"
module RadioDemoM
{
provides interface StdControl;
uses interface Timer;
uses interface Leds;
uses interface SendMsg;
uses interface ReceiveMsg;
uses interface Button;
}
implementation
{
uint16_t m_count;
uint16_t m_lqi;
uint16_t m_rssi;
TOS_Msg m_msg;
bool m_is_sending;
uint16_t m_timeout;
uint16_t m_mode;
enum {
MODE_COUNT_RADIO,
MODE_EXCHANGE_RSSI,
MODE_EXCHANGE_LQI,
PERIOD_COUNT_RADIO = 333,
PERIOD_EXCHANGE_RSSI = 33,
PERIOD_EXCHANGE_LQI = 33,
TIMEOUT_EXCHANGE_RSSI = 100,
TIMEOUT_EXCHANGE_LQI = 100,
};
command result_t StdControl.init()
{
m_count = 0;
m_lqi = 0;
m_timeout = 0;
m_is_sending = FALSE;
m_mode = MODE_COUNT_RADIO;
return SUCCESS;
}
command result_t StdControl.start()
{
call Button.enable();
m_mode = MODE_COUNT_RADIO;
if( TOS_LOCAL_ADDRESS == 1 )
call Timer.start( TIMER_REPEAT, PERIOD_COUNT_RADIO );
return SUCCESS;
}
command result_t StdControl.stop()
{
call Timer.stop();
return SUCCESS;
}
void sendCount( uint16_t count )
{
if( m_is_sending == FALSE )
{
CountMsg_t* body = (CountMsg_t*)m_msg.data;
body->n = count;
body->src = 10000 + m_mode;
if( call SendMsg.send(TOS_BCAST_ADDR,sizeof(CountMsg_t),&m_msg) == SUCCESS )
{
m_is_sending = TRUE;
}
}
}
event result_t Timer.fired()
{
if( TOS_LOCAL_ADDRESS == 1 )
{
m_count++;
switch( m_mode )
{
case MODE_COUNT_RADIO:
call Leds.set( m_count );
break;
case MODE_EXCHANGE_RSSI:
case MODE_EXCHANGE_LQI:
if( m_timeout == 0 )
call Leds.set(0);
if( m_timeout > 0 )
m_timeout--;
break;
}
sendCount( m_count );
}
else
{
// timeout for address != 1
call Leds.set(0);
}
return SUCCESS;
}
event result_t SendMsg.sendDone(TOS_MsgPtr msg, result_t result)
{
m_is_sending = FALSE;
return SUCCESS;
}
task void processLQI()
{
uint16_t leds = 1;
if( m_lqi >= 85 ) leds |= 2;
if( m_lqi >= 106 ) leds |= 4;
call Leds.set( leds );
}
task void processRSSI()
{
uint16_t leds = 1;
if( m_rssi >= 20 ) leds |= 2; //prev 15
if( m_rssi >= 40 ) leds |= 4; //prev 35
call Leds.set( leds );
}
event TOS_MsgPtr ReceiveMsg.receive( TOS_MsgPtr msg )
{
CountMsg_t* body = (CountMsg_t*)msg->data;
if( body->src >= 10000 )
{
if( TOS_LOCAL_ADDRESS == 1 )
{
m_timeout = (TIMEOUT_EXCHANGE_LQI + PERIOD_EXCHANGE_LQI - 1) / PERIOD_EXCHANGE_LQI;
m_lqi = msg->lqi;
post processLQI();
}
else
{
m_mode = body->src - 10000;
switch( m_mode )
{
case MODE_COUNT_RADIO:
m_count = body->n;
call Leds.set( m_count );
call Timer.stop(); //stop timeout
break;
case MODE_EXCHANGE_RSSI:
call Timer.start( TIMER_ONE_SHOT, TIMEOUT_EXCHANGE_RSSI ); //timeout
m_rssi = (msg->strength + 60) & 255;
post processRSSI();
sendCount( m_count );
break;
case MODE_EXCHANGE_LQI:
call Timer.start( TIMER_ONE_SHOT, TIMEOUT_EXCHANGE_LQI ); //timeout
m_lqi = msg->lqi;
post processLQI();
sendCount( m_count );
break;
}
}
}
return msg;
}
task void switchMode()
{
switch( m_mode )
{
case MODE_COUNT_RADIO:
m_mode = MODE_EXCHANGE_RSSI;
call Timer.start( TIMER_REPEAT, PERIOD_EXCHANGE_RSSI );
break;
case MODE_EXCHANGE_RSSI:
m_mode = MODE_EXCHANGE_LQI;
call Timer.start( TIMER_REPEAT, PERIOD_EXCHANGE_LQI );
break;
case MODE_EXCHANGE_LQI:
default:
m_mode = MODE_COUNT_RADIO;
call Timer.start( TIMER_REPEAT, PERIOD_COUNT_RADIO );
break;
}
}
async event void Button.pressed(uint32_t time) {
if( TOS_LOCAL_ADDRESS == 1 )
post switchMode();
}
async event void Button.released(uint32_t time) { }
}
//----------------------------------------------------------------------------------------------
includes CountMsg;
module CountReceiveM
{
provides interface StdControl;
uses interface ReceiveMsg;
uses interface Leds;
}
implementation
{
command result_t StdControl.init()
{
call Leds.init();
return SUCCESS;
}
command result_t StdControl.start()
{
return SUCCESS;
}
command result_t StdControl.stop()
{
return SUCCESS;
}
event TOS_MsgPtr ReceiveMsg.receive( TOS_MsgPtr msg )
{
CountMsg_t* body = (CountMsg_t*)msg->data;
call Leds.set( body->n );
return msg;
}
}
//-----------------------------------------------------------------------------------------------
includes CountMsg;
module CountSendM
{
provides interface StdControl;
uses interface Timer;
uses interface SendMsg;
uses interface Leds;
}
implementation
{
TOS_Msg m_msg;
int m_int;
bool m_sending;
command result_t StdControl.init()
{
m_int = 0;
m_sending = FALSE;
call Leds.init();
return SUCCESS;
}
command result_t StdControl.start()
{
call Timer.start( TIMER_REPEAT, 200 );
return SUCCESS;
}
command result_t StdControl.stop()
{
return SUCCESS;
}
event result_t Timer.fired()
{
if( m_sending == FALSE )
{
CountMsg_t* body = (CountMsg_t*)m_msg.data;
body->n = m_int;
body->src = TOS_LOCAL_ADDRESS;
if( call SendMsg.send( TOS_BCAST_ADDR, sizeof(CountMsg_t), &m_msg ) == SUCCESS )
{
call Leds.set( m_int );
m_sending = TRUE;
}
}
m_int++;
return SUCCESS;
}
event result_t SendMsg.sendDone( TOS_MsgPtr msg, result_t success )
{
m_sending = FALSE;
return SUCCESS;
}
}
//-----------------------------------------------------------------------------------------
#include "Delta.h"
#include "circularQueue.h"
module DeltaM {
provides {
interface StdControl;
}
uses {
interface Send as SendDeltaMsg;
interface Intercept as SnoopDeltaMsg;
interface RouteControl;
interface RouteStatistics;
interface ADC;
interface Timer;
interface Timer as TimerBlink;
interface Leds;
}
}
implementation {
/************************* VARIABLES *******************************/
uint16_t m_adc;
uint32_t m_seqno;
TOS_Msg msg[DELTA_QUEUE_SIZE];
CircularQueue_t queue;
/************************* HELPER FUNCTIONS ************************/
task void sendData() {
uint16_t _length;
int i;
uint16_t neighbors[MHOP_PARENT_SIZE];
uint16_t quality[MHOP_PARENT_SIZE];
if (cqueue_pushBack( &queue ) == SUCCESS) {
DeltaMsg* dmsg = (DeltaMsg*)call SendDeltaMsg.getBuffer(&msg[queue.back], &_length);
atomic dmsg->reading = m_adc;
dmsg->parent = call RouteControl.getParent();
call RouteStatistics.getNeighbors(neighbors, MHOP_PARENT_SIZE);
call RouteStatistics.getNeighborQuality(quality, MHOP_PARENT_SIZE);
for (i = 0; i < MHOP_PARENT_SIZE; i++) {
dmsg->neighbors[i] = neighbors[i];
dmsg->quality[i] = quality[i];
}
dmsg->neighborsize = MHOP_PARENT_SIZE;
dmsg->retransmissions = call RouteStatistics.getRetransmissions();
dmsg->seqno = m_seqno;
if (call SendDeltaMsg.send( &msg[queue.back], sizeof(DeltaMsg) ) == SUCCESS) {
call Leds.redOn();
}
else {
// remove from queue
cqueue_popBack( &queue );
}
}
// always increase seqno. gives a better idea of how many packets
// really have been dropped
m_seqno++;
}
void blinkBlue() {
call Leds.yellowOn();
call TimerBlink.start(TIMER_ONE_SHOT, 20);
}
/************************* STD CONTROL *****************************/
command result_t StdControl.init() {
cqueue_init( &queue, DELTA_QUEUE_SIZE );
return SUCCESS;
}
command result_t StdControl.start() {
call Timer.start( TIMER_REPEAT, DELTA_TIME );
return SUCCESS;
}
command result_t StdControl.stop() {
return SUCCESS;
}
/************************* TIMER ***********************************/
event result_t Timer.fired() {
call ADC.getData();
return SUCCESS;
}
event result_t TimerBlink.fired() {
call Leds.yellowOff();
return SUCCESS;
}
/************************* ADC *************************************/
async event result_t ADC.dataReady(uint16_t data) {
m_adc = data;
post sendData();
return SUCCESS;
}
/************************* SEND ************************************/
event result_t SendDeltaMsg.sendDone(TOS_MsgPtr _msg, result_t _success) {
cqueue_popFront( &queue );
if (cqueue_isEmpty( &queue )) {
call Leds.redOff();
}
return SUCCESS;
}
/************************* SEND ************************************/
event result_t SnoopDeltaMsg.intercept(TOS_MsgPtr _msg, void* payload, uint16_t payloadLen) {
blinkBlue();
return SUCCESS;
}
}