Skip to content

Commit 31dcafa

Browse files
authored
Refine Sntp_ConvertToUnixTime (#105)
Refine Sntp_ConvertToUnixTime We do not need uint64_t for Unix timestamp as we can fit the time till 7 Feb 2106 06:28:15 in uint32_t and beyond that we cannot differentiate between NTP era 0 and era 1 timestamps. Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent 941ad2f commit 31dcafa

File tree

10 files changed

+29
-66
lines changed

10 files changed

+29
-66
lines changed

.github/workflows/ci.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ jobs:
9797
- name: Run Coverage
9898
run: |
9999
make -C build/ coverage
100-
declare -a EXCLUDE=("\*test\*" "\*CMakeCCompilerId.c")
101-
echo ${EXCLUDE[@]} | xargs lcov --rc branch_coverage=1 --ignore-errors empty --ignore-errors source -r build/coverage.info -o build/coverage.info
102-
lcov --rc branch_coverage=1 --ignore-errors empty --ignore-errors source --list build/coverage.info
100+
lcov --rc branch_coverage=1 -r build/coverage.info -o build/coverage.info
101+
lcov --rc branch_coverage=1 --summary build/coverage.info
103102
104103
- name: Check Coverage
105104
uses: FreeRTOS/CI-CD-Github-Actions/coverage-cop@main
@@ -126,14 +125,14 @@ jobs:
126125
path: ./
127126

128127
formatting:
129-
runs-on: ubuntu-20.04
128+
runs-on: ubuntu-latest
130129
steps:
131130
- uses: actions/checkout@v3
132131
- name: Check formatting
133132
uses: FreeRTOS/CI-CD-Github-Actions/formatting@main
134133
with:
135134
path: ./
136-
exclude-dirs: .git
135+
exclude-dirs: .git, test/unit-test/CMock
137136

138137
git-secrets:
139138
runs-on: ubuntu-latest
@@ -207,7 +206,7 @@ jobs:
207206

208207
proof_ci:
209208
if: ${{ github.event.pull_request }} || ${{ github.event.workflow }}
210-
runs-on: ubuntu-20.04
209+
runs-on: ubuntu-latest
211210
steps:
212211
- name: Set up CBMC runner
213212
uses: FreeRTOS/CI-CD-Github-Actions/set_up_cbmc_runner@main

.github/workflows/formatting.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
if: ${{ github.event.issue.pull_request &&
1717
( ( github.event.comment.body == '/bot run uncrustify' ) ||
1818
( github.event.comment.body == '/bot run formatting' ) ) }}
19-
runs-on: ubuntu-20.04
19+
runs-on: ubuntu-latest
2020
steps:
2121
- name: Apply Formatting Fix
2222
uses: FreeRTOS/CI-CD-Github-Actions/formatting-bot@main

docs/doxygen/code_examples/example_sntp_client_posix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void sntpClient_SetTime( const SntpServerInfo_t * pTimeServer,
139139
SntpLeapSecondInfo_t leapSecondInfo )
140140
{
141141
/* @[code_example_sntp_converttounixtime] */
142-
UnixTime_t unixSecs;
142+
uint32_t unixSecs;
143143
uint32_t unixMs;
144144
SntpStatus_t status = Sntp_ConvertToUnixTime( pServerTime, &unixSecs, &unixMs );
145145

24.1 KB
Loading

docs/doxygen/include/size_table.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<tr>
1616
<td>core_sntp_serializer.c</td>
1717
<td><center>1.0K</center></td>
18-
<td><center>0.8K</center></td>
18+
<td><center>0.7K</center></td>
1919
</tr>
2020
<tr>
2121
<td><b>Total estimates</b></td>
2222
<td><b><center>2.7K</center></b></td>
23-
<td><b><center>2.1K</center></b></td>
23+
<td><b><center>2.0K</center></b></td>
2424
</tr>
2525
</table>

source/core_sntp_serializer.c

+11-15
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ SntpStatus_t Sntp_CalculatePollInterval( uint16_t clockFreqTolerance,
812812
}
813813

814814
SntpStatus_t Sntp_ConvertToUnixTime( const SntpTimestamp_t * pSntpTime,
815-
UnixTime_t * pUnixTimeSecs,
815+
uint32_t * pUnixTimeSecs,
816816
uint32_t * pUnixTimeMicrosecs )
817817
{
818818
SntpStatus_t status = SntpSuccess;
@@ -821,24 +821,20 @@ SntpStatus_t Sntp_ConvertToUnixTime( const SntpTimestamp_t * pSntpTime,
821821
{
822822
status = SntpErrorBadParameter;
823823
}
824-
else
824+
825+
if( status == SntpSuccess )
825826
{
826-
/* Handle case when timestamp represents date in SNTP era 1
827-
* (i.e. time from 7 Feb 2036 6:28:16 UTC onwards). */
828-
if( pSntpTime->seconds <= SNTP_TIME_AT_LARGEST_UNIX_TIME_SECS )
827+
/* Handle case when SNTP timestamp is in SNTP era 0 time range
828+
* (i.e. time before 7 Feb 2036 6:28:15 UTC). */
829+
if( pSntpTime->seconds >= SNTP_TIME_AT_UNIX_EPOCH_SECS )
829830
{
830-
/* Unix Time ( seconds ) = Seconds Duration in
831-
* [UNIX epoch, SNTP Era 1 Epoch Time]
832-
* +
833-
* Sntp Time since Era 1 Epoch
834-
*/
835-
*pUnixTimeSecs = ( UnixTime_t ) ( UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + ( UnixTime_t ) ( pSntpTime->seconds ) );
831+
*pUnixTimeSecs = pSntpTime->seconds - SNTP_TIME_AT_UNIX_EPOCH_SECS;
836832
}
837-
838-
/* Handle case when SNTP timestamp is in SNTP era 1 time range. */
839-
if( pSntpTime->seconds >= SNTP_TIME_AT_UNIX_EPOCH_SECS )
833+
else
840834
{
841-
*pUnixTimeSecs = ( UnixTime_t ) ( ( UnixTime_t ) ( pSntpTime->seconds ) - SNTP_TIME_AT_UNIX_EPOCH_SECS );
835+
/* Handle case when timestamp represents date in SNTP era 1
836+
* (i.e. time from 7 Feb 2036 6:28:16 UTC onwards). */
837+
*pUnixTimeSecs = UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + pSntpTime->seconds;
842838
}
843839

844840
/* Convert SNTP fractions to microseconds for UNIX time. */

source/include/core_sntp_serializer.h

+2-24
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@
4343
#endif
4444
/* *INDENT-ON* */
4545

46-
/**
47-
* @brief Type representing seconds since Unix epoch (January 1, 1970 UTC).
48-
*
49-
* The width of this type depends on the configuration macro USE_LEGACY_TIME_API:
50-
* - If USE_LEGACY_TIME_API is defined, a 32-bit unsigned integer is used.
51-
* This limits date representation to the year 2038 (Y2038 limitation).
52-
* - Otherwise, a 64-bit unsigned integer is used for Y2038 compliance.
53-
*/
54-
#ifdef USE_LEGACY_TIME_API
55-
typedef uint32_t UnixTime_t; /**< 32-bit Unix time for legacy systems. */
56-
#else
57-
typedef uint64_t UnixTime_t; /**< 64-bit Unix time for Y2038 compliance. */
58-
#endif
59-
6046
/**
6147
* @ingroup sntp_constants
6248
* @brief The base packet size of request and response of the (S)NTP protocol.
@@ -504,15 +490,7 @@ SntpStatus_t Sntp_CalculatePollInterval( uint16_t clockFreqTolerance,
504490
* @brief Utility to convert SNTP timestamp (that uses 1st Jan 1900 as the epoch) to
505491
* UNIX timestamp (that uses 1st Jan 1970 as the epoch).
506492
*
507-
* @note This function converts SNTP timestamps to UNIX time supporting both 32-bit and
508-
* 64-bit representations based on the configuration macro USE_LEGACY_TIME_API.
509-
*
510-
* - If USE_LEGACY_TIME_API is defined, the conversion is limited to the date range
511-
* from 1st Jan 1970 0h 0m 0s (UNIX epoch) to 19th Jan 2038 3h 14m 7s, due to the
512-
* 32-bit width limitation.
513-
*
514-
* - If USE_LEGACY_TIME_API is not defined, 64-bit UNIX time representation is used,
515-
* allowing conversion of SNTP timestamps beyond the year 2038 (Y2038 problem mitigated).
493+
* Refer to (this image)[docs/doxygen/images/Ntp_To_Unix_Time.png].
516494
*
517495
* @note The function also correctly handles SNTP era overflow (from 7 Feb 2036 6h 28m 16s,
518496
* i.e., SNTP era 1) to ensure accurate conversion across SNTP eras.
@@ -529,7 +507,7 @@ SntpStatus_t Sntp_CalculatePollInterval( uint16_t clockFreqTolerance,
529507
*/
530508
/* @[define_sntp_converttounixtime] */
531509
SntpStatus_t Sntp_ConvertToUnixTime( const SntpTimestamp_t * pSntpTime,
532-
UnixTime_t * pUnixTimeSecs,
510+
uint32_t * pUnixTimeSecs,
533511
uint32_t * pUnixTimeMicrosecs );
534512
/* @[define_sntp_converttounixtime] */
535513

test/cbmc/proofs/Sntp_ConvertToUnixTime/Sntp_ConvertToUnixTime_harness.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
void harness()
3333
{
3434
SntpTimestamp_t * pSntpTime;
35-
UnixTime_t * pUnixTimeSecs;
35+
uint32_t * pUnixTimeSecs;
3636
uint32_t * pUnixTimeMicrosecs;
3737
SntpStatus_t sntpStatus;
3838

3939
pSntpTime = malloc( sizeof( SntpTimestamp_t ) );
40-
pUnixTimeSecs = malloc( sizeof( UnixTime_t ) );
40+
pUnixTimeSecs = malloc( sizeof( uint32_t ) );
4141
pUnixTimeMicrosecs = malloc( sizeof( uint32_t ) );
4242

4343
sntpStatus = Sntp_ConvertToUnixTime( pSntpTime, pUnixTimeSecs, pUnixTimeMicrosecs );

test/unit-test/core_sntp_serializer_utest.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ void test_ConvertToUnixTime_InvalidParams( void )
877877

878878
/* Use same memory for UNIX seconds and microseconds as we are not
879879
* testing those values. */
880-
UnixTime_t unixTime;
880+
uint32_t unixTime;
881881
uint32_t unixTimeMs;
882882

883883
/* Test with NULL SNTP time. */
@@ -900,7 +900,7 @@ void test_ConvertToUnixTime_InvalidParams( void )
900900
void test_ConvertToUnixTime_Nominal( void )
901901
{
902902
SntpTimestamp_t sntpTime = TEST_TIMESTAMP;
903-
UnixTime_t unixTimeSecs;
903+
uint32_t unixTimeSecs;
904904
uint32_t unixTimeMs;
905905

906906
#define TEST_SNTP_TO_UNIX_CONVERSION( sntpTimeSecs, sntpTimeFracs, \

tools/cmock/coverage.cmake

+3-13
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR}
1515
--initial
1616
--capture
1717
--rc branch_coverage=1
18-
--ignore-errors empty
19-
--ignore-errors source
2018
--rc genhtml_branch_coverage=1
2119
--output-file=${CMAKE_BINARY_DIR}/base_coverage.info
22-
--quiet
20+
--include "*source*"
2321
)
2422
file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*")
2523

@@ -49,13 +47,11 @@ execute_process(COMMAND ruby
4947
execute_process(
5048
COMMAND lcov --capture
5149
--rc branch_coverage=1
52-
--ignore-errors empty
53-
--ignore-errors source
5450
--rc genhtml_branch_coverage=1
5551
--base-directory ${CMAKE_BINARY_DIR}
5652
--directory ${CMAKE_BINARY_DIR}
5753
--output-file ${CMAKE_BINARY_DIR}/second_coverage.info
58-
--quiet
54+
--include "*source*"
5955
)
6056

6157
# combile baseline results (zeros) with the one after running the tests
@@ -67,16 +63,10 @@ execute_process(
6763
--output-file ${CMAKE_BINARY_DIR}/coverage.info
6864
--no-external
6965
--rc branch_coverage=1
70-
--ignore-errors empty
71-
--ignore-errors source
72-
--quiet
7366
)
7467
execute_process(
7568
COMMAND genhtml --rc branch_coverage=1
76-
--ignore-errors empty
77-
--ignore-errors source
7869
--branch-coverage
7970
--output-directory ${CMAKE_BINARY_DIR}/coverage
8071
${CMAKE_BINARY_DIR}/coverage.info
81-
--quiet
82-
)
72+
)

0 commit comments

Comments
 (0)