updated radio driver, simplified code
This commit is contained in:
parent
0cb572bcb6
commit
07753fb01f
3 changed files with 217 additions and 323 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -26,247 +26,236 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: cooja-radio.c,v 1.10 2009/04/16 14:38:41 fros4943 Exp $
|
* $Id: cooja-radio.c,v 1.11 2010/03/09 08:11:05 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "contiki.h"
|
#include "contiki.h"
|
||||||
|
|
||||||
|
#include "sys/cooja_mt.h"
|
||||||
|
#include "lib/simEnvChange.h"
|
||||||
|
|
||||||
|
#include "net/rime/packetbuf.h"
|
||||||
|
#include "net/rime/rimestats.h"
|
||||||
|
#include "net/netstack.h"
|
||||||
|
|
||||||
#include "dev/radio.h"
|
#include "dev/radio.h"
|
||||||
#include "dev/cooja-radio.h"
|
#include "dev/cooja-radio.h"
|
||||||
#include "lib/simEnvChange.h"
|
|
||||||
#include "sys/cooja_mt.h"
|
|
||||||
|
|
||||||
#define USING_CCA 1
|
#define COOJA_RADIO_BUFSIZE PACKETBUF_SIZE
|
||||||
#define USING_CCA_BUSYWAIT 1
|
|
||||||
#define CCA_BUSYWAIT_MS 100
|
|
||||||
#define CCA_SS_THRESHOLD -95
|
#define CCA_SS_THRESHOLD -95
|
||||||
|
|
||||||
#define SS_NOTHING -100
|
|
||||||
|
|
||||||
const struct simInterface radio_interface;
|
const struct simInterface radio_interface;
|
||||||
|
|
||||||
// COOJA variables
|
/* COOJA */
|
||||||
char simTransmitting = 0;
|
|
||||||
char simReceiving = 0;
|
char simReceiving = 0;
|
||||||
|
|
||||||
char simInDataBuffer[COOJA_RADIO_BUFSIZE];
|
char simInDataBuffer[COOJA_RADIO_BUFSIZE];
|
||||||
int simInSize = 0;
|
int simInSize = 0;
|
||||||
char simInPolled = 0;
|
|
||||||
char simOutDataBuffer[COOJA_RADIO_BUFSIZE];
|
char simOutDataBuffer[COOJA_RADIO_BUFSIZE];
|
||||||
int simOutSize = 0;
|
int simOutSize = 0;
|
||||||
|
|
||||||
char simRadioHWOn = 1;
|
char simRadioHWOn = 1;
|
||||||
int simSignalStrength = SS_NOTHING;
|
int simSignalStrength = -100;
|
||||||
int simLastSignalStrength = SS_NOTHING;
|
int simLastSignalStrength = -100;
|
||||||
char simPower = 100;
|
char simPower = 100;
|
||||||
int simRadioChannel = 26;
|
int simRadioChannel = 26;
|
||||||
int inSendFunction = 0;
|
|
||||||
|
|
||||||
static void (* receiver_callback)(const struct radio_driver *);
|
static const void *pending_data;
|
||||||
|
|
||||||
const struct radio_driver cooja_radio =
|
PROCESS(cooja_radio_process, "cooja radio process");
|
||||||
{
|
|
||||||
radio_send,
|
|
||||||
radio_read,
|
|
||||||
radio_set_receiver,
|
|
||||||
radio_on,
|
|
||||||
radio_off,
|
|
||||||
};
|
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
void
|
|
||||||
radio_set_receiver(void (* recv)(const struct radio_driver *))
|
|
||||||
{
|
|
||||||
receiver_callback = recv;
|
|
||||||
}
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
int
|
|
||||||
radio_on(void)
|
|
||||||
{
|
|
||||||
simRadioHWOn = 1;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
int
|
|
||||||
radio_off(void)
|
|
||||||
{
|
|
||||||
simRadioHWOn = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
radio_set_channel(int channel)
|
radio_set_channel(int channel)
|
||||||
{
|
{
|
||||||
simRadioChannel = channel;
|
simRadioChannel = channel;
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
int
|
|
||||||
radio_sstrength(void)
|
|
||||||
{
|
|
||||||
return simLastSignalStrength;
|
|
||||||
}
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
int
|
|
||||||
radio_current_sstrength(void)
|
|
||||||
{
|
|
||||||
return simSignalStrength;
|
|
||||||
}
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
void
|
void
|
||||||
radio_set_txpower(unsigned char power)
|
radio_set_txpower(unsigned char power)
|
||||||
{
|
{
|
||||||
/* 1 - 100: Number indicating output power */
|
/* 1 - 100: Number indicating output power */
|
||||||
simPower = power;
|
simPower = power;
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
int
|
||||||
doInterfaceActionsBeforeTick(void)
|
radio_signal_strength_last(void)
|
||||||
{
|
{
|
||||||
// If radio is turned off, do nothing
|
return simLastSignalStrength;
|
||||||
if (!simRadioHWOn) {
|
|
||||||
simInSize = 0;
|
|
||||||
simInPolled = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't fall asleep while receiving (in main file)
|
|
||||||
if (simReceiving) {
|
|
||||||
simLastSignalStrength = simSignalStrength;
|
|
||||||
simDontFallAsleep = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no incoming radio data, do nothing
|
|
||||||
if (simInSize == 0) {
|
|
||||||
simInPolled = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check size of received packet
|
|
||||||
if (simInSize > COOJA_RADIO_BUFSIZE) {
|
|
||||||
// Drop packet by not delivering
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ** Good place to add explicit manchester/gcr-encoding
|
|
||||||
|
|
||||||
if(receiver_callback != NULL && !simInPolled) {
|
|
||||||
simDoReceiverCallback = 1;
|
|
||||||
simInPolled = 1;
|
|
||||||
} else {
|
|
||||||
simInPolled = 0;
|
|
||||||
simDontFallAsleep = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
int
|
int
|
||||||
radio_read(void *buf, unsigned short bufsize)
|
radio_signal_strength_current(void)
|
||||||
{
|
{
|
||||||
int tmpInSize = simInSize;
|
return simSignalStrength;
|
||||||
|
|
||||||
if( bufsize < simInSize ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(simInSize > 0) {
|
|
||||||
memcpy(buf, simInDataBuffer, simInSize);
|
|
||||||
simInSize = 0;
|
|
||||||
return tmpInSize;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
radio_on(void)
|
||||||
|
{
|
||||||
|
simRadioHWOn = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
radio_off(void)
|
||||||
|
{
|
||||||
|
simRadioHWOn = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static void
|
||||||
|
doInterfaceActionsBeforeTick(void)
|
||||||
|
{
|
||||||
|
if (!simRadioHWOn) {
|
||||||
|
simInSize = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (simReceiving) {
|
||||||
|
simLastSignalStrength = simSignalStrength;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (simInSize > 0) {
|
||||||
|
process_poll(&cooja_radio_process);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
doInterfaceActionsAfterTick(void)
|
doInterfaceActionsAfterTick(void)
|
||||||
{
|
{
|
||||||
// Make sure we are awake during radio activity
|
|
||||||
if (simReceiving || simTransmitting) {
|
|
||||||
simDontFallAsleep = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
int
|
static int
|
||||||
|
radio_read(void *buf, unsigned short bufsize)
|
||||||
|
{
|
||||||
|
int tmp = simInSize;
|
||||||
|
|
||||||
|
if (simInSize == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(bufsize < simInSize) {
|
||||||
|
simInSize = 0; /* rx flush */
|
||||||
|
RIMESTATS_ADD(toolong);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(buf, simInDataBuffer, simInSize);
|
||||||
|
simInSize = 0;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
radio_send(const void *payload, unsigned short payload_len)
|
radio_send(const void *payload, unsigned short payload_len)
|
||||||
{
|
{
|
||||||
/* If radio already actively transmitting, drop packet*/
|
|
||||||
if(inSendFunction) {
|
|
||||||
return COOJA_RADIO_DROPPED;
|
|
||||||
}
|
|
||||||
|
|
||||||
inSendFunction = 1;
|
|
||||||
|
|
||||||
/* If radio is turned off, do nothing */
|
|
||||||
if(!simRadioHWOn) {
|
if(!simRadioHWOn) {
|
||||||
inSendFunction = 0;
|
/* TODO Turn on radio temporarily during tx */
|
||||||
return COOJA_RADIO_DROPPED;
|
return RADIO_TX_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Drop packet if data size too large */
|
|
||||||
if(payload_len > COOJA_RADIO_BUFSIZE) {
|
if(payload_len > COOJA_RADIO_BUFSIZE) {
|
||||||
inSendFunction = 0;
|
return RADIO_TX_ERR;
|
||||||
return COOJA_RADIO_TOOLARGE;
|
|
||||||
}
|
}
|
||||||
|
if(payload_len == 0) {
|
||||||
/* Drop packet if no data length */
|
return RADIO_TX_ERR;
|
||||||
if(payload_len <= 0) {
|
}
|
||||||
inSendFunction = 0;
|
if(simOutSize > 0) {
|
||||||
return COOJA_RADIO_ZEROLEN;
|
return RADIO_TX_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy packet data to temporary storage */
|
/* Copy packet data to temporary storage */
|
||||||
memcpy(simOutDataBuffer, payload, payload_len);
|
memcpy(simOutDataBuffer, payload, payload_len);
|
||||||
simOutSize = payload_len;
|
simOutSize = payload_len;
|
||||||
|
|
||||||
#if USING_CCA_BUSYWAIT
|
/* Transmit */
|
||||||
/* Busy-wait until both radio HW and ether is ready */
|
while(simOutSize > 0) {
|
||||||
{
|
|
||||||
int retries = 0;
|
|
||||||
while(retries < CCA_BUSYWAIT_MS && !simNoYield &&
|
|
||||||
(simSignalStrength > CCA_SS_THRESHOLD || simReceiving)) {
|
|
||||||
retries++;
|
|
||||||
cooja_mt_yield();
|
|
||||||
if(!(simSignalStrength > CCA_SS_THRESHOLD || simReceiving)) {
|
|
||||||
/* Wait one extra tick before transmission starts */
|
|
||||||
cooja_mt_yield();
|
cooja_mt_yield();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif /* USING_CCA_BUSYWAIT */
|
|
||||||
|
|
||||||
#if USING_CCA
|
return RADIO_TX_OK;
|
||||||
if(simSignalStrength > CCA_SS_THRESHOLD || simReceiving) {
|
|
||||||
inSendFunction = 0;
|
|
||||||
return COOJA_RADIO_DROPPED;
|
|
||||||
}
|
|
||||||
#endif /* USING_CCA */
|
|
||||||
|
|
||||||
if(simOutSize <= 0) {
|
|
||||||
inSendFunction = 0;
|
|
||||||
return COOJA_RADIO_DROPPED;
|
|
||||||
}
|
|
||||||
|
|
||||||
// - Initiate transmission -
|
|
||||||
simTransmitting = 1;
|
|
||||||
|
|
||||||
// Busy-wait while transmitting
|
|
||||||
while(simTransmitting && !simNoYield) {
|
|
||||||
cooja_mt_yield();
|
|
||||||
}
|
|
||||||
if (simTransmitting) {
|
|
||||||
simDontFallAsleep = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
inSendFunction = 0;
|
|
||||||
return COOJA_RADIO_OK;
|
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void radio_call_receiver()
|
static int
|
||||||
|
prepare_packet(const void *data, unsigned short len)
|
||||||
{
|
{
|
||||||
receiver_callback(&cooja_radio);
|
pending_data = data;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
transmit_packet(unsigned short len)
|
||||||
|
{
|
||||||
|
int ret = RADIO_TX_ERR;
|
||||||
|
if(pending_data != NULL) {
|
||||||
|
ret = radio_send(pending_data, len);
|
||||||
|
pending_data = NULL;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
receiving_packet(void)
|
||||||
|
{
|
||||||
|
return simReceiving;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
pending_packet(void)
|
||||||
|
{
|
||||||
|
return !simReceiving && simInSize > 0;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
cca(void)
|
||||||
|
{
|
||||||
|
if(simSignalStrength > CCA_SS_THRESHOLD) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
PROCESS_THREAD(cooja_radio_process, ev, data)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
PROCESS_BEGIN();
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
|
||||||
|
|
||||||
|
packetbuf_clear();
|
||||||
|
len = radio_read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
||||||
|
if(len > 0) {
|
||||||
|
packetbuf_set_datalen(len);
|
||||||
|
NETSTACK_RDC.input();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PROCESS_END();
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
static int
|
||||||
|
init(void)
|
||||||
|
{
|
||||||
|
process_start(&cooja_radio_process, NULL);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
const struct radio_driver cooja_radio_driver =
|
||||||
|
{
|
||||||
|
init,
|
||||||
|
prepare_packet,
|
||||||
|
transmit_packet,
|
||||||
|
radio_send,
|
||||||
|
radio_read,
|
||||||
|
cca,
|
||||||
|
receiving_packet,
|
||||||
|
pending_packet,
|
||||||
|
radio_on,
|
||||||
|
radio_off,
|
||||||
|
};
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
SIM_INTERFACE(radio_interface,
|
SIM_INTERFACE(radio_interface,
|
||||||
doInterfaceActionsBeforeTick,
|
doInterfaceActionsBeforeTick,
|
||||||
doInterfaceActionsAfterTick);
|
doInterfaceActionsAfterTick);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -28,45 +28,16 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: cooja-radio.h,v 1.5 2009/04/01 13:44:34 fros4943 Exp $
|
* $Id: cooja-radio.h,v 1.6 2010/03/09 08:11:05 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __COOJA_RADIO_H__
|
#ifndef __COOJA_RADIO_H__
|
||||||
#define __COOJA_RADIO_H__
|
#define __COOJA_RADIO_H__
|
||||||
|
|
||||||
#include "contiki.h"
|
#include "contiki.h"
|
||||||
#include "contiki-conf.h"
|
|
||||||
|
|
||||||
#include "dev/radio.h"
|
#include "dev/radio.h"
|
||||||
|
|
||||||
#include "net/uip.h"
|
extern const struct radio_driver cooja_radio_driver;
|
||||||
#include "net/uip-fw.h"
|
|
||||||
|
|
||||||
#define COOJA_RADIO_BUFSIZE UIP_BUFSIZE
|
|
||||||
#define COOJA_RADIO_DROPPED UIP_FW_DROPPED
|
|
||||||
#define COOJA_RADIO_TOOLARGE UIP_FW_TOOLARGE
|
|
||||||
#define COOJA_RADIO_ZEROLEN UIP_FW_ZEROLEN
|
|
||||||
#define COOJA_RADIO_OK UIP_FW_OK
|
|
||||||
|
|
||||||
extern const struct radio_driver cooja_radio;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Turn radio hardware on.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
radio_on(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Turn radio hardware off.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
radio_off(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set radio receiver.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
radio_set_receiver(void (* recv)(const struct radio_driver *));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set radio channel.
|
* Set radio channel.
|
||||||
|
@ -77,45 +48,21 @@ radio_set_channel(int channel);
|
||||||
/**
|
/**
|
||||||
* Set transmission power of transceiver.
|
* Set transmission power of transceiver.
|
||||||
*
|
*
|
||||||
* \param p The power of the tranceiver, between 1 (lowest) and 100
|
* \param p The power of the transceiver, between 1 and 100.
|
||||||
* (highest).
|
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
radio_set_txpower(unsigned char p);
|
radio_set_txpower(unsigned char p);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a packet from the given buffer with the given length.
|
* The signal strength of the last received packet
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
radio_send(const void *payload, unsigned short payload_len);
|
radio_signal_strength_last(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if an incoming packet has been received.
|
* This current signal strength.
|
||||||
*
|
|
||||||
* This function checks the receive buffer to see if an entire packet
|
|
||||||
* has been received.
|
|
||||||
*
|
|
||||||
* \return The length of the received packet, or 0 if no packet has
|
|
||||||
* been received.
|
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
radio_read(void *buf, unsigned short bufsize);
|
radio_signal_strength_current(void);
|
||||||
|
|
||||||
/**
|
|
||||||
* This function returns the signal strength of the last
|
|
||||||
* received packet. This function typically is called when a packet
|
|
||||||
* has been received.
|
|
||||||
*/
|
|
||||||
int radio_sstrength(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function measures and returns the signal strength.
|
|
||||||
*/
|
|
||||||
int radio_current_sstrength(void);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal COOJA function, calls the current receiver function.
|
|
||||||
*/
|
|
||||||
void radio_call_receiver(void);
|
|
||||||
|
|
||||||
#endif /* __COOJA_RADIO_H__ */
|
#endif /* __COOJA_RADIO_H__ */
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: ContikiRadio.java,v 1.31 2010/02/05 09:07:13 fros4943 Exp $
|
* $Id: ContikiRadio.java,v 1.32 2010/03/09 08:11:05 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.cooja.contikimote.interfaces;
|
package se.sics.cooja.contikimote.interfaces;
|
||||||
|
@ -66,7 +66,6 @@ import se.sics.cooja.radiomediums.UDGM;
|
||||||
*
|
*
|
||||||
* Contiki variables:
|
* Contiki variables:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>char simTransmitting (1=mote radio is transmitting)
|
|
||||||
* <li>char simReceiving (1=mote radio is receiving)
|
* <li>char simReceiving (1=mote radio is receiving)
|
||||||
* <li>char simInPolled
|
* <li>char simInPolled
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -174,7 +173,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isReceiving() {
|
public boolean isReceiving() {
|
||||||
return isLockedAtReceiving();
|
return myMoteMemory.getByteValueOf("simReceiving") == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInterfered() {
|
public boolean isInterfered() {
|
||||||
|
@ -191,45 +190,31 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
interfereAnyReception();
|
interfereAnyReception();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lockInReceivingMode();
|
|
||||||
|
myMoteMemory.setByteValueOf("simReceiving", (byte) 1);
|
||||||
|
mote.requestImmediateWakeup();
|
||||||
|
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
lastEvent = RadioEvent.RECEPTION_STARTED;
|
lastEvent = RadioEvent.RECEPTION_STARTED;
|
||||||
|
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void signalReceptionEnd() {
|
public void signalReceptionEnd() {
|
||||||
if (isInterfered() || packetToMote == null) {
|
if (isInterfered || packetToMote == null) {
|
||||||
// Reset interfered flag
|
|
||||||
isInterfered = false;
|
isInterfered = false;
|
||||||
|
|
||||||
// Reset data
|
|
||||||
packetToMote = null;
|
packetToMote = null;
|
||||||
myMoteMemory.setIntValueOf("simInSize", 0);
|
myMoteMemory.setIntValueOf("simInSize", 0);
|
||||||
|
} else {
|
||||||
// Unlock (if locked)
|
|
||||||
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
|
|
||||||
|
|
||||||
mote.requestImmediateWakeup();
|
|
||||||
|
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
|
||||||
lastEvent = RadioEvent.RECEPTION_FINISHED;
|
|
||||||
this.setChanged();
|
|
||||||
this.notifyObservers();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlock (if locked)
|
|
||||||
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
|
|
||||||
|
|
||||||
// Set data
|
|
||||||
myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length);
|
myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length);
|
||||||
myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData());
|
myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData());
|
||||||
|
}
|
||||||
|
|
||||||
|
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
|
||||||
|
mote.requestImmediateWakeup();
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
lastEvent = RadioEvent.RECEPTION_FINISHED;
|
lastEvent = RadioEvent.RECEPTION_FINISHED;
|
||||||
mote.requestImmediateWakeup();
|
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
}
|
}
|
||||||
|
@ -239,7 +224,10 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interfereAnyReception() {
|
public void interfereAnyReception() {
|
||||||
if (!isInterfered()) {
|
if (isInterfered()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
isInterfered = true;
|
isInterfered = true;
|
||||||
|
|
||||||
lastEvent = RadioEvent.RECEPTION_INTERFERED;
|
lastEvent = RadioEvent.RECEPTION_INTERFERED;
|
||||||
|
@ -247,10 +235,9 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public double getCurrentOutputPower() {
|
public double getCurrentOutputPower() {
|
||||||
// TODO Implement method
|
/* TODO Implement method */
|
||||||
logger.warn("Not implemented, always returning 0 dBm");
|
logger.warn("Not implemented, always returning 0 dBm");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -275,43 +262,16 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
return mote.getInterfaces().getPosition();
|
return mote.getInterfaces().getPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return True if locked at transmitting
|
|
||||||
*/
|
|
||||||
private boolean isLockedAtTransmitting() {
|
|
||||||
return myMoteMemory.getByteValueOf("simTransmitting") == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return True if locked at receiving
|
|
||||||
*/
|
|
||||||
private boolean isLockedAtReceiving() {
|
|
||||||
return myMoteMemory.getByteValueOf("simReceiving") == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Locks underlying Contiki system in receiving mode. This may, but does not
|
|
||||||
* have to, be used during a simulated data transfer that takes longer than
|
|
||||||
* one tick to complete. The system is unlocked by delivering the received
|
|
||||||
* data to the mote.
|
|
||||||
*/
|
|
||||||
private void lockInReceivingMode() {
|
|
||||||
mote.requestImmediateWakeup();
|
|
||||||
|
|
||||||
// Lock core radio in receiving loop
|
|
||||||
myMoteMemory.setByteValueOf("simReceiving", (byte) 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void doActionsAfterTick() {
|
public void doActionsAfterTick() {
|
||||||
|
long now = mote.getSimulation().getSimulationTime();
|
||||||
|
|
||||||
/* Check if radio hardware status changed */
|
/* Check if radio hardware status changed */
|
||||||
if (radioOn != (myMoteMemory.getByteValueOf("simRadioHWOn") == 1)) {
|
if (radioOn != (myMoteMemory.getByteValueOf("simRadioHWOn") == 1)) {
|
||||||
radioOn = !radioOn;
|
radioOn = !radioOn;
|
||||||
|
|
||||||
if (!radioOn) {
|
if (!radioOn) {
|
||||||
// Reset status
|
|
||||||
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
|
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
|
||||||
myMoteMemory.setIntValueOf("simInSize", 0);
|
myMoteMemory.setIntValueOf("simInSize", 0);
|
||||||
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
|
|
||||||
myMoteMemory.setIntValueOf("simOutSize", 0);
|
myMoteMemory.setIntValueOf("simOutSize", 0);
|
||||||
isTransmitting = false;
|
isTransmitting = false;
|
||||||
lastEvent = RadioEvent.HW_OFF;
|
lastEvent = RadioEvent.HW_OFF;
|
||||||
|
@ -319,7 +279,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
lastEvent = RadioEvent.HW_ON;
|
lastEvent = RadioEvent.HW_ON;
|
||||||
}
|
}
|
||||||
|
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
lastEventTime = now;
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
}
|
}
|
||||||
|
@ -327,7 +287,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if radio output power changed
|
/* Check if radio output power changed */
|
||||||
if (myMoteMemory.getByteValueOf("simPower") != oldOutputPowerIndicator) {
|
if (myMoteMemory.getByteValueOf("simPower") != oldOutputPowerIndicator) {
|
||||||
oldOutputPowerIndicator = myMoteMemory.getByteValueOf("simPower");
|
oldOutputPowerIndicator = myMoteMemory.getByteValueOf("simPower");
|
||||||
lastEvent = RadioEvent.UNKNOWN;
|
lastEvent = RadioEvent.UNKNOWN;
|
||||||
|
@ -335,34 +295,28 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Are we transmitting but should stop? */
|
/* Ongoing transmission */
|
||||||
/* TODO Use time events */
|
if (isTransmitting && now >= transmissionEndTime) {
|
||||||
if (isTransmitting
|
|
||||||
&& mote.getSimulation().getSimulationTime() >= transmissionEndTime) {
|
|
||||||
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
|
|
||||||
myMoteMemory.setIntValueOf("simOutSize", 0);
|
myMoteMemory.setIntValueOf("simOutSize", 0);
|
||||||
isTransmitting = false;
|
isTransmitting = false;
|
||||||
|
mote.requestImmediateWakeup();
|
||||||
|
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
lastEventTime = now;
|
||||||
lastEvent = RadioEvent.TRANSMISSION_FINISHED;
|
lastEvent = RadioEvent.TRANSMISSION_FINISHED;
|
||||||
// TODO Energy consumption of transmitted packet?
|
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
//logger.debug("----- CONTIKI TRANSMISSION ENDED -----");
|
/*logger.debug("----- CONTIKI TRANSMISSION ENDED -----");*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a new transmission should be started
|
/* New transmission */
|
||||||
if (!isTransmitting && myMoteMemory.getByteValueOf("simTransmitting") == 1) {
|
|
||||||
int size = myMoteMemory.getIntValueOf("simOutSize");
|
int size = myMoteMemory.getIntValueOf("simOutSize");
|
||||||
if (size <= 0) {
|
if (!isTransmitting && size > 0) {
|
||||||
logger.warn("Skipping zero sized Contiki packet (no size)");
|
|
||||||
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
packetFromMote = new COOJARadioPacket(myMoteMemory.getByteArray("simOutDataBuffer", size));
|
packetFromMote = new COOJARadioPacket(myMoteMemory.getByteArray("simOutDataBuffer", size));
|
||||||
|
|
||||||
if (packetFromMote.getPacketData() == null || packetFromMote.getPacketData().length == 0) {
|
if (packetFromMote.getPacketData() == null || packetFromMote.getPacketData().length == 0) {
|
||||||
logger.warn("Skipping zero sized Contiki packet (no buffer)");
|
logger.warn("Skipping zero sized Contiki packet (no buffer)");
|
||||||
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
|
myMoteMemory.setIntValueOf("simOutSize", 0);
|
||||||
|
mote.requestImmediateWakeup();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,9 +325,9 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
/* Calculate transmission duration (us) */
|
/* Calculate transmission duration (us) */
|
||||||
/* XXX Currently floored due to millisecond scheduling! */
|
/* XXX Currently floored due to millisecond scheduling! */
|
||||||
long duration = (int) (Simulation.MILLISECOND*((8 * size /*bits*/) / RADIO_TRANSMISSION_RATE_kbps));
|
long duration = (int) (Simulation.MILLISECOND*((8 * size /*bits*/) / RADIO_TRANSMISSION_RATE_kbps));
|
||||||
transmissionEndTime = mote.getSimulation().getSimulationTime() + Math.max(1, duration);
|
transmissionEndTime = now + Math.max(1, duration);
|
||||||
lastEventTime = mote.getSimulation().getSimulationTime();
|
|
||||||
|
|
||||||
|
lastEventTime = now;
|
||||||
lastEvent = RadioEvent.TRANSMISSION_STARTED;
|
lastEvent = RadioEvent.TRANSMISSION_STARTED;
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
|
@ -385,6 +339,10 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA
|
||||||
this.notifyObservers();
|
this.notifyObservers();
|
||||||
//logger.debug("----- CONTIKI PACKET DELIVERED -----");
|
//logger.debug("----- CONTIKI PACKET DELIVERED -----");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isTransmitting && transmissionEndTime > now) {
|
||||||
|
mote.scheduleNextWakeup(transmissionEndTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JPanel getInterfaceVisualizer() {
|
public JPanel getInterfaceVisualizer() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue