Projeto teste do Programa de integracao com biblioteca SGAM_MDW !! Aqui visa um usuario que usa o projeto implementado para a placa NUCLEO F429ZI

Dependencies:   sgam_mdw sgam_mdw_NUCLEOF429ZI_impl Cayenne-LPP

Revision:
16:2b3715dd0f13
Parent:
14:8ec6bdff6f67
Child:
17:579f6353c6fb
Child:
21:1a2ecd140da8
--- a/main.cpp	Thu Jun 27 02:49:26 2019 +0000
+++ b/main.cpp	Sat Jun 29 02:16:00 2019 +0000
@@ -6,6 +6,8 @@
 #include "ControlImpl.h"
 #include "Gyroscope.h"
 #include "LoRAWanComm.h"
+#include "LoRaWANInterface.h"
+#include "lora_radio_helper.h"
 
 ControlImpl ctrl;
 
@@ -19,9 +21,12 @@
 static uint8_t LORAWAN_APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xD0, 0x2F };
 static uint8_t LORAWAN_APP_KEY[] = { 0x75, 0xC6, 0xF2, 0xFB, 0xEE, 0xA9, 0x82, 0x6C, 0xA0, 0xBD, 0xB9, 0x0F, 0xC9, 0xEC, 0xF7, 0x10 };
 
-static uint32_t DEVADDR_1 = 0x2601177B;
-static uint8_t NWKSKEY_1[] = { 0x2A, 0xE9, 0x3F, 0x5E, 0x4C, 0x25, 0xDC, 0x34, 0x16, 0x82, 0x1B, 0xD1, 0x1A, 0x7F, 0xD0, 0xF6 };
-static uint8_t APPSKEY_1[] = { 0x78, 0x59, 0x61, 0xBB, 0x6B, 0xB7, 0xDE, 0x57, 0x80, 0x74, 0xAF, 0xED, 0x10, 0xD7, 0x47, 0x18 };
+// static uint32_t DEVADDR_1 = 0x2601177B;
+// static uint8_t NWKSKEY_1[] = { 0x2A, 0xE9, 0x3F, 0x5E, 0x4C, 0x25, 0xDC, 0x34, 0x16, 0x82, 0x1B, 0xD1, 0x1A, 0x7F, 0xD0, 0xF6 };
+// static uint8_t APPSKEY_1[] = { 0x78, 0x59, 0x61, 0xBB, 0x6B, 0xB7, 0xDE, 0x57, 0x80, 0x74, 0xAF, 0xED, 0x10, 0xD7, 0x47, 0x18 };
+
+static void lora_event_handler(lorawan_event_t event);
+static int16_t count_message;
 
 int main() {
     D_LOG("=============== INICIO ===============\r\n");
@@ -31,19 +36,28 @@
 
     Communication<LoraData>* comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN");
 
+    ////////////////////////////////////////////////////////////////////////////////////////////
     // 1 - configura os callbacks do loran WAN e inicializa !!
-    LoraData* data = new LoraData();
-    comm->initialize(data);
+    LoraData* data = new LoraData(LORAWAN_DEV_EUI, LORAWAN_APP_EUI, LORAWAN_APP_KEY, (LoRaRadio*)&radio);
 
+    // TODO: 
+    /**
+     * 1 - funcao de callbakc
+     * 2 - configuracoes de connecção
+     */
 
+    if( comm->initialize(data) != 1 ) {
+        D_LOG("Inicialização falhou !!\r\n");
+        return -1;
+    }
+
+    ////////////////////////////////////////////////////////////////////////////////////////////
     // 2 - Tentar conectar !!
     if(comm->connect() != TRUE) {
         D_LOG("Não pode connectar ao lora wan !");
         return -1;
     }
 
-
-
     // int count = 0;
 
     // Sensor<float>* temp = (Sensor<float>*) control->getSensor("Temperature");
@@ -60,3 +74,116 @@
     D_LOG("=============== FINAL ===============\r\n");
     return 1;
 }
+
+
+
+/**
+ * Receive a message from the Network Server
+ */
+static void receive_message()
+{
+    printf("receive_message()\n");
+    
+    // uint8_t rx_buffer[50] = { 0 };
+    // int16_t retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
+    //                                   sizeof(rx_buffer),
+    //                                   MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG);
+
+    // if (retcode < 0) {
+    //     printf("receive() - Error code %d \r\n", retcode);
+    //     return;
+    // }
+
+    // printf("RX Data (%d bytes): ", retcode);
+    // for (uint8_t i = 0; i < retcode; i++) {
+    //     printf("%02x ", rx_buffer[i]);
+    // }
+    printf("\r\n");
+}
+
+/**
+ * Sends a message to the Network Server
+ */
+static void send_message() {
+    //printf("send_message()\n");
+        
+    // YOUR CODE HERE
+    // int16_t temperature = 10;
+    // //printf("temperature = (%d)\n", temperature);
+    // CayenneLPP payload(50);
+    // payload.addTemperature(1, temperature);
+    // int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload.getBuffer(), payload.getSize(), MSG_UNCONFIRMED_FLAG);
+    // //printf("lorawan.send = retcode [%d]\n",retcode);
+
+    // if (retcode < 0) {
+    //     retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - Duty cycle violation\r\n")
+    //             : printf("send() - Error code %d \r\n", retcode);
+                
+    //     if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS)
+    //         printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS"); 
+
+    //     if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
+    //         //retry in 3 seconds
+    //         ev_queue.call_in(3000, send_message);
+    //     }
+    //     else {
+    //         ev_queue.call_in(TX_INTERVAL, send_message);
+    //     }
+    //     return;
+    // }
+
+    // ev_queue.call_in(TX_INTERVAL, send_message);
+
+    //receive_message();
+    //printf("%d bytes scheduled for transmission \r\n", retcode);
+}
+
+/**
+ * Event handler
+ */
+static void lora_event_handler(lorawan_event_t event)
+{
+    switch (event) {
+        case CONNECTED:
+            printf("# Connection - Successful \r\n");            
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                send_message();
+            } else {
+                // ev_queue.call_in(TX_INTERVAL, send_message);
+            }
+            break;
+
+        case DISCONNECTED:
+            // ev_queue.break_dispatch();
+            printf("# Disconnected Successfully \r\n");
+            break;
+        case RX_DONE:
+            printf("# Received message from Network Server \r\n");
+            receive_message();
+            break;
+        case RX_TIMEOUT:
+        case RX_ERROR:
+            printf("# Error in reception - Code = %d \r\n", event);
+            break;
+        case TX_DONE:    
+            count_message++;        
+            printf("# Message Sent to Network Server - Count [%d] \r\n", count_message);
+            break;
+        case TX_TIMEOUT:
+        case TX_ERROR:
+        case TX_CRYPTO_ERROR:
+        case TX_SCHEDULING_ERROR:
+            printf("# Transmission Error - EventCode = %d \r\n", event);
+            break;        
+        case JOIN_FAILURE:
+            printf("# OTAA Failed - Check Keys \r\n");
+            break;
+        case UPLINK_REQUIRED:
+            printf("# Uplink required by NS \r\n");
+            send_message();
+            break;
+        default:
+            printf("# Unknown Event \r\n");
+            MBED_ASSERT("# Unknown Event");
+    }
+}