1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 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  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 
33 #include "FreeRTOS.h"
34 #include "task.h"
35 
36 #include "date_and_time.h"
37 
38 int iTimeZone;
39 
40 uint32_t ulSeconds, ulMsec;
41 
42 /*
43  * You can add the following code to you FreeRTOSConfig file:
44  *
45  *  extern TickType_t ulSeconds, ulMsec;
46  *
47  #define traceINCREASE_TICK_COUNT( xTicksToJump ) \
48  *  { \
49  *      ulMsec += xTicksToJump; \
50  *      if( ulMsec >= 1000 ) \
51  *      { \
52  *          ulSeconds += ( ulMsec / 1000ul ); \
53  *          ulMsec = ( ulMsec % 1000ul ); \
54  *      } \
55  *  }
56  *
57  *
58  #define traceTASK_INCREMENT_TICK( xTickCount ) \
59  *  if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE ) \
60  *  { \
61  *      if( ++ulMsec >= 1000 ) \
62  *      { \
63  *          ulMsec = 0; \
64  *          ulSeconds++; \
65  *      } \
66  *  }
67  *
68  */
69 
70 
FreeRTOS_time(time_t * pxTime)71 time_t FreeRTOS_time( time_t * pxTime )
72 {
73     time_t uxTime;
74 
75     /* Critical section required if running on a 16 bit processor. */
76     portTICK_TYPE_ENTER_CRITICAL();
77     {
78         uxTime = ( time_t ) ulSeconds;
79     }
80     portTICK_TYPE_EXIT_CRITICAL();
81 
82     if( pxTime != NULL )
83     {
84         *pxTime = uxTime;
85     }
86 
87     return uxTime;
88 }
89 /*-----------------------------------------------------------*/
90 
FreeRTOS_settime(time_t * pxTime)91 void FreeRTOS_settime( time_t * pxTime )
92 {
93     /* Critical section required if running on a 16 bit processor. */
94     portTICK_TYPE_ENTER_CRITICAL();
95     {
96         ulSeconds = ( uint32_t ) *pxTime;
97         ulMsec = ( uint32_t ) 0;
98     }
99     portTICK_TYPE_EXIT_CRITICAL();
100 }
101 /*-----------------------------------------------------------*/
102 
FreeRTOS_get_secs_msec(time_t * pulMsec)103 time_t FreeRTOS_get_secs_msec( time_t * pulMsec )
104 {
105     time_t uxReturn;
106 
107     /* Critical section required if running on a 16 bit processor. */
108     portTICK_TYPE_ENTER_CRITICAL();
109     {
110         uxReturn = ( time_t ) ulSeconds;
111 
112         if( pulMsec != NULL )
113         {
114             *pulMsec = ulMsec;
115         }
116     }
117     portTICK_TYPE_EXIT_CRITICAL();
118 
119     return uxReturn;
120 }
121 /*-----------------------------------------------------------*/
122 
123 
FreeRTOS_set_secs_msec(time_t * pulSeconds,time_t * pulMsec)124 void FreeRTOS_set_secs_msec( time_t * pulSeconds,
125                              time_t * pulMsec )
126 {
127     /* Critical section required if running on a 16 bit processor. */
128     portTICK_TYPE_ENTER_CRITICAL();
129     {
130         ulSeconds = *pulSeconds;
131 
132         if( pulMsec != NULL )
133         {
134             ulMsec = *pulMsec;
135         }
136     }
137     portTICK_TYPE_EXIT_CRITICAL();
138 }
139 /*-----------------------------------------------------------*/
140