Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPC UA 兼容 v1.14.0 的 open62541 #87

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions modules/opcua/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#ifdef UA_ENABLE_PUBSUB

#include <open62541/plugin/log_stdout.h>
#if OPCUA_VERSION < 10400
#include <open62541/plugin/pubsub_udp.h>
#endif
#include <open62541/server_config_default.h>

#ifdef UA_ENABLE_PUBSUB_MQTT
Expand All @@ -36,7 +38,9 @@ Publisher<TransportID::UDP_UADP>::Publisher(const std::string &pub_name, const s
const std::vector<UserConfig> &users) : Server(port, pub_name, users), _name(pub_name)
{
//////////////////// 添加连接配置 ////////////////////
#if OPCUA_VERSION < 10400
UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP());
#endif
UA_PubSubConnectionConfig connect_config{};
std::string cn_name_str = _name + "Connection";
connect_config.name = UA_STRING(helper::to_char(cn_name_str));
Expand All @@ -46,7 +50,13 @@ Publisher<TransportID::UDP_UADP>::Publisher(const std::string &pub_name, const s
UA_NetworkAddressUrlDataType address_url{UA_STRING_NULL, UA_STRING(helper::to_char(url_str))};
UA_Variant_setScalar(&connect_config.address, &address_url, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
// 用哈希值作为发布者 ID
connect_config.publisherId.numeric = _strhash(_name + "Connection") % 0x8000000u;
#if OPCUA_VERSION >= 10400
connect_config.publisherIdType = UA_PUBLISHERIDTYPE_UINT16;
connect_config.publisherId.uint16 = _strhash(_name + "Connection") % 0x4000u;
#else
connect_config.publisherIdType = UA_PUBSUB_PUBLISHERID_NUMERIC;
connect_config.publisherId.numeric = _strhash(_name + "Connection") % 0x4000u;
#endif
auto status = UA_Server_addPubSubConnection(_server, &connect_config, &_connection_id);
if (status != UA_STATUSCODE_GOOD)
{
Expand Down Expand Up @@ -106,7 +116,7 @@ bool Publisher<TransportID::UDP_UADP>::publish(const std::vector<PublishedDataSe
wg_config.name = UA_STRING(helper::to_char(wg_name_str));
wg_config.publishingInterval = duration;
wg_config.enabled = UA_FALSE;
wg_config.writerGroupId = _strhash(_name + "WriterGroup") % 0x8000u;
wg_config.writerGroupId = 0x4000u + _strhash(_name + "WriterGroup") % 0x4000u;
wg_config.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
wg_config.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
wg_config.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
Expand Down Expand Up @@ -134,7 +144,7 @@ bool Publisher<TransportID::UDP_UADP>::publish(const std::vector<PublishedDataSe
UA_DataSetWriterConfig dsw_config{};
std::string dsw_name_str = _name + "DataSetWriter";
dsw_config.name = UA_STRING(helper::to_char(dsw_name_str));
dsw_config.dataSetWriterId = _strhash(_name + "DataSetWriter") % 0x8000u;
dsw_config.dataSetWriterId = 0x8000u + _strhash(_name + "DataSetWriter") % 0x4000u;
dsw_config.keyFrameCount = para::opcua_param.KEY_FRAME_COUNT;
status = UA_Server_addDataSetWriter(_server, _wg_id, _pds_id, &dsw_config, &_dsw_id);
if (status != UA_STATUSCODE_GOOD)
Expand Down
4 changes: 3 additions & 1 deletion modules/opcua/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ Server::Server(uint16_t port, std::string_view name, const std::vector<UserConfi
}
// 配置
config->accessControl.clear(&config->accessControl);
#if OPCUA_VERSION >= 10300
#if OPCUA_VERSION >= 10400
UA_AccessControl_default(config, false, nullptr, usr_passwd.size(), usr_passwd.data());
#elif OPCUA_VERSION >= 10300 && OPCUA_VERSION < 10400
UA_AccessControl_default(config, false, nullptr,
&config->securityPolicies[config->securityPoliciesSize - 1].policyUri,
usr_passwd.size(), usr_passwd.data());
Expand Down
14 changes: 11 additions & 3 deletions modules/opcua/src/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#ifdef UA_ENABLE_PUBSUB

#include <open62541/plugin/log_stdout.h>
#if OPCUA_VERSION < 10400
#include <open62541/plugin/pubsub_udp.h>
#endif
#include <open62541/server_config_default.h>

#ifdef UA_ENABLE_PUBSUB_MQTT
Expand All @@ -33,15 +35,21 @@ Subscriber<TransportID::UDP_UADP>::Subscriber(const std::string &sub_name, const
const std::vector<UserConfig> &users) : Server(port, sub_name, users), _name(sub_name)
{
//////////////////// 添加连接配置 ////////////////////
#if OPCUA_VERSION < 10400
UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP());
#endif
UA_PubSubConnectionConfig connect_config{};
std::string cn_name_str = _name + "Connection";
connect_config.name = UA_STRING(helper::to_char(cn_name_str));
connect_config.transportProfileUri = UA_STRING(const_cast<char *>("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp"));
connect_config.enabled = UA_TRUE;
UA_NetworkAddressUrlDataType address_url{UA_STRING_NULL, UA_STRING(helper::to_char(address))};
UA_Variant_setScalarCopy(&connect_config.address, &address_url, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
#if OPCUA_VERSION >= 10400
connect_config.publisherId.uint32 = UA_UInt32_random();
#else
connect_config.publisherId.numeric = UA_UInt32_random();
#endif
auto status = UA_Server_addPubSubConnection(_server, &connect_config, &_connection_id);
if (status != UA_STATUSCODE_GOOD)
{
Expand Down Expand Up @@ -74,10 +82,10 @@ std::vector<UA_NodeId> Subscriber<TransportID::UDP_UADP>::subscribe(const std::s
UA_DataSetReaderConfig dsr_config{};
std::string dsr_name = pub_name + "DataSetReader";
dsr_config.name = UA_STRING(helper::to_char(dsr_name));
UA_UInt32 publisher_id = _strhash(pub_name + "Connection") % 0x8000000u;
UA_UInt16 publisher_id = _strhash(pub_name + "Connection") % 0x4000u;
UA_Variant_setScalar(&dsr_config.publisherId, &publisher_id, &UA_TYPES[UA_TYPES_UINT16]);
dsr_config.writerGroupId = _strhash(pub_name + "WriterGroup") % 0x8000u;
dsr_config.dataSetWriterId = _strhash(pub_name + "DataSetWriter") % 0x8000u;
dsr_config.writerGroupId = 0x4000u + _strhash(pub_name + "WriterGroup") % 0x4000u;
dsr_config.dataSetWriterId = 0x8000u + _strhash(pub_name + "DataSetWriter") % 0x4000u;

// 设置数 DSR 中的元数据配置
std::string dataset_name = _name + "DataSetMetaData";
Expand Down
Loading