/******************************************************************************
 *
 * Copyright 2013, Silicon Image, Inc.  All rights reserved.
 * No part of this work may be reproduced, modified, distributed, transmitted,
 * transcribed, or translated into any language or computer format, in any form
 * or by any means without written permission of
 * Silicon Image, Inc., 1140 East Arques Avenue, Sunnyvale, California 94085
 *
 *****************************************************************************/
/**
 * @file Sii977xLib.c
 *
 * @brief Defines the exported functions for the DLL application.
 *
 *****************************************************************************/
#define SII_DEBUG 0

/***** #include statements ***************************************************/

#include <stdio.h>
#include <string.h>

#include "si_datatypes.h"
#include "si_system_api.h"
#include "sii9777rx_api.h"
#include "si_drv_cra_api.h"
#include "si_drv_isp_api.h"
#include "si_drv_ipc_api.h"
#include "sii9777_platform_api.h"
#include "sii9777rx_ipc_def.h"
#include "sii9777_user_def.h"

/***** Register Module name **************************************************/

SII_MODULE_NAME_SET(common_api);

/***** local macro definitions ***********************************************/

#ifndef SII9777_USER_DEF__MULTI_THREAD
#define SII9777_USER_DEF__MULTI_THREAD          0
#endif

#define RESULT_CODE__CMD_SUCCESS                ((uint8_t)0x00)
#define RESULT_CODE__CMD_ERR_FAIL               ((uint8_t)0xFF)

#define EVENT_MASK_INTERNAL                     (SII9777_EVENT_FLAGS__FLASH_DONE)
#define EVENT_MASK_EXTERNAL                     (~EVENT_MASK_INTERNAL)

//#define IPC_COMPLETION_TIMEOUT                  3200   /* milliseconds */
#define IPC_COMPLETION_TIMEOUT                  200   /* milliseconds */
#define BOOT_TIME_MAX                           5000   /* milliseconds */

#define RETURN_API(retval)                      { if( retval ) { SII_LOG1B(("(RETVAL=%08lX)\n", retval)); } return retval; }

//#define SII977X_LIB_ES0_MHL
#ifdef SII977X_LIB_ES0_MHL
#include "sii9777mhl_api.h"
#endif
#define CBUS_DEVCAP_LEN                         16
#define CBUS_EXTENDED_DEVCAP_LEN                4

#if( SII9777_USER_DEF__MULTI_THREAD )
  #if( SII9777_USER_DEF__IPC_SEMAPHORE )
    #define MUTEX_HANDLE_TAKE(pObj)             sMutexHandleTake(pObj)
    #define MUTEX_HANDLE_GIVE(pObj)             sMutexHandleGive(pObj)
  #else
    #define MUTEX_HANDLE_TAKE(pObj)             sMutexApiTake(pObj)
    #define MUTEX_HANDLE_GIVE(pObj)             sMutexApiGive(pObj)
  #endif

  #define MUTEX_API_TAKE(pObj)                  sMutexApiTake(pObj)
  #define MUTEX_API_GIVE(pObj)                  sMutexApiGive(pObj)
#else
  #define MUTEX_HANDLE_TAKE(pObj)               (0)
  #define MUTEX_HANDLE_GIVE(pObj)
  #define MUTEX_API_TAKE(pObj)                  (0)
  #define MUTEX_API_GIVE(pObj)
#endif

/***** local type definitions ************************************************/

typedef enum
{
    FLASHING_STATE_PROGRAMMING,
    FLASHING_STATE_DONE
} FlashingState_t;

typedef uint8_t Mutex_t;

typedef struct
{
    Sii9777Config_t                   config;
    Sii9777EventFlags_t               eventFlagsStat;
    Sii9777EventFlags_t               eventFlagsMask;
    Sii9777EventFlags_t               eventFlagsUser;
    uint32_t                          hInst;
    Sii9777EventCallbackFunc_t        siiLibEvtCbFunc;
    SiiPlatformDevId_t                devId;
#if( SII9777_USER_DEF__MULTI_THREAD )
    SiiPlatformSemaphore_t            semaphoreApi;
  #if( SII9777_USER_DEF__IPC_SEMAPHORE )
    SiiPlatformSemaphore_t            semaphoreIpc;
    SiiPlatformSemaphore_t            semaphoreHandle;
    bool_t                            bIpcGiven;
    bool_t                            bFromUserCallBack;
  #endif
#endif

    uint32_t		                  flashAddr;
    uint32_t		                  flashSize;
    bool_t                            bFlashRead;
    uint8_t*                          pFlashPacket;
#if( SII9777_FLASH_BUFFER_SIZE )
    uint8_t*                          pPacketPntr;
    uint16_t                          packetSize;
#endif

    SiiDrvIpcTagCode_t                tagCode;
    Sii9777BootStat_t                 bootStat;
    SiiDrvIpcInst_t                   ipcInst;
    SiiDrvIpcMesInst_t                mesReqInst;
    SiiDrvIpcMesInst_t                mesAckInst;
    SiiSysTimeMilli_t                 bootTimeOut;

    uint16_t                          servDatPntr;
} Obj_t;

/***** local prototypes ******************************************************/

static SiiRetVal_t sCbusLocalDevcapSet( Obj_t* pObj, uint8_t *pData, uint8_t offset, uint8_t len );
static SiiRetVal_t sCbusLocalDevcapGet( Obj_t* pObj, uint8_t *poData, uint8_t offset, uint8_t len );

/***** local data objects ****************************************************/

static SiiSysObjListInst_t        sObjListInst = SII_INST_NULL;
static Obj_t*                     spObj        = NULL;

/***** call-back functions ***************************************************/

/***** public functions ******************************************************/

#include "Sii9777ComLib.c"

