Skip to content

Commit

Permalink
Update misc. parse functions for bounds checking and style
Browse files Browse the repository at this point in the history
- All test variations pass.
  • Loading branch information
bconway committed Feb 2, 2024
1 parent 897b4fc commit 24c7475
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pkg/decode/globalsat/ls11x.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func ls11x(body []byte) ([]*decode.Point, error) {

// Parse humidity.
hum := float64(binary.BigEndian.Uint16(body[3:5])) / 100
if hum > 100 {
return msgs, decode.ErrFormat("ls11x", "humidity outside allowed range",
body)
}

msgs = append(msgs, &decode.Point{Attr: "humidity_pct", Value: hum})

return msgs, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/decode/globalsat/ls11x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestLs11x(t *testing.T) {
// LS-11X bad length.
{"000102030405", nil, "ls11x format bad length: 000102030405"},
{"0001020304050607", nil, "ls11x format bad length: 0001020304050607"},
// LS-11X humidity outside allowed range.
{"020a18f0e70000", []*decode.Point{
{Attr: "temp_c", Value: 25.8},
{Attr: "temp_f", Value: 78.5},
}, "ls11x format humidity outside allowed range: 020a18f0e70000"},
}

for _, test := range tests {
Expand Down
4 changes: 2 additions & 2 deletions pkg/decode/radiobridge/supervisory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func supervisory(body []byte) ([]*decode.Point, error) {
// Parse battery level.
vInt := body[4] >> 4
vFract := body[4] & clearVolt
batt := float64(vInt) + float64(vFract)/10
msgs = append(msgs, &decode.Point{Attr: "battery_v", Value: batt})
volt := float64(vInt) + float64(vFract)/10
msgs = append(msgs, &decode.Point{Attr: "battery_v", Value: volt})

// Parse event count.
if len(body) >= 11 {
Expand Down
12 changes: 6 additions & 6 deletions pkg/decode/tektelic/channel_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func chanHumidity(body []byte) ([]*decode.Point, []byte, error) {
body)
}

// Parse humidity.
humidity, rem, err := typeHumidity(body)
// Parse hum.
hum, rem, err := typeHumidity(body)
if err != nil {
return nil, nil, err
}

return []*decode.Point{{Attr: "humidity_pct", Value: humidity}}, rem, nil
return []*decode.Point{{Attr: "humidity_pct", Value: hum}}, rem, nil
}

// chanBatteryV parses a Battery (V) data channel from a []byte according to the
Expand All @@ -91,11 +91,11 @@ func chanBatteryV(body []byte) ([]*decode.Point, []byte, error) {
body)
}

// Parse voltage.
voltage, rem, err := typeAnalogV(body)
// Parse volt.
volt, rem, err := typeAnalogV(body)
if err != nil {
return nil, nil, err
}

return []*decode.Point{{Attr: "battery_v", Value: voltage}}, rem, nil
return []*decode.Point{{Attr: "battery_v", Value: volt}}, rem, nil
}
8 changes: 8 additions & 0 deletions pkg/decode/tektelic/channel_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func TestChanTempC(t *testing.T) {
{Attr: "temp_c", Value: 1.0},
{Attr: "temp_f", Value: 33.8},
}, []byte{}, ""},
{"036700ca", []*decode.Point{
{Attr: "temp_c", Value: 20.2},
{Attr: "temp_f", Value: 68.4},
}, []byte{}, ""},
{"0367fff0", []*decode.Point{
{Attr: "temp_c", Value: -1.6},
{Attr: "temp_f", Value: 29.1},
}, []byte{}, ""},
{"036700c4", []*decode.Point{
{Attr: "temp_c", Value: 19.6},
{Attr: "temp_f", Value: 67.3},
Expand Down
16 changes: 10 additions & 6 deletions pkg/decode/tektelic/data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ func typeHumidity(body []byte) (float64, []byte, error) {
return 0, nil, decode.ErrFormat("typeHumidity", "bad identifier", body)
}

// Parse humidity.
humidity := float64(body[2]) / 2
// Parse hum.
hum := float64(body[2]) / 2
if hum > 100 {
return 0, nil, decode.ErrFormat("typeHumidity", "outside allowed range",
body)
}

return humidity, body[3:], nil
return hum, body[3:], nil
}

// typeAnalogV parses a Analog (V) data type from a []byte according to the spec
Expand All @@ -87,8 +91,8 @@ func typeAnalogV(body []byte) (float64, []byte, error) {
return 0, nil, decode.ErrFormat("typeAnalogV", "bad identifier", body)
}

// Parse voltage.
voltage := float64(int16(binary.BigEndian.Uint16(body[2:4]))) / 100
// Parse volt.
volt := float64(binary.BigEndian.Uint16(body[2:4])) / 100

return voltage, body[4:], nil
return volt, body[4:], nil
}
3 changes: 3 additions & 0 deletions pkg/decode/tektelic/data_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestTypeTempC(t *testing.T) {
// Temperature.
{"0367000a", 1, []byte{}, ""},
{"036700ca", 20.2, []byte{}, ""},
{"0367fff0", -1.6, []byte{}, ""},
{"036700c4", 19.6, []byte{}, ""},
{"036700c404687f", 19.6, []byte{0x04, 0x68, 0x7f}, ""},
// Temperature bad length.
Expand Down Expand Up @@ -120,6 +121,8 @@ func TestTypeHumidity(t *testing.T) {
{"04", 0, nil, "typeHumidity format bad length: 04"},
// Humidity bad identifier.
{"04697f", 0, nil, "typeHumidity format bad identifier: 04697f"},
// Humidity outside allowed range.
{"0468f0", 0, nil, "typeHumidity format outside allowed range: 0468f0"},
}

for _, test := range tests {
Expand Down

0 comments on commit 24c7475

Please sign in to comment.