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