Skip to content

Commit

Permalink
feat(plc4go/bacnetip): add BVLCResult
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 22, 2024
1 parent dd1e22d commit 00b2c21
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 121 deletions.
58 changes: 52 additions & 6 deletions plc4go/internal/bacnetip/BACnetVirtualLinkLayerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package bacnetip

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -322,13 +323,46 @@ func (b *AnnexJCodec) String() string {
}

func (b *AnnexJCodec) Indication(args Args, kwargs KWArgs) error {
// Note: our BVLC are all annexJ at the moment
return b.Request(args, kwargs)
b.log.Debug().Stringer("args", args).Stringer("kwargs", kwargs).Msg("Indication")

rpdu := args.Get0PDU()

// 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")
}

// encode it as a PDU
pdu := NewPDU(nil)
if err := bvlpdu.Encode(pdu); err != nil {
return errors.Wrap(err, "error encoding PDU")
}

// send it downstream
return b.Request(NewArgs(pdu), NoKWArgs)
}

func (b *AnnexJCodec) Confirmation(args Args, kwargs KWArgs) error {
// Note: our BVLC are all annexJ at the moment
return b.Response(args, kwargs)
b.log.Debug().Stringer("args", args).Stringer("kwargs", kwargs).Msg("Confirmation")

pdu := args.Get0PDU()

// interpret as a BVLL PDU
bvlpdu := NewBVLPDU(nil)
if err := bvlpdu.Decode(pdu); err != nil {
return errors.Wrap(err, "error decoding pdu")
}

// get the class related to the function
rpdu := BVLPDUTypes[bvlpdu.GetBvlcFunction()]()
if err := rpdu.Decode(bvlpdu); err != nil {
return errors.Wrap(err, "error decoding PDU")
}

// send it upstream
return b.Response(NewArgs(rpdu), NoKWArgs)
}

type _BIPSAP interface {
Expand Down Expand Up @@ -445,7 +479,7 @@ func (b *BIPSimple) String() string {

func (b *BIPSimple) Indication(args Args, kwargs KWArgs) error {
b.log.Debug().Stringer("Args", args).Stringer("KWArgs", kwargs).Msg("Indication")
pdu := args.Get0NPDU()
pdu := args.Get0PDU()
if pdu == nil {
return errors.New("no pdu")
}
Expand Down Expand Up @@ -486,7 +520,19 @@ func (b *BIPSimple) 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) {
// TODO: come up with a better way to check that... this is hugely inefficient
_data := pdu.GetPDUUserData()
_ = _data
data := pdu.GetPduData()
bvlcParse, err := readWriteModel.NPDUParse(context.Background(), data, uint16(len(data)))
if err != nil {
panic(err)
}

// TODO: we need to work with the inner types here....
panic("todo")

switch msg := bvlcParse.(type) {
// some kind of response to a request
case readWriteModel.BVLCResultExactly:
// send this to the service access point
Expand Down
48 changes: 7 additions & 41 deletions plc4go/internal/bacnetip/PDU.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,44 +1057,8 @@ func (d *_PDUData) deepCopy() *_PDUData {
return &copyPDUData
}

type APCI interface {
PCI
}

type _APCI struct {
*_PCI
}

func newAPCI(pduUserData spi.Message, pduSource *Address, pduDestination *Address, expectingReply bool, networkPriority readWriteModel.NPDUNetworkPriority) *_APCI {
return &_APCI{
_PCI: newPCI(pduUserData, pduSource, pduDestination, expectingReply, networkPriority),
}
}

func (a *_APCI) Update(apci Arg) error {
if err := a._PCI.Update(apci); err != nil {
return errors.Wrap(err, "error updating _PCI")
}
switch pci := apci.(type) {
case APCI:
// TODO: update coordinates....
return nil
default:
return errors.Errorf("invalid APCI type %T", pci)
}
}

func (a *_APCI) deepCopy() *_APCI {
_pci := a._PCI.deepCopy()
return &_APCI{_pci}
}

func (a *_APCI) String() string {
return fmt.Sprintf("APCI{%s}", a._PCI)
}

type PDU interface {
APCI
PCI
PDUData
GetMessage() spi.Message // TODO: check if we still need that... ()
DeepCopy() PDU
Expand All @@ -1106,14 +1070,14 @@ type PDUContract interface {
}

type _PDU struct {
*_APCI
*_PCI
*_PDUData
PDUContract
}

func NewPDU(pduUserData spi.Message, pduOptions ...PDUOption) PDU {
p := &_PDU{
_APCI: newAPCI(pduUserData, nil, nil, false, readWriteModel.NPDUNetworkPriority_NORMAL_MESSAGE),
_PCI: newPCI(pduUserData, nil, nil, false, readWriteModel.NPDUNetworkPriority_NORMAL_MESSAGE),
}
p.PDUContract = p
for _, option := range pduOptions {
Expand All @@ -1125,7 +1089,7 @@ func NewPDU(pduUserData spi.Message, pduOptions ...PDUOption) PDU {

func NewPDUFromPDUWithNewMessage(pdu PDU, pduUserData spi.Message, pduOptions ...PDUOption) PDU {
p := &_PDU{
_APCI: newAPCI(pduUserData, pdu.GetPDUSource(), pdu.GetPDUDestination(), pdu.GetExpectingReply(), pdu.GetNetworkPriority()),
_PCI: newPCI(pduUserData, pdu.GetPDUSource(), pdu.GetPDUDestination(), pdu.GetExpectingReply(), pdu.GetNetworkPriority()),
}
p.PDUContract = p
for _, option := range pduOptions {
Expand Down Expand Up @@ -1177,7 +1141,9 @@ func (p *_PDU) GetMessage() spi.Message {
}

func (p *_PDU) deepCopy() *_PDU {
return &_PDU{_APCI: p._APCI.deepCopy(), _PDUData: p._PDUData.deepCopy()}
pduCopy := &_PDU{_PCI: p._PCI.deepCopy(), _PDUData: p._PDUData.deepCopy()}
pduCopy.PDUContract = pduCopy
return pduCopy
}

func (p *_PDU) DeepCopy() PDU {
Expand Down
Loading

0 comments on commit 00b2c21

Please sign in to comment.