xref: /btstack/test/le_audio/main.c (revision a9ec7ee053cb5b64c1651a56c2d80606d96c8a9d)
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"
7210277393SMatthias Ringwald 
730fad7dbeSMatthias Ringwald #define HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS 0xFC09
740fad7dbeSMatthias Ringwald const hci_cmd_t hci_zephyr_read_static_address = {
750fad7dbeSMatthias Ringwald         HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS, ""
760fad7dbeSMatthias Ringwald };
7710277393SMatthias Ringwald 
7810277393SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
7910277393SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
8010277393SMatthias Ringwald static char tlv_db_path[100];
8110277393SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
8210277393SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
83944898feSMatthias Ringwald 
84944898feSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
85944898feSMatthias Ringwald 
86944898feSMatthias Ringwald static bool is_zephyr;
870fad7dbeSMatthias Ringwald static bool zephyr_read_static_address;
880fad7dbeSMatthias Ringwald static bd_addr_t zephyr_static_address;
89944898feSMatthias Ringwald 
9010277393SMatthias Ringwald // shutdown
9110277393SMatthias Ringwald static bool shutdown_triggered;
9210277393SMatthias Ringwald 
9310277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
9410277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
9510277393SMatthias Ringwald 
9610277393SMatthias Ringwald static hci_transport_config_uart_t config = {
9710277393SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
98944898feSMatthias Ringwald     1000000,
9910277393SMatthias Ringwald     0,  // main baudrate
10010277393SMatthias Ringwald     1,  // flow control
10110277393SMatthias Ringwald     NULL,
10210277393SMatthias Ringwald };
10310277393SMatthias Ringwald 
1040fad7dbeSMatthias Ringwald static void setup_tlv(bd_addr_t addr){
1050fad7dbeSMatthias Ringwald     printf("BTstack up and running on %s.\n", bd_addr_to_str(addr));
10654736c11SMatthias Ringwald     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
10754736c11SMatthias Ringwald     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(addr));
10854736c11SMatthias Ringwald     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
10910277393SMatthias Ringwald     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
11010277393SMatthias Ringwald     btstack_tlv_set_instance(tlv_impl, &tlv_context);
11110277393SMatthias Ringwald #ifdef ENABLE_CLASSIC
11210277393SMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
11310277393SMatthias Ringwald #endif
11410277393SMatthias Ringwald #ifdef ENABLE_BLE
11510277393SMatthias Ringwald     le_device_db_tlv_configure(tlv_impl, &tlv_context);
11610277393SMatthias Ringwald #endif
1170fad7dbeSMatthias Ringwald }
1180fad7dbeSMatthias Ringwald 
1190fad7dbeSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1200fad7dbeSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1210fad7dbeSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
1220fad7dbeSMatthias Ringwald         case BTSTACK_EVENT_STATE:
1230fad7dbeSMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
1240fad7dbeSMatthias Ringwald                 case HCI_STATE_WORKING:
1250fad7dbeSMatthias Ringwald                     if (is_zephyr){
1260fad7dbeSMatthias Ringwald                         zephyr_read_static_address = true;
1270fad7dbeSMatthias Ringwald                     } else {
1280fad7dbeSMatthias Ringwald                         bd_addr_t local_addr;
1290fad7dbeSMatthias Ringwald                         gap_local_bd_addr(local_addr);
1300fad7dbeSMatthias Ringwald                         setup_tlv(local_addr);
1310fad7dbeSMatthias Ringwald                     }
13210277393SMatthias Ringwald                     break;
13310277393SMatthias Ringwald                 case HCI_STATE_OFF:
13410277393SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
13510277393SMatthias Ringwald                     if (!shutdown_triggered) break;
13610277393SMatthias Ringwald                     // reset stdin
13710277393SMatthias Ringwald                     btstack_stdin_reset();
13810277393SMatthias Ringwald                     log_info("Good bye, see you.\n");
13910277393SMatthias Ringwald                     exit(0);
14010277393SMatthias Ringwald                     break;
14110277393SMatthias Ringwald                 default:
14210277393SMatthias Ringwald                     break;
14310277393SMatthias Ringwald             }
14410277393SMatthias Ringwald             break;
14510277393SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
146*a9ec7ee0SMatthias Ringwald             switch (hci_event_command_complete_get_command_opcode(packet)){
147*a9ec7ee0SMatthias Ringwald                 case HCI_OPCODE_HCI_READ_BD_ADDR:
14810277393SMatthias Ringwald                     if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
14910277393SMatthias Ringwald                     // terminate, name 248 chars
15010277393SMatthias Ringwald                     packet[6+248] = 0;
15110277393SMatthias Ringwald                     printf("Local name: %s\n", &packet[6]);
152*a9ec7ee0SMatthias Ringwald                     break;
153*a9ec7ee0SMatthias Ringwald                 case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
15410277393SMatthias Ringwald                     local_version_information_handler(packet);
155*a9ec7ee0SMatthias Ringwald                     break;
156*a9ec7ee0SMatthias Ringwald                 case HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS:
1570fad7dbeSMatthias Ringwald                     reverse_48(&packet[7], zephyr_static_address);
1580fad7dbeSMatthias Ringwald                     gap_random_address_set(zephyr_static_address);
1590fad7dbeSMatthias Ringwald                     setup_tlv(zephyr_static_address);
160*a9ec7ee0SMatthias Ringwald                     break;
161*a9ec7ee0SMatthias Ringwald                 default:
162*a9ec7ee0SMatthias Ringwald                     break;
163944898feSMatthias Ringwald             }
16410277393SMatthias Ringwald             break;
16510277393SMatthias Ringwald         default:
16610277393SMatthias Ringwald             break;
16710277393SMatthias Ringwald     }
1680fad7dbeSMatthias Ringwald 
1690fad7dbeSMatthias Ringwald     if (zephyr_read_static_address && hci_can_send_command_packet_now()){
1700fad7dbeSMatthias Ringwald         zephyr_read_static_address = false;
1710fad7dbeSMatthias Ringwald         hci_send_cmd(&hci_zephyr_read_static_address);
1720fad7dbeSMatthias Ringwald     }
17310277393SMatthias Ringwald }
17410277393SMatthias Ringwald 
17510277393SMatthias Ringwald static void trigger_shutdown(void){
17610277393SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
17710277393SMatthias Ringwald     log_info("sigint_handler: shutting down");
17810277393SMatthias Ringwald     shutdown_triggered = true;
17910277393SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
18010277393SMatthias Ringwald }
18110277393SMatthias Ringwald 
18210277393SMatthias Ringwald static int led_state = 0;
18310277393SMatthias Ringwald void hal_led_toggle(void){
18410277393SMatthias Ringwald     led_state = 1 - led_state;
18510277393SMatthias Ringwald     printf("LED State %u\n", led_state);
18610277393SMatthias Ringwald }
18710277393SMatthias Ringwald 
18810277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
18910277393SMatthias Ringwald     printf("Local version information:\n");
19010277393SMatthias Ringwald     uint16_t hci_version    = packet[6];
19110277393SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
19210277393SMatthias Ringwald     uint16_t lmp_version    = packet[9];
19310277393SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
19410277393SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
19510277393SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
19610277393SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
19710277393SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
19810277393SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
19910277393SMatthias Ringwald     printf("- Manufacturer   0x%04x\n", manufacturer);
20010277393SMatthias Ringwald     switch (manufacturer){
20110277393SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
20210277393SMatthias Ringwald             printf("PacketCraft HCI Controller\n");
20310277393SMatthias Ringwald             break;
204944898feSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
205944898feSMatthias Ringwald             printf("Zephyr HCI Controller\n");
206944898feSMatthias Ringwald             is_zephyr = true;
207944898feSMatthias Ringwald             break;
20810277393SMatthias Ringwald         default:
20910277393SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
21010277393SMatthias Ringwald             break;
21110277393SMatthias Ringwald     }
21210277393SMatthias Ringwald }
21310277393SMatthias Ringwald 
21410277393SMatthias Ringwald int main(int argc, const char * argv[]){
21510277393SMatthias Ringwald 
21610277393SMatthias Ringwald 	/// GET STARTED with BTstack ///
21710277393SMatthias Ringwald 	btstack_memory_init();
21810277393SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
21910277393SMatthias Ringwald 
22010277393SMatthias Ringwald 
22110277393SMatthias Ringwald     // pre-select serial device
22210277393SMatthias Ringwald     config.device_name = "/dev/tty.usbmodemD5D5237DC25B1"; // BL654 with PTS Firmware
22310277393SMatthias Ringwald 
22410277393SMatthias Ringwald     // accept path from command line
22510277393SMatthias Ringwald     bool second_device = false;
22610277393SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
22710277393SMatthias Ringwald         config.device_name = argv[2];
22810277393SMatthias Ringwald         second_device = true;
22910277393SMatthias Ringwald         argc -= 2;
2300033ffe4SMatthias Ringwald         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
23110277393SMatthias Ringwald     }
23210277393SMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
23310277393SMatthias Ringwald 
23410277393SMatthias Ringwald     // log into file using HCI_DUMP_BTSNOOP format
23510277393SMatthias Ringwald     char * pklg_path = "/tmp/hci_dump.btsnoop";
23610277393SMatthias Ringwald     if (second_device){
23710277393SMatthias Ringwald         pklg_path = "/tmp/hci_dump2.btsnoop";
23810277393SMatthias Ringwald     }
23910277393SMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_BTSNOOP);
24010277393SMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
24110277393SMatthias Ringwald     hci_dump_init(hci_dump_impl);
24210277393SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
24310277393SMatthias Ringwald 
24410277393SMatthias Ringwald     // init HCI
24510277393SMatthias Ringwald     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
24610277393SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
24710277393SMatthias Ringwald 	hci_init(transport, (void*) &config);
24810277393SMatthias Ringwald 
24910277393SMatthias Ringwald #ifdef HAVE_PORTAUDIO
25010277393SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
25110277393SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
25210277393SMatthias Ringwald #endif
25310277393SMatthias Ringwald 
25410277393SMatthias Ringwald     // inform about BTstack state
25510277393SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
25610277393SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
25710277393SMatthias Ringwald 
25810277393SMatthias Ringwald     // register callback for CTRL-c
25910277393SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
26010277393SMatthias Ringwald 
26110277393SMatthias Ringwald     // setup app
26210277393SMatthias Ringwald     btstack_main(argc, argv);
26310277393SMatthias Ringwald 
26410277393SMatthias Ringwald     // go
26510277393SMatthias Ringwald     btstack_run_loop_execute();
26610277393SMatthias Ringwald 
26710277393SMatthias Ringwald     return 0;
26810277393SMatthias Ringwald }
269