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#pragma once
7
8#include <stdint.h>
9#include <inttypes.h>
10#include <stdbool.h>
11#include <stdlib.h>
12#include <golioth/config.h>
13
14/*--------------------------------------------------
15 * Time
16 *------------------------------------------------*/
17
18// For functions that take a wait/timeout parameter, -1 will wait forever
19#define GOLIOTH_SYS_WAIT_FOREVER -1
20
21void golioth_sys_msleep(uint32_t ms);
22uint64_t golioth_sys_now_ms(void);
23
24/*--------------------------------------------------
25 * Semaphores
26 *------------------------------------------------*/
27
28// Opaque handle for semaphore
29typedef void *golioth_sys_sem_t;
30
31golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count);
32bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait);
36
37/*--------------------------------------------------
38 * Software Timers
39 *------------------------------------------------*/
40
41// Opaque handle for timers
42typedef void *golioth_sys_timer_t;
43
44typedef void (*golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg);
45
53
58
59/*--------------------------------------------------
60 * Threads
61 *------------------------------------------------*/
62
63// Opaque handle for threads
65
66typedef void (*golioth_sys_thread_fn_t)(void *user_arg);
67
69{
70 const char *name;
72 void *user_arg;
73 int32_t stack_size; // in bytes
74 int32_t prio; // large numbers == high priority
75};
76
79
80/*--------------------------------------------------
81 * Malloc/Free
82 *------------------------------------------------*/
83
84// Can be overridden via golioth_{user,port}_config
85#ifndef golioth_sys_malloc
86#define golioth_sys_malloc(sz) malloc((sz))
87#endif
88
89#ifndef golioth_sys_free
90#define golioth_sys_free(ptr) free((ptr))
91#endif
92
93/*--------------------------------------------------
94 * Random
95 *------------------------------------------------*/
96
97// Can be overridden via golioth_{user,port}_config
98#ifndef golioth_sys_srand
99#define golioth_sys_srand(seed) srand((seed))
100#endif
101
102#ifndef golioth_sys_rand
103#define golioth_sys_rand() rand()
104#endif
105
106/*--------------------------------------------------
107 * Misc
108 *------------------------------------------------*/
109
110// Can be used to adjust behavior based on client connection status,
111// e.g. to turn logging backends on and off.
114
115/*--------------------------------------------------
116 * Logging
117 *------------------------------------------------*/
118
119#if defined(CONFIG_GOLIOTH_DEBUG_LOG)
120
121#include <stdio.h>
122#include "golioth_debug.h"
123
124#define LOG_COLOR_RED "31"
125#define LOG_COLOR_GREEN "32"
126#define LOG_COLOR_BROWN "33"
127#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
128#define LOG_RESET_COLOR "\033[0m"
129#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
130#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
131#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
132#define LOG_COLOR_D
133#define LOG_COLOR_V
134
135#ifndef GLTH_LOGX
136#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
137 do \
138 { \
139 if ((LEVEL) <= golioth_debug_get_log_level()) \
140 { \
141 uint64_t now_ms = golioth_sys_now_ms(); \
142 printf(COLOR "%s (%" PRIu64 ") %s: ", LEVEL_STR, now_ms, TAG); \
143 printf(__VA_ARGS__); \
144 golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
145 printf("%s", LOG_RESET_COLOR); \
146 puts(""); \
147 } \
148 } while (0)
149#endif
150
151// Logging macros can be overridden from golioth_{user,port}config.h
152#ifndef GLTH_LOGV
153#define GLTH_LOGV(TAG, ...) \
154 GLTH_LOGX(LOG_COLOR_V, GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE, "V", TAG, __VA_ARGS__)
155#endif
156
157#ifndef GLTH_LOGD
158#define GLTH_LOGD(TAG, ...) \
159 GLTH_LOGX(LOG_COLOR_D, GOLIOTH_DEBUG_LOG_LEVEL_DEBUG, "D", TAG, __VA_ARGS__)
160#endif
161
162#ifndef GLTH_LOGI
163#define GLTH_LOGI(TAG, ...) \
164 GLTH_LOGX(LOG_COLOR_I, GOLIOTH_DEBUG_LOG_LEVEL_INFO, "I", TAG, __VA_ARGS__)
165#endif
166
167#ifndef GLTH_LOGW
168#define GLTH_LOGW(TAG, ...) \
169 GLTH_LOGX(LOG_COLOR_W, GOLIOTH_DEBUG_LOG_LEVEL_WARN, "W", TAG, __VA_ARGS__)
170#endif
171
172#ifndef GLTH_LOGE
173#define GLTH_LOGE(TAG, ...) \
174 GLTH_LOGX(LOG_COLOR_E, GOLIOTH_DEBUG_LOG_LEVEL_ERROR, "E", TAG, __VA_ARGS__)
175#endif
176
177#ifndef GLTH_LOG_BUFFER_HEXDUMP
178#define GLTH_LOG_BUFFER_HEXDUMP(TAG, payload, size, level) \
179 do \
180 { \
181 if ((level) <= golioth_debug_get_log_level()) \
182 { \
183 golioth_debug_hexdump(TAG, payload, size); \
184 } \
185 } while (0);
186#endif
187
188#else /* CONFIG_GOLIOTH_DEBUG_LOG */
189
190#define GLTH_LOGV(TAG, ...)
191#define GLTH_LOGD(TAG, ...)
192#define GLTH_LOGI(TAG, ...)
193#define GLTH_LOGW(TAG, ...)
194#define GLTH_LOGE(TAG, ...)
195#define GLTH_LOG_BUFFER_HEXDUMP(TAG, ...)
196
197#endif /* CONFIG_GOLIOTH_DEBUG_LOG */
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:64
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:66
void golioth_sys_msleep(uint32_t ms)
void * golioth_sys_sem_t
Definition golioth_sys.h:29
void * golioth_sys_timer_t
Definition golioth_sys.h:42
void(* golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg)
Definition golioth_sys.h:44
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:71
golioth_sys_timer_fn_t fn
Definition golioth_sys.h:50
const char * name
Definition golioth_sys.h:48