From 952ebc21135c63d817961297026238acc36d1d95 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 20 Jun 2024 07:05:58 +0200 Subject: [PATCH] Use compile time timestamp as NTP backup. --- examples/opcua_server/opcua_server.ino | 2 + src/Arduino_open62541.h | 1 + src/cvt_time.cpp | 70 ++++++++++++++++++++++++++ src/cvt_time.h | 35 +++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/cvt_time.cpp create mode 100644 src/cvt_time.h diff --git a/examples/opcua_server/opcua_server.ino b/examples/opcua_server/opcua_server.ino index 4960b3a..cbdc533 100644 --- a/examples/opcua_server/opcua_server.ino +++ b/examples/opcua_server/opcua_server.ino @@ -198,6 +198,8 @@ void setup() auto const epoch = opcua::NTPUtils::getTime(udp_client); if (epoch > 0) { set_time(epoch); /* Directly set RTC of Arduino Opta. */ + } else { + set_time(opcua::cvt_time(__DATE__)); /* Configure Arduino Opta with time at compile time as last time of defense. */ } /* Initialize heap memory. */ diff --git a/src/Arduino_open62541.h b/src/Arduino_open62541.h index 3e6a57c..68fe6a4 100644 --- a/src/Arduino_open62541.h +++ b/src/Arduino_open62541.h @@ -16,6 +16,7 @@ #include "open62541.h" #include "o1heap/o1heap.h" #include "NTPUtils.h" +#include "cvt_time.h" #include "ArduinoOpta.h" #include "ArduinoOptaVariant.h" diff --git a/src/cvt_time.cpp b/src/cvt_time.cpp new file mode 100644 index 0000000..46e6d6a --- /dev/null +++ b/src/cvt_time.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2024 Arduino + * + * SPDX-License-Identifier: MPL-2.0 + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include "cvt_time.h" + +#include +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +namespace opcua +{ + +/************************************************************************************** + * FUNCTION DEFINITION + **************************************************************************************/ + +time_t cvt_time(char const * time) +{ + static time_t build_time = 0; + + if (!build_time) { + char s_month[5]; + int month, day, year; + struct tm t = + { + 0 /* tm_sec */, + 0 /* tm_min */, + 0 /* tm_hour */, + 0 /* tm_mday */, + 0 /* tm_mon */, + 0 /* tm_year */, + 0 /* tm_wday */, + 0 /* tm_yday */, + 0 /* tm_isdst */ + }; + static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; + + sscanf(time, "%s %d %d", s_month, &day, &year); + + month = (strstr(month_names, s_month) - month_names) / 3; + + t.tm_mon = month; + t.tm_mday = day; + t.tm_year = year - 1900; + t.tm_isdst = -1; + + build_time = mktime(&t); + } + + return build_time; +} + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +} /* opcua */ diff --git a/src/cvt_time.h b/src/cvt_time.h new file mode 100644 index 0000000..231d182 --- /dev/null +++ b/src/cvt_time.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 Arduino + * + * SPDX-License-Identifier: MPL-2.0 + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +namespace opcua +{ + +/************************************************************************************** + * FUNCTION DECLARATION + **************************************************************************************/ + +time_t cvt_time(char const * time); + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +} /* opcua */