ENEL400 / Mbed 2 deprecated LoRaTerminal

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Files at this revision

API Documentation at this revision

Comitter:
rba90
Date:
Tue Aug 23 04:50:45 2016 +0000
Parent:
15:cfc7d817e444
Child:
17:7093a533b4ae
Commit message:
add function that report rssi and snr when transmit

Changed in this revision

AlohaTransceiver.lib Show annotated file Show diff for this revision Revisions of this file
SerialInterfaceProtocol.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AlohaTransceiver.lib	Fri Aug 12 03:34:51 2016 +0000
+++ b/AlohaTransceiver.lib	Tue Aug 23 04:50:45 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ENEL400/code/AlohaTransceiver/#c6e2e2cd6e5f
+http://developer.mbed.org/teams/ENEL400/code/AlohaTransceiver/#3e6483550f25
--- a/SerialInterfaceProtocol.lib	Fri Aug 12 03:34:51 2016 +0000
+++ b/SerialInterfaceProtocol.lib	Tue Aug 23 04:50:45 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ENEL400/code/SerialInterfaceProtocol/#100865963801
+http://developer.mbed.org/teams/ENEL400/code/SerialInterfaceProtocol/#9a8085508415
--- a/main.cpp	Fri Aug 12 03:34:51 2016 +0000
+++ b/main.cpp	Tue Aug 23 04:50:45 2016 +0000
@@ -20,9 +20,22 @@
 InterruptIn button(USER_BUTTON);
 
 // sensors
-#define SUPPLY_VOLTAGE 3.3;
+#define SUPPLY_VOLTAGE 3.3f;
 AnalogIn TempSensor(PA_0);
 
+// 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;
+
 float getTemperature() 
 {
     float reading = TempSensor.read();
@@ -67,6 +80,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 (0x03)
+    packet.setCommand(0x03);
+    
+    // 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;
+}
+
 /*
  * Format: 
  * <        :start flag
@@ -450,10 +499,82 @@
                     
                     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, 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:
@@ -500,7 +621,7 @@
     
     // register callback functions for SIP
     SIP.registerCommand(0x00, toggleChecksum);
-    SIP.registerCommand(0x01, NULL);                // disable sendMessage for now
+    SIP.registerCommand(0x01, queryServiceQuality);
     SIP.registerCommand(0x02, configureRadio);
     SIP.registerCommand(0x03, radioUpdateSettings);