Nagaraj Krishnamurthy / LoRaWAN-NAMote72-Application-Demo_IoTium

Dependencies:   LoRaWAN-lib SX1272Lib lib_gps lib_mma8451q lib_mpl3115a2 mbed

Fork of LoRaWAN-NAMote72-Application-Demo_Multitech by Nagaraj Krishnamurthy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaMacLayerService.cpp Source File

LoRaMacLayerService.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: MAC Layer Services: MLME & MCPS
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainer: Uttam Bhat
00014 */
00015 
00016 #include "LoRaMacLayerService.h"
00017 
00018 /*!
00019  * \brief   MCPS-Confirm event function
00020  *
00021  * \param   [IN] McpsConfirm - Pointer to the confirm structure,
00022  *               containing confirm attributes.
00023  */
00024 void McpsConfirm( McpsConfirm_t *McpsConfirm )
00025 {
00026     if( McpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
00027     {
00028         switch( McpsConfirm->McpsRequest )
00029         {
00030             case MCPS_UNCONFIRMED:
00031             {
00032                 // Check Datarate
00033                 // Check TxPower
00034                 break;
00035             }
00036             case MCPS_CONFIRMED:
00037             {
00038                 // Check Datarate
00039                 // Check TxPower
00040                 // Check AckReceived
00041                 // Check NbRetries
00042                 LoRaMacUplinkStatus.Acked = McpsConfirm->AckReceived;
00043                 break;
00044             }
00045             case MCPS_PROPRIETARY:
00046             {
00047                 break;
00048             }
00049             default:
00050                 break;
00051         }        
00052     }
00053 
00054     LoRaMacUplinkStatus.Datarate = McpsConfirm->Datarate;
00055     LoRaMacUplinkStatus.UplinkCounter = McpsConfirm->UpLinkCounter;
00056     LoRaMacUplinkStatus.TxPower = McpsConfirm->TxPower;
00057 
00058     IsTxIntUpdate = true;
00059 }
00060 
00061 /*!
00062  * \brief   MCPS-Indication event function
00063  *
00064  * \param   [IN] McpsIndication - Pointer to the indication structure,
00065  *               containing indication attributes.
00066  */
00067 void McpsIndication( McpsIndication_t *McpsIndication )
00068 {
00069     uint8_t port;
00070     
00071     Gps.service( );
00072 
00073     if( McpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
00074     {
00075         return;
00076     }
00077 
00078     switch( McpsIndication->McpsIndication )
00079     {
00080         case MCPS_UNCONFIRMED:
00081         {
00082             break;
00083         }
00084         case MCPS_CONFIRMED:
00085         {
00086             break;
00087         }
00088         case MCPS_PROPRIETARY:
00089         {
00090             break;
00091         }
00092         case MCPS_MULTICAST:
00093         {
00094             break;
00095         }
00096         default:
00097             break;
00098     }
00099 
00100     // Check Multicast
00101     // Check Port
00102     // Check Datarate
00103     // Check FramePending
00104     // Check Buffer
00105     // Check BufferSize
00106     // Check Rssi
00107     // Check Snr
00108     // Check RxSlot
00109     LoRaMacDownlinkStatus.Rssi = McpsIndication->Rssi;
00110     if( McpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
00111     {
00112         // Invert and divide by 4
00113         LoRaMacDownlinkStatus.Snr = ( ( ~McpsIndication->Snr + 1 ) & 0xFF ) >> 2;
00114         LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
00115     }
00116     else
00117     {
00118         // Divide by 4
00119         LoRaMacDownlinkStatus.Snr = ( McpsIndication->Snr & 0xFF ) >> 2;
00120     }
00121     LoRaMacDownlinkStatus.DownlinkCounter++;
00122     LoRaMacDownlinkStatus.RxData = McpsIndication->RxData;      
00123     LoRaMacDownlinkStatus.Port = McpsIndication->Port;
00124     LoRaMacDownlinkStatus.Buffer = McpsIndication->Buffer;
00125     LoRaMacDownlinkStatus.BufferSize = McpsIndication->BufferSize;
00126     LoRaMacDownlinkStatus.RxSlot = McpsIndication->RxSlot;
00127 
00128     if( ComplianceTest.Running == 1 )
00129     {
00130         port = 224;
00131         ComplianceTest.DownLinkCounter++;
00132     }
00133     else
00134     {
00135         port = McpsIndication->Port;
00136     }
00137     
00138     if( McpsIndication->RxData == true )
00139     {
00140         switch( port )
00141         {
00142         case 1: // The application LED can be controlled on port 1 or 2
00143         case 2:            
00144             break;
00145         case 224:
00146             PrepareComplianceTestFrame( McpsIndication );            
00147             break;
00148         default:
00149             break;
00150         }
00151     }
00152 
00153     IsRxUpdate = true;
00154 
00155 }
00156 
00157 /*!
00158  * \brief   MLME-Confirm event function
00159  *
00160  * \param   [IN] MlmeConfirm - Pointer to the confirm structure,
00161  *               containing confirm attributes.
00162  */
00163 void MlmeConfirm( MlmeConfirm_t *MlmeConfirm )
00164 {
00165     if( MlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
00166     {
00167         switch( MlmeConfirm->MlmeRequest )
00168         {
00169             case MLME_JOIN:
00170             {
00171                 // Status is OK, node has joined the network
00172                 IsNetworkJoinedStatusUpdate = true;
00173                 break;
00174             }
00175             case MLME_LINK_CHECK:
00176             {
00177                 // Check DemodMargin
00178                 // Check NbGateways
00179                 if( ComplianceTest.Running == true )
00180                 {
00181                     ComplianceTest.LinkCheck = true;
00182                     ComplianceTest.DemodMargin = MlmeConfirm->DemodMargin;
00183                     ComplianceTest.NbGateways = MlmeConfirm->NbGateways;
00184                 }
00185                 break;
00186             }
00187             default:
00188                 break;
00189         }
00190     }
00191 
00192     // Schedule next packet transmission
00193     TimerSetValue( &TxNextPacketTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE );
00194     TimerStart( &TxNextPacketTimer );
00195 
00196     DeviceState = DEVICE_STATE_SLEEP;
00197 }