uint32_t LoRaMacGetDownLinkCounter( void ) { MibRequestConfirm_t mibGet; mibGet.Type = MIB_DOWNLINK_COUNTER; LoRaMacMibGetRequestConfirm( &mibGet ); return mibGet.Param.DownLinkCounter; }
static bool _is_mac_joined(semtech_loramac_t *mac) { mutex_lock(&mac->lock); MibRequestConfirm_t mibReq; mibReq.Type = MIB_NETWORK_JOINED; LoRaMacMibGetRequestConfirm(&mibReq); bool is_joined = mibReq.Param.IsNetworkJoined; mutex_unlock(&mac->lock); return is_joined; }
uint8_t LoRaMacSendConfirmedFrame( uint8_t fPort, void *fBuffer, uint16_t fBufferSize, uint8_t nbRetries ) { MibRequestConfirm_t mibGet; McpsReq_t mcpsRequest; uint8_t retStatus; memset1( ( uint8_t* )&LoRaMacEventInfo, 0, sizeof( LoRaMacEventInfo ) ); mibGet.Type = MIB_CHANNELS_DATARATE; LoRaMacMibGetRequestConfirm( &mibGet ); mcpsRequest.Type = MCPS_CONFIRMED; mcpsRequest.Req.Confirmed.fBuffer = fBuffer; mcpsRequest.Req.Confirmed.fBufferSize = fBufferSize; mcpsRequest.Req.Confirmed.fPort = fPort; mcpsRequest.Req.Confirmed.nbRetries = nbRetries; mcpsRequest.Req.Confirmed.Datarate = mibGet.Param.ChannelsDatarate; switch( LoRaMacMcpsRequest( &mcpsRequest ) ) { case LORAMAC_STATUS_OK: retStatus = 0U; break; case LORAMAC_STATUS_BUSY: retStatus = 1U; break; case LORAMAC_STATUS_NO_NETWORK_JOINED: retStatus = 2U; break; case LORAMAC_STATUS_LENGTH_ERROR: case LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR: retStatus = 3U; break; case LORAMAC_STATUS_SERVICE_UNKNOWN: retStatus = 4U; break; case LORAMAC_STATUS_DEVICE_OFF: retStatus = 6U; break; default: retStatus = 1U; break; } return retStatus; }
static inline void _save_uplink_counter(semtech_loramac_t *mac) { size_t pos = SEMTECH_LORAMAC_EEPROM_START + SEMTECH_LORAMAC_EEPROM_MAGIC_LEN + LORAMAC_DEVEUI_LEN + LORAMAC_APPEUI_LEN + LORAMAC_APPKEY_LEN + LORAMAC_APPSKEY_LEN + LORAMAC_NWKSKEY_LEN + LORAMAC_DEVADDR_LEN; uint8_t counter[4]; mutex_lock(&mac->lock); MibRequestConfirm_t mibReq; mibReq.Type = MIB_UPLINK_COUNTER; LoRaMacMibGetRequestConfirm(&mibReq); counter[0] = (uint8_t)(mibReq.Param.UpLinkCounter >> 24); counter[1] = (uint8_t)(mibReq.Param.UpLinkCounter >> 16); counter[2] = (uint8_t)(mibReq.Param.UpLinkCounter >> 8); counter[3] = (uint8_t)(mibReq.Param.UpLinkCounter); mutex_unlock(&mac->lock); pos += eeprom_write(pos, counter, 4); }
/*! * \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 { // Network not joined yet. Try to join again MlmeReq_t mlmeReq; mlmeReq.Type = MLME_JOIN; mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; mlmeReq.Req.Join.Datarate = LORAWAN_DEFAULT_DATARATE; if( LoRaMacMlmeRequest( &mlmeReq ) == LORAMAC_STATUS_OK ) { DeviceState = DEVICE_STATE_SLEEP; } else { DeviceState = DEVICE_STATE_CYCLE; } } } }
/*! * \brief Function executed on TxNextPacket Timeout event * If network not joined then changes state to DEVICE_STATE_JOIN * else changes state to DEVICE_STATE_SEND */ static void OnTxNextPacketTimerEvent( void ) { pc.printf("OnTxNextPacketTimerEvent\r\n"); MibRequestConfirm_t mibReq; LoRaMacStatus_t status; TimerStop( &TxNextPacketTimer ); mibReq.Type = MIB_NETWORK_JOINED; status = LoRaMacMibGetRequestConfirm( &mibReq ); // Check the MAC status if( status == LORAMAC_STATUS_OK ) { if( mibReq.Param.IsNetworkJoined == true ) { DeviceState = DEVICE_STATE_SEND; pc.printf("Set device state to send\r\n"); // NextTx = true; } else { DeviceState = DEVICE_STATE_JOIN; pc.printf("Set device state to join\r\n"); } NextTx = true; // Must be set to true to either send message or join } }
bool RegionCN470ANextChannel( NextChanParams_t *nextChanParams, uint8_t *channel, TimerTime_t *time, TimerTime_t *aggregatedTimeOff ) { uint8_t nbEnabledChannels = 0; uint8_t delayTx = 0; uint8_t enabledChannels[CN470A_MAX_NB_CHANNELS] = { 0 }; TimerTime_t nextTxDelay = 0; MibRequestConfirm_t mib_req; static uint8_t RxFreqBandNum = 0; if (RegionCommonCountChannels( ChannelsMask, 0, 1) == 0 ) { // Reactivate default channels ChannelsMask[0] |= LC( 1 ) + LC( 2 ) + LC( 3 ); } if (nextChanParams->AggrTimeOff <= TimerGetElapsedTime( nextChanParams->LastAggrTx)) { // Reset Aggregated time off *aggregatedTimeOff = 0; // Update bands Time OFF nextTxDelay = RegionCommonUpdateBandTimeOff(nextChanParams->Joined, nextChanParams->DutyCycleEnabled, Bands, CN470A_MAX_NB_BANDS); // Search how many channels are enabled nbEnabledChannels = CountNbOfEnabledChannels( nextChanParams->Joined, nextChanParams->Datarate, ChannelsMask, Channels, Bands, enabledChannels, &delayTx ); } else { delayTx++; nextTxDelay = nextChanParams->AggrTimeOff - TimerGetElapsedTime( nextChanParams->LastAggrTx ); } if ( nbEnabledChannels > 0 ) { // We found a valid channel *channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )]; *time = 0; } else { if ( delayTx > 0 ) { // Delay transmission due to AggregatedTimeOff or to a band time off *time = nextTxDelay; return true; } // Datarate not supported by any channel, restore defaults ChannelsMask[0] |= LC( 1 ) + LC( 2 ) + LC( 3 ); *time = 0; return false; } mib_req.Type = MIB_NETWORK_JOINED; LoRaMacMibGetRequestConfirm(&mib_req); if (mib_req.Param.IsNetworkJoined == false) { if (nextChanParams->joinmethod == STORED_JOIN_METHOD) { if (nextChanParams->freqband > 15) { nextChanParams->freqband = 1; // reset to defautl value } TxFreqBandNum = nextChanParams->freqband; } else { if (nextChanParams->joinmethod == SCAN_JOIN_METHOD && nextChanParams->update_freqband) { NextAvailableFreqBandIdx = find_next_available_freqband(); } else if (nextChanParams->joinmethod == DEF_JOIN_METHOD) { NextAvailableFreqBandIdx = 0; } nextChanParams->update_freqband = false; TxFreqBandNum = FreqBandNum[NextAvailableFreqBandIdx]; } } if (get_lora_freq_type() == FREQ_TYPE_INTER) { if (FreqBandNum[NextAvailableFreqBandIdx] > 7) { RxFreqBandNum = TxFreqBandNum - 8; } else { RxFreqBandNum = TxFreqBandNum + 8; } } else { //IntraFreq RxFreqBandNum = TxFreqBandNum; } nextChanParams->freqband = TxFreqBandNum; nextChanParams->NextAvailableRxFreqBandNum = RxFreqBandNum; nextChanParams->NextAvailableTxFreqBandNum = TxFreqBandNum; //update the freq due to the change of FreqBand Num for ( uint8_t i = 0; i < CN470A_MAX_NB_CHANNELS; i++ ) { Channels[i].Frequency = 470300000 + (FreqBandStartChannelNum[nextChanParams->NextAvailableRxFreqBandNum] + i) * 200000; TxChannels[i].Frequency = 470300000 + (FreqBandStartChannelNum[nextChanParams->NextAvailableTxFreqBandNum] + i) * 200000; } return true; }