xref: /btstack/example/avrcp_browsing_client.c (revision e07b53a61840d4ca0866385b8fc553ed6dbd96aa)
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 <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 
62 #include "btstack.h"
63 #include "avrcp_browsing_controller.h"
64 
65 #ifdef HAVE_BTSTACK_STDIN
66 #include "btstack_stdin.h"
67 #endif
68 
69 #ifdef HAVE_BTSTACK_STDIN
70 // mac 2011: static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3};
71 // pts: static bd_addr_t remote = {0x00, 0x1B, 0xDC, 0x08, 0x0A, 0xA5};
72 // mac 2013:
73 // static const char * device_addr_string = "84:38:35:65:d1:15";
74 // iPhone 5S: static const char * device_addr_string = "54:E4:3A:26:A2:39";
75 // phone 2013:
76 static const char * device_addr_string = "D8:BB:2C:DF:F1:08";
77 static bd_addr_t device_addr;
78 #endif
79 
80 static uint16_t avrcp_cid = 0;
81 static uint8_t  avrcp_connected = 0;
82 
83 static uint16_t browsing_cid = 0;
84 static uint8_t  avrcp_browsing_connected = 0;
85 static uint8_t  sdp_avrcp_browsing_controller_service_buffer[200];
86 
87 static btstack_packet_callback_registration_t hci_event_callback_registration;
88 
89 static uint8_t ertm_buffer[10000];
90 static l2cap_ertm_config_t ertm_config = {
91     1,  // ertm mandatory
92     2,  // max transmit, some tests require > 1
93     2000,
94     12000,
95     144,    // l2cap ertm mtu
96     4,
97     4,
98 };
99 
100 
101 /* @section Main Application Setup
102  *
103  * @text The Listing MainConfiguration shows how to setup AVRCP Controller Browsing service.
104  * To announce AVRCP Controller Browsing service, you need to create corresponding
105  * SDP record and register it with the SDP service.
106  * You'll also need to register several packet handlers:
107  * - stdin_process callback - used to trigger AVRCP commands, such are get media players, playlists, albums, etc. Requires HAVE_BTSTACK_STDIN.
108  * - avrcp_browsing_controller_packet_handler - used to receive answers for AVRCP commands.
109  *
110  */
111 
112 /* LISTING_START(MainConfiguration): Setup Audio Sink and AVRCP Controller services */
113 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
114 #ifdef HAVE_BTSTACK_STDIN
115 static void stdin_process(char cmd);
116 #endif
117 
118 #define BROWSING_ENABLED 1
119 
120 int btstack_main(int argc, const char * argv[]);
121 int btstack_main(int argc, const char * argv[]){
122     (void)argc;
123     (void)argv;
124 
125     // Register for HCI events.
126     hci_event_callback_registration.callback = &avrcp_browsing_controller_packet_handler;
127     hci_add_event_handler(&hci_event_callback_registration);
128 
129     // Initialize L2CAP.
130     l2cap_init();
131 
132     // Initialize AVRCP Controller.
133     avrcp_controller_init();
134     // Register AVRCP for HCI events.
135     avrcp_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
136 
137     // Initialize AVRCP Browsing Controller, HCI events will be sent to the AVRCP Controller callback.
138     avrcp_browsing_controller_init();
139 
140     // Initialize SDP.
141     sdp_init();
142 
143     // Create AVRCP service record and register it with SDP.
144     memset(sdp_avrcp_browsing_controller_service_buffer, 0, sizeof(sdp_avrcp_browsing_controller_service_buffer));
145     avrcp_controller_create_sdp_record(sdp_avrcp_browsing_controller_service_buffer, 0x10001, BROWSING_ENABLED, 1, NULL, NULL);
146     sdp_register_service(sdp_avrcp_browsing_controller_service_buffer);
147 
148     // Set local name with a template Bluetooth address, that will be automatically
149     // replaced with a actual address once it is available, i.e. when BTstack boots
150     // up and starts talking to a Bluetooth module.
151     gap_set_local_name("AVRCP Browsing Client 00:00:00:00:00:00");
152     gap_discoverable_control(1);
153     gap_set_class_of_device(0x200408);
154 
155 #ifdef HAVE_BTSTACK_STDIN
156     // Parse human readable Bluetooth address.
157     sscanf_bd_addr(device_addr_string, device_addr);
158     btstack_stdin_setup(stdin_process);
159 #endif
160     printf("Starting BTstack ...\n");
161     hci_power_control(HCI_POWER_ON);
162     return 0;
163 }
164 /* LISTING_END */
165 
166 
167 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
168     UNUSED(channel);
169     UNUSED(size);
170     if (packet_type != HCI_EVENT_PACKET) return;
171     if (hci_event_packet_get_type(packet) != HCI_EVENT_AVRCP_META) return;
172 
173     uint16_t local_cid;
174     uint8_t  status = 0xFF;
175     bd_addr_t adress;
176 
177     switch (packet[2]){
178         case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: {
179             local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet);
180             if (browsing_cid != 0 && browsing_cid != local_cid) {
181                 printf("AVRCP Browsing Client: AVRCP Controller connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid);
182                 return;
183             }
184 
185             status = avrcp_subevent_connection_established_get_status(packet);
186             if (status != ERROR_CODE_SUCCESS){
187                 printf("AVRCP Browsing Client: AVRCP Controller connection failed: status 0x%02x\n", status);
188                 browsing_cid = 0;
189                 return;
190             }
191 
192             avrcp_cid = local_cid;
193             avrcp_connected = 1;
194             avrcp_subevent_connection_established_get_bd_addr(packet, adress);
195             printf("AVRCP Browsing Client: AVRCP Controller Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(adress), avrcp_cid);
196             return;
197         }
198         case AVRCP_SUBEVENT_CONNECTION_RELEASED:
199             printf("AVRCP Browsing Client: AVRCP Controller Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet));
200             browsing_cid = 0;
201             avrcp_browsing_connected = 0;
202             return;
203 
204         case AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED: {
205             local_cid = avrcp_subevent_browsing_connection_established_get_browsing_cid(packet);
206             if (browsing_cid != 0 && browsing_cid != local_cid) {
207                 printf("AVRCP Browsing Client: AVRCP Browsing Controller Connection failed, expected 0x%02X l2cap cid, received 0x%02X\n", browsing_cid, local_cid);
208                 return;
209             }
210 
211             status = avrcp_subevent_browsing_connection_established_get_status(packet);
212             if (status != ERROR_CODE_SUCCESS){
213                 printf("AVRCP Browsing Client: AVRCP Browsing Controller Connection failed: status 0x%02x\n", status);
214                 browsing_cid = 0;
215                 return;
216             }
217 
218             browsing_cid = local_cid;
219             avrcp_browsing_connected = 1;
220             avrcp_subevent_browsing_connection_established_get_bd_addr(packet, adress);
221             printf("AVRCP Browsing Client: AVRCP Browsing Controller Channel successfully opened: %s, browsing_cid 0x%02x\n", bd_addr_to_str(adress), browsing_cid);
222             return;
223         }
224         case AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED:
225             printf("AVRCP Browsing Client: AVRCP Browsing Controller Channel released: browsing_cid 0x%02x\n", avrcp_subevent_browsing_connection_released_get_browsing_cid(packet));
226             browsing_cid = 0;
227             avrcp_browsing_connected = 0;
228             return;
229         default:
230             printf("AVRCP Browsing Client: event is not parsed\n");
231             break;
232     }
233 }
234 
235 #ifdef HAVE_BTSTACK_STDIN
236 static void show_usage(void){
237     bd_addr_t      iut_address;
238     gap_local_bd_addr(iut_address);
239     printf("\n--- Bluetooth AVRCP Controller Connection Test Console %s ---\n", bd_addr_to_str(iut_address));
240     printf("a      - AVRCP Controller create connection to addr %s\n", bd_addr_to_str(device_addr));
241     printf("c      - AVRCP Browsing Controller create connection to addr %s\n", bd_addr_to_str(device_addr));
242     printf("C      - AVRCP Browsing Controller disconnect\n");
243     printf("A      - AVRCP Controller disconnect\n");
244     printf("---\n");
245 }
246 #endif
247 
248 #ifdef HAVE_BTSTACK_STDIN
249 static void stdin_process(char cmd){
250     uint8_t status = ERROR_CODE_SUCCESS;
251 
252     switch (cmd){
253         case 'a':
254             printf(" - Create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr));
255             status = avrcp_controller_connect(device_addr, &avrcp_cid);
256             break;
257         case 'A':
258             if (avrcp_connected){
259                 printf(" - AVRCP Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr));
260                 status = avrcp_controller_disconnect(avrcp_cid);
261                 break;
262             }
263             printf("AVRCP Controller already disconnected\n");
264             break;
265 
266         case 'c':
267             if (!avrcp_connected) {
268                 printf(" You must first create AVRCP connection for control to addr %s.\n", bd_addr_to_str(device_addr));
269                 break;
270             }
271             printf(" - Create AVRCP connection for browsing to addr %s.\n", bd_addr_to_str(device_addr));
272             status = avrcp_browsing_controller_connect(device_addr, ertm_buffer, sizeof(ertm_buffer), &ertm_config, &browsing_cid);
273             break;
274         case 'C':
275             if (avrcp_browsing_connected){
276                 printf(" - AVRCP Browsing Controller disconnect from addr %s.\n", bd_addr_to_str(device_addr));
277                 status = avrcp_browsing_controller_disconnect(browsing_cid);
278                 break;
279             }
280             printf("AVRCP Browsing Controller already disconnected\n");
281             break;
282         case '\n':
283         case '\r':
284             break;
285         default:
286             show_usage();
287             return;
288     }
289     if (status != ERROR_CODE_SUCCESS){
290         printf("Could not perform command, status 0x%2x\n", status);
291     }
292 }
293 #endif
294 /* EXAMPLE_END */
295