Add radio energest level tracking to Cooja motes
#include "net/packetbuf.h"
#include "net/netstack.h"
+#include "sys/energest.h"
#include "dev/radio.h"
#include "dev/cooja-radio.h"
@@ -139,6 +140,7 @@ radio_LQI(void)
static int
radio_on(void)
{
+ ENERGEST_ON(ENERGEST_TYPE_LISTEN);
simRadioHWOn = 1;
return 1;
}
@@ -146,6 +148,7 @@ radio_on(void)
static int
radio_off(void)
{
+ ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
simRadioHWOn = 0;
return 1;
}
@@ -207,22 +210,9 @@ channel_clear(void)
static int
radio_send(const void *payload, unsigned short payload_len)
{
- int radiostate = simRadioHWOn;
+ int result;
+ int radio_was_on = simRadioHWOn;
- /* Simulate turnaround time of 2ms for packets, 1ms for acks*/
-#if COOJA_SIMULATE_TURNAROUND
- simProcessRunValue = 1;
- cooja_mt_yield();
- if(payload_len > 3) {
- simProcessRunValue = 1;
- cooja_mt_yield();
- }
-#endif /* COOJA_SIMULATE_TURNAROUND */
-
- if(!simRadioHWOn) {
- /* Turn on radio temporarily */
- simRadioHWOn = 1;
- }
if(payload_len > COOJA_RADIO_BUFSIZE) {
return RADIO_TX_ERR;
}
@@ -233,13 +223,27 @@ radio_send(const void *payload, unsigned short payload_len)
return RADIO_TX_ERR;
}
- /* Transmit on CCA */
-#if COOJA_TRANSMIT_ON_CCA
- if(send_on_cca && !channel_clear()) {
- return RADIO_TX_COLLISION;
+ if(radio_was_on) {
+ ENERGEST_SWITCH(ENERGEST_TYPE_LISTEN, ENERGEST_TYPE_TRANSMIT);
+ } else {
+ /* Turn on radio temporarily */
+ simRadioHWOn = 1;
+ ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
}
-#endif /* COOJA_TRANSMIT_ON_CCA */
+#if COOJA_SIMULATE_TURNAROUND
+ simProcessRunValue = 1;
+ cooja_mt_yield();
+ if(payload_len > 3) {
+ simProcessRunValue = 1;
+ cooja_mt_yield();
+ }
+#endif /* COOJA_SIMULATE_TURNAROUND */
+
+ /* Transmit on CCA */
+ if(COOJA_TRANSMIT_ON_CCA && send_on_cca && !channel_clear()) {
+ result = RADIO_TX_COLLISION;
+ } else {
/* Copy packet data to temporary storage */
memcpy(simOutDataBuffer, payload, payload_len);
simOutSize = payload_len;
@@ -249,8 +253,17 @@ radio_send(const void *payload, unsigned short payload_len)
cooja_mt_yield();
}
- simRadioHWOn = radiostate;
- return RADIO_TX_OK;
+ result = RADIO_TX_OK;
+ }
+
+ if(radio_was_on) {
+ ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
+ } else {
+ ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
+ }
+
+ simRadioHWOn = radio_was_on;
+ return result;
}
/*---------------------------------------------------------------------------*/
static int
