|
| 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 | +} |
0 commit comments