1 /******************************************************************************
2  *
3  *  Copyright (C) 2010-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 action functions the NFA_RW state machine.
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <log/log.h>
27 #include <string.h>
28 
29 #include "ndef_utils.h"
30 #include "nfa_dm_int.h"
31 #include "nfa_mem_co.h"
32 #include "nfa_rw_int.h"
33 #include "nfc_config.h"
34 
35 using android::base::StringPrintf;
36 
37 #define NFA_RW_OPTION_INVALID 0xFF
38 
39 /* Tag sleep req cmd*/
40 uint8_t NFA_RW_TAG_SLP_REQ[] = {0x50, 0x00};
41 
42 /* Local static function prototypes */
43 static tNFC_STATUS nfa_rw_start_ndef_read(void);
44 static tNFC_STATUS nfa_rw_start_ndef_write(void);
45 static tNFC_STATUS nfa_rw_start_ndef_detection(void);
46 static tNFC_STATUS nfa_rw_config_tag_ro(bool b_hard_lock);
47 static bool nfa_rw_op_req_while_busy(tNFA_RW_MSG* p_data);
48 static bool nfa_rw_op_req_while_inactive(tNFA_RW_MSG* p_data);
49 static void nfa_rw_error_cleanup(uint8_t event);
50 static void nfa_rw_presence_check(tNFA_RW_MSG* p_data);
51 static void nfa_rw_handle_t2t_evt(tRW_EVENT event, tRW_DATA* p_rw_data);
52 static bool nfa_rw_detect_ndef(void);
53 static void nfa_rw_cback(tRW_EVENT event, tRW_DATA* p_rw_data);
54 static void nfa_rw_handle_mfc_evt(tRW_EVENT event, tRW_DATA* p_rw_data);
55 
56 extern void rw_t4t_handle_isodep_nak_fallback();
57 
58 /*******************************************************************************
59 **
60 ** Function         nfa_rw_free_ndef_rx_buf
61 **
62 ** Description      Free buffer allocated to hold incoming NDEF message
63 **
64 ** Returns          Nothing
65 **
66 *******************************************************************************/
nfa_rw_free_ndef_rx_buf(void)67 void nfa_rw_free_ndef_rx_buf(void) {
68   if (nfa_rw_cb.p_ndef_buf) {
69     nfa_mem_co_free(nfa_rw_cb.p_ndef_buf);
70     nfa_rw_cb.p_ndef_buf = nullptr;
71   }
72 }
73 
74 /*******************************************************************************
75 **
76 ** Function         nfa_rw_store_ndef_rx_buf
77 **
78 ** Description      Store data into NDEF buffer
79 **
80 ** Returns          Nothing
81 **
82 *******************************************************************************/
nfa_rw_store_ndef_rx_buf(tRW_DATA * p_rw_data)83 static void nfa_rw_store_ndef_rx_buf(tRW_DATA* p_rw_data) {
84   uint8_t* p;
85 
86   p = (uint8_t*)(p_rw_data->data.p_data + 1) + p_rw_data->data.p_data->offset;
87 
88   if ((nfa_rw_cb.ndef_rd_offset + p_rw_data->data.p_data->len) <=
89       nfa_rw_cb.ndef_cur_size) {
90     /* Save data into buffer */
91     memcpy(&nfa_rw_cb.p_ndef_buf[nfa_rw_cb.ndef_rd_offset], p,
92            p_rw_data->data.p_data->len);
93     nfa_rw_cb.ndef_rd_offset += p_rw_data->data.p_data->len;
94   } else {
95     LOG(ERROR) << StringPrintf("Exceed ndef_cur_size error");
96     android_errorWriteLog(0x534e4554, "123583388");
97   }
98 
99   GKI_freebuf(p_rw_data->data.p_data);
100   p_rw_data->data.p_data = nullptr;
101 }
102 
103 /*******************************************************************************
104 **
105 ** Function         nfa_rw_send_data_to_upper
106 **
107 ** Description      Send data to upper layer
108 **
109 ** Returns          Nothing
110 **
111 *******************************************************************************/
nfa_rw_send_data_to_upper(tRW_DATA * p_rw_data)112 static void nfa_rw_send_data_to_upper(tRW_DATA* p_rw_data) {
113   tNFA_CONN_EVT_DATA conn_evt_data;
114 
115   if ((p_rw_data->status == NFC_STATUS_TIMEOUT) ||
116       (p_rw_data->data.p_data == nullptr))
117     return;
118 
119   LOG(VERBOSE) << StringPrintf(
120       "nfa_rw_send_data_to_upper: Len [0x%X] Status [%s]",
121       p_rw_data->data.p_data->len,
122       NFC_GetStatusName(p_rw_data->data.status).c_str());
123 
124   /* Notify conn cback of NFA_DATA_EVT */
125   conn_evt_data.data.status = p_rw_data->data.status;
126   conn_evt_data.data.p_data =
127       (uint8_t*)(p_rw_data->data.p_data + 1) + p_rw_data->data.p_data->offset;
128   conn_evt_data.data.len = p_rw_data->data.p_data->len;
129 
130   nfa_dm_act_conn_cback_notify(NFA_DATA_EVT, &conn_evt_data);
131 
132   GKI_freebuf(p_rw_data->data.p_data);
133   p_rw_data->data.p_data = nullptr;
134 }
135 
136 /*******************************************************************************
137 **
138 ** Function         nfa_rw_error_cleanup
139 **
140 ** Description      Handle failure - signal command complete and notify app
141 **
142 ** Returns          Nothing
143 **
144 *******************************************************************************/
nfa_rw_error_cleanup(uint8_t event)145 static void nfa_rw_error_cleanup(uint8_t event) {
146   tNFA_CONN_EVT_DATA conn_evt_data;
147 
148   nfa_rw_command_complete();
149 
150   conn_evt_data.status = NFA_STATUS_FAILED;
151 
152   nfa_dm_act_conn_cback_notify(event, &conn_evt_data);
153 }
154 
155 /*******************************************************************************
156 **
157 ** Function         nfa_rw_check_start_presence_check_timer
158 **
159 ** Description      Start timer to wait for specified time before presence check
160 **
161 ** Returns          Nothing
162 **
163 *******************************************************************************/
nfa_rw_check_start_presence_check_timer(uint16_t presence_check_start_delay)164 static void nfa_rw_check_start_presence_check_timer(
165     uint16_t presence_check_start_delay) {
166   if (!p_nfa_dm_cfg->auto_presence_check) return;
167 
168   if (nfa_rw_cb.flags & NFA_RW_FL_NOT_EXCL_RF_MODE) {
169     if (presence_check_start_delay) {
170       LOG(VERBOSE) << StringPrintf("Starting presence check timer...");
171       nfa_sys_start_timer(&nfa_rw_cb.tle, NFA_RW_PRESENCE_CHECK_TICK_EVT,
172                           presence_check_start_delay);
173     } else {
174       /* Presence check now */
175       nfa_rw_presence_check(nullptr);
176     }
177   }
178 }
179 
180 /*******************************************************************************
181 **
182 ** Function         nfa_rw_stop_presence_check_timer
183 **
184 ** Description      Stop timer for presence check
185 **
186 ** Returns          Nothing
187 **
188 *******************************************************************************/
nfa_rw_stop_presence_check_timer(void)189 void nfa_rw_stop_presence_check_timer(void) {
190   nfa_sys_stop_timer(&nfa_rw_cb.tle);
191   LOG(VERBOSE) << StringPrintf("Stopped presence check timer (if started)");
192 }
193 
194 /*******************************************************************************
195 **
196 ** Function         nfa_rw_handle_ndef_detect
197 **
198 ** Description      Handler for NDEF detection reader/writer event
199 **
200 ** Returns          Nothing
201 **
202 *******************************************************************************/
nfa_rw_handle_ndef_detect(tRW_DATA * p_rw_data)203 static void nfa_rw_handle_ndef_detect(tRW_DATA* p_rw_data) {
204   tNFA_CONN_EVT_DATA conn_evt_data;
205 
206   LOG(VERBOSE) << StringPrintf(
207       "NDEF Detection completed: cur_size=%i, max_size=%i, flags=0x%x",
208       p_rw_data->ndef.cur_size, p_rw_data->ndef.max_size,
209       p_rw_data->ndef.flags);
210 
211   /* Check if NDEF detection succeeded */
212   if (p_rw_data->ndef.status == NFC_STATUS_OK) {
213     /* Set NDEF detection state */
214     nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_TRUE;
215     nfa_rw_cb.flags |= NFA_RW_FL_NDEF_OK;
216 
217     /* Store ndef properties */
218     conn_evt_data.ndef_detect.status = NFA_STATUS_OK;
219     conn_evt_data.ndef_detect.protocol = p_rw_data->ndef.protocol;
220     conn_evt_data.ndef_detect.cur_size = nfa_rw_cb.ndef_cur_size =
221         p_rw_data->ndef.cur_size;
222     conn_evt_data.ndef_detect.max_size = nfa_rw_cb.ndef_max_size =
223         p_rw_data->ndef.max_size;
224     conn_evt_data.ndef_detect.flags = p_rw_data->ndef.flags;
225 
226     if (p_rw_data->ndef.flags & RW_NDEF_FL_READ_ONLY)
227       nfa_rw_cb.flags |= NFA_RW_FL_TAG_IS_READONLY;
228     else
229       nfa_rw_cb.flags &= ~NFA_RW_FL_TAG_IS_READONLY;
230 
231     /* Determine what operation triggered the NDEF detection procedure */
232     if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
233       /* if ndef detection was done as part of ndef-read operation, then perform
234        * ndef read now */
235       conn_evt_data.status = nfa_rw_start_ndef_read();
236       if (conn_evt_data.status != NFA_STATUS_OK) {
237         /* Failed to start NDEF Read */
238 
239         /* Command complete - perform cleanup, notify app */
240         nfa_rw_command_complete();
241         nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
242       }
243     } else if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
244       /* if ndef detection was done as part of ndef-write operation, then
245        * perform ndef write now */
246       conn_evt_data.status = nfa_rw_start_ndef_write();
247       if (conn_evt_data.status != NFA_STATUS_OK) {
248         /* Failed to start NDEF Write.  */
249 
250         /* Command complete - perform cleanup, notify app */
251         nfa_rw_command_complete();
252         nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
253       }
254     } else {
255       /* current op was stand-alone NFA_DetectNDef. Command complete - perform
256        * cleanup and notify app */
257       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
258       nfa_rw_command_complete();
259 
260       nfa_dm_act_conn_cback_notify(NFA_NDEF_DETECT_EVT, &conn_evt_data);
261     }
262   } else {
263     /* NDEF detection failed... */
264 
265     /* Command complete - perform cleanup, notify app */
266     nfa_rw_command_complete();
267     nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_FALSE;
268     conn_evt_data.status = p_rw_data->ndef.status;
269 
270     if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
271       /* if ndef detection was done as part of ndef-read operation, then notify
272        * NDEF handlers of failure */
273       nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
274 
275       /* Notify app of read status */
276       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
277     } else if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
278       /* if ndef detection was done as part of ndef-write operation, then notify
279        * app of failure */
280       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
281     } else if (nfa_rw_cb.cur_op == NFA_RW_OP_DETECT_NDEF) {
282       conn_evt_data.ndef_detect.protocol = p_rw_data->ndef.protocol;
283       /* current op was stand-alone NFA_DetectNDef. Notify app of failure */
284       if (p_rw_data->ndef.status == NFC_STATUS_TIMEOUT) {
285         /* Tag could have moved away */
286         conn_evt_data.ndef_detect.cur_size = 0;
287         conn_evt_data.ndef_detect.max_size = 0;
288         conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
289         conn_evt_data.ndef_detect.status = NFA_STATUS_TIMEOUT;
290       } else {
291         /* NDEF Detection failed for other reasons */
292         conn_evt_data.ndef_detect.cur_size = nfa_rw_cb.ndef_cur_size =
293             p_rw_data->ndef.cur_size;
294         conn_evt_data.ndef_detect.max_size = nfa_rw_cb.ndef_max_size =
295             p_rw_data->ndef.max_size;
296         conn_evt_data.ndef_detect.flags = p_rw_data->ndef.flags;
297       }
298       nfa_dm_act_conn_cback_notify(NFA_NDEF_DETECT_EVT, &conn_evt_data);
299     }
300 
301     nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
302   }
303 }
304 
305 /*******************************************************************************
306 **
307 ** Function         nfa_rw_handle_tlv_detect
308 **
309 ** Description      Handler for TLV detection reader/writer event
310 **
311 ** Returns          Nothing
312 **
313 *******************************************************************************/
nfa_rw_handle_tlv_detect(tRW_DATA * p_rw_data)314 static void nfa_rw_handle_tlv_detect(tRW_DATA* p_rw_data) {
315   tNFA_CONN_EVT_DATA conn_evt_data;
316 
317   /* Set TLV detection state */
318   if (nfa_rw_cb.cur_op == NFA_RW_OP_SET_TAG_RO) {
319     if (nfa_rw_cb.tlv_st == NFA_RW_TLV_DETECT_ST_OP_NOT_STARTED) {
320       nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_LOCK_TLV_OP_COMPLETE;
321     } else {
322       nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_COMPLETE;
323     }
324   } else {
325     if (nfa_rw_cb.cur_op == NFA_RW_OP_DETECT_LOCK_TLV) {
326       nfa_rw_cb.tlv_st |= NFA_RW_TLV_DETECT_ST_LOCK_TLV_OP_COMPLETE;
327     } else if (nfa_rw_cb.cur_op == NFA_RW_OP_DETECT_MEM_TLV) {
328       nfa_rw_cb.tlv_st |= NFA_RW_TLV_DETECT_ST_MEM_TLV_OP_COMPLETE;
329     }
330   }
331 
332   /* Check if TLV detection succeeded */
333   if (p_rw_data->tlv.status == NFC_STATUS_OK) {
334     LOG(VERBOSE) << StringPrintf("TLV Detection succeeded: num_bytes=%i",
335                                p_rw_data->tlv.num_bytes);
336 
337     /* Store tlv properties */
338     conn_evt_data.tlv_detect.status = NFA_STATUS_OK;
339     conn_evt_data.tlv_detect.protocol = p_rw_data->tlv.protocol;
340     conn_evt_data.tlv_detect.num_bytes = p_rw_data->tlv.num_bytes;
341 
342     /* Determine what operation triggered the TLV detection procedure */
343     if (nfa_rw_cb.cur_op == NFA_RW_OP_SET_TAG_RO) {
344       if (nfa_rw_config_tag_ro(nfa_rw_cb.b_hard_lock) != NFC_STATUS_OK) {
345         /* Failed to set tag read only */
346         conn_evt_data.tlv_detect.status = NFA_STATUS_FAILED;
347         nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
348       }
349     } else {
350       /* current op was stand-alone NFA_DetectTlv. Command complete - perform
351        * cleanup and notify app */
352       nfa_rw_command_complete();
353       nfa_dm_act_conn_cback_notify(NFA_TLV_DETECT_EVT, &conn_evt_data);
354     }
355   }
356 
357   /* Handle failures */
358   if (p_rw_data->tlv.status != NFC_STATUS_OK) {
359     /* Command complete - perform cleanup, notify the app */
360     nfa_rw_command_complete();
361 
362     conn_evt_data.tlv_detect.status = NFA_STATUS_FAILED;
363     if ((nfa_rw_cb.cur_op == NFA_RW_OP_DETECT_LOCK_TLV) ||
364         (nfa_rw_cb.cur_op == NFA_RW_OP_DETECT_MEM_TLV)) {
365       nfa_dm_act_conn_cback_notify(NFA_TLV_DETECT_EVT, &conn_evt_data);
366     } else if (nfa_rw_cb.cur_op == NFA_RW_OP_SET_TAG_RO) {
367       if (nfa_rw_config_tag_ro(nfa_rw_cb.b_hard_lock) != NFC_STATUS_OK) {
368         /* Failed to set tag read only */
369         conn_evt_data.tlv_detect.status = NFA_STATUS_FAILED;
370         nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
371       }
372     }
373   }
374 }
375 
376 /*******************************************************************************
377 **
378 ** Function         nfa_rw_handle_sleep_wakeup_rsp
379 **
380 ** Description      Handl sleep wakeup
381 **
382 ** Returns          Nothing
383 **
384 *******************************************************************************/
nfa_rw_handle_sleep_wakeup_rsp(tNFC_STATUS status)385 void nfa_rw_handle_sleep_wakeup_rsp(tNFC_STATUS status) {
386   tNFC_ACTIVATE_DEVT activate_params;
387   tRW_EVENT event;
388 
389   if ((nfa_rw_cb.halt_event != RW_T2T_MAX_EVT) &&
390       (nfa_rw_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A) &&
391       (nfa_rw_cb.protocol == NFC_PROTOCOL_T2T) &&
392       (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T)) {
393     LOG(VERBOSE) << StringPrintf(
394         "nfa_rw_handle_sleep_wakeup_rsp; Attempt to wake up Type 2 tag from "
395         "HALT State is complete");
396     if (status == NFC_STATUS_OK) {
397       /* Type 2 Tag is wakeup from HALT state */
398       LOG(VERBOSE) << StringPrintf(
399           "nfa_rw_handle_sleep_wakeup_rsp; Handle the NACK rsp received now");
400       /* Initialize control block */
401       activate_params.protocol = nfa_rw_cb.protocol;
402       activate_params.rf_tech_param.param.pa.sel_rsp = nfa_rw_cb.pa_sel_res;
403       activate_params.rf_tech_param.mode = nfa_rw_cb.activated_tech_mode;
404 
405       /* Initialize RW module */
406       if ((RW_SetActivatedTagType(&activate_params, nfa_rw_cback)) !=
407           NFC_STATUS_OK) {
408         /* Log error (stay in NFA_RW_ST_ACTIVATED state until deactivation) */
409         LOG(ERROR) << StringPrintf("RW_SetActivatedTagType failed.");
410         if (nfa_rw_cb.halt_event == RW_T2T_READ_CPLT_EVT) {
411           if (nfa_rw_cb.rw_data.data.p_data)
412             GKI_freebuf(nfa_rw_cb.rw_data.data.p_data);
413           nfa_rw_cb.rw_data.data.p_data = nullptr;
414         }
415         /* Do not try to detect NDEF again but just notify current operation
416          * failed */
417         nfa_rw_cb.halt_event = RW_T2T_MAX_EVT;
418       }
419     }
420 
421     /* The current operation failed with NACK rsp from type 2 tag */
422     nfa_rw_cb.rw_data.status = NFC_STATUS_FAILED;
423     event = nfa_rw_cb.halt_event;
424 
425     /* Got NACK rsp during presence check and legacy presence check performed */
426     if (nfa_rw_cb.cur_op == NFA_RW_OP_PRESENCE_CHECK)
427       nfa_rw_cb.rw_data.status = status;
428 
429     /* If cannot Sleep wakeup tag, then NDEF Detect operation is complete */
430     if ((status != NFC_STATUS_OK) &&
431         (nfa_rw_cb.halt_event == RW_T2T_NDEF_DETECT_EVT))
432       nfa_rw_cb.halt_event = RW_T2T_MAX_EVT;
433 
434     nfa_rw_handle_t2t_evt(event, &nfa_rw_cb.rw_data);
435     nfa_rw_cb.halt_event = RW_T2T_MAX_EVT;
436 
437     /* If Type 2 tag sleep wakeup failed and If in normal mode (not-exclusive RF
438      * mode) then deactivate the link if sleep wakeup failed */
439     if ((nfa_rw_cb.flags & NFA_RW_FL_NOT_EXCL_RF_MODE) &&
440         (status != NFC_STATUS_OK)) {
441       LOG(VERBOSE) << StringPrintf("Sleep wakeup failed. Deactivating...");
442       nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
443     }
444   } else {
445     LOG(VERBOSE) << StringPrintf(
446         "nfa_rw_handle_sleep_wakeup_rsp; Legacy presence check performed");
447     /* Legacy presence check performed */
448     nfa_rw_handle_presence_check_rsp(status);
449   }
450 }
451 
452 /*******************************************************************************
453 **
454 ** Function         nfa_rw_handle_presence_check_rsp
455 **
456 ** Description      Handler RW_T#t_PRESENCE_CHECK_EVT
457 **
458 ** Returns          Nothing
459 **
460 *******************************************************************************/
nfa_rw_handle_presence_check_rsp(tNFC_STATUS status)461 void nfa_rw_handle_presence_check_rsp(tNFC_STATUS status) {
462   NFC_HDR* p_pending_msg;
463 
464   /* Stop the presence check timer - timer may have been started when presence
465    * check started */
466   nfa_rw_stop_presence_check_timer();
467   // The CLF can report more detailed information on the failure cases,
468   // some failures can be considered as success presence check.
469   if ((status == NFA_STATUS_RF_UNEXPECTED_DATA) ||
470       (status == NFA_STATUS_RF_PROTOCOL_ERR)) {
471     LOG(VERBOSE) << StringPrintf("%s - status %x, consider card present",
472                                __func__, status);
473     status = NFA_STATUS_OK;
474   }
475   if (status == NFA_STATUS_OK) {
476     /* Clear the BUSY flag and restart the presence-check timer */
477     nfa_rw_command_complete();
478   } else {
479     /* If presence check failed just clear the BUSY flag */
480     nfa_rw_cb.flags &= ~NFA_RW_FL_API_BUSY;
481   }
482 
483   /* Handle presence check due to auto-presence-check  */
484   if (nfa_rw_cb.flags & NFA_RW_FL_AUTO_PRESENCE_CHECK_BUSY) {
485     nfa_rw_cb.flags &= ~NFA_RW_FL_AUTO_PRESENCE_CHECK_BUSY;
486 
487     /* If an API was called during auto-presence-check, then handle it now */
488     if (nfa_rw_cb.p_pending_msg) {
489       /* If NFA_RwPresenceCheck was called during auto-presence-check, notify
490        * app of result */
491       if (nfa_rw_cb.p_pending_msg->op_req.op == NFA_RW_OP_PRESENCE_CHECK) {
492         /* Notify app of presence check status */
493         tNFA_CONN_EVT_DATA nfa_conn_evt_data;
494         nfa_conn_evt_data.status = status;
495         nfa_dm_act_conn_cback_notify(NFA_PRESENCE_CHECK_EVT,
496                                      &nfa_conn_evt_data);
497         GKI_freebuf(nfa_rw_cb.p_pending_msg);
498         nfa_rw_cb.p_pending_msg = nullptr;
499       }
500       /* For all other APIs called during auto-presence check, perform the
501          command now (if tag is still present) */
502       else if (status == NFC_STATUS_OK) {
503         LOG(VERBOSE) << StringPrintf(
504             "Performing deferred operation after presence check...");
505         p_pending_msg = (NFC_HDR*)nfa_rw_cb.p_pending_msg;
506         nfa_rw_cb.p_pending_msg = nullptr;
507         nfa_rw_handle_event(p_pending_msg);
508         GKI_freebuf(p_pending_msg);
509       } else {
510         /* Tag no longer present. Free command for pending API command */
511         GKI_freebuf(nfa_rw_cb.p_pending_msg);
512         nfa_rw_cb.p_pending_msg = nullptr;
513       }
514     }
515 
516     /* Auto-presence check failed. Deactivate */
517     if (status != NFC_STATUS_OK) {
518       LOG(VERBOSE) << StringPrintf("Auto presence check failed. Deactivating...");
519       nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
520     }
521   }
522   /* Handle presence check due to NFA_RwPresenceCheck API call */
523   else {
524     /* Notify app of presence check status */
525     tNFA_CONN_EVT_DATA nfa_conn_evt_data;
526     nfa_conn_evt_data.status = status;
527     nfa_dm_act_conn_cback_notify(NFA_PRESENCE_CHECK_EVT, &nfa_conn_evt_data);
528 
529     /* If in normal mode (not-exclusive RF mode) then deactivate the link if
530      * presence check failed */
531     if ((nfa_rw_cb.flags & NFA_RW_FL_NOT_EXCL_RF_MODE) &&
532         (nfa_conn_evt_data.status != NFC_STATUS_OK)) {
533       if (nfa_rw_cb.protocol == NFA_PROTOCOL_ISO_DEP) {
534         rw_t4t_handle_isodep_nak_fallback();
535       }
536     }
537   }
538 }
539 
540 /*******************************************************************************
541 **
542 ** Function         nfa_rw_handle_t1t_evt
543 **
544 ** Description      Handler for Type-1 tag reader/writer events
545 **
546 ** Returns          Nothing
547 **
548 *******************************************************************************/
nfa_rw_handle_t1t_evt(tRW_EVENT event,tRW_DATA * p_rw_data)549 static void nfa_rw_handle_t1t_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
550   tNFA_CONN_EVT_DATA conn_evt_data;
551   tNFA_TAG_PARAMS tag_params;
552   uint8_t* p_rid_rsp;
553   tNFA_STATUS activation_status;
554 
555   conn_evt_data.status = p_rw_data->data.status;
556   switch (event) {
557     case RW_T1T_RID_EVT:
558       if (p_rw_data->data.p_data != nullptr) {
559         /* Assume the data is just the response byte sequence */
560         p_rid_rsp = (uint8_t*)(p_rw_data->data.p_data + 1) +
561                     p_rw_data->data.p_data->offset;
562         /* Fetch HR from RID response message */
563         STREAM_TO_ARRAY(tag_params.t1t.hr, p_rid_rsp, T1T_HR_LEN);
564         /* Fetch UID0-3 from RID response message */
565         STREAM_TO_ARRAY(tag_params.t1t.uid, p_rid_rsp, T1T_CMD_UID_LEN);
566         GKI_freebuf(p_rw_data->data.p_data);
567         p_rw_data->data.p_data = nullptr;
568       }
569 
570       /* Command complete - perform cleanup, notify the app */
571       nfa_rw_command_complete();
572 
573       if (p_rw_data->status == NFC_STATUS_TIMEOUT) {
574         activation_status = NFA_STATUS_TIMEOUT;
575       } else {
576         activation_status = NFA_STATUS_OK;
577       }
578 
579       nfa_dm_notify_activation_status(activation_status, &tag_params);
580       break;
581 
582     case RW_T1T_RALL_CPLT_EVT:
583     case RW_T1T_READ_CPLT_EVT:
584     case RW_T1T_RSEG_CPLT_EVT:
585     case RW_T1T_READ8_CPLT_EVT:
586       nfa_rw_send_data_to_upper(p_rw_data);
587 
588       /* Command complete - perform cleanup, notify the app */
589       nfa_rw_command_complete();
590       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
591       break;
592 
593     case RW_T1T_WRITE_E_CPLT_EVT:
594     case RW_T1T_WRITE_NE_CPLT_EVT:
595     case RW_T1T_WRITE_E8_CPLT_EVT:
596     case RW_T1T_WRITE_NE8_CPLT_EVT:
597       nfa_rw_send_data_to_upper(p_rw_data);
598 
599       /* Command complete - perform cleanup, notify the app */
600       nfa_rw_command_complete();
601       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
602       break;
603 
604     case RW_T1T_TLV_DETECT_EVT:
605       nfa_rw_handle_tlv_detect(p_rw_data);
606       break;
607 
608     case RW_T1T_NDEF_DETECT_EVT:
609       nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_COMPLETE;
610 
611       if ((p_rw_data->status != NFC_STATUS_OK) &&
612           (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) &&
613           (p_rw_data->ndef.flags & NFA_RW_NDEF_FL_FORMATABLE) &&
614           (!(p_rw_data->ndef.flags & NFA_RW_NDEF_FL_FORMATED)) &&
615           (p_rw_data->ndef.flags & NFA_RW_NDEF_FL_SUPPORTED)) {
616         /* Tag is in Initialized state, Format the tag first and then Write NDEF
617          */
618         if (RW_T1tFormatNDef() == NFC_STATUS_OK) break;
619       }
620 
621       nfa_rw_handle_ndef_detect(p_rw_data);
622 
623       break;
624 
625     case RW_T1T_NDEF_READ_EVT:
626       nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_COMPLETE;
627       if (p_rw_data->status == NFC_STATUS_OK) {
628         /* Process the ndef record */
629         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
630                                    nfa_rw_cb.ndef_cur_size);
631       } else {
632         /* Notify app of failure */
633         if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
634           /* If current operation is READ_NDEF, then notify ndef handlers of
635            * failure */
636           nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
637         }
638       }
639 
640       /* Command complete - perform cleanup, notify the app */
641       nfa_rw_command_complete();
642       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
643 
644       /* Free ndef buffer */
645       nfa_rw_free_ndef_rx_buf();
646       break;
647 
648     case RW_T1T_NDEF_WRITE_EVT:
649       if (p_rw_data->data.status != NFA_STATUS_OK)
650         nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
651       nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_COMPLETE;
652 
653       /* Command complete - perform cleanup, notify the app */
654       nfa_rw_command_complete();
655 
656       /* Notify app */
657       conn_evt_data.status = (p_rw_data->data.status == NFC_STATUS_OK)
658                                  ? NFA_STATUS_OK
659                                  : NFA_STATUS_FAILED;
660       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
661         /* Update local cursize of ndef message */
662         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
663       }
664 
665       /* Notify app of ndef write complete status */
666       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
667       break;
668 
669     case RW_T1T_SET_TAG_RO_EVT:
670       /* Command complete - perform cleanup, notify the app */
671       nfa_rw_command_complete();
672       nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
673       break;
674 
675     case RW_T1T_RAW_FRAME_EVT:
676       nfa_rw_send_data_to_upper(p_rw_data);
677       /* Command complete - perform cleanup */
678       nfa_rw_command_complete();
679       break;
680 
681     case RW_T1T_PRESENCE_CHECK_EVT: /* Presence check completed */
682       nfa_rw_handle_presence_check_rsp(p_rw_data->status);
683       break;
684 
685     case RW_T1T_FORMAT_CPLT_EVT:
686 
687       if (p_rw_data->data.status == NFA_STATUS_OK)
688         nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
689 
690       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
691         /* if format operation was done as part of ndef-write operation, now
692          * start NDEF Write */
693         if ((p_rw_data->data.status != NFA_STATUS_OK) ||
694             ((conn_evt_data.status = RW_T1tDetectNDef()) != NFC_STATUS_OK)) {
695           /* Command complete - perform cleanup, notify app */
696           nfa_rw_command_complete();
697           nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_FALSE;
698 
699           /* if format operation failed or ndef detection did not start, then
700            * notify app of ndef-write operation failure */
701           conn_evt_data.status = NFA_STATUS_FAILED;
702           nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
703         }
704       } else {
705         /* Command complete - perform cleanup, notify the app */
706         nfa_rw_command_complete();
707         nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
708       }
709       break;
710 
711     case RW_T1T_INTF_ERROR_EVT:
712       nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
713       break;
714   }
715 }
716 
717 /*******************************************************************************
718 **
719 ** Function         nfa_rw_handle_t2t_evt
720 **
721 ** Description      Handler for Type-2 tag reader/writer events
722 **
723 ** Returns          Nothing
724 **
725 *******************************************************************************/
nfa_rw_handle_t2t_evt(tRW_EVENT event,tRW_DATA * p_rw_data)726 static void nfa_rw_handle_t2t_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
727   tNFA_CONN_EVT_DATA conn_evt_data;
728 
729   conn_evt_data.status = p_rw_data->status;
730 
731   if (p_rw_data->status == NFC_STATUS_REJECTED) {
732     LOG(VERBOSE) << StringPrintf(
733         "; Waking the tag first before handling the "
734         "response!");
735     /* Received NACK. Let DM wakeup the tag first (by putting tag to sleep and
736      * then waking it up) */
737     p_rw_data->status = nfa_dm_disc_sleep_wakeup();
738     if (p_rw_data->status == NFC_STATUS_OK) {
739       nfa_rw_cb.halt_event = event;
740       memcpy(&nfa_rw_cb.rw_data, p_rw_data, sizeof(tRW_DATA));
741       return;
742     }
743   }
744 
745   switch (event) {
746     case RW_T2T_READ_CPLT_EVT: /* Read completed          */
747       nfa_rw_send_data_to_upper(p_rw_data);
748       /* Command complete - perform cleanup, notify the app */
749       nfa_rw_command_complete();
750       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
751       break;
752 
753     case RW_T2T_WRITE_CPLT_EVT: /* Write completed         */
754       /* Command complete - perform cleanup, notify the app */
755       nfa_rw_command_complete();
756       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
757       break;
758 
759     case RW_T2T_SELECT_CPLT_EVT: /* Sector select completed */
760       /* Command complete - perform cleanup, notify the app */
761       nfa_rw_command_complete();
762       nfa_dm_act_conn_cback_notify(NFA_SELECT_CPLT_EVT, &conn_evt_data);
763       break;
764 
765     case RW_T2T_NDEF_DETECT_EVT: /* NDEF detection complete */
766       if ((p_rw_data->status == NFC_STATUS_OK) ||
767           ((p_rw_data->status == NFC_STATUS_FAILED) &&
768            ((p_rw_data->ndef.flags == NFA_RW_NDEF_FL_UNKNOWN) ||
769             (nfa_rw_cb.halt_event == RW_T2T_MAX_EVT))) ||
770           (nfa_rw_cb.skip_dyn_locks == true)) {
771         /* NDEF Detection is complete */
772         nfa_rw_cb.skip_dyn_locks = false;
773         nfa_rw_handle_ndef_detect(p_rw_data);
774       } else {
775         /* Try to detect NDEF again, this time without reading dynamic lock
776          * bytes */
777         nfa_rw_cb.skip_dyn_locks = true;
778         nfa_rw_detect_ndef();
779       }
780       break;
781 
782     case RW_T2T_TLV_DETECT_EVT: /* Lock control/Mem/Prop tlv detection complete
783                                  */
784       nfa_rw_handle_tlv_detect(p_rw_data);
785       break;
786 
787     case RW_T2T_NDEF_READ_EVT: /* NDEF read completed     */
788       if (p_rw_data->status == NFC_STATUS_OK) {
789         /* Process the ndef record */
790         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
791                                    nfa_rw_cb.ndef_cur_size);
792       } else {
793         /* Notify app of failure */
794         if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
795           /* If current operation is READ_NDEF, then notify ndef handlers of
796            * failure */
797           nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
798         }
799       }
800 
801       /* Notify app of read status */
802       conn_evt_data.status = p_rw_data->status;
803       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
804       /* Free ndef buffer */
805       nfa_rw_free_ndef_rx_buf();
806 
807       /* Command complete - perform cleanup */
808       nfa_rw_command_complete();
809       break;
810 
811     case RW_T2T_NDEF_WRITE_EVT: /* NDEF write complete     */
812 
813       /* Command complete - perform cleanup, notify the app */
814       nfa_rw_command_complete();
815 
816       /* Notify app */
817       conn_evt_data.status = (p_rw_data->data.status == NFC_STATUS_OK)
818                                  ? NFA_STATUS_OK
819                                  : NFA_STATUS_FAILED;
820       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
821         /* Update local cursize of ndef message */
822         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
823       }
824 
825       /* Notify app of ndef write complete status */
826       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
827 
828       break;
829 
830     case RW_T2T_SET_TAG_RO_EVT:
831       /* Command complete - perform cleanup, notify the app */
832       nfa_rw_command_complete();
833       nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
834       break;
835 
836     case RW_T2T_RAW_FRAME_EVT:
837       nfa_rw_send_data_to_upper(p_rw_data);
838       /* Command complete - perform cleanup */
839       if (p_rw_data->status != NFC_STATUS_CONTINUE) {
840         nfa_rw_command_complete();
841       }
842       break;
843 
844     case RW_T2T_PRESENCE_CHECK_EVT: /* Presence check completed */
845       nfa_rw_handle_presence_check_rsp(p_rw_data->status);
846       break;
847 
848     case RW_T2T_FORMAT_CPLT_EVT:
849       if (p_rw_data->data.status == NFA_STATUS_OK)
850         nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
851 
852       /* Command complete - perform cleanup, notify the app */
853       nfa_rw_command_complete();
854       nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
855       break;
856 
857     case RW_T2T_INTF_ERROR_EVT:
858       nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
859       break;
860   }
861 }
862 
863 /*******************************************************************************
864 **
865 ** Function         nfa_rw_handle_t3t_evt
866 **
867 ** Description      Handler for Type-3 tag reader/writer events
868 **
869 ** Returns          Nothing
870 **
871 *******************************************************************************/
nfa_rw_handle_t3t_evt(tRW_EVENT event,tRW_DATA * p_rw_data)872 static void nfa_rw_handle_t3t_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
873   tNFA_CONN_EVT_DATA conn_evt_data;
874   tNFA_TAG_PARAMS tag_params;
875 
876   switch (event) {
877     case RW_T3T_NDEF_DETECT_EVT: /* NDEF detection complete */
878       nfa_rw_handle_ndef_detect(p_rw_data);
879       break;
880 
881     case RW_T3T_UPDATE_CPLT_EVT: /* Write completed */
882       /* Command complete - perform cleanup, notify the app */
883       nfa_rw_command_complete();
884 
885       /* Notify app */
886       conn_evt_data.status = (p_rw_data->data.status == NFC_STATUS_OK)
887                                  ? NFA_STATUS_OK
888                                  : NFA_STATUS_FAILED;
889       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
890         /* Update local cursize of ndef message */
891         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
892       }
893 
894       /* Notify app of ndef write complete status */
895       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
896 
897       break;
898 
899     case RW_T3T_CHECK_CPLT_EVT: /* Read completed */
900       if (p_rw_data->status == NFC_STATUS_OK) {
901         /* Process the ndef record */
902         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
903                                    nfa_rw_cb.ndef_cur_size);
904       } else {
905         /* Notify app of failure */
906         if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
907           /* If current operation is READ_NDEF, then notify ndef handlers of
908            * failure */
909           nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
910         }
911       }
912 
913       /* Free ndef buffer */
914       nfa_rw_free_ndef_rx_buf();
915 
916       /* Command complete - perform cleanup, notify the app */
917       nfa_rw_command_complete();
918       conn_evt_data.status = p_rw_data->status;
919       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
920       break;
921 
922     case RW_T3T_CHECK_EVT: /* Segment of data received from type 3 tag */
923       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
924         nfa_rw_store_ndef_rx_buf(p_rw_data);
925       } else {
926         nfa_rw_send_data_to_upper(p_rw_data);
927       }
928       break;
929 
930     case RW_T3T_RAW_FRAME_EVT: /* SendRawFrame response */
931       nfa_rw_send_data_to_upper(p_rw_data);
932 
933       if (p_rw_data->status != NFC_STATUS_CONTINUE) {
934         /* Command complete - perform cleanup */
935         nfa_rw_command_complete();
936       }
937       break;
938 
939     case RW_T3T_PRESENCE_CHECK_EVT: /* Presence check completed */
940       nfa_rw_handle_presence_check_rsp(p_rw_data->status);
941       break;
942 
943     case RW_T3T_GET_SYSTEM_CODES_EVT: /* Presence check completed */
944       /* Command complete - perform cleanup */
945       nfa_rw_command_complete();
946 
947       /* System codes retrieved - notify app of ACTIVATION */
948       if (p_rw_data->status == NFC_STATUS_OK) {
949         tag_params.t3t.num_system_codes = p_rw_data->t3t_sc.num_system_codes;
950         tag_params.t3t.p_system_codes = p_rw_data->t3t_sc.p_system_codes;
951       } else {
952         tag_params.t3t.num_system_codes = 0;
953         tag_params.t3t.p_system_codes = nullptr;
954       }
955 
956       nfa_dm_notify_activation_status(NFA_STATUS_OK, &tag_params);
957       break;
958 
959     case RW_T3T_FORMAT_CPLT_EVT: /* Format completed */
960       /* Command complete - perform cleanup, notify the app */
961       nfa_rw_command_complete();
962 
963       /* Notify app */
964       conn_evt_data.status = (p_rw_data->data.status == NFC_STATUS_OK)
965                                  ? NFA_STATUS_OK
966                                  : NFA_STATUS_FAILED;
967 
968       /* Notify app of ndef write complete status */
969       nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
970       break;
971 
972     case RW_T3T_INTF_ERROR_EVT:
973       LOG(VERBOSE) << StringPrintf("%s; send deactivate", __func__);
974       nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
975       conn_evt_data.status = p_rw_data->status;
976       nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
977       break;
978 
979     case RW_T3T_SET_READ_ONLY_CPLT_EVT:
980       /* Command complete - perform cleanup, notify the app */
981       nfa_rw_command_complete();
982 
983       conn_evt_data.status = p_rw_data->status;
984       nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
985       break;
986 
987     default:
988       LOG(VERBOSE) << StringPrintf("; Unhandled RW event 0x%X", event);
989       break;
990   }
991 }
992 
993 /*******************************************************************************
994 **
995 ** Function         nfa_rw_handle_t4t_evt
996 **
997 ** Description      Handler for Type-4 tag reader/writer events
998 **
999 ** Returns          Nothing
1000 **
1001 *******************************************************************************/
nfa_rw_handle_t4t_evt(tRW_EVENT event,tRW_DATA * p_rw_data)1002 static void nfa_rw_handle_t4t_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
1003   tNFA_CONN_EVT_DATA conn_evt_data;
1004 
1005   switch (event) {
1006     case RW_T4T_NDEF_DETECT_EVT: /* Result of NDEF detection procedure */
1007       nfa_rw_handle_ndef_detect(p_rw_data);
1008       break;
1009 
1010     case RW_T4T_NDEF_FORMAT_CPLT_EVT:
1011       /* Command complete - perform cleanup, notify the app */
1012       nfa_rw_command_complete();
1013       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1014       nfa_rw_cb.ndef_cur_size = p_rw_data->ndef.cur_size;
1015       nfa_rw_cb.ndef_max_size = p_rw_data->ndef.max_size;
1016       conn_evt_data.status = (p_rw_data->status == NFC_STATUS_OK)
1017                                  ? NFA_STATUS_OK
1018                                  : NFA_STATUS_FAILED;
1019 
1020       nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
1021       break;
1022 
1023     case RW_T4T_NDEF_READ_EVT: /* Segment of data received from type 4 tag */
1024       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1025         nfa_rw_store_ndef_rx_buf(p_rw_data);
1026       } else {
1027         nfa_rw_send_data_to_upper(p_rw_data);
1028       }
1029       break;
1030 
1031     case RW_T4T_NDEF_READ_CPLT_EVT: /* Read operation completed           */
1032       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1033         nfa_rw_store_ndef_rx_buf(p_rw_data);
1034 
1035         /* Process the ndef record */
1036         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
1037                                    nfa_rw_cb.ndef_cur_size);
1038 
1039         /* Free ndef buffer */
1040         nfa_rw_free_ndef_rx_buf();
1041       } else {
1042         nfa_rw_send_data_to_upper(p_rw_data);
1043       }
1044 
1045       /* Command complete - perform cleanup, notify the app */
1046       nfa_rw_command_complete();
1047       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1048       conn_evt_data.status = NFC_STATUS_OK;
1049       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1050       break;
1051 
1052     case RW_T4T_NDEF_READ_FAIL_EVT: /* Read operation failed              */
1053       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1054         /* If current operation is READ_NDEF, then notify ndef handlers of
1055          * failure */
1056         nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
1057 
1058         /* Free ndef buffer */
1059         nfa_rw_free_ndef_rx_buf();
1060       }
1061 
1062       /* Command complete - perform cleanup, notify the app */
1063       nfa_rw_command_complete();
1064       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1065       conn_evt_data.status = NFA_STATUS_FAILED;
1066       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1067       break;
1068 
1069     case RW_T4T_NDEF_UPDATE_CPLT_EVT: /* Update operation completed         */
1070     case RW_T4T_NDEF_UPDATE_FAIL_EVT: /* Update operation failed            */
1071 
1072       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
1073         /* Update local cursize of ndef message */
1074         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
1075       }
1076 
1077       /* Notify app */
1078       if (event == RW_T4T_NDEF_UPDATE_CPLT_EVT)
1079         conn_evt_data.status = NFA_STATUS_OK;
1080       else
1081         conn_evt_data.status = NFA_STATUS_FAILED;
1082 
1083       /* Command complete - perform cleanup, notify the app */
1084       nfa_rw_command_complete();
1085       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1086       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
1087       break;
1088 
1089     case RW_T4T_RAW_FRAME_EVT: /* Raw Frame data event         */
1090       nfa_rw_send_data_to_upper(p_rw_data);
1091 
1092       if (p_rw_data->status != NFC_STATUS_CONTINUE) {
1093         /* Command complete - perform cleanup */
1094         nfa_rw_command_complete();
1095         nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1096       }
1097       break;
1098 
1099     case RW_T4T_SET_TO_RO_EVT: /* Tag is set as read only          */
1100       conn_evt_data.status = p_rw_data->status;
1101       nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
1102 
1103       nfa_rw_command_complete();
1104       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1105       break;
1106 
1107     case RW_T4T_INTF_ERROR_EVT: /* RF Interface error event         */
1108       conn_evt_data.status = p_rw_data->status;
1109       nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
1110 
1111       nfa_rw_command_complete();
1112       nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1113       break;
1114 
1115     case RW_T4T_PRESENCE_CHECK_EVT: /* Presence check completed */
1116       nfa_rw_handle_presence_check_rsp(p_rw_data->status);
1117       break;
1118 
1119     default:
1120       LOG(VERBOSE) << StringPrintf("; Unhandled RW event 0x%X", event);
1121       break;
1122   }
1123 }
1124 
1125 /*******************************************************************************
1126 **
1127 ** Function         nfa_rw_handle_i93_evt
1128 **
1129 ** Description      Handler for ISO 15693 tag reader/writer events
1130 **
1131 ** Returns          Nothing
1132 **
1133 *******************************************************************************/
nfa_rw_handle_i93_evt(tRW_EVENT event,tRW_DATA * p_rw_data)1134 static void nfa_rw_handle_i93_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
1135   tNFA_CONN_EVT_DATA conn_evt_data;
1136   tNFA_TAG_PARAMS i93_params;
1137 
1138   switch (event) {
1139     case RW_I93_NDEF_DETECT_EVT: /* Result of NDEF detection procedure */
1140       nfa_rw_handle_ndef_detect(p_rw_data);
1141       break;
1142 
1143     case RW_I93_NDEF_READ_EVT: /* Segment of data received from type 4 tag */
1144       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1145         nfa_rw_store_ndef_rx_buf(p_rw_data);
1146       } else {
1147         nfa_rw_send_data_to_upper(p_rw_data);
1148       }
1149       break;
1150 
1151     case RW_I93_NDEF_READ_CPLT_EVT: /* Read operation completed           */
1152       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1153         nfa_rw_store_ndef_rx_buf(p_rw_data);
1154 
1155         /* Process the ndef record */
1156         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
1157                                    nfa_rw_cb.ndef_cur_size);
1158 
1159         /* Free ndef buffer */
1160         nfa_rw_free_ndef_rx_buf();
1161       } else {
1162         nfa_rw_send_data_to_upper(p_rw_data);
1163       }
1164 
1165       /* Command complete - perform cleanup, notify app */
1166       nfa_rw_command_complete();
1167       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1168       conn_evt_data.status = NFC_STATUS_OK;
1169       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1170       break;
1171 
1172     case RW_I93_NDEF_READ_FAIL_EVT: /* Read operation failed              */
1173       if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1174         /* If current operation is READ_NDEF, then notify ndef handlers of
1175          * failure */
1176         nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, nullptr, 0);
1177 
1178         /* Free ndef buffer */
1179         nfa_rw_free_ndef_rx_buf();
1180       }
1181 
1182       /* Command complete - perform cleanup, notify app */
1183       nfa_rw_command_complete();
1184       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1185       conn_evt_data.status = NFA_STATUS_FAILED;
1186       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1187       break;
1188 
1189     case RW_I93_NDEF_UPDATE_CPLT_EVT: /* Update operation completed         */
1190     case RW_I93_NDEF_UPDATE_FAIL_EVT: /* Update operation failed            */
1191 
1192       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
1193         /* Update local cursize of ndef message */
1194         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
1195       }
1196 
1197       /* Command complete - perform cleanup, notify app */
1198       nfa_rw_command_complete();
1199       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1200 
1201       if (event == RW_I93_NDEF_UPDATE_CPLT_EVT)
1202         conn_evt_data.status = NFA_STATUS_OK;
1203       else
1204         conn_evt_data.status = NFA_STATUS_FAILED;
1205 
1206       /* Notify app of ndef write complete status */
1207       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
1208       break;
1209 
1210     case RW_I93_RAW_FRAME_EVT: /* Raw Frame data event         */
1211       nfa_rw_send_data_to_upper(p_rw_data);
1212       if (p_rw_data->status != NFC_STATUS_CONTINUE) {
1213         /* Command complete - perform cleanup */
1214         nfa_rw_command_complete();
1215       }
1216       break;
1217 
1218     case RW_I93_INTF_ERROR_EVT: /* RF Interface error event         */
1219       /* Command complete - perform cleanup, notify app */
1220       nfa_rw_command_complete();
1221 
1222       if (nfa_rw_cb.flags & NFA_RW_FL_ACTIVATION_NTF_PENDING) {
1223         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
1224 
1225         memset(&i93_params, 0x00, sizeof(tNFA_TAG_PARAMS));
1226         memcpy(i93_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
1227 
1228         nfa_dm_notify_activation_status(NFA_STATUS_OK, &i93_params);
1229       } else {
1230         conn_evt_data.status = p_rw_data->status;
1231         nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
1232       }
1233 
1234       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1235       break;
1236 
1237     case RW_I93_PRESENCE_CHECK_EVT: /* Presence check completed */
1238       nfa_rw_handle_presence_check_rsp(p_rw_data->status);
1239       break;
1240 
1241     case RW_I93_FORMAT_CPLT_EVT: /* Format procedure complete          */
1242       if (p_rw_data->data.status == NFA_STATUS_OK)
1243         nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
1244 
1245       /* Command complete - perform cleanup, notify app */
1246       nfa_rw_command_complete();
1247       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1248       conn_evt_data.status = p_rw_data->status;
1249       nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
1250       break;
1251 
1252     case RW_I93_SET_TAG_RO_EVT: /* Set read-only procedure complete   */
1253       nfa_rw_cb.flags |= NFA_RW_FL_TAG_IS_READONLY;
1254 
1255       /* Command complete - perform cleanup, notify app */
1256       nfa_rw_command_complete();
1257       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1258       conn_evt_data.status = p_rw_data->status;
1259       nfa_dm_act_conn_cback_notify(NFA_SET_TAG_RO_EVT, &conn_evt_data);
1260       break;
1261 
1262     case RW_I93_INVENTORY_EVT: /* Response of Inventory              */
1263 
1264       /* Command complete - perform cleanup, notify app */
1265       nfa_rw_command_complete();
1266 
1267       conn_evt_data.i93_cmd_cplt.status = p_rw_data->i93_inventory.status;
1268       conn_evt_data.i93_cmd_cplt.sent_command = I93_CMD_INVENTORY;
1269 
1270       conn_evt_data.i93_cmd_cplt.params.inventory.dsfid =
1271           p_rw_data->i93_inventory.dsfid;
1272       memcpy(conn_evt_data.i93_cmd_cplt.params.inventory.uid,
1273              p_rw_data->i93_inventory.uid, I93_UID_BYTE_LEN);
1274 
1275       nfa_dm_act_conn_cback_notify(NFA_I93_CMD_CPLT_EVT, &conn_evt_data);
1276 
1277       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1278       break;
1279 
1280     case RW_I93_DATA_EVT: /* Response of Read, Get Multi Security */
1281 
1282       /* Command complete - perform cleanup, notify app */
1283       nfa_rw_command_complete();
1284 
1285       conn_evt_data.data.p_data = (uint8_t*)(p_rw_data->i93_data.p_data + 1) +
1286                                   p_rw_data->i93_data.p_data->offset;
1287 
1288       if (nfa_rw_cb.flags & NFA_RW_FL_ACTIVATION_NTF_PENDING) {
1289         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
1290 
1291         i93_params.i93.info_flags =
1292             (I93_INFO_FLAG_DSFID | I93_INFO_FLAG_MEM_SIZE | I93_INFO_FLAG_AFI);
1293         i93_params.i93.afi =
1294             *(conn_evt_data.data.p_data +
1295               nfa_rw_cb.i93_afi_location % nfa_rw_cb.i93_block_size);
1296         i93_params.i93.dsfid = nfa_rw_cb.i93_dsfid;
1297         i93_params.i93.block_size = nfa_rw_cb.i93_block_size;
1298         i93_params.i93.num_block = nfa_rw_cb.i93_num_block;
1299         memcpy(i93_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
1300 
1301         nfa_dm_notify_activation_status(NFA_STATUS_OK, &i93_params);
1302       } else {
1303         conn_evt_data.data.len = p_rw_data->i93_data.p_data->len;
1304 
1305         nfa_dm_act_conn_cback_notify(NFA_DATA_EVT, &conn_evt_data);
1306       }
1307 
1308       GKI_freebuf(p_rw_data->i93_data.p_data);
1309       p_rw_data->i93_data.p_data = nullptr;
1310 
1311       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1312       break;
1313 
1314     case RW_I93_SYS_INFO_EVT: /* Response of System Information     */
1315 
1316       /* Command complete - perform cleanup, notify app */
1317       nfa_rw_command_complete();
1318 
1319       if (nfa_rw_cb.flags & NFA_RW_FL_ACTIVATION_NTF_PENDING) {
1320         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
1321 
1322         nfa_rw_cb.i93_block_size = p_rw_data->i93_sys_info.block_size;
1323         nfa_rw_cb.i93_num_block = p_rw_data->i93_sys_info.num_block;
1324 
1325         i93_params.i93.info_flags = p_rw_data->i93_sys_info.info_flags;
1326         i93_params.i93.dsfid = p_rw_data->i93_sys_info.dsfid;
1327         i93_params.i93.afi = p_rw_data->i93_sys_info.afi;
1328         i93_params.i93.num_block = p_rw_data->i93_sys_info.num_block;
1329         i93_params.i93.block_size = p_rw_data->i93_sys_info.block_size;
1330         i93_params.i93.IC_reference = p_rw_data->i93_sys_info.IC_reference;
1331         memcpy(i93_params.i93.uid, p_rw_data->i93_sys_info.uid,
1332                I93_UID_BYTE_LEN);
1333 
1334         nfa_dm_notify_activation_status(NFA_STATUS_OK, &i93_params);
1335       } else {
1336         conn_evt_data.i93_cmd_cplt.status = p_rw_data->i93_sys_info.status;
1337         conn_evt_data.i93_cmd_cplt.sent_command = I93_CMD_GET_SYS_INFO;
1338 
1339         conn_evt_data.i93_cmd_cplt.params.sys_info.info_flags =
1340             p_rw_data->i93_sys_info.info_flags;
1341         memcpy(conn_evt_data.i93_cmd_cplt.params.sys_info.uid,
1342                p_rw_data->i93_sys_info.uid, I93_UID_BYTE_LEN);
1343         conn_evt_data.i93_cmd_cplt.params.sys_info.dsfid =
1344             p_rw_data->i93_sys_info.dsfid;
1345         conn_evt_data.i93_cmd_cplt.params.sys_info.afi =
1346             p_rw_data->i93_sys_info.afi;
1347         conn_evt_data.i93_cmd_cplt.params.sys_info.num_block =
1348             p_rw_data->i93_sys_info.num_block;
1349         conn_evt_data.i93_cmd_cplt.params.sys_info.block_size =
1350             p_rw_data->i93_sys_info.block_size;
1351         conn_evt_data.i93_cmd_cplt.params.sys_info.IC_reference =
1352             p_rw_data->i93_sys_info.IC_reference;
1353 
1354         /* store tag memory information for writing blocks */
1355         nfa_rw_cb.i93_block_size = p_rw_data->i93_sys_info.block_size;
1356         nfa_rw_cb.i93_num_block = p_rw_data->i93_sys_info.num_block;
1357 
1358         nfa_dm_act_conn_cback_notify(NFA_I93_CMD_CPLT_EVT, &conn_evt_data);
1359       }
1360 
1361       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1362       break;
1363 
1364     case RW_I93_CMD_CMPL_EVT: /* Command complete                   */
1365       /* Command complete - perform cleanup, notify app */
1366       nfa_rw_command_complete();
1367 
1368       if (nfa_rw_cb.flags & NFA_RW_FL_ACTIVATION_NTF_PENDING) {
1369         /* Reader got error code from tag */
1370 
1371         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
1372 
1373         memset(&i93_params, 0x00, sizeof(i93_params));
1374         memcpy(i93_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
1375 
1376         nfa_dm_notify_activation_status(NFA_STATUS_OK, &i93_params);
1377       } else {
1378         conn_evt_data.i93_cmd_cplt.status = p_rw_data->i93_cmd_cmpl.status;
1379         conn_evt_data.i93_cmd_cplt.sent_command =
1380             p_rw_data->i93_cmd_cmpl.command;
1381 
1382         if (conn_evt_data.i93_cmd_cplt.status != NFC_STATUS_OK)
1383           conn_evt_data.i93_cmd_cplt.params.error_code =
1384               p_rw_data->i93_cmd_cmpl.error_code;
1385 
1386         nfa_dm_act_conn_cback_notify(NFA_I93_CMD_CPLT_EVT, &conn_evt_data);
1387       }
1388 
1389       nfa_rw_cb.cur_op = NFA_RW_OP_MAX; /* clear current operation */
1390       break;
1391 
1392     default:
1393       LOG(VERBOSE) << StringPrintf("; Unhandled RW event 0x%X", event);
1394       break;
1395   }
1396 }
1397 
1398 /*******************************************************************************
1399 **
1400 ** Function         nfa_rw_handle_mfc_evt
1401 **
1402 ** Description      Handler for Mifare Classic tag reader/writer events
1403 **
1404 ** Returns          Nothing
1405 **
1406 *******************************************************************************/
nfa_rw_handle_mfc_evt(tRW_EVENT event,tRW_DATA * p_rw_data)1407 static void nfa_rw_handle_mfc_evt(tRW_EVENT event, tRW_DATA* p_rw_data) {
1408   tNFA_CONN_EVT_DATA conn_evt_data;
1409 
1410   conn_evt_data.status = p_rw_data->status;
1411   LOG(VERBOSE) << StringPrintf("nfa_rw_handle_mfc_evt() event = 0x%X", event);
1412 
1413   switch (event) {
1414     /* Read completed */
1415     case RW_MFC_NDEF_READ_CPLT_EVT:
1416       nfa_rw_send_data_to_upper(p_rw_data);
1417       /* Command complete - perform cleanup, notify the app */
1418       nfa_rw_command_complete();
1419       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1420       break;
1421 
1422     /* NDEF detection complete */
1423     case RW_MFC_NDEF_DETECT_EVT:
1424       nfa_rw_handle_ndef_detect(p_rw_data);
1425       break;
1426 
1427     /* NDEF read completed */
1428     case RW_MFC_NDEF_READ_EVT:
1429       if (p_rw_data->status == NFC_STATUS_OK) {
1430         /* Process the ndef record */
1431         nfa_dm_ndef_handle_message(NFA_STATUS_OK, nfa_rw_cb.p_ndef_buf,
1432                                    nfa_rw_cb.ndef_cur_size);
1433       } else {
1434         /* Notify app of failure */
1435         if (nfa_rw_cb.cur_op == NFA_RW_OP_READ_NDEF) {
1436           /* If current operation is READ_NDEF, then notify ndef handlers of
1437            * failure */
1438           nfa_dm_ndef_handle_message(NFA_STATUS_FAILED, NULL, 0);
1439         }
1440       }
1441 
1442       /* Notify app of read status */
1443       conn_evt_data.status = p_rw_data->status;
1444       nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1445       /* Free ndef buffer */
1446       nfa_rw_free_ndef_rx_buf();
1447 
1448       /* Command complete - perform cleanup */
1449       nfa_rw_command_complete();
1450       break;
1451 
1452     /* Raw Frame data event */
1453     case RW_MFC_RAW_FRAME_EVT:
1454       nfa_rw_send_data_to_upper(p_rw_data);
1455 
1456       if (p_rw_data->status != NFC_STATUS_CONTINUE) {
1457         /* Command complete - perform cleanup */
1458         nfa_rw_command_complete();
1459         nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
1460       }
1461       break;
1462 
1463     /* RF Interface error event */
1464     case RW_MFC_INTF_ERROR_EVT:
1465       nfa_dm_act_conn_cback_notify(NFA_RW_INTF_ERROR_EVT, &conn_evt_data);
1466       break;
1467 
1468     case RW_MFC_NDEF_FORMAT_CPLT_EVT:
1469       /* Command complete - perform cleanup, notify the app */
1470       nfa_rw_command_complete();
1471       nfa_dm_act_conn_cback_notify(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
1472       break;
1473 
1474     /* NDEF write completed or failed*/
1475     case RW_MFC_NDEF_WRITE_CPLT_EVT:
1476       if (nfa_rw_cb.cur_op == NFA_RW_OP_WRITE_NDEF) {
1477         /* Update local cursize of ndef message */
1478         nfa_rw_cb.ndef_cur_size = nfa_rw_cb.ndef_wr_len;
1479       }
1480       FALLTHROUGH_INTENDED;
1481 
1482     case RW_MFC_NDEF_WRITE_FAIL_EVT:
1483       /* Command complete - perform cleanup, notify the app */
1484       nfa_rw_command_complete();
1485       nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
1486       break;
1487 
1488     default:
1489       LOG(VERBOSE) << StringPrintf("; Unhandled RW event 0x%X", event);
1490   }
1491 }
1492 
1493 /*******************************************************************************
1494 **
1495 ** Function         nfa_rw_cback
1496 **
1497 ** Description      Callback for reader/writer event notification
1498 **
1499 ** Returns          Nothing
1500 **
1501 *******************************************************************************/
nfa_rw_cback(tRW_EVENT event,tRW_DATA * p_rw_data)1502 static void nfa_rw_cback(tRW_EVENT event, tRW_DATA* p_rw_data) {
1503   LOG(VERBOSE) << StringPrintf("nfa_rw_cback: event=0x%02x", event);
1504 
1505   /* Call appropriate event handler for tag type */
1506   if (event < RW_T1T_MAX_EVT) {
1507     /* Handle Type-1 tag events */
1508     nfa_rw_handle_t1t_evt(event, p_rw_data);
1509   } else if (event < RW_T2T_MAX_EVT) {
1510     /* Handle Type-2 tag events */
1511     nfa_rw_handle_t2t_evt(event, p_rw_data);
1512   } else if (event < RW_T3T_MAX_EVT) {
1513     /* Handle Type-3 tag events */
1514     nfa_rw_handle_t3t_evt(event, p_rw_data);
1515   } else if (event < RW_T4T_MAX_EVT) {
1516     /* Handle Type-4 tag events */
1517     nfa_rw_handle_t4t_evt(event, p_rw_data);
1518   } else if (event < RW_I93_MAX_EVT) {
1519     /* Handle ISO 15693 tag events */
1520     nfa_rw_handle_i93_evt(event, p_rw_data);
1521   } else if (event < RW_MFC_MAX_EVT) {
1522     /* Handle Mifare Classic tag events */
1523     nfa_rw_handle_mfc_evt(event, p_rw_data);
1524   } else {
1525     LOG(ERROR) << StringPrintf("nfa_rw_cback: unhandled event=0x%02x", event);
1526   }
1527 }
1528 
1529 /*******************************************************************************
1530 **
1531 ** Function         nfa_rw_start_ndef_detection
1532 **
1533 ** Description      Start NDEF detection on activated tag
1534 **
1535 ** Returns          Nothing
1536 **
1537 *******************************************************************************/
nfa_rw_start_ndef_detection(void)1538 static tNFC_STATUS nfa_rw_start_ndef_detection(void) {
1539   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
1540   tNFC_STATUS status = NFC_STATUS_FAILED;
1541 
1542   if (NFC_PROTOCOL_T1T == protocol) {
1543     /* Type1Tag    - NFC-A */
1544     status = RW_T1tDetectNDef();
1545   } else if (NFC_PROTOCOL_T2T == protocol) {
1546     /* Type2Tag    - NFC-A */
1547     if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
1548       status = RW_T2tDetectNDef(nfa_rw_cb.skip_dyn_locks);
1549     }
1550   } else if (NFC_PROTOCOL_T3T == protocol) {
1551     /* Type3Tag    - NFC-F */
1552     status = RW_T3tDetectNDef();
1553   } else if (NFC_PROTOCOL_ISO_DEP == protocol) {
1554     /* ISODEP/4A,4B- NFC-A or NFC-B */
1555     status = RW_T4tDetectNDef();
1556   } else if (NFC_PROTOCOL_T5T == protocol) {
1557     /* ISO 15693 */
1558     status = RW_I93DetectNDef();
1559   } else if (NFC_PROTOCOL_MIFARE == protocol) {
1560     status = RW_MfcDetectNDef();
1561   }
1562 
1563   return (status);
1564 }
1565 
1566 /*******************************************************************************
1567 **
1568 ** Function         nfa_rw_start_ndef_read
1569 **
1570 ** Description      Start NDEF read on activated tag
1571 **
1572 ** Returns          Nothing
1573 **
1574 *******************************************************************************/
nfa_rw_start_ndef_read(void)1575 static tNFC_STATUS nfa_rw_start_ndef_read(void) {
1576   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
1577   tNFC_STATUS status = NFC_STATUS_FAILED;
1578   tNFA_CONN_EVT_DATA conn_evt_data;
1579 
1580   /* Handle zero length NDEF message */
1581   if (nfa_rw_cb.ndef_cur_size == 0) {
1582     LOG(VERBOSE) << StringPrintf("NDEF message is zero-length");
1583 
1584     /* Send zero-lengh NDEF message to ndef callback */
1585     nfa_dm_ndef_handle_message(NFA_STATUS_OK, nullptr, 0);
1586 
1587     /* Command complete - perform cleanup, notify app */
1588     nfa_rw_command_complete();
1589     conn_evt_data.status = NFA_STATUS_OK;
1590     nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1591     return NFC_STATUS_OK;
1592   }
1593 
1594   /* Allocate buffer for incoming NDEF message (free previous NDEF rx buffer, if
1595    * needed) */
1596   nfa_rw_free_ndef_rx_buf();
1597   nfa_rw_cb.p_ndef_buf = (uint8_t*)nfa_mem_co_alloc(nfa_rw_cb.ndef_cur_size);
1598   if (nfa_rw_cb.p_ndef_buf == nullptr) {
1599     LOG(ERROR) << StringPrintf(
1600         "Unable to allocate a buffer for reading NDEF (size=%i)",
1601         nfa_rw_cb.ndef_cur_size);
1602 
1603     /* Command complete - perform cleanup, notify app */
1604     nfa_rw_command_complete();
1605     conn_evt_data.status = NFA_STATUS_FAILED;
1606     nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1607     return NFC_STATUS_FAILED;
1608   }
1609   nfa_rw_cb.ndef_rd_offset = 0;
1610 
1611   if (NFC_PROTOCOL_T1T == protocol) {
1612     /* Type1Tag    - NFC-A */
1613     status =
1614         RW_T1tReadNDef(nfa_rw_cb.p_ndef_buf, (uint16_t)nfa_rw_cb.ndef_cur_size);
1615   } else if (NFC_PROTOCOL_T2T == protocol) {
1616     /* Type2Tag    - NFC-A */
1617     if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
1618       status = RW_T2tReadNDef(nfa_rw_cb.p_ndef_buf,
1619                               (uint16_t)nfa_rw_cb.ndef_cur_size);
1620     }
1621   } else if (NFC_PROTOCOL_T3T == protocol) {
1622     /* Type3Tag    - NFC-F */
1623     status = RW_T3tCheckNDef();
1624   } else if (NFC_PROTOCOL_ISO_DEP == protocol) {
1625     /* ISODEP/4A,4B- NFC-A or NFC-B */
1626     status = RW_T4tReadNDef();
1627   } else if (NFC_PROTOCOL_T5T == protocol) {
1628     /* ISO 15693 */
1629     status = RW_I93ReadNDef();
1630   } else if (NFC_PROTOCOL_MIFARE == protocol) {
1631     /* Mifare Classic*/
1632     status =
1633         RW_MfcReadNDef(nfa_rw_cb.p_ndef_buf, (uint16_t)nfa_rw_cb.ndef_cur_size);
1634   }
1635 
1636   return (status);
1637 }
1638 
1639 /*******************************************************************************
1640 **
1641 ** Function         nfa_rw_detect_ndef
1642 **
1643 ** Description      Handler for NFA_RW_API_DETECT_NDEF_EVT
1644 **
1645 ** Returns          TRUE (message buffer to be freed by caller)
1646 **
1647 *******************************************************************************/
nfa_rw_detect_ndef()1648 static bool nfa_rw_detect_ndef() {
1649   tNFA_CONN_EVT_DATA conn_evt_data;
1650   LOG(VERBOSE) << __func__;
1651 
1652   conn_evt_data.ndef_detect.status = nfa_rw_start_ndef_detection();
1653   if (conn_evt_data.ndef_detect.status != NFC_STATUS_OK) {
1654     /* Command complete - perform cleanup, notify app */
1655     nfa_rw_command_complete();
1656     conn_evt_data.ndef_detect.cur_size = 0;
1657     conn_evt_data.ndef_detect.max_size = 0;
1658     conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
1659     nfa_dm_act_conn_cback_notify(NFA_NDEF_DETECT_EVT, &conn_evt_data);
1660   }
1661 
1662   return true;
1663 }
1664 
1665 /*******************************************************************************
1666 **
1667 ** Function         nfa_rw_start_ndef_write
1668 **
1669 ** Description      Start NDEF write on activated tag
1670 **
1671 ** Returns          Nothing
1672 **
1673 *******************************************************************************/
nfa_rw_start_ndef_write(void)1674 static tNFC_STATUS nfa_rw_start_ndef_write(void) {
1675   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
1676   tNFC_STATUS status = NFC_STATUS_FAILED;
1677 
1678   if (nfa_rw_cb.flags & NFA_RW_FL_TAG_IS_READONLY) {
1679     /* error: ndef tag is read-only */
1680     status = NFC_STATUS_FAILED;
1681     LOG(ERROR) << StringPrintf("Unable to write NDEF. Tag is read-only");
1682   } else if (nfa_rw_cb.ndef_max_size < nfa_rw_cb.ndef_wr_len) {
1683     /* error: ndef tag size is too small */
1684     status = NFC_STATUS_BUFFER_FULL;
1685     LOG(ERROR) << StringPrintf(
1686         "Unable to write NDEF. Tag maxsize=%i, request write size=%i",
1687         nfa_rw_cb.ndef_max_size, nfa_rw_cb.ndef_wr_len);
1688   } else {
1689     if (NFC_PROTOCOL_T1T == protocol) {
1690       /* Type1Tag    - NFC-A */
1691       status = RW_T1tWriteNDef((uint16_t)nfa_rw_cb.ndef_wr_len,
1692                                nfa_rw_cb.p_ndef_wr_buf);
1693     } else if (NFC_PROTOCOL_T2T == protocol) {
1694       /* Type2Tag    - NFC-A */
1695       if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
1696         status = RW_T2tWriteNDef((uint16_t)nfa_rw_cb.ndef_wr_len,
1697                                  nfa_rw_cb.p_ndef_wr_buf);
1698       }
1699     } else if (NFC_PROTOCOL_T3T == protocol) {
1700       /* Type3Tag    - NFC-F */
1701       status = RW_T3tUpdateNDef(nfa_rw_cb.ndef_wr_len, nfa_rw_cb.p_ndef_wr_buf);
1702     } else if (NFC_PROTOCOL_ISO_DEP == protocol) {
1703       /* ISODEP/4A,4B- NFC-A or NFC-B */
1704       status = RW_T4tUpdateNDef(nfa_rw_cb.ndef_wr_len, nfa_rw_cb.p_ndef_wr_buf);
1705     } else if (NFC_PROTOCOL_T5T == protocol) {
1706       /* ISO 15693 */
1707       status = RW_I93UpdateNDef((uint16_t)nfa_rw_cb.ndef_wr_len,
1708                                 nfa_rw_cb.p_ndef_wr_buf);
1709     } else if (NFC_PROTOCOL_MIFARE == protocol) {
1710       /* Mifare Tag */
1711       status = RW_MfcWriteNDef((uint16_t)nfa_rw_cb.ndef_wr_len,
1712                                nfa_rw_cb.p_ndef_wr_buf);
1713     }
1714   }
1715 
1716   return (status);
1717 }
1718 
1719 /*******************************************************************************
1720 **
1721 ** Function         nfa_rw_read_ndef
1722 **
1723 ** Description      Handler for NFA_RW_API_READ_NDEF_EVT
1724 **
1725 ** Returns          TRUE (message buffer to be freed by caller)
1726 **
1727 *******************************************************************************/
nfa_rw_read_ndef()1728 static bool nfa_rw_read_ndef() {
1729   tNFA_STATUS status = NFA_STATUS_OK;
1730   tNFA_CONN_EVT_DATA conn_evt_data;
1731 
1732   LOG(VERBOSE) << __func__;
1733 
1734   /* Check if ndef detection has been performed yet */
1735   if (nfa_rw_cb.ndef_st == NFA_RW_NDEF_ST_UNKNOWN) {
1736     /* Perform ndef detection first */
1737     status = nfa_rw_start_ndef_detection();
1738   } else if (nfa_rw_cb.ndef_st == NFA_RW_NDEF_ST_FALSE) {
1739     /* Tag is not NDEF */
1740     status = NFA_STATUS_FAILED;
1741   } else {
1742     /* Perform the NDEF read operation */
1743     status = nfa_rw_start_ndef_read();
1744   }
1745 
1746   /* Handle failure */
1747   if (status != NFA_STATUS_OK) {
1748     /* Command complete - perform cleanup, notify app */
1749     nfa_rw_command_complete();
1750     conn_evt_data.status = status;
1751     nfa_dm_act_conn_cback_notify(NFA_READ_CPLT_EVT, &conn_evt_data);
1752   }
1753 
1754   return true;
1755 }
1756 
1757 /*******************************************************************************
1758 **
1759 ** Function         nfa_rw_write_ndef
1760 **
1761 ** Description      Handler for NFA_RW_API_WRITE_NDEF_EVT
1762 **
1763 ** Returns          TRUE (message buffer to be freed by caller)
1764 **
1765 *******************************************************************************/
nfa_rw_write_ndef(tNFA_RW_MSG * p_data)1766 static bool nfa_rw_write_ndef(tNFA_RW_MSG* p_data) {
1767   tNDEF_STATUS ndef_status;
1768   tNFA_STATUS write_status = NFA_STATUS_OK;
1769   tNFA_CONN_EVT_DATA conn_evt_data;
1770   LOG(VERBOSE) << __func__;
1771 
1772   /* Validate NDEF message */
1773   ndef_status = NDEF_MsgValidate(p_data->op_req.params.write_ndef.p_data,
1774                                  p_data->op_req.params.write_ndef.len, false);
1775   if (ndef_status != NDEF_OK) {
1776     LOG(ERROR) << StringPrintf(
1777         "Invalid NDEF message. NDEF_MsgValidate returned %i", ndef_status);
1778 
1779     /* Command complete - perform cleanup, notify app */
1780     nfa_rw_command_complete();
1781     conn_evt_data.status = NFA_STATUS_FAILED;
1782     nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
1783     return true;
1784   }
1785 
1786   /* Store pointer to source NDEF */
1787   nfa_rw_cb.p_ndef_wr_buf = p_data->op_req.params.write_ndef.p_data;
1788   nfa_rw_cb.ndef_wr_len = p_data->op_req.params.write_ndef.len;
1789 
1790   /* Check if ndef detection has been performed yet */
1791   if (nfa_rw_cb.ndef_st == NFA_RW_NDEF_ST_UNKNOWN) {
1792     /* Perform ndef detection first */
1793     write_status = nfa_rw_start_ndef_detection();
1794   } else if (nfa_rw_cb.ndef_st == NFA_RW_NDEF_ST_FALSE) {
1795     if (nfa_rw_cb.protocol == NFC_PROTOCOL_T1T) {
1796       /* For Type 1 tag, NDEF can be written on Initialized tag
1797        *  Perform ndef detection first to check if tag is in Initialized state
1798        * to Write NDEF */
1799       write_status = nfa_rw_start_ndef_detection();
1800     } else {
1801       /* Tag is not NDEF */
1802       write_status = NFA_STATUS_FAILED;
1803     }
1804   } else {
1805     /* Perform the NDEF read operation */
1806     write_status = nfa_rw_start_ndef_write();
1807   }
1808 
1809   /* Handle failure */
1810   if (write_status != NFA_STATUS_OK) {
1811     /* Command complete - perform cleanup, notify app */
1812     nfa_rw_command_complete();
1813     conn_evt_data.status = write_status;
1814     nfa_dm_act_conn_cback_notify(NFA_WRITE_CPLT_EVT, &conn_evt_data);
1815   }
1816 
1817   return true;
1818 }
1819 
1820 /*******************************************************************************
1821 **
1822 ** Function         nfa_rw_presence_check
1823 **
1824 ** Description      Handler for NFA_RW_API_PRESENCE_CHECK
1825 **
1826 ** Returns          Nothing
1827 **
1828 *******************************************************************************/
nfa_rw_presence_check(tNFA_RW_MSG * p_data)1829 void nfa_rw_presence_check(tNFA_RW_MSG* p_data) {
1830   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
1831   uint8_t sel_res = nfa_rw_cb.pa_sel_res;
1832   tNFC_STATUS status = NFC_STATUS_FAILED;
1833   bool unsupported = false;
1834   uint8_t option = NFA_RW_OPTION_INVALID;
1835   tNFA_RW_PRES_CHK_OPTION op_param = NFA_RW_PRES_CHK_DEFAULT;
1836 
1837   if (NFC_PROTOCOL_T1T == protocol) {
1838     /* Type1Tag    - NFC-A */
1839     status = RW_T1tPresenceCheck();
1840   } else if (NFC_PROTOCOL_T2T == protocol) {
1841     /* If T2T NFC-Forum, then let RW handle presence check */
1842     if (sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
1843       /* Type 2 tag have not sent NACK after activation */
1844       status = RW_T2tPresenceCheck();
1845     } else {
1846       /* Will fall back to deactivate/reactivate */
1847       unsupported = true;
1848     }
1849   } else if (NFC_PROTOCOL_T3T == protocol) {
1850     /* Type3Tag    - NFC-F */
1851     status = RW_T3tPresenceCheck();
1852   } else if (NFC_PROTOCOL_ISO_DEP == protocol) {
1853     /* ISODEP/4A,4B- NFC-A or NFC-B */
1854     if (p_data) {
1855       op_param = p_data->op_req.params.option;
1856     }
1857 
1858     switch (op_param) {
1859       case NFA_RW_PRES_CHK_I_BLOCK:
1860         option = RW_T4T_CHK_EMPTY_I_BLOCK;
1861         break;
1862 
1863       case NFA_RW_PRES_CHK_ISO_DEP_NAK:
1864         if (NFC_GetNCIVersion() >= NCI_VERSION_2_0) {
1865           option = RW_T4T_CHK_ISO_DEP_NAK_PRES_CHK;
1866         }
1867         break;
1868       default:
1869         /* empty I block */
1870         option = RW_T4T_CHK_EMPTY_I_BLOCK;
1871     }
1872 
1873     if (option != NFA_RW_OPTION_INVALID) {
1874       /* use the presence check with the chosen option */
1875       status = RW_T4tPresenceCheck(option);
1876     } else {
1877       /* use sleep/wake for presence check */
1878       unsupported = true;
1879     }
1880   } else if (NFC_PROTOCOL_T5T == protocol) {
1881     /* T5T/ISO 15693 */
1882     status = RW_I93PresenceCheck();
1883   } else {
1884     /* Protocol unsupported by RW module... */
1885     unsupported = true;
1886   }
1887 
1888   if (unsupported) {
1889     if (nfa_rw_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_KOVIO) {
1890       /* start Kovio presence check (deactivate and wait for activation) */
1891       status = nfa_dm_disc_start_kovio_presence_check();
1892     } else {
1893       /* Let DM perform presence check (by putting tag to sleep and then waking
1894        * it up) */
1895       status = nfa_dm_disc_sleep_wakeup();
1896     }
1897   }
1898 
1899   /* Handle presence check failure */
1900   if (status != NFC_STATUS_OK)
1901     nfa_rw_handle_presence_check_rsp(NFC_STATUS_FAILED);
1902   else if (!unsupported) {
1903     nfa_sys_start_timer(&nfa_rw_cb.tle, NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT,
1904                         p_nfa_dm_cfg->presence_check_timeout);
1905   }
1906 }
1907 
1908 /*******************************************************************************
1909 **
1910 ** Function         nfa_rw_presence_check_tick
1911 **
1912 ** Description      Called on expiration of NFA_RW_PRESENCE_CHECK_INTERVAL
1913 **                  Initiate presence check
1914 **
1915 ** Returns          TRUE (caller frees message buffer)
1916 **
1917 *******************************************************************************/
nfa_rw_presence_check_tick(tNFA_RW_MSG * p_data)1918 bool nfa_rw_presence_check_tick(__attribute__((unused)) tNFA_RW_MSG* p_data) {
1919   /* Store the current operation */
1920   nfa_rw_cb.cur_op = NFA_RW_OP_PRESENCE_CHECK;
1921   nfa_rw_cb.flags |= NFA_RW_FL_AUTO_PRESENCE_CHECK_BUSY;
1922   LOG(VERBOSE) << StringPrintf("Auto-presence check starting...");
1923 
1924   /* Perform presence check */
1925   nfa_rw_presence_check(nullptr);
1926 
1927   return true;
1928 }
1929 
1930 /*******************************************************************************
1931 **
1932 ** Function         nfa_rw_presence_check_timeout
1933 **
1934 ** Description      presence check timeout: report presence check failure
1935 **
1936 ** Returns          TRUE (caller frees message buffer)
1937 **
1938 *******************************************************************************/
nfa_rw_presence_check_timeout(tNFA_RW_MSG * p_data)1939 bool nfa_rw_presence_check_timeout(__attribute__((unused))
1940                                    tNFA_RW_MSG* p_data) {
1941   nfa_rw_handle_presence_check_rsp(NFC_STATUS_FAILED);
1942   return true;
1943 }
1944 
1945 /*******************************************************************************
1946 **
1947 ** Function         nfa_rw_format_tag
1948 **
1949 ** Description      Handler for NFA_RW_API_FORMAT_TAG
1950 **
1951 ** Returns          Nothing
1952 **
1953 *******************************************************************************/
nfa_rw_format_tag()1954 static void nfa_rw_format_tag() {
1955   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
1956   tNFC_STATUS status = NFC_STATUS_FAILED;
1957 
1958   if (protocol == NFC_PROTOCOL_T1T) {
1959     status = RW_T1tFormatNDef();
1960   } else if ((protocol == NFC_PROTOCOL_T2T) &&
1961              (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T)) {
1962     status = RW_T2tFormatNDef();
1963   } else if (protocol == NFC_PROTOCOL_T3T) {
1964     status = RW_T3tFormatNDef();
1965   } else if (protocol == NFC_PROTOCOL_T5T) {
1966     status = RW_I93FormatNDef();
1967   } else if (protocol == NFC_PROTOCOL_ISO_DEP) {
1968     status = RW_T4tFormatNDef();
1969   } else if (protocol == NFC_PROTOCOL_MIFARE) {
1970     status = RW_MfcFormatNDef();
1971   }
1972 
1973   /* If unable to format NDEF, notify the app */
1974   if (status != NFC_STATUS_OK) nfa_rw_error_cleanup(NFA_FORMAT_CPLT_EVT);
1975 }
1976 
1977 /*******************************************************************************
1978 **
1979 ** Function         nfa_rw_detect_tlv
1980 **
1981 ** Description      Handler for NFA_RW_API_DETECT_NDEF_EVT
1982 **
1983 ** Returns          TRUE (message buffer to be freed by caller)
1984 **
1985 *******************************************************************************/
nfa_rw_detect_tlv(uint8_t tlv)1986 static bool nfa_rw_detect_tlv(uint8_t tlv) {
1987   LOG(VERBOSE) << __func__;
1988 
1989   switch (nfa_rw_cb.protocol) {
1990     case NFC_PROTOCOL_T1T:
1991       if (RW_T1tLocateTlv(tlv) != NFC_STATUS_OK)
1992         nfa_rw_error_cleanup(NFA_TLV_DETECT_EVT);
1993       break;
1994 
1995     case NFC_PROTOCOL_T2T:
1996       if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
1997         if (RW_T2tLocateTlv(tlv) != NFC_STATUS_OK)
1998           nfa_rw_error_cleanup(NFA_TLV_DETECT_EVT);
1999       }
2000       break;
2001 
2002     default:
2003       break;
2004   }
2005 
2006   return true;
2007 }
2008 
2009 /*******************************************************************************
2010 **
2011 ** Function         nfa_rw_config_tag_ro
2012 **
2013 ** Description      Handler for NFA_RW_OP_SET_TAG_RO
2014 **
2015 ** Returns          TRUE (message buffer to be freed by caller)
2016 **
2017 *******************************************************************************/
nfa_rw_config_tag_ro(bool b_hard_lock)2018 static tNFC_STATUS nfa_rw_config_tag_ro(bool b_hard_lock) {
2019   tNFC_PROTOCOL protocol = nfa_rw_cb.protocol;
2020   tNFC_STATUS status = NFC_STATUS_FAILED;
2021 
2022   LOG(VERBOSE) << __func__;
2023 
2024   if (NFC_PROTOCOL_T1T == protocol) {
2025     /* Type1Tag    - NFC-A */
2026     if ((nfa_rw_cb.tlv_st == NFA_RW_TLV_DETECT_ST_OP_NOT_STARTED) ||
2027         (nfa_rw_cb.tlv_st == NFA_RW_TLV_DETECT_ST_MEM_TLV_OP_COMPLETE)) {
2028       status = RW_T1tLocateTlv(TAG_LOCK_CTRL_TLV);
2029       return (status);
2030     } else {
2031       status = RW_T1tSetTagReadOnly(b_hard_lock);
2032     }
2033   } else if (NFC_PROTOCOL_T2T == protocol) {
2034     /* Type2Tag    - NFC-A */
2035     if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T) {
2036       status = RW_T2tSetTagReadOnly(b_hard_lock);
2037     }
2038   } else if (NFC_PROTOCOL_T3T == protocol) {
2039     /* Type3Tag    - NFC-F */
2040     status = RW_T3tSetReadOnly(b_hard_lock);
2041   } else if (NFC_PROTOCOL_ISO_DEP == protocol) {
2042     /* ISODEP/4A,4B- NFC-A or NFC-B */
2043     status = RW_T4tSetNDefReadOnly();
2044   } else if (NFC_PROTOCOL_T5T == protocol) {
2045     /* ISO 15693 */
2046     status = RW_I93SetTagReadOnly();
2047   }
2048 
2049   if (status == NFC_STATUS_OK) {
2050     nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
2051   } else {
2052     nfa_rw_error_cleanup(NFA_SET_TAG_RO_EVT);
2053   }
2054 
2055   return (status);
2056 }
2057 
2058 /*******************************************************************************
2059 **
2060 ** Function         nfa_rw_t1t_rid
2061 **
2062 ** Description      Handler for T1T_RID API
2063 **
2064 ** Returns          TRUE (message buffer to be freed by caller)
2065 **
2066 *******************************************************************************/
nfa_rw_t1t_rid()2067 static bool nfa_rw_t1t_rid() {
2068   if (RW_T1tRid() != NFC_STATUS_OK) nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2069 
2070   return true;
2071 }
2072 
2073 /*******************************************************************************
2074 **
2075 ** Function         nfa_rw_t1t_rall
2076 **
2077 ** Description      Handler for T1T_ReadAll API
2078 **
2079 ** Returns          TRUE (message buffer to be freed by caller)
2080 **
2081 *******************************************************************************/
nfa_rw_t1t_rall()2082 static bool nfa_rw_t1t_rall() {
2083   if (RW_T1tReadAll() != NFC_STATUS_OK) nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2084 
2085   return true;
2086 }
2087 
2088 /*******************************************************************************
2089 **
2090 ** Function         nfa_rw_t1t_read
2091 **
2092 ** Description      Handler for T1T_Read API
2093 **
2094 ** Returns          TRUE (message buffer to be freed by caller)
2095 **
2096 *******************************************************************************/
nfa_rw_t1t_read(tNFA_RW_MSG * p_data)2097 static bool nfa_rw_t1t_read(tNFA_RW_MSG* p_data) {
2098   tNFA_RW_OP_PARAMS_T1T_READ* p_t1t_read =
2099       (tNFA_RW_OP_PARAMS_T1T_READ*)&(p_data->op_req.params.t1t_read);
2100 
2101   if (RW_T1tRead(p_t1t_read->block_number, p_t1t_read->index) != NFC_STATUS_OK)
2102     nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2103 
2104   return true;
2105 }
2106 
2107 /*******************************************************************************
2108 **
2109 ** Function         nfa_rw_t1t_write
2110 **
2111 ** Description      Handler for T1T_WriteErase/T1T_WriteNoErase API
2112 **
2113 ** Returns          TRUE (message buffer to be freed by caller)
2114 **
2115 *******************************************************************************/
nfa_rw_t1t_write(tNFA_RW_MSG * p_data)2116 static bool nfa_rw_t1t_write(tNFA_RW_MSG* p_data) {
2117   tNFA_RW_OP_PARAMS_T1T_WRITE* p_t1t_write =
2118       (tNFA_RW_OP_PARAMS_T1T_WRITE*)&(p_data->op_req.params.t1t_write);
2119   tNFC_STATUS status;
2120 
2121   if (p_t1t_write->b_erase) {
2122     status = RW_T1tWriteErase(p_t1t_write->block_number, p_t1t_write->index,
2123                               p_t1t_write->p_block_data[0]);
2124   } else {
2125     status = RW_T1tWriteNoErase(p_t1t_write->block_number, p_t1t_write->index,
2126                                 p_t1t_write->p_block_data[0]);
2127   }
2128 
2129   if (status != NFC_STATUS_OK) {
2130     nfa_rw_error_cleanup(NFA_WRITE_CPLT_EVT);
2131   } else {
2132     if (p_t1t_write->block_number == 0x01)
2133       nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
2134   }
2135 
2136   return true;
2137 }
2138 
2139 /*******************************************************************************
2140 **
2141 ** Function         nfa_rw_t1t_rseg
2142 **
2143 ** Description      Handler for T1t_ReadSeg API
2144 **
2145 ** Returns          TRUE (message buffer to be freed by caller)
2146 **
2147 *******************************************************************************/
nfa_rw_t1t_rseg(tNFA_RW_MSG * p_data)2148 static bool nfa_rw_t1t_rseg(tNFA_RW_MSG* p_data) {
2149   tNFA_RW_OP_PARAMS_T1T_READ* p_t1t_read =
2150       (tNFA_RW_OP_PARAMS_T1T_READ*)&(p_data->op_req.params.t1t_read);
2151 
2152   if (RW_T1tReadSeg(p_t1t_read->segment_number) != NFC_STATUS_OK)
2153     nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2154 
2155   return true;
2156 }
2157 
2158 /*******************************************************************************
2159 **
2160 ** Function         nfa_rw_t1t_read8
2161 **
2162 ** Description      Handler for T1T_Read8 API
2163 **
2164 ** Returns          TRUE (message buffer to be freed by caller)
2165 **
2166 *******************************************************************************/
nfa_rw_t1t_read8(tNFA_RW_MSG * p_data)2167 static bool nfa_rw_t1t_read8(tNFA_RW_MSG* p_data) {
2168   tNFA_RW_OP_PARAMS_T1T_READ* p_t1t_read =
2169       (tNFA_RW_OP_PARAMS_T1T_READ*)&(p_data->op_req.params.t1t_read);
2170 
2171   if (RW_T1tRead8(p_t1t_read->block_number) != NFC_STATUS_OK)
2172     nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2173 
2174   return true;
2175 }
2176 
2177 /*******************************************************************************
2178 **
2179 ** Function         nfa_rw_t1t_write8
2180 **
2181 ** Description      Handler for T1T_WriteErase8/T1T_WriteNoErase8 API
2182 **
2183 ** Returns          TRUE (message buffer to be freed by caller)
2184 **
2185 *******************************************************************************/
nfa_rw_t1t_write8(tNFA_RW_MSG * p_data)2186 static bool nfa_rw_t1t_write8(tNFA_RW_MSG* p_data) {
2187   tNFA_RW_OP_PARAMS_T1T_WRITE* p_t1t_write =
2188       (tNFA_RW_OP_PARAMS_T1T_WRITE*)&(p_data->op_req.params.t1t_write);
2189   tNFC_STATUS status;
2190 
2191   if (p_t1t_write->b_erase) {
2192     status =
2193         RW_T1tWriteErase8(p_t1t_write->block_number, p_t1t_write->p_block_data);
2194   } else {
2195     status = RW_T1tWriteNoErase8(p_t1t_write->block_number,
2196                                  p_t1t_write->p_block_data);
2197   }
2198 
2199   if (status != NFC_STATUS_OK) {
2200     nfa_rw_error_cleanup(NFA_WRITE_CPLT_EVT);
2201   } else {
2202     if (p_t1t_write->block_number == 0x01)
2203       nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
2204   }
2205 
2206   return true;
2207 }
2208 
2209 /*******************************************************************************
2210 **
2211 ** Function         nfa_rw_t2t_read
2212 **
2213 ** Description      Handler for T2T_Read API
2214 **
2215 ** Returns          TRUE (message buffer to be freed by caller)
2216 **
2217 *******************************************************************************/
nfa_rw_t2t_read(tNFA_RW_MSG * p_data)2218 static bool nfa_rw_t2t_read(tNFA_RW_MSG* p_data) {
2219   tNFA_RW_OP_PARAMS_T2T_READ* p_t2t_read =
2220       (tNFA_RW_OP_PARAMS_T2T_READ*)&(p_data->op_req.params.t2t_read);
2221   tNFC_STATUS status = NFC_STATUS_FAILED;
2222 
2223   if (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T)
2224     status = RW_T2tRead(p_t2t_read->block_number);
2225 
2226   if (status != NFC_STATUS_OK) nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2227 
2228   return true;
2229 }
2230 
2231 /*******************************************************************************
2232 **
2233 ** Function         nfa_rw_t2t_write
2234 **
2235 ** Description      Handler for T2T_Write API
2236 **
2237 ** Returns          TRUE (message buffer to be freed by caller)
2238 **
2239 *******************************************************************************/
nfa_rw_t2t_write(tNFA_RW_MSG * p_data)2240 static bool nfa_rw_t2t_write(tNFA_RW_MSG* p_data) {
2241   tNFA_RW_OP_PARAMS_T2T_WRITE* p_t2t_write =
2242       (tNFA_RW_OP_PARAMS_T2T_WRITE*)&(p_data->op_req.params.t2t_write);
2243 
2244   if (RW_T2tWrite(p_t2t_write->block_number, p_t2t_write->p_block_data) !=
2245       NFC_STATUS_OK) {
2246     nfa_rw_error_cleanup(NFA_WRITE_CPLT_EVT);
2247   } else {
2248     if (p_t2t_write->block_number == 0x03)
2249       nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
2250   }
2251 
2252   return true;
2253 }
2254 
2255 /*******************************************************************************
2256 **
2257 ** Function         nfa_rw_t2t_sector_select
2258 **
2259 ** Description      Handler for T2T_Sector_Select API
2260 **
2261 ** Returns          TRUE (message buffer to be freed by caller)
2262 **
2263 *******************************************************************************/
nfa_rw_t2t_sector_select(tNFA_RW_MSG * p_data)2264 static bool nfa_rw_t2t_sector_select(tNFA_RW_MSG* p_data) {
2265   tNFA_RW_OP_PARAMS_T2T_SECTOR_SELECT* p_t2t_sector_select =
2266       (tNFA_RW_OP_PARAMS_T2T_SECTOR_SELECT*)&(
2267           p_data->op_req.params.t2t_sector_select);
2268 
2269   if (RW_T2tSectorSelect(p_t2t_sector_select->sector_number) != NFC_STATUS_OK)
2270     nfa_rw_error_cleanup(NFA_SELECT_CPLT_EVT);
2271 
2272   return true;
2273 }
2274 
2275 /*******************************************************************************
2276 **
2277 ** Function         nfa_rw_t3t_read
2278 **
2279 ** Description      Handler for T3T_Read API
2280 **
2281 ** Returns          TRUE (message buffer to be freed by caller)
2282 **
2283 *******************************************************************************/
nfa_rw_t3t_read(tNFA_RW_MSG * p_data)2284 static bool nfa_rw_t3t_read(tNFA_RW_MSG* p_data) {
2285   tNFA_RW_OP_PARAMS_T3T_READ* p_t3t_read =
2286       (tNFA_RW_OP_PARAMS_T3T_READ*)&(p_data->op_req.params.t3t_read);
2287 
2288   if (RW_T3tCheck(p_t3t_read->num_blocks,
2289                   (tT3T_BLOCK_DESC*)p_t3t_read->p_block_desc) != NFC_STATUS_OK)
2290     nfa_rw_error_cleanup(NFA_READ_CPLT_EVT);
2291 
2292   return true;
2293 }
2294 
2295 /*******************************************************************************
2296 **
2297 ** Function         nfa_rw_t3t_write
2298 **
2299 ** Description      Handler for T3T_Write API
2300 **
2301 ** Returns          TRUE (message buffer to be freed by caller)
2302 **
2303 *******************************************************************************/
nfa_rw_t3t_write(tNFA_RW_MSG * p_data)2304 static bool nfa_rw_t3t_write(tNFA_RW_MSG* p_data) {
2305   tNFA_RW_OP_PARAMS_T3T_WRITE* p_t3t_write =
2306       (tNFA_RW_OP_PARAMS_T3T_WRITE*)&(p_data->op_req.params.t3t_write);
2307 
2308   if (RW_T3tUpdate(p_t3t_write->num_blocks,
2309                    (tT3T_BLOCK_DESC*)p_t3t_write->p_block_desc,
2310                    p_t3t_write->p_block_data) != NFC_STATUS_OK)
2311     nfa_rw_error_cleanup(NFA_WRITE_CPLT_EVT);
2312 
2313   return true;
2314 }
2315 
2316 /*******************************************************************************
2317 **
2318 ** Function         nfa_rw_t3t_get_system_codes
2319 **
2320 ** Description      Get system codes (initiated by NFA after activation)
2321 **
2322 ** Returns          TRUE (message buffer to be freed by caller)
2323 **
2324 *******************************************************************************/
nfa_rw_t3t_get_system_codes()2325 static bool nfa_rw_t3t_get_system_codes() {
2326   tNFC_STATUS status;
2327   tNFA_TAG_PARAMS tag_params;
2328 
2329   status = RW_T3tGetSystemCodes();
2330 
2331   if (status != NFC_STATUS_OK) {
2332     /* Command complete - perform cleanup, notify app */
2333     nfa_rw_command_complete();
2334     tag_params.t3t.num_system_codes = 0;
2335     tag_params.t3t.p_system_codes = nullptr;
2336 
2337     nfa_dm_notify_activation_status(NFA_STATUS_OK, &tag_params);
2338   }
2339 
2340   return true;
2341 }
2342 
2343 /*******************************************************************************
2344 **
2345 ** Function         nfa_rw_i93_command
2346 **
2347 ** Description      Handler for ISO 15693 command
2348 **
2349 ** Returns          TRUE (message buffer to be freed by caller)
2350 **
2351 *******************************************************************************/
nfa_rw_i93_command(tNFA_RW_MSG * p_data)2352 static bool nfa_rw_i93_command(tNFA_RW_MSG* p_data) {
2353   tNFA_CONN_EVT_DATA conn_evt_data;
2354   tNFC_STATUS status = NFC_STATUS_OK;
2355   uint8_t i93_command = I93_CMD_STAY_QUIET;
2356 
2357   switch (p_data->op_req.op) {
2358     case NFA_RW_OP_I93_INVENTORY:
2359       i93_command = I93_CMD_INVENTORY;
2360       if (p_data->op_req.params.i93_cmd.uid_present) {
2361         status = RW_I93Inventory(p_data->op_req.params.i93_cmd.afi_present,
2362                                  p_data->op_req.params.i93_cmd.afi,
2363                                  p_data->op_req.params.i93_cmd.uid);
2364       } else {
2365         status = RW_I93Inventory(p_data->op_req.params.i93_cmd.afi_present,
2366                                  p_data->op_req.params.i93_cmd.afi, nullptr);
2367       }
2368       break;
2369 
2370     case NFA_RW_OP_I93_STAY_QUIET:
2371       i93_command = I93_CMD_STAY_QUIET;
2372       status = RW_I93StayQuiet(p_data->op_req.params.i93_cmd.uid);
2373       break;
2374 
2375     case NFA_RW_OP_I93_READ_SINGLE_BLOCK:
2376       i93_command = I93_CMD_READ_SINGLE_BLOCK;
2377       status = RW_I93ReadSingleBlock(
2378           p_data->op_req.params.i93_cmd.first_block_number);
2379       break;
2380 
2381     case NFA_RW_OP_I93_WRITE_SINGLE_BLOCK:
2382       i93_command = I93_CMD_WRITE_SINGLE_BLOCK;
2383       status = RW_I93WriteSingleBlock(
2384           p_data->op_req.params.i93_cmd.first_block_number,
2385           p_data->op_req.params.i93_cmd.p_data);
2386       break;
2387 
2388     case NFA_RW_OP_I93_LOCK_BLOCK:
2389       i93_command = I93_CMD_LOCK_BLOCK;
2390       status = RW_I93LockBlock(
2391           (uint8_t)p_data->op_req.params.i93_cmd.first_block_number);
2392       break;
2393 
2394     case NFA_RW_OP_I93_READ_MULTI_BLOCK:
2395       i93_command = I93_CMD_READ_MULTI_BLOCK;
2396       status = RW_I93ReadMultipleBlocks(
2397           p_data->op_req.params.i93_cmd.first_block_number,
2398           p_data->op_req.params.i93_cmd.number_blocks);
2399       break;
2400 
2401     case NFA_RW_OP_I93_WRITE_MULTI_BLOCK:
2402       i93_command = I93_CMD_WRITE_MULTI_BLOCK;
2403       status = RW_I93WriteMultipleBlocks(
2404           (uint8_t)p_data->op_req.params.i93_cmd.first_block_number,
2405           p_data->op_req.params.i93_cmd.number_blocks,
2406           p_data->op_req.params.i93_cmd.p_data);
2407       break;
2408 
2409     case NFA_RW_OP_I93_SELECT:
2410       i93_command = I93_CMD_SELECT;
2411       status = RW_I93Select(p_data->op_req.params.i93_cmd.p_data);
2412       break;
2413 
2414     case NFA_RW_OP_I93_RESET_TO_READY:
2415       i93_command = I93_CMD_RESET_TO_READY;
2416       status = RW_I93ResetToReady();
2417       break;
2418 
2419     case NFA_RW_OP_I93_WRITE_AFI:
2420       i93_command = I93_CMD_WRITE_AFI;
2421       status = RW_I93WriteAFI(p_data->op_req.params.i93_cmd.afi);
2422       break;
2423 
2424     case NFA_RW_OP_I93_LOCK_AFI:
2425       i93_command = I93_CMD_LOCK_AFI;
2426       status = RW_I93LockAFI();
2427       break;
2428 
2429     case NFA_RW_OP_I93_WRITE_DSFID:
2430       i93_command = I93_CMD_WRITE_DSFID;
2431       status = RW_I93WriteDSFID(p_data->op_req.params.i93_cmd.dsfid);
2432       break;
2433 
2434     case NFA_RW_OP_I93_LOCK_DSFID:
2435       i93_command = I93_CMD_LOCK_DSFID;
2436       status = RW_I93LockDSFID();
2437       break;
2438 
2439     case NFA_RW_OP_I93_GET_SYS_INFO:
2440       i93_command = I93_CMD_GET_SYS_INFO;
2441       if (p_data->op_req.params.i93_cmd.uid_present) {
2442         status = RW_I93GetSysInfo(p_data->op_req.params.i93_cmd.uid);
2443       } else {
2444         status = RW_I93GetSysInfo(nullptr);
2445       }
2446       break;
2447 
2448     case NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS:
2449       i93_command = I93_CMD_GET_MULTI_BLK_SEC;
2450       status = RW_I93GetMultiBlockSecurityStatus(
2451           p_data->op_req.params.i93_cmd.first_block_number,
2452           p_data->op_req.params.i93_cmd.number_blocks);
2453       break;
2454 
2455     case NFA_RW_OP_I93_SET_ADDR_MODE:
2456       i93_command = I93_CMD_SET_ADDR_MODE;
2457       LOG(VERBOSE) << StringPrintf(
2458           "%s - T5T addressing mode (0: addressed, "
2459           "1: non-addressed) is %d",
2460           __func__, p_data->op_req.params.i93_cmd.addr_mode);
2461 
2462       status = RW_I93SetAddressingMode(p_data->op_req.params.i93_cmd.addr_mode);
2463       if (status != NFC_STATUS_OK) {
2464         break;
2465       }
2466 
2467       /* Command complete - perform cleanup, notify app */
2468       nfa_rw_command_complete();
2469       conn_evt_data.i93_cmd_cplt.status = NFA_STATUS_OK;
2470       conn_evt_data.i93_cmd_cplt.sent_command = i93_command;
2471       nfa_dm_act_conn_cback_notify(NFA_I93_CMD_CPLT_EVT, &conn_evt_data);
2472       break;
2473 
2474     default:
2475       break;
2476   }
2477 
2478   if (status != NFC_STATUS_OK) {
2479     /* Command complete - perform cleanup, notify app */
2480     nfa_rw_command_complete();
2481 
2482     conn_evt_data.i93_cmd_cplt.status = NFA_STATUS_FAILED;
2483     conn_evt_data.i93_cmd_cplt.sent_command = i93_command;
2484 
2485     nfa_dm_act_conn_cback_notify(NFA_I93_CMD_CPLT_EVT, &conn_evt_data);
2486   }
2487 
2488   return true;
2489 }
2490 
2491 /*******************************************************************************
2492 **
2493 ** Function         nfa_rw_raw_mode_data_cback
2494 **
2495 ** Description      Handler for incoming tag data for unsupported tag protocols
2496 **                  (forward data to upper layer)
2497 **
2498 ** Returns          nothing
2499 **
2500 *******************************************************************************/
nfa_rw_raw_mode_data_cback(uint8_t conn_id,tNFC_CONN_EVT event,tNFC_CONN * p_data)2501 static void nfa_rw_raw_mode_data_cback(__attribute__((unused)) uint8_t conn_id,
2502                                        tNFC_CONN_EVT event, tNFC_CONN* p_data) {
2503   NFC_HDR* p_msg;
2504   tNFA_CONN_EVT_DATA evt_data;
2505 
2506   LOG(VERBOSE) << StringPrintf("event = 0x%X", event);
2507 
2508   if ((event == NFC_DATA_CEVT) &&
2509       ((p_data->data.status == NFC_STATUS_OK) ||
2510        (p_data->data.status == NFC_STATUS_CONTINUE))) {
2511     p_msg = (NFC_HDR*)p_data->data.p_data;
2512 
2513     if (p_msg) {
2514       evt_data.data.status = p_data->data.status;
2515       evt_data.data.p_data = (uint8_t*)(p_msg + 1) + p_msg->offset;
2516       evt_data.data.len = p_msg->len;
2517 
2518       nfa_dm_conn_cback_event_notify(NFA_DATA_EVT, &evt_data);
2519 
2520       GKI_freebuf(p_msg);
2521     } else {
2522       LOG(ERROR) << StringPrintf(
2523           "received NFC_DATA_CEVT with NULL data pointer");
2524     }
2525   } else if (event == NFC_DEACTIVATE_CEVT) {
2526     NFC_SetStaticRfCback(nullptr);
2527   }
2528 }
2529 
2530 /*******************************************************************************
2531 **
2532 ** Function         nfa_rw_activate_ntf
2533 **
2534 ** Description      Handler for NFA_RW_ACTIVATE_NTF
2535 **
2536 ** Returns          TRUE (message buffer to be freed by caller)
2537 **
2538 *******************************************************************************/
nfa_rw_activate_ntf(tNFA_RW_MSG * p_data)2539 bool nfa_rw_activate_ntf(tNFA_RW_MSG* p_data) {
2540   tNFC_ACTIVATE_DEVT* p_activate_params =
2541       p_data->activate_ntf.p_activate_params;
2542   tNFA_TAG_PARAMS tag_params;
2543   bool activate_notify = true;
2544   uint8_t* p;
2545 
2546   if ((nfa_rw_cb.halt_event != RW_T2T_MAX_EVT) &&
2547       (nfa_rw_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A) &&
2548       (nfa_rw_cb.protocol == NFC_PROTOCOL_T2T) &&
2549       (nfa_rw_cb.pa_sel_res == NFC_SEL_RES_NFC_FORUM_T2T)) {
2550     /* Type 2 tag is wake up from HALT State */
2551     if (nfa_dm_cb.p_activate_ntf != nullptr) {
2552       GKI_freebuf(nfa_dm_cb.p_activate_ntf);
2553       nfa_dm_cb.p_activate_ntf = nullptr;
2554     }
2555     LOG(VERBOSE) << StringPrintf("- Type 2 tag wake up from HALT State");
2556     return true;
2557   }
2558 
2559   LOG(VERBOSE) << __func__;
2560 
2561   /* Initialize control block */
2562   nfa_rw_cb.protocol = p_activate_params->protocol;
2563   nfa_rw_cb.intf_type = p_activate_params->intf_param.type;
2564   nfa_rw_cb.pa_sel_res = p_activate_params->rf_tech_param.param.pa.sel_rsp;
2565   nfa_rw_cb.activated_tech_mode = p_activate_params->rf_tech_param.mode;
2566   nfa_rw_cb.flags = NFA_RW_FL_ACTIVATED;
2567   nfa_rw_cb.cur_op = NFA_RW_OP_MAX;
2568   nfa_rw_cb.halt_event = RW_T2T_MAX_EVT;
2569   nfa_rw_cb.skip_dyn_locks = false;
2570   nfa_rw_cb.ndef_st = NFA_RW_NDEF_ST_UNKNOWN;
2571   nfa_rw_cb.tlv_st = NFA_RW_TLV_DETECT_ST_OP_NOT_STARTED;
2572 
2573   memset(&tag_params, 0, sizeof(tNFA_TAG_PARAMS));
2574 
2575   /* Check if we are in exclusive RF mode */
2576   if (p_data->activate_ntf.excl_rf_not_active) {
2577     /* Not in exclusive RF mode */
2578     nfa_rw_cb.flags |= NFA_RW_FL_NOT_EXCL_RF_MODE;
2579   }
2580 
2581   /* check if the protocol is activated with supported interface */
2582   if (p_activate_params->intf_param.type == NCI_INTERFACE_FRAME) {
2583     if ((p_activate_params->protocol != NFA_PROTOCOL_T1T) &&
2584         (p_activate_params->protocol != NFA_PROTOCOL_T2T) &&
2585         (p_activate_params->protocol != NFA_PROTOCOL_T3T) &&
2586         (p_activate_params->protocol != NFA_PROTOCOL_T5T)) {
2587       nfa_rw_cb.protocol = NFA_PROTOCOL_INVALID;
2588     }
2589   } else if (p_activate_params->intf_param.type == NCI_INTERFACE_ISO_DEP) {
2590     if (p_activate_params->protocol != NFA_PROTOCOL_ISO_DEP) {
2591       nfa_rw_cb.protocol = NFA_PROTOCOL_INVALID;
2592     }
2593   }
2594 
2595   if (nfa_rw_cb.protocol == NFA_PROTOCOL_INVALID) {
2596     /* Only sending raw frame and presence check are supported in this state */
2597 
2598     NFC_SetStaticRfCback(nfa_rw_raw_mode_data_cback);
2599 
2600     /* Notify app of NFA_ACTIVATED_EVT and start presence check timer */
2601     nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
2602     nfa_rw_check_start_presence_check_timer(NFA_RW_PRESENCE_CHECK_INTERVAL);
2603     return true;
2604   }
2605 
2606   /* If protocol not supported by RW module, notify app of NFA_ACTIVATED_EVT and
2607    * start presence check if needed */
2608   if (!nfa_dm_is_protocol_supported(
2609           p_activate_params->protocol,
2610           p_activate_params->rf_tech_param.param.pa.sel_rsp)) {
2611     /* Notify upper layer of NFA_ACTIVATED_EVT if needed, and start presence
2612      * check timer */
2613     /* Set data callback (pass all incoming data to upper layer using
2614      * NFA_DATA_EVT) */
2615     NFC_SetStaticRfCback(nfa_rw_raw_mode_data_cback);
2616 
2617     /* Notify app of NFA_ACTIVATED_EVT and start presence check timer */
2618     nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
2619     nfa_rw_check_start_presence_check_timer(NFA_RW_PRESENCE_CHECK_INTERVAL);
2620     return true;
2621   }
2622 
2623   /* Initialize RW module */
2624   if ((RW_SetActivatedTagType(p_activate_params, nfa_rw_cback)) !=
2625       NFC_STATUS_OK) {
2626     /* Log error (stay in NFA_RW_ST_ACTIVATED state until deactivation) */
2627     LOG(ERROR) << StringPrintf("RW_SetActivatedTagType failed.");
2628     return true;
2629   }
2630 
2631   /* Perform protocol-specific actions */
2632   if (NFC_PROTOCOL_T1T == nfa_rw_cb.protocol) {
2633     /* Retrieve HR and UID fields from activation notification */
2634     memcpy(tag_params.t1t.uid, p_activate_params->rf_tech_param.param.pa.nfcid1,
2635            p_activate_params->rf_tech_param.param.pa.nfcid1_len);
2636 
2637     if (NFC_GetNCIVersion() >= NCI_VERSION_2_0) {
2638       memcpy(tag_params.t1t.hr, p_activate_params->rf_tech_param.param.pa.hr,
2639              NFA_T1T_HR_LEN);
2640     } else {
2641       memcpy(tag_params.t1t.hr,
2642              p_activate_params->intf_param.intf_param.frame.param,
2643              NFA_T1T_HR_LEN);
2644       tNFA_RW_MSG msg;
2645       msg.op_req.op = NFA_RW_OP_T1T_RID;
2646       bool free_buf = nfa_rw_handle_op_req(&msg);
2647       CHECK(free_buf)
2648           << "nfa_rw_handle_op_req is holding on to soon-garbage stack memory.";
2649       /* Delay notifying upper layer of NFA_ACTIVATED_EVT
2650          until HR0/HR1 is received */
2651       activate_notify = false;
2652     }
2653   } else if (NFC_PROTOCOL_T2T == nfa_rw_cb.protocol) {
2654     /* Retrieve UID fields from activation notification */
2655     memcpy(tag_params.t2t.uid, p_activate_params->rf_tech_param.param.pa.nfcid1,
2656            p_activate_params->rf_tech_param.param.pa.nfcid1_len);
2657   } else if (NFC_PROTOCOL_T3T == nfa_rw_cb.protocol) {
2658     /* Delay notifying upper layer of NFA_ACTIVATED_EVT until system codes
2659      * are retrieved */
2660     activate_notify = false;
2661 
2662     /* Issue command to get Felica system codes */
2663     tNFA_RW_MSG msg;
2664     msg.op_req.op = NFA_RW_OP_T3T_GET_SYSTEM_CODES;
2665     bool free_buf = nfa_rw_handle_op_req(&msg);
2666     CHECK(free_buf)
2667         << "nfa_rw_handle_op_req is holding on to soon-garbage stack memory.";
2668   } else if (NFA_PROTOCOL_T5T == nfa_rw_cb.protocol) {
2669     /* Delay notifying upper layer of NFA_ACTIVATED_EVT to retrieve additional
2670      * tag infomation */
2671     nfa_rw_cb.flags |= NFA_RW_FL_ACTIVATION_NTF_PENDING;
2672     activate_notify = false;
2673 
2674     /* store DSFID and UID from activation NTF */
2675     nfa_rw_cb.i93_dsfid = p_activate_params->rf_tech_param.param.pi93.dsfid;
2676 
2677     p = nfa_rw_cb.i93_uid;
2678     ARRAY8_TO_STREAM(p, p_activate_params->rf_tech_param.param.pi93.uid);
2679 
2680     if ((nfa_rw_cb.i93_uid[1] == I93_UID_IC_MFG_CODE_TI) &&
2681         (((nfa_rw_cb.i93_uid[2] & I93_UID_TAG_IT_HF_I_PRODUCT_ID_MASK) ==
2682           I93_UID_TAG_IT_HF_I_STD_CHIP_INLAY) ||
2683          ((nfa_rw_cb.i93_uid[2] & I93_UID_TAG_IT_HF_I_PRODUCT_ID_MASK) ==
2684           I93_UID_TAG_IT_HF_I_PRO_CHIP_INLAY))) {
2685       /* these don't support Get System Information Command */
2686       nfa_rw_cb.i93_block_size = I93_TAG_IT_HF_I_STD_PRO_CHIP_INLAY_BLK_SIZE;
2687       nfa_rw_cb.i93_afi_location =
2688           I93_TAG_IT_HF_I_STD_PRO_CHIP_INLAY_AFI_LOCATION;
2689 
2690       if ((nfa_rw_cb.i93_uid[2] & I93_UID_TAG_IT_HF_I_PRODUCT_ID_MASK) ==
2691           I93_UID_TAG_IT_HF_I_STD_CHIP_INLAY) {
2692         nfa_rw_cb.i93_num_block = I93_TAG_IT_HF_I_STD_CHIP_INLAY_NUM_TOTAL_BLK;
2693       } else {
2694         nfa_rw_cb.i93_num_block = I93_TAG_IT_HF_I_PRO_CHIP_INLAY_NUM_TOTAL_BLK;
2695       }
2696 
2697       /* read AFI */
2698       if (RW_I93ReadSingleBlock((uint8_t)(nfa_rw_cb.i93_afi_location /
2699                                           nfa_rw_cb.i93_block_size)) !=
2700           NFC_STATUS_OK) {
2701         /* notify activation without AFI/IC-Ref */
2702         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
2703         activate_notify = true;
2704 
2705         tag_params.i93.info_flags =
2706             (I93_INFO_FLAG_DSFID | I93_INFO_FLAG_MEM_SIZE);
2707         tag_params.i93.dsfid = nfa_rw_cb.i93_dsfid;
2708         tag_params.i93.block_size = nfa_rw_cb.i93_block_size;
2709         tag_params.i93.num_block = nfa_rw_cb.i93_num_block;
2710         memcpy(tag_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
2711       }
2712     } else {
2713       /* All of ICODE supports Get System Information Command */
2714       /* Tag-it HF-I Plus Chip/Inlay supports Get System Information Command */
2715       /* just try for others */
2716 
2717       if (RW_I93CheckLegacyProduct(nfa_rw_cb.i93_uid[1],
2718                                    nfa_rw_cb.i93_uid[2])) {
2719         if (RW_I93GetSysInfo(nfa_rw_cb.i93_uid) != NFC_STATUS_OK) {
2720           /* notify activation without AFI/MEM size/IC-Ref */
2721           nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
2722           activate_notify = true;
2723 
2724           tag_params.i93.info_flags = I93_INFO_FLAG_DSFID;
2725           tag_params.i93.dsfid = nfa_rw_cb.i93_dsfid;
2726           tag_params.i93.block_size = 0;
2727           tag_params.i93.num_block = 0;
2728           memcpy(tag_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
2729         } else {
2730           /* reset memory size */
2731           nfa_rw_cb.i93_block_size = 0;
2732           nfa_rw_cb.i93_num_block = 0;
2733         }
2734       } else {
2735         nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATION_NTF_PENDING;
2736         activate_notify = true;
2737 
2738         tag_params.i93.info_flags = I93_INFO_FLAG_DSFID;
2739         tag_params.i93.dsfid = nfa_rw_cb.i93_dsfid;
2740         tag_params.i93.block_size = 0;
2741         tag_params.i93.num_block = 0;
2742         memcpy(tag_params.i93.uid, nfa_rw_cb.i93_uid, I93_UID_BYTE_LEN);
2743       }
2744     }
2745   }
2746 
2747   /* Notify upper layer of NFA_ACTIVATED_EVT if needed, and start presence check
2748    * timer */
2749   if (activate_notify) {
2750     nfa_dm_notify_activation_status(NFA_STATUS_OK, &tag_params);
2751     nfa_rw_check_start_presence_check_timer(NFA_RW_PRESENCE_CHECK_INTERVAL);
2752   }
2753 
2754   return true;
2755 }
2756 
2757 /*******************************************************************************
2758 **
2759 ** Function         nfa_rw_deactivate_ntf
2760 **
2761 ** Description      Handler for NFA_RW_DEACTIVATE_NTF
2762 **
2763 ** Returns          TRUE (message buffer to be freed by caller)
2764 **
2765 *******************************************************************************/
nfa_rw_deactivate_ntf(tNFA_RW_MSG * p_data)2766 bool nfa_rw_deactivate_ntf(__attribute__((unused)) tNFA_RW_MSG* p_data) {
2767   /* Clear the activated flag */
2768   nfa_rw_cb.flags &= ~NFA_RW_FL_ACTIVATED;
2769 
2770   /* Free buffer for incoming NDEF message, in case we were in the middle of a
2771    * read operation */
2772   nfa_rw_free_ndef_rx_buf();
2773 
2774   /* If there is a pending command message, then free it */
2775   if (nfa_rw_cb.p_pending_msg) {
2776     if ((nfa_rw_cb.p_pending_msg->op_req.op == NFA_RW_OP_SEND_RAW_FRAME) &&
2777         (nfa_rw_cb.p_pending_msg->op_req.params.send_raw_frame.p_data)) {
2778       GKI_freebuf(nfa_rw_cb.p_pending_msg->op_req.params.send_raw_frame.p_data);
2779     }
2780 
2781     GKI_freebuf(nfa_rw_cb.p_pending_msg);
2782     nfa_rw_cb.p_pending_msg = nullptr;
2783   }
2784 
2785   /* If we are in the process of waking up tag from HALT state */
2786   if (nfa_rw_cb.halt_event == RW_T2T_READ_CPLT_EVT) {
2787     if (nfa_rw_cb.rw_data.data.p_data)
2788       GKI_freebuf(nfa_rw_cb.rw_data.data.p_data);
2789     nfa_rw_cb.rw_data.data.p_data = nullptr;
2790   }
2791 
2792   /* Stop presence check timer (if started) */
2793   nfa_rw_stop_presence_check_timer();
2794 
2795   return true;
2796 }
2797 
2798 /*******************************************************************************
2799 **
2800 ** Function         nfa_rw_handle_op_req
2801 **
2802 ** Description      Handler for NFA_RW_OP_REQUEST_EVT, operation request
2803 **
2804 ** Returns          TRUE if caller should free p_data
2805 **                  FALSE if caller does not need to free p_data
2806 **
2807 *******************************************************************************/
nfa_rw_handle_op_req(tNFA_RW_MSG * p_data)2808 bool nfa_rw_handle_op_req(tNFA_RW_MSG* p_data) {
2809   tNFA_CONN_EVT_DATA conn_evt_data;
2810   bool freebuf = true;
2811   uint16_t presence_check_start_delay = 0;
2812 
2813   /* Check if activated */
2814   if (!(nfa_rw_cb.flags & NFA_RW_FL_ACTIVATED)) {
2815     LOG(ERROR) << StringPrintf("nfa_rw_handle_op_req: not activated");
2816     return (nfa_rw_op_req_while_inactive(p_data));
2817   }
2818   /* Check if currently busy with another API call */
2819   else if (nfa_rw_cb.flags & NFA_RW_FL_API_BUSY) {
2820     return (nfa_rw_op_req_while_busy(p_data));
2821   }
2822   /* Check if currently busy with auto-presence check */
2823   else if (nfa_rw_cb.flags & NFA_RW_FL_AUTO_PRESENCE_CHECK_BUSY) {
2824     /* Cache the command (will be handled once auto-presence check is completed)
2825      */
2826     LOG(VERBOSE) << StringPrintf(
2827         "Deferring operation %i until after auto-presence check is completed",
2828         p_data->op_req.op);
2829     nfa_rw_cb.p_pending_msg = p_data;
2830     nfa_rw_cb.flags |= NFA_RW_FL_API_BUSY;
2831     return false;
2832   }
2833 
2834   LOG(VERBOSE) << StringPrintf("nfa_rw_handle_op_req: op=0x%02x",
2835                              p_data->op_req.op);
2836 
2837   nfa_rw_cb.flags |= NFA_RW_FL_API_BUSY;
2838 
2839   /* Stop the presence check timer */
2840   nfa_rw_stop_presence_check_timer();
2841 
2842   /* Store the current operation */
2843   nfa_rw_cb.cur_op = p_data->op_req.op;
2844 
2845   /* Call appropriate handler for requested operation */
2846   switch (p_data->op_req.op) {
2847     case NFA_RW_OP_DETECT_NDEF:
2848       nfa_rw_detect_ndef();
2849       break;
2850 
2851     case NFA_RW_OP_READ_NDEF:
2852       nfa_rw_read_ndef();
2853       break;
2854 
2855     case NFA_RW_OP_WRITE_NDEF:
2856       nfa_rw_write_ndef(p_data);
2857       break;
2858 
2859     case NFA_RW_OP_SEND_RAW_FRAME:
2860       presence_check_start_delay =
2861           p_data->op_req.params.send_raw_frame.p_data->layer_specific;
2862 
2863       NFC_SendData(NFC_RF_CONN_ID, p_data->op_req.params.send_raw_frame.p_data);
2864 
2865       /* Clear the busy flag */
2866       nfa_rw_cb.flags &= ~NFA_RW_FL_API_BUSY;
2867 
2868       /* Start presence_check after specified delay */
2869       nfa_rw_check_start_presence_check_timer(presence_check_start_delay);
2870       break;
2871 
2872     case NFA_RW_OP_PRESENCE_CHECK:
2873       nfa_rw_presence_check(p_data);
2874       break;
2875 
2876     case NFA_RW_OP_FORMAT_TAG:
2877       nfa_rw_format_tag();
2878       break;
2879 
2880     case NFA_RW_OP_DETECT_LOCK_TLV:
2881       nfa_rw_detect_tlv(TAG_LOCK_CTRL_TLV);
2882       break;
2883 
2884     case NFA_RW_OP_DETECT_MEM_TLV:
2885       nfa_rw_detect_tlv(TAG_MEM_CTRL_TLV);
2886       break;
2887 
2888     case NFA_RW_OP_SET_TAG_RO:
2889       nfa_rw_cb.b_hard_lock = p_data->op_req.params.set_readonly.b_hard_lock;
2890       nfa_rw_config_tag_ro(nfa_rw_cb.b_hard_lock);
2891       break;
2892 
2893     case NFA_RW_OP_T1T_RID:
2894       nfa_rw_t1t_rid();
2895       break;
2896 
2897     case NFA_RW_OP_T1T_RALL:
2898       nfa_rw_t1t_rall();
2899       break;
2900 
2901     case NFA_RW_OP_T1T_READ:
2902       nfa_rw_t1t_read(p_data);
2903       break;
2904 
2905     case NFA_RW_OP_T1T_WRITE:
2906       nfa_rw_t1t_write(p_data);
2907       break;
2908 
2909     case NFA_RW_OP_T1T_RSEG:
2910       nfa_rw_t1t_rseg(p_data);
2911       break;
2912 
2913     case NFA_RW_OP_T1T_READ8:
2914       nfa_rw_t1t_read8(p_data);
2915       break;
2916 
2917     case NFA_RW_OP_T1T_WRITE8:
2918       nfa_rw_t1t_write8(p_data);
2919       break;
2920 
2921     /* Type-2 tag commands */
2922     case NFA_RW_OP_T2T_READ:
2923       nfa_rw_t2t_read(p_data);
2924       break;
2925 
2926     case NFA_RW_OP_T2T_WRITE:
2927       nfa_rw_t2t_write(p_data);
2928       break;
2929 
2930     case NFA_RW_OP_T2T_SECTOR_SELECT:
2931       nfa_rw_t2t_sector_select(p_data);
2932       break;
2933 
2934     case NFA_RW_OP_T2T_READ_DYN_LOCKS:
2935       if (p_data->op_req.params.t2t_read_dyn_locks.read_dyn_locks == true) {
2936         nfa_rw_cb.skip_dyn_locks = false;
2937       } else {
2938         nfa_rw_cb.skip_dyn_locks = true;
2939       }
2940       LOG(VERBOSE) << StringPrintf("%s - Skip reading of dynamic lock bytes: %d",
2941                                  __func__, nfa_rw_cb.skip_dyn_locks);
2942 
2943       /* Command complete - perform cleanup, notify app */
2944       nfa_rw_command_complete();
2945       conn_evt_data.status = NFA_STATUS_OK;
2946       nfa_dm_act_conn_cback_notify(NFA_T2T_CMD_CPLT_EVT, &conn_evt_data);
2947       break;
2948 
2949     /* Type-3 tag commands */
2950     case NFA_RW_OP_T3T_READ:
2951       nfa_rw_t3t_read(p_data);
2952       break;
2953 
2954     case NFA_RW_OP_T3T_WRITE:
2955       nfa_rw_t3t_write(p_data);
2956       break;
2957 
2958     case NFA_RW_OP_T3T_GET_SYSTEM_CODES:
2959       nfa_rw_t3t_get_system_codes();
2960       break;
2961 
2962     /* ISO 15693 tag commands */
2963     case NFA_RW_OP_I93_INVENTORY:
2964     case NFA_RW_OP_I93_STAY_QUIET:
2965     case NFA_RW_OP_I93_READ_SINGLE_BLOCK:
2966     case NFA_RW_OP_I93_WRITE_SINGLE_BLOCK:
2967     case NFA_RW_OP_I93_LOCK_BLOCK:
2968     case NFA_RW_OP_I93_READ_MULTI_BLOCK:
2969     case NFA_RW_OP_I93_WRITE_MULTI_BLOCK:
2970     case NFA_RW_OP_I93_SELECT:
2971     case NFA_RW_OP_I93_RESET_TO_READY:
2972     case NFA_RW_OP_I93_WRITE_AFI:
2973     case NFA_RW_OP_I93_LOCK_AFI:
2974     case NFA_RW_OP_I93_WRITE_DSFID:
2975     case NFA_RW_OP_I93_LOCK_DSFID:
2976     case NFA_RW_OP_I93_GET_SYS_INFO:
2977     case NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS:
2978     case NFA_RW_OP_I93_SET_ADDR_MODE:
2979       nfa_rw_i93_command(p_data);
2980       break;
2981 
2982     default:
2983       LOG(ERROR) << StringPrintf("nfa_rw_handle_api: unhandled operation: %i",
2984                                  p_data->op_req.op);
2985       break;
2986   }
2987 
2988   return (freebuf);
2989 }
2990 
2991 /*******************************************************************************
2992 **
2993 ** Function         nfa_rw_op_req_while_busy
2994 **
2995 ** Description      Handle operation request while busy
2996 **
2997 ** Returns          TRUE if caller should free p_data
2998 **                  FALSE if caller does not need to free p_data
2999 **
3000 *******************************************************************************/
nfa_rw_op_req_while_busy(tNFA_RW_MSG * p_data)3001 static bool nfa_rw_op_req_while_busy(tNFA_RW_MSG* p_data) {
3002   bool freebuf = true;
3003   tNFA_CONN_EVT_DATA conn_evt_data;
3004   uint8_t event;
3005 
3006   LOG(ERROR) << StringPrintf("nfa_rw_op_req_while_busy: unable to handle API");
3007 
3008   /* Return appropriate event for requested API, with status=BUSY */
3009   conn_evt_data.status = NFA_STATUS_BUSY;
3010 
3011   switch (p_data->op_req.op) {
3012     case NFA_RW_OP_DETECT_NDEF:
3013       conn_evt_data.ndef_detect.cur_size = 0;
3014       conn_evt_data.ndef_detect.max_size = 0;
3015       conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
3016       event = NFA_NDEF_DETECT_EVT;
3017       break;
3018     case NFA_RW_OP_READ_NDEF:
3019     case NFA_RW_OP_T1T_RID:
3020     case NFA_RW_OP_T1T_RALL:
3021     case NFA_RW_OP_T1T_READ:
3022     case NFA_RW_OP_T1T_RSEG:
3023     case NFA_RW_OP_T1T_READ8:
3024     case NFA_RW_OP_T2T_READ:
3025     case NFA_RW_OP_T3T_READ:
3026       event = NFA_READ_CPLT_EVT;
3027       break;
3028     case NFA_RW_OP_WRITE_NDEF:
3029     case NFA_RW_OP_T1T_WRITE:
3030     case NFA_RW_OP_T1T_WRITE8:
3031     case NFA_RW_OP_T2T_WRITE:
3032     case NFA_RW_OP_T3T_WRITE:
3033       event = NFA_WRITE_CPLT_EVT;
3034       break;
3035     case NFA_RW_OP_FORMAT_TAG:
3036       event = NFA_FORMAT_CPLT_EVT;
3037       break;
3038     case NFA_RW_OP_DETECT_LOCK_TLV:
3039     case NFA_RW_OP_DETECT_MEM_TLV:
3040       event = NFA_TLV_DETECT_EVT;
3041       break;
3042     case NFA_RW_OP_SET_TAG_RO:
3043       event = NFA_SET_TAG_RO_EVT;
3044       break;
3045     case NFA_RW_OP_T2T_SECTOR_SELECT:
3046       event = NFA_SELECT_CPLT_EVT;
3047       break;
3048     case NFA_RW_OP_I93_INVENTORY:
3049     case NFA_RW_OP_I93_STAY_QUIET:
3050     case NFA_RW_OP_I93_READ_SINGLE_BLOCK:
3051     case NFA_RW_OP_I93_WRITE_SINGLE_BLOCK:
3052     case NFA_RW_OP_I93_LOCK_BLOCK:
3053     case NFA_RW_OP_I93_READ_MULTI_BLOCK:
3054     case NFA_RW_OP_I93_WRITE_MULTI_BLOCK:
3055     case NFA_RW_OP_I93_SELECT:
3056     case NFA_RW_OP_I93_RESET_TO_READY:
3057     case NFA_RW_OP_I93_WRITE_AFI:
3058     case NFA_RW_OP_I93_LOCK_AFI:
3059     case NFA_RW_OP_I93_WRITE_DSFID:
3060     case NFA_RW_OP_I93_LOCK_DSFID:
3061     case NFA_RW_OP_I93_GET_SYS_INFO:
3062     case NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS:
3063       event = NFA_I93_CMD_CPLT_EVT;
3064       break;
3065     default:
3066       return (freebuf);
3067   }
3068   nfa_dm_act_conn_cback_notify(event, &conn_evt_data);
3069 
3070   return (freebuf);
3071 }
3072 
3073 /*******************************************************************************
3074 **
3075 ** Function         nfa_rw_op_req_while_inactive
3076 **
3077 ** Description      Handle operation request while inactive
3078 **
3079 ** Returns          TRUE if caller should free p_data
3080 **                  FALSE if caller does not need to free p_data
3081 **
3082 *******************************************************************************/
nfa_rw_op_req_while_inactive(tNFA_RW_MSG * p_data)3083 static bool nfa_rw_op_req_while_inactive(tNFA_RW_MSG* p_data) {
3084   bool freebuf = true;
3085   tNFA_CONN_EVT_DATA conn_evt_data;
3086   uint8_t event;
3087 
3088   LOG(ERROR) << StringPrintf(
3089       "nfa_rw_op_req_while_inactive: unable to handle API");
3090 
3091   /* Return appropriate event for requested API, with status=REJECTED */
3092   conn_evt_data.status = NFA_STATUS_REJECTED;
3093 
3094   switch (p_data->op_req.op) {
3095     case NFA_RW_OP_DETECT_NDEF:
3096       conn_evt_data.ndef_detect.cur_size = 0;
3097       conn_evt_data.ndef_detect.max_size = 0;
3098       conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
3099       event = NFA_NDEF_DETECT_EVT;
3100       break;
3101     case NFA_RW_OP_READ_NDEF:
3102     case NFA_RW_OP_T1T_RID:
3103     case NFA_RW_OP_T1T_RALL:
3104     case NFA_RW_OP_T1T_READ:
3105     case NFA_RW_OP_T1T_RSEG:
3106     case NFA_RW_OP_T1T_READ8:
3107     case NFA_RW_OP_T2T_READ:
3108     case NFA_RW_OP_T3T_READ:
3109       event = NFA_READ_CPLT_EVT;
3110       break;
3111     case NFA_RW_OP_WRITE_NDEF:
3112     case NFA_RW_OP_T1T_WRITE:
3113     case NFA_RW_OP_T1T_WRITE8:
3114     case NFA_RW_OP_T2T_WRITE:
3115     case NFA_RW_OP_T3T_WRITE:
3116       event = NFA_WRITE_CPLT_EVT;
3117       break;
3118     case NFA_RW_OP_FORMAT_TAG:
3119       event = NFA_FORMAT_CPLT_EVT;
3120       break;
3121     case NFA_RW_OP_DETECT_LOCK_TLV:
3122     case NFA_RW_OP_DETECT_MEM_TLV:
3123       event = NFA_TLV_DETECT_EVT;
3124       break;
3125     case NFA_RW_OP_SET_TAG_RO:
3126       event = NFA_SET_TAG_RO_EVT;
3127       break;
3128     case NFA_RW_OP_T2T_SECTOR_SELECT:
3129       event = NFA_SELECT_CPLT_EVT;
3130       break;
3131     case NFA_RW_OP_I93_INVENTORY:
3132     case NFA_RW_OP_I93_STAY_QUIET:
3133     case NFA_RW_OP_I93_READ_SINGLE_BLOCK:
3134     case NFA_RW_OP_I93_WRITE_SINGLE_BLOCK:
3135     case NFA_RW_OP_I93_LOCK_BLOCK:
3136     case NFA_RW_OP_I93_READ_MULTI_BLOCK:
3137     case NFA_RW_OP_I93_WRITE_MULTI_BLOCK:
3138     case NFA_RW_OP_I93_SELECT:
3139     case NFA_RW_OP_I93_RESET_TO_READY:
3140     case NFA_RW_OP_I93_WRITE_AFI:
3141     case NFA_RW_OP_I93_LOCK_AFI:
3142     case NFA_RW_OP_I93_WRITE_DSFID:
3143     case NFA_RW_OP_I93_LOCK_DSFID:
3144     case NFA_RW_OP_I93_GET_SYS_INFO:
3145     case NFA_RW_OP_I93_GET_MULTI_BLOCK_STATUS:
3146       event = NFA_I93_CMD_CPLT_EVT;
3147       break;
3148     default:
3149       return (freebuf);
3150   }
3151   nfa_dm_act_conn_cback_notify(event, &conn_evt_data);
3152 
3153   return (freebuf);
3154 }
3155 
3156 /*******************************************************************************
3157 **
3158 ** Function         nfa_rw_command_complete
3159 **
3160 ** Description      Handle command complete: clear the busy flag,
3161 **                  and start the presence check timer if applicable.
3162 **
3163 ** Returns          None
3164 **
3165 *******************************************************************************/
nfa_rw_command_complete(void)3166 void nfa_rw_command_complete(void) {
3167   /* Clear the busy flag */
3168   nfa_rw_cb.flags &= ~NFA_RW_FL_API_BUSY;
3169 
3170   /* Restart presence_check timer */
3171   nfa_rw_check_start_presence_check_timer(NFA_RW_PRESENCE_CHECK_INTERVAL);
3172 }
3173