summaryrefslogtreecommitdiffstats
path: root/src/phLlcNfc.c
blob: 36b442b786edfebf57ac7fc727ad602ee978df4a (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * 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  phLlcNfc.c
* \brief Common LLC for the upper layer.
*
* Project: NFC-FRI-1.1
*
* $Date: Wed Apr 28 17:07:03 2010 $
* $Author: ing02260 $
* $Revision: 1.59 $
* $Aliases: NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
*
*/

/*************************** Includes *******************************/
#include <phNfcTypes.h>
#include <phNfcStatus.h>
#include <phOsalNfc.h>
#include <phNfcInterface.h>
#include <phLlcNfc_DataTypes.h>
#include <phLlcNfc.h>
#include <phLlcNfc_Frame.h>
#include <phLlcNfc_Interface.h>
#include <phLlcNfc_Timer.h>

/*********************** End of includes ****************************/

/***************************** Macros *******************************/

/************************ End of macros *****************************/

/***************************** Global variables *******************************/

#ifdef LLC_RELEASE_FLAG
    uint8_t             g_release_flag;
#endif /* #ifdef LLC_RELEASE_FLAG */

/************************ End of global variables *****************************/



/*********************** Local functions ****************************/
/**
* \ingroup grp_hal_nfc_llc
*
* \brief \b Init function
*
* \copydoc page_reg Initialise all variables of the LLC component (Asynchronous function).
*
* \param[in] pContext          LLC context provided by the upper layer. The LLC 
*                              context will be given to the upper layer through the
*                              \ref phLlcNfc_Register function
* \param[in] pLinkInfo         Link information of the hardware 
*
* \retval NFCSTATUS_PENDING            If the command is yet to be processed.
* \retval NFCSTATUS_INVALID_PARAMETER  At least one parameter of the function is invalid.
* \retval Other errors                 Errors related to the lower layers
*
*/
static 
NFCSTATUS 
phLlcNfc_Init (
               void    *pContext, 
               void    *pLinkInfo
               );

/**
* \ingroup grp_hal_nfc_llc
*
* \brief \b Send function
*
* \copydoc page_reg This asynchronous function gets the information from the upper layer and creates the 
*              proper LLC packet to send the information it to the hardware. The number of 
*              bytes written is obtained from the send response callback which has been 
*              registered in \ref phLlcNfc_Register function
*
* \param[in] pContext          LLC context is provided by the upper layer. The LLC 
*                              context earlier was given to the upper layer through the
*                              \ref phLlcNfc_Register function
* \param[in] pLinkInfo         Link information of the hardware.
* \param[in] pLlc_Buf          The information given by the upper layer to send it to 
*                              the lower layer
* \param[in] llcBufLength      the length of pLlc_Buf, that needs to be sent to the 
*                              lower layer is given by the upper layer
*
* \retval NFCSTATUS_PENDING                If the command is yet to be process.
* \retval NFCSTATUS_INVALID_PARAMETER      At least one parameter of the function is invalid.
* \retval Other errors                     Errors related to the lower layers
*
*/
static 
NFCSTATUS 
phLlcNfc_Send ( 
               void            *pContext, 
               void            *pLinkInfo, 
               uint8_t         *pLlcBuf, 
               uint16_t        llcBufLength
               );

/**
* \ingroup grp_hal_nfc_llc
*
* \brief \b Receive function
*
* \copydoc page_reg This asynchronous function gets the length and the required buffer from
*          the upper layer to receive the information from the the hardware. The 
*          received data will be given through the receive response callback 
*          which has been registered in the \b phLlcNfc_Register function
*
* \param[in] pContext          LLC context is provided by the upper layer. The LLC 
*                              context earlier was given to the upper layer through the
*                              \b phLlcNfc_Register function
* \param[in] pLinkInfo         Link information of the hardware
* \param[in] pLlc_Buf          The information given by the upper layer to receive data from
*                              the lower layer
* \param[in] llcBufLength      The length of pLlc_Buf given by the upper layer
*
* \retval NFCSTATUS_PENDING                If the command is yet to be process.
* \retval NFCSTATUS_INVALID_PARAMETER      At least one parameter of the function is invalid.
* \retval Other errors                     Errors related to the lower layers
*
*/
static 
NFCSTATUS 
phLlcNfc_Receive (  
                  void              *pContext, 
                  void              *pLinkInfo, 
                  uint8_t           *pLlcBuf, 
                  uint16_t          llcBufLength
                  );
/******************** End of Local functions ************************/

/********************** Global variables ****************************/

/******************** End of Global Variables ***********************/

NFCSTATUS 
phLlcNfc_Register (
    phNfcIF_sReference_t        *psReference,
    phNfcIF_sCallBack_t         if_callback,
    void                        *psIFConfig
)
{
    NFCSTATUS               result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t      *ps_llc_ctxt = NULL;
    phNfcLayer_sCfg_t       *psconfig = (phNfcLayer_sCfg_t *)psIFConfig;
    
    PH_LLCNFC_PRINT("Llc Register called\n");
    if ((NULL == psReference) || (NULL == psIFConfig) || 
        (NULL == psReference->plower_if) ||
#if 0
        (NULL == if_callback.pif_ctxt) || 
#endif
        (NULL == if_callback.notify) ||
        (NULL == if_callback.receive_complete) ||  
        (NULL == if_callback.send_complete))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {        
        /* Now LLC is in RESET state */
        ps_llc_ctxt = (phLlcNfc_Context_t*)phOsalNfc_GetMemory(
                                        sizeof(phLlcNfc_Context_t));
        if (NULL == ps_llc_ctxt)
        {
            /* Memory allocation failed */
            result = PHNFCSTVAL(CID_NFC_LLC, 
                                NFCSTATUS_INSUFFICIENT_RESOURCES);
        }
        else 
        {
            result = NFCSTATUS_SUCCESS;

            (void)memset(ps_llc_ctxt, 0, sizeof(phLlcNfc_Context_t));

            /* Register the LLC functions to the upper layer */
            psReference->plower_if->init = (pphNfcIF_Interface_t)&phLlcNfc_Init;
            psReference->plower_if->release = (pphNfcIF_Interface_t)&phLlcNfc_Release;
            psReference->plower_if->send = (pphNfcIF_Transact_t)&phLlcNfc_Send;
            psReference->plower_if->receive = (pphNfcIF_Transact_t)&phLlcNfc_Receive;
            /* Copy the LLC context to the upper layer */
            psReference->plower_if->pcontext = ps_llc_ctxt;

            /* Register the callback function from the upper layer */
            ps_llc_ctxt->cb_for_if.receive_complete = if_callback.receive_complete;
            ps_llc_ctxt->cb_for_if.send_complete = if_callback.send_complete;
            ps_llc_ctxt->cb_for_if.notify = if_callback.notify;
            /* Get the upper layer context */
            ps_llc_ctxt->cb_for_if.pif_ctxt = if_callback.pif_ctxt;

            result = phLlcNfc_Interface_Register(ps_llc_ctxt, psconfig);

            if (NFCSTATUS_SUCCESS == result)
            {
#ifdef LLC_RELEASE_FLAG
                g_release_flag = FALSE;
#endif /* #ifdef LLC_RELEASE_FLAG */
            }
        }
    }
    PH_LLCNFC_DEBUG("Llc Register result : 0x%x\n", result);
    return result;
}

static
NFCSTATUS 
phLlcNfc_Init (
    void    *pContext, 
    void    *pLinkInfo
)
{
    NFCSTATUS               result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t      *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    phLlcNfc_LlcPacket_t    s_packet_info;
    
    PH_LLCNFC_PRINT("Llc Init called\n");
    if ((NULL == ps_llc_ctxt) || (NULL == pLinkInfo))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /* Initialisation */
        ps_llc_ctxt->phwinfo = pLinkInfo;
        /* Call the internal frame initialise */
        phLlcNfc_H_Frame_Init(ps_llc_ctxt);
        /* Call the internal LLC TL interface initialise */
        result = phLlcNfc_Interface_Init(ps_llc_ctxt);
        if (NFCSTATUS_SUCCESS == result)
        {
            /* Call the internal LLC timer initialise */
            result = phLlcNfc_TimerInit(ps_llc_ctxt);
        }
        
        if (NFCSTATUS_SUCCESS == result)
        {
            /* Create the static timer */
            phLlcNfc_CreateTimers();               

            /* Create a U frame */
            result = phLlcNfc_H_CreateUFramePayload(ps_llc_ctxt,
                                    &s_packet_info,
                                    &(s_packet_info.llcbuf_len),
                                    phLlcNfc_e_rset);
        }
        if (NFCSTATUS_SUCCESS == result)
        {
            /* Call DAL write */
            result = phLlcNfc_Interface_Write(ps_llc_ctxt, 
                                (uint8_t*)&(s_packet_info.s_llcbuf),
                                (uint32_t)s_packet_info.llcbuf_len);
        }
        if (NFCSTATUS_PENDING == result)
        {
            /* Start the timer */
            result = phLlcNfc_StartTimers(PH_LLCNFC_CONNECTIONTIMER, 0);
            if (NFCSTATUS_SUCCESS == result)
            {
                ps_llc_ctxt->s_frameinfo.sent_frame_type = 
                                                        init_u_rset_frame;
                result = NFCSTATUS_PENDING;
            }
        }
    
        if (NFCSTATUS_PENDING != result)
        {
            (void)phLlcNfc_Release(ps_llc_ctxt, pLinkInfo);
        }
    }
    PH_LLCNFC_DEBUG("Llc Init result : 0x%x\n", result);
    return result;
}

