1 /******************************************************************************
3 * Copyright (C) 2003-2013 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
20 #include "bt_common.h"
22 #include "avrc_defs.h"
25 /*****************************************************************************
27 *****************************************************************************/
28 #if (AVRC_METADATA_INCLUDED == TRUE)
30 #if (AVRC_CTLR_INCLUDED == TRUE)
31 /*******************************************************************************
33 ** Function avrc_ctrl_pars_vendor_cmd
35 ** Description This function parses the vendor specific commands defined by
36 ** Bluetooth SIG for AVRCP Conroller.
38 ** Returns AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
39 ** Otherwise, the error code defined by AVRCP 1.4
41 *******************************************************************************/
42 static tAVRC_STS avrc_ctrl_pars_vendor_cmd(tAVRC_MSG_VENDOR *p_msg, tAVRC_COMMAND *p_result)
44 tAVRC_STS status = AVRC_STS_NO_ERROR;
46 UINT8 *p = p_msg->p_vendor_data;
48 AVRC_TRACE_DEBUG("%s pdu:0x%x", __func__, p_result->pdu);
49 if (!AVRC_IsValidAvcType (p_result->pdu, p_msg->hdr.ctype))
51 AVRC_TRACE_DEBUG("%s detects wrong AV/C type!", __func__);
52 status = AVRC_STS_BAD_CMD;
55 p++; /* skip the reserved byte */
57 BE_STREAM_TO_UINT16 (len, p);
58 if ((len+4) != (p_msg->vendor_len))
60 status = AVRC_STS_INTERNAL_ERR;
63 if (status != AVRC_STS_NO_ERROR)
66 switch (p_result->pdu)
68 case AVRC_PDU_SET_ABSOLUTE_VOLUME:
71 status = AVRC_STS_INTERNAL_ERR;
74 BE_STREAM_TO_UINT8 (p_result->volume.volume, p);
75 p_result->volume.volume = AVRC_MAX_VOLUME & p_result->volume.volume;
79 case AVRC_PDU_REGISTER_NOTIFICATION: /* 0x31 */
80 BE_STREAM_TO_UINT8 (p_result->reg_notif.event_id, p);
81 BE_STREAM_TO_UINT32 (p_result->reg_notif.param, p);
84 status = AVRC_STS_BAD_CMD;
91 /*******************************************************************************
93 ** Function avrc_pars_vendor_cmd
95 ** Description This function parses the vendor specific commands defined by
98 ** Returns AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
99 ** Otherwise, the error code defined by AVRCP 1.4
101 *******************************************************************************/
102 static tAVRC_STS avrc_pars_vendor_cmd(tAVRC_MSG_VENDOR *p_msg, tAVRC_COMMAND *p_result,
103 UINT8 *p_buf, UINT16 buf_len)
105 tAVRC_STS status = AVRC_STS_NO_ERROR;
111 UINT32 u32, u32_2, *p_u32;
112 tAVRC_APP_SETTING *p_app_set;
115 /* Check the vendor data */
116 if (p_msg->vendor_len == 0)
117 return AVRC_STS_NO_ERROR;
118 if (p_msg->p_vendor_data == NULL)
119 return AVRC_STS_INTERNAL_ERR;
121 p = p_msg->p_vendor_data;
122 p_result->pdu = *p++;
123 AVRC_TRACE_DEBUG("%s pdu:0x%x", __func__, p_result->pdu);
124 if (!AVRC_IsValidAvcType (p_result->pdu, p_msg->hdr.ctype))
126 AVRC_TRACE_DEBUG("%s detects wrong AV/C type!", __func__);
127 status = AVRC_STS_BAD_CMD;
130 p++; /* skip the reserved byte */
131 BE_STREAM_TO_UINT16 (len, p);
132 if ((len+4) != (p_msg->vendor_len))
134 status = AVRC_STS_INTERNAL_ERR;
137 if (status != AVRC_STS_NO_ERROR)
140 switch (p_result->pdu)
142 case AVRC_PDU_GET_CAPABILITIES: /* 0x10 */
143 p_result->get_caps.capability_id = *p++;
144 if (!AVRC_IS_VALID_CAP_ID(p_result->get_caps.capability_id))
145 status = AVRC_STS_BAD_PARAM;
147 status = AVRC_STS_INTERNAL_ERR;
150 case AVRC_PDU_LIST_PLAYER_APP_ATTR: /* 0x11 */
151 /* no additional parameters */
153 status = AVRC_STS_INTERNAL_ERR;
156 case AVRC_PDU_LIST_PLAYER_APP_VALUES: /* 0x12 */
157 p_result->list_app_values.attr_id = *p++;
158 if (!AVRC_IS_VALID_ATTRIBUTE(p_result->list_app_values.attr_id))
159 status = AVRC_STS_BAD_PARAM;
161 status = AVRC_STS_INTERNAL_ERR;
164 case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE: /* 0x13 */
165 case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT: /* 0x15 */
166 BE_STREAM_TO_UINT8 (p_result->get_cur_app_val.num_attr, p);
167 if (len != (p_result->get_cur_app_val.num_attr+1))
169 status = AVRC_STS_INTERNAL_ERR;
172 p_u8 = p_result->get_cur_app_val.attrs;
173 for (xx=0, yy=0; xx< p_result->get_cur_app_val.num_attr; xx++)
175 /* only report the valid player app attributes */
176 if (AVRC_IsValidPlayerAttr(*p))
180 p_result->get_cur_app_val.num_attr = yy;
183 status = AVRC_STS_BAD_PARAM;
187 case AVRC_PDU_SET_PLAYER_APP_VALUE: /* 0x14 */
188 BE_STREAM_TO_UINT8 (p_result->set_app_val.num_val, p);
189 size_needed = sizeof(tAVRC_APP_SETTING);
190 if (p_buf && (len == ((p_result->set_app_val.num_val<<1) + 1)))
192 p_result->set_app_val.p_vals = (tAVRC_APP_SETTING *)p_buf;
193 p_app_set = p_result->set_app_val.p_vals;
194 for (xx=0; ((xx< p_result->set_app_val.num_val) && (buf_len > size_needed)); xx++)
196 p_app_set[xx].attr_id = *p++;
197 p_app_set[xx].attr_val = *p++;
198 if (!avrc_is_valid_player_attrib_value(p_app_set[xx].attr_id, p_app_set[xx].attr_val))
199 status = AVRC_STS_BAD_PARAM;
201 if (xx != p_result->set_app_val.num_val)
204 "%s AVRC_PDU_SET_PLAYER_APP_VALUE not enough room:%d orig num_val:%d",
205 __func__, xx, p_result->set_app_val.num_val);
206 p_result->set_app_val.num_val = xx;
211 AVRC_TRACE_ERROR("%s AVRC_PDU_SET_PLAYER_APP_VALUE NULL decode buffer or bad len",
213 status = AVRC_STS_INTERNAL_ERR;
217 case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:/* 0x16 */
219 status = AVRC_STS_INTERNAL_ERR;
222 BE_STREAM_TO_UINT8 (p_result->get_app_val_txt.attr_id, p);
223 if (!AVRC_IS_VALID_ATTRIBUTE(p_result->get_app_val_txt.attr_id))
224 status = AVRC_STS_BAD_PARAM;
227 BE_STREAM_TO_UINT8 (p_result->get_app_val_txt.num_val, p);
228 if ( (len - 2/* attr_id & num_val */) != p_result->get_app_val_txt.num_val)
229 status = AVRC_STS_INTERNAL_ERR;
232 p_u8 = p_result->get_app_val_txt.vals;
233 for (xx=0; xx< p_result->get_app_val_txt.num_val; xx++)
236 if (!avrc_is_valid_player_attrib_value(p_result->get_app_val_txt.attr_id,
239 status = AVRC_STS_BAD_PARAM;
248 case AVRC_PDU_INFORM_DISPLAY_CHARSET: /* 0x17 */
250 status = AVRC_STS_INTERNAL_ERR;
253 BE_STREAM_TO_UINT8 (p_result->inform_charset.num_id, p);
254 if ( (len - 1/* num_id */) != p_result->inform_charset.num_id * 2)
255 status = AVRC_STS_INTERNAL_ERR;
258 p_u16 = p_result->inform_charset.charsets;
259 if (p_result->inform_charset.num_id > AVRC_MAX_CHARSET_SIZE)
260 p_result->inform_charset.num_id = AVRC_MAX_CHARSET_SIZE;
261 for (xx=0; xx< p_result->inform_charset.num_id; xx++)
263 BE_STREAM_TO_UINT16 (p_u16[xx], p);
269 case AVRC_PDU_INFORM_BATTERY_STAT_OF_CT:/* 0x18 */
271 status = AVRC_STS_INTERNAL_ERR;
274 p_result->inform_battery_status.battery_status = *p++;
275 if (!AVRC_IS_VALID_BATTERY_STATUS(p_result->inform_battery_status.battery_status))
276 status = AVRC_STS_BAD_PARAM;
280 case AVRC_PDU_GET_ELEMENT_ATTR: /* 0x20 */
281 if (len < 9) /* UID/8 and num_attr/1 */
282 status = AVRC_STS_INTERNAL_ERR;
285 BE_STREAM_TO_UINT32 (u32, p);
286 BE_STREAM_TO_UINT32 (u32_2, p);
287 if (u32== 0 && u32_2 == 0)
289 BE_STREAM_TO_UINT8 (p_result->get_elem_attrs.num_attr, p);
290 if ( (len - 9/* UID/8 and num_attr/1 */) != (p_result->get_elem_attrs.num_attr * 4))
291 status = AVRC_STS_INTERNAL_ERR;
294 p_u32 = p_result->get_elem_attrs.attrs;
295 if (p_result->get_elem_attrs.num_attr > AVRC_MAX_ELEM_ATTR_SIZE)
296 p_result->get_elem_attrs.num_attr = AVRC_MAX_ELEM_ATTR_SIZE;
297 for (xx=0; xx< p_result->get_elem_attrs.num_attr; xx++)
299 BE_STREAM_TO_UINT32 (p_u32[xx], p);
304 status = AVRC_STS_NOT_FOUND;
308 case AVRC_PDU_GET_PLAY_STATUS: /* 0x30 */
309 /* no additional parameters */
311 status = AVRC_STS_INTERNAL_ERR;
314 case AVRC_PDU_REGISTER_NOTIFICATION: /* 0x31 */
316 status = AVRC_STS_INTERNAL_ERR;
319 BE_STREAM_TO_UINT8 (p_result->reg_notif.event_id, p);
320 BE_STREAM_TO_UINT32 (p_result->reg_notif.param, p);
324 case AVRC_PDU_SET_ABSOLUTE_VOLUME:
327 status = AVRC_STS_INTERNAL_ERR;
331 /* case AVRC_PDU_REQUEST_CONTINUATION_RSP: 0x40 */
332 /* case AVRC_PDU_ABORT_CONTINUATION_RSP: 0x41 */
335 status = AVRC_STS_BAD_CMD;
342 #if (AVRC_CTLR_INCLUDED == TRUE)
343 /*******************************************************************************
345 ** Function AVRC_Ctrl_ParsCommand
347 ** Description This function is used to parse cmds received for CTRL
348 ** Currently it is for SetAbsVolume and Volume Change Notification..
350 ** Returns AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
351 ** Otherwise, the error code defined by AVRCP 1.4
353 *******************************************************************************/
354 tAVRC_STS AVRC_Ctrl_ParsCommand (tAVRC_MSG *p_msg, tAVRC_COMMAND *p_result)
356 tAVRC_STS status = AVRC_STS_INTERNAL_ERR;
358 if (p_msg && p_result)
360 switch (p_msg->hdr.opcode)
362 case AVRC_OP_VENDOR: /* 0x00 Vendor-dependent commands */
363 status = avrc_ctrl_pars_vendor_cmd(&p_msg->vendor, p_result);
367 AVRC_TRACE_ERROR("%s unknown opcode:0x%x", __func__, p_msg->hdr.opcode);
370 p_result->cmd.opcode = p_msg->hdr.opcode;
371 p_result->cmd.status = status;
373 AVRC_TRACE_DEBUG("%s return status:0x%x", __FUNCTION__, status);
378 /*******************************************************************************
380 ** Function AVRC_ParsCommand
382 ** Description This function is a superset of AVRC_ParsMetadata to parse the command.
384 ** Returns AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
385 ** Otherwise, the error code defined by AVRCP 1.4
387 *******************************************************************************/
388 tAVRC_STS AVRC_ParsCommand (tAVRC_MSG *p_msg, tAVRC_COMMAND *p_result, UINT8 *p_buf, UINT16 buf_len)
390 tAVRC_STS status = AVRC_STS_INTERNAL_ERR;
393 if (p_msg && p_result)
395 switch (p_msg->hdr.opcode)
397 case AVRC_OP_VENDOR: /* 0x00 Vendor-dependent commands */
398 status = avrc_pars_vendor_cmd(&p_msg->vendor, p_result, p_buf, buf_len);
401 case AVRC_OP_PASS_THRU: /* 0x7C panel subunit opcode */
402 status = avrc_pars_pass_thru(&p_msg->pass, &id);
403 if (status == AVRC_STS_NO_ERROR)
405 p_result->pdu = (UINT8)id;
410 AVRC_TRACE_ERROR("%s unknown opcode:0x%x", __func__, p_msg->hdr.opcode);
413 p_result->cmd.opcode = p_msg->hdr.opcode;
414 p_result->cmd.status = status;
416 AVRC_TRACE_DEBUG("%s return status:0x%x", __func__, status);
420 #endif /* (AVRC_METADATA_INCLUDED == TRUE) */