xref: /btstack/port/posix-h4/main.c (revision 2f50bc44f0df43a75ff85446b9d0b229b5aff025)
18caefee3SMatthias Ringwald /*
28caefee3SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
38caefee3SMatthias Ringwald  *
48caefee3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
58caefee3SMatthias Ringwald  * modification, are permitted provided that the following conditions
68caefee3SMatthias Ringwald  * are met:
78caefee3SMatthias Ringwald  *
88caefee3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
98caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
108caefee3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
118caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
128caefee3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
138caefee3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
148caefee3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
158caefee3SMatthias Ringwald  *    from this software without specific prior written permission.
168caefee3SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
178caefee3SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
188caefee3SMatthias Ringwald  *    monetary gain.
198caefee3SMatthias Ringwald  *
208caefee3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
218caefee3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
228caefee3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
258caefee3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
268caefee3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
278caefee3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
288caefee3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
298caefee3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
308caefee3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318caefee3SMatthias Ringwald  * SUCH DAMAGE.
328caefee3SMatthias Ringwald  *
338caefee3SMatthias Ringwald  * Please inquire about commercial licensing options at
348caefee3SMatthias Ringwald  * [email protected]
358caefee3SMatthias Ringwald  *
368caefee3SMatthias Ringwald  */
378caefee3SMatthias Ringwald 
38f11fd9a9SMatthias Ringwald #define BTSTACK_FILE__ "main.c"
39ab2c6ae4SMatthias Ringwald 
408caefee3SMatthias Ringwald // *****************************************************************************
418caefee3SMatthias Ringwald //
428caefee3SMatthias Ringwald // minimal setup for HCI code
438caefee3SMatthias Ringwald //
448caefee3SMatthias Ringwald // *****************************************************************************
458caefee3SMatthias Ringwald 
468caefee3SMatthias Ringwald #include <stdint.h>
478caefee3SMatthias Ringwald #include <stdio.h>
488caefee3SMatthias Ringwald #include <stdlib.h>
498caefee3SMatthias Ringwald #include <string.h>
508caefee3SMatthias Ringwald #include <signal.h>
518caefee3SMatthias Ringwald 
527907f069SMatthias Ringwald #include "btstack_config.h"
538caefee3SMatthias Ringwald 
54f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h"
55f11fd9a9SMatthias Ringwald #include "bluetooth_company_id.h"
5671e722a7SMatthias Ringwald #include "btstack_audio.h"
57f11fd9a9SMatthias Ringwald #include "btstack_chipset_bcm.h"
58f11fd9a9SMatthias Ringwald #include "btstack_chipset_cc256x.h"
59f11fd9a9SMatthias Ringwald #include "btstack_chipset_csr.h"
60f11fd9a9SMatthias Ringwald #include "btstack_chipset_em9301.h"
61f11fd9a9SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
62f11fd9a9SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
6316ece135SMatthias Ringwald #include "btstack_debug.h"
64e392c21bSMatthias Ringwald #include "btstack_event.h"
6554b584ffSMatthias Ringwald #include "btstack_memory.h"
6682636622SMatthias Ringwald #include "btstack_run_loop.h"
678f2a52f4SMatthias Ringwald #include "btstack_run_loop_posix.h"
68f11fd9a9SMatthias Ringwald #include "btstack_signal.h"
69f11fd9a9SMatthias Ringwald #include "btstack_stdin.h"
70f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h"
71793a0509SMatthias Ringwald #include "btstack_uart.h"
72f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
7354b584ffSMatthias Ringwald #include "hci.h"
7454b584ffSMatthias Ringwald #include "hci_dump.h"
757435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h"
76c8dfe071SMatthias Ringwald #include "hci_transport.h"
77c8dfe071SMatthias Ringwald #include "hci_transport_h4.h"
78c0cdcfe7SMatthias Ringwald 
7964eccac7SMatthias Ringwald 
807daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
817daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
827daa8bd9SMatthias Ringwald static char tlv_db_path[100];
837daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
847daa8bd9SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
85f11fd9a9SMatthias Ringwald static bd_addr_t             local_addr;
867daa8bd9SMatthias Ringwald 
87793a0509SMatthias Ringwald static int is_bcm;
88f11fd9a9SMatthias Ringwald // shutdown
89f11fd9a9SMatthias Ringwald static bool shutdown_triggered;
90793a0509SMatthias Ringwald 
918caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
92755bc585SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
938caefee3SMatthias Ringwald 
949796ebeaSMatthias Ringwald static hci_transport_config_uart_t config = {
959796ebeaSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
968caefee3SMatthias Ringwald     115200,
979796ebeaSMatthias Ringwald     0,  // main baudrate
984eb6a353SMatthias Ringwald     1,  // flow control
999796ebeaSMatthias Ringwald     NULL,
1008caefee3SMatthias Ringwald };
1018caefee3SMatthias Ringwald 
102d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
103d356a6daSMatthias Ringwald 
104d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
105d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
10664eccac7SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
10764eccac7SMatthias Ringwald         case BTSTACK_EVENT_STATE:
108f11fd9a9SMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
109f11fd9a9SMatthias Ringwald                 case HCI_STATE_WORKING:
110f11fd9a9SMatthias Ringwald                     gap_local_bd_addr(local_addr);
111f11fd9a9SMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
1127daa8bd9SMatthias Ringwald                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
11315d21758SMatthias Ringwald                     strcat(tlv_db_path, bd_addr_to_str_with_delimiter(local_addr, '-'));
1147daa8bd9SMatthias Ringwald                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
1157daa8bd9SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
1167daa8bd9SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
11781862996SMatthias Ringwald #ifdef ENABLE_CLASSIC
11881862996SMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
11981862996SMatthias Ringwald #endif
12081862996SMatthias Ringwald #ifdef ENABLE_BLE
12181862996SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
12281862996SMatthias Ringwald #endif
12364eccac7SMatthias Ringwald                     break;
124f11fd9a9SMatthias Ringwald                 case HCI_STATE_OFF:
125f11fd9a9SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
126f11fd9a9SMatthias Ringwald                     if (!shutdown_triggered) break;
127f11fd9a9SMatthias Ringwald                     // reset stdin
128f11fd9a9SMatthias Ringwald                     btstack_stdin_reset();
129f11fd9a9SMatthias Ringwald                     log_info("Good bye, see you.\n");
130f11fd9a9SMatthias Ringwald                     exit(0);
131f11fd9a9SMatthias Ringwald                     break;
132f11fd9a9SMatthias Ringwald                 default:
133f11fd9a9SMatthias Ringwald                     break;
134f11fd9a9SMatthias Ringwald             }
135f11fd9a9SMatthias Ringwald             break;
13664eccac7SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
13764eccac7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
13832d5309eSMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
13964eccac7SMatthias Ringwald                 // terminate, name 248 chars
14064eccac7SMatthias Ringwald                 packet[6+248] = 0;
14164eccac7SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
14264eccac7SMatthias Ringwald                 if (is_bcm){
14364eccac7SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
14464eccac7SMatthias Ringwald                 }
14564eccac7SMatthias Ringwald             }
146755bc585SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
147755bc585SMatthias Ringwald                 local_version_information_handler(packet);
148755bc585SMatthias Ringwald             }
14964eccac7SMatthias Ringwald             break;
15064eccac7SMatthias Ringwald         default:
15164eccac7SMatthias Ringwald             break;
15264eccac7SMatthias Ringwald     }
153d356a6daSMatthias Ringwald }
154d356a6daSMatthias Ringwald 
155f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){
156b96c8f44SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
157b96c8f44SMatthias Ringwald     log_info("sigint_handler: shutting down");
158f11fd9a9SMatthias Ringwald     shutdown_triggered = true;
1598caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
1608caefee3SMatthias Ringwald }
1618caefee3SMatthias Ringwald 
1628caefee3SMatthias Ringwald static int led_state = 0;
1638caefee3SMatthias Ringwald void hal_led_toggle(void){
1648caefee3SMatthias Ringwald     led_state = 1 - led_state;
1658caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
1668caefee3SMatthias Ringwald }
1673250103aSMatthias Ringwald static void use_fast_uart(void){
1682c2bf480SMatthias Ringwald     printf("Using 921600 baud.\n");
1692c2bf480SMatthias Ringwald     config.baudrate_main = 921600;
1702c2bf480SMatthias Ringwald }
1718caefee3SMatthias Ringwald 
172755bc585SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
173c0cdcfe7SMatthias Ringwald     printf("Local version information:\n");
1745c2e0bddSMatthias Ringwald     uint16_t hci_version    = packet[6];
1755c2e0bddSMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
1765c2e0bddSMatthias Ringwald     uint16_t lmp_version    = packet[9];
177f8fbdce0SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
178f8fbdce0SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
179c0cdcfe7SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
180c0cdcfe7SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
181c0cdcfe7SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
182755bc585SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
183c0cdcfe7SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
184c0cdcfe7SMatthias Ringwald     switch (manufacturer){
18561f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
18699dd7845SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
1873250103aSMatthias Ringwald             use_fast_uart();
188c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
189c0cdcfe7SMatthias Ringwald             break;
19061f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
191c0cdcfe7SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
192ed5d91baSMatthias Ringwald             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
193ed5d91baSMatthias Ringwald                 printf("Error: LMP Subversion does not match initscript! ");
194ed5d91baSMatthias Ringwald                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
195ed5d91baSMatthias Ringwald                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
196ed5d91baSMatthias Ringwald                 exit(10);
197ed5d91baSMatthias Ringwald             }
1983250103aSMatthias Ringwald             use_fast_uart();
199c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
200f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
201f6a20ec9SMatthias Ringwald             printf("eHCILL enabled.\n");
2028c768b83SMatthias Ringwald #else
2038c768b83SMatthias Ringwald             printf("eHCILL disable.\n");
2048a23fc53SMatthias Ringwald #endif
205ed5d91baSMatthias Ringwald 
206c0cdcfe7SMatthias Ringwald             break;
20761f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
208c21d89f3SMatthias Ringwald             printf("Broadcom/Cypress - using BCM driver.\n");
2091ccd94aaSMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
210fc1ce773SMatthias Ringwald             use_fast_uart();
21164eccac7SMatthias Ringwald             is_bcm = 1;
212c0cdcfe7SMatthias Ringwald             break;
21361f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
214c0cdcfe7SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
2153250103aSMatthias Ringwald             use_fast_uart();
216c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
217c0cdcfe7SMatthias Ringwald             break;
2183e05a31cSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
219c0cdcfe7SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
220c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
22107b68b69SMatthias Ringwald             use_fast_uart();
222c0cdcfe7SMatthias Ringwald             break;
22361f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
22464eccac7SMatthias Ringwald             printf("Nordic Semiconductor nRF5 chipset.\n");
22557c1ace8SMatthias Ringwald             break;
226c5541f68SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP:
227c5541f68SMatthias Ringwald             printf("Toshiba - using TC3566x driver.\n");
228c5541f68SMatthias Ringwald             hci_set_chipset(btstack_chipset_tc3566x_instance());
229c5541f68SMatthias Ringwald             use_fast_uart();
230c5541f68SMatthias Ringwald             break;
231*2f50bc44SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
232*2f50bc44SMatthias Ringwald             printf("PacketCraft HCI Controller\n");
233*2f50bc44SMatthias Ringwald             break;
234c0cdcfe7SMatthias Ringwald         default:
235c0cdcfe7SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
236c0cdcfe7SMatthias Ringwald             break;
237c0cdcfe7SMatthias Ringwald     }
238c0cdcfe7SMatthias Ringwald }
239c0cdcfe7SMatthias Ringwald 
2408caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
2418caefee3SMatthias Ringwald 
2428caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
2438caefee3SMatthias Ringwald 	btstack_memory_init();
244528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2458caefee3SMatthias Ringwald 
2467435ec7bSMatthias Ringwald     // log into file using HCI_DUMP_PACKETLOGGER format
247785879c6SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
2487435ec7bSMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
2497435ec7bSMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
2507435ec7bSMatthias Ringwald     hci_dump_init(hci_dump_impl);
251785879c6SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
2528caefee3SMatthias Ringwald 
2538caefee3SMatthias Ringwald     // pick serial port
254e0e73eb2SMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT
255cf524f2bSMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A50285BI"; // BOOST-CC2564MODA New
256e0e73eb2SMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A9OVNX5P"; // RedBear IoT pHAT breakout board
2574eb6a353SMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A900K0VK"; // CSR8811 breakout board
2588caefee3SMatthias Ringwald 
259c5ee5b32SMatthias Ringwald     // accept path from command line
260c5ee5b32SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
261c5ee5b32SMatthias Ringwald         config.device_name = argv[2];
262c5ee5b32SMatthias Ringwald         argc -= 2;
263aa981441SMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
264c5ee5b32SMatthias Ringwald     }
265c5ee5b32SMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
266c5ee5b32SMatthias Ringwald 
2678caefee3SMatthias Ringwald     // init HCI
268793a0509SMatthias Ringwald     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
269793a0509SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
2702d5e09d6SMatthias Ringwald 	hci_init(transport, (void*) &config);
27177b95b41SMatthias Ringwald 
27271e722a7SMatthias Ringwald #ifdef HAVE_PORTAUDIO
27371e722a7SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
27471e722a7SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
27571e722a7SMatthias Ringwald #endif
27671e722a7SMatthias Ringwald 
27777b95b41SMatthias Ringwald     // set BD_ADDR for CSR without Flash/unique address
27877b95b41SMatthias Ringwald     // bd_addr_t own_address = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
27977b95b41SMatthias Ringwald     // btstack_chipset_csr_set_bd_addr(own_address);
2808caefee3SMatthias Ringwald 
281d356a6daSMatthias Ringwald     // inform about BTstack state
282d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
283d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
284d356a6daSMatthias Ringwald 
285f11fd9a9SMatthias Ringwald     // register callback for CTRL-c
286f11fd9a9SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
2878caefee3SMatthias Ringwald 
2888caefee3SMatthias Ringwald     // setup app
2898caefee3SMatthias Ringwald     btstack_main(argc, argv);
2908caefee3SMatthias Ringwald 
2918caefee3SMatthias Ringwald     // go
292528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
2938caefee3SMatthias Ringwald 
2948caefee3SMatthias Ringwald     return 0;
2958caefee3SMatthias Ringwald }
296