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 * \file  phLlcNfc_DataTypes.h
19 * \brief Contains the structure information.
20 *
21 * Project: NFC-FRI-1.1
22 *
23 * $Date: Fri Apr 30 10:03:36 2010 $
24 * $Author: ing02260 $
25 * $Revision: 1.43 $
26 * $Aliases: NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
27 *
28 */
29 
30 #ifndef PHLLCNFC_DATATYPES_H
31 #define PHLLCNFC_DATATYPES_H
32 
33 /**
34 *  \name LLC NFC frame creation, deletion and processing
35 *
36 * File: \ref phLlcNfc_DataTypes.h
37 *
38 */
39 /*@{*/
40 #define PH_LLCNFC_DATATYPES_FILEREVISION "$Revision: 1.43 $" /**< \ingroup grp_hal_nfc_llc */
41 #define PH_LLCNFC_DATATYPES_FILEALIASES  "$Aliases: NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $"     /**< \ingroup grp_hal_nfc_llc */
42 /*@}*/
43 /*************************** Includes *******************************/
44 #include <phNfcCompId.h>
45 /*********************** End of includes ****************************/
46 /***************************** Macros *******************************/
47 
48 /* Trace buffer declaration */
49 #if defined (LLC_TRACE)
50     #include <phOsalNfc.h>
51     #include <stdio.h>
52 
53     extern char                 phOsalNfc_DbgTraceBuffer[];
54     #define trace_buffer        phOsalNfc_DbgTraceBuffer
55 
56     #define MAX_TRACE_BUFFER    150
57     #define PH_LLCNFC_PRINT( str )  phOsalNfc_DbgString(str)
58     #define PH_LLCNFC_PRINT_DATA(buf,len)
59     #define PH_LLCNFC_STRING( str )
60     #define PH_LLCNFC_DEBUG(str, arg) \
61     {                                       \
62         snprintf(trace_buffer,MAX_TRACE_BUFFER,str,arg);   \
63         phOsalNfc_DbgString(trace_buffer);                 \
64     }
65     #define PH_LLCNFC_PRINT_BUFFER(buf,len)     \
66     {                                       \
67         /* uint16_t i = 0;                  \
68         char        trace_buffer[MAX_TRACE_BUFFER];                    \
69         snprintf(trace_buffer,MAX_TRACE_BUFFER,"\n\t %s:",msg);    \
70         phOsalNfc_DbgString(trace);                 */\
71         phOsalNfc_DbgTrace(buf,len);            \
72         phOsalNfc_DbgString("\r");              \
73     }
74 #endif /* #if defined (LLC_TRACE) */
75 
76 
77 #if (!defined (LLC_TRACE) && defined (LLC_DATA_BYTES))
78     #include <phOsalNfc.h>
79 
80     extern char                 phOsalNfc_DbgTraceBuffer[];
81     #define trace_buffer        phOsalNfc_DbgTraceBuffer
82 
83     #define PH_LLCNFC_PRINT( str )
84     #define PH_LLCNFC_PRINT_BUFFER(buf, len)
85     #define PH_LLCNFC_DEBUG(str, arg1)
86     #define PH_LLCNFC_STRING( str )  phOsalNfc_DbgString(str)
87     #define PH_LLCNFC_PRINT_DATA(buf,len)     \
88     {                                       \
89         /* uint16_t i = 0;                  \
90         char        trace_buffer[MAX_TRACE_BUFFER];                    \
91         snprintf(trace_buffer,MAX_TRACE_BUFFER,"\n\t %s:",msg);    \
92         phOsalNfc_DbgString(trace_buffer);                 */\
93         phOsalNfc_DbgTrace(buf,len);            \
94     }
95 #endif /* #if (!defined (LLC_TRACE) && defined (LLC_DATA_BYTES)) */
96 
97 
98 #if (!defined (LLC_TRACE) && !defined (LLC_DATA_BYTES))
99     /** To disable prints */
100     #define PH_LLCNFC_PRINT(str)
101     #define PH_LLCNFC_PRINT_BUFFER(buf, len)
102     #define PH_LLCNFC_DEBUG(str, arg1)
103     #define PH_LLCNFC_PRINT_DATA(buf,len)
104     #define PH_LLCNFC_STRING( str )
105 #endif /* #if (!defined (LLC_TRACE) && !defined (LLC_DATA_BYTES)) */
106 
107 
108 /* If the below MACRO (RECV_NR_CHECK_ENABLE) is
109 DEFINED : then check for the NR frame received from PN544 in the I frame is
110     added. This shall be greater than sent NS from the HOST.
111     This is used to stop the timer
112 COMMENTED : dont check the N(R) frame received from the PN544
113 */
114 /* #define RECV_NR_CHECK_ENABLE */
115 
116 /* If the below MACRO (LLC_UPP_LAYER_NTFY_WRITE_RSP_CB) is
117 DEFINED : then if an I frame is received and the
118         upper layer response callback (before another READ is pended) is called
119         only after sending S frame and wait for the callback and then notify the
120         upper layer
121 COMMENTED : then if an I frame is received and the
122             upper layer response callback (before another READ is pended) is called
123             immediately after sending S frame (not waiting for the sent S frame
124             callback)
125 */
126 /* #define LLC_UPP_LAYER_NTFY_WRITE_RSP_CB */
127 
128 /* PN544 continuously sends an incorrect I frames to the HOST,
129     even after the REJ frame from HOST to PN544
130 If the below MACRO (LLC_RR_INSTEAD_OF_REJ) is
131 DEFINED : then if the received NS = (expected NR - 1) then instead of REJ
132         RR frame is sent
133 COMMENTED : then REJ frame is sent
134 */
135 // #define LLC_RR_INSTEAD_OF_REJ
136 
137 #define SEND_UFRAME
138 
139 /* If the below MACRO (CTRL_WIN_SIZE_COUNT) is
140 DEFINED : then window size will be maximum
141 COMMENTED : then window size is 1
142 */
143 #define CTRL_WIN_SIZE_COUNT
144 
145 /*
146 If the below MACRO (LLC_URSET_NO_DELAY) is
147 DEFINED : then after receiving the UA frame, then immediately this will be
148             notified or further operation will be carried on.
149 COMMENTED : then after receiving the UA frame, then a timer is started, to
150             delay the notifiation or to carry on the next operation
151 */
152 #define LLC_URSET_NO_DELAY
153 
154     /*
155     If the below MACRO (LLC_RELEASE_FLAG) is
156 DEFINED : then whenever LLC release is called the g_release_flag variable
157                 will be made TRUE. Also, NO notification is allowed to the
158                 upper layer.
159 COMMENTED : g_release_flag is not declared and not used
160     */
161     #define LLC_RELEASE_FLAG
162 
163 
164  /*
165     Actually, there is a send and receive error count, if either of them reaches
166     limit, then exception is raised.
167     If the below MACRO (LLC_RSET_INSTEAD_OF_EXCEPTION) is
168 DEFINED : then exception is not raised, instead a U RSET command is sent.
169 COMMENTED : then exception is raised
170     */
171 /* #define LLC_RSET_INSTEAD_OF_EXCEPTION */
172 
173 #ifndef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB
174     /*
175     If the below MACRO (PIGGY_BACK) is
176     DEFINED : After receiving I frame, wait till the ACK timer to expire to send an ACK to PN544.
177     COMMENTED : immediately ACK the received I frame
178     */
179     #define PIGGY_BACK
180 
181 #endif /* LLC_UPP_LAYER_NTFY_WRITE_RSP_CB */
182 
183 #define LLC_SEND_ERROR_COUNT
184 
185 #define RECV_ERROR_FRAME_COUNT                      (0x50U)
186 #define SENT_ERROR_FRAME_COUNT                      (0x50U)
187 
188 /** Initial bytes to read */
189 #define PH_LLCNFC_BYTES_INIT_READ                   (1)
190 /** Maximum buffer that I frame can send */
191 #define PH_LLCNFC_MAX_IFRAME_BUFLEN                 (29)
192 #define PH_LLCNFC_MAX_UFRAME_BUFLEN                 (4)
193 #define PH_LLCNFC_CRC_LENGTH                        (2)
194 #define PH_LLCNFC_MAX_LLC_PAYLOAD                   ((PH_LLCNFC_MAX_IFRAME_BUFLEN) + (4))
195 /** Maximum timer used in the Llc */
196 #define PH_LLCNFC_MAX_TIMER_USED                    (3)
197 /** Maximum timer used in the Llc */
198 #define PH_LLCNFC_MAX_ACK_GUARD_TIMER               (4)
199 /** Maximum I frame that can be stored */
200 #define PH_LLCNFC_MAX_I_FRAME_STORE                 (8)
201 
202 /** Read pending for one byte */
203 #define PH_LLCNFC_READPEND_ONE_BYTE                 (0x01U)
204     /** Read pending for remaining byte */
205 #define PH_LLCNFC_READPEND_REMAIN_BYTE              (0x02U)
206 /** Read pending not done */
207 #define PH_LLCNFC_READPEND_FLAG_OFF                 FALSE
208 #define PH_LLCNFC_MAX_REJ_RETRY_COUNT               (200)
209 
210 
211 /**** Macros for state machine ****/
212 
213 typedef enum phLlcNfc_State
214 {
215     /** This specifies that LLC is in uninitialise state */
216     phLlcNfc_Uninitialise_State              =  0x00,
217     /** This specifies that LLC initialise is in progress */
218     phLlcNfc_Initialising_State              =  0x01,
219     /** This specifies that LLC is in initialise is complete */
220     phLlcNfc_Initialised_State               =  0x02,
221     /** This specifies that LLC is send with the
222     lower layer */
223     phLlcNfc_Sending_State                   =  0x03,
224     /** This specifies that LLC is receive with the
225     lower layer */
226     phLlcNfc_Receiving_State                 =  0x04,
227     /** This specifies that LLC is receive wait with the
228     lower layer */
229     phLlcNfc_ReceiveWait_State               =  0x05,
230     /** This specifies that LLC is resending the I frames */
231     phLlcNfc_Resend_State                    =  0x06
232 }phLlcNfc_State_t;
233 /**** Macros for state machine end ****/
234 
235 /************************ End of macros *****************************/
236 
237 /********************** Callback functions **************************/
238 
239 /******************* End of Callback functions **********************/
240 
241 /********************* Structures and enums *************************/
242 /**
243 *  \ingroup grp_hal_nfc_llc
244 *  \brief Enum to get the baud rate
245 *
246 *  This enum contains the baud rate information.
247 *
248 */
249 /*@{*/
250 typedef enum phLlcNfc_LlcBaudRate
251 {
252     /** Baud rate = 9600 */
253     phLlcNfc_e_9600 = 0x00,
254     /** Baud rate = 19200 */
255     phLlcNfc_e_19200 = 0x01,
256     /** Baud rate = 28800 */
257     phLlcNfc_e_28800 = 0x02,
258     /** Baud rate = 38400 */
259     phLlcNfc_e_38400 = 0x03,
260     /** Baud rate = 57600 */
261     phLlcNfc_e_57600 = 0x04,
262     /** Baud rate = 115200 */
263     phLlcNfc_e_115200 = 0x05,
264     /** Baud rate = 23400 */
265     phLlcNfc_e_234000 = 0x06,
266     /** Baud rate = 46800 */
267     phLlcNfc_e_460800 = 0x07,
268     /** Baud rate = 921600 */
269     phLlcNfc_e_921600 = 0x08,
270     /** Baud rate = 1228000 */
271     phLlcNfc_e_1228000 = 0x09,
272     /** Baud rate error */
273     phLlcNfc_e_bdrate_err = 0xFF
274 }phLlcNfc_LlcBaudRate_t;
275 /*@}*/
276 
277 /**
278 *  \ingroup grp_hal_nfc_llc
279 *  \brief Enum to select the U or I or S frame
280 *
281 *  This enum is to set the frames.
282 *
283 */
284 /*@{*/
285 typedef enum phLlcNfc_LlcCmd
286 {
287     /** This command is for I frame (no command) */
288     phLlcNfc_e_no_cmd = 0xFF,
289     /** This command is for U frame */
290     phLlcNfc_e_rset = 0x19,
291     /** This command is for U frame */
292     phLlcNfc_e_ua = 0x06,
293     /** This is RR command for S frame */
294     phLlcNfc_e_rr = 0x00,
295     /** This is REJ command for S frame */
296     phLlcNfc_e_rej = 0x08,
297     /** This is RNR command for S frame */
298     phLlcNfc_e_rnr = 0x10,
299     /** This is SREJ command for S frame */
300     phLlcNfc_e_srej = 0x18,
301     /** Error command */
302     phLlcNfc_e_error = 0xFE
303 }phLlcNfc_LlcCmd_t;
304 /*@}*/
305 
306 /**
307 *  \ingroup grp_hal_nfc_llc
308 *  \brief Enum to select the U or I or S frame
309 *
310 *  This enum is to set the frames.
311 *
312 */
313 /*@{*/
314 typedef enum phLlcNfc_FrameType
315 {
316     /** U frame type */
317     phLlcNfc_eU_frame = 0x00,
318     /** I frame type */
319     phLlcNfc_eI_frame = 0x01,
320     /** S frame type */
321     phLlcNfc_eS_frame = 0x02,
322     /** Error frame type */
323     phLlcNfc_eErr_frame = 0x03
324 }phLlcNfc_FrameType_t;
325 /*@}*/
326 
327 /**
328 *  \ingroup grp_hal_nfc_llc
329 *  \brief LLC sent frame type
330 *
331 *  This enum values defines what are the frames sent to the PN544
332 *
333 */
334 /*@{*/
335 
336 typedef enum phLlcNfc_eSentFrameType
337 {
338     invalid_frame,
339     /* During initialisation the U RSET is sent to PN544 */
340     init_u_rset_frame,
341     /* During initialisation the UA is sent to PN544 */
342     init_u_a_frame,
343     /* After unsuccessful retries of sending I frame to PN544,
344         URSET is sent */
345     u_rset_frame,
346     /* If PN544 sends the URSET frame in between any transaction, then
347        the UA response shall be sent */
348     u_a_frame,
349     /* S frame is sent to PN544, this will be sent only if an I frame
350         is received from PN544 */
351     s_frame,
352     /* User has sent an I frame, for that a write response callback
353         shall be called */
354     user_i_frame,
355     /* LLC, internally (means stored non acknowledged frames) has sent
356         an I frame as it doesnt get a proper acknowledgement */
357     resend_i_frame,
358     /* LLC, internally (means stored non acknowledged frames) has sent
359         an I frame as it doesnt get a reject as acknowledgement */
360     rejected_i_frame,
361     /* LLC has received a I frame for the re-sent I frames, so an S
362         frame is sent  */
363     resend_s_frame,
364     /* LLC has received a I frame for the re-sent I frames, so an S
365         frame is sent  */
366     resend_rej_s_frame,
367     /* PN544 has sent an I frame, which is wrong, so send a reject S
368         frame */
369     reject_s_frame,
370 #ifdef LLC_RR_INSTEAD_OF_REJ
371 
372     /* RR is sent instead of REJECT */
373     rej_rr_s_frame,
374 
375 #endif /* #ifdef LLC_RR_INSTEAD_OF_REJ */
376     /* For any of the above sent frames, the response shall be received */
377     write_resp_received
378 }phLlcNfc_eSentFrameType_t;
379 
380 /*@}*/
381 
382 /**
383 *  \ingroup grp_hal_nfc_llc
384 *  \brief LLC payload
385 *
386 *  This structure contains both the header information and
387 *  the exact length of the buffer.
388 *
389 */
390 /*@{*/
391 typedef struct phLlcNfc_Payload
392 {
393     /** Llc header information */
394     uint8_t                 llcheader;
395 
396     /** User or received buffer */
397     uint8_t                 llcpayload[PH_LLCNFC_MAX_LLC_PAYLOAD];
398 }phLlcNfc_Payload_t;
399 /*@}*/
400 
401 /**
402 *  \ingroup grp_hal_nfc_llc
403 *  \brief Llc buffer
404 *
405 *  This structure contains the information of the LLC length byte
406 * and payload.
407 *
408 */
409 /*@{*/
410 typedef struct phLlcNfc_Buffer
411 {
412     /** Llc length */
413     uint8_t                 llc_length_byte;
414 
415     /** LLC data including the LLC header and CRC */
416     phLlcNfc_Payload_t      sllcpayload;
417 }phLlcNfc_Buffer_t;
418 /*@}*/
419 
420 /**
421 *  \ingroup grp_hal_nfc_llc
422 *  \brief Packet information
423 *
424 *  This structure contains the length and buffer of the packet.
425 *
426 */
427 /*@{*/
428 typedef struct phLlcNfc_LlcPacket
429 {
430     /** Complete LLC buffer */
431     phLlcNfc_Buffer_t           s_llcbuf;
432 
433     /** LLC buffer length */
434     uint8_t                     llcbuf_len;
435 
436     /** Stored frame needs completion callback, to be sent to HCI */
437     phLlcNfc_eSentFrameType_t   frame_to_send;
438 
439 }phLlcNfc_LlcPacket_t;
440 /*@}*/
441 
442 /**
443 *  \ingroup grp_hal_nfc_llc_helper
444 *  \brief I frame details
445 *
446 *  This structure stores the information of the I frame
447 * (to support sliding window).
448 *
449 */
450 /*@{*/
451 typedef struct phLlcNfc_StoreIFrame
452 {
453     /** Complete LLC packet */
454     phLlcNfc_LlcPacket_t        s_llcpacket[PH_LLCNFC_MAX_I_FRAME_STORE];
455 
456     /** Window size count */
457     uint8_t                     winsize_cnt;
458 
459     /** Start position */
460     uint8_t                     start_pos;
461 
462 }phLlcNfc_StoreIFrame_t;
463 /*@}*/
464 
465 /**
466 *  \ingroup grp_hal_nfc_llc
467 *  \brief LLC timer information
468 *
469 *  This structure contains the timer related information
470 *
471 */
472 /*@{*/
473 typedef struct phLlcNfc_Timerinfo
474 {
475     /** Store the timer id for each timer create */
476     uint32_t                timer_id[PH_LLCNFC_MAX_TIMER_USED];
477 
478     /** This will store the connection time out value */
479     uint16_t                con_to_value;
480 
481     /** This will store the guard time out values */
482     uint16_t                guard_to_value[PH_LLCNFC_MAX_ACK_GUARD_TIMER];
483 
484     /** This will store the guard time out values */
485     uint16_t                iframe_send_count[PH_LLCNFC_MAX_ACK_GUARD_TIMER];
486 
487     /** This will store ns value for the sent N(S) */
488     uint8_t                 timer_ns_value[PH_LLCNFC_MAX_ACK_GUARD_TIMER];
489 
490     /** Each frame stored needs to be  */
491     uint8_t                 frame_type[PH_LLCNFC_MAX_ACK_GUARD_TIMER];
492 
493     /** Index to re-send */
494     uint8_t                 index_to_send;
495 
496     /** This is a count for gaurd time out */
497     uint8_t                 guard_to_count;
498 
499 #ifdef PIGGY_BACK
500     /** This will store the ack time out values */
501     uint16_t                ack_to_value;
502 #endif /* #ifdef PIGGY_BACK */
503 
504     /** This is a timer flag
505         Bit 0 = 1 means connection time out started else stopped
506         Bit 1 = 1 means guard time out started else stopped
507         Bit 2 = 1 means ack time out started else stopped
508     */
509     uint8_t                 timer_flag;
510 }phLlcNfc_Timerinfo_t;
511 /*@}*/
512 
513 /**
514 *  \ingroup grp_hal_nfc_llc
515 *  \brief LLC frame information
516 *
517 *  This structure contains the information of the LLC frame.
518 *
519 */
520 /*@{*/
521 typedef struct phLlcNfc_Frame
522 {
523     /** N(S) - Number of information frame */
524     uint8_t                         n_s;
525 
526     /** N(R) - Number of next information frame to receive */
527     uint8_t                         n_r;
528 
529     /** Store the window size */
530     uint8_t                         window_size;
531 
532     /** SREJ is optional, so store the flag whether it is set or not */
533     uint8_t                         srej_on_off;
534 
535     /** Store the baud rate */
536     uint8_t                         baud_rate;
537 
538     /** Flag to find the rset_recvd */
539     uint8_t                         rset_recvd;
540 
541     /** Complete LLC packet information */
542     phLlcNfc_LlcPacket_t            s_llcpacket;
543 
544     /** Store the I frames, that has been sent, Storage will be till
545         the window size */
546     phLlcNfc_StoreIFrame_t          s_send_store;
547 
548 #ifdef PIGGY_BACK
549     /** Store the I frames, that has been received, Storage will be
550         till the window size */
551     phLlcNfc_StoreIFrame_t          s_recv_store;
552 
553     /** Response received count to send the ACK once it reaches the window size */
554     uint8_t                         resp_recvd_count;
555 #endif /* #ifdef PIGGY_BACK */
556 
557     /** To receive the packet sent by below layer */
558     phLlcNfc_LlcPacket_t            s_recvpacket;
559 
560     /** Number of window I frames has to be sent again */
561     uint8_t                         rejected_ns;
562 
563     /** To store the count received error frames like
564         wrong CRC, REJ and RNR frames */
565     uint8_t                         recv_error_count;
566 
567     /** Sending error frames like REJ frames to the PN544 */
568     uint8_t                         send_error_count;
569 
570     /** Send U frame count  */
571     uint8_t                         retry_cnt;
572 
573     /** Read pending flag, to know that read is already pended
574         or not. Use the below macros to ON and OFF the flag
575         PH_LLCNFC_READPEND_FLAG_OFF
576         PH_LLCNFC_READPEND_ONE_BYTE
577         PH_LLCNFC_READPEND_REMAIN_BYTE
578         */
579     uint8_t                         read_pending;
580 
581     /** Write pending */
582     uint8_t                         write_pending;
583 
584     /** Sent frame type */
585     phLlcNfc_eSentFrameType_t       sent_frame_type;
586 
587     /** upper receive called */
588     uint8_t                         upper_recv_call;
589 
590     /** Status returned during DAL write */
591     NFCSTATUS                       write_status;
592 
593     /** Depending on the "write_status", write call has to be called */
594     phLlcNfc_eSentFrameType_t       write_wait_call;
595 }phLlcNfc_Frame_t;
596 /*@}*/
597 
598 /**
599 *  \ingroup grp_hal_nfc_llc
600 *  \brief LLC Component Context Structure
601 *
602 *  This structure is used to store the current context information
603 *   of the instance.
604 *
605 */
606 /*@{*/
607 typedef struct phLlcNfc_Context
608 {
609     /** Information regarding all the LLC frame */
610     phLlcNfc_Frame_t                s_frameinfo;
611 
612     /** Local send and receive */
613     phNfc_sLowerIF_t                lower_if;
614 
615     /** Register attention, send and receive callback from the
616         register functions of the upper layer */
617     phNfcIF_sCallBack_t             cb_for_if;
618 
619     /** Store the length, which shall be sent later through the
620         "send complete" callback */
621     uint32_t                        send_cb_len;
622 
623     /** Receive buffer provided by the upper layer */
624     uint8_t                         precv_buf[PH_LLCNFC_MAX_LLC_PAYLOAD];
625 
626     /** Receive length provided by the upper layer */
627     uint32_t                        recvbuf_length;
628 
629     /** Llc state */
630     phLlcNfc_State_t                state;
631 
632     /** Hardware information */
633     void                            *phwinfo;
634 
635     /** Timer information */
636     phLlcNfc_Timerinfo_t            s_timerinfo;
637 }phLlcNfc_Context_t;
638 /*@}*/
639 /****************** End of structures and enums *********************/
640 
641 /******************** Function declarations *************************/
642 
643 /******************** Function declarations *************************/
644 #endif /* PHLLCNFC_DATATYPES_H */
645 
646 
647