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_Jewel.c *
22 * \brief HCI Jewel/Topaz Management Routines. *
25 * Project: NFC-FRI-1.1 *
27 * $Date: Mon Mar 29 17:34:47 2010 $ *
28 * $Author: ing04880 $ *
30 * $Aliases: 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 <phHciNfc_Pipe.h>
40 #include <phHciNfc_RFReader.h>
41 #include <phOsalNfc.h>
43 #if defined(TYPE_JEWEL)
44 #include <phHciNfc_Jewel.h>
47 ****************************** Macro Definitions *******************************
49 #define JEWEL_SINGLE_TAG_FOUND 0x00U
50 #define JEWEL_MULTIPLE_TAGS_FOUND 0x03U
51 #define NXP_WRA_CONTINUE_ACTIVATION 0x12U
53 #define NXP_JEWEL_READID 0x78U
54 #define NXP_JEWEL_READID_LENGTH 0x06U
57 *************************** Structure and Enumeration ***************************
61 *************************** Static Function Declaration **************************
66 phHciNfc_Recv_Jewel_Response(
79 phHciNfc_Recv_Jewel_Event(
92 phHciNfc_Jewel_InfoUpdate(
93 phHciNfc_sContext_t *psHciContext,
101 phHciNfc_Recv_Jewel_Packet(
102 phHciNfc_sContext_t *psHciContext,
113 phHciNfc_Jewel_Get_PipeID(
114 phHciNfc_sContext_t *psHciContext,
118 NFCSTATUS status = NFCSTATUS_SUCCESS;
120 if( (NULL != psHciContext)
121 && ( NULL != ppipe_id )
122 && ( NULL != psHciContext->p_jewel_info )
125 phHciNfc_Jewel_Info_t *ps_jewel_info = NULL;
126 ps_jewel_info = (phHciNfc_Jewel_Info_t *)psHciContext->p_jewel_info;
127 *ppipe_id = ps_jewel_info->pipe_id;
131 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
137 phHciNfc_Jewel_Init_Resources(
138 phHciNfc_sContext_t *psHciContext
141 NFCSTATUS status = NFCSTATUS_SUCCESS;
142 phHciNfc_Jewel_Info_t *ps_jewel_info = NULL;
143 if( NULL == psHciContext )
145 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
150 ( NULL == psHciContext->p_jewel_info ) &&
151 (phHciNfc_Allocate_Resource((void **)(&ps_jewel_info),
152 sizeof(phHciNfc_Jewel_Info_t))== NFCSTATUS_SUCCESS)
155 psHciContext->p_jewel_info = ps_jewel_info;
156 ps_jewel_info->current_seq = JEWEL_INVALID_SEQ;
157 ps_jewel_info->next_seq = JEWEL_INVALID_SEQ;
158 ps_jewel_info->pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
162 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
170 phHciNfc_Jewel_Update_PipeInfo(
171 phHciNfc_sContext_t *psHciContext,
173 phHciNfc_Pipe_Info_t *pPipeInfo
176 NFCSTATUS status = NFCSTATUS_SUCCESS;
178 if( NULL == psHciContext )
180 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
182 else if(NULL == psHciContext->p_jewel_info)
184 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
188 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
189 ps_jewel_info = (phHciNfc_Jewel_Info_t *)psHciContext->p_jewel_info ;
191 /* Update the pipe_id of the Jewel Gate obtained from the HCI
193 ps_jewel_info->pipe_id = pipeID;
194 ps_jewel_info->p_pipe_info = pPipeInfo;
195 /* Update the Response Receive routine of the Jewel Gate */
196 pPipeInfo->recv_resp = phHciNfc_Recv_Jewel_Response;
197 /* Update the event Receive routine of the Jewel Gate */
198 pPipeInfo->recv_event = phHciNfc_Recv_Jewel_Event;
206 phHciNfc_Jewel_Update_Info(
207 phHciNfc_sContext_t *psHciContext,
212 NFCSTATUS status = NFCSTATUS_SUCCESS;
214 if (NULL == psHciContext)
216 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
218 else if(NULL == psHciContext->p_jewel_info)
220 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
224 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
225 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
226 psHciContext->p_jewel_info ;
230 case HCI_JEWEL_ENABLE:
232 if (NULL != jewel_info)
234 ps_jewel_info->enable_jewel_gate =
235 *((uint8_t *)jewel_info);
239 case HCI_JEWEL_INFO_SEQ:
241 ps_jewel_info->current_seq = JEWEL_READID_SEQUENCE;
242 ps_jewel_info->next_seq = JEWEL_READID_SEQUENCE;
247 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
256 phHciNfc_Jewel_Info_Sequence (
261 NFCSTATUS status = NFCSTATUS_SUCCESS;
262 phHciNfc_sContext_t *psHciContext =
263 ((phHciNfc_sContext_t *)psHciHandle);
264 static uint8_t paraminfo[NXP_JEWEL_READID_LENGTH + 1] = {0};
266 if( (NULL == psHciContext)
270 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
272 else if((NULL == psHciContext->p_jewel_info) ||
274 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
277 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
281 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
282 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
285 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
286 psHciContext->p_jewel_info ;
287 ps_pipe_info = ps_jewel_info->p_pipe_info;
288 if(NULL == ps_pipe_info )
290 status = PHNFCSTVAL(CID_NFC_HCI,
291 NFCSTATUS_INVALID_HCI_SEQUENCE);
295 switch(ps_jewel_info->current_seq)
297 case JEWEL_READID_SEQUENCE:
299 pipeid = ps_pipe_info->pipe.pipe_id;
300 ps_pipe_info->reg_index = NXP_JEWEL_READID;
301 paraminfo[0] = NXP_JEWEL_READID;
303 ps_pipe_info->param_info = (void *)¶minfo;
304 ps_pipe_info->param_length = NXP_JEWEL_READID_LENGTH + 1;
306 status = phHciNfc_Send_Jewel_Command(psHciContext,
310 if(NFCSTATUS_PENDING == status )
312 ps_jewel_info->next_seq = JEWEL_END_SEQUENCE;
316 case JEWEL_END_SEQUENCE:
318 phNfc_sCompletionInfo_t CompInfo;
320 ps_pipe_info->reg_index = JEWEL_END_SEQUENCE;
321 if (JEWEL_MULTIPLE_TAGS_FOUND ==
322 ps_jewel_info->multiple_tgts_found)
324 CompInfo.status = NFCSTATUS_MULTIPLE_TAGS;
328 CompInfo.status = NFCSTATUS_SUCCESS;
331 CompInfo.info = &(ps_jewel_info->s_jewel_info);
333 ps_jewel_info->s_jewel_info.RemDevType = phHal_eJewel_PICC;
334 ps_jewel_info->current_seq = JEWEL_READID_SEQUENCE;
335 ps_jewel_info->next_seq = JEWEL_READID_SEQUENCE;
336 status = NFCSTATUS_SUCCESS;
337 /* Notify to the upper layer */
338 phHciNfc_Tag_Notify(psHciContext, pHwRef,
339 NFC_NOTIFY_TARGET_DISCOVERED,
345 status = PHNFCSTVAL(CID_NFC_HCI,
346 NFCSTATUS_INVALID_HCI_RESPONSE);
357 phHciNfc_Jewel_InfoUpdate(
358 phHciNfc_sContext_t *psHciContext,
364 NFCSTATUS status = NFCSTATUS_SUCCESS;
365 phHciNfc_Jewel_Info_t *ps_jewel_info = NULL;
366 phHal_sJewelInfo_t *ps_jewel_tag_info = NULL;
368 ps_jewel_info = (phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info);
369 ps_jewel_tag_info = &(ps_jewel_info->s_jewel_info.RemoteDevInfo.Jewel_Info);
373 case NXP_JEWEL_READID:
375 HCI_PRINT_BUFFER("\tJewel ID", reg_value, reg_length);
376 if(NXP_JEWEL_READID_LENGTH == reg_length)
379 ps_jewel_tag_info->HeaderRom0 = reg_value[i++];
380 ps_jewel_tag_info->HeaderRom1 = reg_value[i++];
381 (void)memcpy(ps_jewel_tag_info->Uid,
385 ps_jewel_tag_info->UidLength = (reg_length - i);
389 status = PHNFCSTVAL(CID_NFC_HCI,
390 NFCSTATUS_INVALID_HCI_RESPONSE);
396 status = PHNFCSTVAL(CID_NFC_HCI,
397 NFCSTATUS_INVALID_HCI_RESPONSE);
406 phHciNfc_Recv_Jewel_Packet(
407 phHciNfc_sContext_t *psHciContext,
416 NFCSTATUS status = NFCSTATUS_SUCCESS;
417 phHciNfc_Jewel_Info_t *ps_jewel_info = (phHciNfc_Jewel_Info_t *)
418 (psHciContext->p_jewel_info);
420 if (NXP_JEWEL_READID == ps_jewel_info->p_pipe_info->reg_index)
422 status = phHciNfc_Jewel_InfoUpdate(psHciContext,
423 ps_jewel_info->p_pipe_info->reg_index,
424 pResponse, (uint8_t)length);
428 /* Send Jewel data to the upper layer */
429 HCI_PRINT_BUFFER("Jewel Bytes received", pResponse, length);
430 psHciContext->rx_index = HCP_HEADER_LEN;
438 phHciNfc_Recv_Jewel_Response(
449 NFCSTATUS status = NFCSTATUS_SUCCESS;
450 phHciNfc_sContext_t *psHciContext =
451 (phHciNfc_sContext_t *)psContext;
454 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
457 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
459 else if(NULL == psHciContext->p_jewel_info)
461 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
465 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
466 uint8_t prev_cmd = ANY_GET_PARAMETER;
467 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
468 psHciContext->p_jewel_info ;
469 if( NULL == ps_jewel_info->p_pipe_info)
471 status = PHNFCSTVAL(CID_NFC_HCI,
472 NFCSTATUS_INVALID_HCI_SEQUENCE);
476 prev_cmd = ps_jewel_info->p_pipe_info->prev_msg ;
479 case ANY_GET_PARAMETER:
481 if (length >= HCP_HEADER_LEN)
483 status = phHciNfc_Jewel_InfoUpdate(psHciContext,
484 ps_jewel_info->p_pipe_info->reg_index,
485 &pResponse[HCP_HEADER_LEN],
486 (uint8_t)(length - HCP_HEADER_LEN));
490 status = PHNFCSTVAL(CID_NFC_HCI,
491 NFCSTATUS_INVALID_HCI_RESPONSE);
495 case ANY_SET_PARAMETER:
497 HCI_PRINT("Jewel Parameter Set \n");
498 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
500 ps_jewel_info->next_seq = JEWEL_READID_SEQUENCE;
505 HCI_PRINT("Jewel open pipe complete\n");
506 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
508 ps_jewel_info->next_seq = JEWEL_READID_SEQUENCE;
513 HCI_PRINT("Jewel close pipe complete\n");
514 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
520 HCI_PRINT("Jewel packet received \n");
521 if (length >= HCP_HEADER_LEN)
523 phHciNfc_Append_HCPFrame(psHciContext->recv_buffer,
524 0, pResponse, length);
525 psHciContext->rx_total = length;
526 status = phHciNfc_Recv_Jewel_Packet(psHciContext,
527 &pResponse[HCP_HEADER_LEN],
528 (length - HCP_HEADER_LEN));
532 status = PHNFCSTVAL(CID_NFC_HCI,
533 NFCSTATUS_INVALID_HCI_RESPONSE);
537 case NXP_WRA_CONTINUE_ACTIVATION:
538 case NXP_WR_ACTIVATE_ID:
540 HCI_PRINT("Jewel continue activation or ");
541 HCI_PRINT("reactivation completed \n");
542 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
546 case NXP_WR_PRESCHECK:
548 HCI_PRINT("Presence check completed \n");
551 case NXP_WR_ACTIVATE_NEXT:
553 HCI_PRINT("Activate next completed \n");
554 if (length > HCP_HEADER_LEN)
556 if (JEWEL_MULTIPLE_TAGS_FOUND ==
557 pResponse[HCP_HEADER_LEN])
559 ps_jewel_info->multiple_tgts_found =
560 JEWEL_MULTIPLE_TAGS_FOUND;
564 ps_jewel_info->multiple_tgts_found = FALSE;
569 status = PHNFCSTVAL(CID_NFC_HCI,
570 NFCSTATUS_INVALID_HCI_RESPONSE);
574 case NXP_WR_DISPATCH_TO_UICC:
580 /* Optional error code, if no error code field
581 in the response, then this command is
582 successfully completed */
583 ps_jewel_info->uicc_activation =
584 UICC_CARD_ACTIVATION_SUCCESS;
587 case (HCP_HEADER_LEN + 1):
589 ps_jewel_info->uicc_activation =
590 pResponse[HCP_HEADER_LEN];
592 } /* End of case (HCP_HEADER_LEN + index) */
595 status = PHNFCSTVAL(CID_NFC_HCI,
596 NFCSTATUS_INVALID_HCI_RESPONSE);
600 if (NFCSTATUS_SUCCESS == status)
602 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
609 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
613 if( NFCSTATUS_SUCCESS == status )
615 ps_jewel_info->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
616 ps_jewel_info->current_seq = ps_jewel_info->next_seq;
625 phHciNfc_Recv_Jewel_Event(
636 NFCSTATUS status = NFCSTATUS_SUCCESS;
637 phHciNfc_sContext_t *psHciContext =
638 (phHciNfc_sContext_t *)psContext ;
640 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pEvent)
643 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
645 else if((NULL == psHciContext->p_jewel_info) ||
647 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
650 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
654 phHciNfc_HCP_Packet_t *p_packet = NULL;
655 phHciNfc_Jewel_Info_t *ps_jewel_info = NULL;
656 phHciNfc_HCP_Message_t *message = NULL;
657 uint8_t instruction=0,
660 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
661 psHciContext->p_jewel_info ;
662 p_packet = (phHciNfc_HCP_Packet_t *)pEvent;
663 message = &p_packet->msg.message;
664 /* Get the instruction bits from the Message Header */
665 instruction = (uint8_t) GET_BITS8( message->msg_header,
666 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
668 if ((EVT_TARGET_DISCOVERED == instruction)
669 && ((JEWEL_MULTIPLE_TAGS_FOUND == message->payload[i] )
670 || (JEWEL_SINGLE_TAG_FOUND == message->payload[i]))
673 static phNfc_sCompletionInfo_t pCompInfo;
675 if (JEWEL_MULTIPLE_TAGS_FOUND == message->payload[i])
677 ps_jewel_info->multiple_tgts_found =
678 JEWEL_MULTIPLE_TAGS_FOUND;
679 pCompInfo.status = NFCSTATUS_MULTIPLE_TAGS;
683 ps_jewel_info->multiple_tgts_found = FALSE;
684 pCompInfo.status = NFCSTATUS_SUCCESS;
687 psHciContext->host_rf_type = phHal_eJewel_PCD;
688 ps_jewel_info->s_jewel_info.RemDevType = phHal_eJewel_PICC;
689 ps_jewel_info->current_seq = JEWEL_READID_SEQUENCE;
691 /* Notify to the HCI Generic layer To Update the FSM */
692 phHciNfc_Notify_Event(psHciContext, pHwRef,
693 NFC_NOTIFY_TARGET_DISCOVERED,
699 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
706 phHciNfc_Send_Jewel_Command(
707 phHciNfc_sContext_t *psContext,
713 NFCSTATUS status = NFCSTATUS_SUCCESS;
714 phHciNfc_sContext_t *psHciContext =
715 (phHciNfc_sContext_t *)psContext ;
716 if( (NULL == psHciContext) || (NULL == pHwRef) )
718 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
720 else if((NULL == psHciContext->p_jewel_info) ||
722 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
723 enable_jewel_gate) ||
724 (HCI_UNKNOWN_PIPE_ID ==
725 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
728 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
731 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
735 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
736 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
737 phHciNfc_HCP_Packet_t *hcp_packet = NULL;
738 phHciNfc_HCP_Message_t *hcp_message = NULL;
740 length = HCP_HEADER_LEN;
742 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
743 psHciContext->p_jewel_info ;
744 ps_pipe_info = ps_jewel_info->p_pipe_info;
745 if(NULL == ps_pipe_info )
747 status = PHNFCSTVAL(CID_NFC_HCI,
748 NFCSTATUS_INVALID_HCI_SEQUENCE);
752 psHciContext->tx_total = 0 ;
753 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
755 if (NXP_JEWEL_RAW == cmd)
757 /* Construct the HCP Frame */
758 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
759 (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
760 hcp_message = &(hcp_packet->msg.message);
761 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
762 i, (uint8_t *)ps_pipe_info->param_info,
763 ps_pipe_info->param_length);
764 length =(uint8_t)(length + i + ps_pipe_info->param_length);
768 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_COMMAND);
771 if (NFCSTATUS_SUCCESS == status)
773 ps_pipe_info->sent_msg_type = (uint8_t)HCP_MSG_TYPE_COMMAND;
774 ps_pipe_info->prev_msg = cmd;
775 psHciContext->tx_total = length;
776 psHciContext->response_pending = TRUE;
778 /* Send the Constructed HCP packet to the lower layer */
779 status = phHciNfc_Send_HCP( psHciContext, pHwRef);
780 ps_pipe_info->prev_status = status;
788 phHciNfc_Jewel_GetRID(
789 phHciNfc_sContext_t *psHciContext,
792 NFCSTATUS status = NFCSTATUS_SUCCESS;
793 static uint8_t reader_id_info[NXP_JEWEL_READID_LENGTH] = {0};
795 if( (NULL == psHciContext) || (NULL == pHwRef))
797 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
799 else if((NULL == psHciContext->p_jewel_info) ||
801 ((phHciNfc_Jewel_Info_t *)(psHciContext->p_jewel_info))->
804 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
808 phHciNfc_Jewel_Info_t *ps_jewel_info=NULL;
809 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
812 ps_jewel_info = (phHciNfc_Jewel_Info_t *)
813 psHciContext->p_jewel_info ;
815 ps_pipe_info = ps_jewel_info->p_pipe_info;
816 if(NULL == ps_pipe_info )
818 status = PHNFCSTVAL(CID_NFC_HCI,
819 NFCSTATUS_INVALID_HCI_SEQUENCE);
823 pipeid = ps_jewel_info->pipe_id ;
824 reader_id_info[0] = NXP_JEWEL_READID;
826 ps_pipe_info->param_info = (void *)&reader_id_info;
827 ps_pipe_info->param_length = NXP_JEWEL_READID_LENGTH + 1 ;
829 status = phHciNfc_Send_Jewel_Command(psHciContext,
837 #endif /* #if defined(TYPE_JEWEL) */