
Encryp_u
Dependencies: BLE_API_Encryp CyaSSL-Encryp eMPL_MPU6050 mbed
Fork of Encryptulator2 by
nRF51822/common/assertion.h@4:bfb662bab28e, 2017-05-10 (annotated)
- Committer:
- vbahl2
- Date:
- Wed May 10 18:23:44 2017 +0000
- Revision:
- 4:bfb662bab28e
- Parent:
- 1:fc2f9d636751
en
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:fc2f9d636751 | 1 | /**************************************************************************/ |
yihui | 1:fc2f9d636751 | 2 | /*! |
yihui | 1:fc2f9d636751 | 3 | @file assertion.h |
yihui | 1:fc2f9d636751 | 4 | @author hathach (tinyusb.org) |
yihui | 1:fc2f9d636751 | 5 | |
yihui | 1:fc2f9d636751 | 6 | @section LICENSE |
yihui | 1:fc2f9d636751 | 7 | |
yihui | 1:fc2f9d636751 | 8 | Software License Agreement (BSD License) |
yihui | 1:fc2f9d636751 | 9 | |
yihui | 1:fc2f9d636751 | 10 | Copyright (c) 2013, K. Townsend (microBuilder.eu) |
yihui | 1:fc2f9d636751 | 11 | All rights reserved. |
yihui | 1:fc2f9d636751 | 12 | |
yihui | 1:fc2f9d636751 | 13 | Redistribution and use in source and binary forms, with or without |
yihui | 1:fc2f9d636751 | 14 | modification, are permitted provided that the following conditions are met: |
yihui | 1:fc2f9d636751 | 15 | 1. Redistributions of source code must retain the above copyright |
yihui | 1:fc2f9d636751 | 16 | notice, this list of conditions and the following disclaimer. |
yihui | 1:fc2f9d636751 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
yihui | 1:fc2f9d636751 | 18 | notice, this list of conditions and the following disclaimer in the |
yihui | 1:fc2f9d636751 | 19 | documentation and/or other materials provided with the distribution. |
yihui | 1:fc2f9d636751 | 20 | 3. Neither the name of the copyright holders nor the |
yihui | 1:fc2f9d636751 | 21 | names of its contributors may be used to endorse or promote products |
yihui | 1:fc2f9d636751 | 22 | derived from this software without specific prior written permission. |
yihui | 1:fc2f9d636751 | 23 | |
yihui | 1:fc2f9d636751 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
yihui | 1:fc2f9d636751 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
yihui | 1:fc2f9d636751 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
yihui | 1:fc2f9d636751 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
yihui | 1:fc2f9d636751 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
yihui | 1:fc2f9d636751 | 29 | INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
yihui | 1:fc2f9d636751 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND |
yihui | 1:fc2f9d636751 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
yihui | 1:fc2f9d636751 | 32 | INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS |
yihui | 1:fc2f9d636751 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
yihui | 1:fc2f9d636751 | 34 | */ |
yihui | 1:fc2f9d636751 | 35 | /**************************************************************************/ |
yihui | 1:fc2f9d636751 | 36 | |
yihui | 1:fc2f9d636751 | 37 | /** \file |
yihui | 1:fc2f9d636751 | 38 | * \brief TBD |
yihui | 1:fc2f9d636751 | 39 | * |
yihui | 1:fc2f9d636751 | 40 | * \note TBD |
yihui | 1:fc2f9d636751 | 41 | */ |
yihui | 1:fc2f9d636751 | 42 | |
yihui | 1:fc2f9d636751 | 43 | /** \ingroup TBD |
yihui | 1:fc2f9d636751 | 44 | * \defgroup TBD |
yihui | 1:fc2f9d636751 | 45 | * \brief TBD |
yihui | 1:fc2f9d636751 | 46 | * |
yihui | 1:fc2f9d636751 | 47 | * @{ |
yihui | 1:fc2f9d636751 | 48 | */ |
yihui | 1:fc2f9d636751 | 49 | |
yihui | 1:fc2f9d636751 | 50 | #ifndef _ASSERTION_H_ |
yihui | 1:fc2f9d636751 | 51 | #define _ASSERTION_H_ |
yihui | 1:fc2f9d636751 | 52 | |
yihui | 1:fc2f9d636751 | 53 | #include "projectconfig.h" |
yihui | 1:fc2f9d636751 | 54 | |
yihui | 1:fc2f9d636751 | 55 | #ifdef __cplusplus |
yihui | 1:fc2f9d636751 | 56 | extern "C" |
yihui | 1:fc2f9d636751 | 57 | { |
yihui | 1:fc2f9d636751 | 58 | #endif |
yihui | 1:fc2f9d636751 | 59 | |
yihui | 1:fc2f9d636751 | 60 | static inline void debugger_breakpoint(void) ATTR_ALWAYS_INLINE; |
yihui | 1:fc2f9d636751 | 61 | static inline void debugger_breakpoint(void) |
yihui | 1:fc2f9d636751 | 62 | { |
yihui | 1:fc2f9d636751 | 63 | #ifndef _TEST_ |
yihui | 1:fc2f9d636751 | 64 | __asm("BKPT #0\n"); |
yihui | 1:fc2f9d636751 | 65 | #endif |
yihui | 1:fc2f9d636751 | 66 | } |
yihui | 1:fc2f9d636751 | 67 | |
yihui | 1:fc2f9d636751 | 68 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 69 | // Compile-time Assert |
yihui | 1:fc2f9d636751 | 70 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 71 | #if defined __COUNTER__ && __COUNTER__ != __COUNTER__ |
yihui | 1:fc2f9d636751 | 72 | #define _ASSERT_COUNTER __COUNTER__ |
yihui | 1:fc2f9d636751 | 73 | #else |
yihui | 1:fc2f9d636751 | 74 | #define _ASSERT_COUNTER __LINE__ |
yihui | 1:fc2f9d636751 | 75 | #endif |
yihui | 1:fc2f9d636751 | 76 | |
yihui | 1:fc2f9d636751 | 77 | #define ASSERT_STATIC(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) } |
yihui | 1:fc2f9d636751 | 78 | |
yihui | 1:fc2f9d636751 | 79 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 80 | // Assert Helper |
yihui | 1:fc2f9d636751 | 81 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 82 | //#ifndef _TEST_ |
yihui | 1:fc2f9d636751 | 83 | // #define ASSERT_MESSAGE(format, ...)\ |
yihui | 1:fc2f9d636751 | 84 | // _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 85 | //#else |
yihui | 1:fc2f9d636751 | 86 | // #define ASSERT_MESSAGE(format, ...)\ |
yihui | 1:fc2f9d636751 | 87 | // _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 88 | //#endif |
yihui | 1:fc2f9d636751 | 89 | |
yihui | 1:fc2f9d636751 | 90 | #if CFG_DEBUG == 3 |
yihui | 1:fc2f9d636751 | 91 | #define ASSERT_MESSAGE(format, ...) debugger_breakpoint() |
yihui | 1:fc2f9d636751 | 92 | #elif CFG_DEBUG == 2 |
yihui | 1:fc2f9d636751 | 93 | #define ASSERT_MESSAGE(format, ...) printf("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 94 | #else |
yihui | 1:fc2f9d636751 | 95 | #define ASSERT_MESSAGE(format, ...) |
yihui | 1:fc2f9d636751 | 96 | #endif |
yihui | 1:fc2f9d636751 | 97 | |
yihui | 1:fc2f9d636751 | 98 | #define ASSERT_ERROR_HANDLER(x, para) \ |
yihui | 1:fc2f9d636751 | 99 | return (x) |
yihui | 1:fc2f9d636751 | 100 | |
yihui | 1:fc2f9d636751 | 101 | #define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \ |
yihui | 1:fc2f9d636751 | 102 | do{\ |
yihui | 1:fc2f9d636751 | 103 | setup_statement;\ |
yihui | 1:fc2f9d636751 | 104 | if (!(condition)) {\ |
yihui | 1:fc2f9d636751 | 105 | ASSERT_MESSAGE(format, __VA_ARGS__);\ |
yihui | 1:fc2f9d636751 | 106 | error_handler(error, handler_para);\ |
yihui | 1:fc2f9d636751 | 107 | }\ |
yihui | 1:fc2f9d636751 | 108 | }while(0) |
yihui | 1:fc2f9d636751 | 109 | |
yihui | 1:fc2f9d636751 | 110 | #define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 111 | |
yihui | 1:fc2f9d636751 | 112 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 113 | // error_t Status Assert TODO use ASSERT_DEFINE |
yihui | 1:fc2f9d636751 | 114 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 115 | #define ASSERT_STATUS_MESSAGE(sts, message) \ |
yihui | 1:fc2f9d636751 | 116 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:fc2f9d636751 | 117 | ERROR_NONE == status, status, "%s: %s", ErrorStr[status], message) |
yihui | 1:fc2f9d636751 | 118 | |
yihui | 1:fc2f9d636751 | 119 | #define ASSERT_STATUS(sts) \ |
yihui | 1:fc2f9d636751 | 120 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:fc2f9d636751 | 121 | ERROR_NONE == status, status, "error = %d", status) |
yihui | 1:fc2f9d636751 | 122 | |
yihui | 1:fc2f9d636751 | 123 | #define ASSERT_STATUS_RET_VOID(sts) \ |
yihui | 1:fc2f9d636751 | 124 | ASSERT_DEFINE(error_t status = (error_t)(sts),\ |
yihui | 1:fc2f9d636751 | 125 | ERROR_NONE == status, (void) 0, "error = %d", status) |
yihui | 1:fc2f9d636751 | 126 | |
yihui | 1:fc2f9d636751 | 127 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 128 | // Logical Assert |
yihui | 1:fc2f9d636751 | 129 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 130 | #define ASSERT(...) ASSERT_TRUE(__VA_ARGS__) |
yihui | 1:fc2f9d636751 | 131 | #define ASSERT_TRUE(condition , error) ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false") |
yihui | 1:fc2f9d636751 | 132 | #define ASSERT_FALSE(condition , error) ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true") |
yihui | 1:fc2f9d636751 | 133 | |
yihui | 1:fc2f9d636751 | 134 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 135 | // Pointer Assert |
yihui | 1:fc2f9d636751 | 136 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 137 | #define ASSERT_PTR(...) ASSERT_PTR_NOT_NULL(__VA_ARGS__) |
yihui | 1:fc2f9d636751 | 138 | #define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL") |
yihui | 1:fc2f9d636751 | 139 | #define ASSERT_PTR_NULL(pointer, error) ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL") |
yihui | 1:fc2f9d636751 | 140 | |
yihui | 1:fc2f9d636751 | 141 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 142 | // Integral Assert |
yihui | 1:fc2f9d636751 | 143 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 144 | #define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \ |
yihui | 1:fc2f9d636751 | 145 | ASSERT_DEFINE(\ |
yihui | 1:fc2f9d636751 | 146 | uint32_t exp = (expected); uint32_t act = (actual),\ |
yihui | 1:fc2f9d636751 | 147 | exp==act,\ |
yihui | 1:fc2f9d636751 | 148 | error,\ |
yihui | 1:fc2f9d636751 | 149 | "expected " type_format ", actual " type_format, exp, act) |
yihui | 1:fc2f9d636751 | 150 | |
yihui | 1:fc2f9d636751 | 151 | #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ |
yihui | 1:fc2f9d636751 | 152 | ASSERT_DEFINE(\ |
yihui | 1:fc2f9d636751 | 153 | uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ |
yihui | 1:fc2f9d636751 | 154 | (low <= act) && (act <= up),\ |
yihui | 1:fc2f9d636751 | 155 | error,\ |
yihui | 1:fc2f9d636751 | 156 | "expected within " type_format " - " type_format ", actual " type_format, low, up, act) |
yihui | 1:fc2f9d636751 | 157 | |
yihui | 1:fc2f9d636751 | 158 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 159 | // Integer Assert |
yihui | 1:fc2f9d636751 | 160 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 161 | #define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__) |
yihui | 1:fc2f9d636751 | 162 | #define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 163 | #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 164 | |
yihui | 1:fc2f9d636751 | 165 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 166 | // Hex Assert |
yihui | 1:fc2f9d636751 | 167 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 168 | #define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__) |
yihui | 1:fc2f9d636751 | 169 | #define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 170 | #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) |
yihui | 1:fc2f9d636751 | 171 | |
yihui | 1:fc2f9d636751 | 172 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 173 | // Bin Assert |
yihui | 1:fc2f9d636751 | 174 | //--------------------------------------------------------------------+ |
yihui | 1:fc2f9d636751 | 175 | #define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d" |
yihui | 1:fc2f9d636751 | 176 | #define BIN8_PRINTF_CONVERT(byte) \ |
yihui | 1:fc2f9d636751 | 177 | ((byte) & 0x80 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 178 | ((byte) & 0x40 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 179 | ((byte) & 0x20 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 180 | ((byte) & 0x10 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 181 | ((byte) & 0x08 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 182 | ((byte) & 0x04 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 183 | ((byte) & 0x02 ? 1 : 0), \ |
yihui | 1:fc2f9d636751 | 184 | ((byte) & 0x01 ? 1 : 0) |
yihui | 1:fc2f9d636751 | 185 | |
yihui | 1:fc2f9d636751 | 186 | #define ASSERT_BIN8(...) ASSERT_BIN8_EQUAL(__VA_ARGS__) |
yihui | 1:fc2f9d636751 | 187 | #define ASSERT_BIN8_EQUAL(expected, actual, error)\ |
yihui | 1:fc2f9d636751 | 188 | ASSERT_DEFINE(\ |
yihui | 1:fc2f9d636751 | 189 | uint8_t exp = (expected); uint8_t act = (actual),\ |
yihui | 1:fc2f9d636751 | 190 | exp==act,\ |
yihui | 1:fc2f9d636751 | 191 | error,\ |
yihui | 1:fc2f9d636751 | 192 | "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) ) |
yihui | 1:fc2f9d636751 | 193 | |
yihui | 1:fc2f9d636751 | 194 | #ifdef __cplusplus |
yihui | 1:fc2f9d636751 | 195 | } |
yihui | 1:fc2f9d636751 | 196 | #endif |
yihui | 1:fc2f9d636751 | 197 | |
yihui | 1:fc2f9d636751 | 198 | #endif /* _ASSERTION_H_ */ |
yihui | 1:fc2f9d636751 | 199 | |
yihui | 1:fc2f9d636751 | 200 | /** @} */ |