1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 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  *  Interface to AVRCP optional commands
22  *
23  ******************************************************************************/
24 #include <string.h>
25 
26 #include <cstdint>
27 
28 #include "avct_api.h"
29 #include "avrc_api.h"
30 #include "avrc_defs.h"
31 #include "avrc_int.h"
32 #include "internal_include/bt_target.h"
33 #include "osi/include/allocator.h"
34 #include "stack/include/bt_hdr.h"
35 
36 using namespace bluetooth;
37 
38 /******************************************************************************
39  *
40  * Function         avrc_vendor_msg
41  *
42  * Description      Compose a VENDOR DEPENDENT command according to p_msg
43  *
44  *                  Input Parameters:
45  *                      p_msg: Pointer to VENDOR DEPENDENT message structure.
46  *
47  *                  Output Parameters:
48  *                      None.
49  *
50  * Returns          pointer to a valid GKI buffer if successful.
51  *                  NULL if p_msg is NULL.
52  *
53  *****************************************************************************/
avrc_vendor_msg(tAVRC_MSG_VENDOR * p_msg)54 static BT_HDR* avrc_vendor_msg(tAVRC_MSG_VENDOR* p_msg) {
55   BT_HDR* p_cmd;
56   uint8_t* p_data;
57 
58 /*
59   An AVRC cmd consists of at least of:
60   - A BT_HDR, plus
61   - AVCT_MSG_OFFSET, plus
62   - 3 bytes for ctype, subunit_type and op_vendor, plus
63   - 3 bytes for company_id
64 */
65 #define AVRC_MIN_VENDOR_CMD_LEN (sizeof(BT_HDR) + AVCT_MSG_OFFSET + 3 + 3)
66 
67   if (p_msg == nullptr || AVRC_META_CMD_BUF_SIZE < AVRC_MIN_VENDOR_CMD_LEN + p_msg->vendor_len) {
68     return nullptr;
69   }
70 
71   p_cmd = (BT_HDR*)osi_calloc(AVRC_META_CMD_BUF_SIZE);
72 
73   p_cmd->offset = AVCT_MSG_OFFSET;
74   p_data = (uint8_t*)(p_cmd + 1) + p_cmd->offset;
75   *p_data++ = (p_msg->hdr.ctype & AVRC_CTYPE_MASK);
76   *p_data++ = (p_msg->hdr.subunit_type << AVRC_SUBTYPE_SHIFT) | p_msg->hdr.subunit_id;
77   *p_data++ = AVRC_OP_VENDOR;
78   AVRC_CO_ID_TO_BE_STREAM(p_data, p_msg->company_id);
79   if (p_msg->vendor_len && p_msg->p_vendor_data) {
80     memcpy(p_data, p_msg->p_vendor_data, p_msg->vendor_len);
81   }
82   p_cmd->len = (uint16_t)(p_data + p_msg->vendor_len - (uint8_t*)(p_cmd + 1) - p_cmd->offset);
83   p_cmd->layer_specific = AVCT_DATA_CTRL;
84 
85   return p_cmd;
86 }
87 
88 /******************************************************************************
89  *
90  * Function         AVRC_UnitCmd
91  *
92  * Description      Send a UNIT INFO command to the peer device.  This
93  *                  function can only be called for controller role connections.
94  *                  Any response message from the peer is passed back through
95  *                  the tAVRC_MSG_CBACK callback function.
96  *
97  *                  Input Parameters:
98  *                      handle: Handle of this connection.
99  *
100  *                      label: Transaction label.
101  *
102  *                  Output Parameters:
103  *                      None.
104  *
105  * Returns          AVRC_SUCCESS if successful.
106  *                  AVRC_BAD_HANDLE if handle is invalid.
107  *
108  *****************************************************************************/
AVRC_UnitCmd(uint8_t handle,uint8_t label)109 uint16_t AVRC_UnitCmd(uint8_t handle, uint8_t label) {
110   BT_HDR* p_cmd = (BT_HDR*)osi_calloc(AVRC_CMD_BUF_SIZE);
111   uint8_t* p_data;
112 
113   p_cmd->offset = AVCT_MSG_OFFSET;
114   p_data = (uint8_t*)(p_cmd + 1) + p_cmd->offset;
115   *p_data++ = AVRC_CMD_STATUS;
116   /* unit & id ignore */
117   *p_data++ = (AVRC_SUB_UNIT << AVRC_SUBTYPE_SHIFT) | AVRC_SUBID_IGNORE;
118   *p_data++ = AVRC_OP_UNIT_INFO;
119   memset(p_data, AVRC_CMD_OPRND_PAD, AVRC_UNIT_OPRND_BYTES);
120   p_cmd->len = p_data + AVRC_UNIT_OPRND_BYTES - (uint8_t*)(p_cmd + 1) - p_cmd->offset;
121   p_cmd->layer_specific = AVCT_DATA_CTRL;
122 
123   return AVCT_MsgReq(handle, label, AVCT_CMD, p_cmd);
124 }
125 
126 /******************************************************************************
127  *
128  * Function         AVRC_SubCmd
129  *
130  * Description      Send a SUBUNIT INFO command to the peer device.  This
131  *                  function can only be called for controller role connections.
132  *                  Any response message from the peer is passed back through
133  *                  the tAVRC_MSG_CBACK callback function.
134  *
135  *                  Input Parameters:
136  *                      handle: Handle of this connection.
137  *
138  *                      label: Transaction label.
139  *
140  *                      page: Specifies which part of the subunit type table
141  *                      is requested.  For AVRCP it is typically zero.
142  *                      Value range is 0-7.
143  *
144  *                  Output Parameters:
145  *                      None.
146  *
147  * Returns          AVRC_SUCCESS if successful.
148  *                  AVRC_BAD_HANDLE if handle is invalid.
149  *
150  *****************************************************************************/
AVRC_SubCmd(uint8_t handle,uint8_t label,uint8_t page)151 uint16_t AVRC_SubCmd(uint8_t handle, uint8_t label, uint8_t page) {
152   BT_HDR* p_cmd = (BT_HDR*)osi_calloc(AVRC_CMD_BUF_SIZE);
153   uint8_t* p_data;
154 
155   p_cmd->offset = AVCT_MSG_OFFSET;
156   p_data = (uint8_t*)(p_cmd + 1) + p_cmd->offset;
157   *p_data++ = AVRC_CMD_STATUS;
158   /* unit & id ignore */
159   *p_data++ = (AVRC_SUB_UNIT << AVRC_SUBTYPE_SHIFT) | AVRC_SUBID_IGNORE;
160   *p_data++ = AVRC_OP_SUB_INFO;
161   *p_data++ = ((page & AVRC_SUB_PAGE_MASK) << AVRC_SUB_PAGE_SHIFT) | AVRC_SUB_EXT_CODE;
162   memset(p_data, AVRC_CMD_OPRND_PAD, AVRC_SUB_OPRND_BYTES);
163   p_cmd->len = p_data + AVRC_SUB_OPRND_BYTES - (uint8_t*)(p_cmd + 1) - p_cmd->offset;
164   p_cmd->layer_specific = AVCT_DATA_CTRL;
165 
166   return AVCT_MsgReq(handle, label, AVCT_CMD, p_cmd);
167 }
168 
169 /******************************************************************************
170  *
171  * Function         AVRC_VendorCmd
172  *
173  * Description      Send a VENDOR DEPENDENT command to the peer device.  This
174  *                  function can only be called for controller role connections.
175  *                  Any response message from the peer is passed back through
176  *                  the tAVRC_MSG_CBACK callback function.
177  *
178  *                  Input Parameters:
179  *                      handle: Handle of this connection.
180  *
181  *                      label: Transaction label.
182  *
183  *                      p_msg: Pointer to VENDOR DEPENDENT message structure.
184  *
185  *                  Output Parameters:
186  *                      None.
187  *
188  * Returns          AVRC_SUCCESS if successful.
189  *                  AVRC_BAD_HANDLE if handle is invalid.
190  *
191  *****************************************************************************/
AVRC_VendorCmd(uint8_t handle,uint8_t label,tAVRC_MSG_VENDOR * p_msg)192 uint16_t AVRC_VendorCmd(uint8_t handle, uint8_t label, tAVRC_MSG_VENDOR* p_msg) {
193   BT_HDR* p_buf = avrc_vendor_msg(p_msg);
194   if (p_buf) {
195     return AVCT_MsgReq(handle, label, AVCT_CMD, p_buf);
196   } else {
197     return AVCT_NO_RESOURCES;
198   }
199 }
200 
201 /******************************************************************************
202  *
203  * Function         AVRC_VendorRsp
204  *
205  * Description      Send a VENDOR DEPENDENT response to the peer device.  This
206  *                  function can only be called for target role connections.
207  *                  This function must be called when a VENDOR DEPENDENT
208  *                  command message is received from the peer through the
209  *                  tAVRC_MSG_CBACK callback function.
210  *
211  *                  Input Parameters:
212  *                      handle: Handle of this connection.
213  *
214  *                      label: Transaction label.  Must be the same value as
215  *                      passed with the command message in the callback
216  *                      function.
217  *
218  *                      p_msg: Pointer to VENDOR DEPENDENT message structure.
219  *
220  *                  Output Parameters:
221  *                      None.
222  *
223  * Returns          AVRC_SUCCESS if successful.
224  *                  AVRC_BAD_HANDLE if handle is invalid.
225  *
226  *****************************************************************************/
AVRC_VendorRsp(uint8_t handle,uint8_t label,tAVRC_MSG_VENDOR * p_msg)227 uint16_t AVRC_VendorRsp(uint8_t handle, uint8_t label, tAVRC_MSG_VENDOR* p_msg) {
228   BT_HDR* p_buf = avrc_vendor_msg(p_msg);
229   if (p_buf) {
230     return AVCT_MsgReq(handle, label, AVCT_RSP, p_buf);
231   } else {
232     return AVCT_NO_RESOURCES;
233   }
234 }
235