summaryrefslogtreecommitdiff
path: root/tinyusb
diff options
context:
space:
mode:
authorhathach <[email protected]>2018-03-29 13:07:27 +0700
committerhathach <[email protected]>2018-03-29 13:07:27 +0700
commitc50da4962e47ee9139d66478a8c4809f4761e47b (patch)
tree8769a3f91e1d95525085abc6e61252a807b93434 /tinyusb
parentccf6f03817daf860173a41d72454ae23b2eab974 (diff)
clean up assert and verify
Diffstat (limited to 'tinyusb')
-rw-r--r--tinyusb/class/cdc/cdc_host.c4
-rw-r--r--tinyusb/class/custom_class_host.c2
-rw-r--r--tinyusb/class/hid/hid_device.c4
-rw-r--r--tinyusb/class/hid/hid_host.c6
-rw-r--r--tinyusb/common/assertion.h15
-rw-r--r--tinyusb/common/verify.h118
-rw-r--r--tinyusb/host/hub.c2
-rw-r--r--tinyusb/host/usbh.c2
8 files changed, 75 insertions, 78 deletions
diff --git a/tinyusb/class/cdc/cdc_host.c b/tinyusb/class/cdc/cdc_host.c
index 25abda277..56f36aedb 100644
--- a/tinyusb/class/cdc/cdc_host.c
+++ b/tinyusb/class/cdc/cdc_host.c
@@ -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++)
{
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);
- ASSERT_INT(TUSB_XFER_BULK, p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED);
+ TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, 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 ) ?
&p_cdc->pipe_in : &p_cdc->pipe_out;
diff --git a/tinyusb/class/custom_class_host.c b/tinyusb/class/custom_class_host.c
index 318813f4b..0d24c01b7 100644
--- a/tinyusb/class/custom_class_host.c
+++ b/tinyusb/class/custom_class_host.c
@@ -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++)
{
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 ) ?
&custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out;
diff --git a/tinyusb/class/hid/hid_device.c b/tinyusb/class/hid/hid_device.c
index 8bad4cbbe..47aa55abf 100644
--- a/tinyusb/class/hid/hid_device.c
+++ b/tinyusb/class/hid/hid_device.c
@@ -264,12 +264,12 @@ tusb_error_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface
//------------- HID descriptor -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
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 -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
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)
{
diff --git a/tinyusb/class/hid/hid_host.c b/tinyusb/class/hid/hid_host.c
index 43acddcf2..b0537875a 100644
--- a/tinyusb/class/hid/hid_host.c
+++ b/tinyusb/class/hid/hid_host.c
@@ -76,7 +76,7 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int
{
//------------- parameters validation -------------//
// 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);
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 -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
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 -------------//
p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH];
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
diff --git a/tinyusb/common/assertion.h b/tinyusb/common/assertion.h
index c16e7d1a3..8a132d5b1 100644
--- a/tinyusb/common/assertion.h
+++ b/tinyusb/common/assertion.h
@@ -90,19 +90,8 @@ extern "C"
TUSB_ERROR_NONE == status, status, "%s", tusb_strerr[status])
//--------------------------------------------------------------------+
-// Logical Assert
-//--------------------------------------------------------------------+
-#define ASSERT_FAILED(error) ASSERT_DEFINE( , false, error, "%s", "FAILED")
-
-//--------------------------------------------------------------------+
// 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) \
ASSERT_DEFINE( uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
(low <= act) && (act <= up),\
@@ -112,15 +101,11 @@ extern "C"
//--------------------------------------------------------------------+
// 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__)
//--------------------------------------------------------------------+
// 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__)
diff --git a/tinyusb/common/verify.h b/tinyusb/common/verify.h
index 07ff71793..4704dede0 100644
--- a/tinyusb/common/verify.h
+++ b/tinyusb/common/verify.h
@@ -41,15 +41,21 @@
#include "tusb_option.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
extern "C" {
#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
@@ -76,93 +82,99 @@ static inline void verify_breakpoint(void)
}
#else
-
#define verify_breakpoint()
-
#endif
/*------------------------------------------------------------------*/
-/* VERIFY STATUS
- * - VERIFY_STS_1ARGS : return status of condition if failed
- * - VERIFY_STS_2ARGS : return provided status code if failed
+/* Macro Generator
*------------------------------------------------------------------*/
-#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) \
- do { \
- uint32_t _status = (uint32_t)(sts); \
- if ( 0 != _status ) { _VERIFY_MESS(_status) return _error; }\
- } while(0)
+// 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
-/**
- * Check if status is success (zero), otherwise return
- * - 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 and VERIFY_HDLR -------------*/
+#define VERIFY_DEFINE(_cond, _handler, _error) do { if ( !(_cond) ) { _handler return _error; } } while(0)
-/*------------------------------------------------------------------*/
-/* 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) \
+/*------------- Generator for VERIFY_STATUS and VERIFY_STATUS_HDLR -------------*/
+#define VERIFY_STS_DEF2(sts, _handler) \
do { \
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)
-#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) \
+#define VERIFY_STS_DEF3(sts, _handler, _error) \
do { \
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)
-#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__)
+
+
/*------------------------------------------------------------------*/
/* VERIFY
* - VERIFY_1ARGS : return false if failed
* - VERIFY_2ARGS : return provided value if failed
*------------------------------------------------------------------*/
-#define VERIFY_1ARGS(cond) do { if (!(cond)) return false; } while(0)
-#define VERIFY_2ARGS(cond, _error) do { if (!(cond)) return _error; } while(0)
+#define VERIFY_1ARGS(_cond) VERIFY_DEFINE(_cond, , false)
+#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_HDLR_2ARGS : execute handler, return false if failed
* - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
-#define VERIFY_HDLR_2ARGS(cond, _handler) \
- do { if ( !(cond) ) { _handler; return false; } } while(0)
+#define VERIFY_HDLR_2ARGS(cond, _handler) VERIFY_DEFINE(cond, _handler; , false)
+#define VERIFY_HDLR_3ARGS(cond, _handler, _error) VERIFY_DEFINE(cond, _handler; , _error)
+
+#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
+
+
+/*------------------------------------------------------------------*/
+/* 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_HDLR_3ARGS(cond, _handler, _error) \
- do { if ( !(cond) ) { _handler; return _error; } } while(0)
+#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
- * basically VERIFY with verify_breakpoint as handler
+ * basically VERIFY with verify_breakpoint() as handler
* - 1 arg : return false 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_2ARGS(cond, _error) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return _error;} } while(0)
+#define ASSERT_1ARGS(cond) VERIFY_DEFINE(cond, verify_breakpoint(); , false)
+#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__)
+
+/*------------------------------------------------------------------*/
+/* 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 TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
+#define ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__)
#ifdef __cplusplus
}
diff --git a/tinyusb/host/hub.c b/tinyusb/host/hub.c
index 6432ff62b..7bcd009e8 100644
--- a/tinyusb/host/hub.c
+++ b/tinyusb/host/hub.c
@@ -230,7 +230,7 @@ void hub_isr(pipe_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes)
else
{
// 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) );
}
}
diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c
index 38f4bb346..a491988c3 100644
--- a/tinyusb/host/usbh.c
+++ b/tinyusb/host/usbh.c
@@ -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);
}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
}
}