From 37a4aedb2016fbbd449350ec094aebbd666f6cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20R=C3=BChl?= Date: Mon, 7 Aug 2023 13:41:09 +0200 Subject: [PATCH] fix(plc4go/opcua): fixed issue regarding sending of messages --- plc4go/internal/opcua/MessageCodec.go | 15 ++++++++++----- plc4go/internal/opcua/SecureChannel.go | 25 +------------------------ 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/plc4go/internal/opcua/MessageCodec.go b/plc4go/internal/opcua/MessageCodec.go index 28cb83189a7..962fb4e66eb 100644 --- a/plc4go/internal/opcua/MessageCodec.go +++ b/plc4go/internal/opcua/MessageCodec.go @@ -66,14 +66,18 @@ func (m *MessageCodec) Connect() error { func (m *MessageCodec) Send(message spi.Message) error { m.log.Trace().Stringer("message", message).Msg("Sending message") // Cast the message to the correct type of struct - messagePdu, ok := message.(readWriteModel.MessagePDU) + opcuaApu, ok := message.(readWriteModel.OpcuaAPU) if !ok { - return errors.Errorf("Invalid message type %T", message) + if message, ok := message.(readWriteModel.MessagePDU); ok { + opcuaApu = readWriteModel.NewOpcuaAPU(message, false) + } else { + return errors.Errorf("Invalid message type %T", message) + } } // Serialize the request wbbb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian)) - if err := messagePdu.SerializeWithWriteBuffer(context.Background(), wbbb); err != nil { + if err := opcuaApu.SerializeWithWriteBuffer(context.Background(), wbbb); err != nil { return errors.Wrap(err, "error serializing request") } theBytes := wbbb.GetBytes() @@ -82,6 +86,7 @@ func (m *MessageCodec) Send(message spi.Message) error { if err := m.GetTransportInstance().Write(theBytes); err != nil { return errors.Wrap(err, "error sending request") } + m.log.Trace().Msg("bytes written to transport instance") return nil } @@ -118,10 +123,10 @@ func (m *MessageCodec) Receive() (spi.Message, error) { } ctxForModel := options.GetLoggerContextForModel(context.Background(), m.log, options.WithPassLoggerToModel(m.passLogToModel)) rbbb := utils.NewReadBufferByteBased(readBytes, utils.WithByteOrderForReadBufferByteBased(binary.LittleEndian)) - messagePdu, err := readWriteModel.MessagePDUParseWithBuffer(ctxForModel, rbbb, true) + opcuaAPU, err := readWriteModel.OpcuaAPUParseWithBuffer(ctxForModel, rbbb, true) if err != nil { return nil, errors.New("Could not parse pdu") } - return messagePdu, nil + return opcuaAPU, nil } diff --git a/plc4go/internal/opcua/SecureChannel.go b/plc4go/internal/opcua/SecureChannel.go index cdb1a1c58d1..03f842003f3 100644 --- a/plc4go/internal/opcua/SecureChannel.go +++ b/plc4go/internal/opcua/SecureChannel.go @@ -138,6 +138,7 @@ func NewSecureChannel(log zerolog.Logger, ctx DriverContext, configuration Confi password: configuration.password, securityPolicy: "http://opcfoundation.org/UA/SecurityPolicy#" + configuration.securityPolicy, sessionName: "UaSession:" + APPLICATION_TEXT.GetStringValue() + ":" + uniuri.NewLen(20), + authenticationToken: readWriteModel.NewNodeIdTwoByte(0), clientNonce: []byte(uniuri.NewLen(40)), keyStoreFile: configuration.keyStoreFile, channelTransactionManager: NewSecureChannelTransactionManager(log), @@ -339,9 +340,6 @@ func (s *SecureChannel) onConnect(ctx context.Context, connection *Connection, c func (s *SecureChannel) onConnectOpenSecureChannel(ctx context.Context, connection *Connection, ch chan plc4go.PlcConnectionConnectResult, response readWriteModel.OpcuaAcknowledgeResponse) { transactionId := s.channelTransactionManager.getTransactionIdentifier() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -493,9 +491,6 @@ func (s *SecureChannel) onConnectOpenSecureChannel(ctx context.Context, connecti } func (s *SecureChannel) onConnectCreateSessionRequest(ctx context.Context, connection *Connection, ch chan plc4go.PlcConnectionConnectResult) { - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -638,9 +633,6 @@ func (s *SecureChannel) onConnectActivateSessionRequest(ctx context.Context, con requestHandle := s.getRequestHandle() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -828,9 +820,6 @@ func (s *SecureChannel) onDisconnect(ctx context.Context, connection *Connection func (s *SecureChannel) onDisconnectCloseSecureChannel(ctx context.Context, connection *Connection, message readWriteModel.CloseSessionResponseExactly, response readWriteModel.CloseSessionResponse) { transactionId := s.channelTransactionManager.getTransactionIdentifier() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -968,9 +957,6 @@ func (s *SecureChannel) onDiscover(ctx context.Context, codec *MessageCodec) { func (s *SecureChannel) onDiscoverOpenSecureChannel(ctx context.Context, codec *MessageCodec, opcuaAcknowledgeResponse readWriteModel.OpcuaAcknowledgeResponse) { transactionId := s.channelTransactionManager.getTransactionIdentifier() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -1101,9 +1087,6 @@ func (s *SecureChannel) onDiscoverGetEndpointsRequest(ctx context.Context, codec return } - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -1230,9 +1213,6 @@ func (s *SecureChannel) onDiscoverGetEndpointsRequest(ctx context.Context, codec func (s *SecureChannel) onDiscoverCloseSecureChannel(ctx context.Context, codec *MessageCodec, response readWriteModel.GetEndpointsResponse) { transactionId := s.channelTransactionManager.getTransactionIdentifier() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(), @@ -1333,9 +1313,6 @@ func (s *SecureChannel) keepAlive() { transactionId := s.channelTransactionManager.getTransactionIdentifier() - if s.authenticationToken == nil { - panic("authenticationToken should be set at this point") - } requestHeader := readWriteModel.NewRequestHeader( s.getAuthenticationToken(), s.getCurrentDateTime(),