Skip to content

Commit

Permalink
fix(plc4go/bacnetip): fixed usage of pduData
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 23, 2024
1 parent 3e00789 commit d122577
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 216 deletions.
34 changes: 17 additions & 17 deletions plc4go/internal/bacnetip/ApplicationLayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (s *SSM) getSegment(index uint8) (segmentAPDU PDU, moreFollows bool, err er
// the context
func (s *SSM) appendSegment(apdu PDU) error {
s.log.Debug().Stringer("apdu", apdu).Msg("appendSegment")
switch apdu := apdu.GetMessage().(type) {
switch apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUConfirmedRequestExactly:
if apdu.GetSegmentedMessage() || apdu.GetMoreFollows() {
return errors.New("Can't handle already segmented message")
Expand Down Expand Up @@ -382,7 +382,7 @@ func (s *SSM) fillWindow(sequenceNumber uint8) error {
if err != nil {
return errors.Wrapf(err, "Error sending out segment %d", i)
}
if err := s.ssmSAP.Request(NewArgs(NewPDU(apdu.GetMessage(), WithPDUDestination(s.pduAddress))), NoKWArgs); err != nil {
if err := s.ssmSAP.Request(NewArgs(NewPDU(apdu.GetRootMessage(), WithPDUDestination(s.pduAddress))), NoKWArgs); err != nil {
s.log.Debug().Err(err).Msg("error sending request")
}
if moreFollows {
Expand Down Expand Up @@ -459,7 +459,7 @@ func (c *ClientSSM) Indication(args Args, kwargs KWArgs) error {
apdu := args.Get0PDU()
// make sure we're getting confirmed requests
var apduConfirmedRequest readWriteModel.APDUConfirmedRequest
if apduCasted, ok := apdu.GetMessage().(readWriteModel.APDUConfirmedRequestExactly); !ok {
if apduCasted, ok := apdu.GetRootMessage().(readWriteModel.APDUConfirmedRequestExactly); !ok {
return errors.Errorf("Invalid APDU type %T", apduCasted)
} else {
apduConfirmedRequest = apduCasted
Expand Down Expand Up @@ -623,7 +623,7 @@ func (c *ClientSSM) abort(reason readWriteModel.BACnetAbortReason) (PDU, error)
func (c *ClientSSM) segmentedRequest(apdu PDU) error {
c.log.Debug().Stringer("apdu", apdu).Msg("segmentedRequest")

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
// server is ready for the next segment
case readWriteModel.APDUSegmentAckExactly:
c.log.Debug().Msg("segment ack")
Expand Down Expand Up @@ -760,7 +760,7 @@ func (c *ClientSSM) segmentedRequestTimeout() error {
func (c *ClientSSM) awaitConfirmation(apdu PDU) error {
c.log.Debug().Stringer("apdu", apdu).Msg("awaitConfirmation")

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUAbortExactly:
c.log.Debug().Msg("Server aborted")

Expand Down Expand Up @@ -1090,7 +1090,7 @@ func (s *ServerSSM) Confirmation(args Args, kwargs KWArgs) error {

apdu := args.Get0PDU()

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
// abort response
case readWriteModel.APDUAbortExactly:
s.log.Debug().Msg("abort")
Expand Down Expand Up @@ -1472,7 +1472,7 @@ func (s *ServerSSM) segmentedRequestTimeout() error {
func (s *ServerSSM) awaitResponse(apdu PDU) error {
s.log.Debug().Stringer("apdu", apdu).Msg("awaitResponse")

switch apdu.GetMessage().(type) {
switch apdu.GetRootMessage().(type) {
case readWriteModel.APDUConfirmedRequestExactly:
s.log.Debug().Msg("client is trying this request again")
case readWriteModel.APDUAbortExactly:
Expand Down Expand Up @@ -1511,7 +1511,7 @@ func (s *ServerSSM) segmentedResponse(apdu PDU) error {
s.log.Debug().Stringer("apdu", apdu).Msg("segmentedResponse")

// client is ready for the next segment
switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUSegmentAckExactly:
s.log.Debug().Msg("segment ack")

Expand Down Expand Up @@ -1738,18 +1738,18 @@ func (s *StateMachineAccessPoint) Confirmation(args Args, kwargs KWArgs) error {
case readWriteModel.BACnetConfirmedServiceRequestDeviceCommunicationControlEnableDisable_ENABLE:
s.log.Debug().Msg("communications enabled")
case readWriteModel.BACnetConfirmedServiceRequestDeviceCommunicationControlEnableDisable_DISABLE:
apduType := apdu.GetMessage().(interface {
apduType := apdu.GetRootMessage().(interface {
GetApduType() readWriteModel.ApduType
}).GetApduType()
switch {
case apduType == readWriteModel.ApduType_CONFIRMED_REQUEST_PDU &&
apdu.GetMessage().(readWriteModel.APDUConfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetConfirmedServiceChoice_DEVICE_COMMUNICATION_CONTROL:
apdu.GetRootMessage().(readWriteModel.APDUConfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetConfirmedServiceChoice_DEVICE_COMMUNICATION_CONTROL:
s.log.Debug().Msg("continue with DCC request")
case apduType == readWriteModel.ApduType_CONFIRMED_REQUEST_PDU &&
apdu.GetMessage().(readWriteModel.APDUConfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetConfirmedServiceChoice_REINITIALIZE_DEVICE:
apdu.GetRootMessage().(readWriteModel.APDUConfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetConfirmedServiceChoice_REINITIALIZE_DEVICE:
s.log.Debug().Msg("continue with reinitialize device")
case apduType == readWriteModel.ApduType_UNCONFIRMED_REQUEST_PDU &&
apdu.GetMessage().(readWriteModel.APDUUnconfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetUnconfirmedServiceChoice_WHO_IS:
apdu.GetRootMessage().(readWriteModel.APDUUnconfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetUnconfirmedServiceChoice_WHO_IS:
s.log.Debug().Msg("continue with Who-Is")
default:
s.log.Debug().Msg("not a Who-Is, dropped")
Expand All @@ -1761,7 +1761,7 @@ func (s *StateMachineAccessPoint) Confirmation(args Args, kwargs KWArgs) error {

var pduSource = apdu.GetPDUSource()

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUConfirmedRequestExactly:
// Find duplicates of this request
var tr *ServerSSM
Expand Down Expand Up @@ -1903,15 +1903,15 @@ func (s *StateMachineAccessPoint) SapIndication(args Args, kwargs KWArgs) error
case readWriteModel.BACnetConfirmedServiceRequestDeviceCommunicationControlEnableDisable_DISABLE_INITIATION:
s.log.Debug().Msg("initiation disabled")
// TODO: this should be quarded
if apdu.GetMessage().(readWriteModel.APDU).GetApduType() == readWriteModel.ApduType_UNCONFIRMED_REQUEST_PDU && apdu.(readWriteModel.APDUUnconfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetUnconfirmedServiceChoice_I_AM {
if apdu.GetRootMessage().(readWriteModel.APDU).GetApduType() == readWriteModel.ApduType_UNCONFIRMED_REQUEST_PDU && apdu.(readWriteModel.APDUUnconfirmedRequest).GetServiceRequest().GetServiceChoice() == readWriteModel.BACnetUnconfirmedServiceChoice_I_AM {
s.log.Debug().Msg("continue with I-Am")
} else {
s.log.Debug().Msg("not an I-Am")
return nil
}
}

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUUnconfirmedRequestExactly:
// deliver to the device
if err := s.Request(NewArgs(apdu), NoKWArgs); err != nil {
Expand Down Expand Up @@ -1957,7 +1957,7 @@ func (s *StateMachineAccessPoint) SapConfirmation(args Args, kwargs KWArgs) erro
s.log.Debug().Stringer("Args", args).Stringer("KWArgs", kwargs).Msg("SapConfirmation")
apdu := args.Get0PDU()
pduDestination := apdu.GetPDUDestination()
switch apdu.GetMessage().(type) {
switch apdu.GetRootMessage().(type) {
case readWriteModel.APDUSimpleAckExactly, readWriteModel.APDUComplexAckExactly, readWriteModel.APDUErrorExactly, readWriteModel.APDURejectExactly:
// find the client transaction this is acking
var tr *ServerSSM
Expand Down Expand Up @@ -2083,7 +2083,7 @@ func (a *ApplicationServiceAccessPoint) Indication(args Args, kwargs KWArgs) err
a.log.Debug().Stringer("Args", args).Stringer("KWArgs", kwargs).Msg("Indication")
apdu := args.Get0PDU()

switch _apdu := apdu.GetMessage().(type) {
switch _apdu := apdu.GetRootMessage().(type) {
case readWriteModel.APDUConfirmedRequestExactly:
//assume no errors found
var errorFound error
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/bacnetip/ApplicationModule.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (a *Application) Request(args Args, kwargs KWArgs) error {
apdu := args.Get0PDU()

// double-check the input is the right kind of APDU
switch apdu.GetMessage().(type) {
switch apdu.GetRootMessage().(type) {
case readWriteModel.APDUUnconfirmedRequestExactly, readWriteModel.APDUConfirmedRequestExactly:
default:
return errors.New("APDU expected")
Expand Down Expand Up @@ -536,7 +536,7 @@ func (a *ApplicationIOController) _AppComplete(address *Address, apdu PDU) error
}

// this request is complete
switch apdu.GetMessage().(type) {
switch apdu.GetRootMessage().(type) {
case readWriteModel.APDUSimpleAckExactly, readWriteModel.APDUComplexAckExactly:
if err := queue.CompleteIO(queue.activeIOCB, apdu); err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions plc4go/internal/bacnetip/BACnetVirtualLinkLayerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ func (m *UDPMultiplexer) Confirmation(args Args, kwargs KWArgs) error {
m.log.Debug().Stringer("dest", dest).Msg("dest")

// must have at least one octet
if pdu.GetMessage() == nil {
if pdu.GetRootMessage() == nil {
m.log.Debug().Msg("no data")
return nil
}

// TODO: we only support 0x81 at the moment
if m.annexJ != nil {
return m.annexJ.Response(NewArgs(NewPDU(pdu.GetMessage(), WithPDUSource(src), WithPDUDestination(dest))), NoKWArgs)
return m.annexJ.Response(NewArgs(NewPDU(pdu.GetRootMessage(), WithPDUSource(src), WithPDUDestination(dest))), NoKWArgs)
}

return nil
Expand Down Expand Up @@ -707,7 +707,7 @@ func (b *BIPForeign) Indication(args Args, kwargs KWArgs) error {
switch pdu.GetPDUDestination().AddrType {
case LOCAL_STATION_ADDRESS:
// make an original unicast _PDU
xpdu := readWriteModel.NewBVLCOriginalUnicastNPDU(pdu.GetMessage().(readWriteModel.NPDU), 0)
xpdu := readWriteModel.NewBVLCOriginalUnicastNPDU(pdu.GetRootMessage().(readWriteModel.NPDU), 0)
b.log.Debug().Stringer("xpdu", xpdu).Msg("xpdu")

// send it downstream
Expand All @@ -720,7 +720,7 @@ func (b *BIPForeign) Indication(args Args, kwargs KWArgs) error {
}

// make an original broadcast _PDU
xpdu := readWriteModel.NewBVLCOriginalBroadcastNPDU(pdu.GetMessage().(readWriteModel.NPDU), 0)
xpdu := readWriteModel.NewBVLCOriginalBroadcastNPDU(pdu.GetRootMessage().(readWriteModel.NPDU), 0)

b.log.Debug().Stringer("xpdu", xpdu).Msg("xpdu")

Expand All @@ -739,7 +739,7 @@ func (b *BIPForeign) Confirmation(args Args, kwargs KWArgs) error {
b.log.Debug().Stringer("Args", args).Stringer("KWArgs", kwargs).Msg("Confirmation")
pdu := args.Get0PDU()

switch msg := pdu.GetMessage().(type) {
switch msg := pdu.GetRootMessage().(type) {
// check for a registration request result
case readWriteModel.BVLCResultExactly:
// if we are unbinding, do nothing
Expand Down
53 changes: 37 additions & 16 deletions plc4go/internal/bacnetip/CommunicationsModule.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func init() {

type IPCI interface {
spi.Message
// GetRootMessage returns this. (TODO: check if type switch works without that, as this is spi.Message which delegates)
GetRootMessage() spi.Message
SetPDUUserData(spi.Message)
GetPDUUserData() spi.Message
GetPDUSource() *Address
Expand All @@ -60,15 +62,20 @@ type IPCI interface {
}

type __PCI struct {
rootMessage spi.Message
pduUserData spi.Message
pduSource *Address
pduDestination *Address
}

var _ IPCI = (*__PCI)(nil)

func new__PCI(pduUserData spi.Message, pduSource *Address, pduDestination *Address) *__PCI {
return &__PCI{pduUserData, pduSource, pduDestination}
func new__PCI(rootMessage spi.Message, pduUserData spi.Message, pduSource *Address, pduDestination *Address) *__PCI {
return &__PCI{rootMessage, pduUserData, pduSource, pduDestination}
}

func (p *__PCI) GetRootMessage() spi.Message {
return p.rootMessage
}

func (p *__PCI) SetPDUUserData(pduUserData spi.Message) {
Expand Down Expand Up @@ -98,6 +105,7 @@ func (p *__PCI) SetPDUDestination(destination *Address) {
func (p *__PCI) Update(pci Arg) error {
switch pci := pci.(type) {
case IPCI:
p.rootMessage = pci.GetRootMessage()
p.pduUserData = pci.GetPDUUserData()
p.pduSource = pci.GetPDUSource()
p.pduDestination = pci.GetPDUDestination()
Expand All @@ -108,6 +116,7 @@ func (p *__PCI) Update(pci Arg) error {
}

func (p *__PCI) deepCopy() *__PCI {
rootMessage := p.rootMessage // those are immutable so no copy needed
pduUserData := p.pduUserData // those are immutable so no copy needed
pduSource := p.pduSource
if pduSource != nil {
Expand All @@ -119,53 +128,65 @@ func (p *__PCI) deepCopy() *__PCI {
copyPduDestination := *pduDestination
pduDestination = &copyPduDestination
}
return &__PCI{pduUserData, pduSource, pduDestination}
return &__PCI{rootMessage, pduUserData, pduSource, pduDestination}
}

func (p *__PCI) Serialize() ([]byte, error) {
if p.pduUserData == nil {
if p.rootMessage == nil {
return nil, errors.New("no pdu userdata")
}
return p.pduUserData.Serialize()
return p.rootMessage.Serialize()
}

func (p *__PCI) SerializeWithWriteBuffer(ctx context.Context, writeBuffer utils.WriteBuffer) error {
if p.pduUserData == nil {
if p.rootMessage == nil {
return errors.New("no pdu userdata")
}
return p.pduUserData.SerializeWithWriteBuffer(ctx, writeBuffer)
return p.rootMessage.SerializeWithWriteBuffer(ctx, writeBuffer)
}

func (p *__PCI) GetLengthInBytes(ctx context.Context) uint16 {
if p.pduUserData == nil {
if p.rootMessage == nil {
return 0
}
return p.pduUserData.GetLengthInBytes(ctx)
return p.rootMessage.GetLengthInBytes(ctx)
}

func (p *__PCI) GetLengthInBits(ctx context.Context) uint16 {
if p.pduUserData == nil {
if p.rootMessage == nil {
return 0
}
return p.pduUserData.GetLengthInBits(ctx)
return p.rootMessage.GetLengthInBits(ctx)
}

func (p *__PCI) String() string {
pduUserDataString := ""
rootMessageString := "nil"
if p.rootMessage != nil && globals.ExtendedPDUOutput {
rootMessageString = p.rootMessage.String()
if strings.Contains(rootMessageString, "\n") {
rootMessageString = "\n" + rootMessageString + "\n"
}
} else if p.rootMessage != nil {
if bytes, err := p.rootMessage.Serialize(); err != nil {
rootMessageString = err.Error()
} else {
rootMessageString = Btox(bytes, ".")
}
}
pduUserDataString := "nil"
if p.pduUserData != nil && globals.ExtendedPDUOutput {
pduUserDataString = p.pduUserData.String()
if strings.Contains(pduUserDataString, "\n") {
pduUserDataString = "\n" + pduUserDataString + "\n"
}
pduUserDataString = "pduUserData: " + pduUserDataString + " ,"
} else if p.pduUserData != nil {
if bytes, err := p.pduUserData.Serialize(); err != nil {
pduUserDataString = "pduUserData: " + err.Error() + " ,"
pduUserDataString = err.Error()
} else {
pduUserDataString = "pduUserData: " + Btox(bytes, ".") + " ,"
pduUserDataString = Btox(bytes, ".")
}
}
return fmt.Sprintf("__PCI{%spduSource: %s, pduDestination: %s}", pduUserDataString, p.pduSource, p.pduDestination)
return fmt.Sprintf("__PCI{rootMessage: %s, pduUserData: %s, pduSource: %s, pduDestination: %s}", rootMessageString, pduUserDataString, p.pduSource, p.pduDestination)
}

// _Client is an interface used for documentation
Expand Down
4 changes: 2 additions & 2 deletions plc4go/internal/bacnetip/Device.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (w *WhoIsIAmServices) DoWhoIsRequest(apdu PDU) error {

// extract the parameters
var lowLimit, highLimit *uint
if deviceInstanceRangeLowLimit := apdu.GetMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeLowLimit(); deviceInstanceRangeLowLimit != nil {
if deviceInstanceRangeLowLimit := apdu.GetRootMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeLowLimit(); deviceInstanceRangeLowLimit != nil {
_lowLimit := uint(deviceInstanceRangeLowLimit.GetActualValue())
lowLimit = &_lowLimit
}
if deviceInstanceRangeHighLimit := apdu.GetMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeHighLimit(); deviceInstanceRangeHighLimit != nil {
if deviceInstanceRangeHighLimit := apdu.GetRootMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeHighLimit(); deviceInstanceRangeHighLimit != nil {
_highLimit := uint(deviceInstanceRangeHighLimit.GetActualValue())
highLimit = &_highLimit
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/bacnetip/MessageCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (m *ApplicationLayerMessageCodec) SendRequest(ctx context.Context, message
nil,
nil,
nil,
response.GetMessage().(model.APDU),
response.GetRootMessage().(model.APDU),
0,
),
0,
Expand Down
8 changes: 4 additions & 4 deletions plc4go/internal/bacnetip/NetworkService.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (n *NetworkServiceAccessPoint) Indication(args Args, kwargs KWArgs) error {

pdu := args.Get0PDU()
// get the apdu
apdu := pdu.GetMessage().(readWriteModel.APDU)
apdu := pdu.GetRootMessage().(readWriteModel.APDU)

// build a npdu
pduDestination := pdu.GetPDUDestination()
Expand Down Expand Up @@ -590,7 +590,7 @@ func (n *NetworkServiceAccessPoint) ProcessNPDU(adapter *NetworkAdapter, pdu PDU
forwardMessage bool
)

npdu := pdu.GetMessage().(readWriteModel.NPDU)
npdu := pdu.GetRootMessage().(readWriteModel.NPDU)
sourceAddress := &Address{AddrType: NULL_ADDRESS}
if npdu.GetControl().GetSourceSpecified() {
snet := npdu.GetSourceNetworkAddress()
Expand Down Expand Up @@ -1031,7 +1031,7 @@ func (n *NetworkServiceElement) Indication(args Args, kwargs KWArgs) error {
adapter := args.Get0NetworkAdapter()
npdu := args.Get1PDU()

switch message := npdu.GetMessage().(type) {
switch message := npdu.GetRootMessage().(type) {
case readWriteModel.NPDUExactly:
switch nlm := message.GetNlm().(type) {
case readWriteModel.NLMWhoIsRouterToNetwork:
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func (n *NetworkServiceElement) Confirmation(args Args, kwargs KWArgs) error {
adapter := args.Get0NetworkAdapter()
npdu := args.Get1PDU()

switch message := npdu.GetMessage().(type) {
switch message := npdu.GetRootMessage().(type) {
case readWriteModel.NPDUExactly:
switch nlm := message.GetNlm().(type) {
case readWriteModel.NLMWhoIsRouterToNetwork:
Expand Down
Loading

0 comments on commit d122577

Please sign in to comment.