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