xref: /btstack/port/posix-h4-atwilc3000/main.c (revision 9df3854281ed2a27d34cd863be7d6743c1e066ce)
1ccebdf93SMatthias Ringwald /*
2ccebdf93SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3ccebdf93SMatthias Ringwald  *
4ccebdf93SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5ccebdf93SMatthias Ringwald  * modification, are permitted provided that the following conditions
6ccebdf93SMatthias Ringwald  * are met:
7ccebdf93SMatthias Ringwald  *
8ccebdf93SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9ccebdf93SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10ccebdf93SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11ccebdf93SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12ccebdf93SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13ccebdf93SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14ccebdf93SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15ccebdf93SMatthias Ringwald  *    from this software without specific prior written permission.
16ccebdf93SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17ccebdf93SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18ccebdf93SMatthias Ringwald  *    monetary gain.
19ccebdf93SMatthias Ringwald  *
20ccebdf93SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21ccebdf93SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ccebdf93SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23ccebdf93SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24ccebdf93SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25ccebdf93SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26ccebdf93SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27ccebdf93SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28ccebdf93SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29ccebdf93SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30ccebdf93SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ccebdf93SMatthias Ringwald  * SUCH DAMAGE.
32ccebdf93SMatthias Ringwald  *
33ccebdf93SMatthias Ringwald  * Please inquire about commercial licensing options at
34ccebdf93SMatthias Ringwald  * [email protected]
35ccebdf93SMatthias Ringwald  *
36ccebdf93SMatthias Ringwald  */
37ccebdf93SMatthias Ringwald 
38ccebdf93SMatthias Ringwald #define __BTSTACK_FILE__ "main.c"
39ccebdf93SMatthias Ringwald 
40ccebdf93SMatthias Ringwald // *****************************************************************************
41ccebdf93SMatthias Ringwald //
42ccebdf93SMatthias Ringwald // minimal setup for HCI code
43ccebdf93SMatthias Ringwald //
44ccebdf93SMatthias Ringwald // *****************************************************************************
45ccebdf93SMatthias Ringwald 
46ccebdf93SMatthias Ringwald #include <stdint.h>
47ccebdf93SMatthias Ringwald #include <stdio.h>
48ccebdf93SMatthias Ringwald #include <stdlib.h>
49ccebdf93SMatthias Ringwald #include <string.h>
50ccebdf93SMatthias Ringwald #include <signal.h>
51ccebdf93SMatthias Ringwald 
52ccebdf93SMatthias Ringwald #include "btstack_config.h"
53ccebdf93SMatthias Ringwald 
54ccebdf93SMatthias Ringwald #include "btstack_debug.h"
55ccebdf93SMatthias Ringwald #include "btstack_event.h"
56ccebdf93SMatthias Ringwald #include "btstack_link_key_db_fs.h"
57ccebdf93SMatthias Ringwald #include "btstack_memory.h"
58ccebdf93SMatthias Ringwald #include "btstack_run_loop.h"
59ccebdf93SMatthias Ringwald #include "btstack_run_loop_posix.h"
60ccebdf93SMatthias Ringwald #include "hci.h"
61ccebdf93SMatthias Ringwald #include "hci_dump.h"
62ccebdf93SMatthias Ringwald #include "btstack_stdin.h"
63ccebdf93SMatthias Ringwald 
64ccebdf93SMatthias Ringwald #include "btstack_chipset_atwilc3000.h"
65ccebdf93SMatthias Ringwald #include "wilc3000_bt_firmware.h"
66ccebdf93SMatthias Ringwald 
67ccebdf93SMatthias Ringwald static int main_argc;
68ccebdf93SMatthias Ringwald static const char ** main_argv;
69ccebdf93SMatthias Ringwald static const btstack_uart_block_t * uart_driver;
70ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config;
71ccebdf93SMatthias Ringwald 
72ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
73ccebdf93SMatthias Ringwald 
74ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = {
75ccebdf93SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
76ccebdf93SMatthias Ringwald     115200,
77*9df38542SMatthias Ringwald     921600,  // main baudrate
78ccebdf93SMatthias Ringwald     0,  // flow control
79ccebdf93SMatthias Ringwald     NULL,
80ccebdf93SMatthias Ringwald };
81ccebdf93SMatthias Ringwald 
82ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
83ccebdf93SMatthias Ringwald 
84ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
85ccebdf93SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
86ccebdf93SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
87ccebdf93SMatthias Ringwald         case BTSTACK_EVENT_STATE:
88ccebdf93SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
89ccebdf93SMatthias Ringwald             printf("BTstack up and running.\n");
90ccebdf93SMatthias Ringwald             break;
91ccebdf93SMatthias Ringwald         default:
92ccebdf93SMatthias Ringwald             break;
93ccebdf93SMatthias Ringwald     }
94ccebdf93SMatthias Ringwald }
95ccebdf93SMatthias Ringwald 
96ccebdf93SMatthias Ringwald static void sigint_handler(int param){
97ccebdf93SMatthias Ringwald     UNUSED(param);
98ccebdf93SMatthias Ringwald 
99ccebdf93SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
100ccebdf93SMatthias Ringwald     log_info("sigint_handler: shutting down");
101ccebdf93SMatthias Ringwald 
102ccebdf93SMatthias Ringwald     // reset anyway
103ccebdf93SMatthias Ringwald     btstack_stdin_reset();
104ccebdf93SMatthias Ringwald 
105ccebdf93SMatthias Ringwald     // power down
106ccebdf93SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
107ccebdf93SMatthias Ringwald     hci_close();
108ccebdf93SMatthias Ringwald     log_info("Good bye, see you.\n");
109ccebdf93SMatthias Ringwald     exit(0);
110ccebdf93SMatthias Ringwald }
111ccebdf93SMatthias Ringwald 
112ccebdf93SMatthias Ringwald static int led_state = 0;
113ccebdf93SMatthias Ringwald void hal_led_toggle(void){
114ccebdf93SMatthias Ringwald     led_state = 1 - led_state;
115ccebdf93SMatthias Ringwald     printf("LED State %u\n", led_state);
116ccebdf93SMatthias Ringwald }
117ccebdf93SMatthias Ringwald 
118ccebdf93SMatthias Ringwald static void phase2(int status){
119ccebdf93SMatthias Ringwald 
120ccebdf93SMatthias Ringwald     if (status){
121ccebdf93SMatthias Ringwald         printf("Download firmware failed\n");
122ccebdf93SMatthias Ringwald         return;
123ccebdf93SMatthias Ringwald     }
124ccebdf93SMatthias Ringwald 
125ccebdf93SMatthias Ringwald     printf("Phase 2: Main app\n");
126ccebdf93SMatthias Ringwald 
127ccebdf93SMatthias Ringwald     // init HCI
128ccebdf93SMatthias Ringwald     const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
129ccebdf93SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
130ccebdf93SMatthias Ringwald     hci_init(transport, (void*) &transport_config);
131*9df38542SMatthias Ringwald     hci_set_chipset(btstack_chipset_atwilc3000_instance());
132ccebdf93SMatthias Ringwald     hci_set_link_key_db(link_key_db);
133ccebdf93SMatthias Ringwald 
134ccebdf93SMatthias Ringwald     // inform about BTstack state
135ccebdf93SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
136ccebdf93SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
137ccebdf93SMatthias Ringwald 
138ccebdf93SMatthias Ringwald     // handle CTRL-c
139ccebdf93SMatthias Ringwald     signal(SIGINT, sigint_handler);
140ccebdf93SMatthias Ringwald 
141ccebdf93SMatthias Ringwald     // setup app
142ccebdf93SMatthias Ringwald     btstack_main(main_argc, main_argv);
143ccebdf93SMatthias Ringwald }
144ccebdf93SMatthias Ringwald 
145ccebdf93SMatthias Ringwald 
146ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){
147ccebdf93SMatthias Ringwald 
148ccebdf93SMatthias Ringwald 	/// GET STARTED with BTstack ///
149ccebdf93SMatthias Ringwald 	btstack_memory_init();
150ccebdf93SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
151ccebdf93SMatthias Ringwald 
152ccebdf93SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
153ccebdf93SMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_STDOUT);
154ccebdf93SMatthias Ringwald 
155ccebdf93SMatthias Ringwald     // pick serial port and configure uart block driver
156ccebdf93SMatthias Ringwald     transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7";
157ccebdf93SMatthias Ringwald     uart_driver = btstack_uart_block_posix_instance();
158ccebdf93SMatthias Ringwald 
159ccebdf93SMatthias Ringwald     // extract UART config from transport config, but overide initial uart speed
160ccebdf93SMatthias Ringwald     uart_config.baudrate    = 115200;
161ccebdf93SMatthias Ringwald     uart_config.flowcontrol = 0;
162ccebdf93SMatthias Ringwald     uart_config.device_name = transport_config.device_name;
163ccebdf93SMatthias Ringwald     uart_driver->init(&uart_config);
164ccebdf93SMatthias Ringwald 
165ccebdf93SMatthias Ringwald     main_argc = argc;
166ccebdf93SMatthias Ringwald     main_argv = argv;
167ccebdf93SMatthias Ringwald 
168ccebdf93SMatthias Ringwald     // phase #1 download firmware
169ccebdf93SMatthias Ringwald     printf("Phase 1: Download firmware\n");
170ccebdf93SMatthias Ringwald 
171ccebdf93SMatthias Ringwald     // phase #2 start main app
172c7d3427cSMatthias Ringwald     btstack_chipset_atwilc3000_download_firmware(uart_driver, 921600, atwilc3000_fw_data, atwilc3000_fw_size, &phase2);
173ccebdf93SMatthias Ringwald     // phase2(0);
174ccebdf93SMatthias Ringwald 
175ccebdf93SMatthias Ringwald     // go
176ccebdf93SMatthias Ringwald     btstack_run_loop_execute();
177ccebdf93SMatthias Ringwald     return 0;
178ccebdf93SMatthias Ringwald }
179