xref: /btstack/test/avrcp/avrcp_controller_test.c (revision 05fa60db8457e917100f449c82d2e025d3406ebe)
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     switch (cmd){
226         case 'c':
227             printf(" - Create AVRCP connection to addr %s.\n", bd_addr_to_str(device_addr));
228             avrcp_controller_connect(device_addr, &avrcp_cid);
229             break;
230         case 'B':
231             printf(" - Disconnect\n");
232             avrcp_controller_disconnect(avrcp_cid);
233             break;
234         case 'i':
235             printf(" - get play status\n");
236             avrcp_controller_get_play_status(avrcp_cid);
237             break;
238         case 'j':
239             printf(" - get now playing info\n");
240             avrcp_controller_get_now_playing_info(avrcp_cid);
241             break;
242         case 'k':
243             printf(" - play\n");
244             avrcp_controller_play(avrcp_cid);
245             break;
246         case 'K':
247             printf(" - stop\n");
248             avrcp_controller_stop(avrcp_cid);
249             break;
250         case 'L':
251             printf(" - pause\n");
252             avrcp_controller_pause(avrcp_cid);
253             break;
254         case 'm':
255             printf(" - start fast forward\n");
256             avrcp_controller_start_fast_forward(avrcp_cid);
257             break;
258         case 'M':
259             printf(" - stop fast forward\n");
260             avrcp_controller_stop_fast_forward(avrcp_cid);
261             break;
262         case 'n':
263             printf(" - start rewind\n");
264             avrcp_controller_start_rewind(avrcp_cid);
265             break;
266         case 'N':
267             printf(" - stop rewind\n");
268             avrcp_controller_stop_rewind(avrcp_cid);
269             break;
270         case 'o':
271             printf(" - forward\n");
272             avrcp_controller_forward(avrcp_cid);
273             break;
274         case 'O':
275             printf(" - backward\n");
276             avrcp_controller_backward(avrcp_cid);
277             break;
278         case 'p':
279             printf(" - volume up\n");
280             avrcp_controller_volume_up(avrcp_cid);
281             break;
282         case 'P':
283             printf(" - volume down\n");
284             avrcp_controller_volume_down(avrcp_cid);
285             break;
286         case 'r':
287             printf(" - absolute volume of 50 percent\n");
288             avrcp_controller_set_absolute_volume(avrcp_cid, 50);
289             break;
290         case 's':
291             printf(" - mute\n");
292             avrcp_controller_mute(avrcp_cid);
293             break;
294         case 't':
295             printf(" - skip\n");
296             avrcp_controller_skip(avrcp_cid);
297             break;
298         case 'u':
299             printf(" - query repeat and shuffle mode\n");
300             avrcp_controller_query_shuffle_and_repeat_modes(avrcp_cid);
301             break;
302         case 'v':
303             printf(" - repeat single track\n");
304             avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_SINGLE_TRACK);
305             break;
306         case 'x':
307             printf(" - repeat all tracks\n");
308             avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_ALL_TRACKS);
309             break;
310         case 'X':
311             printf(" - disable repeat mode\n");
312             avrcp_controller_set_repeat_mode(avrcp_cid, AVRCP_REPEAT_MODE_OFF);
313             break;
314         case 'z':
315             printf(" - shuffle all tracks\n");
316             avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_ALL_TRACKS);
317             break;
318         case 'Z':
319             printf(" - disable shuffle mode\n");
320             avrcp_controller_set_shuffle_mode(avrcp_cid, AVRCP_SHUFFLE_MODE_OFF);
321             break;
322         default:
323             show_usage();
324             break;
325     }
326 }
327 #endif
328 
329 int btstack_main(int argc, const char * argv[]);
330 int btstack_main(int argc, const char * argv[]){
331     UNUSED(argc);
332     (void)argv;
333     /* Register for HCI events */
334     hci_event_callback_registration.callback = &packet_handler;
335     hci_add_event_handler(&hci_event_callback_registration);
336 
337     l2cap_init();
338 
339     // Initialize AVRCP COntroller
340     avrcp_controller_init();
341     avrcp_controller_register_packet_handler(&packet_handler);
342 
343     // Initialize SDP
344     sdp_init();
345     memset(sdp_avrcp_controller_service_buffer, 0, sizeof(sdp_avrcp_controller_service_buffer));
346     avrcp_controller_create_sdp_record(sdp_avrcp_controller_service_buffer, 0x10001, AVRCP_BROWSING_ENABLED, 1, NULL, NULL);
347     sdp_register_service(sdp_avrcp_controller_service_buffer);
348 
349     gap_set_local_name("BTstack AVRCP Test");
350     gap_discoverable_control(1);
351     // gap_set_class_of_device(0x200408);
352 
353     // parse human readable Bluetooth address
354     sscanf_bd_addr(device_addr_string, device_addr);
355 
356     // turn on!
357     hci_power_control(HCI_POWER_ON);
358 
359 #ifdef HAVE_BTSTACK_STDIN
360     btstack_stdin_setup(stdin_process);
361 #endif
362     return 0;
363 }
364