xref: /btstack/example/avrcp_browsing_client.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avrcp_browsing_client.c"
39 
40 /*
41  * avrcp_browsing_client.c
42  */
43 
44 // *****************************************************************************
45 /* EXAMPLE_START(avrcp_browsing_client): Browse media players and media information on a remote device.
46  *
47  * @text This example demonstrates how to use the AVRCP Controller Browsing service to
48  * browse madia players and media information on a remote AVRCP Source device.
49  *
50  * @test To test with a remote device, e.g. a mobile phone,
51  * pair from the remote device with the demo, then use the UI for browsing (tap
52  * SPACE on the console to show the available AVRCP commands).
53  *
54  */
55 // *****************************************************************************
56 
57 #include <stdint.h>
58 #include <inttypes.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 
63 #include "btstack.h"
64 
65 #ifdef HAVE_BTSTACK_STDIN
66 #include "btstack_stdin.h"
67 #endif
68 
69 #define AVRCP_BROWSING_MAX_PLAYERS          10
70 #define AVRCP_BROWSING_MAX_FOLDERS          10
71 #define AVRCP_BROWSING_MAX_FOLDER_NAME_LEN  30
72 
73 #ifdef HAVE_BTSTACK_STDIN
74 // mac 2011: static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3};
75 // pts: static bd_addr_t remote = {0x00, 0x1B, 0xDC, 0x08, 0x0A, 0xA5};
76 // mac 2013:
77 // static const char * device_addr_string = "84:38:35:65:d1:15";
78 // iPhone 5S: static const char * device_addr_string = "54:E4:3A:26:A2:39";
79 // phone 2013:
80 static const char * device_addr_string = "D8:BB:2C:DF:F1:08";
81 static bd_addr_t device_addr;
82 #endif
83 
84 static uint16_t avrcp_cid = 0;
85 static uint8_t  avrcp_connected = 0;
86 
87 static uint16_t browsing_cid = 0;
88 static uint8_t  avrcp_browsing_connected = 0;
89 static uint8_t  sdp_avrcp_browsing_controller_service_buffer[200];
90 
91 static uint8_t browsing_query_active = 0;
92 static avrcp_media_item_context_t media_item_context;
93 
94 typedef struct {
95     uint16_t  charset;
96     uint8_t   depth;
97     uint16_t  name_len;
98     char      name[AVRCP_BROWSING_MAX_FOLDER_NAME_LEN];
99 } avrcp_browsing_root_folder_t;
100 
101 typedef struct {
102     uint8_t  uid[8];
103     uint16_t name_len;
104     char     name[AVRCP_BROWSING_MAX_FOLDER_NAME_LEN];
105 } avrcp_browsing_folders_t;
106 
107 static uint8_t  parent_folder_set = 0;
108 static uint8_t  parent_folder_uid[8];
109 static char     parent_folder_name[AVRCP_BROWSING_MAX_FOLDER_NAME_LEN];
110 static avrcp_browsing_folders_t folders[AVRCP_BROWSING_MAX_FOLDERS];
111 static int folder_index = -1;
112 static uint16_t players[AVRCP_BROWSING_MAX_PLAYERS];
113 static int player_index = -1;
114 
115 static btstack_packet_callback_registration_t hci_event_callback_registration;
116 
117 static uint8_t ertm_buffer[10000];
118 static l2cap_ertm_config_t ertm_config = {
119     1,  // ertm mandatory
120     2,  // max transmit, some tests require > 1
121     2000,
122     12000,
123     144,    // l2cap ertm mtu
124     4,
125     4,
126 };
127 
128 
129 static inline int next_index(int * index, int max_value){
130     if ((*index) < max_value){
131         (*index)++;
132     } else {
133         (*index) = 0;
134     }
135     return (*index);
136 }
137 
138 static int next_folder_index(){
139     return next_index(&folder_index, AVRCP_BROWSING_MAX_FOLDERS);
140 }
141 
142 static int next_player_index(){
143     return next_index(&player_index, AVRCP_BROWSING_MAX_PLAYERS);
144 }
145 
146 
147 /* @section Main Application Setup
148  *
149  * @text The Listing MainConfiguration shows how to setup AVRCP Controller Browsing service.
150  * To announce AVRCP Controller Browsing service, you need to create corresponding
151  * SDP record and register it with the SDP service.
152  * You'll also need to register several packet handlers:
153  * - stdin_process callback - used to trigger AVRCP commands, such are get media players, playlists, albums, etc. Requires HAVE_BTSTACK_STDIN.
154  * - avrcp_browsing_controller_packet_handler - used to receive answers for AVRCP commands.
155  *
156  */
157 
158 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP Controller services */
159 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
160 #ifdef HAVE_BTSTACK_STDIN
161 static void stdin_process(char cmd);
162 #endif
163 
164 #define BROWSING_ENABLED 1
165 
166 int btstack_main(int argc, const char * argv[]);
167 int btstack_main(int argc, const char * argv[]){
168     (void)argc;
169     (void)argv;
170 
171     // Register for HCI events.
172     hci_event_callback_registration.callback = &avrcp_browsing_controller_packet_handler;
173     hci_add_event_handler(&hci_event_callback_registration);
174 
175     // Initialize L2CAP.
176     l2cap_init();
177 
178     // Initialize AVRCP Controller.
179     avrcp_controller_init();
180     // Register AVRCP for HCI events.
181     avrcp_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
182 
183     // Initialize AVRCP Browsing Controller, HCI events will be sent to the AVRCP Controller callback.
184     avrcp_browsing_controller_init();
185     // // Register AVRCP for HCI events.
186     // avrcp_browsing_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
187 
188     // Initialize SDP.
189     sdp_init();
190 
191     // Create AVRCP service record and register it with SDP.
192     memset(sdp_avrcp_browsing_controller_service_buffer, 0, sizeof(sdp_avrcp_browsing_controller_service_buffer));
193     avrcp_controller_create_sdp_record(sdp_avrcp_browsing_controller_service_buffer, 0x10001, BROWSING_ENABLED, 1, NULL, NULL);
194     sdp_register_service(sdp_avrcp_browsing_controller_service_buffer);
195 
196     // Set local name with a template Bluetooth address, that will be automatically
197     // replaced with a actual address once it is available, i.e. when BTstack boots
198     // up and starts talking to a Bluetooth module.
199     gap_set_local_name("AVRCP Browsing Client 00:00:00:00:00:00");
200     gap_discoverable_control(1);
201     gap_set_class_of_device(0x200408);
202 
203 #ifdef HAVE_BTSTACK_STDIN
204     // Parse human readable Bluetooth address.
205     sscanf_bd_addr(device_addr_string, device_addr);
206     btstack_stdin_setup(stdin_process);
207 #endif
208     printf("Starting BTstack ...\n");
209     hci_power_control(HCI_POWER_ON);
210     return 0;
211 }
212 /* LISTING_END */
213 
214 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
215     UNUSED(channel);
216     int pos;
217 
218     switch(packet_type){
219         case AVRCP_BROWSING_DATA_PACKET:
220             pos = 0;
221             browsing_query_active = 1;
222             avrcp_browsing_item_type_t data_type = (avrcp_browsing_item_type_t)packet[pos++];
223             pos += 2; // length
224 
225             switch (data_type){
226                 case AVRCP_BROWSING_MEDIA_ROOT_FOLDER:{
227                     avrcp_browsing_root_folder_t root_folder;
228                     root_folder.charset = big_endian_read_16(packet, pos);
229                     pos += 2;
230                     root_folder.depth = packet[pos++];
231                     root_folder.name_len = big_endian_read_16(packet, pos);
232                     pos += 2;
233 
234                     memset(root_folder.name, 0, AVRCP_BROWSING_MAX_FOLDER_NAME_LEN);
235                     root_folder.name_len = btstack_min(big_endian_read_16(packet, pos), AVRCP_BROWSING_MAX_FOLDER_NAME_LEN - 1);
236                     memcpy(root_folder.name, packet+pos, root_folder.name_len);
237                     printf("Found root folder: name %s, depth %d \n", (char *)root_folder.name, root_folder.depth);
238                     break;
239                 }
240                 case AVRCP_BROWSING_MEDIA_PLAYER_ITEM:{
241                     printf("Received media player:  ");
242                     uint16_t player_id = big_endian_read_16(packet, pos);
243                     pos += 2;
244                     avrcp_browsing_media_player_major_type_t major_type = packet[pos++];
245                     avrcp_browsing_media_player_subtype_t subtype = big_endian_read_32(packet, pos);
246                     pos += 4;
247                     uint8_t status = packet[pos++];
248                     uint8_t feature_bitmask[16];
249                     memcpy(feature_bitmask, packet, 16);
250                     pos += 16;
251                     printf("player ID 0x%04x, major_type %d, subtype %d, status %d\n", player_id, major_type, subtype, status);
252                     players[next_player_index()] = player_id;
253                     break;
254                 }
255                 case AVRCP_BROWSING_FOLDER_ITEM:{
256                     int index = next_folder_index();
257                     // printf("Found folder [%d]: ", index);
258                     memcpy(folders[index].uid, packet+pos, 8);
259                     uint32_t folder_uid_high = big_endian_read_32(packet, pos);
260                     pos += 4;
261                     uint32_t folder_uid_low  = big_endian_read_32(packet, pos);
262                     pos += 4;
263                     avrcp_browsing_folder_type_t folder_type = packet[pos++];
264                     uint8_t  is_playable = packet[pos++];
265                     uint16_t charset = big_endian_read_16(packet, pos);
266                     pos += 2;
267                     uint16_t displayable_name_length = big_endian_read_16(packet, pos);
268                     pos += 2;
269 
270                     char value[AVRCP_BROWSING_MAX_FOLDER_NAME_LEN];
271 
272                     memset(value, 0, AVRCP_BROWSING_MAX_FOLDER_NAME_LEN);
273                     uint16_t value_len = btstack_min(displayable_name_length, AVRCP_BROWSING_MAX_FOLDER_NAME_LEN - 1);
274                     memcpy(value, packet+pos, value_len);
275 
276                     printf("UID 0x%08" PRIx32 "%08" PRIx32 ", type 0x%02x, is_playable %d, charset 0x%02x, name %s\n",
277                         folder_uid_high, folder_uid_low, folder_type, is_playable, charset, value);
278                     memcpy(folders[index].name, value, value_len);
279                     folders[index].name_len = value_len;
280                     break;
281                 }
282                 case AVRCP_BROWSING_MEDIA_ELEMENT_ITEM:{
283                     printf("Found media: ");
284 
285                     uint32_t media_uid_high = big_endian_read_32(packet, pos);
286                     pos += 4;
287                     uint32_t media_uid_low  = big_endian_read_32(packet, pos+4);
288                     pos += 4;
289                     avrcp_browsing_media_type_t media_type = packet[pos++];
290                     uint16_t charset = big_endian_read_16(packet, pos);
291                     pos += 2;
292                     uint16_t displayable_name_length = big_endian_read_16(packet, pos);
293                     pos += 2;
294                     pos += displayable_name_length;
295                     printf("Media UID: 0x%08" PRIx32 "%08" PRIx32 ", media_type 0x%02x, charset 0x%02x, displayable_name_length %d\n", media_uid_high, media_uid_low, media_type, charset, displayable_name_length);
296 
297                     uint8_t num_attributes = packet[pos++];
298                     printf("Num media attributes %d\n", num_attributes);
299 
300                     for (avrcp_media_item_iterator_init(&media_item_context, size-pos, packet+pos); avrcp_media_item_iterator_has_more(&media_item_context); avrcp_media_item_iterator_next(&media_item_context)){
301                         uint32_t attr_id            = avrcp_media_item_iterator_get_attr_id(&media_item_context);
302                         uint16_t attr_charset       = avrcp_media_item_iterator_get_attr_charset(&media_item_context);
303                         uint16_t attr_value_length  = avrcp_media_item_iterator_get_attr_value_len(&media_item_context);
304                         const uint8_t * attr_value  = avrcp_media_item_iterator_get_attr_value(&media_item_context);
305 
306                         printf("Attr ID 0x%08" PRIx32 ", charset %d, attr_value_length %d, value %s", attr_id, attr_charset, attr_value_length, attr_value);
307                     }
308                     break;
309                 }
310 
311                 default:
312                     log_error("AVRCP browsing: unknown browsable item type 0%02x", data_type);
313                     break;
314             }
315             break;
316         case HCI_EVENT_PACKET:
317             if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return;
318             uint16_t local_cid;
319             uint8_t  status = 0xFF;
320             bd_addr_t address;
321 
322             switch (packet[2]){
323                 case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: {
324                     local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet);
325                     if (browsing_cid != 0 && browsing_cid != local_cid) {
326                         printf("AVRCP Controller connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid);
327                         return;
328                     }
329 
330                     status = avrcp_subevent_connection_established_get_status(packet);
331                     if (status != ERROR_CODE_SUCCESS){
332                         printf("AVRCP Controller connection failed: status 0x%02x\n", status);
333                         browsing_cid = 0;
334                         return;
335                     }
336 
337                     avrcp_cid = local_cid;
338                     avrcp_connected = 1;
339                     avrcp_subevent_connection_established_get_bd_addr(packet, address);
340                     printf("AVRCP Controller connected.\n");
341                     return;
342                 }
343                 case AVRCP_SUBEVENT_CONNECTION_RELEASED:
344                     printf("AVRCP Browsing Client released.\n");
345                     browsing_cid = 0;
346                     avrcp_browsing_connected = 0;
347                     folder_index = 0;
348                     memset(folders, 0, sizeof(folders));
349                     return;
350                 case AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED: {
351                     local_cid = avrcp_subevent_browsing_connection_established_get_browsing_cid(packet);
352                     if (browsing_cid != 0 && browsing_cid != local_cid) {
353                         printf("AVRCP Browsing Client connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid);
354                         return;
355                     }
356 
357                     status = avrcp_subevent_browsing_connection_established_get_status(packet);
358                     if (status != ERROR_CODE_SUCCESS){
359                         printf("AVRCP Browsing Client connection failed: status 0x%02x\n", status);
360                         browsing_cid = 0;
361                         return;
362                     }
363 
364                     browsing_cid = local_cid;
365                     avrcp_browsing_connected = 1;
366                     avrcp_subevent_browsing_connection_established_get_bd_addr(packet, address);
367                     printf("AVRCP Browsing Client connected\n");
368                     return;
369                 }
370                 case AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED:
371                     printf("AVRCP Browsing Client released\n");
372                     browsing_cid = 0;
373                     avrcp_browsing_connected = 0;
374                     return;
375                 case AVRCP_SUBEVENT_BROWSING_MEDIA_ITEM_DONE:
376                     browsing_query_active = 0;
377                     if (avrcp_subevent_browsing_media_item_done_get_browsing_status(packet) != AVRCP_BROWSING_ERROR_CODE_SUCCESS){
378                         printf("AVRCP Browsing query done with browsing status 0x%02x, bluetooth status 0x%02x.\n",
379                             avrcp_subevent_browsing_media_item_done_get_browsing_status(packet),
380                             avrcp_subevent_browsing_media_item_done_get_bluetooth_status(packet));
381                         break;
382                     }
383                     printf("AVRCP Browsing cmd: DONE.\n");
384                     break;
385                 default:
386                     printf("AVRCP Browsing Client: event is not parsed\n");
387                     break;
388             }
389             break;
390 
391         default:
392             break;
393     }
394 
395 }
396 
397 #ifdef HAVE_BTSTACK_STDIN
398 static void show_usage(void){
399     bd_addr_t      iut_address;
400     gap_local_bd_addr(iut_address);
401     printf("\n--- Bluetooth AVRCP Controller Connection Test Console %s ---\n", bd_addr_to_str(iut_address));
402     printf("e      - AVRCP Controller create connection to addr %s\n", bd_addr_to_str(device_addr));
403     printf("c      - AVRCP Browsing Controller create connection to addr %s\n", bd_addr_to_str(device_addr));
404     printf("C      - AVRCP Browsing Controller disconnect\n");
405     printf("E      - AVRCP Controller disconnect\n");
406 
407     printf("a      - Set first found player as addressed player\n");
408     printf("b      - Set first found player as browsed player\n");
409 
410     printf("m      - Get media players\n");
411     printf("f      - Browse folders\n");
412     printf("u      - Go up one level\n");
413     printf("d      - Go down one level\n");
414     printf("i      - Browse media items\n");
415     printf("---\n");
416 }
417 #endif
418 
419 
420 #ifdef HAVE_BTSTACK_STDIN
421 static void stdin_process(char cmd){
422     uint8_t status = ERROR_CODE_SUCCESS;
423 
424     if (cmd != 'a' && cmd != 'A' && cmd != 'c' && cmd != 'C'){
425         if (browsing_query_active){
426             printf("Query active, try later!\n");
427             return;
428         }
429     }
430 
431     switch (cmd){
432         case 'e':
433             printf(" - Create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr));
434             status = avrcp_controller_connect(device_addr, &avrcp_cid);
435             break;
436         case 'E':
437             if (avrcp_connected){
438                 printf(" - AVRCP Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr));
439                 status = avrcp_controller_disconnect(avrcp_cid);
440                 break;
441             }
442             printf("AVRCP Controller already disconnected\n");
443             break;
444 
445         case 'c':
446             if (!avrcp_connected) {
447                 printf(" You must first create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr));
448                 break;
449             }
450             printf(" - Create AVRCP connection for browsing to addr %s.\n", bd_addr_to_str(device_addr));
451             status = avrcp_browsing_controller_connect(device_addr, ertm_buffer, sizeof(ertm_buffer), &ertm_config, &browsing_cid);
452             break;
453         case 'C':
454             if (avrcp_browsing_connected){
455                 printf(" - AVRCP Browsing Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr));
456                 status = avrcp_browsing_controller_disconnect(browsing_cid);
457                 break;
458             }
459             printf("AVRCP Browsing Controller already disconnected\n");
460             break;
461         case '\n':
462         case '\r':
463             break;
464 
465         default:
466             if (!avrcp_browsing_connected){
467                 show_usage();
468                 printf("Please connect the AVRCP Browsing client\n");
469                 break;
470             }
471 
472             switch (cmd) {
473                 case 'a':
474                     if (player_index < 0) {
475                         printf("Get media players first\n");
476                         break;
477                     }
478                     printf("Set addressed player\n");
479                     status = avrcp_browsing_controller_set_addressed_player(browsing_cid, players[0]);
480                     break;
481                 case 'b':
482                     if (player_index < 0) {
483                         printf("Get media players first\n");
484                         break;
485                     }
486                     printf("Set browsed player\n");
487                     status = avrcp_browsing_controller_set_browsed_player(browsing_cid, players[0]);
488                     break;
489                 case 'm':
490                     printf("AVRCP Browsing: get media players\n");
491                     player_index = -1;
492                     status = avrcp_browsing_controller_get_media_players(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL);
493                     break;
494                 case 'f':
495                     printf("AVRCP Browsing: browse folders\n");
496                     folder_index = -1;
497                     status = avrcp_browsing_controller_browse_file_system(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL);
498                     break;
499                 case 'i':
500                     printf("AVRCP Browsing: browse media items\n");
501                     avrcp_browsing_controller_browse_media(browsing_cid, 0, 0xFFFFFFFF, AVRCP_MEDIA_ATTR_ALL);
502                     break;
503                 case 'u':
504                     printf("AVRCP Browsing: go up one level\n");
505                     status = avrcp_browsing_controller_go_up_one_level(browsing_cid);
506                     folder_index = -1;
507                     break;
508                 case 'd':
509                     if (folder_index < 0 && !parent_folder_set){
510                         printf("AVRCP Browsing: no folders available\n");
511                         break;
512                     }
513                     if (!parent_folder_set){
514                         parent_folder_set = 1;
515                         memcpy(parent_folder_name, folders[0].name, folders[0].name_len);
516                         memcpy(parent_folder_uid, folders[0].uid, 8);
517                     }
518                     printf("AVRCP Browsing: go down one level of %s\n", (char *)parent_folder_name);
519                     status = avrcp_browsing_controller_go_down_one_level(browsing_cid, parent_folder_uid);
520                     folder_index = -1;
521                     break;
522                 default:
523                     show_usage();
524                     break;
525             }
526             break;
527     }
528 
529     if (status != ERROR_CODE_SUCCESS){
530         printf("Could not perform command, status 0x%2x\n", status);
531     }
532 }
533 #endif
534 /* EXAMPLE_END */
535