• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 functions that interface with the NFC NCI transport.
22  *  On the receive side, it routes events to the appropriate handler
23  *  (callback). On the transmit side, it manages the command transmission.
24  *
25  ******************************************************************************/
26 #include <string.h>
27 
28 #include <android-base/stringprintf.h>
29 #include <android/hardware/nfc/1.1/types.h>
30 #include <base/logging.h>
31 
32 #include "nfc_target.h"
33 
34 #include "bt_types.h"
35 #include "ce_int.h"
36 #include "gki.h"
37 #include "nci_hmsgs.h"
38 #include "nfc_int.h"
39 #include "rw_int.h"
40 
41 #if (NFC_RW_ONLY == FALSE)
42 
43 #include "llcp_int.h"
44 
45 /* NFC mandates support for at least one logical connection;
46  * Update max_conn to the NFCC capability on InitRsp */
47 #define NFC_SET_MAX_CONN_DEFAULT() \
48   { nfc_cb.max_conn = 1; }
49 
50 #else /* NFC_RW_ONLY */
51 #define ce_init()
52 #define llcp_init()
53 
54 #define NFC_SET_MAX_CONN_DEFAULT()
55 
56 #endif /* NFC_RW_ONLY */
57 
58 using android::base::StringPrintf;
59 using android::hardware::nfc::V1_1::NfcEvent;
60 
61 extern bool nfc_debug_enabled;
62 extern void delete_stack_non_volatile_store(bool forceDelete);
63 
64 /****************************************************************************
65 ** Declarations
66 ****************************************************************************/
67 tNFC_CB nfc_cb;
68 
69 #if (NFC_RW_ONLY == FALSE)
70 #define NFC_NUM_INTERFACE_MAP 2
71 #else
72 #define NFC_NUM_INTERFACE_MAP 1
73 #endif
74 
75 static const tNCI_DISCOVER_MAPS nfc_interface_mapping[NFC_NUM_INTERFACE_MAP] = {
76     /* Protocols that use Frame Interface do not need to be included in the
77        interface mapping */
78     {NCI_PROTOCOL_ISO_DEP, NCI_INTERFACE_MODE_POLL_N_LISTEN,
79      NCI_INTERFACE_ISO_DEP}
80 #if (NFC_RW_ONLY == FALSE)
81     ,
82     /* this can not be set here due to 2079xB0 NFCC issues */
83     {NCI_PROTOCOL_NFC_DEP, NCI_INTERFACE_MODE_POLL_N_LISTEN,
84      NCI_INTERFACE_NFC_DEP}
85 #endif
86 };
87 
88 /*******************************************************************************
89 **
90 ** Function         nfc_state_name
91 **
92 ** Description      This function returns the state name.
93 **
94 ** NOTE             conditionally compiled to save memory.
95 **
96 ** Returns          pointer to the name
97 **
98 *******************************************************************************/
nfc_state_name(uint8_t state)99 static std::string nfc_state_name(uint8_t state) {
100   switch (state) {
101     case NFC_STATE_NONE:
102       return "NONE";
103     case NFC_STATE_W4_HAL_OPEN:
104       return "W4_HAL_OPEN";
105     case NFC_STATE_CORE_INIT:
106       return "CORE_INIT";
107     case NFC_STATE_W4_POST_INIT_CPLT:
108       return "W4_POST_INIT_CPLT";
109     case NFC_STATE_IDLE:
110       return "IDLE";
111     case NFC_STATE_OPEN:
112       return "OPEN";
113     case NFC_STATE_CLOSING:
114       return "CLOSING";
115     case NFC_STATE_W4_HAL_CLOSE:
116       return "W4_HAL_CLOSE";
117     case NFC_STATE_NFCC_POWER_OFF_SLEEP:
118       return "NFCC_POWER_OFF_SLEEP";
119     default:
120       return "???? UNKNOWN STATE";
121   }
122 }
123 
124 /*******************************************************************************
125 **
126 ** Function         nfc_hal_event_name
127 **
128 ** Description      This function returns the HAL event name.
129 **
130 ** NOTE             conditionally compiled to save memory.
131 **
132 ** Returns          pointer to the name
133 **
134 *******************************************************************************/
nfc_hal_event_name(uint8_t event)135 static std::string nfc_hal_event_name(uint8_t event) {
136   switch (event) {
137     case HAL_NFC_OPEN_CPLT_EVT:
138       return "HAL_NFC_OPEN_CPLT_EVT";
139     case HAL_NFC_CLOSE_CPLT_EVT:
140       return "HAL_NFC_CLOSE_CPLT_EVT";
141     case HAL_NFC_POST_INIT_CPLT_EVT:
142       return "HAL_NFC_POST_INIT_CPLT_EVT";
143     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
144       return "HAL_NFC_PRE_DISCOVER_CPLT_EVT";
145     case HAL_NFC_REQUEST_CONTROL_EVT:
146       return "HAL_NFC_REQUEST_CONTROL_EVT";
147     case HAL_NFC_RELEASE_CONTROL_EVT:
148       return "HAL_NFC_RELEASE_CONTROL_EVT";
149     case HAL_NFC_ERROR_EVT:
150       return "HAL_NFC_ERROR_EVT";
151     case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
152       return "HCI_NETWORK_RESET";
153     default:
154       return "???? UNKNOWN EVENT";
155   }
156 }
157 
158 /*******************************************************************************
159 **
160 ** Function         nfc_main_notify_enable_status
161 **
162 ** Description      Notify status of Enable/PowerOffSleep/PowerCycle
163 **
164 *******************************************************************************/
nfc_main_notify_enable_status(tNFC_STATUS nfc_status)165 static void nfc_main_notify_enable_status(tNFC_STATUS nfc_status) {
166   tNFC_RESPONSE evt_data;
167 
168   evt_data.status = nfc_status;
169 
170   if (nfc_cb.p_resp_cback) {
171     /* if getting out of PowerOffSleep mode or restarting NFCC */
172     if (nfc_cb.flags & (NFC_FL_RESTARTING | NFC_FL_POWER_CYCLE_NFCC)) {
173       nfc_cb.flags &= ~(NFC_FL_RESTARTING | NFC_FL_POWER_CYCLE_NFCC);
174       if (nfc_status != NFC_STATUS_OK) {
175         nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
176       }
177       (*nfc_cb.p_resp_cback)(NFC_NFCC_RESTART_REVT, &evt_data);
178     } else {
179       (*nfc_cb.p_resp_cback)(NFC_ENABLE_REVT, &evt_data);
180     }
181   }
182 }
183 
184 /*******************************************************************************
185 **
186 ** Function         nfc_enabled
187 **
188 ** Description      NFCC enabled, proceed with stack start up.
189 **
190 ** Returns          void
191 **
192 *******************************************************************************/
nfc_enabled(tNFC_STATUS nfc_status,NFC_HDR * p_init_rsp_msg)193 void nfc_enabled(tNFC_STATUS nfc_status, NFC_HDR* p_init_rsp_msg) {
194   tNFC_RESPONSE evt_data;
195   tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
196   uint8_t* p;
197   uint8_t num_interfaces = 0, xx;
198   uint8_t num_interface_extensions = 0, zz;
199   uint8_t interface_type;
200   int yy = 0;
201   memset(&evt_data, 0, sizeof(tNFC_RESPONSE));
202 
203   if (nfc_status == NCI_STATUS_OK) {
204     nfc_set_state(NFC_STATE_IDLE);
205 
206     p = (uint8_t*)(p_init_rsp_msg + 1) + p_init_rsp_msg->offset +
207         NCI_MSG_HDR_SIZE + 1;
208     /* we currently only support NCI of the same version.
209     * We may need to change this, when we support multiple version of NFCC */
210 
211     evt_data.enable.nci_version = nfc_cb.nci_version;
212     STREAM_TO_UINT32(evt_data.enable.nci_features, p);
213     if (nfc_cb.nci_version == NCI_VERSION_1_0) {
214       STREAM_TO_UINT8(num_interfaces, p);
215       evt_data.enable.nci_interfaces = 0;
216       for (xx = 0; xx < num_interfaces; xx++) {
217         if ((*p) <= NCI_INTERFACE_MAX)
218           evt_data.enable.nci_interfaces |= (1 << (*p));
219         else if (((*p) >= NCI_INTERFACE_FIRST_VS) &&
220                  (yy < NFC_NFCC_MAX_NUM_VS_INTERFACE)) {
221           /* save the VS RF interface in control block, if there's still room */
222           nfc_cb.vs_interface[yy++] = *p;
223         }
224         p++;
225       }
226       nfc_cb.nci_interfaces = evt_data.enable.nci_interfaces;
227       memcpy(evt_data.enable.vs_interface, nfc_cb.vs_interface,
228              NFC_NFCC_MAX_NUM_VS_INTERFACE);
229     }
230     evt_data.enable.max_conn = *p++;
231     STREAM_TO_UINT16(evt_data.enable.max_ce_table, p);
232 #if (NFC_RW_ONLY == FALSE)
233     nfc_cb.max_ce_table = evt_data.enable.max_ce_table;
234     nfc_cb.nci_features = evt_data.enable.nci_features;
235     nfc_cb.max_conn = evt_data.enable.max_conn;
236 #endif
237     nfc_cb.nci_ctrl_size = *p++; /* Max Control Packet Payload Length */
238     p_cb->init_credits = p_cb->num_buff = 0;
239     nfc_set_conn_id(p_cb, NFC_RF_CONN_ID);
240     if (nfc_cb.nci_version == NCI_VERSION_2_0) {
241       if (evt_data.enable.nci_features & NCI_FEAT_HCI_NETWORK) {
242         p_cb = &nfc_cb.conn_cb[NFC_HCI_CONN_ID];
243         nfc_set_conn_id(p_cb, NFC_HCI_CONN_ID);
244         p_cb->id = NFC_HCI_CONN_ID;
245         STREAM_TO_UINT8(p_cb->buff_size, p);
246         STREAM_TO_UINT8(p_cb->num_buff, p);
247         p_cb->init_credits = p_cb->num_buff;
248         evt_data.enable.hci_packet_size = p_cb->buff_size;
249         evt_data.enable.hci_conn_credits = p_cb->init_credits;
250         DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
251             "hci num_buf=%d buf_size=%d", p_cb->num_buff, p_cb->buff_size);
252       } else {
253         /*HCI n/w not enabled skip data buff size and data credit HCI conn */
254         p += 2;
255       }
256       STREAM_TO_UINT16(evt_data.enable.max_nfc_v_size, p);
257       STREAM_TO_UINT8(num_interfaces, p);
258 #if (NFC_RW_ONLY == FALSE)
259       nfc_cb.hci_packet_size = evt_data.enable.hci_packet_size;
260       nfc_cb.hci_conn_credits = evt_data.enable.hci_conn_credits;
261       nfc_cb.nci_max_v_size = evt_data.enable.max_nfc_v_size;
262 #endif
263       evt_data.enable.nci_interfaces = 0;
264 
265       for (xx = 0; xx < num_interfaces; xx++) {
266         if ((*p) <= NCI_INTERFACE_MAX)
267           evt_data.enable.nci_interfaces |= (1 << (*p));
268         else if (((*p) >= NCI_INTERFACE_FIRST_VS) &&
269                  (yy < NFC_NFCC_MAX_NUM_VS_INTERFACE)) {
270           /* save the VS RF interface in control block, if there's still room */
271           nfc_cb.vs_interface[yy++] = *p;
272         }
273         interface_type = *p++;
274         num_interface_extensions = *p++;
275         for (zz = 0; zz < num_interface_extensions; zz++) {
276           if (((*p) < NCI_INTERFACE_EXTENSION_MAX) &&
277               (interface_type <= NCI_INTERFACE_MAX)) {
278             nfc_cb.nci_intf_extensions |= (1 << (*p));
279             nfc_cb.nci_intf_extension_map[*p] = (1 << interface_type);
280           }
281           p++;
282         }
283       }
284 
285       nfc_cb.nci_interfaces = evt_data.enable.nci_interfaces;
286       memcpy(evt_data.enable.vs_interface, nfc_cb.vs_interface,
287              NFC_NFCC_MAX_NUM_VS_INTERFACE);
288     } else {
289       STREAM_TO_UINT16(evt_data.enable.max_param_size, p);
290       evt_data.enable.manufacture_id = *p++;
291       STREAM_TO_ARRAY(evt_data.enable.nfcc_info, p, NFC_NFCC_INFO_LEN);
292     }
293     NFC_DiscoveryMap(nfc_cb.num_disc_maps,
294                      (tNCI_DISCOVER_MAPS*)nfc_cb.p_disc_maps, nullptr);
295   }
296   /* else not successful. the buffers will be freed in nfc_free_conn_cb () */
297   else {
298     if (nfc_cb.flags & NFC_FL_RESTARTING) {
299       nfc_set_state(NFC_STATE_NFCC_POWER_OFF_SLEEP);
300     } else {
301       nfc_free_conn_cb(p_cb);
302 
303       /* if NFCC didn't respond to CORE_RESET or CORE_INIT */
304       if (nfc_cb.nfc_state == NFC_STATE_CORE_INIT) {
305         /* report status after closing HAL */
306         nfc_cb.p_hal->close();
307         return;
308       } else
309         nfc_set_state(NFC_STATE_NONE);
310     }
311   }
312 
313   nfc_main_notify_enable_status(nfc_status);
314 }
315 
316 /*******************************************************************************
317 **
318 ** Function         nfc_set_state
319 **
320 ** Description      Set the state of NFC stack
321 **
322 ** Returns          void
323 **
324 *******************************************************************************/
nfc_set_state(tNFC_STATE nfc_state)325 void nfc_set_state(tNFC_STATE nfc_state) {
326   DLOG_IF(INFO, nfc_debug_enabled)
327       << StringPrintf("nfc_set_state %d (%s)->%d (%s)", nfc_cb.nfc_state,
328                       nfc_state_name(nfc_cb.nfc_state).c_str(), nfc_state,
329                       nfc_state_name(nfc_state).c_str());
330   nfc_cb.nfc_state = nfc_state;
331 }
332 
333 /*******************************************************************************
334 **
335 ** Function         nfc_gen_cleanup
336 **
337 ** Description      Clean up for both going into low power mode and disabling
338 **                  NFC
339 **
340 *******************************************************************************/
nfc_gen_cleanup(void)341 void nfc_gen_cleanup(void) {
342   nfc_cb.flags &= ~NFC_FL_DEACTIVATING;
343 
344   /* the HAL pre-discover is still active - clear the pending flag/free the
345    * buffer */
346   if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING) {
347     nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
348     GKI_freebuf(nfc_cb.p_disc_pending);
349     nfc_cb.p_disc_pending = nullptr;
350   }
351 
352   nfc_cb.flags &= ~(NFC_FL_CONTROL_REQUESTED | NFC_FL_CONTROL_GRANTED |
353                     NFC_FL_HAL_REQUESTED);
354 
355   nfc_stop_timer(&nfc_cb.deactivate_timer);
356 
357   /* Reset the connection control blocks */
358   nfc_reset_all_conn_cbs();
359 
360   if (nfc_cb.p_nci_init_rsp) {
361     GKI_freebuf(nfc_cb.p_nci_init_rsp);
362     nfc_cb.p_nci_init_rsp = nullptr;
363   }
364 
365   /* clear any pending CMD/RSP */
366   nfc_main_flush_cmd_queue();
367 }
368 
369 /*******************************************************************************
370 **
371 ** Function         nfc_main_handle_hal_evt
372 **
373 ** Description      Handle BT_EVT_TO_NFC_MSGS
374 **
375 *******************************************************************************/
nfc_main_handle_hal_evt(tNFC_HAL_EVT_MSG * p_msg)376 void nfc_main_handle_hal_evt(tNFC_HAL_EVT_MSG* p_msg) {
377   uint8_t* ps;
378 
379   DLOG_IF(INFO, nfc_debug_enabled)
380       << StringPrintf("HAL event=0x%x", p_msg->hal_evt);
381 
382   switch (p_msg->hal_evt) {
383     case HAL_NFC_OPEN_CPLT_EVT: /* only for failure case */
384       nfc_enabled(NFC_STATUS_FAILED, nullptr);
385       break;
386 
387     case HAL_NFC_CLOSE_CPLT_EVT:
388       if (nfc_cb.p_resp_cback) {
389         if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE) {
390           if (nfc_cb.flags & NFC_FL_POWER_OFF_SLEEP) {
391             nfc_cb.flags &= ~NFC_FL_POWER_OFF_SLEEP;
392             nfc_set_state(NFC_STATE_NFCC_POWER_OFF_SLEEP);
393             (*nfc_cb.p_resp_cback)(NFC_NFCC_POWER_OFF_REVT, nullptr);
394           } else {
395             nfc_set_state(NFC_STATE_NONE);
396             (*nfc_cb.p_resp_cback)(NFC_DISABLE_REVT, nullptr);
397             nfc_cb.p_resp_cback = nullptr;
398           }
399         } else {
400           /* found error during initialization */
401           nfc_set_state(NFC_STATE_NONE);
402           nfc_main_notify_enable_status(NFC_STATUS_FAILED);
403         }
404       }
405       break;
406 
407     case HAL_NFC_POST_INIT_CPLT_EVT:
408       if (nfc_cb.p_nci_init_rsp) {
409         /*
410         ** if NFC_Disable() is called before receiving
411         ** HAL_NFC_POST_INIT_CPLT_EVT, then wait for HAL_NFC_CLOSE_CPLT_EVT.
412         */
413         if (nfc_cb.nfc_state == NFC_STATE_W4_POST_INIT_CPLT) {
414           if (p_msg->status == HAL_NFC_STATUS_OK) {
415             nfc_enabled(NCI_STATUS_OK, nfc_cb.p_nci_init_rsp);
416           } else /* if post initailization failed */
417           {
418             nfc_enabled(NCI_STATUS_FAILED, nullptr);
419           }
420         }
421 
422         GKI_freebuf(nfc_cb.p_nci_init_rsp);
423         nfc_cb.p_nci_init_rsp = nullptr;
424       }
425       break;
426 
427     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
428       /* restore the command window, no matter if the discover command is still
429        * pending */
430       nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
431       nfc_cb.flags &= ~NFC_FL_CONTROL_GRANTED;
432       if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING) {
433         /* issue the discovery command now, if it is still pending */
434         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
435         ps = (uint8_t*)nfc_cb.p_disc_pending;
436         nci_snd_discover_cmd(*ps, (tNFC_DISCOVER_PARAMS*)(ps + 1));
437         GKI_freebuf(nfc_cb.p_disc_pending);
438         nfc_cb.p_disc_pending = nullptr;
439       } else {
440         /* check if there's other pending commands */
441         nfc_ncif_check_cmd_queue(nullptr);
442       }
443 
444       if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
445         nfc_ncif_event_status(NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
446       break;
447 
448     case HAL_NFC_REQUEST_CONTROL_EVT:
449       nfc_cb.flags |= NFC_FL_CONTROL_REQUESTED;
450       nfc_cb.flags |= NFC_FL_HAL_REQUESTED;
451       nfc_ncif_check_cmd_queue(nullptr);
452       break;
453 
454     case HAL_NFC_RELEASE_CONTROL_EVT:
455       if (nfc_cb.flags & NFC_FL_CONTROL_GRANTED) {
456         nfc_cb.flags &= ~NFC_FL_CONTROL_GRANTED;
457         nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
458         nfc_ncif_check_cmd_queue(nullptr);
459 
460         if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
461           nfc_ncif_event_status(NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
462       }
463       break;
464 
465     case HAL_NFC_ERROR_EVT:
466       switch (p_msg->status) {
467         case HAL_NFC_STATUS_ERR_TRANSPORT:
468           /* Notify app of transport error */
469           if (nfc_cb.p_resp_cback) {
470             (*nfc_cb.p_resp_cback)(NFC_NFCC_TRANSPORT_ERR_REVT, nullptr);
471 
472             /* if enabling NFC, notify upper layer of failure after closing HAL
473              */
474             if (nfc_cb.nfc_state < NFC_STATE_IDLE) {
475               nfc_enabled(NFC_STATUS_FAILED, nullptr);
476             }
477           }
478           break;
479 
480         case HAL_NFC_STATUS_ERR_CMD_TIMEOUT:
481           nfc_ncif_event_status(NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
482 
483           /* if enabling NFC, notify upper layer of failure after closing HAL */
484           if (nfc_cb.nfc_state < NFC_STATE_IDLE) {
485             nfc_enabled(NFC_STATUS_FAILED, nullptr);
486             return;
487           }
488           break;
489 
490         case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
491           delete_stack_non_volatile_store(true);
492           break;
493 
494         default:
495           break;
496       }
497       break;
498 
499     default:
500       LOG(ERROR) << StringPrintf("unhandled event (0x%x).", p_msg->hal_evt);
501       break;
502   }
503 }
504 
505 /*******************************************************************************
506 **
507 ** Function         nfc_main_flush_cmd_queue
508 **
509 ** Description      This function is called when setting power off sleep state.
510 **
511 ** Returns          void
512 **
513 *******************************************************************************/
nfc_main_flush_cmd_queue(void)514 void nfc_main_flush_cmd_queue(void) {
515   NFC_HDR* p_msg;
516 
517   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
518 
519   /* initialize command window */
520   nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
521 
522   /* Stop command-pending timer */
523   nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
524 
525   /* dequeue and free buffer */
526   while ((p_msg = (NFC_HDR*)GKI_dequeue(&nfc_cb.nci_cmd_xmit_q)) != nullptr) {
527     GKI_freebuf(p_msg);
528   }
529 }
530 
531 /*******************************************************************************
532 **
533 ** Function         nfc_main_post_hal_evt
534 **
535 ** Description      This function posts HAL event to NFC_TASK
536 **
537 ** Returns          void
538 **
539 *******************************************************************************/
nfc_main_post_hal_evt(uint8_t hal_evt,tHAL_NFC_STATUS status)540 void nfc_main_post_hal_evt(uint8_t hal_evt, tHAL_NFC_STATUS status) {
541   tNFC_HAL_EVT_MSG* p_msg;
542 
543   p_msg = (tNFC_HAL_EVT_MSG*)GKI_getbuf(sizeof(tNFC_HAL_EVT_MSG));
544   if (p_msg != nullptr) {
545     /* Initialize NFC_HDR */
546     p_msg->hdr.len = 0;
547     p_msg->hdr.event = BT_EVT_TO_NFC_MSGS;
548     p_msg->hdr.offset = 0;
549     p_msg->hdr.layer_specific = 0;
550     p_msg->hal_evt = hal_evt;
551     p_msg->status = status;
552     GKI_send_msg(NFC_TASK, NFC_MBOX_ID, p_msg);
553   } else {
554     LOG(ERROR) << StringPrintf("No buffer");
555   }
556 }
557 
558 /*******************************************************************************
559 **
560 ** Function         nfc_main_hal_cback
561 **
562 ** Description      HAL event handler
563 **
564 ** Returns          void
565 **
566 *******************************************************************************/
nfc_main_hal_cback(uint8_t event,tHAL_NFC_STATUS status)567 static void nfc_main_hal_cback(uint8_t event, tHAL_NFC_STATUS status) {
568   DLOG_IF(INFO, nfc_debug_enabled)
569       << StringPrintf("nfc_main_hal_cback event: %s(0x%x), status=%d",
570                       nfc_hal_event_name(event).c_str(), event, status);
571 
572   switch (event) {
573     case HAL_NFC_OPEN_CPLT_EVT:
574       /*
575       ** if NFC_Disable() is called before receiving HAL_NFC_OPEN_CPLT_EVT,
576       ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
577       */
578       if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_OPEN) {
579         if (status == HAL_NFC_STATUS_OK) {
580           /* Notify NFC_TASK that NCI tranport is initialized */
581           GKI_send_event(NFC_TASK, NFC_TASK_EVT_TRANSPORT_READY);
582         } else {
583           nfc_main_post_hal_evt(event, status);
584         }
585       }
586       break;
587 
588     case HAL_NFC_CLOSE_CPLT_EVT:
589     case HAL_NFC_POST_INIT_CPLT_EVT:
590     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
591     case HAL_NFC_REQUEST_CONTROL_EVT:
592     case HAL_NFC_RELEASE_CONTROL_EVT:
593     case HAL_NFC_ERROR_EVT:
594     case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
595       nfc_main_post_hal_evt(event, status);
596       break;
597 
598     default:
599       DLOG_IF(INFO, nfc_debug_enabled)
600           << StringPrintf("nfc_main_hal_cback unhandled event %x", event);
601       break;
602   }
603 }
604 
605 /*******************************************************************************
606 **
607 ** Function         nfc_main_hal_data_cback
608 **
609 ** Description      HAL data event handler
610 **
611 ** Returns          void
612 **
613 *******************************************************************************/
nfc_main_hal_data_cback(uint16_t data_len,uint8_t * p_data)614 static void nfc_main_hal_data_cback(uint16_t data_len, uint8_t* p_data) {
615   NFC_HDR* p_msg;
616 
617   /* ignore all data while shutting down NFCC */
618   if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE) {
619     return;
620   }
621 
622   if (p_data) {
623     p_msg = (NFC_HDR*)GKI_getpoolbuf(NFC_NCI_POOL_ID);
624     if (p_msg != nullptr) {
625       /* Initialize NFC_HDR */
626       p_msg->len = data_len;
627       p_msg->event = BT_EVT_TO_NFC_NCI;
628       p_msg->offset = NFC_RECEIVE_MSGS_OFFSET;
629 
630       /* no need to check length, it always less than pool size */
631       memcpy((uint8_t*)(p_msg + 1) + p_msg->offset, p_data, p_msg->len);
632 
633       GKI_send_msg(NFC_TASK, NFC_MBOX_ID, p_msg);
634     } else {
635       LOG(ERROR) << StringPrintf("No buffer");
636     }
637   }
638 }
639 
640 /*******************************************************************************
641 **
642 ** Function         NFC_Enable
643 **
644 ** Description      This function enables NFC. Prior to calling NFC_Enable:
645 **                  - the NFCC must be powered up, and ready to receive
646 **                    commands.
647 **                  - GKI must be enabled
648 **                  - NFC_TASK must be started
649 **                  - NCIT_TASK must be started (if using dedicated NCI
650 **                    transport)
651 **
652 **                  This function opens the NCI transport (if applicable),
653 **                  resets the NFC controller, and initializes the NFC
654 **                  subsystems.
655 **
656 **                  When the NFC startup procedure is completed, an
657 **                  NFC_ENABLE_REVT is returned to the application using the
658 **                  tNFC_RESPONSE_CBACK.
659 **
660 ** Returns          tNFC_STATUS
661 **
662 *******************************************************************************/
NFC_Enable(tNFC_RESPONSE_CBACK * p_cback)663 tNFC_STATUS NFC_Enable(tNFC_RESPONSE_CBACK* p_cback) {
664   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
665 
666   /* Validate callback */
667   if (!p_cback) {
668     return (NFC_STATUS_INVALID_PARAM);
669   }
670   nfc_cb.p_resp_cback = p_cback;
671 
672   /* Open HAL transport. */
673   nfc_set_state(NFC_STATE_W4_HAL_OPEN);
674   nfc_cb.p_hal->open(nfc_main_hal_cback, nfc_main_hal_data_cback);
675 
676   return (NFC_STATUS_OK);
677 }
678 
679 /*******************************************************************************
680 **
681 ** Function         NFC_Disable
682 **
683 ** Description      This function performs clean up routines for shutting down
684 **                  NFC and closes the NCI transport (if using dedicated NCI
685 **                  transport).
686 **
687 **                  When the NFC shutdown procedure is completed, an
688 **                  NFC_DISABLED_REVT is returned to the application using the
689 **                  tNFC_RESPONSE_CBACK.
690 **
691 ** Returns          nothing
692 **
693 *******************************************************************************/
NFC_Disable(void)694 void NFC_Disable(void) {
695   DLOG_IF(INFO, nfc_debug_enabled)
696       << StringPrintf("nfc_state = %d", nfc_cb.nfc_state);
697 
698   if ((nfc_cb.nfc_state == NFC_STATE_NONE) ||
699       (nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP)) {
700     nfc_set_state(NFC_STATE_NONE);
701     if (nfc_cb.p_resp_cback) {
702       (*nfc_cb.p_resp_cback)(NFC_DISABLE_REVT, nullptr);
703       nfc_cb.p_resp_cback = nullptr;
704     }
705     return;
706   }
707 
708   /* Close transport and clean up */
709   nfc_task_shutdown_nfcc();
710 }
711 
712 /*******************************************************************************
713 **
714 ** Function         NFC_Init
715 **
716 ** Description      This function initializes control block for NFC
717 **
718 ** Returns          nothing
719 **
720 *******************************************************************************/
NFC_Init(tHAL_NFC_ENTRY * p_hal_entry_tbl)721 void NFC_Init(tHAL_NFC_ENTRY* p_hal_entry_tbl) {
722   int xx;
723 
724   /* Clear nfc control block */
725   memset(&nfc_cb, 0, sizeof(tNFC_CB));
726 
727   /* Reset the nfc control block */
728   for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++) {
729     nfc_cb.conn_cb[xx].conn_id = NFC_ILLEGAL_CONN_ID;
730   }
731 
732   /* NCI init */
733   nfc_cb.p_hal = p_hal_entry_tbl;
734   nfc_cb.nfc_state = NFC_STATE_NONE;
735   nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
736   nfc_cb.nci_wait_rsp_tout = NFC_CMD_CMPL_TIMEOUT;
737   nfc_cb.p_disc_maps = nfc_interface_mapping;
738   nfc_cb.num_disc_maps = NFC_NUM_INTERFACE_MAP;
739   nfc_cb.nci_ctrl_size = NCI_CTRL_INIT_SIZE;
740   nfc_cb.reassembly = true;
741   nfc_cb.nci_version = NCI_VERSION_UNKNOWN;
742   rw_init();
743   ce_init();
744   llcp_init();
745   NFC_SET_MAX_CONN_DEFAULT();
746 }
747 
748 /*******************************************************************************
749 **
750 ** Function         NFC_GetLmrtSize
751 **
752 ** Description      Called by application wto query the Listen Mode Routing
753 **                  Table size supported by NFCC
754 **
755 ** Returns          Listen Mode Routing Table size
756 **
757 *******************************************************************************/
NFC_GetLmrtSize(void)758 uint16_t NFC_GetLmrtSize(void) {
759   uint16_t size = 0;
760 #if (NFC_RW_ONLY == FALSE)
761   size = nfc_cb.max_ce_table;
762 #endif
763   return size;
764 }
765 
766 /*******************************************************************************
767 **
768 ** Function         NFC_SetConfig
769 **
770 ** Description      This function is called to send the configuration parameter
771 **                  TLV to NFCC. The response from NFCC is reported by
772 **                  tNFC_RESPONSE_CBACK as NFC_SET_CONFIG_REVT.
773 **
774 ** Parameters       tlv_size - the length of p_param_tlvs.
775 **                  p_param_tlvs - the parameter ID/Len/Value list
776 **
777 ** Returns          tNFC_STATUS
778 **
779 *******************************************************************************/
NFC_SetConfig(uint8_t tlv_size,uint8_t * p_param_tlvs)780 tNFC_STATUS NFC_SetConfig(uint8_t tlv_size, uint8_t* p_param_tlvs) {
781   return nci_snd_core_set_config(p_param_tlvs, tlv_size);
782 }
783 
784 /*******************************************************************************
785 **
786 ** Function         NFC_GetConfig
787 **
788 ** Description      This function is called to retrieve the parameter TLV from
789 **                  NFCC. The response from NFCC is reported by
790 **                  tNFC_RESPONSE_CBACK as NFC_GET_CONFIG_REVT.
791 **
792 ** Parameters       num_ids - the number of parameter IDs
793 **                  p_param_ids - the parameter ID list.
794 **
795 ** Returns          tNFC_STATUS
796 **
797 *******************************************************************************/
NFC_GetConfig(uint8_t num_ids,uint8_t * p_param_ids)798 tNFC_STATUS NFC_GetConfig(uint8_t num_ids, uint8_t* p_param_ids) {
799   return nci_snd_core_get_config(p_param_ids, num_ids);
800 }
801 
802 /*******************************************************************************
803 **
804 ** Function         NFC_DiscoveryMap
805 **
806 ** Description      This function is called to set the discovery interface
807 **                  mapping. The response from NFCC is reported by
808 **                  tNFC_DISCOVER_CBACK as NFC_MAP_DEVT.
809 **
810 ** Parameters       num - the number of items in p_params.
811 **                  p_maps - the discovery interface mappings
812 **                  p_cback - the discovery callback function
813 **
814 ** Returns          tNFC_STATUS
815 **
816 *******************************************************************************/
NFC_DiscoveryMap(uint8_t num,tNFC_DISCOVER_MAPS * p_maps,tNFC_DISCOVER_CBACK * p_cback)817 tNFC_STATUS NFC_DiscoveryMap(uint8_t num, tNFC_DISCOVER_MAPS* p_maps,
818                              tNFC_DISCOVER_CBACK* p_cback) {
819   uint8_t num_disc_maps = num;
820   uint8_t xx, yy, num_intf, intf_mask;
821   tNFC_DISCOVER_MAPS
822       max_maps[NFC_NFCC_MAX_NUM_VS_INTERFACE + NCI_INTERFACE_MAX];
823   bool is_supported;
824 
825   nfc_cb.p_discv_cback = p_cback;
826   num_intf = 0;
827   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
828       "nci_interfaces supported by NFCC: 0x%x", nfc_cb.nci_interfaces);
829 
830   for (xx = 0; xx < NFC_NFCC_MAX_NUM_VS_INTERFACE + NCI_INTERFACE_MAX; xx++) {
831     memset(&max_maps[xx], 0x00, sizeof(tNFC_DISCOVER_MAPS));
832   }
833 
834   for (xx = 0; xx < num_disc_maps; xx++) {
835     is_supported = false;
836     if (p_maps[xx].intf_type > NCI_INTERFACE_MAX) {
837       for (yy = 0; yy < NFC_NFCC_MAX_NUM_VS_INTERFACE; yy++) {
838         if (nfc_cb.vs_interface[yy] == p_maps[xx].intf_type)
839           is_supported = true;
840       }
841       DLOG_IF(INFO, nfc_debug_enabled)
842           << StringPrintf("[%d]: vs intf_type:0x%x is_supported:%d", xx,
843                           p_maps[xx].intf_type, is_supported);
844     } else {
845       intf_mask = (1 << (p_maps[xx].intf_type));
846       if (intf_mask & nfc_cb.nci_interfaces) {
847         is_supported = true;
848       }
849       DLOG_IF(INFO, nfc_debug_enabled)
850           << StringPrintf("[%d]: intf_type:%d intf_mask: 0x%x is_supported:%d",
851                           xx, p_maps[xx].intf_type, intf_mask, is_supported);
852     }
853     if (is_supported)
854       memcpy(&max_maps[num_intf++], &p_maps[xx], sizeof(tNFC_DISCOVER_MAPS));
855     else {
856       LOG(WARNING) << StringPrintf(
857           "NFC_DiscoveryMap interface=0x%x is not supported by NFCC",
858           p_maps[xx].intf_type);
859     }
860   }
861 
862   return nci_snd_discover_map_cmd(num_intf, (tNCI_DISCOVER_MAPS*)max_maps);
863 }
864 
865 /*******************************************************************************
866 **
867 ** Function         NFC_DiscoveryStart
868 **
869 ** Description      This function is called to start Polling and/or Listening.
870 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as
871 **                  NFC_START_DEVT. The notification from NFCC is reported by
872 **                  tNFC_DISCOVER_CBACK as NFC_RESULT_DEVT.
873 **
874 ** Parameters       num_params - the number of items in p_params.
875 **                  p_params - the discovery parameters
876 **                  p_cback - the discovery callback function
877 **
878 ** Returns          tNFC_STATUS
879 **
880 *******************************************************************************/
NFC_DiscoveryStart(uint8_t num_params,tNFC_DISCOVER_PARAMS * p_params,tNFC_DISCOVER_CBACK * p_cback)881 tNFC_STATUS NFC_DiscoveryStart(uint8_t num_params,
882                                tNFC_DISCOVER_PARAMS* p_params,
883                                tNFC_DISCOVER_CBACK* p_cback) {
884   uint8_t* p;
885   int params_size;
886   tNFC_STATUS status = NFC_STATUS_NO_BUFFERS;
887 
888   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
889   if (nfc_cb.p_disc_pending) {
890     LOG(ERROR) << StringPrintf("There's pending NFC_DiscoveryStart");
891     status = NFC_STATUS_BUSY;
892   } else {
893     nfc_cb.p_discv_cback = p_cback;
894     nfc_cb.flags |= NFC_FL_DISCOVER_PENDING;
895     nfc_cb.flags |= NFC_FL_CONTROL_REQUESTED;
896     params_size = sizeof(tNFC_DISCOVER_PARAMS) * num_params;
897     nfc_cb.p_disc_pending =
898         GKI_getbuf((uint16_t)(NFC_HDR_SIZE + 1 + params_size));
899     if (nfc_cb.p_disc_pending) {
900       p = (uint8_t*)nfc_cb.p_disc_pending;
901       *p++ = num_params;
902       memcpy(p, p_params, params_size);
903       status = NFC_STATUS_CMD_STARTED;
904       nfc_ncif_check_cmd_queue(nullptr);
905     }
906   }
907 
908   DLOG_IF(INFO, nfc_debug_enabled)
909       << StringPrintf("NFC_DiscoveryStart status: 0x%x", status);
910   return status;
911 }
912 
913 /*******************************************************************************
914 **
915 ** Function         NFC_DiscoverySelect
916 **
917 ** Description      If tNFC_DISCOVER_CBACK reports status=NFC_MULTIPLE_PROT,
918 **                  the application needs to use this function to select the
919 **                  the logical endpoint to continue. The response from NFCC is
920 **                  reported by tNFC_DISCOVER_CBACK as NFC_SELECT_DEVT.
921 **
922 ** Parameters       rf_disc_id - The ID identifies the remote device.
923 **                  protocol - the logical endpoint on the remote devide
924 **                  rf_interface - the RF interface to communicate with NFCC
925 **
926 ** Returns          tNFC_STATUS
927 **
928 *******************************************************************************/
NFC_DiscoverySelect(uint8_t rf_disc_id,uint8_t protocol,uint8_t rf_interface)929 tNFC_STATUS NFC_DiscoverySelect(uint8_t rf_disc_id, uint8_t protocol,
930                                 uint8_t rf_interface) {
931   return nci_snd_discover_select_cmd(rf_disc_id, protocol, rf_interface);
932 }
933 
934 /*******************************************************************************
935 **
936 ** Function         NFC_ConnCreate
937 **
938 ** Description      This function is called to create a logical connection with
939 **                  NFCC for data exchange.
940 **
941 ** Parameters       dest_type - the destination type
942 **                  id   - the NFCEE ID or RF Discovery ID .
943 **                  protocol   - the protocol.
944 **                  p_cback - the connection callback function
945 **
946 ** Returns          tNFC_STATUS
947 **
948 *******************************************************************************/
NFC_ConnCreate(uint8_t dest_type,uint8_t id,uint8_t protocol,tNFC_CONN_CBACK * p_cback)949 tNFC_STATUS NFC_ConnCreate(uint8_t dest_type, uint8_t id, uint8_t protocol,
950                            tNFC_CONN_CBACK* p_cback) {
951   tNFC_STATUS status = NFC_STATUS_FAILED;
952   tNFC_CONN_CB* p_cb;
953   uint8_t num_tlv = 0, tlv_size = 0;
954   uint8_t param_tlvs[4], *pp;
955 
956   p_cb = nfc_alloc_conn_cb(p_cback);
957   if (p_cb) {
958     p_cb->id = id;
959     pp = param_tlvs;
960     if (dest_type == NCI_DEST_TYPE_NFCEE) {
961       num_tlv = 1;
962       UINT8_TO_STREAM(pp, NCI_CON_CREATE_TAG_NFCEE_VAL);
963       UINT8_TO_STREAM(pp, 2);
964       UINT8_TO_STREAM(pp, id);
965       UINT8_TO_STREAM(pp, protocol);
966       tlv_size = 4;
967     } else if (dest_type == NCI_DEST_TYPE_REMOTE) {
968       num_tlv = 1;
969       UINT8_TO_STREAM(pp, NCI_CON_CREATE_TAG_RF_DISC_ID);
970       UINT8_TO_STREAM(pp, 1);
971       UINT8_TO_STREAM(pp, id);
972       tlv_size = 3;
973     } else if (dest_type == NCI_DEST_TYPE_NFCC) {
974       p_cb->id = NFC_TEST_ID;
975     }
976     /* Add handling of NCI_DEST_TYPE_REMOTE when more RF interface definitions
977      * are added */
978     p_cb->act_protocol = protocol;
979     p_cb->p_cback = p_cback;
980     status = nci_snd_core_conn_create(dest_type, num_tlv, tlv_size, param_tlvs);
981     if (status == NFC_STATUS_FAILED) nfc_free_conn_cb(p_cb);
982   }
983   return status;
984 }
985 
986 /*******************************************************************************
987 **
988 ** Function         NFC_ConnClose
989 **
990 ** Description      This function is called to close a logical connection with
991 **                  NFCC.
992 **
993 ** Parameters       conn_id - the connection id.
994 **
995 ** Returns          tNFC_STATUS
996 **
997 *******************************************************************************/
NFC_ConnClose(uint8_t conn_id)998 tNFC_STATUS NFC_ConnClose(uint8_t conn_id) {
999   tNFC_CONN_CB* p_cb = nfc_find_conn_cb_by_conn_id(conn_id);
1000   tNFC_STATUS status = NFC_STATUS_FAILED;
1001 
1002   if (p_cb) {
1003     status = nci_snd_core_conn_close(conn_id);
1004   }
1005   return status;
1006 }
1007 
1008 /*******************************************************************************
1009 **
1010 ** Function         NFC_SetStaticRfCback
1011 **
1012 ** Description      This function is called to update the data callback function
1013 **                  to receive the data for the given connection id.
1014 **
1015 ** Parameters       p_cback - the connection callback function
1016 **
1017 ** Returns          Nothing
1018 **
1019 *******************************************************************************/
NFC_SetStaticRfCback(tNFC_CONN_CBACK * p_cback)1020 void NFC_SetStaticRfCback(tNFC_CONN_CBACK* p_cback) {
1021   tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1022 
1023   p_cb->p_cback = p_cback;
1024   /* just in case DH has received NCI data before the data callback is set
1025    * check if there's any data event to report on this connection id */
1026   nfc_data_event(p_cb);
1027 }
1028 
1029 /*******************************************************************************
1030 **
1031 ** Function         NFC_SetReassemblyFlag
1032 **
1033 ** Description      This function is called to set if nfc will reassemble
1034 **                  nci packet as much as its buffer can hold or it should not
1035 **                  reassemble but forward the fragmented nci packet to layer
1036 **                  above. If nci data pkt is fragmented, nfc may send multiple
1037 **                  NFC_DATA_CEVT with status NFC_STATUS_CONTINUE before sending
1038 **                  NFC_DATA_CEVT with status NFC_STATUS_OK based on reassembly
1039 **                  configuration and reassembly buffer size
1040 **
1041 ** Parameters       reassembly - flag to indicate if nfc may reassemble or not
1042 **
1043 ** Returns          Nothing
1044 **
1045 *******************************************************************************/
NFC_SetReassemblyFlag(bool reassembly)1046 void NFC_SetReassemblyFlag(bool reassembly) { nfc_cb.reassembly = reassembly; }
1047 
1048 /*******************************************************************************
1049 **
1050 ** Function         NFC_SendData
1051 **
1052 ** Description      This function is called to send the given data packet
1053 **                  to the connection identified by the given connection id.
1054 **
1055 ** Parameters       conn_id - the connection id.
1056 **                  p_data - the data packet.
1057 **                  p_data->offset must be >= NCI_MSG_OFFSET_SIZE +
1058 **                  NCI_DATA_HDR_SIZE
1059 **                  The data payload starts at
1060 **                  ((uint8_t *) (p_data + 1) + p_data->offset)
1061 **
1062 ** Returns          tNFC_STATUS
1063 **
1064 *******************************************************************************/
NFC_SendData(uint8_t conn_id,NFC_HDR * p_data)1065 tNFC_STATUS NFC_SendData(uint8_t conn_id, NFC_HDR* p_data) {
1066   tNFC_STATUS status = NFC_STATUS_FAILED;
1067   tNFC_CONN_CB* p_cb = nfc_find_conn_cb_by_conn_id(conn_id);
1068 
1069   if (p_cb && p_data &&
1070       p_data->offset >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE) {
1071     status = nfc_ncif_send_data(p_cb, p_data);
1072   }
1073 
1074   if (status != NFC_STATUS_OK) GKI_freebuf(p_data);
1075 
1076   return status;
1077 }
1078 
1079 /*******************************************************************************
1080 **
1081 ** Function         NFC_FlushData
1082 **
1083 ** Description      This function is called to discard the tx data queue of
1084 **                  the given connection id.
1085 **
1086 ** Parameters       conn_id - the connection id.
1087 **
1088 ** Returns          tNFC_STATUS
1089 **
1090 *******************************************************************************/
NFC_FlushData(uint8_t conn_id)1091 tNFC_STATUS NFC_FlushData(uint8_t conn_id) {
1092   tNFC_STATUS status = NFC_STATUS_FAILED;
1093   tNFC_CONN_CB* p_cb = nfc_find_conn_cb_by_conn_id(conn_id);
1094   void* p_buf;
1095 
1096   if (p_cb) {
1097     status = NFC_STATUS_OK;
1098     while ((p_buf = GKI_dequeue(&p_cb->tx_q)) != nullptr) GKI_freebuf(p_buf);
1099   }
1100 
1101   return status;
1102 }
1103 
1104 /*******************************************************************************
1105 **
1106 ** Function         NFC_Deactivate
1107 **
1108 ** Description      This function is called to stop the discovery process or
1109 **                  put the listen device in sleep mode or terminate the NFC
1110 **                  link.
1111 **
1112 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK
1113 **                  as NFC_DEACTIVATE_DEVT.
1114 **
1115 ** Parameters       deactivate_type - NFC_DEACTIVATE_TYPE_IDLE, to IDLE mode.
1116 **                                    NFC_DEACTIVATE_TYPE_SLEEP to SLEEP mode.
1117 **                                    NFC_DEACTIVATE_TYPE_SLEEP_AF to SLEEP_AF
1118 **                                    mode.
1119 **
1120 ** Returns          tNFC_STATUS
1121 **
1122 *******************************************************************************/
NFC_Deactivate(tNFC_DEACT_TYPE deactivate_type)1123 tNFC_STATUS NFC_Deactivate(tNFC_DEACT_TYPE deactivate_type) {
1124   tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1125   tNFC_STATUS status = NFC_STATUS_OK;
1126 
1127   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1128       "NFC_Deactivate %d (%s) deactivate_type:%d", nfc_cb.nfc_state,
1129       nfc_state_name(nfc_cb.nfc_state).c_str(), deactivate_type);
1130 
1131   if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING) {
1132     /* the HAL pre-discover is still active - clear the pending flag */
1133     nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
1134     if (!(nfc_cb.flags & NFC_FL_HAL_REQUESTED)) {
1135       /* if HAL did not request for control, clear this bit now */
1136       nfc_cb.flags &= ~NFC_FL_CONTROL_REQUESTED;
1137     }
1138     GKI_freebuf(nfc_cb.p_disc_pending);
1139     nfc_cb.p_disc_pending = nullptr;
1140     return NFC_STATUS_OK;
1141   }
1142 
1143   if (nfc_cb.nfc_state == NFC_STATE_OPEN) {
1144     nfc_set_state(NFC_STATE_CLOSING);
1145     DLOG_IF(INFO, nfc_debug_enabled)
1146         << StringPrintf("act_protocol %d credits:%d/%d", p_cb->act_protocol,
1147                         p_cb->init_credits, p_cb->num_buff);
1148     if ((p_cb->act_protocol == NCI_PROTOCOL_NFC_DEP) &&
1149         (p_cb->init_credits != p_cb->num_buff)) {
1150       nfc_cb.flags |= NFC_FL_DEACTIVATING;
1151       nfc_cb.deactivate_timer.param = (uintptr_t)deactivate_type;
1152       nfc_start_timer(&nfc_cb.deactivate_timer,
1153                       (uint16_t)(NFC_TTYPE_WAIT_2_DEACTIVATE),
1154                       NFC_DEACTIVATE_TIMEOUT);
1155       return status;
1156     }
1157   }
1158 
1159   status = nci_snd_deactivate_cmd(deactivate_type);
1160   return status;
1161 }
1162 /*******************************************************************************
1163 **
1164 ** Function         NFC_SetPowerSubState
1165 **
1166 ** Description      This function is called to send the power sub state (screen
1167 **                  state) to NFCC. The response from NFCC is reported by
1168 **                  tNFC_RESPONSE_CBACK as NFC_SET_POWER_STATE_REVT.
1169 **
1170 ** Parameters       scree_state
1171 **
1172 ** Returns          tNFC_STATUS
1173 **
1174 *******************************************************************************/
NFC_SetPowerSubState(uint8_t screen_state)1175 tNFC_STATUS NFC_SetPowerSubState(uint8_t screen_state) {
1176   return nci_snd_core_set_power_sub_state(screen_state);
1177 }
1178 /*******************************************************************************
1179 **
1180 ** Function         NFC_UpdateRFCommParams
1181 **
1182 ** Description      This function is called to update RF Communication
1183 **                  parameters once the Frame RF Interface has been activated.
1184 **
1185 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
1186 **                  as NFC_RF_COMM_PARAMS_UPDATE_REVT.
1187 **
1188 ** Returns          tNFC_STATUS
1189 **
1190 *******************************************************************************/
NFC_UpdateRFCommParams(tNFC_RF_COMM_PARAMS * p_params)1191 tNFC_STATUS NFC_UpdateRFCommParams(tNFC_RF_COMM_PARAMS* p_params) {
1192   uint8_t tlvs[12];
1193   uint8_t* p = tlvs;
1194   uint8_t data_exch_config;
1195 
1196   /* RF Technology and Mode */
1197   if (p_params->include_rf_tech_mode) {
1198     UINT8_TO_STREAM(p, NCI_RF_PARAM_ID_TECH_N_MODE);
1199     UINT8_TO_STREAM(p, 1);
1200     UINT8_TO_STREAM(p, p_params->rf_tech_n_mode);
1201   }
1202 
1203   /* Transmit Bit Rate */
1204   if (p_params->include_tx_bit_rate) {
1205     UINT8_TO_STREAM(p, NCI_RF_PARAM_ID_TX_BIT_RATE);
1206     UINT8_TO_STREAM(p, 1);
1207     UINT8_TO_STREAM(p, p_params->tx_bit_rate);
1208   }
1209 
1210   /* Receive Bit Rate */
1211   if (p_params->include_tx_bit_rate) {
1212     UINT8_TO_STREAM(p, NCI_RF_PARAM_ID_RX_BIT_RATE);
1213     UINT8_TO_STREAM(p, 1);
1214     UINT8_TO_STREAM(p, p_params->rx_bit_rate);
1215   }
1216 
1217   /* NFC-B Data Exchange Configuration */
1218   if (p_params->include_nfc_b_config) {
1219     UINT8_TO_STREAM(p, NCI_RF_PARAM_ID_B_DATA_EX_PARAM);
1220     UINT8_TO_STREAM(p, 1);
1221 
1222     data_exch_config = (p_params->min_tr0 & 0x03) << 6; /* b7b6 : Mininum TR0 */
1223     data_exch_config |= (p_params->min_tr1 & 0x03)
1224                         << 4; /* b5b4 : Mininum TR1 */
1225     data_exch_config |= (p_params->suppression_eos & 0x01)
1226                         << 3; /* b3 :   Suppression of EoS */
1227     data_exch_config |= (p_params->suppression_sos & 0x01)
1228                         << 2; /* b2 :   Suppression of SoS */
1229     data_exch_config |= (p_params->min_tr2 & 0x03); /* b1b0 : Mininum TR2 */
1230 
1231     UINT8_TO_STREAM(p, data_exch_config);
1232   }
1233 
1234   return nci_snd_parameter_update_cmd(tlvs, (uint8_t)(p - tlvs));
1235 }
1236 
1237 /*******************************************************************************
1238 **
1239 ** Function         NFC_SetPowerOffSleep
1240 **
1241 ** Description      This function closes/opens transport and turns off/on NFCC.
1242 **
1243 ** Returns          tNFC_STATUS
1244 **
1245 *******************************************************************************/
NFC_SetPowerOffSleep(bool enable)1246 tNFC_STATUS NFC_SetPowerOffSleep(bool enable) {
1247   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("enable = %d", enable);
1248 
1249   if ((enable == false) &&
1250       (nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP)) {
1251     nfc_cb.flags |= NFC_FL_RESTARTING;
1252 
1253     /* open transport */
1254     nfc_set_state(NFC_STATE_W4_HAL_OPEN);
1255     nfc_cb.p_hal->open(nfc_main_hal_cback, nfc_main_hal_data_cback);
1256 
1257     return NFC_STATUS_OK;
1258   } else if ((enable == true) && (nfc_cb.nfc_state == NFC_STATE_IDLE)) {
1259     /* close transport to turn off NFCC and clean up */
1260     nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
1261     nfc_task_shutdown_nfcc();
1262 
1263     return NFC_STATUS_OK;
1264   }
1265 
1266   LOG(ERROR) << StringPrintf("invalid state = %d", nfc_cb.nfc_state);
1267   return NFC_STATUS_FAILED;
1268 }
1269 
1270 /*******************************************************************************
1271 **
1272 ** Function         NFC_PowerCycleNFCC
1273 **
1274 ** Description      This function turns off and then on NFCC.
1275 **
1276 ** Returns          tNFC_STATUS
1277 **
1278 *******************************************************************************/
NFC_PowerCycleNFCC(void)1279 tNFC_STATUS NFC_PowerCycleNFCC(void) {
1280   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1281 
1282   if (nfc_cb.nfc_state == NFC_STATE_IDLE) {
1283     /* power cycle NFCC */
1284     nfc_cb.flags |= NFC_FL_POWER_CYCLE_NFCC;
1285     nfc_task_shutdown_nfcc();
1286 
1287     return NFC_STATUS_OK;
1288   }
1289 
1290   LOG(ERROR) << StringPrintf("invalid state = %d", nfc_cb.nfc_state);
1291   return NFC_STATUS_FAILED;
1292 }
1293 
1294 /*******************************************************************************
1295 **
1296 ** Function         NFC_GetNCIVersion
1297 **
1298 ** Description      Called by higher layer to get the current nci
1299 **                  version of nfc.
1300 **
1301 ** Returns          NCI version NCI2.0 / NCI1.0
1302 **
1303 *******************************************************************************/
NFC_GetNCIVersion()1304 uint8_t NFC_GetNCIVersion() { return nfc_cb.nci_version; }
1305 
1306 /*******************************************************************************
1307 **
1308 ** Function         NFC_ISODEPNakPresCheck
1309 **
1310 ** Description      This function is called to send the ISO DEP nak presenc
1311 **                  check cmd to check that the remote end point in RF field.
1312 **
1313 **                  The response from NFCC is reported by call back.The ntf
1314 **                  indicates success if card is present in field or failed
1315 **                  if card is lost.
1316 **
1317 ** Returns          tNFC_STATUS
1318 **
1319 *******************************************************************************/
NFC_ISODEPNakPresCheck()1320 tNFC_STATUS NFC_ISODEPNakPresCheck() {
1321   return nci_snd_iso_dep_nak_presence_check_cmd();
1322 }
1323 
1324 /*******************************************************************************
1325 **
1326 ** Function         NFC_SetStaticHciCback
1327 **
1328 ** Description      This function is called to update the data callback function
1329 **                  to receive the data for the static Hci connection id.
1330 **
1331 ** Parameters       p_cback - the connection callback function
1332 **
1333 ** Returns          Nothing
1334 **
1335 *******************************************************************************/
NFC_SetStaticHciCback(tNFC_CONN_CBACK * p_cback)1336 void NFC_SetStaticHciCback(tNFC_CONN_CBACK* p_cback) {
1337   DLOG_IF(INFO, nfc_debug_enabled)
1338       << StringPrintf("%s dest: %d", __func__, NCI_DEST_TYPE_NFCEE);
1339   tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_HCI_CONN_ID];
1340   tNFC_CONN evt_data;
1341 
1342   p_cb->p_cback = p_cback;
1343   if (p_cback && p_cb->buff_size && p_cb->num_buff) {
1344     DLOG_IF(INFO, nfc_debug_enabled)
1345         << StringPrintf("%s dest: %d", __func__, NCI_DEST_TYPE_NFCEE);
1346     evt_data.conn_create.status = NFC_STATUS_OK;
1347     evt_data.conn_create.dest_type = NCI_DEST_TYPE_NFCEE;
1348     evt_data.conn_create.id = p_cb->id;
1349     evt_data.conn_create.buff_size = p_cb->buff_size;
1350     evt_data.conn_create.num_buffs = p_cb->num_buff;
1351     (*p_cback)(NFC_HCI_CONN_ID, NFC_CONN_CREATE_CEVT, &evt_data);
1352   }
1353 }
1354 
1355 /*******************************************************************************
1356 **
1357 ** Function         NFC_GetStatusName
1358 **
1359 ** Description      This function returns the status name.
1360 **
1361 ** NOTE             conditionally compiled to save memory.
1362 **
1363 ** Returns          pointer to the name
1364 **
1365 *******************************************************************************/
NFC_GetStatusName(tNFC_STATUS status)1366 std::string NFC_GetStatusName(tNFC_STATUS status) {
1367   switch (status) {
1368     case NFC_STATUS_OK:
1369       return "OK";
1370     case NFC_STATUS_REJECTED:
1371       return "REJECTED";
1372     case NFC_STATUS_MSG_CORRUPTED:
1373       return "CORRUPTED";
1374     case NFC_STATUS_BUFFER_FULL:
1375       return "BUFFER_FULL";
1376     case NFC_STATUS_FAILED:
1377       return "FAILED";
1378     case NFC_STATUS_NOT_INITIALIZED:
1379       return "NOT_INITIALIZED";
1380     case NFC_STATUS_SYNTAX_ERROR:
1381       return "SYNTAX_ERROR";
1382     case NFC_STATUS_SEMANTIC_ERROR:
1383       return "SEMANTIC_ERROR";
1384     case NFC_STATUS_UNKNOWN_GID:
1385       return "UNKNOWN_GID";
1386     case NFC_STATUS_UNKNOWN_OID:
1387       return "UNKNOWN_OID";
1388     case NFC_STATUS_INVALID_PARAM:
1389       return "INVALID_PARAM";
1390     case NFC_STATUS_MSG_SIZE_TOO_BIG:
1391       return "MSG_SIZE_TOO_BIG";
1392     case NFC_STATUS_ALREADY_STARTED:
1393       return "ALREADY_STARTED";
1394     case NFC_STATUS_ACTIVATION_FAILED:
1395       return "ACTIVATION_FAILED";
1396     case NFC_STATUS_TEAR_DOWN:
1397       return "TEAR_DOWN";
1398     case NFC_STATUS_RF_TRANSMISSION_ERR:
1399       return "RF_TRANSMISSION_ERR";
1400     case NFC_STATUS_RF_PROTOCOL_ERR:
1401       return "RF_PROTOCOL_ERR";
1402     case NFC_STATUS_TIMEOUT:
1403       return "TIMEOUT";
1404     case NFC_STATUS_EE_INTF_ACTIVE_FAIL:
1405       return "EE_INTF_ACTIVE_FAIL";
1406     case NFC_STATUS_EE_TRANSMISSION_ERR:
1407       return "EE_TRANSMISSION_ERR";
1408     case NFC_STATUS_EE_PROTOCOL_ERR:
1409       return "EE_PROTOCOL_ERR";
1410     case NFC_STATUS_EE_TIMEOUT:
1411       return "EE_TIMEOUT";
1412     case NFC_STATUS_CMD_STARTED:
1413       return "CMD_STARTED";
1414     case NFC_STATUS_HW_TIMEOUT:
1415       return "HW_TIMEOUT";
1416     case NFC_STATUS_CONTINUE:
1417       return "CONTINUE";
1418     case NFC_STATUS_REFUSED:
1419       return "REFUSED";
1420     case NFC_STATUS_BAD_RESP:
1421       return "BAD_RESP";
1422     case NFC_STATUS_CMD_NOT_CMPLTD:
1423       return "CMD_NOT_CMPLTD";
1424     case NFC_STATUS_NO_BUFFERS:
1425       return "NO_BUFFERS";
1426     case NFC_STATUS_WRONG_PROTOCOL:
1427       return "WRONG_PROTOCOL";
1428     case NFC_STATUS_BUSY:
1429       return "BUSY";
1430     case NFC_STATUS_LINK_LOSS:
1431       return "LINK_LOSS";
1432     case NFC_STATUS_BAD_LENGTH:
1433       return "BAD_LENGTH";
1434     case NFC_STATUS_BAD_HANDLE:
1435       return "BAD_HANDLE";
1436     case NFC_STATUS_CONGESTED:
1437       return "CONGESTED";
1438     default:
1439       return "UNKNOWN";
1440   }
1441 }
1442