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 #include <openthread/logging.h>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /**
47  * Represents exit codes used when OpenThread exits.
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      * Invalid state.
88      */
89     OT_EXIT_INVALID_STATE = 7,
90 
91 };
92 
93 /**
94  * Converts an exit code into a string.
95  *
96  * @param[in]  aExitCode  An exit code.
97  *
98  * @returns  A string representation of an exit code.
99  */
100 const char *otExitCodeToString(uint8_t aExitCode);
101 
102 /**
103  * Checks for the specified condition, which is expected to commonly be true,
104  * and both records exit status and terminates the program if the condition is false.
105  *
106  * @param[in]   aCondition  The condition to verify
107  * @param[in]   aExitCode   The exit code.
108  */
109 #define VerifyOrDie(aCondition, aExitCode)                                                         \
110     do                                                                                             \
111     {                                                                                              \
112         if (!(aCondition))                                                                         \
113         {                                                                                          \
114             const char *start = strrchr(__FILE__, '/');                                            \
115             OT_UNUSED_VARIABLE(start);                                                             \
116             otLogCritPlat("%s() at %s:%d: %s", __func__, (start ? start + 1 : __FILE__), __LINE__, \
117                           otExitCodeToString(aExitCode));                                          \
118             exit(aExitCode);                                                                       \
119         }                                                                                          \
120     } while (false)
121 
122 /**
123  * Checks for the specified error code, which is expected to commonly be successful,
124  * and both records exit status and terminates the program if the error code is unsuccessful.
125  *
126  * @param[in]  aError  An error code to be evaluated against OT_ERROR_NONE.
127  */
128 #define SuccessOrDie(aError)             \
129     VerifyOrDie(aError == OT_ERROR_NONE, \
130                 (aError == OT_ERROR_INVALID_ARGS ? OT_EXIT_INVALID_ARGUMENTS : OT_EXIT_FAILURE))
131 
132 /**
133  * Unconditionally both records exit status and terminates the program.
134  *
135  * @param[in]   aExitCode   The exit code.
136  */
137 #define DieNow(aExitCode) VerifyOrDie(false, aExitCode)
138 
139 /**
140  * Unconditionally both records exit status and exit message and terminates the program.
141  *
142  * @param[in]   aMessage    The exit message.
143  * @param[in]   aExitCode   The exit code.
144  */
145 #define DieNowWithMessage(aMessage, aExitCode)                                                 \
146     do                                                                                         \
147     {                                                                                          \
148         otLogCritPlat("exit(%d): %s line %d, %s, %s", aExitCode, __func__, __LINE__, aMessage, \
149                       otExitCodeToString(aExitCode));                                          \
150         exit(aExitCode);                                                                       \
151     } while (false)
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif // PLATFORM_EXIT_CODE_H_
158