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