From c0cf066900de5c2b8727f8065c5a30dfd2f37054 Mon Sep 17 00:00:00 2001 From: Piotr Dymacz Date: Sun, 12 Mar 2023 20:40:41 +0100 Subject: [PATCH] mcu-firmware: include host support for 'hci_uart' firmware This includes shell script for host side support of the 'hci_uart' MCU firmware type. The script calls 'btattach' with matching tty interface and baud rate as arguments, resulting in new Bluetooth HCI controller registration in the system. Both UART and USB interfaces are supported. Signed-off-by: Piotr Dymacz --- feeds/mcu/mcu-firmware/Makefile | 2 +- .../files/hci_uart/etc/mcu.d/hci_uart.sh | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100755 feeds/mcu/mcu-firmware/files/hci_uart/etc/mcu.d/hci_uart.sh diff --git a/feeds/mcu/mcu-firmware/Makefile b/feeds/mcu/mcu-firmware/Makefile index f66eb19a1..eaa702305 100644 --- a/feeds/mcu/mcu-firmware/Makefile +++ b/feeds/mcu/mcu-firmware/Makefile @@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=mcu-firmware -PKG_VERSION:=2023-03-10 +PKG_VERSION:=2023-03-12 PKG_RELEASE:=1 PKG_MAINTAINER:=Piotr Dymacz diff --git a/feeds/mcu/mcu-firmware/files/hci_uart/etc/mcu.d/hci_uart.sh b/feeds/mcu/mcu-firmware/files/hci_uart/etc/mcu.d/hci_uart.sh new file mode 100755 index 000000000..48a722bb8 --- /dev/null +++ b/feeds/mcu/mcu-firmware/files/hci_uart/etc/mcu.d/hci_uart.sh @@ -0,0 +1,79 @@ +#!/bin/sh + +. /lib/functions.sh +. /lib/functions/mcu.sh + +attach_hci_controller() { + local section="$1" + + local sn + local pid + local interface + local uart_path + local uart_baud + local uart_flow + + [ -n "$section" ] || return 1 + command -v btattach > /dev/null 2>&1 || return 1 + + config_load mcu + config_get sn "$section" sn + config_get interface "$section" interface + config_get uart_path "$section" uart_path + config_get uart_baud "$section" uart_baud "115200" + config_get uart_flow "$section" uart_flow "0" + + [ -n "$sn" ] || return 1 + + if [ "$interface" = "usb" ]; then + uart_baud="1000000" + uart_flow="1" + + dev_found="" + usb_path="/sys/bus/usb/devices/*" + for dev_path in $usb_path; do + dev="$(basename "$dev_path")" + [[ $dev == *":"* ]] && continue + + [ "$sn" = "$(cat "${dev_path}/serial" 2>/dev/null)" ] && { + dev_found="$dev" + break + } + done + + [ -n "$dev_found" ] || return 1 + + usb_path="/sys/bus/usb/devices/${dev_found}*/tty/*" + for tty_path in $usb_path; do + tty="$(basename "$tty_path")" + [ -c "/dev/${tty}" ] && { + uart_path="/dev/${tty}" + break + } + done + fi + + [ -c "$uart_path" ] || return 1 + + # Give MCU some time for BLE controller setup + sleep 1 + + if [ "$uart_flow" = "1" ]; then + btattach -B "$uart_path" -S "$uart_baud" > /dev/null 2>&1 & + else + btattach -B "$uart_path" -S "$uart_baud" -N > /dev/null 2>&1 & + fi + + pid="$!" + + kill -0 "$pid" > /dev/null 2>&1 && { + echo "$pid" > "/var/run/mcu.${sn}.pid" + return 0 + } + + return 1 +} + +attach_hci_controller "$1" || exit 1 + +exit 0