Morse Micro IoT SDK  2.9.7
mmosal.h
1/*
2 * Copyright 2021-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "mmport.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
39typedef void (*mmosal_app_init_cb_t)(void);
40
54int mmosal_main(mmosal_app_init_cb_t app_init_cb);
55
60/*
61 * ---------------------------------------------------------------------------------------------
62 */
63
83void *mmosal_malloc_(size_t size);
84
98void *mmosal_malloc_dbg(size_t size, const char *name, unsigned line_number);
99
105void mmosal_free(void *p);
106
115void *mmosal_realloc(void *ptr, size_t size);
116
125void *mmosal_calloc(size_t nitems, size_t size);
126
127#ifndef MMOSAL_TRACK_ALLOCATIONS
137#define mmosal_malloc(size) mmosal_malloc_(size)
138#else
139/* Note: we use function name because it should be shorter than the full filename path. */
149#define mmosal_malloc(size) mmosal_malloc_dbg(size, __func__, __LINE__)
150#endif
151
156/*
157 * ---------------------------------------------------------------------------------------------
158 */
159
173typedef void (*mmosal_task_fn_t)(void *arg);
174
177{
188};
189
201struct mmosal_task *mmosal_task_create(mmosal_task_fn_t task_fn, void *argument,
202 enum mmosal_task_priority priority,
203 unsigned stack_size_u32, const char *name);
204
212void mmosal_task_delete(struct mmosal_task *task);
213
219struct mmosal_task *mmosal_task_get_active(void);
220
225
231void mmosal_task_sleep(uint32_t duration_ms);
232
240#define MMOSAL_TASK_ENTER_CRITICAL() \
241 do { \
242 MMPORT_MEM_SYNC(); \
243 mmosal_task_enter_critical(); \
244 } while (0)
245
251#define MMOSAL_TASK_EXIT_CRITICAL() \
252 do { \
253 MMPORT_MEM_SYNC(); \
254 mmosal_task_exit_critical(); \
255 } while (0)
256
267
276
284
292
298const char *mmosal_task_name(void);
299
304/*
305 * ---------------------------------------------------------------------------------------------
306 */
307
323struct mmosal_mutex *mmosal_mutex_create(const char *name);
324
330void mmosal_mutex_delete(struct mmosal_mutex *mutex);
331
340bool mmosal_mutex_get(struct mmosal_mutex *mutex, uint32_t timeout_ms);
341
349bool mmosal_mutex_release(struct mmosal_mutex *mutex);
350
354#define MMOSAL_MUTEX_GET_INF(_mutex) \
355 do { \
356 bool ok__ = mmosal_mutex_get((_mutex), UINT32_MAX); \
357 MMOSAL_ASSERT(ok__); \
358 } while (0)
359
363#define MMOSAL_MUTEX_RELEASE(_mutex) \
364 do { \
365 bool ok__ = mmosal_mutex_release(_mutex); \
366 MMOSAL_ASSERT(ok__); \
367 } while (0)
368
376bool mmosal_mutex_is_held_by_active_task(struct mmosal_mutex *mutex);
377
382/*
383 * ---------------------------------------------------------------------------------------------
384 */
385
404struct mmosal_sem *mmosal_sem_create(unsigned max_count, unsigned initial_count, const char *name);
405
411void mmosal_sem_delete(struct mmosal_sem *sem);
412
422bool mmosal_sem_give(struct mmosal_sem *sem);
423
433bool mmosal_sem_give_from_isr(struct mmosal_sem *sem);
434
443bool mmosal_sem_wait(struct mmosal_sem *sem, uint32_t timeout_ms);
444
452uint32_t mmosal_sem_get_count(struct mmosal_sem *sem);
453
458/*
459 * ---------------------------------------------------------------------------------------------
460 */
461
477struct mmosal_semb *mmosal_semb_create(const char *name);
478
484void mmosal_semb_delete(struct mmosal_semb *semb);
485
495bool mmosal_semb_give(struct mmosal_semb *semb);
496
506bool mmosal_semb_give_from_isr(struct mmosal_semb *semb);
507
516bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms);
517
522/*
523 * ---------------------------------------------------------------------------------------------
524 */
525
543struct mmosal_queue *mmosal_queue_create(size_t num_items, size_t item_size, const char *name);
544
550void mmosal_queue_delete(struct mmosal_queue *queue);
551
565bool mmosal_queue_pop(struct mmosal_queue *queue, void *item, uint32_t timeout_ms);
566
580bool mmosal_queue_push(struct mmosal_queue *queue, const void *item, uint32_t timeout_ms);
581
593bool mmosal_queue_pop_from_isr(struct mmosal_queue *queue, void *item);
594
606bool mmosal_queue_push_from_isr(struct mmosal_queue *queue, const void *item);
607
612/*
613 * ---------------------------------------------------------------------------------------------
614 */
615
629uint32_t mmosal_get_time_ms(void);
630
636uint32_t mmosal_get_time_ticks(void);
637
644
662static inline bool mmosal_time_lt(uint32_t a, uint32_t b)
663{
664 return ((int32_t)(a - b)) < 0;
665}
666
667
685static inline bool mmosal_time_le(uint32_t a, uint32_t b)
686{
687 return ((int32_t)(a - b)) <= 0;
688}
689
706static inline uint32_t mmosal_time_max(uint32_t a, uint32_t b)
707{
708 if (mmosal_time_lt(a, b))
709 {
710 return b;
711 }
712 else
713 {
714 return a;
715 }
716}
717
725static inline bool mmosal_time_has_passed(uint32_t t)
726{
728}
729
734/*
735 * ---------------------------------------------------------------------------------------------
736 */
737
753struct mmosal_timer;
754
760typedef void (*timer_callback_t)(struct mmosal_timer *timer);
761
778struct mmosal_timer *mmosal_timer_create(const char *name, uint32_t timer_period_ms,
779 bool auto_reload, void *arg, timer_callback_t callback);
780
781
787void mmosal_timer_delete(struct mmosal_timer *timer);
788
796bool mmosal_timer_start(struct mmosal_timer *timer);
797
805bool mmosal_timer_stop(struct mmosal_timer *timer);
806
807
818bool mmosal_timer_change_period(struct mmosal_timer *timer, uint32_t new_period);
819
827void *mmosal_timer_get_arg(struct mmosal_timer *timer);
828
836bool mmosal_is_timer_active(struct mmosal_timer *timer);
837
842/*
843 * ---------------------------------------------------------------------------------------------
844 */
845
854#ifndef MMOSAL_FILEID
856#define MMOSAL_FILEID 0
857#endif
858
863#ifndef MMOSAL_MAX_FAILURE_RECORDS
864#define MMOSAL_MAX_FAILURE_RECORDS 4
865#endif
866
869{
871 uint32_t pc;
873 uint32_t lr;
875 uint32_t fileid;
877 uint32_t line;
880 uint32_t platform_info[4];
881};
882
889void mmosal_log_failure_info(const struct mmosal_failure_info *info);
890
891#ifdef MMOSAL_NO_DEBUGLOG
893#define MMOSAL_LOG_FAILURE_INFO(...)
894#else
897#define MMOSAL_LOG_FAILURE_INFO(...) \
898 do { \
899 void *pc; \
900 struct mmosal_failure_info info = { \
901 .lr = (uint32_t)MMPORT_GET_LR(), \
902 .fileid = MMOSAL_FILEID, \
903 .line = __LINE__, \
904 .platform_info = { __VA_ARGS__ }, \
905 }; \
906 MMPORT_GET_PC(pc); \
907 info.pc = (uint32_t)pc; \
908 mmosal_log_failure_info(&info); \
909 } while (0)
910#endif
911
917void mmosal_impl_assert(void);
918
919#ifndef MMOSAL_NOASSERT
926#ifndef MMOSAL_ASSERT
927#define MMOSAL_ASSERT(expr) \
928 do { \
929 if (!(expr)) { \
930 MMOSAL_LOG_FAILURE_INFO(0); \
931 mmosal_impl_assert(); \
932 while (1) \
933 {} \
934 } \
935 } while (0)
936#endif
937
945#ifndef MMOSAL_ASSERT_LOG_DATA
946#define MMOSAL_ASSERT_LOG_DATA(expr, ...) \
947 do { \
948 if (!(expr)) { \
949 MMOSAL_LOG_FAILURE_INFO(__VA_ARGS__); \
950 mmosal_impl_assert(); \
951 while (1) \
952 {} \
953 } \
954 } while (0)
955#endif
956#else
958#define MMOSAL_ASSERT(expr) (void)(expr)
960#define MMOSAL_ASSERT_LOG_DATA(expre, ...) (void)(expr)
961#endif
962
963#ifdef ENABLE_MMOSAL_DEV_ASSERT
965#define MMOSAL_DEV_ASSERT(x) MMOSAL_ASSERT(x)
968#define MMOSAL_DEV_ASSERT_LOG_DATA(x, ...) MMOSAL_ASSERT_LOG_DATA(x, __VA_ARGS__)
969#else
971#define MMOSAL_DEV_ASSERT(x) ((void)(x))
974#define MMOSAL_DEV_ASSERT_LOG_DATA(x, ...) ((void)(x))
975#endif
976
977#if defined(ENABLE_MANUAL_FAILURE_LOG_PROCESSING) && ENABLE_MANUAL_FAILURE_LOG_PROCESSING
996bool mmosal_extract_failure_info(struct mmosal_failure_info *buf, uint32_t *failure_count);
997#endif
1015int mmosal_printf(const char *format, ...);
1016
1030static inline bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
1031{
1032 bool ret;
1033
1034 if (size == 0)
1035 {
1036 return true;
1037 }
1038
1039 strncpy(dst, src, size);
1040
1041 ret = dst[size-1] != '\0';
1042 dst[size-1] = '\0';
1043 return ret;
1044}
1045
1057#ifndef MMOSAL_DEPRECATED_API_ENABLED
1060#define MMOSAL_DEPRECATED_API_ENABLED (1)
1061#endif
1062
1063#if MMOSAL_DEPRECATED_API_ENABLED
1064
1073void mmosal_task_join(struct mmosal_task *task);
1074
1090bool mmosal_task_wait_for_notification(uint32_t timeout_ms);
1091
1104void mmosal_task_notify(struct mmosal_task *task);
1105
1118void mmosal_task_notify_from_isr(struct mmosal_task *task);
1119
1120#endif
1121
1124#ifdef __cplusplus
1125}
1126#endif
1127
void mmosal_log_failure_info(const struct mmosal_failure_info *info)
Log failure information in a way that it is preserved across reboots so that it can be available for ...
void mmosal_impl_assert(void)
Assertion handler implementation.
void mmosal_task_join(struct mmosal_task *task)
Block until the given task has terminated.
void mmosal_task_notify_from_isr(struct mmosal_task *task)
Notifies a waiting task (mmosal_task_wait_for_notification()) that it can continue.
void mmosal_task_notify(struct mmosal_task *task)
Notifies a waiting task (mmosal_task_wait_for_notification()) that it can continue.
bool mmosal_task_wait_for_notification(uint32_t timeout_ms)
Blocks the current task until a notification is received.
int mmosal_main(mmosal_app_init_cb_t app_init_cb)
OS main function.
void(* mmosal_app_init_cb_t)(void)
Application initialization callback (see mmosal_main for details).
Definition: mmosal.h:39
void * mmosal_calloc(size_t nitems, size_t size)
Equivalent of standard library calloc().
void * mmosal_malloc_dbg(size_t size, const char *name, unsigned line_number)
Allocate memory of the given size and return a pointer to it (malloc) – debug version.
void mmosal_free(void *p)
Free the given memory allocation.
void * mmosal_malloc_(size_t size)
Allocate memory of the given size and return a pointer to it (malloc).
void * mmosal_realloc(void *ptr, size_t size)
Equivalent of standard library realloc().
int mmosal_printf(const char *format,...)
OS abstracted version of printf used by morselib.
static bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
A safer version of strncpy.
Definition: mmosal.h:1030
struct mmosal_mutex * mmosal_mutex_create(const char *name)
Create a new mutex.
bool mmosal_mutex_is_held_by_active_task(struct mmosal_mutex *mutex)
Check whether the given mutex is held by the active thread.
bool mmosal_mutex_release(struct mmosal_mutex *mutex)
Release a mutex.
void mmosal_mutex_delete(struct mmosal_mutex *mutex)
Delete a mutex.
bool mmosal_mutex_get(struct mmosal_mutex *mutex, uint32_t timeout_ms)
Acquire a mutex.
bool mmosal_queue_pop_from_isr(struct mmosal_queue *queue, void *item)
Pop an item from the queue (from ISR context).
struct mmosal_queue * mmosal_queue_create(size_t num_items, size_t item_size, const char *name)
Create a new queue.
bool mmosal_queue_push(struct mmosal_queue *queue, const void *item, uint32_t timeout_ms)
Push an item into the queue.
void mmosal_queue_delete(struct mmosal_queue *queue)
Delete a queue.
bool mmosal_queue_push_from_isr(struct mmosal_queue *queue, const void *item)
Push an item into the queue (from ISR context).
bool mmosal_queue_pop(struct mmosal_queue *queue, void *item, uint32_t timeout_ms)
Pop an item from the queue.
bool mmosal_semb_give_from_isr(struct mmosal_semb *semb)
Give a binary semaphore (from ISR context).
bool mmosal_semb_wait(struct mmosal_semb *semb, uint32_t timeout_ms)
Wait for a counting semaphore.
struct mmosal_semb * mmosal_semb_create(const char *name)
Create a new binary semaphore.
bool mmosal_semb_give(struct mmosal_semb *semb)
Give a binary semaphore.
void mmosal_semb_delete(struct mmosal_semb *semb)
Delete the given binary semaphore.
bool mmosal_sem_give(struct mmosal_sem *sem)
Give a counting semaphore.
void mmosal_sem_delete(struct mmosal_sem *sem)
Delete the given counting semaphore.
bool mmosal_sem_give_from_isr(struct mmosal_sem *sem)
Give a counting semaphore (from ISR context).
struct mmosal_sem * mmosal_sem_create(unsigned max_count, unsigned initial_count, const char *name)
Create a new counting semaphore.
uint32_t mmosal_sem_get_count(struct mmosal_sem *sem)
Returns the current count of the semaphore.
bool mmosal_sem_wait(struct mmosal_sem *sem, uint32_t timeout_ms)
Wait for a counting semaphore.
struct mmosal_task * mmosal_task_create(mmosal_task_fn_t task_fn, void *argument, enum mmosal_task_priority priority, unsigned stack_size_u32, const char *name)
Create a new task.
void mmosal_task_yield(void)
Yield the active task.
struct mmosal_task * mmosal_task_get_active(void)
Get the handle of the active task.
void mmosal_task_exit_critical(void)
Exit critical section.
void mmosal_enable_interrupts(void)
Enable interrupts.
const char * mmosal_task_name(void)
Get the name of the running task.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
void(* mmosal_task_fn_t)(void *arg)
Type definition for task main functions.
Definition: mmosal.h:173
void mmosal_disable_interrupts(void)
Disable interrupts.
void mmosal_task_delete(struct mmosal_task *task)
Delete the given task.
void mmosal_task_enter_critical(void)
Enter critical section.
mmosal_task_priority
Enumeration of task priorities (ordered lowest to highest).
Definition: mmosal.h:177
@ MMOSAL_TASK_PRI_LOW
Low priority.
Definition: mmosal.h:183
@ MMOSAL_TASK_PRI_MIN
Minimum priority.
Definition: mmosal.h:181
@ MMOSAL_TASK_PRI_HIGH
High priority.
Definition: mmosal.h:187
@ MMOSAL_TASK_PRI_NORM
Normal priority.
Definition: mmosal.h:185
@ MMOSAL_TASK_PRI_IDLE
Idle task priority.
Definition: mmosal.h:179
bool mmosal_timer_start(struct mmosal_timer *timer)
Start a timer.
void mmosal_timer_delete(struct mmosal_timer *timer)
Delete a timer.
bool mmosal_timer_change_period(struct mmosal_timer *timer, uint32_t new_period)
Change timer period.
bool mmosal_is_timer_active(struct mmosal_timer *timer)
Queries the timer to determine if it running.
void * mmosal_timer_get_arg(struct mmosal_timer *timer)
Get the opaque argument associated with a given timer.
void(* timer_callback_t)(struct mmosal_timer *timer)
Function type definition for timer callbacks.
Definition: mmosal.h:760
bool mmosal_timer_stop(struct mmosal_timer *timer)
Stop a timer.
struct mmosal_timer * mmosal_timer_create(const char *name, uint32_t timer_period_ms, bool auto_reload, void *arg, timer_callback_t callback)
Create a new timer.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static uint32_t mmosal_time_max(uint32_t a, uint32_t b)
Given two times, return the one that is greatest (taking into account wrapping).
Definition: mmosal.h:706
uint32_t mmosal_ticks_per_second(void)
Get the number of ticks in a second.
static bool mmosal_time_le(uint32_t a, uint32_t b)
Check if time a is less than or equal to time b, taking into account wrapping.
Definition: mmosal.h:685
uint32_t mmosal_get_time_ticks(void)
Get the system time in ticks.
static bool mmosal_time_has_passed(uint32_t t)
Check if the given time has already passed.
Definition: mmosal.h:725
static bool mmosal_time_lt(uint32_t a, uint32_t b)
Check if time a is less than time b, taking into account wrapping.
Definition: mmosal.h:662
char buf[1408]
Statically allocated buffer for HTTP GET request, just under 1 packet size.
Definition: sslclient.c:177
Data structure used to store information about a failure that can be preserved across reset.
Definition: mmosal.h:869
uint32_t fileid
File identifier.
Definition: mmosal.h:875
uint32_t pc
Content of the PC register at assertion.
Definition: mmosal.h:871
uint32_t lr
Content of the LR register at assertion.
Definition: mmosal.h:873
uint32_t line
Source code line at which the assertion was triggered.
Definition: mmosal.h:877
uint32_t platform_info[4]
Arbitrary platform-specific failure info.
Definition: mmosal.h:880