ua_pubsub_manager.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright (c) 2017-2019 Fraunhofer IOSB (Author: Andreas Ebner)
  6. * Copyright (c) 2018 Fraunhofer IOSB (Author: Julius Pfrommer)
  7. */
  8. #include "server/ua_server_internal.h"
  9. #include "ua_pubsub_ns0.h"
  10. #ifdef UA_ENABLE_PUBSUB /* conditional compilation */
  11. #define UA_DATETIMESTAMP_2000 125911584000000000
  12. UA_StatusCode
  13. UA_Server_addPubSubConnection(UA_Server *server,
  14. const UA_PubSubConnectionConfig *connectionConfig,
  15. UA_NodeId *connectionIdentifier) {
  16. /* Find the matching UA_PubSubTransportLayers */
  17. UA_PubSubTransportLayer *tl = NULL;
  18. for(size_t i = 0; i < server->config.pubsubTransportLayersSize; i++) {
  19. if(connectionConfig &&
  20. UA_String_equal(&server->config.pubsubTransportLayers[i].transportProfileUri,
  21. &connectionConfig->transportProfileUri)) {
  22. tl = &server->config.pubsubTransportLayers[i];
  23. }
  24. }
  25. if(!tl) {
  26. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  27. "PubSub Connection creation failed. Requested transport layer not found.");
  28. return UA_STATUSCODE_BADNOTFOUND;
  29. }
  30. /* Create a copy of the connection config */
  31. UA_PubSubConnectionConfig *tmpConnectionConfig = (UA_PubSubConnectionConfig *)
  32. UA_calloc(1, sizeof(UA_PubSubConnectionConfig));
  33. if(!tmpConnectionConfig){
  34. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  35. "PubSub Connection creation failed. Out of Memory.");
  36. return UA_STATUSCODE_BADOUTOFMEMORY;
  37. }
  38. UA_StatusCode retval = UA_PubSubConnectionConfig_copy(connectionConfig, tmpConnectionConfig);
  39. if(retval != UA_STATUSCODE_GOOD){
  40. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  41. "PubSub Connection creation failed. Could not copy the config.");
  42. return retval;
  43. }
  44. /* Create new connection and add to UA_PubSubManager */
  45. UA_PubSubConnection *newConnectionsField = (UA_PubSubConnection *)
  46. UA_calloc(1, sizeof(UA_PubSubConnection));
  47. if(!newConnectionsField) {
  48. UA_PubSubConnectionConfig_clear(tmpConnectionConfig);
  49. UA_free(tmpConnectionConfig);
  50. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  51. "PubSub Connection creation failed. Out of Memory.");
  52. return UA_STATUSCODE_BADOUTOFMEMORY;
  53. }
  54. if (server->pubSubManager.connectionsSize != 0)
  55. TAILQ_INSERT_TAIL(&server->pubSubManager.connections, newConnectionsField, listEntry);
  56. else {
  57. TAILQ_INIT(&server->pubSubManager.connections);
  58. TAILQ_INSERT_HEAD(&server->pubSubManager.connections, newConnectionsField, listEntry);
  59. }
  60. server->pubSubManager.connectionsSize++;
  61. LIST_INIT(&newConnectionsField->writerGroups);
  62. newConnectionsField->config = tmpConnectionConfig;
  63. /* Open the channel */
  64. newConnectionsField->channel = tl->createPubSubChannel(newConnectionsField->config);
  65. if(!newConnectionsField->channel) {
  66. UA_PubSubConnection_clear(server, newConnectionsField);
  67. TAILQ_REMOVE(&server->pubSubManager.connections, newConnectionsField, listEntry);
  68. server->pubSubManager.connectionsSize--;
  69. UA_free(newConnectionsField);
  70. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  71. "PubSub Connection creation failed. Transport layer creation problem.");
  72. return UA_STATUSCODE_BADINTERNALERROR;
  73. }
  74. UA_PubSubManager_generateUniqueNodeId(server, &newConnectionsField->identifier);
  75. if(connectionIdentifier)
  76. UA_NodeId_copy(&newConnectionsField->identifier, connectionIdentifier);
  77. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  78. addPubSubConnectionRepresentation(server, newConnectionsField);
  79. #endif
  80. return UA_STATUSCODE_GOOD;
  81. }
  82. UA_StatusCode
  83. UA_Server_removePubSubConnection(UA_Server *server, const UA_NodeId connection) {
  84. //search the identified Connection and store the Connection index
  85. UA_PubSubConnection *currentConnection = UA_PubSubConnection_findConnectionbyId(server, connection);
  86. if(!currentConnection)
  87. return UA_STATUSCODE_BADNOTFOUND;
  88. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  89. removePubSubConnectionRepresentation(server, currentConnection);
  90. #endif
  91. server->pubSubManager.connectionsSize--;
  92. UA_PubSubConnection_clear(server, currentConnection);
  93. TAILQ_REMOVE(&server->pubSubManager.connections, currentConnection, listEntry);
  94. UA_free(currentConnection);
  95. return UA_STATUSCODE_GOOD;
  96. }
  97. UA_AddPublishedDataSetResult
  98. UA_Server_addPublishedDataSet(UA_Server *server, const UA_PublishedDataSetConfig *publishedDataSetConfig,
  99. UA_NodeId *pdsIdentifier) {
  100. UA_AddPublishedDataSetResult result = {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  101. if(!publishedDataSetConfig){
  102. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  103. "PublishedDataSet creation failed. No config passed in.");
  104. return result;
  105. }
  106. if(publishedDataSetConfig->publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS){
  107. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  108. "PublishedDataSet creation failed. Unsupported PublishedDataSet type.");
  109. return result;
  110. }
  111. //deep copy the given connection config
  112. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  113. memset(&tmpPublishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  114. if(UA_PublishedDataSetConfig_copy(publishedDataSetConfig, &tmpPublishedDataSetConfig) != UA_STATUSCODE_GOOD){
  115. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  116. "PublishedDataSet creation failed. Configuration copy failed.");
  117. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  118. return result;
  119. }
  120. //create new PDS and add to UA_PubSubManager
  121. UA_PublishedDataSet *newPubSubDataSetField = (UA_PublishedDataSet *)
  122. UA_calloc(1, sizeof(UA_PublishedDataSet));
  123. if(!newPubSubDataSetField) {
  124. UA_PublishedDataSetConfig_clear(&tmpPublishedDataSetConfig);
  125. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  126. "PublishedDataSet creation failed. Out of Memory.");
  127. result.addResult = UA_STATUSCODE_BADOUTOFMEMORY;
  128. return result;
  129. }
  130. memset(newPubSubDataSetField, 0, sizeof(UA_PublishedDataSet));
  131. TAILQ_INIT(&newPubSubDataSetField->fields);
  132. newPubSubDataSetField->config = tmpPublishedDataSetConfig;
  133. if (server->pubSubManager.publishedDataSetsSize != 0)
  134. TAILQ_INSERT_TAIL(&server->pubSubManager.publishedDataSets, newPubSubDataSetField, listEntry);
  135. else {
  136. TAILQ_INIT(&server->pubSubManager.publishedDataSets);
  137. TAILQ_INSERT_HEAD(&server->pubSubManager.publishedDataSets, newPubSubDataSetField, listEntry);
  138. }
  139. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  140. //parse template config and add fields (later PubSub batch)
  141. }
  142. //generate unique nodeId
  143. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSetField->identifier);
  144. if(pdsIdentifier != NULL){
  145. UA_NodeId_copy(&newPubSubDataSetField->identifier, pdsIdentifier);
  146. }
  147. result.addResult = UA_STATUSCODE_GOOD;
  148. result.fieldAddResults = NULL;
  149. result.fieldAddResultsSize = 0;
  150. //fill the DataSetMetaData
  151. switch(tmpPublishedDataSetConfig.publishedDataSetType){
  152. case UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE:
  153. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.itemsTemplate.metaData,
  154. &newPubSubDataSetField->dataSetMetaData) != UA_STATUSCODE_GOOD){
  155. UA_Server_removeDataSetField(server, newPubSubDataSetField->identifier);
  156. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  157. }
  158. break;
  159. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS_TEMPLATE:
  160. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.eventTemplate.metaData,
  161. &newPubSubDataSetField->dataSetMetaData) != UA_STATUSCODE_GOOD){
  162. UA_Server_removeDataSetField(server, newPubSubDataSetField->identifier);
  163. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  164. }
  165. break;
  166. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS:
  167. newPubSubDataSetField->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  168. newPubSubDataSetField->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  169. newPubSubDataSetField->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  170. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSetField->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  171. UA_Server_removeDataSetField(server, newPubSubDataSetField->identifier);
  172. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  173. }
  174. newPubSubDataSetField->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  175. break;
  176. case UA_PUBSUB_DATASET_PUBLISHEDITEMS:
  177. newPubSubDataSetField->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  178. newPubSubDataSetField->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  179. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSetField->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  180. UA_Server_removeDataSetField(server, newPubSubDataSetField->identifier);
  181. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  182. }
  183. newPubSubDataSetField->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  184. newPubSubDataSetField->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  185. break;
  186. }
  187. server->pubSubManager.publishedDataSetsSize++;
  188. result.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  189. result.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  190. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  191. addPublishedDataItemsRepresentation(server, newPubSubDataSetField);
  192. #endif
  193. return result;
  194. }
  195. UA_StatusCode
  196. UA_Server_removePublishedDataSet(UA_Server *server, const UA_NodeId pds) {
  197. //search the identified PublishedDataSet and store the PDS index
  198. UA_PublishedDataSet *publishedDataSet = UA_PublishedDataSet_findPDSbyId(server, pds);
  199. if(!publishedDataSet){
  200. return UA_STATUSCODE_BADNOTFOUND;
  201. }
  202. if(publishedDataSet->config.configurationFrozen){
  203. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  204. "Remove PublishedDataSet failed. PublishedDataSet is frozen.");
  205. return UA_STATUSCODE_BADCONFIGURATIONERROR;
  206. }
  207. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  208. UA_PubSubConnection *tmpConnectoin;
  209. TAILQ_FOREACH(tmpConnectoin, &server->pubSubManager.connections, listEntry){
  210. UA_WriterGroup *writerGroup;
  211. LIST_FOREACH(writerGroup, &tmpConnectoin->writerGroups, listEntry){
  212. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  213. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  214. if(UA_NodeId_equal(&currentWriter->connectedDataSet, &publishedDataSet->identifier)){
  215. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  216. }
  217. }
  218. }
  219. }
  220. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  221. removePublishedDataSetRepresentation(server, publishedDataSet);
  222. #endif
  223. UA_PublishedDataSet_clear(server, publishedDataSet);
  224. server->pubSubManager.publishedDataSetsSize--;
  225. TAILQ_REMOVE(&server->pubSubManager.publishedDataSets, publishedDataSet, listEntry);
  226. UA_free(publishedDataSet);
  227. return UA_STATUSCODE_GOOD;
  228. }
  229. /* Calculate the time difference between current time and UTC (00:00) on January
  230. * 1, 2000. */
  231. UA_UInt32
  232. UA_PubSubConfigurationVersionTimeDifference() {
  233. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  234. return timeDiffSince2000;
  235. }
  236. /* Generate a new unique NodeId. This NodeId will be used for the information
  237. * model representation of PubSub entities. */
  238. void
  239. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  240. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  241. UA_Node *newNode = UA_NODESTORE_NEW(server, UA_NODECLASS_OBJECT);
  242. UA_NODESTORE_INSERT(server, newNode, &newNodeId);
  243. UA_NodeId_copy(&newNodeId, nodeId);
  244. }
  245. /* Delete the current PubSub configuration including all nested members. This
  246. * action also delete the configured PubSub transport Layers. */
  247. void
  248. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  249. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  250. /* Stop and unfreeze all WriterGroups */
  251. UA_PubSubConnection *tmpConnection;
  252. TAILQ_FOREACH(tmpConnection, &server->pubSubManager.connections, listEntry){
  253. for(size_t i = 0; i < pubSubManager->connectionsSize; i++) {
  254. UA_WriterGroup *writerGroup;
  255. LIST_FOREACH(writerGroup, &tmpConnection->writerGroups, listEntry) {
  256. UA_WriterGroup_setPubSubState(server, UA_PUBSUBSTATE_DISABLED, writerGroup);
  257. UA_Server_unfreezeWriterGroupConfiguration(server, writerGroup->identifier);
  258. }
  259. }
  260. }
  261. //free the currently configured transport layers
  262. UA_free(server->config.pubsubTransportLayers);
  263. server->config.pubsubTransportLayersSize = 0;
  264. //remove Connections and WriterGroups
  265. UA_PubSubConnection *tmpConnection1, *tmpConnection2;
  266. TAILQ_FOREACH_SAFE(tmpConnection1, &server->pubSubManager.connections, listEntry, tmpConnection2){
  267. UA_Server_removePubSubConnection(server, tmpConnection1->identifier);
  268. }
  269. UA_PublishedDataSet *tmpPDS1, *tmpPDS2;
  270. TAILQ_FOREACH_SAFE(tmpPDS1, &server->pubSubManager.publishedDataSets, listEntry, tmpPDS2){
  271. UA_Server_removePublishedDataSet(server, tmpPDS1->identifier);
  272. }
  273. }
  274. /***********************************/
  275. /* PubSub Jobs abstraction */
  276. /***********************************/
  277. #ifndef UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING
  278. /* If UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_INTERRUPT is enabled, a custom callback
  279. * management must be linked to the application */
  280. UA_StatusCode
  281. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  282. void *data, UA_Double interval_ms, UA_UInt64 *callbackId) {
  283. return UA_Timer_addRepeatedCallback(&server->timer, (UA_ApplicationCallback)callback,
  284. server, data, interval_ms, callbackId);
  285. }
  286. UA_StatusCode
  287. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  288. UA_Double interval_ms) {
  289. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval_ms);
  290. }
  291. void
  292. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  293. UA_Timer_removeCallback(&server->timer, callbackId);
  294. }
  295. #endif /* UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING */
  296. #endif /* UA_ENABLE_PUBSUB */