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 * \file  phHciNfc_WI.c                                                        *
22 * \brief HCI WI gate Management Routines.                                     *
23 *                                                                             *
24 *                                                                             *
25 * Project: NFC-FRI-1.1                                                        *
26 *                                                                             *
27 * $Date: Tue Aug 18 10:22:34 2009 $                                           *
28 * $Author: ing04880 $                                                         *
29 * $Revision: 1.33 $                                                            *
30 * $Aliases: NFC_FRI1.1_WK934_R31_1,NFC_FRI1.1_WK941_PREP1,NFC_FRI1.1_WK941_PREP2,NFC_FRI1.1_WK941_1,NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $                                                                *                                                                             *
31 * =========================================================================== *
32 */
33 
34 /*
35 ***************************** Header File Inclusion ****************************
36 */
37 #include <phNfcCompId.h>
38 #include <phHciNfc_Pipe.h>
39 #include <phHciNfc_WI.h>
40 #include <phOsalNfc.h>
41 #include <phHciNfc_Emulation.h>
42 /*
43 ****************************** Macro Definitions *******************************
44 */
45 /*  WI gate specific Events definition */
46 #define NXP_EVT_SE_START_OF_TRANSACTION (0x01U)
47 #define NXP_EVT_SE_END_OF_TRANSACTION   (0x02U)
48 #define NXP_EVT_SE_SWITCH_MODE          (0x03U)
49 #define NXP_EVT_SE_TRANSACTION          (0x04U)
50 
51 /* WI Gate registry Settings */
52 /* set default mode mode as virtual mode */
53 #define NXP_SE_DEFAULTMODE_INDEX        (0x01)
54 #define NXP_SE_EVENTS_INDEX             (0x05)
55 
56 /* Set Bit 0 and Bit 1 to report Start of transaction and End of transaction*/
57 #define WI_ENABLE_EVENTS                (0x04)
58 #define WI_VIRTUALMODE                  (0x01)
59 #define WI_OFFMODE                      (0x00)
60 #define AID_SIZE                        (0x20)
61 /****************** Structure and Enumeration ****************************/
62 
63 
64 /****************** Static Function Declaration **************************/
65 
66 static uint8_t paypass_removal[2]          = {0x50, 0x00};
67 static uint8_t mifare_access               = 0x60;
68 
69 static
70 NFCSTATUS
71 phHciNfc_Recv_WI_Response(
72                               void  *psContext,
73                               void  *pHwRef,
74                               uint8_t *pResponse,
75 #ifdef ONE_BYTE_LEN
76                               uint8_t            length
77 #else
78                               uint16_t           length
79 #endif
80                          );
81 
82 static
83 NFCSTATUS
84 phHciNfc_Recv_WI_Event(
85                         void    *psContext,
86                         void    *pHwRef,
87                         uint8_t *pEvent,
88 #ifdef ONE_BYTE_LEN
89                         uint8_t            length
90 #else
91                         uint16_t           length
92 #endif
93                     );
94 
95 static
96 NFCSTATUS
97 phHciNfc_Send_WI_Event(
98                         phHciNfc_sContext_t     *psHciContext,
99                         void                    *pHwRef,
100                         uint8_t                 pipe_id,
101                         uint8_t                 event
102                     );
103 
104 static
105 NFCSTATUS
106 phHciNfc_WI_InfoUpdate(
107                             phHciNfc_sContext_t     *psHciContext,
108                             uint8_t                 index,
109                             uint8_t                 *reg_value,
110                             uint8_t                 reg_length
111                             );
112 
113 
114 #if defined (WI_UPDATE_SEQ)
115 static
116 NFCSTATUS
117 phHciNfc_WI_Update_Sequence(
118                                 phHciNfc_sContext_t     *psHciContext,
119                                 phHciNfc_eSeqType_t     WI_seq
120                          );
121 #endif /* #if defined (WI_UPDATE_SEQ) */
122 
123 /*
124 *************************** Function Definitions ***************************
125 */
126 
127 
128 
129 NFCSTATUS
phHciNfc_WI_Init_Resources(phHciNfc_sContext_t * psHciContext)130 phHciNfc_WI_Init_Resources(
131                            phHciNfc_sContext_t  *psHciContext
132                           )
133 {
134     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
135     phHciNfc_WI_Info_t          *p_WI_info=NULL;
136 
137     if( NULL == psHciContext )
138     {
139         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
140     }
141     else
142     {
143         if(( NULL == psHciContext->p_wi_info ) &&
144              (phHciNfc_Allocate_Resource((void **)(&p_WI_info),
145             sizeof(phHciNfc_WI_Info_t))== NFCSTATUS_SUCCESS))
146         {
147             psHciContext->p_wi_info = p_WI_info;
148             p_WI_info->current_seq = eWI_PipeOpen;
149             p_WI_info->next_seq = eWI_PipeOpen;
150             p_WI_info->pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
151         }
152         else
153         {
154             status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
155         }
156 
157     }
158     return status;
159 }
160 
161 NFCSTATUS
phHciNfc_WI_Get_PipeID(phHciNfc_sContext_t * psHciContext,uint8_t * ppipe_id)162 phHciNfc_WI_Get_PipeID(
163                             phHciNfc_sContext_t        *psHciContext,
164                             uint8_t                    *ppipe_id
165                             )
166 {
167     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
168 
169     if( (NULL != psHciContext)
170         && ( NULL != ppipe_id )
171         && ( NULL != psHciContext->p_wi_info )
172         )
173     {
174         phHciNfc_WI_Info_t     *p_wi_info=NULL;
175         p_wi_info = (phHciNfc_WI_Info_t *)
176             psHciContext->p_wi_info ;
177         *ppipe_id =  p_wi_info->pipe_id  ;
178     }
179     else
180     {
181         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
182     }
183     return status;
184 }
185 
186 
187 NFCSTATUS
phHciNfc_WI_Update_PipeInfo(phHciNfc_sContext_t * psHciContext,uint8_t pipeID,phHciNfc_Pipe_Info_t * pPipeInfo)188 phHciNfc_WI_Update_PipeInfo(
189                                   phHciNfc_sContext_t     *psHciContext,
190                                   uint8_t                 pipeID,
191                                   phHciNfc_Pipe_Info_t    *pPipeInfo
192                            )
193 {
194     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
195 
196     if( NULL == psHciContext )
197     {
198         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
199     }
200     else if(NULL == psHciContext->p_wi_info)
201     {
202         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
203     }
204     else
205     {
206         phHciNfc_WI_Info_t *p_WI_info=NULL;
207         p_WI_info = (phHciNfc_WI_Info_t *)
208                                 psHciContext->p_wi_info ;
209         /* Update the pipe_id of the WI Gate obtained from HCI Response */
210         p_WI_info->pipe_id = pipeID;
211         p_WI_info->p_pipe_info = pPipeInfo;
212         if ( NULL != pPipeInfo)
213         {
214             /* Update the Response Receive routine of the WI Gate */
215             pPipeInfo->recv_resp = &phHciNfc_Recv_WI_Response;
216             /* Update the event Receive routine of the WI Gate */
217             pPipeInfo->recv_event = &phHciNfc_Recv_WI_Event;
218         }
219     }
220 
221     return status;
222 }
223 
224 #if defined (WI_UPDATE_SEQ)
225 static
226 NFCSTATUS
phHciNfc_WI_Update_Sequence(phHciNfc_sContext_t * psHciContext,phHciNfc_eSeqType_t WI_seq)227 phHciNfc_WI_Update_Sequence(
228                                 phHciNfc_sContext_t     *psHciContext,
229                                 phHciNfc_eSeqType_t     WI_seq
230                              )
231 {
232     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
233     phHciNfc_WI_Info_t          *p_WI_info=NULL;
234     if( NULL == psHciContext )
235     {
236         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
237     }
238     else if ( NULL == psHciContext->p_wi_info )
239     {
240         status = PHNFCSTVAL(CID_NFC_HCI,
241                         NFCSTATUS_INVALID_HCI_INFORMATION);
242     }
243     else
244     {
245         p_WI_info = (phHciNfc_WI_Info_t *)
246                             psHciContext->p_wi_info ;
247         switch(WI_seq)
248         {
249             case RESET_SEQ:
250             case INIT_SEQ:
251             {
252                 p_WI_info->current_seq = eWI_PipeOpen;
253                 p_WI_info->next_seq = eWI_SetDefaultMode ;
254             }break;
255             case UPDATE_SEQ:
256             {
257                 p_WI_info->current_seq = p_WI_info->next_seq;
258 
259             }break;
260             case REL_SEQ:
261             {
262                 p_WI_info->current_seq = eWI_PipeOpen;
263                 p_WI_info->next_seq = eWI_PipeClose ;
264             }break;
265             default:
266             {
267                 break;
268             }
269         }/* End of Update Sequence Switch */
270     }
271     return status;
272 
273 }
274 #endif /* #if defined (WI_UPDATE_SEQ) */
275 
276 NFCSTATUS
phHciNfc_WI_Configure_Default(void * psHciHandle,void * pHwRef,uint8_t enable_type)277 phHciNfc_WI_Configure_Default(
278                               void                  *psHciHandle,
279                               void                  *pHwRef,
280                               uint8_t               enable_type
281                           )
282 {
283     NFCSTATUS               status = NFCSTATUS_SUCCESS;
284     static uint8_t          param = 0;
285     phHciNfc_sContext_t     *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
286 
287     if( (NULL == psHciContext)||(NULL == pHwRef))
288     {
289       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
290     }
291     else if ( NULL == psHciContext->p_wi_info )
292     {
293         status = PHNFCSTVAL(CID_NFC_HCI,
294                         NFCSTATUS_INVALID_HCI_INFORMATION);
295     }
296     else
297     {
298         phHciNfc_WI_Info_t          *p_wi_info=NULL;
299         phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
300 
301         p_wi_info = (phHciNfc_WI_Info_t*)psHciContext->p_wi_info;
302 
303         p_pipe_info = p_wi_info->p_pipe_info;
304         if(NULL == p_pipe_info)
305         {
306             status = PHNFCSTVAL(CID_NFC_HCI,
307                             NFCSTATUS_INVALID_HCI_INFORMATION);
308         }
309         else
310         {
311             p_pipe_info->reg_index = NXP_SE_DEFAULTMODE_INDEX;
312             /* Enable/Disable Default Virtual Mode for SmartMx */
313             param = (uint8_t)enable_type;
314             p_pipe_info->param_info =(void*)&param ;
315             p_pipe_info->param_length = sizeof(param) ;
316             status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
317                                     p_wi_info->pipe_id,(uint8_t)ANY_SET_PARAMETER);
318 
319         }/* End of else part*/
320     }
321     return status;
322 }
323 
324 NFCSTATUS
phHciNfc_WI_Get_Default(void * psHciHandle,void * pHwRef)325 phHciNfc_WI_Get_Default(
326                               void                  *psHciHandle,
327                               void                  *pHwRef
328                               )
329 {
330     NFCSTATUS               status = NFCSTATUS_SUCCESS;
331     phHciNfc_sContext_t     *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
332 
333     if( (NULL == psHciContext)||(NULL == pHwRef))
334     {
335         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
336     }
337     else if ( NULL == psHciContext->p_wi_info )
338     {
339         status = PHNFCSTVAL(CID_NFC_HCI,
340                             NFCSTATUS_INVALID_HCI_INFORMATION);
341     }
342     else
343     {
344         phHciNfc_WI_Info_t          *p_wiinfo=NULL;
345         phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
346 
347         p_wiinfo = (phHciNfc_WI_Info_t*)psHciContext->p_wi_info;
348 
349         p_pipe_info = p_wiinfo->p_pipe_info;
350         if(NULL == p_pipe_info)
351         {
352             status = PHNFCSTVAL(CID_NFC_HCI,
353                                 NFCSTATUS_INVALID_HCI_INFORMATION);
354         }
355         else
356         {
357             p_pipe_info->reg_index = NXP_SE_DEFAULTMODE_INDEX;
358 
359             status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
360                                             p_wiinfo->pipe_id,
361                                             (uint8_t)ANY_GET_PARAMETER);
362 
363         }/* End of else part*/
364     }
365     return status;
366 }
367 
368 
369 NFCSTATUS
phHciNfc_WI_Configure_Mode(void * psHciHandle,void * pHwRef,phHal_eSmartMX_Mode_t e_smx_mode)370 phHciNfc_WI_Configure_Mode(
371                               void                *psHciHandle,
372                               void                *pHwRef,
373                               phHal_eSmartMX_Mode_t   e_smx_mode
374                           )
375 {
376     NFCSTATUS               status = NFCSTATUS_SUCCESS;
377     static uint8_t          param = 0;
378     phHciNfc_sContext_t     *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
379 
380     if( (NULL == psHciContext)||(NULL == pHwRef))
381     {
382       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
383     }
384     else if ( NULL == psHciContext->p_wi_info )
385     {
386         status = PHNFCSTVAL(CID_NFC_HCI,
387                         NFCSTATUS_INVALID_HCI_INFORMATION);
388     }
389     else
390     {
391         phHciNfc_WI_Info_t          *p_wi_info=NULL;
392         phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
393 
394         p_wi_info = (phHciNfc_WI_Info_t*)psHciContext->p_wi_info;
395 
396         p_pipe_info = p_wi_info->p_pipe_info;
397         if(NULL == p_pipe_info)
398         {
399             status = PHNFCSTVAL(CID_NFC_HCI,
400                             NFCSTATUS_INVALID_HCI_INFORMATION);
401         }
402         else
403         {
404             /* Switch the Mode of the SmartMx */
405             param = (uint8_t)e_smx_mode;
406             p_pipe_info->param_info =(void*)&param ;
407             p_pipe_info->param_length = sizeof(param) ;
408             status = phHciNfc_Send_WI_Event( psHciContext, pHwRef,
409                                 p_wi_info->pipe_id, NXP_EVT_SE_SWITCH_MODE );
410             /* Send the Success Status as this is an event */
411             status = ( (status == NFCSTATUS_PENDING)?
412                                 NFCSTATUS_SUCCESS : status);
413 
414         }/* End of else part*/
415     }
416     return status;
417 }
418 
419 
420 NFCSTATUS
phHciNfc_WI_Configure_Notifications(void * psHciHandle,void * pHwRef,phHciNfc_WI_Events_t eNotification)421 phHciNfc_WI_Configure_Notifications(
422                                     void        *psHciHandle,
423                                     void        *pHwRef,
424                                     phHciNfc_WI_Events_t eNotification
425                                 )
426 {
427     NFCSTATUS               status = NFCSTATUS_SUCCESS;
428     static uint8_t          param = 0;
429     phHciNfc_sContext_t     *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
430 
431     if( (NULL == psHciContext)||(NULL == pHwRef))
432     {
433       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
434     }
435     else if ( NULL == psHciContext->p_wi_info )
436     {
437         status = PHNFCSTVAL(CID_NFC_HCI,
438                         NFCSTATUS_INVALID_HCI_INFORMATION);
439     }
440     else
441     {
442         phHciNfc_WI_Info_t          *p_wi_info=NULL;
443         phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
444 
445 
446         p_wi_info = (phHciNfc_WI_Info_t*)psHciContext->p_wi_info;
447         p_pipe_info = p_wi_info->p_pipe_info;
448         if(NULL == p_pipe_info)
449         {
450             status = PHNFCSTVAL(CID_NFC_HCI,
451                                 NFCSTATUS_INVALID_HCI_INFORMATION);
452         }
453         else
454         {
455             if(eEnableEvents == eNotification)
456             {
457                 /* Enable start and end of transaction events*/
458                 param = WI_ENABLE_EVENTS;
459             }
460             else
461             {
462                 /* Disable Events*/
463                 param = FALSE ;
464             }
465             p_pipe_info->reg_index = NXP_SE_EVENTS_INDEX;
466             p_pipe_info->param_info =(void*)&param ;
467             p_pipe_info->param_length = sizeof(param) ;
468 
469             status = phHciNfc_Send_Generic_Cmd(psHciContext,pHwRef,
470             p_wi_info->pipe_id,(uint8_t)ANY_SET_PARAMETER);
471         }
472     }
473     return status;
474 }
475 
476 
477 /*!
478 * \brief Sends WI gate specfic HCI Events to the connected reader device.
479 * This function Sends the WI mode specific HCI Event frames in the HCP packet format to the
480 * connected reader device.
481 */
482 
483 static
484 NFCSTATUS
phHciNfc_Send_WI_Event(phHciNfc_sContext_t * psHciContext,void * pHwRef,uint8_t pipe_id,uint8_t event)485 phHciNfc_Send_WI_Event(
486                        phHciNfc_sContext_t      *psHciContext,
487                        void                     *pHwRef,
488                        uint8_t                  pipe_id,
489                        uint8_t                  event
490                        )
491 {
492     phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
493     phHciNfc_HCP_Message_t  *hcp_message = NULL;
494     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
495     uint8_t                 length = 0;
496     uint8_t                 i=0;
497     NFCSTATUS               status = NFCSTATUS_SUCCESS;
498 
499     if( (NULL == psHciContext)
500         || ( pipe_id > PHHCINFC_MAX_PIPE)
501         ||(NULL == psHciContext->p_pipe_list[pipe_id])
502         )
503     {
504         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
505         HCI_DEBUG("%s: Invalid Arguments passed \n",
506             "phHciNfc_Send_WI_Event");
507     }
508     else
509     {
510         p_pipe_info = (phHciNfc_Pipe_Info_t *)
511             psHciContext->p_pipe_list[pipe_id];
512         psHciContext->tx_total = 0 ;
513         length =length+HCP_HEADER_LEN ;
514 
515         if( NXP_EVT_SE_SWITCH_MODE == event)
516         {
517             hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
518             /* Construct the HCP Frame */
519             phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
520                                     (uint8_t) pipe_id, HCP_MSG_TYPE_EVENT, event);
521             hcp_message = &(hcp_packet->msg.message);
522             phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
523                                             i, (uint8_t *)p_pipe_info->param_info,
524                                             p_pipe_info->param_length);
525             length =(uint8_t)(length + i + p_pipe_info->param_length);
526         }
527         else
528         {
529             status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INSTRUCTION );
530             HCI_DEBUG("%s: Invalid Send Event Request \n","phHciNfc_Send_WI_Event");
531         }
532 
533         if( NFCSTATUS_SUCCESS == status )
534         {
535             p_pipe_info->sent_msg_type = HCP_MSG_TYPE_EVENT ;
536             p_pipe_info->prev_msg = event ;
537             psHciContext->tx_total = length;
538 
539             /* Send the Constructed HCP packet to the lower layer */
540             status = phHciNfc_Send_HCP( psHciContext, pHwRef );
541             p_pipe_info->prev_status = NFCSTATUS_PENDING;
542         }
543     }
544     return status;
545 }
546 
547 static
548 NFCSTATUS
phHciNfc_Recv_WI_Response(void * psContext,void * pHwRef,uint8_t * pResponse,uint8_t length)549 phHciNfc_Recv_WI_Response(
550                           void                *psContext,
551                           void                *pHwRef,
552                           uint8_t             *pResponse,
553 #ifdef ONE_BYTE_LEN
554                           uint8_t             length
555 #else
556                           uint16_t            length
557 #endif
558                           )
559 {
560     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
561     phHciNfc_sContext_t         *psHciContext =
562                                 (phHciNfc_sContext_t *)psContext;
563 
564 
565     if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
566         || (length == 0))
567     {
568         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
569     }
570     else if(NULL == psHciContext->p_wi_info)
571     {
572         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
573     }
574     else
575     {
576         phHciNfc_WI_Info_t     *p_wiinfo=NULL;
577         uint8_t                 prev_cmd = ANY_GET_PARAMETER;
578         p_wiinfo = (phHciNfc_WI_Info_t *)psHciContext->p_wi_info ;
579 
580         if( NULL == p_wiinfo->p_pipe_info)
581         {
582             status = PHNFCSTVAL(CID_NFC_HCI,
583                 NFCSTATUS_INVALID_HCI_INFORMATION);
584         }
585         else
586         {
587             prev_cmd = p_wiinfo->p_pipe_info->prev_msg ;
588             switch(prev_cmd)
589             {
590                 case ANY_GET_PARAMETER:
591                 {
592                     if (length > HCP_HEADER_LEN)
593                     {
594                         status = phHciNfc_WI_InfoUpdate (psHciContext,
595                                         p_wiinfo->p_pipe_info->reg_index,
596                                         &pResponse[HCP_HEADER_LEN],
597                                         (uint8_t)(length - HCP_HEADER_LEN));
598                     }
599                     else
600                     {
601                         status = PHNFCSTVAL(CID_NFC_HCI,
602                                             NFCSTATUS_INVALID_HCI_RESPONSE);
603                     }
604                     break;
605                 }
606                 case ANY_SET_PARAMETER:
607                 {
608                     HCI_PRINT("WI Parameter Set \n");
609                     status = phHciNfc_EmuMgmt_Update_Seq(psHciContext,
610                                                         UPDATE_SEQ);
611                     break;
612                 }
613                 case ANY_OPEN_PIPE:
614                 {
615                     HCI_PRINT("WI gate open pipe complete\n");
616                     status = phHciNfc_EmuMgmt_Update_Seq(psHciContext,
617                                                         UPDATE_SEQ);
618                     break;
619                 }
620                 case ANY_CLOSE_PIPE:
621                 {
622                     HCI_PRINT("WI close pipe complete\n");
623                     status = phHciNfc_EmuMgmt_Update_Seq(psHciContext,
624                                                         UPDATE_SEQ);
625                     break;
626                 }
627                 default:
628                 {
629                     status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
630                     break;
631                 }
632             }
633             if( NFCSTATUS_SUCCESS == status )
634             {
635                 p_wiinfo->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
636                 p_wiinfo->current_seq = p_wiinfo->next_seq;
637             }
638         }
639     }
640     return status;
641 }
642 
643 static
644 NFCSTATUS
phHciNfc_Recv_WI_Event(void * psContext,void * pHwRef,uint8_t * pEvent,uint8_t length)645 phHciNfc_Recv_WI_Event(
646                        void               *psContext,
647                        void               *pHwRef,
648                        uint8_t            *pEvent,
649 #ifdef ONE_BYTE_LEN
650                        uint8_t             length
651 #else
652                        uint16_t            length
653 #endif
654                        )
655 {
656     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
657     phHal_sEventInfo_t          EventInfo;
658     /* phNfc_sNotificationInfo_t   NotificationInfo; */
659     phHciNfc_sContext_t         *psHciContext =(phHciNfc_sContext_t *)psContext;
660 
661 
662     if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pEvent)
663         || (length == 0))
664     {
665         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
666     }
667     else if(NULL == psHciContext->p_wi_info)
668     {
669         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
670     }
671     else
672     {
673         phHciNfc_HCP_Packet_t       *p_packet = NULL;
674         phHciNfc_HCP_Message_t      *message = NULL;
675         phHciNfc_WI_Info_t          *p_wi_info = NULL;
676         uint8_t                     EventType = 0;
677 
678         p_wi_info = (phHciNfc_WI_Info_t *)psHciContext->p_wi_info ;
679 
680         p_packet = (phHciNfc_HCP_Packet_t *)pEvent;
681         message = &(p_packet->msg.message);
682         /* Get the instruction bits from the Message Header */
683         EventType = (uint8_t) GET_BITS8( message->msg_header,
684             HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
685 
686         EventInfo.eventHost = phHal_eHostController;
687         EventInfo.eventSource = phHal_ePICC_DevType;
688         /* Now check for possible Transaction events for reporting */
689         switch(EventType)
690         {
691             case NXP_EVT_SE_START_OF_TRANSACTION:
692             {
693                 EventInfo.eventType = NFC_EVT_START_OF_TRANSACTION;
694                 break;
695             }
696             case NXP_EVT_SE_END_OF_TRANSACTION:
697             {
698                 EventInfo.eventType = NFC_EVT_END_OF_TRANSACTION;
699                 break;
700             }
701             case NXP_EVT_SE_TRANSACTION:
702             {
703                 EventInfo.eventType = NFC_EVT_TRANSACTION;
704                 EventInfo.eventInfo.aid.buffer = (uint8_t *)p_wi_info->aid;
705                 /* check for AID data is at least 1 byte is their */
706                 if (length > HCP_HEADER_LEN)
707                 {
708                     EventInfo.eventInfo.aid.length = length - HCP_HEADER_LEN;
709                     memcpy((void *)p_wi_info->aid, message->payload,
710                            EventInfo.eventInfo.aid.length );
711                 }
712 
713                 /* Filter Transaction event */
714                 if (EventInfo.eventInfo.aid.length == 4)
715                 {
716                     EventInfo.eventType = NFC_EVT_APDU_RECEIVED;
717                 }
718                 else if (EventInfo.eventInfo.aid.length == 2)
719                 {
720                     if (!memcmp(paypass_removal, EventInfo.eventInfo.aid.buffer, EventInfo.eventInfo.aid.length))
721                     {
722                         EventInfo.eventType = NFC_EVT_EMV_CARD_REMOVAL;
723                     }
724                     else if(mifare_access == EventInfo.eventInfo.aid.buffer[0])
725                     {
726                         EventInfo.eventType = NFC_EVT_MIFARE_ACCESS;
727                     }
728                 }
729 
730                 EventInfo.eventInfo.aid.buffer = (uint8_t *)p_wi_info->aid;
731                 (void) memcpy((void *)p_wi_info->aid,message->payload,
732                                 EventInfo.eventInfo.aid.length );
733                 break;
734             }
735             default:
736             {
737                 status = PHNFCSTVAL(CID_NFC_HCI,
738                                 NFCSTATUS_INVALID_HCI_INSTRUCTION);
739                 break;
740             }
741         }
742         if (NFCSTATUS_SUCCESS == status )
743         {
744             phHciNfc_Notify_Event(  psHciContext, pHwRef,
745             NFC_NOTIFY_EVENT, (void*)&EventInfo);
746         }
747     }
748     return status;
749 }
750 
751 static
752 NFCSTATUS
phHciNfc_WI_InfoUpdate(phHciNfc_sContext_t * psHciContext,uint8_t index,uint8_t * reg_value,uint8_t reg_length)753 phHciNfc_WI_InfoUpdate(
754                        phHciNfc_sContext_t     *psHciContext,
755                        uint8_t                 index,
756                        uint8_t                 *reg_value,
757                        uint8_t                 reg_length
758                        )
759 {
760     NFCSTATUS               status = NFCSTATUS_SUCCESS;
761     phHciNfc_WI_Info_t      *p_wiinfo = NULL;
762 
763     p_wiinfo = psHciContext->p_wi_info;
764 
765     if ((NXP_SE_DEFAULTMODE_INDEX == index) &&
766         (sizeof(*reg_value) == reg_length))
767     {
768         p_wiinfo->default_type = *reg_value;
769     }
770     else
771     {
772         status = PHNFCSTVAL(CID_NFC_HCI,
773                             NFCSTATUS_INVALID_HCI_RESPONSE);
774     }
775 
776     return status;
777 }
778 
779