• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 2010 NXP Semiconductors
3   *
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
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
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.
15   */
16  
17  
18  /*!
19  * =========================================================================== *
20  *                                                                             *
21  *                                                                             *
22  * \file  phHciNfc_Generic.c                                                   *
23  * \brief Generic HCI Source for the HCI Management.                           *
24  *                                                                             *
25  *                                                                             *
26  * Project: NFC-FRI-1.1                                                        *
27  *                                                                             *
28  * $Date: Tue Jun  8 09:31:49 2010 $                                           *
29  * $Author: ing04880 $                                                         *
30  * $Revision: 1.108 $                                                           *
31  * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
32  *                                                                             *
33  * =========================================================================== *
34  */
35  
36  /*
37  ################################################################################
38  ***************************** Header File Inclusion ****************************
39  ################################################################################
40  */
41  
42  #include <phNfcCompId.h>
43  #include <phHciNfc_Sequence.h>
44  #include <phHciNfc_Pipe.h>
45  #include <phHciNfc_AdminMgmt.h>
46  #include <phHciNfc_IDMgmt.h>
47  #include <phHciNfc_LinkMgmt.h>
48  #include <phHciNfc_PollingLoop.h>
49  #include <phHciNfc_RFReader.h>
50  #include <phHciNfc_RFReaderA.h>
51  #include <phOsalNfc.h>
52  
53  /*
54  ################################################################################
55  ****************************** Macro Definitions *******************************
56  ################################################################################
57  */
58  
59  /* HCI timeout value */
60  uint32_t nxp_nfc_hci_response_timeout = NXP_NFC_HCI_TIMEOUT;
61  
62  /*
63  ################################################################################
64  ************************ Static Variable Definitions ***************************
65  ################################################################################
66  */
67  
68  
69  #if  (NXP_NFC_HCI_TIMER == 1)
70  
71  #define NXP_HCI_RESPONSE_TIMEOUT  (NXP_NFC_HCI_TIMEOUT)
72  
73  #include <phOsalNfc_Timer.h>
74  /** \internal HCI Response Timer to detect the
75   * Stalled HCI Response */
76  static uint32_t                    hci_resp_timer_id = NXP_INVALID_TIMER_ID;
77  static phHciNfc_sContext_t        *gpsHciContext= NULL;
78  
79  #endif /* (NXP_NFC_HCI_TIMER == 1) */
80  
81  
82  /*
83  ################################################################################
84  ************************* Function Prototype Declaration ***********************
85  ################################################################################
86  */
87  
88  #if  (NXP_NFC_HCI_TIMER == 1)
89  
90  static
91  void
92  phHciNfc_Response_Timeout (
93                  uint32_t resp_timer_id, void *pContext
94                  );
95  
96  #endif /* (NXP_NFC_HCI_TIMER == 1) */
97  
98  /**
99   * \ingroup grp_hci_nfc
100   *
101   *  The phHciNfc_Send function sends the HCI Commands to the
102   *  corresponding peripheral device, described by the HCI Context Structure.
103   *
104   *  \param[in]  psContext               psContext is the context of
105   *                                      the HCI Layer.
106   *  \param[in]  pHwRef                  pHwRef is the Information of
107   *                                      the Device Interface Link .
108   *  \param[in]  pdata                   Pointer to the buffer containing
109   *                                      the command to be sent.
110   *  \param[in] length                   Variable that receives
111   *                                      the number of bytes actually sent.
112   *
113   *  \retval NFCSTATUS_PENDING           Command successfully sent.
114   *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
115   *                                      could not be interpreted properly.
116   *  \retval Other errors                Errors related to the lower layers
117   *
118   */
119  
120  static
121   NFCSTATUS
122   phHciNfc_Send(
123                      void                    *psContext,
124                      void                    *pHwRef,
125                      uint8_t                 *pdata,
126  #ifdef ONE_BYTE_LEN
127                      uint8_t                 length
128  #else
129                      uint16_t                length
130  #endif
131                );
132  
133   static
134   NFCSTATUS
135   phHciNfc_Process_HCP (
136                                  phHciNfc_sContext_t     *psHciContext,
137                                  void                    *pHwRef,
138                                  void                    *pdata,
139  #ifdef ONE_BYTE_LEN
140                                  uint8_t                 length
141  #else
142                                  uint16_t                length
143  #endif
144                        );
145  
146  
147  static
148  NFCSTATUS
149  phHciNfc_Process_Response (
150                                  phHciNfc_sContext_t     *psHciContext,
151                                  void                    *pHwRef,
152                                  void                    *pdata,
153  #ifdef ONE_BYTE_LEN
154                                  uint8_t                 length
155  #else
156                                  uint16_t                length
157  #endif
158                           );
159  
160  static
161  NFCSTATUS
162  phHciNfc_Error_Response (
163                                  phHciNfc_sContext_t     *psHciContext,
164                                  void                    *pHwRef,
165                                  void                    *pdata,
166  #ifdef ONE_BYTE_LEN
167                                  uint8_t                 length
168  #else
169                                  uint16_t                length
170  #endif
171                           );
172  
173  static
174  NFCSTATUS
175  phHciNfc_Process_Event (
176                                  phHciNfc_sContext_t     *psHciContext,
177                                  void                    *pHwRef,
178                                  void                    *pdata,
179  #ifdef ONE_BYTE_LEN
180                                  uint8_t                 length
181  #else
182                                  uint16_t                length
183  #endif
184                           );
185  
186  
187  static
188  NFCSTATUS
189  phHciNfc_Process_Command (
190                                  phHciNfc_sContext_t     *psHciContext,
191                                  void                    *pHwRef,
192                                  void                    *pdata,
193  #ifdef ONE_BYTE_LEN
194                                  uint8_t                 length
195  #else
196                                  uint16_t                length
197  #endif
198                           );
199  
200  
201  static
202  void
203  phHciNfc_Reset_Pipe_MsgInfo(
204                              phHciNfc_Pipe_Info_t    *p_pipe_info
205                          );
206  
207  static
208  void
209  phHciNfc_Build_HCPMessage(
210                                  phHciNfc_HCP_Packet_t *hcp_packet,
211                                  uint8_t             msg_type,
212                                  uint8_t             instruction
213                            );
214  
215  static
216  void
217  phHciNfc_Build_HCPHeader(
218                                  phHciNfc_HCP_Packet_t *hcp_packet,
219                                  uint8_t             chainbit,
220                                  uint8_t             pipe_id
221                            );
222  /**
223   * \ingroup grp_hci_nfc
224   *
225   *  The phHciNfc_Receive_HCP function receive the HCI Host Control Packet
226   *  Frames from the device.
227   *
228   *  \param[in]  psHciContext            psHciContext is the context of
229   *                                      the HCI Layer.
230   *  \param[in]  pHwRef                  pHwRef is the Information of
231   *                                      the Device Interface Link .
232   *  \param[in] pdata                    Pointer to the response buffer that
233   *                                      receives the response read.
234   *  \param[in] length                   Variable that receives
235   *                                      the number of bytes read.
236   *
237   *  \retval NFCSTATUS_PENDING           HCP Frame receive pending.
238   *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
239   *                                      could not be interpreted properly.
240   *  \retval Other errors                Other related errors
241   *
242   *
243   */
244  
245  
246  static
247  NFCSTATUS
248  phHciNfc_Receive_HCP (
249                              phHciNfc_sContext_t     *psHciContext,
250                              void                    *pHwRef,
251                              uint8_t                 *pdata,
252  #ifdef ONE_BYTE_LEN
253                              uint8_t                 length
254  #else
255                              uint16_t                length
256  #endif
257                       );
258  
259  
260  /*
261  ################################################################################
262  ***************************** Function Definitions *****************************
263  ################################################################################
264  */
265  
266  
267  #if  (NXP_NFC_HCI_TIMER == 1)
268  
269  static
270  void
phHciNfc_Response_Timeout(uint32_t resp_timer_id,void * pContext)271  phHciNfc_Response_Timeout (
272                      uint32_t resp_timer_id, void *pContext
273                  )
274  {
275      phNfc_sCompletionInfo_t  comp_info = {0,0,0};
276  
277      if ( ( NULL != gpsHciContext)
278              && (resp_timer_id == hci_resp_timer_id ))
279      {
280          pphNfcIF_Notification_CB_t  p_upper_notify =
281              gpsHciContext->p_upper_notify;
282          void                        *p_upper_context =
283                                  gpsHciContext->p_upper_context;
284          phHal_sHwReference_t        *pHwRef = gpsHciContext->p_hw_ref;
285  		uint32_t				i = 0;
286  
287  
288          HCI_DEBUG(" HCI TIMEOUT: HCI Response Timeout Occurred in %X Timer\n"
289                                                                   ,resp_timer_id);
290          /* Stop the Response Timer */
291          phOsalNfc_Timer_Stop( hci_resp_timer_id );
292  
293  		comp_info.status = PHNFCSTVAL(CID_NFC_HCI,
294                          NFCSTATUS_BOARD_COMMUNICATION_ERROR);
295          /* Roll Back to the Select State */
296          phHciNfc_FSM_Rollback(gpsHciContext);
297  
298  		for(i=0;i < PHHCINFC_MAX_PIPE; i++)
299  		{
300  			phHciNfc_Reset_Pipe_MsgInfo(gpsHciContext->p_pipe_list[i]);
301  		}
302  
303          /* Notify the Error/Success Scenario to the upper layer */
304          phHciNfc_Notify( p_upper_notify, p_upper_context,
305                      pHwRef, (uint8_t) NFC_NOTIFY_DEVICE_ERROR, &comp_info );
306      }
307  
308      return ;
309  
310  }
311  
312  #endif /* (NXP_NFC_HCI_TIMER == 1) */
313  
314  
315  
316  /*!
317   * \brief Allocation of the HCI Interface resources.
318   *
319   * This function releases and frees all the resources used by HCI Command and
320   * Response Mechanism
321   */
322  
323   NFCSTATUS
phHciNfc_Allocate_Resource(void ** ppBuffer,uint16_t size)324   phHciNfc_Allocate_Resource (
325                                  void                **ppBuffer,
326                                  uint16_t            size
327                              )
328  {
329      NFCSTATUS           status = NFCSTATUS_SUCCESS;
330  
331      *ppBuffer = (void *) phOsalNfc_GetMemory(size);
332      if( *ppBuffer != NULL )
333      {
334          (void )memset(((void *)*ppBuffer), 0,
335                                      size);
336      }
337      else
338      {
339          *ppBuffer = NULL;
340          status = PHNFCSTVAL(CID_NFC_HCI,
341                          NFCSTATUS_INSUFFICIENT_RESOURCES);
342      }
343      return status;
344  }
345  
346  
347  
348  /*!
349   * \brief Release of the HCI Interface resources.
350   *
351   * This function releases and frees all the resources used by HCI Command and
352   * Response Mechanism
353   */
354   void
phHciNfc_Release_Resources(phHciNfc_sContext_t ** ppsHciContext)355   phHciNfc_Release_Resources (
356                                  phHciNfc_sContext_t **ppsHciContext
357                              )
358  {
359      uint8_t i = 0;
360  
361  
362  #if  (NXP_NFC_HCI_TIMER == 1)
363  
364      if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
365      {
366          /* Stop and Un-Intialise the Response Timer */
367          phOsalNfc_Timer_Stop( hci_resp_timer_id );
368          phOsalNfc_Timer_Delete( hci_resp_timer_id );
369          HCI_DEBUG(" HCI : Timer %X Stopped and Released\n",
370                                              hci_resp_timer_id);
371          hci_resp_timer_id = NXP_INVALID_TIMER_ID;
372      }
373      gpsHciContext = NULL;
374  
375  #endif /* (NXP_NFC_HCI_TIMER == 1) */
376  
377  
378      if(NULL != (*ppsHciContext)->p_admin_info)
379      {
380          phOsalNfc_FreeMemory((*ppsHciContext)->p_admin_info);
381          (*ppsHciContext)->p_admin_info = NULL;
382      }
383      if(NULL !=(*ppsHciContext)->p_link_mgmt_info)
384      {
385          phOsalNfc_FreeMemory((*ppsHciContext)->p_link_mgmt_info);
386          (*ppsHciContext)->p_link_mgmt_info = NULL;
387      }
388      if(NULL !=(*ppsHciContext)->p_identity_info)
389      {
390          phOsalNfc_FreeMemory((*ppsHciContext)->p_identity_info);
391          (*ppsHciContext)->p_identity_info = NULL;
392      }
393      if(NULL !=(*ppsHciContext)->p_device_mgmt_info)
394      {
395          phOsalNfc_FreeMemory((*ppsHciContext)->p_device_mgmt_info);
396          (*ppsHciContext)->p_device_mgmt_info = NULL;
397      }
398      if(NULL !=(*ppsHciContext)->p_reader_mgmt_info)
399      {
400          phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_mgmt_info);
401          (*ppsHciContext)->p_reader_mgmt_info = NULL;
402      }
403      if(NULL !=(*ppsHciContext)->p_poll_loop_info)
404      {
405          phOsalNfc_FreeMemory((*ppsHciContext)->p_poll_loop_info);
406          (*ppsHciContext)->p_poll_loop_info = NULL;
407      }
408      if(NULL !=(*ppsHciContext)->p_reader_a_info)
409      {
410          phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_a_info);
411          (*ppsHciContext)->p_reader_a_info = NULL;
412      }
413  #ifdef TYPE_B
414      if(NULL !=(*ppsHciContext)->p_reader_b_info)
415      {
416          phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_b_info);
417          (*ppsHciContext)->p_reader_b_info = NULL;
418      }
419  #endif
420  #ifdef TYPE_FELICA
421      if(NULL !=(*ppsHciContext)->p_felica_info)
422      {
423          phOsalNfc_FreeMemory((*ppsHciContext)->p_felica_info);
424          (*ppsHciContext)->p_felica_info = NULL;
425      }
426  #endif
427  #ifdef TYPE_JEWEL
428      if(NULL !=(*ppsHciContext)->p_jewel_info)
429      {
430          phOsalNfc_FreeMemory((*ppsHciContext)->p_jewel_info);
431          (*ppsHciContext)->p_jewel_info = NULL;
432      }
433  #endif
434  #ifdef  TYPE_ISO15693
435      if(NULL !=(*ppsHciContext)->p_iso_15693_info)
436      {
437          phOsalNfc_FreeMemory((*ppsHciContext)->p_iso_15693_info);
438          (*ppsHciContext)->p_iso_15693_info = NULL;
439      }
440  #endif /* #ifdef    TYPE_ISO15693 */
441  #ifdef ENABLE_P2P
442      if(NULL !=(*ppsHciContext)->p_nfcip_info)
443      {
444          phOsalNfc_FreeMemory((*ppsHciContext)->p_nfcip_info);
445          (*ppsHciContext)->p_nfcip_info = NULL;
446      }
447  #endif
448      if(NULL !=(*ppsHciContext)->p_emulation_mgmt_info)
449      {
450          phOsalNfc_FreeMemory((*ppsHciContext)->p_emulation_mgmt_info);
451          (*ppsHciContext)->p_emulation_mgmt_info = NULL;
452      }
453      if(NULL !=(*ppsHciContext)->p_wi_info)
454      {
455          phOsalNfc_FreeMemory((*ppsHciContext)->p_wi_info);
456          (*ppsHciContext)->p_wi_info = NULL;
457      }
458      if(NULL !=(*ppsHciContext)->p_swp_info)
459      {
460          phOsalNfc_FreeMemory((*ppsHciContext)->p_swp_info);
461          (*ppsHciContext)->p_swp_info = NULL;
462      }
463      if(NULL !=(*ppsHciContext)->p_uicc_info)
464      {
465          phOsalNfc_FreeMemory((*ppsHciContext)->p_uicc_info);
466          (*ppsHciContext)->p_uicc_info = NULL;
467      }
468  #ifdef HOST_EMULATION
469      if(NULL !=(*ppsHciContext)->p_ce_a_info)
470      {
471          phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_a_info);
472          (*ppsHciContext)->p_ce_a_info = NULL;
473      }
474      if(NULL !=(*ppsHciContext)->p_ce_b_info)
475      {
476          phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_b_info);
477          (*ppsHciContext)->p_ce_b_info = NULL;
478      }
479  #endif
480  
481      for(i=0;i < PHHCINFC_MAX_PIPE; i++)
482      {
483          if(NULL != (*ppsHciContext)->p_pipe_list[i])
484          {
485              phOsalNfc_FreeMemory((*ppsHciContext)->p_pipe_list[i]);
486          }
487      }
488  
489  
490      phOsalNfc_FreeMemory((*ppsHciContext));
491      (*ppsHciContext) = NULL;
492  
493      return ;
494  }
495  
496  static
497  void
phHciNfc_Reset_Pipe_MsgInfo(phHciNfc_Pipe_Info_t * p_pipe_info)498  phHciNfc_Reset_Pipe_MsgInfo(
499                              phHciNfc_Pipe_Info_t    *p_pipe_info
500                          )
501  {
502      if (p_pipe_info != NULL)
503      {
504          p_pipe_info->sent_msg_type = HCP_MSG_TYPE_RESERVED;
505          p_pipe_info->prev_msg = MSG_INSTRUCTION_UNKNWON;
506          p_pipe_info->prev_status = NFCSTATUS_INVALID_HCI_INSTRUCTION;
507          p_pipe_info->param_info = NULL;
508          p_pipe_info->param_length = FALSE ;
509      }
510      return;
511  }
512  
513  
514  void
phHciNfc_Release_Lower(phHciNfc_sContext_t * psHciContext,void * pHwRef)515  phHciNfc_Release_Lower(
516                      phHciNfc_sContext_t         *psHciContext,
517                      void                        *pHwRef
518                 )
519  {
520      phNfc_sLowerIF_t            *plower_if =
521                                      &(psHciContext->lower_interface);
522      NFCSTATUS            status = NFCSTATUS_SUCCESS;
523  
524      PHNFC_UNUSED_VARIABLE(status);
525      if(NULL != plower_if->release)
526      {
527          status = plower_if->release((void *)plower_if->pcontext,
528                                          (void *)pHwRef);
529          (void) memset((void *)plower_if, 0, sizeof(phNfc_sLowerIF_t));
530          HCI_DEBUG(" HCI Releasing the Lower Layer Resources: Status = %02X\n"
531                                                                      ,status);
532      }
533  
534      return;
535  }
536  
537  
538  
539  /*!
540   * \brief Sends the HCI Commands to the corresponding peripheral device.
541   *
542   * This function sends the HCI Commands to the connected NFC Pheripheral device
543   */
544   static
545   NFCSTATUS
phHciNfc_Send(void * psContext,void * pHwRef,uint8_t * pdata,uint8_t length)546   phHciNfc_Send (
547                        void                  *psContext,
548                        void                  *pHwRef,
549                        uint8_t               *pdata,
550  #ifdef ONE_BYTE_LEN
551                        uint8_t               length
552  #else
553                        uint16_t              length
554  #endif
555                       )
556  {
557      phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
558      NFCSTATUS               status = NFCSTATUS_SUCCESS;
559  
560      phNfc_sLowerIF_t        *plower_if = &(psHciContext->lower_interface);
561  
562      if( (NULL != plower_if)
563          && (NULL != plower_if->send)
564        )
565      {
566          HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
567          HCI_DEBUG("HCI: Response Pending status --> %s \n",
568              (psHciContext->response_pending)?"TRUE":"FALSE");
569          HCI_PRINT_BUFFER("Send Buffer",pdata,length);
570          /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_IN_PROGRESS; */
571  
572  #if  (NXP_NFC_HCI_TIMER == 1)
573  
574      if (
575          (TRUE != psHciContext->tx_hcp_chaining)
576          &&  (TRUE == psHciContext->response_pending)
577          && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
578         )
579      {
580          /* Start the HCI Response Timer */
581          phOsalNfc_Timer_Start( hci_resp_timer_id,
582                  nxp_nfc_hci_response_timeout, phHciNfc_Response_Timeout, NULL );
583          HCI_DEBUG(" HCI : Timer %X Started \n", hci_resp_timer_id);
584      }
585  
586  #endif /* (NXP_NFC_HCI_TIMER == 1) */
587  
588          status = plower_if->send((void *)plower_if->pcontext,
589                                  (void *)pHwRef, pdata, length);
590      }
591  
592      return status;
593  }
594  
595  
596  /*!
597   * \brief Receives the HCI Response from the corresponding peripheral device.
598   *
599   * This function receives the HCI Command Response to the connected NFC
600   * Pheripheral device.
601   */
602  
603  NFCSTATUS
phHciNfc_Receive(void * psContext,void * pHwRef,uint8_t * pdata,uint8_t length)604  phHciNfc_Receive(
605                          void                *psContext,
606                          void                *pHwRef,
607                          uint8_t             *pdata,
608  #ifdef ONE_BYTE_LEN
609                          uint8_t             length
610  #else
611                          uint16_t            length
612  #endif
613                      )
614  {
615      phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
616      phNfc_sLowerIF_t *plower_if = NULL ;
617      NFCSTATUS         status = NFCSTATUS_SUCCESS;
618  
619      if(NULL == psHciContext )
620      {
621          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
622      }
623      else
624      {
625          plower_if = &(psHciContext->lower_interface);
626  
627          if( (NULL != plower_if)
628              && (NULL != plower_if->receive)
629            )
630          {
631              /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_IN_PROGRESS; */
632              status = plower_if->receive((void *)plower_if->pcontext,
633                                      (void *)pHwRef, pdata, length);
634          }
635      }
636      return status;
637  }
638  
639  
640  /*!
641   * \brief Sends the HCP Packet to the lower link layer .
642   *
643   * This function Sends the HCI Data in the HCP packet format to the below
644   * Link layer.
645   */
646  
647   NFCSTATUS
phHciNfc_Send_HCP(phHciNfc_sContext_t * psHciContext,void * pHwRef)648   phHciNfc_Send_HCP (
649                                  phHciNfc_sContext_t *psHciContext,
650                                  void                *pHwRef
651                     )
652  {
653      NFCSTATUS               status = NFCSTATUS_SUCCESS;
654      phHciNfc_HCP_Packet_t   *tx_data = (phHciNfc_HCP_Packet_t *)
655                                      psHciContext->send_buffer;
656      /* Skip the HCP Header Byte initially */
657      uint16_t                tx_length = psHciContext->tx_total - 1 ;
658      uint16_t                hcp_index = HCP_ZERO_LEN;
659      uint8_t                 pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
660      static  uint8_t         chain_bit = HCP_CHAINBIT_DEFAULT;
661  
662      pipe_id =  (uint8_t) GET_BITS8( tx_data->hcp_header,
663          HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
664  
665      /* Fragmentation of the HCP Frames */
666      if ( tx_length > PHHCINFC_MAX_PACKET_DATA )
667      {
668          tx_data = &psHciContext->tx_packet;
669          (void)memset((void *)tx_data, FALSE,
670                          sizeof(phHciNfc_HCP_Packet_t));
671          if (HCP_CHAINBIT_DEFAULT == chain_bit)
672          {
673              /* HCI Chaining Needs to be Done */
674              psHciContext->tx_remain = tx_length;
675              psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
676              chain_bit = HCP_CHAINBIT_BEGIN;
677              /* Increment the Fragment index to skip the HCP Header */
678              psHciContext->tx_hcp_frgmnt_index++;
679              psHciContext->tx_hcp_chaining = TRUE ;
680              tx_length = PHHCINFC_MAX_PACKET_DATA ;
681          }
682          else if ( psHciContext->tx_remain > PHHCINFC_MAX_PACKET_DATA )
683          {
684  			/* Intermediate Chained HCI Frames */
685              tx_length = PHHCINFC_MAX_PACKET_DATA ;
686          }
687          else
688          {
689              /* End of Chaining Reached */
690              chain_bit = HCP_CHAINBIT_END;
691              tx_length = psHciContext->tx_remain ;
692              psHciContext->tx_hcp_chaining = FALSE ;
693          }
694  
695          /* Build the HCP Header to have Chaining Enabled */
696          phHciNfc_Build_HCPHeader(tx_data, chain_bit , pipe_id );
697  
698          phHciNfc_Append_HCPFrame((uint8_t *)tx_data->msg.payload, hcp_index,
699              (&psHciContext->send_buffer[psHciContext->tx_hcp_frgmnt_index])
700              , tx_length );
701      }
702      else
703      {
704          /* No Chaining Required */
705          chain_bit = HCP_CHAINBIT_DEFAULT;
706  
707          psHciContext->tx_hcp_chaining = FALSE ;
708  
709          psHciContext->tx_remain = tx_length ;
710      }
711  
712      /* Include the Skipped HCP Header Byte */
713      tx_length++;
714  
715      status = phHciNfc_Send ( (void *) psHciContext, pHwRef,
716                          (uint8_t *)tx_data, tx_length );
717  
718      return status;
719  }
720  
721  
722  /*!
723   * \brief Receives the HCP Packet from the lower link layer .
724   *
725   * This function receives the HCI Data in the HCP packet format from the below
726   * Link layer.
727   */
728   static
729   NFCSTATUS
phHciNfc_Receive_HCP(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t * pdata,uint8_t length)730   phHciNfc_Receive_HCP (
731                                  phHciNfc_sContext_t *psHciContext,
732                                  void                *pHwRef,
733                                  uint8_t             *pdata,
734  #ifdef ONE_BYTE_LEN
735                                  uint8_t             length
736  #else
737                                  uint16_t            length
738  #endif
739                     )
740  {
741      NFCSTATUS               status = NFCSTATUS_SUCCESS;
742      phHciNfc_HCP_Packet_t   *packet = NULL;
743      uint8_t                 chainbit = HCP_CHAINBIT_DEFAULT;
744      uint16_t                hcp_index = 0;
745  
746      packet = (phHciNfc_HCP_Packet_t *)pdata;
747      chainbit = (uint8_t) GET_BITS8( packet->hcp_header,
748          HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN);
749      hcp_index = psHciContext->rx_hcp_frgmnt_index;
750      HCI_PRINT_BUFFER("Receive Buffer",((uint8_t *)pdata),length);
751      if (HCP_CHAINBIT_BEGIN == chainbit)
752      {
753          /* pdata = (uint8_t *)&psHciContext->rx_packet; */
754          /* De Fragmentation of the Received HCP Frames */
755          /* Subsequent Chaining Frames */
756          if( hcp_index  > 0 )
757          {
758              /* Copy the obtained fragment and receive the next fragment */
759              phHciNfc_Append_HCPFrame(
760                  psHciContext->recv_buffer, hcp_index,
761                      (uint8_t *)&pdata[HCP_MESSAGE_LEN],
762                              (length - HCP_MESSAGE_LEN) );
763              psHciContext->rx_hcp_frgmnt_index =(uint16_t)
764                          (hcp_index + length - HCP_MESSAGE_LEN);
765          }
766          /* First Chaining Frame*/
767          else
768          {
769              psHciContext->rx_hcp_chaining = TRUE ;
770              /* Copy the obtained fragment and receive the next fragment */
771              phHciNfc_Append_HCPFrame(psHciContext->recv_buffer,
772                  hcp_index, pdata, length);
773              psHciContext->rx_hcp_frgmnt_index = ( hcp_index + length ) ;
774  
775          }
776          status = phHciNfc_Receive ( (void *) psHciContext, pHwRef,
777                                                  pdata, length);
778      }
779      else
780      {
781          if(TRUE == psHciContext->rx_hcp_chaining)
782          {
783              /* If the chaining was done earlier */
784              psHciContext->rx_hcp_chaining = FALSE ;
785              /* Copy the Remaining buffer to the RX_BUFFER */
786              phHciNfc_Append_HCPFrame(
787                  psHciContext->recv_buffer, hcp_index,
788                      (uint8_t *)&pdata[HCP_MESSAGE_LEN],
789                              (length - HCP_MESSAGE_LEN) );
790              /* If there is chaining done the return the same data */
791              psHciContext->rx_total =
792                          (hcp_index + length - HCP_MESSAGE_LEN);
793              psHciContext->rx_hcp_frgmnt_index = FALSE ;
794          }
795          else
796          {
797              (void) memcpy( psHciContext->recv_buffer, pdata, length);
798              /* If there is no chaining done then return the same data */
799              psHciContext->rx_total = (hcp_index + length);
800  
801          }
802      }
803  
804      return status;
805  }
806  
807  
808  /*!
809   * \brief Receives the HCP Packet from the lower link layer .
810   *
811   * This function receives the HCI Data in the HCP packet format from the below
812   * Link layer.
813   */
814  
815   static
816   NFCSTATUS
phHciNfc_Process_HCP(phHciNfc_sContext_t * psHciContext,void * pHwRef,void * pdata,uint8_t length)817   phHciNfc_Process_HCP (
818                                  phHciNfc_sContext_t     *psHciContext,
819                                  void                    *pHwRef,
820                                  void                    *pdata,
821  #ifdef ONE_BYTE_LEN
822                                  uint8_t             length
823  #else
824                                  uint16_t            length
825  #endif
826                        )
827  {
828      phHciNfc_HCP_Packet_t   *packet = NULL;
829      phHciNfc_HCP_Message_t  *message = NULL;
830      NFCSTATUS               status = NFCSTATUS_SUCCESS;
831      uint8_t                 msg_type = 0;
832  
833      if( (NULL == pdata)
834          || ( length < HCP_HEADER_LEN )
835        )
836      {
837          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
838      }
839      else
840      {
841          status = phHciNfc_Receive_HCP( psHciContext, pHwRef, pdata, length );
842      }/* End of the Valid Data Handling */
843  
844      if( NFCSTATUS_SUCCESS  == status )
845      {
846          packet = (phHciNfc_HCP_Packet_t *)psHciContext->recv_buffer;
847          length =
848  #ifdef ONE_BYTE_LEN
849              (uint8_t)
850  #endif
851              psHciContext->rx_total ;
852          message = &packet->msg.message;
853          /* HCI_PRINT_BUFFER("Total Receive Buffer",((uint8_t *)pdata),length); */
854          msg_type = (uint8_t) GET_BITS8( message->msg_header,
855              HCP_MSG_TYPE_OFFSET, HCP_MSG_TYPE_LEN);
856          switch ( msg_type )
857          {
858              case HCP_MSG_TYPE_RESPONSE:
859              {
860                  status = phHciNfc_Process_Response( psHciContext,
861                                                  pHwRef, (void *)packet, length );
862                  break;
863              }
864              case HCP_MSG_TYPE_EVENT:
865              {
866                  status = phHciNfc_Process_Event( psHciContext,
867                                                  pHwRef,(void *)packet, length );
868                  break;
869              }
870              case HCP_MSG_TYPE_COMMAND:
871              {
872  
873                  status = phHciNfc_Process_Command( psHciContext,
874                                                  pHwRef, (void *)packet, length );
875                  break;
876              }
877              /* case HCP_MSG_TYPE_RESERVED: */
878              default:
879              {
880                  status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
881                  break;
882              }
883          }
884      }/* End of Receive HCP Status */
885      return status;
886  }
887  
888  
889   static
890   NFCSTATUS
phHciNfc_Process_Response(phHciNfc_sContext_t * psHciContext,void * pHwRef,void * pdata,uint8_t length)891   phHciNfc_Process_Response (
892                                   phHciNfc_sContext_t    *psHciContext,
893                                   void                   *pHwRef,
894                                   void                   *pdata,
895  #ifdef ONE_BYTE_LEN
896                                   uint8_t             length
897  #else
898                                   uint16_t            length
899  #endif
900                               )
901  {
902      phHciNfc_HCP_Packet_t   *packet = NULL;
903      phHciNfc_HCP_Message_t  *message = NULL;
904      uint8_t                 instruction=0;
905      uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
906      phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
907  
908      NFCSTATUS               status = NFCSTATUS_SUCCESS;
909  
910      packet = (phHciNfc_HCP_Packet_t *)pdata;
911      message = &packet->msg.message;
912      /* Get the instruction bits from the Message Header */
913      instruction = (uint8_t) GET_BITS8( message->msg_header,
914                                  HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
915      /* Get the Pipe ID from the HCP Header */
916      pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
917                                  HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
918  
919  #if  (NXP_NFC_HCI_TIMER == 1)
920  
921      if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
922      {
923          /* Stop the HCI Response Timer */
924          HCI_DEBUG(" HCI : Timer %X Stopped \n", hci_resp_timer_id);
925          phOsalNfc_Timer_Stop( hci_resp_timer_id );
926      }
927  
928  #endif /* (NXP_NFC_HCI_TIMER == 1) */
929  
930      if (pipe_id >=  PHHCINFC_MAX_PIPE )
931      {
932          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
933      }
934      else if( ((uint8_t) ANY_OK != instruction)
935          && ( (pipe_id !=    PIPETYPE_STATIC_ADMIN )
936          && ( ADM_CLEAR_ALL_PIPE != (psHciContext->p_pipe_list[pipe_id])->prev_msg ))
937          )
938      {
939          status = phHciNfc_Error_Response( psHciContext, pHwRef, pdata, length );
940      }
941      else
942      {
943          p_pipe_info = psHciContext->p_pipe_list[pipe_id];
944          if( ( NULL != p_pipe_info )
945              &&   ( HCP_MSG_TYPE_COMMAND == p_pipe_info->sent_msg_type  )
946              &&   ( NULL != p_pipe_info->recv_resp )
947          )
948          {
949              status = psHciContext->p_pipe_list[pipe_id]->recv_resp( psHciContext,
950                                                              pHwRef, pdata, length );
951          }
952          else
953          {
954              status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
955          }
956          /* There is no Pending Response */
957          psHciContext->response_pending = FALSE ;
958          HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
959              __FUNCTION__);
960          if( NFCSTATUS_SUCCESS == status )
961          {
962              phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
963              status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
964  
965          }/* End of Success Status validation */
966          else
967          {
968              HCI_DEBUG("HCI: Status --> %X \n", status );
969          }
970  
971      } /* End of the Valid Response handling */
972      return status;
973  }
974  
975  
976  static
977   NFCSTATUS
phHciNfc_Error_Response(phHciNfc_sContext_t * psHciContext,void * pHwRef,void * pdata,uint8_t length)978   phHciNfc_Error_Response (
979                                  phHciNfc_sContext_t     *psHciContext,
980                                  void                    *pHwRef,
981                                  void                    *pdata,
982  #ifdef ONE_BYTE_LEN
983                                  uint8_t                 length
984  #else
985                                  uint16_t                length
986  #endif
987                           )
988  {
989  
990      phHciNfc_HCP_Packet_t   *packet = (phHciNfc_HCP_Packet_t *)pdata;
991      phHciNfc_HCP_Message_t  *message = &packet->msg.message;
992      NFCSTATUS               status = NFCSTATUS_SUCCESS;
993      uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
994  #if defined(HCI_TRACE) || defined (ERROR_INSTRUCTION)
995      uint8_t                 instruction = 0;
996      instruction = (uint8_t) GET_BITS8(message->msg_header,
997                              HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
998  #endif
999  
1000      /* Get the Pipe ID from the HCP Header */
1001      pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
1002                                  HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
1003      /* Process the Error Response based on the obtained instruction */
1004  #ifdef ERROR_INSTRUCTION
1005      switch(instruction)
1006      {
1007          case ANY_E_NOT_CONNECTED:
1008          case ANY_E_CMD_PAR_UNKNOWN:
1009          case ANY_E_NOK:
1010          case ANY_E_PIPES_FULL:
1011          case ANY_E_REG_PAR_UNKNOWN:
1012          case ANY_E_PIPE_NOT_OPENED:
1013          case ANY_E_CMD_NOT_SUPPORTED:
1014          case ANY_E_TIMEOUT:
1015          case ANY_E_REG_ACCESS_DENIED:
1016          case ANY_E_PIPE_ACCESS_DENIED:
1017          {
1018              /* Receive Error Notification to the Upper Layer */
1019              status = PHNFCSTVAL( CID_NFC_HCI, \
1020                              message->msg_header);
1021              phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, length );
1022              /* Return Success as the Error Sequence is already handled */
1023              psHciContext->response_pending = FALSE ;
1024              HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
1025              __FUNCTION__);
1026              status = NFCSTATUS_SUCCESS;
1027              break;
1028          }
1029              /* The Statement should not reach this case */
1030          /* case ANY_OK: */
1031          default:
1032          {
1033              /* status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE); */
1034              break;
1035          }
1036      }
1037  #else
1038      status = PHNFCSTVAL( CID_NFC_HCI, message->msg_header);
1039      HCI_DEBUG("HCI Error Response(%u) from the Device \n", instruction);
1040      psHciContext->response_pending = FALSE ;
1041      HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
1042          __FUNCTION__);
1043      phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
1044      phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
1045      /* Return Success as the Error Sequence is already handled */
1046      status = NFCSTATUS_SUCCESS;
1047  #endif
1048  
1049      return status;
1050  }
1051  
1052  
1053  static
1054   NFCSTATUS
phHciNfc_Process_Event(phHciNfc_sContext_t * psHciContext,void * pHwRef,void * pdata,uint8_t length)1055   phHciNfc_Process_Event (
1056                                  phHciNfc_sContext_t     *psHciContext,
1057                                  void                    *pHwRef,
1058                                  void                    *pdata,
1059  #ifdef ONE_BYTE_LEN
1060                                  uint8_t                 length
1061  #else
1062                                  uint16_t                length
1063  #endif
1064                          )
1065  {
1066      phHciNfc_HCP_Packet_t   *packet = NULL;
1067      phHciNfc_HCP_Message_t  *message = NULL;
1068      phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
1069      uint8_t                 instruction=0;
1070      uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
1071  
1072      NFCSTATUS               status = NFCSTATUS_SUCCESS;
1073  
1074      packet = (phHciNfc_HCP_Packet_t *)pdata;
1075      message = &packet->msg.message;
1076      /* Get the instruction bits from the Message Header */
1077      PHNFC_UNUSED_VARIABLE(instruction);
1078      instruction = (uint8_t) GET_BITS8( message->msg_header,
1079                                  HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
1080      /* Get the Pipe ID from the HCP Header */
1081      pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
1082                                  HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
1083  
1084      if (pipe_id >=  PHHCINFC_MAX_PIPE )
1085      {
1086          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
1087      }
1088      else
1089      {
1090          p_pipe_info = psHciContext->p_pipe_list[pipe_id];
1091      }
1092  
1093      if( (p_pipe_info != NULL ) )
1094      {
1095          if( NULL != p_pipe_info->recv_event)
1096          {
1097              status = p_pipe_info->recv_event( psHciContext, pHwRef,
1098                                                          pdata, length );
1099          }
1100          else
1101          {
1102              HCI_DEBUG(" Event Handling Not Supported by the #%u Pipe \n",
1103                                                          pipe_id);
1104              status = PHNFCSTVAL(CID_NFC_HCI,
1105                                          NFCSTATUS_FEATURE_NOT_SUPPORTED);
1106          }
1107      }
1108      else
1109      {
1110          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1111      }
1112  
1113      HCI_DEBUG("HCI: In Function: %s \n",
1114          __FUNCTION__);
1115      HCI_DEBUG("HCI: Response Pending status --> %s \n",
1116          (psHciContext->response_pending)?"TRUE":"FALSE");
1117      HCI_DEBUG("HCI: Event Pending status --> %s \n",
1118          (psHciContext->event_pending)?"TRUE":"FALSE");
1119  
1120          if ((TRUE == psHciContext->response_pending)
1121          || (TRUE == psHciContext->event_pending))
1122      {
1123          (void)memset(psHciContext->recv_buffer,
1124              FALSE, PHHCINFC_MAX_BUFFERSIZE);
1125          (void)memset((void *)&psHciContext->rx_packet,
1126              FALSE, sizeof(phHciNfc_HCP_Packet_t));
1127  
1128          /* Reset the Received Data Index */
1129          psHciContext->rx_index = ZERO;
1130          /* Reset the size of the total response data received */
1131          psHciContext->rx_total = ZERO;
1132  
1133          /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
1134          /* Receive the Response Packet */
1135  
1136          status = phHciNfc_Receive( psHciContext, pHwRef,
1137                      (uint8_t *)(&psHciContext->rx_packet),
1138                      sizeof(phHciNfc_HCP_Packet_t) );
1139  
1140          /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
1141          Status = %02X\n",status); */
1142      }
1143      else
1144      {
1145          if(
1146  /* #define EVENT_NOTIFY */
1147  #ifndef EVENT_NOTIFY
1148              ( NFCSTATUS_SUCCESS == status  )
1149              || ( NFCSTATUS_RF_TIMEOUT == status  )
1150              || (( NFCSTATUS_MORE_INFORMATION == status  )
1151  #else
1152              ((FALSE == psHciContext->event_pending )
1153  #endif
1154              && ( pipe_id <= PHHCINFC_MAX_PIPE ))
1155              )
1156          {
1157              /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
1158              status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
1159  
1160          }/* End of Success Status validation */
1161          else
1162          {
1163              HCI_DEBUG(" HCI: Pipe-ID --> %02X  \n", pipe_id);
1164              HCI_DEBUG(" HCI: PROCESS EVENT - Pending/Invalid Status : %X\n", status);
1165          }
1166      }
1167  
1168      return status;
1169  }
1170  
1171  static
1172   NFCSTATUS
phHciNfc_Process_Command(phHciNfc_sContext_t * psHciContext,void * pHwRef,void * pdata,uint8_t length)1173   phHciNfc_Process_Command (
1174                                  phHciNfc_sContext_t     *psHciContext,
1175                                  void                    *pHwRef,
1176                                  void                    *pdata,
1177  #ifdef ONE_BYTE_LEN
1178                                  uint8_t                 length
1179  #else
1180                                  uint16_t                length
1181  #endif
1182                               )
1183  {
1184      phHciNfc_HCP_Packet_t   *packet = NULL;
1185      phHciNfc_HCP_Message_t  *message = NULL;
1186      phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
1187      uint8_t                 instruction=0;
1188      uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
1189  
1190      NFCSTATUS               status = NFCSTATUS_SUCCESS;
1191  
1192      packet = (phHciNfc_HCP_Packet_t *)pdata;
1193      message = &packet->msg.message;
1194      /* Get the instruction bits from the Message Header */
1195      PHNFC_UNUSED_VARIABLE(instruction);
1196  
1197      instruction = (uint8_t) GET_BITS8( message->msg_header,
1198                                  HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
1199      /* Get the Pipe ID from the HCP Header */
1200      pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
1201                                  HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
1202      if (pipe_id >=  PHHCINFC_MAX_PIPE )
1203      {
1204          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
1205      }
1206      else
1207      {
1208          p_pipe_info = psHciContext->p_pipe_list[pipe_id];
1209      }
1210  
1211      if( (p_pipe_info != NULL )
1212          )
1213      {
1214          if( NULL != p_pipe_info->recv_cmd)
1215          {
1216              status = p_pipe_info->recv_cmd( psHciContext,   pHwRef,
1217                                                          pdata, length );
1218          }
1219          else
1220          {
1221              HCI_DEBUG(" Command Handling Not Supported by the #%u Pipe \n",
1222                                                          pipe_id);
1223              status = PHNFCSTVAL(CID_NFC_HCI,
1224                                          NFCSTATUS_FEATURE_NOT_SUPPORTED);
1225          }
1226      }
1227      else
1228      {
1229          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1230      }
1231  
1232      HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
1233      HCI_DEBUG("HCI: Response Pending status --> %s \n",
1234          (psHciContext->response_pending)?"TRUE":"FALSE");
1235  
1236      if(( NFCSTATUS_SUCCESS == status )
1237          && (TRUE != psHciContext->response_pending)
1238          )
1239      {
1240          /* Reset the Pipe Information Stored in the particular Pipe */
1241          /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
1242          /* Resume the Execution Sequence */
1243          status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
1244  
1245      }/* End of Success Status validation */
1246  
1247      return status;
1248  }
1249  
1250  
1251  static
1252  void
phHciNfc_Build_HCPMessage(phHciNfc_HCP_Packet_t * hcp_packet,uint8_t msg_type,uint8_t instruction)1253  phHciNfc_Build_HCPMessage(
1254                                  phHciNfc_HCP_Packet_t *hcp_packet,
1255                                  uint8_t             msg_type,
1256                                  uint8_t             instruction
1257                            )
1258  {
1259      phHciNfc_HCP_Message_t  *hcp_message = NULL;
1260  
1261      hcp_message = &(hcp_packet->msg.message);
1262      /* Set the type to the provided message type in the HCP Message Header */
1263      hcp_message->msg_header = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_TYPE_OFFSET,
1264                  HCP_MSG_TYPE_LEN, msg_type);
1265      /* Set the instruction to the kind of instruction in the HCP Message Header */
1266      hcp_message->msg_header  = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_INSTRUCTION_OFFSET,
1267                  HCP_MSG_INSTRUCTION_LEN, instruction);
1268      /* hcp_message->msg_header = hcp_message->msg_header | temp ; */
1269  
1270  }
1271  
1272  
1273  static
1274  void
phHciNfc_Build_HCPHeader(phHciNfc_HCP_Packet_t * hcp_packet,uint8_t chainbit,uint8_t pipe_id)1275  phHciNfc_Build_HCPHeader(
1276                                  phHciNfc_HCP_Packet_t *hcp_packet,
1277                                  uint8_t             chainbit,
1278                                  uint8_t             pipe_id
1279                            )
1280  {
1281      /* Set the Chaining bit to the default type */
1282      hcp_packet->hcp_header = (uint8_t) SET_BITS8(hcp_packet->hcp_header,
1283                  HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN, chainbit);
1284      /* Populate the Pipe ID to the HCP Header */
1285      hcp_packet->hcp_header  = (uint8_t) SET_BITS8(hcp_packet->hcp_header,HCP_PIPEID_OFFSET,
1286                  HCP_PIPEID_LEN, pipe_id);
1287  
1288  }
1289  
1290  /*!
1291   * \brief Builds the HCP Frame Packet.
1292   *
1293   * This function builds the HCP Frame in the HCP packet format to send to the
1294   * connected reader device.
1295   */
1296  
1297  void
phHciNfc_Build_HCPFrame(phHciNfc_HCP_Packet_t * hcp_packet,uint8_t chainbit,uint8_t pipe_id,uint8_t msg_type,uint8_t instruction)1298   phHciNfc_Build_HCPFrame (
1299                                  phHciNfc_HCP_Packet_t *hcp_packet,
1300                                  uint8_t             chainbit,
1301                                  uint8_t             pipe_id,
1302                                  uint8_t             msg_type,
1303                                  uint8_t             instruction
1304                            )
1305  {
1306      /* Fills the HCP Header in the packet */
1307      phHciNfc_Build_HCPHeader( hcp_packet,chainbit,pipe_id );
1308      /* Fills the HCP Message in the packet */
1309      phHciNfc_Build_HCPMessage( hcp_packet,msg_type,instruction );
1310  }
1311  
1312  /*!
1313   * \brief Appends the HCP Frame Packet.
1314   *
1315   * This function Appends the HCP Frame of the HCP packet to complete the
1316   * entire HCP Packet.
1317   */
1318  
1319  void
phHciNfc_Append_HCPFrame(uint8_t * hcp_data,uint16_t hcp_index,uint8_t * src_data,uint16_t src_len)1320   phHciNfc_Append_HCPFrame (
1321  /*                              phHciNfc_sContext_t     *psHciContext, */
1322                                  uint8_t                 *hcp_data,
1323                                  uint16_t                hcp_index,
1324                                  uint8_t                 *src_data,
1325                                  uint16_t                src_len
1326                            )
1327  {
1328      uint16_t src_index = 0;
1329      if( (NULL != src_data)
1330          /* && (hcp_index >= 0) */
1331          && (src_len > 0)
1332          )
1333      {
1334          for(src_index=0; src_index < src_len ; src_index++)
1335          {
1336              hcp_data[hcp_index + src_index] = src_data[src_index];
1337          }
1338      }
1339      return;
1340  }
1341  
1342  
1343  /*!
1344   * \brief Sends the Generic HCI Commands to the connected reader device.
1345   *
1346   * This function Sends the Generic HCI Command frames in the HCP packet format to the
1347   * connected reader device.
1348   */
1349  
1350   NFCSTATUS
phHciNfc_Send_Generic_Cmd(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t pipe_id,uint8_t cmd)1351   phHciNfc_Send_Generic_Cmd (
1352                                  phHciNfc_sContext_t *psHciContext,
1353                                  void                *pHwRef,
1354                                  uint8_t             pipe_id,
1355                                  uint8_t             cmd
1356                      )
1357   {
1358      phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
1359      phHciNfc_HCP_Message_t  *hcp_message = NULL;
1360      phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
1361      uint16_t                 length = 0;
1362      uint16_t                 i=0;
1363      NFCSTATUS               status = NFCSTATUS_SUCCESS;
1364  
1365      if((NULL == psHciContext)
1366          || ( pipe_id > PHHCINFC_MAX_PIPE)
1367          ||(NULL == psHciContext->p_pipe_list[pipe_id])
1368        )
1369      {
1370          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1371          HCI_DEBUG("%s: Invalid Arguments passed \n",
1372                                                  "phHciNfc_Send_Generic_Cmd");
1373      }
1374      else
1375      {
1376          p_pipe_info = (phHciNfc_Pipe_Info_t *)
1377                                  psHciContext->p_pipe_list[pipe_id];
1378          psHciContext->tx_total = 0 ;
1379          length +=  HCP_HEADER_LEN ;
1380          switch( cmd )
1381          {
1382              case ANY_SET_PARAMETER:
1383              {
1384  
1385                  hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
1386                  /* Construct the HCP Frame */
1387                  phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
1388                                          (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
1389                  hcp_message = &(hcp_packet->msg.message);
1390                  hcp_message->payload[i++] = p_pipe_info->reg_index ;
1391                  phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
1392                                              i, (uint8_t *)p_pipe_info->param_info,
1393                                              p_pipe_info->param_length);
1394                  length =(uint16_t)(length + i + p_pipe_info->param_length);
1395                  break;
1396              }
1397              case ANY_GET_PARAMETER:
1398              {
1399  
1400                  hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
1401                  /* Construct the HCP Frame */
1402                  phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
1403                                          (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
1404                  hcp_message = &(hcp_packet->msg.message);
1405                  hcp_message->payload[i++] = p_pipe_info->reg_index ;
1406                  length =(uint16_t)(length + i);
1407                  break;
1408              }
1409              case ANY_OPEN_PIPE:
1410              case ANY_CLOSE_PIPE:
1411              {
1412  
1413                  hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
1414                  /* Construct the HCP Frame */
1415                  phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
1416                                          (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
1417                  break;
1418              }
1419              default:
1420              {
1421                  status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED );
1422                  HCI_DEBUG("%s: Statement Should Not Occur \n","phHciNfc_Send_Generic_Cmd");
1423                  break;
1424              }
1425          }
1426          if( NFCSTATUS_SUCCESS == status )
1427          {
1428              p_pipe_info->sent_msg_type = HCP_MSG_TYPE_COMMAND;
1429              p_pipe_info->prev_msg = cmd;
1430              psHciContext->tx_total = length;
1431              psHciContext->response_pending = TRUE ;
1432              /* Send the Constructed HCP packet to the lower layer */
1433              status = phHciNfc_Send_HCP( psHciContext, pHwRef );
1434              p_pipe_info->prev_status = NFCSTATUS_PENDING;
1435          }
1436      }
1437  
1438      return status;
1439  }
1440  
1441  
1442  /*!
1443   * \brief Sets the parameter of the registers in a particular Pipe.
1444   *
1445   * This function configures the registers in a particular Pipe.
1446   */
1447  
1448  NFCSTATUS
phHciNfc_Set_Param(phHciNfc_sContext_t * psHciContext,void * pHwRef,phHciNfc_Pipe_Info_t * p_pipe_info,uint8_t reg_index,void * p_param,uint16_t param_length)1449  phHciNfc_Set_Param (
1450                                  phHciNfc_sContext_t     *psHciContext,
1451                                  void                    *pHwRef,
1452                                  phHciNfc_Pipe_Info_t    *p_pipe_info,
1453                                  uint8_t                 reg_index,
1454                                  void                    *p_param,
1455                                  uint16_t                 param_length
1456                      )
1457   {
1458      NFCSTATUS               status = NFCSTATUS_SUCCESS ;
1459  
1460      if( (NULL == p_pipe_info)
1461          || (NULL == p_param)
1462          || (0 == param_length)
1463          )
1464      {
1465          status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION );
1466      }
1467      else
1468      {
1469          p_pipe_info->param_info = (uint8_t *)p_param;
1470          p_pipe_info->param_length =  param_length;
1471          p_pipe_info->reg_index = reg_index;
1472          status = phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef,
1473              (uint8_t)p_pipe_info->pipe.pipe_id,
1474                  (uint8_t)ANY_SET_PARAMETER);
1475          p_pipe_info->prev_status = status;
1476      }
1477  
1478      return status;
1479   }
1480  
1481  
1482  #if 0
1483   /*!
1484   * \brief Gets the parameter of the registers in a particular Pipe.
1485   *
1486   * This function configures the registers in a particular Pipe.
1487   */
1488  
1489   NFCSTATUS
1490   phHciNfc_Get_Param (
1491                                  phHciNfc_sContext_t     *psHciContext,
1492                                  void                    *pHwRef,
1493                                  phHciNfc_Pipe_Info_t    *p_pipe_info,
1494                                  uint8_t                 reg_index,
1495                      )
1496   {
1497      NFCSTATUS               status = NFCSTATUS_SUCCESS ;
1498  
1499      return status;
1500   }
1501  #endif
1502  
1503  
1504  void
phHciNfc_Send_Complete(void * psContext,void * pHwRef,phNfc_sTransactionInfo_t * pInfo)1505  phHciNfc_Send_Complete (
1506                                  void                    *psContext,
1507                                  void                    *pHwRef,
1508                                  phNfc_sTransactionInfo_t *pInfo
1509                         )
1510  {
1511      NFCSTATUS               status = NFCSTATUS_SUCCESS ;
1512      uint16_t                length = 0;
1513  
1514      HCI_PRINT("HCI Send Completion....\n");
1515      if ( (NULL != psContext)
1516          && (NULL != pInfo)
1517          && (NULL != pHwRef)
1518          )
1519      {
1520          phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
1521          status = pInfo->status ;
1522          length = pInfo->length ;
1523          /* HCI_DEBUG("HCI Lower Layer Send Completion Before Receive,\
1524                                                  Status = %02X\n",status); */
1525          if(status != NFCSTATUS_SUCCESS)
1526          {
1527              /* Handle the Error Scenario */
1528              (void)memset(psHciContext->send_buffer,
1529                                              FALSE, PHHCINFC_MAX_BUFFERSIZE);
1530              /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE;*/
1531              phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
1532          }
1533          else
1534          {
1535  	        HCI_DEBUG("HCI Send Completion... Length = %02X\n", length);
1536              /* To complete the send complete with the send
1537               * or receive with chaining.
1538               */
1539              if( (TRUE == psHciContext->tx_hcp_chaining)
1540                  &&( psHciContext->tx_remain > HCP_ZERO_LEN ))
1541              {
1542                  /* Skip the HCP Header Byte Sent */
1543                  psHciContext->tx_remain -= length - 1;
1544  
1545                  /* Skip the HCP Header Byte Sent */
1546                  psHciContext->tx_hcp_frgmnt_index += length - 1;
1547  
1548                  /* Send the Remaining HCP Data Frames */
1549                  status = phHciNfc_Send_HCP( psHciContext, pHwRef );
1550  
1551                  HCI_DEBUG("HCI (Chaining) Send Resume: Status = %02X\n", status);
1552  
1553                  if( ( NFCSTATUS_SUCCESS != status )
1554                      && (NFCSTATUS_PENDING != status )
1555                      )
1556                  {
1557                      phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
1558                  }/* End of the Status check */
1559              }
1560              else
1561              {
1562                  psHciContext->tx_total = HCP_ZERO_LEN ;
1563                  psHciContext->tx_remain = HCP_ZERO_LEN ;
1564                  psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
1565                  HCI_DEBUG("HCI: %s: response_pending=%s, event_pending=%s",
1566                          __FUNCTION__,
1567                          (psHciContext->response_pending)?"TRUE":"FALSE",
1568                          (psHciContext->event_pending)?"TRUE":"FALSE"
1569                           );
1570                  if ((TRUE == psHciContext->response_pending)
1571                      || (TRUE == psHciContext->event_pending))
1572                  {
1573                      (void) memset(psHciContext->recv_buffer,
1574                          FALSE, PHHCINFC_MAX_BUFFERSIZE);
1575                      (void) memset((void *)&psHciContext->rx_packet,
1576                          FALSE, sizeof(phHciNfc_HCP_Packet_t));
1577  
1578                      /* Reset the Received Data Index */
1579                      psHciContext->rx_index = ZERO;
1580                      /* Reset the size of the total response data received */
1581                      psHciContext->rx_total = ZERO;
1582  
1583                      /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
1584                      /* Receive the Response Packet */
1585                      status = phHciNfc_Receive( psHciContext, pHwRef,
1586                                  (uint8_t *)(&psHciContext->rx_packet),
1587                                  sizeof(phHciNfc_HCP_Packet_t) );
1588  
1589                      /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
1590                      Status = %02X\n",status); */
1591  
1592                      if( ( NFCSTATUS_SUCCESS != status )
1593                           && (NFCSTATUS_PENDING != status )
1594                          )
1595                      {
1596                          phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
1597                      }/* End of the Status check */
1598                  }
1599                  else
1600                  {
1601                      status = phHciNfc_Resume_Sequence(psHciContext, pHwRef );
1602                  }
1603              }
1604  
1605          } /* End of status != Success */
1606  
1607      } /* End of Context != NULL  */
1608  }
1609  
1610  
1611  void
phHciNfc_Receive_Complete(void * psContext,void * pHwRef,phNfc_sTransactionInfo_t * pInfo)1612  phHciNfc_Receive_Complete (
1613                                  void                    *psContext,
1614                                  void                    *pHwRef,
1615                                  phNfc_sTransactionInfo_t *pInfo
1616                                  )
1617  {
1618      NFCSTATUS               status = NFCSTATUS_SUCCESS ;
1619      void                    *pdata = NULL ;
1620      uint16_t                length = 0 ;
1621  
1622      HCI_PRINT("HCI Receive Completion....\n");
1623      if ( (NULL != psContext)
1624          && (NULL != pInfo)
1625          && (NULL != pHwRef)
1626          )
1627      {
1628          phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
1629  
1630          status = pInfo->status ;
1631          pdata = pInfo->buffer ;
1632          length = pInfo->length ;
1633          HCI_DEBUG("HCI Lower Layer Receive Completion, Status = %02X\n",status);
1634          if( NFCSTATUS_SUCCESS != status )
1635          {
1636              /* Handle the Error Scenario */
1637              /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE; */
1638              phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t)length );
1639          }
1640          else
1641          {
1642               /* Receive the remaining Response Packet */
1643              /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_COMPLETE; */
1644              status = phHciNfc_Process_HCP( psHciContext, pHwRef, pdata,(uint8_t) length );
1645              if( ( NFCSTATUS_SUCCESS != status )
1646                  && (NFCSTATUS_PENDING != status )
1647                )
1648              {
1649                  phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
1650              }
1651          }
1652      }
1653  }
1654  
1655  void
phHciNfc_Notify(pphNfcIF_Notification_CB_t p_upper_notify,void * p_upper_context,void * pHwRef,uint8_t type,void * pInfo)1656  phHciNfc_Notify(
1657                      pphNfcIF_Notification_CB_t  p_upper_notify,
1658                      void                        *p_upper_context,
1659                      void                        *pHwRef,
1660                      uint8_t                     type,
1661                      void                        *pInfo
1662                 )
1663  {
1664      if( ( NULL != p_upper_notify) )
1665      {
1666          /* Notify the to the Upper Layer */
1667          (p_upper_notify)(p_upper_context, pHwRef, type, pInfo);
1668      }
1669  
1670  }
1671  
1672  
1673  void
phHciNfc_Tag_Notify(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t type,void * pInfo)1674  phHciNfc_Tag_Notify(
1675                              phHciNfc_sContext_t     *psHciContext,
1676                              void                    *pHwRef,
1677                              uint8_t                 type,
1678                              void                    *pInfo
1679                 )
1680  {
1681      phNfc_sCompletionInfo_t *psCompInfo =
1682                                  (phNfc_sCompletionInfo_t *)pInfo;
1683      pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
1684      void                        *pcontext = psHciContext->p_upper_context;
1685      NFCSTATUS               status = NFCSTATUS_SUCCESS;
1686  
1687      switch( psHciContext->hci_state.next_state )
1688      {
1689          case hciState_Activate:
1690          {
1691              /* Roll Back to the Select State */
1692              phHciNfc_FSM_Rollback(psHciContext);
1693              break;
1694          }
1695          case hciState_Select:
1696          {
1697              status = phHciNfc_FSM_Complete(psHciContext);
1698              break;
1699          }
1700          default:
1701          {
1702              /* Roll Back to the Select State */
1703              phHciNfc_FSM_Rollback(psHciContext);
1704              break;
1705          }
1706  
1707      }
1708  
1709      if(NFCSTATUS_SUCCESS == status )
1710      {
1711              /* Notify the Tag Events to the Upper layer */
1712          phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
1713                                  type, psCompInfo);
1714      }
1715      else
1716      {
1717          phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
1718      }
1719  }
1720  
1721  
1722  void
phHciNfc_Target_Select_Notify(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t type,void * pInfo)1723  phHciNfc_Target_Select_Notify(
1724                              phHciNfc_sContext_t     *psHciContext,
1725                              void                    *pHwRef,
1726                              uint8_t                 type,
1727                              void                    *pInfo
1728                 )
1729  {
1730      phNfc_sCompletionInfo_t *psCompInfo =
1731                                  (phNfc_sCompletionInfo_t *)pInfo;
1732      pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
1733      void                        *pcontext = psHciContext->p_upper_context;
1734      NFCSTATUS               status = NFCSTATUS_SUCCESS;
1735  
1736      switch( psHciContext->hci_state.next_state )
1737      {
1738          case hciState_Listen:
1739          {
1740              /* Roll Back to the Select State */
1741              status = phHciNfc_FSM_Complete(psHciContext);
1742              break;
1743          }
1744          case hciState_Select:
1745          {
1746              status = phHciNfc_FSM_Complete(psHciContext);
1747              break;
1748          }
1749          default:
1750          {
1751              /* Roll Back to the Select State */
1752              phHciNfc_FSM_Rollback(psHciContext);
1753              break;
1754          }
1755  
1756      }
1757  
1758      if(NFCSTATUS_SUCCESS == status )
1759      {
1760              /* Notify the Tag Events to the Upper layer */
1761          phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
1762                                  type, psCompInfo);
1763      }
1764      else
1765      {
1766          phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
1767      }
1768  
1769  }
1770  
1771  
1772  
1773  
1774  void
phHciNfc_Release_Notify(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t type,void * pInfo)1775  phHciNfc_Release_Notify(
1776                              phHciNfc_sContext_t     *psHciContext,
1777                              void                    *pHwRef,
1778                              uint8_t                 type,
1779                              void                    *pInfo
1780                 )
1781  {
1782      phNfc_sCompletionInfo_t *psCompInfo =
1783                                  (phNfc_sCompletionInfo_t *)pInfo;
1784      pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
1785      void                        *pcontext = psHciContext->p_upper_context;
1786      phHciNfc_Release_Resources( &psHciContext );
1787          /* Notify the Failure to the Upper Layer */
1788      phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
1789                              type, psCompInfo);
1790  }
1791  
1792  
1793  void
phHciNfc_Notify_Event(void * psContext,void * pHwRef,uint8_t type,void * pInfo)1794  phHciNfc_Notify_Event(
1795                              void                    *psContext,
1796                              void                    *pHwRef,
1797                              uint8_t                 type,
1798                              void                    *pInfo
1799                      )
1800  {
1801      NFCSTATUS            status = NFCSTATUS_SUCCESS;
1802  
1803      if ( (NULL != psContext)
1804          && (NULL != pInfo)
1805          && (NULL != pHwRef)
1806          )
1807      {
1808          phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
1809  
1810          /* Process based on the Notification type */
1811          switch(type)
1812          {
1813              case NFC_NOTIFY_INIT_COMPLETED:
1814              {
1815                  phNfc_sCompletionInfo_t *psCompInfo =
1816                                              (phNfc_sCompletionInfo_t *)pInfo;
1817                  if(NFCSTATUS_SUCCESS == psCompInfo->status)
1818                  {
1819  
1820  #if  (NXP_NFC_HCI_TIMER == 1)
1821                      if ( NXP_INVALID_TIMER_ID == hci_resp_timer_id )
1822                      {
1823                          /* Create and Intialise the Response Timer */
1824                          hci_resp_timer_id = phOsalNfc_Timer_Create( );
1825                          HCI_DEBUG(" HCI : Timer %X Created \n",
1826                                                              hci_resp_timer_id);
1827                      }
1828                      else
1829                      {
1830                          HCI_DEBUG(" HCI : Timer Already Created, Timer ID : %X\n",
1831                                                                  hci_resp_timer_id);
1832                      }
1833                      gpsHciContext = psHciContext;
1834  
1835  #endif /* (NXP_NFC_HCI_TIMER == 1) */
1836  
1837                       /* Complete the Initialisation Sequence */
1838                      status = phHciNfc_Resume_Sequence(psContext ,pHwRef);
1839                  }
1840                  else
1841                  {
1842                      /* Notify the Error Scenario to the Upper Layer */
1843                      phHciNfc_Notify(psHciContext->p_upper_notify,
1844                                      psHciContext->p_upper_context, pHwRef,
1845                                              NFC_NOTIFY_ERROR, psCompInfo);
1846                  }
1847                  break;
1848              }
1849              case NFC_NOTIFY_INIT_FAILED:
1850              {
1851                   /* Notify the Failure to the Upper Layer */
1852                  phHciNfc_Release_Notify( psContext,pHwRef,
1853                                          type, pInfo );
1854                  break;
1855              }
1856              case NFC_NOTIFY_RECV_COMPLETED:
1857              {
1858                  /* Receive Completed from the Lower Layer */
1859                  phHciNfc_Receive_Complete(psContext,pHwRef,pInfo);
1860  
1861                  break;
1862              }
1863              case NFC_NOTIFY_SEND_COMPLETED:
1864              {
1865                  /* Receive Completed from the Lower Layer */
1866                  phHciNfc_Send_Complete(psContext,pHwRef,pInfo);
1867  
1868                  break;
1869              }
1870              case NFC_NOTIFY_TRANSCEIVE_COMPLETED:
1871              {
1872                  /* TODO: TO handle Both Send and Receive Complete */
1873                  break;
1874              }
1875              case NFC_NOTIFY_TARGET_DISCOVERED:
1876              {
1877                  HCI_PRINT(" PICC Discovery ! Obtain PICC Info .... \n");
1878                  /* psHciContext->hci_seq = PL_DURATION_SEQ; */
1879                  if ( hciState_Unknown == psHciContext->hci_state.next_state )
1880                  {
1881  
1882                      status = phHciNfc_FSM_Update ( psHciContext, hciState_Select );
1883  
1884  
1885                      if (NFCSTATUS_SUCCESS != status)
1886                      {
1887                         status = phHciNfc_ReaderMgmt_Deselect(
1888                              psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE);
1889                      }
1890                  }
1891                  else
1892                  {
1893  #ifdef SW_RELEASE_TARGET
1894                      /*status = phHciNfc_ReaderMgmt_Deselect(
1895                          psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE); */
1896                      psHciContext->target_release = TRUE;
1897  #else
1898                      status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
1899  #endif
1900                  }
1901                  break;
1902              }
1903              /* To Notify the Target Released Notification
1904               * to the Above Layer */
1905              case NFC_NOTIFY_TARGET_RELEASED:
1906              /* To Notify the NFC Secure Element Transaction
1907               * Information to the Above Layer */
1908              /* case NFC_NOTIFY_TRANSACTION: */
1909              /* To Notify the Generic Events To the Upper
1910               * Layer */
1911              case NFC_NOTIFY_EVENT:
1912              /* To Notify the Data Receive  Notification
1913               * to the Above Layer */
1914              case NFC_NOTIFY_RECV_EVENT:
1915              {
1916                  phNfc_sCompletionInfo_t *psCompInfo =
1917  		                (phNfc_sCompletionInfo_t *)pInfo;
1918  
1919                  if (((TRUE == psHciContext->event_pending) ||
1920                      (NFCSTATUS_RF_TIMEOUT == psCompInfo->status))
1921                      && ( hciState_Transact == psHciContext->hci_state.next_state))
1922                  {
1923                      /* Rollback due to Transmission Error */
1924                      phHciNfc_FSM_Rollback(psHciContext);
1925                  }
1926                  psHciContext->event_pending = FALSE;
1927                  phHciNfc_Notify(psHciContext->p_upper_notify,
1928                              psHciContext->p_upper_context, pHwRef,
1929                                  type, pInfo);
1930                  break;
1931              }
1932              case NFC_NOTIFY_DEVICE_ACTIVATED:
1933              {
1934                  HCI_PRINT("  Device Activated! Obtaining Remote Reader Info .... \n");
1935                  if ( hciState_Unknown == psHciContext->hci_state.next_state )
1936                  {
1937                      switch (psHciContext->host_rf_type)
1938                      {
1939                          case phHal_eISO14443_A_PCD:
1940                          case phHal_eISO14443_B_PCD:
1941                          case phHal_eISO14443_BPrime_PCD:
1942                          case phHal_eFelica_PCD:
1943                          {
1944                              break;
1945                          }
1946                          case phHal_eNfcIP1_Initiator:
1947                          case phHal_eNfcIP1_Target:
1948                          {
1949                              break;
1950                          }
1951                          case phHal_eUnknown_DevType:
1952                          default:
1953                          {
1954                              status = PHNFCSTVAL(CID_NFC_HCI,
1955                                          NFCSTATUS_INVALID_PARAMETER);
1956                              break;
1957                          }
1958  
1959                      }
1960                      status = phHciNfc_FSM_Update ( psHciContext, hciState_Listen );
1961                  }
1962                  else
1963                  {
1964                      status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
1965                  }
1966                  break;
1967              }
1968              case NFC_NOTIFY_DEVICE_DEACTIVATED:
1969              {
1970                  HCI_PRINT(" Device De-Activated! \n");
1971                  if ( hciState_Unknown == psHciContext->hci_state.next_state )
1972                  {
1973                      status = phHciNfc_FSM_Update ( psHciContext, hciState_Initialise );
1974                      if(NFCSTATUS_SUCCESS == status)
1975                      {
1976                          /* Complete to the Select State */
1977                          status = phHciNfc_FSM_Complete(psHciContext);
1978                      }
1979                      else
1980                      {
1981                          HCI_PRINT(" Device Deactivated.. But Invalid State \n");
1982                          status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
1983                      }
1984                  }
1985                  else
1986                  {
1987                      status = phHciNfc_ReaderMgmt_Update_Sequence(
1988                                                  psHciContext, INFO_SEQ );
1989  
1990                      if(( hciState_Listen == psHciContext->hci_state.next_state)
1991                          || (hciState_Transact == psHciContext->hci_state.next_state))
1992                      {
1993                          psHciContext->hci_state.next_state = hciState_Initialise;
1994                          /* Roll Back to the Default State */
1995                          status = phHciNfc_FSM_Complete(psHciContext);
1996                      }
1997                  }
1998                  psHciContext->event_pending = FALSE;
1999                  phHciNfc_Notify(psHciContext->p_upper_notify,
2000                              psHciContext->p_upper_context, pHwRef,
2001                              NFC_NOTIFY_EVENT, pInfo);
2002                  break;
2003              }
2004              case NFC_NOTIFY_DEVICE_ERROR:
2005              {
2006                  phNfc_sCompletionInfo_t *psCompInfo =
2007                                              (phNfc_sCompletionInfo_t *)pInfo;
2008  
2009                  psCompInfo->status = ( NFCSTATUS_BOARD_COMMUNICATION_ERROR
2010                                          != PHNFCSTATUS(psCompInfo->status))?
2011                                              NFCSTATUS_BOARD_COMMUNICATION_ERROR:
2012                                                  psCompInfo->status ;
2013  
2014  #if  (NXP_NFC_HCI_TIMER == 1)
2015  
2016                  if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
2017                  {
2018                      HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X",
2019                                                            psCompInfo->status);
2020                      /* Stop and Un-Intialise the Response Timer */
2021                      phOsalNfc_Timer_Stop( hci_resp_timer_id );
2022                  }
2023  
2024  #endif /* (NXP_NFC_HCI_TIMER == 1) */
2025  
2026                  phHciNfc_Notify(psHciContext->p_upper_notify,
2027                              psHciContext->p_upper_context, pHwRef,
2028                              (uint8_t) NFC_NOTIFY_DEVICE_ERROR, pInfo);
2029  
2030                  break;
2031              }
2032  
2033              case NFC_NOTIFY_ERROR:
2034              default:
2035              {
2036                  phNfc_sCompletionInfo_t *psCompInfo =
2037                                              (phNfc_sCompletionInfo_t *)pInfo;
2038  
2039  #if  (NXP_NFC_HCI_TIMER == 1)
2040  
2041                  if (( NFCSTATUS_BOARD_COMMUNICATION_ERROR == PHNFCSTATUS(psCompInfo->status))
2042                          && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id ))
2043                  {
2044                      HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X",
2045                                                            psCompInfo->status);
2046                      /* Stop the HCI Response Timer */
2047                      phOsalNfc_Timer_Stop( hci_resp_timer_id );
2048                  }
2049  
2050  #endif /* (NXP_NFC_HCI_TIMER == 1) */
2051  
2052                  phHciNfc_Error_Sequence( psHciContext, pHwRef,
2053                                                          psCompInfo->status, NULL, 0);
2054                  break;
2055              }
2056          } /* End of Switch */
2057      } /* End of Context != NULL  */
2058  }
2059  
2060