radio driver with busy-wait support

This commit is contained in:
fros4943 2006-10-02 15:06:12 +00:00
parent f3311d5d4c
commit 41578b35c4
2 changed files with 79 additions and 108 deletions

View file

@ -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: radio-arch.c,v 1.1 2006/08/21 12:11:20 fros4943 Exp $ * $Id: radio-arch.c,v 1.2 2006/10/02 15:06:12 fros4943 Exp $
*/ */
#include "dev/radio-arch.h" #include "dev/radio-arch.h"
@ -40,109 +40,56 @@
#include "net/uip.h" #include "net/uip.h"
#include "net/uip-fw.h" #include "net/uip-fw.h"
#include "sys/etimer.h" #include "sys/etimer.h"
#include "sys/cooja_mt.h"
#include "sys/log.h" #include "sys/log.h"
const struct simInterface radio_interface; const struct simInterface radio_interface;
// COOJA variables // COOJA variables
char simReceivedPacket; char simTransmitting;
char simSentPacket; char simReceiving;
char simReceivedPacketData[UIP_BUFSIZE];
char simSentPacketData[UIP_BUFSIZE]; char simInDataBuffer[UIP_BUFSIZE];
int simSentPacketSize; int simInSize;
int simReceivedPacketSize; char simOutDataBuffer[UIP_BUFSIZE];
char simEtherBusy; int simOutSize;
int retryCounter;
char simRadioHWOn = 1; char simRadioHWOn = 1;
int simSignalStrength;
// Ether process
PROCESS(ether_process, "Simulated Ether");
PROCESS_THREAD(ether_process, ev, data)
{
static struct etimer send_timer;
PROCESS_BEGIN();
// All outgoing messages pass through this process
// By using the COOJA variables simEtherBusy and !!!!TODO signalstrength!!!!
// this may be used to imitate a simple MAC protocol.
while(1) {
PROCESS_WAIT_EVENT();
// MAC protocol imitiation
// (this process is polled from simDoSend())
// Confirm we actually have data to send and radio hardware is on
if (simRadioHWOn && simSentPacketSize > 0) {
// Wait some random time to avoid initial collisions
// MAC uses external random generator to get stochastic radio behaviour
etimer_set(&send_timer, rand() % 20);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
retryCounter = 0;
while (simEtherBusy && retryCounter < 5) {
retryCounter++;
// Wait some random time hoping ether will free
// MAC uses external random generator to get stochastic radio behaviour
etimer_set(&send_timer, rand() % 20);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
}
if (simEtherBusy) {
log_message("MAC layer skipping packet", "");
} else {
// Tell COOJA about our new packet
simSentPacket = 1;
}
}
}
PROCESS_END();
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
static void static void
doInterfaceActionsBeforeTick(void) doInterfaceActionsBeforeTick(void)
{ {
// Handle incoming network packets if any // If radio is turned off, do nothing
if (simReceivedPacket) {
// If hardware is turned off, just remove packet
if (!simRadioHWOn) { if (!simRadioHWOn) {
simReceivedPacket = 0; simInSize = 0;
simReceivedPacketSize = 0;
return; return;
} }
// Reset flag // If no incoming radio data, do nothing
simReceivedPacket = 0; if (simInSize == 0) {
if (simReceivedPacketSize == 0) {
fprintf(stderr, "simReceivedPacketSize == 0: Didn't I receive a packet?\n");
return; return;
} }
// Copy incoming data to correct buffers and call handling routines // Busy-wait while receiving (in main file)
uip_len = simReceivedPacketSize; while (simReceiving) {
busyWaitNext = 1;
return;
}
if(uip_len > UIP_BUFSIZE) { // Check size of received packet
fprintf(stderr, "doInterfaceActionsBeforeTick>> uip_len too large - dropping\n"); if (simInSize > UIP_BUFSIZE) {
uip_len = 0; // Drop packet by not delivering
} else { return;
memcpy(&uip_buf[UIP_LLH_LEN], &simReceivedPacketData[0], simReceivedPacketSize); }
simReceivedPacketSize = 0;
// Handle new packet // Hand over new packet to uIP
uip_len = simInSize;
memcpy(&uip_buf[UIP_LLH_LEN], &simInDataBuffer[0], simInSize);
tcpip_input(); tcpip_input();
} simInSize = 0;
}
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -155,30 +102,54 @@ doInterfaceActionsAfterTick(void)
u8_t u8_t
simDoSend(void) simDoSend(void)
{ {
// If hardware is turned off, just remove packet // If radio is turned off, do nothing
if (!simRadioHWOn) { if (!simRadioHWOn) {
// Should we reset uip_len if radio is off? // TODO Should we reset uip_len if radio is off?
uip_len = 0; uip_len = 0;
return UIP_FW_DROPPED; return UIP_FW_DROPPED;
} }
// If outgoing data, but too large, drop it // Drop packet if data size too large
if(uip_len > UIP_BUFSIZE) { if(uip_len > UIP_BUFSIZE) {
fprintf(stderr, "simDoSend>> uip_len too large - dropping\n");
uip_len = 0; uip_len = 0;
return UIP_FW_TOOLARGE; return UIP_FW_TOOLARGE;
} }
// If outgoing data, back it up, and wake ether process // Drop packet if no data length
if (uip_len > 0) { if (uip_len <= 0) {
// Backup packet data/size
memcpy(&simSentPacketData[0], &uip_buf[UIP_LLH_LEN], uip_len);
simSentPacketSize = uip_len;
process_poll(&ether_process);
return UIP_FW_OK;
}
return UIP_FW_ZEROLEN; return UIP_FW_ZEROLEN;
}
// - Initiate transmission -
simTransmitting = 1;
// Copy packet data to temporary storage
memcpy(&simOutDataBuffer[0], &uip_buf[UIP_LLH_LEN], uip_len);
simOutSize = uip_len;
// Busy-wait while we are receiving
while (simReceiving) {
cooja_mt_yield();
}
// Busy-wait until ether is ready, or die (MAC imitation)
int retries=0;
/* while (retries < 5 && simSignalStrength > -80) {
// TODO Retry and signal strength threshold values?
retries++;
printf("WAITING FOR ETHER! (null)\n");
cooja_mt_yield();
}
if (simSignalStrength > -80) {
return UIP_FW_DROPPED;
}
*/
// Busy-wait while transmitting
while (simTransmitting) {
cooja_mt_yield();
}
return UIP_FW_OK;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/** /**

View file

@ -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: radio-arch.h,v 1.1 2006/08/21 12:11:18 fros4943 Exp $ * $Id: radio-arch.h,v 1.2 2006/10/02 15:06:12 fros4943 Exp $
*/ */
#ifndef __RADIO_ARCH_H__ #ifndef __RADIO_ARCH_H__
@ -35,7 +35,7 @@
#include "contiki.h" #include "contiki.h"
#include "net/uip.h" #include "net/uip.h"
PROCESS_NAME(ether_process);
u8_t simDoSend(void); u8_t simDoSend(void);
#endif /* __RADIO_ARCH_H__ */ #endif /* __RADIO_ARCH_H__ */