ua_pubsub_manager.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if(!publishedDataSetConfig){
  135. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  136. "PublishedDataSet creation failed. No config passed in.");
  137. return (UA_AddPublishedDataSetResult) {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  138. }
  139. if(publishedDataSetConfig->publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS){
  140. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  141. "PublishedDataSet creation failed. Unsupported PublishedDataSet type.");
  142. return (UA_AddPublishedDataSetResult) {UA_STATUSCODE_BADINVALIDARGUMENT, 0, NULL, {0, 0}};
  143. }
  144. //deep copy the given connection config
  145. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  146. memset(&tmpPublishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  147. if(UA_PublishedDataSetConfig_copy(publishedDataSetConfig, &tmpPublishedDataSetConfig) != UA_STATUSCODE_GOOD){
  148. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  149. "PublishedDataSet creation failed. Configuration copy failed.");
  150. return (UA_AddPublishedDataSetResult) {UA_STATUSCODE_BADINTERNALERROR, 0, NULL, {0, 0}};
  151. }
  152. //create new PDS and add to UA_PubSubManager
  153. UA_PublishedDataSet *newPubSubDataSetField = (UA_PublishedDataSet *)
  154. UA_realloc(server->pubSubManager.publishedDataSets,
  155. sizeof(UA_PublishedDataSet) * (server->pubSubManager.publishedDataSetsSize + 1));
  156. if(!newPubSubDataSetField) {
  157. UA_PublishedDataSetConfig_deleteMembers(&tmpPublishedDataSetConfig);
  158. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
  159. "PublishedDataSet creation failed. Out of Memory.");
  160. return (UA_AddPublishedDataSetResult) {UA_STATUSCODE_BADOUTOFMEMORY, 0, NULL, {0, 0}};
  161. }
  162. server->pubSubManager.publishedDataSets = newPubSubDataSetField;
  163. UA_PublishedDataSet *newPubSubDataSet = &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize];
  164. memset(newPubSubDataSet, 0, sizeof(UA_PublishedDataSet));
  165. LIST_INIT(&newPubSubDataSet->fields);
  166. //workaround - fixing issue with queue.h and realloc.
  167. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  168. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  169. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  170. }
  171. }
  172. newPubSubDataSet->config = tmpPublishedDataSetConfig;
  173. if(tmpPublishedDataSetConfig.publishedDataSetType == UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE){
  174. //parse template config and add fields (later PubSub batch)
  175. }
  176. //generate unique nodeId
  177. UA_PubSubManager_generateUniqueNodeId(server, &newPubSubDataSet->identifier);
  178. if(pdsIdentifier != NULL){
  179. UA_NodeId_copy(&newPubSubDataSet->identifier, pdsIdentifier);
  180. }
  181. server->pubSubManager.publishedDataSetsSize++;
  182. UA_AddPublishedDataSetResult result = {UA_STATUSCODE_GOOD, 0, NULL,
  183. {UA_PubSubConfigurationVersionTimeDifference(),
  184. UA_PubSubConfigurationVersionTimeDifference()}};
  185. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  186. addPublishedDataItemsRepresentation(server, newPubSubDataSet);
  187. #endif
  188. return result;
  189. }
  190. UA_StatusCode
  191. UA_Server_removePublishedDataSet(UA_Server *server, UA_NodeId pds) {
  192. //search the identified PublishedDataSet and store the PDS index
  193. UA_PublishedDataSet *publishedDataSet = NULL;
  194. size_t publishedDataSetIndex;
  195. for(publishedDataSetIndex = 0; publishedDataSetIndex < server->pubSubManager.publishedDataSetsSize; publishedDataSetIndex++){
  196. if(UA_NodeId_equal(&server->pubSubManager.publishedDataSets[publishedDataSetIndex].identifier, &pds)){
  197. publishedDataSet = &server->pubSubManager.publishedDataSets[publishedDataSetIndex];
  198. break;
  199. };
  200. }
  201. if(!publishedDataSet){
  202. return UA_STATUSCODE_BADNOTFOUND;
  203. }
  204. //search for referenced writers -> delete this writers. (Standard: writer must be connected with PDS)
  205. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  206. UA_WriterGroup *writerGroup;
  207. LIST_FOREACH(writerGroup, &server->pubSubManager.connections[i].writerGroups, listEntry){
  208. UA_DataSetWriter *currentWriter, *tmpWriterGroup;
  209. LIST_FOREACH_SAFE(currentWriter, &writerGroup->writers, listEntry, tmpWriterGroup){
  210. if(UA_NodeId_equal(&currentWriter->identifier, &publishedDataSet->identifier)){
  211. UA_Server_removeDataSetWriter(server, currentWriter->identifier);
  212. }
  213. }
  214. }
  215. }
  216. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  217. removePublishedDataSetRepresentation(server, publishedDataSet);
  218. #endif
  219. UA_PublishedDataSet_deleteMembers(server, publishedDataSet);
  220. server->pubSubManager.publishedDataSetsSize--;
  221. //copy the last PDS to the removed PDS inside the allocated memory block
  222. if(server->pubSubManager.publishedDataSetsSize != publishedDataSetIndex){
  223. memcpy(&server->pubSubManager.publishedDataSets[publishedDataSetIndex],
  224. &server->pubSubManager.publishedDataSets[server->pubSubManager.publishedDataSetsSize],
  225. sizeof(UA_PublishedDataSet));
  226. }
  227. if(server->pubSubManager.publishedDataSetsSize <= 0){
  228. UA_free(server->pubSubManager.publishedDataSets);
  229. server->pubSubManager.publishedDataSets = NULL;
  230. } else {
  231. server->pubSubManager.publishedDataSets = (UA_PublishedDataSet *)
  232. UA_realloc(server->pubSubManager.publishedDataSets, sizeof(UA_PublishedDataSet) * server->pubSubManager.publishedDataSetsSize);
  233. if(!server->pubSubManager.publishedDataSets){
  234. return UA_STATUSCODE_BADINTERNALERROR;
  235. }
  236. //workaround - fixing issue with queue.h and realloc.
  237. for(size_t n = 0; n < server->pubSubManager.publishedDataSetsSize; n++){
  238. if(server->pubSubManager.publishedDataSets[n].fields.lh_first){
  239. server->pubSubManager.publishedDataSets[n].fields.lh_first->listEntry.le_prev = &server->pubSubManager.publishedDataSets[n].fields.lh_first;
  240. }
  241. }
  242. }
  243. return UA_STATUSCODE_GOOD;
  244. }
  245. /* Calculate the time difference between current time and UTC (00:00) on January
  246. * 1, 2000. */
  247. UA_UInt32
  248. UA_PubSubConfigurationVersionTimeDifference() {
  249. UA_UInt32 timeDiffSince2000 = (UA_UInt32) (UA_DateTime_now() - UA_DATETIMESTAMP_2000);
  250. return timeDiffSince2000;
  251. }
  252. /* Generate a new unique NodeId. This NodeId will be used for the information
  253. * model representation of PubSub entities. */
  254. void
  255. UA_PubSubManager_generateUniqueNodeId(UA_Server *server, UA_NodeId *nodeId) {
  256. UA_NodeId newNodeId = UA_NODEID_NUMERIC(0, 0);
  257. UA_Node *newNode = UA_Nodestore_new(server, UA_NODECLASS_OBJECT);
  258. UA_Nodestore_insert(server, newNode, &newNodeId);
  259. UA_NodeId_copy(&newNodeId, nodeId);
  260. }
  261. /* Delete the current PubSub configuration including all nested members. This
  262. * action also delete the configured PubSub transport Layers. */
  263. void
  264. UA_PubSubManager_delete(UA_Server *server, UA_PubSubManager *pubSubManager) {
  265. UA_LOG_INFO(server->config.logger, UA_LOGCATEGORY_SERVER, "PubSub cleanup was called.");
  266. //remove Connections and WriterGroups
  267. while(pubSubManager->connectionsSize > 0){
  268. UA_Server_removePubSubConnection(server, pubSubManager->connections[pubSubManager->connectionsSize-1].identifier);
  269. }
  270. while(pubSubManager->publishedDataSetsSize > 0){
  271. UA_Server_removePublishedDataSet(server, pubSubManager->publishedDataSets[pubSubManager->publishedDataSetsSize-1].identifier);
  272. }
  273. //free the currently configured transport layers
  274. for(size_t i = 0; i < server->config.pubsubTransportLayersSize; i++){
  275. UA_free(&server->config.pubsubTransportLayers[i]);
  276. }
  277. }
  278. /***********************************/
  279. /* PubSub Jobs abstraction */
  280. /***********************************/
  281. UA_StatusCode
  282. UA_PubSubManager_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  283. void *data, UA_UInt32 interval, UA_UInt64 *callbackId) {
  284. return UA_Timer_addRepeatedCallback(&server->timer, (UA_TimerCallback)callback,
  285. data, interval, callbackId);
  286. }
  287. UA_StatusCode
  288. UA_PubSubManager_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  289. UA_UInt32 interval) {
  290. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId, interval);
  291. }
  292. UA_StatusCode
  293. UA_PubSubManager_removeRepeatedPubSubCallback(UA_Server *server, UA_UInt64 callbackId) {
  294. return UA_Timer_removeRepeatedCallback(&server->timer, callbackId);
  295. }
  296. #endif /* UA_ENABLE_PUBSUB */