• 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  *  NFA interface for device management
22  *
23  ******************************************************************************/
24 #include <string.h>
25 
26 #include <android-base/stringprintf.h>
27 #include <base/logging.h>
28 #include <log/log.h>
29 
30 #include "ndef_utils.h"
31 #include "nfa_api.h"
32 #include "nfa_ce_int.h"
33 
34 using android::base::StringPrintf;
35 
36 extern bool nfc_debug_enabled;
37 
38 /*****************************************************************************
39 **  Constants
40 *****************************************************************************/
41 
42 /*****************************************************************************
43 **  APIs
44 *****************************************************************************/
45 /*******************************************************************************
46 **
47 ** Function         NFA_Init
48 **
49 ** Description      This function initializes control blocks for NFA
50 **
51 **                  p_hal_entry_tbl points to a table of HAL entry points
52 **
53 **                  NOTE: the buffer that p_hal_entry_tbl points must be
54 **                  persistent until NFA is disabled.
55 **
56 ** Returns          none
57 **
58 *******************************************************************************/
NFA_Init(tHAL_NFC_ENTRY * p_hal_entry_tbl)59 void NFA_Init(tHAL_NFC_ENTRY* p_hal_entry_tbl) {
60   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
61   nfa_sys_init();
62   nfa_dm_init();
63   nfa_p2p_init();
64   nfa_snep_init(false);
65   nfa_rw_init();
66   nfa_ce_init();
67   nfa_ee_init();
68   if (nfa_ee_max_ee_cfg != 0) {
69     nfa_dm_cb.get_max_ee = p_hal_entry_tbl->get_max_ee;
70     nfa_hci_init();
71   }
72 
73   /* Initialize NFC module */
74   NFC_Init(p_hal_entry_tbl);
75 }
76 
77 /*******************************************************************************
78 **
79 ** Function         NFA_Enable
80 **
81 ** Description      This function enables NFC. Prior to calling NFA_Enable,
82 **                  the NFCC must be powered up, and ready to receive commands.
83 **                  This function enables the tasks needed by NFC, opens the NCI
84 **                  transport, resets the NFC controller, downloads patches to
85 **                  the NFCC (if necessary), and initializes the NFC subsystems.
86 **
87 **                  This function should only be called once - typically when
88 **                  NFC is enabled during boot-up, or when NFC is enabled from a
89 **                  settings UI. Subsequent calls to NFA_Enable while NFA is
90 **                  enabling or enabled will be ignored. When the NFC startup
91 **                  procedure is completed, an NFA_DM_ENABLE_EVT is returned to
92 **                  the application using the tNFA_DM_CBACK.
93 **
94 ** Returns          NFA_STATUS_OK if successfully initiated
95 **                  NFA_STATUS_FAILED otherwise
96 **
97 *******************************************************************************/
NFA_Enable(tNFA_DM_CBACK * p_dm_cback,tNFA_CONN_CBACK * p_conn_cback)98 tNFA_STATUS NFA_Enable(tNFA_DM_CBACK* p_dm_cback,
99                        tNFA_CONN_CBACK* p_conn_cback) {
100   tNFA_DM_API_ENABLE* p_msg;
101 
102   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
103 
104   /* Validate parameters */
105   if ((!p_dm_cback) || (!p_conn_cback)) {
106     LOG(ERROR) << StringPrintf("error null callback");
107     return (NFA_STATUS_FAILED);
108   }
109 
110   p_msg = (tNFA_DM_API_ENABLE*)GKI_getbuf(sizeof(tNFA_DM_API_ENABLE));
111   if (p_msg != nullptr) {
112     p_msg->hdr.event = NFA_DM_API_ENABLE_EVT;
113     p_msg->p_dm_cback = p_dm_cback;
114     p_msg->p_conn_cback = p_conn_cback;
115 
116     nfa_sys_sendmsg(p_msg);
117 
118     return (NFA_STATUS_OK);
119   }
120 
121   return (NFA_STATUS_FAILED);
122 }
123 
124 /*******************************************************************************
125 **
126 ** Function         NFA_Disable
127 **
128 ** Description      This function is called to shutdown NFC. The tasks for NFC
129 **                  are terminated, and clean up routines are performed. This
130 **                  function is typically called during platform shut-down, or
131 **                  when NFC is disabled from a settings UI. When the NFC
132 **                  shutdown procedure is completed, an NFA_DM_DISABLE_EVT is
133 **                  returned to the application using the tNFA_DM_CBACK.
134 **
135 **                  The platform should wait until the NFC_DISABLE_REVT is
136 **                  received before powering down the NFC chip and NCI
137 **                  transport. This is required to so that NFA can gracefully
138 **                  shut down any open connections.
139 **
140 ** Returns          NFA_STATUS_OK if successfully initiated
141 **                  NFA_STATUS_FAILED otherwise
142 **
143 *******************************************************************************/
NFA_Disable(bool graceful)144 tNFA_STATUS NFA_Disable(bool graceful) {
145   tNFA_DM_API_DISABLE* p_msg;
146 
147   DLOG_IF(INFO, nfc_debug_enabled)
148       << StringPrintf("NFA_Disable (graceful=%i)", graceful);
149 
150   p_msg = (tNFA_DM_API_DISABLE*)GKI_getbuf(sizeof(tNFA_DM_API_DISABLE));
151   if (p_msg != nullptr) {
152     p_msg->hdr.event = NFA_DM_API_DISABLE_EVT;
153     p_msg->graceful = graceful;
154 
155     nfa_sys_sendmsg(p_msg);
156 
157     return (NFA_STATUS_OK);
158   }
159 
160   return (NFA_STATUS_FAILED);
161 }
162 
163 /*******************************************************************************
164 **
165 ** Function         NFA_GetNCIVersion
166 **
167 ** Description      Returns the NCI version of the NFCC to upper layer
168 **
169 **
170 ** Returns          NCI version NCI2.0 / NCI1.0
171 **
172 *******************************************************************************/
NFA_GetNCIVersion()173 uint8_t NFA_GetNCIVersion() { return NFC_GetNCIVersion(); }
174 
175 /*******************************************************************************
176 **
177 ** Function         NFA_SetPowerSubStateForScreenState
178 **
179 ** Description      Update the power sub-state as per current screen state to
180 **                  NFCC.
181 **
182 ** Returns          NFA_STATUS_OK if successfully initiated
183 **                  NFA_STATUS_FAILED otherwise
184 **
185 *******************************************************************************/
NFA_SetPowerSubStateForScreenState(uint8_t screenState)186 tNFA_STATUS NFA_SetPowerSubStateForScreenState(uint8_t screenState) {
187   DLOG_IF(INFO, nfc_debug_enabled)
188       << StringPrintf("%s: state:0x%X", __func__, screenState);
189 
190   uint8_t nci_scren_state = 0xFF;
191   uint16_t buf_size = sizeof(tNFA_DM_API_SET_POWER_SUB_STATE);
192   tNFA_DM_API_SET_POWER_SUB_STATE* p_msg =
193       (tNFA_DM_API_SET_POWER_SUB_STATE*)GKI_getbuf(buf_size);
194 
195   if (p_msg != nullptr) {
196     p_msg->hdr.event = NFA_DM_API_SET_POWER_SUB_STATE_EVT;
197     switch (screenState) {
198       case NFA_SCREEN_STATE_ON_UNLOCKED:
199         nci_scren_state = SCREEN_STATE_ON_UNLOCKED;
200         break;
201       case NFA_SCREEN_STATE_OFF_UNLOCKED:
202         nci_scren_state = SCREEN_STATE_OFF_UNLOCKED;
203         break;
204       case NFA_SCREEN_STATE_ON_LOCKED:
205         nci_scren_state = SCREEN_STATE_ON_LOCKED;
206         break;
207       case NFA_SCREEN_STATE_OFF_LOCKED:
208         nci_scren_state = SCREEN_STATE_OFF_LOCKED;
209         break;
210 
211       default:
212         DLOG_IF(INFO, nfc_debug_enabled)
213             << StringPrintf("%s, unknown screen state", __func__);
214         break;
215     }
216 
217     p_msg->screen_state = nci_scren_state;
218 
219     nfa_sys_sendmsg(p_msg);
220     return (NFA_STATUS_OK);
221   }
222   return (NFA_STATUS_FAILED);
223 }
224 /*******************************************************************************
225 **
226 ** Function         NFA_SetConfig
227 **
228 ** Description      Set the configuration parameters to NFCC. The result is
229 **                  reported with an NFA_DM_SET_CONFIG_EVT in the tNFA_DM_CBACK
230 **                  callback.
231 **
232 ** Note:            If RF discovery is started,
233 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
234 **                  happen before calling this function. Most Configuration
235 **                  parameters are related to RF discovery.
236 **
237 ** Returns          NFA_STATUS_OK if successfully initiated
238 **                  NFA_STATUS_BUSY if previous setting is on-going
239 **                  NFA_STATUS_FAILED otherwise
240 **
241 *******************************************************************************/
NFA_SetConfig(tNFA_PMID param_id,uint8_t length,uint8_t * p_data)242 tNFA_STATUS NFA_SetConfig(tNFA_PMID param_id, uint8_t length, uint8_t* p_data) {
243   tNFA_DM_API_SET_CONFIG* p_msg;
244 
245   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("param_id:0x%X", param_id);
246 
247   p_msg = (tNFA_DM_API_SET_CONFIG*)GKI_getbuf(
248       (uint16_t)(sizeof(tNFA_DM_API_SET_CONFIG) + length));
249   if (p_msg != nullptr) {
250     p_msg->hdr.event = NFA_DM_API_SET_CONFIG_EVT;
251 
252     p_msg->param_id = param_id;
253     p_msg->length = length;
254     p_msg->p_data = (uint8_t*)(p_msg + 1);
255 
256     /* Copy parameter data */
257     memcpy(p_msg->p_data, p_data, length);
258 
259     nfa_sys_sendmsg(p_msg);
260 
261     return (NFA_STATUS_OK);
262   }
263 
264   return (NFA_STATUS_FAILED);
265 }
266 
267 /*******************************************************************************
268 **
269 ** Function         NFA_GetConfig
270 **
271 ** Description      Get the configuration parameters from NFCC. The result is
272 **                  reported with an NFA_DM_GET_CONFIG_EVT in the tNFA_DM_CBACK
273 **                  callback.
274 **
275 ** Returns          NFA_STATUS_OK if successfully initiated
276 **                  NFA_STATUS_FAILED otherwise
277 **
278 *******************************************************************************/
NFA_GetConfig(uint8_t num_ids,tNFA_PMID * p_param_ids)279 tNFA_STATUS NFA_GetConfig(uint8_t num_ids, tNFA_PMID* p_param_ids) {
280   tNFA_DM_API_GET_CONFIG* p_msg;
281 
282   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("num_ids: %i", num_ids);
283 
284   p_msg = (tNFA_DM_API_GET_CONFIG*)GKI_getbuf(
285       (uint16_t)(sizeof(tNFA_DM_API_GET_CONFIG) + num_ids));
286   if (p_msg != nullptr) {
287     p_msg->hdr.event = NFA_DM_API_GET_CONFIG_EVT;
288 
289     p_msg->num_ids = num_ids;
290     p_msg->p_pmids = (tNFA_PMID*)(p_msg + 1);
291 
292     /* Copy the param IDs */
293     memcpy(p_msg->p_pmids, p_param_ids, num_ids);
294 
295     nfa_sys_sendmsg(p_msg);
296 
297     return (NFA_STATUS_OK);
298   }
299 
300   return (NFA_STATUS_FAILED);
301 }
302 
303 /*******************************************************************************
304 **
305 ** Function         NFA_RequestExclusiveRfControl
306 **
307 ** Description      Request exclusive control of NFC.
308 **                  - Previous behavior (polling/tag reading, DH card emulation)
309 **                    will be suspended .
310 **                  - Polling and listening will be done based on the specified
311 **                    params
312 **
313 **                  The NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT event of
314 **                  tNFA_CONN_CBACK indicates the status of the operation.
315 **
316 **                  NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT indicates link
317 **                  activation/deactivation.
318 **
319 **                  NFA_SendRawFrame is used to send data to the peer.
320 **                  NFA_DATA_EVT indicates data from the peer.
321 **
322 **                  If a tag is activated, then the NFA_RW APIs may be used to
323 **                  send commands to the tag. Incoming NDEF messages are sent to
324 **                  the NDEF callback.
325 **
326 **                  Once exclusive RF control has started, NFA will not activate
327 **                  LLCP internally. The application has exclusive control of
328 **                  the link.
329 **
330 ** Note:            If RF discovery is started,
331 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
332 **                  happen before calling this function
333 **
334 ** Returns          NFA_STATUS_OK if successfully initiated
335 **                  NFA_STATUS_FAILED otherwise
336 **
337 *******************************************************************************/
NFA_RequestExclusiveRfControl(tNFA_TECHNOLOGY_MASK poll_mask,tNFA_LISTEN_CFG * p_listen_cfg,tNFA_CONN_CBACK * p_conn_cback,tNFA_NDEF_CBACK * p_ndef_cback)338 tNFA_STATUS NFA_RequestExclusiveRfControl(tNFA_TECHNOLOGY_MASK poll_mask,
339                                           tNFA_LISTEN_CFG* p_listen_cfg,
340                                           tNFA_CONN_CBACK* p_conn_cback,
341                                           tNFA_NDEF_CBACK* p_ndef_cback) {
342   tNFA_DM_API_REQ_EXCL_RF_CTRL* p_msg;
343 
344   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("poll_mask=0x%x", poll_mask);
345 
346   if (!p_conn_cback) {
347     LOG(ERROR) << StringPrintf("error null callback");
348     return (NFA_STATUS_FAILED);
349   }
350 
351   p_msg = (tNFA_DM_API_REQ_EXCL_RF_CTRL*)GKI_getbuf(
352       sizeof(tNFA_DM_API_REQ_EXCL_RF_CTRL));
353   if (p_msg != nullptr) {
354     p_msg->hdr.event = NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT;
355     p_msg->poll_mask = poll_mask;
356     p_msg->p_conn_cback = p_conn_cback;
357     p_msg->p_ndef_cback = p_ndef_cback;
358 
359     if (p_listen_cfg)
360       memcpy(&p_msg->listen_cfg, p_listen_cfg, sizeof(tNFA_LISTEN_CFG));
361     else
362       memset(&p_msg->listen_cfg, 0x00, sizeof(tNFA_LISTEN_CFG));
363 
364     nfa_sys_sendmsg(p_msg);
365 
366     return (NFA_STATUS_OK);
367   }
368 
369   return (NFA_STATUS_FAILED);
370 }
371 
372 /*******************************************************************************
373 **
374 ** Function         NFA_ReleaseExclusiveRfControl
375 **
376 ** Description      Release exclusive control of NFC. Once released, behavior
377 **                  prior to obtaining exclusive RF control will resume.
378 **
379 ** Returns          NFA_STATUS_OK if successfully initiated
380 **                  NFA_STATUS_FAILED otherwise
381 **
382 *******************************************************************************/
NFA_ReleaseExclusiveRfControl(void)383 tNFA_STATUS NFA_ReleaseExclusiveRfControl(void) {
384   NFC_HDR* p_msg;
385 
386   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
387 
388   if (!nfa_dm_cb.p_excl_conn_cback) {
389     LOG(ERROR) << StringPrintf(
390         "Exclusive rf control is not in "
391         "progress");
392     return (NFA_STATUS_FAILED);
393   }
394 
395   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
396   if (p_msg != nullptr) {
397     p_msg->event = NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT;
398     nfa_sys_sendmsg(p_msg);
399     return (NFA_STATUS_OK);
400   }
401 
402   return (NFA_STATUS_FAILED);
403 }
404 
405 /*******************************************************************************
406 **
407 ** Function         NFA_EnablePolling
408 **
409 ** Description      Enable polling for technologies specified by poll_mask.
410 **
411 **                  The following events (notified using the connection
412 **                  callback registered with NFA_Enable) are generated during
413 **                  polling:
414 **
415 **                  - NFA_POLL_ENABLED_EVT indicates whether or not polling
416 **                    successfully enabled.
417 **                  - NFA_DISC_RESULT_EVT indicates there are more than one
418 **                    devices, so application must select one of tags by calling
419 **                    NFA_Select().
420 **                  - NFA_SELECT_RESULT_EVT indicates whether previous selection
421 **                    was successful or not. If it was failed then application
422 **                    must select again or deactivate by calling
423 **                    NFA_Deactivate().
424 **                  - NFA_ACTIVATED_EVT is generated when an NFC link is
425 **                    activated.
426 **                  - NFA_NDEF_DETECT_EVT is generated if tag is activated
427 **                  - NFA_LLCP_ACTIVATED_EVT/NFA_LLCP_DEACTIVATED_EVT is
428 **                    generated if NFC-DEP is activated
429 **                  - NFA_DEACTIVATED_EVT will be returned after deactivating
430 **                    NFC link.
431 **
432 ** Note:            If RF discovery is started,
433 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
434 **                  happen before calling this function
435 **
436 ** Returns          NFA_STATUS_OK if successfully initiated
437 **                  NFA_STATUS_FAILED otherwise
438 **
439 *******************************************************************************/
NFA_EnablePolling(tNFA_TECHNOLOGY_MASK poll_mask)440 tNFA_STATUS NFA_EnablePolling(tNFA_TECHNOLOGY_MASK poll_mask) {
441   tNFA_DM_API_ENABLE_POLL* p_msg;
442 
443   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("0x%X", poll_mask);
444 
445   p_msg = (tNFA_DM_API_ENABLE_POLL*)GKI_getbuf(sizeof(tNFA_DM_API_ENABLE_POLL));
446   if (p_msg != nullptr) {
447     p_msg->hdr.event = NFA_DM_API_ENABLE_POLLING_EVT;
448     p_msg->poll_mask = poll_mask;
449 
450     nfa_sys_sendmsg(p_msg);
451 
452     return (NFA_STATUS_OK);
453   }
454 
455   return (NFA_STATUS_FAILED);
456 }
457 
458 /*******************************************************************************
459 **
460 ** Function         NFA_DisablePolling
461 **
462 ** Description      Disable polling
463 **                  NFA_POLL_DISABLED_EVT will be returned after stopping
464 **                  polling.
465 **
466 ** Note:            If RF discovery is started,
467 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
468 **                  happen before calling this function
469 **
470 ** Returns          NFA_STATUS_OK if successfully initiated
471 **                  NFA_STATUS_FAILED otherwise
472 **
473 *******************************************************************************/
NFA_DisablePolling(void)474 tNFA_STATUS NFA_DisablePolling(void) {
475   NFC_HDR* p_msg;
476 
477   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
478 
479   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
480   if (p_msg != nullptr) {
481     p_msg->event = NFA_DM_API_DISABLE_POLLING_EVT;
482 
483     nfa_sys_sendmsg(p_msg);
484 
485     return (NFA_STATUS_OK);
486   }
487 
488   return (NFA_STATUS_FAILED);
489 }
490 
491 /*******************************************************************************
492 **
493 ** Function         NFA_EnableListening
494 **
495 ** Description      Enable listening.
496 **                  NFA_LISTEN_ENABLED_EVT will be returned after listening is
497 **                  allowed.
498 **
499 **                  The actual listening technologies are specified by other NFA
500 **                  API functions. Such functions include (but not limited to)
501 **                  NFA_CeConfigureUiccListenTech.
502 **                  If NFA_DisableListening () is called to ignore the listening
503 **                  technologies, NFA_EnableListening () is called to restore
504 **                  the listening technologies set by these functions.
505 **
506 ** Note:            If RF discovery is started,
507 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
508 **                  happen before calling this function
509 **
510 ** Returns          NFA_STATUS_OK if successfully initiated
511 **                  NFA_STATUS_FAILED otherwise
512 **
513 *******************************************************************************/
NFA_EnableListening(void)514 tNFA_STATUS NFA_EnableListening(void) {
515   NFC_HDR* p_msg;
516 
517   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
518 
519   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
520   if (p_msg != nullptr) {
521     p_msg->event = NFA_DM_API_ENABLE_LISTENING_EVT;
522 
523     nfa_sys_sendmsg(p_msg);
524 
525     return (NFA_STATUS_OK);
526   }
527 
528   return (NFA_STATUS_FAILED);
529 }
530 
531 /*******************************************************************************
532 **
533 ** Function         NFA_DisableListening
534 **
535 ** Description      Disable listening
536 **                  NFA_LISTEN_DISABLED_EVT will be returned after stopping
537 **                  listening. This function is called to exclude listen at RF
538 **                  discovery.
539 **
540 ** Note:            If RF discovery is started,
541 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
542 **                  happen before calling this function
543 **
544 ** Returns          NFA_STATUS_OK if successfully initiated
545 **                  NFA_STATUS_FAILED otherwise
546 **
547 *******************************************************************************/
NFA_DisableListening(void)548 tNFA_STATUS NFA_DisableListening(void) {
549   NFC_HDR* p_msg;
550 
551   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
552 
553   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
554   if (p_msg != nullptr) {
555     p_msg->event = NFA_DM_API_DISABLE_LISTENING_EVT;
556 
557     nfa_sys_sendmsg(p_msg);
558 
559     return (NFA_STATUS_OK);
560   }
561 
562   return (NFA_STATUS_FAILED);
563 }
564 
565 /*******************************************************************************
566 **
567 ** Function         NFA_PauseP2p
568 **
569 ** Description      Pause P2P services.
570 **                  NFA_P2P_PAUSED_EVT will be returned after P2P services are
571 **                  disabled.
572 **
573 **                  The P2P services enabled by NFA_P2p* API functions are not
574 **                  available. NFA_ResumeP2p() is called to resume the P2P
575 **                  services.
576 **
577 ** Note:            If RF discovery is started,
578 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
579 **                  happen before calling this function
580 **
581 ** Returns          NFA_STATUS_OK if successfully initiated
582 **                  NFA_STATUS_FAILED otherwise
583 **
584 *******************************************************************************/
NFA_PauseP2p(void)585 tNFA_STATUS NFA_PauseP2p(void) {
586   NFC_HDR* p_msg;
587 
588   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
589 
590   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
591   if (p_msg != nullptr) {
592     p_msg->event = NFA_DM_API_PAUSE_P2P_EVT;
593 
594     nfa_sys_sendmsg(p_msg);
595 
596     return (NFA_STATUS_OK);
597   }
598 
599   return (NFA_STATUS_FAILED);
600 }
601 
602 /*******************************************************************************
603 **
604 ** Function         NFA_ResumeP2p
605 **
606 ** Description      Resume P2P services.
607 **                  NFA_P2P_RESUMED_EVT will be returned after P2P services are.
608 **                  enables again.
609 **
610 ** Note:            If RF discovery is started,
611 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
612 **                  happen before calling this function
613 **
614 ** Returns          NFA_STATUS_OK if successfully initiated
615 **                  NFA_STATUS_FAILED otherwise
616 **
617 *******************************************************************************/
NFA_ResumeP2p(void)618 tNFA_STATUS NFA_ResumeP2p(void) {
619   NFC_HDR* p_msg;
620 
621   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
622 
623   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
624   if (p_msg != nullptr) {
625     p_msg->event = NFA_DM_API_RESUME_P2P_EVT;
626 
627     nfa_sys_sendmsg(p_msg);
628 
629     return (NFA_STATUS_OK);
630   }
631 
632   return (NFA_STATUS_FAILED);
633 }
634 
635 /*******************************************************************************
636 **
637 ** Function         NFA_SetP2pListenTech
638 **
639 ** Description      This function is called to set listen technology for
640 **                  NFC-DEP. This funtion may be called before or after starting
641 **                  any server on NFA P2P/CHO/SNEP.
642 **                  If there is no technology for NFC-DEP, P2P listening will be
643 **                  stopped.
644 **
645 **                  NFA_SET_P2P_LISTEN_TECH_EVT without data will be returned.
646 **
647 ** Note:            If RF discovery is started,
648 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
649 **                  happen before calling this function
650 **
651 ** Returns          NFA_STATUS_OK if successfully initiated
652 **                  NFA_STATUS_FAILED otherwise
653 **
654 *******************************************************************************/
NFA_SetP2pListenTech(tNFA_TECHNOLOGY_MASK tech_mask)655 tNFA_STATUS NFA_SetP2pListenTech(tNFA_TECHNOLOGY_MASK tech_mask) {
656   tNFA_DM_API_SET_P2P_LISTEN_TECH* p_msg;
657 
658   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("tech_mask:0x%X", tech_mask);
659 
660   p_msg = (tNFA_DM_API_SET_P2P_LISTEN_TECH*)GKI_getbuf(
661       sizeof(tNFA_DM_API_SET_P2P_LISTEN_TECH));
662   if (p_msg != nullptr) {
663     p_msg->hdr.event = NFA_DM_API_SET_P2P_LISTEN_TECH_EVT;
664     p_msg->tech_mask = tech_mask;
665 
666     nfa_sys_sendmsg(p_msg);
667 
668     return (NFA_STATUS_OK);
669   }
670 
671   return (NFA_STATUS_FAILED);
672 }
673 
674 /*******************************************************************************
675 **
676 ** Function         NFA_StartRfDiscovery
677 **
678 ** Description      Start RF discovery
679 **                  RF discovery parameters shall be set by other APIs.
680 **
681 **                  An NFA_RF_DISCOVERY_STARTED_EVT indicates whether starting
682 **                  was successful or not.
683 **
684 ** Returns          NFA_STATUS_OK if successfully initiated
685 **                  NFA_STATUS_FAILED otherwise
686 **
687 *******************************************************************************/
NFA_StartRfDiscovery(void)688 tNFA_STATUS NFA_StartRfDiscovery(void) {
689   NFC_HDR* p_msg;
690 
691   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
692 
693   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
694   if (p_msg != nullptr) {
695     p_msg->event = NFA_DM_API_START_RF_DISCOVERY_EVT;
696 
697     nfa_sys_sendmsg(p_msg);
698 
699     return (NFA_STATUS_OK);
700   }
701 
702   return (NFA_STATUS_FAILED);
703 }
704 
705 /*******************************************************************************
706 **
707 ** Function         NFA_StopRfDiscovery
708 **
709 ** Description      Stop RF discovery
710 **
711 **                  An NFA_RF_DISCOVERY_STOPPED_EVT indicates whether stopping
712 **                  was successful or not.
713 **
714 ** Returns          NFA_STATUS_OK if successfully initiated
715 **                  NFA_STATUS_FAILED otherwise
716 **
717 *******************************************************************************/
NFA_StopRfDiscovery(void)718 tNFA_STATUS NFA_StopRfDiscovery(void) {
719   NFC_HDR* p_msg;
720 
721   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
722 
723   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
724   if (p_msg != nullptr) {
725     p_msg->event = NFA_DM_API_STOP_RF_DISCOVERY_EVT;
726 
727     nfa_sys_sendmsg(p_msg);
728 
729     return (NFA_STATUS_OK);
730   }
731 
732   return (NFA_STATUS_FAILED);
733 }
734 
735 /*******************************************************************************
736 **
737 ** Function         NFA_SetRfDiscoveryDuration
738 **
739 ** Description      Set the duration of the single discovery period in [ms].
740 **                  Allowable range: 0 ms to 0xFFFF ms.
741 **
742 **                  If discovery is already started, the application should
743 **                  call NFA_StopRfDiscovery prior to calling
744 **                  NFA_SetRfDiscoveryDuration, and then call
745 **                  NFA_StartRfDiscovery afterwards to restart discovery using
746 **                  the new duration.
747 **
748 ** Note:            If RF discovery is started,
749 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
750 **                  happen before calling this function
751 **
752 ** Returns:
753 **                  NFA_STATUS_OK, if command accepted
754 **                  NFA_STATUS_FAILED: otherwise
755 **
756 *******************************************************************************/
NFA_SetRfDiscoveryDuration(uint16_t discovery_period_ms)757 tNFA_STATUS NFA_SetRfDiscoveryDuration(uint16_t discovery_period_ms) {
758   tNFA_DM_API_SET_RF_DISC_DUR* p_msg;
759 
760   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
761 
762   /* Post the API message */
763   p_msg = (tNFA_DM_API_SET_RF_DISC_DUR*)GKI_getbuf(
764       sizeof(tNFA_DM_API_SET_RF_DISC_DUR));
765   if (p_msg != nullptr) {
766     p_msg->hdr.event = NFA_DM_API_SET_RF_DISC_DURATION_EVT;
767 
768     /* Set discovery duration */
769     p_msg->rf_disc_dur_ms = discovery_period_ms;
770 
771     nfa_sys_sendmsg(p_msg);
772 
773     return (NFA_STATUS_OK);
774   }
775 
776   return (NFA_STATUS_FAILED);
777 }
778 
779 /*******************************************************************************
780 **
781 ** Function         NFA_Select
782 **
783 ** Description      Select one from detected devices during discovery
784 **                  (from NFA_DISC_RESULT_EVTs). The application should wait for
785 **                  the final NFA_DISC_RESULT_EVT before selecting.
786 **
787 **                  An NFA_SELECT_RESULT_EVT indicates whether selection was
788 **                  successful or not. If failed then application must select
789 **                  again or deactivate by NFA_Deactivate().
790 **
791 ** Returns          NFA_STATUS_OK if successfully initiated
792 **                  NFA_STATUS_INVALID_PARAM if RF interface is not matched
793 **                  protocol
794 **                  NFA_STATUS_FAILED otherwise
795 **
796 *******************************************************************************/
NFA_Select(uint8_t rf_disc_id,tNFA_NFC_PROTOCOL protocol,tNFA_INTF_TYPE rf_interface)797 tNFA_STATUS NFA_Select(uint8_t rf_disc_id, tNFA_NFC_PROTOCOL protocol,
798                        tNFA_INTF_TYPE rf_interface) {
799   tNFA_DM_API_SELECT* p_msg;
800 
801   DLOG_IF(INFO, nfc_debug_enabled)
802       << StringPrintf("rf_disc_id:0x%X, protocol:0x%X, rf_interface:0x%X",
803                       rf_disc_id, protocol, rf_interface);
804 
805   if (((rf_interface == NFA_INTERFACE_ISO_DEP) &&
806        (protocol != NFA_PROTOCOL_ISO_DEP)) ||
807       ((rf_interface == NFA_INTERFACE_NFC_DEP) &&
808        (protocol != NFA_PROTOCOL_NFC_DEP))) {
809     LOG(ERROR) << StringPrintf("RF interface is not matched protocol");
810     return (NFA_STATUS_INVALID_PARAM);
811   }
812 
813   p_msg =
814       (tNFA_DM_API_SELECT*)GKI_getbuf((uint16_t)(sizeof(tNFA_DM_API_SELECT)));
815   if (p_msg != nullptr) {
816     p_msg->hdr.event = NFA_DM_API_SELECT_EVT;
817     p_msg->rf_disc_id = rf_disc_id;
818     p_msg->protocol = protocol;
819     p_msg->rf_interface = rf_interface;
820 
821     nfa_sys_sendmsg(p_msg);
822 
823     return (NFA_STATUS_OK);
824   }
825 
826   return (NFA_STATUS_FAILED);
827 }
828 
829 /*******************************************************************************
830 **
831 ** Function         NFA_UpdateRFCommParams
832 **
833 ** Description      This function is called to update RF Communication
834 **                  parameters once the Frame RF Interface has been activated.
835 **
836 **                  An NFA_UPDATE_RF_PARAM_RESULT_EVT indicates whether updating
837 **                  was successful or not.
838 **
839 ** Returns          NFA_STATUS_OK if successfully initiated
840 **                  NFA_STATUS_FAILED otherwise
841 **
842 *******************************************************************************/
NFA_UpdateRFCommParams(tNFA_RF_COMM_PARAMS * p_params)843 tNFA_STATUS NFA_UpdateRFCommParams(tNFA_RF_COMM_PARAMS* p_params) {
844   tNFA_DM_API_UPDATE_RF_PARAMS* p_msg;
845 
846   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
847 
848   p_msg = (tNFA_DM_API_UPDATE_RF_PARAMS*)GKI_getbuf(
849       (uint16_t)(sizeof(tNFA_DM_API_UPDATE_RF_PARAMS)));
850   if (p_msg != nullptr) {
851     p_msg->hdr.event = NFA_DM_API_UPDATE_RF_PARAMS_EVT;
852     memcpy(&p_msg->params, p_params, sizeof(tNFA_RF_COMM_PARAMS));
853 
854     nfa_sys_sendmsg(p_msg);
855 
856     return (NFA_STATUS_OK);
857   }
858 
859   return (NFA_STATUS_FAILED);
860 }
861 
862 /*******************************************************************************
863 **
864 ** Function         NFA_Deactivate
865 **
866 ** Description
867 **                  If sleep_mode=TRUE:
868 **                      Deselect the activated device by deactivating into sleep
869 **                      mode.
870 **
871 **                      An NFA_DEACTIVATE_FAIL_EVT indicates that selection was
872 **                      not successful. Application can select another
873 **                      discovered device or deactivate by NFA_Deactivate()
874 **                      after receiving NFA_DEACTIVATED_EVT.
875 **
876 **                      Deactivating to sleep mode is not allowed when NFCC is
877 **                      in wait-for-host-select mode, or in listen-sleep states;
878 **                      NFA will deactivate to idle or discovery state for these
879 **                      cases respectively.
880 **
881 **
882 **                  If sleep_mode=FALSE:
883 **                      Deactivate the connection (e.g. as a result of presence
884 **                      check failure) NFA_DEACTIVATED_EVT will indicate that
885 **                      link is deactivated. Polling/listening will resume
886 **                      (unless the nfcc is in wait_for-all-discoveries state)
887 **
888 **
889 ** Returns          NFA_STATUS_OK if successfully initiated
890 **                  NFA_STATUS_FAILED otherwise
891 **
892 *******************************************************************************/
NFA_Deactivate(bool sleep_mode)893 extern tNFA_STATUS NFA_Deactivate(bool sleep_mode) {
894   tNFA_DM_API_DEACTIVATE* p_msg;
895 
896   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("sleep_mode:%i", sleep_mode);
897 
898   p_msg = (tNFA_DM_API_DEACTIVATE*)GKI_getbuf(
899       (uint16_t)(sizeof(tNFA_DM_API_DEACTIVATE)));
900   if (p_msg != nullptr) {
901     p_msg->hdr.event = NFA_DM_API_DEACTIVATE_EVT;
902     p_msg->sleep_mode = sleep_mode;
903 
904     nfa_sys_sendmsg(p_msg);
905 
906     return (NFA_STATUS_OK);
907   }
908 
909   return (NFA_STATUS_FAILED);
910 }
911 
912 /*******************************************************************************
913 **
914 ** Function         NFA_SendRawFrame
915 **
916 ** Description      Send a raw frame over the activated interface with the NFCC.
917 **                  This function can only be called after NFC link is
918 **                  activated.
919 **
920 **                  If the activated interface is a tag and auto-presence check
921 **                  is enabled then presence_check_start_delay can be used to
922 **                  indicate the delay in msec after which the next auto
923 **                  presence check command can be sent.
924 **                  NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY can be used as the
925 **                  default value for the delay.
926 **
927 ** Returns          NFA_STATUS_OK if successfully initiated
928 **                  NFA_STATUS_FAILED otherwise
929 **
930 *******************************************************************************/
NFA_SendRawFrame(uint8_t * p_raw_data,uint16_t data_len,uint16_t presence_check_start_delay)931 tNFA_STATUS NFA_SendRawFrame(uint8_t* p_raw_data, uint16_t data_len,
932                              uint16_t presence_check_start_delay) {
933   NFC_HDR* p_msg;
934   uint16_t size;
935   uint8_t* p;
936 
937   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("data_len:%d", data_len);
938 
939   /* Validate parameters */
940   if ((data_len == 0) || (p_raw_data == nullptr))
941     return (NFA_STATUS_INVALID_PARAM);
942 
943   size = NFC_HDR_SIZE + NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + data_len;
944   /* Check for integer overflow */
945   if (size < data_len) {
946     android_errorWriteLog(0x534e4554, "120664978");
947     return NFA_STATUS_INVALID_PARAM;
948   }
949   p_msg = (NFC_HDR*)GKI_getbuf(size);
950   if (p_msg != nullptr) {
951     p_msg->event = NFA_DM_API_RAW_FRAME_EVT;
952     p_msg->layer_specific = presence_check_start_delay;
953     p_msg->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
954     p_msg->len = data_len;
955 
956     p = (uint8_t*)(p_msg + 1) + p_msg->offset;
957     memcpy(p, p_raw_data, data_len);
958 
959     nfa_sys_sendmsg(p_msg);
960 
961     return (NFA_STATUS_OK);
962   }
963 
964   return (NFA_STATUS_FAILED);
965 }
966 
967 /*******************************************************************************
968 ** NDEF Handler APIs
969 *******************************************************************************/
970 
971 /*******************************************************************************
972 **
973 ** Function         NFA_RegisterNDefTypeHandler
974 **
975 ** Description      This function allows the applications to register for
976 **                  specific types of NDEF records. When NDEF records are
977 **                  received, NFA will parse the record-type field, and pass
978 **                  the record to the registered tNFA_NDEF_CBACK.
979 **
980 **                  For records types which were not registered, the record will
981 **                  be sent to the default handler. A default type-handler may
982 **                  be registered by calling this NFA_RegisterNDefTypeHandler
983 **                  with tnf=NFA_TNF_DEFAULT. In this case, all un-registered
984 **                  record types will be sent to the callback. Only one default
985 **                  handler may be registered at a time.
986 **
987 **                  An NFA_NDEF_REGISTER_EVT will be sent to the tNFA_NDEF_CBACK
988 **                  to indicate that registration was successful, and provide a
989 **                  handle for this record type.
990 **
991 ** Returns          NFA_STATUS_OK if successfully initiated
992 **                  NFA_STATUS_FAILED otherwise
993 **
994 *******************************************************************************/
NFA_RegisterNDefTypeHandler(bool handle_whole_message,tNFA_TNF tnf,uint8_t * p_type_name,uint8_t type_name_len,tNFA_NDEF_CBACK * p_ndef_cback)995 tNFA_STATUS NFA_RegisterNDefTypeHandler(bool handle_whole_message, tNFA_TNF tnf,
996                                         uint8_t* p_type_name,
997                                         uint8_t type_name_len,
998                                         tNFA_NDEF_CBACK* p_ndef_cback) {
999   tNFA_DM_API_REG_NDEF_HDLR* p_msg;
1000 
1001   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1002       "handle whole ndef message: %i, "
1003       "tnf=0x%02x",
1004       handle_whole_message, tnf);
1005 
1006   /* Check for NULL callback */
1007   if (!p_ndef_cback) {
1008     LOG(ERROR) << StringPrintf("error - null callback");
1009     return (NFA_STATUS_INVALID_PARAM);
1010   }
1011 
1012   p_msg = (tNFA_DM_API_REG_NDEF_HDLR*)GKI_getbuf(
1013       (uint16_t)(sizeof(tNFA_DM_API_REG_NDEF_HDLR) + type_name_len));
1014   if (p_msg != nullptr) {
1015     p_msg->hdr.event = NFA_DM_API_REG_NDEF_HDLR_EVT;
1016 
1017     p_msg->flags =
1018         (handle_whole_message ? NFA_NDEF_FLAGS_HANDLE_WHOLE_MESSAGE : 0);
1019     p_msg->tnf = tnf;
1020     p_msg->name_len = type_name_len;
1021     p_msg->p_ndef_cback = p_ndef_cback;
1022     memcpy(p_msg->name, p_type_name, type_name_len);
1023 
1024     nfa_sys_sendmsg(p_msg);
1025 
1026     return (NFA_STATUS_OK);
1027   }
1028 
1029   return (NFA_STATUS_FAILED);
1030 }
1031 
1032 /*******************************************************************************
1033 **
1034 ** Function         NFA_RegisterNDefUriHandler
1035 **
1036 ** Description      This API is a special-case of NFA_RegisterNDefTypeHandler
1037 **                  with TNF=NFA_TNF_WKT, and type_name='U' (URI record); and
1038 **                  allows registering for specific URI types (e.g. 'tel:' or
1039 **                  'mailto:').
1040 **
1041 **                  An NFA_NDEF_REGISTER_EVT will be sent to the tNFA_NDEF_CBACK
1042 **                  to indicate that registration was successful, and provide a
1043 **                  handle for this registration.
1044 **
1045 **                  If uri_id=NFA_NDEF_URI_ID_ABSOLUTE, then p_abs_uri contains
1046 **                  the unabridged URI. For all other uri_id values,
1047 **                  the p_abs_uri parameter is ignored (i.e the URI prefix is
1048 **                  implied by uri_id). See [NFC RTD URI] for more information.
1049 **
1050 ** Returns          NFA_STATUS_OK if successfully initiated
1051 **                  NFA_STATUS_FAILED otherwise
1052 **
1053 *******************************************************************************/
NFA_RegisterNDefUriHandler(bool handle_whole_message,tNFA_NDEF_URI_ID uri_id,uint8_t * p_abs_uri,uint8_t uri_id_len,tNFA_NDEF_CBACK * p_ndef_cback)1054 extern tNFA_STATUS NFA_RegisterNDefUriHandler(bool handle_whole_message,
1055                                               tNFA_NDEF_URI_ID uri_id,
1056                                               uint8_t* p_abs_uri,
1057                                               uint8_t uri_id_len,
1058                                               tNFA_NDEF_CBACK* p_ndef_cback) {
1059   tNFA_DM_API_REG_NDEF_HDLR* p_msg;
1060 
1061   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1062       "handle whole ndef message: %i, "
1063       "uri_id=0x%02x",
1064       handle_whole_message, uri_id);
1065 
1066   /* Check for NULL callback */
1067   if (!p_ndef_cback) {
1068     LOG(ERROR) << StringPrintf("error - null callback");
1069     return (NFA_STATUS_INVALID_PARAM);
1070   }
1071 
1072   p_msg = (tNFA_DM_API_REG_NDEF_HDLR*)GKI_getbuf(
1073       (uint16_t)(sizeof(tNFA_DM_API_REG_NDEF_HDLR) + uri_id_len));
1074   if (p_msg != nullptr) {
1075     p_msg->hdr.event = NFA_DM_API_REG_NDEF_HDLR_EVT;
1076 
1077     p_msg->flags = NFA_NDEF_FLAGS_WKT_URI;
1078 
1079     if (handle_whole_message) {
1080       p_msg->flags |= NFA_NDEF_FLAGS_HANDLE_WHOLE_MESSAGE;
1081     }
1082 
1083     /* abs_uri is only valid fir uri_id=NFA_NDEF_URI_ID_ABSOLUTE */
1084     if (uri_id != NFA_NDEF_URI_ID_ABSOLUTE) {
1085       uri_id_len = 0;
1086     }
1087 
1088     p_msg->tnf = NFA_TNF_WKT;
1089     p_msg->uri_id = uri_id;
1090     p_msg->name_len = uri_id_len;
1091     p_msg->p_ndef_cback = p_ndef_cback;
1092     memcpy(p_msg->name, p_abs_uri, uri_id_len);
1093 
1094     nfa_sys_sendmsg(p_msg);
1095 
1096     return (NFA_STATUS_OK);
1097   }
1098 
1099   return (NFA_STATUS_FAILED);
1100 }
1101 
1102 /*******************************************************************************
1103 **
1104 ** Function         NFA_DeregisterNDefTypeHandler
1105 **
1106 ** Description      Deregister NDEF record type handler.
1107 **
1108 ** Returns          NFA_STATUS_OK if successfully initiated
1109 **                  NFA_STATUS_FAILED otherwise
1110 **
1111 *******************************************************************************/
NFA_DeregisterNDefTypeHandler(tNFA_HANDLE ndef_type_handle)1112 extern tNFA_STATUS NFA_DeregisterNDefTypeHandler(tNFA_HANDLE ndef_type_handle) {
1113   tNFA_DM_API_DEREG_NDEF_HDLR* p_msg;
1114 
1115   DLOG_IF(INFO, nfc_debug_enabled)
1116       << StringPrintf("handle 0x%08x", ndef_type_handle);
1117 
1118   p_msg = (tNFA_DM_API_DEREG_NDEF_HDLR*)GKI_getbuf(
1119       (uint16_t)(sizeof(tNFA_DM_API_DEREG_NDEF_HDLR)));
1120   if (p_msg != nullptr) {
1121     p_msg->hdr.event = NFA_DM_API_DEREG_NDEF_HDLR_EVT;
1122     p_msg->ndef_type_handle = ndef_type_handle;
1123 
1124     nfa_sys_sendmsg(p_msg);
1125 
1126     return (NFA_STATUS_OK);
1127   }
1128 
1129   return (NFA_STATUS_FAILED);
1130 }
1131 
1132 /*******************************************************************************
1133 **
1134 ** Function         NFA_PowerOffSleepMode
1135 **
1136 ** Description      This function is called to enter or leave NFCC Power Off
1137 **                  Sleep mode NFA_DM_PWR_MODE_CHANGE_EVT will be sent to
1138 **                  indicate status.
1139 **
1140 **                  start_stop : TRUE if entering Power Off Sleep mode
1141 **                               FALSE if leaving Power Off Sleep mode
1142 **
1143 ** Returns          NFA_STATUS_OK if successfully initiated
1144 **                  NFA_STATUS_FAILED otherwise
1145 **
1146 *******************************************************************************/
NFA_PowerOffSleepMode(bool start_stop)1147 tNFA_STATUS NFA_PowerOffSleepMode(bool start_stop) {
1148   NFC_HDR* p_msg;
1149 
1150   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("start_stop=%d", start_stop);
1151 
1152   if (nfa_dm_cb.flags & NFA_DM_FLAGS_SETTING_PWR_MODE) {
1153     LOG(ERROR) << StringPrintf("NFA DM is busy to update power mode");
1154     return (NFA_STATUS_FAILED);
1155   } else {
1156     nfa_dm_cb.flags |= NFA_DM_FLAGS_SETTING_PWR_MODE;
1157   }
1158 
1159   p_msg = (NFC_HDR*)GKI_getbuf(sizeof(NFC_HDR));
1160   if (p_msg != nullptr) {
1161     p_msg->event = NFA_DM_API_POWER_OFF_SLEEP_EVT;
1162     p_msg->layer_specific = start_stop;
1163 
1164     nfa_sys_sendmsg(p_msg);
1165 
1166     return (NFA_STATUS_OK);
1167   }
1168 
1169   return (NFA_STATUS_FAILED);
1170 }
1171 
1172 /*******************************************************************************
1173 **
1174 ** Function         NFA_RegVSCback
1175 **
1176 ** Description      This function is called to register or de-register a
1177 **                  callback function to receive Proprietary NCI response and
1178 **                  notification events. The maximum number of callback
1179 **                  functions allowed is NFC_NUM_VS_CBACKS
1180 **
1181 ** Returns          tNFC_STATUS
1182 **
1183 *******************************************************************************/
NFA_RegVSCback(bool is_register,tNFA_VSC_CBACK * p_cback)1184 tNFC_STATUS NFA_RegVSCback(bool is_register, tNFA_VSC_CBACK* p_cback) {
1185   tNFA_DM_API_REG_VSC* p_msg;
1186 
1187   DLOG_IF(INFO, nfc_debug_enabled)
1188       << StringPrintf("is_register=%d", is_register);
1189 
1190   if (p_cback == nullptr) {
1191     LOG(ERROR) << StringPrintf("requires a valid callback function");
1192     return (NFA_STATUS_FAILED);
1193   }
1194 
1195   p_msg = (tNFA_DM_API_REG_VSC*)GKI_getbuf(sizeof(tNFA_DM_API_REG_VSC));
1196   if (p_msg != nullptr) {
1197     p_msg->hdr.event = NFA_DM_API_REG_VSC_EVT;
1198     p_msg->is_register = is_register;
1199     p_msg->p_cback = p_cback;
1200 
1201     nfa_sys_sendmsg(p_msg);
1202 
1203     return (NFA_STATUS_OK);
1204   }
1205 
1206   return (NFA_STATUS_FAILED);
1207 }
1208 
1209 /*******************************************************************************
1210 **
1211 ** Function         NFA_SendVsCommand
1212 **
1213 ** Description      This function is called to send an NCI Vendor Specific
1214 **                  command to NFCC.
1215 **
1216 **                  oid             - The opcode of the VS command.
1217 **                  cmd_params_len  - The command parameter len
1218 **                  p_cmd_params    - The command parameter
1219 **                  p_cback         - The callback function to receive the
1220 **                                    command status
1221 **
1222 ** Returns          NFA_STATUS_OK if successfully initiated
1223 **                  NFA_STATUS_FAILED otherwise
1224 **
1225 *******************************************************************************/
NFA_SendVsCommand(uint8_t oid,uint8_t cmd_params_len,uint8_t * p_cmd_params,tNFA_VSC_CBACK * p_cback)1226 tNFA_STATUS NFA_SendVsCommand(uint8_t oid, uint8_t cmd_params_len,
1227                               uint8_t* p_cmd_params, tNFA_VSC_CBACK* p_cback) {
1228   tNFA_DM_API_SEND_VSC* p_msg;
1229   uint16_t size = sizeof(tNFA_DM_API_SEND_VSC) + cmd_params_len;
1230 
1231   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("oid=0x%x", oid);
1232 
1233   p_msg = (tNFA_DM_API_SEND_VSC*)GKI_getbuf(size);
1234   if (p_msg != nullptr) {
1235     p_msg->hdr.event = NFA_DM_API_SEND_VSC_EVT;
1236     p_msg->oid = oid;
1237     p_msg->p_cback = p_cback;
1238     if (cmd_params_len && p_cmd_params) {
1239       p_msg->cmd_params_len = cmd_params_len;
1240       p_msg->p_cmd_params = (uint8_t*)(p_msg + 1);
1241       memcpy(p_msg->p_cmd_params, p_cmd_params, cmd_params_len);
1242     } else {
1243       p_msg->cmd_params_len = 0;
1244       p_msg->p_cmd_params = nullptr;
1245     }
1246 
1247     nfa_sys_sendmsg(p_msg);
1248 
1249     return (NFA_STATUS_OK);
1250   }
1251 
1252   return (NFA_STATUS_FAILED);
1253 }
1254 
1255 /*******************************************************************************
1256 **
1257 ** Function         NFA_SendRawVsCommand
1258 **
1259 ** Description      This function is called to send raw Vendor Specific
1260 **                  command to NFCC.
1261 **
1262 **                  cmd_params_len  - The command parameter len
1263 **                  p_cmd_params    - The command parameter
1264 **                  p_cback         - The callback function to receive the
1265 **                                    command
1266 **
1267 ** Returns          NFA_STATUS_OK if successfully initiated
1268 **                  NFA_STATUS_FAILED otherwise
1269 **
1270 *******************************************************************************/
NFA_SendRawVsCommand(uint8_t cmd_params_len,uint8_t * p_cmd_params,tNFA_VSC_CBACK * p_cback)1271 tNFA_STATUS NFA_SendRawVsCommand(uint8_t cmd_params_len, uint8_t* p_cmd_params,
1272                                  tNFA_VSC_CBACK* p_cback) {
1273   if (cmd_params_len == 0x00 || p_cmd_params == nullptr || p_cback == nullptr) {
1274     return NFA_STATUS_INVALID_PARAM;
1275   }
1276   uint16_t size = sizeof(tNFA_DM_API_SEND_VSC) + cmd_params_len;
1277   tNFA_DM_API_SEND_VSC* p_msg = (tNFA_DM_API_SEND_VSC*)GKI_getbuf(size);
1278 
1279   if (p_msg != nullptr) {
1280     p_msg->hdr.event = NFA_DM_API_SEND_RAW_VS_EVT;
1281     p_msg->p_cback = p_cback;
1282     p_msg->cmd_params_len = cmd_params_len;
1283     p_msg->p_cmd_params = (uint8_t*)(p_msg + 1);
1284     memcpy(p_msg->p_cmd_params, p_cmd_params, cmd_params_len);
1285 
1286     nfa_sys_sendmsg(p_msg);
1287 
1288     return NFA_STATUS_OK;
1289   }
1290 
1291   return NFA_STATUS_FAILED;
1292 }
1293 
1294 /*******************************************************************************
1295 **
1296 ** Function:        NFA_EnableDtamode
1297 **
1298 ** Description:     Enable DTA Mode
1299 **
1300 ** Returns:         none:
1301 **
1302 *******************************************************************************/
NFA_EnableDtamode(tNFA_eDtaModes eDtaMode)1303 void NFA_EnableDtamode(tNFA_eDtaModes eDtaMode) {
1304   DLOG_IF(INFO, nfc_debug_enabled)
1305       << StringPrintf("%s: 0x%x ", __func__, eDtaMode);
1306   appl_dta_mode_flag = 0x01;
1307   nfa_dm_cb.eDtaMode = eDtaMode;
1308 }
1309