text stringlengths 0 1.47k |
|---|
if err != nil { |
die("open %s: %v", *stpPath, err) |
} |
stp = fd |
} |
if *bdaddr != "" && *uart == "" { |
if err := setBdaddr(stp, *bdaddr); err != nil { |
fmt.Fprintf(os.Stderr, "btbridge: set address %s: %v (keeping the controller's own)\n", *bdaddr, err) |
} else { |
fmt.Fprintf(os.Stderr, "btbridge: controller address set to %s\n", *bdaddr) |
} |
} |
vhci, err := os.OpenFile(*vhciPath, os.O_RDWR, 0) |
if err != nil { |
die("open %s: %v", *vhciPath, err) |
} |
// Register a primary (BR/EDR + LE) controller at once instead of waiting |
// for the driver's one-second timeout. |
if _, err := vhci.Write([]byte{vendorPkt, 0x00}); err != nil { |
die("vhci: create device: %v", err) |
} |
fmt.Fprintln(os.Stderr, "btbridge: hci device registered") |
errc := make(chan error, 2) |
go func() { errc <- vhciToStp(vhci, stp) }() |
go func() { errc <- stpToVhci(stp, vhci, *maxPage, *p192) }() |
die("%v", <-errc) |
} |
// vhciToStp forwards packets the kernel stack sends (vhci reads block). |
func vhciToStp(vhci *os.File, stp int) error { |
buf := make([]byte, 65536) |
for { |
n, err := vhci.Read(buf) |
if err != nil { |
return fmt.Errorf("vhci: read: %w", err) |
} |
if n == 0 || buf[0] == vendorPkt { |
continue // vhci's "device created" notice and the like |
} |
if err := writeAll(stp, buf[:n]); err != nil { |
return fmt.Errorf("stp: write %d bytes: %w", n, err) |
} |
} |
} |
// stpToVhci forwards packets from the controller; waits with select when the |
// driver has nothing queued. What a read returns is framed into whole packets |
// first (h4.go): the Echo Dot's driver does not keep packet boundaries. |
func stpToVhci(stp int, vhci *os.File, maxPage int, p192 bool) error { |
buf := make([]byte, readSize) |
var framer h4Framer |
var runs backoff |
dropped := 0 |
for { |
n, err := syscall.Read(stp, buf) |
if err == syscall.EINTR { |
continue |
} |
if err == syscall.EAGAIN { |
// Nothing queued, said the other way round: the same as a read of no bytes. |
wait(stp) |
continue |
} |
if err != nil { |
// Not fatal on its own — a driver refusing this read may take the next — but asking again |
// at once is what filled a Dot's kernel log and kept its cores awake, so this waits. |
pause, report, giveUp := runs.fail(err) |
if giveUp { |
return fmt.Errorf("stp: read: %w (%d times in a row)", err, giveUpAfter) |
} |
if report { |
fmt.Fprintf(os.Stderr, "btbridge: stp: read: %v (waiting; said once for the run)\n", err) |
} |
time.Sleep(pause) |
continue |
} |
if refused := runs.ok(); refused > 0 { |
fmt.Fprintf(os.Stderr, "btbridge: stp: reading again after %d refused\n", refused) |
} |
if n == 0 { |
wait(stp) |
continue |
} |
for _, pkt := range framer.feed(buf[:n]) { |
fixSupportedCommands(pkt) |
if maxPage >= 0 && capFeaturePages(pkt, byte(maxPage)) { |
fmt.Fprintln(os.Stderr, "btbridge: extended features capped at page", maxPage) |
} |
if p192 && p192Key(pkt) { |
fmt.Fprintln(os.Stderr, "btbridge: a new link key passed on as the P-192 kind") |
} |
if _, err := vhci.Write(pkt); err != nil { |
return fmt.Errorf("vhci: write %d bytes: %w", len(pkt), err) |
} |
} |
if framer.dropped != dropped { |
fmt.Fprintf(os.Stderr, "btbridge: skipped %d bytes that began no packet\n", framer.dropped-dropped) |
dropped = framer.dropped |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.