1
Dependencies: X_NUCLEO_IKS01A1 LoRaWAN-lib SX1276Lib mbed
app/LoRaDeviceStateProc.cpp@8:88e8a1c7b88a, 2018-11-09 (annotated)
- Committer:
- emerette
- Date:
- Fri Nov 09 21:52:19 2018 +0000
- Revision:
- 8:88e8a1c7b88a
- Parent:
- 7:730764a067dc
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ubhat | 0:42863a11464a | 1 | /* |
ubhat | 0:42863a11464a | 2 | / _____) _ | | |
ubhat | 0:42863a11464a | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
ubhat | 0:42863a11464a | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
ubhat | 0:42863a11464a | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
ubhat | 0:42863a11464a | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
ubhat | 0:42863a11464a | 7 | (C)2015 Semtech |
ubhat | 0:42863a11464a | 8 | |
ubhat | 0:42863a11464a | 9 | Description: Process function calls from various Device states |
ubhat | 0:42863a11464a | 10 | |
ubhat | 0:42863a11464a | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
ubhat | 0:42863a11464a | 12 | |
ubhat | 0:42863a11464a | 13 | Maintainer: Uttam Bhat |
ubhat | 0:42863a11464a | 14 | */ |
ubhat | 0:42863a11464a | 15 | |
ubhat | 0:42863a11464a | 16 | #include "LoRaDeviceStateProc.h" |
ubhat | 0:42863a11464a | 17 | #include "LoRaMacLayerService.h" |
ubhat | 0:42863a11464a | 18 | |
ubhat | 0:42863a11464a | 19 | eDevicState DeviceState; |
ubhat | 0:42863a11464a | 20 | |
ubhat | 0:42863a11464a | 21 | sLoRaMacUplinkStatus LoRaMacUplinkStatus; |
ubhat | 0:42863a11464a | 22 | |
ubhat | 0:42863a11464a | 23 | sLoRaMacDownlinkStatus LoRaMacDownlinkStatus; |
ubhat | 0:42863a11464a | 24 | |
ubhat | 0:42863a11464a | 25 | LoRaMacPrimitives_t LoRaPrimitives; |
ubhat | 0:42863a11464a | 26 | |
ubhat | 0:42863a11464a | 27 | LoRaMacCallback_t LoRaCallbacks; |
ubhat | 0:42863a11464a | 28 | |
ubhat | 0:42863a11464a | 29 | MibRequestConfirm_t LoRaMibReq; |
ubhat | 0:42863a11464a | 30 | |
ubhat | 0:42863a11464a | 31 | MlmeReq_t mlmeReq; |
ubhat | 0:42863a11464a | 32 | |
ubhat | 6:711a4e008afa | 33 | uint16_t ChannelMaskTemp[6] = {0}; |
ubhat | 6:711a4e008afa | 34 | |
ubhat | 7:730764a067dc | 35 | static bool DutyCycleFlag = true; |
ubhat | 0:42863a11464a | 36 | |
ubhat | 0:42863a11464a | 37 | /*! |
ubhat | 0:42863a11464a | 38 | * \brief Function executed on TxNextPacket Timeout event |
ubhat | 0:42863a11464a | 39 | */ |
ubhat | 6:711a4e008afa | 40 | |
ubhat | 0:42863a11464a | 41 | static void OnTxNextPacketTimerEvent( void ) |
ubhat | 0:42863a11464a | 42 | { |
ubhat | 0:42863a11464a | 43 | MibRequestConfirm_t mibReq; |
ubhat | 0:42863a11464a | 44 | LoRaMacStatus_t status; |
ubhat | 0:42863a11464a | 45 | |
ubhat | 0:42863a11464a | 46 | TimerStop( &TxNextPacketTimer ); |
ubhat | 0:42863a11464a | 47 | |
ubhat | 0:42863a11464a | 48 | mibReq.Type = MIB_NETWORK_JOINED; |
ubhat | 0:42863a11464a | 49 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
ubhat | 0:42863a11464a | 50 | |
ubhat | 0:42863a11464a | 51 | if( status == LORAMAC_STATUS_OK ) |
ubhat | 0:42863a11464a | 52 | { |
ubhat | 0:42863a11464a | 53 | if( mibReq.Param.IsNetworkJoined == true ) |
ubhat | 0:42863a11464a | 54 | { |
ubhat | 6:711a4e008afa | 55 | DeviceState = DEVICE_STATE_SEND; |
ubhat | 6:711a4e008afa | 56 | |
ubhat | 6:711a4e008afa | 57 | if( DutyCycleFlag == false) |
ubhat | 6:711a4e008afa | 58 | { |
ubhat | 6:711a4e008afa | 59 | IsDutyCycleUpdate = true; |
ubhat | 6:711a4e008afa | 60 | |
ubhat | 6:711a4e008afa | 61 | DutyCycleFlag = true; |
ubhat | 7:730764a067dc | 62 | |
ubhat | 7:730764a067dc | 63 | mibReq.Type = MIB_ADR; |
ubhat | 7:730764a067dc | 64 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
ubhat | 7:730764a067dc | 65 | LoRaMacMibSetRequestConfirm( &mibReq ); |
ubhat | 6:711a4e008afa | 66 | } |
ubhat | 0:42863a11464a | 67 | } |
ubhat | 0:42863a11464a | 68 | else |
ubhat | 0:42863a11464a | 69 | { |
ubhat | 0:42863a11464a | 70 | DeviceState = DEVICE_STATE_JOIN; |
ubhat | 0:42863a11464a | 71 | } |
ubhat | 0:42863a11464a | 72 | NextTx = true; |
ubhat | 0:42863a11464a | 73 | } |
ubhat | 0:42863a11464a | 74 | } |
ubhat | 0:42863a11464a | 75 | |
ubhat | 6:711a4e008afa | 76 | /*! |
ubhat | 6:711a4e008afa | 77 | * \brief Function executed on Duty Cycle Timeout event |
ubhat | 6:711a4e008afa | 78 | */ |
ubhat | 6:711a4e008afa | 79 | static void OnDutyCycleEvent( void ) |
ubhat | 6:711a4e008afa | 80 | { |
ubhat | 6:711a4e008afa | 81 | TimerStop( &TxDutyCycleTimer ); |
ubhat | 6:711a4e008afa | 82 | |
ubhat | 6:711a4e008afa | 83 | IsDutyCycleUpdate = true; |
ubhat | 6:711a4e008afa | 84 | |
ubhat | 6:711a4e008afa | 85 | DutyCycleCntr++; |
ubhat | 6:711a4e008afa | 86 | |
ubhat | 6:711a4e008afa | 87 | if( DutyCycleCntr >= (2 * APP_TX_DUTYCYCLE_HOUR ) ) |
ubhat | 6:711a4e008afa | 88 | { |
ubhat | 6:711a4e008afa | 89 | DutyCycleIndx++; |
ubhat | 6:711a4e008afa | 90 | |
ubhat | 6:711a4e008afa | 91 | DutyCycleCntr = 0; |
ubhat | 6:711a4e008afa | 92 | } |
ubhat | 6:711a4e008afa | 93 | } |
ubhat | 6:711a4e008afa | 94 | |
ubhat | 7:730764a067dc | 95 | static void OnDelayEvent( void ) |
ubhat | 7:730764a067dc | 96 | { |
ubhat | 7:730764a067dc | 97 | TimerStop( &TxDelayTimer ); |
ubhat | 7:730764a067dc | 98 | |
ubhat | 7:730764a067dc | 99 | DutyCycleCntr++; |
ubhat | 7:730764a067dc | 100 | |
ubhat | 7:730764a067dc | 101 | if( DutyCycleCntr >= (2 * APP_DELAY_HOUR ) ) |
ubhat | 7:730764a067dc | 102 | { |
ubhat | 7:730764a067dc | 103 | IsDeviceReady = true; |
ubhat | 7:730764a067dc | 104 | |
ubhat | 7:730764a067dc | 105 | DutyCycleFlag = false; |
ubhat | 7:730764a067dc | 106 | |
ubhat | 7:730764a067dc | 107 | DutyCycleCntr = 0; |
ubhat | 7:730764a067dc | 108 | } |
ubhat | 7:730764a067dc | 109 | else |
ubhat | 7:730764a067dc | 110 | { |
ubhat | 7:730764a067dc | 111 | InitDelayTimer( ); |
ubhat | 7:730764a067dc | 112 | } |
ubhat | 7:730764a067dc | 113 | } |
ubhat | 7:730764a067dc | 114 | |
ubhat | 0:42863a11464a | 115 | void DeviceInit( void ) |
ubhat | 0:42863a11464a | 116 | { |
ubhat | 0:42863a11464a | 117 | LoRaPrimitives.MacMcpsConfirm = McpsConfirm; |
ubhat | 0:42863a11464a | 118 | LoRaPrimitives.MacMcpsIndication = McpsIndication; |
ubhat | 0:42863a11464a | 119 | LoRaPrimitives.MacMlmeConfirm = MlmeConfirm; |
ubhat | 0:42863a11464a | 120 | LoRaCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
ubhat | 0:42863a11464a | 121 | LoRaMacInitialization( &LoRaPrimitives, &LoRaCallbacks ); |
ubhat | 0:42863a11464a | 122 | |
ubhat | 0:42863a11464a | 123 | TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); |
ubhat | 0:42863a11464a | 124 | |
ubhat | 6:711a4e008afa | 125 | TimerInit( &TxDutyCycleTimer, OnDutyCycleEvent ); |
ubhat | 6:711a4e008afa | 126 | |
ubhat | 7:730764a067dc | 127 | TimerInit( &TxDelayTimer, OnDelayEvent ); |
ubhat | 7:730764a067dc | 128 | |
ubhat | 0:42863a11464a | 129 | LoRaMibReq.Type = MIB_ADR; |
ubhat | 7:730764a067dc | 130 | if( IsDeviceReady == true ) |
ubhat | 7:730764a067dc | 131 | { |
ubhat | 7:730764a067dc | 132 | LoRaMibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
ubhat | 7:730764a067dc | 133 | } |
ubhat | 7:730764a067dc | 134 | else |
ubhat | 7:730764a067dc | 135 | { |
ubhat | 7:730764a067dc | 136 | LoRaMibReq.Param.AdrEnable = 0; |
ubhat | 7:730764a067dc | 137 | } |
ubhat | 0:42863a11464a | 138 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 139 | |
ubhat | 0:42863a11464a | 140 | LoRaMibReq.Type = MIB_PUBLIC_NETWORK; |
ubhat | 0:42863a11464a | 141 | LoRaMibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
ubhat | 0:42863a11464a | 142 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 143 | |
ubhat | 0:42863a11464a | 144 | LoRaMibReq.Type = MIB_CHANNELS_TX_POWER; |
ubhat | 0:42863a11464a | 145 | LoRaMibReq.Param.ChannelsTxPower = LORAWAN_TX_POWER; |
ubhat | 0:42863a11464a | 146 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 147 | |
ubhat | 6:711a4e008afa | 148 | LoRaMibReq.Type = MIB_CHANNELS_MASK; |
ubhat | 6:711a4e008afa | 149 | LoRaMibReq.Param.ChannelsMask = ChannelMaskTemp; |
ubhat | 6:711a4e008afa | 150 | |
ubhat | 6:711a4e008afa | 151 | #ifdef USE_BAND_915_HYBRID_BLOCK_A |
ubhat | 6:711a4e008afa | 152 | |
ubhat | 6:711a4e008afa | 153 | ChannelMaskTemp[0] = 0x00FF; |
ubhat | 6:711a4e008afa | 154 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 155 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 156 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 157 | ChannelMaskTemp[4] = 0x0001; |
ubhat | 6:711a4e008afa | 158 | |
ubhat | 6:711a4e008afa | 159 | #elif defined USE_BAND_915_HYBRID_BLOCK_B |
ubhat | 6:711a4e008afa | 160 | |
ubhat | 6:711a4e008afa | 161 | ChannelMaskTemp[0] = 0xFF00; |
ubhat | 6:711a4e008afa | 162 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 163 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 164 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 165 | ChannelMaskTemp[4] = 0x0002; |
ubhat | 6:711a4e008afa | 166 | |
ubhat | 6:711a4e008afa | 167 | #elif defined USE_BAND_915_HYBRID_BLOCK_C |
ubhat | 6:711a4e008afa | 168 | |
ubhat | 6:711a4e008afa | 169 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 170 | ChannelMaskTemp[1] = 0x00FF; |
ubhat | 6:711a4e008afa | 171 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 172 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 173 | ChannelMaskTemp[4] = 0x0004; |
ubhat | 6:711a4e008afa | 174 | |
ubhat | 6:711a4e008afa | 175 | #elif defined USE_BAND_915_HYBRID_BLOCK_D |
ubhat | 6:711a4e008afa | 176 | |
ubhat | 6:711a4e008afa | 177 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 178 | ChannelMaskTemp[1] = 0xFF00; |
ubhat | 6:711a4e008afa | 179 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 180 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 181 | ChannelMaskTemp[4] = 0x0008; |
ubhat | 6:711a4e008afa | 182 | |
ubhat | 6:711a4e008afa | 183 | #elif defined USE_BAND_915_HYBRID_BLOCK_E |
ubhat | 6:711a4e008afa | 184 | |
ubhat | 6:711a4e008afa | 185 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 186 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 187 | ChannelMaskTemp[2] = 0x00FF; |
ubhat | 6:711a4e008afa | 188 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 189 | ChannelMaskTemp[4] = 0x0010; |
ubhat | 6:711a4e008afa | 190 | |
ubhat | 6:711a4e008afa | 191 | #elif defined USE_BAND_915_HYBRID_BLOCK_F |
ubhat | 6:711a4e008afa | 192 | |
ubhat | 6:711a4e008afa | 193 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 194 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 195 | ChannelMaskTemp[2] = 0xFF00; |
ubhat | 6:711a4e008afa | 196 | ChannelMaskTemp[3] = 0x0000; |
ubhat | 6:711a4e008afa | 197 | ChannelMaskTemp[4] = 0x0020; |
ubhat | 6:711a4e008afa | 198 | |
ubhat | 6:711a4e008afa | 199 | #elif defined USE_BAND_915_HYBRID_BLOCK_G |
ubhat | 6:711a4e008afa | 200 | |
ubhat | 6:711a4e008afa | 201 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 202 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 203 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 204 | ChannelMaskTemp[3] = 0x00FF; |
ubhat | 6:711a4e008afa | 205 | ChannelMaskTemp[4] = 0x0040; |
ubhat | 6:711a4e008afa | 206 | |
ubhat | 6:711a4e008afa | 207 | #elif defined USE_BAND_915_HYBRID_BLOCK_H |
ubhat | 6:711a4e008afa | 208 | |
ubhat | 6:711a4e008afa | 209 | ChannelMaskTemp[0] = 0x0000; |
ubhat | 6:711a4e008afa | 210 | ChannelMaskTemp[1] = 0x0000; |
ubhat | 6:711a4e008afa | 211 | ChannelMaskTemp[2] = 0x0000; |
ubhat | 6:711a4e008afa | 212 | ChannelMaskTemp[3] = 0xFF00; |
ubhat | 6:711a4e008afa | 213 | ChannelMaskTemp[4] = 0x0080; |
ubhat | 6:711a4e008afa | 214 | |
ubhat | 6:711a4e008afa | 215 | #elif defined USE_BAND_915 |
ubhat | 6:711a4e008afa | 216 | |
ubhat | 6:711a4e008afa | 217 | ChannelMaskTemp[0] = 0xFFFF; |
ubhat | 6:711a4e008afa | 218 | ChannelMaskTemp[1] = 0xFFFF; |
ubhat | 6:711a4e008afa | 219 | ChannelMaskTemp[2] = 0xFFFF; |
ubhat | 6:711a4e008afa | 220 | ChannelMaskTemp[3] = 0xFFFF; |
ubhat | 6:711a4e008afa | 221 | ChannelMaskTemp[4] = 0x00FF; |
ubhat | 6:711a4e008afa | 222 | ChannelMaskTemp[5] = 0x0000; |
ubhat | 6:711a4e008afa | 223 | |
ubhat | 6:711a4e008afa | 224 | #endif |
ubhat | 6:711a4e008afa | 225 | |
ubhat | 6:711a4e008afa | 226 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 6:711a4e008afa | 227 | |
ubhat | 0:42863a11464a | 228 | LoRaMacDownlinkStatus.DownlinkCounter = 0; |
ubhat | 0:42863a11464a | 229 | } |
ubhat | 0:42863a11464a | 230 | |
ubhat | 0:42863a11464a | 231 | void DeviceJoinUpdate( void ) |
ubhat | 0:42863a11464a | 232 | { |
ubhat | 0:42863a11464a | 233 | LoRaMibReq.Type = MIB_NETWORK_JOINED; |
ubhat | 0:42863a11464a | 234 | LoRaMacMibGetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 235 | } |
ubhat | 0:42863a11464a | 236 | |
ubhat | 0:42863a11464a | 237 | void DeviceJoin( void ) |
ubhat | 0:42863a11464a | 238 | { |
ubhat | 0:42863a11464a | 239 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
ubhat | 0:42863a11464a | 240 | |
ubhat | 0:42863a11464a | 241 | mlmeReq.Type = MLME_JOIN; |
ubhat | 0:42863a11464a | 242 | |
ubhat | 0:42863a11464a | 243 | mlmeReq.Req.Join.DevEui = DevEui; |
ubhat | 0:42863a11464a | 244 | mlmeReq.Req.Join.AppEui = AppEui; |
ubhat | 0:42863a11464a | 245 | mlmeReq.Req.Join.AppKey = AppKey; |
ubhat | 0:42863a11464a | 246 | |
ubhat | 0:42863a11464a | 247 | if( NextTx == true ) |
ubhat | 0:42863a11464a | 248 | { |
ubhat | 0:42863a11464a | 249 | LoRaMacMlmeRequest( &mlmeReq ); |
ubhat | 0:42863a11464a | 250 | } |
ubhat | 0:42863a11464a | 251 | |
ubhat | 0:42863a11464a | 252 | #else |
ubhat | 0:42863a11464a | 253 | // Choose a random device address if not already defined in Config.h |
ubhat | 0:42863a11464a | 254 | if( DevAddr == 0 ) |
ubhat | 0:42863a11464a | 255 | { |
ubhat | 0:42863a11464a | 256 | // Random seed initialization |
ubhat | 0:42863a11464a | 257 | srand1( BoardGetRandomSeed( ) ); |
ubhat | 0:42863a11464a | 258 | DevAddr = randr( 0, 0x01FFFFFF ); |
ubhat | 0:42863a11464a | 259 | } |
ubhat | 0:42863a11464a | 260 | |
ubhat | 0:42863a11464a | 261 | LoRaMibReq.Type = MIB_NET_ID; |
ubhat | 0:42863a11464a | 262 | LoRaMibReq.Param.NetID = LORAWAN_NETWORK_ID; |
ubhat | 0:42863a11464a | 263 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 264 | |
ubhat | 0:42863a11464a | 265 | LoRaMibReq.Type = MIB_DEV_ADDR; |
ubhat | 0:42863a11464a | 266 | LoRaMibReq.Param.DevAddr = DevAddr; |
ubhat | 0:42863a11464a | 267 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 268 | |
ubhat | 0:42863a11464a | 269 | LoRaMibReq.Type = MIB_NWK_SKEY; |
ubhat | 0:42863a11464a | 270 | LoRaMibReq.Param.NwkSKey = NwkSKey; |
ubhat | 0:42863a11464a | 271 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 272 | |
ubhat | 0:42863a11464a | 273 | LoRaMibReq.Type = MIB_APP_SKEY; |
ubhat | 0:42863a11464a | 274 | LoRaMibReq.Param.AppSKey = AppSKey; |
ubhat | 0:42863a11464a | 275 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 276 | |
ubhat | 0:42863a11464a | 277 | LoRaMibReq.Type = MIB_NETWORK_JOINED; |
ubhat | 0:42863a11464a | 278 | LoRaMibReq.Param.IsNetworkJoined = true; |
ubhat | 0:42863a11464a | 279 | LoRaMacMibSetRequestConfirm( &LoRaMibReq ); |
ubhat | 0:42863a11464a | 280 | |
ubhat | 0:42863a11464a | 281 | #endif |
ubhat | 0:42863a11464a | 282 | } |
ubhat | 0:42863a11464a | 283 | |
ubhat | 0:42863a11464a | 284 | /*! |
ubhat | 0:42863a11464a | 285 | * \brief Prepares the payload of the frame |
ubhat | 0:42863a11464a | 286 | */ |
ubhat | 0:42863a11464a | 287 | void PrepareTxFrame( uint8_t port ) |
ubhat | 0:42863a11464a | 288 | { |
ubhat | 0:42863a11464a | 289 | |
ubhat | 0:42863a11464a | 290 | if( port == 224 ) |
ubhat | 0:42863a11464a | 291 | { |
ubhat | 0:42863a11464a | 292 | RunComplianceTest( ); |
ubhat | 0:42863a11464a | 293 | } |
ubhat | 0:42863a11464a | 294 | else |
ubhat | 0:42863a11464a | 295 | { |
ubhat | 0:42863a11464a | 296 | PrepareLoRaFrame( port ); |
ubhat | 0:42863a11464a | 297 | } |
ubhat | 0:42863a11464a | 298 | } |
ubhat | 0:42863a11464a | 299 | |
ubhat | 0:42863a11464a | 300 | /*! |
ubhat | 0:42863a11464a | 301 | * \brief Prepares the payload of the frame |
ubhat | 0:42863a11464a | 302 | * |
ubhat | 0:42863a11464a | 303 | * \retval [0: frame could be send, 1: error] |
ubhat | 0:42863a11464a | 304 | */ |
ubhat | 0:42863a11464a | 305 | bool SendFrame( void ) |
ubhat | 0:42863a11464a | 306 | { |
ubhat | 0:42863a11464a | 307 | McpsReq_t mcpsReq; |
ubhat | 0:42863a11464a | 308 | LoRaMacTxInfo_t txInfo; |
ubhat | 0:42863a11464a | 309 | |
ubhat | 0:42863a11464a | 310 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
ubhat | 0:42863a11464a | 311 | { |
ubhat | 0:42863a11464a | 312 | // Send empty frame in order to flush MAC commands |
ubhat | 0:42863a11464a | 313 | mcpsReq.Type = MCPS_UNCONFIRMED; |
ubhat | 0:42863a11464a | 314 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
ubhat | 0:42863a11464a | 315 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
ubhat | 0:42863a11464a | 316 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
ubhat | 0:42863a11464a | 317 | |
ubhat | 0:42863a11464a | 318 | LoRaMacUplinkStatus.Acked = false; |
ubhat | 0:42863a11464a | 319 | LoRaMacUplinkStatus.Port = 0; |
ubhat | 0:42863a11464a | 320 | LoRaMacUplinkStatus.Buffer = NULL; |
ubhat | 0:42863a11464a | 321 | LoRaMacUplinkStatus.BufferSize = 0; |
ubhat | 0:42863a11464a | 322 | } |
ubhat | 0:42863a11464a | 323 | else |
ubhat | 0:42863a11464a | 324 | { |
ubhat | 0:42863a11464a | 325 | LoRaMacUplinkStatus.Acked = false; |
ubhat | 0:42863a11464a | 326 | LoRaMacUplinkStatus.Port = AppPort; |
ubhat | 0:42863a11464a | 327 | LoRaMacUplinkStatus.Buffer = AppData; |
ubhat | 0:42863a11464a | 328 | LoRaMacUplinkStatus.BufferSize = AppDataSize; |
ubhat | 0:42863a11464a | 329 | |
ubhat | 0:42863a11464a | 330 | if( ( IsTxConfirmed == false ) || ( LoRaMacUplinkStatus.UplinkCounter == 0 ) ) |
ubhat | 0:42863a11464a | 331 | { |
ubhat | 0:42863a11464a | 332 | mcpsReq.Type = MCPS_UNCONFIRMED; |
ubhat | 0:42863a11464a | 333 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
ubhat | 0:42863a11464a | 334 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
ubhat | 0:42863a11464a | 335 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
ubhat | 0:42863a11464a | 336 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
ubhat | 0:42863a11464a | 337 | } |
ubhat | 0:42863a11464a | 338 | else |
ubhat | 0:42863a11464a | 339 | { |
ubhat | 0:42863a11464a | 340 | mcpsReq.Type = MCPS_CONFIRMED; |
ubhat | 0:42863a11464a | 341 | mcpsReq.Req.Confirmed.fPort = AppPort; |
ubhat | 0:42863a11464a | 342 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
ubhat | 0:42863a11464a | 343 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
ubhat | 0:42863a11464a | 344 | mcpsReq.Req.Confirmed.NbTrials = 8; |
ubhat | 0:42863a11464a | 345 | mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
ubhat | 0:42863a11464a | 346 | } |
ubhat | 0:42863a11464a | 347 | } |
ubhat | 0:42863a11464a | 348 | |
ubhat | 0:42863a11464a | 349 | LoRaMacUplinkStatus.Type = mcpsReq.Type; |
ubhat | 0:42863a11464a | 350 | |
ubhat | 0:42863a11464a | 351 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
ubhat | 0:42863a11464a | 352 | { |
ubhat | 0:42863a11464a | 353 | return false; |
ubhat | 0:42863a11464a | 354 | } |
ubhat | 0:42863a11464a | 355 | return true; |
ubhat | 6:711a4e008afa | 356 | } |