example for iot course

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include <stdio.h>
00018 
00019 #include "lorawan/LoRaWANInterface.h"
00020 #include "lorawan/system/lorawan_data_structures.h"
00021 #include "events/EventQueue.h"
00022 
00023 // Application helpers
00024 #include "trace_helper.h"
00025 #include "lora_radio_helper.h"
00026 
00027 using namespace events;
00028 
00029 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
00030 // This example only communicates with much shorter messages (<30 bytes).
00031 // If longer messages are used, these buffers must be changed accordingly.
00032 uint8_t tx_buffer[30];
00033 uint8_t rx_buffer[30];
00034 
00035 /*
00036  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
00037  */
00038 #define TX_TIMER                        10000
00039 
00040 /**
00041  * Maximum number of events for the event queue.
00042  * 10 is the safe number for the stack events, however, if application
00043  * also uses the queue for whatever purposes, this number should be increased.
00044  */
00045 #define MAX_NUMBER_OF_EVENTS            10
00046 
00047 /**
00048  * Maximum number of retries for CONFIRMED messages before giving up
00049  */
00050 #define CONFIRMED_MSG_RETRY_COUNTER     3
00051 
00052 
00053 /**
00054 * This event queue is the global event queue for both the
00055 * application and stack. To conserve memory, the stack is designed to run
00056 * in the same thread as the application and the application is responsible for
00057 * providing an event queue to the stack that will be used for ISR deferment as
00058 * well as application information event queuing.
00059 */
00060 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
00061 
00062 /**
00063  * Event handler.
00064  *
00065  * This will be passed to the LoRaWAN stack to queue events for the
00066  * application which in turn drive the application.
00067  */
00068 static void lora_event_handler(lorawan_event_t event);
00069 
00070 /**
00071  * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
00072  */
00073 static LoRaWANInterface lorawan(radio);
00074 
00075 /**
00076  * Application specific callbacks
00077  */
00078 static lorawan_app_callbacks_t callbacks;
00079 
00080 /**
00081  * Entry point for application
00082  */
00083 int main(void)
00084 {
00085     // setup tracing
00086     setup_trace();
00087 
00088     // stores the status of a call to LoRaWAN protocol
00089     lorawan_status_t retcode;
00090 
00091     // Initialize LoRaWAN stack
00092     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
00093         printf("\r\n LoRa initialization failed! \r\n");
00094         return -1;
00095     }
00096 
00097     printf("\r\n Mbed LoRaWANStack initialized \r\n");
00098 
00099     // prepare application callbacks
00100     callbacks.events = mbed::callback(lora_event_handler);
00101     lorawan.add_app_callbacks(&callbacks);
00102 
00103     // Set number of retries in case of CONFIRMED messages
00104     if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
00105             != LORAWAN_STATUS_OK) {
00106         printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
00107         return -1;
00108     }
00109 
00110     printf("\r\n CONFIRMED message retries : %d \r\n",
00111            CONFIRMED_MSG_RETRY_COUNTER);
00112 
00113     // Enable adaptive data rate
00114     if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
00115         printf("\r\n enable_adaptive_datarate failed! \r\n");
00116         return -1;
00117     }
00118 
00119     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
00120 
00121     retcode = lorawan.connect();
00122 
00123     if (retcode == LORAWAN_STATUS_OK ||
00124             retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
00125     } else {
00126         printf("\r\n Connection error, code = %d \r\n", retcode);
00127         return -1;
00128     }
00129 
00130     printf("\r\n Connection - In Progress ...\r\n");
00131 
00132     // make your event queue dispatching events forever
00133     ev_queue.dispatch_forever();
00134 
00135     return 0;
00136 }
00137 
00138 /**
00139  * Sends a message to the Network Server
00140  */
00141 static void send_message()
00142 {
00143     uint16_t packet_len;
00144     int16_t retcode;
00145     printf("\n\rHello world");    
00146 
00147     packet_len = sprintf((char *) tx_buffer, "Hello world");
00148 
00149     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
00150                            MSG_UNCONFIRMED_FLAG);
00151 
00152     if (retcode < 0) {
00153         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
00154         : printf("\r\n send() - Error code %d \r\n", retcode);
00155 
00156         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
00157             //retry in 3 seconds
00158             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00159                 ev_queue.call_in(3000, send_message);
00160             }
00161         }
00162         return;
00163     }
00164 
00165     printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
00166     memset(tx_buffer, 0, sizeof(tx_buffer));
00167 }
00168 
00169 /**
00170  * Event handler
00171  */
00172 static void lora_event_handler(lorawan_event_t event)
00173 {
00174     switch (event) {
00175         case CONNECTED:
00176             printf("\r\n Connection - Successful \r\n");
00177             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00178                 send_message();
00179             } else {
00180                 ev_queue.call_every(TX_TIMER, send_message);
00181             }
00182 
00183             break;
00184         case DISCONNECTED:
00185             ev_queue.break_dispatch();
00186             printf("\r\n Disconnected Successfully \r\n");
00187             break;
00188         case TX_DONE:
00189             printf("\r\n Message Sent to Network Server \r\n");
00190             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00191                 send_message();
00192             }
00193             break;
00194         case TX_TIMEOUT:
00195         case TX_ERROR:
00196         case TX_CRYPTO_ERROR:
00197         case TX_SCHEDULING_ERROR:
00198             printf("\r\n Transmission Error - EventCode = %d \r\n", event);
00199             // try again
00200             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00201                 send_message();
00202             }
00203             break;
00204         case RX_DONE:
00205             printf("\r\n Received message from Network Server \r\n");
00206             break;
00207         case RX_TIMEOUT:
00208         case RX_ERROR:
00209             printf("\r\n Error in reception - Code = %d \r\n", event);
00210             break;
00211         case JOIN_FAILURE:
00212             printf("\r\n OTAA Failed - Check Keys \r\n");
00213             break;
00214         case UPLINK_REQUIRED:
00215             printf("\r\n Uplink required by NS \r\n");
00216             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
00217                 send_message();
00218             }
00219             break;
00220         default:
00221             MBED_ASSERT("Unknown Event");
00222     }
00223 }
00224 
00225 // EOF