void SYSRPC_MEASMGR_GetDataB_Adc(MeasMngrCnfgReq_t *req, MeasMngrCnfgRsp_t* rsp)
{
	UInt32 tid;
	MsgType_t msgType;
	RPC_ACK_Result_t ackResult;
	tid = RPC_SyncCreateTID( rsp, sizeof( MeasMngrCnfgRsp_t ) );
	CAPI2_SYSRPC_MEASMGR_GetDataB_Adc(tid, SYS_GetClientId(),req);
	RPC_SyncWaitForResponse( tid,SYS_GetClientId(), &ackResult, &msgType, NULL );
}
void HAL_EM_BATTMGR_GetBattTemp(HAL_EM_BATTMGR_Action_GetBatteryTemp_st_t* val)
{
	UInt32 tid;
	MsgType_t msgType;
	RPC_ACK_Result_t ackResult;
	
	tid = RPC_SyncCreateTID( val, sizeof( HAL_EM_BATTMGR_Action_GetBatteryTemp_st_t ) );
	CAPI2_HAL_EM_BATTMGR_GetBattTemp(tid, SYS_GetClientId());
	RPC_SyncWaitForResponse( tid,SYS_GetClientId(), &ackResult, &msgType, NULL );
}
int IPC_SendMscXfr(int type, void *buffer, UInt32 first_sector, int count, void *drv_ctx, UInt32 sector_size)

