Basic MAC data interface for LoRa transceiver
Dependents: LoRaBaseStation LoRaTerminal
AlohaTransceiver.cpp@29:6c59b0bba861, 2016-09-03 (annotated)
- Committer:
- rba90
- Date:
- Sat Sep 03 02:47:46 2016 +0000
- Revision:
- 29:6c59b0bba861
- Parent:
- 28:2c0e115b4f72
- Child:
- 30:bccad60351ac
minor changes to the debug text
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:e2ccabf3f30c | 1 | #include "AlohaTransceiver.h" |
rba90 | 0:e2ccabf3f30c | 2 | #include "mbed.h" |
rba90 | 0:e2ccabf3f30c | 3 | #include "radio.h" |
rba90 | 18:3e6483550f25 | 4 | #include "debug.h" |
rba90 | 0:e2ccabf3f30c | 5 | #include "AlohaFrame.h" |
rba90 | 15:0dc9df48687a | 6 | #include "RingBuffer.h" |
rba90 | 0:e2ccabf3f30c | 7 | |
rba90 | 28:2c0e115b4f72 | 8 | #define ALLOW_CALLBACK_DEBUG 0 |
rba90 | 18:3e6483550f25 | 9 | |
rba90 | 28:2c0e115b4f72 | 10 | #define SET_FLAG(t, x) (t) |= 1 << (x) |
rba90 | 28:2c0e115b4f72 | 11 | #define CLEAR_FLAG(t, x) (t) &= ~(1 << (x)) |
rba90 | 28:2c0e115b4f72 | 12 | #define TOGGLE_FLAG(t, x) (t) ^= 1 << (x) |
rba90 | 28:2c0e115b4f72 | 13 | #define CHECK_FLAG(t, x) (t) >> (x) & 1 |
rba90 | 27:463688e3bd12 | 14 | |
rba90 | 0:e2ccabf3f30c | 15 | // declear the type of radio state |
rba90 | 0:e2ccabf3f30c | 16 | typedef enum |
rba90 | 0:e2ccabf3f30c | 17 | { |
rba90 | 0:e2ccabf3f30c | 18 | LOWPOWER = 0, |
rba90 | 0:e2ccabf3f30c | 19 | IDLE, |
rba90 | 0:e2ccabf3f30c | 20 | |
rba90 | 0:e2ccabf3f30c | 21 | RX, |
rba90 | 0:e2ccabf3f30c | 22 | RX_TIMEOUT, |
rba90 | 0:e2ccabf3f30c | 23 | RX_ERROR, |
rba90 | 0:e2ccabf3f30c | 24 | |
rba90 | 0:e2ccabf3f30c | 25 | TX, |
rba90 | 0:e2ccabf3f30c | 26 | TX_TIMEOUT, |
rba90 | 0:e2ccabf3f30c | 27 | |
rba90 | 0:e2ccabf3f30c | 28 | CAD, |
rba90 | 0:e2ccabf3f30c | 29 | CAD_DONE |
rba90 | 0:e2ccabf3f30c | 30 | }AppStates_t; |
rba90 | 0:e2ccabf3f30c | 31 | |
rba90 | 0:e2ccabf3f30c | 32 | // radio driver related variables |
rba90 | 0:e2ccabf3f30c | 33 | static int16_t RssiValue; |
rba90 | 0:e2ccabf3f30c | 34 | static int8_t SnrValue; |
rba90 | 0:e2ccabf3f30c | 35 | |
rba90 | 0:e2ccabf3f30c | 36 | static volatile AppStates_t State; |
rba90 | 0:e2ccabf3f30c | 37 | static RadioEvents_t RadioEvents; |
rba90 | 0:e2ccabf3f30c | 38 | |
rba90 | 27:463688e3bd12 | 39 | // rx queue |
rba90 | 27:463688e3bd12 | 40 | CircularBuffer<AlohaFrame *> AlohaRxQueue(10); |
rba90 | 17:c6e2e2cd6e5f | 41 | |
rba90 | 0:e2ccabf3f30c | 42 | // callback functions for radio driver |
rba90 | 0:e2ccabf3f30c | 43 | void OnTxDone(); |
rba90 | 0:e2ccabf3f30c | 44 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
rba90 | 0:e2ccabf3f30c | 45 | void OnTxTimeout(); |
rba90 | 0:e2ccabf3f30c | 46 | void OnRxTimeout(); |
rba90 | 0:e2ccabf3f30c | 47 | void OnRxError(); |
rba90 | 0:e2ccabf3f30c | 48 | void OnFhssChangeChannel( uint8_t channelIndex ); |
rba90 | 0:e2ccabf3f30c | 49 | void OnCadDone(); |
rba90 | 0:e2ccabf3f30c | 50 | |
rba90 | 0:e2ccabf3f30c | 51 | // radio driver |
rba90 | 20:bd26d3cbc0bd | 52 | #ifdef DRIVER_SX1276 |
rba90 | 0:e2ccabf3f30c | 53 | SX1276MB1xAS Radio( NULL ); |
rba90 | 0:e2ccabf3f30c | 54 | #endif |
rba90 | 0:e2ccabf3f30c | 55 | |
rba90 | 20:bd26d3cbc0bd | 56 | #ifdef DRIVER_INAIR |
rba90 | 0:e2ccabf3f30c | 57 | SX1276inAir Radio( NULL ); |
rba90 | 0:e2ccabf3f30c | 58 | #endif |
rba90 | 0:e2ccabf3f30c | 59 | |
rba90 | 0:e2ccabf3f30c | 60 | |
rba90 | 0:e2ccabf3f30c | 61 | /* |
rba90 | 0:e2ccabf3f30c | 62 | * Abstract interface for accessing radio driver |
rba90 | 0:e2ccabf3f30c | 63 | */ |
rba90 | 0:e2ccabf3f30c | 64 | |
rba90 | 27:463688e3bd12 | 65 | AlohaTransceiver::AlohaTransceiver(uint8_t id): |
rba90 | 27:463688e3bd12 | 66 | AlohaTxQueue(10) |
rba90 | 0:e2ccabf3f30c | 67 | { |
rba90 | 11:3e1ff29da71a | 68 | // store unique device id |
rba90 | 11:3e1ff29da71a | 69 | deviceId = id; |
rba90 | 11:3e1ff29da71a | 70 | |
rba90 | 17:c6e2e2cd6e5f | 71 | // initialize sequenceid |
rba90 | 17:c6e2e2cd6e5f | 72 | memset(seqid, 0x0, sizeof(seqid)); |
rba90 | 17:c6e2e2cd6e5f | 73 | |
rba90 | 28:2c0e115b4f72 | 74 | // clear flags |
rba90 | 28:2c0e115b4f72 | 75 | flags = 0xff; |
rba90 | 28:2c0e115b4f72 | 76 | |
rba90 | 0:e2ccabf3f30c | 77 | // configure properties |
rba90 | 0:e2ccabf3f30c | 78 | #if USE_MODEM_LORA == 1 |
rba90 | 0:e2ccabf3f30c | 79 | Settings.Power = TX_OUTPUT_POWER; |
rba90 | 0:e2ccabf3f30c | 80 | Settings.Bandwidth = LORA_BANDWIDTH; |
rba90 | 0:e2ccabf3f30c | 81 | Settings.Datarate = LORA_SPREADING_FACTOR; |
rba90 | 0:e2ccabf3f30c | 82 | Settings.Coderate = LORA_CODINGRATE; |
rba90 | 0:e2ccabf3f30c | 83 | Settings.PreambleLen = LORA_PREAMBLE_LENGTH; |
rba90 | 0:e2ccabf3f30c | 84 | Settings.SymbolTimeout = LORA_SYMBOL_TIMEOUT; |
rba90 | 0:e2ccabf3f30c | 85 | Settings.FixLen = LORA_FIX_LENGTH_PAYLOAD_ON; |
rba90 | 0:e2ccabf3f30c | 86 | Settings.PayloadLen = 0; |
rba90 | 0:e2ccabf3f30c | 87 | Settings.CrcOn = LORA_CRC_ENABLED; |
rba90 | 0:e2ccabf3f30c | 88 | Settings.FreqHopOn = LORA_FHSS_ENABLED; |
rba90 | 0:e2ccabf3f30c | 89 | Settings.HopPeriod = LORA_NB_SYMB_HOP; |
rba90 | 0:e2ccabf3f30c | 90 | Settings.IqInverted = LORA_IQ_INVERSION_ON; |
rba90 | 0:e2ccabf3f30c | 91 | Settings.RxContinuous = true; |
rba90 | 0:e2ccabf3f30c | 92 | Settings.TxTimeout = TX_TIMEOUT_VALUE; |
rba90 | 0:e2ccabf3f30c | 93 | |
rba90 | 0:e2ccabf3f30c | 94 | #elif USE_MODEM_FSK == 1 |
rba90 | 0:e2ccabf3f30c | 95 | // TODO: Complete settings for FSK mode |
rba90 | 0:e2ccabf3f30c | 96 | #error "FSK not implemented" |
rba90 | 0:e2ccabf3f30c | 97 | #else |
rba90 | 0:e2ccabf3f30c | 98 | #error "Please define a modem in the compiler options." |
rba90 | 0:e2ccabf3f30c | 99 | #endif |
rba90 | 0:e2ccabf3f30c | 100 | } |
rba90 | 0:e2ccabf3f30c | 101 | |
rba90 | 0:e2ccabf3f30c | 102 | AlohaTransceiver::~AlohaTransceiver() |
rba90 | 0:e2ccabf3f30c | 103 | { |
rba90 | 0:e2ccabf3f30c | 104 | |
rba90 | 0:e2ccabf3f30c | 105 | } |
rba90 | 0:e2ccabf3f30c | 106 | |
rba90 | 6:f545f5aa7de3 | 107 | void AlohaTransceiver::boardInit() |
rba90 | 0:e2ccabf3f30c | 108 | { |
rba90 | 0:e2ccabf3f30c | 109 | // configure callback functions |
rba90 | 0:e2ccabf3f30c | 110 | RadioEvents.TxDone = OnTxDone; |
rba90 | 0:e2ccabf3f30c | 111 | RadioEvents.RxDone = OnRxDone; |
rba90 | 0:e2ccabf3f30c | 112 | RadioEvents.RxError = OnRxError; |
rba90 | 0:e2ccabf3f30c | 113 | RadioEvents.TxTimeout = OnTxTimeout; |
rba90 | 0:e2ccabf3f30c | 114 | RadioEvents.RxTimeout = OnRxTimeout; |
rba90 | 0:e2ccabf3f30c | 115 | Radio.Init( &RadioEvents ); |
rba90 | 0:e2ccabf3f30c | 116 | |
rba90 | 0:e2ccabf3f30c | 117 | // verify the connection with the board |
rba90 | 0:e2ccabf3f30c | 118 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
rba90 | 0:e2ccabf3f30c | 119 | { |
rba90 | 18:3e6483550f25 | 120 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 121 | debug( "Radio could not be detected!\n\r" ); |
rba90 | 18:3e6483550f25 | 122 | #endif |
rba90 | 0:e2ccabf3f30c | 123 | wait( 1 ); |
rba90 | 0:e2ccabf3f30c | 124 | } |
rba90 | 18:3e6483550f25 | 125 | #ifdef DEBUG_ALOHA |
rba90 | 0:e2ccabf3f30c | 126 | printf("RadioRegVersion: %d\r\n", Radio.Read( REG_VERSION )); |
rba90 | 18:3e6483550f25 | 127 | #endif |
rba90 | 0:e2ccabf3f30c | 128 | } |
rba90 | 0:e2ccabf3f30c | 129 | |
rba90 | 0:e2ccabf3f30c | 130 | void AlohaTransceiver::updateSettings() |
rba90 | 0:e2ccabf3f30c | 131 | { |
rba90 | 6:f545f5aa7de3 | 132 | Radio.SetChannel( RF_FREQUENCY ); |
rba90 | 0:e2ccabf3f30c | 133 | #if USE_MODEM_LORA == 1 |
rba90 | 0:e2ccabf3f30c | 134 | |
rba90 | 0:e2ccabf3f30c | 135 | Radio.SetTxConfig( MODEM_LORA, Settings.Power, 0, Settings.Bandwidth, |
rba90 | 0:e2ccabf3f30c | 136 | Settings.Datarate, Settings.Coderate, |
rba90 | 0:e2ccabf3f30c | 137 | Settings.PreambleLen, Settings.FixLen, |
rba90 | 0:e2ccabf3f30c | 138 | Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod, |
rba90 | 0:e2ccabf3f30c | 139 | Settings.IqInverted, Settings.TxTimeout ); |
rba90 | 0:e2ccabf3f30c | 140 | |
rba90 | 0:e2ccabf3f30c | 141 | Radio.SetRxConfig( MODEM_LORA, Settings.Bandwidth, Settings.Datarate, |
rba90 | 0:e2ccabf3f30c | 142 | Settings.Coderate, 0, Settings.PreambleLen, |
rba90 | 0:e2ccabf3f30c | 143 | Settings.SymbolTimeout, Settings.FixLen, Settings.PayloadLen, |
rba90 | 0:e2ccabf3f30c | 144 | Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod, |
rba90 | 0:e2ccabf3f30c | 145 | Settings.IqInverted, Settings.RxContinuous ); |
rba90 | 0:e2ccabf3f30c | 146 | |
rba90 | 0:e2ccabf3f30c | 147 | #elif USE_MODEM_FSK == 1 |
rba90 | 0:e2ccabf3f30c | 148 | #error "FSK not implemented" |
rba90 | 0:e2ccabf3f30c | 149 | #else |
rba90 | 0:e2ccabf3f30c | 150 | #error "Please define a modem in the compiler options." |
rba90 | 0:e2ccabf3f30c | 151 | #endif |
rba90 | 0:e2ccabf3f30c | 152 | } |
rba90 | 0:e2ccabf3f30c | 153 | |
rba90 | 6:f545f5aa7de3 | 154 | void AlohaTransceiver::enable() |
rba90 | 6:f545f5aa7de3 | 155 | { |
rba90 | 6:f545f5aa7de3 | 156 | // entering passive receiver mode |
rba90 | 10:065a4b58c6ff | 157 | Radio.Rx( 0 ); |
rba90 | 6:f545f5aa7de3 | 158 | } |
rba90 | 6:f545f5aa7de3 | 159 | |
rba90 | 0:e2ccabf3f30c | 160 | void AlohaTransceiver::poll() |
rba90 | 0:e2ccabf3f30c | 161 | { |
rba90 | 0:e2ccabf3f30c | 162 | switch( State ) |
rba90 | 0:e2ccabf3f30c | 163 | { |
rba90 | 0:e2ccabf3f30c | 164 | case RX: |
rba90 | 5:c3741633dc6f | 165 | { |
rba90 | 27:463688e3bd12 | 166 | // process packet if received |
rba90 | 27:463688e3bd12 | 167 | while (AlohaRxQueue.getCounter() > 0) |
rba90 | 25:bcd1cee4e5a8 | 168 | { |
rba90 | 27:463688e3bd12 | 169 | // pop from queue |
rba90 | 27:463688e3bd12 | 170 | AlohaFrame *frame = AlohaRxQueue.dequeue(); |
rba90 | 27:463688e3bd12 | 171 | |
rba90 | 27:463688e3bd12 | 172 | // check destination |
rba90 | 27:463688e3bd12 | 173 | // if the destination is the device id, then processing, otherwise drop the packet and continue |
rba90 | 27:463688e3bd12 | 174 | // listening |
rba90 | 27:463688e3bd12 | 175 | if (frame->getDestinationAddress() == (deviceId & 0x0f)) |
rba90 | 26:e87c8d345644 | 176 | { |
rba90 | 27:463688e3bd12 | 177 | uint8_t type = frame->getType(); |
rba90 | 27:463688e3bd12 | 178 | |
rba90 | 27:463688e3bd12 | 179 | // schedule the ack frame immediatly after the data frame is received |
rba90 | 27:463688e3bd12 | 180 | if (type == AlohaFrame::Aloha_Data) |
rba90 | 29:6c59b0bba861 | 181 | { |
rba90 | 29:6c59b0bba861 | 182 | #ifdef DEBUG_ALOHA |
rba90 | 29:6c59b0bba861 | 183 | printf("\r\nRXDATA::SRC_ADDR:0x%x,SEQID:0x%x\r\n", frame->getSourceAddress(), frame->getSequenceID()); |
rba90 | 29:6c59b0bba861 | 184 | #endif |
rba90 | 27:463688e3bd12 | 185 | sendAck(frame); |
rba90 | 27:463688e3bd12 | 186 | } |
rba90 | 27:463688e3bd12 | 187 | else if (type == AlohaFrame::Aloha_ACK) |
rba90 | 27:463688e3bd12 | 188 | { |
rba90 | 26:e87c8d345644 | 189 | #ifdef DEBUG_ALOHA |
rba90 | 29:6c59b0bba861 | 190 | printf("\r\nRXACK::SRC_ADDR:0x%x,SEQID:0x%x\r\n", frame->getSourceAddress(), frame->getSequenceID()); |
rba90 | 26:e87c8d345644 | 191 | #endif |
rba90 | 27:463688e3bd12 | 192 | } |
rba90 | 27:463688e3bd12 | 193 | |
rba90 | 27:463688e3bd12 | 194 | // check registered callback function |
rba90 | 27:463688e3bd12 | 195 | // execute callback functions if registered |
rba90 | 27:463688e3bd12 | 196 | if (AlohaTypeCallbackTable[type] != NULL) |
rba90 | 27:463688e3bd12 | 197 | { |
rba90 | 27:463688e3bd12 | 198 | uint8_t payload_length = frame->getPayloadLength(); |
rba90 | 27:463688e3bd12 | 199 | uint8_t payload[payload_length]; |
rba90 | 27:463688e3bd12 | 200 | uint8_t src_addr = frame->getSourceAddress(); |
rba90 | 27:463688e3bd12 | 201 | |
rba90 | 27:463688e3bd12 | 202 | // extract payload |
rba90 | 27:463688e3bd12 | 203 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 27:463688e3bd12 | 204 | { |
rba90 | 27:463688e3bd12 | 205 | payload[i] = frame->getPayload(i); |
rba90 | 27:463688e3bd12 | 206 | } |
rba90 | 27:463688e3bd12 | 207 | |
rba90 | 27:463688e3bd12 | 208 | // execute callback function |
rba90 | 27:463688e3bd12 | 209 | AlohaTypeCallbackTable[type](payload, payload_length, src_addr); |
rba90 | 27:463688e3bd12 | 210 | } |
rba90 | 26:e87c8d345644 | 211 | } |
rba90 | 17:c6e2e2cd6e5f | 212 | |
rba90 | 27:463688e3bd12 | 213 | // free memory |
rba90 | 27:463688e3bd12 | 214 | delete frame; |
rba90 | 0:e2ccabf3f30c | 215 | } |
rba90 | 0:e2ccabf3f30c | 216 | |
rba90 | 10:065a4b58c6ff | 217 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 218 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 219 | break; |
rba90 | 0:e2ccabf3f30c | 220 | } |
rba90 | 0:e2ccabf3f30c | 221 | case TX: |
rba90 | 0:e2ccabf3f30c | 222 | { |
rba90 | 28:2c0e115b4f72 | 223 | // set tx to done, allow next transmission |
rba90 | 28:2c0e115b4f72 | 224 | setTxDoneFlag(); |
rba90 | 28:2c0e115b4f72 | 225 | |
rba90 | 10:065a4b58c6ff | 226 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 227 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 228 | break; |
rba90 | 0:e2ccabf3f30c | 229 | } |
rba90 | 0:e2ccabf3f30c | 230 | case RX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 231 | { |
rba90 | 10:065a4b58c6ff | 232 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 233 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 234 | break; |
rba90 | 0:e2ccabf3f30c | 235 | } |
rba90 | 0:e2ccabf3f30c | 236 | case RX_ERROR: |
rba90 | 0:e2ccabf3f30c | 237 | { |
rba90 | 10:065a4b58c6ff | 238 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 239 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 240 | break; |
rba90 | 0:e2ccabf3f30c | 241 | } |
rba90 | 0:e2ccabf3f30c | 242 | case TX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 243 | { |
rba90 | 29:6c59b0bba861 | 244 | // set tx to done, allow next transmission |
rba90 | 29:6c59b0bba861 | 245 | setTxDoneFlag(); |
rba90 | 29:6c59b0bba861 | 246 | |
rba90 | 10:065a4b58c6ff | 247 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 248 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 249 | break; |
rba90 | 0:e2ccabf3f30c | 250 | } |
rba90 | 0:e2ccabf3f30c | 251 | case LOWPOWER: |
rba90 | 0:e2ccabf3f30c | 252 | { |
rba90 | 22:2e39f382f782 | 253 | // transmit packet when the radio is free |
rba90 | 28:2c0e115b4f72 | 254 | if (getTxDoneFlag() && AlohaTxQueue.getCounter() > 0) |
rba90 | 22:2e39f382f782 | 255 | { |
rba90 | 22:2e39f382f782 | 256 | AlohaFrame *frame = AlohaTxQueue.dequeue(); |
rba90 | 22:2e39f382f782 | 257 | |
rba90 | 22:2e39f382f782 | 258 | // create a buffer for transmit |
rba90 | 22:2e39f382f782 | 259 | uint8_t frame_length = frame->getPayloadLength() + FIXED_BYTE; |
rba90 | 22:2e39f382f782 | 260 | uint8_t buffer[frame_length]; // 4 fix fields |
rba90 | 22:2e39f382f782 | 261 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 22:2e39f382f782 | 262 | |
rba90 | 22:2e39f382f782 | 263 | // copy content to buffer |
rba90 | 22:2e39f382f782 | 264 | frame->serialize(buffer); |
rba90 | 22:2e39f382f782 | 265 | |
rba90 | 22:2e39f382f782 | 266 | // send to radio |
rba90 | 22:2e39f382f782 | 267 | Radio.Send(buffer, frame_length); |
rba90 | 22:2e39f382f782 | 268 | |
rba90 | 28:2c0e115b4f72 | 269 | // block the next transmission until previous transmission is done |
rba90 | 28:2c0e115b4f72 | 270 | clearTxDoneFlag(); |
rba90 | 28:2c0e115b4f72 | 271 | |
rba90 | 22:2e39f382f782 | 272 | // free memory |
rba90 | 22:2e39f382f782 | 273 | delete frame; |
rba90 | 22:2e39f382f782 | 274 | } |
rba90 | 0:e2ccabf3f30c | 275 | break; |
rba90 | 0:e2ccabf3f30c | 276 | } |
rba90 | 0:e2ccabf3f30c | 277 | default: |
rba90 | 0:e2ccabf3f30c | 278 | { |
rba90 | 0:e2ccabf3f30c | 279 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 280 | break; |
rba90 | 0:e2ccabf3f30c | 281 | } |
rba90 | 0:e2ccabf3f30c | 282 | } |
rba90 | 0:e2ccabf3f30c | 283 | } |
rba90 | 0:e2ccabf3f30c | 284 | |
rba90 | 24:8f31c33c7675 | 285 | bool AlohaTransceiver::send(BasicPacket *packet) |
rba90 | 24:8f31c33c7675 | 286 | { |
rba90 | 24:8f31c33c7675 | 287 | // for the reason that we only transmit basic packet format, the |
rba90 | 24:8f31c33c7675 | 288 | // length is always 8 |
rba90 | 24:8f31c33c7675 | 289 | uint8_t payload_length = 8; |
rba90 | 24:8f31c33c7675 | 290 | |
rba90 | 24:8f31c33c7675 | 291 | // get destination address |
rba90 | 24:8f31c33c7675 | 292 | uint8_t destination_address = findNextHop(packet->getDestinationID()); |
rba90 | 24:8f31c33c7675 | 293 | |
rba90 | 24:8f31c33c7675 | 294 | // serialize the packet from upper layer |
rba90 | 24:8f31c33c7675 | 295 | uint8_t buffer[payload_length]; |
rba90 | 24:8f31c33c7675 | 296 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 24:8f31c33c7675 | 297 | |
rba90 | 24:8f31c33c7675 | 298 | // copy bytes into buffer |
rba90 | 24:8f31c33c7675 | 299 | packet->serialize(buffer); |
rba90 | 17:c6e2e2cd6e5f | 300 | |
rba90 | 17:c6e2e2cd6e5f | 301 | // create a new frame |
rba90 | 22:2e39f382f782 | 302 | AlohaFrame *frame = new AlohaFrame(); |
rba90 | 17:c6e2e2cd6e5f | 303 | |
rba90 | 22:2e39f382f782 | 304 | // set properfies |
rba90 | 24:8f31c33c7675 | 305 | // set payload as data |
rba90 | 22:2e39f382f782 | 306 | frame->setType(AlohaFrame::Aloha_Data); |
rba90 | 24:8f31c33c7675 | 307 | |
rba90 | 24:8f31c33c7675 | 308 | // set payload length (8 bytes for BasicPacket) |
rba90 | 22:2e39f382f782 | 309 | frame->setPayloadLength(payload_length); |
rba90 | 24:8f31c33c7675 | 310 | |
rba90 | 24:8f31c33c7675 | 311 | // set mac layer device id |
rba90 | 22:2e39f382f782 | 312 | frame->setSourceAddress(deviceId); |
rba90 | 24:8f31c33c7675 | 313 | |
rba90 | 24:8f31c33c7675 | 314 | // set mac layer destination id |
rba90 | 24:8f31c33c7675 | 315 | // for multiple hop system, the destination is the next hop |
rba90 | 24:8f31c33c7675 | 316 | // in this case, we use destination id from upper layer |
rba90 | 24:8f31c33c7675 | 317 | frame->setDestinationAddress(packet->getDestinationID()); |
rba90 | 24:8f31c33c7675 | 318 | |
rba90 | 24:8f31c33c7675 | 319 | // set full message flag (always true in this case) |
rba90 | 22:2e39f382f782 | 320 | frame->setFullMessageFlag(0x1); |
rba90 | 22:2e39f382f782 | 321 | |
rba90 | 24:8f31c33c7675 | 322 | // use dest_addr as key for accessing sequence id |
rba90 | 24:8f31c33c7675 | 323 | // the seqid should increase as it successfully transmit the packet |
rba90 | 24:8f31c33c7675 | 324 | frame->setSequenceID(seqid[destination_address]++); |
rba90 | 24:8f31c33c7675 | 325 | |
rba90 | 22:2e39f382f782 | 326 | // set payload |
rba90 | 17:c6e2e2cd6e5f | 327 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 17:c6e2e2cd6e5f | 328 | { |
rba90 | 24:8f31c33c7675 | 329 | frame->setPayload(i, buffer[i]); |
rba90 | 17:c6e2e2cd6e5f | 330 | } |
rba90 | 17:c6e2e2cd6e5f | 331 | |
rba90 | 17:c6e2e2cd6e5f | 332 | // calculate crc |
rba90 | 22:2e39f382f782 | 333 | frame->generateCrc(); |
rba90 | 17:c6e2e2cd6e5f | 334 | |
rba90 | 22:2e39f382f782 | 335 | // push frame to the back of queue |
rba90 | 22:2e39f382f782 | 336 | AlohaTxQueue.enqueue(frame); |
rba90 | 27:463688e3bd12 | 337 | |
rba90 | 27:463688e3bd12 | 338 | // debug |
rba90 | 27:463688e3bd12 | 339 | #ifdef DEBUG_ALOHA |
rba90 | 29:6c59b0bba861 | 340 | printf("\r\nTXDATA::DEST_ADDR:0x%x,SEQID:0x%x\r\n", frame->getDestinationAddress(), frame->getSequenceID()); |
rba90 | 27:463688e3bd12 | 341 | #endif |
rba90 | 17:c6e2e2cd6e5f | 342 | return true; |
rba90 | 0:e2ccabf3f30c | 343 | } |
rba90 | 0:e2ccabf3f30c | 344 | |
rba90 | 25:bcd1cee4e5a8 | 345 | void AlohaTransceiver::sendAck(AlohaFrame *inFrame) |
rba90 | 25:bcd1cee4e5a8 | 346 | { |
rba90 | 25:bcd1cee4e5a8 | 347 | // create a new frame by calling it's constructor |
rba90 | 25:bcd1cee4e5a8 | 348 | AlohaFrame *outFrame = new AlohaFrame(); |
rba90 | 25:bcd1cee4e5a8 | 349 | |
rba90 | 25:bcd1cee4e5a8 | 350 | // set frame type |
rba90 | 25:bcd1cee4e5a8 | 351 | outFrame->setType(AlohaFrame::Aloha_ACK); |
rba90 | 25:bcd1cee4e5a8 | 352 | |
rba90 | 25:bcd1cee4e5a8 | 353 | // set payload length (0 byte payload for ack frame) |
rba90 | 25:bcd1cee4e5a8 | 354 | outFrame->setPayloadLength(0); |
rba90 | 25:bcd1cee4e5a8 | 355 | |
rba90 | 25:bcd1cee4e5a8 | 356 | // set mac layer device id |
rba90 | 25:bcd1cee4e5a8 | 357 | outFrame->setSourceAddress(deviceId); |
rba90 | 25:bcd1cee4e5a8 | 358 | |
rba90 | 25:bcd1cee4e5a8 | 359 | // set mac layer destination id |
rba90 | 25:bcd1cee4e5a8 | 360 | outFrame->setDestinationAddress(inFrame->getSourceAddress()); |
rba90 | 25:bcd1cee4e5a8 | 361 | |
rba90 | 25:bcd1cee4e5a8 | 362 | // set full message flag |
rba90 | 25:bcd1cee4e5a8 | 363 | outFrame->setFullMessageFlag(0x1); |
rba90 | 25:bcd1cee4e5a8 | 364 | |
rba90 | 25:bcd1cee4e5a8 | 365 | // set seqid |
rba90 | 25:bcd1cee4e5a8 | 366 | // the sequence id is always the source id + 1 |
rba90 | 25:bcd1cee4e5a8 | 367 | outFrame->setSequenceID(inFrame->getSequenceID() + 1); |
rba90 | 25:bcd1cee4e5a8 | 368 | |
rba90 | 25:bcd1cee4e5a8 | 369 | // no payload |
rba90 | 25:bcd1cee4e5a8 | 370 | |
rba90 | 25:bcd1cee4e5a8 | 371 | // generate cec |
rba90 | 25:bcd1cee4e5a8 | 372 | outFrame->generateCrc(); |
rba90 | 25:bcd1cee4e5a8 | 373 | |
rba90 | 25:bcd1cee4e5a8 | 374 | // push frame to the back of queue |
rba90 | 25:bcd1cee4e5a8 | 375 | AlohaTxQueue.enqueue(outFrame); |
rba90 | 25:bcd1cee4e5a8 | 376 | |
rba90 | 25:bcd1cee4e5a8 | 377 | // debug |
rba90 | 25:bcd1cee4e5a8 | 378 | #ifdef DEBUG_ALOHA |
rba90 | 29:6c59b0bba861 | 379 | printf("\r\nTXACK::DEST_ADDR:0x%x,SEQID:0x%x\r\n", outFrame->getDestinationAddress(), outFrame->getSequenceID()); |
rba90 | 25:bcd1cee4e5a8 | 380 | #endif |
rba90 | 25:bcd1cee4e5a8 | 381 | } |
rba90 | 25:bcd1cee4e5a8 | 382 | |
rba90 | 28:2c0e115b4f72 | 383 | bool AlohaTransceiver::getTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 384 | { |
rba90 | 28:2c0e115b4f72 | 385 | return CHECK_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 386 | } |
rba90 | 28:2c0e115b4f72 | 387 | |
rba90 | 28:2c0e115b4f72 | 388 | void AlohaTransceiver::setTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 389 | { |
rba90 | 28:2c0e115b4f72 | 390 | SET_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 391 | } |
rba90 | 28:2c0e115b4f72 | 392 | |
rba90 | 28:2c0e115b4f72 | 393 | void AlohaTransceiver::clearTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 394 | { |
rba90 | 28:2c0e115b4f72 | 395 | CLEAR_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 396 | } |
rba90 | 28:2c0e115b4f72 | 397 | |
rba90 | 0:e2ccabf3f30c | 398 | void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 399 | { |
rba90 | 0:e2ccabf3f30c | 400 | AlohaTypeCallbackTable[type] = f; |
rba90 | 0:e2ccabf3f30c | 401 | } |
rba90 | 0:e2ccabf3f30c | 402 | |
rba90 | 0:e2ccabf3f30c | 403 | void AlohaTransceiver::deRegisterType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 404 | { |
rba90 | 0:e2ccabf3f30c | 405 | AlohaTypeCallbackTable[type] = NULL; |
rba90 | 0:e2ccabf3f30c | 406 | } |
rba90 | 0:e2ccabf3f30c | 407 | |
rba90 | 2:fa264e48d5f7 | 408 | int16_t AlohaTransceiver::getRssi() |
rba90 | 2:fa264e48d5f7 | 409 | { |
rba90 | 2:fa264e48d5f7 | 410 | return RssiValue; |
rba90 | 2:fa264e48d5f7 | 411 | } |
rba90 | 2:fa264e48d5f7 | 412 | |
rba90 | 2:fa264e48d5f7 | 413 | int8_t AlohaTransceiver::getSnr() |
rba90 | 2:fa264e48d5f7 | 414 | { |
rba90 | 2:fa264e48d5f7 | 415 | return SnrValue; |
rba90 | 2:fa264e48d5f7 | 416 | } |
rba90 | 2:fa264e48d5f7 | 417 | |
rba90 | 17:c6e2e2cd6e5f | 418 | uint8_t AlohaTransceiver::getDeviceID() |
rba90 | 11:3e1ff29da71a | 419 | { |
rba90 | 11:3e1ff29da71a | 420 | return deviceId; |
rba90 | 11:3e1ff29da71a | 421 | } |
rba90 | 11:3e1ff29da71a | 422 | |
rba90 | 17:c6e2e2cd6e5f | 423 | void AlohaTransceiver::setDeviceID(uint8_t id) |
rba90 | 11:3e1ff29da71a | 424 | { |
rba90 | 11:3e1ff29da71a | 425 | deviceId = id; |
rba90 | 11:3e1ff29da71a | 426 | } |
rba90 | 11:3e1ff29da71a | 427 | |
rba90 | 24:8f31c33c7675 | 428 | uint8_t AlohaTransceiver::findNextHop(uint8_t addr) |
rba90 | 24:8f31c33c7675 | 429 | { |
rba90 | 24:8f31c33c7675 | 430 | // TODO: maintain a routing lookup table for choosing shortest path |
rba90 | 24:8f31c33c7675 | 431 | return addr; |
rba90 | 24:8f31c33c7675 | 432 | } |
rba90 | 24:8f31c33c7675 | 433 | |
rba90 | 8:4bda842f73d4 | 434 | #if USE_MODEM_LORA == 1 |
rba90 | 8:4bda842f73d4 | 435 | AlohaTransceiver::LoRaSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 436 | { |
rba90 | 8:4bda842f73d4 | 437 | return &Settings; |
rba90 | 8:4bda842f73d4 | 438 | } |
rba90 | 8:4bda842f73d4 | 439 | |
rba90 | 8:4bda842f73d4 | 440 | #elif USE_MODEM_FSK == 1 |
rba90 | 8:4bda842f73d4 | 441 | AlohaTransceiver::FskSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 442 | { |
rba90 | 8:4bda842f73d4 | 443 | return &Settings; |
rba90 | 8:4bda842f73d4 | 444 | } |
rba90 | 8:4bda842f73d4 | 445 | #else |
rba90 | 8:4bda842f73d4 | 446 | #error "Please define a modem in the compiler options." |
rba90 | 8:4bda842f73d4 | 447 | #endif |
rba90 | 8:4bda842f73d4 | 448 | |
rba90 | 0:e2ccabf3f30c | 449 | void OnTxDone( void ) |
rba90 | 0:e2ccabf3f30c | 450 | { |
rba90 | 0:e2ccabf3f30c | 451 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 452 | State = TX; |
rba90 | 18:3e6483550f25 | 453 | |
rba90 | 18:3e6483550f25 | 454 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 455 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnTxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 456 | #endif |
rba90 | 0:e2ccabf3f30c | 457 | } |
rba90 | 0:e2ccabf3f30c | 458 | |
rba90 | 0:e2ccabf3f30c | 459 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
rba90 | 0:e2ccabf3f30c | 460 | { |
rba90 | 27:463688e3bd12 | 461 | uint8_t payload_length; |
rba90 | 27:463688e3bd12 | 462 | |
rba90 | 0:e2ccabf3f30c | 463 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 464 | |
rba90 | 0:e2ccabf3f30c | 465 | // safeguard: if size exceeded maximum buffer size, it will cause memory overflow |
rba90 | 27:463688e3bd12 | 466 | payload_length = size ? BUFFER_SIZE : size <= BUFFER_SIZE; |
rba90 | 27:463688e3bd12 | 467 | |
rba90 | 27:463688e3bd12 | 468 | // create a new frame instance |
rba90 | 27:463688e3bd12 | 469 | AlohaFrame *frame = new AlohaFrame(payload, payload_length); |
rba90 | 27:463688e3bd12 | 470 | |
rba90 | 27:463688e3bd12 | 471 | // push onto the end of queue |
rba90 | 27:463688e3bd12 | 472 | AlohaRxQueue.enqueue(frame); |
rba90 | 0:e2ccabf3f30c | 473 | |
rba90 | 0:e2ccabf3f30c | 474 | RssiValue = rssi; |
rba90 | 0:e2ccabf3f30c | 475 | SnrValue = snr; |
rba90 | 0:e2ccabf3f30c | 476 | State = RX; |
rba90 | 18:3e6483550f25 | 477 | |
rba90 | 18:3e6483550f25 | 478 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 479 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 480 | #endif |
rba90 | 0:e2ccabf3f30c | 481 | } |
rba90 | 0:e2ccabf3f30c | 482 | |
rba90 | 0:e2ccabf3f30c | 483 | void OnTxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 484 | { |
rba90 | 0:e2ccabf3f30c | 485 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 486 | State = TX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 487 | |
rba90 | 18:3e6483550f25 | 488 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 489 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnTxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 490 | #endif |
rba90 | 0:e2ccabf3f30c | 491 | } |
rba90 | 0:e2ccabf3f30c | 492 | |
rba90 | 0:e2ccabf3f30c | 493 | void OnRxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 494 | { |
rba90 | 0:e2ccabf3f30c | 495 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 496 | State = RX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 497 | |
rba90 | 18:3e6483550f25 | 498 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 499 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 500 | #endif |
rba90 | 0:e2ccabf3f30c | 501 | } |
rba90 | 0:e2ccabf3f30c | 502 | |
rba90 | 0:e2ccabf3f30c | 503 | void OnRxError( void ) |
rba90 | 0:e2ccabf3f30c | 504 | { |
rba90 | 0:e2ccabf3f30c | 505 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 506 | State = RX_ERROR; |
rba90 | 18:3e6483550f25 | 507 | |
rba90 | 18:3e6483550f25 | 508 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 509 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxError\n\r" ); |
rba90 | 18:3e6483550f25 | 510 | #endif |
rba90 | 0:e2ccabf3f30c | 511 | } |
rba90 | 24:8f31c33c7675 | 512 |