summaryrefslogtreecommitdiffstats
path: root/src/phLibNfc_target.c
blob: 982d8dd5057cedd39c98a7c155134eec0d6c5315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*!
 * \file phLibNfc_Target.c

 * Project: NFC FRI 1.1
 *
 * $Date: Thu Oct 15 15:24:43 2009 $
 * $Author: ing07299 $
 * $Revision: 1.12 $
 * $Aliases: NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK944_SDK,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK949_SDK_INT,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK1003_SDK,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1008_SDK,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1007_SDK,NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $
 *
 */

/*
************************* Header Files ***************************************
*/

#include <phLibNfcStatus.h>
#include <phLibNfc.h>
#include <phHal4Nfc.h>
#include <phOsalNfc.h>
#include <phLibNfc_Internal.h>
#include <phLibNfc_ndef_raw.h>
#include <phLibNfc_initiator.h>
#include <phLibNfc_discovery.h>

/*
*************************** Macro's  ****************************************
*/

#ifndef STATIC_DISABLE
#define STATIC static
#else
//#undef STATIC
#define STATIC 
#endif
/*
*************************** Global Variables **********************************
*/

/*
*************************** Static Function Declaration ***********************
*/

/* Remote device receive callback */
STATIC void phLibNfc_RemoteDev_Receive_Cb(
                                void            *context,
                                phNfc_sData_t   *rec_rsp_data, 
                                NFCSTATUS       status
                                );

/* Remote device Send callback */
STATIC void phLibNfc_RemoteDev_Send_Cb(
                                void        *Context,
                                NFCSTATUS   status
                                );

/*
*************************** Function Definitions ******************************
*/

