text
stringlengths
0
1.47k
f.feed(join(acl[3:], sco))
check(t, first, evt)
}
// Garbage before a packet is skipped and counted, and framing recovers.
func TestResyncsAfterGarbage(t *testing.T) {
var f h4Framer
check(t, f.feed(join([]byte{0x00, 0xff}, evt)), evt)
if f.dropped != 2 {
t.Errorf("dropped %d bytes, want 2", f.dropped)
}
}
// A phone's P-256 link key reaches the kernel as the P-192 kind, authenticated or not as it was, and
// nothing else is touched: other key types, other events, a short packet.
func TestP256KeysArePassedOnAsP192(t *testing.T) {
note := func(kind byte) []byte {
p := []byte{h4Event, 0x18, 0x17}
p = append(p, make([]byte, 6+16)...)
return append(p, kind)
}
for _, c := range []struct{ in, want byte }{{8, 5}, {7, 4}, {5, 5}, {4, 4}, {0, 0}, {6, 6}} {
p := note(c.in)
changed := p192Key(p)
if p[25] != c.want || changed != (c.in != c.want) {
t.Errorf("type %d: became %d, changed %v; want %d", c.in, p[25], changed, c.want)
}
}
other := note(8)
other[1] = 0x0e
if p192Key(other) || other[25] != 8 {
t.Error("a packet that is not a Link Key Notification was changed")
}
if p192Key(note(8)[:20]) {
t.Error("a short packet was changed")
}
}
# ===== file: cmd/btbridge/main.go =====
//go:build linux
// btbridge makes the MediaTek Bluetooth driver's raw H4 channel (/dev/stpbt,
// from the vendor mt76x8_bt module) look like a Linux HCI device by copying
// packets both ways through the kernel's virtual HCI driver (/dev/vhci,
// CONFIG_BT_HCIVHCI). BlueZ then sees hci0 like on any other board.
//
// btbridge [-stp /dev/stpbt] [-vhci /dev/vhci]
// btbridge -uart /dev/ttyMT1 -hcd <patch.hcd> [-baud 3000000] (a Broadcom controller; brcm.go)
//
// Each read on either side returns one whole H4 packet (type byte first);
// each write must be one whole packet. The vendor driver's read returns 0
// bytes when its queue is empty instead of blocking, so that side is polled
// with select(2) and a short timeout, and it is never asked for more than it
// will give (read.go). A read that fails waits before the next one; anything
// else ends the bridge after a pause, so init's respawn does not spin.
package main
import (
"flag"
"fmt"
"os"
"strings"
"syscall"
"time"
"golang.org/x/sys/unix"
)
const vendorPkt = 0xff // HCI_VENDOR_PKT: vhci control packets, never forwarded
func main() {
stpPath := flag.String("stp", "/dev/stpbt", "vendor driver's H4 character device")
vhciPath := flag.String("vhci", "/dev/vhci", "kernel virtual HCI device")
bdaddr := flag.String("bdaddr", "idme", "public address to give the controller first: 12 hex digits, "+
"\"idme\" for the factory one from /proc/idme/bt_mac_addr, \"\" to leave the firmware's")
uart := flag.String("uart", "", "a Broadcom controller on this tty instead of -stp (brcm.go)")
hcd := flag.String("hcd", "", "with -uart: the firmware patch (.hcd) to load")
baud := flag.Int("baud", 3000000, "with -uart: the speed to run the UART at once patched")
maxPage := flag.Int("max-feature-page", -1, "report at most this extended features page to the kernel "+
"(1 on the Echo Dot, whose controller refuses page 2 after claiming it); -1 leaves replies alone")
p192 := flag.Bool("p192-keys", false, "report a phone's P-256 link keys to the kernel as the P-192 kind "+
"(the Echo Show, whose controller encrypts them with E0; h4.go p192Key)")
flag.Parse()
if *bdaddr == "idme" {
b, err := os.ReadFile("/proc/idme/bt_mac_addr")
if err != nil {
fmt.Fprintf(os.Stderr, "btbridge: no factory address: %v\n", err)
}
*bdaddr = strings.TrimRight(strings.TrimSpace(string(b)), "\x00")
}
var stp int
if *uart != "" {
fd, err := brcmInit(*uart, *hcd, *baud, *bdaddr)
if err != nil {
die("%s: %v", *uart, err)
}
stp = fd
} else {
fd, err := syscall.Open(*stpPath, syscall.O_RDWR, 0)