IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
time_service.h File Reference

Time service APIs for local time access and synchronization triggers. More...

#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "esp_err.h"
+ Include dependency graph for time_service.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

esp_err_t time_service_init (void)
 Initialize time service state and restore persisted time fallback.
 
esp_err_t time_service_set_epoch (int64_t epoch)
 Set current UTC epoch and persist it as valid time.
 
void time_service_notify_network_connected (bool connected)
 Notify the service about network connectivity changes.
 
bool time_service_get_local_time (struct tm *out_tm, int64_t *out_epoch)
 Get local time adjusted by the configured timezone offset.
 
void time_service_apply_timezone (const char *location_id, int16_t fallback_offset_min)
 Apply a timezone by IANA location ID with DST support.
 

Detailed Description

Time service APIs for local time access and synchronization triggers.

Definition in file time_service.h.

Function Documentation

◆ time_service_apply_timezone()

void time_service_apply_timezone ( const char *  location_id,
int16_t  fallback_offset_min 
)

Apply a timezone by IANA location ID with DST support.

Looks up a POSIX TZ string for the given IANA location ID and calls setenv("TZ", ...) + tzset() to activate it. For the "custom" ID or any unknown ID, generates a fixed-offset POSIX string from fallback_offset_min (no DST is applied in that case).

Must be called after any timezone configuration change and once during init.

Parameters
location_idIANA timezone location string, or "custom".
fallback_offset_minUTC offset in minutes east of UTC (used only for custom/unknown IDs).

Definition at line 207 of file time_service.c.

208{
209 const char *posix_str = NULL;
210 char generated[32];
211 size_t i;
212
213 if (location_id != NULL) {
214 for (i = 0; i < s_tz_table_count; ++i) {
215 if (strcmp(location_id, s_tz_table[i].location_id) == 0) {
216 posix_str = s_tz_table[i].posix_tz;
217 break;
218 }
219 }
220 }
221
222 if (posix_str == NULL) {
223 time_service_posix_from_offset(fallback_offset_min, generated, sizeof(generated));
224 posix_str = generated;
225 }
226
227 setenv("TZ", posix_str, 1);
228 tzset();
229
230 ESP_LOGI(TAG, "Timezone: location=\"%s\" posix=\"%s\"",
231 location_id ? location_id : "custom", posix_str);
232}
const char * posix_tz
static const size_t s_tz_table_count
static const char * TAG
static const time_service_tz_entry_t s_tz_table[]
static void time_service_posix_from_offset(int16_t offset_min, char *buf, size_t buf_len)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ time_service_get_local_time()

bool time_service_get_local_time ( struct tm *  out_tm,
int64_t *  out_epoch 
)

Get local time adjusted by the configured timezone offset.

Offset may be set directly or derived from a selected timezone location.

Parameters
[out]out_tmOptional pointer to receive broken-down local time.
[out]out_epochOptional pointer to receive local epoch seconds.
Returns
true if valid time is available, false otherwise.

Definition at line 315 of file time_service.c.

316{
317 bool persist_valid_epoch = false;
318 time_t utc_time = time(NULL);
319 struct tm local_tm;
320
321 if (utc_time <= 0) {
322 return false;
323 }
324
325 xSemaphoreTake(s_time_lock, portMAX_DELAY);
326 if (!s_time_valid) {
327 s_time_valid = true;
328 persist_valid_epoch = true;
329 }
330 xSemaphoreGive(s_time_lock);
331
332 if (persist_valid_epoch) {
333 app_state_set_time_epoch((int64_t)utc_time, true);
334 }
335
336 /* localtime_r respects the TZ env var set by time_service_apply_timezone(),
337 * giving correct DST-aware broken-down time. */
338 memset(&local_tm, 0, sizeof(local_tm));
339 localtime_r(&utc_time, &local_tm);
340
341 if (out_tm != NULL) {
342 *out_tm = local_tm;
343 }
344
345 if (out_epoch != NULL) {
346 /* Return UTC epoch. Callers that need display-local time use out_tm. */
347 *out_epoch = (int64_t)utc_time;
348 }
349
350 return true;
351}
esp_err_t app_state_set_time_epoch(int64_t epoch, bool valid)
Update stored epoch and validity flag.
Definition app_state.c:242
static SemaphoreHandle_t s_time_lock
static bool s_time_valid
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ time_service_init()

