Skip to content

Commit ff73d83

Browse files
committed
feat: add support for os_exit
Add feature to allow system to exit on failures as well as exit of a predefined delay.
1 parent 7b9c642 commit ff73d83

File tree

11 files changed

+158
-1
lines changed

11 files changed

+158
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ configure_file (
5454

5555
# Add platform-dependent targets early, so they can be configured by
5656
# platform
57-
add_library(osal "")
57+
add_library(osal
58+
"src/osal_utils.c"
59+
)
5860

5961
# Use position independent code if platform supports shared libraries
6062
get_property(SUPPORTS_SHARED GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
@@ -112,6 +114,7 @@ install(FILES
112114
install(FILES
113115
include/osal.h
114116
include/osal_log.h
117+
include/osal_utils.h
115118
DESTINATION include
116119
)
117120

cmake/Windows.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ target_compile_options(osal
2525
/WX
2626
/wd4100
2727
/wd4152
28+
/wd4127 # conditional expression is constant
2829
>
2930

3031
$<$<C_COMPILER_ID:GCC>:

include/osal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ typedef void os_timer_t;
8080
typedef void os_tick_t;
8181
#endif
8282

83+
#ifndef OS_EXIT
84+
typedef int os_exit_t;
85+
#endif
86+
8387
void * os_malloc (size_t size);
8488
void os_free (void * ptr);
8589

@@ -132,6 +136,8 @@ void os_timer_start (os_timer_t * timer);
132136
void os_timer_stop (os_timer_t * timer);
133137
void os_timer_destroy (os_timer_t * timer);
134138

139+
void os_exit (os_exit_t code);
140+
135141
#ifdef __cplusplus
136142
}
137143
#endif

include/osal_utils.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*********************************************************************
2+
* _ _ _
3+
* _ __ | |_ _ | | __ _ | |__ ___
4+
* | '__|| __|(_)| | / _` || '_ \ / __|
5+
* | | | |_ _ | || (_| || |_) |\__ \
6+
* |_| \__|(_)|_| \__,_||_.__/ |___/
7+
*
8+
* www.rt-labs.com
9+
* Copyright 2025 rt-labs AB, Sweden.
10+
*
11+
* This software is licensed under the terms of the BSD 3-clause
12+
* license. See the file LICENSE distributed with this software for
13+
* full license information.
14+
********************************************************************/
15+
16+
#ifndef OSAL_UTILS_H
17+
#define OSAL_UTILS_H
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#include "osal.h"
24+
25+
void os_exit_later (os_exit_t code, const char * reason, uint32_t us);
26+
27+
#ifdef __cplusplus
28+
}
29+
#endif
30+
31+
#endif /* OSAL_UTILS_H */

src/freertos/osal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,8 @@ void os_timer_destroy (os_timer_t * timer)
255255
CC_ASSERT (status == pdPASS);
256256
free (timer);
257257
}
258+
259+
void os_exit (os_exit_t code)
260+
{
261+
exit (code);
262+
}

src/linux/osal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,8 @@ void os_timer_destroy (os_timer_t * timer)
573573
timer_delete (timer->timerid);
574574
free (timer);
575575
}
576+
577+
void os_exit (os_exit_t code)
578+
{
579+
exit (code);
580+
}

src/osal_utils.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*********************************************************************
2+
* _ _ _
3+
* _ __ | |_ _ | | __ _ | |__ ___
4+
* | '__|| __|(_)| | / _` || '_ \ / __|
5+
* | | | |_ _ | || (_| || |_) |\__ \
6+
* |_| \__|(_)|_| \__,_||_.__/ |___/
7+
*
8+
* www.rt-labs.com
9+
* Copyright 2025 rt-labs AB, Sweden.
10+
*
11+
* This software is licensed under the terms of the BSD 3-clause
12+
* license. See the file LICENSE distributed with this software for
13+
* full license information.
14+
********************************************************************/
15+
16+
#include <string.h>
17+
#include <inttypes.h>
18+
#include "osal_utils.h"
19+
#include "osal_log.h"
20+
21+
#ifndef LOG_LEVEL
22+
#define LOG_LEVEL LOG_LEVEL_INFO
23+
#endif
24+
25+
typedef struct os_exit_later_ctx
26+
{
27+
const char * reason;
28+
os_exit_t code;
29+
} os_exit_later_ctx_t;
30+
31+
static void os_exit_later_callback (os_timer_t *timer, void * arg)
32+
{
33+
os_exit_later_ctx_t * ctx = arg;
34+
LOG_INFO (LOG_STATE_ON, "Exiting system due to: %s\n", ctx->reason);
35+
os_exit (ctx->code);
36+
}
37+
38+
void os_exit_later (os_exit_t code, const char * reason, uint32_t us)
39+
{
40+
os_exit_later_ctx_t * ctx;
41+
os_timer_t * tmr;
42+
LOG_INFO (LOG_STATE_ON, "System will exit in %"PRIu32" microseconds due to: %s\n", us, reason);
43+
44+
ctx = os_malloc (sizeof (os_exit_later_ctx_t));
45+
CC_ASSERT (ctx != NULL);
46+
47+
memset (ctx, 0, sizeof (os_exit_later_ctx_t));
48+
ctx->code = code;
49+
ctx->reason = reason;
50+
51+
tmr = os_timer_create (us, os_exit_later_callback, ctx, true);
52+
CC_ASSERT (tmr != NULL);
53+
54+
os_timer_start (tmr);
55+
}

src/rt-kernel/osal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,8 @@ void os_timer_destroy (os_timer_t * timer)
222222
{
223223
tmr_destroy (timer);
224224
}
225+
226+
void os_exit (os_exit_t code)
227+
{
228+
exit (code);
229+
}

src/windows/osal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,8 @@ void os_timer_destroy (os_timer_t * timer)
367367
{
368368
free (timer);
369369
}
370+
371+
void os_exit (os_exit_t code)
372+
{
373+
exit (code);
374+
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ target_sources(osal_test PRIVATE
3838

3939
# Unit tests
4040
test_osal.cpp
41+
test_osal_utils.cpp
4142

4243
# Testrunner
4344
osal_test.cpp

0 commit comments

Comments
 (0)