1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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 is the private interface file for NFA_RW
22  *
23  ******************************************************************************/
24 #ifndef NFA_RW_INT_H
25 #define NFA_RW_INT_H
26 
27 #include "nfa_api.h"
28 #include "nfa_rw_api.h"
29 #include "nfa_sys.h"
30 #include "nfc_api.h"
31 #include "rw_api.h"
32 
33 /*****************************************************************************
34 **  Constants and data types
35 *****************************************************************************/
36 
37 /* Interval for performing presence check (in ms) */
38 #ifndef NFA_RW_PRESENCE_CHECK_INTERVAL
39 #define NFA_RW_PRESENCE_CHECK_INTERVAL 750
40 #endif
41 
42 /* TLV detection status */
43 #define NFA_RW_TLV_DETECT_ST_OP_NOT_STARTED 0x00 /* No Tlv detected */
44 /* Lock control tlv detected */
45 #define NFA_RW_TLV_DETECT_ST_LOCK_TLV_OP_COMPLETE 0x01
46 /* Memory control tlv detected */
47 #define NFA_RW_TLV_DETECT_ST_MEM_TLV_OP_COMPLETE 0x02
48 /* Both Lock and Memory control Tlvs are detected */
49 #define NFA_RW_TLV_DETECT_ST_COMPLETE 0x03
50 
51 typedef uint8_t tNFA_RW_TLV_ST;
52 
53 /* RW events */
54 enum {
55   NFA_RW_OP_REQUEST_EVT = NFA_SYS_EVT_START(NFA_ID_RW),
56   NFA_RW_ACTIVATE_NTF_EVT,
57   NFA_RW_DEACTIVATE_NTF_EVT,
58   NFA_RW_PRESENCE_CHECK_TICK_EVT,
59   NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT,
60   NFA_RW_MAX_EVT
61 };
62 
63 /* BTA_RW operations */
64 enum {
65   NFA_RW_OP_DETECT_NDEF,
66   NFA_RW_OP_READ_NDEF,
67   NFA_RW_OP_WRITE_NDEF,
68   NFA_RW_OP_PRESENCE_CHECK,
69   NFA_RW_OP_FORMAT_TAG,
70   NFA_RW_OP_SEND_RAW_FRAME,
71 
72   /* Exclusive Type-1,Type-2 tag operations */
73   NFA_RW_OP_DETECT_LOCK_TLV,
74   NFA_RW_OP_DETECT_MEM_TLV,
75   NFA_RW_OP_SET_TAG_RO,
76 
77   /* Exclusive Type-1 tag operations */
78   NFA_RW_OP_T1T_RID,
79   NFA_RW_OP_T1T_RALL,
80   NFA_RW_OP_T1T_READ,
81   NFA_RW_OP_T1T_WRITE,
82   NFA_RW_OP_T1T_RSEG,
83   NFA_RW_OP_T1T_READ8,
84   NFA_RW_OP_T1T_WRITE8,
85 
86   /* Exclusive Type-2 tag operations */
87   NFA_RW_OP_T2T_READ,
88   NFA_RW_OP_T2T_WRITE,
89   NFA_RW_OP_T2T_SECTOR_SELECT,
90 
91   /* Exclusive Type-3 tag operations */
92   NFA_RW_OP_T3T_READ,
93   NFA_RW_OP_T3T_WRITE,
94   NFA_RW_OP_T3T_GET_SYSTEM_CODES,
95 
96   /* Exclusive ISO 15693 tag operations */
97   NFA_RW_OP_I93_INVENTORY,
98   NFA_RW_OP_I93_STAY_QUIET,
99   NFA_RW_OP_I93_READ_SINGLE_BLOCK,
100   NFA_RW_OP_I93_WRITE_SINGLE_BLOCK,
101   NFA_RW_OP_I93_LOCK_BLOCK,
102   NFA_RW_OP_I93_READ_MULTI_BLOCK,
103   NFA_RW_OP_I93_WRITE_MULTI_BLOCK,
104   NFA_RW_OP_I93_SELECT,
105   NFA_RW_OP_I93_RESET_TO_READY,
106   NFA_RW_OP_I93_WRITE_AFI,
107   NFA_RW_OP_I93_LOCK_AFI,
108   NFA_RW_OP_I93_WRITE_DSFID,
109   NFA_RW_OP_I93_LOCK_DSFID,
110   NFA_RW_OP_I93_GET_SYS_INFO,
111   NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS,
112 
113   NFA_RW_OP_MAX
114 };
115 typedef uint8_t tNFA_RW_OP;
116 
117 /* Enumeration of parameter structios for nfa_rw operations */
118 
119 /* NFA_RW_OP_WRITE_NDEF params */
120 typedef struct {
121   uint32_t len;
122   uint8_t* p_data;
123 } tNFA_RW_OP_PARAMS_WRITE_NDEF;
124 
125 /* NFA_RW_OP_SEND_RAW_FRAME params */
126 typedef struct { NFC_HDR* p_data; } tNFA_RW_OP_PARAMS_SEND_RAW_FRAME;
127 
128 /* NFA_RW_OP_SET_TAG_RO params */
129 typedef struct { bool b_hard_lock; } tNFA_RW_OP_PARAMS_CONFIG_READ_ONLY;
130 
131 /* NFA_RW_OP_T1T_READ params */
132 typedef struct {
133   uint8_t segment_number;
134   uint8_t block_number;
135   uint8_t index;
136 } tNFA_RW_OP_PARAMS_T1T_READ;
137 
138 /* NFA_RW_OP_T1T_WRITE_E8,NFA_RW_OP_T1T_WRITE_NE8
139    NFA_RW_OP_T1T_WRITE_E, NFA_RW_OP_T1T_WRITE_NE params  */
140 typedef struct {
141   bool b_erase;
142   uint8_t block_number;
143   uint8_t index;
144   uint8_t p_block_data[8];
145 } tNFA_RW_OP_PARAMS_T1T_WRITE;
146 
147 /* NFA_RW_OP_T2T_READ params */
148 typedef struct { uint8_t block_number; } tNFA_RW_OP_PARAMS_T2T_READ;
149 
150 /* NFA_RW_OP_T2T_WRITE params */
151 typedef struct {
152   uint8_t block_number;
153   uint8_t p_block_data[4];
154 } tNFA_RW_OP_PARAMS_T2T_WRITE;
155 
156 /* NFA_RW_OP_T2T_SECTOR_SELECT params */
157 typedef struct { uint8_t sector_number; } tNFA_RW_OP_PARAMS_T2T_SECTOR_SELECT;
158 
159 /* NFA_RW_OP_T3T_READ params */
160 typedef struct {
161   uint8_t num_blocks;
162   tNFA_T3T_BLOCK_DESC* p_block_desc;
163 } tNFA_RW_OP_PARAMS_T3T_READ;
164 
165 /* NFA_RW_OP_T3T_WRITE params */
166 typedef struct {
167   uint8_t num_blocks;
168   tNFA_T3T_BLOCK_DESC* p_block_desc;
169   uint8_t* p_block_data;
170 } tNFA_RW_OP_PARAMS_T3T_WRITE;
171 
172 /* NFA_RW_OP_I93_XXX params */
173 typedef struct {
174   bool uid_present;
175   uint8_t uid[I93_UID_BYTE_LEN];
176   bool afi_present;
177   uint8_t afi;
178   uint8_t dsfid;
179   uint16_t first_block_number;
180   uint16_t number_blocks;
181   uint8_t* p_data;
182 } tNFA_RW_OP_PARAMS_I93_CMD;
183 
184 /* Union of params for all reader/writer operations */
185 typedef union {
186   /* params for NFA_RW_OP_WRITE_NDEF */
187   tNFA_RW_OP_PARAMS_WRITE_NDEF write_ndef;
188 
189   /* params for NFA_RW_OP_SEND_RAW_FRAME */
190   tNFA_RW_OP_PARAMS_SEND_RAW_FRAME send_raw_frame;
191 
192   /* params for NFA_RW_OP_SET_TAG_RO */
193   tNFA_RW_OP_PARAMS_CONFIG_READ_ONLY set_readonly;
194 
195   /* params for NFA_RW_OP_T2T_READ and NFA_RW_OP_T1T_WRITE */
196   tNFA_RW_OP_PARAMS_T1T_READ t1t_read;
197   tNFA_RW_OP_PARAMS_T1T_WRITE t1t_write;
198 
199   /* params for NFA_RW_OP_T2T_READ,NFA_RW_OP_T2T_WRITE and
200    * NFA_RW_OP_T2T_SECTOR_SELECT */
201   tNFA_RW_OP_PARAMS_T2T_READ t2t_read;
202   tNFA_RW_OP_PARAMS_T2T_WRITE t2t_write;
203   tNFA_RW_OP_PARAMS_T2T_SECTOR_SELECT t2t_sector_select;
204 
205   /* params for NFA_RW_OP_T3T_READ and NFA_RW_OP_T3T_WRITE */
206   tNFA_RW_OP_PARAMS_T3T_READ t3t_read;
207   tNFA_RW_OP_PARAMS_T3T_WRITE t3t_write;
208 
209   /* params for NFA_RW_OP_PRESENCE_CHECK */
210   tNFA_RW_PRES_CHK_OPTION option;
211 
212   /* params for ISO 15693 */
213   tNFA_RW_OP_PARAMS_I93_CMD i93_cmd;
214 
215 } tNFA_RW_OP_PARAMS;
216 
217 /* data type for NFA_RW_op_req_EVT */
218 typedef struct {
219   NFC_HDR hdr;
220   tNFA_RW_OP op; /* NFA RW operation */
221   tNFA_RW_OP_PARAMS params;
222 } tNFA_RW_OPERATION;
223 
224 /* data type for NFA_RW_ACTIVATE_NTF */
225 typedef struct {
226   NFC_HDR hdr;
227   tNFC_ACTIVATE_DEVT* p_activate_params; /* Data from NFC_ACTIVATE_DEVT      */
228   bool excl_rf_not_active;               /* TRUE if not in exclusive RF mode */
229 } tNFA_RW_ACTIVATE_NTF;
230 
231 /* union of all data types */
232 typedef union {
233   /* GKI event buffer header */
234   NFC_HDR hdr;
235   tNFA_RW_OPERATION op_req;
236   tNFA_RW_ACTIVATE_NTF activate_ntf;
237 } tNFA_RW_MSG;
238 
239 /* NDEF detection status */
240 enum {
241   NFA_RW_NDEF_ST_UNKNOWN = 0, /* NDEF detection not performed yet */
242   NFA_RW_NDEF_ST_TRUE,        /* Tag is NDEF */
243   NFA_RW_NDEF_ST_FALSE        /* Tag is not NDEF */
244 };
245 typedef uint8_t tNFA_RW_NDEF_ST;
246 
247 /* flags for RW control block */
248 /* Activation while not in exclusive RF mode                                */
249 #define NFA_RW_FL_NOT_EXCL_RF_MODE 0x01
250 /* Waiting for response from tag for auto-presence check                    */
251 #define NFA_RW_FL_AUTO_PRESENCE_CHECK_BUSY 0x02
252 /* Read only tag                                                            */
253 #define NFA_RW_FL_TAG_IS_READONLY 0x04
254 /* Busy retrieving additional tag information                               */
255 #define NFA_RW_FL_ACTIVATION_NTF_PENDING 0x08
256 /* Tag operation is in progress                                             */
257 #define NFA_RW_FL_API_BUSY 0x10
258 /* Tag is been activated                                                    */
259 #define NFA_RW_FL_ACTIVATED 0x20
260 /* NDEF DETECTed OK                                                         */
261 #define NFA_RW_FL_NDEF_OK 0x40
262 
263 /* NFA RW control block */
264 typedef struct {
265   tNFA_RW_OP cur_op; /* Current operation */
266 
267   TIMER_LIST_ENT tle; /* list entry for nfa_rw timer */
268   tNFA_RW_MSG*
269       p_pending_msg; /* Pending API (if busy performing presence check) */
270 
271   /* Tag info */
272   tNFC_PROTOCOL protocol;
273   tNFC_INTF_TYPE intf_type;
274   uint8_t pa_sel_res;
275   tNFC_RF_TECH_N_MODE activated_tech_mode; /* activated technology and mode */
276 
277   bool b_hard_lock;
278 
279   tNFA_RW_MSG*
280       p_buffer_rw_msg; /* Buffer to hold incoming cmd while reading tag id */
281 
282   /* TLV info */
283   tNFA_RW_TLV_ST tlv_st; /* TLV detection status */
284 
285   /* NDEF info */
286   tNFA_RW_NDEF_ST ndef_st; /* NDEF detection status */
287   uint32_t ndef_max_size;  /* max number of bytes available for NDEF data */
288   uint32_t ndef_cur_size;  /* current size of stored NDEF data (in bytes) */
289   uint8_t* p_ndef_buf;
290   uint32_t ndef_rd_offset; /* current read-offset of incoming NDEF data */
291 
292   /* Current NDEF Write info */
293   uint8_t* p_ndef_wr_buf; /* Pointer to NDEF data being written */
294   uint32_t ndef_wr_len;   /* Length of NDEF data being written */
295 
296   /* Reactivating type 2 tag after NACK rsp */
297   tRW_EVENT halt_event; /* Event ID from stack after NACK response */
298   tRW_DATA rw_data;     /* Event Data from stack after NACK response */
299   bool skip_dyn_locks;  /* To skip reading dynamic locks during NDEF Detect */
300 
301   /* Flags (see defintions for NFA_RW_FL_* ) */
302   uint8_t flags;
303 
304   /* ISO 15693 tag memory information */
305   uint16_t i93_afi_location;
306   uint8_t i93_dsfid;
307   uint8_t i93_block_size;
308   uint16_t i93_num_block;
309   uint8_t i93_uid[I93_UID_BYTE_LEN];
310 } tNFA_RW_CB;
311 extern tNFA_RW_CB nfa_rw_cb;
312 
313 /* type definition for action functions */
314 typedef bool (*tNFA_RW_ACTION)(tNFA_RW_MSG* p_data);
315 
316 /* Internal nfa_rw function prototypes */
317 extern void nfa_rw_stop_presence_check_timer(void);
318 
319 /* Action function prototypes */
320 extern bool nfa_rw_handle_op_req(tNFA_RW_MSG* p_data);
321 extern bool nfa_rw_activate_ntf(tNFA_RW_MSG* p_data);
322 extern bool nfa_rw_deactivate_ntf(tNFA_RW_MSG* p_data);
323 extern bool nfa_rw_presence_check_tick(tNFA_RW_MSG* p_data);
324 extern bool nfa_rw_presence_check_timeout(tNFA_RW_MSG* p_data);
325 extern void nfa_rw_handle_sleep_wakeup_rsp(tNFC_STATUS status);
326 extern void nfa_rw_handle_presence_check_rsp(tNFC_STATUS status);
327 extern void nfa_rw_command_complete(void);
328 extern bool nfa_rw_handle_event(NFC_HDR* p_msg);
329 
330 extern void nfa_rw_free_ndef_rx_buf(void);
331 extern void nfa_rw_sys_disable(void);
332 
333 #endif /* NFA_DM_INT_H */
334