From baa64bf7954e3bfa4b4f533730dc55e3b80d0ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20S=C3=B6ntgen?= Date: Wed, 20 Aug 2014 16:49:41 +0200 Subject: [PATCH] vfs: add Rtc_file_system The Rtc_file_system reads the current time from a Rtc_session and provides the time as '%Y-%m-%d %H:%M\n' to all users of the vfs node. Fixes #1238. --- repos/os/include/vfs/rtc_file_system.h | 79 ++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 repos/os/include/vfs/rtc_file_system.h diff --git a/repos/os/include/vfs/rtc_file_system.h b/repos/os/include/vfs/rtc_file_system.h new file mode 100644 index 000000000..389bc3553 --- /dev/null +++ b/repos/os/include/vfs/rtc_file_system.h @@ -0,0 +1,79 @@ +/* + * \brief Rtc file system + * \author Josef Soentgen + * \date 2014-08-20 + */ + +/* + * Copyright (C) 2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#ifndef _INCLUDE__VFS__RTC_FILE_SYSTEM_H_ +#define _INCLUDE__VFS__RTC_FILE_SYSTEM_H_ + +/* Genode includes */ +#include +#include + +/* libc includes */ +#include + +namespace Vfs { class Rtc_file_system; } + + +class Vfs::Rtc_file_system : public Single_file_system +{ + private: + + Rtc::Connection _rtc; + + public: + + Rtc_file_system(Xml_node config) + : + Single_file_system(NODE_TYPE_CHAR_DEVICE, name(), config) + { } + + static char const *name() { return "rtc"; } + + + /******************************** + ** File I/O service interface ** + ********************************/ + + Write_result write(Vfs_handle *, char const *, size_t count, size_t &count_out) override + { + return WRITE_ERR_IO; + } + + /** + * Read the current time from the RTC + * + * On each read the current time is queried and afterwards formated + * as '%Y-%m-%d %H:%M\n'. + */ + Read_result read(Vfs_handle *vfs_handle, char *dst, size_t count, size_t &out_count) override + { + time_t t = _rtc.get_current_time() / 1000000ULL; + + struct tm *tm = localtime(&t); + + char buf[16 + 1 + 1]; + Genode::snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d\n", + 1900 + tm->tm_year, /* years since 1900 */ + 1 + tm->tm_mon, /* months since January [0-11] */ + tm->tm_mday, tm->tm_hour, tm->tm_min); + + size_t len = count > sizeof(buf) ? sizeof(buf) : count; + Genode::memcpy(dst, buf, len); + + out_count = len; + + return READ_OK; + } +}; + +#endif /* _INCLUDE__VFS__RTC_FILE_SYSTEM_H_ */