You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/04/19 10:42:03 UTC

[GitHub] mkiiskila closed pull request #1011: lora/node; average rssi/snr from received frames.

mkiiskila closed pull request #1011: lora/node; average rssi/snr from received frames.
URL: https://github.com/apache/mynewt-core/pull/1011
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/net/lora/node/include/node/lora.h b/net/lora/node/include/node/lora.h
index 15ebfdf26..546ffe347 100644
--- a/net/lora/node/include/node/lora.h
+++ b/net/lora/node/include/node/lora.h
@@ -277,9 +277,27 @@ extern lora_link_chk_cb lora_link_chk_cb_func;
 int lora_app_join(uint8_t *dev_eui, uint8_t *app_eui, uint8_t *app_key,
                   uint8_t trials);
 
+/**
+ * Tells whether we have successfully joined a LoRa network or not.
+ *
+ * @return LORA_APP_STATUS_ALREADY_JOINED if joined.
+ */
+int lora_node_chk_if_joined(void);
+
 /* Performs a link check */
 int lora_app_link_check(void);
 
+/**
+ * Query RSSI and SNR average for data received over LoRA.
+ *
+ * @param rssi Pointer to where to store the RSSI.
+ * @param snr Pointer to where to store the SNR.
+ *
+ * @return 0 if we have collected samples. non-zero if not.
+ */
+int lora_node_link_qual(int16_t *rssi, int16_t *snr);
+
+
 /*
  * Maximum payload that can be sent in the next frame.
  */
diff --git a/net/lora/node/include/node/lora_priv.h b/net/lora/node/include/node/lora_priv.h
index f4da4487e..3460d51ee 100644
--- a/net/lora/node/include/node/lora_priv.h
+++ b/net/lora/node/include/node/lora_priv.h
@@ -41,6 +41,12 @@ struct lora_mac_obj
     /* Link check event */
     struct os_event lm_link_chk_ev;
 
+#define LORA_DELTA_SHIFT        3
+#define LORA_AVG_SHIFT	        4
+    /* Averaging of RSSI/SNR for received frames */
+    int16_t lm_rssi_avg;
+    int16_t lm_snr_avg;
+
     /* TODO: this is temporary until we figure out a better way to deal */
     /* Transmit queue timer */
     struct os_callout lm_txq_timer;
@@ -91,6 +97,7 @@ void lora_node_chk_txq(void);
 bool lora_node_txq_empty(void);
 bool lora_mac_srv_ack_requested(void);
 uint8_t lora_mac_cmd_buffer_len(void);
+void lora_node_qual_sample(int16_t rssi, int16_t snr);
 
 /* Lora debug log */
 #define LORA_NODE_DEBUG_LOG
diff --git a/net/lora/node/src/lora_node.c b/net/lora/node/src/lora_node.c
index 9a8d0be9c..371b8909b 100644
--- a/net/lora/node/src/lora_node.c
+++ b/net/lora/node/src/lora_node.c
@@ -401,7 +401,7 @@ lora_mac_task(void *arg)
  * @return int  LORA_APP_STATUS_ALREADY_JOINED if joined.
  *              LORA_APP_STATUS_NO_NETWORK if not joined
  */
-static int
+int
 lora_node_chk_if_joined(void)
 {
     int rc;
@@ -547,6 +547,67 @@ lora_mac_link_chk_event(struct os_event *ev)
 #endif
 #endif
 
+/**
+ * Helper routine to track measurement averages.
+ *
+ * @param orig State variable
+ * @param sample Latest sample to add to average.
+ */
+static void
+lora_node_calc_avg(int16_t *orig, uint16_t sample)
+{
+    int16_t tmp;
+
+    tmp = *orig;
+    if (tmp) {
+	/*
+	 * The following magic is equivalent to algorithm
+         * avg = sample/16 + avg*15/16 in fixed point.
+	 */
+	tmp += (sample << LORA_DELTA_SHIFT) - (tmp >> LORA_AVG_SHIFT);
+	*orig = tmp;
+    } else {
+	/*
+	 * No measurement yet.
+	 */
+	*orig = sample << (LORA_AVG_SHIFT + LORA_DELTA_SHIFT);
+    }
+}
+
+void
+lora_node_qual_sample(int16_t rssi, int16_t snr)
+{
+    lora_node_calc_avg(&g_lora_mac_data.lm_rssi_avg, rssi);
+    lora_node_calc_avg(&g_lora_mac_data.lm_snr_avg, snr);
+}
+
+/**
+ * Report tracked RSSI/SNR averages
+ *
+ * @param rssi Pointer to where to store RSSI average.
+ * @param snr Pointer to where to store SNR average.
+ *
+ * @return 0 if returned data is valid, non-zero otherwise
+ */
+int
+lora_node_link_qual(int16_t *rssi, int16_t *snr)
+{
+    struct lora_mac_obj *lmo = &g_lora_mac_data;
+
+    if (lmo->lm_rssi_avg || lmo->lm_snr_avg) {
+        /*
+         * Rounds down. XXX
+         */
+        *rssi = g_lora_mac_data.lm_rssi_avg >> (LORA_AVG_SHIFT +
+                                                LORA_DELTA_SHIFT);
+        *snr = g_lora_mac_data.lm_snr_avg >> (LORA_AVG_SHIFT +
+                                              LORA_DELTA_SHIFT);
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
 struct os_eventq *
 lora_node_mac_evq_get(void)
 {
diff --git a/net/lora/node/src/mac/LoRaMac.c b/net/lora/node/src/mac/LoRaMac.c
index e52cc349b..316420eb8 100644
--- a/net/lora/node/src/mac/LoRaMac.c
+++ b/net/lora/node/src/mac/LoRaMac.c
@@ -1342,6 +1342,7 @@ lora_mac_process_radio_rx(struct os_event *ev)
                 (g_lora_mac_data.txpkt.pkt_type != MLME_JOIN)) {
                 goto process_rx_done;
             }
+            lora_node_qual_sample(rxi->rxdinfo.rssi, snr);
 
             LoRaMacJoinDecrypt( payload + 1, size - 1, LoRaMacAppKey, LoRaMacRxPayload + 1 );
 
@@ -1445,6 +1446,7 @@ lora_mac_process_radio_rx(struct os_event *ev)
                 downLinkCounter = DownLinkCounter;
             }
 
+            lora_node_qual_sample(rxi->rxdinfo.rssi, snr);
             fCtrl.Value = payload[hdrlen++];
 
             sequenceCounter = ( uint16_t )payload[hdrlen++];


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services