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