Skip to content

Commit

Permalink
feat(plc4go/bacnetip): WriteBroadcastDistributionTable
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 22, 2024
1 parent 00b2c21 commit ecf4443
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
1 change: 0 additions & 1 deletion plc4go/internal/bacnetip/BACnetVirtualLinkLayerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ func (b *AnnexJCodec) Indication(args Args, kwargs KWArgs) error {

// encode it as a generic BVLL PDU
bvlpdu := NewBVLPDU(nil)
// TODO: runtime cast might be dangerous
if err := rpdu.(interface{ Encode(Arg) error }).Encode(bvlpdu); err != nil {
return errors.Wrap(err, "error encoding PDU")
}
Expand Down
39 changes: 32 additions & 7 deletions plc4go/internal/bacnetip/bvll.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package bacnetip

import (
"context"
"encoding/binary"
"fmt"

readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
Expand Down Expand Up @@ -109,7 +110,6 @@ func NewBVLPDU(bvlc readWriteModel.BVLC) BVLPDU {
b := &_BVLPDU{
bvlc: bvlc,
}
//b.bvlc = readWriteModel.NewBVLC() // TODO: using this function leads to a npe
b._BVLCI = NewBVLCI(bvlc).(*_BVLCI)
b._PDUData = newPDUData(b)
return b
Expand Down Expand Up @@ -282,7 +282,7 @@ func NewWriteBroadcastDistributionTable(opts ...func(*WriteBroadcastDistribution
for _, opt := range opts {
opt(b)
}
b._BVLPDU = NewBVLPDU(nil).(*_BVLPDU)
b._BVLPDU = NewBVLPDU(readWriteModel.NewBVLCWriteBroadcastDistributionTable(b.produceBroadcastDistributionTable(), 0)).(*_BVLPDU)
return b, nil
}

Expand All @@ -296,6 +296,35 @@ func (w *WriteBroadcastDistributionTable) GetBvlciBDT() []*Address {
return w.bvlciBDT
}

func (w *WriteBroadcastDistributionTable) produceBroadcastDistributionTable() (entries []readWriteModel.BVLCBroadcastDistributionTableEntry) {
for _, address := range w.bvlciBDT {
addr := address.AddrAddress[:4]
port := uint16(47808)
if address.AddrPort != nil {
port = *address.AddrPort
}
mask := make([]byte, 4)
if address.AddrMask != nil {
binary.BigEndian.PutUint32(mask, *address.AddrMask)
}
entries = append(entries, readWriteModel.NewBVLCBroadcastDistributionTableEntry(addr, port, mask))
}
return
}

func (w *WriteBroadcastDistributionTable) produceBvlciBDT(entries []readWriteModel.BVLCBroadcastDistributionTableEntry) (bvlciBDT []*Address) {
for _, entry := range entries {
addr := entry.GetIp()
port := entry.GetPort()
var portArray = make([]byte, 2)
binary.BigEndian.PutUint16(portArray, port)
address, _ := NewAddress(zerolog.Nop(), append(addr, portArray...))
mask := binary.BigEndian.Uint32(entry.GetBroadcastDistributionMap())
address.AddrMask = &mask
bvlciBDT = append(bvlciBDT, address)
}
return
}
func (w *WriteBroadcastDistributionTable) Encode(bvlpdu Arg) error {
switch bvlpdu := bvlpdu.(type) {
case BVLPDU:
Expand Down Expand Up @@ -324,11 +353,7 @@ func (w *WriteBroadcastDistributionTable) Decode(bvlpdu Arg) error {
switch bvlc := pduUserData.(type) {
case readWriteModel.BVLCWriteBroadcastDistributionTable:
w.setBVLC(bvlc)
for _, entry := range bvlc.GetTable() {
// TODO: what is with port and the map??
address, _ := NewAddress(zerolog.Nop(), entry.GetIp())
w.bvlciBDT = append(w.bvlciBDT, address)
}
w.bvlciBDT = w.produceBvlciBDT(bvlc.GetTable())
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/bacnetip/tests/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func MatchPdu(localLog zerolog.Logger, pdu bacnetip.PDU, pduType any, pduAttrs m
if !equal {
switch want := want.(type) {
case []byte:
localLog.Debug().Bytes("got", got).Bytes("want", want).Msg("mismatch")
localLog.Debug().Hex("got", got).Hex("want", want).Msg("mismatch")
default:
localLog.Debug().Bytes("got", got).Interface("want", want).Msg("mismatch")
localLog.Debug().Hex("got", got).Interface("want", want).Msg("mismatch")
}
}
return equal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func (suite *TestAnnexJCodecSuite) TestResult() {
}

func (suite *TestAnnexJCodecSuite) TestWriteBroadcastDistributionTable() {
suite.T().Skip("something is odd here") // TODO: check what is going on with the output...
// write an empty table
pduBytes, err := bacnetip.Xtob("81.01.0004")
suite.Require().NoError(err)
Expand Down

0 comments on commit ecf4443

Please sign in to comment.