ua_pubsub_manager.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_realloc(server->pubSubManager.publishedDataSets,
  123. sizeof(UA_PublishedDataSet) * (server->pubSubManager.publishedDataSetsSize + 1));
  124. if(!newPubSubDataSetField) {
  125. UA_PublishedDataSetConfig_clear(&tmpPublishedDataSetConfig);
  126. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  127. "PublishedDataSet creation failed. Out of Memory.");
  128. result.addResult = UA_STATUSCODE_BADOUTOFMEMORY;
  129. return result;
  130. }
  131. server->pubSubManager.publishedDataSets = newPubSubDataSetField;
  132. UA_PublishedDataSet *newPubSubDataSet = &server->pubSubManager.publishedDataSets[(server->pubSubManager.publishedDataSetsSize)];
  133. memset(newPubSubDataSet, 0, sizeof(UA_PublishedDataSet));
  134. TAILQ_INIT(&newPubSubDataSet->fields);
  135. //workaround - fixing issue with queue.h and realloc.
  136. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  137. if(server->pubSubManager.publishedDataSets[n].fields.tqh_first){
  138. server->pubSubManager.publishedDataSets[n].fields.tqh_first->listEntry.tqe_prev = &server->pubSubManager.publishedDataSets[n].fields.tqh_first;
  139. }
  140. }
  141. newPubSubDataSet->config = tmpPublishedDataSetConfig;
  142. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  143. //parse template config and add fields (later PubSub batch)
  144. }
  145. //generate unique nodeId
  146. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSet->identifier);
  147. if(pdsIdentifier != NULL){
  148. UA_NodeId_copy(&newPubSubDataSet->identifier, pdsIdentifier);
  149. }
  150. result.addResult = UA_STATUSCODE_GOOD;
  151. result.fieldAddResults = NULL;
  152. result.fieldAddResultsSize = 0;
  153. //fill the DataSetMetaData
  154. switch(tmpPublishedDataSetConfig.publishedDataSetType){
  155. case UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE:
  156. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.itemsTemplate.metaData,
  157. &newPubSubDataSet->dataSetMetaData) != UA_STATUSCODE_GOOD){
  158. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  159. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  160. }
  161. break;
  162. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS_TEMPLATE:
  163. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.eventTemplate.metaData,
  164. &newPubSubDataSet->dataSetMetaData) != UA_STATUSCODE_GOOD){
  165. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  166. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  167. }
  168. break;
  169. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS:
  170. newPubSubDataSet->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  171. newPubSubDataSet->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  172. newPubSubDataSet->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  173. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSet->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  174. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  175. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  176. }
  177. newPubSubDataSet->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  178. break;
  179. case UA_PUBSUB_DATASET_PUBLISHEDITEMS:
  180. newPubSubDataSet->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  181. newPubSubDataSet->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  182. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSet->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  183. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  184. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  185. }
  186. newPubSubDataSet->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  187. newPubSubDataSet->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  188. break;
  189. }
  190. server->pubSubManager.publishedDataSetsSize++;
  191. result.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  192. result.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  193. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  194. addPublishedDataItemsRepresentation(server, newPubSubDataSet);
  195. #endif
  196. return result;
  197. }
  198. UA_StatusCode
  199. UA_Server_removePublishedDataSet(UA_Server *server, const UA_NodeId pds) {
  200. //search the identified PublishedDataSet and store the PDS index
  201. UA_PublishedDataSet *publishedDataSet = NULL;
  202. size_t publishedDataSetIndex;
  203. for(publishedDataSetIndex = 0; publishedDataSetIndex < server->pubSubManager.publishedDataSetsSize; publishedDataSetIndex++){
  204. if(UA_NodeId_equal(&server->pubSubManager.publishedDataSets[publishedDataSetIndex].identifier, &pds)){
  205. publishedDataSet = &server->pubSubManager.publishedDataSets[publishedDataSetIndex];
  206. break;
  207. }
  208. }
  209. if(!publishedDataSet){
  210. return UA_STATUSCODE_BADNOTFOUND;
  211. }
  212. if(publishedDataSet->config.configurationFrozen){
  213. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  214. "Remove PublishedDataSet failed. PublishedDataSet is frozen.");
  215. return UA_STATUSCODE_BADCONFIGURATIONERROR;
  216. }
  217. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  218. UA_PubSubConnection *tmpConnectoin;
  219. TAILQ_FOREACH(tmpConnectoin, &server->pubSubManager.connections, listEntry){
  220. UA_WriterGroup *writerGroup;
  221. LIST_FOREACH(writerGroup, &tmpConnectoin->writerGroups, listEntry){
  222. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  223. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  224. if(UA_NodeId_equal(&currentWriter->connectedDataSet, &publishedDataSet->identifier)){
  225. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  226. }
  227. }
  228. }
  229. }
  230. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  231. removePublishedDataSetRepresentation(server, publishedDataSet);
  232. #endif
  233. UA_PublishedDataSet_clear(server, publishedDataSet);
  234. server->pubSubManager.publishedDataSetsSize--;
  235. //copy the last PDS to the removed PDS inside the allocated memory block
  236. if(server->pubSubManager.publishedDataSetsSize != publishedDataSetIndex){
  237. memcpy(&server->pubSubManager.publishedDataSets[publishedDataSetIndex],
  238. &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize],
  239. sizeof(UA_PublishedDataSet));
  240. }
  241. if(server->pubSubManager.publishedDataSetsSize <= 0){
  242. UA_free(server->pubSubManager.publishedDataSets);
  243. server->pubSubManager.publishedDataSets = NULL;
  244. } else {
  245. server->pubSubManager.publishedDataSets = (UA_PublishedDataSet *)
  246. UA_realloc(server->pubSubManager.publishedDataSets, sizeof(UA_PublishedDataSet) * server->pubSubManager.publishedDataSetsSize);
  247. if(!server->pubSubManager.publishedDataSets){
  248. return UA_STATUSCODE_BADINTERNALERROR;
  249. }
  250. //workaround - fixing issue with queue.h and realloc.
  251. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  252. if(server->pubSubManager.publishedDataSets[n].fields.tqh_first){
  253. server->pubSubManager.publishedDataSets[n].fields.tqh_first->listEntry.tqe_prev = &server->pubSubManager.publishedDataSets[n].fields.tqh_first;
  254. }
  255. }
  256. }
  257. return UA_STATUSCODE_GOOD;
  258. }
  259. /* Calculate the time difference between current time and UTC (00:00) on January
  260. * 1, 2000. */
  261. UA_UInt32
  262. UA_PubSubConfigurationVersionTimeDifference() {
  263. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  264. return timeDiffSince2000;
  265. }
  266. /* Generate a new unique NodeId. This NodeId will be used for the information
  267. * model representation of PubSub entities. */
  268. void
  269. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  270. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  271. UA_Node *newNode = UA_NODESTORE_NEW(server, UA_NODECLASS_OBJECT);
  272. UA_NODESTORE_INSERT(server, newNode, &newNodeId);
  273. UA_NodeId_copy(&newNodeId, nodeId);
  274. }
  275. /* Delete the current PubSub configuration including all nested members. This
  276. * action also delete the configured PubSub transport Layers. */
  277. void
  278. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  279. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  280. /* Stop and unfreeze all WriterGroups */
  281. UA_PubSubConnection *tmpConnection;
  282. TAILQ_FOREACH(tmpConnection, &server->pubSubManager.connections, listEntry){
  283. for(size_t i = 0; i < pubSubManager->connectionsSize; i++) {
  284. UA_WriterGroup *writerGroup;
  285. LIST_FOREACH(writerGroup, &tmpConnection->writerGroups, listEntry) {
  286. UA_WriterGroup_setPubSubState(server, UA_PUBSUBSTATE_DISABLED, writerGroup);
  287. UA_Server_unfreezeWriterGroupConfiguration(server, writerGroup->identifier);
  288. }
  289. }
  290. }
  291. //free the currently configured transport layers
  292. UA_free(server->config.pubsubTransportLayers);
  293. server->config.pubsubTransportLayersSize = 0;
  294. //remove Connections and WriterGroups
  295. UA_PubSubConnection *tmpConnection1, *tmpConnection2;
  296. TAILQ_FOREACH_SAFE(tmpConnection1, &server->pubSubManager.connections, listEntry, tmpConnection2){
  297. UA_Server_removePubSubConnection(server, tmpConnection1->identifier);
  298. }
  299. while(pubSubManager->publishedDataSetsSize > 0){
  300. UA_Server_removePublishedDataSet(server, pubSubManager->publishedDataSets[pubSubManager->publishedDataSetsSize-1].identifier);
  301. }
  302. }
  303. /***********************************/
  304. /* PubSub Jobs abstraction */
  305. /***********************************/
  306. #ifndef UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING
  307. /* If UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_INTERRUPT is enabled, a custom callback
  308. * management must be linked to the application */
  309. UA_StatusCode
  310. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  311. void *data, UA_Double interval_ms, UA_UInt64 *callbackId) {
  312. return UA_Timer_addRepeatedCallback(&server->timer, (UA_ApplicationCallback)callback,
  313. server, data, interval_ms, callbackId);
  314. }
  315. UA_StatusCode
  316. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  317. UA_Double interval_ms) {
  318. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval_ms);
  319. }
  320. void
  321. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  322. UA_Timer_removeCallback(&server->timer, callbackId);
  323. }
  324. #endif /* UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING */
  325. #endif /* UA_ENABLE_PUBSUB */