• 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 to NFCEE - API functions
22  *
23  ******************************************************************************/
24 #include <android-base/stringprintf.h>
25 #include <base/logging.h>
26 
27 #include "nfa_dm_int.h"
28 #include "nfa_ee_api.h"
29 #include "nfa_ee_int.h"
30 #include "nfc_int.h"
31 
32 using android::base::StringPrintf;
33 
34 extern bool nfc_debug_enabled;
35 
36 /*****************************************************************************
37 **  APIs
38 *****************************************************************************/
39 /*******************************************************************************
40 **
41 ** Function         NFA_EeDiscover
42 **
43 ** Description      This function retrieves the NFCEE information from NFCC.
44 **                  The NFCEE information is reported in NFA_EE_DISCOVER_EVT.
45 **
46 **                  This function may be called when a system supports removable
47 **                  NFCEEs,
48 **
49 ** Returns          NFA_STATUS_OK if information is retrieved successfully
50 **                  NFA_STATUS_FAILED If wrong state (retry later)
51 **                  NFA_STATUS_INVALID_PARAM If bad parameter
52 **
53 *******************************************************************************/
NFA_EeDiscover(tNFA_EE_CBACK * p_cback)54 tNFA_STATUS NFA_EeDiscover(tNFA_EE_CBACK* p_cback) {
55   tNFA_EE_API_DISCOVER* p_msg;
56   tNFA_STATUS status = NFA_STATUS_FAILED;
57 
58   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
59 
60   if (nfa_ee_cb.em_state != NFA_EE_EM_STATE_INIT_DONE) {
61     LOG(ERROR) << StringPrintf("NFA_EeDiscover bad em state: %d",
62                                nfa_ee_cb.em_state);
63     status = NFA_STATUS_FAILED;
64   } else if ((nfa_ee_cb.p_ee_disc_cback != nullptr) || (p_cback == nullptr)) {
65     LOG(ERROR) << StringPrintf("in progress or NULL callback function");
66     status = NFA_STATUS_INVALID_PARAM;
67   } else {
68     p_msg = (tNFA_EE_API_DISCOVER*)GKI_getbuf(sizeof(tNFA_EE_API_DISCOVER));
69     if (p_msg != nullptr) {
70       p_msg->hdr.event = NFA_EE_API_DISCOVER_EVT;
71       p_msg->p_cback = p_cback;
72 
73       nfa_sys_sendmsg(p_msg);
74 
75       status = NFA_STATUS_OK;
76     }
77   }
78 
79   return status;
80 }
81 
82 /*******************************************************************************
83 **
84 ** Function         NFA_EeGetInfo
85 **
86 ** Description      This function retrieves the NFCEE information from NFA.
87 **                  The actual number of NFCEE is returned in p_num_nfcee
88 **                  and NFCEE information is returned in p_info
89 **
90 ** Returns          NFA_STATUS_OK if information is retrieved successfully
91 **                  NFA_STATUS_FAILED If wrong state (retry later)
92 **                  NFA_STATUS_INVALID_PARAM If bad parameter
93 **
94 *******************************************************************************/
NFA_EeGetInfo(uint8_t * p_num_nfcee,tNFA_EE_INFO * p_info)95 tNFA_STATUS NFA_EeGetInfo(uint8_t* p_num_nfcee, tNFA_EE_INFO* p_info) {
96   int xx, ret = nfa_ee_cb.cur_ee;
97   tNFA_EE_ECB* p_cb = nfa_ee_cb.ecb;
98   uint8_t max_ret;
99   uint8_t num_ret = 0;
100 
101   DLOG_IF(INFO, nfc_debug_enabled)
102       << StringPrintf("NFA_EeGetInfo em_state:%d cur_ee:%d", nfa_ee_cb.em_state,
103                       nfa_ee_cb.cur_ee);
104   /* validate parameters */
105   if (p_info == nullptr || p_num_nfcee == nullptr) {
106     LOG(ERROR) << StringPrintf("NFA_EeGetInfo bad parameter");
107     return (NFA_STATUS_INVALID_PARAM);
108   }
109   max_ret = *p_num_nfcee;
110   *p_num_nfcee = 0;
111   if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT) {
112     LOG(ERROR) << StringPrintf("NFA_EeGetInfo bad em state: %d",
113                                nfa_ee_cb.em_state);
114     return (NFA_STATUS_FAILED);
115   }
116 
117   /* compose output */
118   for (xx = 0; (xx < ret) && (num_ret < max_ret); xx++, p_cb++) {
119     DLOG_IF(INFO, nfc_debug_enabled)
120         << StringPrintf("xx:%d max_ret:%d, num_ret:%d ee_status:0x%x", xx,
121                         max_ret, num_ret, p_cb->ee_status);
122     if ((p_cb->ee_status & NFA_EE_STATUS_INT_MASK) ||
123         (p_cb->ee_status == NFA_EE_STATUS_REMOVED)) {
124       continue;
125     }
126     p_info->ee_handle = NFA_HANDLE_GROUP_EE | (tNFA_HANDLE)p_cb->nfcee_id;
127     p_info->ee_status = p_cb->ee_status;
128     p_info->num_interface = p_cb->num_interface;
129     p_info->num_tlvs = p_cb->num_tlvs;
130     p_info->la_protocol = p_cb->la_protocol;
131     p_info->lb_protocol = p_cb->lb_protocol;
132     p_info->lf_protocol = p_cb->lf_protocol;
133     memcpy(p_info->ee_interface, p_cb->ee_interface, p_cb->num_interface);
134     memcpy(p_info->ee_tlv, p_cb->ee_tlv, p_cb->num_tlvs * sizeof(tNFA_EE_TLV));
135     p_info->ee_power_supply_status = p_cb->ee_power_supply_status;
136     p_info++;
137     num_ret++;
138   }
139   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("num_ret:%d", num_ret);
140   *p_num_nfcee = num_ret;
141   return (NFA_STATUS_OK);
142 }
143 
144 /*******************************************************************************
145 **
146 ** Function         NFA_EeRegister
147 **
148 ** Description      This function registers a callback function to receive the
149 **                  events from NFA-EE module.
150 **
151 ** Returns          NFA_STATUS_OK if successfully initiated
152 **                  NFA_STATUS_FAILED otherwise
153 **                  NFA_STATUS_INVALID_PARAM If bad parameter
154 **
155 *******************************************************************************/
NFA_EeRegister(tNFA_EE_CBACK * p_cback)156 tNFA_STATUS NFA_EeRegister(tNFA_EE_CBACK* p_cback) {
157   tNFA_EE_API_REGISTER* p_msg;
158   tNFA_STATUS status = NFA_STATUS_FAILED;
159 
160   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
161 
162   if (p_cback == nullptr) {
163     LOG(ERROR) << StringPrintf("with NULL callback function");
164     status = NFA_STATUS_INVALID_PARAM;
165   } else {
166     p_msg = (tNFA_EE_API_REGISTER*)GKI_getbuf(sizeof(tNFA_EE_API_REGISTER));
167     if (p_msg != nullptr) {
168       p_msg->hdr.event = NFA_EE_API_REGISTER_EVT;
169       p_msg->p_cback = p_cback;
170 
171       nfa_sys_sendmsg(p_msg);
172 
173       status = NFA_STATUS_OK;
174     }
175   }
176 
177   return status;
178 }
179 
180 /*******************************************************************************
181 **
182 ** Function         NFA_EeDeregister
183 **
184 ** Description      This function de-registers the callback function
185 **
186 ** Returns          NFA_STATUS_OK if successfully initiated
187 **                  NFA_STATUS_FAILED otherwise
188 **                  NFA_STATUS_INVALID_PARAM If bad parameter
189 **
190 *******************************************************************************/
NFA_EeDeregister(tNFA_EE_CBACK * p_cback)191 tNFA_STATUS NFA_EeDeregister(tNFA_EE_CBACK* p_cback) {
192   tNFA_EE_API_DEREGISTER* p_msg;
193   tNFA_STATUS status = NFA_STATUS_INVALID_PARAM;
194   int index = NFA_EE_MAX_CBACKS;
195   int xx;
196 
197   for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++) {
198     if (nfa_ee_cb.p_ee_cback[xx] == p_cback) {
199       index = xx;
200       status = NFA_STATUS_FAILED;
201       break;
202     }
203   }
204 
205   DLOG_IF(INFO, nfc_debug_enabled)
206       << StringPrintf("%d, status:%d", index, status);
207   if ((status != NFA_STATUS_INVALID_PARAM) &&
208       (p_msg = (tNFA_EE_API_DEREGISTER*)GKI_getbuf(
209            sizeof(tNFA_EE_API_DEREGISTER))) != nullptr) {
210     p_msg->hdr.event = NFA_EE_API_DEREGISTER_EVT;
211     p_msg->index = index;
212 
213     nfa_sys_sendmsg(p_msg);
214 
215     status = NFA_STATUS_OK;
216   }
217 
218   return status;
219 }
220 
221 /*******************************************************************************
222 **
223 ** Function         NFA_EeModeSet
224 **
225 ** Description      This function is called to activate
226 **                  (mode = NFA_EE_MD_ACTIVATE) or deactivate
227 **                  (mode = NFA_EE_MD_DEACTIVATE) the NFCEE identified by the
228 **                  given ee_handle. The result of this operation is reported
229 **                  with the NFA_EE_MODE_SET_EVT.
230 **
231 ** Returns          NFA_STATUS_OK if successfully initiated
232 **                  NFA_STATUS_FAILED otherwise
233 **                  NFA_STATUS_INVALID_PARAM If bad parameter
234 **
235 *******************************************************************************/
NFA_EeModeSet(tNFA_HANDLE ee_handle,tNFA_EE_MD mode)236 tNFA_STATUS NFA_EeModeSet(tNFA_HANDLE ee_handle, tNFA_EE_MD mode) {
237   tNFA_EE_API_MODE_SET* p_msg;
238   tNFA_STATUS status = NFA_STATUS_FAILED;
239   tNFA_EE_ECB *p_cb, *p_found = nullptr;
240   uint32_t xx;
241   uint8_t nfcee_id = (ee_handle & 0xFF);
242 
243   p_cb = nfa_ee_cb.ecb;
244   for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++) {
245     if (nfcee_id == p_cb->nfcee_id) {
246       p_found = p_cb;
247       break;
248     }
249   }
250   DLOG_IF(INFO, nfc_debug_enabled)
251       << StringPrintf("handle:<0x%x>, mode:0x%02X", ee_handle, mode);
252 
253   if (p_found == nullptr) {
254     LOG(ERROR) << StringPrintf("invalid NFCEE:0x%04x", ee_handle);
255     status = NFA_STATUS_INVALID_PARAM;
256   } else {
257     p_msg = (tNFA_EE_API_MODE_SET*)GKI_getbuf(sizeof(tNFA_EE_API_MODE_SET));
258     if (p_msg != nullptr) {
259       p_msg->hdr.event = NFA_EE_API_MODE_SET_EVT;
260       p_msg->nfcee_id = nfcee_id;
261       p_msg->mode = mode;
262       p_msg->p_cb = p_found;
263 
264       nfa_sys_sendmsg(p_msg);
265 
266       status = NFA_STATUS_OK;
267     }
268   }
269 
270   return status;
271 }
272 
273 /*******************************************************************************
274 **
275 ** Function         NFA_EeSetDefaultTechRouting
276 **
277 ** Description      This function is called to add, change or remove the
278 **                  default routing based on RF technology in the listen mode
279 **                  routing table for the given ee_handle. The status of this
280 **                  operation is reported as the NFA_EE_SET_TECH_CFG_EVT.
281 **
282 ** Note:            If RF discovery is started,
283 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
284 **                  happen before calling this function
285 **
286 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
287 **                  function to change the listen mode routing is called.
288 **
289 ** Returns          NFA_STATUS_OK if successfully initiated
290 **                  NFA_STATUS_FAILED otherwise
291 **                  NFA_STATUS_INVALID_PARAM If bad parameter
292 **
293 *******************************************************************************/
NFA_EeSetDefaultTechRouting(tNFA_HANDLE ee_handle,tNFA_TECHNOLOGY_MASK technologies_switch_on,tNFA_TECHNOLOGY_MASK technologies_switch_off,tNFA_TECHNOLOGY_MASK technologies_battery_off,tNFA_TECHNOLOGY_MASK technologies_screen_lock,tNFA_TECHNOLOGY_MASK technologies_screen_off,tNFA_TECHNOLOGY_MASK technologies_screen_off_lock)294 tNFA_STATUS NFA_EeSetDefaultTechRouting(
295     tNFA_HANDLE ee_handle, tNFA_TECHNOLOGY_MASK technologies_switch_on,
296     tNFA_TECHNOLOGY_MASK technologies_switch_off,
297     tNFA_TECHNOLOGY_MASK technologies_battery_off,
298     tNFA_TECHNOLOGY_MASK technologies_screen_lock,
299     tNFA_TECHNOLOGY_MASK technologies_screen_off,
300     tNFA_TECHNOLOGY_MASK technologies_screen_off_lock) {
301   tNFA_EE_API_SET_TECH_CFG* p_msg;
302   tNFA_STATUS status = NFA_STATUS_FAILED;
303   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
304   tNFA_EE_ECB* p_cb;
305 
306   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
307       ""
308       "handle:<0x%x>technology_mask:<0x%x>/<0x%x>/<0x%x><0x%x><0x%x><0x%x>",
309       ee_handle, technologies_switch_on, technologies_switch_off,
310       technologies_battery_off, technologies_screen_lock,
311       technologies_screen_off, technologies_screen_off_lock);
312   p_cb = nfa_ee_find_ecb(nfcee_id);
313 
314   if (p_cb == nullptr) {
315     LOG(ERROR) << StringPrintf("Bad ee_handle");
316     status = NFA_STATUS_INVALID_PARAM;
317   } else {
318     p_msg =
319         (tNFA_EE_API_SET_TECH_CFG*)GKI_getbuf(sizeof(tNFA_EE_API_SET_TECH_CFG));
320     if (p_msg != nullptr) {
321       p_msg->hdr.event = NFA_EE_API_SET_TECH_CFG_EVT;
322       p_msg->nfcee_id = nfcee_id;
323       p_msg->p_cb = p_cb;
324       p_msg->technologies_switch_on = technologies_switch_on;
325       p_msg->technologies_switch_off = technologies_switch_off;
326       p_msg->technologies_battery_off = technologies_battery_off;
327       p_msg->technologies_screen_lock = technologies_screen_lock;
328       p_msg->technologies_screen_off = technologies_screen_off;
329       p_msg->technologies_screen_off_lock = technologies_screen_off_lock;
330 
331       nfa_sys_sendmsg(p_msg);
332 
333       status = NFA_STATUS_OK;
334     }
335   }
336 
337   return status;
338 }
339 
340 /*******************************************************************************
341 **
342 ** Function         NFA_EeClearDefaultTechRouting
343 **
344 ** Description      This function is called to remove the default routing based
345 **                  on RF technology in the listen mode routing table for the
346 **                  given ee_handle. The status of this operation is reported
347 **                  as the NFA_EE_CLEAR_TECH_CFG_EVT.
348 **
349 ** Note:            If RF discovery is started,
350 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
351 **                  happen before calling this function
352 **
353 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
354 **                  function to change the listen mode routing is called.
355 **
356 ** Returns          NFA_STATUS_OK if successfully initiated
357 **                  NFA_STATUS_FAILED otherwise
358 **                  NFA_STATUS_INVALID_PARAM If bad parameter
359 **
360 *******************************************************************************/
NFA_EeClearDefaultTechRouting(tNFA_HANDLE ee_handle,tNFA_TECHNOLOGY_MASK clear_technology)361 tNFA_STATUS NFA_EeClearDefaultTechRouting(
362     tNFA_HANDLE ee_handle, tNFA_TECHNOLOGY_MASK clear_technology) {
363   tNFA_EE_API_SET_TECH_CFG* p_msg;
364   tNFA_STATUS status = NFA_STATUS_FAILED;
365   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
366   tNFA_EE_ECB* p_cb;
367 
368   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
369       "handle:<0x%x>clear technology_mask:<0x%x>", ee_handle, clear_technology);
370   if (!clear_technology) {
371     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("nothing to clear");
372     status = NFA_STATUS_OK;
373     return status;
374   }
375 
376   p_cb = nfa_ee_find_ecb(nfcee_id);
377 
378   if (p_cb == nullptr) {
379     LOG(ERROR) << StringPrintf("Bad ee_handle");
380     status = NFA_STATUS_INVALID_PARAM;
381   } else {
382     p_msg = (tNFA_EE_API_CLEAR_TECH_CFG*)GKI_getbuf(
383         sizeof(tNFA_EE_API_CLEAR_TECH_CFG));
384     if (p_msg != nullptr) {
385       p_msg->hdr.event = NFA_EE_API_CLEAR_TECH_CFG_EVT;
386       p_msg->nfcee_id = nfcee_id;
387       p_msg->p_cb = p_cb;
388       p_msg->technologies_switch_on = clear_technology;
389       p_msg->technologies_switch_off = clear_technology;
390       p_msg->technologies_battery_off = clear_technology;
391       p_msg->technologies_screen_lock = clear_technology;
392       p_msg->technologies_screen_off = clear_technology;
393       p_msg->technologies_screen_off_lock = clear_technology;
394 
395       nfa_sys_sendmsg(p_msg);
396 
397       status = NFA_STATUS_OK;
398     }
399   }
400 
401   return status;
402 }
403 
404 /*******************************************************************************
405 **
406 ** Function         NFA_EeSetDefaultProtoRouting
407 **
408 ** Description      This function is called to add, change or remove the
409 **                  default routing based on Protocol in the listen mode routing
410 **                  table for the given ee_handle. The status of this
411 **                  operation is reported as the NFA_EE_SET_PROTO_CFG_EVT.
412 **
413 ** Note:            If RF discovery is started,
414 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
415 **                  happen before calling this function
416 **
417 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
418 **                  function to change the listen mode routing is called.
419 **
420 ** Returns          NFA_STATUS_OK if successfully initiated
421 **                  NFA_STATUS_FAILED otherwise
422 **                  NFA_STATUS_INVALID_PARAM If bad parameter
423 **
424 *******************************************************************************/
NFA_EeSetDefaultProtoRouting(tNFA_HANDLE ee_handle,tNFA_PROTOCOL_MASK protocols_switch_on,tNFA_PROTOCOL_MASK protocols_switch_off,tNFA_PROTOCOL_MASK protocols_battery_off,tNFA_PROTOCOL_MASK protocols_screen_lock,tNFA_PROTOCOL_MASK protocols_screen_off,tNFA_PROTOCOL_MASK protocols_screen_off_lock)425 tNFA_STATUS NFA_EeSetDefaultProtoRouting(
426     tNFA_HANDLE ee_handle, tNFA_PROTOCOL_MASK protocols_switch_on,
427     tNFA_PROTOCOL_MASK protocols_switch_off,
428     tNFA_PROTOCOL_MASK protocols_battery_off,
429     tNFA_PROTOCOL_MASK protocols_screen_lock,
430     tNFA_PROTOCOL_MASK protocols_screen_off,
431     tNFA_PROTOCOL_MASK protocols_screen_off_lock) {
432   tNFA_EE_API_SET_PROTO_CFG* p_msg;
433   tNFA_STATUS status = NFA_STATUS_FAILED;
434   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
435   tNFA_EE_ECB* p_cb;
436 
437   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
438       "handle:<0x%x>protocol_mask:<0x%x>/<0x%x>/<0x%x><0x%x><0x%x><0x%x>",
439       ee_handle, protocols_switch_on, protocols_switch_off,
440       protocols_battery_off, protocols_screen_lock, protocols_screen_off,
441       protocols_screen_off_lock);
442   p_cb = nfa_ee_find_ecb(nfcee_id);
443 
444   if (p_cb == nullptr) {
445     LOG(ERROR) << StringPrintf("Bad ee_handle");
446     status = NFA_STATUS_INVALID_PARAM;
447   } else {
448     p_msg = (tNFA_EE_API_SET_PROTO_CFG*)GKI_getbuf(
449         sizeof(tNFA_EE_API_SET_PROTO_CFG));
450     if (p_msg != nullptr) {
451       p_msg->hdr.event = NFA_EE_API_SET_PROTO_CFG_EVT;
452       p_msg->nfcee_id = nfcee_id;
453       p_msg->p_cb = p_cb;
454       p_msg->protocols_switch_on = protocols_switch_on;
455       p_msg->protocols_switch_off = protocols_switch_off;
456       p_msg->protocols_battery_off = protocols_battery_off;
457       p_msg->protocols_screen_lock = protocols_screen_lock;
458       p_msg->protocols_screen_off = protocols_screen_off;
459       p_msg->protocols_screen_off_lock = protocols_screen_off_lock;
460 
461       nfa_sys_sendmsg(p_msg);
462 
463       status = NFA_STATUS_OK;
464     }
465   }
466 
467   return status;
468 }
469 
470 /*******************************************************************************
471 **
472 ** Function         NFA_EeClearDefaultProtoRouting
473 **
474 ** Description      This function is called to remove the default routing based
475 **                  on RF technology in the listen mode routing table for the
476 **                  given ee_handle. The status of this operation is reported
477 **                  as the NFA_EE_CLEAR_TECH_CFG_EVT.
478 **
479 ** Note:            If RF discovery is started,
480 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
481 **                  happen before calling this function
482 **
483 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
484 **                  function to change the listen mode routing is called.
485 **
486 ** Returns          NFA_STATUS_OK if successfully initiated
487 **                  NFA_STATUS_FAILED otherwise
488 **                  NFA_STATUS_INVALID_PARAM If bad parameter
489 **
490 *******************************************************************************/
NFA_EeClearDefaultProtoRouting(tNFA_HANDLE ee_handle,tNFA_PROTOCOL_MASK clear_protocol)491 tNFA_STATUS NFA_EeClearDefaultProtoRouting(tNFA_HANDLE ee_handle,
492                                            tNFA_PROTOCOL_MASK clear_protocol) {
493   tNFA_EE_API_SET_PROTO_CFG* p_msg;
494   tNFA_STATUS status = NFA_STATUS_FAILED;
495   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
496   tNFA_EE_ECB* p_cb;
497 
498   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
499       "handle:<0x%x>clear protocol_mask:<0x%x>", ee_handle, clear_protocol);
500   if (!clear_protocol) {
501     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("nothing to clear");
502     status = NFA_STATUS_OK;
503     return status;
504   }
505 
506   p_cb = nfa_ee_find_ecb(nfcee_id);
507 
508   if (p_cb == nullptr) {
509     LOG(ERROR) << StringPrintf("Bad ee_handle");
510     status = NFA_STATUS_INVALID_PARAM;
511   } else {
512     p_msg = (tNFA_EE_API_SET_PROTO_CFG*)GKI_getbuf(
513         sizeof(tNFA_EE_API_SET_PROTO_CFG));
514     if (p_msg != nullptr) {
515       p_msg->hdr.event = NFA_EE_API_CLEAR_PROTO_CFG_EVT;
516       p_msg->nfcee_id = nfcee_id;
517       p_msg->p_cb = p_cb;
518       p_msg->protocols_switch_on = clear_protocol;
519       p_msg->protocols_switch_off = clear_protocol;
520       p_msg->protocols_battery_off = clear_protocol;
521       p_msg->protocols_screen_lock = clear_protocol;
522       p_msg->protocols_screen_off = clear_protocol;
523       p_msg->protocols_screen_off_lock = clear_protocol;
524 
525       nfa_sys_sendmsg(p_msg);
526 
527       status = NFA_STATUS_OK;
528     }
529   }
530 
531   return status;
532 }
533 
534 /*******************************************************************************
535 **
536 ** Function         NFA_EeAddAidRouting
537 **
538 ** Description      This function is called to add an AID entry in the
539 **                  listen mode routing table in NFCC. The status of this
540 **                  operation is reported as the NFA_EE_ADD_AID_EVT.
541 **
542 ** Note:            If RF discovery is started,
543 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
544 **                  happen before calling this function
545 **
546 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
547 **                  function to change the listen mode routing is called.
548 **
549 ** Returns          NFA_STATUS_OK if successfully initiated
550 **                  NFA_STATUS_FAILED otherwise
551 **                  NFA_STATUS_INVALID_PARAM If bad parameter
552 **
553 *******************************************************************************/
NFA_EeAddAidRouting(tNFA_HANDLE ee_handle,uint8_t aid_len,uint8_t * p_aid,tNFA_EE_PWR_STATE power_state,uint8_t aidInfo)554 tNFA_STATUS NFA_EeAddAidRouting(tNFA_HANDLE ee_handle, uint8_t aid_len,
555                                 uint8_t* p_aid, tNFA_EE_PWR_STATE power_state,
556                                 uint8_t aidInfo) {
557   tNFA_EE_API_ADD_AID* p_msg;
558   tNFA_STATUS status = NFA_STATUS_FAILED;
559   uint16_t size = sizeof(tNFA_EE_API_ADD_AID) + aid_len;
560   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
561   tNFA_EE_ECB* p_cb;
562 
563   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:<0x%x>", ee_handle);
564   p_cb = nfa_ee_find_ecb(nfcee_id);
565 
566   /* validate parameters - make sure the AID is in valid length range */
567   if ((p_cb == nullptr) ||
568       ((NFA_GetNCIVersion() == NCI_VERSION_2_0) && (aid_len != 0) &&
569        (p_aid == nullptr)) ||
570       ((NFA_GetNCIVersion() != NCI_VERSION_2_0) &&
571        ((aid_len == 0) || (p_aid == nullptr) || (aid_len < NFA_MIN_AID_LEN))) ||
572       (aid_len > NFA_MAX_AID_LEN)) {
573     LOG(ERROR) << StringPrintf("Bad ee_handle or AID (len=%d)", aid_len);
574     status = NFA_STATUS_INVALID_PARAM;
575   } else {
576     p_msg = (tNFA_EE_API_ADD_AID*)GKI_getbuf(size);
577     if (p_msg != nullptr) {
578       if (p_aid != nullptr)
579         DLOG_IF(INFO, nfc_debug_enabled)
580             << StringPrintf("aid:<%02x%02x>", p_aid[0], p_aid[1]);
581       p_msg->hdr.event = NFA_EE_API_ADD_AID_EVT;
582       p_msg->nfcee_id = nfcee_id;
583       p_msg->p_cb = p_cb;
584       p_msg->aid_len = aid_len;
585       p_msg->power_state = power_state;
586       p_msg->p_aid = (uint8_t*)(p_msg + 1);
587       p_msg->aidInfo = aidInfo;
588       if (p_aid != nullptr) memcpy(p_msg->p_aid, p_aid, aid_len);
589 
590       nfa_sys_sendmsg(p_msg);
591 
592       status = NFA_STATUS_OK;
593     }
594   }
595 
596   return status;
597 }
598 
599 /*******************************************************************************
600 **
601 ** Function         NFA_EeRemoveAidRouting
602 **
603 ** Description      This function is called to remove the given AID entry from
604 **                  the listen mode routing table. If the entry configures VS,
605 **                  it is also removed. The status of this operation is reported
606 **                  as the NFA_EE_REMOVE_AID_EVT.
607 **
608 ** Note:            If RF discovery is started,
609 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
610 **                  happen before calling this function
611 **
612 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
613 **                  function to change the listen mode routing is called.
614 **
615 ** Returns          NFA_STATUS_OK if successfully initiated
616 **                  NFA_STATUS_FAILED otherwise
617 **                  NFA_STATUS_INVALID_PARAM If bad parameter
618 **
619 *******************************************************************************/
NFA_EeRemoveAidRouting(uint8_t aid_len,uint8_t * p_aid)620 tNFA_STATUS NFA_EeRemoveAidRouting(uint8_t aid_len, uint8_t* p_aid) {
621   tNFA_EE_API_REMOVE_AID* p_msg;
622   tNFA_STATUS status = NFA_STATUS_FAILED;
623   uint16_t size = sizeof(tNFA_EE_API_REMOVE_AID) + aid_len;
624 
625   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
626   if (((NFA_GetNCIVersion() == NCI_VERSION_2_0) && (aid_len != 0) &&
627        (p_aid == nullptr)) ||
628       ((NFA_GetNCIVersion() != NCI_VERSION_2_0) &&
629        ((aid_len == 0) || (p_aid == nullptr) || (aid_len < NFA_MIN_AID_LEN))) ||
630       (aid_len > NFA_MAX_AID_LEN)) {
631     LOG(ERROR) << StringPrintf("Bad AID");
632     status = NFA_STATUS_INVALID_PARAM;
633   } else {
634     p_msg = (tNFA_EE_API_REMOVE_AID*)GKI_getbuf(size);
635     if (p_msg != nullptr) {
636       p_msg->hdr.event = NFA_EE_API_REMOVE_AID_EVT;
637       p_msg->aid_len = aid_len;
638       p_msg->p_aid = (uint8_t*)(p_msg + 1);
639       memcpy(p_msg->p_aid, p_aid, aid_len);
640 
641       nfa_sys_sendmsg(p_msg);
642 
643       status = NFA_STATUS_OK;
644     }
645   }
646 
647   return status;
648 }
649 
650 /*******************************************************************************
651 **
652 ** Function         NFA_EeAddSystemCodeRouting
653 **
654 ** Description      This function is called to add an system code entry in the
655 **                  listen mode routing table in NFCC. The status of this
656 **                  operation is reported as the NFA_EE_ADD_SYSCODE_EVT.
657 **
658 ** Note:            If RF discovery is started,
659 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
660 **                  happen before calling this function
661 **
662 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
663 **                  function to change the listen mode routing is called.
664 **
665 ** Returns          NFA_STATUS_OK if successfully initiated
666 **                  NFA_STATUS_FAILED otherwise
667 **                  NFA_STATUS_INVALID_PARAM If bad parameter
668 **
669 *******************************************************************************/
NFA_EeAddSystemCodeRouting(uint16_t systemcode,tNFA_HANDLE ee_handle,tNFA_EE_PWR_STATE power_state)670 tNFA_STATUS NFA_EeAddSystemCodeRouting(uint16_t systemcode,
671                                        tNFA_HANDLE ee_handle,
672                                        tNFA_EE_PWR_STATE power_state) {
673   tNFA_STATUS status = NFA_STATUS_FAILED;
674   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
675   DLOG_IF(INFO, nfc_debug_enabled)
676       << StringPrintf("NFA_EeAddSystemCodeRouting(): handle:<0x%x>", ee_handle);
677   tNFA_EE_ECB* p_cb = nfa_ee_find_ecb(nfcee_id);
678 
679   if (p_cb == nullptr || systemcode == 0) {
680     LOG(ERROR) << StringPrintf("Bad ee_handle or System Code");
681     status = NFA_STATUS_INVALID_PARAM;
682   } else if ((NFA_GetNCIVersion() != NCI_VERSION_2_0) &&
683              (nfc_cb.isScbrSupported == false)) {
684     LOG(ERROR) << StringPrintf("Invalid NCI Version/SCBR not supported");
685     status = NFA_STATUS_NOT_SUPPORTED;
686   } else {
687     tNFA_EE_API_ADD_SYSCODE* p_msg =
688         (tNFA_EE_API_ADD_SYSCODE*)GKI_getbuf(sizeof(tNFA_EE_API_ADD_SYSCODE));
689     if (p_msg != nullptr) {
690       p_msg->hdr.event = NFA_EE_API_ADD_SYSCODE_EVT;
691       p_msg->power_state = power_state;
692       p_msg->nfcee_id = nfcee_id;
693       p_msg->p_cb = p_cb;
694       // adjust endianness of syscode
695       p_msg->syscode = (systemcode & 0x00FF) << 8 | (systemcode & 0xFF00) >> 8;
696       nfa_sys_sendmsg(p_msg);
697       status = NFA_STATUS_OK;
698     }
699   }
700   return status;
701 }
702 
703 /*******************************************************************************
704 **
705 ** Function         NFA_EeRemoveSystemCodeRouting
706 **
707 ** Description      This function is called to remove the given System Code
708 **                  based entry from the listen mode routing table. The status
709 **                  of this operation is reported as the
710 **                  NFA_EE_REMOVE_SYSCODE_EVT.
711 **
712 ** Note:            If RF discovery is started,
713 **                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
714 **                  happen before calling this function
715 **
716 ** Note:            NFA_EeUpdateNow() should be called after last NFA-EE
717 **                  function to change the listen mode routing is called.
718 **
719 ** Returns          NFA_STATUS_OK if successfully initiated
720 **                  NFA_STATUS_FAILED otherwise
721 **                  NFA_STATUS_INVALID_PARAM If bad parameter
722 **
723 *******************************************************************************/
NFA_EeRemoveSystemCodeRouting(uint16_t systemcode)724 tNFA_STATUS NFA_EeRemoveSystemCodeRouting(uint16_t systemcode) {
725   tNFA_STATUS status = NFA_STATUS_FAILED;
726 
727   if (systemcode == 0) {
728     LOG(ERROR) << "Bad ee_handle or System Code";
729     status = NFA_STATUS_INVALID_PARAM;
730   } else if ((NFA_GetNCIVersion() != NCI_VERSION_2_0) &&
731              (nfc_cb.isScbrSupported == false)) {
732     LOG(ERROR) << "Invalid NCI Version/SCBR Not supported";
733     status = NFA_STATUS_NOT_SUPPORTED;
734   } else {
735     tNFA_EE_API_REMOVE_SYSCODE* p_msg = (tNFA_EE_API_REMOVE_SYSCODE*)GKI_getbuf(
736         sizeof(tNFA_EE_API_REMOVE_SYSCODE));
737     if (p_msg != nullptr) {
738       p_msg->hdr.event = NFA_EE_API_REMOVE_SYSCODE_EVT;
739       p_msg->syscode = (systemcode & 0x00FF) << 8 | (systemcode & 0xFF00) >> 8;
740       nfa_sys_sendmsg(p_msg);
741       status = NFA_STATUS_OK;
742     }
743   }
744   return status;
745 }
746 
747 /*******************************************************************************
748 **
749 ** Function         NFA_GetAidTableSize
750 **
751 ** Description      This function is called to get the Maximum AID routing table
752 *size.
753 **
754 ** Returns          AID routing table maximum size
755 **
756 *******************************************************************************/
NFA_GetAidTableSize()757 uint16_t NFA_GetAidTableSize() { return nfa_ee_find_max_aid_cfg_len(); }
758 
759 /*******************************************************************************
760 **
761 ** Function         NFA_EeGetLmrtRemainingSize
762 **
763 ** Description      This function is called to get remaining size of the
764 **                  Listen Mode Routing Table.
765 **                  The remaining size is reported in NFA_EE_REMAINING_SIZE_EVT
766 **
767 ** Returns          NFA_STATUS_OK if successfully initiated
768 **                  NFA_STATUS_FAILED otherwise
769 **
770 *******************************************************************************/
NFA_EeGetLmrtRemainingSize(void)771 tNFA_STATUS NFA_EeGetLmrtRemainingSize(void) {
772   tNFA_EE_API_LMRT_SIZE* p_msg;
773   tNFA_STATUS status = NFA_STATUS_FAILED;
774 
775   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
776   p_msg = (tNFA_EE_API_LMRT_SIZE*)GKI_getbuf(sizeof(tNFA_EE_API_LMRT_SIZE));
777   if (p_msg != nullptr) {
778     p_msg->event = NFA_EE_API_LMRT_SIZE_EVT;
779     nfa_sys_sendmsg(p_msg);
780     status = NFA_STATUS_OK;
781   }
782 
783   return status;
784 }
785 
786 /******************************************************************************
787 **
788 ** Function         NFA_EeUpdateNow
789 **
790 ** Description      This function is called to send the current listen mode
791 **                  routing table and VS configuration to the NFCC (without
792 **                  waiting for NFA_EE_ROUT_TIMEOUT_VAL).
793 **
794 **                  The status of this operation is
795 **                  reported with the NFA_EE_UPDATED_EVT.
796 **
797 ** Returns          NFA_STATUS_OK if successfully initiated
798 **                  NFA_STATUS_SEMANTIC_ERROR is update is currently in progress
799 **                  NFA_STATUS_FAILED otherwise
800 **
801 *******************************************************************************/
NFA_EeUpdateNow(void)802 tNFA_STATUS NFA_EeUpdateNow(void) {
803   NFC_HDR* p_msg;
804   tNFA_STATUS status = NFA_STATUS_FAILED;
805 
806   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
807   if (nfa_ee_cb.ee_wait_evt & NFA_EE_WAIT_UPDATE_ALL) {
808     LOG(ERROR) << StringPrintf("update in progress");
809     status = NFA_STATUS_SEMANTIC_ERROR;
810   } else {
811     p_msg = (NFC_HDR*)GKI_getbuf(NFC_HDR_SIZE);
812     if (p_msg != nullptr) {
813       p_msg->event = NFA_EE_API_UPDATE_NOW_EVT;
814 
815       nfa_sys_sendmsg(p_msg);
816 
817       status = NFA_STATUS_OK;
818     }
819   }
820 
821   return status;
822 }
823 
824 /*******************************************************************************
825 **
826 ** Function         NFA_EeConnect
827 **
828 ** Description      Open connection to an NFCEE attached to the NFCC
829 **
830 **                  The status of this operation is
831 **                  reported with the NFA_EE_CONNECT_EVT.
832 **
833 ** Returns          NFA_STATUS_OK if successfully initiated
834 **                  NFA_STATUS_FAILED otherwise
835 **                  NFA_STATUS_INVALID_PARAM If bad parameter
836 **
837 *******************************************************************************/
NFA_EeConnect(tNFA_HANDLE ee_handle,uint8_t ee_interface,tNFA_EE_CBACK * p_cback)838 tNFA_STATUS NFA_EeConnect(tNFA_HANDLE ee_handle, uint8_t ee_interface,
839                           tNFA_EE_CBACK* p_cback) {
840   tNFA_EE_API_CONNECT* p_msg;
841   tNFA_STATUS status = NFA_STATUS_FAILED;
842   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
843   tNFA_EE_ECB* p_cb;
844 
845   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
846       "handle:<0x%x> ee_interface:0x%x", ee_handle, ee_interface);
847   p_cb = nfa_ee_find_ecb(nfcee_id);
848 
849   if ((p_cb == nullptr) || (p_cback == nullptr)) {
850     LOG(ERROR) << StringPrintf("Bad ee_handle or NULL callback function");
851     status = NFA_STATUS_INVALID_PARAM;
852   } else {
853     p_msg = (tNFA_EE_API_CONNECT*)GKI_getbuf(sizeof(tNFA_EE_API_CONNECT));
854     if (p_msg != nullptr) {
855       p_msg->hdr.event = NFA_EE_API_CONNECT_EVT;
856       p_msg->nfcee_id = nfcee_id;
857       p_msg->p_cb = p_cb;
858       p_msg->ee_interface = ee_interface;
859       p_msg->p_cback = p_cback;
860 
861       nfa_sys_sendmsg(p_msg);
862 
863       status = NFA_STATUS_OK;
864     }
865   }
866 
867   return status;
868 }
869 
870 /*******************************************************************************
871 **
872 ** Function         NFA_EeSendData
873 **
874 ** Description      Send data to the given NFCEE.
875 **                  This function shall be called after NFA_EE_CONNECT_EVT is
876 **                  reported and before NFA_EeDisconnect is called on the given
877 **                  ee_handle.
878 **
879 ** Returns          NFA_STATUS_OK if successfully initiated
880 **                  NFA_STATUS_FAILED otherwise
881 **                  NFA_STATUS_INVALID_PARAM If bad parameter
882 **
883 *******************************************************************************/
NFA_EeSendData(tNFA_HANDLE ee_handle,uint16_t data_len,uint8_t * p_data)884 tNFA_STATUS NFA_EeSendData(tNFA_HANDLE ee_handle, uint16_t data_len,
885                            uint8_t* p_data) {
886   tNFA_EE_API_SEND_DATA* p_msg;
887   tNFA_STATUS status = NFA_STATUS_FAILED;
888   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
889   tNFA_EE_ECB* p_cb;
890 
891   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:<0x%x>", ee_handle);
892 
893   p_cb = nfa_ee_find_ecb(nfcee_id);
894 
895   if ((p_cb == nullptr) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN) ||
896       (p_data == nullptr)) {
897     LOG(ERROR) << StringPrintf("Bad ee_handle or NULL data");
898     status = NFA_STATUS_INVALID_PARAM;
899   } else {
900     p_msg = (tNFA_EE_API_SEND_DATA*)GKI_getbuf(
901         (uint16_t)(sizeof(tNFA_EE_API_SEND_DATA) + data_len));
902     if (p_msg != nullptr) {
903       p_msg->hdr.event = NFA_EE_API_SEND_DATA_EVT;
904       p_msg->nfcee_id = nfcee_id;
905       p_msg->p_cb = p_cb;
906       p_msg->data_len = data_len;
907       p_msg->p_data = (uint8_t*)(p_msg + 1);
908       memcpy(p_msg->p_data, p_data, data_len);
909 
910       nfa_sys_sendmsg(p_msg);
911 
912       status = NFA_STATUS_OK;
913     }
914   }
915 
916   return status;
917 }
918 
919 /*******************************************************************************
920 **
921 ** Function         NFA_EeDisconnect
922 **
923 ** Description      Disconnect (if a connection is currently open) from an
924 **                  NFCEE interface. The result of this operation is reported
925 **                  with the NFA_EE_DISCONNECT_EVT.
926 **
927 ** Returns          NFA_STATUS_OK if successfully initiated
928 **                  NFA_STATUS_FAILED otherwise
929 **                  NFA_STATUS_INVALID_PARAM If bad parameter
930 **
931 *******************************************************************************/
NFA_EeDisconnect(tNFA_HANDLE ee_handle)932 tNFA_STATUS NFA_EeDisconnect(tNFA_HANDLE ee_handle) {
933   tNFA_EE_API_DISCONNECT* p_msg;
934   tNFA_STATUS status = NFA_STATUS_FAILED;
935   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
936   tNFA_EE_ECB* p_cb;
937 
938   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:<0x%x>", ee_handle);
939   p_cb = nfa_ee_find_ecb(nfcee_id);
940 
941   if ((p_cb == nullptr) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN)) {
942     LOG(ERROR) << StringPrintf("Bad ee_handle");
943     status = NFA_STATUS_INVALID_PARAM;
944   } else {
945     p_msg = (tNFA_EE_API_DISCONNECT*)GKI_getbuf(sizeof(tNFA_EE_API_DISCONNECT));
946     if (p_msg != nullptr) {
947       p_msg->hdr.event = NFA_EE_API_DISCONNECT_EVT;
948       p_msg->nfcee_id = nfcee_id;
949       p_msg->p_cb = p_cb;
950 
951       nfa_sys_sendmsg(p_msg);
952 
953       status = NFA_STATUS_OK;
954     }
955   }
956 
957   return status;
958 }
959 
960 /*******************************************************************************
961 **
962 ** Function         NFA_EePowerAndLinkCtrl
963 **
964 ** Description      This Control Message is used by the DH to constrain the way
965 **                  the NFCC manages the power supply and communication links
966 **                  between the NFCC and its connected NFCEEs.
967 **
968 ** Returns          NFA_STATUS_OK if successfully initiated
969 **                  NFA_STATUS_FAILED otherwise
970 **                  NFA_STATUS_INVALID_PARAM If bad parameter
971 **
972 *******************************************************************************/
NFA_EePowerAndLinkCtrl(tNFA_HANDLE ee_handle,uint8_t config)973 tNFA_STATUS NFA_EePowerAndLinkCtrl(tNFA_HANDLE ee_handle, uint8_t config) {
974   tNFA_EE_API_PWR_AND_LINK_CTRL* p_msg;
975   tNFA_STATUS status = NFA_STATUS_FAILED;
976   uint8_t nfcee_id = (uint8_t)(ee_handle & 0xFF);
977   tNFA_EE_ECB* p_cb;
978 
979   DLOG_IF(INFO, nfc_debug_enabled)
980       << StringPrintf("handle:<0x%x>, config:<0x%x>", ee_handle, config);
981   p_cb = nfa_ee_find_ecb(nfcee_id);
982 
983   if ((p_cb == nullptr) || (p_cb->ee_status != NFA_EE_STATUS_ACTIVE)) {
984     LOG(ERROR) << StringPrintf("Bad ee_handle");
985     status = NFA_STATUS_INVALID_PARAM;
986   } else {
987     p_msg = (tNFA_EE_API_PWR_AND_LINK_CTRL*)GKI_getbuf(
988         sizeof(tNFA_EE_API_PWR_AND_LINK_CTRL));
989     if (p_msg != nullptr) {
990       p_msg->hdr.event = NFA_EE_API_PWR_AND_LINK_CTRL_EVT;
991       p_msg->nfcee_id = nfcee_id;
992       p_msg->config = config;
993 
994       nfa_sys_sendmsg(p_msg);
995 
996       status = NFA_STATUS_OK;
997     }
998   }
999 
1000   return status;
1001 }
1002