esp_err_t time_service_init ( void  )

Initialize time service state and restore persisted time fallback.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 234 of file time_service.c.

235{
236 int64_t saved_epoch = 0;
237 bool saved_valid = false;
238
239 if (s_time_lock == NULL) {
240 s_time_lock = xSemaphoreCreateMutex();
241 if (s_time_lock == NULL) {
242 return ESP_ERR_NO_MEM;
243 }
244 }
245
246 app_state_get_time_epoch(&saved_epoch, &saved_valid);
247
248 xSemaphoreTake(s_time_lock, portMAX_DELAY);
249 s_network_connected = false;
250 s_sntp_initialized = false;
252 s_time_valid = saved_valid && (saved_epoch > 0);
253 xSemaphoreGive(s_time_lock);
254
255 if (s_time_valid) {
256 ESP_RETURN_ON_ERROR(time_service_set_system_epoch(saved_epoch), TAG,
257 "restore saved epoch failed");
258 }
259
260 /* Apply persisted timezone so localtime_r() is DST-aware from boot. */
261 {
262 app_config_t cfg;
265 }
266
267 return ESP_OK;
268}
void app_state_get_time_epoch(int64_t *epoch, bool *valid)
Get stored epoch value and validity flag.
Definition app_state.c:234
void app_state_get_config(app_config_t *config)
Get current persisted configuration.
Definition app_state.c:214
Persisted configuration model.
Definition app_state.h:40
char timezone_location[APP_TIMEZONE_LOCATION_MAX_LEN+1]
Definition app_state.h:48
int16_t tz_offset_min
Definition app_state.h:49
void time_service_apply_timezone(const char *location_id, int16_t fallback_offset_min)
Apply a timezone by IANA location ID with DST support.
static bool s_network_connected
static bool s_sntp_initialized
static bool s_sntp_sync_task_running
static esp_err_t time_service_set_system_epoch(int64_t epoch)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ time_service_notify_network_connected()

void time_service_notify_network_connected ( bool  connected)

Notify the service about network connectivity changes.

Used to trigger SNTP synchronization when connectivity is available.

Parameters
connectedtrue if network is connected, false otherwise.

Definition at line 283 of file time_service.c.

284{
285 bool start_sync_task = false;
286
287 xSemaphoreTake(s_time_lock, portMAX_DELAY);
288 s_network_connected = connected;
289
290#if CONFIG_TIME_SERVICE_SNTP_ENABLE
291 if (connected && !s_sntp_sync_task_running) {
293 start_sync_task = true;
294 }
295#endif
296
297 xSemaphoreGive(s_time_lock);
298
299 if (start_sync_task) {
300 BaseType_t task_ok = xTaskCreate(time_service_sntp_sync_task,
301 "time_sntp_sync",
302 4096,
303 NULL,
304 4,
305 NULL);
306 if (task_ok != pdPASS) {
307 xSemaphoreTake(s_time_lock, portMAX_DELAY);
309 xSemaphoreGive(s_time_lock);
310 ESP_LOGE(TAG, "Failed to create SNTP sync task");
311 }
312 }
313}
static void time_service_sntp_sync_task(void *arg)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ time_service_set_epoch()

esp_err_t time_service_set_epoch ( int64_t  epoch)

Set current UTC epoch and persist it as valid time.

Parameters
epochUTC epoch seconds.
Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 270 of file time_service.c.

271{
272 ESP_RETURN_ON_FALSE(epoch > 0, ESP_ERR_INVALID_ARG, TAG, "epoch must be > 0");
273
274 ESP_RETURN_ON_ERROR(time_service_set_system_epoch(epoch), TAG, "settimeofday failed");
275
276 xSemaphoreTake(s_time_lock, portMAX_DELAY);
277 s_time_valid = true;
278 xSemaphoreGive(s_time_lock);
279
280 return app_state_set_time_epoch(epoch, true);
281}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: