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