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