~aleteoryx/muditaos

194368db9dfbea0ecd9c6d221de84f3d01f0355c — Lucjan Bryndza 5 years ago 622a714
[EGD-5047] Add support for newlib time

Add support for newlib time standard syscalls.
M board/rt1051/newlib/io_syscalls.cpp => board/rt1051/newlib/io_syscalls.cpp +22 -0
@@ 9,6 9,8 @@
#include <newlib/vfs_io_syscalls.hpp>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <time/time_syscalls.hpp>

extern "C"
{
    using namespace vfsn::internal;


@@ 128,4 130,24 @@ extern "C"
    {
        return syscalls::umount(_REENT->_errno, special_file);
    }
    int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp)
    {
        return utils::internal::syscalls::gettimeofday(r->_errno, tp, tzp);
    }
    void __tz_lock()
    {
        utils::internal::syscalls::tz_lock();
    }
    void __tz_unlock()
    {
        utils::internal::syscalls::tz_unlock();
    }
    void __env_lock(struct _reent *reent)
    {
        utils::internal::syscalls::env_lock(reent->_errno);
    }
    void __env_unlock(struct _reent *reent)
    {
        utils::internal::syscalls::env_unlock(reent->_errno);
    }
}

A module-utils/board/cross/time_syscalls.cpp => module-utils/board/cross/time_syscalls.cpp +54 -0
@@ 0,0 1,54 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/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 != RtcBspOK) {
            _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

M module-utils/targets/Target_Cross.cmake => module-utils/targets/Target_Cross.cmake +1 -1
@@ 1,7 1,7 @@
set(BOARD_SOURCES ${BOARD_SOURCES}

        ${CMAKE_CURRENT_SOURCE_DIR}/board/cross/log_rt1051.cpp

        ${CMAKE_CURRENT_SOURCE_DIR}/board/cross/time_syscalls.cpp
        CACHE INTERNAL ""
        )


A module-utils/time/time_syscalls.hpp => module-utils/time/time_syscalls.hpp +15 -0
@@ 0,0 1,15 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <sys/time.h>

namespace utils::internal::syscalls
{
    int gettimeofday(int &_errno_, struct timeval *tp, void *tzp);
    void tz_lock();
    void tz_unlock();
    void env_lock(int &_errno_);
    void env_unlock(int &_errno_);
} // namespace utils::internal::syscalls