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