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 "bluetooth_company_id.h"
55 #include "btstack_debug.h"
56 #include "btstack_event.h"
57 #include "ble/le_device_db_tlv.h"
58 #include "classic/btstack_link_key_db_tlv.h"
59 #include "btstack_memory.h"
60 #include "btstack_run_loop.h"
61 #include "btstack_run_loop_posix.h"
62 #include "hci.h"
63 #include "hci_dump.h"
64 #include "hci_dump_posix_fs.h"
65 #include "hci_transport.h"
66 #include "hci_transport_usb.h"
67 #include "btstack_stdin.h"
68 #include "btstack_audio.h"
69 #include "btstack_tlv_posix.h"
70
71 #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
72 #define TLV_DB_PATH_POSTFIX ".tlv"
73 static char tlv_db_path[100];
74 static const btstack_tlv_t * tlv_impl;
75 static btstack_tlv_posix_t tlv_context;
76 static bd_addr_t local_addr;
77
78 int btstack_main(int argc, const char * argv[]);
79
80 static btstack_packet_callback_registration_t hci_event_callback_registration;
81
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)82 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
83 UNUSED(channel);
84 UNUSED(size);
85 if (packet_type != HCI_EVENT_PACKET) return;
86 switch (hci_event_packet_get_type(packet)){
87 case BTSTACK_EVENT_STATE:
88 switch (btstack_event_state_get_state(packet)){
89 case HCI_STATE_WORKING:
90 gap_local_bd_addr(local_addr);
91 btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
92 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(local_addr));
93 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
94 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
95 btstack_tlv_set_instance(tlv_impl, &tlv_context);
96 #ifdef ENABLE_CLASSIC
97 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
98 #endif
99 #ifdef ENABLE_BLE
100 le_device_db_tlv_configure(tlv_impl, &tlv_context);
101 #endif
102 break;
103 default:
104 break;
105 }
106 break;
107 default:
108 break;
109 }
110 }
111
btstack_assert_failed(const char * file,uint16_t line_nr)112 void btstack_assert_failed(const char * file, uint16_t line_nr){
113 printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
114 while(1);
115 }
116
117 #define USB_MAX_PATH_LEN 7
main(int argc,const char * argv[])118 int main(int argc, const char * argv[]){
119
120 uint8_t usb_path[USB_MAX_PATH_LEN];
121 int usb_path_len = 0;
122 const char * usb_path_string = NULL;
123 if (argc >= 3 && strcmp(argv[1], "-u") == 0){
124 // parse command line options for "-u 11:22:33"
125 usb_path_string = argv[2];
126 while (1){
127 char * delimiter;
128 int port = strtol(usb_path_string, &delimiter, 16);
129 usb_path[usb_path_len] = port;
130 usb_path_len++;
131 if (!delimiter) break;
132 if (*delimiter != ':' && *delimiter != '-') break;
133 usb_path_string = delimiter+1;
134 }
135 argc -= 2;
136 memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
137 }
138
139 /// GET STARTED with BTstack ///
140 btstack_memory_init();
141 btstack_run_loop_init(btstack_run_loop_posix_get_instance());
142
143 if (usb_path_len){
144 hci_transport_usb_set_path(usb_path_len, usb_path);
145 }
146
147 // init HCI
148 hci_init(hci_transport_usb_instance(), NULL);
149
150 #ifdef HAVE_PORTAUDIO
151 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
152 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
153 #endif
154
155 // inform about BTstack state
156 hci_event_callback_registration.callback = &packet_handler;
157 hci_add_event_handler(&hci_event_callback_registration);
158
159 // setup app
160 btstack_main(argc, argv);
161
162 // go
163 btstack_run_loop_execute();
164
165 return 0;
166 }
167