xref: /btstack/port/posix-h4-nxp/main.c (revision 1c00f28917faf559d5dec77b7541b2427ed13d3c)
1cc528b9dSMatthias Ringwald /*
2cc528b9dSMatthias Ringwald  * Copyright (C) 2023 BlueKitchen GmbH
3cc528b9dSMatthias Ringwald  *
4cc528b9dSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5cc528b9dSMatthias Ringwald  * modification, are permitted provided that the following conditions
6cc528b9dSMatthias Ringwald  * are met:
7cc528b9dSMatthias Ringwald  *
8cc528b9dSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9cc528b9dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10cc528b9dSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11cc528b9dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12cc528b9dSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13cc528b9dSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14cc528b9dSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15cc528b9dSMatthias Ringwald  *    from this software without specific prior written permission.
16cc528b9dSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17cc528b9dSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18cc528b9dSMatthias Ringwald  *    monetary gain.
19cc528b9dSMatthias Ringwald  *
20cc528b9dSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21cc528b9dSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cc528b9dSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cc528b9dSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24cc528b9dSMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25cc528b9dSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26cc528b9dSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27cc528b9dSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28cc528b9dSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29cc528b9dSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30cc528b9dSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31cc528b9dSMatthias Ringwald  * SUCH DAMAGE.
32cc528b9dSMatthias Ringwald  *
33cc528b9dSMatthias Ringwald  * Please inquire about commercial licensing options at
34cc528b9dSMatthias Ringwald  * [email protected]
35cc528b9dSMatthias Ringwald  *
36cc528b9dSMatthias Ringwald  */
37cc528b9dSMatthias Ringwald 
38cc528b9dSMatthias Ringwald #define BTSTACK_FILE__ "main.c"
39cc528b9dSMatthias Ringwald 
40cc528b9dSMatthias Ringwald // *****************************************************************************
41cc528b9dSMatthias Ringwald //
42cc528b9dSMatthias Ringwald // minimal setup for HCI code
43cc528b9dSMatthias Ringwald //
44cc528b9dSMatthias Ringwald // *****************************************************************************
45cc528b9dSMatthias Ringwald 
46cc528b9dSMatthias Ringwald #include <stdint.h>
47cc528b9dSMatthias Ringwald #include <stdio.h>
48cc528b9dSMatthias Ringwald #include <stdlib.h>
49cc528b9dSMatthias Ringwald #include <string.h>
50cc528b9dSMatthias Ringwald #include <signal.h>
5120a60f96SMatthias Ringwald #include <unistd.h>
52cc528b9dSMatthias Ringwald 
53cc528b9dSMatthias Ringwald #include "btstack_config.h"
54cc528b9dSMatthias Ringwald 
55cc528b9dSMatthias Ringwald #include "ble/le_device_db_tlv.h"
56cc528b9dSMatthias Ringwald #include "btstack_chipset_nxp.h"
57*1c00f289SMatthias Ringwald #include "btstack_audio.h"
58cc528b9dSMatthias Ringwald #include "btstack_debug.h"
59cc528b9dSMatthias Ringwald #include "btstack_event.h"
60cc528b9dSMatthias Ringwald #include "btstack_memory.h"
61cc528b9dSMatthias Ringwald #include "btstack_run_loop_posix.h"
62cc528b9dSMatthias Ringwald #include "btstack_signal.h"
63cc528b9dSMatthias Ringwald #include "btstack_stdin.h"
64cc528b9dSMatthias Ringwald #include "btstack_tlv_posix.h"
65cc528b9dSMatthias Ringwald #include "btstack_uart.h"
66cc528b9dSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
67cc528b9dSMatthias Ringwald #include "hci.h"
68cc528b9dSMatthias Ringwald #include "hci_dump.h"
69cc528b9dSMatthias Ringwald #include "hci_dump_posix_fs.h"
70cc528b9dSMatthias Ringwald #include "hci_transport_h4.h"
71cc528b9dSMatthias Ringwald 
72cc528b9dSMatthias Ringwald 
73cc528b9dSMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
74cc528b9dSMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
75cc528b9dSMatthias Ringwald static char tlv_db_path[100];
76cc528b9dSMatthias Ringwald static const btstack_tlv_t * tlv_impl;
77cc528b9dSMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
78cc528b9dSMatthias Ringwald static bd_addr_t             local_addr;
79cc528b9dSMatthias Ringwald 
80cc528b9dSMatthias Ringwald static int main_argc;
81cc528b9dSMatthias Ringwald static const char ** main_argv;
82cc528b9dSMatthias Ringwald 
83cc528b9dSMatthias Ringwald static const btstack_uart_t * uart_driver;
84cc528b9dSMatthias Ringwald static btstack_uart_config_t uart_config;
85cc528b9dSMatthias Ringwald 
86cc528b9dSMatthias Ringwald // shutdown
87cc528b9dSMatthias Ringwald static bool shutdown_triggered;
88cc528b9dSMatthias Ringwald 
89cc528b9dSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
90cc528b9dSMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
91cc528b9dSMatthias Ringwald 
92cc528b9dSMatthias Ringwald static hci_transport_config_uart_t transport_config = {
93cc528b9dSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
94cc528b9dSMatthias Ringwald     115200,
95cc528b9dSMatthias Ringwald     0,  // main baudrate
962364ad0cSMatthias Ringwald     1,  // flow control
97cc528b9dSMatthias Ringwald     NULL,
98cc528b9dSMatthias Ringwald };
99cc528b9dSMatthias Ringwald 
100cc528b9dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
101cc528b9dSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)102cc528b9dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
103cc528b9dSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
104cc528b9dSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
105cc528b9dSMatthias Ringwald         case BTSTACK_EVENT_STATE:
106cc528b9dSMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
107cc528b9dSMatthias Ringwald                 case HCI_STATE_WORKING:
108cc528b9dSMatthias Ringwald                     gap_local_bd_addr(local_addr);
109cc528b9dSMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
110cc528b9dSMatthias Ringwald                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
111cc528b9dSMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
112cc528b9dSMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
113cc528b9dSMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
114cc528b9dSMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
115cc528b9dSMatthias Ringwald #ifdef ENABLE_CLASSIC
116cc528b9dSMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
117cc528b9dSMatthias Ringwald #endif
118cc528b9dSMatthias Ringwald #ifdef ENABLE_BLE
119cc528b9dSMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
120cc528b9dSMatthias Ringwald #endif
121cc528b9dSMatthias Ringwald                     break;
122cc528b9dSMatthias Ringwald                 case HCI_STATE_OFF:
123cc528b9dSMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
124cc528b9dSMatthias Ringwald                     if (!shutdown_triggered) break;
125cc528b9dSMatthias Ringwald                     // reset stdin
126cc528b9dSMatthias Ringwald                     btstack_stdin_reset();
127cc528b9dSMatthias Ringwald                     log_info("Good bye, see you.\n");
128cc528b9dSMatthias Ringwald                     exit(0);
129cc528b9dSMatthias Ringwald                     break;
130cc528b9dSMatthias Ringwald                 default:
131cc528b9dSMatthias Ringwald                     break;
132cc528b9dSMatthias Ringwald             }
133cc528b9dSMatthias Ringwald             break;
134cc528b9dSMatthias Ringwald         default:
135cc528b9dSMatthias Ringwald             break;
136cc528b9dSMatthias Ringwald     }
137cc528b9dSMatthias Ringwald }
138cc528b9dSMatthias Ringwald 
trigger_shutdown(void)139cc528b9dSMatthias Ringwald static void trigger_shutdown(void){
140cc528b9dSMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
141cc528b9dSMatthias Ringwald     log_info("sigint_handler: shutting down");
142cc528b9dSMatthias Ringwald     shutdown_triggered = true;
143cc528b9dSMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
144cc528b9dSMatthias Ringwald }
145cc528b9dSMatthias Ringwald 
146cc528b9dSMatthias Ringwald static int led_state = 0;
hal_led_toggle(void)147cc528b9dSMatthias Ringwald void hal_led_toggle(void){
148cc528b9dSMatthias Ringwald     led_state = 1 - led_state;
149cc528b9dSMatthias Ringwald     printf("LED State %u\n", led_state);
150cc528b9dSMatthias Ringwald }
151cc528b9dSMatthias Ringwald 
nxp_phase2(uint8_t status)152cc528b9dSMatthias Ringwald static void nxp_phase2(uint8_t status){
153cc528b9dSMatthias Ringwald 
154cc528b9dSMatthias Ringwald     if (status){
155cc528b9dSMatthias Ringwald         printf("Download firmware failed\n");
156cc528b9dSMatthias Ringwald         return;
157cc528b9dSMatthias Ringwald     }
158cc528b9dSMatthias Ringwald 
159cc528b9dSMatthias Ringwald     printf("Phase 2: Main app\n");
160cc528b9dSMatthias Ringwald 
16120a60f96SMatthias Ringwald     // assert last ack was sent at original speed
16220a60f96SMatthias Ringwald     usleep(100000);
16320a60f96SMatthias Ringwald 
164c6e26c7dSMatthias Ringwald     // re-configure UART: enable flow control, set initial baudrate
165c6e26c7dSMatthias Ringwald     transport_config.baudrate_init = btstack_chipset_nxp_get_initial_baudrate();
1662364ad0cSMatthias Ringwald     uart_driver->set_flowcontrol(transport_config.flowcontrol);
1672364ad0cSMatthias Ringwald 
168cc528b9dSMatthias Ringwald     // init HCI
169cc528b9dSMatthias Ringwald     const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
170cc528b9dSMatthias Ringwald     hci_init(transport, (void*) &transport_config);
1719d1825b2SMatthias Ringwald     hci_set_chipset(btstack_chipset_nxp_instance());
172cc528b9dSMatthias Ringwald 
173*1c00f289SMatthias Ringwald #ifdef HAVE_PORTAUDIO
174*1c00f289SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
175*1c00f289SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
176*1c00f289SMatthias Ringwald #endif
177*1c00f289SMatthias Ringwald 
178cc528b9dSMatthias Ringwald     // inform about BTstack state
179cc528b9dSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
180cc528b9dSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
181cc528b9dSMatthias Ringwald 
182cc528b9dSMatthias Ringwald     // register callback for CTRL-c
183cc528b9dSMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
184cc528b9dSMatthias Ringwald 
185cc528b9dSMatthias Ringwald     // setup app
186cc528b9dSMatthias Ringwald     btstack_main(main_argc, main_argv);
187cc528b9dSMatthias Ringwald }
188cc528b9dSMatthias Ringwald 
main(int argc,const char * argv[])189cc528b9dSMatthias Ringwald int main(int argc, const char * argv[]){
190cc528b9dSMatthias Ringwald 
191cc528b9dSMatthias Ringwald 	/// GET STARTED with BTstack ///
192cc528b9dSMatthias Ringwald 	btstack_memory_init();
193cc528b9dSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
194cc528b9dSMatthias Ringwald 
195cc528b9dSMatthias Ringwald     // log into file using HCI_DUMP_PACKETLOGGER format
196cc528b9dSMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
197cc528b9dSMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
198cc528b9dSMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
199cc528b9dSMatthias Ringwald     hci_dump_init(hci_dump_impl);
200cc528b9dSMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
201cc528b9dSMatthias Ringwald 
202cc528b9dSMatthias Ringwald     // pick serial port
203cc528b9dSMatthias Ringwald     transport_config.device_name = "/dev/tty.usbserial-A506WORJ"; // DVK-ST60-2230C / 88W8997
204cc528b9dSMatthias Ringwald     // transport_config.device_name = "/dev/tty.usbserial-FT1XBGIM";  // murata m.2 adapter
205cc528b9dSMatthias Ringwald 
206cc528b9dSMatthias Ringwald     // accept path from command line
207cc528b9dSMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
208cc528b9dSMatthias Ringwald         transport_config.device_name = argv[2];
209cc528b9dSMatthias Ringwald         argc -= 2;
210cc528b9dSMatthias Ringwald         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
211cc528b9dSMatthias Ringwald     }
212cc528b9dSMatthias Ringwald     printf("H4 device: %s\n", transport_config.device_name);
213cc528b9dSMatthias Ringwald 
214cc528b9dSMatthias Ringwald     uart_driver = btstack_uart_posix_instance();
215cc528b9dSMatthias Ringwald 
216cc528b9dSMatthias Ringwald     // extract UART config from transport config
217cc528b9dSMatthias Ringwald     uart_config.baudrate    = transport_config.baudrate_init;
2182364ad0cSMatthias Ringwald     uart_config.flowcontrol = 0;
219cc528b9dSMatthias Ringwald     uart_config.device_name = transport_config.device_name;
220cc528b9dSMatthias Ringwald     uart_driver->init(&uart_config);
221cc528b9dSMatthias Ringwald 
222cc528b9dSMatthias Ringwald     main_argc = argc;
223cc528b9dSMatthias Ringwald     main_argv = argv;
224cc528b9dSMatthias Ringwald 
225cc528b9dSMatthias Ringwald     const char * firmware_v1 = "uartuart8997_bt_v4.bin";
226c6e26c7dSMatthias Ringwald     printf("Phase 1: Download firmware\n");
227cc528b9dSMatthias Ringwald     btstack_chipset_nxp_set_v1_firmware_path(firmware_v1);
228cc528b9dSMatthias Ringwald     btstack_chipset_nxp_download_firmware_with_uart(uart_driver, &nxp_phase2);
229cc528b9dSMatthias Ringwald 
230cc528b9dSMatthias Ringwald     // go
231cc528b9dSMatthias Ringwald     btstack_run_loop_execute();
232cc528b9dSMatthias Ringwald     return 0;
233cc528b9dSMatthias Ringwald }
234