clean up assert and verify

This commit is contained in:
hathach 2018-03-29 13:07:27 +07:00
parent ccf6f03817
commit c50da4962e
8 changed files with 75 additions and 78 deletions

View File

@ -199,8 +199,8 @@ tusb_error_t cdch_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_
for(uint32_t i=0; i<2; i++) for(uint32_t i=0; i<2; i++)
{ {
tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc;
ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED);
ASSERT_INT(TUSB_XFER_BULK, p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); TU_ASSERT(TUSB_XFER_BULK == p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED);
pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ?
&p_cdc->pipe_in : &p_cdc->pipe_out; &p_cdc->pipe_in : &p_cdc->pipe_out;

View File

@ -117,7 +117,7 @@ tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_
for(uint32_t i=0; i<2; i++) for(uint32_t i=0; i<2; i++)
{ {
tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc;
ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA); TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA);
pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ?
&custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out; &custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out;

View File

@ -264,12 +264,12 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface
//------------- HID descriptor -------------// //------------- HID descriptor -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); TU_ASSERT(HID_DESC_TYPE_HID == p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE);
//------------- Endpoint Descriptor -------------// //------------- Endpoint Descriptor -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
tusb_desc_endpoint_t const *p_desc_endpoint = (tusb_desc_endpoint_t const *) p_desc; tusb_desc_endpoint_t const *p_desc_endpoint = (tusb_desc_endpoint_t const *) p_desc;
ASSERT_INT(TUSB_DESC_ENDPOINT, p_desc_endpoint->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); TU_ASSERT(TUSB_DESC_ENDPOINT == p_desc_endpoint->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE);
if (p_interface_desc->bInterfaceSubClass == HID_SUBCLASS_BOOT) if (p_interface_desc->bInterfaceSubClass == HID_SUBCLASS_BOOT)
{ {

View File

@ -76,7 +76,7 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int
{ {
//------------- parameters validation -------------// //------------- parameters validation -------------//
// TODO change to use is configured function // TODO change to use is configured function
ASSERT_INT (TUSB_DEVICE_STATE_CONFIGURED, tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); TU_ASSERT (TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
VERIFY (report, TUSB_ERROR_INVALID_PARA); VERIFY (report, TUSB_ERROR_INVALID_PARA);
TU_ASSSERT (!hcd_pipe_is_busy(p_hid->pipe_hdl), TUSB_ERROR_INTERFACE_IS_BUSY); TU_ASSSERT (!hcd_pipe_is_busy(p_hid->pipe_hdl), TUSB_ERROR_INTERFACE_IS_BUSY);
@ -188,12 +188,12 @@ tusb_error_t hidh_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_
//------------- HID descriptor -------------// //------------- HID descriptor -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA); TU_ASSERT(HID_DESC_TYPE_HID == p_desc_hid->bDescriptorType, TUSB_ERROR_INVALID_PARA);
//------------- Endpoint Descriptor -------------// //------------- Endpoint Descriptor -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
tusb_desc_endpoint_t const * p_endpoint_desc = (tusb_desc_endpoint_t const *) p_desc; tusb_desc_endpoint_t const * p_endpoint_desc = (tusb_desc_endpoint_t const *) p_desc;
ASSERT_INT(TUSB_DESC_ENDPOINT, p_endpoint_desc->bDescriptorType, TUSB_ERROR_INVALID_PARA); TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint_desc->bDescriptorType, TUSB_ERROR_INVALID_PARA);
OSAL_SUBTASK_BEGIN OSAL_SUBTASK_BEGIN

View File

@ -89,20 +89,9 @@ extern "C"
ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\ ASSERT_DEFINE(tusb_error_t status = (tusb_error_t)(sts),\
TUSB_ERROR_NONE == status, status, "%s", tusb_strerr[status]) TUSB_ERROR_NONE == status, status, "%s", tusb_strerr[status])
//--------------------------------------------------------------------+
// Logical Assert
//--------------------------------------------------------------------+
#define ASSERT_FAILED(error) ASSERT_DEFINE( , false, error, "%s", "FAILED")
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Integral Assert // Integral Assert
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
ASSERT_DEFINE( uint32_t exp = (expected); uint32_t act = (actual),\
exp==act,\
error,\
"expected " type_format ", actual " type_format, exp, act)
#define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \ #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
ASSERT_DEFINE( uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\ ASSERT_DEFINE( uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
(low <= act) && (act <= up),\ (low <= act) && (act <= up),\
@ -112,15 +101,11 @@ extern "C"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Integer Assert // Integer Assert
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define ASSERT_INT(...) ASSERT_INT_EQUAL(__VA_ARGS__)
#define ASSERT_INT_EQUAL(...) ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
#define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__) #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Hex Assert // Hex Assert
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define ASSERT_HEX(...) ASSERT_HEX_EQUAL(__VA_ARGS__)
#define ASSERT_HEX_EQUAL(...) ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
#define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__) #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)

View File

@ -41,15 +41,21 @@
#include "tusb_option.h" #include "tusb_option.h"
#include "tusb_compiler.h" #include "tusb_compiler.h"
/*------------------------------------------------------------------*/
/* This file use an advanced macro technique to mimic the default parameter
* as C++ for the sake of code simplicity. Beware of a headache macro
* manipulation that you are told to stay away.
*
* e.g
*
* - VERIFY( cond ) will return false if cond is false
* - VERIFY( cond, err) will return err instead if cond is false
*------------------------------------------------------------------*/
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/**
* Helper to implement optional parameter for VERIFY Macro family
*/
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// VERIFY Helper // VERIFY Helper
@ -76,93 +82,99 @@ static inline void verify_breakpoint(void)
} }
#else #else
#define verify_breakpoint() #define verify_breakpoint()
#endif #endif
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* VERIFY STATUS /* Macro Generator
* - VERIFY_STS_1ARGS : return status of condition if failed
* - VERIFY_STS_2ARGS : return provided status code if failed
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
#define VERIFY_STS_1ARGS(sts) \
do { \
uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { _VERIFY_MESS(_status) return _status; } \
} while(0)
#define VERIFY_STS_2ARGS(sts, _error) \ // Helper to implement optional parameter for VERIFY Macro family
do { \ #define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
uint32_t _status = (uint32_t)(sts); \ #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
if ( 0 != _status ) { _VERIFY_MESS(_status) return _error; }\
} while(0)
/** /*------------- Generator for VERIFY and VERIFY_HDLR -------------*/
* Check if status is success (zero), otherwise return #define VERIFY_DEFINE(_cond, _handler, _error) do { if ( !(_cond) ) { _handler return _error; } } while(0)
* - status value if called with 1 parameter e.g VERIFY_STATUS(status)
* - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode)
*/
#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/ /*------------- Generator for VERIFY_STATUS and VERIFY_STATUS_HDLR -------------*/
/* VERIFY STATUS WITH HANDLER #define VERIFY_STS_DEF2(sts, _handler) \
* - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed
* - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_HDLR_2ARGS(sts, _handler) \
do { \ do { \
uint32_t _status = (uint32_t)(sts); \ uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _status; }\ if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _status; }\
} while(0) } while(0)
#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) \ #define VERIFY_STS_DEF3(sts, _handler, _error) \
do { \ do { \
uint32_t _status = (uint32_t)(sts); \ uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _error; }\ if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _error; }\
} while(0) } while(0)
#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* VERIFY /* VERIFY
* - VERIFY_1ARGS : return false if failed * - VERIFY_1ARGS : return false if failed
* - VERIFY_2ARGS : return provided value if failed * - VERIFY_2ARGS : return provided value if failed
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
#define VERIFY_1ARGS(cond) do { if (!(cond)) return false; } while(0) #define VERIFY_1ARGS(_cond) VERIFY_DEFINE(_cond, , false)
#define VERIFY_2ARGS(cond, _error) do { if (!(cond)) return _error; } while(0) #define VERIFY_2ARGS(_cond, _error) VERIFY_DEFINE(_cond, , _error)
#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
/**
* Check if condition is success (true), otherwise return
* - false value if called with 1 parameter e.g VERIFY(condition)
* - 2 parameter if called with 2 parameters e.g VERIFY(condition, errorcode)
*/
#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* VERIFY WITH HANDLER /* VERIFY WITH HANDLER
* - VERIFY_HDLR_2ARGS : execute handler, return false if failed * - VERIFY_HDLR_2ARGS : execute handler, return false if failed
* - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed * - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
#define VERIFY_HDLR_2ARGS(cond, _handler) \ #define VERIFY_HDLR_2ARGS(cond, _handler) VERIFY_DEFINE(cond, _handler; , false)
do { if ( !(cond) ) { _handler; return false; } } while(0) #define VERIFY_HDLR_3ARGS(cond, _handler, _error) VERIFY_DEFINE(cond, _handler; , _error)
#define VERIFY_HDLR_3ARGS(cond, _handler, _error) \ #define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
do { if ( !(cond) ) { _handler; return _error; } } while(0)
/*------------------------------------------------------------------*/
/* VERIFY STATUS
* - VERIFY_STS_1ARGS : return status of condition if failed
* - VERIFY_STS_2ARGS : return provided status code if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_1ARGS(sts) VERIFY_STS_DEF2(sts, )
#define VERIFY_STS_2ARGS(sts, _error) VERIFY_STS_DEF3(sts, ,_error)
#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY STATUS WITH HANDLER
* - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed
* - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_HDLR_2ARGS(sts, _handler) VERIFY_STS_DEF2(sts, _handler;)
#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) VERIFY_STS_DEF3(sts, _handler; , _error)
#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__)
#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* ASSERT /* ASSERT
* basically VERIFY with verify_breakpoint as handler * basically VERIFY with verify_breakpoint() as handler
* - 1 arg : return false if failed * - 1 arg : return false if failed
* - 2 arg : return error if failed * - 2 arg : return error if failed
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
#define ASSERT_1ARGS(cond) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return false; } } while(0) #define ASSERT_1ARGS(cond) VERIFY_DEFINE(cond, verify_breakpoint(); , false)
#define ASSERT_2ARGS(cond, _error) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return _error;} } while(0) #define ASSERT_2ARGS(cond, _error) VERIFY_DEFINE(cond, verify_breakpoint(); , _error)
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__) #define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* ASSERT Error
* basically VERIFY Error with verify_breakpoint() as handler
*------------------------------------------------------------------*/
#define ASERT_ERR_1ARGS(_err) VERIFY_STS_DEF2(_err, verify_breakpoint();)
#define ASERT_ERR_2ARGS(_err, _ret) VERIFY_STS_DEF3(_err, verify_breakpoint();, _ret)
#define ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__)
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -230,7 +230,7 @@ void hub_isr(pipe_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes)
else else
{ {
// TODO [HUB] check if hub is still plugged before polling status endpoint since failed usually mean hub unplugged // TODO [HUB] check if hub is still plugged before polling status endpoint since failed usually mean hub unplugged
// ASSERT_INT ( TUSB_ERROR_NONE, hcd_pipe_xfer(pipe_hdl, &p_hub->status_change, 1, true) ); // TU_ASSERT ( TUSB_ERROR_NONE == hcd_pipe_xfer(pipe_hdl, &p_hub->status_change, 1, true) );
} }
} }

View File

@ -259,7 +259,7 @@ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, tusb_event_t even
usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes); usbh_class_drivers[class_index].isr(pipe_hdl, event, xferred_bytes);
}else }else
{ {
ASSERT_FAILED(VOID_RETURN); // something wrong, no one claims the isr's source TU_ASSERT(false); // something wrong, no one claims the isr's source
} }
} }