-
Notifications
You must be signed in to change notification settings - Fork 1
/
metausb.go
121 lines (105 loc) · 3.53 KB
/
metausb.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2017 John Scherff
//
// 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 service
import (
`time`
`github.com/jscherff/cmdb/meta/peripheral`
)
// MetaUsbSvc is an interface that creates serial numbers from seed values.
type MetaUsbSvc interface {
VendorName(vid string) (string, error)
ProductName(vid, pid string) (string, error)
ClassDesc(cid string) (string, error)
SubClassDesc(cid, sid string) (string, error)
ProtocolDesc(cid, sid, pid string) (string, error)
LastUpdate() (time.Time)
Raw() *peripheral.Usb
Refresh() (error)
Save() (error)
}
// metaUsbSvc is a service that implements the MetaUsbSvc interface.
type metaUsbSvc struct {
*peripheral.Usb
ConfigFile string
}
// NewMetaUsbSvc returns an object that implements the MetaUsbSvc interface.
func NewMetaUsbSvc(cf string) (MetaUsbSvc, error) {
var this *metaUsbSvc
if usb, err := peripheral.NewUsb(cf); err != nil {
return nil, err
} else {
this = &metaUsbSvc{Usb: usb, ConfigFile: cf}
}
return this, nil
}
// VendorName returns the name of the vendor given the vendor ID.
func (this *metaUsbSvc) VendorName(vid string) (string, error) {
if vendor, err := this.GetVendor(vid); err != nil {
return ``, err
} else {
return vendor.String(), nil
}
}
// ProductName returns the name of the product given the vendor and product ID.
func (this *metaUsbSvc) ProductName(vid, pid string) (string, error) {
if vendor, err := this.GetVendor(vid); err != nil {
return ``, err
} else if product, err := vendor.GetProduct(pid); err != nil {
return ``, err
} else {
return product.String(), nil
}
}
// ClassDesc returns the class description for the given class ID.
func (this *metaUsbSvc) ClassDesc(cid string) (string, error) {
if class, err := this.GetClass(cid); err != nil {
return ``, err
} else {
return class.String(), nil
}
}
// SubClassDesc returns the subclass description for the given class and subclass ID.
func (this *metaUsbSvc) SubClassDesc(cid, sid string) (string, error) {
if class, err := this.GetClass(cid); err != nil {
return ``, err
} else if subClass, err := class.GetSubClass(sid); err != nil {
return ``, err
} else {
return subClass.String(), nil
}
}
// ProtocolDesc returns the protocol description for the given class, subclass, and protocol ID.
func (this *metaUsbSvc) ProtocolDesc(cid, sid, pid string) (string, error) {
if class, err := this.GetClass(cid); err != nil {
return ``, err
} else if subClass, err := class.GetSubClass(sid); err != nil {
return ``, err
} else if protocol, err := subClass.GetProtocol(pid); err != nil {
return ``, err
} else {
return protocol.String(), nil
}
}
// LastUpdate returns the date the metadata was last updated from source.
func (this *metaUsbSvc) LastUpdate() (time.Time) {
return this.Usb.LastUpdate()
}
// Save saves a copy of the in-memory metadata to the configuration file.
func (this *metaUsbSvc) Save() (error) {
return this.Usb.Save(this.ConfigFile)
}
// Raw returns the raw underlying metadata object.
func (this *metaUsbSvc) Raw() (*peripheral.Usb) {
return this.Usb
}