Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU

Revision:
23:378bd4d025d0
Parent:
21:0f6635739f66
Child:
24:2136cf94fedb
diff -r b7471336cffc -r 378bd4d025d0 main.cpp
--- a/main.cpp	Wed Aug 24 10:46:58 2016 +0000
+++ b/main.cpp	Wed Aug 31 03:38:21 2016 +0000
@@ -394,7 +394,7 @@
     } 
     
     // decode the user message
-    uint8_t sensor_type = payload[0];
+    uint8_t source_type = payload[0];
     uint8_t node_id = payload[1];
     
     // create a command packet
@@ -413,7 +413,7 @@
     packet.setCommand(0x0);
     
     // store sensor type in data block 0
-    packet.setData(0, sensor_type);
+    packet.setData(0, source_type);
     
     // generate crc
     packet.generateCrc();
@@ -435,6 +435,7 @@
 {
     // decode user message
     uint8_t node_id = payload[0];
+    uint8_t source_type = 0x2;          // query for service quality
     
     // create a command packet
     ControlPacket packet;
@@ -448,8 +449,11 @@
     // set destination id as node id
     packet.setDestinationID(node_id);
     
-    // set command as query (0x3)
-    packet.setCommand(0x3);
+    // set command as query (treat it similar to sensor)
+    packet.setCommand(0x0);
+    
+    // store sensor type in data block 0
+    packet.setData(0, source_type);
     
     // generate crc
     packet.generateCrc();
@@ -489,76 +493,75 @@
             uint8_t command = controlPacket.getCommand();
             switch(command)
             {
-                case 0x2:                   // received response for service quality
-                {
-                    uint8_t payload[6];
-                    
-                    // first byte is the snr of foreign host
-                    payload[0] = controlPacket.getData(0);
-                    
-                    // second and third byte are the rssi of foreign host
-                    payload[1] = controlPacket.getData(1);
-                    payload[2] = controlPacket.getData(2);
-                    
-                    // fourth byte is the snr of local host
-                    payload[3] = (uint8_t) aloha.getSnr();
-                    
-                    // fifth and sixth byte are the rssi of local host
-                    int_string_16 rssi;
-                    rssi.int16 = aloha.getRssi();
-                    
-                    payload[4] = rssi.bytes[0];
-                    payload[5] = rssi.bytes[1];
-                    
-                    // make response
-                    SIP.respond(0xf1, payload, 6);
-                    
-                    break;   
-                }
-                case 0x3:                  // request for service quality
+                case 0x0:               // query
                 {
-                    int_string_16 rssi;
-                    uint8_t snr;
-                    
-                    // get rssi and snr
-                    rssi.int16 = aloha.getRssi();
-                    snr = aloha.getSnr();
-                    
-                    // create response
-                    ControlPacket response;
-                    
-                    // set layer 3 sequence id
-                    response.setSequenceID(0x0);
-                    
-                    // set source id
-                    response.setSourceID(aloha.getDeviceID());
-                    
-                    // set destination id
-                    response.setDestinationID(src_addr);
-                    
-                    // set command as respond
-                    response.setCommand(0x2);
-                    
-                    // set payload
-                    response.setData(0, (uint8_t) snr);         // store SNR
-                    response.setData(1, (uint8_t) rssi.bytes[0]);   // store higher bits of RSSI
-                    response.setData(2, (uint8_t) rssi.bytes[1]);   // store lower bits of RSSI
-                
-                    // generate crc
-                    response.generateCrc();
-                    
-                    // create buffer for transmission
-                    uint8_t buffer[8];
-                    memset(buffer, 0x0, sizeof(buffer));
-                    
-                    // copy bytes into buffer
-                    response.serialize(buffer);
-                    
-                    // send to aloha transceiver
-                    aloha.send(buffer, 8, src_addr);
+                    // execute query
+                    uint8_t sensor_type = controlPacket.getData(0);
+                    switch (sensor_type)
+                    {
+                        case 0x0:       // temperature: NULL
+                        {
+                            break;
+                        }
+                        case 0x1:       // door: NULL
+                        {
+                            break;
+                        }
+                        case 0x2:       // service quality
+                        {
+                            int_string_16 rssi;
+                            uint8_t snr;
+                            
+                            // get rssi and snr
+                            rssi.int16 = aloha.getRssi();
+                            snr = aloha.getSnr();
+                            
+                            // create a response
+                            DataBlockPacket response;
+                            
+                            // set sequence id (the sequence id is configured as incoming sequence id + 1)
+                            response.setSequenceID(controlPacket.getSequenceID() + 1);
+                            
+                            // set source id (the source id is the current device ID)
+                            response.setSourceID(aloha.getDeviceID());
+                            
+                            // set destination id (the destination ID is the original source address)
+                            response.setDestinationID(controlPacket.getSourceID());
+                            
+                            // set source type (service quality)
+                            response.setSourceType(0x2);
+                            
+                            // set payload type (byte)
+                            response.setPayloadType(0x0);
+                            
+                            // set payload
+                            response.setData(0, (uint8_t) snr);             // store SNR
+                            response.setData(1, (uint8_t) rssi.bytes[0]);   // store higher bits of RSSI
+                            response.setData(2, (uint8_t) rssi.bytes[1]);   // store lower bits of RSSI
+                            
+                            // calculate crc
+                            response.generateCrc();
+                            
+                            // serialize and send it
+                            uint8_t buffer[8];
+                            memset(buffer, 0x0, sizeof(buffer));
+                            
+                            // copy bytes into buffer
+                            response.serialize(buffer);
+                            
+                            // send to aloha transceiver
+                            aloha.send(buffer, 8, src_addr);
+                            
+                            break;
+                        }
+                        
+                        default:
+                            break;
+                    }
                     
                     break;
                 }
+
                 default:
                     break;
             }
@@ -570,18 +573,18 @@
         {
             DataBlockPacket dataBlockPacket(payload);
             
-            uint8_t sensor_type = dataBlockPacket.getSourceType();
-            switch(sensor_type)
+            uint8_t source_type = dataBlockPacket.getSourceType();
+            switch(source_type)
             {
                 case 0x0:   // temperature sensor
                 {
                     uint8_t payload[3];
                     
                     // copy sensor type
-                    payload[0] = 0x00;           // assume 0x00 is temperature sensor
+                    payload[0] = 0x0;           // assume 0x00 is temperature sensor
                     
                     // copy sensor data
-                    payload[1] = src_addr;       // assume we only use one byte
+                    payload[1] = dataBlockPacket.getSourceID();       // assume we only use one byte
                     
                     // copy address of source node
                     payload[2] = dataBlockPacket.getData(0);
@@ -596,10 +599,10 @@
                     uint8_t payload[3];
                     
                     // copy sensor type
-                    payload[0] = 0x01;
+                    payload[0] = 0x1;
                     
                     // copy sensor data
-                    payload[1] = src_addr;
+                    payload[1] = dataBlockPacket.getSourceID();
                     
                     // copy address of source node
                     payload[2] = dataBlockPacket.getData(0);
@@ -609,11 +612,43 @@
                     
                     break;
                 }
+                case 0x2:   // service quality
+                {
+                    uint8_t payload[8];
+                    
+                    // copy source type
+                    payload[0] = 0x2;
+                    
+                    // copy sensor data
+                    payload[1] = dataBlockPacket.getSourceID();
+                    
+                    // first byte is the snr of foreign host
+                    payload[2] = dataBlockPacket.getData(0);
+                    
+                    // second and third byte are the rssi of foreign host
+                    payload[3] = dataBlockPacket.getData(1);
+                    payload[4] = dataBlockPacket.getData(2);
+                    
+                    // fourth byte is the snr of local host
+                    payload[5] = (uint8_t) aloha.getSnr();
+                    
+                    // fifth and sixth byte are the rssi of local host
+                    int_string_16 rssi;
+                    rssi.int16 = aloha.getRssi();
+                    
+                    payload[6] = rssi.bytes[0];
+                    payload[7] = rssi.bytes[1];
+                    
+                    // make response
+                    SIP.respond(0xf0, payload, 8);
+                    
+                    break;
+                }
+                
                 default:
                     break;
             }
             
-            // do something
             break;
         }
         default: