Golioth Firmware SDK
Loading...
Searching...
No Matches
golioth_sys.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Golioth, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifdef __cplusplus
8extern "C"
9{
10#endif
11
12#pragma once
13
14#include <stdint.h>
15#include <inttypes.h>
16#include <stdbool.h>
17#include <stdlib.h>
18#include <golioth/config.h>
19
20/*--------------------------------------------------
21 * Time
22 *------------------------------------------------*/
23
24// For functions that take a wait/timeout parameter, -1 will wait forever
25#define GOLIOTH_SYS_WAIT_FOREVER -1
26
27void golioth_sys_msleep(uint32_t ms);
28uint64_t golioth_sys_now_ms(void);
29
30/*--------------------------------------------------
31 * Semaphores
32 *------------------------------------------------*/
33
34// Opaque handle for semaphore
35typedef void *golioth_sys_sem_t;
36
37golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count);
38bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait);
42
43/*--------------------------------------------------
44 * Software Timers
45 *------------------------------------------------*/
46
47// Opaque handle for timers
48typedef void *golioth_sys_timer_t;
49
50typedef void (*golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg);
51
59
64
65/*--------------------------------------------------
66 * Threads
67 *------------------------------------------------*/
68
69// Opaque handle for threads
71
72typedef void (*golioth_sys_thread_fn_t)(void *user_arg);
73
75{
76 const char *name;
78 void *user_arg;
79 int32_t stack_size; // in bytes
80 int32_t prio; // large numbers == high priority
81};
82
85
86/*--------------------------------------------------
87 * Malloc/Free
88 *------------------------------------------------*/
89
90// Can be overridden via golioth_{user,port}_config
91#ifndef golioth_sys_malloc
92#define golioth_sys_malloc(sz) malloc((sz))
93#endif
94
95#ifndef golioth_sys_free
96#define golioth_sys_free(ptr) free((ptr))
97#endif
98
99/*--------------------------------------------------
100 * Random
101 *------------------------------------------------*/
102
103// Can be overridden via golioth_{user,port}_config
104#ifndef golioth_sys_srand
105#define golioth_sys_srand(seed) srand((seed))
106#endif
107
108#ifndef golioth_sys_rand
109#define golioth_sys_rand() rand()
110#endif
111
112/*--------------------------------------------------
113 * Misc
114 *------------------------------------------------*/
115
116// Can be used to adjust behavior based on client connection status,
117// e.g. to turn logging backends on and off.
120
121/*--------------------------------------------------
122 * Logging
123 *------------------------------------------------*/
124
125#if defined(CONFIG_GOLIOTH_DEBUG_LOG)
126
127#include <stdio.h>
128#include "golioth_debug.h"
129
130#define LOG_COLOR_RED "31"
131#define LOG_COLOR_GREEN "32"
132#define LOG_COLOR_BROWN "33"
133#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
134#define LOG_RESET_COLOR "\033[0m"
135#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
136#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
137#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
138#define LOG_COLOR_D
139#define LOG_COLOR_V
140
141#ifndef GLTH_LOGX
142#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
143 do \
144 { \
145 if ((LEVEL) <= golioth_debug_get_log_level()) \
146 { \
147 uint64_t now_ms = golioth_sys_now_ms(); \
148 printf(COLOR "%s (%" PRIu64 ") %s: ", LEVEL_STR, now_ms, TAG); \
149 printf(__VA_ARGS__); \
150 golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
151 printf("%s", LOG_RESET_COLOR); \
152 puts(""); \
153 } \
154 } while (0)
155#endif
156
157// Logging macros can be overridden from golioth_{user,port}config.h
158#ifndef GLTH_LOGV
159#define GLTH_LOGV(TAG, ...) \
160 GLTH_LOGX(LOG_COLOR_V, GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE, "V", TAG, __VA_ARGS__)
161#endif
162
163#ifndef GLTH_LOGD
164#define GLTH_LOGD(TAG, ...) \
165 GLTH_LOGX(LOG_COLOR_D, GOLIOTH_DEBUG_LOG_LEVEL_DEBUG, "D", TAG, __VA_ARGS__)
166#endif
167
168#ifndef GLTH_LOGI
169#define GLTH_LOGI(TAG, ...) \
170 GLTH_LOGX(LOG_COLOR_I, GOLIOTH_DEBUG_LOG_LEVEL_INFO, "I", TAG, __VA_ARGS__)
171#endif
172
173#ifndef GLTH_LOGW
174#define GLTH_LOGW(TAG, ...) \
175 GLTH_LOGX(LOG_COLOR_W, GOLIOTH_DEBUG_LOG_LEVEL_WARN, "W", TAG, __VA_ARGS__)
176#endif
177
178#ifndef GLTH_LOGE
179#define GLTH_LOGE(TAG, ...) \
180 GLTH_LOGX(LOG_COLOR_E, GOLIOTH_DEBUG_LOG_LEVEL_ERROR, "E", TAG, __VA_ARGS__)
181#endif
182
183#ifndef GLTH_LOG_BUFFER_HEXDUMP
184#define GLTH_LOG_BUFFER_HEXDUMP(TAG, payload, size, level) \
185 do \
186 { \
187 if ((level) <= golioth_debug_get_log_level()) \
188 { \
189 golioth_debug_hexdump(TAG, payload, size); \
190 } \
191 } while (0);
192#endif
193
194#else /* CONFIG_GOLIOTH_DEBUG_LOG */
195
196#define GLTH_LOGV(TAG, ...)
197#define GLTH_LOGD(TAG, ...)
198#define GLTH_LOGI(TAG, ...)
199#define GLTH_LOGW(TAG, ...)
200#define GLTH_LOGE(TAG, ...)
201#define GLTH_LOG_BUFFER_HEXDUMP(TAG, ...)
202
203#endif /* CONFIG_GOLIOTH_DEBUG_LOG */
204
205#ifdef __cplusplus
206}
207#endif
golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count)
bool golioth_sys_sem_give(golioth_sys_sem_t sem)
bool golioth_sys_timer_reset(golioth_sys_timer_t timer)
void * golioth_sys_thread_t
Definition golioth_sys.h:70
bool golioth_sys_timer_start(golioth_sys_timer_t timer)
bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait)
void golioth_sys_client_connected(void *client)
void golioth_sys_timer_destroy(golioth_sys_timer_t timer)
golioth_sys_thread_t golioth_sys_thread_create(const struct golioth_thread_config *config)
void(* golioth_sys_thread_fn_t)(void *user_arg)
Definition golioth_sys.h:72
void golioth_sys_msleep(uint32_t ms)
void * golioth_sys_sem_t
Definition golioth_sys.h:35
void * golioth_sys_timer_t
Definition golioth_sys.h:48
void(* golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg)
Definition golioth_sys.h:50
void golioth_sys_client_disconnected(void *client)
void golioth_sys_thread_destroy(golioth_sys_thread_t thread)
int golioth_sys_sem_get_fd(golioth_sys_sem_t sem)
golioth_sys_timer_t golioth_sys_timer_create(const struct golioth_timer_config *config)
uint64_t golioth_sys_now_ms(void)
void golioth_sys_sem_destroy(golioth_sys_sem_t sem)
golioth_sys_thread_fn_t fn
Definition golioth_sys.h:77
golioth_sys_timer_fn_t fn
Definition golioth_sys.h:56
const char * name
Definition golioth_sys.h:54