SiiInst_t Sii9777Create( SiiPlatformDevId_t devId, Sii9777EventCallbackFunc_t pCallback, Sii9777Config_t* pConfig )
{
    SiiInst_t     parentInst = SII_INST_NULL;
    Obj_t*        pObj       = NULL;
    SiiRetVal_t   retVal     = 0;

    if( !sObjListInst )
    {
        sObjListInst = SiiSysObjListCreate(SII_MODULE_NAME_GET(), sizeof(Obj_t));
        if( !sObjListInst )
        {
            retVal = 1;
        }
    }

    if( !retVal )
    {
        /* Allocate memory for object */
        pObj = (Obj_t*)SiiSysObjInstanceCreate(sObjListInst, parentInst, "RX");
        if( !pObj )
        {
            /* Check for empty list */
            if( !SiiSysObjFirstGet(sObjListInst) )
            {
                /* If empty then delete list of objects */
                SiiSysObjListDelete(sObjListInst);
                sObjListInst = SII_INST_NULL;
            }
            retVal = 2;
        }
    }

    SII_LOG1A("Create", pObj, (""));
    if( !retVal )
    {
        spObj = pObj;

        pObj->devId               = devId, 
        /* allocate DrvAdapt handle */
        pObj->siiLibEvtCbFunc     = pCallback;
        pObj->config              = *pConfig;
        pObj->eventFlagsStat      = 0x00000000;
        pObj->eventFlagsMask      = SII9777_EVENT_FLAGS__BOOT_DONE;
        pObj->eventFlagsUser      = 0x00000000;
        pObj->flashAddr           = 0;
        pObj->flashSize           = 0;
        pObj->bFlashRead          = true;
        pObj->pFlashPacket        = NULL;
#if( SII9777_FLASH_BUFFER_SIZE    )
        pObj->pPacketPntr         = NULL;
        pObj->packetSize          = 0;
#endif
        pObj->tagCode             = 0;
        pObj->bootStat            = SII9777_BOOT_STAT__IN_PROGRESS;
        pObj->ipcInst             = SII_INST_NULL;
        pObj->mesReqInst          = SII_INST_NULL;
        pObj->mesAckInst          = SII_INST_NULL;
        pObj->bootTimeOut         = 0;
#if( SII9777_USER_DEF__MULTI_THREAD )
        pObj->semaphoreApi        = 0;
  #if( SII9777_USER_DEF__IPC_SEMAPHORE )
        pObj->semaphoreIpc        = 0;
        pObj->semaphoreHandle     = 0;
        pObj->bIpcGiven           = false;
        pObj->bFromUserCallBack   = false;
  #endif
#endif
    }

#if( SII9777_USER_DEF__MULTI_THREAD )
    /* Create semaphore that functions as Mutex for making API multi-thread safe */
    if( !retVal && (SII_PLATFORM_STATUS__SUCCESS != SiiPlatformSemaphoreCreate("SemaphoreApi", 1, 1, &pObj->semaphoreApi)) )
    {
        SII_ASSERT(0);
        sResourcesDelete(pObj);
        retVal = 3;
    }

  #if( SII9777_USER_DEF__IPC_SEMAPHORE )
    if( !retVal && (SII_PLATFORM_STATUS__SUCCESS != SiiPlatformSemaphoreCreate("SemaphoreIpc", 1, 0, &pObj->semaphoreIpc)) )
    {
        SII_ASSERT(0);
        sResourcesDelete(pObj);
        retVal = 3;
    }

    if( !retVal && (SII_PLATFORM_STATUS__SUCCESS != SiiPlatformSemaphoreCreate("SemaphoreHandle", 1, 1, &pObj->semaphoreHandle)) )
    {
        SII_ASSERT(0);
        sResourcesDelete(pObj);
        retVal = 4;
    }
  #endif
#endif

    if( !retVal )
    {
        SiiDrvIpcConfig_t ipcCfg;

        ipcCfg.devId = devId;
        
        /* initialize IPC module */
        pObj->ipcInst = SiiDrvIpcCreate(pObj->config.pNameStr, SII_OBJ2INST(pObj), &ipcCfg);
        if( !pObj->ipcInst )
        {
            SII_ASSERT(0);
            sResourcesDelete(pObj);
            retVal = 5;
        }
    }

    if( !retVal )
    {
        /* initialize IPC Request message */
        pObj->mesReqInst = SiiDrvIpcMessageCreate();
        if( !pObj->mesReqInst )
        {
            SII_ASSERT(0);
            sResourcesDelete(pObj);
            retVal = 6;
        }
    }

    if( !retVal )
    {
        /* initialize IPC Acknowledge message */
        pObj->mesAckInst = SiiDrvIpcMessageCreate();
        if( !pObj->mesAckInst )
        {
            SII_ASSERT(0);
            sResourcesDelete(pObj);
            retVal = 7;
        }
    }

    if( !retVal )
    {
        /* Enable INT signal activation */
        sInterruptsEnableSet(pObj, true);

        if( pConfig->bDeviceReset )
        {
            /* Apply device reset */
            sDeviceRestart(pObj);
        }
        else
        {
            /* Device may still be booting, set boot timeout timer */
            SiiSysTimeOutMilliSet(&pObj->bootTimeOut, BOOT_TIME_MAX);
        }

//        MUTEX_API_GIVE(pObj);
    }
    SII_LOG1B(("RETVAL = %d\n", retVal));
    return (retVal) ? (SII_INST_NULL) : (SII_OBJ2INST(pObj));
}
 
void Sii9777Delete( SiiInst_t inst )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Delete", pObj, (""));

        /* Disable INT signal activation */
        sInterruptsEnableSet(pObj, false);

        sResourcesDelete(pObj);
    }
}

