Do not store pointers
This commit is contained in:
parent
4b21608884
commit
8ad17d7f8a
|
@ -31,10 +31,13 @@ var Error = struct {
|
||||||
|
|
||||||
type FastbootDevice struct {
|
type FastbootDevice struct {
|
||||||
Serial string
|
Serial string
|
||||||
Device *gousb.Device
|
Bus int
|
||||||
|
Address int
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) {
|
func FindDevices() ([]FastbootDevice, error) {
|
||||||
|
ctx := gousb.NewContext()
|
||||||
|
defer ctx.Close()
|
||||||
var fastbootDevices []FastbootDevice
|
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 {
|
||||||
|
@ -51,19 +54,27 @@ func FindDevices(ctx *gousb.Context) ([]FastbootDevice, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
for _, d := range devs {
|
||||||
|
d.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for _, dev := range devs {
|
for _, dev := range devs {
|
||||||
serial, err := dev.SerialNumber()
|
serial, err := dev.SerialNumber()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
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
|
return fastbootDevices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
|
func FindDevice(serial string) (FastbootDevice, error) {
|
||||||
var fastbootDevice FastbootDevice
|
var fastbootDevice FastbootDevice
|
||||||
|
ctx := gousb.NewContext()
|
||||||
|
defer ctx.Close()
|
||||||
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 {
|
||||||
|
@ -79,6 +90,12 @@ func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
|
||||||
return fastbootDevice, err
|
return fastbootDevice, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
for _, d := range devs {
|
||||||
|
d.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for _, dev := range devs {
|
for _, dev := range devs {
|
||||||
serialNumber, err := dev.SerialNumber()
|
serialNumber, err := dev.SerialNumber()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -87,14 +104,20 @@ func FindDevice(ctx *gousb.Context, serial string) (FastbootDevice, error) {
|
||||||
if serial != serialNumber {
|
if serial != serialNumber {
|
||||||
continue
|
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
|
return fastbootDevice, Error.DeviceNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *FastbootDevice) Send(data []byte) error {
|
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 {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -110,7 +133,13 @@ func (d *FastbootDevice) Send(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *FastbootDevice) GetMaxPacketSize() (int, 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 {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -125,7 +154,13 @@ func (d *FastbootDevice) GetMaxPacketSize() (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *FastbootDevice) Recv() (FastbootResponseStatus, []byte, 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 {
|
if err != nil {
|
||||||
return Status.FAIL, nil, err
|
return Status.FAIL, nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue