xref: /btstack/port/posix-h4-atwilc3000/main.c (revision a15efc861737d1da360dc5169bcf7780f5e7e649)
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"
65*a15efc86SMatthias Ringwald // #include "wilc3000_bt_firmware.h"
66*a15efc86SMatthias Ringwald #include "wilc3000_ble_firmware.h"
67ccebdf93SMatthias Ringwald 
68ccebdf93SMatthias Ringwald static int main_argc;
69ccebdf93SMatthias Ringwald static const char ** main_argv;
70ccebdf93SMatthias Ringwald static const btstack_uart_block_t * uart_driver;
71ccebdf93SMatthias Ringwald static btstack_uart_config_t uart_config;
72ccebdf93SMatthias Ringwald 
73ccebdf93SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
74ccebdf93SMatthias Ringwald 
75ccebdf93SMatthias Ringwald static hci_transport_config_uart_t transport_config = {
76ccebdf93SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
776e0d5aedSMatthias Ringwald     921600,
786e0d5aedSMatthias Ringwald     0,       // main baudrate
79f14824f7SMatthias Ringwald     1,       // flow control
80ccebdf93SMatthias Ringwald     NULL,
81ccebdf93SMatthias Ringwald };
82ccebdf93SMatthias Ringwald 
83ccebdf93SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
84ccebdf93SMatthias Ringwald 
85ccebdf93SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
86ccebdf93SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
87ccebdf93SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
88ccebdf93SMatthias Ringwald         case BTSTACK_EVENT_STATE:
89ccebdf93SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
90ccebdf93SMatthias Ringwald             printf("BTstack up and running.\n");
91ccebdf93SMatthias Ringwald             break;
92ccebdf93SMatthias Ringwald         default:
93ccebdf93SMatthias Ringwald             break;
94ccebdf93SMatthias Ringwald     }
95ccebdf93SMatthias Ringwald }
96ccebdf93SMatthias Ringwald 
97ccebdf93SMatthias Ringwald static void sigint_handler(int param){
98ccebdf93SMatthias Ringwald     UNUSED(param);
99ccebdf93SMatthias Ringwald 
100ccebdf93SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
101ccebdf93SMatthias Ringwald     log_info("sigint_handler: shutting down");
102ccebdf93SMatthias Ringwald 
103ccebdf93SMatthias Ringwald     // reset anyway
104ccebdf93SMatthias Ringwald     btstack_stdin_reset();
105ccebdf93SMatthias Ringwald 
106ccebdf93SMatthias Ringwald     // power down
107ccebdf93SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
108ccebdf93SMatthias Ringwald     hci_close();
109ccebdf93SMatthias Ringwald     log_info("Good bye, see you.\n");
110ccebdf93SMatthias Ringwald     exit(0);
111ccebdf93SMatthias Ringwald }
112ccebdf93SMatthias Ringwald 
113ccebdf93SMatthias Ringwald static int led_state = 0;
114ccebdf93SMatthias Ringwald void hal_led_toggle(void){
115ccebdf93SMatthias Ringwald     led_state = 1 - led_state;
116ccebdf93SMatthias Ringwald     printf("LED State %u\n", led_state);
117ccebdf93SMatthias Ringwald }
118ccebdf93SMatthias Ringwald 
119ccebdf93SMatthias Ringwald static void phase2(int status){
120ccebdf93SMatthias Ringwald 
121ccebdf93SMatthias Ringwald     if (status){
122ccebdf93SMatthias Ringwald         printf("Download firmware failed\n");
123ccebdf93SMatthias Ringwald         return;
124ccebdf93SMatthias Ringwald     }
125ccebdf93SMatthias Ringwald 
126ccebdf93SMatthias Ringwald     printf("Phase 2: Main app\n");
127ccebdf93SMatthias Ringwald 
128ccebdf93SMatthias Ringwald     // init HCI
129ccebdf93SMatthias Ringwald     const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
130ccebdf93SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
131ccebdf93SMatthias Ringwald     hci_init(transport, (void*) &transport_config);
1329df38542SMatthias Ringwald     hci_set_chipset(btstack_chipset_atwilc3000_instance());
133ccebdf93SMatthias Ringwald     hci_set_link_key_db(link_key_db);
134ccebdf93SMatthias Ringwald 
135ccebdf93SMatthias Ringwald     // inform about BTstack state
136ccebdf93SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
137ccebdf93SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
138ccebdf93SMatthias Ringwald 
139ccebdf93SMatthias Ringwald     // handle CTRL-c
140ccebdf93SMatthias Ringwald     signal(SIGINT, sigint_handler);
141ccebdf93SMatthias Ringwald 
142ccebdf93SMatthias Ringwald     // setup app
143ccebdf93SMatthias Ringwald     btstack_main(main_argc, main_argv);
144ccebdf93SMatthias Ringwald }
145ccebdf93SMatthias Ringwald 
146ccebdf93SMatthias Ringwald 
147ccebdf93SMatthias Ringwald int main(int argc, const char * argv[]){
148ccebdf93SMatthias Ringwald 
149ccebdf93SMatthias Ringwald 	/// GET STARTED with BTstack ///
150ccebdf93SMatthias Ringwald 	btstack_memory_init();
151ccebdf93SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
152ccebdf93SMatthias Ringwald 
153ccebdf93SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
154f14824f7SMatthias Ringwald     hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
155ccebdf93SMatthias Ringwald 
156ccebdf93SMatthias Ringwald     // pick serial port and configure uart block driver
157ccebdf93SMatthias Ringwald     transport_config.device_name = "/dev/tty.usbserial-A96PXBJ7";
158ccebdf93SMatthias Ringwald     uart_driver = btstack_uart_block_posix_instance();
159ccebdf93SMatthias Ringwald 
1606e0d5aedSMatthias Ringwald     // extract UART config from transport config, but disable flow control and use default baudrate
1616e0d5aedSMatthias Ringwald     uart_config.baudrate    = HCI_DEFAULT_BAUDRATE;
162ccebdf93SMatthias Ringwald     uart_config.flowcontrol = 0;
163ccebdf93SMatthias Ringwald     uart_config.device_name = transport_config.device_name;
164ccebdf93SMatthias Ringwald     uart_driver->init(&uart_config);
165ccebdf93SMatthias Ringwald 
166ccebdf93SMatthias Ringwald     main_argc = argc;
167ccebdf93SMatthias Ringwald     main_argv = argv;
168ccebdf93SMatthias Ringwald 
169ccebdf93SMatthias Ringwald     // phase #1 download firmware
170ccebdf93SMatthias Ringwald     printf("Phase 1: Download firmware\n");
171ccebdf93SMatthias Ringwald 
172ccebdf93SMatthias Ringwald     // phase #2 start main app
173*a15efc86SMatthias Ringwald     btstack_chipset_atwilc3000_download_firmware(uart_driver, transport_config.baudrate_init, transport_config.flowcontrol, (const uint8_t *) firmware_ble, sizeof(firmware_ble), &phase2);
174ccebdf93SMatthias Ringwald 
175ccebdf93SMatthias Ringwald     // go
176ccebdf93SMatthias Ringwald     btstack_run_loop_execute();
177ccebdf93SMatthias Ringwald     return 0;
178ccebdf93SMatthias Ringwald }
179