2006-08-21 14:11:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, 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.
|
|
|
|
*
|
2007-05-15 16:39:52 +02:00
|
|
|
* $Id: radio-arch.c,v 1.16 2007/05/15 14:39:52 fros4943 Exp $
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dev/radio-arch.h"
|
|
|
|
#include "dev/radio.h"
|
|
|
|
|
|
|
|
#include "lib/simEnvChange.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "net/uip.h"
|
|
|
|
#include "net/uip-fw.h"
|
2007-04-23 10:46:35 +02:00
|
|
|
#include "net/rime.h"
|
2006-08-21 14:11:16 +02:00
|
|
|
#include "sys/etimer.h"
|
2006-10-02 17:06:12 +02:00
|
|
|
#include "sys/cooja_mt.h"
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
#include "sys/log.h"
|
|
|
|
|
2006-10-11 11:06:41 +02:00
|
|
|
#define MAX_RETRIES 100
|
2006-12-04 16:26:33 +01:00
|
|
|
#define SS_INTERFERENCE -70
|
2006-10-09 16:13:04 +02:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
const struct simInterface radio_interface;
|
|
|
|
|
|
|
|
// COOJA variables
|
2006-10-02 17:06:12 +02:00
|
|
|
char simTransmitting;
|
|
|
|
char simReceiving;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2006-10-02 17:06:12 +02:00
|
|
|
char simInDataBuffer[UIP_BUFSIZE];
|
|
|
|
int simInSize;
|
|
|
|
char simOutDataBuffer[UIP_BUFSIZE];
|
|
|
|
int simOutSize;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2006-10-02 17:06:12 +02:00
|
|
|
char simRadioHWOn = 1;
|
2006-10-09 16:13:04 +02:00
|
|
|
int simSignalStrength = -200;
|
2007-05-15 16:39:52 +02:00
|
|
|
int simLastSignalStrength = 0;
|
2006-10-05 16:44:43 +02:00
|
|
|
char simPower = 100;
|
2006-10-11 17:13:57 +02:00
|
|
|
int simRadioChannel = 1;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2007-04-23 10:46:35 +02:00
|
|
|
int inSendFunction = 0;
|
|
|
|
|
2007-03-20 21:08:51 +01:00
|
|
|
enum {
|
|
|
|
UIP,
|
|
|
|
RIME,
|
|
|
|
};
|
|
|
|
|
2006-10-11 17:13:57 +02:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
radio_set_channel(int channel)
|
|
|
|
{
|
|
|
|
simRadioChannel = channel;
|
|
|
|
}
|
2006-10-05 16:44:43 +02:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
radio_sstrength(void)
|
|
|
|
{
|
2007-05-15 16:39:52 +02:00
|
|
|
return simLastSignalStrength;
|
2006-10-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2007-03-20 21:08:51 +01:00
|
|
|
void
|
|
|
|
radio_set_txpower(unsigned char power)
|
|
|
|
{
|
|
|
|
/* 1 - 100: Number indicating output power */
|
2006-10-05 16:44:43 +02:00
|
|
|
simPower = power;
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
doInterfaceActionsBeforeTick(void)
|
|
|
|
{
|
2006-10-02 17:06:12 +02:00
|
|
|
// If radio is turned off, do nothing
|
|
|
|
if (!simRadioHWOn) {
|
|
|
|
simInSize = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-05 14:09:40 +02:00
|
|
|
// Don't fall asleep while receiving (in main file)
|
2006-10-05 09:46:53 +02:00
|
|
|
if (simReceiving) {
|
2007-05-15 16:39:52 +02:00
|
|
|
simLastSignalStrength = simSignalStrength;
|
2006-10-05 14:09:40 +02:00
|
|
|
simDontFallAsleep = 1;
|
2006-10-02 17:06:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-05 09:46:53 +02:00
|
|
|
// If no incoming radio data, do nothing
|
|
|
|
if (simInSize == 0) {
|
2006-10-02 17:06:12 +02:00
|
|
|
return;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
2006-10-02 17:06:12 +02:00
|
|
|
|
|
|
|
// Check size of received packet
|
|
|
|
if (simInSize > UIP_BUFSIZE) {
|
|
|
|
// Drop packet by not delivering
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-05 16:44:43 +02:00
|
|
|
// ** Good place to add explicit manchester/gcr-encoding
|
2007-03-20 21:08:51 +01:00
|
|
|
if(simInDataBuffer[0] == UIP) {
|
|
|
|
// Hand over new packet to uIP
|
|
|
|
uip_len = simInSize - 1;
|
|
|
|
memcpy(&uip_buf[UIP_LLH_LEN], &simInDataBuffer[1], simInSize - 1);
|
|
|
|
|
|
|
|
if(simNoYield) {
|
|
|
|
simDoTcpipInput = 1;
|
|
|
|
} else {
|
|
|
|
tcpip_input();
|
|
|
|
}
|
|
|
|
} else if(simInDataBuffer[0] == RIME) {
|
|
|
|
/* If we are not configured to use uIP, we use Rime instead. */
|
|
|
|
rimebuf_copyfrom(&simInDataBuffer[1], simInSize - 1);
|
|
|
|
/* log_message("rime input", "");*/
|
|
|
|
rime_input();
|
|
|
|
}
|
2006-10-05 16:44:43 +02:00
|
|
|
|
2006-10-02 17:06:12 +02:00
|
|
|
simInSize = 0;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
doInterfaceActionsAfterTick(void)
|
|
|
|
{
|
2006-10-06 12:45:53 +02:00
|
|
|
// Make sure we are awake during radio activity
|
|
|
|
if (simReceiving || simTransmitting) {
|
|
|
|
simDontFallAsleep = 1;
|
|
|
|
return;
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2007-03-20 21:08:51 +01:00
|
|
|
static u8_t
|
|
|
|
simDoLLSend(unsigned char *buf, int len, int uip_or_rime)
|
2006-08-21 14:11:16 +02:00
|
|
|
{
|
2007-04-23 10:46:35 +02:00
|
|
|
/* If radio already actively transmitting, drop packet*/
|
|
|
|
if(inSendFunction) {
|
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
inSendFunction = 1;
|
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* If radio is turned off, do nothing */
|
2007-03-20 21:08:51 +01:00
|
|
|
if(!simRadioHWOn) {
|
2007-04-23 10:46:35 +02:00
|
|
|
inSendFunction = 0;
|
2006-08-21 14:11:16 +02:00
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* Drop packet if data size too large */
|
|
|
|
if(len > UIP_BUFSIZE) {
|
2007-04-23 10:46:35 +02:00
|
|
|
inSendFunction = 0;
|
2006-08-21 14:11:16 +02:00
|
|
|
return UIP_FW_TOOLARGE;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* Drop packet if no data length */
|
2007-03-20 21:08:51 +01:00
|
|
|
if(len <= 0) {
|
2007-04-23 10:46:35 +02:00
|
|
|
inSendFunction = 0;
|
2006-10-02 17:06:12 +02:00
|
|
|
return UIP_FW_ZEROLEN;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* ** Good place to add explicit manchester/gcr-decoding */
|
2007-03-20 21:08:51 +01:00
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* Copy packet data to temporary storage */
|
2007-03-20 21:08:51 +01:00
|
|
|
simOutDataBuffer[0] = uip_or_rime;
|
|
|
|
memcpy(&simOutDataBuffer[1], buf, len);
|
|
|
|
simOutSize = len + 1;
|
|
|
|
|
2007-03-18 20:31:36 +01:00
|
|
|
/* Busy-wait until both radio HW and ether is ready */
|
2006-10-11 11:06:41 +02:00
|
|
|
{
|
2007-03-20 21:08:51 +01:00
|
|
|
int retries = 0;
|
|
|
|
while(retries < MAX_RETRIES && !simNoYield &&
|
|
|
|
(simSignalStrength > SS_INTERFERENCE || simReceiving)) {
|
|
|
|
retries++;
|
|
|
|
cooja_mt_yield();
|
|
|
|
if(!(simSignalStrength > SS_INTERFERENCE || simReceiving)) {
|
|
|
|
/* Wait one extra tick before transmission starts */
|
2006-10-09 16:13:04 +02:00
|
|
|
cooja_mt_yield();
|
2007-03-20 21:08:51 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-02 17:06:12 +02:00
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
|
|
|
if(simSignalStrength > SS_INTERFERENCE || simReceiving) {
|
2007-04-23 10:46:35 +02:00
|
|
|
inSendFunction = 0;
|
2007-03-20 21:08:51 +01:00
|
|
|
return UIP_FW_DROPPED;
|
2006-10-02 17:06:12 +02:00
|
|
|
}
|
2006-10-05 16:44:43 +02:00
|
|
|
|
|
|
|
// - Initiate transmission -
|
|
|
|
simTransmitting = 1;
|
|
|
|
|
2006-10-02 17:06:12 +02:00
|
|
|
// Busy-wait while transmitting
|
2007-03-20 21:08:51 +01:00
|
|
|
while(simTransmitting && !simNoYield) {
|
2006-10-02 17:06:12 +02:00
|
|
|
cooja_mt_yield();
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
2007-04-23 10:46:35 +02:00
|
|
|
inSendFunction = 0;
|
2006-10-02 17:06:12 +02:00
|
|
|
return UIP_FW_OK;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2007-03-20 21:08:51 +01:00
|
|
|
void
|
|
|
|
rime_driver_send(void)
|
|
|
|
{
|
|
|
|
/* log_message("rime driver send", "");*/
|
|
|
|
simDoLLSend(rimebuf_hdrptr(), rimebuf_totlen(), RIME);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2007-03-18 20:31:36 +01:00
|
|
|
u8_t
|
|
|
|
simDoSend()
|
|
|
|
{
|
|
|
|
/* If radio is turned off, reset uip_len */
|
2007-03-20 21:08:51 +01:00
|
|
|
if(!simRadioHWOn) {
|
2007-03-18 20:31:36 +01:00
|
|
|
uip_len = 0;
|
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
if(uip_len > UIP_BUFSIZE) {
|
2007-03-18 20:31:36 +01:00
|
|
|
uip_len = 0;
|
|
|
|
return UIP_FW_DROPPED;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
if(uip_len <= 0) {
|
2007-03-18 20:31:36 +01:00
|
|
|
return UIP_FW_ZEROLEN;
|
|
|
|
}
|
2007-03-20 21:08:51 +01:00
|
|
|
|
|
|
|
return simDoLLSend(&uip_buf[UIP_LLH_LEN], uip_len, UIP);
|
2007-03-18 20:31:36 +01:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2006-08-21 14:11:16 +02:00
|
|
|
/**
|
|
|
|
* \brief Turn radio on.
|
|
|
|
*
|
|
|
|
* This function turns the radio hardware on.
|
|
|
|
*/
|
|
|
|
void
|
2007-03-20 21:08:51 +01:00
|
|
|
radio_on(void)
|
|
|
|
{
|
2006-08-21 14:11:16 +02:00
|
|
|
simRadioHWOn = 1;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Turn radio off.
|
|
|
|
*
|
|
|
|
* This function turns the radio hardware off.
|
|
|
|
*/
|
2007-03-20 21:08:51 +01:00
|
|
|
void
|
|
|
|
radio_off(void)
|
|
|
|
{
|
2006-08-21 14:11:16 +02:00
|
|
|
simRadioHWOn = 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
SIM_INTERFACE(radio_interface,
|
|
|
|
doInterfaceActionsBeforeTick,
|
|
|
|
doInterfaceActionsAfterTick);
|