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