Skip to content

Commit

Permalink
Fix compile errors in golang opcua driver.
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <[email protected]>
  • Loading branch information
splatch committed Sep 24, 2024
1 parent 4956fa1 commit c56b51e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 56 deletions.
21 changes: 5 additions & 16 deletions plc4go/internal/opcua/Reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ package opcua
import (
"context"
"encoding/binary"
"runtime/debug"
"strconv"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"runtime/debug"

apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/opcua/readwrite/model"
Expand Down Expand Up @@ -96,32 +94,23 @@ func (m *Reader) readSync(ctx context.Context, readRequest apiModel.PlcReadReque
readWriteModel.TimestampsToReturn_timestampsToReturnNeither,
readValueArray)

identifier, err := strconv.ParseUint(opcuaReadRequest.GetExtensionId(), 10, 16)
if err != nil {
result <- spiModel.NewDefaultPlcReadRequestResult(readRequest, nil, errors.Wrapf(err, "error parsing identifier"))
return
}

identifier := opcuaReadRequest.GetExtensionId()
expandedNodeId := readWriteModel.NewExpandedNodeId(false, //Namespace Uri Specified
false, //Server Index Specified
readWriteModel.NewNodeIdFourByte(0, uint16(identifier)),
nil,
nil)

extObject := readWriteModel.NewExtensionObject(
expandedNodeId,
nil,
opcuaReadRequest,
false)
extObject := readWriteModel.NewExtensiblePayload(readWriteModel.NewRootExtensionObject(opcuaReadRequest, expandedNodeId, identifier), nil, 0)

buffer := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
if err = extObject.SerializeWithWriteBuffer(ctx, buffer); err != nil {
if err := extObject.SerializeWithWriteBuffer(ctx, buffer); err != nil {
result <- spiModel.NewDefaultPlcReadRequestResult(readRequest, nil, errors.Wrapf(err, "Unable to serialise the ReadRequest"))
return
}

consumer := func(opcuaResponse []byte) {
reply, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
reply, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
result <- spiModel.NewDefaultPlcReadRequestResult(readRequest, nil, errors.Wrapf(err, "Unable to read the reply"))
return
Expand Down
30 changes: 15 additions & 15 deletions plc4go/internal/opcua/SecureChannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
)

var (
SECURITY_POLICY_NONE = readWriteModel.NewPascalString(utils.ToPtr("http://opcfoundation.org/UA/SecurityPolicy#None"))
SECURITY_POLICY_NONE = readWriteModel.NewPascalString(utils.MakePtr("http://opcfoundation.org/UA/SecurityPolicy#None"))
NULL_STRING = readWriteModel.NewPascalString(nil)
NULL_BYTE_STRING = readWriteModel.NewPascalByteString(-1, nil)
NULL_EXPANDED_NODEID = readWriteModel.NewExpandedNodeId(false,
Expand All @@ -73,9 +73,9 @@ var (
INET_ADDRESS_PATTERN = regexp.MustCompile(`(.(?P<transportCode>tcp))?://(?P<transportHost>[\w.-]+)(:(?P<transportPort>\d*))?`)

URI_PATTERN = regexp.MustCompile(`^(?P<protocolCode>opc)` + INET_ADDRESS_PATTERN.String() + `(?P<transportEndpoint>[\w/=]*)[?]?`)
APPLICATION_URI = readWriteModel.NewPascalString(utils.ToPtr("urn:apache:plc4x:client"))
PRODUCT_URI = readWriteModel.NewPascalString(utils.ToPtr("urn:apache:plc4x:client"))
APPLICATION_TEXT = readWriteModel.NewPascalString(utils.ToPtr("OPCUA client for the Apache PLC4X:PLC4J project"))
APPLICATION_URI = readWriteModel.NewPascalString(utils.MakePtr("urn:apache:plc4x:client"))
PRODUCT_URI = readWriteModel.NewPascalString(utils.MakePtr("urn:apache:plc4x:client"))
APPLICATION_TEXT = readWriteModel.NewPascalString(utils.MakePtr("OPCUA client for the Apache PLC4X:PLC4J project"))
DEFAULT_CONNECTION_LIFETIME = uint32(36000000)
)

Expand Down Expand Up @@ -439,7 +439,7 @@ func (s *SecureChannel) onConnectOpenSecureChannel(ctx context.Context, connecti
messagePDU := opcuaAPU.GetMessage()
opcuaOpenResponse := messagePDU.(readWriteModel.OpcuaOpenResponse)
readBuffer := utils.NewReadBufferByteBased(opcuaOpenResponse.(readWriteModel.BinaryPayload).GetPayload(), utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian))
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, readBuffer, false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, readBuffer, false)
if err != nil {
return errors.Wrap(err, "error parsing")
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func (s *SecureChannel) onConnectCreateSessionRequest(ctx context.Context, conne
applicationName := readWriteModel.NewLocalizedText(
true,
true,
readWriteModel.NewPascalString(utils.ToPtr("en")),
readWriteModel.NewPascalString(utils.MakePtr("en")),
APPLICATION_TEXT)

var discoveryUrls []readWriteModel.PascalString
Expand Down Expand Up @@ -541,7 +541,7 @@ func (s *SecureChannel) onConnectCreateSessionRequest(ctx context.Context, conne
}

consumer := func(opcuaResponse []byte) {
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
s.log.Error().Err(err).Msg("error parsing")
connection.fireConnectionError(err, ch)
Expand Down Expand Up @@ -655,7 +655,7 @@ func (s *SecureChannel) onConnectActivateSessionRequest(ctx context.Context, con
}

consumer := func(opcuaResponse []byte) {
message, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
message, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
s.log.Error().Err(err).Msg("error parsing")
return
Expand All @@ -673,7 +673,7 @@ func (s *SecureChannel) onConnectActivateSessionRequest(ctx context.Context, con
}
s.log.Debug().Msg("Got Activate Session Response Connection Response")

extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
s.log.Error().Err(err).Msg("error parsing")
return
Expand Down Expand Up @@ -751,7 +751,7 @@ func (s *SecureChannel) onDisconnect(ctx context.Context, connection *Connection
}

consumer := func(opcuaResponse []byte) {
message, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
message, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
s.log.Error().Err(err).Msg("error parsing")
return
Expand All @@ -768,7 +768,7 @@ func (s *SecureChannel) onDisconnect(ctx context.Context, connection *Connection
}
s.log.Debug().Msg("Got Close Session Response Connection Response")

extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
s.log.Error().Err(err).Msg("error parsing")
return
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func (s *SecureChannel) onDiscoverOpenSecureChannel(ctx context.Context, codec *
messagePDU := opcuaAPU.GetMessage()
opcuaOpenResponse := messagePDU.(readWriteModel.OpcuaOpenResponse)
readBuffer := utils.NewReadBufferByteBased(opcuaOpenResponse.(readWriteModel.BinaryPayload).GetPayload(), utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian))
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, readBuffer, false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, readBuffer, false)
if err != nil {
return errors.Wrap(err, "error parsing")
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@ func (s *SecureChannel) onDiscoverGetEndpointsRequest(ctx context.Context, codec
messagePDU := opcuaAPU.GetMessage()
messageResponse := messagePDU.(readWriteModel.OpcuaMessageResponse)
readBuffer := utils.NewReadBufferByteBased(messageResponse.(readWriteModel.BinaryPayload).GetPayload(), utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian))
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, readBuffer, false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, readBuffer, false)
if err != nil {
return errors.Wrap(err, "error parsing")
}
Expand Down Expand Up @@ -1388,7 +1388,7 @@ func (s *SecureChannel) keepAlive() {
messagePDU := opcuaAPU.GetMessage()
opcuaOpenResponse := messagePDU.(readWriteModel.OpcuaOpenResponse)
readBuffer := utils.NewReadBufferByteBased(opcuaOpenResponse.(readWriteModel.BinaryPayload).GetPayload(), utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian))
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, readBuffer, false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, readBuffer, false)
if err != nil {
return errors.Wrap(err, "error parsing")
}
Expand Down Expand Up @@ -1584,7 +1584,7 @@ func (s *SecureChannel) getIdentityToken(tokenType readWriteModel.UserTokenType,
readWriteModel.NewPascalString(policyId),
readWriteModel.NewPascalString(&s.username),
readWriteModel.NewPascalByteString(int32(len(encryptedPassword)), encryptedPassword),
readWriteModel.NewPascalString(utils.ToPtr(PASSWORD_ENCRYPTION_ALGORITHM)),
readWriteModel.NewPascalString(utils.MakePtr(PASSWORD_ENCRYPTION_ALGORITHM)),
)
extExpandedNodeId := readWriteModel.NewExpandedNodeId(
false, //Namespace Uri Specified
Expand Down
16 changes: 5 additions & 11 deletions plc4go/internal/opcua/Subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"context"
"encoding/binary"
"runtime/debug"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -143,21 +142,16 @@ func (s *Subscriber) onSubscribeCreateSubscription(ctx context.Context, cycleTim
0,
)

identifier, err := strconv.ParseUint(createSubscriptionRequest.GetExtensionId(), 10, 16)
if err != nil {
return nil, errors.Wrapf(err, "error parsing identifier")
}
identifier := createSubscriptionRequest.GetExtensionId()
expandedNodeId := readWriteModel.NewExpandedNodeId(false, //Namespace Uri Specified
false, //Server Index Specified
readWriteModel.NewNodeIdFourByte(0, uint16(identifier)),
nil,
nil)

extObject := readWriteModel.NewExtensionObject(
expandedNodeId,
nil,
createSubscriptionRequest,
false)
extObject := readWriteModel.NewExtensiblePayload(readWriteModel.NewRootExtensionObject(
createSubscriptionRequest, expandedNodeId, createSubscriptionRequest.GetExtensionId(),
), nil, 0)

buffer := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
if err := extObject.SerializeWithWriteBuffer(ctx, buffer); err != nil {
Expand All @@ -168,7 +162,7 @@ func (s *Subscriber) onSubscribeCreateSubscription(ctx context.Context, cycleTim
errorChan := make(chan error, 100) // TODO: bit oversized to not block anything. Discards errors
/* Functional Consumer example using inner class */
consumer := func(opcuaResponse []byte) {
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer(ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
extensionObject, err := readWriteModel.ExtensionObjectParseWithBuffer[readWriteModel.ExtensionObject](ctx, utils.NewReadBufferByteBased(opcuaResponse, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)), false)
if err != nil {
errorChan <- errors.Wrap(err, "error Parsing")
return
Expand Down
23 changes: 9 additions & 14 deletions plc4go/internal/opcua/Writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ package opcua
import (
"context"
"encoding/binary"
"runtime/debug"
"strconv"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"runtime/debug"

apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
Expand Down Expand Up @@ -115,25 +113,22 @@ func (m *Writer) WriteSync(ctx context.Context, writeRequest apiModel.PlcWriteRe
writeValueArray,
)

identifier, err := strconv.ParseUint(opcuaWriteRequest.GetExtensionId(), 10, 16)
if err != nil {
result <- spiModel.NewDefaultPlcWriteRequestResult(writeRequest, nil, errors.Wrapf(err, "error parsing identifier"))
return
}
identifier := opcuaWriteRequest.GetExtensionId()
expandedNodeId := readWriteModel.NewExpandedNodeId(false, //Namespace Uri Specified
false, //Server Index Specified
readWriteModel.NewNodeIdFourByte(0, uint16(identifier)),
nil,
nil)

extObject := readWriteModel.NewExtensionObject(
expandedNodeId,
nil,
opcuaWriteRequest,
false)
extObject := readWriteModel.NewExtensionObject(expandedNodeId)

//expandedNodeId,
//nil,
//opcuaWriteRequest,
//false)

buffer := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
if err = extObject.SerializeWithWriteBuffer(ctx, buffer); err != nil {
if err := extObject.SerializeWithWriteBuffer(ctx, buffer); err != nil {
result <- spiModel.NewDefaultPlcWriteRequestResult(writeRequest, nil, errors.Wrapf(err, "Unable to serialise the ReadRequest"))
return
}
Expand Down

0 comments on commit c56b51e

Please sign in to comment.