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 * This file contains functions that NCI vendor specific interface with the
22 * NFCC. 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 <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 #include <string.h>
29
30 #include "gki.h"
31 #include "nfc_int.h"
32 #include "nfc_target.h"
33
34 using android::base::StringPrintf;
35
36 /****************************************************************************
37 ** Declarations
38 ****************************************************************************/
39
40 /*******************************************************************************
41 **
42 ** Function NFC_RegVSCback
43 **
44 ** Description This function is called to register or de-register a
45 ** callback function to receive Proprietary NCI response and
46 ** notification events. The maximum number of callback
47 ** functions allowed is NFC_NUM_VS_CBACKS
48 **
49 ** Returns tNFC_STATUS
50 **
51 *******************************************************************************/
NFC_RegVSCback(bool is_register,tNFC_VS_CBACK * p_cback)52 tNFC_STATUS NFC_RegVSCback(bool is_register, tNFC_VS_CBACK* p_cback) {
53 tNFC_STATUS status = NFC_STATUS_FAILED;
54 int i;
55
56 if (is_register) {
57 for (i = 0; i < NFC_NUM_VS_CBACKS; i++) {
58 /* find an empty spot to hold the callback function */
59 if (nfc_cb.p_vs_cb[i] == nullptr) {
60 nfc_cb.p_vs_cb[i] = p_cback;
61 status = NFC_STATUS_OK;
62 break;
63 }
64 }
65 } else {
66 for (i = 0; i < NFC_NUM_VS_CBACKS; i++) {
67 /* find the callback to de-register */
68 if (nfc_cb.p_vs_cb[i] == p_cback) {
69 nfc_cb.p_vs_cb[i] = nullptr;
70 status = NFC_STATUS_OK;
71 break;
72 }
73 }
74 }
75 return status;
76 }
77
78 /*******************************************************************************
79 **
80 ** Function NFC_SendRawVsCommand
81 **
82 ** Description This function is called to send the raw vendor specific
83 ** command to NFCC. The response from NFCC is reported to the
84 ** given tNFC_VS_CBACK.
85 **
86 ** Parameters p_data - The command buffer
87 **
88 ** Returns tNFC_STATUS
89 **
90 *******************************************************************************/
NFC_SendRawVsCommand(NFC_HDR * p_data,tNFC_VS_CBACK * p_cback)91 tNFC_STATUS NFC_SendRawVsCommand(NFC_HDR* p_data, tNFC_VS_CBACK* p_cback) {
92 if (gki_utils == nullptr) {
93 gki_utils = new GkiUtils();
94 }
95 /* Validate parameters */
96 if (p_data == nullptr || (p_data->len > NCI_MAX_VSC_SIZE)) {
97 LOG(ERROR) << StringPrintf("%s: buffer offset must be >= %d", __func__,
98 NCI_VSC_MSG_HDR_SIZE);
99 if (p_data) gki_utils->freebuf(p_data);
100 return NFC_STATUS_INVALID_PARAM;
101 }
102
103 p_data->event = BT_EVT_TO_NFC_NCI;
104 p_data->layer_specific = NFC_WAIT_RSP_RAW_VS;
105 /* save the callback function in the BT_HDR, to receive the response */
106 ((tNFC_NCI_VS_MSG*)p_data)->p_cback = p_cback;
107
108 nfc_ncif_check_cmd_queue(p_data);
109 return NFC_STATUS_OK;
110 }
111
112 /*******************************************************************************
113 **
114 ** Function NFC_SendVsCommand
115 **
116 ** Description This function is called to send the given vendor specific
117 ** command to NFCC. The response from NFCC is reported to the
118 ** given tNFC_VS_CBACK as (oid).
119 **
120 ** Parameters oid - The opcode of the VS command.
121 ** p_data - The parameters for the VS command
122 **
123 ** Returns tNFC_STATUS
124 **
125 *******************************************************************************/
NFC_SendVsCommand(uint8_t oid,NFC_HDR * p_data,tNFC_VS_CBACK * p_cback)126 tNFC_STATUS NFC_SendVsCommand(uint8_t oid, NFC_HDR* p_data,
127 tNFC_VS_CBACK* p_cback) {
128 tNFC_STATUS status = NFC_STATUS_OK;
129 uint8_t* pp;
130
131 if (gki_utils == nullptr) {
132 gki_utils = new GkiUtils();
133 }
134
135 /* Allow VSC with 0-length payload */
136 if (p_data == nullptr) {
137 p_data = NCI_GET_CMD_BUF(0);
138 if (p_data) {
139 p_data->offset = NCI_VSC_MSG_HDR_SIZE;
140 p_data->len = 0;
141 }
142 }
143
144 /* Validate parameters */
145 if ((p_data == nullptr) || (p_data->offset < NCI_VSC_MSG_HDR_SIZE) ||
146 (p_data->len > NCI_MAX_VSC_SIZE)) {
147 LOG(ERROR) << StringPrintf("%s: buffer offset must be >= %d", __func__,
148 NCI_VSC_MSG_HDR_SIZE);
149 if (p_data) gki_utils->freebuf(p_data);
150 return NFC_STATUS_INVALID_PARAM;
151 }
152
153 p_data->event = BT_EVT_TO_NFC_NCI;
154 p_data->layer_specific = NFC_WAIT_RSP_VSC;
155 /* save the callback function in the NFC_HDR, to receive the response */
156 ((tNFC_NCI_VS_MSG*)p_data)->p_cback = p_cback;
157
158 p_data->offset -= NCI_MSG_HDR_SIZE;
159 pp = (uint8_t*)(p_data + 1) + p_data->offset;
160 NCI_MSG_BLD_HDR0(pp, NCI_MT_CMD, NCI_GID_PROP);
161 NCI_MSG_BLD_HDR1(pp, oid);
162 *pp = (uint8_t)p_data->len;
163 p_data->len += NCI_MSG_HDR_SIZE;
164 nfc_ncif_check_cmd_queue(p_data);
165 return status;
166 }
167