Virgil IoT KIT
Macros
macros.h File Reference

Macros to simplify code. More...

#include <virgil/iot/logger/logger.h>

Go to the source code of this file.

Macros

#define CHECK(CONDITION, MESSAGE, ...)
 Check condition and perform goto terminate if non-successful. More...
 
#define CHECK_RET(CONDITION, RETCODE, MESSAGE, ...)
 Check condition and return RETCODE if non-successful. More...
 
#define BOOL_CHECK(CONDITION, MESSAGE, ...)   CHECK((CONDITION), (MESSAGE), ##__VA_ARGS__)
 Check condition and perform goto terminate if non-successful. More...
 
#define BOOL_CHECK_RET(CONDITION, MESSAGE, ...)   CHECK_RET((CONDITION), false, (MESSAGE), ##__VA_ARGS__)
 Check condition and return false if non-successful. More...
 
#define MEMCMP_CHECK(BUF1, BUF2, SIZE)
 Compares two buffer and perform goto terminate if non-successful. More...
 
#define MEMCMP_CHECK_RET(BUF1, BUF2, SIZE, RETCODE)
 Compares two buffers and return RETCODE if non-successful. More...
 
#define CHECK_NOT_ZERO(ARG)   CHECK((ARG), "Argument " #ARG " must not be zero")
 Checks that ARG is non-zero and perform goto terminate in case of zero one. More...
 
#define CHECK_NOT_ZERO_RET(ARG, RETCODE)   CHECK_RET((ARG), (RETCODE), "Argument " #ARG " must not be zero") \
 Checks that ARG is non-zero and return RETCODE in case of zero one. More...
 

Detailed Description

Macros to simplify code.

Here you can find different macros to simplify code usage.

Each macros does specified operation and checks its result. If it is successful, there is no other action. In another case it outputs messages and terminates its execution. There are two ways to terminate normal flow :

Message uses Logger module for output. You can use printf-like syntax with variables.

Here you can see some examples :

int a;
const char *buf1;
const char *buf2;
size_t buf_size;
// Goto terminate in case of error
CHECK(a == 3, "a = %d while it must be equal to 3", a);
// Return in case of error
CHECK_RET(a == 3, VS_CODE_ERR_INCORRECT_PARAMETER, "a = %d while it must be equal to 3", a);
// Compare two buffers
MEMCMP_CHECK_RET(buf1, buf2, buf_size, VS_CODE_OLD_VERSION);
// Checks variable to be not-NULL
// Process error
terminate:

Macro Definition Documentation

◆ BOOL_CHECK

#define BOOL_CHECK (   CONDITION,
  MESSAGE,
  ... 
)    CHECK((CONDITION), (MESSAGE), ##__VA_ARGS__)

Check condition and perform goto terminate if non-successful.

  1. CONDITION is compared with boolean false.
  2. If they are equal, MESSAGES is logged and function jumps to terminate label.
Warning
terminate label must be present in current function.
Parameters
[in]CONDITIONOperation to be checked.
[in]MESSAGEString with printf-like parameter to be logged in case of non-successful operation.

◆ BOOL_CHECK_RET

#define BOOL_CHECK_RET (   CONDITION,
  MESSAGE,
  ... 
)    CHECK_RET((CONDITION), false, (MESSAGE), ##__VA_ARGS__)

Check condition and return false if non-successful.

  1. CONDITION is compared with boolean false.
  2. If they are equal, MESSAGES is logged and function returns false.
Parameters
[in]CONDITIONOperation to be checked.
[in]MESSAGEString with printf-like parameter to be logged in case of non-successful operation.
Returns
false in case of error

◆ CHECK

#define CHECK (   CONDITION,
  MESSAGE,
  ... 
)
Value:
do { \
if (!(CONDITION)) { \
VS_LOG_ERROR((MESSAGE), ##__VA_ARGS__); \
goto terminate; \
} \
} while(0)

Check condition and perform goto terminate if non-successful.

  1. CONDITION is compared with zero code.
  2. If they are equal, MESSAGES is logged and function jumps to terminate label.
Warning
terminate label must be present in current function.
Parameters
[in]CONDITIONOperation to be checked.
[in]MESSAGEString with printf-like parameter to be logged in case of non-successful operation.

◆ CHECK_NOT_ZERO

#define CHECK_NOT_ZERO (   ARG)    CHECK((ARG), "Argument " #ARG " must not be zero")

Checks that ARG is non-zero and perform goto terminate in case of zero one.

  1. ARG is compared with zero.
  2. If ARG is zero, result message is logged and function jumps to terminate label.
Warning
terminate label must be present in current function.
Parameters
[in]ARGArgument to be checked.

◆ CHECK_NOT_ZERO_RET

#define CHECK_NOT_ZERO_RET (   ARG,
  RETCODE 
)    CHECK_RET((ARG), (RETCODE), "Argument " #ARG " must not be zero") \

Checks that ARG is non-zero and return RETCODE in case of zero one.

  1. ARG is compared with zero.
  2. If ARG is zero, result message is logged and returns RETCODE.
Warning
terminate label must be present in current function.
Parameters
[in]ARGArgument to be checked.
[in]RETCODEReturn code in case of unsuccessful result.
Returns
RETCODE in case of error

◆ CHECK_RET

#define CHECK_RET (   CONDITION,
  RETCODE,
  MESSAGE,
  ... 
)
Value:
do { \
if (!(CONDITION)) { \
VS_LOG_ERROR((MESSAGE), ##__VA_ARGS__); \
return (RETCODE); \
} \
} while(0)

Check condition and return RETCODE if non-successful.

  1. CONDITION is compared with zero code.
  2. If they are equal, MESSAGES is logged and function returns RETCODE.
Parameters
[in]CONDITIONOperation to be checked.
[in]RETCODEReturn code in case of error.
[in]MESSAGEString with printf-like parameter to be logged in case of non-successful operation.
Returns
RETCODE in case of error

◆ MEMCMP_CHECK

#define MEMCMP_CHECK (   BUF1,
  BUF2,
  SIZE 
)
Value:
CHECK(VS_IOT_MEMCMP((BUF1), (BUF2), (SIZE)) == 0, \
#BUF1 " is not equal to " #BUF2 " while comparing %d bytes", \
(int)(SIZE))

