OSDN Git Service

AVRCP: Initialize buffer for attribute values to be written to
[android-x86/system-bt.git] / stack / avrc / avrc_pars_ct.c
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
20 #include "bt_common.h"
21 #include "avrc_api.h"
22 #include "avrc_defs.h"
23 #include "avrc_int.h"
24 #include "bt_utils.h"
25 #include "log/log.h"
26
27 /*****************************************************************************
28 **  Global data
29 *****************************************************************************/
30
31 #if (AVRC_METADATA_INCLUDED == TRUE)
32
33 /*******************************************************************************
34 **
35 ** Function         avrc_pars_vendor_rsp
36 **
37 ** Description      This function parses the vendor specific commands defined by
38 **                  Bluetooth SIG
39 **
40 ** Returns          AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
41 **                  Otherwise, the error code defined by AVRCP 1.4
42 **
43 *******************************************************************************/
44 static tAVRC_STS avrc_pars_vendor_rsp(tAVRC_MSG_VENDOR *p_msg, tAVRC_RESPONSE *p_result)
45 {
46     tAVRC_STS  status = AVRC_STS_NO_ERROR;
47     UINT8   *p;
48     UINT16  len;
49 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
50     UINT8 eventid=0;
51 #endif
52
53     /* Check the vendor data */
54     if (p_msg->vendor_len == 0)
55         return AVRC_STS_NO_ERROR;
56     if (p_msg->p_vendor_data == NULL)
57         return AVRC_STS_INTERNAL_ERR;
58
59     p = p_msg->p_vendor_data;
60     BE_STREAM_TO_UINT8 (p_result->pdu, p);
61     p++; /* skip the reserved/packe_type byte */
62     BE_STREAM_TO_UINT16 (len, p);
63     AVRC_TRACE_DEBUG("%s ctype:0x%x pdu:0x%x, len:%d/0x%x",
64                      __func__, p_msg->hdr.ctype, p_result->pdu, len, len);
65     if (p_msg->hdr.ctype == AVRC_RSP_REJ)
66     {
67         p_result->rsp.status = *p;
68         return p_result->rsp.status;
69     }
70
71     switch (p_result->pdu)
72     {
73     /* case AVRC_PDU_REQUEST_CONTINUATION_RSP: 0x40 */
74     /* case AVRC_PDU_ABORT_CONTINUATION_RSP:   0x41 */
75
76 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
77     case AVRC_PDU_SET_ABSOLUTE_VOLUME:      /* 0x50 */
78         if (len != 1)
79             status = AVRC_STS_INTERNAL_ERR;
80         else
81         {
82             BE_STREAM_TO_UINT8 (p_result->volume.volume, p);
83         }
84         break;
85 #endif /* (AVRC_ADV_CTRL_INCLUDED == TRUE) */
86
87     case AVRC_PDU_REGISTER_NOTIFICATION:    /* 0x31 */
88 #if (AVRC_ADV_CTRL_INCLUDED == TRUE)
89         BE_STREAM_TO_UINT8 (eventid, p);
90         if(AVRC_EVT_VOLUME_CHANGE==eventid
91             && (AVRC_RSP_CHANGED==p_msg->hdr.ctype || AVRC_RSP_INTERIM==p_msg->hdr.ctype
92             || AVRC_RSP_REJ==p_msg->hdr.ctype || AVRC_RSP_NOT_IMPL==p_msg->hdr.ctype))
93         {
94             p_result->reg_notif.status=p_msg->hdr.ctype;
95             p_result->reg_notif.event_id=eventid;
96             BE_STREAM_TO_UINT8 (p_result->reg_notif.param.volume, p);
97         }
98         AVRC_TRACE_DEBUG("%s PDU reg notif response:event %x, volume %x",
99                          __func__, eventid, p_result->reg_notif.param.volume);
100 #endif /* (AVRC_ADV_CTRL_INCLUDED == TRUE) */
101         break;
102     default:
103         status = AVRC_STS_BAD_CMD;
104         break;
105     }
106
107     return status;
108 }
109
110 void avrc_parse_notification_rsp (UINT8 *p_stream, tAVRC_REG_NOTIF_RSP *p_rsp)
111 {
112     BE_STREAM_TO_UINT8(p_rsp->event_id, p_stream);
113     switch (p_rsp->event_id)
114     {
115         case AVRC_EVT_PLAY_STATUS_CHANGE:
116             BE_STREAM_TO_UINT8(p_rsp->param.play_status, p_stream);
117             break;
118
119         case AVRC_EVT_TRACK_CHANGE:
120             BE_STREAM_TO_ARRAY(p_stream, p_rsp->param.track, 8);
121             break;
122
123         case AVRC_EVT_APP_SETTING_CHANGE:
124             BE_STREAM_TO_UINT8(p_rsp->param.player_setting.num_attr, p_stream);
125             for (int index = 0; index < p_rsp->param.player_setting.num_attr; index++)
126             {
127                 BE_STREAM_TO_UINT8(p_rsp->param.player_setting.attr_id[index], p_stream);
128                 BE_STREAM_TO_UINT8(p_rsp->param.player_setting.attr_value[index], p_stream);
129             }
130             break;
131
132         case AVRC_EVT_NOW_PLAYING_CHANGE:
133             break;
134
135         case AVRC_EVT_AVAL_PLAYERS_CHANGE:
136             break;
137
138         case AVRC_EVT_ADDR_PLAYER_CHANGE:
139             break;
140
141         case AVRC_EVT_UIDS_CHANGE:
142             break;
143
144         case AVRC_EVT_TRACK_REACHED_END:
145         case AVRC_EVT_TRACK_REACHED_START:
146         case AVRC_EVT_PLAY_POS_CHANGED:
147         case AVRC_EVT_BATTERY_STATUS_CHANGE:
148         case AVRC_EVT_SYSTEM_STATUS_CHANGE:
149         default:
150             break;
151     }
152 }
153
154 #if (AVRC_CTLR_INCLUDED == TRUE)
155 /*******************************************************************************
156 **
157 ** Function         avrc_ctrl_pars_vendor_rsp
158 **
159 ** Description      This function parses the vendor specific commands defined by
160 **                  Bluetooth SIG
161 **
162 ** Returns          AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
163 **                  Otherwise, the error code defined by AVRCP 1.4
164 **
165 *******************************************************************************/
166 static tAVRC_STS avrc_ctrl_pars_vendor_rsp(
167     tAVRC_MSG_VENDOR *p_msg, tAVRC_RESPONSE *p_result, UINT8* p_buf, UINT16* buf_len)
168 {
169     UINT8   *p = p_msg->p_vendor_data;
170     BE_STREAM_TO_UINT8 (p_result->pdu, p);
171     p++; /* skip the reserved/packe_type byte */
172
173     UINT16  len;
174     BE_STREAM_TO_UINT16 (len, p);
175     AVRC_TRACE_DEBUG("%s ctype:0x%x pdu:0x%x, len:%d",
176                      __func__, p_msg->hdr.ctype, p_result->pdu, len);
177     /* Todo: Issue in handling reject, check */
178     if (p_msg->hdr.ctype == AVRC_RSP_REJ)
179     {
180         p_result->rsp.status = *p;
181         return p_result->rsp.status;
182     }
183
184     /* TODO: Break the big switch into functions. */
185     switch (p_result->pdu)
186     {
187     /* case AVRC_PDU_REQUEST_CONTINUATION_RSP: 0x40 */
188     /* case AVRC_PDU_ABORT_CONTINUATION_RSP:   0x41 */
189
190     case AVRC_PDU_REGISTER_NOTIFICATION:
191         avrc_parse_notification_rsp(p, &p_result->reg_notif);
192         break;
193
194     case AVRC_PDU_GET_CAPABILITIES:
195         if (len == 0)
196         {
197             p_result->get_caps.count = 0;
198             p_result->get_caps.capability_id = 0;
199             break;
200         }
201         BE_STREAM_TO_UINT8(p_result->get_caps.capability_id, p);
202         BE_STREAM_TO_UINT8(p_result->get_caps.count, p);
203         AVRC_TRACE_DEBUG("%s cap id = %d, cap_count = %d ",
204                          __func__, p_result->get_caps.capability_id, p_result->get_caps.count);
205         if (p_result->get_caps.capability_id == AVRC_CAP_COMPANY_ID)
206         {
207             for(int xx = 0; ((xx < p_result->get_caps.count) && (xx < AVRC_CAP_MAX_NUM_COMP_ID));
208                 xx++)
209             {
210                 BE_STREAM_TO_UINT24(p_result->get_caps.param.company_id[xx], p);
211             }
212         }
213         else if (p_result->get_caps.capability_id == AVRC_CAP_EVENTS_SUPPORTED)
214         {
215             for(int xx = 0; ((xx < p_result->get_caps.count) && (xx < AVRC_CAP_MAX_NUM_EVT_ID));
216                 xx++)
217             {
218                 BE_STREAM_TO_UINT8(p_result->get_caps.param.event_id[xx], p);
219             }
220         }
221         break;
222
223     case AVRC_PDU_LIST_PLAYER_APP_ATTR:
224         if (len == 0)
225         {
226             p_result->list_app_attr.num_attr = 0;
227             break;
228         }
229         BE_STREAM_TO_UINT8(p_result->list_app_attr.num_attr, p);
230         AVRC_TRACE_DEBUG("%s attr count = %d ", __func__, p_result->list_app_attr.num_attr);
231
232         if (p_result->list_app_attr.num_attr > AVRC_MAX_APP_ATTR_SIZE) {
233             android_errorWriteLog(0x534e4554, "63146237");
234             p_result->list_app_attr.num_attr = AVRC_MAX_APP_ATTR_SIZE;
235         }
236
237         for(int xx = 0; xx < p_result->list_app_attr.num_attr; xx++)
238         {
239             BE_STREAM_TO_UINT8(p_result->list_app_attr.attrs[xx], p);
240         }
241         break;
242
243     case AVRC_PDU_LIST_PLAYER_APP_VALUES:
244         if (len == 0)
245         {
246             p_result->list_app_values.num_val = 0;
247             break;
248         }
249         BE_STREAM_TO_UINT8(p_result->list_app_values.num_val, p);
250         AVRC_TRACE_DEBUG("%s value count = %d ", __func__, p_result->list_app_values.num_val);
251         for(int xx = 0; xx < p_result->list_app_values.num_val; xx++)
252         {
253             BE_STREAM_TO_UINT8(p_result->list_app_values.vals[xx], p);
254         }
255         break;
256
257     case AVRC_PDU_GET_CUR_PLAYER_APP_VALUE:
258     {
259         if (len == 0)
260         {
261             p_result->get_cur_app_val.num_val = 0;
262             break;
263         }
264         BE_STREAM_TO_UINT8(p_result->get_cur_app_val.num_val, p);
265         tAVRC_APP_SETTING *app_sett =
266             (tAVRC_APP_SETTING*)osi_malloc(p_result->get_cur_app_val.num_val*sizeof(tAVRC_APP_SETTING));
267         AVRC_TRACE_DEBUG("%s attr count = %d ", __func__, p_result->get_cur_app_val.num_val);
268
269         if (p_result->get_cur_app_val.num_val > AVRC_MAX_APP_ATTR_SIZE) {
270             android_errorWriteLog(0x534e4554, "63146237");
271             p_result->get_cur_app_val.num_val = AVRC_MAX_APP_ATTR_SIZE;
272         }
273
274         for (int xx = 0; xx < p_result->get_cur_app_val.num_val; xx++)
275         {
276             BE_STREAM_TO_UINT8(app_sett[xx].attr_id, p);
277             BE_STREAM_TO_UINT8(app_sett[xx].attr_val, p);
278         }
279         p_result->get_cur_app_val.p_vals = app_sett;
280     }
281         break;
282
283     case AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT:
284     {
285         UINT8                    num_attrs;
286
287         if (len == 0)
288         {
289             p_result->get_app_attr_txt.num_attr = 0;
290             break;
291         }
292         BE_STREAM_TO_UINT8(num_attrs, p);
293         if (num_attrs > AVRC_MAX_APP_ATTR_SIZE) {
294             num_attrs = AVRC_MAX_APP_ATTR_SIZE;
295         }
296         AVRC_TRACE_DEBUG("%s attr count = %d ", __func__, p_result->get_app_attr_txt.num_attr);
297         p_result->get_app_attr_txt.num_attr = num_attrs;
298         p_result->get_app_attr_txt.p_attrs = (tAVRC_APP_SETTING_TEXT*)osi_malloc(num_attrs * sizeof(tAVRC_APP_SETTING_TEXT));
299         for (int xx = 0; xx < num_attrs; xx++)
300         {
301             BE_STREAM_TO_UINT8(p_result->get_app_attr_txt.p_attrs[xx].attr_id, p);
302             BE_STREAM_TO_UINT16(p_result->get_app_attr_txt.p_attrs[xx].charset_id, p);
303             BE_STREAM_TO_UINT8(p_result->get_app_attr_txt.p_attrs[xx].str_len, p);
304             if (p_result->get_app_attr_txt.p_attrs[xx].str_len != 0)
305             {
306                 UINT8 *p_str = (UINT8 *)osi_malloc(p_result->get_app_attr_txt.p_attrs[xx].str_len);
307                 BE_STREAM_TO_ARRAY(p, p_str, p_result->get_app_attr_txt.p_attrs[xx].str_len);
308                 p_result->get_app_attr_txt.p_attrs[xx].p_str = p_str;
309             } else {
310                 p_result->get_app_attr_txt.p_attrs[xx].p_str = NULL;
311             }
312         }
313     }
314         break;
315
316     case AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT:
317     {
318         UINT8                    num_vals;
319
320         if (len == 0)
321         {
322             p_result->get_app_val_txt.num_attr = 0;
323             break;
324         }
325         BE_STREAM_TO_UINT8(num_vals, p);
326         if (num_vals > AVRC_MAX_APP_ATTR_SIZE) {
327             num_vals = AVRC_MAX_APP_ATTR_SIZE;
328         }
329         p_result->get_app_val_txt.num_attr = num_vals;
330         AVRC_TRACE_DEBUG("%s value count = %d ", __func__, p_result->get_app_val_txt.num_attr);
331
332         p_result->get_app_val_txt.p_attrs = (tAVRC_APP_SETTING_TEXT *)osi_malloc(num_vals * sizeof(tAVRC_APP_SETTING_TEXT));
333         for (int i = 0; i < num_vals; i++) {
334             BE_STREAM_TO_UINT8(p_result->get_app_val_txt.p_attrs[i].attr_id, p);
335             BE_STREAM_TO_UINT16(p_result->get_app_val_txt.p_attrs[i].charset_id, p);
336             BE_STREAM_TO_UINT8(p_result->get_app_val_txt.p_attrs[i].str_len, p);
337             if (p_result->get_app_val_txt.p_attrs[i].str_len != 0) {
338                 UINT8 *p_str = (UINT8 *)osi_malloc(p_result->get_app_val_txt.p_attrs[i].str_len);
339                 BE_STREAM_TO_ARRAY(p, p_str, p_result->get_app_val_txt.p_attrs[i].str_len);
340                 p_result->get_app_val_txt.p_attrs[i].p_str = p_str;
341             } else {
342                 p_result->get_app_val_txt.p_attrs[i].p_str = NULL;
343             }
344         }
345     }
346         break;
347
348     case AVRC_PDU_SET_PLAYER_APP_VALUE:
349         /* nothing comes as part of this rsp */
350         break;
351
352     case AVRC_PDU_GET_ELEMENT_ATTR:
353     {
354         UINT8               num_attrs;
355
356         if (len <= 0)
357         {
358             p_result->get_elem_attrs.num_attr = 0;
359             break;
360         }
361         BE_STREAM_TO_UINT8(num_attrs, p);
362         p_result->get_elem_attrs.num_attr = num_attrs;
363         if (num_attrs)
364         {
365             tAVRC_ATTR_ENTRY *p_attrs =
366                 (tAVRC_ATTR_ENTRY*)osi_malloc(num_attrs * sizeof(tAVRC_ATTR_ENTRY));
367             for (int i = 0; i < num_attrs; i++) {
368                 BE_STREAM_TO_UINT32(p_attrs[i].attr_id, p);
369                 BE_STREAM_TO_UINT16(p_attrs[i].name.charset_id, p);
370                 BE_STREAM_TO_UINT16(p_attrs[i].name.str_len, p);
371                 if (p_attrs[i].name.str_len > 0) {
372                     p_attrs[i].name.p_str = (UINT8 *)osi_malloc(p_attrs[i].name.str_len);
373                     BE_STREAM_TO_ARRAY(p, p_attrs[i].name.p_str, p_attrs[i].name.str_len);
374                 }
375             }
376             p_result->get_elem_attrs.p_attrs = p_attrs;
377         }
378     }
379         break;
380
381     case AVRC_PDU_GET_PLAY_STATUS:
382         if (len == 0)
383         {
384             break;
385         }
386         BE_STREAM_TO_UINT32(p_result->get_play_status.song_len, p);
387         BE_STREAM_TO_UINT32(p_result->get_play_status.song_pos, p);
388         BE_STREAM_TO_UINT8(p_result->get_play_status.play_status, p);
389         break;
390
391     default:
392         return AVRC_STS_BAD_CMD;
393     }
394     return AVRC_STS_NO_ERROR;
395 }
396
397 /*******************************************************************************
398 **
399 ** Function         AVRC_Ctrl_ParsResponse
400 **
401 ** Description      This function is a parse response for AVRCP Controller.
402 **
403 ** Returns          AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
404 **                  Otherwise, the error code defined by AVRCP 1.4
405 **
406 *******************************************************************************/
407 tAVRC_STS AVRC_Ctrl_ParsResponse (tAVRC_MSG *p_msg, tAVRC_RESPONSE *p_result, UINT8 *p_buf, UINT16* buf_len)
408 {
409     tAVRC_STS  status = AVRC_STS_INTERNAL_ERR;
410     if (p_msg && p_result)
411     {
412         switch (p_msg->hdr.opcode)
413         {
414         case AVRC_OP_VENDOR:     /*  0x00    Vendor-dependent commands */
415             status = avrc_ctrl_pars_vendor_rsp(&p_msg->vendor, p_result, p_buf,buf_len);
416             break;
417
418         default:
419             AVRC_TRACE_ERROR("%s unknown opcode:0x%x", __func__, p_msg->hdr.opcode);
420             break;
421         }
422         p_result->rsp.opcode = p_msg->hdr.opcode;
423         p_result->rsp.status = status;
424     }
425     return status;
426 }
427 #endif /* (AVRC_CTRL_INCLUDED) == TRUE) */
428 /*******************************************************************************
429 **
430 ** Function         AVRC_ParsResponse
431 **
432 ** Description      This function is a superset of AVRC_ParsMetadata to parse the response.
433 **
434 ** Returns          AVRC_STS_NO_ERROR, if the message in p_data is parsed successfully.
435 **                  Otherwise, the error code defined by AVRCP 1.4
436 **
437 *******************************************************************************/
438 tAVRC_STS AVRC_ParsResponse (tAVRC_MSG *p_msg, tAVRC_RESPONSE *p_result, UINT8 *p_buf, UINT16 buf_len)
439 {
440     tAVRC_STS  status = AVRC_STS_INTERNAL_ERR;
441     UINT16  id;
442     UNUSED(p_buf);
443     UNUSED(buf_len);
444
445     if (p_msg && p_result)
446     {
447         switch (p_msg->hdr.opcode)
448         {
449         case AVRC_OP_VENDOR:     /*  0x00    Vendor-dependent commands */
450             status = avrc_pars_vendor_rsp(&p_msg->vendor, p_result);
451             break;
452
453         case AVRC_OP_PASS_THRU:  /*  0x7C    panel subunit opcode */
454             status = avrc_pars_pass_thru(&p_msg->pass, &id);
455             if (status == AVRC_STS_NO_ERROR)
456             {
457                 p_result->pdu = (UINT8)id;
458             }
459             break;
460
461         default:
462             AVRC_TRACE_ERROR("%s unknown opcode:0x%x", __func__, p_msg->hdr.opcode);
463             break;
464         }
465         p_result->rsp.opcode = p_msg->hdr.opcode;
466         p_result->rsp.status = status;
467     }
468     return status;
469 }
470 #endif /* (AVRC_METADATA_INCLUDED == TRUE) */