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