SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS MCU software
3 *
4 * Copyright (C) 2024, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
22#pragma once
23
24#include "bsp/bsp.hpp"
25#include <etl/string.h>
26#include <time.h>
27#include <zephyr/device.h>
28#include <zephyr/drivers/gnss.h>
29#include <zephyr/drivers/rtc.h>
30#include <zephyr/drivers/uart.h>
31#include <zephyr/kernel.h>
32#include <zephyr/logging/log.h>
33#include <zephyr/sys/timeutil.h>
34
35namespace satnogs::comms
36{
37
59class time
60{
61public:
62 static time &
64 {
65 static time instance;
66 return instance;
67 }
68
73 enum class time_src : uint8_t
74 {
79 1,
81 2,
82 };
83
84 static constexpr size_t to_string_max_size = 20;
98
99 /* Singleton */
100 time(time const &) = delete;
101
102 void
103 operator=(time const &) = delete;
104
105 void
106 gnss_data_cb(const struct device *dev, const struct gnss_data *data);
107
109 get(uint64_t &t);
110
112 get(struct tm &t);
113
114 void
115 set(const struct tm &t);
116
117 void
118 gnss(struct gnss_data &data);
119
120 uint64_t
121 uptime();
122
123 uint64_t
125
126 uint64_t
128
129 size_t
130 to_string(const struct tm &tm, etl::istring &s,
131 time::time_granularity granularity) const;
132 size_t
133 to_string(const struct tm &tm, etl::istring &s) const;
134
136 {
137 public:
138 to_string_exception(const char *file_name, int line)
139 : exception(exception::severity::MINOR, file_name, line,
140 error_msg{"Convertion of date to string failed", "tmtostr",
141 ENOTSUP})
142 {
143 }
144 };
145
146private:
147 /* Milliseconds Since Epoch (January 1, 1970, UTC.) */
148 uint64_t m_millis_since_epoch_gnss;
149 bool m_gnss_received_fix;
150 int64_t m_last_gnss_update_uptime;
151 int64_t m_last_nmea_update_uptime;
152 const struct device *m_rtc;
153 bool m_init;
154 mutable struct k_mutex m_mtx;
155 struct gnss_data m_gnss_data;
156
157 time();
158};
159
160} // namespace satnogs::comms
A class representing error messages in the SatNOGS-COMMS system.
Definition exception.hpp:83
Exception base class.
Definition exception.hpp:63
severity
Severity levels of exceptions.
Definition exception.hpp:71
@ MINOR
Failure having minimal impact.
Definition exception.hpp:75
exception(severity sev, const char *file, int lineno, const error_msg &err_msg)
Constructor for the exception class.
to_string_exception(const char *file_name, int line)
Definition time.hpp:138
void set(const struct tm &t)
Sets the RTC time.
Definition time.cpp:200
void gnss(struct gnss_data &data)
Retrieve latest GNSS information.
Definition time.cpp:237
time_src get(uint64_t &t)
Gets the time in ms.
Definition time.cpp:111
uint64_t uptime()
Gets the current system uptime in milliseconds.
Definition time.cpp:167
uint64_t last_gnss_update_uptime()
Gets the system uptime (in milliseconds) at the time of the last GNSS fix.
Definition time.cpp:179
uint64_t last_nmea_update_uptime()
Definition time.cpp:185
static time & get_instance()
Definition time.hpp:63
size_t to_string(const struct tm &tm, etl::istring &s, time::time_granularity granularity) const
Return the string represantation of a struct tm time instance following the ISO-8601 paradigm....
Definition time.cpp:290
void operator=(time const &)=delete
time_granularity
Granularity of time represented as string.
Definition time.hpp:90
void gnss_data_cb(const struct device *dev, const struct gnss_data *data)
Definition time.cpp:54
time(time const &)=delete
time_src
Source of the reported time.
Definition time.hpp:74
@ UPTIME
No RTC or GNSS time source. The uptime is used to track time.
Definition time.hpp:80
@ GNSS_ONLY
No RTC installed, but there is time information from a GNSS fix.
Definition time.hpp:78
static constexpr size_t to_string_max_size
Definition time.hpp:84