Basic MAC data interface for LoRa transceiver
Dependents: LoRaBaseStation LoRaTerminal
Diff: AlohaTransceiver.cpp
- Revision:
- 27:463688e3bd12
- Parent:
- 26:e87c8d345644
- Child:
- 28:2c0e115b4f72
--- a/AlohaTransceiver.cpp Sat Sep 03 01:11:42 2016 +0000 +++ b/AlohaTransceiver.cpp Sat Sep 03 02:02:22 2016 +0000 @@ -6,6 +6,7 @@ #include "RingBuffer.h" + // declear the type of radio state typedef enum { @@ -24,16 +25,14 @@ }AppStates_t; // radio driver related variables -static uint16_t BufferSize; -static uint8_t Buffer[BUFFER_SIZE]; - static int16_t RssiValue; static int8_t SnrValue; static volatile AppStates_t State; static RadioEvents_t RadioEvents; - +// rx queue +CircularBuffer<AlohaFrame *> AlohaRxQueue(10); // callback functions for radio driver void OnTxDone(); @@ -58,7 +57,8 @@ * Abstract interface for accessing radio driver */ -AlohaTransceiver::AlohaTransceiver(uint8_t id) +AlohaTransceiver::AlohaTransceiver(uint8_t id): + AlohaTxQueue(10) { // store unique device id deviceId = id; @@ -155,45 +155,52 @@ { case RX: { - // create new frame instance - AlohaFrame frame(Buffer, BufferSize); - - // check destination - // if the destination is the device id, then processing, otherwise drop the packet and continue - // listening - if (frame.getDestinationAddress() == (deviceId & 0x0f)) + // process packet if received + while (AlohaRxQueue.getCounter() > 0) { - // schedule the ack frame immediatly after the data frame is received - if (frame.getType() == AlohaFrame::Aloha_Data) - { - sendAck(&frame); - } - else if (frame.getType() == AlohaFrame::Aloha_ACK) + // pop from queue + AlohaFrame *frame = AlohaRxQueue.dequeue(); + + // check destination + // if the destination is the device id, then processing, otherwise drop the packet and continue + // listening + if (frame->getDestinationAddress() == (deviceId & 0x0f)) { + uint8_t type = frame->getType(); + + // schedule the ack frame immediatly after the data frame is received + if (type == AlohaFrame::Aloha_Data) + { + sendAck(frame); + } + else if (type == AlohaFrame::Aloha_ACK) + { #ifdef DEBUG_ALOHA - printf("RXACK::SEQID:0x%x\r\n", frame.getSequenceID()); + printf("RXACK::SEQID:0x%x\r\n", frame->getSequenceID()); #endif + } + + // check registered callback function + // execute callback functions if registered + if (AlohaTypeCallbackTable[type] != NULL) + { + uint8_t payload_length = frame->getPayloadLength(); + uint8_t payload[payload_length]; + uint8_t src_addr = frame->getSourceAddress(); + + // extract payload + for (uint8_t i = 0; i < payload_length; i++) + { + payload[i] = frame->getPayload(i); + } + + // execute callback function + AlohaTypeCallbackTable[type](payload, payload_length, src_addr); + } } - - // check registered callback function - // execute callback functions if registered - uint8_t type = frame.getType(); - if (AlohaTypeCallbackTable[type] != NULL) - { - uint8_t payload_length = frame.getPayloadLength(); - uint8_t payload[payload_length]; - uint8_t src_addr = frame.getSourceAddress(); - - // extract payload - for (uint8_t i = 0; i < payload_length; i++) - { - payload[i] = frame.getPayload(i); - } - - // execute callback function - AlohaTypeCallbackTable[type](payload, payload_length, src_addr); - } + // free memory + delete frame; } Radio.Rx( 0 ); @@ -307,7 +314,11 @@ // push frame to the back of queue AlohaTxQueue.enqueue(frame); - + + // debug +#ifdef DEBUG_ALOHA + printf("\r\nTXDATA::ADDR:0x%x, SEQID:0x%x\r\n", frame->getDestinationAddress(), frame->getSequenceID()); +#endif return true; } @@ -406,24 +417,31 @@ State = TX; #ifdef DEBUG_ALOHA - debug("> OnTxDone\n\r" ); + debug(0, "> OnTxDone\n\r" ); #endif } void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { + uint8_t payload_length; + Radio.Sleep( ); // safeguard: if size exceeded maximum buffer size, it will cause memory overflow - BufferSize = size ? BUFFER_SIZE : size <= BUFFER_SIZE; + payload_length = size ? BUFFER_SIZE : size <= BUFFER_SIZE; + + // create a new frame instance + AlohaFrame *frame = new AlohaFrame(payload, payload_length); + + // push onto the end of queue + AlohaRxQueue.enqueue(frame); - memcpy( Buffer, payload, BufferSize ); RssiValue = rssi; SnrValue = snr; State = RX; #ifdef DEBUG_ALOHA - debug("> OnRxDone\n\r" ); + debug(0, "> OnRxDone\n\r" ); #endif } @@ -433,18 +451,17 @@ State = TX_TIMEOUT; #ifdef DEBUG_ALOHA - debug("> OnTxTimeout\n\r" ); + debug(0, "> OnTxTimeout\n\r" ); #endif } void OnRxTimeout( void ) { Radio.Sleep( ); - Buffer[ BufferSize ] = 0; State = RX_TIMEOUT; #ifdef DEBUG_ALOHA - debug("> OnRxTimeout\n\r" ); + debug(0, "> OnRxTimeout\n\r" ); #endif } @@ -454,7 +471,7 @@ State = RX_ERROR; #ifdef DEBUG_ALOHA - debug( "> OnRxError\n\r" ); + debug(0, "> OnRxError\n\r" ); #endif }