wayne roberts / Mbed OS LoRaWAN_singlechannel_gateway

Dependencies:   SX127x sx12xx_hal

Files at this revision

API Documentation at this revision

Comitter:
dudmuck
Date:
Mon Aug 14 11:32:56 2017 -0700
Parent:
17:763412df6872
Child:
19:c20591c95746
Commit message:
had print filtering for mac and application layers

Changed in this revision

commands.h Show annotated file Show diff for this revision Revisions of this file
lorawan.cpp Show annotated file Show diff for this revision Revisions of this file
lorawan.h 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/commands.h	Fri Aug 11 15:37:39 2017 -0700
+++ b/commands.h	Mon Aug 14 11:32:56 2017 -0700
@@ -5,5 +5,5 @@
 #define CMD_PWM             0x08
 #define CMD_TX_POWER        0x09
 
-#define SENSOR_PORT     11
+#define SENSOR_PORT     12
 #define TEXT_PORT       16
--- a/lorawan.cpp	Fri Aug 11 15:37:39 2017 -0700
+++ b/lorawan.cpp	Mon Aug 14 11:32:56 2017 -0700
@@ -28,7 +28,7 @@
 uint8_t LoRaWan::user_downlink[128];
 volatile uint16_t LoRaWan::rx_slot;
 volatile uint32_t LoRaWan::rx_ms;    // captured timer milliseconds at RxDone
-bool LoRaWan::do_downlink;
+volatile flags_t LoRaWan::flags;
 uint32_t LoRaWan::dev_addr_filter;
 
 #ifdef USE_BAND_915
@@ -389,7 +389,7 @@
     uint8_t* current = uncyphered;
     uint32_t applicationNonce = rand() & 0xffffff;  // 24bit
 
-    if (do_downlink) {
+    if (flags.do_downlink) {
         printf("SendJoinComplete(): tx busy\r\n");
         return;
     }
@@ -444,7 +444,7 @@
     lora.RegPayloadLength = current - uncyphered;
     uint32_t now_ms = timer.read_ms();
     queue.call_in(rx_ms + 100 - now_ms, send_downlink);
-    do_downlink = true;
+    flags.do_downlink = true;
     mote->FCntDown = 0;
 
     GenerateSessionKey(false, mote->app_key, network_id, applicationNonce, deviceNonce, mote->app_session_key);
@@ -650,7 +650,7 @@
 
 
     queue.call_in(rx_ms + RECEIVE_DELAY_ms - timer.read_ms(), send_downlink);
-    do_downlink = true;
+    flags.do_downlink = true;
 }
 
 void LoRaWan::parse_uplink(ota_mote_t* mote)
@@ -669,9 +669,12 @@
         rxFRMPayload_length = (lora.RegRxNbBytes - LORA_FRAMEMICBYTES) - (rxofs + 1);
         rxFRMPayload = &radio.rx_buf[rxofs+1];
         rx_fport_ptr = &radio.rx_buf[rxofs];
-        filtered_printf(mote->dev_addr, "port:%d, len:%d, ", *rx_fport_ptr, rxFRMPayload_length);
-    } else
-        filtered_printf(mote->dev_addr, "no-payload\r\n");
+        if (!flags.filter_mac)
+            filtered_printf(mote->dev_addr, "port:%d, len:%d", *rx_fport_ptr, rxFRMPayload_length);
+    } else {
+        if (!flags.filter_mac)
+            filtered_printf(mote->dev_addr, "no-payload");
+    }
 
     LoRa_GenerateDataFrameIntegrityCode(mote->network_session_key, radio.rx_buf, lora.RegRxNbBytes-LORA_FRAMEMICBYTES, rx_fhdr->DevAddr, true, rx_fhdr->FCnt, (uint8_t*)&calculated_mic);
 
