xref: /btstack/port/libusb/main.c (revision 9ec2630ce4bfd36adcb438d2e8ba6fc26890f857)
18caefee3SMatthias Ringwald /*
28caefee3SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
38caefee3SMatthias Ringwald  *
48caefee3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
58caefee3SMatthias Ringwald  * modification, are permitted provided that the following conditions
68caefee3SMatthias Ringwald  * are met:
78caefee3SMatthias Ringwald  *
88caefee3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
98caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
108caefee3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
118caefee3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
128caefee3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
138caefee3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
148caefee3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
158caefee3SMatthias Ringwald  *    from this software without specific prior written permission.
168caefee3SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
178caefee3SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
188caefee3SMatthias Ringwald  *    monetary gain.
198caefee3SMatthias Ringwald  *
208caefee3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
218caefee3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
228caefee3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
238caefee3SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
248caefee3SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
258caefee3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
268caefee3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
278caefee3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
288caefee3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
298caefee3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
308caefee3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318caefee3SMatthias Ringwald  * SUCH DAMAGE.
328caefee3SMatthias Ringwald  *
338caefee3SMatthias Ringwald  * Please inquire about commercial licensing options at
348caefee3SMatthias Ringwald  * [email protected]
358caefee3SMatthias Ringwald  *
368caefee3SMatthias Ringwald  */
378caefee3SMatthias Ringwald 
388caefee3SMatthias Ringwald // *****************************************************************************
398caefee3SMatthias Ringwald //
408caefee3SMatthias Ringwald // minimal setup for HCI code
418caefee3SMatthias Ringwald //
428caefee3SMatthias Ringwald // *****************************************************************************
438caefee3SMatthias Ringwald 
448caefee3SMatthias Ringwald #include <stdint.h>
458caefee3SMatthias Ringwald #include <stdio.h>
468caefee3SMatthias Ringwald #include <stdlib.h>
478caefee3SMatthias Ringwald #include <string.h>
488caefee3SMatthias Ringwald #include <signal.h>
498caefee3SMatthias Ringwald 
507907f069SMatthias Ringwald #include "btstack_config.h"
518caefee3SMatthias Ringwald 
52a98592bcSMatthias Ringwald #include "btstack_debug.h"
53d356a6daSMatthias Ringwald #include "btstack_event.h"
54a98592bcSMatthias Ringwald #include "btstack_link_key_db_fs.h"
55a98592bcSMatthias Ringwald #include "btstack_memory.h"
5682636622SMatthias Ringwald #include "btstack_run_loop.h"
57b7d596c1SMatthias Ringwald #include "btstack_run_loop_posix.h"
588caefee3SMatthias Ringwald #include "hal_led.h"
598caefee3SMatthias Ringwald #include "hci.h"
608caefee3SMatthias Ringwald #include "hci_dump.h"
618caefee3SMatthias Ringwald #include "stdin_support.h"
628caefee3SMatthias Ringwald 
638caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
648caefee3SMatthias Ringwald 
65d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
66d356a6daSMatthias Ringwald 
67d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
68*9ec2630cSMatthias Ringwald     UNUSED(channel);
69*9ec2630cSMatthias Ringwald     UNUSED(size);
70d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
71d356a6daSMatthias Ringwald     if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
72d356a6daSMatthias Ringwald     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
73d356a6daSMatthias Ringwald     printf("BTstack up and running.\n");
74d356a6daSMatthias Ringwald }
75d356a6daSMatthias Ringwald 
768caefee3SMatthias Ringwald static void sigint_handler(int param){
77*9ec2630cSMatthias Ringwald     UNUSED(param);
788caefee3SMatthias Ringwald 
798caefee3SMatthias Ringwald #ifndef _WIN32
808caefee3SMatthias Ringwald     // reset anyway
818caefee3SMatthias Ringwald     btstack_stdin_reset();
828caefee3SMatthias Ringwald #endif
838caefee3SMatthias Ringwald 
848caefee3SMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
858caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
868caefee3SMatthias Ringwald     hci_close();
878caefee3SMatthias Ringwald     log_info("Good bye, see you.\n");
888caefee3SMatthias Ringwald     exit(0);
898caefee3SMatthias Ringwald }
908caefee3SMatthias Ringwald 
918caefee3SMatthias Ringwald static int led_state = 0;
928caefee3SMatthias Ringwald void hal_led_toggle(void){
938caefee3SMatthias Ringwald     led_state = 1 - led_state;
948caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
958caefee3SMatthias Ringwald }
968caefee3SMatthias Ringwald 
97d356a6daSMatthias Ringwald 
98e13e7f54SMatthias Ringwald #define USB_MAX_PATH_LEN 7
998caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
1008caefee3SMatthias Ringwald 
101e13e7f54SMatthias Ringwald     uint8_t usb_path[USB_MAX_PATH_LEN];
102e13e7f54SMatthias Ringwald     int usb_path_len = 0;
103e13e7f54SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
104e13e7f54SMatthias Ringwald         // parse command line options for "-u 11:22:33"
105e13e7f54SMatthias Ringwald         const char * port_str = argv[2];
106e13e7f54SMatthias Ringwald         printf("Specified USB Path: ");
107e13e7f54SMatthias Ringwald         while (1){
108e13e7f54SMatthias Ringwald             char * delimiter;
109e13e7f54SMatthias Ringwald             int port = strtol(port_str, &delimiter, 16);
110e13e7f54SMatthias Ringwald             usb_path[usb_path_len] = port;
111e13e7f54SMatthias Ringwald             usb_path_len++;
112e13e7f54SMatthias Ringwald             printf("%02x ", port);
113e13e7f54SMatthias Ringwald             if (!delimiter) break;
114a4c4a5d6SMatthias Ringwald             if (*delimiter != ':' && *delimiter != '-') break;
115e13e7f54SMatthias Ringwald             port_str = delimiter+1;
116e13e7f54SMatthias Ringwald         }
117e13e7f54SMatthias Ringwald         printf("\n");
118e13e7f54SMatthias Ringwald     }
119e13e7f54SMatthias Ringwald 
1208caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
1218caefee3SMatthias Ringwald 	btstack_memory_init();
122528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
1238caefee3SMatthias Ringwald 
124e13e7f54SMatthias Ringwald     if (usb_path_len){
125e13e7f54SMatthias Ringwald         hci_transport_usb_set_path(usb_path_len, usb_path);
126e13e7f54SMatthias Ringwald     }
127e13e7f54SMatthias Ringwald 
1288caefee3SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
12902065453SMatthias Ringwald 
13002065453SMatthias Ringwald     char pklg_path[100];
13102065453SMatthias Ringwald     strcpy(pklg_path, "/tmp/hci_dump");
13202065453SMatthias Ringwald     if (usb_path_len){
13302065453SMatthias Ringwald         strcat(pklg_path, "_");
13402065453SMatthias Ringwald         strcat(pklg_path, argv[2]);
13502065453SMatthias Ringwald     }
13602065453SMatthias Ringwald     strcat(pklg_path, ".pklg");
13702065453SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
13802065453SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
1398caefee3SMatthias Ringwald 
1408caefee3SMatthias Ringwald     // init HCI
1412d5e09d6SMatthias Ringwald 	hci_init(hci_transport_usb_instance(), NULL);
14235454696SMatthias Ringwald 
14335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
1442d5e09d6SMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_fs_instance());
14535454696SMatthias Ringwald #endif
1468caefee3SMatthias Ringwald 
147d356a6daSMatthias Ringwald     // inform about BTstack state
148d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
149d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
150d356a6daSMatthias Ringwald 
1518caefee3SMatthias Ringwald     // handle CTRL-c
1528caefee3SMatthias Ringwald     signal(SIGINT, sigint_handler);
1538caefee3SMatthias Ringwald 
1548caefee3SMatthias Ringwald     // setup app
1558caefee3SMatthias Ringwald     btstack_main(argc, argv);
1568caefee3SMatthias Ringwald 
1578caefee3SMatthias Ringwald     // go
158528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
1598caefee3SMatthias Ringwald 
1608caefee3SMatthias Ringwald     return 0;
1618caefee3SMatthias Ringwald }
162