diff --git a/openair3/NAS/COMMON/UTIL/TLVDecoder.c b/openair3/NAS/COMMON/UTIL/TLVDecoder.c index 0ed4b6f456..625176319e 100644 --- a/openair3/NAS/COMMON/UTIL/TLVDecoder.c +++ b/openair3/NAS/COMMON/UTIL/TLVDecoder.c @@ -23,28 +23,4 @@ #include #include -#include "TLVDecoder.h" - int errorCodeDecoder = 0; - -static const char* const errorCodeStringDecoder[] = { - "No error", - "Buffer NULL", - "Buffer too short", - "Unexpected IEI", - "Mandatory field not present", - "Wrong message type", - "EXT value doesn't match", - "Protocol not supported", -}; - -void tlv_decode_perror(void) -{ - if (errorCodeDecoder >= 0) - // No error or TLV_DECODE_ERR_OK - return; - - printf("TLV decoder error: (%d, %s)\n", errorCodeDecoder, errorCodeStringDecoder[errorCodeDecoder * -1]); -} - - diff --git a/openair3/NAS/COMMON/UTIL/TLVDecoder.h b/openair3/NAS/COMMON/UTIL/TLVDecoder.h index 1f93d7a554..90b95cd6bb 100644 --- a/openair3/NAS/COMMON/UTIL/TLVDecoder.h +++ b/openair3/NAS/COMMON/UTIL/TLVDecoder.h @@ -95,48 +95,56 @@ typedef enum { extern int errorCodeDecoder; -void tlv_decode_perror(void); +#ifdef ENABLE_TESTS +#define TLV_DEC_ERROR(...) fprintf(stderr, "TLV Decoder: " __VA_ARGS__) +#else +#define TLV_DEC_ERROR(...) // Do nothing +#endif -#define CHECK_PDU_POINTER_AND_LENGTH_DECODER(bUFFER, mINIMUMlENGTH, lENGTH) \ - if (bUFFER == NULL) \ - { \ - printf("(%s:%d) Got NULL pointer for the payload\n", \ - __FILE__, __LINE__); \ - errorCodeDecoder = TLV_DECODE_BUFFER_NULL; \ - LOG_FUNC_RETURN(TLV_DECODE_BUFFER_NULL); \ - } \ - if (lENGTH < mINIMUMlENGTH) \ - { \ - printf("(%s:%d) Expecting at least %d bytes, got %u\n", \ - __FILE__, __LINE__, mINIMUMlENGTH, lENGTH); \ - errorCodeDecoder = TLV_DECODE_BUFFER_TOO_SHORT; \ - LOG_FUNC_RETURN(TLV_DECODE_BUFFER_TOO_SHORT); \ - } +#define CHECK_PDU_POINTER_AND_LENGTH_DECODER(bUFFER, mINIMUMlENGTH, lENGTH) \ + do { \ + if ((bUFFER) == NULL) { \ + TLV_DEC_ERROR("(%s:%d) Got NULL pointer for the payload\n", __FILE__, __LINE__); \ + return TLV_DECODE_BUFFER_NULL; \ + } \ + if ((lENGTH) < (mINIMUMlENGTH)) { \ + TLV_DEC_ERROR("(%s:%d) Expecting at least %d bytes, got %u\n", __FILE__, __LINE__, (mINIMUMlENGTH), (lENGTH)); \ + return TLV_DECODE_BUFFER_TOO_SHORT; \ + } \ + } while (0) -#define CHECK_LENGTH_DECODER(bUFFERlENGTH, lENGTH) \ - if (bUFFERlENGTH < lENGTH) \ - { \ - errorCodeDecoder = TLV_DECODE_BUFFER_TOO_SHORT; \ - LOG_FUNC_RETURN(TLV_DECODE_BUFFER_TOO_SHORT); \ - } +#define CHECK_LENGTH_DECODER(bUFFERlENGTH, lENGTH) \ + do { \ + if ((bUFFERlENGTH) < (lENGTH)) { \ + TLV_DEC_ERROR("(%s:%d) Buffer is too short, length (%u) is less than the required length (%u)\n", \ + __FILE__, \ + __LINE__, \ + (bUFFERlENGTH), \ + (lENGTH)); \ + return TLV_DECODE_BUFFER_TOO_SHORT; \ + } \ + } while (0) -#define CHECK_MESSAGE_TYPE(mESSAGE_tYPE, bUFFER) \ - { \ - if (mESSAGE_tYPE != bUFFER) \ - { \ - errorCodeDecoder = TLV_DECODE_WRONG_MESSAGE_TYPE; \ - LOG_FUNC_RETURN(errorCodeDecoder); \ - } \ - } +#define CHECK_MESSAGE_TYPE(mESSAGE_TYPE, BUFFER) \ + do { \ + if ((MESSAGE_TYPE) != (BUFFER)) { \ + TLV_DEC_ERROR("(%s:%d) Wrong message type: Got: %u, Expected: %u\n", __FILE__, __LINE__, (BUFFER), (MESSAGE_TYPE)); \ + return TLV_DECODE_WRONG_MESSAGE_TYPE; \ + } \ + } while (0) -#define CHECK_IEI_DECODER(iEI, bUFFER) \ - if(iEI != bUFFER) \ - { \ - printf("IEI is different than the one expected." \ - "(Got: 0x%x, expecting: 0x%x)\n", bUFFER, iEI); \ - errorCodeDecoder = TLV_DECODE_UNEXPECTED_IEI; \ - LOG_FUNC_RETURN(TLV_DECODE_UNEXPECTED_IEI); \ - } +#define CHECK_IEI_DECODER(iEI, bUFFER) \ + do { \ + if ((iEI) != (bUFFER)) { \ + TLV_DEC_ERROR( \ + "IEI check failure: %s:%d: IEI is different than the one expected: " \ + "(Got: 0x%x, expecting: 0x%x)\n", \ + __FILE__, \ + __LINE__, \ + (bUFFER), \ + (iEI)); \ + return TLV_DECODE_UNEXPECTED_IEI; \ + } \ + } while (0) #endif /* define (TLV_DECODER_H_) */ -