1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 /*
28  * This file belongs to the demo for showing use of the coreSNTP library for synchronizing
29  * system time with internet time and maintaining correct wall-clock time.
30  *
31  * This file shows how an application task can query Coordinated Universal Time (UTC) from system,
32  * that is maintained and synchronized periodically in the SNTP client (daemon) task of the demo
33  * project. Refer to the the SNTPClientTask.c file in this project for the SNTP client task.
34  */
35 
36 /* Standard includes. */
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <time.h>
41 
42 /* Kernel includes. */
43 #include "FreeRTOS.h"
44 #include "task.h"
45 
46 /* Demo includes. */
47 #include "common_demo_include.h"
48 
49 /*-----------------------------------------------------------*/
50 
51 /**
52  * @brief The periodicity of the application task in query and logging system time. This is the time
53  * interval between consecutive system clock time queries in the sample application task.
54  */
55 #define CLOCK_QUERY_TASK_DELAY_MS    ( 1000 )
56 
57 /*-----------------------------------------------------------*/
58 
printTime(const UTCTime_t * pUnixTime)59 void printTime( const UTCTime_t * pUnixTime )
60 {
61     struct tm * currTime;
62     time_t time;
63 
64     /* Obtain the broken-down UTC representation of the current system time. */
65     time = pUnixTime->secs;
66     currTime = gmtime( &time );
67 
68     /* Log the time as both UNIX timestamp and Human Readable time. */
69     LogInfo( ( "Time:\nUNIX=%lusecs %lums\nHuman Readable=%lu-%02lu-%02lu %02luh:%02lum:%02lus",
70                pUnixTime->secs, pUnixTime->msecs,
71                currTime->tm_year + 1900, currTime->tm_mon + 1, currTime->tm_mday,
72                currTime->tm_hour, currTime->tm_min, currTime->tm_sec ) );
73 }
74 
75 /*************************************************************************************/
76 
77 /* Sample application task that will query and log system time every second. */
sampleAppTask(void * pvParameters)78 void sampleAppTask( void * pvParameters )
79 {
80     UTCTime_t systemTime;
81 
82     while( 1 )
83     {
84         systemGetWallClockTime( &systemTime );
85 
86         printTime( &systemTime );
87 
88         vTaskDelay( pdMS_TO_TICKS( CLOCK_QUERY_TASK_DELAY_MS ) );
89     }
90 }
91 
92 /*-----------------------------------------------------------*/
93