xref: /btstack/port/posix-h4/main.c (revision 8caefee39d444df6d8908a96a844825f10fbdaa4)
1*8caefee3SMatthias Ringwald /*
2*8caefee3SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*8caefee3SMatthias Ringwald  *
4*8caefee3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*8caefee3SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*8caefee3SMatthias Ringwald  * are met:
7*8caefee3SMatthias Ringwald  *
8*8caefee3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*8caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*8caefee3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*8caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*8caefee3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*8caefee3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*8caefee3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*8caefee3SMatthias Ringwald  *    from this software without specific prior written permission.
16*8caefee3SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*8caefee3SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*8caefee3SMatthias Ringwald  *    monetary gain.
19*8caefee3SMatthias Ringwald  *
20*8caefee3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*8caefee3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*8caefee3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*8caefee3SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*8caefee3SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*8caefee3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*8caefee3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*8caefee3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*8caefee3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*8caefee3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*8caefee3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*8caefee3SMatthias Ringwald  * SUCH DAMAGE.
32*8caefee3SMatthias Ringwald  *
33*8caefee3SMatthias Ringwald  * Please inquire about commercial licensing options at
34*8caefee3SMatthias Ringwald  * [email protected]
35*8caefee3SMatthias Ringwald  *
36*8caefee3SMatthias Ringwald  */
37*8caefee3SMatthias Ringwald 
38*8caefee3SMatthias Ringwald // *****************************************************************************
39*8caefee3SMatthias Ringwald //
40*8caefee3SMatthias Ringwald // minimal setup for HCI code
41*8caefee3SMatthias Ringwald //
42*8caefee3SMatthias Ringwald // *****************************************************************************
43*8caefee3SMatthias Ringwald 
44*8caefee3SMatthias Ringwald #include <stdint.h>
45*8caefee3SMatthias Ringwald #include <stdio.h>
46*8caefee3SMatthias Ringwald #include <stdlib.h>
47*8caefee3SMatthias Ringwald #include <string.h>
48*8caefee3SMatthias Ringwald #include <signal.h>
49*8caefee3SMatthias Ringwald 
50*8caefee3SMatthias Ringwald #include "btstack-config.h"
51*8caefee3SMatthias Ringwald 
52*8caefee3SMatthias Ringwald #include <btstack/run_loop.h>
53*8caefee3SMatthias Ringwald 
54*8caefee3SMatthias Ringwald #include "debug.h"
55*8caefee3SMatthias Ringwald #include "btstack_memory.h"
56*8caefee3SMatthias Ringwald #include "hci.h"
57*8caefee3SMatthias Ringwald #include "hci_dump.h"
58*8caefee3SMatthias Ringwald #include "stdin_support.h"
59*8caefee3SMatthias Ringwald 
60*8caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
61*8caefee3SMatthias Ringwald 
62*8caefee3SMatthias Ringwald static hci_uart_config_t hci_uart_config_generic = {
63*8caefee3SMatthias Ringwald     NULL,
64*8caefee3SMatthias Ringwald     115200,
65*8caefee3SMatthias Ringwald };
66*8caefee3SMatthias Ringwald 
67*8caefee3SMatthias Ringwald static void sigint_handler(int param){
68*8caefee3SMatthias Ringwald 
69*8caefee3SMatthias Ringwald #ifndef _WIN32
70*8caefee3SMatthias Ringwald     // reset anyway
71*8caefee3SMatthias Ringwald     btstack_stdin_reset();
72*8caefee3SMatthias Ringwald #endif
73*8caefee3SMatthias Ringwald 
74*8caefee3SMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
75*8caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
76*8caefee3SMatthias Ringwald     hci_close();
77*8caefee3SMatthias Ringwald     log_info("Good bye, see you.\n");
78*8caefee3SMatthias Ringwald     exit(0);
79*8caefee3SMatthias Ringwald }
80*8caefee3SMatthias Ringwald 
81*8caefee3SMatthias Ringwald static int led_state = 0;
82*8caefee3SMatthias Ringwald void hal_led_toggle(void){
83*8caefee3SMatthias Ringwald     led_state = 1 - led_state;
84*8caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
85*8caefee3SMatthias Ringwald }
86*8caefee3SMatthias Ringwald 
87*8caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
88*8caefee3SMatthias Ringwald 
89*8caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
90*8caefee3SMatthias Ringwald 	btstack_memory_init();
91*8caefee3SMatthias Ringwald     run_loop_init(RUN_LOOP_POSIX);
92*8caefee3SMatthias Ringwald 
93*8caefee3SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
94*8caefee3SMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
95*8caefee3SMatthias Ringwald 
96*8caefee3SMatthias Ringwald     // pick serial port
97*8caefee3SMatthias Ringwald     hci_uart_config_generic.device_name = "/dev/tty.usbmodem1413";
98*8caefee3SMatthias Ringwald 
99*8caefee3SMatthias Ringwald     // init HCI
100*8caefee3SMatthias Ringwald 	hci_transport_t    * transport = hci_transport_h4_instance();
101*8caefee3SMatthias Ringwald     remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_fs;
102*8caefee3SMatthias Ringwald 
103*8caefee3SMatthias Ringwald 	hci_init(transport, (void*) &hci_uart_config_generic, NULL, remote_db);
104*8caefee3SMatthias Ringwald 
105*8caefee3SMatthias Ringwald     // handle CTRL-c
106*8caefee3SMatthias Ringwald     signal(SIGINT, sigint_handler);
107*8caefee3SMatthias Ringwald 
108*8caefee3SMatthias Ringwald     // setup app
109*8caefee3SMatthias Ringwald     btstack_main(argc, argv);
110*8caefee3SMatthias Ringwald 
111*8caefee3SMatthias Ringwald     // go
112*8caefee3SMatthias Ringwald     run_loop_execute();
113*8caefee3SMatthias Ringwald 
114*8caefee3SMatthias Ringwald     return 0;
115*8caefee3SMatthias Ringwald }
116