• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * RCP chip reset is not able to be done by OT
93      */
94     OT_EXIT_RCP_RESET_REQUIRED = 8,
95 };
96 
97 /**
98  * Converts an exit code into a string.
99  *
100  * @param[in]  aExitCode  An exit code.
101  *
102  * @returns  A string representation of an exit code.
103  */
104 const char *otExitCodeToString(uint8_t aExitCode);
105 
106 /**
107  * Checks for the specified condition, which is expected to commonly be true,
108  * and both records exit status and terminates the program if the condition is false.
109  *
110  * @param[in]   aCondition  The condition to verify
111  * @param[in]   aExitCode   The exit code.
112  */
113 #define VerifyOrDie(aCondition, aExitCode)                                                         \
114     do                                                                                             \
115     {                                                                                              \
116         if (!(aCondition))                                                                         \
117         {                                                                                          \
118             const char *start = strrchr(__FILE__, '/');                                            \
119             OT_UNUSED_VARIABLE(start);                                                             \
120             otLogCritPlat("%s() at %s:%d: %s", __func__, (start ? start + 1 : __FILE__), __LINE__, \
121                           otExitCodeToString(aExitCode));                                          \
122             exit(aExitCode);                                                                       \
123         }                                                                                          \
124     } while (false)
125 
126 /**
127  * Checks for the specified error code, which is expected to commonly be successful,
128  * and both records exit status and terminates the program if the error code is unsuccessful.
129  *
130  * @param[in]  aError  An error code to be evaluated against OT_ERROR_NONE.
131  */
132 #define SuccessOrDie(aError)             \
133     VerifyOrDie(aError == OT_ERROR_NONE, \
134                 (aError == OT_ERROR_INVALID_ARGS ? OT_EXIT_INVALID_ARGUMENTS : OT_EXIT_FAILURE))
135 
136 /**
137  * Unconditionally both records exit status and terminates the program.
138  *
139  * @param[in]   aExitCode   The exit code.
140  */
141 #define DieNow(aExitCode) VerifyOrDie(false, aExitCode)
142 
143 /**
144  * Unconditionally both records exit status and exit message and terminates the program.
145  *
146  * @param[in]   aMessage    The exit message.
147  * @param[in]   aExitCode   The exit code.
148  */
149 #define DieNowWithMessage(aMessage, aExitCode)                                                 \
150     do                                                                                         \
151     {                                                                                          \
152         otLogCritPlat("exit(%d): %s line %d, %s, %s", aExitCode, __func__, __LINE__, aMessage, \
153                       otExitCodeToString(aExitCode));                                          \
154         exit(aExitCode);                                                                       \
155     } while (false)
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif // PLATFORM_EXIT_CODE_H_
162