1 /******************************************************************************
2 *
3 * Copyright (C) 2011-2014 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * NFA interface for card emulation
22 *
23 ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <string.h>
27
28 #include "nfa_api.h"
29 #include "nfa_ce_int.h"
30
31 using android::base::StringPrintf;
32
33 /*******************************************************************************
34 **
35 ** Function nfa_ce_api_deregister_listen
36 **
37 ** Description Internal function called by listening for Felica system
38 ** code, ISO-DEP AID, or UICC technology
39 **
40 ** Returns:
41 ** NFA_STATUS_OK, if command accepted
42 ** NFA_STATUS_BAD_HANDLE invalid handle
43 ** NFA_STATUS_FAILED: otherwise
44 **
45 *******************************************************************************/
nfa_ce_api_deregister_listen(tNFA_HANDLE handle,uint32_t listen_info)46 tNFA_STATUS nfa_ce_api_deregister_listen(tNFA_HANDLE handle,
47 uint32_t listen_info) {
48 tNFA_CE_MSG* p_ce_msg;
49
50 /* Validate handle */
51 if ((listen_info != NFA_CE_LISTEN_INFO_UICC) &&
52 ((handle & NFA_HANDLE_GROUP_MASK) != NFA_HANDLE_GROUP_CE)) {
53 LOG(ERROR) << StringPrintf("nfa_ce_api_reregister_listen: Invalid handle");
54 return (NFA_STATUS_BAD_HANDLE);
55 }
56
57 p_ce_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)(sizeof(tNFA_CE_MSG)));
58 if (p_ce_msg != nullptr) {
59 p_ce_msg->hdr.event = NFA_CE_API_DEREG_LISTEN_EVT;
60 p_ce_msg->dereg_listen.handle = handle;
61 p_ce_msg->dereg_listen.listen_info = listen_info;
62
63 nfa_sys_sendmsg(p_ce_msg);
64
65 return (NFA_STATUS_OK);
66 } else {
67 LOG(ERROR) << StringPrintf("nfa_ce_api_reregister_listen: Out of buffers");
68 return (NFA_STATUS_FAILED);
69 }
70 }
71
72 /*****************************************************************************
73 ** APIs
74 *****************************************************************************/
75
76 /*******************************************************************************
77 **
78 ** Function NFA_CeConfigureLocalTag
79 **
80 ** Description Configure local NDEF tag.
81 **
82 ** Tag events will be notifed using the tNFA_CONN_CBACK
83 ** (registered during NFA_Enable)
84 **
85 ** The NFA_CE_LOCAL_TAG_CONFIGURED_EVT reports the status of
86 ** the operation.
87 **
88 ** Activation and deactivation are reported using the
89 ** NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT events
90 **
91 ** If a write-request is received to update the tag memory,
92 ** an NFA_CE_NDEF_WRITE_CPLT_EVT will notify the application,
93 ** along with a buffer containing the updated contents.
94 **
95 ** To disable the local NDEF tag, set protocol_mask=0
96 **
97 ** The NDEF data provided by p_ndef_data must be persistent
98 ** as long as the local NDEF tag is enabled.
99 **
100 **
101 ** Note: If RF discovery is started,
102 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
103 ** happen before calling this function. Also, Input parameters
104 ** p_uid and uid_len are reserved for future use.
105 **
106 ** Returns:
107 ** NFA_STATUS_OK, if command accepted
108 ** NFA_STATUS_INVALID_PARAM,
109 ** if protocol_maks is not 0 and p_ndef_data is NULL
110 ** (or)uid_len is not 0
111 ** (or)if protocol mask is set for Type 1 or Type 2
112 **
113 ** NFA_STATUS_FAILED: otherwise
114 **
115 *******************************************************************************/
NFA_CeConfigureLocalTag(tNFA_PROTOCOL_MASK protocol_mask,uint8_t * p_ndef_data,uint16_t ndef_cur_size,uint16_t ndef_max_size,bool read_only,uint8_t uid_len,uint8_t * p_uid)116 tNFA_STATUS NFA_CeConfigureLocalTag(tNFA_PROTOCOL_MASK protocol_mask,
117 uint8_t* p_ndef_data,
118 uint16_t ndef_cur_size,
119 uint16_t ndef_max_size, bool read_only,
120 uint8_t uid_len, uint8_t* p_uid)
121
122 {
123 tNFA_CE_MSG* p_msg;
124
125 LOG(VERBOSE) << __func__;
126
127 if (protocol_mask) {
128 /* If any protocols are specified, then NDEF buffer pointer must be non-NULL
129 */
130 if (p_ndef_data == nullptr) {
131 LOG(ERROR) << StringPrintf(
132 "NFA_CeConfigureLocalTag: NULL ndef data pointer");
133 return (NFA_STATUS_INVALID_PARAM);
134 }
135
136 if ((protocol_mask & NFA_PROTOCOL_MASK_T1T) ||
137 (protocol_mask & NFA_PROTOCOL_MASK_T2T)) {
138 LOG(ERROR) << StringPrintf(
139 "NFA_CeConfigureLocalTag: Cannot emulate Type 1 / Type 2 tag");
140 return (NFA_STATUS_INVALID_PARAM);
141 }
142
143 if (uid_len) {
144 LOG(ERROR) << StringPrintf(
145 "NFA_CeConfigureLocalTag: Cannot Set UID for Protocol_mask: 0x%x",
146 protocol_mask);
147 return (NFA_STATUS_INVALID_PARAM);
148 }
149 }
150 p_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_CE_MSG));
151 if (p_msg != nullptr) {
152 p_msg->local_tag.hdr.event = NFA_CE_API_CFG_LOCAL_TAG_EVT;
153
154 /* Copy ndef info */
155 p_msg->local_tag.protocol_mask = protocol_mask;
156 p_msg->local_tag.p_ndef_data = p_ndef_data;
157 p_msg->local_tag.ndef_cur_size = ndef_cur_size;
158 p_msg->local_tag.ndef_max_size = ndef_max_size;
159 p_msg->local_tag.read_only = read_only;
160 p_msg->local_tag.uid_len = uid_len;
161
162 if (uid_len) memcpy(p_msg->local_tag.uid, p_uid, uid_len);
163
164 nfa_sys_sendmsg(p_msg);
165
166 return (NFA_STATUS_OK);
167 }
168
169 return (NFA_STATUS_FAILED);
170 }
171
172 /*******************************************************************************
173 **
174 ** Function NFA_CeConfigureUiccListenTech
175 **
176 ** Description Configure listening for the UICC, using the specified
177 ** technologies.
178 **
179 ** Events will be notifed using the tNFA_CONN_CBACK
180 ** (registered during NFA_Enable)
181 **
182 ** The NFA_CE_UICC_LISTEN_CONFIGURED_EVT reports the status of
183 ** the operation.
184 **
185 ** Activation and deactivation are reported using the
186 ** NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT events
187 **
188 ** Note: If RF discovery is started,
189 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
190 ** happen before calling this function
191 **
192 ** Returns:
193 ** NFA_STATUS_OK, if command accepted
194 ** NFA_STATUS_FAILED: otherwise
195 **
196 *******************************************************************************/
NFA_CeConfigureUiccListenTech(tNFA_HANDLE ee_handle,tNFA_TECHNOLOGY_MASK tech_mask)197 tNFA_STATUS NFA_CeConfigureUiccListenTech(tNFA_HANDLE ee_handle,
198 tNFA_TECHNOLOGY_MASK tech_mask) {
199 #if (NFC_NFCEE_INCLUDED == TRUE)
200 tNFA_CE_MSG* p_msg;
201
202 LOG(VERBOSE) << StringPrintf("ee_handle = 0x%x", ee_handle);
203
204 /* If tech_mask is zero, then app is disabling listening for specified uicc */
205 if (tech_mask == 0) {
206 return (nfa_ce_api_deregister_listen(ee_handle, NFA_CE_LISTEN_INFO_UICC));
207 }
208
209 /* Otherwise then app is configuring uicc listen for the specificed
210 * technologies */
211 p_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_CE_MSG));
212 if (p_msg != nullptr) {
213 p_msg->reg_listen.hdr.event = NFA_CE_API_REG_LISTEN_EVT;
214 p_msg->reg_listen.listen_type = NFA_CE_REG_TYPE_UICC;
215
216 p_msg->reg_listen.ee_handle = ee_handle;
217 p_msg->reg_listen.tech_mask = tech_mask;
218
219 nfa_sys_sendmsg(p_msg);
220
221 return (NFA_STATUS_OK);
222 }
223 #else
224 LOG(ERROR) << StringPrintf(
225 "NFCEE related functions are not "
226 "enabled!");
227 #endif
228 return (NFA_STATUS_FAILED);
229 }
230
231 /*******************************************************************************
232 **
233 ** Function NFA_CeRegisterFelicaSystemCodeOnDH
234 **
235 ** Description Register listening callback for Felica system code
236 **
237 ** The NFA_CE_REGISTERED_EVT reports the status of the
238 ** operation.
239 **
240 ** Note: If RF discovery is started,
241 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
242 ** happen before calling this function
243 **
244 ** Returns:
245 ** NFA_STATUS_OK, if command accepted
246 ** NFA_STATUS_FAILED: otherwise
247 **
248 *******************************************************************************/
NFA_CeRegisterFelicaSystemCodeOnDH(uint16_t system_code,uint8_t nfcid2[NCI_RF_F_UID_LEN],uint8_t t3tPmm[NCI_T3T_PMM_LEN],tNFA_CONN_CBACK * p_conn_cback)249 tNFA_STATUS NFA_CeRegisterFelicaSystemCodeOnDH(uint16_t system_code,
250 uint8_t nfcid2[NCI_RF_F_UID_LEN],
251 uint8_t t3tPmm[NCI_T3T_PMM_LEN],
252 tNFA_CONN_CBACK* p_conn_cback) {
253 tNFA_CE_MSG* p_msg;
254
255 LOG(VERBOSE) << __func__;
256
257 /* Validate parameters */
258 if (p_conn_cback == nullptr) return (NFA_STATUS_INVALID_PARAM);
259
260 p_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_CE_MSG));
261 if (p_msg != nullptr) {
262 p_msg->reg_listen.hdr.event = NFA_CE_API_REG_LISTEN_EVT;
263 p_msg->reg_listen.p_conn_cback = p_conn_cback;
264 p_msg->reg_listen.listen_type = NFA_CE_REG_TYPE_FELICA;
265
266 /* Listen info */
267 memcpy(p_msg->reg_listen.nfcid2, nfcid2, NCI_RF_F_UID_LEN);
268 memcpy(p_msg->reg_listen.t3tPmm, t3tPmm, NCI_T3T_PMM_LEN);
269 p_msg->reg_listen.system_code = system_code;
270
271 nfa_sys_sendmsg(p_msg);
272
273 return (NFA_STATUS_OK);
274 }
275
276 return (NFA_STATUS_FAILED);
277 }
278
279 /*******************************************************************************
280 **
281 ** Function NFA_CeDeregisterFelicaSystemCodeOnDH
282 **
283 ** Description Deregister listening callback for Felica
284 ** (previously registered using
285 ** NFA_CeRegisterFelicaSystemCodeOnDH)
286 **
287 ** The NFA_CE_DEREGISTERED_EVT reports the status of the
288 ** operation.
289 **
290 ** Note: If RF discovery is started,
291 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
292 ** happen before calling this function
293 **
294 ** Returns NFA_STATUS_OK if successfully initiated
295 ** NFA_STATUS_BAD_HANDLE if invalid handle
296 ** NFA_STATUS_FAILED otherwise
297 **
298 *******************************************************************************/
NFA_CeDeregisterFelicaSystemCodeOnDH(tNFA_HANDLE handle)299 tNFA_STATUS NFA_CeDeregisterFelicaSystemCodeOnDH(tNFA_HANDLE handle) {
300 LOG(VERBOSE) << StringPrintf("handle:0x%X", handle);
301 return (nfa_ce_api_deregister_listen(handle, NFA_CE_LISTEN_INFO_FELICA));
302 }
303
304 /*******************************************************************************
305 **
306 ** Function NFA_CeRegisterAidOnDH
307 **
308 ** Description Register listening callback for the specified ISODEP AID
309 **
310 ** The NFA_CE_REGISTERED_EVT reports the status of the
311 ** operation.
312 **
313 ** If no AID is specified (aid_len=0), and the pointer to the
314 ** AID value is null then p_conn_cback will get notifications
315 ** for any AIDs routed to the DH. This over-rides callbacks
316 ** registered for specific AIDs.
317 **
318 ** Note: If RF discovery is started,
319 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
320 ** happen before calling this function
321 **
322 ** Returns:
323 ** NFA_STATUS_OK, if command accepted
324 ** NFA_STATUS_FAILED: otherwise
325 **
326 *******************************************************************************/
NFA_CeRegisterAidOnDH(uint8_t aid[NFC_MAX_AID_LEN],uint8_t aid_len,tNFA_CONN_CBACK * p_conn_cback)327 tNFA_STATUS NFA_CeRegisterAidOnDH(uint8_t aid[NFC_MAX_AID_LEN], uint8_t aid_len,
328 tNFA_CONN_CBACK* p_conn_cback) {
329 tNFA_CE_MSG* p_msg;
330
331 LOG(VERBOSE) << __func__;
332
333 /* Validate parameters */
334 if ((p_conn_cback == nullptr) || ((aid_len != 0) && (aid == nullptr)) ||
335 ((aid_len == 0) && (aid != nullptr)))
336 return (NFA_STATUS_INVALID_PARAM);
337
338 p_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_CE_MSG));
339 if (p_msg != nullptr) {
340 p_msg->reg_listen.hdr.event = NFA_CE_API_REG_LISTEN_EVT;
341 p_msg->reg_listen.p_conn_cback = p_conn_cback;
342 p_msg->reg_listen.listen_type = NFA_CE_REG_TYPE_ISO_DEP;
343
344 if (aid_len != 0) {
345 memcpy(p_msg->reg_listen.aid, aid, aid_len);
346 }
347 p_msg->reg_listen.aid_len = aid_len;
348
349 nfa_sys_sendmsg(p_msg);
350
351 return (NFA_STATUS_OK);
352 }
353
354 return (NFA_STATUS_FAILED);
355 }
356
357 /*******************************************************************************
358 **
359 ** Function NFA_CeDeregisterAidOnDH
360 **
361 ** Description Deregister listening callback for ISODEP AID
362 ** (previously registered using NFA_CeRegisterAidOnDH)
363 **
364 ** The NFA_CE_DEREGISTERED_EVT reports the status of the
365 ** operation.
366 **
367 ** Note: If RF discovery is started,
368 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
369 ** happen before calling this function
370 **
371 ** Returns NFA_STATUS_OK if successfully initiated
372 ** NFA_STATUS_BAD_HANDLE if invalid handle
373 ** NFA_STATUS_FAILED otherwise
374 **
375 *******************************************************************************/
NFA_CeDeregisterAidOnDH(tNFA_HANDLE handle)376 tNFA_STATUS NFA_CeDeregisterAidOnDH(tNFA_HANDLE handle) {
377 LOG(VERBOSE) << StringPrintf("handle:0x%X", handle);
378 return (nfa_ce_api_deregister_listen(handle, NFA_CE_LISTEN_INFO_T4T_AID));
379 }
380
381 /*******************************************************************************
382 **
383 ** Function NFA_CeSetIsoDepListenTech
384 **
385 ** Description Set the technologies (NFC-A and/or NFC-B) to listen for when
386 ** NFA_CeConfigureLocalTag or NFA_CeDeregisterAidOnDH are
387 ** called.
388 **
389 ** By default (if this API is not called), NFA will listen
390 ** for both NFC-A and NFC-B for ISODEP.
391 **
392 ** Note: If listening for ISODEP on UICC, the DH listen callbacks
393 ** may still get activate notifications for ISODEP if the
394 ** reader/writer selects an AID that is not routed to the UICC
395 ** (regardless of whether A or B was disabled using
396 ** NFA_CeSetIsoDepListenTech)
397 **
398 ** Note: If RF discovery is started,
399 ** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
400 ** happen before calling this function
401 **
402 ** Returns:
403 ** NFA_STATUS_OK, if command accepted
404 ** NFA_STATUS_FAILED: otherwise
405 **
406 *******************************************************************************/
NFA_CeSetIsoDepListenTech(tNFA_TECHNOLOGY_MASK tech_mask)407 tNFA_STATUS NFA_CeSetIsoDepListenTech(tNFA_TECHNOLOGY_MASK tech_mask) {
408 tNFA_CE_MSG* p_msg;
409 tNFA_TECHNOLOGY_MASK use_mask =
410 (NFA_TECHNOLOGY_MASK_A | NFA_TECHNOLOGY_MASK_B);
411
412 LOG(VERBOSE) << StringPrintf("0x%x", tech_mask);
413 if (((tech_mask & use_mask) == 0) || ((tech_mask & ~use_mask) != 0)) {
414 LOG(ERROR) << StringPrintf(
415 "NFA_CeSetIsoDepListenTech: Invalid technology mask");
416 return (NFA_STATUS_INVALID_PARAM);
417 }
418
419 p_msg = (tNFA_CE_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_CE_MSG));
420 if (p_msg != nullptr) {
421 p_msg->hdr.event = NFA_CE_API_CFG_ISODEP_TECH_EVT;
422 p_msg->hdr.layer_specific = tech_mask;
423
424 nfa_sys_sendmsg(p_msg);
425
426 return (NFA_STATUS_OK);
427 }
428
429 return (NFA_STATUS_FAILED);
430 }
431