Skip to content

Commit 0096fa2

Browse files
committed
samd/mbedtls: Add time and date functions for mbedtls.
Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent d5e5ede commit 0096fa2

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

ports/samd/mbedtls/mbedtls_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
2727
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
2828

29+
// Time hook.
30+
#include <time.h>
31+
extern time_t samd_rtctime_seconds(time_t *timer);
32+
#define MBEDTLS_PLATFORM_TIME_MACRO samd_rtctime_seconds
2933
// Set MicroPython-specific options.
3034
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1)
3135

ports/samd/mbedtls/mbedtls_port.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
2828

2929
#include "mbedtls_config.h"
3030
#include <stdint.h>
31-
uint32_t trng_random_u32();
31+
uint32_t trng_random_u32(void);
32+
#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE)
33+
#include <stdbool.h>
34+
#include "py/runtime.h"
35+
#include "shared/timeutils/timeutils.h"
36+
extern void rtc_gettime(timeutils_struct_time_t *tm);
37+
#endif
3238

3339
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
3440

@@ -40,3 +46,31 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
4046
}
4147

4248
#endif
49+
50+
#if defined(MBEDTLS_HAVE_TIME)
51+
time_t samd_rtctime_seconds(time_t *timer) {
52+
timeutils_struct_time_t tm;
53+
rtc_gettime(&tm);
54+
return timeutils_seconds_since_epoch(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
55+
}
56+
#endif
57+
58+
#if defined(MBEDTLS_HAVE_TIME_DATE)
59+
struct tm *gmtime(const time_t *timep) {
60+
static struct tm tm;
61+
timeutils_struct_time_t tm_buf = {0};
62+
timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf);
63+
64+
tm.tm_sec = tm_buf.tm_sec;
65+
tm.tm_min = tm_buf.tm_min;
66+
tm.tm_hour = tm_buf.tm_hour;
67+
tm.tm_mday = tm_buf.tm_mday;
68+
tm.tm_mon = tm_buf.tm_mon - 1;
69+
tm.tm_year = tm_buf.tm_year - 1900;
70+
tm.tm_wday = tm_buf.tm_wday;
71+
tm.tm_yday = tm_buf.tm_yday;
72+
tm.tm_isdst = -1;
73+
74+
return &tm;
75+
}
76+
#endif

0 commit comments

Comments
 (0)