ua_pubsub_manager.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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-2018 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_deleteMembers(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_deleteMembers(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. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  87. addPubSubConnectionRepresentation(server, newConnection);
  88. #endif
  89. return UA_STATUSCODE_GOOD;
  90. }
  91. UA_StatusCode
  92. UA_Server_removePubSubConnection(UA_Server *server, const UA_NodeId connection) {
  93. //search the identified Connection and store the Connection index
  94. size_t connectionIndex;
  95. UA_PubSubConnection *currentConnection = NULL;
  96. for(connectionIndex = 0; connectionIndex < server->pubSubManager.connectionsSize; connectionIndex++){
  97. if(UA_NodeId_equal(&connection, &server->pubSubManager.connections[connectionIndex].identifier)){
  98. currentConnection = &server->pubSubManager.connections[connectionIndex];
  99. break;
  100. }
  101. }
  102. if(!currentConnection)
  103. return UA_STATUSCODE_BADNOTFOUND;
  104. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  105. removePubSubConnectionRepresentation(server, currentConnection);
  106. #endif
  107. UA_PubSubConnection_deleteMembers(server, currentConnection);
  108. server->pubSubManager.connectionsSize--;
  109. //remove the connection from the pubSubManager, move the last connection
  110. //into the allocated memory of the deleted connection
  111. if(server->pubSubManager.connectionsSize != connectionIndex){
  112. memcpy(&server->pubSubManager.connections[connectionIndex],
  113. &server->pubSubManager.connections[server->pubSubManager.connectionsSize],
  114. sizeof(UA_PubSubConnection));
  115. }
  116. if(server->pubSubManager.connectionsSize <= 0){
  117. UA_free(server->pubSubManager.connections);
  118. server->pubSubManager.connections = NULL;
  119. } else {
  120. server->pubSubManager.connections = (UA_PubSubConnection *)
  121. UA_realloc(server->pubSubManager.connections, sizeof(UA_PubSubConnection) * server->pubSubManager.connectionsSize);
  122. if(!server->pubSubManager.connections){
  123. return UA_STATUSCODE_BADINTERNALERROR;
  124. }
  125. //workaround - fixing issue with queue.h and realloc.
  126. for(size_t n = 0; n < server->pubSubManager.connectionsSize; n++){
  127. if(server->pubSubManager.connections[n].writerGroups.lh_first){
  128. server->pubSubManager.connections[n].writerGroups.lh_first->listEntry.le_prev = &server->pubSubManager.connections[n].writerGroups.lh_first;
  129. }
  130. }
  131. }
  132. return UA_STATUSCODE_GOOD;
  133. }
  134. UA_AddPublishedDataSetResult
  135. UA_Server_addPublishedDataSet(UA_Server *server, const UA_PublishedDataSetConfig *publishedDataSetConfig,
  136. UA_NodeId *pdsIdentifier) {
  137. UA_AddPublishedDataSetResult result = {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  138. if(!publishedDataSetConfig){
  139. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  140. "PublishedDataSet creation failed. No config passed in.");
  141. return result;
  142. }
  143. if(publishedDataSetConfig->publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS){
  144. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  145. "PublishedDataSet creation failed. Unsupported PublishedDataSet type.");
  146. return result;
  147. }
  148. //deep copy the given connection config
  149. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  150. memset(&tmpPublishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  151. if(UA_PublishedDataSetConfig_copy(publishedDataSetConfig, &tmpPublishedDataSetConfig) != UA_STATUSCODE_GOOD){
  152. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  153. "PublishedDataSet creation failed. Configuration copy failed.");
  154. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  155. return result;
  156. }
  157. //create new PDS and add to UA_PubSubManager
  158. UA_PublishedDataSet *newPubSubDataSetField = (UA_PublishedDataSet *)
  159. UA_realloc(server->pubSubManager.publishedDataSets,
  160. sizeof(UA_PublishedDataSet) * (server->pubSubManager.publishedDataSetsSize + 1));
  161. if(!newPubSubDataSetField) {
  162. UA_PublishedDataSetConfig_deleteMembers(&tmpPublishedDataSetConfig);
  163. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  164. "PublishedDataSet creation failed. Out of Memory.");
  165. result.addResult = UA_STATUSCODE_BADOUTOFMEMORY;
  166. return result;
  167. }
  168. server->pubSubManager.publishedDataSets = newPubSubDataSetField;
  169. UA_PublishedDataSet *newPubSubDataSet = &server->pubSubManager.publishedDataSets[(server->pubSubManager.publishedDataSetsSize)];
  170. memset(newPubSubDataSet, 0, sizeof(UA_PublishedDataSet));
  171. LIST_INIT(&newPubSubDataSet->fields);
  172. //workaround - fixing issue with queue.h and realloc.
  173. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  174. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  175. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  176. }
  177. }
  178. newPubSubDataSet->config = tmpPublishedDataSetConfig;
  179. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  180. //parse template config and add fields (later PubSub batch)
  181. }
  182. //generate unique nodeId
  183. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSet->identifier);
  184. if(pdsIdentifier != NULL){
  185. UA_NodeId_copy(&newPubSubDataSet->identifier, pdsIdentifier);
  186. }
  187. server->pubSubManager.publishedDataSetsSize++;
  188. result.addResult = UA_STATUSCODE_GOOD;
  189. result.fieldAddResults = NULL;
  190. result.fieldAddResultsSize = 0;
  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. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  213. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  214. UA_WriterGroup *writerGroup;
  215. LIST_FOREACH(writerGroup, &server->pubSubManager.connections[i].writerGroups, listEntry){
  216. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  217. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  218. if(UA_NodeId_equal(&currentWriter->connectedDataSet, &publishedDataSet->identifier)){
  219. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  220. }
  221. }
  222. }
  223. }
  224. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  225. removePublishedDataSetRepresentation(server, publishedDataSet);
  226. #endif
  227. UA_PublishedDataSet_deleteMembers(server, publishedDataSet);
  228. server->pubSubManager.publishedDataSetsSize--;
  229. //copy the last PDS to the removed PDS inside the allocated memory block
  230. if(server->pubSubManager.publishedDataSetsSize != publishedDataSetIndex){
  231. memcpy(&server->pubSubManager.publishedDataSets[publishedDataSetIndex],
  232. &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize],
  233. sizeof(UA_PublishedDataSet));
  234. }
  235. if(server->pubSubManager.publishedDataSetsSize <= 0){
  236. UA_free(server->pubSubManager.publishedDataSets);
  237. server->pubSubManager.publishedDataSets = NULL;
  238. } else {
  239. server->pubSubManager.publishedDataSets = (UA_PublishedDataSet *)
  240. UA_realloc(server->pubSubManager.publishedDataSets, sizeof(UA_PublishedDataSet) * server->pubSubManager.publishedDataSetsSize);
  241. if(!server->pubSubManager.publishedDataSets){
  242. return UA_STATUSCODE_BADINTERNALERROR;
  243. }
  244. //workaround - fixing issue with queue.h and realloc.
  245. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  246. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  247. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  248. }
  249. }
  250. }
  251. return UA_STATUSCODE_GOOD;
  252. }
  253. /* Calculate the time difference between current time and UTC (00:00) on January
  254. * 1, 2000. */
  255. UA_UInt32
  256. UA_PubSubConfigurationVersionTimeDifference() {
  257. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  258. return timeDiffSince2000;
  259. }
  260. /* Generate a new unique NodeId. This NodeId will be used for the information
  261. * model representation of PubSub entities. */
  262. void
  263. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  264. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  265. UA_Node *newNode = UA_Nodestore_newNode(server->nsCtx, UA_NODECLASS_OBJECT);
  266. UA_Nodestore_insertNode(server->nsCtx, newNode, &newNodeId);
  267. UA_NodeId_copy(&newNodeId, nodeId);
  268. }
  269. /* Delete the current PubSub configuration including all nested members. This
  270. * action also delete the configured PubSub transport Layers. */
  271. void
  272. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  273. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  274. //free the currently configured transport layers
  275. UA_free(server->config.pubsubTransportLayers);
  276. server->config.pubsubTransportLayersSize = 0;
  277. //remove Connections and WriterGroups
  278. while(pubSubManager->connectionsSize > 0){
  279. UA_Server_removePubSubConnection(server, pubSubManager->connections[pubSubManager->connectionsSize-1].identifier);
  280. }
  281. while(pubSubManager->publishedDataSetsSize > 0){
  282. UA_Server_removePublishedDataSet(server, pubSubManager->publishedDataSets[pubSubManager->publishedDataSetsSize-1].identifier);
  283. }
  284. }
  285. /***********************************/
  286. /* PubSub Jobs abstraction */
  287. /***********************************/
  288. #ifndef UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING
  289. /* If UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_INTERRUPT is enabled, a custom callback
  290. * management must be linked to the application */
  291. UA_StatusCode
  292. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  293. void *data, UA_Double interval_ms, UA_UInt64 *callbackId) {
  294. return UA_Timer_addRepeatedCallback(&server->timer, (UA_ApplicationCallback)callback,
  295. server, data, interval_ms, callbackId);
  296. }
  297. UA_StatusCode
  298. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  299. UA_Double interval_ms) {
  300. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval_ms);
  301. }
  302. void
  303. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  304. UA_Timer_removeCallback(&server->timer, callbackId);
  305. }
  306. #endif /* UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING */
  307. #endif /* UA_ENABLE_PUBSUB */