mirror of
https://github.com/Fishwaldo/go-dcdc200.git
synced 2025-03-15 11:31:22 +00:00
replace logger with go-logr
This commit is contained in:
parent
cfe9aa0ae4
commit
835d3b965b
9 changed files with 114 additions and 56 deletions
12
cmd/main.go
12
cmd/main.go
|
@ -33,13 +33,15 @@ import (
|
|||
|
||||
"github.com/Fishwaldo/go-dcdc200"
|
||||
"github.com/Fishwaldo/go-dcdc200/internal/sim"
|
||||
"github.com/Fishwaldo/go-logadapter/loggers/std"
|
||||
"github.com/bombsimon/logrusr/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
var simulation = flag.Bool("s", false, "Enable Simulation Mode")
|
||||
var capture = flag.Bool("c", false, "Enable Packet Capture")
|
||||
var debug = flag.Bool("d", false, "Enable Debug Mode")
|
||||
var help = flag.Bool("h", false, "Help")
|
||||
flag.Parse()
|
||||
if *help {
|
||||
|
@ -54,8 +56,14 @@ func main() {
|
|||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
dc.Init(stdlogger.DefaultLogger(), *simulation)
|
||||
logsink := logrus.New()
|
||||
if *debug {
|
||||
logsink.SetLevel(logrus.TraceLevel)
|
||||
}
|
||||
|
||||
log := logrusr.New(logsink)
|
||||
dc.Init(log, *simulation)
|
||||
log.V(2).Info("Debug Enabled")
|
||||
if *capture {
|
||||
if *simulation {
|
||||
fmt.Printf("Can't Enable Capture in Simulation Mode\n")
|
||||
|
|
28
dcdcusb.go
28
dcdcusb.go
|
@ -34,7 +34,7 @@ import (
|
|||
"github.com/Fishwaldo/go-dcdc200/internal"
|
||||
"github.com/Fishwaldo/go-dcdc200/internal/realusb"
|
||||
"github.com/Fishwaldo/go-dcdc200/internal/sim"
|
||||
"github.com/Fishwaldo/go-logadapter"
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
type usbifI interface {
|
||||
|
@ -46,7 +46,7 @@ type usbifI interface {
|
|||
|
||||
// Main Structure for the DCDCUSB Communications
|
||||
type DcDcUSB struct {
|
||||
log logadapter.Logger
|
||||
log logr.Logger
|
||||
connected bool
|
||||
captureData bool
|
||||
simulation bool
|
||||
|
@ -124,16 +124,20 @@ type Params struct {
|
|||
}
|
||||
|
||||
// Initialize the DCDCUSB Communications. Should be first function called before any other methods are called
|
||||
// Pass a logadapter.Logger as the logger for this package and set simulation to true if you wish to reply a Captured Session instead of
|
||||
// Pass a logr.Logger as the logger for this package and set simulation to true if you wish to reply a Captured Session instead of
|
||||
// live data.
|
||||
func (dc *DcDcUSB) Init(log logadapter.Logger, simulation bool) {
|
||||
func (dc *DcDcUSB) Init(log logr.Logger, simulation bool) {
|
||||
dc.log = log
|
||||
dc.connected = false
|
||||
dc.simulation = simulation
|
||||
if !simulation {
|
||||
dc.log.Info("Enabling Real USB Mode")
|
||||
dc.usbif = realusb.Init(dc.log)
|
||||
} else {
|
||||
sim.SetCaptureFile("dcdcusb.cap")
|
||||
dc.log.Info("Enabling Simulation USB Mode")
|
||||
if err := sim.SetCaptureFile("dcdcusb.cap"); err != nil {
|
||||
dc.log.Error(err, "SetCaptureFile Failed", "file", "dcdcusb.cap")
|
||||
}
|
||||
dc.usbif = sim.Init(dc.log)
|
||||
}
|
||||
}
|
||||
|
@ -170,31 +174,31 @@ func (dc *DcDcUSB) Close() {
|
|||
func (dc *DcDcUSB) GetAllParam(ctx context.Context) (Params, error) {
|
||||
recv, len, err := dc.usbif.GetAllParam(ctx)
|
||||
if err != nil {
|
||||
dc.log.Warn("GetAllParams Call Failed: %s", err)
|
||||
dc.log.V(2).Info("GetAllParams Call Failed", "error", err)
|
||||
return Params{}, err
|
||||
}
|
||||
if len != 24 {
|
||||
dc.log.Warn("Got Short Read From USB")
|
||||
dc.log.Info("Got Short Read From USB", "len", len)
|
||||
return Params{}, fmt.Errorf("got Short Read from USB")
|
||||
}
|
||||
|
||||
if dc.captureData {
|
||||
if dc.simulation {
|
||||
dc.log.Warn("Running in Simulation Mode, Can't Capture")
|
||||
dc.log.Error(nil, "Running in Simulation Mode, Can't Capture")
|
||||
} else {
|
||||
f, err := os.OpenFile("dcdcusb.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
dc.log.Fatal("Can't open File for Capture: %s", err)
|
||||
dc.log.Error(err, "Can't open File for Capture")
|
||||
}
|
||||
|
||||
if _, err := f.Write(recv); err != nil {
|
||||
dc.log.Fatal("Can't write Text to File: %s", err)
|
||||
dc.log.Error(err, "Can't write Text to File")
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
|
||||
dc.log.Trace("Got %v", recv)
|
||||
dc.log.V(3).Info("Got Data", "data", recv)
|
||||
params, err := dc.parseAllValues(recv)
|
||||
return params, err
|
||||
}
|
||||
|
@ -229,7 +233,7 @@ func (dc *DcDcUSB) parseAllValues(buf []byte) (Params, error) {
|
|||
param.TimerPRWSW = dc.convertTime(buf[17:19])
|
||||
param.TimerSoftOff = dc.convertTime(buf[19:21])
|
||||
param.TimerHardOff = dc.convertTime(buf[21:23])
|
||||
dc.log.Trace("DCDC Params: %+v\n", param)
|
||||
dc.log.V(3).Info("DCDC Params", "params", param)
|
||||
return param, nil
|
||||
}
|
||||
return Params{}, errors.New("unknown command recieved")
|
||||
|
|
|
@ -27,13 +27,17 @@ package dcdcusb
|
|||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"github.com/Fishwaldo/go-logadapter/loggers/std"
|
||||
"log"
|
||||
"os"
|
||||
"github.com/go-logr/stdr"
|
||||
)
|
||||
|
||||
func TestParseAllValues(t *testing.T) {
|
||||
var test1 = []byte{130, 133, 7, 76, 75, 43, 27, 133, 215, 251, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 68, 0, 0, 167}
|
||||
dc := DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(), false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
logr := stdr.New(logsink)
|
||||
dc.Init(logr, false)
|
||||
result, err := dc.parseAllValues(test1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Returned from parseAllValues: %v", err)
|
||||
|
@ -109,7 +113,9 @@ func TestParseAllValues(t *testing.T) {
|
|||
func TestParseAllValues2(t *testing.T) {
|
||||
var test1 = []byte{130, 133, 8, 76, 0, 43, 27, 133, 205, 251, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 127, 0, 0, 167}
|
||||
dc := DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(), false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
logr := stdr.New(logsink)
|
||||
dc.Init(logr, false)
|
||||
result, err := dc.parseAllValues(test1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Returned from parseAllValues: %v", err)
|
||||
|
@ -185,7 +191,9 @@ func TestParseAllValues2(t *testing.T) {
|
|||
func TestParseAllValues3(t *testing.T) {
|
||||
var test1 = []byte{130, 133, 8, 76, 0, 44, 25, 133, 205, 251, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 167}
|
||||
dc := DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(), false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
logr := stdr.New(logsink)
|
||||
dc.Init(logr, false)
|
||||
result, err := dc.parseAllValues(test1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Returned from parseAllValues: %v", err)
|
||||
|
@ -260,7 +268,9 @@ func TestParseAllValues3(t *testing.T) {
|
|||
func TestParseAllValues4(t *testing.T) {
|
||||
var test1 = []byte{130, 133, 16, 76, 0, 44, 27, 133, 205, 247, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 167}
|
||||
dc := DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(),false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
logr := stdr.New(logsink)
|
||||
dc.Init(logr,false)
|
||||
result, err := dc.parseAllValues(test1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Returned from parseAllValues: %v", err)
|
||||
|
@ -336,7 +346,9 @@ func TestParseAllValues4(t *testing.T) {
|
|||
func TestParseAllValues5(t *testing.T) {
|
||||
var test1 = []byte{130, 133, 16, 76, 0, 44, 9, 133, 205, 247, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 167}
|
||||
dc := DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(), false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
logr := stdr.New(logsink)
|
||||
dc.Init(logr, false)
|
||||
result, err := dc.parseAllValues(test1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error Returned from parseAllValues: %v", err)
|
||||
|
|
|
@ -3,17 +3,21 @@ package dcdcusb_test
|
|||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Fishwaldo/go-dcdc200"
|
||||
"github.com/Fishwaldo/go-logadapter/loggers/std"
|
||||
"github.com/go-logr/stdr"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
dc := dcdcusb.DcDcUSB{}
|
||||
dc.Init(stdlogger.DefaultLogger(), false)
|
||||
logsink := log.New(os.Stdout, "", 0);
|
||||
log := stdr.New(logsink)
|
||||
|
||||
dc.Init(log, false)
|
||||
if ok, err := dc.Scan(); !ok {
|
||||
log.Fatalf("Scan Failed: %v", err)
|
||||
log.Error(err, "Scan Failed")
|
||||
return
|
||||
}
|
||||
defer dc.Close()
|
||||
|
|
4
go.mod
4
go.mod
|
@ -4,5 +4,9 @@ go 1.13
|
|||
|
||||
require (
|
||||
github.com/Fishwaldo/go-logadapter v0.0.2
|
||||
github.com/bombsimon/logrusr/v2 v2.0.1
|
||||
github.com/go-logr/logr v1.2.2
|
||||
github.com/go-logr/stdr v1.2.2
|
||||
github.com/google/gousb v2.1.0+incompatible
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
)
|
||||
|
|
23
go.sum
23
go.sum
|
@ -1,23 +1,36 @@
|
|||
github.com/Fishwaldo/go-logadapter v0.0.2 h1:RxFOr+bEDqQ1rPUmjUX5u8fGLCKY0Lyea+9AxDqzaW4=
|
||||
github.com/Fishwaldo/go-logadapter v0.0.2/go.mod h1:aRbQ8rWdpeD0WWo241ctqgk/yRto8Axg09EkwWiVGK0=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM=
|
||||
github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/google/gousb v2.1.0+incompatible h1:ApzMDjF3FeO219QwWybJxYfFhXQzPLOEy0o+w9k5DNI=
|
||||
github.com/google/gousb v2.1.0+incompatible/go.mod h1:Tl4HdAs1ThE3gECkNwz+1MWicX6FXddhJEw7L8jRDiI=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
|
||||
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
|
@ -43,6 +56,8 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
|
||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@ -56,9 +71,11 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
|
|||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/Fishwaldo/go-dcdc200/internal"
|
||||
"github.com/Fishwaldo/go-logadapter"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/google/gousb"
|
||||
)
|
||||
|
||||
|
@ -22,10 +22,10 @@ type UsbIF struct {
|
|||
dev *gousb.Device
|
||||
intf *gousb.Interface
|
||||
done func()
|
||||
log logadapter.Logger
|
||||
log logr.Logger
|
||||
}
|
||||
|
||||
func Init(log logadapter.Logger) (*UsbIF) {
|
||||
func Init(log logr.Logger) (*UsbIF) {
|
||||
usbif := UsbIF{log: log, ctx: gousb.NewContext()}
|
||||
return &usbif
|
||||
}
|
||||
|
@ -40,34 +40,34 @@ func (usbif *UsbIF) Scan() (bool, error) {
|
|||
var err error
|
||||
usbif.dev, err = usbif.ctx.OpenDeviceWithVIDPID(dcdc200_vid, dcdc200_pid)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Could Not Open Device: %v", err)
|
||||
usbif.log.Error(err, "Could Not Open Device")
|
||||
usbif.Close()
|
||||
return false, err
|
||||
}
|
||||
if usbif.dev == nil {
|
||||
usbif.log.Warn("Can't Find Device")
|
||||
usbif.log.Error(nil, "Can't Find Device")
|
||||
usbif.Close()
|
||||
return false, nil
|
||||
}
|
||||
err = usbif.dev.SetAutoDetach(true)
|
||||
if err != nil {
|
||||
usbif.log.Error("%s.SetAutoDetach(true): %v", usbif.dev, err)
|
||||
usbif.log.Error(err, "SetAutoDetach Failed", "usbif", usbif.dev)
|
||||
}
|
||||
|
||||
confignum, _ := usbif.dev.ActiveConfigNum()
|
||||
usbif.log.Trace("Device Config: %s %d", usbif.dev.String(), confignum)
|
||||
usbif.log.V(3).Info("Device Config", "Device", usbif.dev.String(), "config", confignum)
|
||||
// desc, _ := dc.dev.GetStringDescriptor(1)
|
||||
// manu, _ := dc.dev.Manufacturer()
|
||||
// prod, _ := dc.dev.Product()
|
||||
// serial, _ := dc.dev.SerialNumber()
|
||||
usbif.intf, usbif.done, err = usbif.dev.DefaultInterface()
|
||||
if err != nil {
|
||||
usbif.log.Error("%s.Interface(): %v", usbif.dev, err)
|
||||
usbif.log.Error(err, "Interface Failed", "Device", usbif.dev)
|
||||
usbif.Close()
|
||||
return false, err
|
||||
}
|
||||
usbif.log.Trace("Interface: %s", usbif.intf.String())
|
||||
usbif.log = usbif.log.With("Device", usbif.intf.String())
|
||||
usbif.log.V(3).Info("USB Interface Attached", "Interface", usbif.intf.String())
|
||||
usbif.log = usbif.log.WithName("Device " + usbif.intf.String())
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
@ -86,12 +86,12 @@ func (usbif *UsbIF) Close() {
|
|||
|
||||
func (usbif *UsbIF) GetAllParam(ctx context.Context) ([]byte, int, error) {
|
||||
if usbif.intf == nil {
|
||||
usbif.log.Warn("Interface Not Opened")
|
||||
usbif.log.Error(nil, "Interface Not Opened")
|
||||
return nil, 0, fmt.Errorf("interface Not Opened")
|
||||
}
|
||||
outp, err := usbif.intf.OutEndpoint(0x01)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Can't Get OutEndPoint: %s", err)
|
||||
usbif.log.Error(err, "Can't Get OutEndPoint")
|
||||
return nil, 0, err
|
||||
}
|
||||
//log.Printf("OutEndpoint: %v", outp)
|
||||
|
@ -101,13 +101,13 @@ func (usbif *UsbIF) GetAllParam(ctx context.Context) ([]byte, int, error) {
|
|||
//log.Printf("About to Send %v", send)
|
||||
len, err := outp.WriteContext(ctx, send)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Cant Send GetAllValues Command: %s (%v) - %d", err, send, len)
|
||||
usbif.log.Error(err, "Cant Send GetAllValues Command", "Command", send, "Len", len)
|
||||
return nil, 0, err
|
||||
}
|
||||
//log.Printf("Sent %d Bytes", len)
|
||||
inp, err := usbif.intf.InEndpoint(0x81)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Can't Get OutPoint: %s", err)
|
||||
usbif.log.Error(err, "Can't Get OutPoint")
|
||||
return nil, 0, err
|
||||
}
|
||||
//log.Printf("InEndpoint: %v", inp)
|
||||
|
@ -115,7 +115,7 @@ func (usbif *UsbIF) GetAllParam(ctx context.Context) ([]byte, int, error) {
|
|||
var recv = make([]byte, 24)
|
||||
len, err = inp.ReadContext(ctx, recv)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Can't Read GetAllValues Command: %s", err)
|
||||
usbif.log.Error(err, "Can't Read GetAllValues Command")
|
||||
return nil, 0, err
|
||||
}
|
||||
return recv, len, nil
|
||||
|
|
|
@ -5,31 +5,31 @@ package realusb
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/Fishwaldo/go-logadapter"
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
type UsbIF struct {
|
||||
log logadapter.Logger
|
||||
log logr.Logger
|
||||
}
|
||||
|
||||
func Init(log logadapter.Logger) (*UsbIF) {
|
||||
usbif := UsbIF{log: log}
|
||||
log.Panic("Not Compiled with USB Support!")
|
||||
log.Error(nil, "Not Compiled with USB Support!")
|
||||
return &usbif
|
||||
}
|
||||
|
||||
func (usbif *UsbIF) SetUSBDebug(level int) {
|
||||
usbif.log.Panic("Not Compiled with USB Support!")
|
||||
usbif.log.Error(nil, "Not Compiled with USB Support!")
|
||||
}
|
||||
|
||||
func (usbif *UsbIF) Scan() (bool, error) {
|
||||
usbif.log.Panic("Not Compiled with USB Support")
|
||||
usbif.log.Error(nil, "Not Compiled with USB Support")
|
||||
return false, fmt.Errorf("Not Compiled with USB Support")
|
||||
}
|
||||
func (usbif *UsbIF) Close() {
|
||||
usbif.log.Panic("Not Compiled with USB Support")
|
||||
usbif.log.Error(nil, "Not Compiled with USB Support")
|
||||
}
|
||||
func (usbif *UsbIF) GetAllParam(ctx context.Context) ([]byte, int, error) {
|
||||
usbif.log.Panic("Not Compiled with USB Support")
|
||||
usbif.log.Error(nil, "Not Compiled with USB Support")
|
||||
return nil, 0, fmt.Errorf("not compiled with USB Support")
|
||||
}
|
|
@ -6,33 +6,39 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Fishwaldo/go-logadapter"
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
type SimIF struct {
|
||||
log logadapter.Logger
|
||||
log logr.Logger
|
||||
file *os.File
|
||||
}
|
||||
|
||||
var captureFile string
|
||||
|
||||
func SetCaptureFile(name string) error {
|
||||
// fmt.Printf("Simulation Open File: %s\n", name)
|
||||
file, err := os.Open(name)
|
||||
if err != nil {
|
||||
// fmt.Printf("Open Failed: %s", err);
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
captureFile = name
|
||||
// fmt.Printf("Simulation File: %s\n", captureFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Init(log logadapter.Logger) *SimIF {
|
||||
func Init(log logr.Logger) *SimIF {
|
||||
usbif := SimIF{log: log}
|
||||
usbif.log.Info("Enabling Simulation Replay")
|
||||
if captureFile == "" {
|
||||
usbif.log.Error(nil, "USB Capture File not Specified");
|
||||
}
|
||||
var err error
|
||||
usbif.file, err = os.Open(captureFile)
|
||||
if err != nil {
|
||||
usbif.log.Panic("cannot open USB Capture File %s: %s", captureFile, err)
|
||||
usbif.log.Error(err, "cannot open USB Capture File", "file", captureFile)
|
||||
}
|
||||
return &usbif
|
||||
}
|
||||
|
@ -54,28 +60,31 @@ func (usbif *SimIF) Close() {
|
|||
}
|
||||
func (usbif *SimIF) GetAllParam(ctx context.Context) ([]byte, int, error) {
|
||||
if usbif.file == nil {
|
||||
usbif.log.Panic("usb Capture File Not Opened")
|
||||
usbif.log.Error(nil, "usb Capture File Not Opened")
|
||||
return nil, 0, fmt.Errorf("usb Capture File Not Opened")
|
||||
}
|
||||
cap := make([]byte, 24)
|
||||
var len int
|
||||
var err error
|
||||
if offset, err := usbif.file.Seek(0, io.SeekCurrent); err == nil {
|
||||
usbif.log.V(2).Info("USB Capture File at Position", "offset", offset)
|
||||
}
|
||||
len, err = usbif.file.Read(cap)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
usbif.file.Seek(0, 0)
|
||||
_, err = usbif.file.Read(cap)
|
||||
if err != nil {
|
||||
usbif.log.Warn("Seek to start of file Failed: %s", err)
|
||||
usbif.log.Error(err, "Seek to start of file Failed")
|
||||
return nil, 0, fmt.Errorf("seek to Start of File Failed: %s", err)
|
||||
}
|
||||
} else {
|
||||
usbif.log.Warn("Read From Capture File Returned Error: %s", err)
|
||||
usbif.log.Error(err, "Read From Capture File Returned Error")
|
||||
return nil, 0, fmt.Errorf("read from Capture FIle Returned %s", err)
|
||||
}
|
||||
}
|
||||
if len != 24 {
|
||||
usbif.log.Warn("Short Read from USB Capture File: %d", len)
|
||||
usbif.log.Error(nil, "Short Read from USB Capture File", "len", len)
|
||||
return nil, 0, fmt.Errorf("short Read from USB Capture File: %d", len)
|
||||
}
|
||||
return cap, len, nil
|
||||
|
|
Loading…
Add table
Reference in a new issue