Golioth Firmware SDK
golioth_sys.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7 #include "golioth_config.h"
8 
9 /*--------------------------------------------------
10  * Time
11  *------------------------------------------------*/
12 
13 // For functions that take a wait/timeout parameter, -1 will wait forever
14 #define GOLIOTH_SYS_WAIT_FOREVER -1
15 
16 void golioth_sys_msleep(uint32_t ms);
17 uint64_t golioth_sys_now_ms(void);
18 
19 /*--------------------------------------------------
20  * Semaphores
21  *------------------------------------------------*/
22 
23 // Opaque handle for semaphore
24 typedef void* golioth_sys_sem_t;
25 
26 golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count);
27 bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait);
30 
31 /*--------------------------------------------------
32  * Software Timers
33  *------------------------------------------------*/
34 
35 // Opaque handle for timers
36 typedef void* golioth_sys_timer_t;
37 
38 typedef void (*golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void* user_arg);
39 
40 typedef struct {
41  const char* name;
42  uint32_t expiration_ms;
44  void* user_arg;
46 
51 
52 /*--------------------------------------------------
53  * Threads
54  *------------------------------------------------*/
55 
56 // Opaque handle for threads
57 typedef void* golioth_sys_thread_t;
58 
59 typedef void (*golioth_sys_thread_fn_t)(void* user_arg);
60 
61 typedef struct {
62  const char* name;
64  void* user_arg;
65  int32_t stack_size; // in bytes
66  int32_t prio; // large numbers == high priority
68 
71 
72 /*--------------------------------------------------
73  * Malloc/Free
74  *------------------------------------------------*/
75 
76 // Can be overridden via golioth_{user,port}_config
77 #ifndef golioth_sys_malloc
78 #define golioth_sys_malloc(sz) malloc((sz))
79 #endif
80 
81 #ifndef golioth_sys_free
82 #define golioth_sys_free(ptr) free((ptr))
83 #endif
84 
85 /*--------------------------------------------------
86  * Logging
87  *------------------------------------------------*/
88 
89 #if CONFIG_GOLIOTH_DEBUG_LOG_ENABLE
90 
91 #include <stdio.h>
92 #include "golioth_debug.h"
93 
94 #define LOG_COLOR_RED "31"
95 #define LOG_COLOR_GREEN "32"
96 #define LOG_COLOR_BROWN "33"
97 #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
98 #define LOG_RESET_COLOR "\033[0m"
99 #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
100 #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
101 #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
102 #define LOG_COLOR_D
103 #define LOG_COLOR_V
104 
105 #ifndef GLTH_LOGX
106 #define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
107  do { \
108  if ((LEVEL) <= golioth_debug_get_log_level()) { \
109  uint64_t now_ms = golioth_time_millis(); \
110  printf(COLOR "%s (%" PRIu64 ") %s: ", LEVEL_STR, now_ms, TAG); \
111  printf(__VA_ARGS__); \
112  golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
113  printf("%s", LOG_RESET_COLOR); \
114  puts(""); \
115  } \
116  } while (0)
117 #endif
118 
119 // Logging macros can be overridden from golioth_{user,port}config.h
120 #ifndef GLTH_LOGV
121 #define GLTH_LOGV(TAG, ...) \
122  GLTH_LOGX(LOG_COLOR_V, GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE, "V", TAG, __VA_ARGS__)
123 #endif
124 
125 #ifndef GLTH_LOGD
126 #define GLTH_LOGD(TAG, ...) \
127  GLTH_LOGX(LOG_COLOR_D, GOLIOTH_DEBUG_LOG_LEVEL_DEBUG, "D", TAG, __VA_ARGS__)
128 #endif
129 
130 #ifndef GLTH_LOGI
131 #define GLTH_LOGI(TAG, ...) \
132  GLTH_LOGX(LOG_COLOR_I, GOLIOTH_DEBUG_LOG_LEVEL_INFO, "I", TAG, __VA_ARGS__)
133 #endif
134 
135 #ifndef GLTH_LOGW
136 #define GLTH_LOGW(TAG, ...) \
137  GLTH_LOGX(LOG_COLOR_W, GOLIOTH_DEBUG_LOG_LEVEL_WARN, "W", TAG, __VA_ARGS__)
138 #endif
139 
140 #ifndef GLTH_LOGE
141 #define GLTH_LOGE(TAG, ...) \
142  GLTH_LOGX(LOG_COLOR_E, GOLIOTH_DEBUG_LOG_LEVEL_ERROR, "E", TAG, __VA_ARGS__)
143 #endif
144 
145 #ifndef GLTH_LOG_BUFFER_HEXDUMP
146 #define GLTH_LOG_BUFFER_HEXDUMP(TAG, payload, size, level) \
147  do { \
148  if ((level) <= golioth_debug_get_log_level()) { \
149  golioth_debug_hexdump(TAG, payload, size); \
150  } \
151  } while (0);
152 #endif
153 
154 #else /* CONFIG_GOLIOTH_DEBUG_LOG_ENABLE */
155 
156 #define GLTH_LOGV(TAG, ...)
157 #define GLTH_LOGD(TAG, ...)
158 #define GLTH_LOGI(TAG, ...)
159 #define GLTH_LOGW(TAG, ...)
160 #define GLTH_LOGE(TAG, ...)
161 #define GLTH_LOG_BUFFER_HEXDUMP(TAG, ...)
162 
163 #endif /* CONFIG_GOLIOTH_DEBUG_LOG_ENABLE */
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:57
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)
golioth_sys_timer_t golioth_sys_timer_create(golioth_sys_timer_config_t config)
void golioth_sys_timer_destroy(golioth_sys_timer_t timer)
void(* golioth_sys_thread_fn_t)(void *user_arg)
Definition: golioth_sys.h:59
void golioth_sys_msleep(uint32_t ms)
void * golioth_sys_sem_t
Definition: golioth_sys.h:24
void * golioth_sys_timer_t
Definition: golioth_sys.h:36
golioth_sys_thread_t golioth_sys_thread_create(golioth_sys_thread_config_t config)
void(* golioth_sys_timer_fn_t)(golioth_sys_timer_t timer, void *user_arg)
Definition: golioth_sys.h:38
void golioth_sys_thread_destroy(golioth_sys_thread_t thread)
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:63
golioth_sys_timer_fn_t fn
Definition: golioth_sys.h:43