NFCSTATUS 
phLlcNfc_Release(
    void    *pContext, 
    void    *pLinkInfo
)
{
    NFCSTATUS               result = PHNFCSTVAL(CID_NFC_LLC, 
                                            NFCSTATUS_INVALID_PARAMETER);
    phLlcNfc_Context_t     *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    /* 
        1. Free all the memory allocated in initialise
    */
    PH_LLCNFC_PRINT("Llc release called\n");

    if ((NULL != ps_llc_ctxt) && (NULL != pLinkInfo))
    {
        result = NFCSTATUS_SUCCESS;
        ps_llc_ctxt->phwinfo = pLinkInfo;
#ifdef INCLUDE_DALINIT_DEINIT   
        if (NULL != ps_llc_ctxt->lower_if.release)
        {
            result = ps_llc_ctxt->lower_if.release(
                            ps_llc_ctxt->lower_if.pcontext, 
                            pLinkInfo);
        }
#endif
        if (NULL != ps_llc_ctxt->lower_if.transact_abort)
        {
            result = ps_llc_ctxt->lower_if.transact_abort(
                            ps_llc_ctxt->lower_if.pcontext, 
                            pLinkInfo);
        }
        if (NULL != ps_llc_ctxt->lower_if.unregister)
        {
            result = ps_llc_ctxt->lower_if.unregister(
                            ps_llc_ctxt->lower_if.pcontext, 
                            pLinkInfo);
        }
        
        /* Call the internal LLC timer un-initialise */
        phLlcNfc_TimerUnInit(ps_llc_ctxt);
        phLlcNfc_H_Frame_DeInit(&ps_llc_ctxt->s_frameinfo);
        (void)memset(ps_llc_ctxt, 0, sizeof(phLlcNfc_Context_t));
        phOsalNfc_FreeMemory(ps_llc_ctxt);
        ps_llc_ctxt = NULL;

#ifdef LLC_RELEASE_FLAG
        g_release_flag = TRUE;
#endif /* #ifdef LLC_RELEASE_FLAG */

    }
    PH_LLCNFC_DEBUG("Llc release result : 0x%04X\n", result);
    return result;
}

