Basic MAC data interface for LoRa transceiver
Dependents: LoRaBaseStation LoRaTerminal
AlohaTransceiver.cpp@23:4b51a8e27f6a, 2016-09-02 (annotated)
- Committer:
- rba90
- Date:
- Fri Sep 02 04:20:26 2016 +0000
- Revision:
- 23:4b51a8e27f6a
- Parent:
- 22:2e39f382f782
- Child:
- 24:8f31c33c7675
add door sensor interrupt
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 | 18:3e6483550f25 | 8 | |
rba90 | 0:e2ccabf3f30c | 9 | // declear the type of radio state |
rba90 | 0:e2ccabf3f30c | 10 | typedef enum |
rba90 | 0:e2ccabf3f30c | 11 | { |
rba90 | 0:e2ccabf3f30c | 12 | LOWPOWER = 0, |
rba90 | 0:e2ccabf3f30c | 13 | IDLE, |
rba90 | 0:e2ccabf3f30c | 14 | |
rba90 | 0:e2ccabf3f30c | 15 | RX, |
rba90 | 0:e2ccabf3f30c | 16 | RX_TIMEOUT, |
rba90 | 0:e2ccabf3f30c | 17 | RX_ERROR, |
rba90 | 0:e2ccabf3f30c | 18 | |
rba90 | 0:e2ccabf3f30c | 19 | TX, |
rba90 | 0:e2ccabf3f30c | 20 | TX_TIMEOUT, |
rba90 | 0:e2ccabf3f30c | 21 | |
rba90 | 0:e2ccabf3f30c | 22 | CAD, |
rba90 | 0:e2ccabf3f30c | 23 | CAD_DONE |
rba90 | 0:e2ccabf3f30c | 24 | }AppStates_t; |
rba90 | 0:e2ccabf3f30c | 25 | |
rba90 | 0:e2ccabf3f30c | 26 | // radio driver related variables |
rba90 | 0:e2ccabf3f30c | 27 | static uint16_t BufferSize; |
rba90 | 0:e2ccabf3f30c | 28 | static uint8_t Buffer[BUFFER_SIZE]; |
rba90 | 0:e2ccabf3f30c | 29 | |
rba90 | 0:e2ccabf3f30c | 30 | static int16_t RssiValue; |
rba90 | 0:e2ccabf3f30c | 31 | static int8_t SnrValue; |
rba90 | 0:e2ccabf3f30c | 32 | |
rba90 | 0:e2ccabf3f30c | 33 | static volatile AppStates_t State; |
rba90 | 0:e2ccabf3f30c | 34 | static RadioEvents_t RadioEvents; |
rba90 | 0:e2ccabf3f30c | 35 | |
rba90 | 17:c6e2e2cd6e5f | 36 | |
rba90 | 17:c6e2e2cd6e5f | 37 | |
rba90 | 0:e2ccabf3f30c | 38 | // callback functions for radio driver |
rba90 | 0:e2ccabf3f30c | 39 | void OnTxDone(); |
rba90 | 0:e2ccabf3f30c | 40 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
rba90 | 0:e2ccabf3f30c | 41 | void OnTxTimeout(); |
rba90 | 0:e2ccabf3f30c | 42 | void OnRxTimeout(); |
rba90 | 0:e2ccabf3f30c | 43 | void OnRxError(); |
rba90 | 0:e2ccabf3f30c | 44 | void OnFhssChangeChannel( uint8_t channelIndex ); |
rba90 | 0:e2ccabf3f30c | 45 | void OnCadDone(); |
rba90 | 0:e2ccabf3f30c | 46 | |
rba90 | 0:e2ccabf3f30c | 47 | // radio driver |
rba90 | 20:bd26d3cbc0bd | 48 | #ifdef DRIVER_SX1276 |
rba90 | 0:e2ccabf3f30c | 49 | SX1276MB1xAS Radio( NULL ); |
rba90 | 0:e2ccabf3f30c | 50 | #endif |
rba90 | 0:e2ccabf3f30c | 51 | |
rba90 | 20:bd26d3cbc0bd | 52 | #ifdef DRIVER_INAIR |
rba90 | 0:e2ccabf3f30c | 53 | SX1276inAir Radio( NULL ); |
rba90 | 0:e2ccabf3f30c | 54 | #endif |
rba90 | 0:e2ccabf3f30c | 55 | |
rba90 | 0:e2ccabf3f30c | 56 | |
rba90 | 0:e2ccabf3f30c | 57 | /* |
rba90 | 0:e2ccabf3f30c | 58 | * Abstract interface for accessing radio driver |
rba90 | 0:e2ccabf3f30c | 59 | */ |
rba90 | 0:e2ccabf3f30c | 60 | |
rba90 | 11:3e1ff29da71a | 61 | AlohaTransceiver::AlohaTransceiver(uint8_t id) |
rba90 | 0:e2ccabf3f30c | 62 | { |
rba90 | 11:3e1ff29da71a | 63 | // store unique device id |
rba90 | 11:3e1ff29da71a | 64 | deviceId = id; |
rba90 | 11:3e1ff29da71a | 65 | |
rba90 | 17:c6e2e2cd6e5f | 66 | // initialize sequenceid |
rba90 | 17:c6e2e2cd6e5f | 67 | memset(seqid, 0x0, sizeof(seqid)); |
rba90 | 17:c6e2e2cd6e5f | 68 | |
rba90 | 0:e2ccabf3f30c | 69 | // configure properties |
rba90 | 0:e2ccabf3f30c | 70 | #if USE_MODEM_LORA == 1 |
rba90 | 0:e2ccabf3f30c | 71 | Settings.Power = TX_OUTPUT_POWER; |
rba90 | 0:e2ccabf3f30c | 72 | Settings.Bandwidth = LORA_BANDWIDTH; |
rba90 | 0:e2ccabf3f30c | 73 | Settings.Datarate = LORA_SPREADING_FACTOR; |
rba90 | 0:e2ccabf3f30c | 74 | Settings.Coderate = LORA_CODINGRATE; |
rba90 | 0:e2ccabf3f30c | 75 | Settings.PreambleLen = LORA_PREAMBLE_LENGTH; |
rba90 | 0:e2ccabf3f30c | 76 | Settings.SymbolTimeout = LORA_SYMBOL_TIMEOUT; |
rba90 | 0:e2ccabf3f30c | 77 | Settings.FixLen = LORA_FIX_LENGTH_PAYLOAD_ON; |
rba90 | 0:e2ccabf3f30c | 78 | Settings.PayloadLen = 0; |
rba90 | 0:e2ccabf3f30c | 79 | Settings.CrcOn = LORA_CRC_ENABLED; |
rba90 | 0:e2ccabf3f30c | 80 | Settings.FreqHopOn = LORA_FHSS_ENABLED; |
rba90 | 0:e2ccabf3f30c | 81 | Settings.HopPeriod = LORA_NB_SYMB_HOP; |
rba90 | 0:e2ccabf3f30c | 82 | Settings.IqInverted = LORA_IQ_INVERSION_ON; |
rba90 | 0:e2ccabf3f30c | 83 | Settings.RxContinuous = true; |
rba90 | 0:e2ccabf3f30c | 84 | Settings.TxTimeout = TX_TIMEOUT_VALUE; |
rba90 | 0:e2ccabf3f30c | 85 | |
rba90 | 0:e2ccabf3f30c | 86 | #elif USE_MODEM_FSK == 1 |
rba90 | 0:e2ccabf3f30c | 87 | // TODO: Complete settings for FSK mode |
rba90 | 0:e2ccabf3f30c | 88 | #error "FSK not implemented" |
rba90 | 0:e2ccabf3f30c | 89 | #else |
rba90 | 0:e2ccabf3f30c | 90 | #error "Please define a modem in the compiler options." |
rba90 | 0:e2ccabf3f30c | 91 | #endif |
rba90 | 0:e2ccabf3f30c | 92 | } |
rba90 | 0:e2ccabf3f30c | 93 | |
rba90 | 0:e2ccabf3f30c | 94 | AlohaTransceiver::~AlohaTransceiver() |
rba90 | 0:e2ccabf3f30c | 95 | { |
rba90 | 0:e2ccabf3f30c | 96 | |
rba90 | 0:e2ccabf3f30c | 97 | } |
rba90 | 0:e2ccabf3f30c | 98 | |
rba90 | 6:f545f5aa7de3 | 99 | void AlohaTransceiver::boardInit() |
rba90 | 0:e2ccabf3f30c | 100 | { |
rba90 | 0:e2ccabf3f30c | 101 | // configure callback functions |
rba90 | 0:e2ccabf3f30c | 102 | RadioEvents.TxDone = OnTxDone; |
rba90 | 0:e2ccabf3f30c | 103 | RadioEvents.RxDone = OnRxDone; |
rba90 | 0:e2ccabf3f30c | 104 | RadioEvents.RxError = OnRxError; |
rba90 | 0:e2ccabf3f30c | 105 | RadioEvents.TxTimeout = OnTxTimeout; |
rba90 | 0:e2ccabf3f30c | 106 | RadioEvents.RxTimeout = OnRxTimeout; |
rba90 | 0:e2ccabf3f30c | 107 | Radio.Init( &RadioEvents ); |
rba90 | 0:e2ccabf3f30c | 108 | |
rba90 | 0:e2ccabf3f30c | 109 | // verify the connection with the board |
rba90 | 0:e2ccabf3f30c | 110 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
rba90 | 0:e2ccabf3f30c | 111 | { |
rba90 | 18:3e6483550f25 | 112 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 113 | debug( "Radio could not be detected!\n\r" ); |
rba90 | 18:3e6483550f25 | 114 | #endif |
rba90 | 0:e2ccabf3f30c | 115 | wait( 1 ); |
rba90 | 0:e2ccabf3f30c | 116 | } |
rba90 | 18:3e6483550f25 | 117 | #ifdef DEBUG_ALOHA |
rba90 | 0:e2ccabf3f30c | 118 | printf("RadioRegVersion: %d\r\n", Radio.Read( REG_VERSION )); |
rba90 | 18:3e6483550f25 | 119 | #endif |
rba90 | 0:e2ccabf3f30c | 120 | } |
rba90 | 0:e2ccabf3f30c | 121 | |
rba90 | 0:e2ccabf3f30c | 122 | void AlohaTransceiver::updateSettings() |
rba90 | 0:e2ccabf3f30c | 123 | { |
rba90 | 6:f545f5aa7de3 | 124 | Radio.SetChannel( RF_FREQUENCY ); |
rba90 | 0:e2ccabf3f30c | 125 | #if USE_MODEM_LORA == 1 |
rba90 | 0:e2ccabf3f30c | 126 | |
rba90 | 0:e2ccabf3f30c | 127 | Radio.SetTxConfig( MODEM_LORA, Settings.Power, 0, Settings.Bandwidth, |
rba90 | 0:e2ccabf3f30c | 128 | Settings.Datarate, Settings.Coderate, |
rba90 | 0:e2ccabf3f30c | 129 | Settings.PreambleLen, Settings.FixLen, |
rba90 | 0:e2ccabf3f30c | 130 | Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod, |
rba90 | 0:e2ccabf3f30c | 131 | Settings.IqInverted, Settings.TxTimeout ); |
rba90 | 0:e2ccabf3f30c | 132 | |
rba90 | 0:e2ccabf3f30c | 133 | Radio.SetRxConfig( MODEM_LORA, Settings.Bandwidth, Settings.Datarate, |
rba90 | 0:e2ccabf3f30c | 134 | Settings.Coderate, 0, Settings.PreambleLen, |
rba90 | 0:e2ccabf3f30c | 135 | Settings.SymbolTimeout, Settings.FixLen, Settings.PayloadLen, |
rba90 | 0:e2ccabf3f30c | 136 | Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod, |
rba90 | 0:e2ccabf3f30c | 137 | Settings.IqInverted, Settings.RxContinuous ); |
rba90 | 0:e2ccabf3f30c | 138 | |
rba90 | 0:e2ccabf3f30c | 139 | #elif USE_MODEM_FSK == 1 |
rba90 | 0:e2ccabf3f30c | 140 | #error "FSK not implemented" |
rba90 | 0:e2ccabf3f30c | 141 | #else |
rba90 | 0:e2ccabf3f30c | 142 | #error "Please define a modem in the compiler options." |
rba90 | 0:e2ccabf3f30c | 143 | #endif |
rba90 | 0:e2ccabf3f30c | 144 | } |
rba90 | 0:e2ccabf3f30c | 145 | |
rba90 | 6:f545f5aa7de3 | 146 | void AlohaTransceiver::enable() |
rba90 | 6:f545f5aa7de3 | 147 | { |
rba90 | 6:f545f5aa7de3 | 148 | // entering passive receiver mode |
rba90 | 10:065a4b58c6ff | 149 | Radio.Rx( 0 ); |
rba90 | 6:f545f5aa7de3 | 150 | } |
rba90 | 6:f545f5aa7de3 | 151 | |
rba90 | 0:e2ccabf3f30c | 152 | void AlohaTransceiver::poll() |
rba90 | 0:e2ccabf3f30c | 153 | { |
rba90 | 0:e2ccabf3f30c | 154 | switch( State ) |
rba90 | 0:e2ccabf3f30c | 155 | { |
rba90 | 0:e2ccabf3f30c | 156 | case RX: |
rba90 | 5:c3741633dc6f | 157 | { |
rba90 | 0:e2ccabf3f30c | 158 | // create new frame instance |
rba90 | 0:e2ccabf3f30c | 159 | AlohaFrame frame(Buffer, BufferSize); |
rba90 | 0:e2ccabf3f30c | 160 | |
rba90 | 11:3e1ff29da71a | 161 | // check destination |
rba90 | 11:3e1ff29da71a | 162 | // if the destination is the device id, then processing, otherwise drop the packet and continue |
rba90 | 11:3e1ff29da71a | 163 | // listening |
rba90 | 11:3e1ff29da71a | 164 | if (frame.getDestinationAddress() == (deviceId & 0x0f)) |
rba90 | 17:c6e2e2cd6e5f | 165 | { |
rba90 | 17:c6e2e2cd6e5f | 166 | // currently we only have 1 payload per frame |
rba90 | 17:c6e2e2cd6e5f | 167 | uint8_t payload_length = frame.getPayloadLength(); |
rba90 | 17:c6e2e2cd6e5f | 168 | uint8_t payload[payload_length]; |
rba90 | 17:c6e2e2cd6e5f | 169 | uint8_t src_addr = frame.getSourceAddress(); |
rba90 | 17:c6e2e2cd6e5f | 170 | uint8_t type = frame.getType(); |
rba90 | 17:c6e2e2cd6e5f | 171 | |
rba90 | 17:c6e2e2cd6e5f | 172 | // extract payload |
rba90 | 17:c6e2e2cd6e5f | 173 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 17:c6e2e2cd6e5f | 174 | { |
rba90 | 17:c6e2e2cd6e5f | 175 | payload[i] = frame.getPayload(i); |
rba90 | 17:c6e2e2cd6e5f | 176 | } |
rba90 | 17:c6e2e2cd6e5f | 177 | |
rba90 | 11:3e1ff29da71a | 178 | // check registered callback function |
rba90 | 11:3e1ff29da71a | 179 | // execute callback functions if registered |
rba90 | 17:c6e2e2cd6e5f | 180 | |
rba90 | 17:c6e2e2cd6e5f | 181 | if (AlohaTypeCallbackTable[type] != NULL) |
rba90 | 11:3e1ff29da71a | 182 | { |
rba90 | 17:c6e2e2cd6e5f | 183 | AlohaTypeCallbackTable[type](payload, payload_length, src_addr); |
rba90 | 11:3e1ff29da71a | 184 | } |
rba90 | 0:e2ccabf3f30c | 185 | } |
rba90 | 0:e2ccabf3f30c | 186 | |
rba90 | 10:065a4b58c6ff | 187 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 188 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 189 | break; |
rba90 | 0:e2ccabf3f30c | 190 | } |
rba90 | 0:e2ccabf3f30c | 191 | case TX: |
rba90 | 0:e2ccabf3f30c | 192 | { |
rba90 | 10:065a4b58c6ff | 193 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 194 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 195 | break; |
rba90 | 0:e2ccabf3f30c | 196 | } |
rba90 | 0:e2ccabf3f30c | 197 | case RX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 198 | { |
rba90 | 10:065a4b58c6ff | 199 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 200 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 201 | break; |
rba90 | 0:e2ccabf3f30c | 202 | } |
rba90 | 0:e2ccabf3f30c | 203 | case RX_ERROR: |
rba90 | 0:e2ccabf3f30c | 204 | { |
rba90 | 10:065a4b58c6ff | 205 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 206 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 207 | break; |
rba90 | 0:e2ccabf3f30c | 208 | } |
rba90 | 0:e2ccabf3f30c | 209 | case TX_TIMEOUT: |
rba90 | 0:e2ccabf3f30c | 210 | { |
rba90 | 10:065a4b58c6ff | 211 | Radio.Rx( 0 ); |
rba90 | 0:e2ccabf3f30c | 212 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 213 | break; |
rba90 | 0:e2ccabf3f30c | 214 | } |
rba90 | 0:e2ccabf3f30c | 215 | case LOWPOWER: |
rba90 | 0:e2ccabf3f30c | 216 | { |
rba90 | 22:2e39f382f782 | 217 | // transmit packet when the radio is free |
rba90 | 23:4b51a8e27f6a | 218 | if (AlohaTxQueue.getCounter() > 0) |
rba90 | 22:2e39f382f782 | 219 | { |
rba90 | 22:2e39f382f782 | 220 | AlohaFrame *frame = AlohaTxQueue.dequeue(); |
rba90 | 22:2e39f382f782 | 221 | |
rba90 | 22:2e39f382f782 | 222 | // create a buffer for transmit |
rba90 | 22:2e39f382f782 | 223 | uint8_t frame_length = frame->getPayloadLength() + FIXED_BYTE; |
rba90 | 22:2e39f382f782 | 224 | uint8_t buffer[frame_length]; // 4 fix fields |
rba90 | 22:2e39f382f782 | 225 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 22:2e39f382f782 | 226 | |
rba90 | 22:2e39f382f782 | 227 | // copy content to buffer |
rba90 | 22:2e39f382f782 | 228 | frame->serialize(buffer); |
rba90 | 22:2e39f382f782 | 229 | |
rba90 | 22:2e39f382f782 | 230 | // send to radio |
rba90 | 22:2e39f382f782 | 231 | Radio.Send(buffer, frame_length); |
rba90 | 22:2e39f382f782 | 232 | |
rba90 | 22:2e39f382f782 | 233 | // free memory |
rba90 | 22:2e39f382f782 | 234 | delete frame; |
rba90 | 22:2e39f382f782 | 235 | } |
rba90 | 0:e2ccabf3f30c | 236 | break; |
rba90 | 0:e2ccabf3f30c | 237 | } |
rba90 | 0:e2ccabf3f30c | 238 | default: |
rba90 | 0:e2ccabf3f30c | 239 | { |
rba90 | 0:e2ccabf3f30c | 240 | State = LOWPOWER; |
rba90 | 0:e2ccabf3f30c | 241 | break; |
rba90 | 0:e2ccabf3f30c | 242 | } |
rba90 | 0:e2ccabf3f30c | 243 | } |
rba90 | 0:e2ccabf3f30c | 244 | } |
rba90 | 0:e2ccabf3f30c | 245 | |
rba90 | 23:4b51a8e27f6a | 246 | bool send(BasicPacket *packet) |
rba90 | 23:4b51a8e27f6a | 247 | { |
rba90 | 23:4b51a8e27f6a | 248 | return true; |
rba90 | 23:4b51a8e27f6a | 249 | } |
rba90 | 23:4b51a8e27f6a | 250 | |
rba90 | 17:c6e2e2cd6e5f | 251 | bool AlohaTransceiver::send(uint8_t *payload, uint8_t payload_length, uint8_t dest_addr) |
rba90 | 0:e2ccabf3f30c | 252 | { |
rba90 | 17:c6e2e2cd6e5f | 253 | // assume the user will not transmit payload longer than 16 bytes |
rba90 | 17:c6e2e2cd6e5f | 254 | if (payload_length > 16) |
rba90 | 17:c6e2e2cd6e5f | 255 | { |
rba90 | 17:c6e2e2cd6e5f | 256 | return false; |
rba90 | 17:c6e2e2cd6e5f | 257 | } |
rba90 | 17:c6e2e2cd6e5f | 258 | |
rba90 | 17:c6e2e2cd6e5f | 259 | // create a new frame |
rba90 | 22:2e39f382f782 | 260 | AlohaFrame *frame = new AlohaFrame(); |
rba90 | 17:c6e2e2cd6e5f | 261 | |
rba90 | 22:2e39f382f782 | 262 | // set properfies |
rba90 | 22:2e39f382f782 | 263 | frame->setType(AlohaFrame::Aloha_Data); |
rba90 | 22:2e39f382f782 | 264 | frame->setPayloadLength(payload_length); |
rba90 | 22:2e39f382f782 | 265 | frame->setSourceAddress(deviceId); |
rba90 | 22:2e39f382f782 | 266 | frame->setDestinationAddress(dest_addr & 0x0f); |
rba90 | 22:2e39f382f782 | 267 | frame->setFullMessageFlag(0x1); |
rba90 | 22:2e39f382f782 | 268 | frame->setSequenceID(seqid[dest_addr]++); // use dest_addr as key for accessing sequence id |
rba90 | 17:c6e2e2cd6e5f | 269 | // the seqid should increase as it successfully transmit the packet |
rba90 | 22:2e39f382f782 | 270 | |
rba90 | 22:2e39f382f782 | 271 | // set payload |
rba90 | 17:c6e2e2cd6e5f | 272 | for (uint8_t i = 0; i < payload_length; i++) |
rba90 | 17:c6e2e2cd6e5f | 273 | { |
rba90 | 22:2e39f382f782 | 274 | frame->setPayload(i, payload[i]); |
rba90 | 17:c6e2e2cd6e5f | 275 | } |
rba90 | 17:c6e2e2cd6e5f | 276 | |
rba90 | 17:c6e2e2cd6e5f | 277 | // calculate crc |
rba90 | 22:2e39f382f782 | 278 | frame->generateCrc(); |
rba90 | 17:c6e2e2cd6e5f | 279 | |
rba90 | 22:2e39f382f782 | 280 | // push frame to the back of queue |
rba90 | 22:2e39f382f782 | 281 | AlohaTxQueue.enqueue(frame); |
rba90 | 22:2e39f382f782 | 282 | |
rba90 | 17:c6e2e2cd6e5f | 283 | return true; |
rba90 | 0:e2ccabf3f30c | 284 | } |
rba90 | 0:e2ccabf3f30c | 285 | |
rba90 | 0:e2ccabf3f30c | 286 | void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 287 | { |
rba90 | 0:e2ccabf3f30c | 288 | AlohaTypeCallbackTable[type] = f; |
rba90 | 0:e2ccabf3f30c | 289 | } |
rba90 | 0:e2ccabf3f30c | 290 | |
rba90 | 0:e2ccabf3f30c | 291 | void AlohaTransceiver::deRegisterType(AlohaFrame::AlohaType_t type, aloha_callback_func f) |
rba90 | 0:e2ccabf3f30c | 292 | { |
rba90 | 0:e2ccabf3f30c | 293 | AlohaTypeCallbackTable[type] = NULL; |
rba90 | 0:e2ccabf3f30c | 294 | } |
rba90 | 0:e2ccabf3f30c | 295 | |
rba90 | 2:fa264e48d5f7 | 296 | int16_t AlohaTransceiver::getRssi() |
rba90 | 2:fa264e48d5f7 | 297 | { |
rba90 | 2:fa264e48d5f7 | 298 | return RssiValue; |
rba90 | 2:fa264e48d5f7 | 299 | } |
rba90 | 2:fa264e48d5f7 | 300 | |
rba90 | 2:fa264e48d5f7 | 301 | int8_t AlohaTransceiver::getSnr() |
rba90 | 2:fa264e48d5f7 | 302 | { |
rba90 | 2:fa264e48d5f7 | 303 | return SnrValue; |
rba90 | 2:fa264e48d5f7 | 304 | } |
rba90 | 2:fa264e48d5f7 | 305 | |
rba90 | 17:c6e2e2cd6e5f | 306 | uint8_t AlohaTransceiver::getDeviceID() |
rba90 | 11:3e1ff29da71a | 307 | { |
rba90 | 11:3e1ff29da71a | 308 | return deviceId; |
rba90 | 11:3e1ff29da71a | 309 | } |
rba90 | 11:3e1ff29da71a | 310 | |
rba90 | 17:c6e2e2cd6e5f | 311 | void AlohaTransceiver::setDeviceID(uint8_t id) |
rba90 | 11:3e1ff29da71a | 312 | { |
rba90 | 11:3e1ff29da71a | 313 | deviceId = id; |
rba90 | 11:3e1ff29da71a | 314 | } |
rba90 | 11:3e1ff29da71a | 315 | |
rba90 | 8:4bda842f73d4 | 316 | #if USE_MODEM_LORA == 1 |
rba90 | 8:4bda842f73d4 | 317 | AlohaTransceiver::LoRaSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 318 | { |
rba90 | 8:4bda842f73d4 | 319 | return &Settings; |
rba90 | 8:4bda842f73d4 | 320 | } |
rba90 | 8:4bda842f73d4 | 321 | |
rba90 | 8:4bda842f73d4 | 322 | #elif USE_MODEM_FSK == 1 |
rba90 | 8:4bda842f73d4 | 323 | AlohaTransceiver::FskSettings_t *AlohaTransceiver::getSettings() |
rba90 | 8:4bda842f73d4 | 324 | { |
rba90 | 8:4bda842f73d4 | 325 | return &Settings; |
rba90 | 8:4bda842f73d4 | 326 | } |
rba90 | 8:4bda842f73d4 | 327 | #else |
rba90 | 8:4bda842f73d4 | 328 | #error "Please define a modem in the compiler options." |
rba90 | 8:4bda842f73d4 | 329 | #endif |
rba90 | 8:4bda842f73d4 | 330 | |
rba90 | 0:e2ccabf3f30c | 331 | void OnTxDone( void ) |
rba90 | 0:e2ccabf3f30c | 332 | { |
rba90 | 0:e2ccabf3f30c | 333 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 334 | State = TX; |
rba90 | 18:3e6483550f25 | 335 | |
rba90 | 18:3e6483550f25 | 336 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 337 | debug("> OnTxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 338 | #endif |
rba90 | 0:e2ccabf3f30c | 339 | } |
rba90 | 0:e2ccabf3f30c | 340 | |
rba90 | 0:e2ccabf3f30c | 341 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
rba90 | 0:e2ccabf3f30c | 342 | { |
rba90 | 0:e2ccabf3f30c | 343 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 344 | |
rba90 | 0:e2ccabf3f30c | 345 | // safeguard: if size exceeded maximum buffer size, it will cause memory overflow |
rba90 | 0:e2ccabf3f30c | 346 | BufferSize = size ? BUFFER_SIZE : size <= BUFFER_SIZE; |
rba90 | 0:e2ccabf3f30c | 347 | |
rba90 | 0:e2ccabf3f30c | 348 | memcpy( Buffer, payload, BufferSize ); |
rba90 | 0:e2ccabf3f30c | 349 | RssiValue = rssi; |
rba90 | 0:e2ccabf3f30c | 350 | SnrValue = snr; |
rba90 | 0:e2ccabf3f30c | 351 | State = RX; |
rba90 | 18:3e6483550f25 | 352 | |
rba90 | 18:3e6483550f25 | 353 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 354 | debug("> OnRxDone\n\r" ); |
rba90 | 18:3e6483550f25 | 355 | #endif |
rba90 | 0:e2ccabf3f30c | 356 | } |
rba90 | 0:e2ccabf3f30c | 357 | |
rba90 | 0:e2ccabf3f30c | 358 | void OnTxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 359 | { |
rba90 | 0:e2ccabf3f30c | 360 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 361 | State = TX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 362 | |
rba90 | 18:3e6483550f25 | 363 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 364 | debug("> OnTxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 365 | #endif |
rba90 | 0:e2ccabf3f30c | 366 | } |
rba90 | 0:e2ccabf3f30c | 367 | |
rba90 | 0:e2ccabf3f30c | 368 | void OnRxTimeout( void ) |
rba90 | 0:e2ccabf3f30c | 369 | { |
rba90 | 0:e2ccabf3f30c | 370 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 371 | Buffer[ BufferSize ] = 0; |
rba90 | 0:e2ccabf3f30c | 372 | State = RX_TIMEOUT; |
rba90 | 18:3e6483550f25 | 373 | |
rba90 | 18:3e6483550f25 | 374 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 375 | debug("> OnRxTimeout\n\r" ); |
rba90 | 18:3e6483550f25 | 376 | #endif |
rba90 | 0:e2ccabf3f30c | 377 | } |
rba90 | 0:e2ccabf3f30c | 378 | |
rba90 | 0:e2ccabf3f30c | 379 | void OnRxError( void ) |
rba90 | 0:e2ccabf3f30c | 380 | { |
rba90 | 0:e2ccabf3f30c | 381 | Radio.Sleep( ); |
rba90 | 0:e2ccabf3f30c | 382 | State = RX_ERROR; |
rba90 | 18:3e6483550f25 | 383 | |
rba90 | 18:3e6483550f25 | 384 | #ifdef DEBUG_ALOHA |
rba90 | 18:3e6483550f25 | 385 | debug( "> OnRxError\n\r" ); |
rba90 | 18:3e6483550f25 | 386 | #endif |
rba90 | 0:e2ccabf3f30c | 387 | } |