Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU

Revision:
14:25e836a5a7bf
Parent:
12:2ead6bfd9f2a
Child:
15:182b95c25cf2
--- a/main.cpp	Wed Aug 10 01:22:08 2016 +0000
+++ b/main.cpp	Thu Aug 11 02:38:44 2016 +0000
@@ -3,6 +3,8 @@
 #include "buffer.h"
 #include "SerialInterfaceProtocol.h"
 #include "AlohaFrame.h"
+#include "ControlPacket.h"
+#include "DataBlockPacket.h"
 
 Serial pc(USBTX, USBRX);
 
@@ -54,13 +56,13 @@
 
 /*
  * Format: 
- * <        :start flag
- * 02       :command
- * xx       :length
- * xx:      :00: get, 01: set
- * xx       :index for parameters
+ * <        : start flag
+ * 02       : command
+ * xx       : length
+ * xx:      : 00: get, 01: set
+ * xx       : index for parameters
  * ...
- * ff       :checksum
+ * ff       :checksum (not currently in used)
  * >        :end flag
  */
 int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
@@ -357,84 +359,103 @@
     return 0;
 }
 
-int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
+/*
+ * Format:
+ * <            : start flag
+ * 04           : command
+ * 02           : length (2 bytes in this case)
+ * 00/01        : query for temperature or door sensor
+ * xx           : node to query
+ * ff           : checksum (not currently in used)
+ * >            : end flag
+ */
+int querySensors(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
 {
-    static uint8_t seqid = 0;
+    // check with payloads
+    if (payload_length != 2)
+    {
+        sprintf((char *) response, "Wrong Payload Length");
+        *response_length = 20;
+        return 1;
+    } 
+    
+    // decode the user message
+    uint8_t sensor_type = payload[0];
+    uint8_t node_id = payload[1];
     
-    // prepare for the frame
-    txFrame.setType(AlohaFrame::Aloha_Data);
-    txFrame.setPayloadLength(0x0);
-    txFrame.setSourceAddress(0x1);
-    txFrame.setDestinationAddress(0x2);
-    txFrame.setFullMessageFlag(0x1);
-    txFrame.setSequenceID(seqid);
-    txFrame.generateCrc();
+    // create a command packet
+    ControlPacket packet;
+    
+    // sequence id is not in used in this case
+    packet.setSequenceID(0x0);
+    
+    // set source id as current device id
+    packet.setSourceID(aloha.getDeviceID());
+    
+    // set destination id as node id
+    packet.setDestinationID(node_id);
     
+    // set command as query (00 in this case)
+    packet.setCommand(0x00);
     
-    uint8_t buffer[20];
+    // store sensor type in data block 0
+    packet.setData(0, sensor_type);
+    
+    // generate crc
+    packet.generateCrc();
+    
+    // create buffer for transmission
+    uint8_t buffer[8];
     memset(buffer, 0x0, sizeof(buffer));
-    txFrame.serialize(buffer);
-    
-    aloha.send(buffer, 20);
-    
-    seqid += 1;
     
-    return 0;   
-}
-
-int userQuery(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
-{
-    // send query data to client
-    // make some dummy response
-    response[0] = 1; // temperature sensor
-    sprintf((char *)response + 1, "22.4");
-    *response_length = 5;
+    // copy bytes into buffer
+    packet.serialize(buffer);
+    
+    // send to aloha transceiver
+    aloha.send(buffer, 8, node_id);
     
     return 0;
 }
 
-void AlohaDataEcho(AlohaFrame *frame)
-{   
-    // get rssi and snr
-    uint16_t rssi = aloha.getRssi();
-    uint8_t snr = aloha.getSnr();
-    
-    uint8_t rssi_h = (rssi & 0xff00) >> 8;
-    uint8_t rssi_l = rssi & 0xff;
+void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr)
+{
+    // try to decode packet
+    BasicPacket packet(payload);
     
-    // Transmit RSSI and SNR back to sender
-    txFrame.setType(AlohaFrame::Aloha_Data);
-    txFrame.setPayloadLength(3);
-    txFrame.setSourceAddress(aloha.getDeviceId());
-    txFrame.setDestinationAddress(frame->getSourceAddress());
-    txFrame.setFullMessageFlag(frame->getFullMessageFlag());
-    txFrame.setSequenceID(frame->getSequenceID() + 1);
-    txFrame.setPayload(0, snr);
-    txFrame.setPayload(1, rssi_h);
-    txFrame.setPayload(2, rssi_l);
-    txFrame.generateCrc();
+    // verify crc
+    if (!packet.verify())
+    {
+        printf("Oops, catch a invalid packet\r\n");
+        return;
+    }
+    
+    // process the packet based on different feature id
+    BasicPacket::L3Fid_t fid = (BasicPacket::L3Fid_t) packet.getFid();
     
-    
-    uint8_t buffer[20];
-    memset(buffer, 0x0, sizeof(buffer));
-    txFrame.serialize(buffer);
-    
-    aloha.send(buffer, 20);
-    
-    // print received message
-    printf("-----------------------------------------\r\n");
-    printf(">Received Frame\r\n");
-    printf(">    Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength());
-    printf(">    SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress());
-    printf(">    FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID());
-    for (uint8_t i = 0; i < frame->getPayloadLength(); i++)
+    // we don't care about the type conversion. just create a new one.
+    switch (fid)
     {
-        printf(">    Payload[%d]: 0x%x\r\n", i, frame->getPayload(i));
+        case BasicPacket::L3ControlPacket:
+        {
+            ControlPacket controlPacket(payload);
+            
+            // currently the base station do not respond to any control packet
+            
+            break;
+        }
+        case BasicPacket::L3DataBlockPacket:
+        {
+            DataBlockPacket dataBlockPacket(payload);
+            
+            // do something
+            break;
+        }
+        default:
+            break;
     }
-    printf(">    CRC: 0x%x\r\n", frame->getCrc());
-    printf("-----------------------------------------\r\n");
 }
 
+
 int main() {
     // initialize radio module
     aloha.boardInit();
@@ -446,13 +467,12 @@
     
     // register callback functions for SIP
     SIP.registerCommand(0x00, toggleChecksum);
-    SIP.registerCommand(0x01, sendMessage);
     SIP.registerCommand(0x02, configureRadio);
     SIP.registerCommand(0x03, radioUpdateSettings);
-    SIP.registerCommand(0x04, userQuery);
+    SIP.registerCommand(0x04, querySensors);
     
     // register callback functions for aloha transceiver
-    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho);
+    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataPacketHandler);
     
     while(1) {
         SIP.poll();