aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/kylelemons/gousb/usbid/describe.go
blob: 1746a26eddb55b3494f4f75c0774924cb12f6505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)
}