ua_pubsub_manager.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. */
  7. #include "server/ua_server_internal.h"
  8. #include "ua_pubsub_ns0.h"
  9. #ifdef UA_ENABLE_PUBSUB /* conditional compilation */
  10. #define UA_DATETIMESTAMP_2000 125911584000000000
  11. UA_StatusCode
  12. UA_Server_addPubSubConnection(UA_Server *server, const UA_PubSubConnectionConfig *connectionConfig,
  13. UA_NodeId *connectionIdentifier) {
  14. //iterate over the available UA_PubSubTransportLayers
  15. for(size_t i = 0; i < server->config.pubsubTransportLayersSize; i++) {
  16. if(connectionConfig && UA_String_equal(&server->config.pubsubTransportLayers[i].transportProfileUri,
  17. &connectionConfig->transportProfileUri)){
  18. //create new connection config
  19. UA_PubSubConnectionConfig *tmpConnectionConfig = (UA_PubSubConnectionConfig *)
  20. UA_calloc(1, sizeof(UA_PubSubConnectionConfig));
  21. if(!tmpConnectionConfig){
  22. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  23. "PubSub Connection creation failed. Out of Memory.");
  24. return UA_STATUSCODE_BADOUTOFMEMORY;
  25. }
  26. //deep copy the given connection config
  27. if(UA_PubSubConnectionConfig_copy(connectionConfig, tmpConnectionConfig) != UA_STATUSCODE_GOOD){
  28. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  29. "PubSub Connection creation failed. Config copy problem.");
  30. return UA_STATUSCODE_BADOUTOFMEMORY;
  31. }
  32. //create new connection and add to UA_PubSubManager
  33. UA_PubSubConnection *newConnectionsField = (UA_PubSubConnection *)
  34. UA_realloc(server->pubSubManager.connections,
  35. sizeof(UA_PubSubConnection) * (server->pubSubManager.connectionsSize + 1));
  36. if(!newConnectionsField) {
  37. UA_PubSubConnectionConfig_deleteMembers(tmpConnectionConfig);
  38. UA_free(tmpConnectionConfig);
  39. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  40. "PubSub Connection creation failed. Out of Memory.");
  41. return UA_STATUSCODE_BADOUTOFMEMORY;
  42. }
  43. server->pubSubManager.connections = newConnectionsField;
  44. UA_PubSubConnection *newConnection = &server->pubSubManager.connections[server->pubSubManager.connectionsSize];
  45. memset(newConnection, 0, sizeof(UA_PubSubConnection));
  46. LIST_INIT(&newConnection->writerGroups);
  47. //workaround - fixing issue with queue.h and realloc.
  48. for(size_t n = 0; n < server->pubSubManager.connectionsSize; n++){
  49. if(server->pubSubManager.connections[n].writerGroups.lh_first){
  50. server->pubSubManager.connections[n].writerGroups.lh_first->listEntry.le_prev = &server->pubSubManager.connections[n].writerGroups.lh_first;
  51. }
  52. }
  53. newConnection->config = tmpConnectionConfig;
  54. newConnection->channel = server->config.pubsubTransportLayers[i].createPubSubChannel(newConnection->config);
  55. if(!newConnection->channel){
  56. UA_PubSubConnection_deleteMembers(server, newConnection);
  57. if(server->pubSubManager.connectionsSize > 0){
  58. newConnectionsField = (UA_PubSubConnection *)
  59. UA_realloc(server->pubSubManager.connections,
  60. sizeof(UA_PubSubConnection) * (server->pubSubManager.connectionsSize));
  61. if(!newConnectionsField) {
  62. return UA_STATUSCODE_BADINTERNALERROR;
  63. }
  64. server->pubSubManager.connections = newConnectionsField;
  65. } else {
  66. UA_free(newConnectionsField);
  67. server->pubSubManager.connections = NULL;
  68. }
  69. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  70. "PubSub Connection creation failed. Transport layer creation problem.");
  71. return UA_STATUSCODE_BADINTERNALERROR;
  72. }
  73. UA_PubSubManager_generateUniqueNodeId(server, &newConnection->identifier);
  74. if(connectionIdentifier != NULL){
  75. UA_NodeId_copy(&newConnection->identifier, connectionIdentifier);
  76. }
  77. server->pubSubManager.connectionsSize++;
  78. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  79. addPubSubConnectionRepresentation(server, newConnection);
  80. #endif
  81. return UA_STATUSCODE_GOOD;
  82. }
  83. }
  84. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  85. "PubSub Connection creation failed. Requested transport layer not found.");
  86. return UA_STATUSCODE_BADNOTFOUND;
  87. }
  88. UA_StatusCode
  89. UA_Server_removePubSubConnection(UA_Server *server, const UA_NodeId connection) {
  90. //search the identified Connection and store the Connection index
  91. size_t connectionIndex;
  92. UA_PubSubConnection *currentConnection = NULL;
  93. for(connectionIndex = 0; connectionIndex < server->pubSubManager.connectionsSize; connectionIndex++){
  94. if(UA_NodeId_equal(&connection, &server->pubSubManager.connections[connectionIndex].identifier)){
  95. currentConnection = &server->pubSubManager.connections[connectionIndex];
  96. break;
  97. }
  98. }
  99. if(!currentConnection)
  100. return UA_STATUSCODE_BADNOTFOUND;
  101. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  102. removePubSubConnectionRepresentation(server, currentConnection);
  103. #endif
  104. UA_PubSubConnection_deleteMembers(server, currentConnection);
  105. server->pubSubManager.connectionsSize--;
  106. //remove the connection from the pubSubManager, move the last connection
  107. //into the allocated memory of the deleted connection
  108. if(server->pubSubManager.connectionsSize != connectionIndex){
  109. memcpy(&server->pubSubManager.connections[connectionIndex],
  110. &server->pubSubManager.connections[server->pubSubManager.connectionsSize],
  111. sizeof(UA_PubSubConnection));
  112. }
  113. if(server->pubSubManager.connectionsSize <= 0){
  114. UA_free(server->pubSubManager.connections);
  115. server->pubSubManager.connections = NULL;
  116. } else {
  117. server->pubSubManager.connections = (UA_PubSubConnection *)
  118. UA_realloc(server->pubSubManager.connections, sizeof(UA_PubSubConnection) * server->pubSubManager.connectionsSize);
  119. if(!server->pubSubManager.connections){
  120. return UA_STATUSCODE_BADINTERNALERROR;
  121. }
  122. //workaround - fixing issue with queue.h and realloc.
  123. for(size_t n = 0; n < server->pubSubManager.connectionsSize; n++){
  124. if(server->pubSubManager.connections[n].writerGroups.lh_first){
  125. server->pubSubManager.connections[n].writerGroups.lh_first->listEntry.le_prev = &server->pubSubManager.connections[n].writerGroups.lh_first;
  126. }
  127. }
  128. }
  129. return UA_STATUSCODE_GOOD;
  130. }
  131. UA_AddPublishedDataSetResult
  132. UA_Server_addPublishedDataSet(UA_Server *server, const UA_PublishedDataSetConfig *publishedDataSetConfig,
  133. UA_NodeId *pdsIdentifier) {
  134. UA_AddPublishedDataSetResult result = {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  135. if(!publishedDataSetConfig){
  136. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  137. "PublishedDataSet creation failed. No config passed in.");
  138. return result;
  139. }
  140. if(publishedDataSetConfig->publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS){
  141. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  142. "PublishedDataSet creation failed. Unsupported PublishedDataSet type.");
  143. return result;
  144. }
  145. //deep copy the given connection config
  146. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  147. memset(&tmpPublishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  148. if(UA_PublishedDataSetConfig_copy(publishedDataSetConfig, &tmpPublishedDataSetConfig) != UA_STATUSCODE_GOOD){
  149. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  150. "PublishedDataSet creation failed. Configuration copy failed.");
  151. result.addResult = UA_STATUSCODE_BADINTERNALERROR;
  152. return result;
  153. }
  154. //create new PDS and add to UA_PubSubManager
  155. UA_PublishedDataSet *newPubSubDataSetField = (UA_PublishedDataSet *)
  156. UA_realloc(server->pubSubManager.publishedDataSets,
  157. sizeof(UA_PublishedDataSet) * (server->pubSubManager.publishedDataSetsSize + 1));
  158. if(!newPubSubDataSetField) {
  159. UA_PublishedDataSetConfig_deleteMembers(&tmpPublishedDataSetConfig);
  160. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  161. "PublishedDataSet creation failed. Out of Memory.");
  162. result.addResult = UA_STATUSCODE_BADOUTOFMEMORY;
  163. return result;
  164. }
  165. server->pubSubManager.publishedDataSets = newPubSubDataSetField;
  166. UA_PublishedDataSet *newPubSubDataSet = &server->pubSubManager.publishedDataSets[(server->pubSubManager.publishedDataSetsSize)];
  167. memset(newPubSubDataSet, 0, sizeof(UA_PublishedDataSet));
  168. LIST_INIT(&newPubSubDataSet->fields);
  169. //workaround - fixing issue with queue.h and realloc.
  170. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  171. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  172. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  173. }
  174. }
  175. newPubSubDataSet->config = tmpPublishedDataSetConfig;
  176. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  177. //parse template config and add fields (later PubSub batch)
  178. }
  179. //generate unique nodeId
  180. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSet->identifier);
  181. if(pdsIdentifier != NULL){
  182. UA_NodeId_copy(&newPubSubDataSet->identifier, pdsIdentifier);
  183. }
  184. server->pubSubManager.publishedDataSetsSize++;
  185. result.addResult = UA_STATUSCODE_GOOD;
  186. result.fieldAddResults = NULL;
  187. result.fieldAddResultsSize = 0;
  188. result.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  189. result.configurationVersion.minorVersion = UA_PubSubConfigurationVersionTimeDifference();
  190. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  191. addPublishedDataItemsRepresentation(server, newPubSubDataSet);
  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 = NULL;
  199. size_t publishedDataSetIndex;
  200. for(publishedDataSetIndex = 0; publishedDataSetIndex < server->pubSubManager.publishedDataSetsSize; publishedDataSetIndex++){
  201. if(UA_NodeId_equal(&server->pubSubManager.publishedDataSets[publishedDataSetIndex].identifier, &pds)){
  202. publishedDataSet = &server->pubSubManager.publishedDataSets[publishedDataSetIndex];
  203. break;
  204. };
  205. }
  206. if(!publishedDataSet){
  207. return UA_STATUSCODE_BADNOTFOUND;
  208. }
  209. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  210. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  211. UA_WriterGroup *writerGroup;
  212. LIST_FOREACH(writerGroup, &server->pubSubManager.connections[i].writerGroups, listEntry){
  213. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  214. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  215. if(UA_NodeId_equal(&currentWriter->identifier, &publishedDataSet->identifier)){
  216. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  217. }
  218. }
  219. }
  220. }
  221. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  222. removePublishedDataSetRepresentation(server, publishedDataSet);
  223. #endif
  224. UA_PublishedDataSet_deleteMembers(server, publishedDataSet);
  225. server->pubSubManager.publishedDataSetsSize--;
  226. //copy the last PDS to the removed PDS inside the allocated memory block
  227. if(server->pubSubManager.publishedDataSetsSize != publishedDataSetIndex){
  228. memcpy(&server->pubSubManager.publishedDataSets[publishedDataSetIndex],
  229. &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize],
  230. sizeof(UA_PublishedDataSet));
  231. }
  232. if(server->pubSubManager.publishedDataSetsSize <= 0){
  233. UA_free(server->pubSubManager.publishedDataSets);
  234. server->pubSubManager.publishedDataSets = NULL;
  235. } else {
  236. server->pubSubManager.publishedDataSets = (UA_PublishedDataSet *)
  237. UA_realloc(server->pubSubManager.publishedDataSets, sizeof(UA_PublishedDataSet) * server->pubSubManager.publishedDataSetsSize);
  238. if(!server->pubSubManager.publishedDataSets){
  239. return UA_STATUSCODE_BADINTERNALERROR;
  240. }
  241. //workaround - fixing issue with queue.h and realloc.
  242. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  243. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  244. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  245. }
  246. }
  247. }
  248. return UA_STATUSCODE_GOOD;
  249. }
  250. /* Calculate the time difference between current time and UTC (00:00) on January
  251. * 1, 2000. */
  252. UA_UInt32
  253. UA_PubSubConfigurationVersionTimeDifference() {
  254. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  255. return timeDiffSince2000;
  256. }
  257. /* Generate a new unique NodeId. This NodeId will be used for the information
  258. * model representation of PubSub entities. */
  259. void
  260. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  261. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  262. UA_Node *newNode = UA_Nodestore_new(server, UA_NODECLASS_OBJECT);
  263. UA_Nodestore_insert(server, newNode, &newNodeId);
  264. UA_NodeId_copy(&newNodeId, nodeId);
  265. }
  266. /* Delete the current PubSub configuration including all nested members. This
  267. * action also delete the configured PubSub transport Layers. */
  268. void
  269. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  270. UA_LOG_INFO(server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  271. //free the currently configured transport layers
  272. UA_free(server->config.pubsubTransportLayers);
  273. server->config.pubsubTransportLayersSize = 0;
  274. //remove Connections and WriterGroups
  275. while(pubSubManager->connectionsSize > 0){
  276. UA_Server_removePubSubConnection(server, pubSubManager->connections[pubSubManager->connectionsSize-1].identifier);
  277. }
  278. while(pubSubManager->publishedDataSetsSize > 0){
  279. UA_Server_removePublishedDataSet(server, pubSubManager->publishedDataSets[pubSubManager->publishedDataSetsSize-1].identifier);
  280. }
  281. }
  282. /***********************************/
  283. /* PubSub Jobs abstraction */
  284. /***********************************/
  285. UA_StatusCode
  286. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  287. void *data, UA_UInt32 interval, UA_UInt64 *callbackId) {
  288. return UA_Timer_addRepeatedCallback(&server->timer, (UA_ApplicationCallback)callback,
  289. server, data, interval, callbackId);
  290. }
  291. UA_StatusCode
  292. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  293. UA_UInt32 interval) {
  294. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval);
  295. }
  296. UA_StatusCode
  297. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  298. return UA_Timer_removeRepeatedCallback(&server->timer, callbackId);
  299. }
  300. #endif /* UA_ENABLE_PUBSUB */