text
stringlengths
0
1.47k
found := false
dirs, _ := filepath.Glob("/sys/class/rfkill/rfkill*")
for _, d := range dirs {
typ, _ := os.ReadFile(filepath.Join(d, "type"))
if strings.TrimSpace(string(typ)) != "bluetooth" {
continue
}
found = true
if err := os.WriteFile(filepath.Join(d, "state"), []byte("1"), 0); err != nil {
// Newer kernels take "soft" instead.
_ = os.WriteFile(filepath.Join(d, "soft"), []byte("0"), 0)
}
}
return found
}
// brcmInit brings a Broadcom UART controller from power-on to a patched, fast, addressed H4 device.
func brcmInit(path, hcd string, baud int, bdaddr string) (int, error) {
if rfkillPower() {
fmt.Fprintln(os.Stderr, "btbridge: radio powered through rfkill")
time.Sleep(300 * time.Millisecond)
}
fd, err := openUART(path, 115200)
if err != nil {
return -1, fmt.Errorf("open %s: %w", path, err)
}
fail := func(err error) (int, error) {
syscall.Close(fd)
return -1, err
}
c := &hciConn{fd: fd}
if _, err := c.command(opReset, nil, 3*time.Second); err != nil {
return fail(fmt.Errorf("reset: %w", err))
}
if hcd != "" {
patch, err := os.ReadFile(hcd)
if err != nil {
return fail(err)
}
start := time.Now()
if err := c.patchram(patch); err != nil {
return fail(fmt.Errorf("patch %s: %w", filepath.Base(hcd), err))
}
fmt.Fprintf(os.Stderr, "btbridge: firmware patch %s loaded in %v\n", filepath.Base(hcd), time.Since(start).Round(time.Millisecond))
// The controller restarts into the patch at its default speed.
time.Sleep(250 * time.Millisecond)
if err := setBaud(fd, 115200); err != nil {
return fail(err)
}
c.framer = h4Framer{}
if _, err := c.command(opReset, nil, 3*time.Second); err != nil {
return fail(fmt.Errorf("reset after the patch: %w", err))
}
}
if baud != 115200 {
p := make([]byte, 6)
binary.LittleEndian.PutUint32(p[2:], uint32(baud))
if _, err := c.command(opUpdateBaud, p, 2*time.Second); err != nil {
return fail(fmt.Errorf("baud %d: %w", baud, err))
}
time.Sleep(100 * time.Millisecond)
if err := setBaud(fd, baud); err != nil {
return fail(err)
}
c.framer = h4Framer{}
if _, err := c.command(opReset, nil, 3*time.Second); err != nil {
return fail(fmt.Errorf("reset at %d baud: %w", baud, err))
}
fmt.Fprintf(os.Stderr, "btbridge: UART at %d baud\n", baud)
}
if bdaddr != "" {
a, err := addressBytes(bdaddr)
if err != nil {
fmt.Fprintf(os.Stderr, "btbridge: address %q: %v (keeping the controller's own)\n", bdaddr, err)
} else if _, err := c.command(opWriteBdaddr, a, 2*time.Second); 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)
}
}
return fd, nil
}
// patchram sends the minidriver command and then each record of an .hcd file.
func (c *hciConn) patchram(patch []byte) error {
if _, err := c.command(opMinidriver, nil, 3*time.Second); err != nil {
return fmt.Errorf("download minidriver: %w", err)
}
time.Sleep(50 * time.Millisecond)
n := 0
for len(patch) > 0 {
if len(patch) < 3 || len(patch) < 3+int(patch[2]) {
return fmt.Errorf("record %d is cut short", n)
}
op := binary.LittleEndian.Uint16(patch)
params := patch[3 : 3+int(patch[2])]
patch = patch[3+int(patch[2]):]
if _, err := c.command(op, params, 3*time.Second); err != nil {
if op == opLaunchRAM {
// Some ROMs restart into the patch without answering Launch_RAM; the reset after it