Skip to content

Commit

Permalink
fix(plc4go/opcua): fixed issue regarding sending of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 7, 2023
1 parent a8c6d26 commit 37a4aed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
15 changes: 10 additions & 5 deletions plc4go/internal/opcua/MessageCodec.go
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
25 changes: 1 addition & 24 deletions plc4go/internal/opcua/SecureChannel.go
Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 37a4aed

Please sign in to comment.