xref: /btstack/test/le_audio/main.c (revision 0c608997094b86b83d8b170fab92c8a0e7512b06)
110277393SMatthias Ringwald /*
210277393SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
310277393SMatthias Ringwald  *
410277393SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
510277393SMatthias Ringwald  * modification, are permitted provided that the following conditions
610277393SMatthias Ringwald  * are met:
710277393SMatthias Ringwald  *
810277393SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
910277393SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1010277393SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1110277393SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1210277393SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1310277393SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1410277393SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1510277393SMatthias Ringwald  *    from this software without specific prior written permission.
1610277393SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1710277393SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1810277393SMatthias Ringwald  *    monetary gain.
1910277393SMatthias Ringwald  *
2010277393SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2110277393SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2210277393SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2310277393SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
2410277393SMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2510277393SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2610277393SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2710277393SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2810277393SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2910277393SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3010277393SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3110277393SMatthias Ringwald  * SUCH DAMAGE.
3210277393SMatthias Ringwald  *
3310277393SMatthias Ringwald  * Please inquire about commercial licensing options at
3410277393SMatthias Ringwald  * [email protected]
3510277393SMatthias Ringwald  *
3610277393SMatthias Ringwald  */
3710277393SMatthias Ringwald 
3810277393SMatthias Ringwald #define BTSTACK_FILE__ "main.c"
3910277393SMatthias Ringwald 
4010277393SMatthias Ringwald // *****************************************************************************
4110277393SMatthias Ringwald //
4210277393SMatthias Ringwald // minimal setup for HCI code
4310277393SMatthias Ringwald //
4410277393SMatthias Ringwald // *****************************************************************************
4510277393SMatthias Ringwald 
4610277393SMatthias Ringwald #include <stdint.h>
4710277393SMatthias Ringwald #include <stdio.h>
4810277393SMatthias Ringwald #include <stdlib.h>
4910277393SMatthias Ringwald #include <string.h>
5010277393SMatthias Ringwald #include <signal.h>
5110277393SMatthias Ringwald 
5210277393SMatthias Ringwald #include "btstack_config.h"
5310277393SMatthias Ringwald 
5410277393SMatthias Ringwald #include "ble/le_device_db_tlv.h"
5510277393SMatthias Ringwald #include "bluetooth_company_id.h"
5610277393SMatthias Ringwald #include "btstack_audio.h"
5710277393SMatthias Ringwald #include "btstack_debug.h"
5810277393SMatthias Ringwald #include "btstack_event.h"
5910277393SMatthias Ringwald #include "btstack_memory.h"
6010277393SMatthias Ringwald #include "btstack_run_loop.h"
6110277393SMatthias Ringwald #include "btstack_run_loop_posix.h"
6210277393SMatthias Ringwald #include "btstack_signal.h"
6310277393SMatthias Ringwald #include "btstack_stdin.h"
6410277393SMatthias Ringwald #include "btstack_tlv_posix.h"
6510277393SMatthias Ringwald #include "btstack_uart.h"
6610277393SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
6710277393SMatthias Ringwald #include "hci.h"
6810277393SMatthias Ringwald #include "hci_dump.h"
6910277393SMatthias Ringwald #include "hci_dump_posix_fs.h"
7010277393SMatthias Ringwald #include "hci_transport.h"
7110277393SMatthias Ringwald #include "hci_transport_h4.h"
72*0c608997SMatthias Ringwald #include "btstack_chipset_bcm.h"
7310277393SMatthias Ringwald 
740fad7dbeSMatthias Ringwald #define HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS 0xFC09
750fad7dbeSMatthias Ringwald const hci_cmd_t hci_zephyr_read_static_address = {
760fad7dbeSMatthias Ringwald         HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS, ""
770fad7dbeSMatthias Ringwald };
7810277393SMatthias Ringwald 
7910277393SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
8010277393SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
8110277393SMatthias Ringwald static char tlv_db_path[100];
8210277393SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
8310277393SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
84944898feSMatthias Ringwald 
85944898feSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
86944898feSMatthias Ringwald 
87944898feSMatthias Ringwald static bool is_zephyr;
880fad7dbeSMatthias Ringwald static bool zephyr_read_static_address;
890fad7dbeSMatthias Ringwald static bd_addr_t zephyr_static_address;
90944898feSMatthias Ringwald 
9110277393SMatthias Ringwald // shutdown
9210277393SMatthias Ringwald static bool shutdown_triggered;
9310277393SMatthias Ringwald 
9410277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
9510277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
9610277393SMatthias Ringwald 
9710277393SMatthias Ringwald static hci_transport_config_uart_t config = {
9810277393SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
99*0c608997SMatthias Ringwald     115200,
10010277393SMatthias Ringwald     0,  // main baudrate
10110277393SMatthias Ringwald     1,  // flow control
10210277393SMatthias Ringwald     NULL,
10310277393SMatthias Ringwald };
10410277393SMatthias Ringwald 
1050fad7dbeSMatthias Ringwald static void setup_tlv(bd_addr_t addr){
1060fad7dbeSMatthias Ringwald     printf("BTstack up and running on %s.\n", bd_addr_to_str(addr));
10754736c11SMatthias Ringwald     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
10854736c11SMatthias Ringwald     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(addr));
10954736c11SMatthias Ringwald     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
11010277393SMatthias Ringwald     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
11110277393SMatthias Ringwald     btstack_tlv_set_instance(tlv_impl, &tlv_context);
11210277393SMatthias Ringwald #ifdef ENABLE_CLASSIC
11310277393SMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
11410277393SMatthias Ringwald #endif
11510277393SMatthias Ringwald #ifdef ENABLE_BLE
11610277393SMatthias Ringwald     le_device_db_tlv_configure(tlv_impl, &tlv_context);
11710277393SMatthias Ringwald #endif
1180fad7dbeSMatthias Ringwald }
1190fad7dbeSMatthias Ringwald 
1200fad7dbeSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1210fad7dbeSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1220fad7dbeSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
1230fad7dbeSMatthias Ringwald         case BTSTACK_EVENT_STATE:
1240fad7dbeSMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
1250fad7dbeSMatthias Ringwald                 case HCI_STATE_WORKING:
1260fad7dbeSMatthias Ringwald                     if (is_zephyr){
1270fad7dbeSMatthias Ringwald                         zephyr_read_static_address = true;
1280fad7dbeSMatthias Ringwald                     } else {
1290fad7dbeSMatthias Ringwald                         bd_addr_t local_addr;
1300fad7dbeSMatthias Ringwald                         gap_local_bd_addr(local_addr);
1310fad7dbeSMatthias Ringwald                         setup_tlv(local_addr);
1320fad7dbeSMatthias Ringwald                     }
13310277393SMatthias Ringwald                     break;
13410277393SMatthias Ringwald                 case HCI_STATE_OFF:
13510277393SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
13610277393SMatthias Ringwald                     if (!shutdown_triggered) break;
13710277393SMatthias Ringwald                     // reset stdin
13810277393SMatthias Ringwald                     btstack_stdin_reset();
13910277393SMatthias Ringwald                     log_info("Good bye, see you.\n");
14010277393SMatthias Ringwald                     exit(0);
14110277393SMatthias Ringwald                     break;
14210277393SMatthias Ringwald                 default:
14310277393SMatthias Ringwald                     break;
14410277393SMatthias Ringwald             }
14510277393SMatthias Ringwald             break;
14610277393SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
147a9ec7ee0SMatthias Ringwald             switch (hci_event_command_complete_get_command_opcode(packet)){
148a9ec7ee0SMatthias Ringwald                 case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
14910277393SMatthias Ringwald                     local_version_information_handler(packet);
150a9ec7ee0SMatthias Ringwald                     break;
151a9ec7ee0SMatthias Ringwald                 case HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS:
1520fad7dbeSMatthias Ringwald                     reverse_48(&packet[7], zephyr_static_address);
1530fad7dbeSMatthias Ringwald                     gap_random_address_set(zephyr_static_address);
1540fad7dbeSMatthias Ringwald                     setup_tlv(zephyr_static_address);
155a9ec7ee0SMatthias Ringwald                     break;
156a9ec7ee0SMatthias Ringwald                 default:
157a9ec7ee0SMatthias Ringwald                     break;
158944898feSMatthias Ringwald             }
15910277393SMatthias Ringwald             break;
16010277393SMatthias Ringwald         default:
16110277393SMatthias Ringwald             break;
16210277393SMatthias Ringwald     }
1630fad7dbeSMatthias Ringwald 
1640fad7dbeSMatthias Ringwald     if (zephyr_read_static_address && hci_can_send_command_packet_now()){
1650fad7dbeSMatthias Ringwald         zephyr_read_static_address = false;
1660fad7dbeSMatthias Ringwald         hci_send_cmd(&hci_zephyr_read_static_address);
1670fad7dbeSMatthias Ringwald     }
16810277393SMatthias Ringwald }
16910277393SMatthias Ringwald 
17010277393SMatthias Ringwald static void trigger_shutdown(void){
17110277393SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
17210277393SMatthias Ringwald     log_info("sigint_handler: shutting down");
17310277393SMatthias Ringwald     shutdown_triggered = true;
17410277393SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
17510277393SMatthias Ringwald }
17610277393SMatthias Ringwald 
17710277393SMatthias Ringwald static int led_state = 0;
17810277393SMatthias Ringwald void hal_led_toggle(void){
17910277393SMatthias Ringwald     led_state = 1 - led_state;
18010277393SMatthias Ringwald     printf("LED State %u\n", led_state);
18110277393SMatthias Ringwald }
18210277393SMatthias Ringwald 
18310277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
18410277393SMatthias Ringwald     printf("Local version information:\n");
18510277393SMatthias Ringwald     uint16_t hci_version    = packet[6];
18610277393SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
18710277393SMatthias Ringwald     uint16_t lmp_version    = packet[9];
18810277393SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
18910277393SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
19010277393SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
19110277393SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
19210277393SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
19310277393SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
19410277393SMatthias Ringwald     printf("- Manufacturer   0x%04x\n", manufacturer);
19510277393SMatthias Ringwald     switch (manufacturer){
19610277393SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
19710277393SMatthias Ringwald             printf("PacketCraft HCI Controller\n");
19810277393SMatthias Ringwald             break;
199944898feSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
200944898feSMatthias Ringwald             printf("Zephyr HCI Controller\n");
201944898feSMatthias Ringwald             is_zephyr = true;
202944898feSMatthias Ringwald             break;
203*0c608997SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG:
204*0c608997SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
205*0c608997SMatthias Ringwald             printf("Broadcom/Cypress/Infineon Controller\n");
206*0c608997SMatthias Ringwald             config.baudrate_main = 921600;
207*0c608997SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
208*0c608997SMatthias Ringwald             break;
20910277393SMatthias Ringwald         default:
21010277393SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
21110277393SMatthias Ringwald             break;
21210277393SMatthias Ringwald     }
21310277393SMatthias Ringwald }
21410277393SMatthias Ringwald 
21510277393SMatthias Ringwald int main(int argc, const char * argv[]){
21610277393SMatthias Ringwald 
21710277393SMatthias Ringwald 	/// GET STARTED with BTstack ///
21810277393SMatthias Ringwald 	btstack_memory_init();
21910277393SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
22010277393SMatthias Ringwald 
22110277393SMatthias Ringwald 
22210277393SMatthias Ringwald     // pre-select serial device
223433614beSMatthias Ringwald     config.device_name = "/dev/tty.usbmodemEA7EB9D612C31"; // BL654 with PTS Firmware
22410277393SMatthias Ringwald 
22510277393SMatthias Ringwald     // accept path from command line
22610277393SMatthias Ringwald     bool second_device = false;
22710277393SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
22810277393SMatthias Ringwald         config.device_name = argv[2];
22910277393SMatthias Ringwald         second_device = true;
23010277393SMatthias Ringwald         argc -= 2;
2310033ffe4SMatthias Ringwald         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
23210277393SMatthias Ringwald     }
23310277393SMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
23410277393SMatthias Ringwald 
23510277393SMatthias Ringwald     // log into file using HCI_DUMP_BTSNOOP format
23610277393SMatthias Ringwald     char * pklg_path = "/tmp/hci_dump.btsnoop";
237433614beSMatthias Ringwald     if (strcmp(config.device_name, "/dev/tty.usbmodemEF437DF524C51") == 0){
238433614beSMatthias Ringwald         pklg_path = "/tmp/hci_dump_source.btsnoop";
239433614beSMatthias Ringwald     }
240433614beSMatthias Ringwald     if (strcmp(config.device_name, "/dev/tty.usbmodemE6589B44933B1") == 0){
241433614beSMatthias Ringwald         pklg_path = "/tmp/hci_dump_sink.btsnoop";
24210277393SMatthias Ringwald     }
24310277393SMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_BTSNOOP);
24410277393SMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
24510277393SMatthias Ringwald     hci_dump_init(hci_dump_impl);
24610277393SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
24710277393SMatthias Ringwald 
24810277393SMatthias Ringwald     // init HCI
24910277393SMatthias Ringwald     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
25010277393SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
25110277393SMatthias Ringwald 	hci_init(transport, (void*) &config);
25210277393SMatthias Ringwald 
25310277393SMatthias Ringwald #ifdef HAVE_PORTAUDIO
25410277393SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
25510277393SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
25610277393SMatthias Ringwald #endif
25710277393SMatthias Ringwald 
25810277393SMatthias Ringwald     // inform about BTstack state
25910277393SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
26010277393SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
26110277393SMatthias Ringwald 
26210277393SMatthias Ringwald     // register callback for CTRL-c
26310277393SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
26410277393SMatthias Ringwald 
26510277393SMatthias Ringwald     // setup app
26610277393SMatthias Ringwald     btstack_main(argc, argv);
26710277393SMatthias Ringwald 
26810277393SMatthias Ringwald     // go
26910277393SMatthias Ringwald     btstack_run_loop_execute();
27010277393SMatthias Ringwald 
27110277393SMatthias Ringwald     return 0;
27210277393SMatthias Ringwald }
273