Compares two buffer and perform goto terminate if non-successful.

  1. BUF1 is compared with BUF2. SIZE bytes are compared.
  2. If they are not equal, result message is logged and function jumps to terminate label.
Warning
terminate label must be present in current function.
BUF1 and BUF2 sizes must be the same or bigger than SIZE.
Parameters
[in]BUF1First data buffer to be checked.
[in]BUF2Second data buffer to be checked.
[in]SIZEData size.

◆ MEMCMP_CHECK_RET

#define MEMCMP_CHECK_RET (   BUF1,
  BUF2,
  SIZE,
  RETCODE 
)
Value:
CHECK_RET(VS_IOT_MEMCMP((BUF1), (BUF2), (SIZE)) == 0, (RETCODE), \
#BUF1 " is not equal to " #BUF2 " while comparing %d bytes", \
(int)(SIZE))

Compares two buffers and return RETCODE if non-successful.

  1. BUF1 is compared with BUF2. SIZE bytes are compared.
  2. If they are not equal, result mesage is logged and function returns RET.
Warning
terminate label must be present in current function.
BUF1 and BUF2 sizes must be the same or bigger than SIZE.
Parameters
[in]BUF1First data buffer to be checked.
[in]BUF2Second data buffer to be checked.
[in]SIZEData size.
[in]RETReturn code in case of unsuccessful result.
Returns
RET in case of error
VS_CODE_ERR_NULLPTR_ARGUMENT
@ VS_CODE_ERR_NULLPTR_ARGUMENT
Argument is NULL pointer while it must be not NULL.
Definition: status_code.h:81
CHECK_RET
#define CHECK_RET(CONDITION, RETCODE, MESSAGE,...)
Check condition and return RETCODE if non-successful.
Definition: macros.h:108
VS_IOT_MEMCMP
#define VS_IOT_MEMCMP
memcmp call
Definition: config/pc/stdlib-config.h:62
VS_CODE_OLD_VERSION
@ VS_CODE_OLD_VERSION
Provided file is not newer than the current file.
Definition: status_code.h:79
CHECK_NOT_ZERO_RET
#define CHECK_NOT_ZERO_RET(ARG, RETCODE)
Checks that ARG is non-zero and return RETCODE in case of zero one.
Definition: macros.h:199
MEMCMP_CHECK_RET
#define MEMCMP_CHECK_RET(BUF1, BUF2, SIZE, RETCODE)
Compares two buffers and return RETCODE if non-successful.
Definition: macros.h:171
CHECK
#define CHECK(CONDITION, MESSAGE,...)
Check condition and perform goto terminate if non-successful.
Definition: macros.h:90
VS_CODE_ERR_INCORRECT_PARAMETER
@ VS_CODE_ERR_INCORRECT_PARAMETER
Incorrect parameter.
Definition: status_code.h:84