SiiRetVal_t Sii9777ConfigureSet( SiiInst_t inst, Sii9777Config_t* pConfig )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ConfigureSet", pObj, ("\n"));

        /* Reset Sii9777 device */
        pObj->config = *pConfig;
        sDeviceRestart(pObj);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777ConfigureGet( SiiInst_t inst, Sii9777Config_t* pConfig )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ConfigureGet", pObj, ("\n"));

        *pConfig = pObj->config;
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777Plus5vQuery( SiiInst_t inst, Sii9777RxPort_t port, bool_t *pbPlus5v )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777Plus5vQuery", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_PLUS5V_QUERY);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_PLUS5V_QUERY);
        if( !retVal )
        {
            *pbPlus5v = (Sii9777DualLinkMode_t)SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbPlus5v));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777InputSelectSet( SiiInst_t inst, Sii9777RxPort_t* pPort )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777InputSelectSet", pObj, ("%d", *pPort));

        sRequestMessageInit(pObj, SII9777_INPUTSELECT_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, *pPort);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_INPUTSELECT_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777InputSelectGet( SiiInst_t inst, Sii9777RxPort_t* pPort )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777InputSelectGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_INPUTSELECT_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_INPUTSELECT_GET);
        if( !retVal )
        {
            *pPort = (Sii9777RxPort_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pPort));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidDdcSet( SiiInst_t inst, Sii9777RxPort_t port, bool_t* pbDdc )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidDdcSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pbDdc));

        sRequestMessageInit(pObj, SII9777_EDIDDDC_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbDdc);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDIDDDC_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidDdcGet( SiiInst_t inst, Sii9777RxPort_t port, bool_t* pbDdc )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidDdcGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_EDIDDDC_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDIDDDC_GET);
        if( !retVal )
        {
            *pbDdc = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbDdc));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidReplicateEnableSet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidReplicateEnableSet", pObj, ("%d", *pbOn));

        sRequestMessageInit(pObj, SII9777_EDIDREPLICATE_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDIDREPLICATE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidReplicateEnableGet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidReplicateEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_EDIDREPLICATE_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDIDREPLICATE_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidSet( SiiInst_t inst, Sii9777RxPort_t port, uint16_t offset, uint8_t *pData, uint16_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidSet", pObj, ("port=%d, offset=%04X, length=%d\n", (uint16_t)port, offset, length));
        if( !retVal && (255 < offset) )
        {
            retVal = 40;
        }
        if( !retVal && (256 < (length+offset)) )
        {
            retVal = 50;
        }
        if( !retVal )
        {
            SII_LOG1B_ARRAY(pData, length);

            sRequestMessageInit(pObj, SII9777_EDID_SET);
            SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
            SiiDrvIpcWordPush(pObj->mesReqInst, offset);
            SiiDrvIpcWordPush(pObj->mesReqInst, length);
            SiiDrvIpcArrayPush(pObj->mesReqInst, pData, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDID_SET);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidGet( SiiInst_t inst, Sii9777RxPort_t port, uint16_t offset, uint8_t *pData, uint16_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidSet", pObj, ("port=%d, offset=%04X, length=%d\n", (uint16_t)port, offset, length));
        if( !retVal && (255 < offset) )
        {
            retVal = 40;
        }
        if( !retVal && (256 < (length+offset)) )
        {
            retVal = 50;
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_EDID_GET);
            SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
            SiiDrvIpcWordPush(pObj->mesReqInst, offset);
            SiiDrvIpcWordPush(pObj->mesReqInst, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDID_GET);
        }
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pData, length);
            SII_LOG1B_ARRAY(pData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777EdidQuery( SiiInst_t inst, Sii9777RxPort_t port, uint16_t offset, uint8_t *pData, uint16_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777EdidQuery", pObj, ("port=%d, offset=%04X, length=%d\n", (uint16_t)port, offset, length));

        sRequestMessageInit(pObj, SII9777_EDID_QUERY);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcWordPush(pObj->mesReqInst, offset);
        SiiDrvIpcWordPush(pObj->mesReqInst, length);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_EDID_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pData, length);
            SII_LOG1B_ARRAY(pData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t	Sii9777HpdReplicateEnableSet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HpdReplicateEnableSet", pObj, ("%d", *pbOn));

        sRequestMessageInit(pObj, SII9777_HPDREPLICATEENABLE_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HPDREPLICATEENABLE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t	Sii9777HpdReplicateEnableGet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HpdReplicateEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HPDREPLICATEENABLE_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HPDREPLICATEENABLE_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HpdSet( SiiInst_t inst, Sii9777RxPort_t port, bool_t *pbHpd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HpdReplicateEnableSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pbHpd));

        sRequestMessageInit(pObj, SII9777_HPD_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbHpd);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HPD_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HpdGet( SiiInst_t inst, Sii9777RxPort_t port, bool_t *pbHpd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HpdGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_HPD_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HPD_GET);
        if( !retVal )
        {
            *pbHpd = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pbHpd));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HpdQuery( SiiInst_t inst, Sii9777RxPort_t port, bool_t *pbHpd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HpdQuery", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_HPD_QUERY);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HPD_QUERY);
        if( !retVal )
        {
            *pbHpd = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbHpd));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AvLinkQuery( SiiInst_t inst, Sii9777AvLink_t *pAvLink )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AvLinkQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_AVLINK_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_AVLINK_QUERY);
        if( !retVal )
        {
            *pAvLink = (Sii9777AvLink_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pAvLink));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AvMuteQuery( SiiInst_t inst, bool_t *pbAvMute )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AvMuteQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_AVMUTE_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_AVMUTE_QUERY);
        if( !retVal )
        {
            *pbAvMute = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbAvMute));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777VideoTimingQuery( SiiInst_t inst, Sii9777VideoTiming_t *pVideoTiming )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777VideoTimingQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_VIDEOTIMING_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_VIDEOTIMING_QUERY);
        if( !retVal )
        {
            pVideoTiming->interlaced  = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pVideoTiming->posHsync    = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pVideoTiming->posVsync    = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pVideoTiming->pixelFreq   = SiiDrvIpcLongPop(pObj->mesAckInst);
            pVideoTiming->vTotal      = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->vActive     = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->vSync       = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->vFrontPorch = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->hTotal      = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->hActive     = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->hSync       = SiiDrvIpcWordPop(pObj->mesAckInst);
            pVideoTiming->hFrontPorch = SiiDrvIpcWordPop(pObj->mesAckInst);

            SII_LOG1B(("interlaced  = %d\n",  pVideoTiming->interlaced));
            SII_LOG1B(("posHsync    = %d\n",  pVideoTiming->posHsync));
            SII_LOG1B(("posVsync    = %d\n",  pVideoTiming->posVsync));
            SII_LOG1B(("pixelFreq   = %ld\n", pVideoTiming->pixelFreq));
            SII_LOG1B(("vTotal      = %d\n",  pVideoTiming->vTotal));
            SII_LOG1B(("vActive     = %d\n",  pVideoTiming->vActive));
            SII_LOG1B(("vSync       = %d\n",  pVideoTiming->vSync));
            SII_LOG1B(("vFrontPorch = %d\n",  pVideoTiming->vFrontPorch));
            SII_LOG1B(("hTotal      = %d\n",  pVideoTiming->hTotal));
            SII_LOG1B(("hActive     = %d\n",  pVideoTiming->hActive));
            SII_LOG1B(("hSync       = %d\n",  pVideoTiming->hSync));
            SII_LOG1B(("hFrontPorch = %d\n",  pVideoTiming->hFrontPorch));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777VideoFormatQuery( SiiInst_t inst, Sii9777VideoFmt_t *poVideoFmt )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777VideoFormatQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_VIDEOFMT_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_VIDEOFMT_QUERY);
        if( !retVal )
        {
            poVideoFmt->vidRes              = (Sii9777VidRes_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->mode3D              = (Sii97773dMode_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->pictureAR           = (Sii9777PictAR_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->pixelRepetition     = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->pixFmt.clrFmt       = (Sii9777ClrFmt_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->pixFmt.crSmpl       = (Sii9777CrSmpl_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->pixFmt.bitDepth     = (Sii9777BitDepth_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->activeAR            = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->picScaling          = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->scanInfo            = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->contentType         = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->isITContent         = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->isActiveFormat      = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->isVertBarInfoValid  = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->isHorizBarInfoValid = SiiDrvIpcBytePop(pObj->mesAckInst);
            poVideoFmt->vertBarInfo.lineNumEndOfTopBar      = SiiDrvIpcWordPop(pObj->mesAckInst);
            poVideoFmt->vertBarInfo.lineNumStartOfBottomBar = SiiDrvIpcWordPop(pObj->mesAckInst);
            poVideoFmt->horizBarInfo.pixNumEndOfLeftBar     = SiiDrvIpcWordPop(pObj->mesAckInst);
            poVideoFmt->horizBarInfo.pixNumStartOfRightBar  = SiiDrvIpcWordPop(pObj->mesAckInst);

            SII_LOG1B(("vidRes              = %d\n", (uint16_t)poVideoFmt->vidRes));
            SII_LOG1B(("mode3D              = %d\n", (uint16_t)poVideoFmt->mode3D));
            SII_LOG1B(("pictureAR           = %d\n", (uint16_t)poVideoFmt->pictureAR));
            SII_LOG1B(("pixelRepetition     = %d\n", (uint16_t)poVideoFmt->pixelRepetition));
            SII_LOG1B(("pixFmt.clrFmt       = %d\n", (uint16_t)poVideoFmt->pixFmt.clrFmt));
            SII_LOG1B(("pixFmt.crSmpl       = %d\n", (uint16_t)poVideoFmt->pixFmt.crSmpl));
            SII_LOG1B(("pixFmt.bitDepth     = %d\n", (uint16_t)poVideoFmt->pixFmt.bitDepth));
            SII_LOG1B(("activeAR            = %d\n", (uint16_t)poVideoFmt->activeAR));
            SII_LOG1B(("picScaling          = %d\n", (uint16_t)poVideoFmt->picScaling));
            SII_LOG1B(("scanInfo            = %d\n", (uint16_t)poVideoFmt->scanInfo));
            SII_LOG1B(("contentType         = %d\n", (uint16_t)poVideoFmt->contentType));
            SII_LOG1B(("isITContent         = %d\n", (uint16_t)poVideoFmt->isITContent));
            SII_LOG1B(("isActiveFormat      = %d\n", (uint16_t)poVideoFmt->isActiveFormat));
            SII_LOG1B(("isVertBarInfoValid  = %d\n", (uint16_t)poVideoFmt->isVertBarInfoValid));
            SII_LOG1B(("isHorizBarInfoValid = %d\n", (uint16_t)poVideoFmt->isHorizBarInfoValid));
            SII_LOG1B(("vertBarInfo.lineNumEndOfTopBar      = %d\n", (uint16_t)poVideoFmt->vertBarInfo.lineNumEndOfTopBar));
            SII_LOG1B(("vertBarInfo.lineNumStartOfBottomBar = %d\n", (uint16_t)poVideoFmt->vertBarInfo.lineNumStartOfBottomBar));
            SII_LOG1B(("horizBarInfo.pixNumEndOfLeftBar     = %d\n", (uint16_t)poVideoFmt->horizBarInfo.pixNumEndOfLeftBar));
            SII_LOG1B(("horizBarInfo.pixNumStartOfRightBar  = %d\n", (uint16_t)poVideoFmt->horizBarInfo.pixNumStartOfRightBar));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AudioFormatQuery( SiiInst_t inst, Sii9777AudioFmt_t *pAudioFmt )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AudioFormatQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_AUDIOFMT_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_AUDIOFMT_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pAudioFmt->aifData, 10);
            SiiDrvIpcArrayPop(pObj->mesAckInst, pAudioFmt->csData, 7);

            SII_LOG1B(("Audio Info Frame : "));
            SII_LOG1B_ARRAY(pAudioFmt->aifData, 10);
            SII_LOG1B(("channel status   : "));
            SII_LOG1B_ARRAY(pAudioFmt->csData, 7);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777IsrcQuery( SiiInst_t inst, Sii9777Isrc_t* pIsrc )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777IsrcQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_ISRC_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_ISRC_QUERY);
        if( !retVal )
        {
            pIsrc->isrcCont   = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pIsrc->isrcStatus = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pIsrc->isrcValid  = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SiiDrvIpcArrayPop(pObj->mesAckInst, pIsrc->isrcData, 32);

            SII_LOG1B(("isrcCont   = %02X ", (uint16_t)pIsrc->isrcCont));
            SII_LOG1B(("isrcStatus = %02X ", (uint16_t)pIsrc->isrcStatus));
            SII_LOG1B(("isrcValid  = %02X ", (uint16_t)pIsrc->isrcValid));
            SII_LOG1B_ARRAY(pIsrc->isrcData, 32);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AcpQuery( SiiInst_t inst, Sii9777Acp_t* pAcp )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AcpQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_ACP_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_ACP_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pAcp->acpData, sizeof(pAcp->acpData));
            SII_LOG1B_ARRAY(pAcp->acpData, sizeof(pAcp->acpData));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777SpdQuery( SiiInst_t inst, Sii9777Spd_t* pSpd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777SpdQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_SPD_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_SPD_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pSpd->vendorName, 8);
            SiiDrvIpcArrayPop(pObj->mesAckInst, pSpd->productDescription, 16);
            pSpd->sourceDevInfo = (Sii9777SrcDevInfo_t)SiiDrvIpcBytePop(pObj->mesAckInst);

            SII_LOG1B(("vendorName         : "));
            SII_LOG1B_ARRAY(pSpd->vendorName, 8);
            SII_LOG1B(("productDescription : "));
            SII_LOG1B_ARRAY(pSpd->productDescription, 16);
            SII_LOG1B(("sourceDevInfo      : %02X\n", pSpd->sourceDevInfo));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdrQuery( SiiInst_t inst, Sii9777Hdr_t* pHdr )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdrQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HDR_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDR_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pHdr->hdrData, sizeof(pHdr->hdrData));
            SII_LOG1B_ARRAY(pHdr->hdrData, sizeof(pHdr->hdrData));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}
SiiRetVal_t Sii9777HdcpDdcSet( SiiInst_t inst, Sii9777RxPort_t port, bool_t* pbDdc )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpDdcSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pbDdc));

        sRequestMessageInit(pObj, SII9777_HDCPDDC_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbDdc);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPDDC_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpDdcGet( SiiInst_t inst, Sii9777RxPort_t port, bool_t* pbDdc )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpDdcGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_HDCPDDC_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPDDC_GET);
        if( !retVal )
        {
            *pbDdc = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pbDdc));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpStreamManageInfoQuery( SiiInst_t inst, Sii9777HdcpStreamManageInfo_t* pStreamManageInfo )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpStreamManageInfoQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HDCPSTREAMMANAGEINFO_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPSTREAMMANAGEINFO_QUERY);
        if( !retVal )
        {
            pStreamManageInfo->seqNumM         = SiiDrvIpcLongPop(pObj->mesAckInst);
            pStreamManageInfo->k               = SiiDrvIpcWordPop(pObj->mesAckInst);
            pStreamManageInfo->streamIdType[0] = SiiDrvIpcWordPop(pObj->mesAckInst);
            pStreamManageInfo->streamIdType[1] = SiiDrvIpcWordPop(pObj->mesAckInst);
            pStreamManageInfo->streamIdType[2] = SiiDrvIpcWordPop(pObj->mesAckInst);
            pStreamManageInfo->streamIdType[3] = SiiDrvIpcWordPop(pObj->mesAckInst);

            SII_LOG1B(("seqNumM= %08X\n", pStreamManageInfo->seqNumM));
            SII_LOG1B(("k      = %04X\n", pStreamManageInfo->k));
            SII_LOG1B_ARRAY((uint8_t *)pStreamManageInfo->streamIdType, sizeof(pStreamManageInfo->streamIdType));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpTopologySet( SiiInst_t inst, Sii9777HdcpTopology_t *pTopology )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpTopologySet", pObj, ("\n"));
        SII_LOG1B(("dcp1DeviceDs       = %02X\n", pTopology->bHdcp1DeviceDs));
        SII_LOG1B(("bHdcp20RepeaterDs  = %02X\n", pTopology->bHdcp20RepeaterDs));
        SII_LOG1B(("MaxCascadeExceeded = %02X\n", pTopology->bMaxCascadeExceeded));
        SII_LOG1B(("MaxDevsExceeded    = %02X\n", pTopology->bMaxDevsExceeded));
        SII_LOG1B(("deviceCount        = %02X\n", pTopology->deviceCount));
        SII_LOG1B(("depth              = %02X\n", pTopology->depth));
        SII_LOG1B(("seqNumV            = %08X\n", pTopology->seqNumV));

        sRequestMessageInit(pObj, SII9777_HDCPTOPOLOGY_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, (uint8_t)pTopology->bHdcp1DeviceDs);
        SiiDrvIpcBoolPush(pObj->mesReqInst, (uint8_t)pTopology->bHdcp20RepeaterDs);
        SiiDrvIpcBoolPush(pObj->mesReqInst, (uint8_t)pTopology->bMaxCascadeExceeded);
        SiiDrvIpcBoolPush(pObj->mesReqInst, (uint8_t)pTopology->bMaxDevsExceeded);
        SiiDrvIpcBytePush(pObj->mesReqInst, pTopology->deviceCount);
        SiiDrvIpcBytePush(pObj->mesReqInst, pTopology->depth);
        SiiDrvIpcLongPush(pObj->mesReqInst, pTopology->seqNumV);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPTOPOLOGY_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpTopologyGet( SiiInst_t inst, Sii9777HdcpTopology_t *pTopology )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpTopologyGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HDCPTOPOLOGY_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPTOPOLOGY_GET);
        if( !retVal )
        {
            pTopology->bHdcp1DeviceDs      = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pTopology->bHdcp20RepeaterDs   = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pTopology->bMaxCascadeExceeded = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pTopology->bMaxDevsExceeded    = SiiDrvIpcBoolPop(pObj->mesAckInst);
            pTopology->deviceCount         = SiiDrvIpcBytePop(pObj->mesAckInst);
            pTopology->depth               = SiiDrvIpcBytePop(pObj->mesAckInst);
            pTopology->seqNumV             = SiiDrvIpcLongPop(pObj->mesAckInst);

            SII_LOG1B(("dcp1DeviceDs       = %02X\n", pTopology->bHdcp1DeviceDs));
            SII_LOG1B(("bHdcp20RepeaterDs  = %02X\n", pTopology->bHdcp20RepeaterDs));
            SII_LOG1B(("MaxCascadeExceeded = %02X\n", pTopology->bMaxCascadeExceeded));
            SII_LOG1B(("MaxDevsExceeded    = %02X\n", pTopology->bMaxDevsExceeded));
            SII_LOG1B(("deviceCount        = %02X\n", pTopology->deviceCount));
            SII_LOG1B(("depth              = %02X\n", pTopology->depth));
            SII_LOG1B(("seqNumV            = %08X\n", pTopology->seqNumV));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpBksvRxidListSet( SiiInst_t inst, Sii9777HdcpKsv_t* pBksvList, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpBksvRxidListSet", pObj, ("\n"));

        if( !pBksvList )
        {
            retVal = 1;
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_HDCPBKSVRXIDLIST_SET);
            SiiDrvIpcBytePush(pObj->mesReqInst, len);
            while( len-- )
            {
                SiiDrvIpcArrayPush(pObj->mesReqInst, &pBksvList->b[0], 5);
                SII_LOG1B_ARRAY(&pBksvList->b[0], 5);
                pBksvList++;
             }
             sRequestMessageSend(pObj);

             retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPBKSVRXIDLIST_SET);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpBksvRxidListGet( SiiInst_t inst, Sii9777HdcpKsv_t* pBksvList, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpBksvRxidListGet", pObj, (""));

        if( !pBksvList )
        {
            retVal = 1;
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_HDCPBKSVRXIDLIST_GET);
            SiiDrvIpcBytePush(pObj->mesReqInst, len);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPBKSVRXIDLIST_GET);
        }
        if( !retVal )
        {
            uint8_t length = SiiDrvIpcBytePop(pObj->mesAckInst);

            if( length > len )
            {
                length = len;
            }

            while( length-- )
            {
                SiiDrvIpcArrayPop(pObj->mesAckInst, &pBksvList->b[0], 5);
                SII_LOG1B_ARRAY( &pBksvList->b[0], 5);
                pBksvList++;
            }
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpRepeaterEnableSet( SiiInst_t inst, bool_t *pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpRepeaterEnableSet", pObj, ("%d", (uint16_t)*pbOn));

        sRequestMessageInit(pObj, SII9777_HDCPREPEATERENABLE_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPREPEATERENABLE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpRepeaterEnableGet( SiiInst_t inst, bool_t *pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpRepeaterEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HDCPREPEATERENABLE_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPREPEATERENABLE_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777RxHdcpBksvOwnQuery( SiiInst_t inst, Sii9777HdcpKsv_t* pBksv )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777RxHdcpBksvOwnQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_RXHDCPBKSVOWN_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_RXHDCPBKSVOWN_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, (uint8_t*)pBksv, 5);
            SII_LOG1B_ARRAY((uint8_t*)pBksv, 5);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777RxHdcpRxIdOwnQuery( SiiInst_t inst, Sii9777HdcpKsv_t* pRxId)
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777RxHdcpRxIdOwnQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_RXHDCPRXIDOWN_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_RXHDCPRXIDOWN_QUERY);
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, (uint8_t*)pRxId, 5);
            SII_LOG1B_ARRAY((uint8_t*)pRxId, 5);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpStatusQuery( SiiInst_t inst, Sii9777HdcpStatus_t *pHdcpStat )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpStatusQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_HDCPSTATUS_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPSTATUS_QUERY);
        if( !retVal )
        {
            *pHdcpStat = (Sii9777HdcpStatus_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d\n", (uint16_t)*pHdcpStat));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpEnableSet( SiiInst_t inst, Sii9777TxPort_t port, bool_t *pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpEnableSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pbOn));

        sRequestMessageInit(pObj, SII9777_HDCPENABLE_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPENABLE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777HdcpEnableGet( SiiInst_t inst, Sii9777TxPort_t port, bool_t *pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777HdcpEnableGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_HDCPENABLE_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_HDCPENABLE_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777BitDepthLimitSet( SiiInst_t inst, Sii9777TxPort_t port, Sii9777BitDepth_t *pDeepColor )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777BitDepthLimitSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pDeepColor));

        sRequestMessageInit(pObj, SII9777_BITDEPTHLIMIT_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBytePush(pObj->mesReqInst, *pDeepColor);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_BITDEPTHLIMIT_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777BitDepthLimitGet( SiiInst_t inst, Sii9777TxPort_t port, Sii9777BitDepth_t *pDeepColor )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777BitDepthLimitGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_BITDEPTHLIMIT_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_BITDEPTHLIMIT_GET);
        if( !retVal )
        {
            *pDeepColor = (Sii9777BitDepth_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pDeepColor));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777RxCdSenseQuery( SiiInst_t inst, Sii9777CdSense_t *pCdSense )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777RxCdSenseQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_CDSENSE_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CDSENSE_QUERY);
        if( !retVal )
        {
            *pCdSense = (Sii9777CdSense_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d\n", (uint16_t)*pCdSense));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777MHLVersionQuery(SiiInst_t inst, Sii9777MhlVersion_t *pMhlVersion)
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777MHLVersionQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_MHLVERSION_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_MHLVERSION_QUERY);
        if( !retVal )
        {
            *pMhlVersion = (Sii9777MhlVersion_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d\n", (uint16_t)*pMhlVersion));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusModeQuery(SiiInst_t inst, Sii9777CbusMode_t *pCbusMode)
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusModeQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_CBUSMODE_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSMODE_QUERY);
        if( !retVal )
        {
            *pCbusMode = (Sii9777CbusMode_t)SiiDrvIpcBytePop(pObj->mesAckInst);      
            SII_LOG1B(("%d\n", (uint16_t)*pCbusMode));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777VbusRequestQuery(SiiInst_t inst, Sii9777VbusReq_t *pRequest)
{    
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777VbusRequestQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_VBUSREQUEST_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_VBUSREQUEST_QUERY);
        if( !retVal )
        {
            *pRequest = (Sii9777VbusReq_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d\n", (uint16_t)*pRequest));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777VbusRequestGrant(SiiInst_t inst)
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777VbusRequestGrant", pObj, (""));

        sRequestMessageInit(pObj, SII9777_VBUSREQUESTGRANT);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_VBUSREQUESTGRANT);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapSet( SiiInst_t inst, uint8_t *pData, uint8_t offset, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapSet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)len));
        if( ((offset + len) > CBUS_DEVCAP_LEN) || (len == 0) || !pData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            retVal = sCbusLocalDevcapSet(pObj, pData, offset, len);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapGet( SiiInst_t inst, uint8_t *poData, uint8_t offset, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapGet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)len));
        if( ((offset + len) > CBUS_DEVCAP_LEN) || (len == 0) || !poData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            retVal = sCbusLocalDevcapGet(pObj, poData, offset, len);
        }
        if( !retVal )
        {
            SII_LOG1B_ARRAY((uint8_t*)poData, len);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapPowSet( SiiInst_t inst, Sii9777DevCapVbusPow_t *pCbusPow )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t dcap;

        SII_LOG1A("Sii9777CbusLocalDevcapPowSet", pObj, ("%d\n", (uint16_t)*pCbusPow));

        retVal = sCbusLocalDevcapGet(pObj, &dcap, SII9777_DEVCAP_OFFSET__DEV_CAT, 1);
        if( !retVal )
        {
            if( *pCbusPow == SII9777_VBUS_PROVIDE_POWER )
            {
                dcap |= BIT4_MASK;
            }
            else
            {
                dcap &= ~BIT4_MASK;
            }
            retVal = sCbusLocalDevcapSet(pObj, &dcap, SII9777_DEVCAP_OFFSET__DEV_CAT, 1);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapPowGet( SiiInst_t inst, Sii9777DevCapVbusPow_t *pCbusPow )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t dcap;

        SII_LOG1A("Sii9777CbusLocalDevcapPowGet", pObj, (""));

        retVal = sCbusLocalDevcapGet(pObj, &dcap, SII9777_DEVCAP_OFFSET__DEV_CAT, 1);
        if( !retVal )
        {
            if( dcap & BIT4_MASK )
            {
                *pCbusPow = SII9777_VBUS_PROVIDE_POWER;
            }
            else
            {
                *pCbusPow = SII9777_VBUS_REMOVE_POWER;
            }
            SII_LOG1B(("%dn", (uint16_t)*pCbusPow));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapVideoLinkModeSet( SiiInst_t inst, Sii9777DevCapVidLinkMode_t *pVidLinkMode )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t mode = (uint8_t)*pVidLinkMode;

        SII_LOG1A("Sii9777CbusLocalDevcapVideoLinkModeSet", pObj, ("%d", (uint16_t)*pVidLinkMode));

        retVal = sCbusLocalDevcapSet(pObj, &mode, SII9777_DEVCAP_OFFSET__VID_LINK_MODE, 1);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapVideoLinkModeGet( SiiInst_t inst, Sii9777DevCapVidLinkMode_t *pVidLinkMode )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapVideoLinkModeGet", pObj, (""));

        retVal = sCbusLocalDevcapGet(pObj, (uint8_t*)pVidLinkMode, SII9777_DEVCAP_OFFSET__VID_LINK_MODE, 1);
        if( !retVal )
        {
            SII_LOG1B(("%d\n", (uint16_t)*pVidLinkMode));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapAudioLinkModeSet( SiiInst_t inst, Sii9777DevCapAudLinkMode_t *pAudLinkMode )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t mode = (uint8_t)*pAudLinkMode;

        SII_LOG1A("Sii9777CbusLocalDevcapAudioLinkModeSet", pObj, ("%d", (uint16_t)*pAudLinkMode));

        retVal = sCbusLocalDevcapSet(pObj, &mode, SII9777_DEVCAP_OFFSET__AUD_LINK_MODE, 1);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapAudioLinkModeGet( SiiInst_t inst, Sii9777DevCapAudLinkMode_t *pAudLinkMode )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapAudioLinkModeGet", pObj, (""));

        retVal = sCbusLocalDevcapGet(pObj, (uint8_t*)pAudLinkMode, SII9777_DEVCAP_OFFSET__AUD_LINK_MODE, 1);
        if( !retVal )
        {
            SII_LOG1B(("%d\n", (uint16_t)*pAudLinkMode));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapVideoTypeSet( SiiInst_t inst, Sii9777DevCapVideoType_t *pVideoType )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t mode = (uint8_t)*pVideoType;

        SII_LOG1A("Sii9777CbusLocalDevcapVideoTypeSet", pObj, ("%d", (uint16_t)*pVideoType));

        retVal = sCbusLocalDevcapSet(pObj, &mode, SII9777_DEVCAP_OFFSET__VIDEO_TYPE, 1);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapVideoTypeGet( SiiInst_t inst, Sii9777DevCapVideoType_t *pVideoType )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapVideoTypeGet", pObj, (""));

        retVal = sCbusLocalDevcapGet(pObj, (uint8_t*)pVideoType, SII9777_DEVCAP_OFFSET__VIDEO_TYPE, 1);
        if( !retVal )
        {
            SII_LOG1B(("%d\n", (uint16_t)*pVideoType));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapFeatureFlagSet( SiiInst_t inst, Sii9777DevCapFeatureFlag_t *pFeatureFlag )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        uint8_t mode = (uint8_t)*pFeatureFlag;

        SII_LOG1A("Sii9777CbusLocalDevcapFeatureFlagSet", pObj, ("%d", (uint16_t)*pFeatureFlag));

        retVal = sCbusLocalDevcapSet(pObj, &mode, SII9777_DEVCAP_OFFSET__FEATURE_FLAG, 1);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalDevcapFeatureFlagGet( SiiInst_t inst, Sii9777DevCapFeatureFlag_t *pFeatureFlag )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalDevcapFeatureFlagGet", pObj, (""));

        retVal = sCbusLocalDevcapGet(pObj, (uint8_t*)pFeatureFlag, SII9777_DEVCAP_OFFSET__FEATURE_FLAG, 1);
        if( !retVal )
        {
            SII_LOG1B(("%d\n", (uint16_t)*pFeatureFlag));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusDcapChgSend( SiiInst_t inst )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusDcapChgSend", pObj, (""));

        sRequestMessageInit(pObj, SII9777_CBUSDCAPCHG_SEND);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSDCAPCHG_SEND);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusRemoteDevcapQuery( SiiInst_t inst, uint8_t *poData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusRemoteDevcapQuery", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !poData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_CBUSREMOTEDEVCAP_QUERY);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSREMOTEDEVCAP_QUERY);
        }
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, poData, length);
            SII_LOG1B_ARRAY((uint8_t*)poData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalXDevcapSet( SiiInst_t inst, uint8_t *pData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalXDevcapSet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !pData )
        {
            SII_LOG2B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }

        if( !retVal )
        {
            SII_LOG1B_ARRAY((uint8_t*)pData, length);

            sRequestMessageInit(pObj, SII9777_CBUSLOCALXDEVCAP_SET);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            SiiDrvIpcArrayPush(pObj->mesReqInst, pData, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSLOCALXDEVCAP_SET);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusLocalXDevcapGet( SiiInst_t inst, uint8_t *pData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusLocalXDevcapGet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !pData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_CBUSLOCALXDEVCAP_GET);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSLOCALXDEVCAP_GET);
        }
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pData, length);
            SII_LOG1B_ARRAY((uint8_t*)pData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusRemoteXDevcapQuery( SiiInst_t inst, uint8_t *pData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusRemoteXDevcapQuery", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !pData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_CBUSREMOTEXDEVCAP_QUERY);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSREMOTEXDEVCAP_QUERY);
        }
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, pData, length);
            SII_LOG1B_ARRAY((uint8_t*)pData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusBurstSend( SiiInst_t inst, uint8_t *pData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusBurstSend", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !pData )
        {
            SII_LOG2B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }

        if( !retVal )
        {
            SII_LOG1B_ARRAY((uint8_t*)pData, length);

            sRequestMessageInit(pObj, SII9777_CBUSBURST_SEND);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            SiiDrvIpcArrayPush(pObj->mesReqInst, pData, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSBURST_SEND);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusBurstReceive( SiiInst_t inst, uint8_t *poData, uint8_t offset, uint8_t length )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusBurstReceive", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)length));
        if( ((offset + length) > CBUS_DEVCAP_LEN) || (length == 0) || !poData )
        {
            SII_LOG1B(("ERROR: wrong parameter\n"));
            retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
        }
        if( !retVal )
        {
            sRequestMessageInit(pObj, SII9777_CBUSBURST_RECEIVE);
            SiiDrvIpcBytePush(pObj->mesReqInst, offset);
            SiiDrvIpcBytePush(pObj->mesReqInst, length);
            sRequestMessageSend(pObj);

            retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSBURST_RECEIVE);
        }
        if( !retVal )
        {
            SiiDrvIpcArrayPop(pObj->mesAckInst, poData, length);
            SII_LOG1B_ARRAY((uint8_t*)poData, length);
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AvMuteSet( SiiInst_t inst, Sii9777TxPort_t port, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AvMuteSet", pObj, ("port=%d : %d", (uint16_t)port, (uint16_t)*pbOn));

        sRequestMessageInit(pObj, SII9777_AVMUTE_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_AVMUTE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777AvMuteGet( SiiInst_t inst, Sii9777TxPort_t port, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777AvMuteGet", pObj, ("port=%d : ", (uint16_t)port));

        sRequestMessageInit(pObj, SII9777_AVMUTE_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)port);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_AVMUTE_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", *pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777TmdsSwingLevelSet( SiiInst_t inst, Sii9777TxPort_t pPort, int8_t *tmdsSwingLevel )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777TmdsSwingLevelSet", pObj, ("%d", (int16_t)*tmdsSwingLevel));

        sRequestMessageInit(pObj, SII9777_TMDSSWINGLEVEL_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPort);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)*tmdsSwingLevel);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_TMDSSWINGLEVEL_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777TmdsSwingLevelGet( SiiInst_t inst, Sii9777TxPort_t pPort, int8_t *tmdsSwingLevel )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777TmdsSwingLevelGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_TMDSSWINGLEVEL_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPort);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_TMDSSWINGLEVEL_GET);
        if( !retVal )
        {
            *tmdsSwingLevel = (int8_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*tmdsSwingLevel));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777TmdsPinSwapSet( SiiInst_t inst, Sii9777TxPort_t pPort, Sii9777TmdsPinSwap_t* pTmdsPinSwap )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777TmdsSwingLevelSet", pObj, ("%d", (int16_t)*pTmdsPinSwap));

        sRequestMessageInit(pObj, SII9777_TMDSPINSWAP_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPort);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)*pTmdsPinSwap);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_TMDSPINSWAP_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777TmdsPinSwapGet( SiiInst_t inst, Sii9777TxPort_t pPort, Sii9777TmdsPinSwap_t* pTmdsPinSwap )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777TmdsPinSwapGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_TMDSPINSWAP_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPort);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_TMDSPINSWAP_GET);
        if( !retVal )
        {
            *pTmdsPinSwap = (Sii9777TmdsPinSwap_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*pTmdsPinSwap));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777PrimTxPortSet( SiiInst_t inst, Sii9777TxPort_t* pPort )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777PrimTxPortSet", pObj, ("%d", (int16_t)*pPort));

        sRequestMessageInit(pObj, SII9777_PRIMTXPORT_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, *pPort);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_PRIMTXPORT_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777PrimTxPortGet( SiiInst_t inst, Sii9777TxPort_t* pPort )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777PrimTxPortGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_PRIMTXPORT_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_PRIMTXPORT_GET);
        if( !retVal )
        {
            *pPort = (Sii9777TxPort_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*pPort));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777OutputPixelFormatSet( SiiInst_t inst, Sii9777PixFmt_t* pPixFmt )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777OutputPixelFormatSet", pObj, ("\n"));
        SII_LOG1B(("clrFmt   = %d\n", pPixFmt->clrFmt));
        SII_LOG1B(("crSmpl   = %d\n", pPixFmt->crSmpl));
        SII_LOG1B(("bitDepth = %d\n", pPixFmt->bitDepth));

        sRequestMessageInit(pObj, SII9777_OUTPUTPIXELFORMAT_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPixFmt->clrFmt);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPixFmt->crSmpl);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pPixFmt->bitDepth);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_OUTPUTPIXELFORMAT_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777OutputPixelFormatGet( SiiInst_t inst, Sii9777PixFmt_t* pPixFmt )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777OutputPixelFormatGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_OUTPUTPIXELFORMAT_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_OUTPUTPIXELFORMAT_GET);
        if( !retVal )
        {
            pPixFmt->clrFmt   = (Sii9777ClrFmt_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            pPixFmt->crSmpl   = (Sii9777CrSmpl_t)SiiDrvIpcBytePop(pObj->mesAckInst);
            pPixFmt->bitDepth = (Sii9777BitDepth_t)SiiDrvIpcBytePop(pObj->mesAckInst);

            SII_LOG1B(("clrFmt   = %d\n", pPixFmt->clrFmt));
            SII_LOG1B(("crSmpl   = %d\n", pPixFmt->crSmpl));
            SII_LOG1B(("bitDepth = %d\n", pPixFmt->bitDepth));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkLutSet( SiiInst_t inst, Sii9777VidRes_t* pVidRes, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkLutSet", pObj, ("length=%d, formats=", (int16_t)len));

        sRequestMessageInit(pObj, SII9777_DUALLINKLUT_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, len);
        while( len-- )
        {
            SII_LOG1B((" %d", (int16_t)*pVidRes));
            SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)(*pVidRes));
            pVidRes++;
        }
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKLUT_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkLutGet( SiiInst_t inst, Sii9777VidRes_t* pVidRes, uint8_t len )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkLutGet", pObj, ("length=%d, formats=", (int16_t)len));

        sRequestMessageInit(pObj, SII9777_DUALLINKLUT_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, len);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKLUT_GET);
        if( !retVal )
        {
            while( len-- )
            {
                *pVidRes = (Sii9777VidRes_t)SiiDrvIpcBytePop(pObj->mesAckInst);
                SII_LOG1B((" %d", (int16_t)*pVidRes));
                pVidRes++;
            }
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkManualSet( SiiInst_t inst, bool_t* bDualLink )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkManualSet", pObj, ("%d", (int16_t)*bDualLink));

        sRequestMessageInit(pObj, SII9777_DUALLINKMANUAL_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *bDualLink);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKMANUAL_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkManualGet( SiiInst_t inst, bool_t* bDualLink )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkManualGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_DUALLINKMANUAL_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKMANUAL_GET);
        if( !retVal )
        {
            *bDualLink = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*bDualLink));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkAutoEnableSet( SiiInst_t inst, bool_t* bAuto )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkAutoEnableSet", pObj, ("%d", (int16_t)*bAuto));

        sRequestMessageInit(pObj, SII9777_DUALLINKAUTOENABLE_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *bAuto);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKAUTOENABLE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkAutoEnableGet( SiiInst_t inst, bool_t* bAuto )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkAutoEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_DUALLINKAUTOENABLE_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKAUTOENABLE_GET);
        if( !retVal )
        {
            *bAuto = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*bAuto));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkQuery( SiiInst_t inst, bool_t* bDualLink )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_DUALLINK_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINK_QUERY);
        if( !retVal )
        {
            *bDualLink = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*bDualLink));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkSwapEnableSet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkSwapEnableSet", pObj, ("%d", (int16_t)*pbOn));

        sRequestMessageInit(pObj, SII9777_DUALLINKSWAP_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pbOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKSWAP_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777DualLinkSwapEnableGet( SiiInst_t inst, bool_t* pbOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777DualLinkSwapEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_DUALLINKSWAP_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_DUALLINKSWAP_GET);
        if( !retVal )
        {
            *pbOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (int16_t)*pbOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777ArcEnableSet( SiiInst_t inst, bool_t* pOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ArcEnableSet", pObj, ("%d", (uint16_t)*pOn));

        sRequestMessageInit(pObj, SII9777_ARCENABLE_SET);
        SiiDrvIpcBoolPush(pObj->mesReqInst, *pOn);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_ARCENABLE_SET);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777ArcEnableGet( SiiInst_t inst, bool_t* pOn )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ArcEnableGet", pObj, (""));

        sRequestMessageInit(pObj, SII9777_ARCENABLE_GET);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_ARCENABLE_GET);
        if( !retVal )
        {
            *pOn = SiiDrvIpcBoolPop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pOn));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777ScdcRxConfigQuery( SiiInst_t inst, uint8_t*  pScdcCfg )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ScdcRxConfigQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_SCDCRXCONFIG_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_SCDCRXCONFIG_QUERY);
        if( !retVal )
        {
            *pScdcCfg = SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pScdcCfg));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777ScdcTx0ConfigQuery( SiiInst_t inst, uint8_t*  pScdcCfg )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777ScdcTx0ConfigQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_SCDCTX0CONFIG_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_SCDCTX0CONFIG_QUERY);
        if( !retVal )
        {
            *pScdcCfg = SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("%d", (uint16_t)*pScdcCfg));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusEventQuery( SiiInst_t inst, Sii9777CbusEvt_t *poState )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusEventQuery", pObj, (""));

        sRequestMessageInit(pObj, SII9777_CBUSEVENT_QUERY);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSEVENT_QUERY);
        if( !retVal )
        {
            *poState = (Sii9777CbusEvt_t)SiiDrvIpcWordPop(pObj->mesAckInst);
            SII_LOG1B(("%04X", (uint16_t)*poState));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusMscMsgReceive( SiiInst_t inst, Sii9777MscCmd_t* pMscCmd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusMscMsgReceive", pObj, (""));

        sRequestMessageInit(pObj, SII9777_CBUSMSCMSG_RECEIVE);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSMSCMSG_RECEIVE);
        if( !retVal )
        {
            pMscCmd->subCmd    = SiiDrvIpcBytePop(pObj->mesAckInst);
            pMscCmd->codeValue = SiiDrvIpcBytePop(pObj->mesAckInst);
            SII_LOG1B(("Cmd=0x%02X, Code=0x%02X\n", (uint16_t)pMscCmd->subCmd, (uint16_t)pMscCmd->codeValue));
        }
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

SiiRetVal_t Sii9777CbusMscMsgSend( SiiInst_t inst, Sii9777MscCmd_t* pMscCmd )
{
    Obj_t*      pObj   = (Obj_t*)SII_SYS_OBJ_CHECK(sObjListInst, inst);
    SiiRetVal_t retVal = 0;

    retVal = MUTEX_API_TAKE(pObj);
    if( !retVal )
    {
        SII_LOG1A("Sii9777CbusMscMsgSend", pObj, (""));
        SII_LOG1B(("Cmd=0x%02X, Code=0x%02X\n", (uint16_t)pMscCmd->subCmd, (uint16_t)pMscCmd->codeValue));

        sRequestMessageInit(pObj, SII9777_CBUSMSCMSG_SEND);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pMscCmd->subCmd);
        SiiDrvIpcBytePush(pObj->mesReqInst, (uint8_t)pMscCmd->codeValue);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSMSCMSG_SEND);
    }
    MUTEX_API_GIVE(pObj);
    RETURN_API(retVal);
}

