Improve errors and fix nil pointer

This commit is contained in:
timoxa0 2024-06-21 17:27:31 +05:00
parent 4b21608884
commit 6a022539b0

View file

@ -26,16 +26,20 @@ var Error = struct {
DeviceNotFound error DeviceNotFound error
}{ }{
VarNotFound: errors.New("variable not found"), VarNotFound: errors.New("variable not found"),
DeviceNotFound: gousb.ErrorNotFound, DeviceNotFound: errors.New("device not found"),
} }
type FastbootDevice struct { type FastbootDevice struct {
Serial string Device *gousb.Device
Device *gousb.Device Context *gousb.Context
In *gousb.InEndpoint
Out *gousb.OutEndpoint
Unclaim func()
} }
func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) { func FindDevices() ([]*FastbootDevice, error) {
var fastbootDevices []FastbootDevice ctx := gousb.NewContext()
var fastbootDevices []*FastbootDevice
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool { devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
for _, cfg := range desc.Configs { for _, cfg := range desc.Configs {
for _, ifc := range cfg.Interfaces { for _, ifc := range cfg.Interfaces {
@ -44,101 +48,81 @@ func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) {
} }
} }
} }
return false return true
}) })
if err != nil { if err != nil && len(devs) == 0 {
return nil, err return nil, err
} }
for _, dev := range devs { for _, dev := range devs {
serial, err := dev.SerialNumber() intf, done, err := dev.DefaultInterface()
if err != nil { if err != nil {
continue continue
} }
fastbootDevices = append(fastbootDevices, FastbootDevice{Serial: serial, Device: dev}) inEndpoint, err := intf.InEndpoint(0x81)
if err != nil {
continue
}
outEndpoint, err := intf.OutEndpoint(0x01)
if err != nil {
continue
}
fastbootDevices = append(fastbootDevices, &FastbootDevice{
Device: dev,
Context: ctx,
In: inEndpoint,
Out: outEndpoint,
Unclaim: done,
})
} }
return fastbootDevices, nil return fastbootDevices, nil
} }
func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) { func FindDevice(serial string) (*FastbootDevice, error) {
var fastbootDevice FastbootDevice devs, err := FindDevices()
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
for _, cfg := range desc.Configs {
for _, ifc := range cfg.Interfaces {
for _, alt := range ifc.AltSettings {
return alt.Protocol == 0x03 && alt.Class == 0xff && alt.SubClass == 0x42
}
}
}
return false
})
if err != nil { if err != nil {
return fastbootDevice, err return &FastbootDevice{}, err
} }
for _, dev := range devs { for _, dev := range devs {
serialNumber, err := dev.SerialNumber() s, e := dev.Device.SerialNumber()
if err != nil { if e != nil {
continue continue
} }
if serial != serialNumber { if serial != s {
continue continue
} }
return FastbootDevice{Serial: serial, Device: dev}, nil return dev, nil
} }
return fastbootDevice, Error.DeviceNotFound return &FastbootDevice{}, Error.DeviceNotFound
}
func (d *FastbootDevice) Close() {
d.Unclaim()
d.Device.Close()
d.Context.Close()
} }
func (d *FastbootDevice) Send(data []byte) error { func (d *FastbootDevice) Send(data []byte) error {
intf, done, err := d.Device.DefaultInterface() _, err := d.Out.Write(data)
if err != nil {
return nil
}
defer done()
outEndpoint, err := intf.OutEndpoint(0x01)
if err != nil {
return nil
}
_, err = outEndpoint.Write(data)
return err return err
} }
func (d *FastbootDevice) GetMaxPacketSize() (int, error) { func (d *FastbootDevice) GetMaxPacketSize() (int, error) {
intf, done, err := d.Device.DefaultInterface() return d.Out.Desc.MaxPacketSize, nil
if err != nil {
return 0, err
}
defer done()
outEndpoint, err := intf.OutEndpoint(0x01)
if err != nil {
return 0, err
}
return outEndpoint.Desc.MaxPacketSize, nil
} }
func (d *FastbootDevice) Recv() (FastbootResponseStatus, []byte, error) { func (d *FastbootDevice) Recv() (FastbootResponseStatus, []byte, error) {
intf, done, err := d.Device.DefaultInterface()
if err != nil {
return Status.FAIL, nil, err
}
defer done()
inEndpoint, err := intf.InEndpoint(0x81)
if err != nil {
return Status.FAIL, nil, err
}
var data []byte var data []byte
buf := make([]byte, inEndpoint.Desc.MaxPacketSize) buf := make([]byte, d.In.Desc.MaxPacketSize)
n, _ := inEndpoint.Read(buf) n, err := d.In.Read(buf)
if err != nil {
return Status.FAIL, []byte{}, err
}
data = append(data, buf[:n]...) data = append(data, buf[:n]...)
var status FastbootResponseStatus var status FastbootResponseStatus
switch string(data[:4]) { switch string(data[:4]) {
@ -154,8 +138,11 @@ func (d *FastbootDevice) Recv() (FastbootResponseStatus, []byte, error) {
return status, data[4:], nil return status, data[4:], nil
} }
func (d *FastbootDevice) GerVar(variable string) (string, error) { func (d *FastbootDevice) GetVar(variable string) (string, error) {
d.Send([]byte(fmt.Sprintf("getvar:%s", variable))) err := d.Send([]byte(fmt.Sprintf("getvar:%s", variable)))
if err != nil {
return "", err
}
status, resp, err := d.Recv() status, resp, err := d.Recv()
if status == Status.FAIL { if status == Status.FAIL {
err = Error.VarNotFound err = Error.VarNotFound