You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/01/11 11:10:59 UTC
[plc4x] 03/04: refactor(plc4go/bacnet): cleanup package structure
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
commit d1cbe41c7e14202e0e61a12ca6ca054d60484cf5
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Jan 11 11:00:46 2023 +0100
refactor(plc4go/bacnet): cleanup package structure
---
plc4go/internal/bacnetip/ApplicationLayer.go | 12 +-
plc4go/internal/bacnetip/ApplicationModule.go | 32 ++--
plc4go/internal/bacnetip/Capability.go | 4 +
plc4go/internal/bacnetip/Device.go | 190 +++++++++++++++++++++
.../bacnetip/{local/Device.go => LocalDevice.go} | 3 +-
plc4go/internal/bacnetip/MessageCodec.go | 3 +-
plc4go/internal/bacnetip/Object.go | 9 +-
plc4go/internal/bacnetip/Task.go | 17 +-
plc4go/internal/bacnetip/service/Device.go | 28 ---
plc4go/internal/bacnetip/service/Object.go | 28 ---
10 files changed, 234 insertions(+), 92 deletions(-)
diff --git a/plc4go/internal/bacnetip/ApplicationLayer.go b/plc4go/internal/bacnetip/ApplicationLayer.go
index 0dec61bed2..6fc4a2bf2a 100644
--- a/plc4go/internal/bacnetip/ApplicationLayer.go
+++ b/plc4go/internal/bacnetip/ApplicationLayer.go
@@ -20,7 +20,6 @@
package bacnetip
import (
- "github.com/apache/plc4x/plc4go/internal/bacnetip/local"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
@@ -76,7 +75,7 @@ type SSMSAPRequirements interface {
_ServiceAccessPoint
_Client
GetDeviceInfoCache() *DeviceInfoCache
- GetLocalDevice() *local.LocalDeviceObject
+ GetLocalDevice() *LocalDeviceObject
GetProposedWindowSize() uint8
GetClientTransactions() []*ClientSSM
RemoveClientTransaction(*ClientSSM)
@@ -92,7 +91,7 @@ type SSMSAPRequirements interface {
// SSM - Segmentation State Machine
type SSM struct {
- OneShotTask
+ *OneShotTask
ssmSAP SSMSAPRequirements
@@ -164,6 +163,7 @@ func NewSSM(sap SSMSAPRequirements, pduAddress *Address) (SSM, error) {
maxApduLengthAccepted = sap.GetDefaultMaximumApduLengthAccepted()
}
return SSM{
+ OneShotTask: NewOneShotTask(nil),
ssmSAP: sap,
pduAddress: pduAddress,
deviceInfo: deviceInfo,
@@ -1510,7 +1510,7 @@ type StateMachineAccessPoint struct {
*Client
*ServiceAccessPoint
- localDevice *local.LocalDeviceObject
+ localDevice *LocalDeviceObject
deviceInventory *DeviceInfoCache
nextInvokeId uint8
clientTransactions []*ClientSSM
@@ -1526,7 +1526,7 @@ type StateMachineAccessPoint struct {
applicationTimeout uint
}
-func NewStateMachineAccessPoint(localDevice *local.LocalDeviceObject, deviceInventory *DeviceInfoCache, sapID *int, cid *int) (*StateMachineAccessPoint, error) {
+func NewStateMachineAccessPoint(localDevice *LocalDeviceObject, deviceInventory *DeviceInfoCache, sapID *int, cid *int) (*StateMachineAccessPoint, error) {
log.Debug().Msgf("NewStateMachineAccessPoint localDevice=%v deviceInventory=%v sap=%v cid=%v", localDevice, deviceInventory, sapID, cid)
s := &StateMachineAccessPoint{
@@ -1873,7 +1873,7 @@ func (s *StateMachineAccessPoint) GetDeviceInfoCache() *DeviceInfoCache {
return s.deviceInventory
}
-func (s *StateMachineAccessPoint) GetLocalDevice() *local.LocalDeviceObject {
+func (s *StateMachineAccessPoint) GetLocalDevice() *LocalDeviceObject {
return s.localDevice
}
diff --git a/plc4go/internal/bacnetip/ApplicationModule.go b/plc4go/internal/bacnetip/ApplicationModule.go
index 194af18629..444ccc0a90 100644
--- a/plc4go/internal/bacnetip/ApplicationModule.go
+++ b/plc4go/internal/bacnetip/ApplicationModule.go
@@ -22,8 +22,6 @@ package bacnetip
import (
"encoding/binary"
"fmt"
- "github.com/apache/plc4x/plc4go/internal/bacnetip/local"
- "github.com/apache/plc4x/plc4go/internal/bacnetip/service"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -209,9 +207,9 @@ type Application struct {
*ApplicationServiceElement
Collector
- objectName map[string]*local.LocalDeviceObject
- objectIdentifier map[string]*local.LocalDeviceObject
- localDevice *local.LocalDeviceObject
+ objectName map[string]*LocalDeviceObject
+ objectIdentifier map[string]*LocalDeviceObject
+ localDevice *LocalDeviceObject
deviceInfoCache *DeviceInfoCache
controllers map[string]interface{}
helpers map[string]func(pdu _PDU) error
@@ -219,7 +217,7 @@ type Application struct {
_startup_disabled bool
}
-func NewApplication(localDevice *local.LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*Application, error) {
+func NewApplication(localDevice *LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*Application, error) {
log.Debug().Msgf("NewApplication localDevice=%v localAddress=%v deviceInfoCache=%s aseID=%d", localDevice, &localAddress, deviceInfoCache, aseID)
a := &Application{}
var err error
@@ -229,8 +227,8 @@ func NewApplication(localDevice *local.LocalDeviceObject, localAddress Address,
}
// local objects by ID and name
- a.objectName = map[string]*local.LocalDeviceObject{}
- a.objectIdentifier = map[string]*local.LocalDeviceObject{}
+ a.objectName = map[string]*LocalDeviceObject{}
+ a.objectIdentifier = map[string]*LocalDeviceObject{}
// keep track of the local device
if localDevice != nil {
@@ -311,7 +309,7 @@ type ApplicationIOController struct {
queueByAddress map[string]SieveQueue
}
-func NewApplicationIOController(localDevice *local.LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*ApplicationIOController, error) {
+func NewApplicationIOController(localDevice *LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*ApplicationIOController, error) {
a := &ApplicationIOController{
// queues for each address
queueByAddress: make(map[string]SieveQueue),
@@ -422,8 +420,8 @@ func (a *ApplicationIOController) Confirmation(apdu _PDU) error {
type BIPSimpleApplication struct {
*ApplicationIOController
- *service.WhoIsIAmServices
- *service.ReadWritePropertyServices
+ *WhoIsIAmServices
+ *ReadWritePropertyServices
localAddress Address
asap *ApplicationServiceAccessPoint
smap *StateMachineAccessPoint
@@ -434,15 +432,21 @@ type BIPSimpleApplication struct {
mux *UDPMultiplexer
}
-func NewBIPSimpleApplication(localDevice *local.LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*BIPSimpleApplication, error) {
+func NewBIPSimpleApplication(localDevice *LocalDeviceObject, localAddress Address, deviceInfoCache *DeviceInfoCache, aseID *int) (*BIPSimpleApplication, error) {
b := &BIPSimpleApplication{}
var err error
b.ApplicationIOController, err = NewApplicationIOController(localDevice, localAddress, deviceInfoCache, aseID)
if err != nil {
return nil, errors.Wrap(err, "error creating io controller")
}
-
- b.localAddress = localAddress
+ b.WhoIsIAmServices, err = NewWhoIsIAmServices(b)
+ if err != nil {
+ return nil, errors.Wrap(err, "error WhoIs/IAm services")
+ }
+ b.ReadWritePropertyServices, err = NewReadWritePropertyServices()
+ if err != nil {
+ return nil, errors.Wrap(err, "error read write property services")
+ }
// include a application decoder
b.asap, err = NewApplicationServiceAccessPoint(nil, nil)
diff --git a/plc4go/internal/bacnetip/Capability.go b/plc4go/internal/bacnetip/Capability.go
index c93df3c136..a4d89419e1 100644
--- a/plc4go/internal/bacnetip/Capability.go
+++ b/plc4go/internal/bacnetip/Capability.go
@@ -23,6 +23,10 @@ package bacnetip
type Capability struct {
}
+func NewCapability() *Capability {
+ return &Capability{}
+}
+
// TODO: implement
type Collector struct {
}
diff --git a/plc4go/internal/bacnetip/Device.go b/plc4go/internal/bacnetip/Device.go
new file mode 100644
index 0000000000..a98c9f57ab
--- /dev/null
+++ b/plc4go/internal/bacnetip/Device.go
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package bacnetip
+
+import (
+ "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
+ "github.com/pkg/errors"
+ "github.com/rs/zerolog/log"
+ "strconv"
+ "strings"
+)
+
+type WhoIsIAmServicesRequirements interface {
+ Request(pdu _PDU) error
+}
+
+type WhoIsIAmServices struct {
+ WhoIsIAmServicesRequirements
+ *Capability
+ localDevice *LocalDeviceObject
+}
+
+func NewWhoIsIAmServices(whoIsIAmServicesRequirements WhoIsIAmServicesRequirements) (*WhoIsIAmServices, error) {
+ w := &WhoIsIAmServices{}
+ w.WhoIsIAmServicesRequirements = whoIsIAmServicesRequirements
+ w.Capability = NewCapability()
+ return w, nil
+}
+
+func (w *WhoIsIAmServices) Startup() error {
+ log.Debug().Msg("Startup")
+
+ // send a global broadcast I-Am
+ return w.IAm(nil)
+}
+
+func (w *WhoIsIAmServices) WhoIs(lowLimit, highLimit *uint, address *Address) error {
+ log.Debug().Msg("WhoIs")
+
+ var deviceInstanceRangeLowLimit, deviceInstanceRangeHighLimit uint
+ if lowLimit != nil {
+ if highLimit == nil {
+ return errors.New("highLimit required")
+ }
+ if *lowLimit < 0 || *lowLimit > 4194303 {
+ return errors.New("lowLimit out of range")
+ }
+
+ // low limit is fine
+ deviceInstanceRangeLowLimit = *lowLimit
+ }
+ if highLimit != nil {
+ if lowLimit == nil {
+ return errors.New("lowLimit required")
+ }
+ if *highLimit < 0 || *highLimit > 4194303 {
+ return errors.New("highLimit out of range")
+ }
+
+ // low limit is fine
+ deviceInstanceRangeHighLimit = *highLimit
+ }
+
+ // Build a request
+ whoIs := model.NewBACnetUnconfirmedServiceRequestWhoIs(model.CreateBACnetContextTagUnsignedInteger(0, deviceInstanceRangeLowLimit), model.CreateBACnetContextTagUnsignedInteger(1, deviceInstanceRangeHighLimit), 0)
+
+ log.Debug().Msgf("WhoIs:\n%s", whoIs)
+
+ return w.Request(NewPDU(whoIs, WithPDUDestination(address)))
+}
+
+// DoWhoIsRequest respond to a Who-Is request.
+func (w *WhoIsIAmServices) DoWhoIsRequest(apdu _PDU) error {
+ log.Debug().Msgf("DoWhoIsRequest %s", apdu)
+
+ // ignore this if there's no local device
+ if w.localDevice == nil {
+ log.Debug().Msg("No local device")
+ }
+
+ // extract the parameters
+ var lowLimit, highLimit *uint
+ if deviceInstanceRangeLowLimit := apdu.GetMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeLowLimit(); deviceInstanceRangeLowLimit != nil {
+ _lowLimit := uint(deviceInstanceRangeLowLimit.GetActualValue())
+ lowLimit = &_lowLimit
+ }
+ if deviceInstanceRangeHighLimit := apdu.GetMessage().(model.BACnetUnconfirmedServiceRequestWhoIs).GetDeviceInstanceRangeHighLimit(); deviceInstanceRangeHighLimit != nil {
+ _highLimit := uint(deviceInstanceRangeHighLimit.GetActualValue())
+ highLimit = &_highLimit
+ }
+ // check for consistent parameters
+ if lowLimit != nil {
+ if highLimit == nil {
+ return errors.New("deviceInstanceRangeHighLimit required")
+ }
+ if *lowLimit < 0 || *lowLimit > 4194303 {
+ return errors.New("deviceInstanceRangeHighLimit out of range")
+ }
+ }
+ if highLimit != nil {
+ if lowLimit == nil {
+ return errors.New("deviceInstanceRangeLowLimit required")
+ }
+ if *highLimit < 0 || *highLimit > 4194303 {
+ return errors.New("deviceInstanceRangeHighLimit out of range")
+ }
+ }
+
+ // see we should respond
+ if lowLimit != nil {
+ if uint(w.localDevice.ObjectIdentifier[1]) < *lowLimit {
+ return nil
+ }
+ }
+ if highLimit != nil {
+ if uint(w.localDevice.ObjectIdentifier[1]) > *highLimit {
+ return nil
+ }
+ }
+
+ // generate an I-Am
+ return w.IAm(apdu.GetPDUSource())
+}
+
+func (w *WhoIsIAmServices) IAm(address *Address) error {
+ log.Debug().Msg("IAm")
+
+ // this requires a local device
+ if w.localDevice == nil {
+ log.Debug().Msg("no local device")
+ return nil
+ }
+
+ iAm := model.NewBACnetUnconfirmedServiceRequestIAm(
+ model.CreateBACnetApplicationTagObjectIdentifier(ObjectIdentifierStringToTuple(w.localDevice.ObjectIdentifier)),
+ model.CreateBACnetApplicationTagUnsignedInteger(uint(*w.localDevice.MaximumApduLengthAccepted)),
+ model.CreatBACnetSegmentationTagged(*w.localDevice.SegmentationSupported),
+ model.CreateBACnetVendorIdApplicationTagged(w.localDevice.VendorIdentifier),
+ 0,
+ )
+
+ // defaults to a global broadcast
+ if address == nil {
+ address = NewGlobalBroadcast(nil)
+ }
+ log.Debug().Msgf("IAm %v", iAm)
+
+ return w.Request(NewPDU(iAm, WithPDUDestination(address)))
+}
+
+// DoIAmRequest responds to an I-Am request.
+func (w *WhoIsIAmServices) DoIAmRequest(iam *model.BACnetUnconfirmedServiceRequestIAm) error {
+ // TODO: implement me... upstream impl empty
+ return nil
+}
+
+func ObjectIdentifierStringToTuple(objectIdentifier string) (objectType uint16, instance uint32) {
+ split := strings.Split(objectIdentifier, ":")
+ if len(split) != 2 {
+ panic("broken object identifier")
+ }
+ parsedObjectType, err := strconv.Atoi(split[0])
+ if err != nil {
+ panic(err)
+ }
+ objectType = uint16(parsedObjectType)
+ parsedInstance, err := strconv.Atoi(split[1])
+ if err != nil {
+ panic(err)
+ }
+ instance = uint32(parsedInstance)
+ return
+}
diff --git a/plc4go/internal/bacnetip/local/Device.go b/plc4go/internal/bacnetip/LocalDevice.go
similarity index 97%
rename from plc4go/internal/bacnetip/local/Device.go
rename to plc4go/internal/bacnetip/LocalDevice.go
index dc1632ac35..af32b40194 100644
--- a/plc4go/internal/bacnetip/local/Device.go
+++ b/plc4go/internal/bacnetip/LocalDevice.go
@@ -17,7 +17,7 @@
* under the License.
*/
-package local
+package bacnetip
import (
"fmt"
@@ -34,6 +34,7 @@ type LocalDeviceObject struct {
App interface{}
ObjectName string
ObjectIdentifier string
+ VendorIdentifier uint16
}
func (l *LocalDeviceObject) String() string {
diff --git a/plc4go/internal/bacnetip/MessageCodec.go b/plc4go/internal/bacnetip/MessageCodec.go
index a037367ae5..ee70b515da 100644
--- a/plc4go/internal/bacnetip/MessageCodec.go
+++ b/plc4go/internal/bacnetip/MessageCodec.go
@@ -21,7 +21,6 @@ package bacnetip
import (
"context"
- "github.com/apache/plc4x/plc4go/internal/bacnetip/local"
"github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/default"
@@ -59,7 +58,7 @@ func NewApplicationLayerMessageCodec(udpTransport *udp.Transport, transportUrl u
if err != nil {
return nil, errors.Wrap(err, "error creating address")
}
- application, err := NewBIPSimpleApplication(&local.LocalDeviceObject{}, *address, &a.deviceInfoCache, nil)
+ application, err := NewBIPSimpleApplication(&LocalDeviceObject{}, *address, &a.deviceInfoCache, nil)
if err != nil {
return nil, err
}
diff --git a/plc4go/internal/bacnetip/Object.go b/plc4go/internal/bacnetip/Object.go
index 688f472aa8..742a16176a 100644
--- a/plc4go/internal/bacnetip/Object.go
+++ b/plc4go/internal/bacnetip/Object.go
@@ -19,9 +19,10 @@
package bacnetip
-import readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model"
+type ReadWritePropertyServices struct {
+}
-func GetObjectClass(name string) readWriteModel.BACnetObjectType {
- objectTypeByName, _ := readWriteModel.BACnetObjectTypeByName(name)
- return objectTypeByName
+func NewReadWritePropertyServices() (*ReadWritePropertyServices, error) {
+ // TODO: implement me
+ return nil, nil
}
diff --git a/plc4go/internal/bacnetip/Task.go b/plc4go/internal/bacnetip/Task.go
index 5f0613e5a1..f2ea602325 100644
--- a/plc4go/internal/bacnetip/Task.go
+++ b/plc4go/internal/bacnetip/Task.go
@@ -23,7 +23,6 @@ import (
"github.com/rs/zerolog/log"
"sync"
"time"
- "unsafe"
)
type _TaskRequirements interface {
@@ -62,7 +61,7 @@ func (t *_Task) InstallTask(when *time.Time, delta *time.Duration) {
}
func (t *_Task) processTask() {
- panic("processTask must be overridden")
+ t._TaskRequirements.processTask()
}
func (t *_Task) SuspendTask() {
@@ -73,17 +72,13 @@ func (t *_Task) Resume() {
_taskManager.resumeTask(t)
}
-func (t *_Task) LowerThan(other *_Task) bool {
- return *unsafe.Pointer(t) < *unsafe.Pointer(other)
-}
-
type OneShotTask struct {
*_Task
}
func NewOneShotTask(when *time.Time) *OneShotTask {
o := &OneShotTask{}
- o._TaskRequirements = o
+ o._Task = _New_Task(o)
if when != nil {
o.taskTime = when
}
@@ -96,7 +91,7 @@ type OneShotDeleteTask struct {
func NewOneShotDeleteTask(when *time.Time) *OneShotDeleteTask {
o := &OneShotDeleteTask{}
- o._TaskRequirements = o
+ o._Task = _New_Task(o)
if when != nil {
o.taskTime = when
}
@@ -135,7 +130,7 @@ type RecurringTask struct {
func NewRecurringTask(_TaskRequirements _TaskRequirements, interval *time.Duration, offset *time.Duration) *RecurringTask {
r := &RecurringTask{}
- r._TaskRequirements = _New_Task(_TaskRequirements)
+ r._Task = _New_Task(_TaskRequirements)
// set the interval if it hasn't already been set
if interval != nil {
r.taskInterval = interval
@@ -198,6 +193,10 @@ func init() {
go func() {
for {
task, delta := _taskManager.getNextTask()
+ if task == nil {
+ time.Sleep(10 * time.Millisecond)
+ continue
+ }
_taskManager.processTask(task)
time.Sleep(delta)
}
diff --git a/plc4go/internal/bacnetip/service/Device.go b/plc4go/internal/bacnetip/service/Device.go
deleted file mode 100644
index ae8e18ff9b..0000000000
--- a/plc4go/internal/bacnetip/service/Device.go
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package service
-
-type WhoIsIAmServices struct {
-}
-
-func NewWhoIsIAmServices() (*WhoIsIAmServices, error) {
- // TODO: implement me
- return nil, nil
-}
diff --git a/plc4go/internal/bacnetip/service/Object.go b/plc4go/internal/bacnetip/service/Object.go
deleted file mode 100644
index 946f5d5fa5..0000000000
--- a/plc4go/internal/bacnetip/service/Object.go
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package service
-
-type ReadWritePropertyServices struct {
-}
-
-func NewReadWritePropertyServices() (*ReadWritePropertyServices, error) {
- // TODO: implement me
- return nil, nil
-}