1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file contains header for exit code utilities.
32  */
33 
34 #ifndef PLATFORM_EXIT_CODE_H_
35 #define PLATFORM_EXIT_CODE_H_
36 
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * This enumeration represents exit codes used when OpenThread exits.
47  *
48  */
49 enum
50 {
51     /**
52      * Success.
53      */
54     OT_EXIT_SUCCESS = 0,
55 
56     /**
57      * Generic failure.
58      */
59     OT_EXIT_FAILURE = 1,
60 
61     /**
62      * Invalid arguments.
63      */
64     OT_EXIT_INVALID_ARGUMENTS = 2,
65 
66     /**
67      * Incompatible radio spinel.
68      */
69     OT_EXIT_RADIO_SPINEL_INCOMPATIBLE = 3,
70 
71     /**
72      * Unexpected radio spinel reset.
73      */
74     OT_EXIT_RADIO_SPINEL_RESET = 4,
75 
76     /**
77      * System call or library function error.
78      */
79     OT_EXIT_ERROR_ERRNO = 5,
80 
81     /**
82      * No response from radio spinel.
83      */
84     OT_EXIT_RADIO_SPINEL_NO_RESPONSE = 6,
85 };
86 
87 /**
88  * This function converts an exit code into a string.
89  *
90  * @param[in]  aExitCode  An exit code.
91  *
92  * @returns  A string representation of an exit code.
93  *
94  */
95 const char *otExitCodeToString(uint8_t aExitCode);
96 
97 /**
98  * This macro checks for the specified condition, which is expected to commonly be true,
99  * and both records exit status and terminates the program if the condition is false.
100  *
101  * @param[in]   aCondition  The condition to verify
102  * @param[in]   aExitCode   The exit code.
103  *
104  */
105 #define VerifyOrDie(aCondition, aExitCode)                                                         \
106     do                                                                                             \
107     {                                                                                              \
108         if (!(aCondition))                                                                         \
109         {                                                                                          \
110             const char *start = strrchr(__FILE__, '/');                                            \
111             OT_UNUSED_VARIABLE(start);                                                             \
112             otLogCritPlat("%s() at %s:%d: %s", __func__, (start ? start + 1 : __FILE__), __LINE__, \
113                           otExitCodeToString(aExitCode));                                          \
114             exit(aExitCode);                                                                       \
115         }                                                                                          \
116     } while (false)
117 
118 /**
119  * This macro checks for the specified error code, which is expected to commonly be successful,
120  * and both records exit status and terminates the program if the error code is unsuccessful.
121  *
122  * @param[in]  aError  An error code to be evaluated against OT_ERROR_NONE.
123  *
124  */
125 #define SuccessOrDie(aError)             \
126     VerifyOrDie(aError == OT_ERROR_NONE, \
127                 (aError == OT_ERROR_INVALID_ARGS ? OT_EXIT_INVALID_ARGUMENTS : OT_EXIT_FAILURE))
128 
129 /**
130  * This macro unconditionally both records exit status and terminates the program.
131  *
132  * @param[in]   aExitCode   The exit code.
133  *
134  */
135 #define DieNow(aExitCode) VerifyOrDie(false, aExitCode)
136 
137 /**
138  * This macro unconditionally both records exit status and exit message and terminates the program.
139  *
140  * @param[in]   aMessage    The exit message.
141  * @param[in]   aExitCode   The exit code.
142  *
143  */
144 #define DieNowWithMessage(aMessage, aExitCode)                                                 \
145     do                                                                                         \
146     {                                                                                          \
147         otLogCritPlat("exit(%d): %s line %d, %s, %s", aExitCode, __func__, __LINE__, aMessage, \
148                       otExitCodeToString(aExitCode));                                          \
149         exit(aExitCode);                                                                       \
150     } while (false)
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif // PLATFORM_EXIT_CODE_H_
157