194a5538cSMatthias Ringwald /*
294a5538cSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
394a5538cSMatthias Ringwald *
494a5538cSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
594a5538cSMatthias Ringwald * modification, are permitted provided that the following conditions
694a5538cSMatthias Ringwald * are met:
794a5538cSMatthias Ringwald *
894a5538cSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
994a5538cSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
1094a5538cSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
1194a5538cSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
1294a5538cSMatthias Ringwald * documentation and/or other materials provided with the distribution.
1394a5538cSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
1494a5538cSMatthias Ringwald * contributors may be used to endorse or promote products derived
1594a5538cSMatthias Ringwald * from this software without specific prior written permission.
1694a5538cSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
1794a5538cSMatthias Ringwald * personal benefit and not for any commercial purpose or for
1894a5538cSMatthias Ringwald * monetary gain.
1994a5538cSMatthias Ringwald *
2094a5538cSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2194a5538cSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2294a5538cSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2594a5538cSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2694a5538cSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2794a5538cSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2894a5538cSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2994a5538cSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3094a5538cSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3194a5538cSMatthias Ringwald * SUCH DAMAGE.
3294a5538cSMatthias Ringwald *
3394a5538cSMatthias Ringwald * Please inquire about commercial licensing options at
3494a5538cSMatthias Ringwald * [email protected]
3594a5538cSMatthias Ringwald *
3694a5538cSMatthias Ringwald */
3794a5538cSMatthias Ringwald
38f11fd9a9SMatthias Ringwald #define BTSTACK_FILE__ "main.c"
39ab2c6ae4SMatthias Ringwald
4094a5538cSMatthias Ringwald // *****************************************************************************
4194a5538cSMatthias Ringwald //
4294a5538cSMatthias Ringwald // minimal setup for HCI code
4394a5538cSMatthias Ringwald //
4494a5538cSMatthias Ringwald // *****************************************************************************
4594a5538cSMatthias Ringwald
4694a5538cSMatthias Ringwald #include <stdint.h>
4794a5538cSMatthias Ringwald #include <stdio.h>
4894a5538cSMatthias Ringwald #include <stdlib.h>
4994a5538cSMatthias Ringwald #include <string.h>
5094a5538cSMatthias Ringwald #include <signal.h>
5194a5538cSMatthias Ringwald
5294a5538cSMatthias Ringwald #include "btstack_config.h"
5394a5538cSMatthias Ringwald
54f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h"
55f11fd9a9SMatthias Ringwald #include "bluetooth_company_id.h"
56f11fd9a9SMatthias Ringwald #include "btstack_chipset_cc256x.h"
57f11fd9a9SMatthias Ringwald #include "btstack_chipset_csr.h"
58f11fd9a9SMatthias Ringwald #include "btstack_chipset_em9301.h"
59f11fd9a9SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
60f11fd9a9SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
6194a5538cSMatthias Ringwald #include "btstack_debug.h"
6294a5538cSMatthias Ringwald #include "btstack_event.h"
6394a5538cSMatthias Ringwald #include "btstack_memory.h"
6494a5538cSMatthias Ringwald #include "btstack_run_loop.h"
6594a5538cSMatthias Ringwald #include "btstack_run_loop_posix.h"
66f11fd9a9SMatthias Ringwald #include "btstack_signal.h"
67f11fd9a9SMatthias Ringwald #include "btstack_stdin.h"
68f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h"
69f11fd9a9SMatthias Ringwald #include "btstack_uart.h"
70f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
7194a5538cSMatthias Ringwald #include "hci.h"
7294a5538cSMatthias Ringwald #include "hci_dump.h"
737435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h"
74c8dfe071SMatthias Ringwald #include "hci_transport.h"
75c8dfe071SMatthias Ringwald #include "hci_transport_h5.h"
7694a5538cSMatthias Ringwald
7794a5538cSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
7894a5538cSMatthias Ringwald
797daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
807daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
817daa8bd9SMatthias Ringwald static char tlv_db_path[100];
827daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
837daa8bd9SMatthias Ringwald static btstack_tlv_posix_t tlv_context;
84f11fd9a9SMatthias Ringwald static bd_addr_t local_addr;
85f11fd9a9SMatthias Ringwald static bool shutdown_triggered;
8691858f18SMatthias Ringwald
8794a5538cSMatthias Ringwald static hci_transport_config_uart_t config = {
8894a5538cSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART,
8994a5538cSMatthias Ringwald 115200,
9094a5538cSMatthias Ringwald 0, // main baudrate
9194a5538cSMatthias Ringwald 1, // flow control
9294a5538cSMatthias Ringwald NULL,
93eb9e0ff4SMatthias Ringwald BTSTACK_UART_PARITY_EVEN, // parity
9494a5538cSMatthias Ringwald };
9594a5538cSMatthias Ringwald
9694a5538cSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
9791858f18SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
9894a5538cSMatthias Ringwald
9994a5538cSMatthias Ringwald
10094a5538cSMatthias Ringwald static int led_state = 0;
hal_led_toggle(void)10194a5538cSMatthias Ringwald void hal_led_toggle(void){
10294a5538cSMatthias Ringwald led_state = 1 - led_state;
10394a5538cSMatthias Ringwald printf("LED State %u\n", led_state);
10494a5538cSMatthias Ringwald }
use_fast_uart(void)10594a5538cSMatthias Ringwald static void use_fast_uart(void){
106a59ee665SMatthias Ringwald printf("Using 921600 baud.\n");
107a59ee665SMatthias Ringwald config.baudrate_main = 921600;
10894a5538cSMatthias Ringwald }
10994a5538cSMatthias Ringwald
local_version_information_handler(uint8_t * packet)11091858f18SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
11194a5538cSMatthias Ringwald printf("Local version information:\n");
1125c2e0bddSMatthias Ringwald uint16_t hci_version = packet[6];
1135c2e0bddSMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7);
1145c2e0bddSMatthias Ringwald uint16_t lmp_version = packet[9];
11594a5538cSMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10);
11694a5538cSMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12);
11794a5538cSMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version);
11894a5538cSMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision);
11994a5538cSMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version);
12094a5538cSMatthias Ringwald printf("- LMP Revision 0x%04x\n", lmp_subversion);
12194a5538cSMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer);
12294a5538cSMatthias Ringwald switch (manufacturer){
12361f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
12499dd7845SMatthias Ringwald printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
12594a5538cSMatthias Ringwald use_fast_uart();
12694a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_csr_instance());
12794a5538cSMatthias Ringwald break;
12861f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
12994a5538cSMatthias Ringwald printf("Texas Instruments - CC256x compatible chipset.\n");
13094a5538cSMatthias Ringwald use_fast_uart();
13194a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance());
13294a5538cSMatthias Ringwald break;
13361f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
1347bc1dbabSMatthias Ringwald printf("Broadcom chipset. Not supported by posix-h5 port, please use port/posix-h5-bcm\n");
1357bc1dbabSMatthias Ringwald exit(10);
13694a5538cSMatthias Ringwald break;
13761f37892SMatthias Ringwald case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
13894a5538cSMatthias Ringwald printf("ST Microelectronics - using STLC2500d driver.\n");
13994a5538cSMatthias Ringwald use_fast_uart();
14094a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_stlc2500d_instance());
14194a5538cSMatthias Ringwald break;
1423e05a31cSMatthias Ringwald case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
14394a5538cSMatthias Ringwald printf("EM Microelectronics - using EM9301 driver.\n");
14494a5538cSMatthias Ringwald hci_set_chipset(btstack_chipset_em9301_instance());
14594a5538cSMatthias Ringwald break;
14694a5538cSMatthias Ringwald default:
14794a5538cSMatthias Ringwald printf("Unknown manufacturer / manufacturer not supported yet.\n");
14894a5538cSMatthias Ringwald break;
14994a5538cSMatthias Ringwald }
15094a5538cSMatthias Ringwald }
15194a5538cSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)15294a5538cSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
15394a5538cSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
15491858f18SMatthias Ringwald switch (hci_event_packet_get_type(packet)){
15591858f18SMatthias Ringwald case BTSTACK_EVENT_STATE:
156f11fd9a9SMatthias Ringwald switch(btstack_event_state_get_state(packet)){
157f11fd9a9SMatthias Ringwald case HCI_STATE_WORKING:
158f11fd9a9SMatthias Ringwald gap_local_bd_addr(local_addr);
159f11fd9a9SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
16054736c11SMatthias Ringwald btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
16154736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
16254736c11SMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
1637daa8bd9SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
1647daa8bd9SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context);
16581862996SMatthias Ringwald #ifdef ENABLE_CLASSIC
16681862996SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
16781862996SMatthias Ringwald #endif
16881862996SMatthias Ringwald #ifdef ENABLE_BLE
16981862996SMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context);
17081862996SMatthias Ringwald #endif
17191858f18SMatthias Ringwald break;
172f11fd9a9SMatthias Ringwald case HCI_STATE_OFF:
173f11fd9a9SMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context);
174f11fd9a9SMatthias Ringwald if (!shutdown_triggered) break;
175f11fd9a9SMatthias Ringwald // reset stdin
176f11fd9a9SMatthias Ringwald btstack_stdin_reset();
177f11fd9a9SMatthias Ringwald log_info("Good bye, see you.\n");
178f11fd9a9SMatthias Ringwald exit(0);
179f11fd9a9SMatthias Ringwald break;
180f11fd9a9SMatthias Ringwald default:
181f11fd9a9SMatthias Ringwald break;
182f11fd9a9SMatthias Ringwald }
183f11fd9a9SMatthias Ringwald break;
18491858f18SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE:
185*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){
18691858f18SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
18791858f18SMatthias Ringwald // terminate, name 248 chars
18891858f18SMatthias Ringwald packet[6+248] = 0;
18991858f18SMatthias Ringwald printf("Local name: %s\n", &packet[6]);
19091858f18SMatthias Ringwald }
191*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
19291858f18SMatthias Ringwald local_version_information_handler(packet);
19391858f18SMatthias Ringwald }
19491858f18SMatthias Ringwald break;
19591858f18SMatthias Ringwald default:
19691858f18SMatthias Ringwald break;
19791858f18SMatthias Ringwald }
19891858f18SMatthias Ringwald }
19994a5538cSMatthias Ringwald
trigger_shutdown(void)200f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){
201f11fd9a9SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n");
202f11fd9a9SMatthias Ringwald log_info("sigint_handler: shutting down");
203f11fd9a9SMatthias Ringwald shutdown_triggered = true;
204f11fd9a9SMatthias Ringwald hci_power_control(HCI_POWER_OFF);
205f11fd9a9SMatthias Ringwald }
206f11fd9a9SMatthias Ringwald
main(int argc,const char * argv[])20794a5538cSMatthias Ringwald int main(int argc, const char * argv[]){
20894a5538cSMatthias Ringwald
20994a5538cSMatthias Ringwald /// GET STARTED with BTstack ///
21094a5538cSMatthias Ringwald btstack_memory_init();
21194a5538cSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance());
21294a5538cSMatthias Ringwald
2137435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format
214785879c6SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg";
2157435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
2167435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
2177435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl);
218785879c6SMatthias Ringwald printf("Packet Log: %s\n", pklg_path);
21994a5538cSMatthias Ringwald
22094a5538cSMatthias Ringwald // pick serial port
2216f122aa3SMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT
222cf524f2bSMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A50285BI"; // BOOST-CC2564MODA New
223cf524f2bSMatthias Ringwald // config.device_name = "/dev/tty.usbserial-A9OVNX5P"; // RedBear IoT pHAT breakout board
2246f122aa3SMatthias Ringwald config.device_name = "/dev/tty.usbserial-A900K0VK"; // CSR8811 breakout board
22594a5538cSMatthias Ringwald
226aa981441SMatthias Ringwald // accept path from command line
227aa981441SMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){
228aa981441SMatthias Ringwald config.device_name = argv[2];
229aa981441SMatthias Ringwald argc -= 2;
2300033ffe4SMatthias Ringwald memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
231aa981441SMatthias Ringwald }
232aa981441SMatthias Ringwald printf("H5 device: %s\n", config.device_name);
233aa981441SMatthias Ringwald
23494a5538cSMatthias Ringwald // init HCI
23579530e37SMatthias Ringwald const btstack_uart_t * uart_driver = (const btstack_uart_t *) btstack_uart_posix_instance();
23679530e37SMatthias Ringwald const hci_transport_t * transport = hci_transport_h5_instance(uart_driver);
23794a5538cSMatthias Ringwald hci_init(transport, (void*) &config);
23894a5538cSMatthias Ringwald
23977b95b41SMatthias Ringwald // set BD_ADDR for CSR without Flash/unique address
24077b95b41SMatthias Ringwald // bd_addr_t own_address = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
24177b95b41SMatthias Ringwald // btstack_chipset_csr_set_bd_addr(own_address);
24277b95b41SMatthias Ringwald
2434b871b9eSMatthias Ringwald // enable auto-sleep mode
2444b871b9eSMatthias Ringwald // hci_transport_h5_set_auto_sleep(300);
2454b871b9eSMatthias Ringwald
24694a5538cSMatthias Ringwald // register for HCI events
24794a5538cSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
24894a5538cSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
24994a5538cSMatthias Ringwald
250f11fd9a9SMatthias Ringwald // register callback for CTRL-c
251f11fd9a9SMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown);
25294a5538cSMatthias Ringwald
25394a5538cSMatthias Ringwald // setup app
25494a5538cSMatthias Ringwald btstack_main(argc, argv);
25594a5538cSMatthias Ringwald
25694a5538cSMatthias Ringwald // go
25794a5538cSMatthias Ringwald btstack_run_loop_execute();
25894a5538cSMatthias Ringwald
25994a5538cSMatthias Ringwald return 0;
26094a5538cSMatthias Ringwald }
261