Coverage Report

Created: 2025-03-09 06:50

/src/openweave-core/src/system/SystemClock.cpp
Line
Count
Source (jump to first uncovered line)
1
    /*
2
 *
3
 *    Copyright (c) 2018 Nest Labs, Inc.
4
 *    All rights reserved.
5
 *
6
 *    Licensed under the Apache License, Version 2.0 (the "License");
7
 *    you may not use this file except in compliance with the License.
8
 *    You may obtain a copy of the License at
9
 *
10
 *        http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *    Unless required by applicable law or agreed to in writing, software
13
 *    distributed under the License is distributed on an "AS IS" BASIS,
14
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *    See the License for the specific language governing permissions and
16
 *    limitations under the License.
17
 */
18
19
/**
20
 *    @file
21
 *      Provides default implementations for the platform Get/SetClock_ functions
22
 *      for POSIX and LwIP platforms.
23
 */
24
25
// __STDC_LIMIT_MACROS must be defined for UINT8_MAX to be defined for pre-C++11 clib
26
#ifndef __STDC_LIMIT_MACROS
27
#define __STDC_LIMIT_MACROS
28
#endif // __STDC_LIMIT_MACROS
29
30
// __STDC_CONSTANT_MACROS must be defined for INT64_C and UINT64_C to be defined for pre-C++11 clib
31
#ifndef __STDC_CONSTANT_MACROS
32
#define __STDC_CONSTANT_MACROS
33
#endif // __STDC_CONSTANT_MACROS
34
35
#include <stdint.h>
36
37
#include <SystemLayer/SystemConfig.h>
38
39
#if !WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME
40
41
#include <SystemLayer/SystemClock.h>
42
#include <Weave/Support/CodeUtils.h>
43
#include <SystemLayer/SystemError.h>
44
#include "SystemLayerPrivate.h"
45
46
#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
47
#include <time.h>
48
#if !(HAVE_CLOCK_GETTIME)
49
#include <sys/time.h>
50
#endif
51
#include <errno.h>
52
#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
53
54
#if WEAVE_SYSTEM_CONFIG_USE_LWIP
55
#include <lwip/sys.h>
56
#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP
57
58
namespace nl {
59
namespace Weave {
60
namespace System {
61
namespace Platform {
62
namespace Layer {
63
64
// -------------------- Default Get/SetClock Functions for POSIX Systems --------------------
65
66
#if WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
67
68
#if !HAVE_CLOCK_GETTIME && !HAVE_GETTIMEOFDAY
69
#error "WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS requires either clock_gettime() or gettimeofday()"
70
#endif
71
72
#if HAVE_CLOCK_GETTIME
73
#if HAVE_DECL_CLOCK_BOOTTIME
74
// CLOCK_BOOTTIME is a Linux-specific option to clock_gettime for a clock which compensates for system sleep.
75
0
#define MONOTONIC_CLOCK_ID CLOCK_BOOTTIME
76
0
#define MONOTONIC_RAW_CLOCK_ID CLOCK_MONOTONIC_RAW
77
#else // HAVE_DECL_CLOCK_BOOTTIME
78
// CLOCK_MONOTONIC is defined in POSIX and hence is the default choice
79
#define MONOTONIC_CLOCK_ID CLOCK_MONOTONIC
80
#endif
81
#endif // HAVE_CLOCK_GETTIME
82
83
uint64_t GetClock_Monotonic(void)
84
0
{
85
0
#if HAVE_CLOCK_GETTIME
86
0
    struct timespec ts;
87
0
    int res = clock_gettime(MONOTONIC_CLOCK_ID, &ts);
88
0
    VerifyOrDie(res == 0);
89
0
    return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
90
#else // HAVE_CLOCK_GETTIME
91
    struct timeval tv;
92
    int res = gettimeofday(&tv, NULL);
93
    VerifyOrDie(res == 0);
94
    return (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec;
95
#endif // HAVE_CLOCK_GETTIME
96
0
}
97
98
uint64_t GetClock_MonotonicMS(void)
99
0
{
100
0
    return GetClock_Monotonic() / 1000;
101
0
}
102
103
uint64_t GetClock_MonotonicHiRes(void)
104
0
{
105
0
#if HAVE_CLOCK_GETTIME && defined(MONOTONIC_RAW_CLOCK_ID)
106
0
    struct timespec ts;
107
0
    int res = clock_gettime(MONOTONIC_RAW_CLOCK_ID, &ts);
108
0
    VerifyOrDie(res == 0);
109
0
    return (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
110
#else // HAVE_CLOCK_GETTIME
111
    return GetClock_Monotonic();
112
#endif // HAVE_CLOCK_GETTIME
113
0
}
114
115
Error GetClock_RealTime(uint64_t & curTime)
116
0
{
117
0
#if HAVE_CLOCK_GETTIME
118
0
    struct timespec ts;
119
0
    int res = clock_gettime(CLOCK_REALTIME, &ts);
120
0
    if (res != 0)
121
0
    {
122
0
        return MapErrorPOSIX(errno);
123
0
    }
124
0
    if (ts.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
125
0
    {
126
0
        return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
127
0
    }
128
0
    curTime = (ts.tv_sec * UINT64_C(1000000)) + (ts.tv_nsec / 1000);
129
0
    return WEAVE_SYSTEM_NO_ERROR;
130
#else // HAVE_CLOCK_GETTIME
131
    struct timeval tv;
132
    int res = gettimeofday(&tv, NULL);
133
    if (res != 0)
134
    {
135
        return MapErrorPOSIX(errno);
136
    }
137
    if (tv.tv_sec < WEAVE_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
138
    {
139
        return WEAVE_SYSTEM_ERROR_REAL_TIME_NOT_SYNCED;
140
    }
141
    curTime = (tv.tv_sec * UINT64_C(1000000)) + tv.tv_usec;
142
    return WEAVE_SYSTEM_NO_ERROR;
143
#endif // HAVE_CLOCK_GETTIME
144
0
}
145
146
Error GetClock_RealTimeMS(uint64_t & curTime)
147
0
{
148
0
    Error err = GetClock_RealTime(curTime);
149
0
    curTime = curTime / 1000;
150
0
    return err;
151
0
}
152
153
#if HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY
154
155
Error SetClock_RealTime(uint64_t newCurTime)
156
0
{
157
0
#if HAVE_CLOCK_SETTIME
158
0
    struct timespec ts;
159
0
    ts.tv_sec = static_cast<time_t>(newCurTime / UINT64_C(1000000));
160
0
    ts.tv_nsec = static_cast<long>(newCurTime % UINT64_C(1000000)) * 1000;
161
0
    int res = clock_settime(CLOCK_REALTIME, &ts);
162
0
    if (res != 0)
163
0
    {
164
0
        return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
165
0
    }
166
0
    return WEAVE_SYSTEM_NO_ERROR;
167
#else // HAVE_CLOCK_SETTIME
168
    struct timeval tv;
169
    tv.tv_sec = static_cast<time_t>(newCurTime / UINT64_C(1000000));
170
    tv.tv_usec = static_cast<long>(newCurTime % UINT64_C(1000000));
171
    int res = settimeofday(&tv, NULL);
172
    if (res != 0)
173
    {
174
        return (errno == EPERM) ? WEAVE_SYSTEM_ERROR_ACCESS_DENIED : MapErrorPOSIX(errno);
175
    }
176
    return WEAVE_SYSTEM_NO_ERROR;
177
#endif // HAVE_CLOCK_SETTIME
178
0
}
179
180
#endif // HAVE_CLOCK_SETTIME || HAVE_SETTIMEOFDAY
181
182
#endif // WEAVE_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
183
184
185
// -------------------- Default Get/SetClock Functions for LwIP Systems --------------------
186
187
#if WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME
188
189
uint64_t GetClock_Monotonic(void)
190
{
191
    return GetClock_MonotonicMS();
192
}
193
194
uint64_t GetClock_MonotonicMS(void)
195
{
196
    static volatile uint64_t overflow = 0;
197
    static volatile u32_t lastSample = 0;
198
    static volatile uint8_t lock = 0;
199
    static const uint64_t kOverflowIncrement = static_cast<uint64_t>(0x100000000);
200
201
    uint64_t overflowSample;
202
    u32_t sample;
203
204
    // Tracking timer wrap assumes that this function gets called with
205
    // a period that is less than 1/2 the timer range.
206
    if (__sync_bool_compare_and_swap(&lock, 0, 1))
207
    {
208
        sample = sys_now();
209
210
        if (lastSample > sample)
211
        {
212
            overflow += kOverflowIncrement;
213
        }
214
215
        lastSample = sample;
216
        overflowSample = overflow;
217
218
        __sync_bool_compare_and_swap(&lock, 1, 0);
219
    }
220
    else
221
    {
222
        // a lower priority task is in the block above. Depending where that
223
        // lower task is blocked can spell trouble in a timer wrap condition.
224
        // the question here is what this task should use as an overflow value.
225
        // To fix this race requires a platform api that can be used to
226
        // protect critical sections.
227
        overflowSample = overflow;
228
        sample = sys_now();
229
    }
230
231
    return static_cast<uint64_t>(overflowSample | static_cast<uint64_t>(sample));
232
}
233
234
uint64_t GetClock_MonotonicHiRes(void)
235
{
236
    return GetClock_MonotonicMS();
237
}
238
239
Error GetClock_RealTime(uint64_t & curTime)
240
{
241
    return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
242
}
243
244
Error GetClock_RealTimeMS(uint64_t & curTime)
245
{
246
    return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
247
}
248
249
Error SetClock_RealTime(uint64_t newCurTime)
250
{
251
    return WEAVE_SYSTEM_ERROR_NOT_SUPPORTED;
252
}
253
254
#endif // WEAVE_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME
255
256
} // namespace Layer
257
} // namespace Platform
258
} // namespace System
259
} // namespace Weave
260
} // namespace nl
261
262
#endif // WEAVE_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME