Skip to content

Commit 970e141

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 d6b3956 commit 970e141

11 files changed

Lines changed: 164 additions & 1 deletion

File tree

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
# Suppress certain warnings when building with MSVC
6062
if (WINDOWS_MONO)
@@ -120,6 +122,7 @@ install(FILES
120122
install(FILES
121123
include/osal.h
122124
include/osal_log.h
125+
include/osal_utils.h
123126
DESTINATION include
124127
)
125128

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
@@ -575,3 +575,8 @@ void os_timer_destroy (os_timer_t * timer)
575575
timer_delete (timer->timerid);
576576
free (timer);
577577
}
578+
579+
void os_exit (os_exit_t code)
580+
{
581+
exit (code);
582+
}

src/osal_utils.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
#define MICROSECONDS_PER_SECONDS 10000000u
26+
27+
typedef struct os_exit_later_ctx
28+
{
29+
const char * reason;
30+
os_exit_t code;
31+
} os_exit_later_ctx_t;
32+
33+
static void os_exit_later_callback (os_timer_t *timer, void * arg)
34+
{
35+
os_exit_later_ctx_t * ctx = arg;
36+
LOG_INFO (LOG_STATE_ON,
37+
"Exiting system due to: %s\n",
38+
ctx->reason);
39+
os_exit (ctx->code);
40+
}
41+
42+
void os_exit_later (os_exit_t code, const char * reason, uint32_t us)
43+
{
44+
os_exit_later_ctx_t * ctx;
45+
os_timer_t * tmr;
46+
LOG_INFO (LOG_STATE_ON,
47+
"System will exit in %"PRIu32" seconds due to: %s\n",
48+
us / MICROSECONDS_PER_SECONDS, reason);
49+
50+
ctx = os_malloc (sizeof (os_exit_later_ctx_t));
51+
CC_ASSERT (ctx != NULL);
52+
53+
memset (ctx, 0, sizeof (os_exit_later_ctx_t));
54+
ctx->code = code;
55+
ctx->reason = reason;
56+
57+
tmr = os_timer_create (us, os_exit_later_callback, ctx, true);
58+
CC_ASSERT (tmr != NULL);
59+
60+
os_timer_start (tmr);
61+
}

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)