1 /******************************************************************************
2 *
3 * Copyright (C) 2009-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 * This file contains functions that interface with the NFC NCI transport.
22 * On the receive side, it routes events to the appropriate handler
23 * (callback). On the transmit side, it manages the command transmission.
24 *
25 ******************************************************************************/
26 #include <string.h>
27
28 #include <android-base/stringprintf.h>
29 #include <base/logging.h>
30 #include <log/log.h>
31
32 #include "nfc_target.h"
33
34 #include "bt_types.h"
35 #include "ce_api.h"
36 #include "ce_int.h"
37
38 using android::base::StringPrintf;
39
40 extern bool nfc_debug_enabled;
41
42 tCE_CB ce_cb;
43
44 /*******************************************************************************
45 *******************************************************************************/
ce_init(void)46 void ce_init(void) {
47 memset(&ce_cb, 0, sizeof(tCE_CB));
48
49 /* Initialize tag-specific fields of ce control block */
50 ce_t3t_init();
51 }
52
53 /*******************************************************************************
54 **
55 ** Function CE_SendRawFrame
56 **
57 ** Description This function sends a raw frame to the peer device.
58 **
59 ** Returns tNFC_STATUS
60 **
61 *******************************************************************************/
CE_SendRawFrame(uint8_t * p_raw_data,uint16_t data_len)62 tNFC_STATUS CE_SendRawFrame(uint8_t* p_raw_data, uint16_t data_len) {
63 tNFC_STATUS status = NFC_STATUS_FAILED;
64 NFC_HDR* p_data;
65 uint8_t* p;
66
67 if (ce_cb.p_cback) {
68 if (data_len > GKI_get_pool_bufsize(NFC_RW_POOL_ID) - NCI_MSG_OFFSET_SIZE -
69 NCI_DATA_HDR_SIZE - 1) {
70 android_errorWriteLog(0x534e4554, "157649398");
71 return NFC_STATUS_FAILED;
72 }
73
74 /* a valid opcode for RW */
75 p_data = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
76 if (p_data) {
77 p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
78 p = (uint8_t*)(p_data + 1) + p_data->offset;
79 memcpy(p, p_raw_data, data_len);
80 p_data->len = data_len;
81 DLOG_IF(INFO, nfc_debug_enabled)
82 << StringPrintf("CE SENT raw frame (0x%x)", data_len);
83 status = NFC_SendData(NFC_RF_CONN_ID, p_data);
84 }
85 }
86 return status;
87 }
88
89 /*******************************************************************************
90 **
91 ** Function CE_SetActivatedTagType
92 **
93 ** Description This function selects the tag type for CE mode.
94 **
95 ** Returns tNFC_STATUS
96 **
97 *******************************************************************************/
CE_SetActivatedTagType(tNFC_ACTIVATE_DEVT * p_activate_params,uint16_t t3t_system_code,tCE_CBACK * p_cback)98 tNFC_STATUS CE_SetActivatedTagType(tNFC_ACTIVATE_DEVT* p_activate_params,
99 uint16_t t3t_system_code,
100 tCE_CBACK* p_cback) {
101 tNFC_STATUS status = NFC_STATUS_FAILED;
102 tNFC_PROTOCOL protocol = p_activate_params->protocol;
103
104 DLOG_IF(INFO, nfc_debug_enabled)
105 << StringPrintf("CE_SetActivatedTagType protocol:%d", protocol);
106
107 switch (protocol) {
108 case NFC_PROTOCOL_T1T:
109 case NFC_PROTOCOL_T2T:
110 return NFC_STATUS_FAILED;
111
112 case NFC_PROTOCOL_T3T: /* Type3Tag - NFC-F */
113 /* store callback function before NFC_SetStaticRfCback () */
114 ce_cb.p_cback = p_cback;
115 status = ce_select_t3t(t3t_system_code,
116 p_activate_params->rf_tech_param.param.lf.nfcid2);
117 break;
118
119 case NFC_PROTOCOL_ISO_DEP: /* ISODEP/4A,4B- NFC-A or NFC-B */
120 /* store callback function before NFC_SetStaticRfCback () */
121 ce_cb.p_cback = p_cback;
122 status = ce_select_t4t();
123 break;
124
125 default:
126 LOG(ERROR) << StringPrintf("CE_SetActivatedTagType Invalid protocol");
127 return NFC_STATUS_FAILED;
128 }
129
130 if (status != NFC_STATUS_OK) {
131 NFC_SetStaticRfCback(nullptr);
132 ce_cb.p_cback = nullptr;
133 }
134 return status;
135 }
136