From 500caaeda74dd9c660279036293f4b2997cf0b03 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 9 Sep 2017 09:42:37 +0200 Subject: Add vendor --- .../github.com/kylelemons/gousb/usbid/describe.go | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 vendor/github.com/kylelemons/gousb/usbid/describe.go (limited to 'vendor/github.com/kylelemons/gousb/usbid/describe.go') diff --git a/vendor/github.com/kylelemons/gousb/usbid/describe.go b/vendor/github.com/kylelemons/gousb/usbid/describe.go new file mode 100644 index 0000000..1746a26 --- /dev/null +++ b/vendor/github.com/kylelemons/gousb/usbid/describe.go @@ -0,0 +1,79 @@ +// Copyright 2013 Google Inc. All rights reserved. +// Copyright 2016 the gousb Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package usbid provides human-readable text output for the usb package. +// +// On load, the usbid package parses an embedded mapping of vendors/products +// and class/subclass/protocols. They can also be loaded from a URL or from +// a reader. +// +// The bread and butter of this package are the following two functions: +// Describe - Pretty-print the vendor and product of a device descriptor +// Classify - Pretty-print the class/protocol info for a device/interface +package usbid + +import ( + "fmt" + + "github.com/kylelemons/gousb/usb" +) + +// Describe returns a human readable string describing the vendor and product +// of the given device. +// +// The given val must be one of the following: +// - *usb.Descriptor "Product (Vendor)" +func Describe(val interface{}) string { + switch val := val.(type) { + case *usb.Descriptor: + if v, ok := Vendors[val.Vendor]; ok { + if d, ok := v.Product[val.Product]; ok { + return fmt.Sprintf("%s (%s)", d, v) + } + return fmt.Sprintf("Unknown (%s)", v) + } + return fmt.Sprintf("Unknown %s:%s", val.Vendor, val.Product) + } + return fmt.Sprintf("Unknown (%T)", val) +} + +// Classify returns a human-readable string describing the class, subclass, +// and protocol associated with a device or interface. +// +// The given val must be one of the following: +// - *usb.Descriptor "Class (SubClass) Protocol" +// - usb.InterfaceSetup "IfClass (IfSubClass) IfProtocol" +func Classify(val interface{}) string { + var class, sub, proto uint8 + switch val := val.(type) { + case *usb.Descriptor: + class, sub, proto = val.Class, val.SubClass, val.Protocol + case usb.InterfaceSetup: + class, sub, proto = val.IfClass, val.IfSubClass, val.IfProtocol + default: + return fmt.Sprintf("Unknown (%T)", val) + } + + if c, ok := Classes[class]; ok { + if s, ok := c.SubClass[sub]; ok { + if p, ok := s.Protocol[proto]; ok { + return fmt.Sprintf("%s (%s) %s", c, s, p) + } + return fmt.Sprintf("%s (%s)", c, s) + } + return fmt.Sprintf("%s", c) + } + return fmt.Sprintf("Unknown %s.%s.%s", class, sub, proto) +} -- cgit v1.2.3