static
NFCSTATUS 
phLlcNfc_Send ( 
    void            *pContext, 
    void            *pLinkInfo, 
    uint8_t         *pLlcBuf, 
    uint16_t        llcBufLength
)
{
    /*
        1. Check the function parameters for valid values
        2. Create the I frame llc payload using the upper layer buffer
        3. Send the updated buffer to the below layer
        4. Store the I frame in a list, till acknowledge is received
    */
    NFCSTATUS               result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t      *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    phLlcNfc_Frame_t        *ps_frame_info = NULL;
    phLlcNfc_LlcPacket_t    s_packet_info;
    phLlcNfc_StoreIFrame_t  *ps_store_frame = NULL;
#if 0
    uint8_t                 count = 1;
#endif /* #if 0 */

    PH_LLCNFC_PRINT ("Llc Send called\n");
    if ((NULL == ps_llc_ctxt) || (NULL == pLinkInfo) ||  
        (NULL == pLlcBuf) || (0 == llcBufLength) ||  
        (llcBufLength > PH_LLCNFC_MAX_IFRAME_BUFLEN))
    {
        /* Parameter check failed */
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else if (ps_llc_ctxt->s_frameinfo.s_send_store.winsize_cnt >= 
            ps_llc_ctxt->s_frameinfo.window_size)
    {
        /* Window size check failed */
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_NOT_ALLOWED);
    }
    else
    {
        ps_frame_info = &(ps_llc_ctxt->s_frameinfo);
        ps_store_frame = &(ps_frame_info->s_send_store);

        PH_LLCNFC_DEBUG ("Buffer length : 0x%04X\n", llcBufLength);
        PH_LLCNFC_PRINT_BUFFER (pLlcBuf, llcBufLength);
        
        /* Copy the hardware information */
        ps_llc_ctxt->phwinfo = pLinkInfo;

        /* Create I frame with the user buffer  */
        (void)phLlcNfc_H_CreateIFramePayload (
                                &(ps_llc_ctxt->s_frameinfo), 
                                &s_packet_info, 
                                pLlcBuf, (uint8_t)llcBufLength);


        /* Store the I frame in the send list */
        (void)phLlcNfc_H_StoreIFrame (ps_store_frame, s_packet_info);
        result = NFCSTATUS_PENDING;

#ifdef CTRL_WIN_SIZE_COUNT

        /* No check required */
        if ((TRUE != ps_frame_info->write_pending) && 
            (PH_LLCNFC_READPEND_REMAIN_BYTE != 
            ps_frame_info->read_pending))

#else /* #ifdef CTRL_WIN_SIZE_COUNT */
        
        if (1 == ps_frame_info->s_send_store.winsize_cnt)

#endif /* #ifdef CTRL_WIN_SIZE_COUNT */
        {
            /* Call write to the below layer, only if previous write 
                is completed */
            result = phLlcNfc_Interface_Write (ps_llc_ctxt, 
                                (uint8_t *)&(s_packet_info.s_llcbuf),
                                (uint32_t)s_packet_info.llcbuf_len);

            if ((NFCSTATUS_PENDING == result) || 
                (NFCSTATUS_BUSY == PHNFCSTATUS (result)))
            {
                ps_frame_info->write_status = result;
                if (NFCSTATUS_BUSY == PHNFCSTATUS(result))
                {
                    result = NFCSTATUS_PENDING;
                    ps_frame_info->write_wait_call = (phLlcNfc_eSentFrameType_t)
                            ((resend_i_frame == ps_frame_info->write_wait_call) ? 
                            ps_frame_info->write_wait_call : user_i_frame);
                }
                else
                {
                    /* Start the timer */
                    (void)phLlcNfc_StartTimers (PH_LLCNFC_GUARDTIMER, 
                                                ps_frame_info->n_s);
                    
                    /* "sent_frame_type is updated" only if the data is 
                        written to the lower layer */
                    ps_frame_info->sent_frame_type = user_i_frame;
                }
            }
#if 0
            /* Get the added frame array count */
            count = (uint8_t)((((ps_store_frame->start_pos + 
                            ps_store_frame->winsize_cnt) - count)) % 
                            PH_LLCNFC_MOD_NS_NR);
#endif /* #if 0 */
        }
        else
        {
            ps_frame_info->write_status = PHNFCSTVAL(CID_NFC_LLC, NFCSTATUS_BUSY);
            ps_frame_info->write_wait_call = (phLlcNfc_eSentFrameType_t)
                            ((resend_i_frame == ps_frame_info->write_wait_call) ? 
                            ps_frame_info->write_wait_call : user_i_frame);
        }
    }

    
    PH_LLCNFC_DEBUG ("Llc Send result : 0x%04X\n", result);
    return result;
}

