xref: /btstack/test/avrcp/avrcp_controller_test.c (revision 2e1f7af07f6a4d201438589c1d8aae3edfdaa45e)
1 /*
2  * Copyright (C) 2017 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 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "btstack.h"
44 
45 #define AVRCP_BROWSING_ENABLED 0
46 static btstack_packet_callback_registration_t hci_event_callback_registration;
47 
48 static bd_addr_t device_addr;
49 
50 // iPhone SE: static const char * device_addr_string = "BC:EC:5D:E6:15:03";
51 // iPhone 6:  static const char * device_addr_string = "D8:BB:2C:DF:F1:08";
52 // iPhone 5S:
53 static const char * device_addr_string = "54:E4:3A:26:A2:39";
54 // Wiko Sunny: static const char * device_addr_string = "A0-4C-5B-0F-B2-42";
55 // pts: static const char * device_addr_string = "00:1B:DC:08:0A:A5";
56 
57 static uint16_t avrcp_cid = 0;
58 static uint8_t sdp_avrcp_controller_service_buffer[200];
59 
60 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
61     UNUSED(channel);
62     UNUSED(size);
63     bd_addr_t event_addr;
64     uint16_t local_cid;
65     uint8_t  status = 0xFF;
66     switch (packet_type) {
67         case HCI_EVENT_PACKET:
68             switch (hci_event_packet_get_type(packet)) {
69                 case HCI_EVENT_DISCONNECTION_COMPLETE:
70                     // connection closed -> quit test app
71                     printf("AVRCP: HCI_EVENT_DISCONNECTION_COMPLETE\n");
72                     break;
73                 case HCI_EVENT_AVRCP_META:
74                     switch (packet[2]){
75                         case AVRCP_SUBEVENT_CONNECTION_ESTABLISHED: {
76                             local_cid = avrcp_subevent_connection_established_get_avrcp_cid(packet);
77                             if (avrcp_cid != local_cid) {
78                                 printf("Connection is not established, expected 0x%02X l2cap cid, received 0x%02X\n", avrcp_cid, local_cid);
79                                 return;
80                             }
81 
82                             status = avrcp_subevent_connection_established_get_status(packet);
83                             avrcp_subevent_connection_established_get_bd_addr(packet, event_addr);
84                             if (status != ERROR_CODE_SUCCESS){
85                                 printf("AVRCP Connection failed: status 0x%02x\n", status);
86                                 avrcp_cid = 0;
87                                 return;
88                             }
89                             printf("Channel successfully opened: %s, avrcp_cid 0x%02x\n", bd_addr_to_str(event_addr), avrcp_cid);
90                             // automatically enable notifications
91                             avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED);
92                             avrcp_controller_enable_notification(avrcp_cid, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED);
93                             return;
94                         }
95                         case AVRCP_SUBEVENT_CONNECTION_RELEASED:
96                             printf("Channel released: avrcp_cid 0x%02x\n", avrcp_subevent_connection_released_get_avrcp_cid(packet));
97                             avrcp_cid = 0;
98                             return;
99                         default:
100                             break;
101                     }
102 
103                     status = packet[5];
104                     local_cid = little_endian_read_16(packet, 3);
105                     if (avrcp_cid != local_cid) return;
106 
107                     // avoid printing INTERIM status
108                     if (status == AVRCP_CTYPE_RESPONSE_INTERIM) return;
109 
110                     printf("AVRCP: command status: %s, ", avrcp_ctype2str(status));
111                     switch (packet[2]){
112                         case AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED:
113                             printf("notification, playback status changed %s\n", avrcp_play_status2str(avrcp_subevent_notification_playback_status_changed_get_play_status(packet)));
114                             return;
115                         case AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED:
116                             printf("notification, playing content changed\n");
117                             return;
118                         case AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED:
119                             printf("notification track changed\n");
120                             return;
121                         case AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED:
122                             printf("notification absolute volume changed %d\n", avrcp_subevent_notification_volume_changed_get_absolute_volume(packet));
123                             return;
124                         case AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED:
125                             printf("notification changed\n");
126                             return;
127                         case AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE:{
128                             uint8_t shuffle_mode = avrcp_subevent_shuffle_and_repeat_mode_get_shuffle_mode(packet);
129                             uint8_t repeat_mode  = avrcp_subevent_shuffle_and_repeat_mode_get_repeat_mode(packet);
130                             printf("%s, %s\n", avrcp_shuffle2str(shuffle_mode), avrcp_repeat2str(repeat_mode));
131                             break;
132                         }
133                         case AVRCP_SUBEVENT_NOW_PLAYING_INFO:{
134                             uint8_t value[100];
135                             printf("now playing: \n");
136                             if (avrcp_subevent_now_playing_info_get_title_len(packet) > 0){
137                                 memcpy(value, avrcp_subevent_now_playing_info_get_title(packet), avrcp_subevent_now_playing_info_get_title_len(packet));
138                                 printf("    Title: %s\n", value);
139                             }
140                             if (avrcp_subevent_now_playing_info_get_album_len(packet) > 0){
141                                 memcpy(value, avrcp_subevent_now_playing_info_get_album(packet), avrcp_subevent_now_playing_info_get_album_len(packet));
142                                 printf("    Album: %s\n", value);
143                             }
144                             if (avrcp_subevent_now_playing_info_get_artist_len(packet) > 0){
145                                 memcpy(value, avrcp_subevent_now_playing_info_get_artist(packet), avrcp_subevent_now_playing_info_get_artist_len(packet));
146                                 printf("    Artist: %s\n", value);
147                             }
148                             if (avrcp_subevent_now_playing_info_get_genre_len(packet) > 0){
149                                 memcpy(value, avrcp_subevent_now_playing_info_get_genre(packet), avrcp_subevent_now_playing_info_get_genre_len(packet));
150                                 printf("    Genre: %s\n", value);
151                             }
152                             printf("    Track: %d\n", avrcp_subevent_now_playing_info_get_track(packet));
153                             printf("    Total nr. tracks: %d\n", avrcp_subevent_now_playing_info_get_total_tracks(packet));
154                             printf("    Song length: %d ms\n", avrcp_subevent_now_playing_info_get_song_length(packet));
155                             break;
156                         }
157                         case AVRCP_SUBEVENT_PLAY_STATUS:
158                             printf("song length: %d ms, song position: %d ms, play status: %s\n",
159                                 avrcp_subevent_play_status_get_song_length(packet),
160                                 avrcp_subevent_play_status_get_song_position(packet),
161                                 avrcp_play_status2str(avrcp_subevent_play_status_get_play_status(packet)));
162                             break;
163                         case AVRCP_SUBEVENT_OPERATION_COMPLETE:
164                             printf("operation done %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet)));
165                             break;
166                         case AVRCP_SUBEVENT_OPERATION_START:
167                             printf("operation start %s\n", avrcp_operation2str(avrcp_subevent_operation_complete_get_operation_id(packet)));
168                             break;
169                         case AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE:
170                             // response to set shuffle and repeat mode
171                             printf("\n");
172                             break;
173                         default:
174                             printf("Not implemented\n");
175                             break;
176                     }
177                     break;
178                 default:
179                     break;
180             }
181             break;
182         default:
183             // other packet type
184             break;
185     }
186 
187 }
188 
189 #ifdef HAVE_BTSTACK_STDIN
190 static void show_usage(void){
191     bd_addr_t      iut_address;
192     gap_local_bd_addr(iut_address);
193     printf("\n--- Bluetooth AVRCP Test Console %s ---\n", bd_addr_to_str(iut_address));
194     printf("c      - create connection to addr %s\n", bd_addr_to_str(device_addr));
195     printf("D      - disconnect\n");
196     printf("\n--- Bluetooth AVRCP Commands ---\n");
197     printf("i - get play status\n");
198     printf("j - get now playing info\n");
199     printf("k - play\n");
200     printf("K - stop\n");
201     printf("L - pause\n");
202     printf("m - start fast forward\n");
203     printf("M - stop  fast forward\n");
204     printf("n - start rewind\n");
205     printf("N - stop rewind\n");
206     printf("o - forward\n");
207     printf("O - backward\n");
208     printf("p - volume up\n");
209     printf("P - volume down\n");
210     printf("r - absolute volume of 50 percent\n");
211     printf("s - mute\n");
212     printf("t - skip\n");
213     printf("u - query repeat and shuffle mode\n");
214     printf("v - repeat single track\n");
215     printf("x - repeat all tracks\n");
216     printf("X - disable repeat mode\n");
217     printf("z - shuffle all tracks\n");
218     printf("Z - disable shuffle mode\n");
219 
220     printf("Ctrl-c - exit\n");
221     printf("---\n");
222 }
223 
224 static void stdin_process(char cmd){
225     uint8_t status = ERROR_CODE_SUCCESS;
226     switch (cmd){
227         case 'c':
228             printf(" - Create AVRCP connection to addr %s.\n", bd_addr_to_str(device_addr));
229             status = avrcp_controller_connect(device_addr, &avrcp_cid);
230             break;
231         case 'B':
232             printf(" - Disconnect\n");
233             status = avrcp_controller_disconnect(avrcp_cid);
234             break;
235         case 'i':
236             printf(" - get play status\n");
237             status = avrcp_controller_get_play_status(avrcp_cid);
238             break;
239         case 'j':
240             printf(" - get now playing info\n");
241             status = avrcp_controller_get_now_playing_info(avrcp_cid);
242             break;
243         case 'k':
244             printf(" - play\n");
245             status = avrcp_controller_play(avrcp_cid);
246             break;
247         case 'K':
248             printf(" - stop\n");
249             status = avrcp_controller_stop(avrcp_cid);
250             break;
251         case 'L':
252             printf(" - pause\n");
253             status = avrcp_controller_pause(avrcp_cid);
254             break;
255         case 'm':
256             printf(" - start fast forward\n");
257             status = avrcp_controller_start_fast_forward(avrcp_cid);
258             break;
259         case 'M':
260             printf(" - stop fast forward\n");
261             status = avrcp_controller_stop_fast_forward(avrcp_cid);
262             break;
263         case 'n':
264             printf(" - start rewind\n");
265             status = avrcp_controller_start_rewind(avrcp_cid);
266             break;
267         case 'N':
268             printf(" - stop rewind\n");
269             status = avrcp_controller_stop_rewind(avrcp_cid);
270             break;
271         case 'o':
272             printf(" - forward\n");
273             status = avrcp_controller_forward(avrcp_cid);
274             break;
275         case 'O':
276             printf(" - backward\n");
277             status = avrcp_controller_backward(avrcp_cid);
278             break;
279         case 'p':
280             printf(" - volume up\n");
281             status = avrcp_controller_volume_up(avrcp_cid);
282             break;
283         case 'P':
284             printf(" - volume down\n");
285             status = avrcp_controller_volume_down(avrcp_cid);
286             break;
287         case 'r':
288             printf(" - absolute volume of 50 percent\n");
289             status = avrcp_controller_set_absolute_volume(avrcp_cid, 50);
290             break;
291         case 's':
292             printf(" - mute\n");
293             status = avrcp_controller_mute(avrcp_cid);
294             break;
295         case 't':
296             printf(" - skip\n");
297             status = avrcp_controller_skip(avrcp_cid);
298             break;
299         case 'u':
300             printf(" - query repeat and shuffle mode\n");
301             status = avrcp_controller_query_shuffle_and_repeat_modes(avrcp_cid);
302             break;
303         case 'v':
304             printf(" - repeat single track\n");
305             status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK);
306             break;
307         case 'x':
308             printf(" - repeat all tracks\n");
309             status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_ALL_TRACKS);
310             break;
311         case 'X':
312             printf(" - disable repeat mode\n");
313             status = avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_OFF);
314             break;
315         case 'z':
316             printf(" - shuffle all tracks\n");
317             status = avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS);
318             break;
319         case 'Z':
320             printf(" - disable shuffle mode\n");
321             status = avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_OFF);
322             break;
323         default:
324             show_usage();
325             return;
326     }
327     if (status != ERROR_CODE_SUCCESS){
328         printf("Could not perform command, status 0x%02x\n", status);
329     }
330 }
331 #endif
332 
333 int btstack_main(int argc, const char * argv[]);
334 int btstack_main(int argc, const char * argv[]){
335     UNUSED(argc);
336     (void)argv;
337     /* Register for HCI events */
338     hci_event_callback_registration.callback = &packet_handler;
339     hci_add_event_handler(&hci_event_callback_registration);
340 
341     l2cap_init();
342 
343     // Initialize AVRCP COntroller
344     avrcp_controller_init();
345     avrcp_controller_register_packet_handler(&packet_handler);
346 
347     // Initialize SDP
348     sdp_init();
349     memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer));
350     avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, 0x10001, AVRCP_BROWSING_ENABLED, 1, NULL, NULL);
351     sdp_register_service(sdp_avrcp_controller_service_buffer);
352 
353     gap_set_local_name("BTstack AVRCP Test");
354     gap_discoverable_control(1);
355     // gap_set_class_of_device(0x200408);
356 
357     // parse human readable Bluetooth address
358     sscanf_bd_addr(device_addr_string, device_addr);
359 
360     // turn on!
361     hci_power_control(HCI_POWER_ON);
362 
363 #ifdef HAVE_BTSTACK_STDIN
364     btstack_stdin_setup(stdin_process);
365 #endif
366     return 0;
367 }
368