mQ Branch for NA mote testing

Dependencies:   LoRaWAN-lib SX1272Lib-mQ lib_gps lib_mma8451q lib_mpl3115a2 mbed

Fork of LoRaWAN-NAMote72-Application-Demo by Semtech

Revision:
0:69f2e28d12c1
Child:
5:6ffeac53b7cb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/main.cpp	Tue May 17 00:21:55 2016 +0000
@@ -0,0 +1,237 @@
+/*
+ / _____)             _              | |
+( (____  _____ ____ _| |_ _____  ____| |__
+ \____ \| ___ |    (_   _) ___ |/ ___)  _ \
+ _____) ) ____| | | || |_| ____( (___| | | |
+(______/|_____)_|_|_| \__)_____)\____)_| |_|
+    (C)2015 Semtech
+
+Description: LoRaMac classA device implementation
+
+License: Revised BSD License, see LICENSE.TXT file include in the project
+
+Maintainer: Miguel Luis and Gregory Cristian
+*/
+#include "mbed.h"
+#include "board.h"
+#include "radio.h"
+
+#include "LoRaMac.h"
+#include "Comissioning.h"
+#include "SerialDisplay.h"
+#include "ComplianceTest.h"
+#include "LoRaDeviceStateProc.h"
+#include "LoRaEventProc.h"
+#include "LoRaApp.h"
+
+/*!
+ * Defines a random delay for application data transmission duty cycle. 1s,
+ * value in [us].
+ */
+#define APP_TX_DUTYCYCLE_RND                        1000000
+
+
+#if( OVER_THE_AIR_ACTIVATION != 0 )
+
+uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
+uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
+uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
+
+#else
+
+uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
+uint8_t AppSKey[] = LORAWAN_APPSKEY;
+
+/*!
+ * Device address
+ */
+uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
+
+#endif
+
+/*!
+ * Application port
+ */
+uint8_t AppPort = LORAWAN_APP_PORT;
+
+/*!
+ * User application data size
+ */
+uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
+
+/*!
+ * User application data
+ */
+uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
+
+/*!
+ * Application to handle functions
+ */
+Application LoRaApp( AppData );
+
+/*!
+ * Indicates if the node is sending confirmed or unconfirmed messages
+ */
+uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
+
+/*!
+ * Defines the application data transmission duty cycle
+ */
+static uint32_t TxDutyCycleTime;
+
+/*!
+ * Timer to handle the application data transmission duty cycle
+ */
+TimerEvent_t TxNextPacketTimer;
+
+/*!
+ * Indicates if a new packet can be sent
+ */
+bool NextTx = true;
+
+/*!
+ * LoRaWAN compliance tests support data
+ */
+ComplianceTest_s ComplianceTest;
+
+/*!
+ * Indicates if the MAC layer network join status has changed.
+ */
+bool IsNetworkJoinedStatusUpdate = false;
+
+/*!
+ * Indicates if the message sent.
+ */
+bool IsTxUpdate = false;
+
+/*!
+ * Indicates if the message received in the RX window.
+ */
+bool IsRxUpdate = false;
+
+
+/**
+ * Main application entry point.
+ */
+int main( void )
+{
+
+    // Initialize board peripherals
+    BoardInit( );
+
+    // Initialize Device state
+    DeviceState = DEVICE_STATE_INIT;
+
+    while( 1 )
+    {        
+        Gps.service( );     
+        if( IsNetworkJoinedStatusUpdate == true )
+        {
+            IsNetworkJoinedStatusUpdate = false;
+
+            DeviceJoinUpdate( );
+        }
+
+        if( IsTxUpdate == true )
+        {
+            // If downlink received then update Serial Terminal and execute Rx event
+            IsTxUpdate = false;
+            // Update serial terminal
+            SerialDisplayTxUpdate( );       
+        }
+
+        if( IsRxUpdate == true )
+        {
+            // If downlink received then update Serial Terminal and execute Rx event
+            IsRxUpdate = false;
+            RxEvent( );
+            SerialDisplayRxUpdate( );            
+        }
+        
+        switch( DeviceState )
+        {
+            case DEVICE_STATE_INIT:
+            {
+                // Initialize MAC, MAC services, Primitives
+                DeviceInit( );
+
+                // Change Device state
+                DeviceState = DEVICE_STATE_JOIN;
+                break;
+            }
+            case DEVICE_STATE_JOIN:
+            {
+#if( OVER_THE_AIR_ACTIVATION != 0 )                // OTA 
+
+                // Generate DevEUI if not defined by User
+                BoardGetDevEUI( DevEui );
+
+                // Join N/w server
+                DeviceJoin( );                
+
+                // Show on serial terminal
+                SerialDisplayJoinUpdate( );
+
+                // Execute Join event
+                JoinEvent( );
+
+                // Schedule next packet transmission
+                TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE;
+                DeviceState = DEVICE_STATE_SLEEP;
+
+#else                                           // ABP
+                DeviceJoin( );
+
+                DeviceState = DEVICE_STATE_SEND;
+#endif
+                IsNetworkJoinedStatusUpdate = true;
+                break;
+            }
+            case DEVICE_STATE_SEND:
+            {
+                if( NextTx == true )
+                {
+                    // Prepare payload frame based on application port
+                    PrepareTxFrame( AppPort );
+
+                    // Send payload over the air
+                    NextTx = SendFrame( );
+
+                    // Execute transmit event
+                    TxEvent( );
+                }
+                if( ComplianceTest.Running == true )
+                {
+                    // Schedule next packet transmission as soon as possible
+                    TxDutyCycleTime = COMPLIANCE_TX_DUTYCYCLE;
+                }
+                else
+                {
+                    // Schedule next packet transmission
+                    TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
+                }
+                DeviceState = DEVICE_STATE_SLEEP;
+                break;
+            }
+            case DEVICE_STATE_CYCLE:
+            {
+                // Schedule next packet transmission
+                TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
+                TimerStart( &TxNextPacketTimer );
+
+                DeviceState = DEVICE_STATE_SLEEP;
+                break;
+            }
+            case DEVICE_STATE_SLEEP:
+            {
+                // Wake up through events
+                break;
+            }
+            default:
+            {
+                DeviceState = DEVICE_STATE_INIT;
+                break;
+            }
+        }
+    }
+}