xref: /btstack/port/posix-h5/main.c (revision 94a5538cc69477d2f91829a81568a7c58aac3c4c)
1*94a5538cSMatthias Ringwald /*
2*94a5538cSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*94a5538cSMatthias Ringwald  *
4*94a5538cSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*94a5538cSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*94a5538cSMatthias Ringwald  * are met:
7*94a5538cSMatthias Ringwald  *
8*94a5538cSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*94a5538cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*94a5538cSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*94a5538cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*94a5538cSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*94a5538cSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*94a5538cSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*94a5538cSMatthias Ringwald  *    from this software without specific prior written permission.
16*94a5538cSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*94a5538cSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*94a5538cSMatthias Ringwald  *    monetary gain.
19*94a5538cSMatthias Ringwald  *
20*94a5538cSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*94a5538cSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*94a5538cSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*94a5538cSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*94a5538cSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*94a5538cSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*94a5538cSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*94a5538cSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*94a5538cSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*94a5538cSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*94a5538cSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*94a5538cSMatthias Ringwald  * SUCH DAMAGE.
32*94a5538cSMatthias Ringwald  *
33*94a5538cSMatthias Ringwald  * Please inquire about commercial licensing options at
34*94a5538cSMatthias Ringwald  * [email protected]
35*94a5538cSMatthias Ringwald  *
36*94a5538cSMatthias Ringwald  */
37*94a5538cSMatthias Ringwald 
38*94a5538cSMatthias Ringwald // *****************************************************************************
39*94a5538cSMatthias Ringwald //
40*94a5538cSMatthias Ringwald // minimal setup for HCI code
41*94a5538cSMatthias Ringwald //
42*94a5538cSMatthias Ringwald // *****************************************************************************
43*94a5538cSMatthias Ringwald 
44*94a5538cSMatthias Ringwald #include <stdint.h>
45*94a5538cSMatthias Ringwald #include <stdio.h>
46*94a5538cSMatthias Ringwald #include <stdlib.h>
47*94a5538cSMatthias Ringwald #include <string.h>
48*94a5538cSMatthias Ringwald #include <signal.h>
49*94a5538cSMatthias Ringwald 
50*94a5538cSMatthias Ringwald #include "btstack_config.h"
51*94a5538cSMatthias Ringwald 
52*94a5538cSMatthias Ringwald #include "btstack_debug.h"
53*94a5538cSMatthias Ringwald #include "btstack_event.h"
54*94a5538cSMatthias Ringwald #include "btstack_link_key_db_fs.h"
55*94a5538cSMatthias Ringwald #include "btstack_memory.h"
56*94a5538cSMatthias Ringwald #include "btstack_run_loop.h"
57*94a5538cSMatthias Ringwald #include "btstack_run_loop_posix.h"
58*94a5538cSMatthias Ringwald #include "hci.h"
59*94a5538cSMatthias Ringwald #include "hci_dump.h"
60*94a5538cSMatthias Ringwald #include "stdin_support.h"
61*94a5538cSMatthias Ringwald 
62*94a5538cSMatthias Ringwald #include "btstack_chipset_bcm.h"
63*94a5538cSMatthias Ringwald #include "btstack_chipset_csr.h"
64*94a5538cSMatthias Ringwald #include "btstack_chipset_cc256x.h"
65*94a5538cSMatthias Ringwald #include "btstack_chipset_em9301.h"
66*94a5538cSMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
67*94a5538cSMatthias Ringwald #include "btstack_chipset_tc3566x.h"
68*94a5538cSMatthias Ringwald 
69*94a5538cSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
70*94a5538cSMatthias Ringwald 
71*94a5538cSMatthias Ringwald static hci_transport_config_uart_t config = {
72*94a5538cSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
73*94a5538cSMatthias Ringwald     115200,
74*94a5538cSMatthias Ringwald     0,  // main baudrate
75*94a5538cSMatthias Ringwald     1,  // flow control
76*94a5538cSMatthias Ringwald     NULL,
77*94a5538cSMatthias Ringwald };
78*94a5538cSMatthias Ringwald 
79*94a5538cSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
80*94a5538cSMatthias Ringwald 
81*94a5538cSMatthias Ringwald static void sigint_handler(int param){
82*94a5538cSMatthias Ringwald 
83*94a5538cSMatthias Ringwald #ifndef _WIN32
84*94a5538cSMatthias Ringwald     // reset anyway
85*94a5538cSMatthias Ringwald     btstack_stdin_reset();
86*94a5538cSMatthias Ringwald #endif
87*94a5538cSMatthias Ringwald 
88*94a5538cSMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
89*94a5538cSMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
90*94a5538cSMatthias Ringwald     hci_close();
91*94a5538cSMatthias Ringwald     log_info("Good bye, see you.\n");
92*94a5538cSMatthias Ringwald     exit(0);
93*94a5538cSMatthias Ringwald }
94*94a5538cSMatthias Ringwald 
95*94a5538cSMatthias Ringwald static int led_state = 0;
96*94a5538cSMatthias Ringwald void hal_led_toggle(void){
97*94a5538cSMatthias Ringwald     led_state = 1 - led_state;
98*94a5538cSMatthias Ringwald     printf("LED State %u\n", led_state);
99*94a5538cSMatthias Ringwald }
100*94a5538cSMatthias Ringwald static void use_fast_uart(void){
101*94a5538cSMatthias Ringwald     // printf("Using 921600 baud.\n");
102*94a5538cSMatthias Ringwald     // config.baudrate_main = 921600;
103*94a5538cSMatthias Ringwald }
104*94a5538cSMatthias Ringwald 
105*94a5538cSMatthias Ringwald static void local_version_information_callback(uint8_t * packet){
106*94a5538cSMatthias Ringwald     printf("Local version information:\n");
107*94a5538cSMatthias Ringwald     uint16_t hci_version    = little_endian_read_16(packet, 4);
108*94a5538cSMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 6);
109*94a5538cSMatthias Ringwald     uint16_t lmp_version    = little_endian_read_16(packet, 8);
110*94a5538cSMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
111*94a5538cSMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
112*94a5538cSMatthias Ringwald     printf("- HCI Version  0x%04x\n", hci_version);
113*94a5538cSMatthias Ringwald     printf("- HCI Revision 0x%04x\n", hci_revision);
114*94a5538cSMatthias Ringwald     printf("- LMP Version  0x%04x\n", lmp_version);
115*94a5538cSMatthias Ringwald     printf("- LMP Revision 0x%04x\n", lmp_subversion);
116*94a5538cSMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
117*94a5538cSMatthias Ringwald     switch (manufacturer){
118*94a5538cSMatthias Ringwald         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
119*94a5538cSMatthias Ringwald             printf("Cambridge Silicon Radio CSR chipset.\n");
120*94a5538cSMatthias Ringwald             use_fast_uart();
121*94a5538cSMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
122*94a5538cSMatthias Ringwald             break;
123*94a5538cSMatthias Ringwald         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
124*94a5538cSMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
125*94a5538cSMatthias Ringwald             use_fast_uart();
126*94a5538cSMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
127*94a5538cSMatthias Ringwald             break;
128*94a5538cSMatthias Ringwald         case COMPANY_ID_BROADCOM_CORPORATION:
129*94a5538cSMatthias Ringwald             printf("Broadcom chipset. Not supported yet\n");
130*94a5538cSMatthias Ringwald             // hci_set_chipset(btstack_chipset_bcm_instance());
131*94a5538cSMatthias Ringwald             break;
132*94a5538cSMatthias Ringwald         case COMPANY_ID_ST_MICROELECTRONICS:
133*94a5538cSMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
134*94a5538cSMatthias Ringwald             use_fast_uart();
135*94a5538cSMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
136*94a5538cSMatthias Ringwald             break;
137*94a5538cSMatthias Ringwald         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
138*94a5538cSMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
139*94a5538cSMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
140*94a5538cSMatthias Ringwald             break;
141*94a5538cSMatthias Ringwald         default:
142*94a5538cSMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
143*94a5538cSMatthias Ringwald             break;
144*94a5538cSMatthias Ringwald     }
145*94a5538cSMatthias Ringwald }
146*94a5538cSMatthias Ringwald 
147*94a5538cSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
148*94a5538cSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
149*94a5538cSMatthias Ringwald     if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
150*94a5538cSMatthias Ringwald     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
151*94a5538cSMatthias Ringwald     printf("BTstack up and running.\n");
152*94a5538cSMatthias Ringwald }
153*94a5538cSMatthias Ringwald 
154*94a5538cSMatthias Ringwald 
155*94a5538cSMatthias Ringwald int main(int argc, const char * argv[]){
156*94a5538cSMatthias Ringwald 
157*94a5538cSMatthias Ringwald 	/// GET STARTED with BTstack ///
158*94a5538cSMatthias Ringwald 	btstack_memory_init();
159*94a5538cSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
160*94a5538cSMatthias Ringwald 
161*94a5538cSMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
162*94a5538cSMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
163*94a5538cSMatthias Ringwald 
164*94a5538cSMatthias Ringwald     // pick serial port
165*94a5538cSMatthias Ringwald     config.device_name = "/dev/tty.usbserial-A900K0VK";
166*94a5538cSMatthias Ringwald 
167*94a5538cSMatthias Ringwald     // init HCI
168*94a5538cSMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
169*94a5538cSMatthias Ringwald     const hci_transport_t * transport = hci_transport_h5_instance(uart_driver);
170*94a5538cSMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
171*94a5538cSMatthias Ringwald 	hci_init(transport, (void*) &config);
172*94a5538cSMatthias Ringwald     hci_set_link_key_db(link_key_db);
173*94a5538cSMatthias Ringwald 
174*94a5538cSMatthias Ringwald     // setup dynamic chipset driver setup
175*94a5538cSMatthias Ringwald     hci_set_local_version_information_callback(&local_version_information_callback);
176*94a5538cSMatthias Ringwald 
177*94a5538cSMatthias Ringwald     // register for HCI events
178*94a5538cSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
179*94a5538cSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
180*94a5538cSMatthias Ringwald 
181*94a5538cSMatthias Ringwald     // handle CTRL-c
182*94a5538cSMatthias Ringwald     signal(SIGINT, sigint_handler);
183*94a5538cSMatthias Ringwald 
184*94a5538cSMatthias Ringwald     // setup app
185*94a5538cSMatthias Ringwald     btstack_main(argc, argv);
186*94a5538cSMatthias Ringwald 
187*94a5538cSMatthias Ringwald     // go
188*94a5538cSMatthias Ringwald     btstack_run_loop_execute();
189*94a5538cSMatthias Ringwald 
190*94a5538cSMatthias Ringwald     return 0;
191*94a5538cSMatthias Ringwald }
192