Basic MAC data interface for LoRa transceiver

Dependencies:   L2Frame crc

Dependents:   LoRaBaseStation LoRaTerminal

Revision:
25:bcd1cee4e5a8
Parent:
24:8f31c33c7675
Child:
26:e87c8d345644
--- a/AlohaTransceiver.cpp	Fri Sep 02 04:43:42 2016 +0000
+++ b/AlohaTransceiver.cpp	Sat Sep 03 01:03:30 2016 +0000
@@ -158,28 +158,40 @@
             // create new frame instance
             AlohaFrame frame(Buffer, BufferSize);
             
+            // 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)
+            {
+#ifdef DEBUG_ALOHA
+                printf("TXACK::SEQID:0x%x\r\n", frame.getSequenceID());
+#endif 
+            }
+            
             // check destination
             // if the destination is the device id, then processing, otherwise drop the packet and continue
             // listening
             if (frame.getDestinationAddress() == (deviceId & 0x0f))
-            {               
-                // currently we only have 1 payload per frame
-                uint8_t payload_length = frame.getPayloadLength();
-                uint8_t payload[payload_length];
-                uint8_t src_addr = frame.getSourceAddress();
-                uint8_t type = frame.getType();
-                
-                // extract payload
-                for (uint8_t i = 0; i < payload_length; i++)
-                {
-                    payload[i] = frame.getPayload(i);
-                }
-                
+            {
                 // 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);
                 }
             }
@@ -219,6 +231,10 @@
             {
                 AlohaFrame *frame = AlohaTxQueue.dequeue();
                 
+#ifdef DEBUG_ALOHA
+                printf("\r\nTXDATA:ADDR:0x%x, SEQID:0x%x\r\n", frame->getDestinationAddress(), frame->getSequenceID());
+#endif
+                
                 // create a buffer for transmit
                 uint8_t frame_length = frame->getPayloadLength() + FIXED_BYTE;
                 uint8_t buffer[frame_length];         // 4 fix fields
@@ -299,6 +315,44 @@
     return true;
 }
 
+void AlohaTransceiver::sendAck(AlohaFrame *inFrame)
+{
+    // create a new frame by calling it's constructor
+    AlohaFrame *outFrame = new AlohaFrame();
+    
+    // set frame type
+    outFrame->setType(AlohaFrame::Aloha_ACK);
+    
+    // set payload length (0 byte payload for ack frame)
+    outFrame->setPayloadLength(0);
+    
+    // set mac layer device id
+    outFrame->setSourceAddress(deviceId);
+    
+    // set mac layer destination id
+    outFrame->setDestinationAddress(inFrame->getSourceAddress());
+    
+    // set full message flag
+    outFrame->setFullMessageFlag(0x1);
+    
+    // set seqid
+    // the sequence id is always the source id + 1
+    outFrame->setSequenceID(inFrame->getSequenceID() + 1);
+    
+    // no payload
+    
+    // generate cec
+    outFrame->generateCrc();
+    
+    // push frame to the back of queue
+    AlohaTxQueue.enqueue(outFrame);
+    
+    // debug
+#ifdef DEBUG_ALOHA
+    printf("\r\nTXACK::ADDR:0x%x, SEQID:0x%x\r\n", outFrame->getDestinationAddress(), outFrame->getSequenceID());
+#endif
+}
+
 void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f)
 {
     AlohaTypeCallbackTable[type] = f;