static
NFCSTATUS 
phLlcNfc_Receive (  
    void            *pContext, 
    void            *pLinkInfo, 
    uint8_t         *pLlcBuf, 
    uint16_t        llcBufLength
)
{
    NFCSTATUS               result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t      *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    phLlcNfc_LlcPacket_t    *ps_recv_pkt = NULL;

    PH_LLCNFC_PRINT("Llc Receive called\n");
    if ((NULL == ps_llc_ctxt) || (NULL == pLinkInfo) || 
        (NULL == pLlcBuf) || (0 == llcBufLength) || 
        (llcBufLength > PH_LLCNFC_MAX_IFRAME_BUFLEN))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        ps_llc_ctxt->phwinfo = pLinkInfo;
        
        ps_recv_pkt = &(ps_llc_ctxt->s_frameinfo.s_recvpacket);
        /* Always read the first byte to read the length, then 
        read the entire data later using the 1 byte buffer */
        llcBufLength = PH_LLCNFC_BYTES_INIT_READ;
        /* Call write to the below layer */
        result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                PH_LLCNFC_READWAIT_OFF, 
                                &(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                                llcBufLength);

        ps_llc_ctxt->s_frameinfo.upper_recv_call = TRUE;
        if (NFCSTATUS_PENDING != result)
        {
            ps_llc_ctxt->state = phLlcNfc_Initialised_State;
        }
    }
    PH_LLCNFC_DEBUG("Llc Receive result : 0x%04X\n", result);
    return result;
}