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 /* 2^8=256. CB use uint8_t for BytesPerPage, so max is 7 */
40 #define CE_MAX_BYTE_PER_PAGE 7
41 
42 /* CE Type 3 Tag structures */
43 
44 /* Type 3 Tag NDEF card-emulation */
45 typedef struct {
46   bool initialized;
47   uint8_t version; /* Ver: peer version */
48   uint8_t
49       nbr; /* NBr: number of blocks that can be read using one Check command */
50   uint8_t nbw;    /* Nbw: number of blocks that can be written using one Update
51                      command */
52   uint16_t nmaxb; /* Nmaxb: maximum number of blocks available for NDEF data */
53   uint8_t writef; /* WriteFlag: 00h if writing data finished; 0Fh if writing
54                      data in progress */
55   uint8_t
56       rwflag; /* RWFlag: 00h NDEF is read-only; 01h if read/write available */
57   uint32_t ln;
58   uint8_t* p_buf; /* Current contents for READs */
59 
60   /* Scratch NDEF buffer (for update NDEF commands) */
61   uint8_t scratch_writef;
62   uint32_t scratch_ln;
63   uint8_t* p_scratch_buf; /* Scratch buffer for WRITE/readback */
64 } tCE_T3T_NDEF_INFO;
65 
66 /* Type 3 Tag current command processing */
67 typedef struct {
68   uint16_t service_code_list[T3T_MSG_SERVICE_LIST_MAX];
69   uint8_t* p_block_list_start;
70   uint8_t* p_block_data_start;
71   uint8_t num_services;
72   uint8_t num_blocks;
73 } tCE_T3T_CUR_CMD;
74 
75 /* Type 3 Tag control blcok */
76 typedef struct {
77   uint8_t state;
78   uint16_t system_code;
79   uint8_t local_nfcid2[NCI_RF_F_UID_LEN];
80   uint8_t local_pmm[NCI_T3T_PMM_LEN];
81   tCE_T3T_NDEF_INFO ndef_info;
82   tCE_T3T_CUR_CMD cur_cmd;
83 } tCE_T3T_MEM;
84 
85 /* CE Type 4 Tag control blocks */
86 typedef struct {
87   uint8_t aid_len;
88   uint8_t aid[NFC_MAX_AID_LEN];
89   tCE_CBACK* p_cback;
90 } tCE_T4T_REG_AID; /* registered AID table */
91 
92 typedef struct {
93   TIMER_LIST_ENT timer; /* timeout for update file              */
94   uint8_t cc_file[T4T_FC_TLV_OFFSET_IN_CC + T4T_FILE_CONTROL_TLV_SIZE];
95   uint8_t* p_ndef_msg;    /* storage of NDEF message              */
96   uint16_t nlen;          /* current size of NDEF message         */
97   uint16_t max_file_size; /* size of storage + 2 bytes for NLEN   */
98   uint8_t* p_scratch_buf; /* temp storage of NDEF message for update */
99 
100 /* T4T CE App is selected       */
101 #define CE_T4T_STATUS_T4T_APP_SELECTED 0x01
102 /* Registered AID is selected   */
103 #define CE_T4T_STATUS_REG_AID_SELECTED 0x02
104 /* CC file is selected          */
105 #define CE_T4T_STATUS_CC_FILE_SELECTED 0x04
106 /* NDEF file is selected        */
107 #define CE_T4T_STATUS_NDEF_SELECTED 0x08
108 /* NDEF is read-only            */
109 #define CE_T4T_STATUS_NDEF_FILE_READ_ONLY 0x10
110 /* NDEF is updating             */
111 #define CE_T4T_STATUS_NDEF_FILE_UPDATING 0x20
112 /* Wildcard AID selected        */
113 #define CE_T4T_STATUS_WILDCARD_AID_SELECTED 0x40
114 
115   uint8_t status;
116 
117   tCE_CBACK* p_wildcard_aid_cback; /* registered wildcard AID callback */
118   tCE_T4T_REG_AID reg_aid[CE_T4T_MAX_REG_AID]; /* registered AID table */
119   uint8_t selected_aid_idx;
120 } tCE_T4T_MEM;
121 
122 /* CE memory control blocks */
123 typedef struct {
124   tCE_T3T_MEM t3t;
125   tCE_T4T_MEM t4t;
126 } tCE_MEM;
127 
128 /* CE control blocks */
129 typedef struct {
130   tCE_MEM mem;
131   tCE_CBACK* p_cback;
132   uint8_t* p_ndef;   /* the memory starting from NDEF */
133   uint16_t ndef_max; /* max size of p_ndef */
134   uint16_t ndef_cur; /* current size of p_ndef */
135   tNFC_RF_TECH tech;
136   uint8_t trace_level;
137 
138 } tCE_CB;
139 
140 /*
141 ** CE Type 4 Tag Definition
142 */
143 
144 /* Max data size using a single ReadBinary. 2 bytes are for status bytes */
145 #define CE_T4T_MAX_LE                                          \
146   (NFC_CE_POOL_BUF_SIZE - NFC_HDR_SIZE - NCI_MSG_OFFSET_SIZE - \
147    NCI_DATA_HDR_SIZE - T4T_RSP_STATUS_WORDS_SIZE)
148 
149 /* Max data size using a single UpdateBinary. 6 bytes are for CLA, INS, P1, P2,
150  * Lc */
151 #define CE_T4T_MAX_LC                                        \
152   (NFC_CE_POOL_BUF_SIZE - NFC_HDR_SIZE - NCI_DATA_HDR_SIZE - \
153    T4T_CMD_MAX_HDR_SIZE)
154 
155 /*****************************************************************************
156 **  EXTERNAL FUNCTION DECLARATIONS
157 *****************************************************************************/
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
161 
162 /* Global NFC data */
163 extern tCE_CB ce_cb;
164 
165 extern void ce_init(void);
166 
167 /* ce_t3t internal functions */
168 void ce_t3t_init(void);
169 tNFC_STATUS ce_select_t3t(uint16_t system_code,
170                           uint8_t nfcid2[NCI_RF_F_UID_LEN]);
171 
172 /* ce_t4t internal functions */
173 extern tNFC_STATUS ce_select_t4t(void);
174 extern void ce_t4t_process_timeout(TIMER_LIST_ENT* p_tle);
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif /* CE_INT_H_ */
181