@@ -680,20 +683,24 @@
     rx_mic += radio.rx_buf[lora.RegRxNbBytes-3] << 8;
     rx_mic += radio.rx_buf[lora.RegRxNbBytes-4];
     if (calculated_mic != rx_mic) {
-        filtered_printf(mote->dev_addr, "genMic:%08lx, rxMic:%08lx\r\n", calculated_mic, rx_mic);
-        filtered_printf(mote->dev_addr, "mic fail\n");
+        if (!flags.filter_mac) {
+            filtered_printf(mote->dev_addr, "genMic:%08lx, rxMic:%08lx\r\n", calculated_mic, rx_mic);
+            filtered_printf(mote->dev_addr, "mic fail\n");
+        }
         return;
     }
 
     if (rx_fport_ptr != NULL && *rx_fport_ptr == 0) {
         /* mac commands are encrypted onto port 0 */
         LoRa_EncryptPayload(mote->network_session_key, rxFRMPayload, rxFRMPayload_length, rx_fhdr->DevAddr, true, rx_fhdr->FCnt, decrypted);
-        filtered_printf(mote->dev_addr, "mac commands encrypted on port 0\r\n");
+        if (!flags.filter_mac)
+            filtered_printf(mote->dev_addr, ", mac commands encrypted on port 0");
         parse_mac_command(mote, decrypted, rxFRMPayload_length);
     } else {
         if (rx_fhdr->FCtrl.ulBits.FOptsLen > 0) {
             /* mac commands are in header */
-            filtered_printf(mote->dev_addr, "mac commands in header\r\n");
+            if (!flags.filter_mac)
+                filtered_printf(mote->dev_addr, ", mac commands in header");
             rxofs = sizeof(mhdr_t) + sizeof(fhdr_t);
             parse_mac_command(mote, &radio.rx_buf[rxofs], rx_fhdr->FCtrl.ulBits.FOptsLen);
         }
@@ -731,7 +738,7 @@
 
             lora.RegPayloadLength = tx_fhdr->FCtrl.dlBits.FOptsLen + mote->user_downlink_length + 1; // +1 for fport
             radio.tx_buf[0] = user_dowlink_mtype << 5; // MHDR
-            printf("DL-send %d\r\n", mote->user_downlink_length);
+            printf(", DL-send %d", mote->user_downlink_length);
         } else {
             /* downlink not triggered by user_downlink */
             /* downlink triggered by FOpotsLen > 0 or conf_uplink */
@@ -745,8 +752,15 @@
 
     if (decrypt_payload) {
         LoRa_EncryptPayload(mote->app_session_key, rxFRMPayload, rxFRMPayload_length, rx_fhdr->DevAddr, true, rx_fhdr->FCnt, decrypted);
-        decrypted_uplink(mote->dev_addr, decrypted, rxFRMPayload_length, *rx_fport_ptr);
+        if (!flags.filter_app) {
+            if (!flags.filter_mac)
+                LoRaWan::filtered_printf(mote->dev_addr, ", ");
+            decrypted_uplink(mote->dev_addr, decrypted, rxFRMPayload_length, *rx_fport_ptr);
+        }
     }
+
+    if (!flags.filter_app || !flags.filter_mac)
+        LoRaWan::filtered_printf(mote->dev_addr, "\r\n");
 }
 
 void LoRaWan::parse_join_req(ota_mote_t* mote)
@@ -836,22 +850,27 @@
             }
         }
 
-        filtered_printf(mote->dev_addr, "%u, %lu, %.1fdB, %ddBm, ",
-            LoRaWan::rx_slot,
-            time(NULL),
-            lora.RegPktSnrValue / 4.0,
-            lora.get_pkt_rssi()
-        );
+        if (!flags.filter_mac)
+            filtered_printf(mote->dev_addr, "%u, %lu, %.1fdB, %ddBm, ",
+                LoRaWan::rx_slot,
+                time(NULL),
+                lora.RegPktSnrValue / 4.0,
+                lora.get_pkt_rssi()
+            );
 
