~aleteoryx/muditaos

ref: master muditaos/module-utils/log/board/rt1051/time_syscalls.cpp -rw-r--r-- 1.2 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md
#include <time/time_syscalls.hpp>
#include <bsp/rtc/rtc.hpp>
#include <mutex.hpp>
#include <errno.h>
#include <cstring>

namespace utils::internal::syscalls
{
    namespace
    {
        cpp_freertos::MutexRecursive g_time_lock;
        cpp_freertos::MutexRecursive g_env_lock;
    } // namespace

    int gettimeofday(int &_errno_, struct timeval *tp, void *tzp)
    {
        if (!tp) {
            _errno_ = EINVAL;
            return -1;
        }
        time_t curtime;
        auto err = bsp::rtc::getCurrentTimestamp(&curtime);
        if (err != bsp::rtc::ErrorCode::OK) {
            _errno_ = EIO;
            return -1;
        }
        tp->tv_sec  = curtime;
        tp->tv_usec = 0;
        return 0;
    }
    void tz_lock()
    {
        g_time_lock.Lock();
    }
    void tz_unlock()
    {
        g_time_lock.Unlock();
    }

    void env_lock(int &_errno_)
    {
        if (!g_env_lock.Lock()) {
            _errno_ = ENXIO;
        }
    }
    void env_unlock(int &_errno_)
    {
        if (!g_env_lock.Unlock()) {
            _errno_ = ENXIO;
        }
    }
} // namespace utils::internal::syscalls