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