Ejemplo n.º 1
0
// FindDevices finds likely MTP devices without opening them.
func FindDevices(c *usb.Context) ([]*Device, error) {
	l, err := c.GetDeviceList()
	if err != nil {
		return nil, err
	}

	var cands []*Device
	for _, d := range l {
		dd, err := d.GetDeviceDescriptor()
		if err != nil {
			continue
		}

		for i := byte(0); i < dd.NumConfigurations; i++ {
			cdecs, err := d.GetConfigDescriptor(i)
			if err != nil {
				return nil, fmt.Errorf("GetConfigDescriptor %d: %v", i, err)
			}
			for _, iface := range cdecs.Interfaces {
				for _, a := range iface.AltSetting {
					if len(a.EndPoints) != 3 {
						continue
					}

					m := Device{}
					for _, s := range a.EndPoints {
						switch {
						case s.Direction() == usb.ENDPOINT_IN && s.TransferType() == usb.TRANSFER_TYPE_INTERRUPT:
							m.eventEp = s.EndpointAddress
						case s.Direction() == usb.ENDPOINT_IN && s.TransferType() == usb.TRANSFER_TYPE_BULK:
							m.fetchEp = s.EndpointAddress
						case s.Direction() == usb.ENDPOINT_OUT && s.TransferType() == usb.TRANSFER_TYPE_BULK:
							m.sendEp = s.EndpointAddress
						}
					}
					if m.sendEp > 0 && m.fetchEp > 0 && m.eventEp > 0 {
						m.devDescr = *dd
						m.ifaceDescr = a
						m.dev = d.Ref()
						m.configIndex = i
						cands = append(cands, &m)
					}
				}
			}
		}
	}
	l.Done()

	return cands, nil
}
Ejemplo n.º 2
0
// FindDevices finds likely MTP devices without opening them.
func FindDevices(c *usb.Context) ([]*Device, error) {
	l, err := c.GetDeviceList()
	if err != nil {
		return nil, err
	}

	var cands []*Device
	for _, d := range l {
		cand := candidateFromDeviceDescriptor(d)
		if cand != nil {
			cands = append(cands, cand)
		}
	}
	l.Done()

	return cands, nil
}