|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 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 | +#include "TimeSync.h" |
| 20 | +#include <esp_sntp.h> |
| 21 | +#include <lib/support/CHIPMemString.h> |
| 22 | +#include <lib/support/logging/CHIPLogging.h> |
| 23 | + |
| 24 | +static constexpr time_t kMinValidTimeStampEpoch = 1704067200; // 1 Jan 2019 |
| 25 | +static constexpr uint32_t kSecondsInADay = 24 * 60 * 60; |
| 26 | + |
| 27 | +namespace { |
| 28 | +const uint8_t kMaxNtpServerStringSize = 128; |
| 29 | +char sSntpServerName[kMaxNtpServerStringSize + 1]; |
| 30 | + |
| 31 | +CHIP_ERROR GetLocalTimeString(char * buf, size_t buf_len) |
| 32 | +{ |
| 33 | + VerifyOrReturnError(buf_len > 0, CHIP_ERROR_INVALID_ARGUMENT); |
| 34 | + struct tm timeinfo; |
| 35 | + char strftime_buf[64]; |
| 36 | + time_t now; |
| 37 | + time(&now); |
| 38 | + localtime_r(&now, &timeinfo); |
| 39 | + if (strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%dT%H:%M:%S%z", &timeinfo) == 0) |
| 40 | + { |
| 41 | + ChipLogError(DeviceLayer, "Buffer too small"); |
| 42 | + return CHIP_ERROR_BUFFER_TOO_SMALL; |
| 43 | + } |
| 44 | + size_t print_size = snprintf(buf, buf_len, "%s, DST: %s", strftime_buf, timeinfo.tm_isdst ? "Yes" : "No"); |
| 45 | + if (print_size >= (buf_len - 1)) |
| 46 | + { |
| 47 | + ChipLogError(DeviceLayer, "Buffer size %d insufficient for localtime string. Required size: %d", static_cast<int>(buf_len), |
| 48 | + static_cast<int>(print_size)); |
| 49 | + return CHIP_ERROR_BUFFER_TOO_SMALL; |
| 50 | + } |
| 51 | + return CHIP_NO_ERROR; |
| 52 | +} |
| 53 | + |
| 54 | +bool ValidateTime() |
| 55 | +{ |
| 56 | + time_t now; |
| 57 | + time(&now); |
| 58 | + return (now > kMinValidTimeStampEpoch); |
| 59 | +} |
| 60 | + |
| 61 | +CHIP_ERROR PrintCurrentTime() |
| 62 | +{ |
| 63 | + char local_time[64] = { 0 }; |
| 64 | + ReturnErrorOnFailure(GetLocalTimeString(local_time, sizeof(local_time))); |
| 65 | + if (!ValidateTime()) |
| 66 | + { |
| 67 | + ChipLogProgress(DeviceLayer, "Time not synchronised yet."); |
| 68 | + return CHIP_ERROR_INCORRECT_STATE; |
| 69 | + } |
| 70 | + ChipLogProgress(DeviceLayer, "The current time is: %s.", local_time); |
| 71 | + return CHIP_NO_ERROR; |
| 72 | +} |
| 73 | + |
| 74 | +void TimeSyncCallback(struct timeval * tv) |
| 75 | +{ |
| 76 | + ChipLogProgress(DeviceLayer, "SNTP Synchronised."); |
| 77 | + if (PrintCurrentTime() != CHIP_NO_ERROR) |
| 78 | + { |
| 79 | + ChipLogError(DeviceLayer, "SNTP time is not synchronised."); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +} // anonymous namespace |
| 84 | + |
| 85 | +namespace chip { |
| 86 | +namespace Esp32TimeSync { |
| 87 | +void Init(const char * aSntpServerName, const uint16_t aSyncSntpIntervalDay) |
| 88 | +{ |
| 89 | + chip::Platform::CopyString(sSntpServerName, aSntpServerName); |
| 90 | + if (esp_sntp_enabled()) |
| 91 | + { |
| 92 | + ChipLogProgress(DeviceLayer, "SNTP already initialized."); |
| 93 | + } |
| 94 | + ChipLogProgress(DeviceLayer, "Initializing SNTP. Using the SNTP server: %s", sSntpServerName); |
| 95 | + esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); |
| 96 | + esp_sntp_setservername(0, sSntpServerName); |
| 97 | + esp_sntp_set_sync_interval(kSecondsInADay * aSyncSntpIntervalDay); |
| 98 | + esp_sntp_init(); |
| 99 | + sntp_set_time_sync_notification_cb(TimeSyncCallback); |
| 100 | +} |
| 101 | +} // namespace Esp32TimeSync |
| 102 | +} // namespace chip |
0 commit comments