Skip to content

Commit 139228c

Browse files
committed
Use compile time timestamp as NTP backup.
1 parent 4eff67f commit 139228c

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

examples/opcua_server/opcua_server.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ void setup()
198198
auto const epoch = opcua::NTPUtils::getTime(udp_client);
199199
if (epoch > 0) {
200200
set_time(epoch); /* Directly set RTC of Arduino Opta. */
201+
} else {
202+
set_time(opcua::cvt_time(__DATE__)); /* Configure Arduino Opta with time at compile time as last time of defense. */
201203
}
202204

203205
/* Initialize heap memory. */

src/Arduino_open62541.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "open62541.h"
1717
#include "o1heap/o1heap.h"
1818
#include "NTPUtils.h"
19+
#include "cvt_time.h"
1920

2021
#include "ArduinoOpta.h"
2122
#include "ArduinoOptaVariant.h"

src/cvt_time.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024 Arduino
3+
*
4+
* SPDX-License-Identifier: MPL-2.0
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
10+
/**************************************************************************************
11+
* INCLUDE
12+
**************************************************************************************/
13+
14+
#include "cvt_time.h"
15+
16+
#include <stdio.h>
17+
#include <string.h>
18+
19+
/**************************************************************************************
20+
* NAMESPACE
21+
**************************************************************************************/
22+
23+
namespace opcua
24+
{
25+
26+
/**************************************************************************************
27+
* FUNCTION DEFINITION
28+
**************************************************************************************/
29+
30+
time_t cvt_time(char const * time)
31+
{
32+
static time_t build_time = 0;
33+
34+
if (!build_time) {
35+
char s_month[5];
36+
int month, day, year;
37+
struct tm t =
38+
{
39+
0 /* tm_sec */,
40+
0 /* tm_min */,
41+
0 /* tm_hour */,
42+
0 /* tm_mday */,
43+
0 /* tm_mon */,
44+
0 /* tm_year */,
45+
0 /* tm_wday */,
46+
0 /* tm_yday */,
47+
0 /* tm_isdst */
48+
};
49+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
50+
51+
sscanf(time, "%s %d %d", s_month, &day, &year);
52+
53+
month = (strstr(month_names, s_month) - month_names) / 3;
54+
55+
t.tm_mon = month;
56+
t.tm_mday = day;
57+
t.tm_year = year - 1900;
58+
t.tm_isdst = -1;
59+
60+
build_time = mktime(&t);
61+
}
62+
63+
return build_time;
64+
}
65+
66+
/**************************************************************************************
67+
* NAMESPACE
68+
**************************************************************************************/
69+
70+
} /* opcua */

src/cvt_time.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024 Arduino
3+
*
4+
* SPDX-License-Identifier: MPL-2.0
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
10+
#pragma once
11+
12+
/**************************************************************************************
13+
* INCLUDE
14+
**************************************************************************************/
15+
16+
#include <time.h>
17+
18+
/**************************************************************************************
19+
* NAMESPACE
20+
**************************************************************************************/
21+
22+
namespace opcua
23+
{
24+
25+
/**************************************************************************************
26+
* FUNCTION DECLARATION
27+
**************************************************************************************/
28+
29+
time_t cvt_time(char const * time);
30+
31+
/**************************************************************************************
32+
* NAMESPACE
33+
**************************************************************************************/
34+
35+
} /* opcua */

0 commit comments

Comments
 (0)