-        if (mhdr->bits.MType == MTYPE_UNCONF_UP)
-            filtered_printf(mote->dev_addr, "MTYPE_UNCONF_UP, ");
-        else if (mhdr->bits.MType == MTYPE_CONF_UP)
-            filtered_printf(mote->dev_addr, "MTYPE_CONF_UP, ");
+        if (!flags.filter_mac) {
+            if (mhdr->bits.MType == MTYPE_UNCONF_UP)
+                filtered_printf(mote->dev_addr, "MTYPE_UNCONF_UP, ");
+            else if (mhdr->bits.MType == MTYPE_CONF_UP)
+                filtered_printf(mote->dev_addr, "MTYPE_CONF_UP, ");
+        }
 
         if (mote != NULL) {
-            filtered_printf(mote->dev_addr, "mote:%lx, ", mote->dev_addr);
+            if (!flags.filter_mac)
+                filtered_printf(mote->dev_addr, "mote:%lx, ", mote->dev_addr);
             parse_uplink(mote);
-            filtered_printf(mote->dev_addr, "\r\n");
+            if (!flags.filter_mac)
+                filtered_printf(mote->dev_addr, "\r\n");
         } else {
             printf("mote-not-found %08lx\r\n", fhdr->DevAddr);
         }
--- a/lorawan.h	Fri Aug 11 15:37:39 2017 -0700
+++ b/lorawan.h	Mon Aug 14 11:32:56 2017 -0700
@@ -102,6 +102,16 @@
 #define N_MOTES     8
 extern ota_mote_t motes[N_MOTES];   /* from Comissioning.h */
 
+typedef struct {
+    uint8_t filter_mac : 1; 
+    uint8_t filter_app : 1; 
+    uint8_t do_downlink : 1; 
+    uint8_t get_tx_done : 1;
+    uint8_t beacon_guard : 1;
+    uint8_t restore_tx_invert : 1;
+    uint8_t restore_header_mode : 1;
+    uint8_t beacon_loaded : 1;
+} flags_t;
 
 class LoRaWan {
     private:
@@ -120,9 +130,10 @@
         static uint8_t user_downlink[];
         static const uint8_t Datarates[];
         static volatile uint32_t rx_ms;
-        static bool do_downlink;
         static uint32_t dev_addr_filter;
         static void filtered_printf(uint32_t dev_addr, const char* format, ...);
+
+        static volatile flags_t flags;
 };
 
 
--- a/main.cpp	Fri Aug 11 15:37:39 2017 -0700
+++ b/main.cpp	Mon Aug 14 11:32:56 2017 -0700
@@ -205,23 +205,24 @@
     }
 }
 
