#ifndef VMF_SYNC_SHARED_MEMORY_H
#define VMF_SYNC_SHARED_MEMORY_H

#include <stdint.h>

/* VMF SSM (Sync Shared Memory) — opaque handle */
typedef struct VMF_SSM_HANDLE_S VMF_SSM_HANDLE_T;

/* Legacy SSM reader/writer handle (lower-case) */
typedef struct ssm_handle_s ssm_handle_t;

/* Uppercase alias used in kdp2_host_stream.c */
typedef ssm_handle_t SSM_HANDLE_T;

/* SSM buffer descriptor */
typedef struct {
    void          *buffer;
    unsigned char *buffer_phys_addr;
    uint32_t       buf_size;
    uint32_t       _pad[4];
} SSM_BUFFER_T;

/* SSM writer init options */
typedef struct {
    const char  *name;
    uint32_t     buf_size;
    uint32_t     alignment;
    uint32_t     pshared;
    void        *pUserData;
    void       (*fp_setup_buffer)(unsigned char *buf, uint32_t size, void *pUserData);
    uint32_t     _pad[4];
} SSM_WRITER_INIT_OPTION_T;

/* VMF frame info (opaque stub) */
typedef struct {
    uint32_t _pad[32];
} VMF_FRAME_INFO_T;

/* Lowercase alias for SSM_BUFFER_T used in kdp2_host_stream.c */
typedef SSM_BUFFER_T ssm_buffer_t;

/* SSM reader image scheme */
typedef enum {
    VMF_SSM_READER_SCHEME_BLOCK     = 0,
    VMF_SSM_READER_SCHEME_NONBLOCK  = 1,
} VMF_SSM_READER_SCHEME;

/* SSM header size */
#define VMF_MAX_SSM_HEADER_SIZE  128u

int  VMF_SSM_Open(VMF_SSM_HANDLE_T **handle, const char *name, uint32_t size);
int  VMF_SSM_Close(VMF_SSM_HANDLE_T *handle);

/* SSM reader API */
ssm_handle_t *SSM_Reader_Init(const char *name, int mode);
int  SSM_Reader_Open(ssm_handle_t **handle, const char *name);
int  SSM_Reader_Close(ssm_handle_t *handle);
int  SSM_Reader_GetBuf(ssm_handle_t *handle, void **buf, uint32_t *size, int timeout_ms);
int  SSM_Reader_ReleaseBuf(ssm_handle_t *handle, void *buf);
void SSM_Reader_Wakeup(ssm_handle_t *handle);
int  SSM_Reader_ReturnReceiveBuff(ssm_handle_t *handle, SSM_BUFFER_T *buf);
int  SSM_Reader_ReturnBuff(ssm_handle_t *handle, SSM_BUFFER_T *buf);

/* SSM writer API */
ssm_handle_t *SSM_Writer_Init(SSM_WRITER_INIT_OPTION_T *opt);
int  SSM_Writer_SendGetBuff(ssm_handle_t *handle, SSM_BUFFER_T *buf);
int  SSM_Release(ssm_handle_t *handle);

/* VSRC SSM info helpers */
void VMF_VSRC_SSM_GetInfo(void *buf, void *info);
void VMF_VSRC_SSM_SetInfo(void *buf, void *info);

int  SSM_Reader_ReturnReceiveNewestBuff(ssm_handle_t *handle, SSM_BUFFER_T *buf, VMF_SSM_READER_SCHEME scheme);

#endif /* VMF_SYNC_SHARED_MEMORY_H */