/**
* Interface used to receive data from initiator at target side during P2P
* communication.
*/
NFCSTATUS phLibNfc_RemoteDev_Receive(phLibNfc_Handle       hRemoteDevice,  
                                pphLibNfc_Receive_RspCb_t  pReceiveRspCb,  
                                void                       *pContext
                                ) 
{
    NFCSTATUS RetVal = NFCSTATUS_FAILED;
    /*Check Lib Nfc is initialized*/
    if((NULL == gpphLibContext)|| 
        (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    {
        RetVal = NFCSTATUS_NOT_INITIALISED;
    }/*Check application has sent valid parameters*/
    else if (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateRelease)
    {
        RetVal = NFCSTATUS_DESELECTED;
    }
    else if((NULL == pReceiveRspCb)
        || (NULL == pContext)
        || (0 == hRemoteDevice))
    {
        RetVal= NFCSTATUS_INVALID_PARAMETER;
    }   
    else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
    {
        RetVal = NFCSTATUS_SHUTDOWN;
    }
    else if((TRUE == gpphLibContext->status.GenCb_pending_status)       
        ||(NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb)
        ||(phHal_eNfcIP1_Target==
        ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))
    {
        /*Previous callback is pending or if initiator uses this api */
        RetVal = NFCSTATUS_REJECTED;
    }/*check for Discovered initiator handle and handle sent by application */
    else if(gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle != hRemoteDevice)
    {
        RetVal= NFCSTATUS_INVALID_DEVICE;
    }
#ifdef LLCP_TRANSACT_CHANGES
    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
    {
        RetVal = NFCSTATUS_BUSY;
    }
#endif /* #ifdef LLCP_TRANSACT_CHANGES */
    else
    {
        if(eLibNfcHalStatePresenceChk ==
                gpphLibContext->LibNfcState.next_state)
        {
            gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
            RetVal = NFCSTATUS_PENDING;
        }
        else
        {
            /*Call below layer receive and register the callback with it*/
            PHDBG_INFO("LibNfc:P2P Receive In Progress");
            RetVal =phHal4Nfc_Receive(                                          
                            gpphLibContext->psHwReference,
                            (phHal4Nfc_TransactInfo_t*)gpphLibContext->psTransInfo,
                            (pphLibNfc_Receive_RspCb_t)
                            phLibNfc_RemoteDev_Receive_Cb,
                            (void *)gpphLibContext
                            );  
        }   
        if(NFCSTATUS_PENDING == RetVal)
        {
            /*Update the Next state as Transaction*/
            gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb= pReceiveRspCb;
            gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = pContext;
            gpphLibContext->status.GenCb_pending_status=TRUE;
            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
        }
        else
        {
            RetVal = NFCSTATUS_FAILED;
        }       
    }
    return RetVal;
}
/**
* Response callback for Remote Device Receive.
*/
STATIC void phLibNfc_RemoteDev_Receive_Cb(
                                    void            *context,
                                    phNfc_sData_t   *rec_rsp_data, 
                                    NFCSTATUS       status
                                    )
{
    pphLibNfc_Receive_RspCb_t       pClientCb=NULL;
    
    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)context;
    void                    *pUpperLayerContext=NULL;

    /* Check for the context returned by below layer */
    if(pLibNfc_Ctxt != gpphLibContext)
    {   /*wrong context returned*/
        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    }
    else
    {
        pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb;
        pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx;

        gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = NULL;
        gpphLibContext->status.GenCb_pending_status = FALSE;
        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
        {   /*shutdown called before completion of P2P receive allow
              shutdown to happen */
            phLibNfc_Pending_Shutdown();
            status = NFCSTATUS_SHUTDOWN;    
        }
        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
        {
            status = NFCSTATUS_ABORTED;
        }
        else
        {
            if((NFCSTATUS_SUCCESS != status) && 
                (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION ) )
            {
                /*During p2p receive operation initiator was removed
                from RF field of target*/
                status = NFCSTATUS_DESELECTED;
            }
            else
            {
                status = NFCSTATUS_SUCCESS;
            }
        }   
        /* Update current state */
        phLibNfc_UpdateCurState(status,gpphLibContext);
               
        if (NULL != pClientCb)
        {
            /*Notify to upper layer status and No. of bytes
             actually received */
            pClientCb(pUpperLayerContext, rec_rsp_data, status);          
        }
    }
    return;
}

/**
* Interface used to send data from target to initiator during P2P communication
*/
NFCSTATUS 
phLibNfc_RemoteDev_Send(
                        phLibNfc_Handle      hRemoteDevice,  
                        phNfc_sData_t *      pTransferData,  
                        pphLibNfc_RspCb_t    pSendRspCb,  
                        void                 *pContext
                        )
{
    NFCSTATUS RetVal = NFCSTATUS_FAILED;
    /*Check Lib Nfc stack is initilized*/
    if((NULL == gpphLibContext)|| 
        (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    {
        RetVal = NFCSTATUS_NOT_INITIALISED;
    }
    else if (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateRelease)
    {
        RetVal = NFCSTATUS_DESELECTED;
    }
    /*Check application has sent the valid parameters*/
    else if((NULL == pTransferData)
        || (NULL == pSendRspCb)
        || (NULL == pTransferData->buffer)
        || (0 == pTransferData->length)
        || (NULL == pContext)
        || (0 == hRemoteDevice))
    {
        RetVal= NFCSTATUS_INVALID_PARAMETER;
    }   
    else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
    {
        RetVal = NFCSTATUS_SHUTDOWN;
    }
	else if((TRUE == gpphLibContext->status.GenCb_pending_status)       
        ||(NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb)
        ||(phHal_eNfcIP1_Target==
        ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))
    {
        /*Previous callback is pending or local device is Initiator
        then don't allow */
        RetVal = NFCSTATUS_REJECTED;
    }/*Check for Discovered initiator handle and handle sent by application */
    else if(gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle != hRemoteDevice)
    {
        RetVal= NFCSTATUS_INVALID_DEVICE;
    }
    else if((NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb))
    {
        RetVal =NFCSTATUS_BUSY ;
    }
#ifdef LLCP_TRANSACT_CHANGES
    else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
            && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
    {
        RetVal= NFCSTATUS_BUSY;
    }
#endif /* #ifdef LLCP_TRANSACT_CHANGES */
    else
    {
        if(eLibNfcHalStatePresenceChk ==
                gpphLibContext->LibNfcState.next_state)
        {
            gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
            RetVal = NFCSTATUS_PENDING;
        }
        else
        {
            if(gpphLibContext->psTransInfo!=NULL)
            {
                (void)memset(gpphLibContext->psTransInfo,
                                0,
                                sizeof(phLibNfc_sTransceiveInfo_t));                
            
                gpphLibContext->psTransInfo->addr =UNKNOWN_BLOCK_ADDRESS;
                /*pointer to send data */
                gpphLibContext->psTransInfo->sSendData.buffer = 
                                                    pTransferData->buffer;   
                /*size of send data*/
                gpphLibContext->psTransInfo->sSendData.length = 
                                                    pTransferData->length;   

                /* Copy remote device type */
                gpphLibContext->sNfcIp_Context.TransactInfoRole.remotePCDType =
                    ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType;
                /*Call Hal4 Send API and register callback with it*/
                PHDBG_INFO("LibNfc:P2P send In Progress");
                RetVal= phHal4Nfc_Send(                                          
                                gpphLibContext->psHwReference,
                                &(gpphLibContext->sNfcIp_Context.TransactInfoRole),
                                gpphLibContext->psTransInfo->sSendData,
                                (pphLibNfc_RspCb_t)
                                phLibNfc_RemoteDev_Send_Cb,
                                (void *)gpphLibContext
                                ); 
            }
        }
        if(NFCSTATUS_PENDING == RetVal)
        {
            /* Update next state to transaction */
            gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb= pSendRspCb;
            gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = pContext;
            gpphLibContext->status.GenCb_pending_status=TRUE;
            gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
        }
        else
        {
            RetVal = NFCSTATUS_FAILED;
        }
    }
    return RetVal;
}

/*
* Response callback for Remote Device Send.
*/
STATIC void phLibNfc_RemoteDev_Send_Cb(
                            void        *Context,
                            NFCSTATUS   status
                            )
{
    pphLibNfc_RspCb_t       pClientCb=NULL;
    phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
    void                    *pUpperLayerContext=NULL;

    /* Check for the context returned by below layer */
    if(pLibNfc_Ctxt != gpphLibContext)
    {   /*wrong context returned*/
        phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    }
    else
    {
        if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
        {   /*shutdown called before completion p2p send allow
              shutdown to happen */
            phLibNfc_Pending_Shutdown();
            status = NFCSTATUS_SHUTDOWN;    
        }
        else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
        {
            status = NFCSTATUS_ABORTED;
        }
        else
        {
            gpphLibContext->status.GenCb_pending_status = FALSE;
            if((NFCSTATUS_SUCCESS != status) && 
                (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION ) )
            {
                /*During p2p send operation initator was not present in RF
                field of target*/
                status = NFCSTATUS_DESELECTED;
            }
            else
            {
                status = NFCSTATUS_SUCCESS;
            }
        }
        /* Update current state */
        phLibNfc_UpdateCurState(status,gpphLibContext);

        pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb;
        pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx;

        gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
        gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = NULL;
        if (NULL != pClientCb)
        {
            /* Notify to upper layer status and No. of bytes
             actually written or send to initiator */
            pClientCb(pUpperLayerContext, status);          
        }
    }
    return;
}