xref: /btstack/port/posix-h4/main.c (revision a6ddbcb91c0a0832fd25509d300a30dd97e46749)
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"
5354b584ffSMatthias Ringwald #include "btstack_link_key_db_fs.h"
5454b584ffSMatthias Ringwald #include "btstack_memory.h"
5582636622SMatthias Ringwald #include "btstack_run_loop.h"
568f2a52f4SMatthias Ringwald #include "btstack_run_loop_posix.h"
5754b584ffSMatthias Ringwald #include "hci.h"
5854b584ffSMatthias Ringwald #include "hci_dump.h"
598caefee3SMatthias Ringwald #include "stdin_support.h"
608caefee3SMatthias Ringwald 
61c0cdcfe7SMatthias Ringwald #include "btstack_chipset_bcm.h"
62c0cdcfe7SMatthias Ringwald #include "btstack_chipset_csr.h"
63c0cdcfe7SMatthias Ringwald #include "btstack_chipset_cc256x.h"
64c0cdcfe7SMatthias Ringwald #include "btstack_chipset_em9301.h"
65c0cdcfe7SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
66c0cdcfe7SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
67c0cdcfe7SMatthias Ringwald 
688caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
698caefee3SMatthias Ringwald 
709796ebeaSMatthias Ringwald static hci_transport_config_uart_t config = {
719796ebeaSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
728caefee3SMatthias Ringwald     115200,
739796ebeaSMatthias Ringwald     0,  // main baudrate
749796ebeaSMatthias Ringwald     1,  // flow control
759796ebeaSMatthias Ringwald     NULL,
768caefee3SMatthias Ringwald };
778caefee3SMatthias Ringwald 
788caefee3SMatthias Ringwald static void sigint_handler(int param){
798caefee3SMatthias Ringwald 
808caefee3SMatthias Ringwald #ifndef _WIN32
818caefee3SMatthias Ringwald     // reset anyway
828caefee3SMatthias Ringwald     btstack_stdin_reset();
838caefee3SMatthias Ringwald #endif
848caefee3SMatthias Ringwald 
858caefee3SMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
868caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
878caefee3SMatthias Ringwald     hci_close();
888caefee3SMatthias Ringwald     log_info("Good bye, see you.\n");
898caefee3SMatthias Ringwald     exit(0);
908caefee3SMatthias Ringwald }
918caefee3SMatthias Ringwald 
928caefee3SMatthias Ringwald static int led_state = 0;
938caefee3SMatthias Ringwald void hal_led_toggle(void){
948caefee3SMatthias Ringwald     led_state = 1 - led_state;
958caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
968caefee3SMatthias Ringwald }
972c2bf480SMatthias Ringwald static void using_921600_baud(void){
982c2bf480SMatthias Ringwald     printf("Using 921600 baud.\n");
992c2bf480SMatthias Ringwald     config.baudrate_main = 921600;
1002c2bf480SMatthias Ringwald }
1018caefee3SMatthias Ringwald 
102*a6ddbcb9SMatthias Ringwald static void local_version_information_callback(uint8_t * packet){
103c0cdcfe7SMatthias Ringwald     printf("Local version information:\n");
104f8fbdce0SMatthias Ringwald     uint16_t hci_version    = little_endian_read_16(packet, 4);
105f8fbdce0SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 6);
106f8fbdce0SMatthias Ringwald     uint16_t lmp_version    = little_endian_read_16(packet, 8);
107f8fbdce0SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
108f8fbdce0SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
109c0cdcfe7SMatthias Ringwald     printf("- HCI Version  0x%04x\n", hci_version);
110c0cdcfe7SMatthias Ringwald     printf("- HCI Revision 0x%04x\n", hci_revision);
111c0cdcfe7SMatthias Ringwald     printf("- LMP Version  0x%04x\n", lmp_version);
112c0cdcfe7SMatthias Ringwald     printf("- LMP Revision 0x%04x\n", lmp_subversion);
113c0cdcfe7SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
114c0cdcfe7SMatthias Ringwald     switch (manufacturer){
115c0cdcfe7SMatthias Ringwald         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
116c0cdcfe7SMatthias Ringwald             printf("Cambridge Silicon Radio CSR chipset.\n");
1172c2bf480SMatthias Ringwald             using_921600_baud();
118c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
119c0cdcfe7SMatthias Ringwald             break;
120c0cdcfe7SMatthias Ringwald         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
121c0cdcfe7SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
1222c2bf480SMatthias Ringwald             using_921600_baud();
123c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
124c0cdcfe7SMatthias Ringwald             break;
125c0cdcfe7SMatthias Ringwald         case COMPANY_ID_BROADCOM_CORPORATION:
126c0cdcfe7SMatthias Ringwald             printf("Broadcom chipset. Not supported yet\n");
127c0cdcfe7SMatthias Ringwald             // hci_set_chipset(btstack_chipset_bcm_instance());
128c0cdcfe7SMatthias Ringwald             break;
129c0cdcfe7SMatthias Ringwald         case COMPANY_ID_ST_MICROELECTRONICS:
130c0cdcfe7SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
1312c2bf480SMatthias Ringwald             using_921600_baud();
132c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
133c0cdcfe7SMatthias Ringwald             break;
134c0cdcfe7SMatthias Ringwald         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
135c0cdcfe7SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
136c0cdcfe7SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
137c0cdcfe7SMatthias Ringwald             break;
138c0cdcfe7SMatthias Ringwald         default:
139c0cdcfe7SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
140c0cdcfe7SMatthias Ringwald             break;
141c0cdcfe7SMatthias Ringwald     }
142c0cdcfe7SMatthias Ringwald }
143c0cdcfe7SMatthias Ringwald 
1448caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
1458caefee3SMatthias Ringwald 
1468caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
1478caefee3SMatthias Ringwald 	btstack_memory_init();
148528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
1498caefee3SMatthias Ringwald 
1508caefee3SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
1518caefee3SMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
1528caefee3SMatthias Ringwald 
1538caefee3SMatthias Ringwald     // pick serial port
154c0cdcfe7SMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A900K0VK";
1558caefee3SMatthias Ringwald 
1568caefee3SMatthias Ringwald     // init HCI
15724b3c629SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance();
15854b584ffSMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
1592d5e09d6SMatthias Ringwald 	hci_init(transport, (void*) &config);
1602d5e09d6SMatthias Ringwald     hci_set_link_key_db(link_key_db);
1618caefee3SMatthias Ringwald 
162*a6ddbcb9SMatthias Ringwald     // setup dynamic chipset driver setup
163*a6ddbcb9SMatthias Ringwald     hci_set_local_version_information_callback(&local_version_information_callback);
164c0cdcfe7SMatthias Ringwald 
1658caefee3SMatthias Ringwald     // handle CTRL-c
1668caefee3SMatthias Ringwald     signal(SIGINT, sigint_handler);
1678caefee3SMatthias Ringwald 
1688caefee3SMatthias Ringwald     // setup app
1698caefee3SMatthias Ringwald     btstack_main(argc, argv);
1708caefee3SMatthias Ringwald 
1718caefee3SMatthias Ringwald     // go
172528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
1738caefee3SMatthias Ringwald 
1748caefee3SMatthias Ringwald     return 0;
1758caefee3SMatthias Ringwald }
176