ua_pubsub_manager.c 16 KB

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