ua_pubsub_manager.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_realloc(server->pubSubManager.connections,
  47. sizeof(UA_PubSubConnection) * (server->pubSubManager.connectionsSize + 1));
  48. if(!newConnectionsField) {
  49. UA_PubSubConnectionConfig_clear(tmpConnectionConfig);
  50. UA_free(tmpConnectionConfig);
  51. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  52. "PubSub Connection creation failed. Out of Memory.");
  53. return UA_STATUSCODE_BADOUTOFMEMORY;
  54. }
  55. server->pubSubManager.connections = newConnectionsField;
  56. server->pubSubManager.connectionsSize++;
  57. UA_PubSubConnection *newConnection =
  58. &server->pubSubManager.connections[server->pubSubManager.connectionsSize-1];
  59. /* Initialize the new connection */
  60. memset(newConnection, 0, sizeof(UA_PubSubConnection));
  61. LIST_INIT(&newConnection->writerGroups);
  62. //workaround - fixing issue with queue.h and realloc.
  63. for(size_t n = 0; n < server->pubSubManager.connectionsSize; n++){
  64. if(server->pubSubManager.connections[n].writerGroups.lh_first){
  65. server->pubSubManager.connections[n].writerGroups.lh_first->listEntry.le_prev = &server->pubSubManager.connections[n].writerGroups.lh_first;
  66. }
  67. }
  68. newConnection->config = tmpConnectionConfig;
  69. /* Open the channel */
  70. newConnection->channel = tl->createPubSubChannel(newConnection->config);
  71. if(!newConnection->channel) {
  72. UA_PubSubConnection_clear(server, newConnection);
  73. server->pubSubManager.connectionsSize--;
  74. /* Keep the realloced (longer) array if entries remain */
  75. if(server->pubSubManager.connectionsSize == 0) {
  76. UA_free(server->pubSubManager.connections);
  77. server->pubSubManager.connections = NULL;
  78. }
  79. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  80. "PubSub Connection creation failed. Transport layer creation problem.");
  81. return UA_STATUSCODE_BADINTERNALERROR;
  82. }
  83. UA_PubSubManager_generateUniqueNodeId(server, &newConnection->identifier);
  84. if(connectionIdentifier)
  85. UA_NodeId_copy(&newConnection->identifier, connectionIdentifier);
  86. /* update all writerGroups and set new PTR */
  87. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  88. UA_WriterGroup *wg;
  89. LIST_FOREACH(wg, &server->pubSubManager.connections[i].writerGroups, listEntry){
  90. wg->linkedConnectionPtr = UA_PubSubConnection_findConnectionbyId(server, wg->linkedConnection);
  91. }
  92. }
  93. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  94. addPubSubConnectionRepresentation(server, newConnection);
  95. #endif
  96. return UA_STATUSCODE_GOOD;
  97. }
  98. UA_StatusCode
  99. UA_Server_removePubSubConnection(UA_Server *server, const UA_NodeId connection) {
  100. //search the identified Connection and store the Connection index
  101. size_t connectionIndex;
  102. UA_PubSubConnection *currentConnection = NULL;
  103. for(connectionIndex = 0; connectionIndex < server->pubSubManager.connectionsSize; connectionIndex++){
  104. if(UA_NodeId_equal(&connection, &server->pubSubManager.connections[connectionIndex].identifier)){
  105. currentConnection = &server->pubSubManager.connections[connectionIndex];
  106. break;
  107. }
  108. }
  109. if(!currentConnection)
  110. return UA_STATUSCODE_BADNOTFOUND;
  111. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  112. removePubSubConnectionRepresentation(server, currentConnection);
  113. #endif
  114. UA_PubSubConnection_clear(server, currentConnection);
  115. server->pubSubManager.connectionsSize--;
  116. //remove the connection from the pubSubManager, move the last connection
  117. //into the allocated memory of the deleted connection
  118. if(server->pubSubManager.connectionsSize != connectionIndex){
  119. memcpy(&server->pubSubManager.connections[connectionIndex],
  120. &server->pubSubManager.connections[server->pubSubManager.connectionsSize],
  121. sizeof(UA_PubSubConnection));
  122. }
  123. if(server->pubSubManager.connectionsSize <= 0){
  124. UA_free(server->pubSubManager.connections);
  125. server->pubSubManager.connections = NULL;
  126. } else {
  127. server->pubSubManager.connections = (UA_PubSubConnection *)
  128. UA_realloc(server->pubSubManager.connections, sizeof(UA_PubSubConnection) * server->pubSubManager.connectionsSize);
  129. if(!server->pubSubManager.connections){
  130. return UA_STATUSCODE_BADINTERNALERROR;
  131. }
  132. //workaround - fixing issue with queue.h and realloc.
  133. for(size_t n = 0; n < server->pubSubManager.connectionsSize; n++){
  134. if(server->pubSubManager.connections[n].writerGroups.lh_first){
  135. server->pubSubManager.connections[n].writerGroups.lh_first->listEntry.le_prev = &server->pubSubManager.connections[n].writerGroups.lh_first;
  136. }
  137. }
  138. /* update all writerGroups and set new PTR */
  139. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  140. UA_WriterGroup *wg;
  141. LIST_FOREACH(wg, &server->pubSubManager.connections[i].writerGroups, listEntry){
  142. wg->linkedConnectionPtr = UA_PubSubConnection_findConnectionbyId(server, wg->linkedConnection);
  143. }
  144. }
  145. }
  146. return UA_STATUSCODE_GOOD;
  147. }
  148. UA_AddPublishedDataSetResult
  149. UA_Server_addPublishedDataSet(UA_Server *server, const UA_PublishedDataSetConfig *publishedDataSetConfig,
  150. UA_NodeId *pdsIdentifier) {
  151. UA_AddPublishedDataSetResult result = {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  152. if(!publishedDataSetConfig){
  153. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  154. "PublishedDataSet creation failed. No config passed in.");
  155. return result;
  156. }
  157. if(publishedDataSetConfig->publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS){
  158. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  159. "PublishedDataSet creation failed. Unsupported PublishedDataSet type.");
  160. return result;
  161. }
  162. //deep copy the given connection config
  163. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  164. memset(&tmpPublishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  165. if(UA_PublishedDataSetConfig_copy(publishedDataSetConfig, &tmpPublishedDataSetConfig) != UA_STATUSCODE_GOOD){
  166. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  167. "PublishedDataSet creation failed. Configuration copy failed.");
  168. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  169. return result;
  170. }
  171. //create new PDS and add to UA_PubSubManager
  172. UA_PublishedDataSet *newPubSubDataSetField = (UA_PublishedDataSet *)
  173. UA_realloc(server->pubSubManager.publishedDataSets,
  174. sizeof(UA_PublishedDataSet) * (server->pubSubManager.publishedDataSetsSize + 1));
  175. if(!newPubSubDataSetField) {
  176. UA_PublishedDataSetConfig_clear(&tmpPublishedDataSetConfig);
  177. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  178. "PublishedDataSet creation failed. Out of Memory.");
  179. result.addResult = UA_STATUSCODE_BADOUTOFMEMORY;
  180. return result;
  181. }
  182. server->pubSubManager.publishedDataSets = newPubSubDataSetField;
  183. UA_PublishedDataSet *newPubSubDataSet = &server->pubSubManager.publishedDataSets[(server->pubSubManager.publishedDataSetsSize)];
  184. memset(newPubSubDataSet, 0, sizeof(UA_PublishedDataSet));
  185. TAILQ_INIT(&newPubSubDataSet->fields);
  186. //workaround - fixing issue with queue.h and realloc.
  187. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  188. if(server->pubSubManager.publishedDataSets[n].fields.tqh_first){
  189. server->pubSubManager.publishedDataSets[n].fields.tqh_first->listEntry.tqe_prev = &server->pubSubManager.publishedDataSets[n].fields.tqh_first;
  190. }
  191. }
  192. newPubSubDataSet->config = tmpPublishedDataSetConfig;
  193. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  194. //parse template config and add fields (later PubSub batch)
  195. }
  196. //generate unique nodeId
  197. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSet->identifier);
  198. if(pdsIdentifier != NULL){
  199. UA_NodeId_copy(&newPubSubDataSet->identifier, pdsIdentifier);
  200. }
  201. result.addResult = UA_STATUSCODE_GOOD;
  202. result.fieldAddResults = NULL;
  203. result.fieldAddResultsSize = 0;
  204. //fill the DataSetMetaData
  205. switch(tmpPublishedDataSetConfig.publishedDataSetType){
  206. case UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE:
  207. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.itemsTemplate.metaData,
  208. &newPubSubDataSet->dataSetMetaData) != UA_STATUSCODE_GOOD){
  209. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  210. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  211. }
  212. break;
  213. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS_TEMPLATE:
  214. if(UA_DataSetMetaDataType_copy(&tmpPublishedDataSetConfig.config.eventTemplate.metaData,
  215. &newPubSubDataSet->dataSetMetaData) != UA_STATUSCODE_GOOD){
  216. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  217. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  218. }
  219. break;
  220. case UA_PUBSUB_DATASET_PUBLISHEDEVENTS:
  221. newPubSubDataSet->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  222. newPubSubDataSet->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  223. newPubSubDataSet->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  224. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSet->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  225. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  226. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  227. }
  228. newPubSubDataSet->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  229. break;
  230. case UA_PUBSUB_DATASET_PUBLISHEDITEMS:
  231. newPubSubDataSet->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  232. newPubSubDataSet->dataSetMetaData.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  233. if(UA_String_copy(&tmpPublishedDataSetConfig.name, &newPubSubDataSet->dataSetMetaData.name) != UA_STATUSCODE_GOOD){
  234. UA_Server_removeDataSetField(server, newPubSubDataSet->identifier);
  235. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  236. }
  237. newPubSubDataSet->dataSetMetaData.description = UA_LOCALIZEDTEXT_ALLOC("", "");
  238. newPubSubDataSet->dataSetMetaData.dataSetClassId = UA_GUID_NULL;
  239. break;
  240. }
  241. server->pubSubManager.publishedDataSetsSize++;
  242. result.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  243. result.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  244. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  245. addPublishedDataItemsRepresentation(server, newPubSubDataSet);
  246. #endif
  247. return result;
  248. }
  249. UA_StatusCode
  250. UA_Server_removePublishedDataSet(UA_Server *server, const UA_NodeId pds) {
  251. //search the identified PublishedDataSet and store the PDS index
  252. UA_PublishedDataSet *publishedDataSet = NULL;
  253. size_t publishedDataSetIndex;
  254. for(publishedDataSetIndex = 0; publishedDataSetIndex < server->pubSubManager.publishedDataSetsSize; publishedDataSetIndex++){
  255. if(UA_NodeId_equal(&server->pubSubManager.publishedDataSets[publishedDataSetIndex].identifier, &pds)){
  256. publishedDataSet = &server->pubSubManager.publishedDataSets[publishedDataSetIndex];
  257. break;
  258. }
  259. }
  260. if(!publishedDataSet){
  261. return UA_STATUSCODE_BADNOTFOUND;
  262. }
  263. if(publishedDataSet->config.configurationFrozen){
  264. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  265. "Remove PublishedDataSet failed. PublishedDataSet is frozen.");
  266. return UA_STATUSCODE_BADCONFIGURATIONERROR;
  267. }
  268. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  269. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  270. UA_WriterGroup *writerGroup;
  271. LIST_FOREACH(writerGroup, &server->pubSubManager.connections[i].writerGroups, listEntry){
  272. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  273. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  274. if(UA_NodeId_equal(&currentWriter->connectedDataSet, &publishedDataSet->identifier)){
  275. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  276. }
  277. }
  278. }
  279. }
  280. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  281. removePublishedDataSetRepresentation(server, publishedDataSet);
  282. #endif
  283. UA_PublishedDataSet_clear(server, publishedDataSet);
  284. server->pubSubManager.publishedDataSetsSize--;
  285. //copy the last PDS to the removed PDS inside the allocated memory block
  286. if(server->pubSubManager.publishedDataSetsSize != publishedDataSetIndex){
  287. memcpy(&server->pubSubManager.publishedDataSets[publishedDataSetIndex],
  288. &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize],
  289. sizeof(UA_PublishedDataSet));
  290. }
  291. if(server->pubSubManager.publishedDataSetsSize <= 0){
  292. UA_free(server->pubSubManager.publishedDataSets);
  293. server->pubSubManager.publishedDataSets = NULL;
  294. } else {
  295. server->pubSubManager.publishedDataSets = (UA_PublishedDataSet *)
  296. UA_realloc(server->pubSubManager.publishedDataSets, sizeof(UA_PublishedDataSet) * server->pubSubManager.publishedDataSetsSize);
  297. if(!server->pubSubManager.publishedDataSets){
  298. return UA_STATUSCODE_BADINTERNALERROR;
  299. }
  300. //workaround - fixing issue with queue.h and realloc.
  301. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  302. if(server->pubSubManager.publishedDataSets[n].fields.tqh_first){
  303. server->pubSubManager.publishedDataSets[n].fields.tqh_first->listEntry.tqe_prev = &server->pubSubManager.publishedDataSets[n].fields.tqh_first;
  304. }
  305. }
  306. }
  307. return UA_STATUSCODE_GOOD;
  308. }
  309. /* Calculate the time difference between current time and UTC (00:00) on January
  310. * 1, 2000. */
  311. UA_UInt32
  312. UA_PubSubConfigurationVersionTimeDifference() {
  313. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  314. return timeDiffSince2000;
  315. }
  316. /* Generate a new unique NodeId. This NodeId will be used for the information
  317. * model representation of PubSub entities. */
  318. void
  319. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  320. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  321. UA_Node *newNode = UA_NODESTORE_NEW(server, UA_NODECLASS_OBJECT);
  322. UA_NODESTORE_INSERT(server, newNode, &newNodeId);
  323. UA_NodeId_copy(&newNodeId, nodeId);
  324. }
  325. /* Delete the current PubSub configuration including all nested members. This
  326. * action also delete the configured PubSub transport Layers. */
  327. void
  328. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  329. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  330. /* Stop and unfreeze all WriterGroups */
  331. for(size_t i = 0; i < pubSubManager->connectionsSize; i++) {
  332. UA_WriterGroup *writerGroup;
  333. LIST_FOREACH(writerGroup, &pubSubManager->connections[i].writerGroups, listEntry) {
  334. UA_WriterGroup_setPubSubState(server, UA_PUBSUBSTATE_DISABLED, writerGroup);
  335. UA_Server_unfreezeWriterGroupConfiguration(server, writerGroup->identifier);
  336. }
  337. }
  338. //free the currently configured transport layers
  339. UA_free(server->config.pubsubTransportLayers);
  340. server->config.pubsubTransportLayersSize = 0;
  341. //remove Connections and WriterGroups
  342. while(pubSubManager->connectionsSize > 0){
  343. UA_Server_removePubSubConnection(server, pubSubManager->connections[pubSubManager->connectionsSize-1].identifier);
  344. }
  345. while(pubSubManager->publishedDataSetsSize > 0){
  346. UA_Server_removePublishedDataSet(server, pubSubManager->publishedDataSets[pubSubManager->publishedDataSetsSize-1].identifier);
  347. }
  348. }
  349. /***********************************/
  350. /* PubSub Jobs abstraction */
  351. /***********************************/
  352. #ifndef UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING
  353. /* If UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_INTERRUPT is enabled, a custom callback
  354. * management must be linked to the application */
  355. UA_StatusCode
  356. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  357. void *data, UA_Double interval_ms, UA_UInt64 *callbackId) {
  358. return UA_Timer_addRepeatedCallback(&server->timer, (UA_ApplicationCallback)callback,
  359. server, data, interval_ms, callbackId);
  360. }
  361. UA_StatusCode
  362. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  363. UA_Double interval_ms) {
  364. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval_ms);
  365. }
  366. void
  367. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  368. UA_Timer_removeCallback(&server->timer, callbackId);
  369. }
  370. #endif /* UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING */
  371. #endif /* UA_ENABLE_PUBSUB */