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 * Mutexes
32 *------------------------------------------------*/
33
34// Opaque handle for mutex
35typedef void *golioth_sys_mutex_t;
36
38bool golioth_sys_mutex_lock(golioth_sys_mutex_t mutex, int32_t ms_to_wait);
41
42/*--------------------------------------------------
43 * Semaphores
44 *------------------------------------------------*/
45
46// Opaque handle for semaphore
47typedef void *golioth_sys_sem_t;
48
49golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count);
50bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait);
54
55/*--------------------------------------------------
56 * Software Timers
57 *------------------------------------------------*/
58
59// Opaque handle for timers
60typedef void *golioth_sys_timer_t;
61
62typedef void (*golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg);
63
71
76
77/*--------------------------------------------------
78 * Threads
79 *------------------------------------------------*/
80
81// Opaque handle for threads
83
84typedef void (*golioth_sys_thread_fn_t)(void *user_arg);
85
87{
88 const char *name;
90 void *user_arg;
91 int32_t stack_size; // in bytes
92 int32_t prio; // large numbers == high priority
93};
94
97
98/*--------------------------------------------------
99 * Malloc/Free
100 *------------------------------------------------*/
101
102// Can be overridden via golioth_{user,port}_config
103#ifndef golioth_sys_malloc
104#define golioth_sys_malloc(sz) malloc((sz))
105#endif
106
107#ifndef golioth_sys_free
108#define golioth_sys_free(ptr) free((ptr))
109#endif
110
111/*--------------------------------------------------
112 * Random
113 *------------------------------------------------*/
114
115// Can be overridden via golioth_{user,port}_config
116#ifndef golioth_sys_srand
117#define golioth_sys_srand(seed) srand((seed))
118#endif
119
120#ifndef golioth_sys_rand
121#define golioth_sys_rand() rand()
122#endif
123
124/*--------------------------------------------------
125 * Misc
126 *------------------------------------------------*/
127
128// Can be used to adjust behavior based on client connection status,
129// e.g. to turn logging backends on and off.
132
133/*--------------------------------------------------
134 * Logging
135 *------------------------------------------------*/
136
137#if defined(CONFIG_GOLIOTH_DEBUG_LOG)
138
139#include <stdio.h>
140#include "golioth_debug.h"
141
142#define LOG_COLOR_RED "31"
143#define LOG_COLOR_GREEN "32"
144#define LOG_COLOR_BROWN "33"
145#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
146#define LOG_RESET_COLOR "\033[0m"
147#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
148#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
149#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
150#define LOG_COLOR_D
151#define LOG_COLOR_V
152
153#ifndef GLTH_LOGX
154#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
155 do \
156 { \
157 if ((LEVEL) <= golioth_debug_get_log_level()) \
158 { \
159 uint64_t now_ms = golioth_sys_now_ms(); \
160 printf(COLOR "%s (%" PRIu64 ") %s: ", LEVEL_STR, now_ms, TAG); \
161 printf(__VA_ARGS__); \
162 golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
163 printf("%s", LOG_RESET_COLOR); \
164 puts(""); \
165 } \
166 } while (0)
167#endif
168
169// Logging macros can be overridden from golioth_{user,port}config.h
170#ifndef GLTH_LOGV
171#define GLTH_LOGV(TAG, ...) \
172 GLTH_LOGX(LOG_COLOR_V, GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE, "V", TAG, __VA_ARGS__)
173#endif
174
175#ifndef GLTH_LOGD
176#define GLTH_LOGD(TAG, ...) \
177 GLTH_LOGX(LOG_COLOR_D, GOLIOTH_DEBUG_LOG_LEVEL_DEBUG, "D", TAG, __VA_ARGS__)
178#endif
179
180#ifndef GLTH_LOGI
181#define GLTH_LOGI(TAG, ...) \
182 GLTH_LOGX(LOG_COLOR_I, GOLIOTH_DEBUG_LOG_LEVEL_INFO, "I", TAG, __VA_ARGS__)
183#endif
184
185#ifndef GLTH_LOGW
186#define GLTH_LOGW(TAG, ...) \
187 GLTH_LOGX(LOG_COLOR_W, GOLIOTH_DEBUG_LOG_LEVEL_WARN, "W", TAG, __VA_ARGS__)
188#endif
189
190#ifndef GLTH_LOGE
191#define GLTH_LOGE(TAG, ...) \
192 GLTH_LOGX(LOG_COLOR_E, GOLIOTH_DEBUG_LOG_LEVEL_ERROR, "E", TAG, __VA_ARGS__)
193#endif
194
195#ifndef GLTH_LOG_BUFFER_HEXDUMP
196#define GLTH_LOG_BUFFER_HEXDUMP(TAG, payload, size, level) \
197 do \
198 { \
199 if ((level) <= golioth_debug_get_log_level()) \
200 { \
201 golioth_debug_hexdump(TAG, payload, size); \
202 } \
203 } while (0);
204#endif
205
206#else /* CONFIG_GOLIOTH_DEBUG_LOG */
207
208#define GLTH_LOGV(TAG, ...)
209#define GLTH_LOGD(TAG, ...)
210#define GLTH_LOGI(TAG, ...)
211#define GLTH_LOGW(TAG, ...)
212#define GLTH_LOGE(TAG, ...)
213#define GLTH_LOG_BUFFER_HEXDUMP(TAG, ...)
214
215#endif /* CONFIG_GOLIOTH_DEBUG_LOG */
216
217#ifdef __cplusplus
218}
219#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:82
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_mutex_destroy(golioth_sys_mutex_t mutex)
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:84
void golioth_sys_msleep(uint32_t ms)
void * golioth_sys_sem_t
Definition golioth_sys.h:47
golioth_sys_mutex_t golioth_sys_mutex_create(void)
bool golioth_sys_mutex_lock(golioth_sys_mutex_t mutex, int32_t ms_to_wait)
void * golioth_sys_timer_t
Definition golioth_sys.h:60
void * golioth_sys_mutex_t
Definition golioth_sys.h:35
void(* golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg)
Definition golioth_sys.h:62
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)
bool golioth_sys_mutex_unlock(golioth_sys_mutex_t mutex)
golioth_sys_thread_fn_t fn
Definition golioth_sys.h:89
golioth_sys_timer_fn_t fn
Definition golioth_sys.h:68
const char * name
Definition golioth_sys.h:66