From 79d353b974b9cec70af00cc43ce8a805637bdaec Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Mon, 15 Mar 2021 23:16:42 +0100 Subject: [PATCH 1/2] libc: minimal: implement time() API Implement time() API by using clock_gettime(CLOCK_REALTIME, ...). Signed-off-by: Marcin Niestroj --- lib/libc/minimal/CMakeLists.txt | 2 ++ lib/libc/minimal/include/time.h | 2 ++ lib/libc/minimal/source/time/time.c | 28 +++++++++++++++++++ .../net/civetweb/common/src/libc_extensions.c | 5 ---- 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 lib/libc/minimal/source/time/time.c diff --git a/lib/libc/minimal/CMakeLists.txt b/lib/libc/minimal/CMakeLists.txt index 2c7afb115b28a2..b8f9475ef96c35 100644 --- a/lib/libc/minimal/CMakeLists.txt +++ b/lib/libc/minimal/CMakeLists.txt @@ -20,3 +20,5 @@ zephyr_library_sources( source/stdout/fprintf.c source/time/gmtime.c ) + +zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK source/time/time.c) diff --git a/lib/libc/minimal/include/time.h b/lib/libc/minimal/include/time.h index 2de85621d16326..946f1a83a5327e 100644 --- a/lib/libc/minimal/include/time.h +++ b/lib/libc/minimal/include/time.h @@ -52,6 +52,8 @@ struct tm *gmtime(const time_t *timep); struct tm *gmtime_r(const time_t *_MLIBC_RESTRICT timep, struct tm *_MLIBC_RESTRICT result); +time_t time(time_t *tloc); + #ifdef __cplusplus } #endif diff --git a/lib/libc/minimal/source/time/time.c b/lib/libc/minimal/source/time/time.c new file mode 100644 index 00000000000000..dbb21a3cec2c08 --- /dev/null +++ b/lib/libc/minimal/source/time/time.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Golioth, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +/* clock_gettime() prototype */ +#include + +time_t time(time_t *tloc) +{ + struct timespec ts; + int ret; + + ret = clock_gettime(CLOCK_REALTIME, &ts); + if (ret < 0) { + /* errno is already set by clock_gettime */ + return (time_t) -1; + } + + if (tloc) { + *tloc = ts.tv_sec; + } + + return ts.tv_sec; +} diff --git a/samples/net/civetweb/common/src/libc_extensions.c b/samples/net/civetweb/common/src/libc_extensions.c index c92c8b11144534..5e9e23b4ccf5cb 100644 --- a/samples/net/civetweb/common/src/libc_extensions.c +++ b/samples/net/civetweb/common/src/libc_extensions.c @@ -159,11 +159,6 @@ long long strtoll(const char *str, char **endptr, int base) return (long long)strtol(str, endptr, base); } -time_t time(time_t *t) -{ - return 0; -} - /* * Most of the wrappers below are copies of the wrappers in net/sockets.h, * but they are available only if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled From 2fdafaa9bdbae7844a5d6b25287cbbbe435a8914 Mon Sep 17 00:00:00 2001 From: Marcin Niestroj Date: Thu, 18 Mar 2021 23:27:05 +0100 Subject: [PATCH 2/2] tests: lib: time: add test for time() function Add test cases for time() function. Signed-off-by: Marcin Niestroj --- tests/lib/time/CMakeLists.txt | 8 ++++ tests/lib/time/prj.conf | 2 + tests/lib/time/src/main.c | 70 +++++++++++++++++++++++++++++++++++ tests/lib/time/testcase.yaml | 6 +++ 4 files changed, 86 insertions(+) create mode 100644 tests/lib/time/CMakeLists.txt create mode 100644 tests/lib/time/prj.conf create mode 100644 tests/lib/time/src/main.c create mode 100644 tests/lib/time/testcase.yaml diff --git a/tests/lib/time/CMakeLists.txt b/tests/lib/time/CMakeLists.txt new file mode 100644 index 00000000000000..0ffcb2b7e8216c --- /dev/null +++ b/tests/lib/time/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(time) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/lib/time/prj.conf b/tests/lib/time/prj.conf new file mode 100644 index 00000000000000..786d6bc705ed8b --- /dev/null +++ b/tests/lib/time/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_POSIX_CLOCK=y diff --git a/tests/lib/time/src/main.c b/tests/lib/time/src/main.c new file mode 100644 index 00000000000000..f247e39a0abf78 --- /dev/null +++ b/tests/lib/time/src/main.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 Golioth, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +static void test_time_passing(void) +{ + time_t time_initial_unaligned; + time_t time_initial; + time_t time_current; + int i; + + time_initial_unaligned = time(NULL); + zassert_true(time_initial_unaligned >= 0, "Fail to get time"); + + /* Wait until time() will return new value, which should be aligned */ + for (i = 0; i < 100; i++) { + k_sleep(K_MSEC(10)); + + if (time(NULL) != time_initial_unaligned) { + break; + } + } + + time_initial = time(NULL); + zassert_equal(time_initial, time_initial_unaligned + 1, + "Time (%d) should be one second larger than initially (%d)", + time_initial, time_initial_unaligned); + + for (i = 1; i <= 10; i++) { + k_sleep(K_SECONDS(1)); + + time_current = time(NULL); + zassert_equal(time_current, time_initial + i, + "Current time (%d) does not match expected time (%d)", + (int) time_current, (int) (time_initial + i)); + } +} + +static void test_time_param(void) +{ + time_t time_result; + time_t time_param; + int i; + + time_result = time(&time_param); + + zassert_equal(time_result, time_param, + "time() result does not match param value"); + + for (i = 0; i < 10; i++) { + k_sleep(K_SECONDS(1)); + + zassert_equal(time_result, time_param, + "time() result does not match param value"); + } +} + +void test_main(void) +{ + ztest_test_suite(libc_time, + ztest_unit_test(test_time_passing), + ztest_unit_test(test_time_param) + ); + ztest_run_test_suite(libc_time); +} diff --git a/tests/lib/time/testcase.yaml b/tests/lib/time/testcase.yaml new file mode 100644 index 00000000000000..bb3fd26125602a --- /dev/null +++ b/tests/lib/time/testcase.yaml @@ -0,0 +1,6 @@ +tests: + libraries.libc.time: + tags: libc + filter: not CONFIG_ARCH_POSIX or CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME + integration_platforms: + - mps2_an385