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