/src/connectedhomeip/src/platform/Linux/SystemTimeSupport.cpp
Line | Count | Source |
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 implementations of the CHIP System Layer platform |
22 | | * time/clock functions that are suitable for use on the Posix platform. |
23 | | */ |
24 | | |
25 | | #include <platform/internal/CHIPDeviceLayerInternal.h> |
26 | | |
27 | | #include <lib/support/TimeUtils.h> |
28 | | #include <lib/support/logging/CHIPLogging.h> |
29 | | |
30 | | #include <chrono> |
31 | | #include <errno.h> |
32 | | #include <inttypes.h> |
33 | | #include <sys/time.h> |
34 | | |
35 | | namespace chip { |
36 | | namespace System { |
37 | | namespace Clock { |
38 | | |
39 | | namespace Internal { |
40 | | ClockImpl gClockImpl; |
41 | | } // namespace Internal |
42 | | |
43 | | Microseconds64 ClockImpl::GetMonotonicMicroseconds64() |
44 | 0 | { |
45 | 0 | return std::chrono::duration_cast<Microseconds64>(std::chrono::steady_clock::now().time_since_epoch()); |
46 | 0 | } |
47 | | |
48 | | Milliseconds64 ClockImpl::GetMonotonicMilliseconds64() |
49 | 97.8k | { |
50 | 97.8k | return std::chrono::duration_cast<Milliseconds64>(std::chrono::steady_clock::now().time_since_epoch()); |
51 | 97.8k | } |
52 | | |
53 | | CHIP_ERROR ClockImpl::GetClock_RealTime(Microseconds64 & aCurTime) |
54 | 0 | { |
55 | 0 | struct timeval tv; |
56 | 0 | if (gettimeofday(&tv, nullptr) != 0) |
57 | 0 | { |
58 | 0 | return CHIP_ERROR_POSIX(errno); |
59 | 0 | } |
60 | 0 | if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) |
61 | 0 | { |
62 | 0 | return CHIP_ERROR_REAL_TIME_NOT_SYNCED; |
63 | 0 | } |
64 | 0 | if (tv.tv_usec < 0) |
65 | 0 | { |
66 | 0 | return CHIP_ERROR_REAL_TIME_NOT_SYNCED; |
67 | 0 | } |
68 | 0 | static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!"); |
69 | 0 | aCurTime = Microseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec)); |
70 | 0 | return CHIP_NO_ERROR; |
71 | 0 | } |
72 | | |
73 | | CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Milliseconds64 & aCurTime) |
74 | 0 | { |
75 | 0 | Microseconds64 curTimeUs; |
76 | 0 | auto err = GetClock_RealTime(curTimeUs); |
77 | 0 | aCurTime = std::chrono::duration_cast<Milliseconds64>(curTimeUs); |
78 | 0 | return err; |
79 | 0 | } |
80 | | |
81 | | CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime) |
82 | 0 | { |
83 | 0 | struct timeval tv; |
84 | 0 | tv.tv_sec = static_cast<time_t>(aNewCurTime.count() / UINT64_C(1000000)); |
85 | 0 | tv.tv_usec = static_cast<long>(aNewCurTime.count() % UINT64_C(1000000)); |
86 | 0 | if (settimeofday(&tv, nullptr) != 0) |
87 | 0 | { |
88 | 0 | return (errno == EPERM) ? CHIP_ERROR_ACCESS_DENIED : CHIP_ERROR_POSIX(errno); |
89 | 0 | } |
90 | 0 | #if CHIP_PROGRESS_LOGGING |
91 | 0 | { |
92 | 0 | const time_t timep = tv.tv_sec; |
93 | 0 | struct tm calendar; |
94 | 0 | localtime_r(&timep, &calendar); |
95 | 0 | ChipLogProgress(DeviceLayer, "Real time clock set to %lld (%04d/%02d/%02d %02d:%02d:%02d UTC)", |
96 | 0 | static_cast<long long>(tv.tv_sec), calendar.tm_year, calendar.tm_mon, calendar.tm_mday, calendar.tm_hour, |
97 | 0 | calendar.tm_min, calendar.tm_sec); |
98 | 0 | } |
99 | 0 | #endif // CHIP_PROGRESS_LOGGING |
100 | 0 | return CHIP_NO_ERROR; |
101 | 0 | } |
102 | | |
103 | | } // namespace Clock |
104 | | } // namespace System |
105 | | } // namespace chip |