xref: /btstack/test/classic-oob-pairing/main.c (revision 415a769f958a35f67626d856694f6fe765039566)
1*415a769fSMatthias Ringwald /*
2*415a769fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*415a769fSMatthias Ringwald  *
4*415a769fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*415a769fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*415a769fSMatthias Ringwald  * are met:
7*415a769fSMatthias Ringwald  *
8*415a769fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*415a769fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*415a769fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*415a769fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*415a769fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*415a769fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*415a769fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*415a769fSMatthias Ringwald  *    from this software without specific prior written permission.
16*415a769fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*415a769fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*415a769fSMatthias Ringwald  *    monetary gain.
19*415a769fSMatthias Ringwald  *
20*415a769fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*415a769fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*415a769fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*415a769fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*415a769fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*415a769fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*415a769fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*415a769fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*415a769fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*415a769fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*415a769fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*415a769fSMatthias Ringwald  * SUCH DAMAGE.
32*415a769fSMatthias Ringwald  *
33*415a769fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*415a769fSMatthias Ringwald  * [email protected]
35*415a769fSMatthias Ringwald  *
36*415a769fSMatthias Ringwald  */
37*415a769fSMatthias Ringwald 
38*415a769fSMatthias Ringwald #define __BTSTACK_FILE__ "main.c"
39*415a769fSMatthias Ringwald 
40*415a769fSMatthias Ringwald // *****************************************************************************
41*415a769fSMatthias Ringwald //
42*415a769fSMatthias Ringwald // minimal setup for HCI code
43*415a769fSMatthias Ringwald //
44*415a769fSMatthias Ringwald // *****************************************************************************
45*415a769fSMatthias Ringwald 
46*415a769fSMatthias Ringwald #include <stdint.h>
47*415a769fSMatthias Ringwald #include <stdio.h>
48*415a769fSMatthias Ringwald #include <stdlib.h>
49*415a769fSMatthias Ringwald #include <string.h>
50*415a769fSMatthias Ringwald #include <signal.h>
51*415a769fSMatthias Ringwald 
52*415a769fSMatthias Ringwald #include "btstack_config.h"
53*415a769fSMatthias Ringwald 
54*415a769fSMatthias Ringwald #include "bluetooth_company_id.h"
55*415a769fSMatthias Ringwald #include "btstack_debug.h"
56*415a769fSMatthias Ringwald #include "btstack_event.h"
57*415a769fSMatthias Ringwald #include "ble/le_device_db_tlv.h"
58*415a769fSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
59*415a769fSMatthias Ringwald #include "btstack_memory.h"
60*415a769fSMatthias Ringwald #include "btstack_run_loop.h"
61*415a769fSMatthias Ringwald #include "btstack_run_loop_posix.h"
62*415a769fSMatthias Ringwald #include "hci.h"
63*415a769fSMatthias Ringwald #include "hci_dump.h"
64*415a769fSMatthias Ringwald #include "hci_dump_posix_fs.h"
65*415a769fSMatthias Ringwald #include "hci_transport.h"
66*415a769fSMatthias Ringwald #include "hci_transport_usb.h"
67*415a769fSMatthias Ringwald #include "btstack_stdin.h"
68*415a769fSMatthias Ringwald #include "btstack_audio.h"
69*415a769fSMatthias Ringwald #include "btstack_tlv_posix.h"
70*415a769fSMatthias Ringwald 
71*415a769fSMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
72*415a769fSMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
73*415a769fSMatthias Ringwald static char tlv_db_path[100];
74*415a769fSMatthias Ringwald static const btstack_tlv_t * tlv_impl;
75*415a769fSMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
76*415a769fSMatthias Ringwald static bd_addr_t             local_addr;
77*415a769fSMatthias Ringwald 
78*415a769fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
79*415a769fSMatthias Ringwald 
80*415a769fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
81*415a769fSMatthias Ringwald 
82*415a769fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
83*415a769fSMatthias Ringwald     UNUSED(channel);
84*415a769fSMatthias Ringwald     UNUSED(size);
85*415a769fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
86*415a769fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
87*415a769fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
88*415a769fSMatthias Ringwald             switch (btstack_event_state_get_state(packet)){
89*415a769fSMatthias Ringwald                 case HCI_STATE_WORKING:
90*415a769fSMatthias Ringwald                     gap_local_bd_addr(local_addr);
91*415a769fSMatthias Ringwald                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
92*415a769fSMatthias Ringwald                     strcat(tlv_db_path, bd_addr_to_str(local_addr));
93*415a769fSMatthias Ringwald                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
94*415a769fSMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
95*415a769fSMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
96*415a769fSMatthias Ringwald #ifdef ENABLE_CLASSIC
97*415a769fSMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
98*415a769fSMatthias Ringwald #endif
99*415a769fSMatthias Ringwald #ifdef ENABLE_BLE
100*415a769fSMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
101*415a769fSMatthias Ringwald #endif
102*415a769fSMatthias Ringwald                     break;
103*415a769fSMatthias Ringwald                 default:
104*415a769fSMatthias Ringwald                     break;
105*415a769fSMatthias Ringwald             }
106*415a769fSMatthias Ringwald             break;
107*415a769fSMatthias Ringwald         default:
108*415a769fSMatthias Ringwald             break;
109*415a769fSMatthias Ringwald     }
110*415a769fSMatthias Ringwald }
111*415a769fSMatthias Ringwald 
112*415a769fSMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){
113*415a769fSMatthias Ringwald     printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
114*415a769fSMatthias Ringwald     while(1);
115*415a769fSMatthias Ringwald }
116*415a769fSMatthias Ringwald 
117*415a769fSMatthias Ringwald #define USB_MAX_PATH_LEN 7
118*415a769fSMatthias Ringwald int main(int argc, const char * argv[]){
119*415a769fSMatthias Ringwald 
120*415a769fSMatthias Ringwald     uint8_t usb_path[USB_MAX_PATH_LEN];
121*415a769fSMatthias Ringwald     int usb_path_len = 0;
122*415a769fSMatthias Ringwald     const char * usb_path_string = NULL;
123*415a769fSMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
124*415a769fSMatthias Ringwald         // parse command line options for "-u 11:22:33"
125*415a769fSMatthias Ringwald         usb_path_string = argv[2];
126*415a769fSMatthias Ringwald         while (1){
127*415a769fSMatthias Ringwald             char * delimiter;
128*415a769fSMatthias Ringwald             int port = strtol(usb_path_string, &delimiter, 16);
129*415a769fSMatthias Ringwald             usb_path[usb_path_len] = port;
130*415a769fSMatthias Ringwald             usb_path_len++;
131*415a769fSMatthias Ringwald             if (!delimiter) break;
132*415a769fSMatthias Ringwald             if (*delimiter != ':' && *delimiter != '-') break;
133*415a769fSMatthias Ringwald             usb_path_string = delimiter+1;
134*415a769fSMatthias Ringwald         }
135*415a769fSMatthias Ringwald         argc -= 2;
136*415a769fSMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
137*415a769fSMatthias Ringwald     }
138*415a769fSMatthias Ringwald 
139*415a769fSMatthias Ringwald 	/// GET STARTED with BTstack ///
140*415a769fSMatthias Ringwald 	btstack_memory_init();
141*415a769fSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
142*415a769fSMatthias Ringwald 
143*415a769fSMatthias Ringwald     if (usb_path_len){
144*415a769fSMatthias Ringwald         hci_transport_usb_set_path(usb_path_len, usb_path);
145*415a769fSMatthias Ringwald     }
146*415a769fSMatthias Ringwald 
147*415a769fSMatthias Ringwald     // init HCI
148*415a769fSMatthias Ringwald 	hci_init(hci_transport_usb_instance(), NULL);
149*415a769fSMatthias Ringwald 
150*415a769fSMatthias Ringwald #ifdef HAVE_PORTAUDIO
151*415a769fSMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
152*415a769fSMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
153*415a769fSMatthias Ringwald #endif
154*415a769fSMatthias Ringwald 
155*415a769fSMatthias Ringwald     // inform about BTstack state
156*415a769fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
157*415a769fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
158*415a769fSMatthias Ringwald 
159*415a769fSMatthias Ringwald     // setup app
160*415a769fSMatthias Ringwald     btstack_main(argc, argv);
161*415a769fSMatthias Ringwald 
162*415a769fSMatthias Ringwald     // go
163*415a769fSMatthias Ringwald     btstack_run_loop_execute();
164*415a769fSMatthias Ringwald 
165*415a769fSMatthias Ringwald     return 0;
166*415a769fSMatthias Ringwald }
167