xref: /btstack/port/posix-h4/main.c (revision 1ccd94aa2cd93915c95663186a2a7e84d73c6975)
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
238caefee3SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
248caefee3SMatthias Ringwald  * RINGWALD 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 
38ab2c6ae4SMatthias 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 
5416ece135SMatthias Ringwald #include "btstack_debug.h"
55e392c21bSMatthias Ringwald #include "btstack_event.h"
5654b584ffSMatthias Ringwald #include "btstack_link_key_db_fs.h"
5754b584ffSMatthias Ringwald #include "btstack_memory.h"
5882636622SMatthias Ringwald #include "btstack_run_loop.h"
598f2a52f4SMatthias Ringwald #include "btstack_run_loop_posix.h"
603e05a31cSMatthias Ringwald #include "bluetooth_company_id.h"
6154b584ffSMatthias Ringwald #include "hci.h"
6254b584ffSMatthias Ringwald #include "hci_dump.h"
637ea7688aSMatthias Ringwald #include "btstack_stdin.h"
647daa8bd9SMatthias Ringwald #include "btstack_tlv_posix.h"
658caefee3SMatthias Ringwald 
66c0cdcfe7SMatthias Ringwald #include "btstack_chipset_bcm.h"
67c0cdcfe7SMatthias Ringwald #include "btstack_chipset_csr.h"
68c0cdcfe7SMatthias Ringwald #include "btstack_chipset_cc256x.h"
69c0cdcfe7SMatthias Ringwald #include "btstack_chipset_em9301.h"
70c0cdcfe7SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
71c0cdcfe7SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
72c0cdcfe7SMatthias Ringwald 
7364eccac7SMatthias Ringwald int is_bcm;
7464eccac7SMatthias Ringwald 
757daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
767daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
777daa8bd9SMatthias Ringwald static char tlv_db_path[100];
787daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
797daa8bd9SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
807daa8bd9SMatthias Ringwald 
818caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
82755bc585SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
838caefee3SMatthias Ringwald 
849796ebeaSMatthias Ringwald static hci_transport_config_uart_t config = {
859796ebeaSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
868caefee3SMatthias Ringwald     115200,
879796ebeaSMatthias Ringwald     0,  // main baudrate
884eb6a353SMatthias Ringwald     1,  // flow control
899796ebeaSMatthias Ringwald     NULL,
908caefee3SMatthias Ringwald };
918caefee3SMatthias Ringwald 
92d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
93d356a6daSMatthias Ringwald 
94d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
95cf524f2bSMatthias Ringwald     bd_addr_t addr;
96d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
9764eccac7SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
9864eccac7SMatthias Ringwald         case BTSTACK_EVENT_STATE:
9964eccac7SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
100cf524f2bSMatthias Ringwald             gap_local_bd_addr(addr);
101cf524f2bSMatthias Ringwald             printf("BTstack up and running at %s\n",  bd_addr_to_str(addr));
1027daa8bd9SMatthias Ringwald             // setup TLV
1037daa8bd9SMatthias Ringwald             strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
1047daa8bd9SMatthias Ringwald             strcat(tlv_db_path, bd_addr_to_str(addr));
1057daa8bd9SMatthias Ringwald             strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
1067daa8bd9SMatthias Ringwald             tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
1077daa8bd9SMatthias Ringwald             btstack_tlv_set_instance(tlv_impl, &tlv_context);
10864eccac7SMatthias Ringwald             break;
10964eccac7SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
11064eccac7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
11132d5309eSMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
11264eccac7SMatthias Ringwald                 // terminate, name 248 chars
11364eccac7SMatthias Ringwald                 packet[6+248] = 0;
11464eccac7SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
11564eccac7SMatthias Ringwald                 if (is_bcm){
11664eccac7SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
11764eccac7SMatthias Ringwald                 }
11864eccac7SMatthias Ringwald             }
119755bc585SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
120755bc585SMatthias Ringwald                 local_version_information_handler(packet);
121755bc585SMatthias Ringwald             }
12264eccac7SMatthias Ringwald             break;
12364eccac7SMatthias Ringwald         default:
12464eccac7SMatthias Ringwald             break;
12564eccac7SMatthias Ringwald     }
126d356a6daSMatthias Ringwald }
127d356a6daSMatthias Ringwald 
1288caefee3SMatthias Ringwald static void sigint_handler(int param){
129b96c8f44SMatthias Ringwald     UNUSED(param);
1308caefee3SMatthias Ringwald 
131b96c8f44SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
132b96c8f44SMatthias Ringwald     log_info("sigint_handler: shutting down");
133b96c8f44SMatthias Ringwald 
1348caefee3SMatthias Ringwald     // reset anyway
1358caefee3SMatthias Ringwald     btstack_stdin_reset();
1368caefee3SMatthias Ringwald 
137b96c8f44SMatthias Ringwald     // power down
1388caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
1398caefee3SMatthias Ringwald     hci_close();
1408caefee3SMatthias Ringwald     log_info("Good bye, see you.\n");
1418caefee3SMatthias Ringwald     exit(0);
1428caefee3SMatthias Ringwald }
1438caefee3SMatthias Ringwald 
1448caefee3SMatthias Ringwald static int led_state = 0;
1458caefee3SMatthias Ringwald void hal_led_toggle(void){
1468caefee3SMatthias Ringwald     led_state = 1 - led_state;
1478caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
1488caefee3SMatthias Ringwald }
1493250103aSMatthias Ringwald static void use_fast_uart(void){
150615ae444SMatthias Ringwald #if defined(HAVE_POSIX_B240000_MAPPED_TO_3000000) || defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
1513250103aSMatthias Ringwald     printf("Using 3000000 baud.\n");
1523250103aSMatthias Ringwald     config.baudrate_main = 3000000;
153615ae444SMatthias Ringwald #elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000) || defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
1543250103aSMatthias Ringwald     printf("Using 2000000 baud.\n");
1553250103aSMatthias Ringwald     config.baudrate_main = 2000000;
1563250103aSMatthias Ringwald #else
1572c2bf480SMatthias Ringwald     printf("Using 921600 baud.\n");
1582c2bf480SMatthias Ringwald     config.baudrate_main = 921600;
1593250103aSMatthias Ringwald #endif
1602c2bf480SMatthias Ringwald }
1618caefee3SMatthias Ringwald 
162755bc585SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
163c0cdcfe7SMatthias Ringwald     printf("Local version information:\n");
1645c2e0bddSMatthias Ringwald     uint16_t hci_version    = packet[6];
1655c2e0bddSMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
1665c2e0bddSMatthias Ringwald     uint16_t lmp_version    = packet[9];
167f8fbdce0SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
168f8fbdce0SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
169c0cdcfe7SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
170c0cdcfe7SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
171c0cdcfe7SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
172755bc585SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
173c0cdcfe7SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
174c0cdcfe7SMatthias Ringwald     switch (manufacturer){
17561f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
17699dd7845SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
1773250103aSMatthias Ringwald             use_fast_uart();
178c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
179c0cdcfe7SMatthias Ringwald             break;
18061f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
181c0cdcfe7SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
182ed5d91baSMatthias Ringwald             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
183ed5d91baSMatthias Ringwald                 printf("Error: LMP Subversion does not match initscript! ");
184ed5d91baSMatthias Ringwald                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
185ed5d91baSMatthias Ringwald                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
186ed5d91baSMatthias Ringwald                 exit(10);
187ed5d91baSMatthias Ringwald             }
1883250103aSMatthias Ringwald             use_fast_uart();
189c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
190f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
191f6a20ec9SMatthias Ringwald             printf("eHCILL enabled.\n");
1928c768b83SMatthias Ringwald #else
1938c768b83SMatthias Ringwald             printf("eHCILL disable.\n");
1948a23fc53SMatthias Ringwald #endif
195ed5d91baSMatthias Ringwald 
196c0cdcfe7SMatthias Ringwald             break;
19761f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
19864eccac7SMatthias Ringwald             printf("Broadcom - using BCM driver.\n");
19964eccac7SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
200*1ccd94aaSMatthias Ringwald             use_fast_uart();
201*1ccd94aaSMatthias Ringwald             is_bcm = 1;
202*1ccd94aaSMatthias Ringwald             break;
203*1ccd94aaSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR:
204*1ccd94aaSMatthias Ringwald             printf("Cypress - using BCM driver.\n");
205*1ccd94aaSMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
206fc1ce773SMatthias Ringwald             use_fast_uart();
20764eccac7SMatthias Ringwald             is_bcm = 1;
208c0cdcfe7SMatthias Ringwald             break;
20961f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
210c0cdcfe7SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
2113250103aSMatthias Ringwald             use_fast_uart();
212c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
213c0cdcfe7SMatthias Ringwald             break;
2143e05a31cSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
215c0cdcfe7SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
216c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
21707b68b69SMatthias Ringwald             use_fast_uart();
218c0cdcfe7SMatthias Ringwald             break;
21961f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
22064eccac7SMatthias Ringwald             printf("Nordic Semiconductor nRF5 chipset.\n");
22157c1ace8SMatthias Ringwald             break;
222c5541f68SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP:
223c5541f68SMatthias Ringwald             printf("Toshiba - using TC3566x driver.\n");
224c5541f68SMatthias Ringwald             hci_set_chipset(btstack_chipset_tc3566x_instance());
225c5541f68SMatthias Ringwald             use_fast_uart();
226c5541f68SMatthias Ringwald             break;
227c0cdcfe7SMatthias Ringwald         default:
228c0cdcfe7SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
229c0cdcfe7SMatthias Ringwald             break;
230c0cdcfe7SMatthias Ringwald     }
231c0cdcfe7SMatthias Ringwald }
232c0cdcfe7SMatthias Ringwald 
2338caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
2348caefee3SMatthias Ringwald 
2358caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
2368caefee3SMatthias Ringwald 	btstack_memory_init();
237528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2388caefee3SMatthias Ringwald 
2398caefee3SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
240785879c6SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
241785879c6SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
242785879c6SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
2438caefee3SMatthias Ringwald 
2448caefee3SMatthias Ringwald     // pick serial port
245e0e73eb2SMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT
246cf524f2bSMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A50285BI"; // BOOST-CC2564MODA New
247e0e73eb2SMatthias Ringwald     // config.device_name = "/dev/tty.usbserial-A9OVNX5P"; // RedBear IoT pHAT breakout board
2484eb6a353SMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A900K0VK"; // CSR8811 breakout board
2498caefee3SMatthias Ringwald 
250c5ee5b32SMatthias Ringwald     // accept path from command line
251c5ee5b32SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
252c5ee5b32SMatthias Ringwald         config.device_name = argv[2];
253c5ee5b32SMatthias Ringwald         argc -= 2;
254aa981441SMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
255c5ee5b32SMatthias Ringwald     }
256c5ee5b32SMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
257c5ee5b32SMatthias Ringwald 
2588caefee3SMatthias Ringwald     // init HCI
259084ad01cSMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
260084ad01cSMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
26154b584ffSMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
2622d5e09d6SMatthias Ringwald 	hci_init(transport, (void*) &config);
2632d5e09d6SMatthias Ringwald     hci_set_link_key_db(link_key_db);
26477b95b41SMatthias Ringwald 
26577b95b41SMatthias Ringwald     // set BD_ADDR for CSR without Flash/unique address
26677b95b41SMatthias Ringwald     // bd_addr_t own_address = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
26777b95b41SMatthias Ringwald     // btstack_chipset_csr_set_bd_addr(own_address);
2688caefee3SMatthias Ringwald 
269d356a6daSMatthias Ringwald     // inform about BTstack state
270d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
271d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
272d356a6daSMatthias Ringwald 
2738caefee3SMatthias Ringwald     // handle CTRL-c
2748caefee3SMatthias Ringwald     signal(SIGINT, sigint_handler);
2758caefee3SMatthias Ringwald 
2768caefee3SMatthias Ringwald     // setup app
2778caefee3SMatthias Ringwald     btstack_main(argc, argv);
2788caefee3SMatthias Ringwald 
2798caefee3SMatthias Ringwald     // go
280528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
2818caefee3SMatthias Ringwald 
2828caefee3SMatthias Ringwald     return 0;
2838caefee3SMatthias Ringwald }
284