Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU

Revision:
19:6a59fb0ee921
Parent:
18:f37e567f0497
Child:
20:55e222eeede1
diff -r f37e567f0497 -r 6a59fb0ee921 main.cpp
--- a/main.cpp	Tue Aug 23 00:23:47 2016 +0000
+++ b/main.cpp	Tue Aug 23 04:50:11 2016 +0000
@@ -17,6 +17,20 @@
 AlohaTransceiver aloha(DEVICE_ID);
 AlohaFrame txFrame;
 
+// convert fp32 to 4 byte string
+typedef union
+{
+    float fp32;
+    uint8_t bytes[4];
+} float_bytes_32;
+
+typedef union
+{
+    int16_t int16;
+    uint8_t bytes[2];
+} int_string_16;
+    
+
 void serialInterruptHandler() {
     // Note: you need to actually read from the serial to clear the RX interrupt
     int c = pc.getc();
@@ -395,8 +409,8 @@
     // set destination id as node id
     packet.setDestinationID(node_id);
     
-    // set command as query (00 in this case)
-    packet.setCommand(0x00);
+    // set command as query (0 in this case)
+    packet.setCommand(0x0);
     
     // store sensor type in data block 0
     packet.setData(0, sensor_type);
@@ -417,6 +431,42 @@
     return 0;
 }
 
+int queryServiceQuality(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
+{
+    // decode user message
+    uint8_t node_id = payload[0];
+    
+    // create a command packet
+    ControlPacket packet;
+    
+    // layer 3 sequence id is not 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 (0x3)
+    packet.setCommand(0x3);
+    
+    // generate crc
+    packet.generateCrc();
+    
+    // create buffer for transmission
+    uint8_t buffer[8];
+    memset(buffer, 0x0, sizeof(buffer));
+    
+    // copy bytes into buffer
+    packet.serialize(buffer);
+    
+    // send to aloha transceiver
+    aloha.send(buffer, 8, node_id);
+    
+    return 0;
+}
+
 void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr)
 {
     // try to decode packet
@@ -435,10 +485,87 @@
         {
             ControlPacket controlPacket(payload);
             
-            // currently the base station do not respond to any control packet
+             // execute command
+            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, 3);
+                    
+                    break;   
+                }
+                case 0x3:                  // respond 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
+                    packet.generateCrc();
+                    
+                    // create buffer for transmission
+                    uint8_t buffer[8];
+                    memset(buffer, 0x0, sizeof(buffer));
+                    
+                    // copy bytes into buffer
+                    packet.serialize(buffer);
+                    
+                    // send to aloha transceiver
+                    aloha.send(buffer, 8, src_addr);
+                    
+                    break;
+                }
+                default:
+                    break;
+            }
             
             break;
         }
+        
         case BasicPacket::L3DataBlockPacket:
         {
             DataBlockPacket dataBlockPacket(payload);
@@ -506,7 +633,7 @@
     
     // register callback functions for SIP
     SIP.registerCommand(0x00, toggleChecksum);
-    SIP.registerCommand(0x01, NULL);                    // send message is not available for the base station
+    SIP.registerCommand(0x01, queryServiceQuality);
     SIP.registerCommand(0x02, configureRadio);
     SIP.registerCommand(0x03, radioUpdateSettings);
     SIP.registerCommand(0x04, querySensors);