1 /******************************************************************************
2 *
3 * Copyright (C) 2006-2013 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 #include <string.h>
19 #include "common/bt_target.h"
20 #include "stack/avrc_api.h"
21 #include "stack/avrc_defs.h"
22 #include "avrc_int.h"
23 #include "osi/allocator.h"
24
25 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
26
27 /*****************************************************************************
28 ** Global data
29 *****************************************************************************/
30
31
32 #if (AVRC_METADATA_INCLUDED == TRUE)
33 /*******************************************************************************
34 **
35 ** Function avrc_bld_next_cmd
36 **
37 ** Description This function builds the Request Continue or Abort command.
38 **
39 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
40 ** Otherwise, the error code.
41 **
42 *******************************************************************************/
avrc_bld_next_cmd(tAVRC_NEXT_CMD * p_cmd,BT_HDR * p_pkt)43 static tAVRC_STS avrc_bld_next_cmd (tAVRC_NEXT_CMD *p_cmd, BT_HDR *p_pkt)
44 {
45 UINT8 *p_data, *p_start;
46
47 AVRC_TRACE_API("avrc_bld_next_cmd");
48
49 /* get the existing length, if any, and also the num attributes */
50 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
51 p_data = p_start + 2; /* pdu + rsvd */
52
53 /* add fixed lenth 1 - pdu_id (1) */
54 UINT16_TO_BE_STREAM(p_data, 1);
55 UINT8_TO_BE_STREAM(p_data, p_cmd->target_pdu);
56 p_pkt->len = (p_data - p_start);
57
58 return AVRC_STS_NO_ERROR;
59 }
60
61 /*****************************************************************************
62 ** the following commands are introduced in AVRCP 1.4
63 *****************************************************************************/
64
65 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
66 /*******************************************************************************
67 **
68 ** Function avrc_bld_set_abs_volume_cmd
69 **
70 ** Description This function builds the Set Absolute Volume command.
71 **
72 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
73 ** Otherwise, the error code.
74 **
75 *******************************************************************************/
avrc_bld_set_abs_volume_cmd(tAVRC_SET_VOLUME_CMD * p_cmd,BT_HDR * p_pkt)76 static tAVRC_STS avrc_bld_set_abs_volume_cmd (tAVRC_SET_VOLUME_CMD *p_cmd, BT_HDR *p_pkt)
77 {
78 UINT8 *p_data, *p_start;
79
80 AVRC_TRACE_API("avrc_bld_set_abs_volume_cmd");
81 /* get the existing length, if any, and also the num attributes */
82 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
83 p_data = p_start + 2; /* pdu + rsvd */
84 /* add fixed lenth 1 - volume (1) */
85 UINT16_TO_BE_STREAM(p_data, 1);
86 UINT8_TO_BE_STREAM(p_data, (AVRC_MAX_VOLUME & p_cmd->volume));
87 p_pkt->len = (p_data - p_start);
88 return AVRC_STS_NO_ERROR;
89 }
90
91 /*******************************************************************************
92 **
93 ** Function avrc_bld_vol_change_notfn
94 **
95 ** Description This function builds the register notification for volume change.
96 **
97 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
98 ** Otherwise, the error code.
99 **
100 *******************************************************************************/
avrc_bld_register_change_notfn(UINT8 event_id,UINT32 event_parameter,BT_HDR * p_pkt)101 static tAVRC_STS avrc_bld_register_change_notfn(UINT8 event_id, UINT32 event_parameter, BT_HDR *p_pkt)
102 {
103 UINT8 *p_data, *p_start;
104
105 AVRC_TRACE_API("avrc_bld_vol_change");
106 /* get the existing length, if any, and also the num attributes */
107 // Set the notify value
108 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
109 p_data = p_start + 2; /* pdu + rsvd */
110 /* add fixed length 5 -*/
111 UINT16_TO_BE_STREAM(p_data, 5);
112 UINT8_TO_BE_STREAM(p_data, event_id);
113 UINT32_TO_BE_STREAM(p_data, event_parameter);
114 p_pkt->len = (p_data - p_start);
115 return AVRC_STS_NO_ERROR;
116 }
117 #endif
118
119 /*******************************************************************************
120 **
121 ** Function avrc_bld_init_cmd_buffer
122 **
123 ** Description This function initializes the command buffer based on PDU
124 **
125 ** Returns NULL, if no GKI buffer or failure to build the message.
126 ** Otherwise, the GKI buffer that contains the initialized message.
127 **
128 *******************************************************************************/
avrc_bld_init_cmd_buffer(tAVRC_COMMAND * p_cmd)129 static BT_HDR *avrc_bld_init_cmd_buffer(tAVRC_COMMAND *p_cmd)
130 {
131 UINT8 opcode = avrc_opcode_from_pdu(p_cmd->pdu);
132 AVRC_TRACE_API("avrc_bld_init_cmd_buffer: pdu=%x, opcode=%x", p_cmd->pdu, opcode);
133
134 UINT16 offset = 0;
135 switch (opcode) {
136 case AVRC_OP_PASS_THRU:
137 offset = AVRC_MSG_PASS_THRU_OFFSET;
138 break;
139
140 case AVRC_OP_VENDOR:
141 offset = AVRC_MSG_VENDOR_OFFSET;
142 break;
143 }
144
145 /* allocate and initialize the buffer */
146 BT_HDR *p_pkt = (BT_HDR *)osi_malloc(AVRC_META_CMD_BUF_SIZE);
147 if (p_pkt) {
148 UINT8 *p_data, *p_start;
149
150 p_pkt->layer_specific = AVCT_DATA_CTRL;
151 p_pkt->event = opcode;
152 p_pkt->offset = offset;
153 p_data = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
154 p_start = p_data;
155
156 /* pass thru - group navigation - has a two byte op_id, so dont do it here */
157 if (opcode != AVRC_OP_PASS_THRU) {
158 *p_data++ = p_cmd->pdu;
159 }
160
161 switch (opcode) {
162 case AVRC_OP_VENDOR:
163 /* reserved 0, packet_type 0 */
164 UINT8_TO_BE_STREAM(p_data, 0);
165 /* continue to the next "case to add length */
166 /* add fixed lenth - 0 */
167 UINT16_TO_BE_STREAM(p_data, 0);
168 break;
169 }
170
171 p_pkt->len = (p_data - p_start);
172 }
173 p_cmd->cmd.opcode = opcode;
174 return p_pkt;
175 }
176
177 /*******************************************************************************
178 **
179 ** Function avrc_bld_set_player_value_cmd
180 **
181 ** Description This function builds the Set Player Application Value command.
182 **
183 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
184 ** Otherwise, the error code.
185 **
186 *******************************************************************************/
avrc_bld_set_player_value_cmd(tAVRC_SET_APP_VALUE_CMD * p_cmd,BT_HDR * p_pkt)187 static tAVRC_STS avrc_bld_set_player_value_cmd(tAVRC_SET_APP_VALUE_CMD *p_cmd, BT_HDR *p_pkt)
188 {
189 UINT8 *p_data, *p_start;
190
191 /* get the existing length, if any, and also the num attributes */
192 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
193 p_data = p_start + 2; /* pdu + rsvd */
194 /* add length */
195 UINT16_TO_BE_STREAM(p_data, 3);
196 /* Number of attributes */
197 UINT8_TO_BE_STREAM(p_data, 1);
198 UINT8_TO_BE_STREAM(p_data, p_cmd->p_vals->attr_id);
199 UINT8_TO_BE_STREAM(p_data, p_cmd->p_vals->attr_val);
200
201 p_pkt->len = (p_data - p_start);
202 return AVRC_STS_NO_ERROR;
203 }
204
205 /*******************************************************************************
206 **
207 ** Function avrc_bld_get_element_attr_cmd
208 **
209 ** Description This function builds the Get Element Attribute command.
210 **
211 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
212 ** Otherwise, the error code.
213 **
214 *******************************************************************************/
avrc_bld_get_element_attr_cmd(tAVRC_GET_ELEM_ATTRS_CMD * p_cmd,BT_HDR * p_pkt)215 static tAVRC_STS avrc_bld_get_element_attr_cmd (tAVRC_GET_ELEM_ATTRS_CMD *p_cmd, BT_HDR *p_pkt)
216 {
217 int i;
218 UINT8 *p_data, *p_start;
219
220 AVRC_TRACE_API("avrc_bld_get_element_attr_cmd num_attr: %d", p_cmd->num_attr);
221 /* get the existing length, if any, and also the num attributes */
222 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
223 p_data = p_start + 2; /* pdu + rsvd */
224 /* add length */
225 UINT16_TO_BE_STREAM(p_data, 8 + 1 /* id + attr count */ + p_cmd->num_attr * sizeof(UINT32));
226 /* Identifier 0x0 (PLAYING) */
227 UINT64_TO_BE_STREAM(p_data, (UINT64)(0));
228 /* Attribute count */
229 UINT8_TO_BE_STREAM(p_data, p_cmd->num_attr);
230
231 for (i = 0; i < p_cmd->num_attr; i++) {
232 AVRC_TRACE_API("avrc_bld_get_element_attr_cmd attr_id: %d", p_cmd->attrs[i]);
233 UINT32_TO_BE_STREAM(p_data, p_cmd->attrs[i]);
234 }
235
236 p_pkt->len = (p_data - p_start);
237 return AVRC_STS_NO_ERROR;
238 }
239
avrc_bld_get_caps_cmd(tAVRC_GET_CAPS_CMD * p_cmd,BT_HDR * p_pkt)240 static tAVRC_STS avrc_bld_get_caps_cmd(tAVRC_GET_CAPS_CMD *p_cmd, BT_HDR *p_pkt)
241 {
242 UINT8 *p_data, *p_start;
243
244 AVRC_TRACE_API("avrc_bld_get_caps");
245 /* get the existing length, if any, and also the num attributes */
246 // Set the notify value
247 p_start = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
248 p_data = p_start + 2; /* pdu + rsvd */
249 /* add fixed length 1 */
250 UINT16_TO_BE_STREAM(p_data, 1);
251 /* capability id */
252 UINT8_TO_BE_STREAM(p_data, p_cmd->capability_id);
253 p_pkt->len = (p_data - p_start);
254 return AVRC_STS_NO_ERROR;
255 }
256 /*******************************************************************************
257 **
258 ** Function AVRC_BldCommand
259 **
260 ** Description This function builds the given AVRCP command to the given
261 ** GKI buffer
262 **
263 ** Returns AVRC_STS_NO_ERROR, if the command is built successfully
264 ** Otherwise, the error code.
265 **
266 *******************************************************************************/
AVRC_BldCommand(tAVRC_COMMAND * p_cmd,BT_HDR ** pp_pkt)267 tAVRC_STS AVRC_BldCommand( tAVRC_COMMAND *p_cmd, BT_HDR **pp_pkt)
268 {
269 tAVRC_STS status = AVRC_STS_BAD_PARAM;
270 BT_HDR *p_pkt;
271 BOOLEAN alloc = FALSE;
272
273 AVRC_TRACE_API("AVRC_BldCommand: pdu=%x status=%x", p_cmd->cmd.pdu, p_cmd->cmd.status);
274 if (!p_cmd || !pp_pkt) {
275 AVRC_TRACE_API("AVRC_BldCommand. Invalid parameters passed. p_cmd=%p, pp_pkt=%p",
276 p_cmd, pp_pkt);
277 return AVRC_STS_BAD_PARAM;
278 }
279
280 if (*pp_pkt == NULL) {
281 if ((*pp_pkt = avrc_bld_init_cmd_buffer(p_cmd)) == NULL) {
282 AVRC_TRACE_API("AVRC_BldCommand: Failed to initialize command buffer");
283 return AVRC_STS_INTERNAL_ERR;
284 }
285 alloc = TRUE;
286 }
287 status = AVRC_STS_NO_ERROR;
288 p_pkt = *pp_pkt;
289
290 switch (p_cmd->pdu) {
291 case AVRC_PDU_REQUEST_CONTINUATION_RSP: /* 0x40 */
292 status = avrc_bld_next_cmd(&p_cmd->continu, p_pkt);
293 break;
294
295 case AVRC_PDU_ABORT_CONTINUATION_RSP: /* 0x41 */
296 status = avrc_bld_next_cmd(&p_cmd->abort, p_pkt);
297 break;
298 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
299 case AVRC_PDU_SET_ABSOLUTE_VOLUME: /* 0x50 */
300 status = avrc_bld_set_abs_volume_cmd(&p_cmd->volume, p_pkt);
301 break;
302 #endif
303
304 case AVRC_PDU_SET_PLAYER_APP_VALUE: /* 0x14 */
305 status = avrc_bld_set_player_value_cmd(&p_cmd->set_app_val, p_pkt);
306 break;
307
308 case AVRC_PDU_GET_ELEMENT_ATTR: /* 0x20 */
309 status = avrc_bld_get_element_attr_cmd(&p_cmd->get_elem_attrs, p_pkt);
310 break;
311
312 case AVRC_PDU_REGISTER_NOTIFICATION: /* 0x31 */
313 status = avrc_bld_register_change_notfn(p_cmd->reg_notif.event_id, p_cmd->reg_notif.param, p_pkt);
314 break;
315 case AVRC_PDU_GET_CAPABILITIES:
316 status = avrc_bld_get_caps_cmd(&p_cmd->get_caps, p_pkt);
317 break;
318 }
319
320 if (alloc && (status != AVRC_STS_NO_ERROR) ) {
321 osi_free(p_pkt);
322 *pp_pkt = NULL;
323 }
324 AVRC_TRACE_API("AVRC_BldCommand: returning %d", status);
325 return status;
326 }
327 #endif /* (AVRC_METADATA_INCLUDED == TRUE) */
328
329 #endif /* #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE) */
330