osd-contiki/platform/cooja/dev/cooja-radio.c

282 lines
7.1 KiB
C
Raw Normal View History

/*
2010-03-09 09:11:05 +01:00
* Copyright (c) 2010, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
2010-03-09 09:11:05 +01:00
#include <stdio.h>
#include <string.h>
2010-03-09 09:11:05 +01:00
#include "contiki.h"
2010-03-09 09:11:05 +01:00
#include "sys/cooja_mt.h"
#include "lib/simEnvChange.h"
#include "net/packetbuf.h"
2010-03-09 09:11:05 +01:00
#include "net/rime/rimestats.h"
#include "net/netstack.h"
#include "dev/radio.h"
#include "dev/cooja-radio.h"
2010-03-09 09:11:05 +01:00
#define COOJA_RADIO_BUFSIZE PACKETBUF_SIZE
#define CCA_SS_THRESHOLD -95
2012-03-13 14:17:43 +01:00
#define WITH_TURNAROUND 1
#define WITH_SEND_CCA 1
const struct simInterface radio_interface;
2010-03-09 09:11:05 +01:00
/* COOJA */
char simReceiving = 0;
char simInDataBuffer[COOJA_RADIO_BUFSIZE];
int simInSize = 0;
char simOutDataBuffer[COOJA_RADIO_BUFSIZE];
int simOutSize = 0;
char simRadioHWOn = 1;
2010-03-09 09:11:05 +01:00
int simSignalStrength = -100;
int simLastSignalStrength = -100;
char simPower = 100;
int simRadioChannel = 26;
2010-03-09 09:11:05 +01:00
static const void *pending_data;
2010-03-09 09:11:05 +01:00
PROCESS(cooja_radio_process, "cooja radio process");
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
void
2010-03-09 09:11:05 +01:00
radio_set_channel(int channel)
{
2010-03-09 09:11:05 +01:00
simRadioChannel = channel;
}
/*---------------------------------------------------------------------------*/
void
2010-03-09 09:11:05 +01:00
radio_set_txpower(unsigned char power)
{
2010-03-09 09:11:05 +01:00
/* 1 - 100: Number indicating output power */
simPower = power;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
int
2010-03-09 09:11:05 +01:00
radio_signal_strength_last(void)
{
return simLastSignalStrength;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
int
2010-03-09 09:11:05 +01:00
radio_signal_strength_current(void)
{
return simSignalStrength;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
static int
radio_on(void)
{
2010-03-09 09:11:05 +01:00
simRadioHWOn = 1;
return 1;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
static int
radio_off(void)
{
simRadioHWOn = 0;
return 1;
}
/*---------------------------------------------------------------------------*/
static void
doInterfaceActionsBeforeTick(void)
{
2012-03-13 14:17:43 +01:00
if(!simRadioHWOn) {
simInSize = 0;
return;
}
2012-03-13 14:17:43 +01:00
if(simReceiving) {
simLastSignalStrength = simSignalStrength;
return;
}
2009-04-01 15:44:34 +02:00
2012-03-13 14:17:43 +01:00
if(simInSize > 0) {
2010-03-09 09:11:05 +01:00
process_poll(&cooja_radio_process);
}
}
/*---------------------------------------------------------------------------*/
2010-03-09 09:11:05 +01:00
static void
doInterfaceActionsAfterTick(void)
{
}
/*---------------------------------------------------------------------------*/
static int
2007-11-25 23:44:06 +01:00
radio_read(void *buf, unsigned short bufsize)
{
2010-03-09 09:11:05 +01:00
int tmp = simInSize;
2009-04-01 15:44:34 +02:00
2012-03-13 14:17:43 +01:00
if(simInSize == 0) {
return 0;
}
2010-03-09 09:11:05 +01:00
if(bufsize < simInSize) {
simInSize = 0; /* rx flush */
RIMESTATS_ADD(toolong);
return 0;
}
2010-03-09 09:11:05 +01:00
memcpy(buf, simInDataBuffer, simInSize);
simInSize = 0;
return tmp;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
static int
channel_clear(void)
{
if(simSignalStrength > CCA_SS_THRESHOLD) {
return 0;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int
2007-11-25 23:44:06 +01:00
radio_send(const void *payload, unsigned short payload_len)
{
int radiostate = simRadioHWOn;
/* Simulate turnaround time of 2ms for packets, 1ms for acks*/
#if WITH_TURNAROUND
simProcessRunValue = 1;
cooja_mt_yield();
if(payload_len > 3) {
simProcessRunValue = 1;
cooja_mt_yield();
}
#endif /* WITH_TURNAROUND */
if(!simRadioHWOn) {
/* Turn on radio temporarily */
simRadioHWOn = 1;
}
if(payload_len > COOJA_RADIO_BUFSIZE) {
2010-03-09 09:11:05 +01:00
return RADIO_TX_ERR;
}
2010-03-09 09:11:05 +01:00
if(payload_len == 0) {
return RADIO_TX_ERR;
}
if(simOutSize > 0) {
return RADIO_TX_ERR;
}
2009-04-01 15:44:34 +02:00
2012-03-13 14:17:43 +01:00
/* Transmit on CCA */
#if WITH_SEND_CCA
2012-03-13 14:17:43 +01:00
if(!channel_clear()) {
return RADIO_TX_COLLISION;
}
#endif /* WITH_SEND_CCA */
/* Copy packet data to temporary storage */
memcpy(simOutDataBuffer, payload, payload_len);
simOutSize = payload_len;
2009-04-01 15:44:34 +02:00
2010-03-09 09:11:05 +01:00
/* Transmit */
while(simOutSize > 0) {
cooja_mt_yield();
}
simRadioHWOn = radiostate;
2010-03-09 09:11:05 +01:00
return RADIO_TX_OK;
}
/*---------------------------------------------------------------------------*/
static int
prepare_packet(const void *data, unsigned short len)
{
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);
}
2010-03-09 09:11:05 +01:00
return ret;
}
/*---------------------------------------------------------------------------*/
static int
receiving_packet(void)
{
return simReceiving;
}
/*---------------------------------------------------------------------------*/
static int
pending_packet(void)
{
return !simReceiving && simInSize > 0;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cooja_radio_process, ev, data)
{
int len;
2009-04-01 15:44:34 +02:00
2010-03-09 09:11:05 +01:00
PROCESS_BEGIN();
2009-04-01 15:44:34 +02:00
2010-03-09 09:11:05 +01:00
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();
}
}
2009-04-01 15:44:34 +02:00
2010-03-09 09:11:05 +01:00
PROCESS_END();
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
static int
init(void)
{
2010-03-09 09:11:05 +01:00
process_start(&cooja_radio_process, NULL);
return 1;
}
2010-03-09 09:11:05 +01:00
/*---------------------------------------------------------------------------*/
const struct radio_driver cooja_radio_driver =
{
init,
prepare_packet,
transmit_packet,
radio_send,
radio_read,
channel_clear,
2010-03-09 09:11:05 +01:00
receiving_packet,
pending_packet,
radio_on,
radio_off,
};
/*---------------------------------------------------------------------------*/
SIM_INTERFACE(radio_interface,
doInterfaceActionsBeforeTick,
doInterfaceActionsAfterTick);