contain lorawan with serial_rx enabled

Dependencies:   pulga-lorawan-drv SPI_MX25R Si1133 BME280

Revision:
64:ed68ddac6360
Parent:
63:4ec1808fb547
Child:
65:4090220e19d2
--- a/main.cpp	Fri Feb 26 18:11:06 2021 +0000
+++ b/main.cpp	Tue Mar 02 16:57:31 2021 +0000
@@ -17,19 +17,20 @@
 #include <stdio.h>
 #include "mbed.h"
 
-#include "lorawan/LoRaWANInterface.h"
-#include "lorawan/system/lorawan_data_structures.h"
-#include "events/EventQueue.h"
+//#include "lorawan/LoRaWANInterface.h"
+//#include "lorawan/system/lorawan_data_structures.h"
+//#include "events/EventQueue.h"
 
 // Application helpers
 //#include "DummySensor.h"
 #include "trace_helper.h"
-#include "lora_radio_helper.h"
+//#include "lora_radio_helper.h"
 //#include "BME280.h"
 
 #include "serial.h"
 #include "gps.h"
 
+
 using namespace events;
 
 
@@ -38,8 +39,8 @@
 // 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[256];
-uint8_t rx_buffer[30];
+//uint8_t tx_buffer[256];
+//uint8_t rx_buffer[30];
 
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
@@ -93,12 +94,12 @@
  * This will be passed to the LoRaWAN stack to queue events for the
  * application which in turn drive the application.
  */
-static void lora_event_handler(lorawan_event_t event);
+//static void lora_event_handler(lorawan_event_t event);
 
 /**
  * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
  */
-static LoRaWANInterface lorawan(radio);
+//static LoRaWANInterface lorawan(radio);
 
 /**
  * Application specific callbacks
@@ -109,8 +110,8 @@
  * Entry point for application
  */
  
-mbed::DigitalOut _alive_led(P1_13, 0);
-mbed::DigitalOut _actuated_led(P1_14,1);
+//mbed::DigitalOut _alive_led(P1_13, 0);
+//mbed::DigitalOut _actuated_led(P1_14,1);
 //int lat=0;
 //int lon=0;
 int latitude=0;
@@ -170,7 +171,7 @@
 //        led1 = !led1;
     }
 }
-     
+
 void serial_rx(){
     if(pc.readable()){
         pc.printf("rx: %c\n", pc.getc());
@@ -254,35 +255,57 @@
     lorawan_status_t retcode;
 
     // Initialize LoRaWAN stack
-    if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
+    /*if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
         pc.printf("\r\n LoRa initialization failed! \r\n");
         return -1;
+    }*/
+    
+    if(lorawan_initialize_stack(&ev_queue) != 0){
+        return -1;
     }
 
     pc.printf("\r\n Mbed LoRaWANStack initialized \r\n");
 
     // prepare application callbacks
-    callbacks.events = mbed::callback(lora_event_handler);
+    /*callbacks.events = mbed::callback(lora_event_handler);
     lorawan.add_app_callbacks(&callbacks);
-
+    */
+    
+    callbacks.events = mbed::callback(lora_event_handler);
+    lorawan_add_callbacks(callbacks);
+    
     // Set number of retries in case of CONFIRMED messages
-    if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
+    /*if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
             != LORAWAN_STATUS_OK) {
         pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
         return -1;
+    }*/
+    
+    if(lorawan_set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != 0){
+        pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
+        return -1;
     }
+    
 
     pc.printf("\r\n CONFIRMED message retries : %d \r\n",
            CONFIRMED_MSG_RETRY_COUNTER);
-
+    /*
     // Enable adaptive data rate
     if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
         pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
         return -1;
     }
+    */
+    
+    if(lorawan_enable_adaptive_datarate() != 0){
+        pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
+        return -1;
+    }
+    
 
     pc.printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
-
+    
+    /*
     retcode = lorawan.connect();
 
     if (retcode == LORAWAN_STATUS_OK ||
@@ -291,19 +314,26 @@
         pc.printf("\r\n Connection error, code = %d \r\n", retcode);
         return -1;
     }
-
+    */
+    
+    if(lorawan_connect() != 0){
+        pc.printf("\r\n Connection error, code = %d \r\n", retcode);
+        return -1;
+    }
+    
     pc.printf("\r\n Connection - In Progress ...\r\n");
     
-    _actuated_led =0;
+    //_actuated_led =0;
+    //
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
-
     return 0;
 }
 
 /**
  * Sends a message to the Network Server
  */
+ /*
 static void send_message()
 {
     uint16_t packet_len;
@@ -336,11 +366,12 @@
 //    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
     memset(tx_buffer, 0, sizeof(tx_buffer));
 }
+*/
 
 /**
  * Receive a message from the Network Server
  */
-static void receive_message()
+/*static void receive_message()
 {
     uint8_t port;
     int flags;
@@ -359,20 +390,22 @@
     
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
+*/
 
 /**
  * Event handler
  */
-static void lora_event_handler(lorawan_event_t event)
+void lora_event_handler(lorawan_event_t event)
 {
     switch (event) {
         case CONNECTED:
             pc.printf("\r\n Connection - Successful \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
-            } else {
-                ev_queue.call_every(TX_TIMER, send_message);
-            }
+                //send_message();
+                lora_send_message((uint8_t*)"testeLora", (uint16_t)10);
+            } //else {
+                //ev_queue.call_every(TX_TIMER, (void)lora_send_message((uint8_t*)"testeLoraEvery", (uint16_t)15));
+            //}
 
             break;
         case DISCONNECTED:
@@ -382,7 +415,7 @@
         case TX_DONE:
 //            printf("\r\n Message Sent to Network Server \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                lora_send_message((uint8_t*)"teste", (uint16_t)6);
             }
             break;
         case TX_TIMEOUT:
@@ -398,12 +431,12 @@
 //            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
             // try again
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                lora_send_message((uint8_t*)"teste2", (uint16_t)7);
             }
             break;
         case RX_DONE:
 //            printf("\r\n Received message from Network Server \r\n");
-            receive_message();
+            lora_receive_message();
             break;
         case RX_TIMEOUT:
 //        printf("\r\n Transmission Error RX_Timeout");
@@ -417,7 +450,7 @@
         case UPLINK_REQUIRED:
 //            printf("\r\n Uplink required by NS \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                lora_send_message((uint8_t*)"uplink", (uint16_t)7);
             }
             break;
         default: