xref: /btstack/port/posix-h4/main.c (revision fc1ce7736c1d407097ef6aeafd21ce258b930445)
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 
388caefee3SMatthias Ringwald // *****************************************************************************
398caefee3SMatthias Ringwald //
408caefee3SMatthias Ringwald // minimal setup for HCI code
418caefee3SMatthias Ringwald //
428caefee3SMatthias Ringwald // *****************************************************************************
438caefee3SMatthias Ringwald 
448caefee3SMatthias Ringwald #include <stdint.h>
458caefee3SMatthias Ringwald #include <stdio.h>
468caefee3SMatthias Ringwald #include <stdlib.h>
478caefee3SMatthias Ringwald #include <string.h>
488caefee3SMatthias Ringwald #include <signal.h>
498caefee3SMatthias Ringwald 
507907f069SMatthias Ringwald #include "btstack_config.h"
518caefee3SMatthias Ringwald 
5216ece135SMatthias Ringwald #include "btstack_debug.h"
53e392c21bSMatthias Ringwald #include "btstack_event.h"
5454b584ffSMatthias Ringwald #include "btstack_link_key_db_fs.h"
5554b584ffSMatthias Ringwald #include "btstack_memory.h"
5682636622SMatthias Ringwald #include "btstack_run_loop.h"
578f2a52f4SMatthias Ringwald #include "btstack_run_loop_posix.h"
5854b584ffSMatthias Ringwald #include "hci.h"
5954b584ffSMatthias Ringwald #include "hci_dump.h"
608caefee3SMatthias Ringwald #include "stdin_support.h"
618caefee3SMatthias Ringwald 
62c0cdcfe7SMatthias Ringwald #include "btstack_chipset_bcm.h"
63c0cdcfe7SMatthias Ringwald #include "btstack_chipset_csr.h"
64c0cdcfe7SMatthias Ringwald #include "btstack_chipset_cc256x.h"
65c0cdcfe7SMatthias Ringwald #include "btstack_chipset_em9301.h"
66c0cdcfe7SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
67c0cdcfe7SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
68c0cdcfe7SMatthias Ringwald 
6964eccac7SMatthias Ringwald int is_bcm;
7064eccac7SMatthias Ringwald 
718caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
728caefee3SMatthias Ringwald 
739796ebeaSMatthias Ringwald static hci_transport_config_uart_t config = {
749796ebeaSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
758caefee3SMatthias Ringwald     115200,
769796ebeaSMatthias Ringwald     0,  // main baudrate
779796ebeaSMatthias Ringwald     1,  // flow control
789796ebeaSMatthias Ringwald     NULL,
798caefee3SMatthias Ringwald };
808caefee3SMatthias Ringwald 
81d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
82d356a6daSMatthias Ringwald 
83d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
84d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
8564eccac7SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
8664eccac7SMatthias Ringwald         case BTSTACK_EVENT_STATE:
8764eccac7SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
88d356a6daSMatthias Ringwald             printf("BTstack up and running.\n");
8964eccac7SMatthias Ringwald             break;
9064eccac7SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
9164eccac7SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
9264eccac7SMatthias Ringwald                 // terminate, name 248 chars
9364eccac7SMatthias Ringwald                 packet[6+248] = 0;
9464eccac7SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
9564eccac7SMatthias Ringwald                 if (is_bcm){
9664eccac7SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
9764eccac7SMatthias Ringwald                 }
9864eccac7SMatthias Ringwald             }
9964eccac7SMatthias Ringwald             break;
10064eccac7SMatthias Ringwald         default:
10164eccac7SMatthias Ringwald             break;
10264eccac7SMatthias Ringwald     }
103d356a6daSMatthias Ringwald }
104d356a6daSMatthias Ringwald 
1058caefee3SMatthias Ringwald static void sigint_handler(int param){
1068caefee3SMatthias Ringwald 
1078caefee3SMatthias Ringwald #ifndef _WIN32
1088caefee3SMatthias Ringwald     // reset anyway
1098caefee3SMatthias Ringwald     btstack_stdin_reset();
1108caefee3SMatthias Ringwald #endif
1118caefee3SMatthias Ringwald 
1128caefee3SMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
1138caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
1148caefee3SMatthias Ringwald     hci_close();
1158caefee3SMatthias Ringwald     log_info("Good bye, see you.\n");
1168caefee3SMatthias Ringwald     exit(0);
1178caefee3SMatthias Ringwald }
1188caefee3SMatthias Ringwald 
1198caefee3SMatthias Ringwald static int led_state = 0;
1208caefee3SMatthias Ringwald void hal_led_toggle(void){
1218caefee3SMatthias Ringwald     led_state = 1 - led_state;
1228caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
1238caefee3SMatthias Ringwald }
1243250103aSMatthias Ringwald static void use_fast_uart(void){
125615ae444SMatthias Ringwald #if defined(HAVE_POSIX_B240000_MAPPED_TO_3000000) || defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
1263250103aSMatthias Ringwald     printf("Using 3000000 baud.\n");
1273250103aSMatthias Ringwald     config.baudrate_main = 3000000;
128615ae444SMatthias Ringwald #elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000) || defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
1293250103aSMatthias Ringwald     printf("Using 2000000 baud.\n");
1303250103aSMatthias Ringwald     config.baudrate_main = 2000000;
1313250103aSMatthias Ringwald #else
1322c2bf480SMatthias Ringwald     printf("Using 921600 baud.\n");
1332c2bf480SMatthias Ringwald     config.baudrate_main = 921600;
1343250103aSMatthias Ringwald #endif
1352c2bf480SMatthias Ringwald }
1368caefee3SMatthias Ringwald 
137a6ddbcb9SMatthias Ringwald static void local_version_information_callback(uint8_t * packet){
138c0cdcfe7SMatthias Ringwald     printf("Local version information:\n");
139f8fbdce0SMatthias Ringwald     uint16_t hci_version    = little_endian_read_16(packet, 4);
140f8fbdce0SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 6);
141f8fbdce0SMatthias Ringwald     uint16_t lmp_version    = little_endian_read_16(packet, 8);
142f8fbdce0SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
143f8fbdce0SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
144c0cdcfe7SMatthias Ringwald     printf("- HCI Version  0x%04x\n", hci_version);
145c0cdcfe7SMatthias Ringwald     printf("- HCI Revision 0x%04x\n", hci_revision);
146c0cdcfe7SMatthias Ringwald     printf("- LMP Version  0x%04x\n", lmp_version);
147c0cdcfe7SMatthias Ringwald     printf("- LMP Revision 0x%04x\n", lmp_subversion);
148c0cdcfe7SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
149c0cdcfe7SMatthias Ringwald     switch (manufacturer){
150c0cdcfe7SMatthias Ringwald         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
15164eccac7SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset.\n");
1523250103aSMatthias Ringwald             use_fast_uart();
153c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
154c0cdcfe7SMatthias Ringwald             break;
155c0cdcfe7SMatthias Ringwald         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
156c0cdcfe7SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
1573250103aSMatthias Ringwald             use_fast_uart();
158c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
159f6a20ec9SMatthias Ringwald #ifdef ENABLE_EHCILL
160f6a20ec9SMatthias Ringwald             printf("eHCILL enabled.\n");
1618c768b83SMatthias Ringwald #else
1628c768b83SMatthias Ringwald             printf("eHCILL disable.\n");
1638a23fc53SMatthias Ringwald #endif
164c0cdcfe7SMatthias Ringwald             break;
165c0cdcfe7SMatthias Ringwald         case COMPANY_ID_BROADCOM_CORPORATION:
16664eccac7SMatthias Ringwald             printf("Broadcom - using BCM driver.\n");
16764eccac7SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
168*fc1ce773SMatthias Ringwald 
169*fc1ce773SMatthias Ringwald             use_fast_uart();
17064eccac7SMatthias Ringwald             is_bcm = 1;
171c0cdcfe7SMatthias Ringwald             break;
172c0cdcfe7SMatthias Ringwald         case COMPANY_ID_ST_MICROELECTRONICS:
173c0cdcfe7SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
1743250103aSMatthias Ringwald             use_fast_uart();
175c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
176c0cdcfe7SMatthias Ringwald             break;
177c0cdcfe7SMatthias Ringwald         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
178c0cdcfe7SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
179c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
180c0cdcfe7SMatthias Ringwald             break;
18157c1ace8SMatthias Ringwald         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
18264eccac7SMatthias Ringwald             printf("Nordic Semiconductor nRF5 chipset.\n");
18357c1ace8SMatthias Ringwald             break;
184c0cdcfe7SMatthias Ringwald         default:
185c0cdcfe7SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
186c0cdcfe7SMatthias Ringwald             break;
187c0cdcfe7SMatthias Ringwald     }
188c0cdcfe7SMatthias Ringwald }
189c0cdcfe7SMatthias Ringwald 
1908caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
1918caefee3SMatthias Ringwald 
1928caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
1938caefee3SMatthias Ringwald 	btstack_memory_init();
194528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
1958caefee3SMatthias Ringwald 
1968caefee3SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
1978caefee3SMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
1988caefee3SMatthias Ringwald 
1998caefee3SMatthias Ringwald     // pick serial port
200*fc1ce773SMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A9OVNX5P";
2018caefee3SMatthias Ringwald 
2028caefee3SMatthias Ringwald     // init HCI
203084ad01cSMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
204084ad01cSMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
20554b584ffSMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
2062d5e09d6SMatthias Ringwald 	hci_init(transport, (void*) &config);
2072d5e09d6SMatthias Ringwald     hci_set_link_key_db(link_key_db);
2088caefee3SMatthias Ringwald 
209d356a6daSMatthias Ringwald     // inform about BTstack state
210d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
211d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
212d356a6daSMatthias Ringwald 
213a6ddbcb9SMatthias Ringwald     // setup dynamic chipset driver setup
214a6ddbcb9SMatthias Ringwald     hci_set_local_version_information_callback(&local_version_information_callback);
215c0cdcfe7SMatthias Ringwald 
2168caefee3SMatthias Ringwald     // handle CTRL-c
2178caefee3SMatthias Ringwald     signal(SIGINT, sigint_handler);
2188caefee3SMatthias Ringwald 
2198caefee3SMatthias Ringwald     // setup app
2208caefee3SMatthias Ringwald     btstack_main(argc, argv);
2218caefee3SMatthias Ringwald 
2228caefee3SMatthias Ringwald     // go
223528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
2248caefee3SMatthias Ringwald 
2258caefee3SMatthias Ringwald     return 0;
2268caefee3SMatthias Ringwald }
227