2 * Copyright (C) 2010 NXP Semiconductors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * =========================================================================== *
21 * \file phHciNfc_CEA.c *
22 * \brief HCI card emulation A management routines. *
25 * Project: NFC-FRI-1.1 *
27 * $Date: Fri Aug 21 18:35:05 2009 $ *
28 * $Author: ing04880 $ *
30 * $Aliases: NFC_FRI1.1_WK934_R31_1,NFC_FRI1.1_WK941_PREP1,NFC_FRI1.1_WK941_PREP2,NFC_FRI1.1_WK941_1,NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
32 * =========================================================================== *
36 ***************************** Header File Inclusion ****************************
38 #include <phNfcCompId.h>
39 #include <phNfcHalTypes.h>
40 #include <phHciNfc_Pipe.h>
41 #include <phHciNfc_Emulation.h>
42 #include <phOsalNfc.h>
44 ****************************** Macro Definitions *******************************
46 #if defined (HOST_EMULATION)
47 #include <phHciNfc_CE_B.h>
49 #define CE_B_EVT_NFC_SEND_DATA 0x10U
50 #define CE_B_EVT_NFC_FIELD_ON 0x11U
51 #define CE_B_EVT_NFC_DEACTIVATED 0x12U
52 #define CE_B_EVT_NFC_ACTIVATED 0x13U
53 #define CE_B_EVT_NFC_FIELD_OFF 0x14U
56 *************************** Structure and Enumeration ***************************
61 *************************** Static Function Declaration **************************
66 phHciNfc_Recv_CE_B_Event(
79 phHciNfc_Recv_CE_B_Response(
90 #if defined (SEND_DATA_EVENT)
93 phHciNfc_CE_B_ProcessData(
94 phHciNfc_sContext_t *psHciContext,
99 #endif /* #if defined (SEND_DATA_EVENT) */
102 *************************** Function Definitions ***************************
105 phHciNfc_CE_B_Init_Resources(
106 phHciNfc_sContext_t *psHciContext
109 NFCSTATUS status = NFCSTATUS_SUCCESS;
110 phHciNfc_CE_B_Info_t *ps_ce_b_info = NULL;
111 if( NULL == psHciContext )
113 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
118 ( NULL == psHciContext->p_ce_b_info ) &&
119 (phHciNfc_Allocate_Resource((void **)(&ps_ce_b_info),
120 sizeof(phHciNfc_CE_B_Info_t))== NFCSTATUS_SUCCESS)
123 psHciContext->p_ce_b_info = ps_ce_b_info;
124 ps_ce_b_info->current_seq = HOST_CE_B_INVALID_SEQ;
125 ps_ce_b_info->next_seq = HOST_CE_B_INVALID_SEQ;
126 ps_ce_b_info->pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
130 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
138 phHciNfc_CE_B_Initialise(
139 phHciNfc_sContext_t *psHciContext,
143 NFCSTATUS status = NFCSTATUS_SUCCESS;
144 static uint8_t pupi[] = {0, 0, 0, 0};
145 static uint8_t atqb_info[] = {0x04, 0x00};
147 if ((NULL == psHciContext) || (NULL == pHwRef))
149 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
151 else if(NULL == psHciContext->p_ce_b_info)
153 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
157 phHciNfc_CE_B_Info_t *ps_ce_b_info = ((phHciNfc_CE_B_Info_t *)
158 psHciContext->p_ce_b_info );
159 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
161 ps_pipe_info = ps_ce_b_info->p_pipe_info;
162 if(NULL == ps_pipe_info )
164 status = PHNFCSTVAL(CID_NFC_HCI,
165 NFCSTATUS_INVALID_HCI_INFORMATION);
169 switch(ps_ce_b_info->current_seq)
171 case HOST_CE_B_PIPE_OPEN:
173 status = phHciNfc_Open_Pipe( psHciContext,
174 pHwRef, ps_pipe_info );
175 if(status == NFCSTATUS_SUCCESS)
177 #if defined (CE_B_CONTINUE_SEQ)
178 ps_ce_b_info->next_seq = HOST_CE_B_PUPI_SEQ;
180 ps_ce_b_info->next_seq = HOST_CE_B_ENABLE_SEQ;
181 #endif /* #if defined (CE_CONTINUE_SEQ) */
182 status = NFCSTATUS_PENDING;
186 case HOST_CE_B_PUPI_SEQ:
188 /* HOST Card Emulation B PUPI Configuration */
189 ps_pipe_info->reg_index = HOST_CE_B_ATQB_INDEX;
191 ps_pipe_info->param_info =(void*)&pupi ;
192 ps_pipe_info->param_length = sizeof(pupi) ;
193 status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
194 ps_pipe_info->pipe.pipe_id,
195 (uint8_t)ANY_SET_PARAMETER);
196 if(status == NFCSTATUS_PENDING)
198 ps_ce_b_info->next_seq = HOST_CE_B_ATQB_SEQ;
202 case HOST_CE_B_ATQB_SEQ:
204 /* HOST Card Emulation B ATQB Configuration */
205 ps_pipe_info->reg_index = HOST_CE_B_ATQB_INDEX;
206 /* Configure the ATQA of Host Card Emulation B */
207 ps_pipe_info->param_info = (void*)atqb_info ;
208 ps_pipe_info->param_length = sizeof(atqb_info) ;
209 status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
210 ps_pipe_info->pipe.pipe_id,
211 (uint8_t)ANY_SET_PARAMETER);
212 if(status == NFCSTATUS_PENDING)
214 ps_ce_b_info->next_seq = HOST_CE_B_ENABLE_SEQ;
218 case HOST_CE_B_ENABLE_SEQ:
220 status = phHciNfc_CE_B_Mode( psHciContext,
221 pHwRef, HOST_CE_MODE_ENABLE );
222 if(status == NFCSTATUS_PENDING)
224 ps_ce_b_info->next_seq = HOST_CE_B_DISABLE_SEQ;
225 status = NFCSTATUS_SUCCESS;
241 phHciNfc_CE_B_Release(
242 phHciNfc_sContext_t *psHciContext,
246 NFCSTATUS status = NFCSTATUS_SUCCESS;
247 if ((NULL == psHciContext) || (NULL == pHwRef))
249 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
251 else if(NULL == psHciContext->p_ce_b_info)
253 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
257 phHciNfc_CE_B_Info_t *ps_ce_b_info = ((phHciNfc_CE_B_Info_t *)
258 psHciContext->p_ce_b_info );
259 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
261 ps_pipe_info = ps_ce_b_info->p_pipe_info;
262 if(NULL == ps_pipe_info )
264 status = PHNFCSTVAL(CID_NFC_HCI,
265 NFCSTATUS_INVALID_HCI_INFORMATION);
269 switch(ps_ce_b_info->current_seq)
271 case HOST_CE_B_DISABLE_SEQ:
273 status = phHciNfc_CE_B_Mode( psHciContext,
274 pHwRef, HOST_CE_MODE_DISABLE );
275 if(status == NFCSTATUS_PENDING)
277 ps_ce_b_info->next_seq = HOST_CE_B_PIPE_CLOSE;
281 case HOST_CE_B_PIPE_CLOSE:
283 /* HOST Card Emulation B pipe close sequence */
284 status = phHciNfc_Close_Pipe( psHciContext,
285 pHwRef, ps_pipe_info );
286 if(status == NFCSTATUS_SUCCESS)
288 ps_ce_b_info->next_seq = HOST_CE_B_PIPE_DELETE;
289 status = NFCSTATUS_PENDING;
293 case HOST_CE_B_PIPE_DELETE:
295 /* HOST Card Emulation A pipe delete sequence */
296 status = phHciNfc_Delete_Pipe( psHciContext,
297 pHwRef, ps_pipe_info );
298 if(status == NFCSTATUS_SUCCESS)
301 ps_ce_b_info->pipe_id = HCI_UNKNOWN_PIPE_ID;
302 psHciContext->p_pipe_list[ps_ce_b_info->pipe_id] = NULL;
303 phOsalNfc_FreeMemory((void *)ps_ce_b_info->p_pipe_info);
304 ps_ce_b_info->p_pipe_info = NULL;
306 ps_ce_b_info->next_seq = HOST_CE_B_PIPE_OPEN;
329 NFCSTATUS status = NFCSTATUS_SUCCESS;
330 static uint8_t param = 0 ;
331 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
333 if((NULL == psHciContext)||(NULL == pHwRef))
335 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
337 else if(NULL == psHciContext->p_ce_b_info)
339 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
343 phHciNfc_CE_B_Info_t *ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
344 psHciContext->p_ce_b_info ;
345 phHciNfc_Pipe_Info_t *ps_pipe_info = ps_ce_b_info->p_pipe_info;
347 if (NULL != ps_pipe_info)
349 ps_pipe_info->reg_index = HOST_CE_B_MODE_INDEX;
350 /* Enable/Disable Host Card Emulation A */
351 param = (uint8_t)enable_type;
352 ps_pipe_info->param_info =(void*)¶m ;
353 ps_pipe_info->param_length = sizeof(param) ;
354 status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
355 ps_ce_b_info->pipe_id,(uint8_t)ANY_SET_PARAMETER);
359 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_HCI_GATE_NOT_SUPPORTED);
368 phHciNfc_CE_B_Get_PipeID(
369 phHciNfc_sContext_t *psHciContext,
373 NFCSTATUS status = NFCSTATUS_SUCCESS;
375 if( (NULL != psHciContext)
376 && ( NULL != ppipe_id )
377 && ( NULL != psHciContext->p_ce_b_info )
380 phHciNfc_CE_B_Info_t *ps_ce_b_info=NULL;
381 ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
382 psHciContext->p_ce_b_info ;
383 *ppipe_id = ps_ce_b_info->pipe_id ;
387 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
393 phHciNfc_CE_B_Update_PipeInfo(
394 phHciNfc_sContext_t *psHciContext,
396 phHciNfc_Pipe_Info_t *pPipeInfo
399 NFCSTATUS status = NFCSTATUS_SUCCESS;
401 if( NULL == psHciContext )
403 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
405 else if(NULL == psHciContext->p_ce_b_info)
407 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
411 phHciNfc_CE_B_Info_t *ps_ce_b_info=NULL;
412 ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
413 psHciContext->p_ce_b_info ;
415 ps_ce_b_info->current_seq = HOST_CE_B_PIPE_OPEN;
416 ps_ce_b_info->next_seq = HOST_CE_B_PIPE_OPEN;
417 /* Update the pipe_id of the card emulation A Gate o
418 btained from the HCI Response */
419 ps_ce_b_info->pipe_id = pipeID;
420 if (HCI_UNKNOWN_PIPE_ID != pipeID)
422 ps_ce_b_info->p_pipe_info = pPipeInfo;
423 if (NULL != pPipeInfo)
425 /* Update the Response Receive routine of the card
427 pPipeInfo->recv_resp = &phHciNfc_Recv_CE_B_Response;
428 /* Update the event Receive routine of the card emulation A Gate */
429 pPipeInfo->recv_event = &phHciNfc_Recv_CE_B_Event;
434 ps_ce_b_info->p_pipe_info = NULL;
441 #ifdef CE_B_SEND_EVENT
443 phHciNfc_CE_B_SendData_Event(
450 NFCSTATUS status = NFCSTATUS_SUCCESS;
451 phHciNfc_sContext_t *psHciContext =
452 (phHciNfc_sContext_t *)psContext ;
455 #endif /* #ifdef CE_B_SEND_EVENT */
459 phHciNfc_Recv_CE_B_Response(
470 NFCSTATUS status = NFCSTATUS_SUCCESS;
471 phHciNfc_sContext_t *psHciContext =
472 (phHciNfc_sContext_t *)psContext ;
473 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
476 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
478 else if(NULL == psHciContext->p_ce_b_info)
480 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
484 phHciNfc_CE_B_Info_t *ps_ce_b_info=NULL;
485 uint8_t prev_cmd = ANY_GET_PARAMETER;
486 ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
487 psHciContext->p_ce_b_info ;
488 if( NULL == ps_ce_b_info->p_pipe_info)
490 status = PHNFCSTVAL(CID_NFC_HCI,
491 NFCSTATUS_INVALID_HCI_SEQUENCE);
495 prev_cmd = ps_ce_b_info->p_pipe_info->prev_msg ;
498 case ANY_GET_PARAMETER:
501 status = phHciNfc_CE_B_InfoUpdate(psHciContext,
502 ps_ce_b_info->p_pipe_info->reg_index,
503 &pResponse[HCP_HEADER_LEN],
504 (length - HCP_HEADER_LEN));
508 case ANY_SET_PARAMETER:
510 HCI_PRINT("CE B Parameter Set \n");
515 HCI_PRINT("CE B open pipe complete\n");
520 HCI_PRINT("CE B close pipe complete\n");
525 status = PHNFCSTVAL(CID_NFC_HCI,
526 NFCSTATUS_INVALID_HCI_RESPONSE);
530 if( NFCSTATUS_SUCCESS == status )
532 status = phHciNfc_CE_B_Update_Seq(psHciContext,
534 ps_ce_b_info->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
542 phHciNfc_CE_B_Update_Seq(
543 phHciNfc_sContext_t *psHciContext,
544 phHciNfc_eSeqType_t seq_type
547 phHciNfc_CE_B_Info_t *ps_ce_b_info=NULL;
548 NFCSTATUS status = NFCSTATUS_SUCCESS;
550 if( NULL == psHciContext )
552 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
554 else if( NULL == psHciContext->p_ce_b_info )
556 status = PHNFCSTVAL(CID_NFC_HCI,
557 NFCSTATUS_FEATURE_NOT_SUPPORTED);
561 ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
562 psHciContext->p_ce_b_info ;
568 ps_ce_b_info->next_seq = HOST_CE_B_PIPE_OPEN;
569 ps_ce_b_info->current_seq = HOST_CE_B_PIPE_OPEN;
574 ps_ce_b_info->current_seq = ps_ce_b_info->next_seq;
583 ps_ce_b_info->next_seq = HOST_CE_B_DISABLE_SEQ;
584 ps_ce_b_info->current_seq = HOST_CE_B_DISABLE_SEQ;
599 phHciNfc_Recv_CE_B_Event(
610 NFCSTATUS status = NFCSTATUS_SUCCESS;
611 phHciNfc_sContext_t *psHciContext =
612 (phHciNfc_sContext_t *)psContext ;
613 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pEvent)
616 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
618 else if(NULL == psHciContext->p_ce_b_info)
620 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
624 phHciNfc_HCP_Packet_t *p_packet = NULL;
625 phHciNfc_CE_B_Info_t *ps_ce_b_info=NULL;
626 phHciNfc_HCP_Message_t *message = NULL;
627 static phHal_sEventInfo_t event_info;
628 uint8_t instruction=0;
630 ps_ce_b_info = (phHciNfc_CE_B_Info_t *)
631 psHciContext->p_ce_b_info ;
633 /* Variable was set but never used (ARM warning) */
634 PHNFC_UNUSED_VARIABLE(ps_ce_b_info);
636 p_packet = (phHciNfc_HCP_Packet_t *)pEvent;
637 message = &p_packet->msg.message;
638 /* Get the instruction bits from the Message Header */
639 instruction = (uint8_t) GET_BITS8( message->msg_header,
640 HCP_MSG_INSTRUCTION_OFFSET,
641 HCP_MSG_INSTRUCTION_LEN);
642 psHciContext->host_rf_type = phHal_eISO14443_B_PICC;
643 event_info.eventHost = phHal_eHostController;
644 event_info.eventSource = phHal_eISO14443_B_PICC;
647 case CE_B_EVT_NFC_ACTIVATED:
649 event_info.eventType = NFC_EVT_ACTIVATED;
650 /* Notify to the HCI Generic layer To Update the FSM */
653 case CE_B_EVT_NFC_DEACTIVATED:
655 event_info.eventType = NFC_EVT_DEACTIVATED;
656 HCI_PRINT("CE B Target Deactivated\n");
659 case CE_B_EVT_NFC_SEND_DATA:
661 #if defined (SEND_DATA_EVENT)
662 HCI_PRINT("CE B data is received from the PN544\n");
663 if(length > HCP_HEADER_LEN)
665 status = phHciNfc_CE_B_ProcessData(
666 psHciContext, pHwRef,
667 &pEvent[HCP_HEADER_LEN],
668 (length - HCP_HEADER_LEN));
672 status = PHNFCSTVAL(CID_NFC_HCI,
673 NFCSTATUS_INVALID_HCI_RESPONSE);
675 #endif /* #if defined (SEND_DATA_EVENT) */
678 case CE_B_EVT_NFC_FIELD_ON:
680 HCI_PRINT("CE B field on\n");
681 event_info.eventType = NFC_EVT_FIELD_ON;
684 case CE_B_EVT_NFC_FIELD_OFF:
686 HCI_PRINT("CE B field off\n");
687 event_info.eventType = NFC_EVT_FIELD_OFF;
692 status = PHNFCSTVAL(CID_NFC_HCI,
693 NFCSTATUS_INVALID_HCI_INSTRUCTION);
697 if(NFCSTATUS_SUCCESS == status)
699 phHciNfc_Notify_Event(psHciContext, pHwRef,
707 #if defined (SEND_DATA_EVENT)
710 phHciNfc_CE_B_ProcessData(
711 phHciNfc_sContext_t *psHciContext,
717 NFCSTATUS status = NFCSTATUS_SUCCESS;
719 static uint8_t send_data[] = {0x6D, 0x80};
720 status = phHciNfc_CE_B_SendData_Event(
721 (void *)psHciContext, pHwRef,
722 send_data, sizeof(send_data));
726 #endif /* #if defined (SEND_DATA_EVENT) */
728 #endif /* #if defined (HOST_EMULATION) */