Basic MAC data interface for LoRa transceiver

Dependencies:   L2Frame crc

Dependents:   LoRaBaseStation LoRaTerminal

Revision:
17:c6e2e2cd6e5f
Parent:
15:0dc9df48687a
Child:
18:3e6483550f25
diff -r dc1c26a5fd06 -r c6e2e2cd6e5f AlohaTransceiver.cpp
--- a/AlohaTransceiver.cpp	Mon Aug 08 09:37:41 2016 +0000
+++ b/AlohaTransceiver.cpp	Thu Aug 11 02:37:30 2016 +0000
@@ -31,6 +31,8 @@
 static volatile AppStates_t State;
 static RadioEvents_t RadioEvents;
 
+
+
 // callback functions for radio driver
 void OnTxDone();
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
@@ -59,6 +61,9 @@
     // store unique device id
     deviceId = id;
     
+    // initialize sequenceid
+    memset(seqid, 0x0, sizeof(seqid));
+    
     // configure properties
 #if USE_MODEM_LORA == 1
     Settings.Power = TX_OUTPUT_POWER;
@@ -151,12 +156,25 @@
             // 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
-                if (AlohaTypeCallbackTable[frame.getType()] != NULL)
+                
+                if (AlohaTypeCallbackTable[type] != NULL)
                 {
-                    AlohaTypeCallbackTable[frame.getType()](&frame);
+                    AlohaTypeCallbackTable[type](payload, payload_length, src_addr);
                 }
             }
 
@@ -200,15 +218,51 @@
     }
 }
 
-void AlohaTransceiver::send(uint8_t *buffer, int length)
+bool AlohaTransceiver::send(uint8_t *payload, uint8_t payload_length, uint8_t dest_addr)
 {
-    Radio.Send(buffer, length);
+    // assume the user will not transmit payload longer than 16 bytes
+    if (payload_length > 16)
+    {
+        return false;
+    }
+    
+    // create a new frame
+    AlohaFrame frame;
+    uint8_t frame_length = payload_length + FIXED_BYTE;
+    
+    frame.setType(AlohaFrame::Aloha_Data);
+    frame.setPayloadLength(payload_length);
+    frame.setSourceAddress(deviceId);
+    frame.setDestinationAddress(dest_addr & 0x0f);
+    frame.setFullMessageFlag(0x1);
+    frame.setSequenceID(seqid[dest_addr]++);    // use dest_addr as key for accessing sequence id
+                                                // the seqid should increase as it successfully transmit the packet
+    for (uint8_t i = 0; i < payload_length; i++)
+    {
+        frame.setPayload(i, payload[i]);
+    }
+    
+    // calculate crc
+    frame.generateCrc();
+    
+    // create a buffer for transmit
+    uint8_t buffer[frame_length];         // 4 fix fields
+    memset(buffer, 0x0, sizeof(buffer));
+    
+    // copy content to buffer
+    frame.serialize(buffer);
+    
+    // send to radio 
+    Radio.Send(buffer, frame_length);
     State = LOWPOWER;
+    
+    return true;
 }
 
-void AlohaTransceiver::send(AlohaFrame *frame)
+bool AlohaTransceiver::send(AlohaFrame *frame)
 {
-       
+    // TODO: finish this   
+    return false;
 }
 
 void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f)
@@ -231,12 +285,12 @@
     return SnrValue;
 }
 
-uint8_t AlohaTransceiver::getDeviceId()
+uint8_t AlohaTransceiver::getDeviceID()
 {
     return deviceId;
 }
 
-void AlohaTransceiver::setDeviceId(uint8_t id)
+void AlohaTransceiver::setDeviceID(uint8_t id)
 {
     deviceId = id;
 }