1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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 "avrc_int.h"
22 
23 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
24 
25 #if (AVRC_METADATA_INCLUDED == TRUE)
26 
27 /**************************************************************************
28 **
29 ** Function         AVRC_IsValidAvcType
30 **
31 ** Description      Check if correct AVC type is specified
32 **
33 ** Returns          returns TRUE if it is valid
34 **
35 **
36 *******************************************************************************/
AVRC_IsValidAvcType(UINT8 pdu_id,UINT8 avc_type)37 BOOLEAN AVRC_IsValidAvcType(UINT8 pdu_id, UINT8 avc_type)
38 {
39     BOOLEAN result = FALSE;
40 
41     if (avc_type < AVRC_RSP_NOT_IMPL) { /* command msg */
42         switch (pdu_id) {
43         case AVRC_PDU_GET_CAPABILITIES:            /* 0x10 */
44         case AVRC_PDU_LIST_PLAYER_APP_ATTR:        /* 0x11 */
45         case AVRC_PDU_LIST_PLAYER_APP_VALUES:      /* 0x12 */
46         case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:    /* 0x13 */
47         case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:    /* 0x15 */
48         case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:   /* 0x16 */
49         case AVRC_PDU_GET_ELEMENT_ATTR:            /* 0x20 */
50         case AVRC_PDU_GET_PLAY_STATUS:             /* 0x30 */
51             if (avc_type == AVRC_CMD_STATUS) {
52                 result = TRUE;
53             }
54             break;
55 
56         case AVRC_PDU_SET_PLAYER_APP_VALUE:        /* 0x14 */
57         case AVRC_PDU_INFORM_DISPLAY_CHARSET:      /* 0x17 */
58         case AVRC_PDU_INFORM_BATTERY_STAT_OF_CT:   /* 0x18 */
59         case AVRC_PDU_REQUEST_CONTINUATION_RSP:    /* 0x40 */
60         case AVRC_PDU_ABORT_CONTINUATION_RSP:      /* 0x41 */
61         case AVRC_PDU_SET_ABSOLUTE_VOLUME:         /* 0x50 */
62             if (avc_type == AVRC_CMD_CTRL) {
63                 result = TRUE;
64             }
65             break;
66 
67         case AVRC_PDU_REGISTER_NOTIFICATION:       /* 0x31 */
68             if (avc_type == AVRC_CMD_NOTIF) {
69                 result = TRUE;
70             }
71             break;
72         }
73     } else { /* response msg */
74         if (avc_type >= AVRC_RSP_NOT_IMPL  &&
75                 avc_type <= AVRC_RSP_INTERIM    ) {
76             result = TRUE;
77         }
78     }
79 
80     return result;
81 }
82 
83 /*******************************************************************************
84 **
85 ** Function         avrc_is_valid_player_attrib_value
86 **
87 ** Description      Check if the given attrib value is valid for its attribute
88 **
89 ** Returns          returns TRUE if it is valid
90 **
91 *******************************************************************************/
avrc_is_valid_player_attrib_value(UINT8 attrib,UINT8 value)92 BOOLEAN avrc_is_valid_player_attrib_value(UINT8 attrib, UINT8 value)
93 {
94     BOOLEAN result = FALSE;
95 
96     switch (attrib) {
97     case AVRC_PLAYER_SETTING_EQUALIZER:
98         if ((value > 0)  &&
99                 (value <= AVRC_PLAYER_VAL_ON)) {
100             result = TRUE;
101         }
102         break;
103 
104     case AVRC_PLAYER_SETTING_REPEAT:
105         if ((value > 0)  &&
106                 (value <= AVRC_PLAYER_VAL_GROUP_REPEAT)) {
107             result = TRUE;
108         }
109         break;
110 
111     case AVRC_PLAYER_SETTING_SHUFFLE:
112     case AVRC_PLAYER_SETTING_SCAN:
113         if ((value > 0)  &&
114                 (value <= AVRC_PLAYER_VAL_GROUP_SHUFFLE)) {
115             result = TRUE;
116         }
117         break;
118     }
119 
120     if (attrib >= AVRC_PLAYER_SETTING_LOW_MENU_EXT) {
121         result = TRUE;
122     }
123 
124     if (!result) {
125         AVRC_TRACE_ERROR(
126             "avrc_is_valid_player_attrib_value() found not matching attrib(x%x)-value(x%x) pair!",
127             attrib, value);
128     }
129 
130     return result;
131 }
132 
133 /*******************************************************************************
134 **
135 ** Function         AVRC_IsValidPlayerAttr
136 **
137 ** Description      Check if the given attrib value is a valid one
138 **
139 ** Returns          returns TRUE if it is valid
140 **
141 *******************************************************************************/
AVRC_IsValidPlayerAttr(UINT8 attr)142 BOOLEAN AVRC_IsValidPlayerAttr(UINT8 attr)
143 {
144     BOOLEAN result = FALSE;
145 
146     if ( (attr >= AVRC_PLAYER_SETTING_EQUALIZER && attr <= AVRC_PLAYER_SETTING_SCAN) ||
147             (attr >= AVRC_PLAYER_SETTING_LOW_MENU_EXT) ) {
148         result = TRUE;
149     }
150 
151     return result;
152 }
153 
154 
155 
156 /*******************************************************************************
157 **
158 ** Function         avrc_pars_pass_thru
159 **
160 ** Description      This function parses the pass thru commands defined by
161 **                  Bluetooth SIG
162 **
163 ** Returns          AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
164 **                  Otherwise, the error code defined by AVRCP 1.4
165 **
166 *******************************************************************************/
avrc_pars_pass_thru(tAVRC_MSG_PASS * p_msg,UINT16 * p_vendor_unique_id)167 tAVRC_STS avrc_pars_pass_thru(tAVRC_MSG_PASS *p_msg, UINT16 *p_vendor_unique_id)
168 {
169     UINT8      *p_data;
170     UINT32      co_id;
171     UINT16      id;
172     tAVRC_STS  status = AVRC_STS_BAD_CMD;
173 
174     if (p_msg->op_id == AVRC_ID_VENDOR && p_msg->pass_len == AVRC_PASS_THRU_GROUP_LEN) {
175         p_data = p_msg->p_pass_data;
176         AVRC_BE_STREAM_TO_CO_ID (co_id, p_data);
177         if (co_id == AVRC_CO_METADATA) {
178             BE_STREAM_TO_UINT16 (id, p_data);
179             if (AVRC_IS_VALID_GROUP(id)) {
180                 *p_vendor_unique_id = id;
181                 status = AVRC_STS_NO_ERROR;
182             }
183         }
184     }
185     return status;
186 }
187 
188 /*******************************************************************************
189 **
190 ** Function         avrc_opcode_from_pdu
191 **
192 ** Description      This function returns the opcode of the given pdu
193 **
194 ** Returns          AVRC_OP_VENDOR, AVRC_OP_PASS_THRU or AVRC_OP_BROWSE
195 **
196 *******************************************************************************/
avrc_opcode_from_pdu(UINT8 pdu)197 UINT8 avrc_opcode_from_pdu(UINT8 pdu)
198 {
199     UINT8 opcode = 0;
200 
201     switch (pdu) {
202     case AVRC_PDU_NEXT_GROUP:
203     case AVRC_PDU_PREV_GROUP: /* pass thru */
204         opcode  = AVRC_OP_PASS_THRU;
205         break;
206 
207     default: /* vendor */
208         opcode  = AVRC_OP_VENDOR;
209         break;
210     }
211 
212     return opcode;
213 }
214 
215 /*******************************************************************************
216 **
217 ** Function         avrc_is_valid_opcode
218 **
219 ** Description      This function returns the opcode of the given pdu
220 **
221 ** Returns          AVRC_OP_VENDOR, AVRC_OP_PASS_THRU or AVRC_OP_BROWSE
222 **
223 *******************************************************************************/
avrc_is_valid_opcode(UINT8 opcode)224 BOOLEAN avrc_is_valid_opcode(UINT8 opcode)
225 {
226     BOOLEAN is_valid = FALSE;
227     switch (opcode) {
228     case AVRC_OP_BROWSE:
229     case AVRC_OP_PASS_THRU:
230     case AVRC_OP_VENDOR:
231         is_valid = TRUE;
232         break;
233     }
234     return is_valid;
235 }
236 
237 #endif /* (AVRC_METADATA_INCLUDED == TRUE) */
238 
239 #endif /* #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE) */
240