SW encryption

Revision:
48:b2afcf2d41fb
Parent:
47:b6d132f1079f
Child:
49:3b27f622a798
--- a/main.cpp	Sun Jan 27 21:45:34 2019 +0000
+++ b/main.cpp	Tue Mar 19 16:06:19 2019 +0000
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include <stdio.h>
+#include <iostream>
 
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
@@ -25,13 +26,15 @@
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
+#include "sha256.h"
+
 using namespace events;
 
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 // This example only communicates with much shorter messages (<30 bytes).
 // If longer messages are used, these buffers must be changed accordingly.
-uint8_t tx_buffer[30];
-uint8_t rx_buffer[30];
+uint8_t tx_buffer[44];
+uint8_t rx_buffer[44];
 
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
@@ -96,7 +99,7 @@
     setup_trace();
 
     // stores the status of a call to LoRaWAN protocol
-    lorawan_status_t retcode;
+    lorawan_status_t loraStatus;
 
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
@@ -128,15 +131,14 @@
 
     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
-    retcode = lorawan.connect();
+    loraStatus = lorawan.connect();
 
-    if (retcode == LORAWAN_STATUS_OK ||
-            retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
+    if (loraStatus == LORAWAN_STATUS_OK ||
+            loraStatus == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
-        printf("\r\n Connection error, code = %d \r\n", retcode);
+        printf("\r\n Connection error, code = %d \r\n", loraStatus);
         return -1;
     }
-
     printf("\r\n Connection - In Progress ...\r\n");
 
     // make your event queue dispatching events forever
@@ -148,45 +150,46 @@
 /**
  * Sends a message to the Network Server
  */
-static void send_message()
-{
-    uint16_t packet_len;
-    int16_t retcode;
-    float sensor_value;
+static void send_message() {
+  uint16_t packet_cont;
+  int16_t loraStatus;
+  /*****************************************************************************************************/
+
+  uint8_t hash_content = 77;
+  char hash_str[] = "";
+  sprintf(hash_str, "%d", hash_content);
 
-    if (ds1820.begin()) {
-        ds1820.startConversion();
-        sensor_value = ds1820.read();
-        printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value);
-        ds1820.startConversion();
-    } else {
-        printf("\r\n No sensor found \r\n");
-        return;
-    }
+  static const unsigned char * hash_buffer = (const unsigned char * ) hash_str;
+  static const size_t hash_len = strlen(hash_str);
+  unsigned char output[32];
 
-    packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %3.1f",
-                         sensor_value);
+  mbedtls_sha256(hash_buffer, hash_len, output, 0);
+
+  const char * c = (const char * ) output;
+
+  /*****************************************************************************************************/
 
-    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
-                           MSG_UNCONFIRMED_FLAG);
+  packet_cont = sprintf((char * ) tx_buffer, c);
 
-    if (retcode < 0) {
-        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
-        : printf("\r\n send() - Error code %d \r\n", retcode);
+  loraStatus = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_cont,
+    MSG_UNCONFIRMED_FLAG);
+
+  if (loraStatus < 0) {
+    loraStatus == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") :
+      printf("\r\n send() - Error code %d \r\n", loraStatus);
 
-        if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
-            //retry in 3 seconds
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                ev_queue.call_in(3000, send_message);
-            }
-        }
-        return;
+    if (loraStatus == LORAWAN_STATUS_WOULD_BLOCK) {
+      //retry in 3 seconds
+      if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+        ev_queue.call_in(3000, send_message);
+      }
     }
+    return;
+  }
 
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
-    memset(tx_buffer, 0, sizeof(tx_buffer));
+  printf("\r\n %d bytes scheduled for transmission \r\n", loraStatus);
+  memset(tx_buffer, 0, sizeof(tx_buffer));
 }
-
 /**
  * Receive a message from the Network Server
  */
@@ -267,4 +270,4 @@
     }
 }
 
-// EOF
+// EOF
\ No newline at end of file