1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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  * Smart Card Completion Routing component
19  */
20 
21 #include <phNfcTypes.h>
22 #include<phFriNfc.h>
23 #include <phFriNfc_SmtCrdFmt.h>
24 #include <phNfcCompId.h>
25 #include <phFriNfc_MifStdFormat.h>
26 
27 
28 /*******************************************************************************
29 **
30 ** Function         phFriNfc_SmtCrdFmt_HCrHandler
31 **
32 ** Description      This function is called to complete Completion Routine when gets error.
33 **
34 ** Returns          none.
35 **
36 *******************************************************************************/
phFriNfc_SmtCrdFmt_HCrHandler(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,NFCSTATUS Status)37 void phFriNfc_SmtCrdFmt_HCrHandler(phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt, NFCSTATUS Status)
38 {
39     /* set the state back to the Reset_Init state*/
40     NdefSmtCrdFmt->State =  PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
41 
42     /* set the completion routine*/
43     NdefSmtCrdFmt->CompletionRoutine[PH_FRINFC_SMTCRDFMT_CR_FORMAT].
44         CompletionRoutine(NdefSmtCrdFmt->CompletionRoutine->Context, Status);
45 
46     return;
47 }
48 
49 /*******************************************************************************
50 **
51 ** Function         phFriNfc_NdefSmtCrd_Reset
52 **
53 ** Description      Resets the component instance to the initial state and initializes the
54 **                  internal variables.
55 **                  This function has to be called at the beginning, after creating an instance of
56 **                  phFriNfc_sNdefSmtCrdFmt_t.  Use this function to reset the instance and/or to switch
57 **                  to a different underlying card types.
58 **
59 ** Returns          NFCSTATUS_SUCCESS if operation successful.
60 **                  NFCSTATUS_INVALID_PARAMETER if at least one parameter of the function is invalid.
61 **
62 *******************************************************************************/
phFriNfc_NdefSmtCrd_Reset(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,void * LowerDevice,phHal_sRemoteDevInformation_t * psRemoteDevInfo,uint8_t * SendRecvBuffer,uint16_t * SendRecvBuffLen)63 NFCSTATUS phFriNfc_NdefSmtCrd_Reset(phFriNfc_sNdefSmtCrdFmt_t       *NdefSmtCrdFmt,
64                                     void                            *LowerDevice,
65                                     phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
66                                     uint8_t                         *SendRecvBuffer,
67                                     uint16_t                        *SendRecvBuffLen)
68 {
69     NFCSTATUS   result = NFCSTATUS_SUCCESS;
70     uint8_t     index;
71     if (    (SendRecvBuffLen == NULL) || (NdefSmtCrdFmt == NULL) || (psRemoteDevInfo == NULL) ||
72             (SendRecvBuffer == NULL) ||  (LowerDevice == NULL) ||
73             (*SendRecvBuffLen == 0) ||
74             (*SendRecvBuffLen < PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE) )
75     {
76         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
77     }
78     else
79     {
80         /* Initialize the state to Init */
81         NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
82 
83         for(index = 0;index<PH_FRINFC_SMTCRDFMT_CR;index++)
84         {
85             /* Initialize the NdefMap Completion Routine to Null */
86             NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
87             /* Initialize the NdefMap Completion Routine context to Null  */
88             NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
89         }
90 
91         /* Lower Device(Always Overlapped HAL Struct initialized in application
92          * is registered in NdefMap Lower Device)
93          */
94         NdefSmtCrdFmt->pTransceiveInfo = LowerDevice;
95 
96         /* Remote Device info received from Manual Device Discovery is registered here */
97         NdefSmtCrdFmt->psRemoteDevInfo = psRemoteDevInfo;
98 
99         /* Trx Buffer registered */
100         NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
101 
102         /* Trx Buffer Size */
103         NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
104 
105         /* Register Transfer Buffer Length */
106         NdefSmtCrdFmt->SendLength = 0;
107 
108         /* Initialize the Format status flag*/
109         NdefSmtCrdFmt->FmtProcStatus = 0;
110 
111         /* Reset the Card Type */
112         NdefSmtCrdFmt->CardType = 0;
113 
114         /* Reset MapCompletion Info*/
115         NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
116         NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
117 
118         /* Reset Mifare Standard Container elements*/
119         phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
120     }
121 
122     return (result);
123 }
124 
125 /*******************************************************************************
126 **
127 ** Function         phFriNfc_NdefSmtCrd_SetCR
128 **
129 ** Description      This function allows the caller to set a Completion Routine (notifier).
130 **
131 ** Returns          NFCSTATUS_SUCCESS if operation successful.
132 **                  NFCSTATUS_INVALID_PARAMETER if at least one parameter of the function is invalid.
133 **
134 *******************************************************************************/
phFriNfc_NdefSmtCrd_SetCR(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,uint8_t FunctionID,pphFriNfc_Cr_t CompletionRoutine,void * CompletionRoutineContext)135 NFCSTATUS phFriNfc_NdefSmtCrd_SetCR(phFriNfc_sNdefSmtCrdFmt_t     *NdefSmtCrdFmt,
136                                     uint8_t                       FunctionID,
137                                     pphFriNfc_Cr_t                CompletionRoutine,
138                                     void                          *CompletionRoutineContext)
139 {
140     NFCSTATUS   status = NFCSTATUS_SUCCESS;
141     if ((NdefSmtCrdFmt == NULL) || (FunctionID >= PH_FRINFC_SMTCRDFMT_CR) ||
142         (CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
143     {
144         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, NFCSTATUS_INVALID_PARAMETER);
145     }
146     else
147     {
148         /* Register the application callback with the NdefMap Completion Routine */
149         NdefSmtCrdFmt->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;
150 
151         /* Register the application context with the NdefMap Completion Routine context */
152         NdefSmtCrdFmt->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
153     }
154     return status;
155 }
156 
157 /*******************************************************************************
158 **
159 ** Function         phFriNfc_NdefSmtCrd_Process
160 **
161 ** Description      This function is called by the lower layer (OVR HAL)
162 **                  when an I/O operation has finished. The internal state machine decides
163 **                  whether to call into the lower device again or to complete the process
164 **                  by calling into the upper layer's completion routine, stored within this
165 **                  component's context (phFriNfc_sNdefSmtCrdFmt_t).
166 **
167 ** Returns          none.
168 **
169 *******************************************************************************/
phFriNfc_NdefSmtCrd_Process(void * Context,NFCSTATUS Status)170 void phFriNfc_NdefSmtCrd_Process(void *Context, NFCSTATUS Status)
171 {
172     if ( Context != NULL )
173     {
174         phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
175 
176         switch ( NdefSmtCrdFmt->psRemoteDevInfo->RemDevType )
177         {
178             case phNfc_eMifare_PICC :
179             case phNfc_eISO14443_3A_PICC:
180                 if((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD) ||
181                     (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD) ||
182                     (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD))
183                 {
184                     /* Remote device is Mifare Standard card */
185                     phFriNfc_MfStd_Process(NdefSmtCrdFmt,Status);
186 
187                 }
188                 else
189                 {
190                     Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
191                                          NFCSTATUS_INVALID_REMOTE_DEVICE);
192                 }
193             break;
194             default :
195                 /* Remote device opmode not recognized.
196                  * Probably not NDEF compliant
197                  */
198                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
199                                     NFCSTATUS_INVALID_REMOTE_DEVICE);
200                 /* set the state back to the Reset_Init state*/
201                 NdefSmtCrdFmt->State =  PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
202 
203                 /* set the completion routine*/
204                 NdefSmtCrdFmt->CompletionRoutine[PH_FRINFC_SMTCRDFMT_CR_INVALID_OPE].
205                 CompletionRoutine(NdefSmtCrdFmt->CompletionRoutine->Context, Status);
206             break;
207         }
208     }
209     else
210     {
211         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,\
212                             NFCSTATUS_INVALID_PARAMETER);
213         /* The control should not come here. As Context itself is NULL ,
214          * Can't call the CR
215          */
216     }
217 
218     return;
219 }
220