1415a769fSMatthias Ringwald /*
2415a769fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
3415a769fSMatthias Ringwald *
4415a769fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5415a769fSMatthias Ringwald * modification, are permitted provided that the following conditions
6415a769fSMatthias Ringwald * are met:
7415a769fSMatthias Ringwald *
8415a769fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9415a769fSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10415a769fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11415a769fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12415a769fSMatthias Ringwald * documentation and/or other materials provided with the distribution.
13415a769fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14415a769fSMatthias Ringwald * contributors may be used to endorse or promote products derived
15415a769fSMatthias Ringwald * from this software without specific prior written permission.
16415a769fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17415a769fSMatthias Ringwald * personal benefit and not for any commercial purpose or for
18415a769fSMatthias Ringwald * monetary gain.
19415a769fSMatthias Ringwald *
20415a769fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21415a769fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22415a769fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23415a769fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24415a769fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25415a769fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26415a769fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27415a769fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28415a769fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29415a769fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30415a769fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31415a769fSMatthias Ringwald * SUCH DAMAGE.
32415a769fSMatthias Ringwald *
33415a769fSMatthias Ringwald * Please inquire about commercial licensing options at
34415a769fSMatthias Ringwald * [email protected]
35415a769fSMatthias Ringwald *
36415a769fSMatthias Ringwald */
37415a769fSMatthias Ringwald
38*bc6a318fSMatthias Ringwald #define BTSTACK_FILE__ "main.c"
39415a769fSMatthias Ringwald
40415a769fSMatthias Ringwald // *****************************************************************************
41415a769fSMatthias Ringwald //
42415a769fSMatthias Ringwald // minimal setup for HCI code
43415a769fSMatthias Ringwald //
44415a769fSMatthias Ringwald // *****************************************************************************
45415a769fSMatthias Ringwald
46415a769fSMatthias Ringwald #include <stdint.h>
47415a769fSMatthias Ringwald #include <stdio.h>
48415a769fSMatthias Ringwald #include <stdlib.h>
49415a769fSMatthias Ringwald #include <string.h>
50415a769fSMatthias Ringwald #include <signal.h>
51415a769fSMatthias Ringwald
52415a769fSMatthias Ringwald #include "btstack_config.h"
53415a769fSMatthias Ringwald
54415a769fSMatthias Ringwald #include "bluetooth_company_id.h"
55415a769fSMatthias Ringwald #include "btstack_debug.h"
56415a769fSMatthias Ringwald #include "btstack_event.h"
57415a769fSMatthias Ringwald #include "ble/le_device_db_tlv.h"
58415a769fSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
59415a769fSMatthias Ringwald #include "btstack_memory.h"
60415a769fSMatthias Ringwald #include "btstack_run_loop.h"
61415a769fSMatthias Ringwald #include "btstack_run_loop_posix.h"
62415a769fSMatthias Ringwald #include "hci.h"
63415a769fSMatthias Ringwald #include "hci_dump.h"
64415a769fSMatthias Ringwald #include "hci_dump_posix_fs.h"
65415a769fSMatthias Ringwald #include "hci_transport.h"
66415a769fSMatthias Ringwald #include "hci_transport_usb.h"
67415a769fSMatthias Ringwald #include "btstack_stdin.h"
68415a769fSMatthias Ringwald #include "btstack_audio.h"
69415a769fSMatthias Ringwald #include "btstack_tlv_posix.h"
70415a769fSMatthias Ringwald
71415a769fSMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
72415a769fSMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
73415a769fSMatthias Ringwald static char tlv_db_path[100];
74415a769fSMatthias Ringwald static const btstack_tlv_t * tlv_impl;
75415a769fSMatthias Ringwald static btstack_tlv_posix_t tlv_context;
76415a769fSMatthias Ringwald static bd_addr_t local_addr;
77415a769fSMatthias Ringwald
78415a769fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
79415a769fSMatthias Ringwald
80415a769fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
81415a769fSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)82415a769fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
83415a769fSMatthias Ringwald UNUSED(channel);
84415a769fSMatthias Ringwald UNUSED(size);
85415a769fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
86415a769fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){
87415a769fSMatthias Ringwald case BTSTACK_EVENT_STATE:
88415a769fSMatthias Ringwald switch (btstack_event_state_get_state(packet)){
89415a769fSMatthias Ringwald case HCI_STATE_WORKING:
90415a769fSMatthias Ringwald gap_local_bd_addr(local_addr);
9154736c11SMatthias Ringwald btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
9254736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(local_addr));
9354736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
94415a769fSMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
95415a769fSMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context);
96415a769fSMatthias Ringwald #ifdef ENABLE_CLASSIC
97415a769fSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
98415a769fSMatthias Ringwald #endif
99415a769fSMatthias Ringwald #ifdef ENABLE_BLE
100415a769fSMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context);
101415a769fSMatthias Ringwald #endif
102415a769fSMatthias Ringwald break;
103415a769fSMatthias Ringwald default:
104415a769fSMatthias Ringwald break;
105415a769fSMatthias Ringwald }
106415a769fSMatthias Ringwald break;
107415a769fSMatthias Ringwald default:
108415a769fSMatthias Ringwald break;
109415a769fSMatthias Ringwald }
110415a769fSMatthias Ringwald }
111415a769fSMatthias Ringwald
btstack_assert_failed(const char * file,uint16_t line_nr)112415a769fSMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){
113415a769fSMatthias Ringwald printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
114415a769fSMatthias Ringwald while(1);
115415a769fSMatthias Ringwald }
116415a769fSMatthias Ringwald
117415a769fSMatthias Ringwald #define USB_MAX_PATH_LEN 7
main(int argc,const char * argv[])118415a769fSMatthias Ringwald int main(int argc, const char * argv[]){
119415a769fSMatthias Ringwald
120415a769fSMatthias Ringwald uint8_t usb_path[USB_MAX_PATH_LEN];
121415a769fSMatthias Ringwald int usb_path_len = 0;
122415a769fSMatthias Ringwald const char * usb_path_string = NULL;
123415a769fSMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){
124415a769fSMatthias Ringwald // parse command line options for "-u 11:22:33"
125415a769fSMatthias Ringwald usb_path_string = argv[2];
126415a769fSMatthias Ringwald while (1){
127415a769fSMatthias Ringwald char * delimiter;
128415a769fSMatthias Ringwald int port = strtol(usb_path_string, &delimiter, 16);
129415a769fSMatthias Ringwald usb_path[usb_path_len] = port;
130415a769fSMatthias Ringwald usb_path_len++;
131415a769fSMatthias Ringwald if (!delimiter) break;
132415a769fSMatthias Ringwald if (*delimiter != ':' && *delimiter != '-') break;
133415a769fSMatthias Ringwald usb_path_string = delimiter+1;
134415a769fSMatthias Ringwald }
135415a769fSMatthias Ringwald argc -= 2;
1360033ffe4SMatthias Ringwald memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
137415a769fSMatthias Ringwald }
138415a769fSMatthias Ringwald
139415a769fSMatthias Ringwald /// GET STARTED with BTstack ///
140415a769fSMatthias Ringwald btstack_memory_init();
141415a769fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance());
142415a769fSMatthias Ringwald
143415a769fSMatthias Ringwald if (usb_path_len){
144415a769fSMatthias Ringwald hci_transport_usb_set_path(usb_path_len, usb_path);
145415a769fSMatthias Ringwald }
146415a769fSMatthias Ringwald
147415a769fSMatthias Ringwald // init HCI
148415a769fSMatthias Ringwald hci_init(hci_transport_usb_instance(), NULL);
149415a769fSMatthias Ringwald
150415a769fSMatthias Ringwald #ifdef HAVE_PORTAUDIO
151415a769fSMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
152415a769fSMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
153415a769fSMatthias Ringwald #endif
154415a769fSMatthias Ringwald
155415a769fSMatthias Ringwald // inform about BTstack state
156415a769fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
157415a769fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
158415a769fSMatthias Ringwald
159415a769fSMatthias Ringwald // setup app
160415a769fSMatthias Ringwald btstack_main(argc, argv);
161415a769fSMatthias Ringwald
162415a769fSMatthias Ringwald // go
163415a769fSMatthias Ringwald btstack_run_loop_execute();
164415a769fSMatthias Ringwald
165415a769fSMatthias Ringwald return 0;
166415a769fSMatthias Ringwald }
167