//******************************************************************************
//
// Function Name: STK_ParseEventList
//
// Description:   Parse Event List(refer to 11.14 section 12.25)
//
// Notes:
//
//******************************************************************************
int STK_ParseEventList(SimNumber_t SimId, UInt8 *byte)
{    
    // Check Event List tag
    if (!(byte[0] == 0x19 || byte[0] == 0x99))
    {
        KRIL_DEBUG(DBG_ERROR,"Error Event List tag:0x%X\n", byte[0]);
        return 0;
    }        
    
    if (0 == byte[1])
    {
        KRIL_DEBUG(DBG_ERROR,"Event list length is 0 Error!!!\n");
        return 0;
    }
    
    KRIL_DEBUG(DBG_INFO,"Event Id:%d\n",byte[2]);

    switch (byte[2])
    {
        case 4:
        {
           // User activity
	         CAPI2_SatkApi_SendUserActivityEvent (InitClientInfo(SimId));
	         break;       
        }
    
        case 5:
        {
            // Idle screen available
	          CAPI2_SatkApi_SendIdleScreenAvaiEvent (InitClientInfo(SimId));            
            break;
        }
        
        case 7:
        {
            // Language selection
            if (!STK_ParseLanguageSelection(SimId, &byte[7]))
                return 0;
                
            break;
        }
        
        
        default:
            KRIL_DEBUG(DBG_ERROR,"Unknow Enevt ID:%d\n", byte[2]);
            return 0;
    }

    
    return 1;
}
//RIL_REQUEST_AGPS_CLOSE_PKCS15
void KRIL_AgpsClosePkcs15Handler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
	KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;
	unsigned char *pSockId = (unsigned char *)pdata->ril_cmd->data;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

			KRIL_DEBUG(DBG_ERROR, "KRIL_AgpsClosePkcs15Handler, sockID=%d \n", (unsigned char)(*pSockId));
			CAPI2_SimApi_SendDeactivateAppiReq(pLcsClientInfo, (UInt8) (*pSockId));
			pdata->handler_state = BCM_AGPS_DESELECT_PKCS15;
			break;
		}
		case BCM_AGPS_DESELECT_PKCS15:
		{
			//MsgType_t: ::MSG_SIM_DEACTIVATE_APPLI_RSP
			//ResultData: ::SIM_DEACTIVATE_APPLI_RES_t
			SIM_DEACTIVATE_APPLI_RES_t * rsp = (SIM_DEACTIVATE_APPLI_RES_t *) capi2_rsp->dataBuf;
			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps SendDeactivateAppiReq, result=%d \n", rsp->result);
			CAPI2_SimApi_SendCloseSocketReq(pLcsClientInfo, (UInt8) (*pSockId));
			pdata->handler_state = BCM_RESPCAPI2Cmd;
			break;
		}

		case BCM_RESPCAPI2Cmd:
		{
			//MsgType_t: ::MSG_SIM_CLOSE_SOCKET_RSP
			//ResultData: ::SIM_CLOSE_SOCKET_RES_t

			SIM_CLOSE_SOCKET_RES_t * rsp = (SIM_CLOSE_SOCKET_RES_t *) capi2_rsp->dataBuf;

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps SendCloseSocketReq, result=%d \n", rsp->result);
			pdata->handler_state = BCM_FinishCAPI2Cmd;
			break;
		}
		default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
	}
}
//******************************************************************************
//
// Function Name: STK_SelectItemRsp
//
// Description:   Handle Select Item response
//
// Notes:
//
//******************************************************************************
int STK_SelectItemRsp(SimNumber_t SimId, UInt8 *byte, UInt8 cmdlen)
{
    SATK_ResultCode_t resultcode = SATK_Result_CmdSuccess;
    SATK_ResultCode2_t resultcode2 = SATK_Result_NoCause;
    UInt8 itemId = 0;
    
    // Parse device identities
    if (!STK_ParseDeviceIdentities(&byte[5]))
        return 0;
       
    // Parse Result
    if (!STK_ParseResult(&byte[9], &resultcode, &resultcode2))
        return 0;
    
    cmdlen -= (STK_COMMAND_DETAILS_LEN + STK_DEVICE_IDENTITIES_LEN + STK_RESULT_LEN + (byte[10] - 1));
    byte += (STK_COMMAND_DETAILS_LEN + STK_DEVICE_IDENTITIES_LEN + STK_RESULT_LEN + (byte[10] - 1));
    KRIL_DEBUG(DBG_INFO,"After parsing result: byte[0]:0x%02X cmdlen:%d\n", byte[0], cmdlen);
    
    // Parse Item identifier
    if (cmdlen > 0)
    {
        if (!STK_ParseItemIdentifier(byte, &itemId))
            return 0;
    }
    
    CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_SELECT_ITEM, resultcode, resultcode2, NULL, itemId);
    
    return 1;
}
//******************************************************************************
//
// Function Name: KRIL_StkService_Running
//
// Description:   
// Notes:
//
//******************************************************************************
void KRIL_StkService_Running(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)  // gearn fix java sim card 
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t*)ril_cmd;

    KRIL_DEBUG(DBG_INFO,"pdata->handler_state:0x%lX\n", pdata->handler_state);
    
    if (capi2_rsp && capi2_rsp->result != RESULT_OK)
    {
        KRIL_DEBUG(DBG_ERROR,"CAPI2 response failed:%d\n", capi2_rsp->result);
        pdata->handler_state = BCM_ErrorCAPI2Cmd;
        return;
    }
    
    switch (pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
            CAPI2_SatkApi_SendTerminalProfileReq (InitClientInfo(pdata->ril_cmd->SimId),17,  terminal_profile_data);
            pdata->handler_state = BCM_RESPCAPI2Cmd;
        }    
        
        case BCM_RESPCAPI2Cmd:
            pdata->result = BCM_E_SUCCESS;
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
                    
        default:
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;        
        
    }    
}
//******************************************************************************
//
// Function Name: KRIL_StkHandleCallSetupRequestedHandler
//
// Description:   
// Notes:
//
//******************************************************************************
void KRIL_StkHandleCallSetupRequestedHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t*)ril_cmd;

    KRIL_DEBUG(DBG_INFO,"pdata->handler_state:0x%lX\n", pdata->handler_state);
    
    if (capi2_rsp && capi2_rsp->result != RESULT_OK)
    {
        KRIL_DEBUG(DBG_ERROR,"CAPI2 response failed:%d\n", capi2_rsp->result);
        pdata->handler_state = BCM_ErrorCAPI2Cmd;
        return;
    }
    
    switch (pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
            int *accept = (int*)pdata->ril_cmd->data;
            
            KRIL_DEBUG(DBG_INFO,"accept:%d\n", *accept);
            if (*accept)
                CAPI2_SatkApi_CmdResp(InitClientInfo(pdata->ril_cmd->SimId), SATK_EVENT_SETUP_CALL, SATK_Result_CmdSuccess, 0, NULL, 0);
                //CAPI2_SATK_SendSetupCallRes(GetNewTID(), GetClientID(), SATK_Result_CmdSuccess);
            else
                CAPI2_SatkApi_CmdResp(InitClientInfo(pdata->ril_cmd->SimId), SATK_EVENT_SETUP_CALL, SATK_Result_UserNotAcceptingCallSetup, 0, NULL, 0);
                //CAPI2_SATK_SendSetupCallRes(GetNewTID(), GetClientID(), SATK_Result_UserNotAcceptingCallSetup);
            
            pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;
        }

        case BCM_RESPCAPI2Cmd:
            pdata->result = BCM_E_SUCCESS;
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
                    
        default:
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
    } 
}
void KRIL_SRIL_requestOemSimOut(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
	KRIL_CmdList_t *pdata = (KRIL_CmdList_t*)ril_cmd;
	KRIL_DEBUG(DBG_ERROR,"KRIL_SRIL_requestOemSimOut \n");

	switch (pdata->handler_state)
	{
		case BCM_SendCAPI2Cmd:
		{
			ClientInfo_t clientInfo;
			CAPI2_InitClientInfo(&clientInfo, GetNewTID(), GetClientID());
			clientInfo.simId = pdata->ril_cmd->SimId;

                    if(clientInfo.simId == SIM_ALL)
                   {

                          KRIL_DEBUG(DBG_ERROR,"KRIL_SRIL_requestOemSimOut SIM_ALL \n");

			CAPI2_SimApi_PowerOnOffCard (InitClientInfo(SIM_DUAL_FIRST), FALSE, SIM_POWER_ON_INVALID_MODE);
			CAPI2_SimApi_PowerOnOffCard (InitClientInfo(SIM_DUAL_SECOND), FALSE, SIM_POWER_ON_INVALID_MODE);
                   }
                    else
                    {
                    
                        KRIL_DEBUG(DBG_ERROR,"KRIL_SRIL_requestOemSimOut SIM_SINGLE \n");
                        CAPI2_SimApi_PowerOnOffCard (InitClientInfo(SIM_DUAL_FIRST), FALSE, SIM_POWER_ON_INVALID_MODE);
                    }
			pdata->handler_state = BCM_FinishCAPI2Cmd;
		}
		break;

		default:
		{
			KRIL_DEBUG(DBG_ERROR, "handler_state:%lu error...!\n", pdata->handler_state);
			pdata->handler_state = BCM_ErrorCAPI2Cmd;
		}
		break;
	}
	
	return;
}
//******************************************************************************
//
// Function Name: STK_MenuSelection
//
// Description:   Handle Menu Selection
// Notes:
//
//******************************************************************************
int STK_MenuSelection(SimNumber_t SimId, UInt8 *envelopeCmd)
{
    UInt8 length;
    UInt8 itemId = 0;
    UInt8 helpRequest = 0;
    
    // Check length
    length = envelopeCmd[1];

    // Parse device identities
    if (!STK_ParseDeviceIdentities(&envelopeCmd[2]))
        return 0;    
    
    // Parse item identifier
    if (!STK_ParseItemIdentifier(&envelopeCmd[6], &itemId))
        return 0;
            
    // Check help request
    if (length > 7)
    {
        if (envelopeCmd[9] == 0x15 || envelopeCmd[9] == 0x95)
        {
            KRIL_DEBUG(DBG_ERROR,"Request help information\n");
            helpRequest = 1;
        }
        else
        {
            KRIL_DEBUG(DBG_ERROR,"Error help request tag:0x%X\n",envelopeCmd[9]);
            return 0;
        }
    }
    
    if (helpRequest)
        CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_MENU_SELECTION, 1, 0, NULL, itemId);
    else
        CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_MENU_SELECTION, SATK_Result_CmdSuccess, 0, NULL, itemId);
    
    return 1;
}
//******************************************************************************
//
// Function Name: STK_GetInkeyRsp
//
// Description:   Handle Get Inkey response
//
// Notes:
//
//******************************************************************************
int STK_GetInkeyRsp(SimNumber_t SimId, UInt8 *byte, UInt8 cmdlen)
{
    SATK_ResultCode_t resultcode = SATK_Result_CmdSuccess;
    SATK_ResultCode2_t resultcode2 = SATK_Result_NoCause;
    SATKString_t intext;
    UInt8   string[255];
    
    memset(&intext, 0, sizeof(SATKString_t));
    
    // Parse device identities
    if (!STK_ParseDeviceIdentities(&byte[5]))
        return 0;
       
    // Parse Result
    if (!STK_ParseResult(&byte[9], &resultcode, &resultcode2))
        return 0;
    
    cmdlen -= (STK_COMMAND_DETAILS_LEN + STK_DEVICE_IDENTITIES_LEN + STK_RESULT_LEN + (byte[10] - 1));
    byte += (STK_COMMAND_DETAILS_LEN + STK_DEVICE_IDENTITIES_LEN + STK_RESULT_LEN + (byte[10] - 1));
    KRIL_DEBUG(DBG_INFO,"After parsing result: byte[0]:0x%02X cmdlen:%d\n", byte[0], cmdlen);
    
    // Parse Text string
    if (cmdlen > 0)
    {
        intext.string = string;
        if (!STK_ParseTextString(byte, &intext))
            return 0;
    }
    
    if (intext.len > 0)
        CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_GET_INKEY, resultcode, resultcode2, &intext, 0);
    else
        CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_GET_INKEY, resultcode, resultcode2, NULL, 0);
    
    return 1;    
}
//******************************************************************************
//
// Function Name: STK_PlayToneRsp
//
// Description:   Handle Play Tone response
//
// Notes:
//
//******************************************************************************
int STK_PlayToneRsp(SimNumber_t SimId, UInt8 *byte, UInt8 cmdlen)
{
    SATK_ResultCode_t resultcode = SATK_Result_CmdSuccess;
    SATK_ResultCode2_t resultcode2 = SATK_Result_NoCause;

    // Parse device identities
    if (!STK_ParseDeviceIdentities(&byte[5]))
        return 0;
       
    // Parse Result
    if (!STK_ParseResult(&byte[9], &resultcode, &resultcode2))
        return 0;
    
    CAPI2_SatkApi_SendPlayToneRes(InitClientInfo(SimId), resultcode);
    return 1;
}
//******************************************************************************
//
// Function Name: STK_ParseLanguageSelection
//
// Description:   Parse Language selection event(refer to 11.14 section 12.45)
//
// Notes:
//
//******************************************************************************
int STK_ParseLanguageSelection(SimNumber_t SimId, UInt8 *byte)
{
    UInt16 language;
    
    // Check Language Selection tag
    if (!(byte[0] == 0x2D || byte[0] == 0xAD))
    {
        KRIL_DEBUG(DBG_ERROR,"Error Language Selection tag:0x%X\n", byte[0]);
        return 0;
    }
    
    language =  byte[2] << 8 | byte[3];
    
    CAPI2_SatkApi_SendLangSelectEvent (InitClientInfo(SimId), language);
    return 1;
}
//******************************************************************************
//
// Function Name: STK_SendSMSRsp
//
// Description:   Handle Send SMS response
//
// Notes:
//
//******************************************************************************
int STK_SendSMSRsp(SimNumber_t SimId, UInt8 *byte, UInt8 cmdlen)
{
    SATK_ResultCode_t resultcode = SATK_Result_CmdSuccess;
    SATK_ResultCode2_t resultcode2 = SATK_Result_NoCause;

    // Parse device identities
    if (!STK_ParseDeviceIdentities(&byte[5]))
        return 0;
       
    // Parse Result
    if (!STK_ParseResult(&byte[9], &resultcode, &resultcode2))
        return 0;
    
    CAPI2_SatkApi_CmdResp(InitClientInfo(SimId), SATK_EVENT_SEND_SHORT_MSG, resultcode, resultcode2, NULL, 0);
    return 1;    
}
//******************************************************************************
//
// Function Name: KRIL_StkSetProfile
//
// Description:   
// Notes:
//
//******************************************************************************
void KRIL_StkSetProfile(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t*)ril_cmd;

    KRIL_DEBUG(DBG_INFO,"pdata->handler_state:0x%lX\n", pdata->handler_state);
    
    if (capi2_rsp && capi2_rsp->result != RESULT_OK)
    {
        KRIL_DEBUG(DBG_ERROR,"CAPI2 response failed:%d\n", capi2_rsp->result);
        pdata->handler_state = BCM_ErrorCAPI2Cmd;
        return;
    }
    
    switch (pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
            UInt8 *stkprofile = (UInt8*)pdata->ril_cmd->data;
    
            RawDataPrintfun(stkprofile, pdata->ril_cmd->datalen, "stkprofile");
            
            // Update terminal_profile_data[]
            memcpy(terminal_profile_data, stkprofile, sizeof(terminal_profile_data)/sizeof(UInt8));
            
            CAPI2_SatkApi_SetTermProfile(InitClientInfo(pdata->ril_cmd->SimId), stkprofile,
                pdata->ril_cmd->datalen);
            
            pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;            
        }    
        
        case BCM_RESPCAPI2Cmd:
            pdata->result = BCM_E_SUCCESS;
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
                    
        default:
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;        
        
    }    
}
void KRIL_AgpsRrcMcFailureHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			AgpsCp_McFailure *tdata = (AgpsCp_McFailure *)pdata->ril_cmd->data;
			//LcsClientInfo_t lcsClientInfo = {0};
			ClientInfo_t* lcsClientInfo;
			lcsClientInfo=InitClientInfo(pdata->ril_cmd->SimId);

			//CAPI2_LCS_RrcMeasurementControlFailure(GetNewTID(), GetClientID(), lcsClientInfo, tdata->transId, KRIL_Cp2LcsRrcMcFailure(tdata->failureCode), 0);				
			CAPI2_LcsApi_RrcMeasCtrlFailure(lcsClientInfo, tdata->transId, KRIL_Cp2LcsRrcMcFailure(tdata->failureCode), 0);

            KRIL_DEBUG(DBG_ERROR,"transId:%d\n", tdata->transId);
            pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;
        }

        case BCM_RESPCAPI2Cmd:
        {
            KRIL_DEBUG(DBG_ERROR, "BCM_RESPCAPI2Cmd\n");
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
        }

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
void KRIL_AgpsRrcStatusHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			AgpsCp_RrcStatus *tdata = (AgpsCp_RrcStatus *)pdata->ril_cmd->data;
			//LcsClientInfo_t lcsClientInfo = {0};
			ClientInfo_t* lcsClientInfo;
			lcsClientInfo=InitClientInfo(pdata->ril_cmd->SimId);

			CAPI2_LcsApi_RrcStatus(lcsClientInfo, KRIL_Cp2LcsRrcStatus(*tdata));
			//CAPI2_LCS_RrcStatus(GetNewTID(), GetClientID(), lcsClientInfo, KRIL_Cp2LcsRrcStatus(*tdata));
            pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;
        }

        case BCM_RESPCAPI2Cmd:
        {
            KRIL_DEBUG(DBG_ERROR, "BCM_RESPCAPI2Cmd\n");
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
        }

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
void KRIL_AgpsSimEfHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			const UInt16 mfPath = APDUFILEID_MF;
			AgpsEfReadReq *tdata = (AgpsEfReadReq *)pdata->ril_cmd->data;

			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps calls EFileReadReq, appPath=0x%x, subPath=0x%x, idx=%d, len=%d\n", tdata->appPath,tdata->subPath, tdata->idx, tdata->len);

			if(tdata->len == 0)
			{
				CAPI2_SimApi_SubmitWholeBinaryEFileReadReq( pLcsClientInfo, tdata->pkcs15Hdl, 
					(APDUFileID_t) tdata->subPath, (APDUFileID_t) tdata->appPath, 1, &mfPath);
			}
			else
			{
				CAPI2_SimApi_SubmitBinaryEFileReadReq( pLcsClientInfo, tdata->pkcs15Hdl, 
					(APDUFileID_t) tdata->subPath, (APDUFileID_t) tdata->appPath, tdata->idx, tdata->len, 
								  1, &mfPath);
			}            
			pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;
        }
		case BCM_RESPCAPI2Cmd:
		{
			//capi2_rsp->msgType == MSG_SIM_EFILE_DATA_RSP
			// payload is SIM_EFILE_DATA_t
			SIM_EFILE_DATA_t *rsp = (SIM_EFILE_DATA_t *) capi2_rsp->dataBuf;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps EFileInfoRes result=%d, resLen=%d\n", rsp->result, rsp->data_len);
            if ((rsp->result == SIMACCESS_SUCCESS) && (rsp->data_len <= BRCM_AGPS_MAX_MESSAGE_SIZE))
            {
				AgpsEfData * pEfInfo = NULL;

                pdata->bcm_ril_rsp = kmalloc(sizeof(AgpsEfData), GFP_KERNEL);
                if(!pdata->bcm_ril_rsp) {
                    KRIL_DEBUG(DBG_ERROR, "unable to allocate bcm_ril_rsp buf\n");                
                    pdata->handler_state = BCM_ErrorCAPI2Cmd;             
                    return;
                }
                pdata->rsp_len = sizeof(AgpsEfData);
                pEfInfo = (AgpsEfData*)pdata->bcm_ril_rsp;
                pEfInfo->dataLen = rsp->data_len;
				memcpy(pEfInfo->data, rsp->ptr,  rsp->data_len);

                pdata->handler_state = BCM_FinishCAPI2Cmd;
			}
			else
			{
				//2G case
				KRIL_DEBUG(DBG_INFO, "error result: %d\n", rsp->result);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
void KRIL_AgpsSimRecHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			AgpsEfRecReq *tdata = (AgpsEfRecReq *)pdata->ril_cmd->data;

			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps calls RecReadReq, path=0x%x, idx=%d, recLen=%d, numOfRec=%d\n", tdata->path, tdata->recIdx, tdata->recLen, tdata->numOfRec);

			//CAPI2_SimApi_SubmitRecordEFileReadReq( pLcsClientInfo, 0, (APDUFileID_t)tdata->path, (APDUFileID_t) 0x3F00, tdata->recIdx, tdata->recLen, 0, NULL);

			CAPI2_SimApi_SubmitMulRecordEFileReq( pLcsClientInfo, 0, (APDUFileID_t)tdata->path, (APDUFileID_t) 0x3F00, 
				tdata->recIdx, tdata->numOfRec, tdata->recLen, 0, NULL, FALSE, FALSE);
            
			pdata->handler_state = BCM_RESPCAPI2Cmd;
            break;
        }
		case BCM_RESPCAPI2Cmd:
		{
			//capi2_rsp->msgType == MSG_SIM_EFILE_DATA_RSP
			// payload is SIM_EFILE_DATA_t
			/*
			SIM_EFILE_DATA_t *rsp = (SIM_EFILE_DATA_t *) capi2_rsp->dataBuf;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps EFileInfoRes result=%d, resLen=%d\n", rsp->result, rsp->data_len);
            if ((rsp->result == SIMACCESS_SUCCESS) && (rsp->data_len <= BRCM_AGPS_MAX_MESSAGE_SIZE))
            {
				AgpsEfData * pEfInfo = NULL;

                pdata->bcm_ril_rsp = kmalloc(sizeof(AgpsEfData), GFP_KERNEL);
                if(!pdata->bcm_ril_rsp) {
                    KRIL_DEBUG(DBG_ERROR, "unable to allocate bcm_ril_rsp buf\n");                
                    pdata->handler_state = BCM_ErrorCAPI2Cmd;             
                    return;
                }
                pdata->rsp_len = sizeof(AgpsEfData);
                pEfInfo = (AgpsEfData*)pdata->bcm_ril_rsp;
                pEfInfo->dataLen = rsp->data_len;
				memcpy(pEfInfo->data, rsp->ptr,  rsp->data_len);

                pdata->handler_state = BCM_FinishCAPI2Cmd;
			}
			else
			{
				//2G case
				KRIL_DEBUG(DBG_INFO, "error result: %d\n", rsp->result);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}*/

			// MsgType_t: ::MSG_SIM_MUL_REC_DATA_RSP
			SIM_MUL_REC_DATA_t *rsp = (SIM_MUL_REC_DATA_t *) capi2_rsp->dataBuf;
			int rspDataLen = rsp->rec_len * rsp->num_of_rec;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps MultiREC Res result=%d, resLen=%d recLen=%d\n", rsp->result, rspDataLen, rsp->rec_len);
            if ((rsp->result == SIMACCESS_SUCCESS) && (rspDataLen <= BRCM_AGPS_MAX_MESSAGE_SIZE))
            {
				AgpsRecData * pRecData = NULL;

                pdata->bcm_ril_rsp = kmalloc(sizeof(AgpsRecData), GFP_KERNEL);
                if(!pdata->bcm_ril_rsp) {
                    KRIL_DEBUG(DBG_ERROR, "unable to allocate bcm_ril_rsp buf\n");                
                    pdata->handler_state = BCM_ErrorCAPI2Cmd;             
                    return;
                }
                pdata->rsp_len = sizeof(AgpsRecData);
                pRecData = (AgpsRecData*)pdata->bcm_ril_rsp;
                pRecData->dataLen = rspDataLen;//rsp->data_len;
				pRecData->recLen = rsp->rec_len;
				memcpy(pRecData->data, rsp->rec_data,  rspDataLen);

                pdata->handler_state = BCM_FinishCAPI2Cmd;
			}
			else
			{
				//2G case
				KRIL_DEBUG(DBG_INFO, "KRIL_Agps MultiREC Res error result: %d\n", rsp->result);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
//RIL_REQUEST_AGPS_SIM_EF_INFO
void KRIL_AgpsSimEfInfoHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{

    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;
	AgpsEfInfoCxt* pCxtData = (AgpsEfInfoCxt*)(pdata->cmdContext);

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);
			AgpsEfInfoReq *tdata = (AgpsEfInfoReq *)pdata->ril_cmd->data;

			pCxtData->mEfInfoPath =  tdata->path;

			pCxtData->mSimType = AGPS_SIM_APPL_INVALID;

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps, pkcs15Hdl=%d path=0x%x\n", tdata->pkcs15Hdl, tdata->path);

			//EFDIR socket_id should be 0
			CAPI2_SimApi_GetApplicationType(pLcsClientInfo);
            pdata->handler_state = BCM_AGPS_GET_EF_INFO;
            break;
        }
		case BCM_AGPS_GET_EF_INFO:
		{
			//capi2_rsp->msgType == MSG_SIM_APP_TYPE_RSP
			// payload is SIM_APPL_TYPE_t
			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);
			SIM_APPL_TYPE_t *rsp = (SIM_APPL_TYPE_t *) capi2_rsp->dataBuf;

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps BCM_AGPS_GET_EF_INFO SimType=%d\n", *rsp);
			if(*rsp != SIM_APPL_INVALID)
			{
				switch(*rsp)
				{
				case SIM_APPL_2G:
					pCxtData->mSimType = AGPS_SIM_APPL_2G;
					break;
				case SIM_APPL_3G:
					pCxtData->mSimType = AGPS_SIM_APPL_3G;
					break;
				default:
					pCxtData->mSimType = AGPS_SIM_APPL_INVALID;
					break;
				}

				//EFDIR socket_id should be 0
				CAPI2_SimApi_SubmitEFileInfoReq(pLcsClientInfo, 0, (APDUFileID_t) pCxtData->mEfInfoPath, (APDUFileID_t) 0x3F00, 0, NULL);
				pdata->handler_state = BCM_RESPCAPI2Cmd;
			}
			else
			{
				//Invalid SIM
				KRIL_DEBUG(DBG_INFO, "error SIM_APPL_INVALID\n");
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
        }
		case BCM_RESPCAPI2Cmd:
		{
			//capi2_rsp->msgType == MSG_SIM_EFILE_INFO_RSP
			// payload is SIM_EFILE_INFO_t
			SIM_EFILE_INFO_t *rsp = (SIM_EFILE_INFO_t *) capi2_rsp->dataBuf;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps EFileInfoRes result=%d, recLen=%d, fsize=%d\n", rsp->result, rsp->record_length, rsp->file_size);
            if (rsp->result == SIMACCESS_SUCCESS)
            {
				AgpsEfInfoRes * pEfInfo = NULL;

                pdata->bcm_ril_rsp = kmalloc(sizeof(AgpsEfInfoRes), GFP_KERNEL);
                if(!pdata->bcm_ril_rsp) {
                    KRIL_DEBUG(DBG_ERROR, "unable to allocate bcm_ril_rsp buf\n");                
                    pdata->handler_state = BCM_ErrorCAPI2Cmd;             
                    return;
                }
                pdata->rsp_len = sizeof(AgpsEfInfoRes);
                pEfInfo = (AgpsEfInfoRes*)pdata->bcm_ril_rsp;
                pEfInfo->recLen = rsp->record_length;
				pEfInfo->recNum = (rsp->file_size / rsp->record_length);
				pEfInfo->simType = pCxtData->mSimType;

                pdata->handler_state = BCM_FinishCAPI2Cmd;
			}
			else
			{
				//2G case
				KRIL_DEBUG(DBG_INFO, "error result: %d\n", rsp->result);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
void KRIL_AgpsSendUpLinkHandler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);
    
    switch (pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
            AgpsCp_Data *tdata = (AgpsCp_Data *)pdata->ril_cmd->data;
			//LcsClientInfo_t lcsClientInfo = {0};
			ClientInfo_t* lcsClientInfo;
			lcsClientInfo=InitClientInfo(pdata->ril_cmd->SimId);
            
            if ((NULL != tdata) && (tdata->cPlaneDataLen))
            {
				//KRIL_DEBUG(DBG_ERROR,"AGPS DATA: pro=%d, len=%d, data 0x%lx, 0x%1x, 0x%1x, 0x%1x\n", tdata->protocol, tdata->cPlaneDataLen, tdata->cPlaneData[0], tdata->cPlaneData[1], tdata->cPlaneData[2], tdata->cPlaneData[3]);
				KRIL_DEBUG(DBG_ERROR,"AGPS DATA: pro=%d, len=%d, data 0x%lx\n", tdata->protocol, tdata->cPlaneDataLen, tdata->cPlaneData[0]);
				if(tdata->protocol == AGPS_PROC_RRLP)
				{

					//CAPI2_LCS_SendRrlpDataToNetwork(GetNewTID(), GetClientID(), lcsClientInfo, tdata->cPlaneData, tdata->cPlaneDataLen);
					CAPI2_LcsApi_RrlpSendDataToNetwork(lcsClientInfo, tdata->cPlaneData, tdata->cPlaneDataLen);
				}
				else if(tdata->protocol == AGPS_PROC_RRC)
				{
					CAPI2_LcsApi_RrcSendUlDcch(lcsClientInfo, tdata->cPlaneData, tdata->cPlaneDataLen);
				}
				else
				{
					KRIL_DEBUG(DBG_ERROR,"Unknow UL protocol handler_state:0x%lX\n", pdata->handler_state);
					pdata->handler_state = BCM_ErrorCAPI2Cmd;
					break;
				}
				pdata->handler_state = BCM_RESPCAPI2Cmd;
			}
			else
			{
				KRIL_DEBUG(DBG_ERROR,"Invalid UL data handler_state:0x%lX\n", pdata->handler_state);
				pdata->handler_state = BCM_ErrorCAPI2Cmd;
			}
			
            break;
        }

        case BCM_RESPCAPI2Cmd:
        {
            KRIL_DEBUG(DBG_ERROR, "BCM_RESPCAPI2Cmd\n");
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
        }

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}
void KRIL_AgpsOpenPkcs15Handler(void *ril_cmd, Kril_CAPI2Info_t *capi2_rsp)
{
    KRIL_CmdList_t *pdata = (KRIL_CmdList_t *)ril_cmd;
	AgpsOpenPkcs15Cxt* pCxtData = (AgpsOpenPkcs15Cxt*)(pdata->cmdContext);

    if (capi2_rsp != NULL)
        KRIL_DEBUG(DBG_ERROR, "handler_state:0x%lX::result:%d\n", pdata->handler_state, capi2_rsp->result);

    switch(pdata->handler_state)
    {
        case BCM_SendCAPI2Cmd:
        {
			//SIM_GetApplicationType() returns SIM_APPL_2G or SIM_APPL_3G
			//AgpsCp_RrcStatus *tdata = (AgpsCp_RrcStatus *)pdata->ril_cmd->data;
			//LcsClientInfo_t lcsClientInfo = {0};
			ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);
			pCxtData->mSockID = 0;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps calls IsApplicationSupported\n");

			CAPI2_USimApi_IsApplicationSupported(pLcsClientInfo, PKCS15_APPLICATION);
            pdata->handler_state = BCM_AGPS_PKCS15_SUPPORT;
            break;
        }
		case BCM_AGPS_PKCS15_SUPPORT:
		{
			//capi2_rsp->msgType == MSG_USIM_IS_APP_SUPPORTED_RSP
			// payload is CAPI2_USimApi_IsApplicationSupported_Rsp_t
			CAPI2_USimApi_IsApplicationSupported_Rsp_t *rsp = (CAPI2_USimApi_IsApplicationSupported_Rsp_t *) capi2_rsp->dataBuf;

            KRIL_DEBUG(DBG_ERROR, "KRIL_Agps PKCS support=%d\n", rsp->val);
			if (TRUE == rsp->val)
            {
				ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

				memcpy(&(pCxtData->mAidData), &(rsp->pOutAidData), sizeof(USIM_AID_DATA_t));
				CAPI2_SimApi_SendOpenSocketReq(pLcsClientInfo);
				pdata->handler_state = BCM_AGPS_OPEN_SOCKET;
			}
			else
			{
				KRIL_DEBUG(DBG_INFO, "error result: %d\n", rsp->val);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}
		case BCM_AGPS_OPEN_SOCKET:
		{
			//capi2_rsp->msgType == MSG_SIM_OPEN_SOCKET_RSP
			// payload is SIM_OPEN_SOCKET_RES_t
			SIM_OPEN_SOCKET_RES_t *rsp = (SIM_OPEN_SOCKET_RES_t *) capi2_rsp->dataBuf;

			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps OpenSocket socket_id=%d\n", rsp->socket_id);
            if (rsp->socket_id)
            {
				ClientInfo_t* pLcsClientInfo = InitClientInfo(pdata->ril_cmd->SimId);

				pCxtData->mSockID = rsp->socket_id;
				CAPI2_SimApi_SendSelectAppiReq(pLcsClientInfo, pCxtData->mSockID, pCxtData->mAidData.aidData, pCxtData->mAidData.aidLen);
				pdata->handler_state = BCM_RESPCAPI2Cmd;
			}
			else
			{
				KRIL_DEBUG(DBG_INFO, "error socket_id: %d\n", rsp->socket_id);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}
		case BCM_RESPCAPI2Cmd:
		{
			//capi2_rsp->msgType == MSG_SIM_SELECT_APPLI_RSP
			// payload is SIM_SELECT_APPLI_RES_t
			SIM_SELECT_APPLI_RES_t *rsp = (SIM_SELECT_APPLI_RES_t *) capi2_rsp->dataBuf;
			KRIL_DEBUG(DBG_ERROR, "KRIL_Agps SelectAppi result=%d, pkcsHdl=%d\n", rsp->result, pCxtData->mSockID);
            if (rsp->result == SIMACCESS_SUCCESS)
            {
				unsigned char * pPkcsHdl = NULL;

                pdata->bcm_ril_rsp = kmalloc(sizeof(unsigned char), GFP_KERNEL);
                if(!pdata->bcm_ril_rsp) {
                    KRIL_DEBUG(DBG_ERROR, "unable to allocate bcm_ril_rsp buf\n");                
                    pdata->handler_state = BCM_ErrorCAPI2Cmd;             
                    return;
                }
                pdata->rsp_len = sizeof(unsigned char);
                pPkcsHdl = (unsigned char*)pdata->bcm_ril_rsp;
                *pPkcsHdl = pCxtData->mSockID;

                pdata->handler_state = BCM_FinishCAPI2Cmd;
			}
			else
			{
				//2G case
				KRIL_DEBUG(DBG_INFO, "error result: %d\n", rsp->result);
                pdata->handler_state = BCM_ErrorCAPI2Cmd;

			}
            break;
		}
		/*
        case BCM_RESPCAPI2Cmd:
        {
            KRIL_DEBUG(DBG_ERROR, "BCM_RESPCAPI2Cmd\n");
            pdata->handler_state = BCM_FinishCAPI2Cmd;
            break;
        }*/

        default:
        {
            KRIL_DEBUG(DBG_ERROR,"Error handler_state:0x%lX\n", pdata->handler_state);
            pdata->handler_state = BCM_ErrorCAPI2Cmd;
            break;
        }
    }
}