1f7c85330SMatthias Ringwald /* 2f7c85330SMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH 3f7c85330SMatthias Ringwald * 4f7c85330SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5f7c85330SMatthias Ringwald * modification, are permitted provided that the following conditions 6f7c85330SMatthias Ringwald * are met: 7f7c85330SMatthias Ringwald * 8f7c85330SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9f7c85330SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10f7c85330SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11f7c85330SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12f7c85330SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13f7c85330SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14f7c85330SMatthias Ringwald * contributors may be used to endorse or promote products derived 15f7c85330SMatthias Ringwald * from this software without specific prior written permission. 16f7c85330SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17f7c85330SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18f7c85330SMatthias Ringwald * monetary gain. 19f7c85330SMatthias Ringwald * 20f7c85330SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21f7c85330SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22f7c85330SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23f7c85330SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24f7c85330SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25f7c85330SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26f7c85330SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27f7c85330SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28f7c85330SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29f7c85330SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30f7c85330SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31f7c85330SMatthias Ringwald * SUCH DAMAGE. 32f7c85330SMatthias Ringwald * 33f7c85330SMatthias Ringwald * Please inquire about commercial licensing options at 34f7c85330SMatthias Ringwald * [email protected] 35f7c85330SMatthias Ringwald * 36f7c85330SMatthias Ringwald */ 37f7c85330SMatthias Ringwald 38f7c85330SMatthias Ringwald /* 39f7c85330SMatthias Ringwald * sco_demo_util.c - send/receive test data via SCO, used by hfp_*_demo and hsp_*_demo 40f7c85330SMatthias Ringwald */ 41f7c85330SMatthias Ringwald 42f7c85330SMatthias Ringwald #include "sco_demo_util.h" 43f7c85330SMatthias Ringwald #include <stdio.h> 44f7c85330SMatthias Ringwald 45f7c85330SMatthias Ringwald // configure test mode 46f7c85330SMatthias Ringwald #define SCO_DEMO_MODE_SINE 0 47f7c85330SMatthias Ringwald #define SCO_DEMO_MODE_ASCII 1 48f7c85330SMatthias Ringwald #define SCO_DEMO_MODE_COUNTER 2 49f7c85330SMatthias Ringwald 508b29cfc6SMatthias Ringwald 51f7c85330SMatthias Ringwald // SCO demo configuration 52*38b2eaafSMatthias Ringwald #define SCO_DEMO_MODE SCO_DEMO_MODE_ASCII 53f7c85330SMatthias Ringwald #define SCO_REPORT_PERIOD 100 54f7c85330SMatthias Ringwald 558b29cfc6SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 568b29cfc6SMatthias Ringwald #define SCO_WAV_FILENAME "sco_input.wav" 578b29cfc6SMatthias Ringwald #define SCO_WAV_DURATION_IN_SECONDS 30 588b29cfc6SMatthias Ringwald #endif 598b29cfc6SMatthias Ringwald 60f7c85330SMatthias Ringwald 61f7c85330SMatthias Ringwald #if defined(HAVE_PORTAUDIO) && (SCO_DEMO_MODE == SCO_DEMO_MODE_SINE) 62f7c85330SMatthias Ringwald #define USE_PORTAUDIO 63f7c85330SMatthias Ringwald #endif 64f7c85330SMatthias Ringwald 65f7c85330SMatthias Ringwald #ifdef USE_PORTAUDIO 66f7c85330SMatthias Ringwald #include <portaudio.h> 678b29cfc6SMatthias Ringwald // portaudio config 688b29cfc6SMatthias Ringwald #define NUM_CHANNELS 1 698b29cfc6SMatthias Ringwald #define SAMPLE_RATE 8000 708b29cfc6SMatthias Ringwald #define FRAMES_PER_BUFFER 24 718b29cfc6SMatthias Ringwald #define PA_SAMPLE_TYPE paInt8 72f7c85330SMatthias Ringwald // portaudio globals 73f7c85330SMatthias Ringwald static PaStream * stream; 74f7c85330SMatthias Ringwald #endif 75f7c85330SMatthias Ringwald 76f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 77f7c85330SMatthias Ringwald // input signal: pre-computed sine wave, 160 Hz 78f7c85330SMatthias Ringwald static const uint8_t sine[] = { 79f7c85330SMatthias Ringwald 0, 15, 31, 46, 61, 74, 86, 97, 107, 114, 80f7c85330SMatthias Ringwald 120, 124, 126, 126, 124, 120, 114, 107, 97, 86, 81f7c85330SMatthias Ringwald 74, 61, 46, 31, 15, 0, 241, 225, 210, 195, 82f7c85330SMatthias Ringwald 182, 170, 159, 149, 142, 136, 132, 130, 130, 132, 83f7c85330SMatthias Ringwald 136, 142, 149, 159, 170, 182, 195, 210, 225, 241, 84f7c85330SMatthias Ringwald }; 85f7c85330SMatthias Ringwald #endif 86f7c85330SMatthias Ringwald 87f7c85330SMatthias Ringwald static int phase; 88f7c85330SMatthias Ringwald 898b29cfc6SMatthias Ringwald #ifdef SCO_WAV_FILENAME 908b29cfc6SMatthias Ringwald 918b29cfc6SMatthias Ringwald static FILE * wav_file; 928b29cfc6SMatthias Ringwald static int num_samples_to_write; 938b29cfc6SMatthias Ringwald 948b29cfc6SMatthias Ringwald 958b29cfc6SMatthias Ringwald static void little_endian_fstore_16(FILE * file, uint16_t value){ 968b29cfc6SMatthias Ringwald uint8_t buf[2]; 978b29cfc6SMatthias Ringwald little_endian_store_32(buf, 0, value); 988b29cfc6SMatthias Ringwald fwrite(&buf, 1, 2, file); 998b29cfc6SMatthias Ringwald } 1008b29cfc6SMatthias Ringwald 1018b29cfc6SMatthias Ringwald static void little_endian_fstore_32(FILE * file, uint32_t value){ 1028b29cfc6SMatthias Ringwald uint8_t buf[4]; 1038b29cfc6SMatthias Ringwald little_endian_store_32(buf, 0, value); 1048b29cfc6SMatthias Ringwald fwrite(&buf, 1, 4, file); 1058b29cfc6SMatthias Ringwald } 1068b29cfc6SMatthias Ringwald 1078b29cfc6SMatthias Ringwald static FILE * wav_init(const char * filename){ 1088b29cfc6SMatthias Ringwald printf("SCO Demo: creating wav file %s\n", filename); 1098b29cfc6SMatthias Ringwald return fopen(filename, "wb"); 1108b29cfc6SMatthias Ringwald } 1118b29cfc6SMatthias Ringwald 1128b29cfc6SMatthias Ringwald static void write_wav_header(FILE * file, int sample_rate, int num_channels, int num_samples, int bytes_per_sample){ 1138b29cfc6SMatthias Ringwald printf("SCO Demo: writing wav header: sample rate %u, num channels %u, duration %u s, bytes per sample %u\n", 1148b29cfc6SMatthias Ringwald sample_rate, num_channels, num_samples / sample_rate / num_channels, bytes_per_sample); 1158b29cfc6SMatthias Ringwald 1168b29cfc6SMatthias Ringwald /* write RIFF header */ 1178b29cfc6SMatthias Ringwald fwrite("RIFF", 1, 4, file); 1188b29cfc6SMatthias Ringwald // num_samples = blocks * subbands 1198b29cfc6SMatthias Ringwald uint32_t data_bytes = (uint32_t) (bytes_per_sample * num_samples * num_channels); 1208b29cfc6SMatthias Ringwald little_endian_fstore_32(file, data_bytes + 36); 1218b29cfc6SMatthias Ringwald fwrite("WAVE", 1, 4, file); 1228b29cfc6SMatthias Ringwald 1238b29cfc6SMatthias Ringwald int byte_rate = sample_rate * num_channels * bytes_per_sample; 1248b29cfc6SMatthias Ringwald int bits_per_sample = 8 * bytes_per_sample; 1258b29cfc6SMatthias Ringwald int block_align = num_channels * bits_per_sample; 1268b29cfc6SMatthias Ringwald int fmt_length = 16; 1278b29cfc6SMatthias Ringwald int fmt_format_tag = 1; // PCM 1288b29cfc6SMatthias Ringwald 1298b29cfc6SMatthias Ringwald /* write fmt chunk */ 1308b29cfc6SMatthias Ringwald fwrite("fmt ", 1, 4, file); 1318b29cfc6SMatthias Ringwald little_endian_fstore_32(file, fmt_length); 1328b29cfc6SMatthias Ringwald little_endian_fstore_16(file, fmt_format_tag); 1338b29cfc6SMatthias Ringwald little_endian_fstore_16(file, num_channels); 1348b29cfc6SMatthias Ringwald little_endian_fstore_32(file, sample_rate); 1358b29cfc6SMatthias Ringwald little_endian_fstore_32(file, byte_rate); 1368b29cfc6SMatthias Ringwald little_endian_fstore_16(file, block_align); 1378b29cfc6SMatthias Ringwald little_endian_fstore_16(file, bits_per_sample); 1388b29cfc6SMatthias Ringwald 1398b29cfc6SMatthias Ringwald /* write data chunk */ 1408b29cfc6SMatthias Ringwald fwrite("data", 1, 4, file); 1418b29cfc6SMatthias Ringwald little_endian_fstore_32(file, data_bytes); 1428b29cfc6SMatthias Ringwald } 1438b29cfc6SMatthias Ringwald 1448b29cfc6SMatthias Ringwald static void write_wav_data_uint8(FILE * file, unsigned long num_samples, uint8_t * data){ 1458b29cfc6SMatthias Ringwald fwrite(data, num_samples, 1, file); 1468b29cfc6SMatthias Ringwald } 1478b29cfc6SMatthias Ringwald 1488b29cfc6SMatthias Ringwald #endif 1498b29cfc6SMatthias Ringwald 1508b29cfc6SMatthias Ringwald 151f7c85330SMatthias Ringwald void sco_demo_init(void){ 152f7c85330SMatthias Ringwald 153f7c85330SMatthias Ringwald // status 154f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 155f7c85330SMatthias Ringwald #ifdef HAVE_PORTAUDIO 156f7c85330SMatthias Ringwald printf("SCO Demo: Sending sine wave, audio output via portaudio.\n"); 157f7c85330SMatthias Ringwald #else 158f7c85330SMatthias Ringwald printf("SCO Demo: Sending sine wave, hexdump received data.\n"); 159f7c85330SMatthias Ringwald #endif 160f7c85330SMatthias Ringwald #endif 161f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_ASCII 162f7c85330SMatthias Ringwald printf("SCO Demo: Sending ASCII blocks, print received data.\n"); 163f7c85330SMatthias Ringwald #endif 164f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_COUNTER 165f7c85330SMatthias Ringwald printf("SCO Demo: Sending counter value, hexdump received data.\n"); 166f7c85330SMatthias Ringwald #endif 167f7c85330SMatthias Ringwald 1688b29cfc6SMatthias Ringwald #ifdef SCO_WAV_FILENAME 1698b29cfc6SMatthias Ringwald wav_file = wav_init(SCO_WAV_FILENAME); 1708b29cfc6SMatthias Ringwald const int sample_rate = 8000; 1718b29cfc6SMatthias Ringwald const int num_samples = sample_rate * SCO_WAV_DURATION_IN_SECONDS; 1728b29cfc6SMatthias Ringwald const int num_channels = 1; 1738b29cfc6SMatthias Ringwald const int bytes_per_sample = 1; 1748b29cfc6SMatthias Ringwald write_wav_header(wav_file, sample_rate, num_channels, num_samples, bytes_per_sample); 1758b29cfc6SMatthias Ringwald num_samples_to_write = num_samples; 1768b29cfc6SMatthias Ringwald #endif 1778b29cfc6SMatthias Ringwald 178f7c85330SMatthias Ringwald #ifdef USE_PORTAUDIO 179f7c85330SMatthias Ringwald int err; 180f7c85330SMatthias Ringwald PaStreamParameters outputParameters; 181f7c85330SMatthias Ringwald 182f7c85330SMatthias Ringwald /* -- initialize PortAudio -- */ 183f7c85330SMatthias Ringwald err = Pa_Initialize(); 184f7c85330SMatthias Ringwald if( err != paNoError ) return; 185f7c85330SMatthias Ringwald /* -- setup input and output -- */ 186f7c85330SMatthias Ringwald outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ 187f7c85330SMatthias Ringwald outputParameters.channelCount = NUM_CHANNELS; 188f7c85330SMatthias Ringwald outputParameters.sampleFormat = PA_SAMPLE_TYPE; 189f7c85330SMatthias Ringwald outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; 190f7c85330SMatthias Ringwald outputParameters.hostApiSpecificStreamInfo = NULL; 191f7c85330SMatthias Ringwald /* -- setup stream -- */ 192f7c85330SMatthias Ringwald err = Pa_OpenStream( 193f7c85330SMatthias Ringwald &stream, 194f7c85330SMatthias Ringwald NULL, // &inputParameters, 195f7c85330SMatthias Ringwald &outputParameters, 196f7c85330SMatthias Ringwald SAMPLE_RATE, 197f7c85330SMatthias Ringwald FRAMES_PER_BUFFER, 198f7c85330SMatthias Ringwald paClipOff, /* we won't output out of range samples so don't bother clipping them */ 199f7c85330SMatthias Ringwald NULL, /* no callback, use blocking API */ 200f7c85330SMatthias Ringwald NULL ); /* no callback, so no callback userData */ 201f7c85330SMatthias Ringwald if( err != paNoError ) return; 202f7c85330SMatthias Ringwald /* -- start stream -- */ 203f7c85330SMatthias Ringwald err = Pa_StartStream( stream ); 204f7c85330SMatthias Ringwald if( err != paNoError ) return; 205f7c85330SMatthias Ringwald #endif 206f7c85330SMatthias Ringwald 207f7c85330SMatthias Ringwald #if SCO_DEMO_MODE != SCO_DEMO_MODE_SINE 208f7c85330SMatthias Ringwald hci_set_sco_voice_setting(0x03); // linear, unsigned, 8-bit, transparent 209f7c85330SMatthias Ringwald #endif 210f7c85330SMatthias Ringwald 211f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_ASCII 212f7c85330SMatthias Ringwald phase = 'a'; 213f7c85330SMatthias Ringwald #endif 214f7c85330SMatthias Ringwald } 215f7c85330SMatthias Ringwald 216f7c85330SMatthias Ringwald 217f7c85330SMatthias Ringwald void sco_demo_send(hci_con_handle_t sco_handle){ 218f7c85330SMatthias Ringwald 219f7c85330SMatthias Ringwald if (!sco_handle) return; 220f7c85330SMatthias Ringwald 221f7c85330SMatthias Ringwald const int sco_packet_length = 24 + 3; // hci_get_sco_packet_length(); 222f7c85330SMatthias Ringwald const int sco_payload_length = sco_packet_length - 3; 223f7c85330SMatthias Ringwald 224f7c85330SMatthias Ringwald hci_reserve_packet_buffer(); 225f7c85330SMatthias Ringwald uint8_t * sco_packet = hci_get_outgoing_packet_buffer(); 226f7c85330SMatthias Ringwald // set handle + flags 227f7c85330SMatthias Ringwald little_endian_store_16(sco_packet, 0, sco_handle); 228f7c85330SMatthias Ringwald // set len 229f7c85330SMatthias Ringwald sco_packet[2] = sco_payload_length; 230f7c85330SMatthias Ringwald const int frames_per_packet = sco_payload_length; // for 8-bit data. for 16-bit data it's /2 231f7c85330SMatthias Ringwald 232f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 233f7c85330SMatthias Ringwald int i; 234f7c85330SMatthias Ringwald for (i=0;i<frames_per_packet;i++){ 235f7c85330SMatthias Ringwald sco_packet[3+i] = sine[phase]; 236f7c85330SMatthias Ringwald phase++; 237f7c85330SMatthias Ringwald if (phase >= sizeof(sine)) phase = 0; 238f7c85330SMatthias Ringwald } 239f7c85330SMatthias Ringwald #else 240f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_ASCII 241f7c85330SMatthias Ringwald memset(&sco_packet[3], phase++, frames_per_packet); 242f7c85330SMatthias Ringwald if (phase > 'z') phase = 'a'; 243f7c85330SMatthias Ringwald #else 244*38b2eaafSMatthias Ringwald int j; 245*38b2eaafSMatthias Ringwald for (j=0;j<frames_per_packet;j++){ 246*38b2eaafSMatthias Ringwald sco_packet[3+j] = phase++; 247f7c85330SMatthias Ringwald } 248f7c85330SMatthias Ringwald #endif 249f7c85330SMatthias Ringwald #endif 250f7c85330SMatthias Ringwald hci_send_sco_packet_buffer(sco_packet_length); 251f7c85330SMatthias Ringwald 252f7c85330SMatthias Ringwald // request another send event 253f7c85330SMatthias Ringwald hci_request_sco_can_send_now_event(); 254f7c85330SMatthias Ringwald 255f7c85330SMatthias Ringwald static int count = 0; 256f7c85330SMatthias Ringwald count++; 257f7c85330SMatthias Ringwald if ((count % SCO_REPORT_PERIOD) == 0) printf("SCO: sent %u\n", count); 258f7c85330SMatthias Ringwald } 259f7c85330SMatthias Ringwald 260f7c85330SMatthias Ringwald /** 261f7c85330SMatthias Ringwald * @brief Process received data 262f7c85330SMatthias Ringwald */ 263f7c85330SMatthias Ringwald void sco_demo_receive(uint8_t * packet, uint16_t size){ 264f7c85330SMatthias Ringwald 2658b29cfc6SMatthias Ringwald 2668b29cfc6SMatthias Ringwald int dump_data = 1; 2678b29cfc6SMatthias Ringwald 2688b29cfc6SMatthias Ringwald #ifdef SCO_WAV_FILENAME 2698b29cfc6SMatthias Ringwald if (num_samples_to_write){ 2708b29cfc6SMatthias Ringwald const int num_samples = size - 3; 2718b29cfc6SMatthias Ringwald const int samples_to_write = btstack_min(num_samples, num_samples_to_write); 2728b29cfc6SMatthias Ringwald // convert 8 bit signed to 8 bit unsigned 2738b29cfc6SMatthias Ringwald int i; 2748b29cfc6SMatthias Ringwald for (i=0;i<samples_to_write;i++){ 2758b29cfc6SMatthias Ringwald packet[3+i] += 128; 2768b29cfc6SMatthias Ringwald } 2778b29cfc6SMatthias Ringwald write_wav_data_uint8(wav_file, samples_to_write, &packet[3]); 2788b29cfc6SMatthias Ringwald num_samples_to_write -= samples_to_write; 2798b29cfc6SMatthias Ringwald if (num_samples_to_write == 0){ 2808b29cfc6SMatthias Ringwald printf("SCO Demo: closing wav file\n"); 2818b29cfc6SMatthias Ringwald fclose(wav_file); 2828b29cfc6SMatthias Ringwald } 2838b29cfc6SMatthias Ringwald dump_data = 0; 2848b29cfc6SMatthias Ringwald } 2858b29cfc6SMatthias Ringwald #endif 2868b29cfc6SMatthias Ringwald 287f7c85330SMatthias Ringwald if (packet[1] & 0xf0){ 288f7c85330SMatthias Ringwald printf("SCO CRC Error: %x - data: ", packet[1] >> 4); 289f7c85330SMatthias Ringwald printf_hexdump(&packet[3], size-3); 290f7c85330SMatthias Ringwald return; 291f7c85330SMatthias Ringwald } 292f7c85330SMatthias Ringwald 293f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 294f7c85330SMatthias Ringwald #ifdef USE_PORTAUDIO 295f7c85330SMatthias Ringwald uint32_t start = btstack_run_loop_get_time_ms(); 296f7c85330SMatthias Ringwald Pa_WriteStream( stream, &packet[3], size -3); 297f7c85330SMatthias Ringwald uint32_t end = btstack_run_loop_get_time_ms(); 298f7c85330SMatthias Ringwald if (end - start > 5){ 299f7c85330SMatthias Ringwald printf("Portaudio: write stream took %u ms\n", end - start); 300f7c85330SMatthias Ringwald } 3018b29cfc6SMatthias Ringwald dump_data = 0; 302f7c85330SMatthias Ringwald #endif 3038b29cfc6SMatthias Ringwald #endif 3048b29cfc6SMatthias Ringwald 3058b29cfc6SMatthias Ringwald if (dump_data){ 306f7c85330SMatthias Ringwald printf("data: "); 307f7c85330SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_ASCII 308f7c85330SMatthias Ringwald int i; 309f7c85330SMatthias Ringwald for (i=3;i<size;i++){ 310f7c85330SMatthias Ringwald printf("%c", packet[i]); 311f7c85330SMatthias Ringwald } 312f7c85330SMatthias Ringwald printf("\n"); 3138b29cfc6SMatthias Ringwald dump_data = 0; 3148b29cfc6SMatthias Ringwald #else 315f7c85330SMatthias Ringwald printf_hexdump(&packet[3], size-3); 316f7c85330SMatthias Ringwald #endif 3178b29cfc6SMatthias Ringwald } 318f7c85330SMatthias Ringwald 319f7c85330SMatthias Ringwald static int count = 0; 320f7c85330SMatthias Ringwald count++; 321f7c85330SMatthias Ringwald if ((count % SCO_REPORT_PERIOD) == 0) printf("SCO: received %u\n", count); 322f7c85330SMatthias Ringwald } 323