xref: /btstack/test/security_manager_sc/main.c (revision a921db493845ca4f3e5356bec3efb2b23208c8fa)
1*a921db49SMatthias Ringwald /*
2*a921db49SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*a921db49SMatthias Ringwald  *
4*a921db49SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*a921db49SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*a921db49SMatthias Ringwald  * are met:
7*a921db49SMatthias Ringwald  *
8*a921db49SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*a921db49SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*a921db49SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*a921db49SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*a921db49SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*a921db49SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*a921db49SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*a921db49SMatthias Ringwald  *    from this software without specific prior written permission.
16*a921db49SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*a921db49SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*a921db49SMatthias Ringwald  *    monetary gain.
19*a921db49SMatthias Ringwald  *
20*a921db49SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*a921db49SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*a921db49SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*a921db49SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*a921db49SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*a921db49SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*a921db49SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*a921db49SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*a921db49SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*a921db49SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*a921db49SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*a921db49SMatthias Ringwald  * SUCH DAMAGE.
32*a921db49SMatthias Ringwald  *
33*a921db49SMatthias Ringwald  * Please inquire about commercial licensing options at
34*a921db49SMatthias Ringwald  * [email protected]
35*a921db49SMatthias Ringwald  *
36*a921db49SMatthias Ringwald  */
37*a921db49SMatthias Ringwald 
38*a921db49SMatthias Ringwald #define __BTSTACK_FILE__ "main.c"
39*a921db49SMatthias Ringwald 
40*a921db49SMatthias Ringwald // *****************************************************************************
41*a921db49SMatthias Ringwald //
42*a921db49SMatthias Ringwald // minimal setup for HCI code
43*a921db49SMatthias Ringwald //
44*a921db49SMatthias Ringwald // *****************************************************************************
45*a921db49SMatthias Ringwald 
46*a921db49SMatthias Ringwald #include <stdint.h>
47*a921db49SMatthias Ringwald #include <stdio.h>
48*a921db49SMatthias Ringwald #include <stdlib.h>
49*a921db49SMatthias Ringwald #include <string.h>
50*a921db49SMatthias Ringwald #include <signal.h>
51*a921db49SMatthias Ringwald 
52*a921db49SMatthias Ringwald #include "btstack_config.h"
53*a921db49SMatthias Ringwald 
54*a921db49SMatthias Ringwald #include "bluetooth_company_id.h"
55*a921db49SMatthias Ringwald #include "btstack_debug.h"
56*a921db49SMatthias Ringwald #include "btstack_event.h"
57*a921db49SMatthias Ringwald #include "ble/le_device_db_tlv.h"
58*a921db49SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
59*a921db49SMatthias Ringwald #include "btstack_memory.h"
60*a921db49SMatthias Ringwald #include "btstack_run_loop.h"
61*a921db49SMatthias Ringwald #include "btstack_run_loop_posix.h"
62*a921db49SMatthias Ringwald #include "hal_led.h"
63*a921db49SMatthias Ringwald #include "hci.h"
64*a921db49SMatthias Ringwald #include "hci_dump.h"
65*a921db49SMatthias Ringwald #include "btstack_stdin.h"
66*a921db49SMatthias Ringwald #include "btstack_audio.h"
67*a921db49SMatthias Ringwald #include "btstack_tlv_posix.h"
68*a921db49SMatthias Ringwald #include "btstack_chipset_zephyr.h"
69*a921db49SMatthias Ringwald 
70*a921db49SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
71*a921db49SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
72*a921db49SMatthias Ringwald static char tlv_db_path[100];
73*a921db49SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
74*a921db49SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
75*a921db49SMatthias Ringwald static bd_addr_t             local_addr;
76*a921db49SMatthias Ringwald 
77*a921db49SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
78*a921db49SMatthias Ringwald 
79*a921db49SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
80*a921db49SMatthias Ringwald 
81*a921db49SMatthias Ringwald static bd_addr_t static_address;
82*a921db49SMatthias Ringwald static int using_static_address;
83*a921db49SMatthias Ringwald 
84*a921db49SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
85*a921db49SMatthias Ringwald 
86*a921db49SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
87*a921db49SMatthias Ringwald     printf("Local version information:\n");
88*a921db49SMatthias Ringwald     uint16_t hci_version    = packet[6];
89*a921db49SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
90*a921db49SMatthias Ringwald     uint16_t lmp_version    = packet[9];
91*a921db49SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
92*a921db49SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
93*a921db49SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
94*a921db49SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
95*a921db49SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
96*a921db49SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
97*a921db49SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
98*a921db49SMatthias Ringwald     switch (manufacturer){
99*a921db49SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
100*a921db49SMatthias Ringwald             printf("Linux Foundation - assume Zephyr hci_usb example running on nRF52xx\n");
101*a921db49SMatthias Ringwald             hci_set_chipset(btstack_chipset_zephyr_instance());
102*a921db49SMatthias Ringwald             break;
103*a921db49SMatthias Ringwald         default:
104*a921db49SMatthias Ringwald             break;
105*a921db49SMatthias Ringwald     }
106*a921db49SMatthias Ringwald }
107*a921db49SMatthias Ringwald 
108*a921db49SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
109*a921db49SMatthias Ringwald     UNUSED(channel);
110*a921db49SMatthias Ringwald     UNUSED(size);
111*a921db49SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
112*a921db49SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
113*a921db49SMatthias Ringwald         case BTSTACK_EVENT_STATE:
114*a921db49SMatthias Ringwald             switch (btstack_event_state_get_state(packet)){
115*a921db49SMatthias Ringwald                 case HCI_STATE_WORKING:
116*a921db49SMatthias Ringwald                     gap_local_bd_addr(local_addr);
117*a921db49SMatthias Ringwald                     if (using_static_address){
118*a921db49SMatthias Ringwald                         memcpy(local_addr, static_address, 6);
119*a921db49SMatthias Ringwald                     }
120*a921db49SMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
121*a921db49SMatthias Ringwald                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
122*a921db49SMatthias Ringwald                     strcat(tlv_db_path, bd_addr_to_str(local_addr));
123*a921db49SMatthias Ringwald                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
124*a921db49SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
125*a921db49SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
126*a921db49SMatthias Ringwald #ifdef ENABLE_CLASSIC
127*a921db49SMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
128*a921db49SMatthias Ringwald #endif
129*a921db49SMatthias Ringwald #ifdef ENABLE_BLE
130*a921db49SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
131*a921db49SMatthias Ringwald #endif
132*a921db49SMatthias Ringwald                     break;
133*a921db49SMatthias Ringwald                 case HCI_STATE_OFF:
134*a921db49SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
135*a921db49SMatthias Ringwald                     break;
136*a921db49SMatthias Ringwald                 default:
137*a921db49SMatthias Ringwald                     break;
138*a921db49SMatthias Ringwald             }
139*a921db49SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
140*a921db49SMatthias Ringwald             break;
141*a921db49SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
142*a921db49SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
143*a921db49SMatthias Ringwald                 local_version_information_handler(packet);
144*a921db49SMatthias Ringwald             }
145*a921db49SMatthias Ringwald             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
146*a921db49SMatthias Ringwald                 reverse_48(&packet[7], static_address);
147*a921db49SMatthias Ringwald                 gap_random_address_set(static_address);
148*a921db49SMatthias Ringwald                 using_static_address = 1;
149*a921db49SMatthias Ringwald             }
150*a921db49SMatthias Ringwald             break;
151*a921db49SMatthias Ringwald         default:
152*a921db49SMatthias Ringwald             break;
153*a921db49SMatthias Ringwald     }
154*a921db49SMatthias Ringwald }
155*a921db49SMatthias Ringwald 
156*a921db49SMatthias Ringwald static void sigint_handler(int param){
157*a921db49SMatthias Ringwald     UNUSED(param);
158*a921db49SMatthias Ringwald 
159*a921db49SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
160*a921db49SMatthias Ringwald     log_info("sigint_handler: shutting down");
161*a921db49SMatthias Ringwald 
162*a921db49SMatthias Ringwald     // reset anyway
163*a921db49SMatthias Ringwald     btstack_stdin_reset();
164*a921db49SMatthias Ringwald 
165*a921db49SMatthias Ringwald     // power down
166*a921db49SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
167*a921db49SMatthias Ringwald     hci_close();
168*a921db49SMatthias Ringwald     log_info("Good bye, see you.\n");
169*a921db49SMatthias Ringwald     exit(0);
170*a921db49SMatthias Ringwald }
171*a921db49SMatthias Ringwald 
172*a921db49SMatthias Ringwald static int led_state = 0;
173*a921db49SMatthias Ringwald void hal_led_toggle(void){
174*a921db49SMatthias Ringwald     led_state = 1 - led_state;
175*a921db49SMatthias Ringwald     printf("LED State %u\n", led_state);
176*a921db49SMatthias Ringwald }
177*a921db49SMatthias Ringwald 
178*a921db49SMatthias Ringwald #define USB_MAX_PATH_LEN 7
179*a921db49SMatthias Ringwald int main(int argc, const char * argv[]){
180*a921db49SMatthias Ringwald 
181*a921db49SMatthias Ringwald     uint8_t usb_path[USB_MAX_PATH_LEN];
182*a921db49SMatthias Ringwald     int usb_path_len = 0;
183*a921db49SMatthias Ringwald     const char * usb_path_string = NULL;
184*a921db49SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
185*a921db49SMatthias Ringwald         // parse command line options for "-u 11:22:33"
186*a921db49SMatthias Ringwald         usb_path_string = argv[2];
187*a921db49SMatthias Ringwald         printf("Specified USB Path: ");
188*a921db49SMatthias Ringwald         while (1){
189*a921db49SMatthias Ringwald             char * delimiter;
190*a921db49SMatthias Ringwald             int port = strtol(usb_path_string, &delimiter, 16);
191*a921db49SMatthias Ringwald             usb_path[usb_path_len] = port;
192*a921db49SMatthias Ringwald             usb_path_len++;
193*a921db49SMatthias Ringwald             printf("%02x ", port);
194*a921db49SMatthias Ringwald             if (!delimiter) break;
195*a921db49SMatthias Ringwald             if (*delimiter != ':' && *delimiter != '-') break;
196*a921db49SMatthias Ringwald             usb_path_string = delimiter+1;
197*a921db49SMatthias Ringwald         }
198*a921db49SMatthias Ringwald         printf("\n");
199*a921db49SMatthias Ringwald         argc -= 2;
200*a921db49SMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
201*a921db49SMatthias Ringwald     }
202*a921db49SMatthias Ringwald 
203*a921db49SMatthias Ringwald 	/// GET STARTED with BTstack ///
204*a921db49SMatthias Ringwald 	btstack_memory_init();
205*a921db49SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
206*a921db49SMatthias Ringwald 
207*a921db49SMatthias Ringwald     if (usb_path_len){
208*a921db49SMatthias Ringwald         hci_transport_usb_set_path(usb_path_len, usb_path);
209*a921db49SMatthias Ringwald     }
210*a921db49SMatthias Ringwald 
211*a921db49SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
212*a921db49SMatthias Ringwald 
213*a921db49SMatthias Ringwald     char pklg_path[100];
214*a921db49SMatthias Ringwald     strcpy(pklg_path, "/tmp/hci_dump");
215*a921db49SMatthias Ringwald     if (usb_path_len){
216*a921db49SMatthias Ringwald         strcat(pklg_path, "_");
217*a921db49SMatthias Ringwald         strcat(pklg_path, usb_path_string);
218*a921db49SMatthias Ringwald     }
219*a921db49SMatthias Ringwald     strcat(pklg_path, ".pklg");
220*a921db49SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
221*a921db49SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
222*a921db49SMatthias Ringwald 
223*a921db49SMatthias Ringwald     // init HCI
224*a921db49SMatthias Ringwald 	hci_init(hci_transport_usb_instance(), NULL);
225*a921db49SMatthias Ringwald 
226*a921db49SMatthias Ringwald #ifdef HAVE_PORTAUDIO
227*a921db49SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
228*a921db49SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
229*a921db49SMatthias Ringwald #endif
230*a921db49SMatthias Ringwald 
231*a921db49SMatthias Ringwald     // inform about BTstack state
232*a921db49SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
233*a921db49SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
234*a921db49SMatthias Ringwald 
235*a921db49SMatthias Ringwald     // handle CTRL-c
236*a921db49SMatthias Ringwald     signal(SIGINT, sigint_handler);
237*a921db49SMatthias Ringwald 
238*a921db49SMatthias Ringwald     // setup app
239*a921db49SMatthias Ringwald     btstack_main(argc, argv);
240*a921db49SMatthias Ringwald 
241*a921db49SMatthias Ringwald     // go
242*a921db49SMatthias Ringwald     btstack_run_loop_execute();
243*a921db49SMatthias Ringwald 
244*a921db49SMatthias Ringwald     return 0;
245*a921db49SMatthias Ringwald }
246