text
stringlengths
0
1.47k
}
}
// wait blocks until the fd is readable or 50 ms pass, whichever comes first,
// so a driver without a working poll still gets serviced promptly.
func wait(fd int) {
_ = selectRead(fd, 50*time.Millisecond)
}
// selectRead is select(2) on one descriptor for reading. FdSet.Set picks the word by the platform's
// own width (32-bit words on arm, 64-bit on arm64 and amd64), where indexing Bits by fd/32 was
// right only on 32-bit builds.
func selectRead(fd int, d time.Duration) bool {
var set unix.FdSet
set.Set(fd)
tv := unix.NsecToTimeval(d.Nanoseconds())
n, err := unix.Select(fd+1, &set, nil, nil, &tv)
return err == nil && n > 0
}
// fixSupportedCommands fills in the LE part of the controller's Read Local
// Supported Commands reply. The MT7668 firmware leaves octets 25-28 empty
// although it runs every command they stand for; the kernel builds the LE
// event mask from that table, so without them it never asks for advertising
// reports or LE connection events and every LE scan comes back empty. Only a
// reply with all four octets zero is touched.
func fixSupportedCommands(p []byte) {
// 04 0E plen ncmd 02 10 status, then 64 octets of commands
const first = 7
if len(p) < first+64 || p[0] != 0x04 || p[1] != 0x0e || p[4] != 0x02 || p[5] != 0x10 || p[6] != 0 {
return
}
le := p[first+25 : first+29]
if le[0]|le[1]|le[2]|le[3] != 0 {
return
}
// The Bluetooth 4.0 LE commands: octet 25 has a reserved bit 3, octet 28 ends at bit 6.
copy(le, []byte{0xf7, 0xff, 0xff, 0x7f})
}
// setBdaddr sends MediaTek's vendor Set_BD_ADDR command (opcode 0xFC1A, six
// address bytes little-endian) straight to the controller before the kernel
// stack sees it, so hci0 comes up with the factory address rather than the
// firmware's default. Waits up to two seconds for the Command Complete.
func setBdaddr(stp int, hexaddr string) error {
if len(hexaddr) != 12 {
return fmt.Errorf("want 12 hex digits")
}
cmd := []byte{0x01, 0x1a, 0xfc, 0x06}
for i := 5; i >= 0; i-- { // little-endian on the wire
var v byte
if _, err := fmt.Sscanf(hexaddr[2*i:2*i+2], "%02x", &v); err != nil {
return fmt.Errorf("bad address")
}
cmd = append(cmd, v)
}
if err := writeAll(stp, cmd); err != nil {
return err
}
buf := make([]byte, readSize)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
n, err := syscall.Read(stp, buf)
if err != nil && err != syscall.EINTR && err != syscall.EAGAIN {
return err
}
if n == 0 {
wait(stp)
continue
}
// Command Complete for 0xFC1A: 04 0E len ncmd 1A FC status
if n >= 7 && buf[0] == 0x04 && buf[1] == 0x0e && buf[4] == 0x1a && buf[5] == 0xfc {
if buf[6] != 0 {
return fmt.Errorf("controller status 0x%02x", buf[6])
}
return nil
}
}
return fmt.Errorf("no reply")
}
func writeAll(fd int, b []byte) error {
full := 0
for len(b) > 0 {
n, err := syscall.Write(fd, b)
if err == syscall.EINTR {
continue
}
// The Dot's driver answers ENOSPC while its transmit queue is full: wait
// for it to drain rather than tearing the bridge down (up to a second).
if err == syscall.ENOSPC && full < 100 {
full++
time.Sleep(10 * time.Millisecond)
continue
}
if err != nil {
return err
}
b = b[n:]
}