Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYSTIME] Prevents SysTimeToMs computations that may cause miscalculations when calendarTime is too large #86

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ typedef struct sLoRaMacCtx
* Buffer containing the MAC layer commands
*/
uint8_t MacCommandsBuffer[LORA_MAC_COMMAND_MAX_LENGTH];
/*!
* Time on air accumulation
*/
uint32_t SumSendTime;
/*!
* Send count accumulation
*/
uint32_t SumSendCount;
}LoRaMacCtx_t;

/*!
Expand Down Expand Up @@ -1042,6 +1050,10 @@ static void ProcessRadioTxDone( void )
TimerSetValue( &MacCtx.AckTimeoutTimer, MacCtx.RxWindow2Delay + phyParam.Value );
TimerStart( &MacCtx.AckTimeoutTimer );
}
else if( MacCtx.NodeAckRequested == false )
{
MacCtx.McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
}
#elif (defined( LORAMAC_VERSION ) && (( LORAMAC_VERSION == 0x01000400 ) || ( LORAMAC_VERSION == 0x01010100 )))
if( MacCtx.NodeAckRequested == true )
{
Expand Down Expand Up @@ -1072,13 +1084,6 @@ static void ProcessRadioTxDone( void )
}

RegionSetBandTxDone( Nvm.MacGroup2.Region, &txDone );

#if (defined( LORAMAC_VERSION ) && ( LORAMAC_VERSION == 0x01000300 ))
if( MacCtx.NodeAckRequested == false )
{
MacCtx.McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
}
#endif /* LORAMAC_VERSION */
}

static void PrepareRxDoneAbort( void )
Expand Down Expand Up @@ -4012,6 +4017,9 @@ static LoRaMacStatus_t SendFrameOnChannel( uint8_t channel )
MacCtx.McpsConfirm.TxPower = txPower;
MacCtx.McpsConfirm.Channel = channel;

MacCtx.SumSendCount++;
MacCtx.SumSendTime += MacCtx.TxTimeOnAir;

// Store the time on air
MacCtx.McpsConfirm.TxTimeOnAir = MacCtx.TxTimeOnAir;
MacCtx.MlmeConfirm.TxTimeOnAir = MacCtx.TxTimeOnAir;
Expand Down Expand Up @@ -6672,3 +6680,24 @@ LoRaMacStatus_t LoRaMacDeInitialization( void )
return LORAMAC_STATUS_BUSY;
}
}

uint8_t LoRaMacGetMaxPayloadLength(void)
{
return GetMaxAppPayloadWithoutFOptsLength(Nvm.MacGroup2.ChannelsDatarateDefault);
}

void LoRaMacSendInfoGet(uint32_t *count, uint32_t *time)
{
if(count != NULL) {
*count = MacCtx.SumSendCount;
}
if(time != NULL) {
*time = MacCtx.SumSendTime;
}
}

void LoRaMacSendInfoClear(void)
{
MacCtx.SumSendCount = 0;
MacCtx.SumSendTime = 0;
}
20 changes: 20 additions & 0 deletions Middlewares/Third_Party/LoRaWAN/Mac/LoRaMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,26 @@ LoRaMacStatus_t LoRaMacDeInitialization( void );

LoRaMacStatus_t LoRaMacProcessMicForDatablock( uint8_t *buffer, uint32_t size, uint16_t sessionCnt, uint8_t fragIndex, uint32_t descriptor, uint32_t *mic );

/*!
* \brief LoRaMAC gets the maximum application payload length in the absence of the optional FOpt field
*
* \retval Max length
*/
uint8_t LoRaMacGetMaxPayloadLength( void );

/*!
* \brief LoRaMAC get send info
*
* \param [out] count - Send count accumulation
* \param [out] time - Time on air accumulation
*/
void LoRaMacSendInfoGet(uint32_t *count, uint32_t *time);

/*!
* \brief LoRaMAC send info clear
*/
void LoRaMacSendInfoClear(void);

/*! \} defgroup LORAMAC */

#ifdef __cplusplus
Expand Down
6 changes: 5 additions & 1 deletion Utilities/misc/stm32_systime.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ uint32_t SysTimeToMs( SysTime_t sysTime )
DeltaTime.Seconds = UTIL_SYSTIMDriver.BKUPRead_Seconds();

SysTime_t calendarTime = SysTimeSub( sysTime, DeltaTime );
return calendarTime.Seconds * 1000 + calendarTime.SubSeconds;
int64_t calendar_second = calendarTime.Seconds;
int64_t calendar_subsecond = calendarTime.SubSeconds;
int64_t calendar_time = calendar_second * 1000 + calendar_subsecond;
calendar_time = calendar_time % 4194304000;
return calendar_time;
}

SysTime_t SysTimeFromMs( uint32_t timeMs )
Expand Down