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 * Hash
126 *------------------------------------------------*/
127
128/* Hash functions are only needed when the OTA component is enabled. */
129
132
141
149
163 const uint8_t *input,
164 size_t len);
165
174
184size_t golioth_sys_hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
185
186/*--------------------------------------------------
187 * Misc
188 *------------------------------------------------*/
189
190// Can be used to adjust behavior based on client connection status,
191// e.g. to turn logging backends on and off.
194
195/*--------------------------------------------------
196 * Logging
197 *------------------------------------------------*/
198
199#if defined(CONFIG_GOLIOTH_DEBUG_LOG)
200
201#include <stdio.h>
202#include "golioth_debug.h"
203
204#ifndef LOG_COLOR_RED
205#define LOG_COLOR_RED "31"
206#endif
207
208#ifndef LOG_COLOR_GREEN
209#define LOG_COLOR_GREEN "32"
210#endif
211
212#ifndef LOG_COLOR_BROWN
213#define LOG_COLOR_BROWN "33"
214#endif
215
216#ifndef LOG_COLOR
217#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
218#endif
219
220#ifndef LOG_RESET_COLOR
221#define LOG_RESET_COLOR "\033[0m"
222#endif
223
224#ifndef LOG_COLOR_E
225#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
226#endif
227
228#ifndef LOG_COLOR_W
229#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
230#endif
231
232#ifndef LOG_COLOR_I
233#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
234#endif
235
236#ifndef LOG_COLOR_D
237#define LOG_COLOR_D
238#endif
239
240#ifndef LOG_COLOR_V
241#define LOG_COLOR_V
242#endif
243
244#ifndef GLTH_LOGX
245#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
246 do \
247 { \
248 if ((LEVEL) <= golioth_debug_get_log_level()) \
249 { \
250 uint64_t now_ms = golioth_sys_now_ms(); \
251 printf(COLOR "%s (%" PRIu64 ") %s: ", LEVEL_STR, now_ms, TAG); \
252 printf(__VA_ARGS__); \
253 golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
254 printf("%s", LOG_RESET_COLOR); \
255 puts(""); \
256 } \
257 } while (0)
258#endif
259
260// Logging macros can be overridden from golioth_{user,port}config.h
261#ifndef GLTH_LOGV
262#define GLTH_LOGV(TAG, ...) \
263 GLTH_LOGX(LOG_COLOR_V, GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE, "V", TAG, __VA_ARGS__)
264#endif
265
266#ifndef GLTH_LOGD
267#define GLTH_LOGD(TAG, ...) \
268 GLTH_LOGX(LOG_COLOR_D, GOLIOTH_DEBUG_LOG_LEVEL_DEBUG, "D", TAG, __VA_ARGS__)
269#endif
270
271#ifndef GLTH_LOGI
272#define GLTH_LOGI(TAG, ...) \
273 GLTH_LOGX(LOG_COLOR_I, GOLIOTH_DEBUG_LOG_LEVEL_INFO, "I", TAG, __VA_ARGS__)
274#endif
275
276#ifndef GLTH_LOGW
277#define GLTH_LOGW(TAG, ...) \
278 GLTH_LOGX(LOG_COLOR_W, GOLIOTH_DEBUG_LOG_LEVEL_WARN, "W", TAG, __VA_ARGS__)
279#endif
280
281#ifndef GLTH_LOGE
282#define GLTH_LOGE(TAG, ...) \
283 GLTH_LOGX(LOG_COLOR_E, GOLIOTH_DEBUG_LOG_LEVEL_ERROR, "E", TAG, __VA_ARGS__)
284#endif
285
286#ifndef GLTH_LOG_BUFFER_HEXDUMP
287#define GLTH_LOG_BUFFER_HEXDUMP(TAG, payload, size, level) \
288 do \
289 { \
290 if ((level) <= golioth_debug_get_log_level()) \
291 { \
292 golioth_debug_hexdump(TAG, payload, size); \
293 } \
294 } while (0);
295#endif
296
297#else /* CONFIG_GOLIOTH_DEBUG_LOG */
298
299#define GLTH_LOGV(TAG, ...)
300#define GLTH_LOGD(TAG, ...)
301#define GLTH_LOGI(TAG, ...)
302#define GLTH_LOGW(TAG, ...)
303#define GLTH_LOGE(TAG, ...)
304#define GLTH_LOG_BUFFER_HEXDUMP(TAG, ...)
305
306#endif /* CONFIG_GOLIOTH_DEBUG_LOG */
307
308#ifdef __cplusplus
309}
310#endif
golioth_status
golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count)
enum golioth_status golioth_sys_sha256_finish(golioth_sys_sha256_t sha_ctx, uint8_t *output)
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)
void * golioth_sys_sha256_t
Opaque handle for sha256 context.
bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait)
enum golioth_status golioth_sys_sha256_update(golioth_sys_sha256_t sha_ctx, const uint8_t *input, size_t len)
void golioth_sys_mutex_destroy(golioth_sys_mutex_t mutex)
void golioth_sys_client_connected(void *client)
size_t golioth_sys_hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
void golioth_sys_sha256_destroy(golioth_sys_sha256_t sha_ctx)
void golioth_sys_timer_destroy(golioth_sys_timer_t timer)
golioth_sys_sha256_t golioth_sys_sha256_create(void)
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