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