Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | #ifndef TUSB_VERIFY_H_ |
| 27 | #define TUSB_VERIFY_H_ |
| 28 | |
| 29 | #include <stdbool.h> |
| 30 | #include <stdint.h> |
| 31 | #include "tusb_option.h" |
| 32 | #include "tusb_compiler.h" |
| 33 | |
| 34 | /*------------------------------------------------------------------*/ |
| 35 | /* This file use an advanced macro technique to mimic the default parameter |
| 36 | * as C++ for the sake of code simplicity. Beware of a headache macro |
| 37 | * manipulation that you are told to stay away. |
| 38 | * |
| 39 | * This contains macros for both VERIFY and ASSERT: |
| 40 | * |
| 41 | * VERIFY: Used when there is an error condition which is not the |
| 42 | * fault of the MCU. For example, bounds checking on data |
| 43 | * sent to the micro over USB should use this function. |
| 44 | * Another example is checking for buffer overflows, where |
| 45 | * returning from the active function causes a NAK. |
| 46 | * |
| 47 | * ASSERT: Used for error conditions that are caused by MCU firmware |
| 48 | * bugs. This is used to discover bugs in the code more |
| 49 | * quickly. One example would be adding assertions in library |
| 50 | * function calls to confirm a function's (untainted) |
| 51 | * parameters are valid. |
| 52 | * |
| 53 | * The difference in behavior is that ASSERT triggers a breakpoint while |
| 54 | * verify does not. |
| 55 | * |
| 56 | * #define TU_VERIFY(cond) if(cond) return false; |
| 57 | * #define TU_VERIFY(cond,ret) if(cond) return ret; |
| 58 | * |
| 59 | * #define TU_VERIFY_HDLR(cond,handler) if(cond) {handler; return false;} |
| 60 | * #define TU_VERIFY_HDLR(cond,ret,handler) if(cond) {handler; return ret;} |
| 61 | * |
| 62 | * #define TU_ASSERT(cond) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return false;} |
| 63 | * #define TU_ASSERT(cond,ret) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return ret;} |
| 64 | * |
| 65 | *------------------------------------------------------------------*/ |
| 66 | |
| 67 | #ifdef __cplusplus |
| 68 | extern "C" { |
| 69 | #endif |
| 70 | |
| 71 | //--------------------------------------------------------------------+ |
| 72 | // TU_VERIFY Helper |
| 73 | //--------------------------------------------------------------------+ |
| 74 | |
| 75 | #if CFG_TUSB_DEBUG |
| 76 | #include <stdio.h> |
| 77 | #define _MESS_ERR(_err) tu_printf("%s %d: failed, error = %s\r\n", __func__, __LINE__, tusb_strerr[_err]) |
| 78 | #define _MESS_FAILED() tu_printf("%s %d: ASSERT FAILED\r\n", __func__, __LINE__) |
| 79 | #else |
| 80 | #define _MESS_ERR(_err) do {} while (0) |
| 81 | #define _MESS_FAILED() do {} while (0) |
| 82 | #endif |
| 83 | |
| 84 | // Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7 |
| 85 | #if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) |
| 86 | #define TU_BREAKPOINT() do \ |
| 87 | { \ |
| 88 | volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \ |
| 89 | if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ \ |
| 90 | } while(0) |
| 91 | |
| 92 | #elif defined(__riscv) |
| 93 | #define TU_BREAKPOINT() do { __asm("ebreak\n"); } while(0) |
| 94 | |
| 95 | #else |
| 96 | #define TU_BREAKPOINT() do {} while (0) |
| 97 | #endif |
| 98 | |
| 99 | /*------------------------------------------------------------------*/ |
| 100 | /* Macro Generator |
| 101 | *------------------------------------------------------------------*/ |
| 102 | |
| 103 | // Helper to implement optional parameter for TU_VERIFY Macro family |
| 104 | #define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 |
| 105 | #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 |
| 106 | |
| 107 | /*------------- Generator for TU_VERIFY and TU_VERIFY_HDLR -------------*/ |
| 108 | #define TU_VERIFY_DEFINE(_cond, _handler, _ret) do \ |
| 109 | { \ |
| 110 | if ( !(_cond) ) { _handler; return _ret; } \ |
| 111 | } while(0) |
| 112 | |
| 113 | /*------------------------------------------------------------------*/ |
| 114 | /* TU_VERIFY |
| 115 | * - TU_VERIFY_1ARGS : return false if failed |
| 116 | * - TU_VERIFY_2ARGS : return provided value if failed |
| 117 | *------------------------------------------------------------------*/ |
| 118 | #define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, , false) |
| 119 | #define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, , _ret) |
| 120 | |
| 121 | #define TU_VERIFY(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, UNUSED)(__VA_ARGS__) |
| 122 | |
| 123 | |
| 124 | /*------------------------------------------------------------------*/ |
| 125 | /* TU_VERIFY WITH HANDLER |
| 126 | * - TU_VERIFY_HDLR_2ARGS : execute handler, return false if failed |
| 127 | * - TU_VERIFY_HDLR_3ARGS : execute handler, return provided error if failed |
| 128 | *------------------------------------------------------------------*/ |
| 129 | #define TU_VERIFY_HDLR_2ARGS(_cond, _handler) TU_VERIFY_DEFINE(_cond, _handler, false) |
| 130 | #define TU_VERIFY_HDLR_3ARGS(_cond, _handler, _ret) TU_VERIFY_DEFINE(_cond, _handler, _ret) |
| 131 | |
| 132 | #define TU_VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__) |
| 133 | |
| 134 | /*------------------------------------------------------------------*/ |
| 135 | /* ASSERT |
| 136 | * basically TU_VERIFY with TU_BREAKPOINT() as handler |
| 137 | * - 1 arg : return false if failed |
| 138 | * - 2 arg : return error if failed |
| 139 | *------------------------------------------------------------------*/ |
| 140 | #define ASSERT_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), false) |
| 141 | #define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), _ret) |
| 142 | |
| 143 | #ifndef TU_ASSERT |
| 144 | #define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__) |
| 145 | #endif |
| 146 | |
| 147 | // TODO remove TU_ASSERT_ERR() later |
| 148 | |
| 149 | /*------------- Generator for TU_VERIFY_ERR and TU_VERIFY_ERR_HDLR -------------*/ |
| 150 | #define TU_VERIFY_ERR_DEF2(_error, _handler) do \ |
| 151 | { \ |
| 152 | uint32_t _err = (uint32_t)(_error); \ |
| 153 | if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; } \ |
| 154 | } while(0) |
| 155 | |
| 156 | #define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) do \ |
| 157 | { \ |
| 158 | uint32_t _err = (uint32_t)(_error); \ |
| 159 | if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; } \ |
| 160 | } while(0) |
| 161 | |
| 162 | /*------------------------------------------------------------------*/ |
| 163 | /* ASSERT Error |
| 164 | * basically TU_VERIFY Error with TU_BREAKPOINT() as handler |
| 165 | *------------------------------------------------------------------*/ |
| 166 | #define ASSERT_ERR_1ARGS(_error) TU_VERIFY_ERR_DEF2(_error, TU_BREAKPOINT()) |
| 167 | #define ASSERT_ERR_2ARGS(_error, _ret) TU_VERIFY_ERR_DEF3(_error, TU_BREAKPOINT(), _ret) |
| 168 | |
| 169 | #ifndef TU_ASSERT_ERR |
| 170 | #define TU_ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_ERR_2ARGS, ASSERT_ERR_1ARGS,UNUSED)(__VA_ARGS__) |
| 171 | #endif |
| 172 | |
| 173 | /*------------------------------------------------------------------*/ |
| 174 | /* ASSERT HDLR |
| 175 | *------------------------------------------------------------------*/ |
| 176 | |
| 177 | #ifdef __cplusplus |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #endif /* TUSB_VERIFY_H_ */ |