Application example using LoRaWAN-lib MAC layer implementation
Dependencies: mbed LoRaWAN-lib SX1276Lib
Fork of LoRaWAN-demo-76 by
Diff: app/main.cpp
- Revision:
- 8:9d6683e60974
- Parent:
- 7:3173f0508a98
--- a/app/main.cpp Tue Jul 05 15:01:57 2016 +0000 +++ b/app/main.cpp Thu Aug 25 13:20:05 2016 +0000 @@ -18,7 +18,6 @@ #include "LoRaMac.h" #include "Comissioning.h" -#include "SerialDisplay.h" /*! * Defines the application data transmission duty cycle. 5s, value in [us]. @@ -125,7 +124,7 @@ /*! * User application data */ -static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; +static char AppData[LORAWAN_APP_DATA_MAX_SIZE]; /*! * Indicates if the node is sending confirmed or unconfirmed messages @@ -135,7 +134,6 @@ /*! * Defines the application data transmission duty cycle */ -static uint32_t TxDutyCycleTime; /*! * Timer to handle the application data transmission duty cycle @@ -145,7 +143,7 @@ /*! * Specifies the state of the application LED */ -static bool AppLedStateOn = false; +//static bool AppLedStateOn = false; volatile bool Led3StateChanged = false; /*! * Timer to handle the state of LED1 @@ -163,7 +161,6 @@ /*! * Indicates if a new packet can be sent */ -static bool NextTx = true; /*! * Device states @@ -201,7 +198,7 @@ /*! * Indicates if the MAC layer network join status has changed. */ -static bool IsNetworkJoinedStatusUpdate = false; + /*! * Strucure containing the Uplink status @@ -212,7 +209,7 @@ int8_t Datarate; uint16_t UplinkCounter; uint8_t Port; - uint8_t *Buffer; + char *Buffer; uint8_t BufferSize; }LoRaMacUplinkStatus; volatile bool UplinkStatusUpdated = false; @@ -232,103 +229,21 @@ }LoRaMacDownlinkStatus; volatile bool DownlinkStatusUpdated = false; -void SerialDisplayRefresh( void ) -{ - MibRequestConfirm_t mibReq; - - SerialDisplayInit( ); - SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); - -#if( OVER_THE_AIR_ACTIVATION == 0 ) - SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); - SerialDisplayUpdateDevAddr( DevAddr ); - SerialDisplayUpdateKey( 12, NwkSKey ); - SerialDisplayUpdateKey( 13, AppSKey ); -#endif - SerialDisplayUpdateEui( 5, DevEui ); - SerialDisplayUpdateEui( 6, AppEui ); - SerialDisplayUpdateKey( 7, AppKey ); - - mibReq.Type = MIB_NETWORK_JOINED; - LoRaMacMibGetRequestConfirm( &mibReq ); - SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); - - SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); -#if defined( USE_BAND_868 ) - SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); -#else - SerialDisplayUpdateDutyCycle( false ); -#endif - SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); - - SerialDisplayUpdateLedState( 3, AppLedStateOn ); -} - -void SerialRxProcess( void ) -{ - if( SerialDisplayReadable( ) == true ) - { - switch( SerialDisplayGetChar( ) ) - { - case 'R': - case 'r': - // Refresh Serial screen - SerialDisplayRefresh( ); - break; - default: - break; - } - } -} /*! * \brief Prepares the payload of the frame */ -static void PrepareTxFrame( uint8_t port ) +static void PrepareTxFrame(char* value ) { - switch( port ) - { - case 15: - { - AppData[0] = AppLedStateOn; - if( IsTxConfirmed == true ) - { - AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8; - AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter; - AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8; - AppData[4] = LoRaMacDownlinkStatus.Rssi; - AppData[5] = LoRaMacDownlinkStatus.Snr; - } - } - break; - case 224: - if( ComplianceTest.LinkCheck == true ) - { - ComplianceTest.LinkCheck = false; - AppDataSize = 3; - AppData[0] = 5; - AppData[1] = ComplianceTest.DemodMargin; - AppData[2] = ComplianceTest.NbGateways; - ComplianceTest.State = 1; - } - else - { - switch( ComplianceTest.State ) - { - case 4: - ComplianceTest.State = 1; - break; - case 1: - AppDataSize = 2; - AppData[0] = ComplianceTest.DownLinkCounter >> 8; - AppData[1] = ComplianceTest.DownLinkCounter; - break; - } - } - break; - default: - break; - } + + for( int i = 0;i < strlen(value);i++) + AppData[i] = value[i]; + //AppData[0] = 0x12; + //AppData[1] = 0x34; + //AppData[2] = 0x56; + //AppData[3] = 0x78; + //AppData[4] = 0x90; + //AppData[5] = 0x12; } /*! @@ -353,7 +268,7 @@ LoRaMacUplinkStatus.Port = 0; LoRaMacUplinkStatus.Buffer = NULL; LoRaMacUplinkStatus.BufferSize = 0; - SerialDisplayUpdateFrameType( false ); + //SerialDisplayUpdateFrameType( false ); } else { @@ -361,7 +276,7 @@ LoRaMacUplinkStatus.Port = AppPort; LoRaMacUplinkStatus.Buffer = AppData; LoRaMacUplinkStatus.BufferSize = AppDataSize; - SerialDisplayUpdateFrameType( IsTxConfirmed ); + //SerialDisplayUpdateFrameType( IsTxConfirmed ); if( IsTxConfirmed == false ) { @@ -390,55 +305,6 @@ } /*! - * \brief Function executed on TxNextPacket Timeout event - */ -static void OnTxNextPacketTimerEvent( void ) -{ - MibRequestConfirm_t mibReq; - LoRaMacStatus_t status; - - TimerStop( &TxNextPacketTimer ); - - mibReq.Type = MIB_NETWORK_JOINED; - status = LoRaMacMibGetRequestConfirm( &mibReq ); - - if( status == LORAMAC_STATUS_OK ) - { - if( mibReq.Param.IsNetworkJoined == true ) - { - DeviceState = DEVICE_STATE_SEND; - NextTx = true; - } - else - { - DeviceState = DEVICE_STATE_JOIN; - } - } -} - -/*! - * \brief Function executed on Led 1 Timeout event - */ -static void OnLed1TimerEvent( void ) -{ - TimerStop( &Led1Timer ); - // Switch LED 1 OFF - Led1State = false; - Led1StateChanged = true; -} - -/*! - * \brief Function executed on Led 2 Timeout event - */ -static void OnLed2TimerEvent( void ) -{ - TimerStop( &Led2Timer ); - // Switch LED 2 OFF - Led2State = false; - Led2StateChanged = true; -} - -/*! * \brief MCPS-Confirm event function * * \param [IN] mcpsConfirm - Pointer to the confirm structure, @@ -482,7 +348,7 @@ UplinkStatusUpdated = true; } - NextTx = true; + } /*! @@ -560,7 +426,6 @@ case 2: if( mcpsIndication->BufferSize == 1 ) { - AppLedStateOn = mcpsIndication->Buffer[0] & 0x01; Led3StateChanged = true; } break; @@ -687,9 +552,7 @@ case MLME_JOIN: { // Status is OK, node has joined the network - IsNetworkJoinedStatusUpdate = true; DeviceState = DEVICE_STATE_SEND; - NextTx = true; break; } case MLME_LINK_CHECK: @@ -708,7 +571,6 @@ break; } } - NextTx = true; UplinkStatusUpdated = true; } @@ -717,63 +579,16 @@ */ int main( void ) { + char send_data[] = "abcdefghijklmn"; LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; BoardInit( ); - SerialDisplayInit( ); - - SerialDisplayUpdateEui( 5, DevEui ); - SerialDisplayUpdateEui( 6, AppEui ); - SerialDisplayUpdateKey( 7, AppKey ); - -#if( OVER_THE_AIR_ACTIVATION == 0 ) - SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); - SerialDisplayUpdateDevAddr( DevAddr ); - SerialDisplayUpdateKey( 12, NwkSKey ); - SerialDisplayUpdateKey( 13, AppSKey ); -#endif - DeviceState = DEVICE_STATE_INIT; while( 1 ) { - SerialRxProcess( ); - if( IsNetworkJoinedStatusUpdate == true ) - { - IsNetworkJoinedStatusUpdate = false; - mibReq.Type = MIB_NETWORK_JOINED; - LoRaMacMibGetRequestConfirm( &mibReq ); - SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); - } - if( Led1StateChanged == true ) - { - Led1StateChanged = false; - SerialDisplayUpdateLedState( 1, Led1State ); - } - if( Led2StateChanged == true ) - { - Led2StateChanged = false; - SerialDisplayUpdateLedState( 2, Led2State ); - } - if( Led3StateChanged == true ) - { - Led3StateChanged = false; - SerialDisplayUpdateLedState( 3, AppLedStateOn ); - } - if( UplinkStatusUpdated == true ) - { - UplinkStatusUpdated = false; - SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); - } - if( DownlinkStatusUpdated == true ) - { - DownlinkStatusUpdated = false; - SerialDisplayUpdateLedState( 2, Led2State ); - SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); - } - switch( DeviceState ) { case DEVICE_STATE_INIT: @@ -783,15 +598,6 @@ LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); - - TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); - - TimerInit( &Led1Timer, OnLed1TimerEvent ); - TimerSetValue( &Led1Timer, 25000 ); - - TimerInit( &Led2Timer, OnLed2TimerEvent ); - TimerSetValue( &Led2Timer, 25000 ); - mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); @@ -802,7 +608,6 @@ #if defined( USE_BAND_868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); - SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); @@ -819,10 +624,6 @@ #endif #endif - SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); - SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); - SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); - LoRaMacDownlinkStatus.DownlinkCounter = 0; DeviceState = DEVICE_STATE_JOIN; @@ -867,39 +668,18 @@ DeviceState = DEVICE_STATE_SEND; #endif - IsNetworkJoinedStatusUpdate = true; break; } case DEVICE_STATE_SEND: { - if( NextTx == true ) - { - SerialDisplayUpdateUplinkAcked( false ); - SerialDisplayUpdateDonwlinkRxData( false ); - PrepareTxFrame( AppPort ); - - NextTx = SendFrame( ); - } - if( ComplianceTest.Running == true ) - { - // Schedule next packet transmission - TxDutyCycleTime = 5000000; // 5000000 us - } - else - { - // Schedule next packet transmission - TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); - } + PrepareTxFrame(send_data); + SendFrame(); DeviceState = DEVICE_STATE_CYCLE; break; } case DEVICE_STATE_CYCLE: { - DeviceState = DEVICE_STATE_SLEEP; - - // Schedule next packet transmission - TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); - TimerStart( &TxNextPacketTimer ); + DeviceState = DEVICE_STATE_INIT; break; } case DEVICE_STATE_SLEEP: