xref: /btstack/test/le_audio/main.c (revision 944898fe9c7c711ea4185a9924f6be9510c17c55)
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"
57*944898feSMatthias Ringwald #include "btstack_chipset_zephyr.h"
5810277393SMatthias Ringwald #include "btstack_debug.h"
5910277393SMatthias Ringwald #include "btstack_event.h"
6010277393SMatthias Ringwald #include "btstack_memory.h"
6110277393SMatthias Ringwald #include "btstack_run_loop.h"
6210277393SMatthias Ringwald #include "btstack_run_loop_posix.h"
6310277393SMatthias Ringwald #include "btstack_signal.h"
6410277393SMatthias Ringwald #include "btstack_stdin.h"
6510277393SMatthias Ringwald #include "btstack_tlv_posix.h"
6610277393SMatthias Ringwald #include "btstack_uart.h"
6710277393SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
6810277393SMatthias Ringwald #include "hci.h"
6910277393SMatthias Ringwald #include "hci_dump.h"
7010277393SMatthias Ringwald #include "hci_dump_posix_fs.h"
7110277393SMatthias Ringwald #include "hci_transport.h"
7210277393SMatthias Ringwald #include "hci_transport_h4.h"
7310277393SMatthias Ringwald 
7410277393SMatthias Ringwald 
7510277393SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
7610277393SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
7710277393SMatthias Ringwald static char tlv_db_path[100];
7810277393SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
7910277393SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
8010277393SMatthias Ringwald static bd_addr_t             local_addr;
8110277393SMatthias Ringwald 
82*944898feSMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
83*944898feSMatthias Ringwald static bd_addr_t static_address;
84*944898feSMatthias Ringwald 
85*944898feSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
86*944898feSMatthias Ringwald 
87*944898feSMatthias Ringwald static bool is_zephyr;
88*944898feSMatthias Ringwald 
8910277393SMatthias Ringwald // shutdown
9010277393SMatthias Ringwald static bool shutdown_triggered;
9110277393SMatthias Ringwald 
9210277393SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
9310277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
9410277393SMatthias Ringwald 
9510277393SMatthias Ringwald static hci_transport_config_uart_t config = {
9610277393SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
97*944898feSMatthias Ringwald     1000000,
9810277393SMatthias Ringwald     0,  // main baudrate
9910277393SMatthias Ringwald     1,  // flow control
10010277393SMatthias Ringwald     NULL,
10110277393SMatthias Ringwald };
10210277393SMatthias Ringwald 
10310277393SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10410277393SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
10510277393SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
10610277393SMatthias Ringwald         case BTSTACK_EVENT_STATE:
10710277393SMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
10810277393SMatthias Ringwald                 case HCI_STATE_WORKING:
109*944898feSMatthias Ringwald                     if (is_zephyr){
110*944898feSMatthias Ringwald                         memcpy(local_addr, static_address, 6);
111*944898feSMatthias Ringwald                     } else {
11210277393SMatthias Ringwald                         gap_local_bd_addr(local_addr);
113*944898feSMatthias Ringwald                     }
11410277393SMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
11510277393SMatthias Ringwald                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
11610277393SMatthias Ringwald                     strcat(tlv_db_path, bd_addr_to_str(local_addr));
11710277393SMatthias Ringwald                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
11810277393SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
11910277393SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
12010277393SMatthias Ringwald #ifdef ENABLE_CLASSIC
12110277393SMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
12210277393SMatthias Ringwald #endif
12310277393SMatthias Ringwald #ifdef ENABLE_BLE
12410277393SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
12510277393SMatthias Ringwald #endif
12610277393SMatthias Ringwald                     break;
12710277393SMatthias Ringwald                 case HCI_STATE_OFF:
12810277393SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
12910277393SMatthias Ringwald                     if (!shutdown_triggered) break;
13010277393SMatthias Ringwald                     // reset stdin
13110277393SMatthias Ringwald                     btstack_stdin_reset();
13210277393SMatthias Ringwald                     log_info("Good bye, see you.\n");
13310277393SMatthias Ringwald                     exit(0);
13410277393SMatthias Ringwald                     break;
13510277393SMatthias Ringwald                 default:
13610277393SMatthias Ringwald                     break;
13710277393SMatthias Ringwald             }
13810277393SMatthias Ringwald             break;
13910277393SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
14010277393SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
14110277393SMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
14210277393SMatthias Ringwald                 // terminate, name 248 chars
14310277393SMatthias Ringwald                 packet[6+248] = 0;
14410277393SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
14510277393SMatthias Ringwald             }
14610277393SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
14710277393SMatthias Ringwald                 local_version_information_handler(packet);
14810277393SMatthias Ringwald             }
149*944898feSMatthias Ringwald             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
150*944898feSMatthias Ringwald                 reverse_48(&packet[7], static_address);
151*944898feSMatthias Ringwald                 gap_random_address_set(static_address);
152*944898feSMatthias Ringwald             }
15310277393SMatthias Ringwald             break;
15410277393SMatthias Ringwald         default:
15510277393SMatthias Ringwald             break;
15610277393SMatthias Ringwald     }
15710277393SMatthias Ringwald }
15810277393SMatthias Ringwald 
15910277393SMatthias Ringwald static void trigger_shutdown(void){
16010277393SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
16110277393SMatthias Ringwald     log_info("sigint_handler: shutting down");
16210277393SMatthias Ringwald     shutdown_triggered = true;
16310277393SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
16410277393SMatthias Ringwald }
16510277393SMatthias Ringwald 
16610277393SMatthias Ringwald static int led_state = 0;
16710277393SMatthias Ringwald void hal_led_toggle(void){
16810277393SMatthias Ringwald     led_state = 1 - led_state;
16910277393SMatthias Ringwald     printf("LED State %u\n", led_state);
17010277393SMatthias Ringwald }
17110277393SMatthias Ringwald 
17210277393SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
17310277393SMatthias Ringwald     printf("Local version information:\n");
17410277393SMatthias Ringwald     uint16_t hci_version    = packet[6];
17510277393SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
17610277393SMatthias Ringwald     uint16_t lmp_version    = packet[9];
17710277393SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
17810277393SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
17910277393SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
18010277393SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
18110277393SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
18210277393SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
18310277393SMatthias Ringwald     printf("- Manufacturer   0x%04x\n", manufacturer);
18410277393SMatthias Ringwald     switch (manufacturer){
18510277393SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
18610277393SMatthias Ringwald             printf("PacketCraft HCI Controller\n");
18710277393SMatthias Ringwald             break;
188*944898feSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
189*944898feSMatthias Ringwald             printf("Zephyr HCI Controller\n");
190*944898feSMatthias Ringwald             is_zephyr = true;
191*944898feSMatthias Ringwald             break;
19210277393SMatthias Ringwald         default:
19310277393SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
19410277393SMatthias Ringwald             break;
19510277393SMatthias Ringwald     }
19610277393SMatthias Ringwald }
19710277393SMatthias Ringwald 
19810277393SMatthias Ringwald int main(int argc, const char * argv[]){
19910277393SMatthias Ringwald 
20010277393SMatthias Ringwald 	/// GET STARTED with BTstack ///
20110277393SMatthias Ringwald 	btstack_memory_init();
20210277393SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
20310277393SMatthias Ringwald 
20410277393SMatthias Ringwald 
20510277393SMatthias Ringwald     // pre-select serial device
20610277393SMatthias Ringwald     config.device_name = "/dev/tty.usbmodemD5D5237DC25B1"; // BL654 with PTS Firmware
20710277393SMatthias Ringwald 
20810277393SMatthias Ringwald     // accept path from command line
20910277393SMatthias Ringwald     bool second_device = false;
21010277393SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
21110277393SMatthias Ringwald         config.device_name = argv[2];
21210277393SMatthias Ringwald         second_device = true;
21310277393SMatthias Ringwald         argc -= 2;
21410277393SMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
21510277393SMatthias Ringwald     }
21610277393SMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
21710277393SMatthias Ringwald 
21810277393SMatthias Ringwald     // log into file using HCI_DUMP_BTSNOOP format
21910277393SMatthias Ringwald     char * pklg_path = "/tmp/hci_dump.btsnoop";
22010277393SMatthias Ringwald     if (second_device){
22110277393SMatthias Ringwald         pklg_path = "/tmp/hci_dump2.btsnoop";
22210277393SMatthias Ringwald     }
22310277393SMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_BTSNOOP);
22410277393SMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
22510277393SMatthias Ringwald     hci_dump_init(hci_dump_impl);
22610277393SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
22710277393SMatthias Ringwald 
22810277393SMatthias Ringwald     // init HCI
22910277393SMatthias Ringwald     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
23010277393SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
23110277393SMatthias Ringwald 	hci_init(transport, (void*) &config);
232*944898feSMatthias Ringwald     hci_set_chipset(btstack_chipset_zephyr_instance());
23310277393SMatthias Ringwald 
23410277393SMatthias Ringwald #ifdef HAVE_PORTAUDIO
23510277393SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
23610277393SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
23710277393SMatthias Ringwald #endif
23810277393SMatthias Ringwald 
23910277393SMatthias Ringwald     // inform about BTstack state
24010277393SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
24110277393SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
24210277393SMatthias Ringwald 
24310277393SMatthias Ringwald     // register callback for CTRL-c
24410277393SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
24510277393SMatthias Ringwald 
24610277393SMatthias Ringwald     // setup app
24710277393SMatthias Ringwald     btstack_main(argc, argv);
24810277393SMatthias Ringwald 
24910277393SMatthias Ringwald     // go
25010277393SMatthias Ringwald     btstack_run_loop_execute();
25110277393SMatthias Ringwald 
25210277393SMatthias Ringwald     return 0;
25310277393SMatthias Ringwald }
254