-//#define GPO_IDX     6
 void decrypted_uplink(uint32_t dev_addr, uint8_t* buf, uint8_t buflen, uint8_t port)
 {
     if (port == SENSOR_PORT) {
         uint8_t i = 0;
         uint16_t lum = buf[i++] << 8;
         lum += buf[i++];
-        LoRaWan::filtered_printf(dev_addr, "SENSOR lum:%u\r\n", lum);
+        LoRaWan::filtered_printf(dev_addr, "SENSOR lum:%u", lum);
         while (i < buflen) {
-            uint16_t a_a, a_b, p;
+            uint16_t seq, a_a, a_b, p;
+            seq = buf[i++] << 8;
+            seq += buf[i++];
             a_a = buf[i++] << 8;
             a_a += buf[i++];
             a_b = buf[i++] << 8;
             a_b += buf[i++];
             p = buf[i++];
-            LoRaWan::filtered_printf(dev_addr, "%x, %u, %u, %u, %u\r\n",
-                dev_addr,
+            LoRaWan::filtered_printf(dev_addr, ", %u, %u, %u, %u, %u",
+                seq,
                 a_a,    /* analog */
                 a_b,    /* analog */
                 (p & 2) >> 1,    /* digital in */
@@ -233,28 +234,23 @@
         LoRaWan::filtered_printf(dev_addr, "port%u: ", port);
         for (i = 0; i < buflen; i++)
             LoRaWan::filtered_printf(dev_addr, "%02x ", buf[i]);
-        LoRaWan::filtered_printf(dev_addr, "\r\n");
     }
 }
 
 EventQueue queue;
 
-volatile bool get_tx_done;
-volatile bool beacon_guard;
-bool restore_tx_invert;
-bool restore_header_mode;
 
 void send_downlink()
 {
-    if (!beacon_guard && LoRaWan::do_downlink) {            
+    if (!LoRaWan::flags.beacon_guard && LoRaWan::flags.do_downlink) {            
         radio.set_opmode(RF_OPMODE_STANDBY);
         radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
         lora.invert_tx(true);
-        restore_tx_invert = true;
+        LoRaWan::flags.restore_tx_invert = true;
         lora.setRxPayloadCrcOn(false);
         lora.start_tx(lora.RegPayloadLength);
-        LoRaWan::do_downlink = false;
-        get_tx_done = true;
+        LoRaWan::flags.do_downlink = false;
+        LoRaWan::flags.get_tx_done = true;
         tx_ms = timer.read_ms();
     }
 }
@@ -276,7 +272,7 @@
             LoRaWan::rx_ms = timer.read_ms();
             radio.set_opmode(RF_OPMODE_STANDBY);                       
             LoRaWan::parse_receive();
-            if (!LoRaWan::do_downlink) {
+            if (!LoRaWan::flags.do_downlink) {
                 /* if not sending downlink, start receiver now */
                 lora.start_rx(RF_OPMODE_RECEIVER);
             }
@@ -289,19 +285,19 @@
 volatile float beacon_send_at;
 volatile float beacon_loaded_at;
 
-volatile bool beacon_loaded;
+//volatile bool beacon_loaded;
 void
 send_beacon()
 {
     prev_beacon_send_at = beacon_send_at;
     beacon_send_at = timer.read();
 
-    if (!beacon_loaded)
+    if (!LoRaWan::flags.beacon_loaded)
         return;
 
     radio.set_opmode(RF_OPMODE_TRANSMITTER);
-    beacon_loaded = false;
-    get_tx_done = true;
+    LoRaWan::flags.beacon_loaded = false;
+    LoRaWan::flags.get_tx_done = true;
     tx_ms = timer.read_ms();
 }
 
@@ -339,12 +335,12 @@
     lora.RegPayloadLength = BEACON_SIZE;
     radio.write_reg(REG_LR_PAYLOADLENGTH, lora.RegPayloadLength);
     lora.setHeaderMode(true);
-    restore_header_mode = true;
+    LoRaWan::flags.restore_header_mode = true;
 
     if (skip_beacon_cnt > 0) {
         //printf("skip_beacon_cnt:%d\r\n", skip_beacon_cnt);
         lora.invert_tx(true);
-        restore_tx_invert = true;
+        LoRaWan::flags.restore_tx_invert = true;
         skip_beacon_cnt--;
     }
 
@@ -370,14 +366,14 @@
 
     // prepare for tx to occur in send_beacon()
     radio.set_opmode(RF_OPMODE_SYNTHESIZER_TX);
-    beacon_loaded = true;
+    LoRaWan::flags.beacon_loaded = true;
 
     beacon_loaded_at = timer.read();
 }
 
 void load_beacon()
 {
-    beacon_guard = true;
+    LoRaWan::flags.beacon_guard = true;
     queue.call_in(200, _load_beacon);
 }
 
@@ -584,7 +580,7 @@
     lora.RegIrqFlags.octet = radio.read_reg(REG_LR_IRQFLAGS);
     printLoraIrqs(false);
 
-    printf(" do_downlink:%u get_tx_done:%u, ", LoRaWan::do_downlink, get_tx_done);
+    printf(" do_downlink:%u get_tx_done:%u, ", LoRaWan::flags.do_downlink, LoRaWan::flags.get_tx_done);
     lora.RegTest33.octet = radio.read_reg(REG_LR_TEST33);     // invert_i_q
     lora.RegDriftInvert.octet = radio.read_reg(REG_LR_DRIFT_INVERT);
     printf("modemstat:%02x, rxinv:%x,%x\r\n", radio.read_reg(REG_LR_MODEMSTAT), lora.RegTest33.octet, lora.RegDriftInvert.octet);
@@ -737,6 +733,25 @@
     printf("txp index %u to mote %" PRIx32 "\r\n", txi, mote->dev_addr);
 }
 
+void cmd_filter_mac(uint8_t idx)
+{
+    LoRaWan::flags.filter_mac ^= 1;
+    printf("filter_mac:%d\r\n", LoRaWan::flags.filter_mac);
+}
+
+void cmd_filter_payload(uint8_t idx)
+{
+    LoRaWan::flags.filter_app ^= 1;
+    printf("filter_payload:%d\r\n", LoRaWan::flags.filter_app);
+}
+
+void cmd_filter_off(uint8_t idx)
+{
+    LoRaWan::flags.filter_mac = 0;
+    LoRaWan::flags.filter_app = 0;
+    printf("filter_off\r\n");
+}
+
 void cmd_beacon_endnode_txp(uint8_t idx)
 {
     unsigned txi;
@@ -783,6 +798,9 @@
     { "bp", cmd_beacon_pwm, "%u %u", "send pwm period, duty on beacon"},
     { "ntxp", cmd_endnode_txp, "%x %u", "send txpower index to dev_addr"},
     { "bntxp", cmd_beacon_endnode_txp, "%u", "send txpower index on beacon"},
+    { "fm", cmd_filter_mac, "", "toggle filter mac layer printing "},
+    { "fp", cmd_filter_payload, "", "toggle filter payload printing"},
+    { "fo", cmd_filter_off, "", "disable filtering "},
     { NULL, NULL, NULL, NULL }
 };
 
@@ -884,33 +902,33 @@
         console();
             
         if (radio.dio0) {
-            if (get_tx_done) {
-                get_tx_done = false;
+            if (LoRaWan::flags.get_tx_done) {
+                LoRaWan::flags.get_tx_done = false;
                 lora.RegIrqFlags.octet = 0;
                 lora.RegIrqFlags.bits.TxDone = 1;
                 radio.write_reg(REG_LR_IRQFLAGS, lora.RegIrqFlags.octet);        
             
-                if (restore_tx_invert) {
+                if (LoRaWan::flags.restore_tx_invert) {
                     lora.invert_tx(false);
-                    restore_tx_invert = false;
+                    LoRaWan::flags.restore_tx_invert = false;
                 }
 
-                if (restore_header_mode) {
+                if (LoRaWan::flags.restore_header_mode) {
                     lora.setHeaderMode(false);
-                    restore_header_mode = false;
+                    LoRaWan::flags.restore_header_mode = false;
                 }
                 
                 lora.start_rx(RF_OPMODE_RECEIVER);
 
-                if (beacon_guard) {   // beacon done transmitting
+                if (LoRaWan::flags.beacon_guard) {   // beacon done transmitting
                     measure_ambient();
-                    beacon_guard = false;
+                    LoRaWan::flags.beacon_guard = false;
                 }
             } else {
-                 if (!beacon_guard)
+                 if (!LoRaWan::flags.beacon_guard)
                     service_radio();
             }
-        } else if (get_tx_done) {
+        } else if (LoRaWan::flags.get_tx_done) {
             /* dio0 not yet asserted */
             uint32_t since = timer.read_ms() - tx_ms;
             if (since > 4000) {
@@ -928,12 +946,12 @@
             }
         }
         
-        if (LoRaWan::do_downlink && !beacon_guard) {  
+        if (LoRaWan::flags.do_downlink && !LoRaWan::flags.beacon_guard) {  
             uint32_t since = timer.read_ms() - LoRaWan::rx_ms;
             if (since > 110)
                 printf("since-tx:%lu\r\n", since);
             if (since > 500) {
-                LoRaWan::do_downlink = false;
+                LoRaWan::flags.do_downlink = false;
                 printf("stalled-tx\r\n");
                 cmd_rx_restart(0);
             }