From 3f77f2c60ca80ea9189f85193dbd746e9a6034a9 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 5 Jun 2020 15:41:08 +1000 Subject: [PATCH] unix/btstack_usb: Allow choosing adaptor via environment variable. This allows running (for example): env MICROPYBTUSB=2-2 ./micropython-dev ../../examples/bluetooth/ble_temperature_central.py --- ports/unix/btstack_usb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ports/unix/btstack_usb.c b/ports/unix/btstack_usb.c index da9d72fe1..76f32d6d2 100644 --- a/ports/unix/btstack_usb.c +++ b/ports/unix/btstack_usb.c @@ -110,7 +110,23 @@ void mp_bluetooth_btstack_port_init(void) { btstack_run_loop_embedded_get_instance()->init(); } - // TODO: allow setting USB device path via cmdline/env var. + // MICROPYBTUSB can be a ':'' or '-' separated port list. + char *path = getenv("MICROPYBTUSB"); + if (path != NULL) { + uint8_t usb_path[7] = {0}; + size_t usb_path_len = 0; + + while (usb_path_len < MP_ARRAY_SIZE(usb_path)) { + char *delimiter; + usb_path[usb_path_len++] = strtol(path, &delimiter, 16); + if (!delimiter || (*delimiter != ':' && *delimiter != '-')) { + break; + } + path = delimiter + 1; + } + + hci_transport_usb_set_path(usb_path_len, usb_path); + } // hci_dump_open(NULL, HCI_DUMP_STDOUT); hci_init(hci_transport_usb_instance(), NULL);