Skip to content

Commit

Permalink
Use compile time timestamp as NTP backup.
Browse files Browse the repository at this point in the history
  • Loading branch information
aentinger committed Jun 20, 2024
1 parent 46b6c4f commit 952ebc2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/opcua_server/opcua_server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions src/Arduino_open62541.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "open62541.h"
#include "o1heap/o1heap.h"
#include "NTPUtils.h"
#include "cvt_time.h"

#include "ArduinoOpta.h"
#include "ArduinoOptaVariant.h"
Expand Down
70 changes: 70 additions & 0 deletions src/cvt_time.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <string.h>

/**************************************************************************************
* 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 */
35 changes: 35 additions & 0 deletions src/cvt_time.h
Original file line number Diff line number Diff line change
@@ -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 <time.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace opcua
{

/**************************************************************************************
* FUNCTION DECLARATION
**************************************************************************************/

time_t cvt_time(char const * time);

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* opcua */

0 comments on commit 952ebc2

Please sign in to comment.