#ifndef _ESECOMM_UTILS_H
#define _ESECOMM_UTILS_H

#include "esecomm_debug_log.h"
#include "tz_esecomm_errors.h"

#define ARRAY_SIZE(x)  (uint32_t)((sizeof(x) / sizeof((x)[0])))

#define TZ_ESECOMM_LITTLE_ENDIAN (0)
#define TZ_ESECOMM_BIG_ENDIAN    (1)

int hexa_digits_to_bytes(uint8_t *, uint8_t *, int);
uint8_t * bytes_to_hexastr(uint8_t *pbytes, int size);

static __inline int in_range(uint32_t low, uint32_t high, uint32_t x)
{
	if ((x < low) || (high < low))
		return -1;

	return (x - low) <= (high - low) ? 0 : -1;
}

/**
 * @brief
 * Fill response based on result code
 *
 * @param[in] result_code - function result code
 * @param[out] response   - pointer to store text response
 */
void fill_response(uint32_t result_code, uint8_t *response);

uint8_t get_endianness();

/**@Validate a buffer is within the given range, including buffer>=minAddr and buffer+bufLen<=maxAddr.
 *
 * @param[in] buffer:  The starting address of the buffer
 * @param[in] bufLen: The length of the buffer. 
 * @param[in] minAddr:  The minimum address allowed for the buffer.
 * @param[in] maxAddr:  The maximum address allowed for the buffer (the address of the last byte in the buffer plus 1).
 *
 * Return 1 if the buffer passes the validation. Otherwise, return 0.
 * */
int isBufferInRange(const void *buffer, const uint32_t bufLen, const void *minAddr, const void *maxAddr);

/**@Set all bytes in a buffer to be zero.
 *
 * @param[in] buffer:  The starting address of the buffer
 * @param[in] bufLen: The length of the buffer. 
 * Return void.
 * */
void zeroOutBuffer(const void *buffer, const uint32_t bufLen);

#endif
