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