xref: /btstack/port/posix-h4-nxp/main.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
1 /*
2  * Copyright (C) 2023 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 BLUEKITCHEN
24  * GMBH 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 #include <unistd.h>
52 
53 #include "btstack_config.h"
54 
55 #include "ble/le_device_db_tlv.h"
56 #include "btstack_chipset_nxp.h"
57 #include "btstack_debug.h"
58 #include "btstack_event.h"
59 #include "btstack_memory.h"
60 #include "btstack_run_loop_posix.h"
61 #include "btstack_signal.h"
62 #include "btstack_stdin.h"
63 #include "btstack_tlv_posix.h"
64 #include "btstack_uart.h"
65 #include "classic/btstack_link_key_db_tlv.h"
66 #include "hci.h"
67 #include "hci_dump.h"
68 #include "hci_dump_posix_fs.h"
69 #include "hci_transport_h4.h"
70 
71 
72 #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
73 #define TLV_DB_PATH_POSTFIX ".tlv"
74 static char tlv_db_path[100];
75 static const btstack_tlv_t * tlv_impl;
76 static btstack_tlv_posix_t   tlv_context;
77 static bd_addr_t             local_addr;
78 
79 static int main_argc;
80 static const char ** main_argv;
81 
82 static const btstack_uart_t * uart_driver;
83 static btstack_uart_config_t uart_config;
84 
85 // shutdown
86 static bool shutdown_triggered;
87 
88 int btstack_main(int argc, const char * argv[]);
89 static void local_version_information_handler(uint8_t * packet);
90 
91 static hci_transport_config_uart_t transport_config = {
92     HCI_TRANSPORT_CONFIG_UART,
93     115200,
94     0,  // main baudrate
95     1,  // flow control
96     NULL,
97 };
98 
99 static btstack_packet_callback_registration_t hci_event_callback_registration;
100 
101 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
102     if (packet_type != HCI_EVENT_PACKET) return;
103     switch (hci_event_packet_get_type(packet)){
104         case BTSTACK_EVENT_STATE:
105             switch(btstack_event_state_get_state(packet)){
106                 case HCI_STATE_WORKING:
107                     gap_local_bd_addr(local_addr);
108                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
109                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
110                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
111                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
112                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
113                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
114 #ifdef ENABLE_CLASSIC
115                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
116 #endif
117 #ifdef ENABLE_BLE
118                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
119 #endif
120                     break;
121                 case HCI_STATE_OFF:
122                     btstack_tlv_posix_deinit(&tlv_context);
123                     if (!shutdown_triggered) break;
124                     // reset stdin
125                     btstack_stdin_reset();
126                     log_info("Good bye, see you.\n");
127                     exit(0);
128                     break;
129                 default:
130                     break;
131             }
132             break;
133         default:
134             break;
135     }
136 }
137 
138 static void trigger_shutdown(void){
139     printf("CTRL-C - SIGINT received, shutting down..\n");
140     log_info("sigint_handler: shutting down");
141     shutdown_triggered = true;
142     hci_power_control(HCI_POWER_OFF);
143 }
144 
145 static int led_state = 0;
146 void hal_led_toggle(void){
147     led_state = 1 - led_state;
148     printf("LED State %u\n", led_state);
149 }
150 
151 static void nxp_phase2(uint8_t status){
152 
153     if (status){
154         printf("Download firmware failed\n");
155         return;
156     }
157 
158     printf("Phase 2: Main app\n");
159 
160     // assert last ack was sent at original speed
161     usleep(100000);
162 
163     // re-configure UART: enable flow control, set initial baudrate
164     transport_config.baudrate_init = btstack_chipset_nxp_get_initial_baudrate();
165     uart_driver->set_flowcontrol(transport_config.flowcontrol);
166 
167     // init HCI
168     const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
169     hci_init(transport, (void*) &transport_config);
170     hci_set_chipset(btstack_chipset_nxp_instance());
171 
172     // inform about BTstack state
173     hci_event_callback_registration.callback = &packet_handler;
174     hci_add_event_handler(&hci_event_callback_registration);
175 
176     // register callback for CTRL-c
177     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
178 
179     // setup app
180     btstack_main(main_argc, main_argv);
181 }
182 
183 int main(int argc, const char * argv[]){
184 
185 	/// GET STARTED with BTstack ///
186 	btstack_memory_init();
187     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
188 
189     // log into file using HCI_DUMP_PACKETLOGGER format
190     const char * pklg_path = "/tmp/hci_dump.pklg";
191     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
192     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
193     hci_dump_init(hci_dump_impl);
194     printf("Packet Log: %s\n", pklg_path);
195 
196     // pick serial port
197     transport_config.device_name = "/dev/tty.usbserial-A506WORJ"; // DVK-ST60-2230C / 88W8997
198     // transport_config.device_name = "/dev/tty.usbserial-FT1XBGIM";  // murata m.2 adapter
199 
200     // accept path from command line
201     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
202         transport_config.device_name = argv[2];
203         argc -= 2;
204         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
205     }
206     printf("H4 device: %s\n", transport_config.device_name);
207 
208     uart_driver = btstack_uart_posix_instance();
209 
210     // extract UART config from transport config
211     uart_config.baudrate    = transport_config.baudrate_init;
212     uart_config.flowcontrol = 0;
213     uart_config.device_name = transport_config.device_name;
214     uart_driver->init(&uart_config);
215 
216     main_argc = argc;
217     main_argv = argv;
218 
219     const char * firmware_v1 = "uartuart8997_bt_v4.bin";
220     printf("Phase 1: Download firmware\n");
221     btstack_chipset_nxp_set_v1_firmware_path(firmware_v1);
222     btstack_chipset_nxp_download_firmware_with_uart(uart_driver, &nxp_phase2);
223 
224     // go
225     btstack_run_loop_execute();
226     return 0;
227 }
228