/*
 *******************************************************************************
 *  Copyright (c) 2010-2022 VATICS(KNERON) Inc. All rights reserved.
 *
 *  +-----------------------------------------------------------------+
 *  | THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED |
 *  | AND COPIED IN ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH  |
 *  | A LICENSE AND WITH THE INCLUSION OF THE THIS COPY RIGHT NOTICE. |
 *  | THIS SOFTWARE OR ANY OTHER COPIES OF THIS SOFTWARE MAY NOT BE   |
 *  | PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. THE   |
 *  | OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED.        |
 *  |                                                                 |
 *  | THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT   |
 *  | ANY PRIOR NOTICE AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY |
 *  | VATICS(KNERON) INC.                                             |
 *  +-----------------------------------------------------------------+
 *
 *******************************************************************************
 */
#ifndef SHARED_COMPACT_MEMORY_H
#define SHARED_COMPACT_MEMORY_H

#ifdef __cplusplus
extern "C" {
#endif

#include <pthread.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>

#define VMF_VENC_OUTPUT_SCM_HEADER 256 //! SCM Encode Header Size

struct _scm_buffer_t_
{
	uint32_t dwOccupiedSpace;
	uint8_t *pbyVirtAddr;
	uint8_t *pbyPhysAddr;
};

typedef struct _scm_buffer_t_ scm_buffer_t;
typedef scm_buffer_t SCM_BUFFER_T;

typedef struct _scm_handle_t_ scm_handle_t;
typedef scm_handle_t SCM_HANDLE_T;

extern const SCM_BUFFER_T SCM_BUFFER_T_DEFAULT;

/**
 * @brief Create a SharedCompactMemory writer
 *
 * @param[in] The name of object to be created or opened.
 * @param[in] The buffer size of the object. Please use bigger than 2*(check size). Recommend 2.5*(check size).
 * @param[in] The check size of the object. Every time SCM writer will make sure this size for writing buffer.
 * @param[in] The flag for block mode. True: Writer will waiting reader if buffer is not enough.
 *                                     False: Writer will remove the oldest data in buffer if buffer is not enough.
 * @param[in] The flag for limit mode. True: SCM handle will only hold the maximum 16 data logs in handle.
 *                                     False: SCM handle will not limit the amount of data logs in handle.
 *
 * @return The handle of shared compact memroy.
 */
SCM_HANDLE_T *SCM_InitWriter(const char *, uint32_t, uint32_t, bool, bool);

/**
 * @brief Create a SharedCompactMemory reader
 *
 * @param[in] The name of object to be created or opened.
 *
 * @return The handle of shared compact memroy.
 */
SCM_HANDLE_T *SCM_InitReader(const char *);

/**
 * @brief Function to explicitly recycle SharedCompactMemory handle.
 *
 * @param[in] The handle of shared compact memory.
 *
 * @return 0 with no error, -1 otherwise
 */
int SCM_Release(SCM_HANDLE_T *);

/**
 * @brief Record the current 'scm buffer' and update it with new writable address.
 *
 * @param[in] The handle of shared compact memory.
 * @param[in] The pointer of SCM_BUFFER_T structure.
 *
 * @return 0 with no error, -1 otherwise
 */
int SCM_SendGetWriterBuff(SCM_HANDLE_T *, SCM_BUFFER_T *);

/**
 * @brief Release the current 'scm buffer'and update it with new readable address.
 *
 * @param[in] The handle of shared compact memory.
 * @param[in] The pointer of SCM_BUFFER_T structure.
 *
 * @return 0 with no error, -2 when awakened, -1 otherwise
 */
int SCM_ReturnReceiveReaderBuff(SCM_HANDLE_T *, SCM_BUFFER_T *);

/**
 * @brief It is used to relase a 'scm buffer' (For reader only)
 *
 * @param[in] The handle of shared compact memory.
 * @param[in] The pointer of srb_buffer_t structure.
 *
 * @return 0 with no error, -1 otherwise
 */
int SCM_ReturnReaderBuff(SCM_HANDLE_T *, SCM_BUFFER_T *);

/**
 * @brief Wake up the reader while it is waiting for a buffer coming from the writer.
 *
 * @param[in] The handle of shared compact memroy.
 *
 * @return 0 with no error, -1 otherwise
 */
int SCM_WakeupReader(SCM_HANDLE_T *);

#ifdef __cplusplus
}
#endif

#endif //SHARED_COMPACT_MEMORY_H
