Basic MAC data interface for LoRa transceiver
Dependents: LoRaBaseStation LoRaTerminal
AlohaTransceiver.cpp@28:2c0e115b4f72, 2016-09-03 (annotated)
- Committer:
- rba90
- Date:
- Sat Sep 03 02:37:53 2016 +0000
- Revision:
- 28:2c0e115b4f72
- Parent:
- 27:463688e3bd12
- Child:
- 29:6c59b0bba861
packet is only transmitted only if previous transmission is done
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 | 27:463688e3bd12 | 181 | { |
rba90 | 27:463688e3bd12 | 182 | sendAck(frame); |
rba90 | 27:463688e3bd12 | 183 | } |
rba90 | 27:463688e3bd12 | 184 | else if (type == AlohaFrame::Aloha_ACK) |
rba90 | 27:463688e3bd12 | 185 | { |
rba90 | 26:e87c8d345644 | 186 | #ifdef DEBUG_ALOHA |
rba90 | 27:463688e3bd12 | 187 | printf("RXACK::SEQID:0x%x\r\n", frame->getSequenceID()); |
rba90 | 26:e87c8d345644 | 188 | #endif |
rba90 | 27:463688e3bd12 | 189 | } |
rba90 | 27:463688e3bd12 | 190 | |
rba90 | 27:463688e3bd12 | 191 | // check registered callback function |
rba90 | 27:463688e3bd12 | 192 | // execute callback functions if registered |
rba90 | 27:463688e3bd12 | 193 | if (AlohaTypeCallbackTable[type] != NULL) |
rba90 | 27:463688e3bd12 | 194 | { |
rba90 | 27:463688e3bd12 | 195 | uint8_t payload_length = frame->getPayloadLength(); |
rba90 | 27:463688e3bd12 | 196 | uint8_t payload[payload_length]; |
rba90 | 27:463688e3bd12 | 197 | uint8_t src_addr = frame->getSourceAddress(); |
rba90 | 27:463688e3bd12 | 198 | |
rba90 | 27:463688e3bd12 | 199 | // extract payload |
rba90 | 27:463688e3bd12 | 200 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 27:463688e3bd12 | 201 | { |
rba90 | 27:463688e3bd12 | 202 | payload[i] = frame->getPayload(i); |
rba90 | 27:463688e3bd12 | 203 | } |
rba90 | 27:463688e3bd12 | 204 | |
rba90 | 27:463688e3bd12 | 205 | // execute callback function |
rba90 | 27:463688e3bd12 | 206 | AlohaTypeCallbackTable[type](payload, payload_length, src_addr); |
rba90 | 27:463688e3bd12 | 207 | } |
rba90 | 26:e87c8d345644 | 208 | } |
rba90 | 17:c6e2e2cd6e5f | 209 | |
rba90 | 27:463688e3bd12 | 210 | // free memory |
rba90 | 27:463688e3bd12 | 211 | delete frame; |
rba90 | 0:e2ccabf3f30c | 212 | } |
rba90 | 0:e2ccabf3f30c | 213 | |
rba90 | 10:065a4b58c6ff | 214 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 215 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 216 | break; |
rba90 | 0:e2ccabf3f30c | 217 | } |
rba90 | 0:e2ccabf3f30c | 218 | case TX: |
rba90 | 0:e2ccabf3f30c | 219 | { |
rba90 | 28:2c0e115b4f72 | 220 | // set tx to done, allow next transmission |
rba90 | 28:2c0e115b4f72 | 221 | setTxDoneFlag(); |
rba90 | 28:2c0e115b4f72 | 222 | |
rba90 | 10:065a4b58c6ff | 223 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 224 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 225 | break; |
rba90 | 0:e2ccabf3f30c | 226 | } |
rba90 | 0:e2ccabf3f30c | 227 | case RX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 228 | { |
rba90 | 10:065a4b58c6ff | 229 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 230 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 231 | break; |
rba90 | 0:e2ccabf3f30c | 232 | } |
rba90 | 0:e2ccabf3f30c | 233 | case RX_ERROR: |
rba90 | 0:e2ccabf3f30c | 234 | { |
rba90 | 10:065a4b58c6ff | 235 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 236 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 237 | break; |
rba90 | 0:e2ccabf3f30c | 238 | } |
rba90 | 0:e2ccabf3f30c | 239 | case TX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 240 | { |
rba90 | 10:065a4b58c6ff | 241 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 242 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 243 | break; |
rba90 | 0:e2ccabf3f30c | 244 | } |
rba90 | 0:e2ccabf3f30c | 245 | case LOWPOWER: |
rba90 | 0:e2ccabf3f30c | 246 | { |
rba90 | 22:2e39f382f782 | 247 | // transmit packet when the radio is free |
rba90 | 28:2c0e115b4f72 | 248 | if (getTxDoneFlag() && AlohaTxQueue.getCounter() > 0) |
rba90 | 22:2e39f382f782 | 249 | { |
rba90 | 22:2e39f382f782 | 250 | AlohaFrame *frame = AlohaTxQueue.dequeue(); |
rba90 | 22:2e39f382f782 | 251 | |
rba90 | 22:2e39f382f782 | 252 | // create a buffer for transmit |
rba90 | 22:2e39f382f782 | 253 | uint8_t frame_length = frame->getPayloadLength() + FIXED_BYTE; |
rba90 | 22:2e39f382f782 | 254 | uint8_t buffer[frame_length]; // 4 fix fields |
rba90 | 22:2e39f382f782 | 255 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 22:2e39f382f782 | 256 | |
rba90 | 22:2e39f382f782 | 257 | // copy content to buffer |
rba90 | 22:2e39f382f782 | 258 | frame->serialize(buffer); |
rba90 | 22:2e39f382f782 | 259 | |
rba90 | 22:2e39f382f782 | 260 | // send to radio |
rba90 | 22:2e39f382f782 | 261 | Radio.Send(buffer, frame_length); |
rba90 | 22:2e39f382f782 | 262 | |
rba90 | 28:2c0e115b4f72 | 263 | // block the next transmission until previous transmission is done |
rba90 | 28:2c0e115b4f72 | 264 | clearTxDoneFlag(); |
rba90 | 28:2c0e115b4f72 | 265 | |
rba90 | 22:2e39f382f782 | 266 | // free memory |
rba90 | 22:2e39f382f782 | 267 | delete frame; |
rba90 | 22:2e39f382f782 | 268 | } |
rba90 | 0:e2ccabf3f30c | 269 | break; |
rba90 | 0:e2ccabf3f30c | 270 | } |
rba90 | 0:e2ccabf3f30c | 271 | default: |
rba90 | 0:e2ccabf3f30c | 272 | { |
rba90 | 0:e2ccabf3f30c | 273 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 274 | break; |
rba90 | 0:e2ccabf3f30c | 275 | } |
rba90 | 0:e2ccabf3f30c | 276 | } |
rba90 | 0:e2ccabf3f30c | 277 | } |
rba90 | 0:e2ccabf3f30c | 278 | |
rba90 | 24:8f31c33c7675 | 279 | bool AlohaTransceiver::send(BasicPacket *packet) |
rba90 | 24:8f31c33c7675 | 280 | { |
rba90 | 24:8f31c33c7675 | 281 | // for the reason that we only transmit basic packet format, the |
rba90 | 24:8f31c33c7675 | 282 | // length is always 8 |
rba90 | 24:8f31c33c7675 | 283 | uint8_t payload_length = 8; |
rba90 | 24:8f31c33c7675 | 284 | |
rba90 | 24:8f31c33c7675 | 285 | // get destination address |
rba90 | 24:8f31c33c7675 | 286 | uint8_t destination_address = findNextHop(packet->getDestinationID()); |
rba90 | 24:8f31c33c7675 | 287 | |
rba90 | 24:8f31c33c7675 | 288 | // serialize the packet from upper layer |
rba90 | 24:8f31c33c7675 | 289 | uint8_t buffer[payload_length]; |
rba90 | 24:8f31c33c7675 | 290 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 24:8f31c33c7675 | 291 | |
rba90 | 24:8f31c33c7675 | 292 | // copy bytes into buffer |
rba90 | 24:8f31c33c7675 | 293 | packet->serialize(buffer); |
rba90 | 17:c6e2e2cd6e5f | 294 | |
rba90 | 17:c6e2e2cd6e5f | 295 | // create a new frame |
rba90 | 22:2e39f382f782 | 296 | AlohaFrame *frame = new AlohaFrame(); |
rba90 | 17:c6e2e2cd6e5f | 297 | |
rba90 | 22:2e39f382f782 | 298 | // set properfies |
rba90 | 24:8f31c33c7675 | 299 | // set payload as data |
rba90 | 22:2e39f382f782 | 300 | frame->setType(AlohaFrame::Aloha_Data); |
rba90 | 24:8f31c33c7675 | 301 | |
rba90 | 24:8f31c33c7675 | 302 | // set payload length (8 bytes for BasicPacket) |
rba90 | 22:2e39f382f782 | 303 | frame->setPayloadLength(payload_length); |
rba90 | 24:8f31c33c7675 | 304 | |
rba90 | 24:8f31c33c7675 | 305 | // set mac layer device id |
rba90 | 22:2e39f382f782 | 306 | frame->setSourceAddress(deviceId); |
rba90 | 24:8f31c33c7675 | 307 | |
rba90 | 24:8f31c33c7675 | 308 | // set mac layer destination id |
rba90 | 24:8f31c33c7675 | 309 | // for multiple hop system, the destination is the next hop |
rba90 | 24:8f31c33c7675 | 310 | // in this case, we use destination id from upper layer |
rba90 | 24:8f31c33c7675 | 311 | frame->setDestinationAddress(packet->getDestinationID()); |
rba90 | 24:8f31c33c7675 | 312 | |
rba90 | 24:8f31c33c7675 | 313 | // set full message flag (always true in this case) |
rba90 | 22:2e39f382f782 | 314 | frame->setFullMessageFlag(0x1); |
rba90 | 22:2e39f382f782 | 315 | |
rba90 | 24:8f31c33c7675 | 316 | // use dest_addr as key for accessing sequence id |
rba90 | 24:8f31c33c7675 | 317 | // the seqid should increase as it successfully transmit the packet |
rba90 | 24:8f31c33c7675 | 318 | frame->setSequenceID(seqid[destination_address]++); |
rba90 | 24:8f31c33c7675 | 319 | |
rba90 | 22:2e39f382f782 | 320 | // set payload |
rba90 | 17:c6e2e2cd6e5f | 321 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 17:c6e2e2cd6e5f | 322 | { |
rba90 | 24:8f31c33c7675 | 323 | frame->setPayload(i, buffer[i]); |
rba90 | 17:c6e2e2cd6e5f | 324 | } |
rba90 | 17:c6e2e2cd6e5f | 325 | |
rba90 | 17:c6e2e2cd6e5f | 326 | // calculate crc |
rba90 | 22:2e39f382f782 | 327 | frame->generateCrc(); |
rba90 | 17:c6e2e2cd6e5f | 328 | |
rba90 | 22:2e39f382f782 | 329 | // push frame to the back of queue |
rba90 | 22:2e39f382f782 | 330 | AlohaTxQueue.enqueue(frame); |
rba90 | 27:463688e3bd12 | 331 | |
rba90 | 27:463688e3bd12 | 332 | // debug |
rba90 | 27:463688e3bd12 | 333 | #ifdef DEBUG_ALOHA |
rba90 | 27:463688e3bd12 | 334 | printf("\r\nTXDATA::ADDR:0x%x, SEQID:0x%x\r\n", frame->getDestinationAddress(), frame->getSequenceID()); |
rba90 | 27:463688e3bd12 | 335 | #endif |
rba90 | 17:c6e2e2cd6e5f | 336 | return true; |
rba90 | 0:e2ccabf3f30c | 337 | } |
rba90 | 0:e2ccabf3f30c | 338 | |
rba90 | 25:bcd1cee4e5a8 | 339 | void AlohaTransceiver::sendAck(AlohaFrame *inFrame) |
rba90 | 25:bcd1cee4e5a8 | 340 | { |
rba90 | 25:bcd1cee4e5a8 | 341 | // create a new frame by calling it's constructor |
rba90 | 25:bcd1cee4e5a8 | 342 | AlohaFrame *outFrame = new AlohaFrame(); |
rba90 | 25:bcd1cee4e5a8 | 343 | |
rba90 | 25:bcd1cee4e5a8 | 344 | // set frame type |
rba90 | 25:bcd1cee4e5a8 | 345 | outFrame->setType(AlohaFrame::Aloha_ACK); |
rba90 | 25:bcd1cee4e5a8 | 346 | |
rba90 | 25:bcd1cee4e5a8 | 347 | // set payload length (0 byte payload for ack frame) |
rba90 | 25:bcd1cee4e5a8 | 348 | outFrame->setPayloadLength(0); |
rba90 | 25:bcd1cee4e5a8 | 349 | |
rba90 | 25:bcd1cee4e5a8 | 350 | // set mac layer device id |
rba90 | 25:bcd1cee4e5a8 | 351 | outFrame->setSourceAddress(deviceId); |
rba90 | 25:bcd1cee4e5a8 | 352 | |
rba90 | 25:bcd1cee4e5a8 | 353 | // set mac layer destination id |
rba90 | 25:bcd1cee4e5a8 | 354 | outFrame->setDestinationAddress(inFrame->getSourceAddress()); |
rba90 | 25:bcd1cee4e5a8 | 355 | |
rba90 | 25:bcd1cee4e5a8 | 356 | // set full message flag |
rba90 | 25:bcd1cee4e5a8 | 357 | outFrame->setFullMessageFlag(0x1); |
rba90 | 25:bcd1cee4e5a8 | 358 | |
rba90 | 25:bcd1cee4e5a8 | 359 | // set seqid |
rba90 | 25:bcd1cee4e5a8 | 360 | // the sequence id is always the source id + 1 |
rba90 | 25:bcd1cee4e5a8 | 361 | outFrame->setSequenceID(inFrame->getSequenceID() + 1); |
rba90 | 25:bcd1cee4e5a8 | 362 | |
rba90 | 25:bcd1cee4e5a8 | 363 | // no payload |
rba90 | 25:bcd1cee4e5a8 | 364 | |
rba90 | 25:bcd1cee4e5a8 | 365 | // generate cec |
rba90 | 25:bcd1cee4e5a8 | 366 | outFrame->generateCrc(); |
rba90 | 25:bcd1cee4e5a8 | 367 | |
rba90 | 25:bcd1cee4e5a8 | 368 | // push frame to the back of queue |
rba90 | 25:bcd1cee4e5a8 | 369 | AlohaTxQueue.enqueue(outFrame); |
rba90 | 25:bcd1cee4e5a8 | 370 | |
rba90 | 25:bcd1cee4e5a8 | 371 | // debug |
rba90 | 25:bcd1cee4e5a8 | 372 | #ifdef DEBUG_ALOHA |
rba90 | 25:bcd1cee4e5a8 | 373 | printf("\r\nTXACK::ADDR:0x%x, SEQID:0x%x\r\n", outFrame->getDestinationAddress(), outFrame->getSequenceID()); |
rba90 | 25:bcd1cee4e5a8 | 374 | #endif |
rba90 | 25:bcd1cee4e5a8 | 375 | } |
rba90 | 25:bcd1cee4e5a8 | 376 | |
rba90 | 28:2c0e115b4f72 | 377 | bool AlohaTransceiver::getTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 378 | { |
rba90 | 28:2c0e115b4f72 | 379 | return CHECK_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 380 | } |
rba90 | 28:2c0e115b4f72 | 381 | |
rba90 | 28:2c0e115b4f72 | 382 | void AlohaTransceiver::setTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 383 | { |
rba90 | 28:2c0e115b4f72 | 384 | SET_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 385 | } |
rba90 | 28:2c0e115b4f72 | 386 | |
rba90 | 28:2c0e115b4f72 | 387 | void AlohaTransceiver::clearTxDoneFlag() |
rba90 | 28:2c0e115b4f72 | 388 | { |
rba90 | 28:2c0e115b4f72 | 389 | CLEAR_FLAG(flags, 0); |
rba90 | 28:2c0e115b4f72 | 390 | } |
rba90 | 28:2c0e115b4f72 | 391 | |
rba90 | 0:e2ccabf3f30c | 392 | void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 393 | { |
rba90 | 0:e2ccabf3f30c | 394 | AlohaTypeCallbackTable[type] = f; |
rba90 | 0:e2ccabf3f30c | 395 | } |
rba90 | 0:e2ccabf3f30c | 396 | |
rba90 | 0:e2ccabf3f30c | 397 | void AlohaTransceiver::deRegisterType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 398 | { |
rba90 | 0:e2ccabf3f30c | 399 | AlohaTypeCallbackTable[type] = NULL; |
rba90 | 0:e2ccabf3f30c | 400 | } |
rba90 | 0:e2ccabf3f30c | 401 | |
rba90 | 2:fa264e48d5f7 | 402 | int16_t AlohaTransceiver::getRssi() |
rba90 | 2:fa264e48d5f7 | 403 | { |
rba90 | 2:fa264e48d5f7 | 404 | return RssiValue; |
rba90 | 2:fa264e48d5f7 | 405 | } |
rba90 | 2:fa264e48d5f7 | 406 | |
rba90 | 2:fa264e48d5f7 | 407 | int8_t AlohaTransceiver::getSnr() |
rba90 | 2:fa264e48d5f7 | 408 | { |
rba90 | 2:fa264e48d5f7 | 409 | return SnrValue; |
rba90 | 2:fa264e48d5f7 | 410 | } |
rba90 | 2:fa264e48d5f7 | 411 | |
rba90 | 17:c6e2e2cd6e5f | 412 | uint8_t AlohaTransceiver::getDeviceID() |
rba90 | 11:3e1ff29da71a | 413 | { |
rba90 | 11:3e1ff29da71a | 414 | return deviceId; |
rba90 | 11:3e1ff29da71a | 415 | } |
rba90 | 11:3e1ff29da71a | 416 | |
rba90 | 17:c6e2e2cd6e5f | 417 | void AlohaTransceiver::setDeviceID(uint8_t id) |
rba90 | 11:3e1ff29da71a | 418 | { |
rba90 | 11:3e1ff29da71a | 419 | deviceId = id; |
rba90 | 11:3e1ff29da71a | 420 | } |
rba90 | 11:3e1ff29da71a | 421 | |
rba90 | 24:8f31c33c7675 | 422 | uint8_t AlohaTransceiver::findNextHop(uint8_t addr) |
rba90 | 24:8f31c33c7675 | 423 | { |
rba90 | 24:8f31c33c7675 | 424 | // TODO: maintain a routing lookup table for choosing shortest path |
rba90 | 24:8f31c33c7675 | 425 | return addr; |
rba90 | 24:8f31c33c7675 | 426 | } |
rba90 | 24:8f31c33c7675 | 427 | |
rba90 | 8:4bda842f73d4 | 428 | #if USE_MODEM_LORA == 1 |
rba90 | 8:4bda842f73d4 | 429 | AlohaTransceiver::LoRaSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 430 | { |
rba90 | 8:4bda842f73d4 | 431 | return &Settings; |
rba90 | 8:4bda842f73d4 | 432 | } |
rba90 | 8:4bda842f73d4 | 433 | |
rba90 | 8:4bda842f73d4 | 434 | #elif USE_MODEM_FSK == 1 |
rba90 | 8:4bda842f73d4 | 435 | AlohaTransceiver::FskSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 436 | { |
rba90 | 8:4bda842f73d4 | 437 | return &Settings; |
rba90 | 8:4bda842f73d4 | 438 | } |
rba90 | 8:4bda842f73d4 | 439 | #else |
rba90 | 8:4bda842f73d4 | 440 | #error "Please define a modem in the compiler options." |
rba90 | 8:4bda842f73d4 | 441 | #endif |
rba90 | 8:4bda842f73d4 | 442 | |
rba90 | 0:e2ccabf3f30c | 443 | void OnTxDone( void ) |
rba90 | 0:e2ccabf3f30c | 444 | { |
rba90 | 0:e2ccabf3f30c | 445 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 446 | State = TX; |
rba90 | 18:3e6483550f25 | 447 | |
rba90 | 18:3e6483550f25 | 448 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 449 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnTxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 450 | #endif |
rba90 | 0:e2ccabf3f30c | 451 | } |
rba90 | 0:e2ccabf3f30c | 452 | |
rba90 | 0:e2ccabf3f30c | 453 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
rba90 | 0:e2ccabf3f30c | 454 | { |
rba90 | 27:463688e3bd12 | 455 | uint8_t payload_length; |
rba90 | 27:463688e3bd12 | 456 | |
rba90 | 0:e2ccabf3f30c | 457 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 458 | |
rba90 | 0:e2ccabf3f30c | 459 | // safeguard: if size exceeded maximum buffer size, it will cause memory overflow |
rba90 | 27:463688e3bd12 | 460 | payload_length = size ? BUFFER_SIZE : size <= BUFFER_SIZE; |
rba90 | 27:463688e3bd12 | 461 | |
rba90 | 27:463688e3bd12 | 462 | // create a new frame instance |
rba90 | 27:463688e3bd12 | 463 | AlohaFrame *frame = new AlohaFrame(payload, payload_length); |
rba90 | 27:463688e3bd12 | 464 | |
rba90 | 27:463688e3bd12 | 465 | // push onto the end of queue |
rba90 | 27:463688e3bd12 | 466 | AlohaRxQueue.enqueue(frame); |
rba90 | 0:e2ccabf3f30c | 467 | |
rba90 | 0:e2ccabf3f30c | 468 | RssiValue = rssi; |
rba90 | 0:e2ccabf3f30c | 469 | SnrValue = snr; |
rba90 | 0:e2ccabf3f30c | 470 | State = RX; |
rba90 | 18:3e6483550f25 | 471 | |
rba90 | 18:3e6483550f25 | 472 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 473 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 474 | #endif |
rba90 | 0:e2ccabf3f30c | 475 | } |
rba90 | 0:e2ccabf3f30c | 476 | |
rba90 | 0:e2ccabf3f30c | 477 | void OnTxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 478 | { |
rba90 | 0:e2ccabf3f30c | 479 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 480 | State = TX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 481 | |
rba90 | 18:3e6483550f25 | 482 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 483 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnTxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 484 | #endif |
rba90 | 0:e2ccabf3f30c | 485 | } |
rba90 | 0:e2ccabf3f30c | 486 | |
rba90 | 0:e2ccabf3f30c | 487 | void OnRxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 488 | { |
rba90 | 0:e2ccabf3f30c | 489 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 490 | State = RX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 491 | |
rba90 | 18:3e6483550f25 | 492 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 493 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 494 | #endif |
rba90 | 0:e2ccabf3f30c | 495 | } |
rba90 | 0:e2ccabf3f30c | 496 | |
rba90 | 0:e2ccabf3f30c | 497 | void OnRxError( void ) |
rba90 | 0:e2ccabf3f30c | 498 | { |
rba90 | 0:e2ccabf3f30c | 499 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 500 | State = RX_ERROR; |
rba90 | 18:3e6483550f25 | 501 | |
rba90 | 18:3e6483550f25 | 502 | #ifdef DEBUG_ALOHA |
rba90 | 28:2c0e115b4f72 | 503 | debug_if(ALLOW_CALLBACK_DEBUG, "> OnRxError\n\r" ); |
rba90 | 18:3e6483550f25 | 504 | #endif |
rba90 | 0:e2ccabf3f30c | 505 | } |
rba90 | 24:8f31c33c7675 | 506 |