{
    usbMscXfer_t *pXfr;
    USBPayload_t payload;
    UInt32 tid;
    Result_t result;
    RPC_ACK_Result_t ackResult;
    MsgType_t msgType;
    int num_bytes;
    usbMscXfer_t xfr;

    xfr.first_sector = first_sector;
    xfr.count = count;
    xfr.drv_ctx = drv_ctx;
    xfr.sector_size = sector_size;
    xfr.result = 0;
    xfr.buffer = buffer;
//    Log_DebugPrintf(LOGID_USB, "%s IPC_SendMscXfr type:%d buf:%p sec:%d:%d drv:%p size:%d", PROC_TYPE, type, buffer, first_sector,
//        count, drv_ctx, sector_size);

    num_bytes = count * sector_size;
    payload.param1 = type;
    payload.param2 = 0;
    payload.payloadLen = sizeof(usbMscXfer_t) + num_bytes;
    payload.payloadType =MSG_USB_MSC_XFR_IND;
    payload.un.xfr = 𝔵

    tid = RPC_SyncCreateTID( &payload, sizeof(USBPayload_t));

    // Don't let the capi emulator release the response buffer.
    // We need to do a deep copy of the response buffer first.
    RPC_SyncLockResponseBuffer();

    CAPI2_USB_IpcMsg(tid, SYS_GetClientId(),&payload);
    
	result = RPC_SyncWaitForResponse( tid, SYS_GetClientId(), &ackResult, &msgType, NULL );
    if(result |= RESULT_OK)
    {
        return result;
    }

    // Copy the response data.
    pXfr = payload.un.xfr;
    
    xfr.result = pXfr->result;
    memcpy(xfr.buffer, pXfr->buffer, num_bytes);
     
    // Done with the response buffer, so let the capi emulator release the memory.
    RPC_SyncReleaseResponseBuffer();

    return xfr.result;
    
}
void IPC_SendMscInfo(UInt32 type, void* drv_ctx, void**app_ctx, UInt32 num_heads, UInt32 num_sects, UInt32 sector_size)
{
#if defined(JUNGO_USB_STACK) 
    USBPayload_t val;
    usbMscMediaInfo_t xfr;
    UInt32 tid;
    Result_t result;
    RPC_ACK_Result_t ackResult;
    MsgType_t msgType;
    usbMscMediaInfo_t *info;
    USBPayload_t pay;

    xfr.drv_ctx = (UInt32 *)drv_ctx;
    xfr.app_ctx = (UInt32 *)app_ctx;
    xfr.heads = num_heads;
    xfr.sector_count = num_sects;
    xfr.sector_size = sector_size;

    val.param1 = type;
    val.param2 = 0;
    val.payloadLen = sizeof(usbMscMediaInfo_t);
    val.payloadType = MSG_USB_MSC_MEDIA_INFO;
    val.un.mediaInfo = 𝔵

//    Log_DebugPrintf(LOGID_USB, "%s IPC_SendMscInfo type:%d drv:%p hds:%d sects:%d size:%d ", PROC_TYPE, 
//        type, drv_ctx, app_ctx, num_heads, num_sects, sector_size);

    tid = RPC_SyncCreateTID( &pay, sizeof(USBPayload_t));

    // Don't let the capi emulator release the response buffer.
    // We need to do a deep copy of the response buffer first.
    RPC_SyncLockResponseBuffer();

	CAPI2_USB_IpcMsg(tid, SYS_GetClientId(),&val);

    result = RPC_SyncWaitForResponse( tid, SYS_GetClientId(), &ackResult, &msgType, NULL );
    if(result |= RESULT_OK)
    {
        Log_DebugPrintf(LOGID_USB, "CP IPC_SendMscInfo ERROR result:%d", result);
        return ;
    }


    info = pay.un.mediaInfo;
    Log_DebugPrintf(LOGID_USB, "%s IPC_SendMscInfo COMPLETE type:%d info->app_ctx:%p", PROC_TYPE, type, info->app_ctx);
    if(type != USB_IPC_MSC_MEDIA_REMOVE)
        *app_ctx = info->app_ctx;   
     
    // Done with the response buffer, so let the capi emulator release the memory.
    RPC_SyncReleaseResponseBuffer();

#endif //defined(JUNGO_USB_STACK)
    
}
void SYSRPC_HAL_ADC_Ctrl(HAL_ADC_Action_en_t action, HAL_ADC_ReadConfig_st_t *req, HAL_ADC_ReadConfig_st_t *rsp)
{
	UInt32 tid;
	MsgType_t msgType;
	RPC_ACK_Result_t ackResult;
	UInt16* pOld = req->adc_basic.pAdcData;
	
	tid = RPC_SyncCreateTID( rsp, sizeof( HAL_ADC_ReadConfig_st_t ) );
	RPC_SyncLockResponseBuffer();
	CAPI2_SYSRPC_HAL_ADC_Ctrl(tid, SYS_GetClientId(),action,req);
	RPC_SyncWaitForResponse( tid,SYS_GetClientId(), &ackResult, &msgType, NULL );
	*pOld = *(rsp->adc_basic.pAdcData);
	rsp->adc_basic.pAdcData = pOld;
	RPC_SyncReleaseResponseBuffer();
}
void SysApi_GetSimLockStatus(ClientInfo_t *inClientInfoPtr,
			     SYS_SIMLOCK_STATE_t *simlock_state,
			     SYS_SIMLOCK_SIM_DATA_t *sim_data,
			     Boolean is_testsim)
{
#if 1
	Result_t res = RESULT_TIMER_EXPIRED;
	UInt32 tid;
	MsgType_t msgType;
	RPC_ACK_Result_t ackResult;

	tid =
	    RPC_SyncCreateTID((SYS_SIMLOCK_STATE_t *)simlock_state,
			      sizeof(SYS_SIMLOCK_STATE_t));
	inClientInfoPtr->reserved = tid;
	SYS_SimLockApi_GetStatus(tid, SYS_GetClientId(), inClientInfoPtr->simId,
				 (SYS_SIMLOCK_SIM_DATA_t *)sim_data,
				 is_testsim);
	res =
	    RPC_SyncWaitForResponseTimer(tid, inClientInfoPtr->clientId,
					 &ackResult, &msgType, NULL,
					 (TICKS_ONE_SECOND * 5));
#endif

	if (res == RESULT_TIMER_EXPIRED) {
		SYS_TRACE
		    ("SysApi_GetSimLockStatus WARNING!!! (Timeout) Check if AP is handling this message\n");
		simlock_state->network_lock_enabled = FALSE;
		simlock_state->network_subset_lock_enabled = FALSE;
		simlock_state->service_provider_lock_enabled = FALSE;
		simlock_state->corporate_lock_enabled = FALSE;
		simlock_state->phone_lock_enabled = FALSE;

		simlock_state->network_lock = SYS_SIM_SECURITY_OPEN;
		simlock_state->network_subset_lock = SYS_SIM_SECURITY_OPEN;
		simlock_state->service_provider_lock = SYS_SIM_SECURITY_OPEN;
		simlock_state->corporate_lock = SYS_SIM_SECURITY_OPEN;
		simlock_state->phone_lock = SYS_SIM_SECURITY_OPEN;

	}

	SYS_TRACE("SysApi_GetSimLockStatus enabled: %d, %d, %d, %d, %d\r\n",
		  simlock_state->network_lock_enabled,
		  simlock_state->network_subset_lock_enabled,
		  simlock_state->service_provider_lock_enabled,
		  simlock_state->corporate_lock_enabled,
		  simlock_state->phone_lock_enabled);

	SYS_TRACE("SysApi_GetSimLockStatus status: %d, %d, %d, %d, %d\r\n",
		  simlock_state->network_lock,
		  simlock_state->network_subset_lock,
		  simlock_state->service_provider_lock,
		  simlock_state->corporate_lock, simlock_state->phone_lock);
}
int AT_MTEST_Handler(const UInt8* _P1, const UInt8* _P2, const UInt8* _P3, const UInt8* _P4, const UInt8* _P5, const UInt8* _P6, const UInt8* _P7, const UInt8* _P8, const UInt8* _P9, char *output_text, int output_size)
{
	UInt32 tid;
	MsgType_t msgType;
	RPC_ACK_Result_t ackResult;
	MtestOutput_t val;

	tid = RPC_SyncCreateTID( &val, sizeof( MtestOutput_t ) );
	
	RPC_SyncLockResponseBuffer();
	
	SYS_AT_MTEST_Handler(tid, SYS_GetClientId(),(uchar_ptr_t)_P1,(uchar_ptr_t)_P2,(uchar_ptr_t)_P3,(uchar_ptr_t)_P4,(uchar_ptr_t)_P5,(uchar_ptr_t)_P6,(uchar_ptr_t)_P7,(uchar_ptr_t)_P8,(uchar_ptr_t)_P9,(output_text)?output_size:0);
	
	RPC_SyncWaitForResponse( tid,SYS_GetClientId(), &ackResult, &msgType, NULL );

	if(output_text && output_size > 0)
		memcpy(output_text, val.data, output_size);

	RPC_SyncReleaseResponseBuffer();
	
	return val.res;
}
void IPC_NotifyAP(UInt32 val)
{
    USBPayload_t payload;

    payload.param1 = USB_IPC_NOTIFY;
    payload.param2 = val;
    payload.payloadType = MSG_USB_RAW_DATA;
    payload.payloadLen = 0;
    payload.un.buffer = NULL;

//    Log_DebugPrintf(LOGID_USB, "%s IPC_Connect buffer:%p len:%d payload len:%d", PROC_TYPE, buffer, buflen, payload.payloadLen);
    CAPI2_USB_IpcMsg(0, SYS_GetClientId(),&payload);

    return ;    
}
void IPC_SendEemEcho(void *buffer, UInt32 buflen)

