forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_time.h
151 lines (133 loc) · 4.94 KB
/
date_time.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef DATE_TIME_H__
#define DATE_TIME_H__
#include <zephyr/types.h>
#include <time.h>
#include <stdbool.h>
/**
* @defgroup date_time Date Time Library
* @{
* @brief Library that maintains the current date time UTC which can be fetched
* in order to timestamp data.
*/
#ifdef __cplusplus
extern "C" {
#endif
/** @brief Date time notification event types used to signal the application. */
enum date_time_evt_type {
/** Date time library has obtained valid time from the modem. */
DATE_TIME_OBTAINED_MODEM,
/** Date time library has obtained valid time from NTP servers. */
DATE_TIME_OBTAINED_NTP,
/** Date time library has obtained valid time from external source. */
DATE_TIME_OBTAINED_EXT,
/** Date time library does not have valid time. */
DATE_TIME_NOT_OBTAINED
};
/** @brief Struct with data received from the Date time library. */
struct date_time_evt {
/** Type of event. */
enum date_time_evt_type type;
};
/** @brief Date time library asynchronous event handler.
*
* @param[in] evt The event and any associated parameters.
*/
typedef void (*date_time_evt_handler_t)(const struct date_time_evt *evt);
/** @brief Set the current date time.
*
* @note See http://www.cplusplus.com/reference/ctime/tm/ for accepted input
* format.
*
* @param[in] new_date_time Pointer to a tm structure.
*
* @return 0 If the operation was successful.
* @return -EINVAL If a member of the passing variable new_date_time does not
* adhere to the tm structure format, or pointer is passed in as NULL.
*/
int date_time_set(const struct tm *new_date_time);
/** @brief Get the date time UTC when the passing variable uptime was set.
* This function requires that k_uptime_get() has been called on the
* passing variable uptime prior to the function call.
*
* @warning If the function fails, the passed in variable retains its
* old value.
*
* @param[in, out] uptime Pointer to a previously set uptime.
*
* @return 0 If the operation was successful.
* @return -ENODATA If the library does not have a valid date time UTC.
* @return -EINVAL If the passed in pointer is NULL, dereferenced value is too large,
* or already converted.
*/
int date_time_uptime_to_unix_time_ms(int64_t *uptime);
/** @brief Get the current date time UTC.
*
* @warning If the function fails, the passed in variable retains its
* old value.
*
* @param[out] unix_time_ms Pointer to a variable to store the current date
* time UTC.
*
* @return 0 If the operation was successful.
* @return -ENODATA If the library does not have a valid date time UTC.
* @return -EINVAL If the passed in pointer is NULL.
*/
int date_time_now(int64_t *unix_time_ms);
/** @brief Convenience function that checks if the library has obtained
* an initial valid date time.
*
* @note If this function returns false there is no point of
* subsequent calls to other functions in this API that
* depend on the validity of the internal date time. We
* know that they would fail beforehand.
*
* @return true The library has obtained an initial date time.
* @return false The library has not obtained an initial date time.
*/
bool date_time_is_valid(void);
/** @brief Register an event handler for Date time library events.
*
* @warning The library only allows for one event handler to be registered
* at a time. A passed in event handler in this function will
* overwrite the previously set event handler.
*
* @param evt_handler Event handler. Handler is de-registered if parameter is
* NULL.
*/
void date_time_register_handler(date_time_evt_handler_t evt_handler);
/** @brief Asynchronous update of internal date time UTC. This function
* initiates a date time update regardless of the internal update
* interval. If an event handler is provided it will be updated
* with library events, accordingly.
*
* @param evt_handler Event handler. If the passed in pointer is NULL the
* previous registered event handler is not de-registered.
* This means that library events will still be received in
* the previously registered event handler.
*
* @return 0 If the operation was successful.
*/
int date_time_update_async(date_time_evt_handler_t evt_handler);
/** @brief Clear the current date time held by the library.
*
* @return 0 If the operation was successful.
*/
int date_time_clear(void);
/** @brief Clear a timestamp in unix time ms.
*
* @param[in, out] unix_timestamp Pointer to a unix timestamp.
*
* @return 0 If the operation was successful.
* @return -EINVAL If the passed in pointer is NULL.
*/
int date_time_timestamp_clear(int64_t *unix_timestamp);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* DATE_TIME_H__ */