This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 6e237f2e3a fix(plc4go/bacnetip): WriteBroadcastDistributionTable
6e237f2e3a is described below
commit 6e237f2e3acbc23d65c774889f3b41e5ac644e06
Author: Sebastian Rühl <[email protected]>
AuthorDate: Thu Aug 22 18:05:56 2024 +0200
fix(plc4go/bacnetip): WriteBroadcastDistributionTable
---
plc4go/internal/bacnetip/NetworkService.go | 4 +-
plc4go/internal/bacnetip/PDU.go | 12 +++---
plc4go/internal/bacnetip/bvll.go | 50 +++++++++++++++++-----
plc4go/internal/bacnetip/npdu.go | 26 +++++------
plc4go/internal/bacnetip/primitivedata.go | 8 ++--
.../bacnetip/tests/test_bvll/test_codec_test.go | 31 +++++++++++++-
.../test_utilities/test_state_machine_test.go | 4 +-
7 files changed, 96 insertions(+), 39 deletions(-)
diff --git a/plc4go/internal/bacnetip/NetworkService.go
b/plc4go/internal/bacnetip/NetworkService.go
index 999bd6a460..c3b19e9b9d 100644
--- a/plc4go/internal/bacnetip/NetworkService.go
+++ b/plc4go/internal/bacnetip/NetworkService.go
@@ -541,7 +541,7 @@ func buildNPDU(hopCount uint8, source *Address, destination
*Address, expectingR
if sourceLengthValue > math.MaxUint8 {
return nil, errors.New("source address length
overflows")
}
- sourceLengthValueUint8 := uint8(sourceLengthValue)
+ sourceLengthValueUint8 := sourceLengthValue
sourceLength = &sourceLengthValueUint8
sourceAddress = source.AddrAddress
if sourceLengthValueUint8 == 0 {
@@ -561,7 +561,7 @@ func buildNPDU(hopCount uint8, source *Address, destination
*Address, expectingR
if destinationLengthValue > math.MaxUint8 {
return nil, errors.New("source address length
overflows")
}
- destinationLengthValueUint8 := uint8(destinationLengthValue)
+ destinationLengthValueUint8 := destinationLengthValue
destinationLength = &destinationLengthValueUint8
destinationAddress = destination.AddrAddress
if destinationLengthValueUint8 == 0 {
diff --git a/plc4go/internal/bacnetip/PDU.go b/plc4go/internal/bacnetip/PDU.go
index 77d3ec52ca..cb3551d3b7 100644
--- a/plc4go/internal/bacnetip/PDU.go
+++ b/plc4go/internal/bacnetip/PDU.go
@@ -938,8 +938,8 @@ type PDUData interface {
GetData(dlen int) ([]byte, error)
Put(byte)
PutData(...byte)
- PutShort(int16)
- PutLong(int64)
+ PutShort(uint16)
+ PutLong(uint32)
}
type _PDUDataRequirements interface {
@@ -1032,17 +1032,17 @@ func (d *_PDUData) PutData(n ...byte) {
d.cachedData = append(d.cachedData, n...)
}
-func (d *_PDUData) PutShort(n int16) {
+func (d *_PDUData) PutShort(n uint16) {
d.checkCache()
ba := make([]byte, 2)
- binary.BigEndian.PutUint16(ba, uint16(n))
+ binary.BigEndian.PutUint16(ba, n)
d.cachedData = append(d.cachedData, ba...)
}
-func (d *_PDUData) PutLong(n int64) {
+func (d *_PDUData) PutLong(n uint32) {
d.checkCache()
ba := make([]byte, 4)
- binary.BigEndian.PutUint64(ba, uint64(n))
+ binary.BigEndian.PutUint32(ba, n)
d.cachedData = append(d.cachedData, ba...)
}
diff --git a/plc4go/internal/bacnetip/bvll.go b/plc4go/internal/bacnetip/bvll.go
index 2ba8277165..feb97caa78 100644
--- a/plc4go/internal/bacnetip/bvll.go
+++ b/plc4go/internal/bacnetip/bvll.go
@@ -237,7 +237,7 @@ func (n *Result) Encode(bvlpdu Arg) error {
if err := bvlpdu.Update(n); err != nil {
return errors.Wrap(err, "error updating BVLPDU")
}
- bvlpdu.PutShort(int16(n.bvlciResultCode))
+ bvlpdu.PutShort(uint16(n.bvlciResultCode))
bvlpdu.setBVLC(n.bvlc)
return nil
default:
@@ -333,7 +333,7 @@ func (w *WriteBroadcastDistributionTable) Encode(bvlpdu
Arg) error {
}
for _, bdte := range w.bvlciBDT {
bvlpdu.PutData(bdte.AddrAddress...)
- bvlpdu.PutLong(int64(*bdte.AddrMask))
+ bvlpdu.PutLong(*bdte.AddrMask)
}
bvlpdu.setBVLC(w.bvlc)
return nil
@@ -366,27 +366,55 @@ func (w *WriteBroadcastDistributionTable) String() string
{
return fmt.Sprintf("WriteBroadcastDistributionTable{%v, bvlciBDT: %v}",
w._BVLPDU, w.bvlciBDT)
}
-// TODO: finish
type ReadBroadcastDistributionTable struct {
*_BVLPDU
}
var _ BVLPDU = (*ReadBroadcastDistributionTable)(nil)
-func NewReadBroadcastDistributionTable() (BVLPDU, error) {
+func NewReadBroadcastDistributionTable(opts
...func(*ReadBroadcastDistributionTable)) (*ReadBroadcastDistributionTable,
error) {
b := &ReadBroadcastDistributionTable{}
- b._BVLPDU = NewBVLPDU(nil).(*_BVLPDU)
+ for _, opt := range opts {
+ opt(b)
+ }
+ b._BVLPDU =
NewBVLPDU(readWriteModel.NewBVLCReadBroadcastDistributionTable()).(*_BVLPDU)
return b, nil
}
-func (b *ReadBroadcastDistributionTable) Encode(pdu Arg) error {
- // TODO: finish
- return nil
+func (w *ReadBroadcastDistributionTable) Encode(bvlpdu Arg) error {
+ switch bvlpdu := bvlpdu.(type) {
+ case BVLPDU:
+ if err := bvlpdu.Update(w); err != nil {
+ return errors.Wrap(err, "error updating BVLPDU")
+ }
+ bvlpdu.setBVLC(w.bvlc)
+ return nil
+ default:
+ return errors.Errorf("invalid BVLPDU type %T", bvlpdu)
+ }
}
-func (b *ReadBroadcastDistributionTable) Decode(pdu Arg) error {
- // TODO: finish
- return nil
+func (w *ReadBroadcastDistributionTable) Decode(bvlpdu Arg) error {
+ switch bvlpdu := bvlpdu.(type) {
+ case BVLPDU:
+ if err := w.Update(bvlpdu); err != nil {
+ return errors.Wrap(err, "error updating BVLPDU")
+ }
+ switch pduUserData := bvlpdu.GetPDUUserData().(type) {
+ case readWriteModel.BVLCReadBroadcastDistributionTableExactly:
+ switch bvlc := pduUserData.(type) {
+ case readWriteModel.BVLCReadBroadcastDistributionTable:
+ w.setBVLC(bvlc)
+ }
+ }
+ return nil
+ default:
+ return errors.Errorf("invalid BVLPDU type %T", bvlpdu)
+ }
+}
+
+func (w *ReadBroadcastDistributionTable) String() string {
+ return fmt.Sprintf("ReadBroadcastDistributionTable{%v}", w._BVLPDU)
}
// TODO: finish
diff --git a/plc4go/internal/bacnetip/npdu.go b/plc4go/internal/bacnetip/npdu.go
index b9b82d8916..434347fece 100644
--- a/plc4go/internal/bacnetip/npdu.go
+++ b/plc4go/internal/bacnetip/npdu.go
@@ -179,7 +179,7 @@ func (n *_NPDU) buildNPDU(hopCount uint8, source *Address,
destination *Address,
if sourceLengthValue > math.MaxUint8 {
return nil, errors.New("source address length
overflows")
}
- sourceLengthValueUint8 := uint8(sourceLengthValue)
+ sourceLengthValueUint8 := sourceLengthValue
sourceLength = &sourceLengthValueUint8
sourceAddress = source.AddrAddress
if sourceLengthValueUint8 == 0 {
@@ -199,7 +199,7 @@ func (n *_NPDU) buildNPDU(hopCount uint8, source *Address,
destination *Address,
if destinationLengthValue > math.MaxUint8 {
return nil, errors.New("source address length
overflows")
}
- destinationLengthValueUint8 := uint8(destinationLengthValue)
+ destinationLengthValueUint8 := destinationLengthValue
destinationLength = &destinationLengthValueUint8
destinationAddress = destination.AddrAddress
if destinationLengthValueUint8 == 0 {
@@ -405,7 +405,7 @@ func (n *WhoIsRouterToNetwork) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
if n.wirtnNetwork != nil {
- npdu.PutShort(int16(*n.wirtnNetwork))
+ npdu.PutShort(*n.wirtnNetwork)
}
npdu.setNPDU(n.npdu)
npdu.setNLM(n.nlm)
@@ -476,7 +476,7 @@ func (i *IAmRouterToNetwork) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
for _, net := range i.iartnNetworkList {
- npdu.PutShort(int16(net))
+ npdu.PutShort(net)
}
npdu.setNPDU(i.npdu)
npdu.setNLM(i.nlm)
@@ -556,7 +556,7 @@ func (i *ICouldBeRouterToNetwork) Encode(npdu Arg) error {
if err := npdu.Update(i); err != nil {
return errors.Wrap(err, "error updating _NPCI")
}
- npdu.PutShort(int16(i.icbrtnNetwork))
+ npdu.PutShort(i.icbrtnNetwork)
npdu.Put(i.icbrtnPerformanceIndex)
npdu.setNPDU(i.npdu)
npdu.setNLM(i.nlm)
@@ -639,7 +639,7 @@ func (n *RejectMessageToNetwork) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
npdu.Put(byte(n.rmtnRejectionReason))
- npdu.PutShort(int16(n.rmtnDNET))
+ npdu.PutShort(n.rmtnDNET)
npdu.setNPDU(n.npdu)
npdu.setNLM(n.nlm)
npdu.setAPDU(n.apdu)
@@ -710,7 +710,7 @@ func (r *RouterBusyToNetwork) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
for _, net := range r.GetRbtnNetworkList() {
- npdu.PutShort(int16(net))
+ npdu.PutShort(net)
}
npdu.setNPDU(r.npdu)
npdu.setNLM(r.nlm)
@@ -781,7 +781,7 @@ func (r *RouterAvailableToNetwork) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
for _, net := range r.GetRatnNetworkList() {
- npdu.PutShort(int16(net))
+ npdu.PutShort(net)
}
npdu.setNPDU(r.npdu)
npdu.setNLM(r.nlm)
@@ -930,7 +930,7 @@ func (r *InitializeRoutingTable) Encode(npdu Arg) error {
return errors.Wrap(err, "error updating _NPCI")
}
for _, rte := range r.irtTable {
- npdu.PutShort(int16(rte.rtDNET))
+ npdu.PutShort(rte.rtDNET)
npdu.Put(rte.rtPortId)
npdu.Put(byte(len(rte.rtPortInfo)))
npdu.PutData(rte.rtPortInfo...)
@@ -1025,7 +1025,7 @@ func (r *InitializeRoutingTableAck) Encode(npdu Arg)
error {
return errors.Wrap(err, "error updating _NPCI")
}
for _, rte := range r.irtaTable {
- npdu.PutShort(int16(rte.rtDNET))
+ npdu.PutShort(rte.rtDNET)
npdu.Put(rte.rtPortId)
npdu.Put(byte(len(rte.rtPortInfo)))
npdu.PutData(rte.rtPortInfo...)
@@ -1109,7 +1109,7 @@ func (n *EstablishConnectionToNetwork) Encode(npdu Arg)
error {
if err := npdu.Update(n); err != nil {
return errors.Wrap(err, "error updating _NPCI")
}
- npdu.PutShort(int16(n.ectnDNET))
+ npdu.PutShort(n.ectnDNET)
npdu.Put(n.ectnTerminationTime)
npdu.setNPDU(n.npdu)
npdu.setNLM(n.nlm)
@@ -1180,7 +1180,7 @@ func (n *DisconnectConnectionToNetwork) Encode(npdu Arg)
error {
if err := npdu.Update(n); err != nil {
return errors.Wrap(err, "error updating _NPCI")
}
- npdu.PutShort(int16(n.dctnDNET))
+ npdu.PutShort(n.dctnDNET)
npdu.setNPDU(n.npdu)
npdu.setNLM(n.nlm)
npdu.setAPDU(n.apdu)
@@ -1315,7 +1315,7 @@ func (n *NetworkNumberIs) Encode(npdu Arg) error {
if err := npdu.Update(n); err != nil {
return errors.Wrap(err, "error updating _NPCI")
}
- npdu.PutShort(int16(n.nniNet))
+ npdu.PutShort(n.nniNet)
flag := uint8(0)
if n.nniFlag {
flag = 1
diff --git a/plc4go/internal/bacnetip/primitivedata.go
b/plc4go/internal/bacnetip/primitivedata.go
index 3c8c563862..a1413748a0 100644
--- a/plc4go/internal/bacnetip/primitivedata.go
+++ b/plc4go/internal/bacnetip/primitivedata.go
@@ -228,10 +228,10 @@ func (t *tag) Encode(pdu PDUData) {
pdu.Put(byte(t.tagLVT))
} else if t.tagLVT <= 65535 {
pdu.Put(254)
- pdu.PutShort(int16(t.tagLVT))
+ pdu.PutShort(uint16(t.tagLVT))
} else {
pdu.Put(255)
- pdu.PutLong(int64(t.tagLVT))
+ pdu.PutLong(uint32(t.tagLVT))
}
}
@@ -940,7 +940,7 @@ func NewUnsigned(arg Arg) (*Unsigned, error) {
func (i *Unsigned) Encode(tag Tag) {
data := make([]byte, 4)
- binary.BigEndian.PutUint32(data, uint32(i.value))
+ binary.BigEndian.PutUint32(data, i.value)
// reduce the value to the smallest number of bytes
for len(data) > 1 && data[0] == 0 {
@@ -1058,7 +1058,7 @@ func (i *Unsigned8) Decode(tag Tag) error {
// get the data
rslt := uint8(0)
for _, c := range tagData {
- rslt = (rslt << 8) + uint8(c)
+ rslt = (rslt << 8) + c
}
// save the result
diff --git a/plc4go/internal/bacnetip/tests/test_bvll/test_codec_test.go
b/plc4go/internal/bacnetip/tests/test_bvll/test_codec_test.go
index a3ab26ca59..e876ea0538 100644
--- a/plc4go/internal/bacnetip/tests/test_bvll/test_codec_test.go
+++ b/plc4go/internal/bacnetip/tests/test_bvll/test_codec_test.go
@@ -47,6 +47,14 @@ func WriteBroadcastDistributionTable(bdt
...*bacnetip.Address) *bacnetip.WriteBr
return writeBroadcastDistributionTable
}
+func ReadBroadcastDistributionTable() *bacnetip.ReadBroadcastDistributionTable
{
+ readBroadcastDistributionTable, err :=
bacnetip.NewReadBroadcastDistributionTable()
+ if err != nil {
+ panic(err)
+ }
+ return readBroadcastDistributionTable
+}
+
type TestAnnexJCodecSuite struct {
suite.Suite
@@ -172,7 +180,7 @@ func (suite *TestAnnexJCodecSuite)
TestWriteBroadcastDistributionTable() {
err =
suite.Response(bacnetip.NewArgs(bacnetip.NewPDU(&bacnetip.MessageBridge{Bytes:
pduBytes})), bacnetip.NoKWArgs)
suite.Assert().NoError(err)
- err =
suite.Confirmation(bacnetip.NewArgs((*bacnetip.WriteBroadcastDistributionTable)(nil)),
bacnetip.NewKWArgs(bacnetip.KWBvlciBDT, nil))
+ err =
suite.Confirmation(bacnetip.NewArgs((*bacnetip.WriteBroadcastDistributionTable)(nil)),
bacnetip.NewKWArgs(bacnetip.KWBvlciBDT, []*bacnetip.Address{}))
// write table with an element
addr, _ := bacnetip.NewAddress(zerolog.Nop(), "192.168.0.254/24")
@@ -195,7 +203,28 @@ func (suite *TestAnnexJCodecSuite)
TestWriteBroadcastDistributionTable() {
err =
suite.Response(bacnetip.NewArgs(bacnetip.NewPDU(&bacnetip.MessageBridge{Bytes:
pduBytes})), bacnetip.NoKWArgs)
suite.Assert().NoError(err)
err =
suite.Confirmation(bacnetip.NewArgs((*bacnetip.WriteBroadcastDistributionTable)(nil)),
bacnetip.NewKWArgs(bacnetip.KWBvlciBDT, []*bacnetip.Address{addr}))
+}
+func (suite *TestAnnexJCodecSuite) TestReadBroadcastDistributionTable() {
+ // Read an empty table
+ pduBytes, err := bacnetip.Xtob("81.02.0004")
+ suite.Require().NoError(err)
+ { // Parse with plc4x parser to validate
+ parse, err :=
readWriteModel.BVLCParse(testutils.TestContext(suite.T()), pduBytes)
+ suite.Assert().NoError(err)
+ if parse != nil {
+ suite.T().Log("\n" + parse.String())
+ }
+ }
+
+ err = suite.Request(bacnetip.NewArgs(ReadBroadcastDistributionTable()),
bacnetip.NoKWArgs)
+ suite.Assert().NoError(err)
+ err = suite.Indication(bacnetip.NoArgs,
bacnetip.NewKWArgs(bacnetip.KWPDUData, pduBytes))
+ suite.Assert().NoError(err)
+
+ err =
suite.Response(bacnetip.NewArgs(bacnetip.NewPDU(&bacnetip.MessageBridge{Bytes:
pduBytes})), bacnetip.NoKWArgs)
+ suite.Assert().NoError(err)
+ err =
suite.Confirmation(bacnetip.NewArgs((*bacnetip.ReadBroadcastDistributionTable)(nil)),
bacnetip.NoKWArgs)
}
func TestAnnexJCodec(t *testing.T) {
diff --git
a/plc4go/internal/bacnetip/tests/test_utilities/test_state_machine_test.go
b/plc4go/internal/bacnetip/tests/test_utilities/test_state_machine_test.go
index a668e054f4..0248f7c95a 100644
--- a/plc4go/internal/bacnetip/tests/test_utilities/test_state_machine_test.go
+++ b/plc4go/internal/bacnetip/tests/test_utilities/test_state_machine_test.go
@@ -171,12 +171,12 @@ func (t TPDU) PutData(b ...byte) {
panic("implement me")
}
-func (t TPDU) PutShort(i int16) {
+func (t TPDU) PutShort(i uint16) {
//TODO implement me
panic("implement me")
}
-func (t TPDU) PutLong(i int64) {
+func (t TPDU) PutLong(i uint32) {
//TODO implement me
panic("implement me")
}