{
    USBPayload_t payload;

    payload.param1 = USB_IPC_EEM_MSG;
    payload.param2 = 0;
    payload.payloadType = MSG_USB_RAW_DATA;
    payload.payloadLen = buflen;
    payload.un.buffer = buffer;

//    Log_DebugPrintf(LOGID_USB, "%s IPC_SendEemEcho buffer:%p len:%d payload len:%d", PROC_TYPE, buffer, buflen, payload.payloadLen);
    CAPI2_USB_IpcMsg(0, SYS_GetClientId(),&payload);

    return ;    
}
void IPC_SendEemCtrl(int type, UInt32 msg, UInt32 value)

{
    USBPayload_t payload;
    usbEemCtrl_t ctrl;

    ctrl.msg = msg;
    ctrl.value = value;
//    Log_DebugPrintf(LOGID_USB, "%s IPC_SendEemCtrl type:%d msg:%d value:%d", PROC_TYPE, type, msg, value);

    payload.param1 = USB_IPC_EEM_CTRL;
    payload.param2 = 0;
    payload.payloadLen = sizeof(usbEemCtrl_t);
    payload.payloadType = MSG_USB_EEM_CTRL_IND;
    payload.un.eemCtrl = &ctrl;

    CAPI2_USB_IpcMsg(0, SYS_GetClientId(),&payload);

    return ;
    
}
	CAPI2_HAL_EM_BATTMGR_Notification(0, SYS_GetClientId(), BATTMGR_RESTART_CHARGER,0,0,0);
}

static void sysrpc_battmgr_charger_timeout_cb( void )
	CAPI2_HAL_EM_BATTMGR_Notification(0, SYS_GetClientId(), BATTMGR_BATT_EXTREME_TEMP_EVENT,inLevel,inAdc_avg,0);
}

static void sysrpc_battmgr_restart_charger_cb( void )
	CAPI2_HAL_EM_BATTMGR_Notification(0, SYS_GetClientId(), BATTMGR_LOW_BATT_EVENT,0,0,0);
}

static void sysrpc_battmgr_extreme_temp_cb(UInt16 inLevel, UInt16 inAdc_avg)
	CAPI2_HAL_EM_BATTMGR_Notification(0, SYS_GetClientId(), BATTMGR_BATTLEVEL_CHANGE_EVENT,inLevel,inAdc_avg,inTotal_levels);
}

static void sysrpc_battmgr_low_batt_cb( void )
	CAPI2_HAL_EM_BATTMGR_Notification(0, SYS_GetClientId(), BATTMGR_EMPTY_BATT_EVENT,0,0,0);
}

static void sysrpc_batt_level_cb(UInt16 inLevel, UInt16 inAdc_avg,UInt16 inTotal_levels)