xref: /btstack/port/posix-h4-zephyr/main.c (revision f11fd9a990fedbf11b7c70e38b9da44019506e13)
16807484cSMatthias Ringwald /*
26807484cSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
36807484cSMatthias Ringwald  *
46807484cSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
56807484cSMatthias Ringwald  * modification, are permitted provided that the following conditions
66807484cSMatthias Ringwald  * are met:
76807484cSMatthias Ringwald  *
86807484cSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
96807484cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
106807484cSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
116807484cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
126807484cSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
136807484cSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
146807484cSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
156807484cSMatthias Ringwald  *    from this software without specific prior written permission.
166807484cSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
176807484cSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
186807484cSMatthias Ringwald  *    monetary gain.
196807484cSMatthias Ringwald  *
206807484cSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
216807484cSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226807484cSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
236807484cSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
246807484cSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
256807484cSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
266807484cSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
276807484cSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
286807484cSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
296807484cSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
306807484cSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316807484cSMatthias Ringwald  * SUCH DAMAGE.
326807484cSMatthias Ringwald  *
336807484cSMatthias Ringwald  * Please inquire about commercial licensing options at
346807484cSMatthias Ringwald  * [email protected]
356807484cSMatthias Ringwald  *
366807484cSMatthias Ringwald  */
376807484cSMatthias Ringwald 
38793a0509SMatthias Ringwald #define BTSTACK_FILE__ "main.c"
396807484cSMatthias Ringwald 
406807484cSMatthias Ringwald // *****************************************************************************
416807484cSMatthias Ringwald //
426807484cSMatthias Ringwald // minimal setup for HCI code
436807484cSMatthias Ringwald //
446807484cSMatthias Ringwald // *****************************************************************************
456807484cSMatthias Ringwald 
466807484cSMatthias Ringwald #include <stdint.h>
476807484cSMatthias Ringwald #include <stdio.h>
486807484cSMatthias Ringwald #include <stdlib.h>
496807484cSMatthias Ringwald #include <string.h>
506807484cSMatthias Ringwald #include <signal.h>
516807484cSMatthias Ringwald 
526807484cSMatthias Ringwald #include "btstack_config.h"
536807484cSMatthias Ringwald 
54*f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h"
55*f11fd9a9SMatthias Ringwald #include "bluetooth_company_id.h"
56*f11fd9a9SMatthias Ringwald #include "btstack_chipset_zephyr.h"
576807484cSMatthias Ringwald #include "btstack_debug.h"
586807484cSMatthias Ringwald #include "btstack_event.h"
596807484cSMatthias Ringwald #include "btstack_memory.h"
606807484cSMatthias Ringwald #include "btstack_run_loop.h"
616807484cSMatthias Ringwald #include "btstack_run_loop_posix.h"
62*f11fd9a9SMatthias Ringwald #include "btstack_signal.h"
63*f11fd9a9SMatthias Ringwald #include "btstack_stdin.h"
64*f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h"
65793a0509SMatthias Ringwald #include "btstack_uart.h"
666807484cSMatthias Ringwald #include "hci.h"
676807484cSMatthias Ringwald #include "hci_dump.h"
687435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h"
69c8dfe071SMatthias Ringwald #include "hci_transport.h"
70c8dfe071SMatthias Ringwald #include "hci_transport_h4.h"
716807484cSMatthias Ringwald 
726807484cSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
736807484cSMatthias Ringwald 
747daa8bd9SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
757daa8bd9SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
767daa8bd9SMatthias Ringwald static char tlv_db_path[100];
777daa8bd9SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
787daa8bd9SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
79*f11fd9a9SMatthias Ringwald static bool shutdown_triggered;
807daa8bd9SMatthias Ringwald 
816807484cSMatthias Ringwald static hci_transport_config_uart_t config = {
826807484cSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
836807484cSMatthias Ringwald     1000000,
846807484cSMatthias Ringwald     0,  // main baudrate
856807484cSMatthias Ringwald     1,  // flow control
866807484cSMatthias Ringwald     NULL,
876807484cSMatthias Ringwald };
886807484cSMatthias Ringwald 
896807484cSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
906807484cSMatthias Ringwald 
91360243beSMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
92360243beSMatthias Ringwald static bd_addr_t static_address;
93360243beSMatthias Ringwald 
946807484cSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
956807484cSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
966807484cSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
976807484cSMatthias Ringwald         case BTSTACK_EVENT_STATE:
98*f11fd9a9SMatthias Ringwald             switch(btstack_event_state_get_state(packet)){
99*f11fd9a9SMatthias Ringwald                 case HCI_STATE_WORKING:
100360243beSMatthias Ringwald                     printf("BTstack up and running as %s\n",  bd_addr_to_str(static_address));
1017daa8bd9SMatthias Ringwald                     // setup TLV
1027daa8bd9SMatthias Ringwald                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
103dbf302f7SMatthias Ringwald                     strcat(tlv_db_path, bd_addr_to_str(static_address));
1047daa8bd9SMatthias Ringwald                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
1057daa8bd9SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
1067daa8bd9SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
10781862996SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
108360243beSMatthias Ringwald                     break;
109*f11fd9a9SMatthias Ringwald                 case HCI_STATE_OFF:
110*f11fd9a9SMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
111*f11fd9a9SMatthias Ringwald                     if (!shutdown_triggered) break;
112*f11fd9a9SMatthias Ringwald                     // reset stdin
113*f11fd9a9SMatthias Ringwald                     btstack_stdin_reset();
114*f11fd9a9SMatthias Ringwald                     log_info("Good bye, see you.\n");
115*f11fd9a9SMatthias Ringwald                     exit(0);
116*f11fd9a9SMatthias Ringwald                     break;
117*f11fd9a9SMatthias Ringwald                 default:
118*f11fd9a9SMatthias Ringwald                     break;
119*f11fd9a9SMatthias Ringwald             }
120*f11fd9a9SMatthias Ringwald             break;
121360243beSMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
122360243beSMatthias Ringwald             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
123360243beSMatthias Ringwald                 reverse_48(&packet[7], static_address);
124dbf302f7SMatthias Ringwald                 gap_random_address_set(static_address);
125360243beSMatthias Ringwald             }
1266807484cSMatthias Ringwald             break;
1276807484cSMatthias Ringwald         default:
1286807484cSMatthias Ringwald             break;
1296807484cSMatthias Ringwald     }
1306807484cSMatthias Ringwald }
1316807484cSMatthias Ringwald 
1326807484cSMatthias Ringwald 
133*f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){
1346807484cSMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
1356807484cSMatthias Ringwald     log_info("sigint_handler: shutting down");
136*f11fd9a9SMatthias Ringwald     shutdown_triggered = true;
1376807484cSMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
1386807484cSMatthias Ringwald }
1396807484cSMatthias Ringwald 
1406807484cSMatthias Ringwald static int led_state = 0;
1416807484cSMatthias Ringwald void hal_led_toggle(void){
1426807484cSMatthias Ringwald     led_state = 1 - led_state;
1436807484cSMatthias Ringwald     printf("LED State %u\n", led_state);
1446807484cSMatthias Ringwald }
1456807484cSMatthias Ringwald 
1466807484cSMatthias Ringwald int main(int argc, const char * argv[]){
1476807484cSMatthias Ringwald 
1486807484cSMatthias Ringwald 	/// GET STARTED with BTstack ///
1496807484cSMatthias Ringwald 	btstack_memory_init();
1506807484cSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
1516807484cSMatthias Ringwald 
1527435ec7bSMatthias Ringwald     // log into file using HCI_DUMP_PACKETLOGGER format
153785879c6SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
1547435ec7bSMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
1557435ec7bSMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
1567435ec7bSMatthias Ringwald     hci_dump_init(hci_dump_impl);
157785879c6SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
1586807484cSMatthias Ringwald 
1596807484cSMatthias Ringwald     // pick serial port
1602cc827d4SMatthias Ringwald     config.device_name = "/dev/tty.usbmodem0006830491191"; // PCA10056 nRF52840
1616807484cSMatthias Ringwald 
1626807484cSMatthias Ringwald     // accept path from command line
1636807484cSMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
1646807484cSMatthias Ringwald         config.device_name = argv[2];
1656807484cSMatthias Ringwald         argc -= 2;
166aa981441SMatthias Ringwald         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
1676807484cSMatthias Ringwald     }
1686807484cSMatthias Ringwald     printf("H4 device: %s\n", config.device_name);
1696807484cSMatthias Ringwald 
1706807484cSMatthias Ringwald     // init HCI
171793a0509SMatthias Ringwald     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
172793a0509SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
1736807484cSMatthias Ringwald 	hci_init(transport, (void*) &config);
174360243beSMatthias Ringwald     hci_set_chipset(btstack_chipset_zephyr_instance());
1756807484cSMatthias Ringwald 
1766807484cSMatthias Ringwald     // inform about BTstack state
1776807484cSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1786807484cSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1796807484cSMatthias Ringwald 
180*f11fd9a9SMatthias Ringwald     // register callback for CTRL-c
181*f11fd9a9SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
1826807484cSMatthias Ringwald 
1836807484cSMatthias Ringwald     // setup app
1846807484cSMatthias Ringwald     btstack_main(argc, argv);
1856807484cSMatthias Ringwald 
1862cc827d4SMatthias Ringwald     // sm required to setup static random Bluetooth address
1872cc827d4SMatthias Ringwald     sm_init();
1882cc827d4SMatthias Ringwald 
1896807484cSMatthias Ringwald     // go
1906807484cSMatthias Ringwald     btstack_run_loop_execute();
1916807484cSMatthias Ringwald 
1926807484cSMatthias Ringwald     return 0;
1936807484cSMatthias Ringwald }
194