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:
19:af268b4202f0
Parent:
16:2b3715dd0f13
Child:
20:b25436619b59
Child:
22:b69940ec3452
--- a/main.cpp	Sat Jun 29 02:16:00 2019 +0000
+++ b/main.cpp	Sun Jul 07 12:31:42 2019 -0300
@@ -1,5 +1,3 @@
-// #define DEBUG_SGAM
-
 #include "mbed.h"
 #include "sgam_mdw.h"
 
@@ -28,6 +26,9 @@
 static void lora_event_handler(lorawan_event_t event);
 static int16_t count_message;
 
+EventQueue ev_queue;
+static lorawan_app_callbacks_t callbacks;
+
 int main() {
     D_LOG("=============== INICIO ===============\r\n");
 
@@ -37,46 +38,45 @@
     Communication<LoraData>* comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN");
 
     ////////////////////////////////////////////////////////////////////////////////////////////
-    // 1 - configura os callbacks do loran WAN e inicializa !!
-    LoraData* data = new LoraData(LORAWAN_DEV_EUI, LORAWAN_APP_EUI, LORAWAN_APP_KEY, (LoRaRadio*)&radio);
+    // 1 - Configura os callbacks do loran WAN e inicializa os dados !!
+    // Dados de Conexão
+    lorawan_connect_t connect_params;
+    connect_params.connect_type = LORAWAN_CONNECTION_OTAA;
 
-    // TODO: 
-    /**
-     * 1 - funcao de callbakc
-     * 2 - configuracoes de connecção
-     */
+    connect_params.connection_u.otaa.dev_eui = LORAWAN_DEV_EUI;
+    connect_params.connection_u.otaa.app_eui = LORAWAN_APP_EUI;
+    connect_params.connection_u.otaa.app_key = LORAWAN_APP_KEY;
+    connect_params.connection_u.otaa.nb_trials = 10;
 
-    if( comm->initialize(data) != 1 ) {
+    LoraData* data = new LoraData(&connect_params, (LoRaRadio*)&radio, &ev_queue );
+    callbacks.events = mbed::callback(lora_event_handler);
+    data->prepareCallBack(callbacks);
+
+    /////////////////////////////////////////////////////////////////////////////////////////////
+    // 2 - INICIALIZA A CONEXAO
+    if( comm->initialize(data) != TRUE) {
         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 !");
+    // 3 - Tentar conectar 
+    lorawan_status_t retcode = (lorawan_status_t) comm->connect();
+    if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
+        printf("Connection - In Progress ...\r\n");
+    } else {
+        printf("Connection error, code = %d \r\n", retcode);
         return -1;
     }
 
-    // int count = 0;
-
-    // Sensor<float>* temp = (Sensor<float>*) control->getSensor("Temperature");
-    // // Sensor<GyroscopeData>* gyro = (Sensor<GyroscopeData>*) control->getSensor("Gyroscope");
-    
-    // while( count++ < 10) {
-    //     float* f = temp->getValue();
-    //     D_LOG("Temperatura => %f \r\n", *f);
-        
-    //     wait(1.5);
-    // }
+    // make your event queue dispatching events forever
+    ev_queue.dispatch_forever();
 
     control->finalize();
     D_LOG("=============== FINAL ===============\r\n");
     return 1;
 }
 
-
-
 /**
  * Receive a message from the Network Server
  */
@@ -143,47 +143,47 @@
  */
 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;
+    // 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");
-    }
+    //     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");
+    // }
 }