Skip to content

Commit

Permalink
fix(inputs.s7comm): Fix bit queries (#14068)
Browse files Browse the repository at this point in the history
  • Loading branch information
phagemann authored Nov 13, 2023
1 parent 2d8416c commit 19c3d26
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ require (
github.com/redis/go-redis/v9 v9.2.1
github.com/riemann/riemann-go-client v0.5.1-0.20211206220514-f58f10cdce16
github.com/robbiet480/go.nut v0.0.0-20220219091450-bd8f121e1fa1
github.com/robinson/gos7 v0.0.0-20231012111941-bdaa10e92e16
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e
github.com/safchain/ethtool v0.3.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sensu/sensu-go/api/core/v2 v2.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2039,8 +2039,8 @@ github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/robinson/gos7 v0.0.0-20231012111941-bdaa10e92e16 h1:bhgGFmhTZpESybQyk+uxZ+dAd3yNSJSano2fN7HGmlA=
github.com/robinson/gos7 v0.0.0-20231012111941-bdaa10e92e16/go.mod h1:AMHIeh1KJ7Xa2RVOMHdv9jXKrpw0D4EWGGQMHLb2doc=
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e h1:Ofp6C2iX58K698sGpIXZFp3furntNlhIjeyLkcrAiek=
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e/go.mod h1:AMHIeh1KJ7Xa2RVOMHdv9jXKrpw0D4EWGGQMHLb2doc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
Expand Down
22 changes: 19 additions & 3 deletions plugins/inputs/s7comm/s7comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ func handleFieldAddress(address string) (*gos7.S7DataItem, converterFunc, error)
}

// Check the amount parameter if any
var extra int
var extra, bit int
switch dtype {
case "X", "S":
case "S":
// We require an extra parameter
x := groups["extra"]
if x == "" {
Expand All @@ -317,6 +317,21 @@ func handleFieldAddress(address string) (*gos7.S7DataItem, converterFunc, error)
if extra < 1 {
return nil, nil, fmt.Errorf("invalid extra parameter %d", extra)
}
case "X":
// We require an extra parameter
x := groups["extra"]
if x == "" {
return nil, nil, errors.New("extra parameter required")
}

bit, err = strconv.Atoi(x)
if err != nil {
return nil, nil, fmt.Errorf("invalid extra parameter: %w", err)
}
if bit < 0 || bit > 7 {
// Ensure bit address is valid
return nil, nil, fmt.Errorf("invalid extra parameter: bit address %d out of range", bit)
}
default:
if groups["extra"] != "" {
return nil, nil, errors.New("extra parameter specified but not used")
Expand Down Expand Up @@ -348,14 +363,15 @@ func handleFieldAddress(address string) (*gos7.S7DataItem, converterFunc, error)
item := &gos7.S7DataItem{
Area: area,
WordLen: wordlen,
Bit: bit,
DBNumber: areaidx,
Start: start,
Amount: amount,
Data: make([]byte, buflen),
}

// Determine the type converter function
f := determineConversion(dtype, extra)
f := determineConversion(dtype)
return item, f, nil
}

Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/s7comm/s7comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func TestFieldMappings(t *testing.T) {
{
Area: 0x84,
WordLen: 0x01,
Bit: 2,
DBNumber: 5,
Start: 3,
Amount: 1,
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/s7comm/type_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

var helper = &gos7.Helper{}

func determineConversion(dtype string, extra int) converterFunc {
func determineConversion(dtype string) converterFunc {
switch dtype {
case "X":
return func(buf []byte) interface{} {
return (buf[0] & (1 << extra)) != 0
return buf[0] != 0
}
case "B":
return func(buf []byte) interface{} {
Expand Down

0 comments on commit 19c3d26

Please sign in to comment.