Do not store pointers

This commit is contained in:
timoxa0 2024-06-17 23:47:02 +05:00
parent 4b21608884
commit 8ad17d7f8a

View file

@ -30,11 +30,14 @@ var Error = struct {
}
type FastbootDevice struct {
Serial string
Device *gousb.Device
Serial string
Bus int
Address int
}
func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) {
func FindDevices() ([]FastbootDevice, error) {
ctx := gousb.NewContext()
defer ctx.Close()
var fastbootDevices []FastbootDevice
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
for _, cfg := range desc.Configs {
@ -51,19 +54,27 @@ func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) {
return nil, err
}
defer func() {
for _, d := range devs {
d.Close()
}
}()
for _, dev := range devs {
serial, err := dev.SerialNumber()
if err != nil {
continue
}
fastbootDevices = append(fastbootDevices, FastbootDevice{Serial: serial, Device: dev})
fastbootDevices = append(fastbootDevices, FastbootDevice{Serial: serial, Bus: dev.Desc.Bus, Address: dev.Desc.Address})
}
return fastbootDevices, nil
}
func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
func FindDevice(serial string) (FastbootDevice, error) {
var fastbootDevice FastbootDevice
ctx := gousb.NewContext()
defer ctx.Close()
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
for _, cfg := range desc.Configs {
for _, ifc := range cfg.Interfaces {
@ -79,6 +90,12 @@ func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
return fastbootDevice, err
}
defer func() {
for _, d := range devs {
d.Close()
}
}()
for _, dev := range devs {
serialNumber, err := dev.SerialNumber()
if err != nil {
@ -87,14 +104,20 @@ func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
if serial != serialNumber {
continue
}
return FastbootDevice{Serial: serial, Device: dev}, nil
return FastbootDevice{Serial: serial, Bus: dev.Desc.Bus, Address: dev.Desc.Address}, nil
}
return fastbootDevice, Error.DeviceNotFound
}
func (d *FastbootDevice) Send(data []byte) error {
intf, done, err := d.Device.DefaultInterface()
ctx := gousb.NewContext()
devs, _ := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
return desc.Bus == d.Bus && desc.Address == d.Address
})
defer devs[0].Close()
defer ctx.Close()
intf, done, err := devs[0].DefaultInterface()
if err != nil {
return nil
}
@ -110,7 +133,13 @@ func (d *FastbootDevice) Send(data []byte) error {
}
func (d *FastbootDevice) GetMaxPacketSize() (int, error) {
intf, done, err := d.Device.DefaultInterface()
ctx := gousb.NewContext()
devs, _ := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
return desc.Bus == d.Bus && desc.Address == d.Address
})
defer devs[0].Close()
defer ctx.Close()
intf, done, err := devs[0].DefaultInterface()
if err != nil {
return 0, err
}
@ -125,7 +154,13 @@ func (d *FastbootDevice) GetMaxPacketSize() (int, error) {
}
func (d *FastbootDevice) Recv() (FastbootResponseStatus, []byte, error) {
intf, done, err := d.Device.DefaultInterface()
ctx := gousb.NewContext()
devs, _ := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
return desc.Bus == d.Bus && desc.Address == d.Address
})
defer devs[0].Close()
defer ctx.Close()
intf, done, err := devs[0].DefaultInterface()
if err != nil {
return Status.FAIL, nil, err
}