/***** local functions *******************************************************/

static SiiRetVal_t sCbusLocalDevcapSet( Obj_t* pObj, uint8_t *pData, uint8_t offset, uint8_t len )
{
    SiiRetVal_t retVal = 0;

    SII_LOG2A("Sii9777CbusLocalDevcapSet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)len));
    if( ((offset + len) > CBUS_DEVCAP_LEN) || (len == 0) || !pData )
    {
        SII_LOG2B(("ERROR: wrong parameter\n"));
        retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
    }

    if( !retVal )
    {
        SII_LOG2B_ARRAY((uint8_t*)pData, len);

        sRequestMessageInit(pObj, SII9777_CBUSLOCALDEVCAP_SET);
        SiiDrvIpcBytePush(pObj->mesReqInst, offset);
        SiiDrvIpcBytePush(pObj->mesReqInst, len);
        SiiDrvIpcArrayPush(pObj->mesReqInst, pData, len);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSLOCALDEVCAP_SET);
    }
    return retVal;
}

static SiiRetVal_t sCbusLocalDevcapGet( Obj_t* pObj, uint8_t *poData, uint8_t offset, uint8_t len )
{
    SiiRetVal_t retVal = 0;

    SII_LOG2A("Sii9777CbusLocalDevcapGet", pObj, ("offset=%d, length=%d : ", (uint16_t)offset, (uint16_t)len));
    if( ((offset + len) > CBUS_DEVCAP_LEN) || (len == 0) || !poData )
    {
        SII_LOG2B(("ERROR: wrong parameter\n"));
        retVal = SII9777_RETVAL_ERR__PARAMETER; // error happen
    }
    if( !retVal )
    {
        sRequestMessageInit(pObj, SII9777_CBUSLOCALDEVCAP_GET);
        SiiDrvIpcBytePush(pObj->mesReqInst, offset);
        SiiDrvIpcBytePush(pObj->mesReqInst, len);
        sRequestMessageSend(pObj);

        retVal = sWaitForValidIpcResponseMessage(pObj, SII9777_CBUSLOCALDEVCAP_GET);
    }
    if( !retVal )
    {
        SiiDrvIpcArrayPop(pObj->mesAckInst, poData, len);
        SII_LOG2B_ARRAY((uint8_t*)poData, len);
    }
    return retVal;
}

/***** end of file ***********************************************************/
