Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Revision:
20:ff70a415660a
Parent:
18:ce8dc94163d8
Child:
22:fea9289f31b3
--- a/main.cpp	Wed Aug 24 10:53:16 2016 +0000
+++ b/main.cpp	Wed Aug 31 03:39:04 2016 +0000
@@ -43,6 +43,12 @@
     return (output_voltage - 0.25) / 0.028;
 }
 
+bool getDoorState()
+{
+    // TODO: Jamie, you need to implement this!
+    return true;
+}
+
 void serialInterruptHandler() {
     // Note: you need to actually read from the serial to clear the RX interrupt
     int c = pc.getc();
@@ -84,6 +90,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;
@@ -97,8 +104,11 @@
     // set destination id as node id
     packet.setDestinationID(node_id);
     
-    // set command as query (0x03)
-    packet.setCommand(0x03);
+    // 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();
@@ -456,22 +466,22 @@
                             // create a response
                             DataBlockPacket response;
                             
-                            // set sequence id
-                            response.setSequenceID(0x0);
+                            // set sequence id (the sequence id is configured as incoming sequence id + 1)
+                            response.setSequenceID(controlPacket.getSequenceID() + 1);
                             
-                            // set source id
+                            // set source id (the source id is the current device ID)
                             response.setSourceID(aloha.getDeviceID());
                             
-                            // set destination id
-                            response.setDestinationID(src_addr);
+                            // set destination id (the destination ID is the original source address)
+                            response.setDestinationID(controlPacket.getSourceID());
                             
                             // set source type (temperature)
                             response.setSourceType(0x0);
                             
-                            // set payload type
-                            response.setPayloadType(0x0);
+                            // set payload type (signed integer)
+                            response.setPayloadType(0x9);
                             
-                            // copy temperature
+                            // copy temperature (use first byte)
                             response.setData(0, temp);
                             
                             // calculate crc
@@ -491,6 +501,89 @@
                         }
                         case 0x1:   // door sensor
                         {
+                            bool doorState = getDoorState();
+                            
+                            // 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 (door state)
+                            response.setSourceType(0x1);
+                            
+                            // set payload type (boolean)
+                            response.setPayloadType(0x8);
+                            
+                            // copy door state (first byte)
+                            response.setData(0, doorState);
+                            
+                            // 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;
+                        }
+                        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:
@@ -500,77 +593,6 @@
                     break;
                 }
                 
-                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
-                {
-                    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);
-                    
-                    break;
-                }
-                
                 default:
                     break;
             }
@@ -581,7 +603,46 @@
         {
             DataBlockPacket dataBlockPacket(payload);
             
-            // do something
+            uint8_t source_type = dataBlockPacket.getSourceType();
+            switch(source_type)
+            {
+                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;
+            }
+            
             break;
         }
         default: