1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2014 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains the Near Field Communication (NFC) Card Emulation
22  *  mode related internal function / definitions.
23  *
24  ******************************************************************************/
25 
26 #ifndef CE_INT_H_
27 #define CE_INT_H_
28 
29 #include "ce_api.h"
30 
31 #if (CE_TEST_INCLUDED == FALSE)
32 #define CE_MIN_SUP_PROTO NCI_PROTOCOL_FELICA
33 #define CE_MAX_SUP_PROTO NCI_PROTOCOL_ISO4
34 #else
35 #define CE_MIN_SUP_PROTO NCI_PROTOCOL_TYPE1
36 #define CE_MAX_SUP_PROTO NCI_PROTOCOL_MIFARE
37 #endif
38 
39 /* CE Type 3 Tag structures */
40 
41 /* Type 3 Tag NDEF card-emulation */
42 typedef struct {
43   bool initialized;
44   uint8_t version; /* Ver: peer version */
45   uint8_t
46       nbr; /* NBr: number of blocks that can be read using one Check command */
47   uint8_t nbw;    /* Nbw: number of blocks that can be written using one Update
48                      command */
49   uint16_t nmaxb; /* Nmaxb: maximum number of blocks available for NDEF data */
50   uint8_t writef; /* WriteFlag: 00h if writing data finished; 0Fh if writing
51                      data in progress */
52   uint8_t
53       rwflag; /* RWFlag: 00h NDEF is read-only; 01h if read/write available */
54   uint32_t ln;
55   uint8_t* p_buf; /* Current contents for READs */
56 
57   /* Scratch NDEF buffer (for update NDEF commands) */
58   uint8_t scratch_writef;
59   uint32_t scratch_ln;
60   uint8_t* p_scratch_buf; /* Scratch buffer for WRITE/readback */
61 } tCE_T3T_NDEF_INFO;
62 
63 /* Type 3 Tag current command processing */
64 typedef struct {
65   uint16_t service_code_list[T3T_MSG_SERVICE_LIST_MAX];
66   uint8_t* p_block_list_start;
67   uint8_t* p_block_data_start;
68   uint8_t num_services;
69   uint8_t num_blocks;
70 } tCE_T3T_CUR_CMD;
71 
72 /* Type 3 Tag control blcok */
73 typedef struct {
74   uint8_t state;
75   uint16_t system_code;
76   uint8_t local_nfcid2[NCI_RF_F_UID_LEN];
77   uint8_t local_pmm[NCI_T3T_PMM_LEN];
78   tCE_T3T_NDEF_INFO ndef_info;
79   tCE_T3T_CUR_CMD cur_cmd;
80 } tCE_T3T_MEM;
81 
82 /* CE Type 4 Tag control blocks */
83 typedef struct {
84   uint8_t aid_len;
85   uint8_t aid[NFC_MAX_AID_LEN];
86   tCE_CBACK* p_cback;
87 } tCE_T4T_REG_AID; /* registered AID table */
88 
89 typedef struct {
90   TIMER_LIST_ENT timer; /* timeout for update file              */
91   uint8_t cc_file[T4T_FC_TLV_OFFSET_IN_CC + T4T_FILE_CONTROL_TLV_SIZE];
92   uint8_t* p_ndef_msg;    /* storage of NDEF message              */
93   uint16_t nlen;          /* current size of NDEF message         */
94   uint16_t max_file_size; /* size of storage + 2 bytes for NLEN   */
95   uint8_t* p_scratch_buf; /* temp storage of NDEF message for update */
96 
97 /* T4T CE App is selected       */
98 #define CE_T4T_STATUS_T4T_APP_SELECTED 0x01
99 /* Registered AID is selected   */
100 #define CE_T4T_STATUS_REG_AID_SELECTED 0x02
101 /* CC file is selected          */
102 #define CE_T4T_STATUS_CC_FILE_SELECTED 0x04
103 /* NDEF file is selected        */
104 #define CE_T4T_STATUS_NDEF_SELECTED 0x08
105 /* NDEF is read-only            */
106 #define CE_T4T_STATUS_NDEF_FILE_READ_ONLY 0x10
107 /* NDEF is updating             */
108 #define CE_T4T_STATUS_NDEF_FILE_UPDATING 0x20
109 /* Wildcard AID selected        */
110 #define CE_T4T_STATUS_WILDCARD_AID_SELECTED 0x40
111 
112   uint8_t status;
113 
114   tCE_CBACK* p_wildcard_aid_cback; /* registered wildcard AID callback */
115   tCE_T4T_REG_AID reg_aid[CE_T4T_MAX_REG_AID]; /* registered AID table */
116   uint8_t selected_aid_idx;
117 } tCE_T4T_MEM;
118 
119 /* CE memory control blocks */
120 typedef struct {
121   tCE_T3T_MEM t3t;
122   tCE_T4T_MEM t4t;
123 } tCE_MEM;
124 
125 /* CE control blocks */
126 typedef struct {
127   tCE_MEM mem;
128   tCE_CBACK* p_cback;
129   uint8_t* p_ndef;   /* the memory starting from NDEF */
130   uint16_t ndef_max; /* max size of p_ndef */
131   uint16_t ndef_cur; /* current size of p_ndef */
132   tNFC_RF_TECH tech;
133 } tCE_CB;
134 
135 /*
136 ** CE Type 4 Tag Definition
137 */
138 
139 /* Max data size using a single ReadBinary. 2 bytes are for status bytes */
140 #define CE_T4T_MAX_LE                                          \
141   (NFC_CE_POOL_BUF_SIZE - NFC_HDR_SIZE - NCI_MSG_OFFSET_SIZE - \
142    NCI_DATA_HDR_SIZE - T4T_RSP_STATUS_WORDS_SIZE)
143 
144 /* Max data size using a single UpdateBinary. 6 bytes are for CLA, INS, P1, P2,
145  * Lc */
146 #define CE_T4T_MAX_LC                                        \
147   (NFC_CE_POOL_BUF_SIZE - NFC_HDR_SIZE - NCI_DATA_HDR_SIZE - \
148    T4T_CMD_MAX_HDR_SIZE)
149 
150 /*****************************************************************************
151 **  EXTERNAL FUNCTION DECLARATIONS
152 *****************************************************************************/
153 
154 /* Global NFC data */
155 extern tCE_CB ce_cb;
156 
157 extern void ce_init(void);
158 
159 /* ce_t3t internal functions */
160 void ce_t3t_init(void);
161 tNFC_STATUS ce_select_t3t(uint16_t system_code,
162                           uint8_t nfcid2[NCI_RF_F_UID_LEN]);
163 
164 /* ce_t4t internal functions */
165 extern tNFC_STATUS ce_select_t4t(void);
166 extern void ce_t4t_process_timeout(TIMER_LIST_ENT* p_tle);
167 
168 #endif /* CE_INT_H_ */
169