xref: /aosp_15_r20/system/nfc/src/nfc/nfc/nfc_vs.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  *  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   /* Validate parameters */
93   if (p_data == nullptr || (p_data->len > NCI_MAX_VSC_SIZE)) {
94     LOG(ERROR) << StringPrintf("buffer offset must be >= %d",
95                                NCI_VSC_MSG_HDR_SIZE);
96     if (p_data) GKI_freebuf(p_data);
97     return NFC_STATUS_INVALID_PARAM;
98   }
99 
100   p_data->event = BT_EVT_TO_NFC_NCI;
101   p_data->layer_specific = NFC_WAIT_RSP_RAW_VS;
102   /* save the callback function in the BT_HDR, to receive the response */
103   ((tNFC_NCI_VS_MSG*)p_data)->p_cback = p_cback;
104 
105   nfc_ncif_check_cmd_queue(p_data);
106   return NFC_STATUS_OK;
107 }
108 
109 /*******************************************************************************
110 **
111 ** Function         NFC_SendVsCommand
112 **
113 ** Description      This function is called to send the given vendor specific
114 **                  command to NFCC. The response from NFCC is reported to the
115 **                  given tNFC_VS_CBACK as (oid).
116 **
117 ** Parameters       oid - The opcode of the VS command.
118 **                  p_data - The parameters for the VS command
119 **
120 ** Returns          tNFC_STATUS
121 **
122 *******************************************************************************/
NFC_SendVsCommand(uint8_t oid,NFC_HDR * p_data,tNFC_VS_CBACK * p_cback)123 tNFC_STATUS NFC_SendVsCommand(uint8_t oid, NFC_HDR* p_data,
124                               tNFC_VS_CBACK* p_cback) {
125   tNFC_STATUS status = NFC_STATUS_OK;
126   uint8_t* pp;
127 
128   /* Allow VSC with 0-length payload */
129   if (p_data == nullptr) {
130     p_data = NCI_GET_CMD_BUF(0);
131     if (p_data) {
132       p_data->offset = NCI_VSC_MSG_HDR_SIZE;
133       p_data->len = 0;
134     }
135   }
136 
137   /* Validate parameters */
138   if ((p_data == nullptr) || (p_data->offset < NCI_VSC_MSG_HDR_SIZE) ||
139       (p_data->len > NCI_MAX_VSC_SIZE)) {
140     LOG(ERROR) << StringPrintf("buffer offset must be >= %d",
141                                NCI_VSC_MSG_HDR_SIZE);
142     if (p_data) GKI_freebuf(p_data);
143     return NFC_STATUS_INVALID_PARAM;
144   }
145 
146   p_data->event = BT_EVT_TO_NFC_NCI;
147   p_data->layer_specific = NFC_WAIT_RSP_VSC;
148   /* save the callback function in the NFC_HDR, to receive the response */
149   ((tNFC_NCI_VS_MSG*)p_data)->p_cback = p_cback;
150 
151   p_data->offset -= NCI_MSG_HDR_SIZE;
152   pp = (uint8_t*)(p_data + 1) + p_data->offset;
153   NCI_MSG_BLD_HDR0(pp, NCI_MT_CMD, NCI_GID_PROP);
154   NCI_MSG_BLD_HDR1(pp, oid);
155   *pp = (uint8_t)p_data->len;
156   p_data->len += NCI_MSG_HDR_SIZE;
157   nfc_ncif_check_cmd_queue(p_data);
158   return status;
159 }
160