coreSNTP v1.2.0
Client library for synchronizing device time with internet time using Simple Network Time Protocol (SNTP)
core_sntp_serializer.h
Go to the documentation of this file.
1/*
2 * coreSNTP v1.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
33#ifndef CORE_SNTP_SERIALIZER_H_
34#define CORE_SNTP_SERIALIZER_H_
35
36/* Standard include. */
37#include <stdint.h>
38#include <stddef.h>
39
40/* *INDENT-OFF* */
41#ifdef __cplusplus
42 extern "C" {
43#endif
44/* *INDENT-ON* */
45
54#define SNTP_PACKET_BASE_SIZE ( 48U )
55
68#define SNTP_FRACTION_VALUE_PER_MICROSECOND ( 4295U )
69
85#define SNTP_TIME_AT_UNIX_EPOCH_SECS ( 2208988800U )
86
99#define SNTP_TIME_AT_LARGEST_UNIX_TIME_SECS ( 61505151U )
100
112#define UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME ( 2085978496U )
113
122#define SNTP_KISS_OF_DEATH_CODE_LENGTH ( 4U )
123
132#define SNTP_KISS_OF_DEATH_CODE_NONE ( 0U )
133
139typedef enum SntpStatus
140{
145
150
156
161
167
173
180
185
191
197
203
211
218
225
231
237
243
250
264typedef enum SntpLeapSecondInfo
265{
266 NoLeapSecond = 0x00,
272
283typedef struct SntpTimestamp
284{
285 uint32_t seconds;
286 uint32_t fractions;
289
295typedef struct SntpResponse
296{
301
307
320
347
348
381/* @[define_sntp_serializerequest] */
383 uint32_t randomNumber,
384 void * pBuffer,
385 size_t bufferSize );
386/* @[define_sntp_serializerequest] */
387
447/* @[define_sntp_deserializeresponse] */
449 const SntpTimestamp_t * pResponseRxTime,
450 const void * pResponseBuffer,
451 size_t bufferSize,
452 SntpResponseData_t * pParsedResponse );
453/* @[define_sntp_deserializeresponse] */
454
488/* @[define_sntp_calculatepollinterval] */
489SntpStatus_t Sntp_CalculatePollInterval( uint16_t clockFreqTolerance,
490 uint16_t desiredAccuracy,
491 uint32_t * pPollInterval );
492/* @[define_sntp_calculatepollinterval] */
493
494
523/* @[define_sntp_converttounixtime] */
525 uint32_t * pUnixTimeSecs,
526 uint32_t * pUnixTimeMicrosecs );
527/* @[define_sntp_converttounixtime] */
528
529/* *INDENT-OFF* */
530#ifdef __cplusplus
531 }
532#endif
533/* *INDENT-ON* */
534
535#endif /* ifndef CORE_SNTP_SERIALIZER_H_ */
SntpStatus_t Sntp_DeserializeResponse(const SntpTimestamp_t *pRequestTime, const SntpTimestamp_t *pResponseRxTime, const void *pResponseBuffer, size_t bufferSize, SntpResponseData_t *pParsedResponse)
De-serializes an SNTP packet received from a server as a response to a SNTP request.
Definition: core_sntp_serializer.c:673
SntpStatus_t Sntp_CalculatePollInterval(uint16_t clockFreqTolerance, uint16_t desiredAccuracy, uint32_t *pPollInterval)
Utility to calculate the poll interval of sending periodic time queries to servers to achieve a desir...
Definition: core_sntp_serializer.c:748
SntpStatus_t Sntp_ConvertToUnixTime(const SntpTimestamp_t *pSntpTime, uint32_t *pUnixTimeSecs, uint32_t *pUnixTimeMicrosecs)
Utility to convert SNTP timestamp (that uses 1st Jan 1900 as the epoch) to UNIX timestamp (that uses ...
Definition: core_sntp_serializer.c:812
SntpStatus_t Sntp_SerializeRequest(SntpTimestamp_t *pRequestTime, uint32_t randomNumber, void *pBuffer, size_t bufferSize)
Serializes an SNTP request packet to use for querying a time server.
Definition: core_sntp_serializer.c:605
SntpLeapSecondInfo_t
Enumeration for leap second information that an SNTP server can send its response to a time request....
Definition: core_sntp_serializer.h:265
SntpStatus_t
Enumeration of status codes that can be returned by the coreSNTP Library API.
Definition: core_sntp_serializer.h:140
@ LastMinuteHas59Seconds
A leap second should be inserted in the last minute before midnight.
Definition: core_sntp_serializer.h:268
@ LastMinuteHas61Seconds
There is no upcoming leap second adjustment.
Definition: core_sntp_serializer.h:267
@ AlarmServerNotSynchronized
A leap second should be deleted from the last minute before midnight.
Definition: core_sntp_serializer.h:269
@ SntpErrorSendTimeout
A timeout occurred in sending time request packet over the network to a server through the Sntp_SendT...
Definition: core_sntp_serializer.h:230
@ SntpErrorBadParameter
Invalid parameter passed to an API function.
Definition: core_sntp_serializer.h:149
@ SntpErrorBufferTooSmall
Application provided insufficient buffer space for serializing or de-serializing an SNTP packet....
Definition: core_sntp_serializer.h:179
@ SntpZeroPollInterval
Poll interval value is under 1 second which cannot be calculated by Sntp_CalculatePollInterval.
Definition: core_sntp_serializer.h:190
@ SntpErrorResponseTimeout
A timeout has occurred in receiving server response with the Sntp_ReceiveTimeResponse API.
Definition: core_sntp_serializer.h:236
@ SntpNoResponseReceived
No SNTP packet for server response is received from the network by the Sntp_ReceiveTimeResponse API.
Definition: core_sntp_serializer.h:242
@ SntpInvalidResponse
Server response failed validation checks for expected data in SNTP packet.
Definition: core_sntp_serializer.h:184
@ SntpErrorAuthFailure
Failure from the user-supplied authentication interface, SntpAuthenticationInterface_t,...
Definition: core_sntp_serializer.h:224
@ SntpErrorNetworkFailure
Networking operation of sending or receiving SNTP packet through the user-defined UDP transport inter...
Definition: core_sntp_serializer.h:210
@ SntpErrorDnsFailure
The user-defined DNS resolution interface, SntpResolveDns_t, failed to resolve address for a time ser...
Definition: core_sntp_serializer.h:202
@ SntpRejectedResponseChangeServer
Server sent a Kiss-o'-Death message with non-retryable code (i.e. DENY or RSTR).
Definition: core_sntp_serializer.h:160
@ SntpRejectedResponseRetryWithBackoff
Server sent a Kiss-o'-Death message with a RATE code, which means that client should back-off before ...
Definition: core_sntp_serializer.h:166
@ SntpSuccess
Successful operation of an SNTP API.
Definition: core_sntp_serializer.h:144
@ SntpRejectedResponse
Server sent a Kiss-o'-Death message to reject the request for time. This status can be returned by th...
Definition: core_sntp_serializer.h:155
@ SntpServerNotAuthenticated
Time server is not authenticated from the authentication data in its response. This status can be ret...
Definition: core_sntp_serializer.h:217
@ SntpRejectedResponseOtherCode
Server sent a Kiss-o'-Death message with a code, specific to the server. Application can inspect the ...
Definition: core_sntp_serializer.h:172
@ SntpErrorTimeNotSupported
SNTP timestamp cannot be converted to UNIX time as time does not lie in time range supported by Sntp_...
Definition: core_sntp_serializer.h:196
@ SntpErrorContextNotInitialized
The SNTP context passed to Sntp_SendTimeRequest or Sntp_ReceiveTimeResponse APIs is is uninitialized.
Definition: core_sntp_serializer.h:248
Structure representing data parsed from an SNTP response from server as well as data of arithmetic ca...
Definition: core_sntp_serializer.h:296
SntpTimestamp_t serverTime
The timestamp sent by the server.
Definition: core_sntp_serializer.h:300
uint32_t rejectedResponseCode
If a server responded with Kiss-o'-Death message to reject time request, this is the fixed length ASC...
Definition: core_sntp_serializer.h:319
SntpLeapSecondInfo_t leapSecondType
The information of an upcoming leap second in the server response.
Definition: core_sntp_serializer.h:306
int64_t clockOffsetMs
The offset (in milliseconds) of the system clock relative to the server time calculated from timestam...
Definition: core_sntp_serializer.h:345
Structure representing an SNTP timestamp.
Definition: core_sntp_serializer.h:284
uint32_t fractions
The fractions part of the SNTP timestamp with resolution of 2^(-32) ~ 232 picoseconds.
Definition: core_sntp_serializer.h:286
uint32_t seconds
Number of seconds since epoch time.
Definition: core_sntp_serializer.h:285