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_ISO15693.c *
22 * \brief HCI ISO-15693 management routines. *
25 * Project: NFC-FRI-1.1 *
27 * $Date: Thu Feb 11 18:54:47 2010 $ *
28 * $Author: ing04880 $ *
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_ISO15693)
44 #include <phHciNfc_ISO15693.h>
47 ****************************** Macro Definitions *******************************
49 #define ISO_15693_INVENTORY_INDEX 0x01U
50 #define ISO_15693_AFI_INDEX 0x02U
52 #define ISO_15693_INVENTORY_LENGTH 0x0AU
53 #define ISO_15693_AFI_LENGTH 0x01U
55 #define ISO_15693_SINGLE_TAG_FOUND 0x00U
56 #define ISO_15693_MULTIPLE_TAGS_FOUND 0x03U
59 *************************** Structure and Enumeration ***************************
63 *************************** Static Function Declaration **************************
68 phHciNfc_ISO15693_InfoUpdate(
69 phHciNfc_sContext_t *psHciContext,
77 phHciNfc_Recv_ISO15693_Response(
90 phHciNfc_Recv_ISO15693_Event(
102 *************************** Function Definitions ***************************
106 phHciNfc_ISO15693_Init_Resources(
107 phHciNfc_sContext_t *psHciContext
110 NFCSTATUS status = NFCSTATUS_SUCCESS;
111 phHciNfc_ISO15693_Info_t *ps_15693_info=NULL;
112 if( NULL == psHciContext )
114 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
118 if (NULL != psHciContext->p_iso_15693_info)
120 status = NFCSTATUS_SUCCESS;
122 else if(( NULL == psHciContext->p_iso_15693_info ) &&
123 (phHciNfc_Allocate_Resource((void **)(&ps_15693_info),
124 sizeof(phHciNfc_ISO15693_Info_t))== NFCSTATUS_SUCCESS)
127 psHciContext->p_iso_15693_info = ps_15693_info;
128 ps_15693_info->current_seq = ISO15693_INVENTORY;
129 ps_15693_info->next_seq = ISO15693_INVENTORY;
130 ps_15693_info->ps_15693_pipe_info = NULL;
134 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
143 phHciNfc_ISO15693_Get_PipeID(
144 phHciNfc_sContext_t *psHciContext,
148 NFCSTATUS status = NFCSTATUS_SUCCESS;
149 if( (NULL != psHciContext)
150 && ( NULL != ppipe_id )
151 && ( NULL != psHciContext->p_iso_15693_info )
154 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
155 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
156 psHciContext->p_iso_15693_info ;
157 *ppipe_id = ps_15693_info->ps_15693_pipe_info->pipe.pipe_id;
161 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
168 phHciNfc_ISO15693_Update_PipeInfo(
169 phHciNfc_sContext_t *psHciContext,
171 phHciNfc_Pipe_Info_t *pPipeInfo
174 NFCSTATUS status = NFCSTATUS_SUCCESS;
175 if((NULL == psHciContext) || (NULL == pPipeInfo))
177 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
179 else if(NULL == psHciContext->p_iso_15693_info)
181 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
185 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
186 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
187 psHciContext->p_iso_15693_info ;
189 /* Update the pipe_id of the ISO15693 Gate obtained from
191 ps_15693_info->ps_15693_pipe_info = pPipeInfo;
192 ps_15693_info->pipe_id = pipeID;
193 ps_15693_info->ps_15693_pipe_info->pipe.pipe_id = pipeID;
194 /* Update the Response Receive routine of the ISO15693 Gate */
195 pPipeInfo->recv_resp = &phHciNfc_Recv_ISO15693_Response;
196 /* Update the event Receive routine of the ISO15693 Gate */
197 pPipeInfo->recv_event = &phHciNfc_Recv_ISO15693_Event;
204 phHciNfc_ISO15693_Update_Info(
205 phHciNfc_sContext_t *psHciContext,
210 NFCSTATUS status = NFCSTATUS_SUCCESS;
212 if (NULL == psHciContext)
214 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
216 else if(NULL == psHciContext->p_iso_15693_info)
218 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
222 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
223 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
224 psHciContext->p_iso_15693_info;
228 case HCI_ISO_15693_ENABLE:
230 if (NULL != iso_15693_info)
232 ps_15693_info->enable_iso_15693_gate =
233 *((uint8_t *)iso_15693_info);
237 case HCI_ISO_15693_INFO_SEQ:
239 ps_15693_info->current_seq = ISO15693_INVENTORY;
240 ps_15693_info->next_seq = ISO15693_INVENTORY;
245 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
255 phHciNfc_ISO15693_Info_Sequence (
260 NFCSTATUS status = NFCSTATUS_SUCCESS;
261 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
262 if( (NULL == psHciContext)
266 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
268 else if((NULL == psHciContext->p_iso_15693_info) ||
269 (HCI_ISO_15693_ENABLE !=
270 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
271 enable_iso_15693_gate))
273 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
277 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
278 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
281 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
282 psHciContext->p_iso_15693_info;
283 ps_pipe_info = ps_15693_info->ps_15693_pipe_info;
285 if(NULL == ps_pipe_info )
287 status = PHNFCSTVAL(CID_NFC_HCI,
288 NFCSTATUS_INVALID_HCI_SEQUENCE);
292 switch(ps_15693_info->current_seq)
294 case ISO15693_INVENTORY:
296 ps_pipe_info->reg_index = ISO_15693_INVENTORY_INDEX;
297 pipeid = ps_pipe_info->pipe.pipe_id ;
298 /* Fill the data buffer and send the command to the
301 phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef,
302 pipeid, (uint8_t)ANY_GET_PARAMETER);
303 if(NFCSTATUS_PENDING == status )
305 ps_15693_info->next_seq = ISO15693_AFI;
311 ps_pipe_info->reg_index = ISO_15693_AFI_INDEX;
312 pipeid = ps_pipe_info->pipe.pipe_id ;
313 /* Fill the data buffer and send the command to the
316 phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef,
317 pipeid, (uint8_t)ANY_GET_PARAMETER);
318 if(NFCSTATUS_PENDING == status )
320 ps_15693_info->next_seq = ISO15693_END_SEQUENCE;
324 case ISO15693_END_SEQUENCE:
326 phNfc_sCompletionInfo_t CompInfo;
327 if (ISO_15693_MULTIPLE_TAGS_FOUND ==
328 ps_15693_info->multiple_tgts_found)
330 CompInfo.status = NFCSTATUS_MULTIPLE_TAGS;
334 CompInfo.status = NFCSTATUS_SUCCESS;
337 CompInfo.info = &(ps_15693_info->iso15693_info);
339 ps_15693_info->iso15693_info.RemDevType =
340 phHal_eISO15693_PICC;
341 ps_15693_info->current_seq = ISO15693_INVENTORY;
342 ps_15693_info->next_seq = ISO15693_INVENTORY;
343 status = NFCSTATUS_SUCCESS;
344 /* Notify to the upper layer */
345 phHciNfc_Tag_Notify(psHciContext, pHwRef,
346 NFC_NOTIFY_TARGET_DISCOVERED,
352 status = PHNFCSTVAL(CID_NFC_HCI,
353 NFCSTATUS_INVALID_HCI_RESPONSE);
364 phHciNfc_ISO15693_InfoUpdate(
365 phHciNfc_sContext_t *psHciContext,
371 NFCSTATUS status = NFCSTATUS_SUCCESS;
372 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
375 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
376 (psHciContext->p_iso_15693_info);
381 case ISO_15693_INVENTORY_INDEX:
383 if (ISO_15693_INVENTORY_LENGTH == reg_length)
385 ps_15693_info->iso15693_info.RemoteDevInfo
386 .Iso15693_Info.Flags = *(reg_value + i );
388 ps_15693_info->iso15693_info.RemoteDevInfo
389 .Iso15693_Info.Dsfid = *(reg_value + i );
391 (void)memcpy(ps_15693_info->iso15693_info.
392 RemoteDevInfo.Iso15693_Info.Uid,
393 (reg_value+i), (reg_length - i ));
394 ps_15693_info->iso15693_info.RemoteDevInfo
395 .Iso15693_Info.UidLength = ( reg_length - i );
396 HCI_PRINT_BUFFER("\tISO 15693 inventory", reg_value, reg_length);
400 status = PHNFCSTVAL(CID_NFC_HCI,
401 NFCSTATUS_INVALID_HCI_RESPONSE);
405 case ISO_15693_AFI_INDEX:
407 if (ISO_15693_AFI_LENGTH == reg_length)
409 ps_15693_info->iso15693_info.RemoteDevInfo
410 .Iso15693_Info.Afi = *(reg_value + i );
411 HCI_PRINT_BUFFER("\tISO 15693 AFI", reg_value, reg_length);
415 status = PHNFCSTVAL(CID_NFC_HCI,
416 NFCSTATUS_INVALID_HCI_RESPONSE);
422 status = PHNFCSTVAL(CID_NFC_HCI,
423 NFCSTATUS_INVALID_HCI_RESPONSE);
433 phHciNfc_Recv_ISO15693_Response(
444 NFCSTATUS status = NFCSTATUS_SUCCESS;
445 phHciNfc_sContext_t *psHciContext =
446 (phHciNfc_sContext_t *)psContext ;
449 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
452 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
454 else if(NULL == psHciContext->p_iso_15693_info)
456 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
460 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
461 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
462 uint8_t prev_cmd = ANY_GET_PARAMETER;
464 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
465 psHciContext->p_iso_15693_info;
467 ps_pipe_info = ps_15693_info->ps_15693_pipe_info;
468 if( NULL == ps_pipe_info)
470 status = PHNFCSTVAL(CID_NFC_HCI,
471 NFCSTATUS_INVALID_HCI_SEQUENCE);
475 prev_cmd = ps_pipe_info->prev_msg ;
478 case ANY_GET_PARAMETER:
480 status = phHciNfc_ISO15693_InfoUpdate(psHciContext,
481 ps_pipe_info->reg_index,
482 &pResponse[HCP_HEADER_LEN],
483 (uint8_t)(length - HCP_HEADER_LEN));
485 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
490 case ANY_SET_PARAMETER:
492 HCI_PRINT("ISO 15693 Parameter Set \n");
493 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
495 ps_15693_info->next_seq = ISO15693_INVENTORY;
500 HCI_PRINT("ISO 15693 open pipe complete\n");
501 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
503 ps_15693_info->next_seq = ISO15693_INVENTORY;
508 HCI_PRINT("ISO 15693 close pipe complete\n");
509 status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext,
514 case NXP_ISO15693_CMD:
516 if (length >= HCP_HEADER_LEN)
518 HCI_PRINT("ISO 15693 packet received \n");
519 /* Copy buffer to the receive buffer */
520 phHciNfc_Append_HCPFrame(psHciContext->recv_buffer,
521 0, pResponse, length);
522 psHciContext->rx_total = length;
523 psHciContext->rx_index = HCP_HEADER_LEN;
524 HCI_PRINT_BUFFER("ISO 15693 Bytes received",
529 status = PHNFCSTVAL(CID_NFC_HCI,
530 NFCSTATUS_INVALID_HCI_RESPONSE);
536 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
539 }/* End of switch(prev_cmd) */
541 if( NFCSTATUS_SUCCESS == status )
543 ps_pipe_info->prev_status = NFCSTATUS_SUCCESS;
544 ps_15693_info->current_seq = ps_15693_info->next_seq;
553 phHciNfc_Recv_ISO15693_Event(
564 NFCSTATUS status = NFCSTATUS_SUCCESS;
565 phHciNfc_sContext_t *psHciContext =
566 (phHciNfc_sContext_t *)psContext ;
567 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pEvent)
570 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
572 else if((NULL == psHciContext->p_iso_15693_info) ||
573 (HCI_ISO_15693_ENABLE !=
574 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
575 enable_iso_15693_gate))
577 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
581 phHciNfc_HCP_Packet_t *p_packet = NULL;
582 phHciNfc_ISO15693_Info_t *ps_15693_info=NULL;
583 phHciNfc_HCP_Message_t *message = NULL;
584 uint8_t instruction=0,
587 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
588 psHciContext->p_iso_15693_info;
589 p_packet = (phHciNfc_HCP_Packet_t *)pEvent;
590 message = &p_packet->msg.message;
591 /* Get the instruction bits from the Message Header */
592 instruction = (uint8_t) GET_BITS8( message->msg_header,
593 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
595 if ((EVT_TARGET_DISCOVERED == instruction)
596 && ((ISO_15693_MULTIPLE_TAGS_FOUND == message->payload[i])
597 || (ISO_15693_SINGLE_TAG_FOUND == message->payload[i]))
600 phNfc_sCompletionInfo_t pCompInfo;
602 /* #define NFC_ISO_15693_MULTIPLE_TAGS_SUPPORT 0x00 */
603 #if (NFC_ISO_15693_MULTIPLE_TAGS_SUPPORT >= 0x01)
605 if (ISO_15693_MULTIPLE_TAGS_FOUND == message->payload[i])
607 ps_15693_info->multiple_tgts_found = ISO_15693_MULTIPLE_TAGS_FOUND;
608 pCompInfo.status = NFCSTATUS_MULTIPLE_TAGS;
611 #endif /* #if (NFC_ISO_15693_MULTIPLE_TAGS_SUPPORT <= 0x01) */
613 ps_15693_info->multiple_tgts_found = FALSE;
614 pCompInfo.status = NFCSTATUS_SUCCESS;
616 /* CompInfo.info = &(ps_15693_info->iso15693_info); */
618 psHciContext->host_rf_type = phHal_eISO15693_PCD;
619 ps_15693_info->iso15693_info.RemDevType = phHal_eISO15693_PICC;
620 ps_15693_info->current_seq = ISO15693_INVENTORY;
621 /* Notify to the HCI Generic layer To Update the FSM */
622 phHciNfc_Notify_Event(psHciContext, pHwRef,
623 NFC_NOTIFY_TARGET_DISCOVERED,
629 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
636 phHciNfc_Send_ISO15693_Command(
637 phHciNfc_sContext_t *psHciContext,
643 NFCSTATUS status = NFCSTATUS_SUCCESS;
645 if( (NULL == psHciContext) || (NULL == pHwRef) )
647 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
649 else if((NULL == psHciContext->p_iso_15693_info) ||
650 (HCI_ISO_15693_ENABLE !=
651 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
652 enable_iso_15693_gate) ||
653 (HCI_UNKNOWN_PIPE_ID ==
654 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
657 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
660 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
664 phHciNfc_ISO15693_Info_t *ps_15693_info=NULL;
665 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
666 phHciNfc_HCP_Packet_t *hcp_packet = NULL;
667 phHciNfc_HCP_Message_t *hcp_message = NULL;
669 uint16_t length = HCP_HEADER_LEN;
671 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
672 psHciContext->p_iso_15693_info ;
673 ps_pipe_info = ps_15693_info->ps_15693_pipe_info;
674 if(NULL == ps_pipe_info )
676 status = PHNFCSTVAL(CID_NFC_HCI,
677 NFCSTATUS_INVALID_HCI_SEQUENCE);
681 psHciContext->tx_total = 0 ;
682 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
683 /* Construct the HCP Frame */
684 if (NXP_ISO15693_CMD == cmd)
686 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
687 (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
688 hcp_message = &(hcp_packet->msg.message);
692 hcp_message->payload[i++] =
693 psHciContext->p_xchg_info->params.tag_info.cmd_type ;
695 hcp_message->payload[i++] =
696 psHciContext->p_xchg_info->params.tag_info.addr ;
698 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
699 i, (uint8_t *)ps_pipe_info->param_info,
700 ps_pipe_info->param_length);
701 length =(uint16_t)(length + i + ps_pipe_info->param_length);
703 ps_pipe_info->sent_msg_type = (uint8_t)HCP_MSG_TYPE_COMMAND;
704 ps_pipe_info->prev_msg = cmd;
705 psHciContext->tx_total = length;
706 psHciContext->response_pending = TRUE;
708 /* Send the Constructed HCP packet to the lower layer */
709 status = phHciNfc_Send_HCP( psHciContext, pHwRef);
710 ps_pipe_info->prev_status = status;
718 phHciNfc_ISO15693_Set_AFI(
724 NFCSTATUS status = NFCSTATUS_SUCCESS;
725 phHciNfc_sContext_t *psHciContext =
726 (phHciNfc_sContext_t *)psContext ;
728 if( (NULL == psHciContext) || (NULL == pHwRef))
730 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
732 else if((NULL == psHciContext->p_iso_15693_info) ||
733 (HCI_ISO_15693_ENABLE !=
734 ((phHciNfc_ISO15693_Info_t *)(psHciContext->p_iso_15693_info))->
735 enable_iso_15693_gate))
737 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
741 phHciNfc_ISO15693_Info_t *ps_15693_info = NULL;
742 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
745 ps_15693_info = (phHciNfc_ISO15693_Info_t *)
746 psHciContext->p_iso_15693_info ;
747 ps_pipe_info = ps_15693_info->ps_15693_pipe_info;
749 if( NULL == ps_pipe_info)
751 status = PHNFCSTVAL(CID_NFC_HCI,
752 NFCSTATUS_INVALID_HCI_SEQUENCE);
756 ps_pipe_info->reg_index = ISO_15693_AFI_INDEX;
757 ps_pipe_info->param_info = &afi_value;
758 ps_pipe_info->param_length = sizeof(afi_value);
759 pipeid = ps_pipe_info->pipe.pipe_id ;
760 status = phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef,
761 pipeid, (uint8_t)ANY_SET_PARAMETER);
767 #endif /* #if defined (TYPE_ISO15693) */