Skip to content

Commit 3cceea4

Browse files
committed
fix(plc4go/bacnetip): nasty bug in match pdu fixed
1 parent a86fe67 commit 3cceea4

File tree

10 files changed

+413
-145
lines changed

10 files changed

+413
-145
lines changed

plc4go/internal/bacnetip/bvll.go

+41-38
Original file line numberDiff line numberDiff line change
@@ -594,21 +594,18 @@ func (w *ForwardedNPDU) Decode(bvlpdu Arg) error {
594594
}
595595
switch pduUserData := bvlpdu.GetPDUUserData().(type) {
596596
case readWriteModel.BVLCForwardedNPDUExactly:
597-
switch bvlc := pduUserData.(type) {
598-
case readWriteModel.BVLCForwardedNPDU:
599-
addr := bvlc.GetIp()
600-
port := bvlc.GetPort()
601-
var portArray = make([]byte, 2)
602-
binary.BigEndian.PutUint16(portArray, port)
603-
var err error
604-
address, err := NewAddress(zerolog.Nop(), append(addr, portArray...))
605-
if err != nil {
606-
return errors.Wrap(err, "error creating address")
607-
}
608-
w.bvlciAddress = address
609-
610-
w.setBVLC(bvlc)
597+
addr := pduUserData.GetIp()
598+
port := pduUserData.GetPort()
599+
var portArray = make([]byte, 2)
600+
binary.BigEndian.PutUint16(portArray, port)
601+
var err error
602+
address, err := NewAddress(zerolog.Nop(), append(addr, portArray...))
603+
if err != nil {
604+
return errors.Wrap(err, "error creating address")
611605
}
606+
w.bvlciAddress = address
607+
608+
w.setBVLC(pduUserData)
612609
}
613610
return nil
614611
default:
@@ -744,28 +741,31 @@ type OriginalUnicastNPDU struct {
744741

745742
var _ BVLPDU = (*OriginalUnicastNPDU)(nil)
746743

747-
func NewOriginalUnicastNPDU(pdu PDU, opts ...func(*OriginalUnicastNPDU)) (BVLPDU, error) {
748-
b := &OriginalUnicastNPDU{}
744+
func NewOriginalUnicastNPDU(pdu PDU, opts ...func(*OriginalUnicastNPDU)) (*OriginalUnicastNPDU, error) {
745+
o := &OriginalUnicastNPDU{}
749746
for _, opt := range opts {
750-
opt(b)
747+
opt(o)
751748
}
752749
switch npdu := pdu.(type) {
753750
case readWriteModel.NPDUExactly:
754-
b._BVLPDU = NewBVLPDU(readWriteModel.NewBVLCOriginalUnicastNPDU(npdu, npdu.GetLengthInBytes(context.Background()))).(*_BVLPDU)
751+
o._BVLPDU = NewBVLPDU(readWriteModel.NewBVLCOriginalUnicastNPDU(o.produceInnerNPDU(npdu))).(*_BVLPDU)
752+
case nil:
753+
o._BVLPDU = NewBVLPDU(nil).(*_BVLPDU)
755754
default:
756755
// TODO: re-encode seems expensive... check if there is a better option (e.g. only do it on the message bridge)
757-
parse, err := readWriteModel.BVLCParse(context.Background(), pdu.GetPduData())
756+
data := pdu.GetPduData()
757+
parse, err := readWriteModel.NPDUParse(context.Background(), data, uint16(len(data)))
758758
if err != nil {
759759
return nil, errors.Wrap(err, "error re-encoding")
760760
}
761-
b._BVLPDU = NewBVLPDU(parse).(*_BVLPDU)
761+
o._BVLPDU = NewBVLPDU(readWriteModel.NewBVLCOriginalUnicastNPDU(o.produceInnerNPDU(parse))).(*_BVLPDU)
762762
}
763763
// Do a post construct for a bit more easy initialization
764-
for _, f := range b._postConstruct {
764+
for _, f := range o._postConstruct {
765765
f()
766766
}
767-
b._postConstruct = nil
768-
return b, nil
767+
o._postConstruct = nil
768+
return o, nil
769769
}
770770

771771
func WithOriginalUnicastNPDUDestination(destination *Address) func(*OriginalUnicastNPDU) {
@@ -784,42 +784,45 @@ func WithOriginalUnicastNPDUUserData(userData spi.Message) func(*OriginalUnicast
784784
}
785785
}
786786

787-
func (n *OriginalUnicastNPDU) Encode(bvlpdu Arg) error {
787+
func (o *OriginalUnicastNPDU) produceInnerNPDU(inNpdu readWriteModel.NPDU) (npdu readWriteModel.NPDU, bvlcPayloadLength uint16) {
788+
npdu = inNpdu
789+
return
790+
}
791+
792+
func (o *OriginalUnicastNPDU) Encode(bvlpdu Arg) error {
788793
switch bvlpdu := bvlpdu.(type) {
789794
case BVLPDU:
790-
if err := bvlpdu.Update(n); err != nil {
795+
if err := bvlpdu.Update(o); err != nil {
791796
return errors.Wrap(err, "error updating BVLPDU")
792797
}
793-
bvlpdu.setBVLC(n.bvlc)
794-
bvlpdu.PutData(n.getPDUData()...)
798+
799+
bvlpdu.PutData(o.getPDUData()...)
800+
801+
bvlpdu.setBVLC(o.bvlc)
795802
return nil
796803
default:
797804
return errors.Errorf("invalid BVLPDU type %T", bvlpdu)
798805
}
799806
}
800807

801-
func (n *OriginalUnicastNPDU) Decode(bvlpdu Arg) error {
808+
func (o *OriginalUnicastNPDU) Decode(bvlpdu Arg) error {
802809
switch bvlpdu := bvlpdu.(type) {
803810
case BVLPDU:
804-
if err := n.Update(bvlpdu); err != nil {
811+
if err := o.Update(bvlpdu); err != nil {
805812
return errors.Wrap(err, "error updating BVLPDU")
806813
}
807814
switch pduUserData := bvlpdu.GetPDUUserData().(type) {
808-
case readWriteModel.BVLCExactly:
809-
switch bvlc := pduUserData.(type) {
810-
case readWriteModel.BVLCOriginalUnicastNPDU:
811-
n.setBVLC(bvlc)
812-
n.PutData(bvlpdu.GetPduData()...)
813-
}
815+
case readWriteModel.BVLCOriginalUnicastNPDUExactly:
816+
o.setBVLC(pduUserData)
814817
}
815818
return nil
816819
default:
817820
return errors.Errorf("invalid BVLPDU type %T", bvlpdu)
818821
}
819822
}
820823

821-
func (n *OriginalUnicastNPDU) String() string {
822-
return fmt.Sprintf("OriginalUnicastNPDU{%s}", n._BVLPDU)
824+
func (o *OriginalUnicastNPDU) String() string {
825+
return fmt.Sprintf("OriginalUnicastNPDU{%s}", o._BVLPDU)
823826
}
824827

825828
type OriginalBroadcastNPDU struct {
@@ -829,7 +832,7 @@ type OriginalBroadcastNPDU struct {
829832
_postConstruct []func()
830833
}
831834

832-
func NewOriginalBroadcastNPDU(pdu PDU, opts ...func(*OriginalBroadcastNPDU)) (BVLPDU, error) {
835+
func NewOriginalBroadcastNPDU(pdu PDU, opts ...func(*OriginalBroadcastNPDU)) (*OriginalBroadcastNPDU, error) {
833836
b := &OriginalBroadcastNPDU{}
834837
for _, opt := range opts {
835838
opt(b)

plc4go/internal/bacnetip/comp.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,46 @@ const (
159159
KWBvlciFDT = KnownKey("bvlciFDT")
160160
)
161161

162-
type MessageBridge struct {
162+
type MessageBridge interface {
163+
spi.Message
164+
_PDUDataRequirements
165+
}
166+
167+
type messageBridge struct {
163168
Bytes []byte
164169
}
165170

166-
var _ spi.Message = (*MessageBridge)(nil)
167-
var _ _PDUDataRequirements = (*MessageBridge)(nil)
171+
func NewMessageBridge(bytes ...byte) MessageBridge {
172+
m := &messageBridge{Bytes: make([]byte, len(bytes))}
173+
copy(m.Bytes, bytes)
174+
if len(m.Bytes) == 0 {
175+
m.Bytes = nil
176+
}
177+
return m
178+
}
179+
180+
var _ MessageBridge = (*messageBridge)(nil)
168181

169-
func (m *MessageBridge) String() string {
182+
func (m *messageBridge) String() string {
170183
return Btox(m.Bytes, "")
171184
}
172185

173-
func (m *MessageBridge) Serialize() ([]byte, error) {
186+
func (m *messageBridge) Serialize() ([]byte, error) {
174187
return m.Bytes, nil
175188
}
176189

177-
func (m *MessageBridge) SerializeWithWriteBuffer(_ context.Context, writeBuffer utils.WriteBuffer) error {
190+
func (m *messageBridge) SerializeWithWriteBuffer(_ context.Context, writeBuffer utils.WriteBuffer) error {
178191
return writeBuffer.WriteByteArray("Bytes", m.Bytes)
179192
}
180193

181-
func (m *MessageBridge) GetLengthInBytes(_ context.Context) uint16 {
194+
func (m *messageBridge) GetLengthInBytes(_ context.Context) uint16 {
182195
return uint16(len(m.Bytes))
183196
}
184197

185-
func (m *MessageBridge) GetLengthInBits(ctx context.Context) uint16 {
198+
func (m *messageBridge) GetLengthInBits(ctx context.Context) uint16 {
186199
return m.GetLengthInBytes(ctx) * 8
187200
}
188201

189-
func (m *MessageBridge) getPDUData() []byte {
202+
func (m *messageBridge) getPDUData() []byte {
190203
return m.Bytes
191204
}

0 commit comments

Comments
 (0)