ua_pubsub.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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_types_encoding_binary.h"
  8. #include "ua_server_pubsub.h"
  9. #include "server/ua_server_internal.h"
  10. #include "ua_pubsub.h"
  11. #include "ua_pubsub_manager.h"
  12. #include "ua_pubsub_networkmessage.h"
  13. /**********************************************/
  14. /* Connection */
  15. /**********************************************/
  16. UA_StatusCode
  17. UA_PubSubConnectionConfig_copy(const UA_PubSubConnectionConfig *src,
  18. UA_PubSubConnectionConfig *dst) {
  19. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  20. memcpy(dst, src, sizeof(UA_PubSubConnectionConfig));
  21. retVal |= UA_String_copy(&src->name, &dst->name);
  22. retVal |= UA_Variant_copy(&src->address, &dst->address);
  23. retVal |= UA_String_copy(&src->transportProfileUri, &dst->transportProfileUri);
  24. retVal |= UA_Variant_copy(&src->connectionTransportSettings, &dst->connectionTransportSettings);
  25. if(src->connectionPropertiesSize > 0){
  26. dst->connectionProperties = (UA_KeyValuePair *)
  27. UA_calloc(src->connectionPropertiesSize, sizeof(UA_KeyValuePair));
  28. if(!dst->connectionProperties){
  29. return UA_STATUSCODE_BADOUTOFMEMORY;
  30. }
  31. for(size_t i = 0; i < src->connectionPropertiesSize; i++){
  32. retVal |= UA_QualifiedName_copy(&src->connectionProperties[i].key,
  33. &dst->connectionProperties[i].key);
  34. retVal |= UA_Variant_copy(&src->connectionProperties[i].value,
  35. &dst->connectionProperties[i].value);
  36. }
  37. }
  38. return retVal;
  39. }
  40. UA_StatusCode
  41. UA_Server_getPubSubConnectionConfig(UA_Server *server, const UA_NodeId connection,
  42. UA_PubSubConnectionConfig *config) {
  43. if(!config)
  44. return UA_STATUSCODE_BADINVALIDARGUMENT;
  45. UA_PubSubConnection *currentPubSubConnection =
  46. UA_PubSubConnection_findConnectionbyId(server, connection);
  47. if(!currentPubSubConnection)
  48. return UA_STATUSCODE_BADNOTFOUND;
  49. UA_PubSubConnectionConfig tmpPubSubConnectionConfig;
  50. //deep copy of the actual config
  51. UA_PubSubConnectionConfig_copy(currentPubSubConnection->config, &tmpPubSubConnectionConfig);
  52. *config = tmpPubSubConnectionConfig;
  53. return UA_STATUSCODE_GOOD;
  54. }
  55. UA_PubSubConnection *
  56. UA_PubSubConnection_findConnectionbyId(UA_Server *server, UA_NodeId connectionIdentifier) {
  57. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  58. if(UA_NodeId_equal(&connectionIdentifier, &server->pubSubManager.connections[i].identifier)){
  59. return &server->pubSubManager.connections[i];
  60. }
  61. }
  62. return NULL;
  63. }
  64. void
  65. UA_PubSubConnectionConfig_deleteMembers(UA_PubSubConnectionConfig *connectionConfig) {
  66. UA_String_deleteMembers(&connectionConfig->name);
  67. UA_String_deleteMembers(&connectionConfig->transportProfileUri);
  68. UA_Variant_deleteMembers(&connectionConfig->connectionTransportSettings);
  69. UA_Variant_deleteMembers(&connectionConfig->address);
  70. for(size_t i = 0; i < connectionConfig->connectionPropertiesSize; i++){
  71. UA_QualifiedName_deleteMembers(&connectionConfig->connectionProperties[i].key);
  72. UA_Variant_deleteMembers(&connectionConfig->connectionProperties[i].value);
  73. }
  74. UA_free(connectionConfig->connectionProperties);
  75. }
  76. void
  77. UA_PubSubConnection_deleteMembers(UA_Server *server, UA_PubSubConnection *connection) {
  78. //delete connection config
  79. UA_PubSubConnectionConfig_deleteMembers(connection->config);
  80. //remove contained WriterGroups
  81. UA_WriterGroup *writerGroup, *tmpWriterGroup;
  82. LIST_FOREACH_SAFE(writerGroup, &connection->writerGroups, listEntry, tmpWriterGroup){
  83. UA_Server_removeWriterGroup(server, writerGroup->identifier);
  84. }
  85. UA_NodeId_deleteMembers(&connection->identifier);
  86. if(connection->channel){
  87. connection->channel->close(connection->channel);
  88. }
  89. UA_free(connection->config);
  90. }
  91. UA_StatusCode
  92. UA_Server_addWriterGroup(UA_Server *server, const UA_NodeId connection,
  93. const UA_WriterGroupConfig *writerGroupConfig,
  94. UA_NodeId *writerGroupIdentifier) {
  95. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  96. if(!writerGroupConfig)
  97. return UA_STATUSCODE_BADINVALIDARGUMENT;
  98. //search the connection by the given connectionIdentifier
  99. UA_PubSubConnection *currentConnectionContext =
  100. UA_PubSubConnection_findConnectionbyId(server, connection);
  101. if(!currentConnectionContext)
  102. return UA_STATUSCODE_BADNOTFOUND;
  103. //allocate memory for new WriterGroup
  104. UA_WriterGroup *newWriterGroup = (UA_WriterGroup *) UA_calloc(1, sizeof(UA_WriterGroup));
  105. if (!newWriterGroup)
  106. return UA_STATUSCODE_BADOUTOFMEMORY;
  107. newWriterGroup->linkedConnection = currentConnectionContext->identifier;
  108. UA_PubSubManager_generateUniqueNodeId(server, &newWriterGroup->identifier);
  109. if(writerGroupIdentifier){
  110. UA_NodeId_copy(&newWriterGroup->identifier, writerGroupIdentifier);
  111. }
  112. UA_WriterGroupConfig tmpWriterGroupConfig;
  113. //deep copy of the config
  114. retVal |= UA_WriterGroupConfig_copy(writerGroupConfig, &tmpWriterGroupConfig);
  115. newWriterGroup->config = tmpWriterGroupConfig;
  116. retVal |= UA_WriterGroup_addPublishCallback(server, newWriterGroup);
  117. LIST_INSERT_HEAD(&currentConnectionContext->writerGroups, newWriterGroup, listEntry);
  118. return retVal;
  119. }
  120. UA_StatusCode
  121. UA_Server_removeWriterGroup(UA_Server *server, const UA_NodeId writerGroup){
  122. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup);
  123. if(!wg)
  124. return UA_STATUSCODE_BADNOTFOUND;
  125. UA_PubSubConnection *connection =
  126. UA_PubSubConnection_findConnectionbyId(server, wg->linkedConnection);
  127. if(!connection)
  128. return UA_STATUSCODE_BADNOTFOUND;
  129. //unregister the publish callback
  130. if(UA_PubSubManager_removeRepeatedPubSubCallback(server, wg->publishCallbackId) != UA_STATUSCODE_GOOD)
  131. return UA_STATUSCODE_BADINTERNALERROR;
  132. UA_WriterGroup_deleteMembers(server, wg);
  133. UA_free(wg);
  134. return UA_STATUSCODE_GOOD;
  135. }
  136. /**********************************************/
  137. /* PublishedDataSet */
  138. /**********************************************/
  139. UA_StatusCode
  140. UA_PublishedDataSetConfig_copy(const UA_PublishedDataSetConfig *src,
  141. UA_PublishedDataSetConfig *dst) {
  142. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  143. memcpy(dst, src, sizeof(UA_PublishedDataSetConfig));
  144. retVal |= UA_String_copy(&src->name, &dst->name);
  145. switch(src->publishedDataSetType){
  146. case UA_PUBSUB_DATASET_PUBLISHEDITEMS:
  147. //no additional items
  148. break;
  149. case UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE:
  150. if (src->config.itemsTemplate.variablesToAddSize > 0){
  151. dst->config.itemsTemplate.variablesToAdd = (UA_PublishedVariableDataType *) UA_calloc(
  152. src->config.itemsTemplate.variablesToAddSize, sizeof(UA_PublishedVariableDataType));
  153. }
  154. for(size_t i = 0; i < src->config.itemsTemplate.variablesToAddSize; i++){
  155. retVal |= UA_PublishedVariableDataType_copy(&src->config.itemsTemplate.variablesToAdd[i],
  156. &dst->config.itemsTemplate.variablesToAdd[i]);
  157. }
  158. retVal |= UA_DataSetMetaDataType_copy(&src->config.itemsTemplate.metaData,
  159. &dst->config.itemsTemplate.metaData);
  160. break;
  161. default:
  162. return UA_STATUSCODE_BADINVALIDARGUMENT;
  163. }
  164. return retVal;
  165. }
  166. UA_StatusCode
  167. UA_Server_getPublishedDataSetConfig(UA_Server *server, const UA_NodeId pds,
  168. UA_PublishedDataSetConfig *config){
  169. if(!config)
  170. return UA_STATUSCODE_BADINVALIDARGUMENT;
  171. UA_PublishedDataSet *currentPublishedDataSet = UA_PublishedDataSet_findPDSbyId(server, pds);
  172. if(!currentPublishedDataSet)
  173. return UA_STATUSCODE_BADNOTFOUND;
  174. UA_PublishedDataSetConfig tmpPublishedDataSetConfig;
  175. //deep copy of the actual config
  176. UA_PublishedDataSetConfig_copy(&currentPublishedDataSet->config, &tmpPublishedDataSetConfig);
  177. *config = tmpPublishedDataSetConfig;
  178. return UA_STATUSCODE_GOOD;
  179. }
  180. UA_PublishedDataSet *
  181. UA_PublishedDataSet_findPDSbyId(UA_Server *server, UA_NodeId identifier){
  182. for(size_t i = 0; i < server->pubSubManager.publishedDataSetsSize; i++){
  183. if(UA_NodeId_equal(&server->pubSubManager.publishedDataSets[i].identifier, &identifier)){
  184. return &server->pubSubManager.publishedDataSets[i];
  185. }
  186. }
  187. return NULL;
  188. }
  189. void
  190. UA_PublishedDataSetConfig_deleteMembers(UA_PublishedDataSetConfig *pdsConfig){
  191. //delete pds config
  192. UA_String_deleteMembers(&pdsConfig->name);
  193. switch (pdsConfig->publishedDataSetType){
  194. case UA_PUBSUB_DATASET_PUBLISHEDITEMS:
  195. //no additional items
  196. break;
  197. case UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE:
  198. if (pdsConfig->config.itemsTemplate.variablesToAddSize > 0){
  199. for(size_t i = 0; i < pdsConfig->config.itemsTemplate.variablesToAddSize; i++){
  200. UA_PublishedVariableDataType_deleteMembers(&pdsConfig->config.itemsTemplate.variablesToAdd[i]);
  201. }
  202. UA_free(pdsConfig->config.itemsTemplate.variablesToAdd);
  203. }
  204. UA_DataSetMetaDataType_deleteMembers(&pdsConfig->config.itemsTemplate.metaData);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. void
  211. UA_PublishedDataSet_deleteMembers(UA_Server *server, UA_PublishedDataSet *publishedDataSet){
  212. UA_PublishedDataSetConfig_deleteMembers(&publishedDataSet->config);
  213. //delete PDS
  214. UA_DataSetMetaDataType_deleteMembers(&publishedDataSet->dataSetMetaData);
  215. UA_DataSetField *field, *tmpField;
  216. LIST_FOREACH_SAFE(field, &publishedDataSet->fields, listEntry, tmpField) {
  217. UA_Server_removeDataSetField(server, field->identifier);
  218. }
  219. UA_NodeId_deleteMembers(&publishedDataSet->identifier);
  220. }
  221. UA_DataSetFieldResult
  222. UA_Server_addDataSetField(UA_Server *server, const UA_NodeId publishedDataSet,
  223. const UA_DataSetFieldConfig *fieldConfig,
  224. UA_NodeId *fieldIdentifier) {
  225. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  226. if(!fieldConfig)
  227. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADINVALIDARGUMENT, {0, 0}};
  228. UA_PublishedDataSet *currentDataSet = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet);
  229. if(currentDataSet == NULL)
  230. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADNOTFOUND, {0, 0}};
  231. if(currentDataSet->config.publishedDataSetType != UA_PUBSUB_DATASET_PUBLISHEDITEMS)
  232. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADNOTIMPLEMENTED, {0, 0}};
  233. UA_DataSetField *newField = (UA_DataSetField *) UA_calloc(1, sizeof(UA_DataSetField));
  234. if(!newField)
  235. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADINTERNALERROR, {0, 0}};
  236. UA_DataSetFieldConfig tmpFieldConfig;
  237. retVal |= UA_DataSetFieldConfig_copy(fieldConfig, &tmpFieldConfig);
  238. newField->config = tmpFieldConfig;
  239. UA_PubSubManager_generateUniqueNodeId(server, &newField->identifier);
  240. if(fieldIdentifier != NULL){
  241. UA_NodeId_copy(&newField->identifier, fieldIdentifier);
  242. }
  243. newField->publishedDataSet = currentDataSet->identifier;
  244. //update major version of parent published data set
  245. currentDataSet->dataSetMetaData.configurationVersion.majorVersion = UA_PubSubConfigurationVersionTimeDifference();
  246. LIST_INSERT_HEAD(&currentDataSet->fields, newField, listEntry);
  247. if(newField->config.field.variable.promotedField)
  248. currentDataSet->promotedFieldsCount++;
  249. currentDataSet->fieldSize++;
  250. UA_DataSetFieldResult result =
  251. {retVal, {currentDataSet->dataSetMetaData.configurationVersion.majorVersion,
  252. currentDataSet->dataSetMetaData.configurationVersion.minorVersion}};
  253. return result;
  254. }
  255. UA_DataSetFieldResult
  256. UA_Server_removeDataSetField(UA_Server *server, const UA_NodeId dsf) {
  257. UA_DataSetField *currentField = UA_DataSetField_findDSFbyId(server, dsf);
  258. if(!currentField)
  259. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADNOTFOUND, {0, 0}};
  260. UA_PublishedDataSet *parentPublishedDataSet =
  261. UA_PublishedDataSet_findPDSbyId(server, currentField->publishedDataSet);
  262. if(!parentPublishedDataSet)
  263. return (UA_DataSetFieldResult) {UA_STATUSCODE_BADNOTFOUND, {0, 0}};
  264. parentPublishedDataSet->fieldSize--;
  265. if(currentField->config.field.variable.promotedField)
  266. parentPublishedDataSet->promotedFieldsCount--;
  267. /* update major version of PublishedDataSet */
  268. parentPublishedDataSet->dataSetMetaData.configurationVersion.majorVersion =
  269. UA_PubSubConfigurationVersionTimeDifference();
  270. UA_DataSetField_deleteMembers(currentField);
  271. UA_free(currentField);
  272. UA_DataSetFieldResult result =
  273. {UA_STATUSCODE_GOOD, {parentPublishedDataSet->dataSetMetaData.configurationVersion.majorVersion,
  274. parentPublishedDataSet->dataSetMetaData.configurationVersion.minorVersion}};
  275. return result;
  276. }
  277. /**********************************************/
  278. /* DataSetWriter */
  279. /**********************************************/
  280. UA_StatusCode
  281. UA_DataSetWriterConfig_copy(const UA_DataSetWriterConfig *src,
  282. UA_DataSetWriterConfig *dst){
  283. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  284. memcpy(dst, src, sizeof(UA_DataSetWriterConfig));
  285. retVal |= UA_String_copy(&src->name, &dst->name);
  286. retVal |= UA_String_copy(&src->dataSetName, &dst->dataSetName);
  287. retVal |= UA_ExtensionObject_copy(&src->messageSettings, &dst->messageSettings);
  288. dst->dataSetWriterProperties = (UA_KeyValuePair *)
  289. UA_calloc(src->dataSetWriterPropertiesSize, sizeof(UA_KeyValuePair));
  290. if(!dst->dataSetWriterProperties)
  291. return UA_STATUSCODE_BADOUTOFMEMORY;
  292. for(size_t i = 0; i < src->dataSetWriterPropertiesSize; i++){
  293. retVal |= UA_KeyValuePair_copy(&src->dataSetWriterProperties[i], &dst->dataSetWriterProperties[i]);
  294. }
  295. return retVal;
  296. }
  297. UA_StatusCode
  298. UA_Server_getDataSetWriterConfig(UA_Server *server, const UA_NodeId dsw,
  299. UA_DataSetWriterConfig *config){
  300. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  301. if(!config)
  302. return UA_STATUSCODE_BADINVALIDARGUMENT;
  303. UA_DataSetWriter *currentDataSetWriter = UA_DataSetWriter_findDSWbyId(server, dsw);
  304. if(!currentDataSetWriter)
  305. return UA_STATUSCODE_BADNOTFOUND;
  306. UA_DataSetWriterConfig tmpWriterConfig;
  307. //deep copy of the actual config
  308. retVal |= UA_DataSetWriterConfig_copy(&currentDataSetWriter->config, &tmpWriterConfig);
  309. *config = tmpWriterConfig;
  310. return retVal;
  311. }
  312. UA_DataSetWriter *
  313. UA_DataSetWriter_findDSWbyId(UA_Server *server, UA_NodeId identifier) {
  314. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  315. UA_WriterGroup *tmpWriterGroup;
  316. LIST_FOREACH(tmpWriterGroup, &server->pubSubManager.connections[i].writerGroups, listEntry){
  317. UA_DataSetWriter *tmpWriter;
  318. LIST_FOREACH(tmpWriter, &tmpWriterGroup->writers, listEntry){
  319. if(UA_NodeId_equal(&tmpWriter->identifier, &identifier)){
  320. return tmpWriter;
  321. }
  322. }
  323. }
  324. }
  325. return NULL;
  326. }
  327. void
  328. UA_DataSetWriterConfig_deleteMembers(UA_DataSetWriterConfig *pdsConfig) {
  329. UA_String_deleteMembers(&pdsConfig->name);
  330. UA_String_deleteMembers(&pdsConfig->dataSetName);
  331. for(size_t i = 0; i < pdsConfig->dataSetWriterPropertiesSize; i++){
  332. UA_KeyValuePair_deleteMembers(&pdsConfig->dataSetWriterProperties[i]);
  333. }
  334. UA_free(pdsConfig->dataSetWriterProperties);
  335. UA_ExtensionObject_deleteMembers(&pdsConfig->messageSettings);
  336. }
  337. void
  338. UA_DataSetWriter_deleteMembers(UA_Server *server, UA_DataSetWriter *dataSetWriter){
  339. UA_DataSetWriterConfig_deleteMembers(&dataSetWriter->config);
  340. //delete DataSetWriter
  341. UA_NodeId_deleteMembers(&dataSetWriter->identifier);
  342. UA_NodeId_deleteMembers(&dataSetWriter->linkedWriterGroup);
  343. UA_NodeId_deleteMembers(&dataSetWriter->connectedDataSet);
  344. LIST_REMOVE(dataSetWriter, listEntry);
  345. //delete lastSamples store
  346. for(size_t i = 0; i < dataSetWriter->lastSamplesCount; i++){
  347. UA_DataValue_delete(dataSetWriter->lastSamples[i].value);
  348. }
  349. LIST_REMOVE(dataSetWriter, listEntry);
  350. UA_free(dataSetWriter->lastSamples);
  351. }
  352. /**********************************************/
  353. /* WriterGroup */
  354. /**********************************************/
  355. UA_StatusCode
  356. UA_WriterGroupConfig_copy(const UA_WriterGroupConfig *src,
  357. UA_WriterGroupConfig *dst){
  358. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  359. memcpy(dst, src, sizeof(UA_WriterGroupConfig));
  360. retVal |= UA_String_copy(&src->name, &dst->name);
  361. retVal |= UA_ExtensionObject_copy(&src->transportSettings, &dst->transportSettings);
  362. retVal |= UA_ExtensionObject_copy(&src->messageSettings, &dst->messageSettings);
  363. dst->groupProperties = (UA_KeyValuePair *) UA_calloc(src->groupPropertiesSize, sizeof(UA_KeyValuePair));
  364. if(!dst->groupProperties)
  365. return UA_STATUSCODE_BADOUTOFMEMORY;
  366. for(size_t i = 0; i < src->groupPropertiesSize; i++){
  367. retVal |= UA_KeyValuePair_copy(&src->groupProperties[i], &dst->groupProperties[i]);
  368. }
  369. return retVal;
  370. }
  371. UA_StatusCode
  372. UA_Server_getWriterGroupConfig(UA_Server *server, const UA_NodeId writerGroup,
  373. UA_WriterGroupConfig *config){
  374. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  375. if(!config)
  376. return UA_STATUSCODE_BADINVALIDARGUMENT;
  377. UA_WriterGroup *currentWriterGroup = UA_WriterGroup_findWGbyId(server, writerGroup);
  378. if(!currentWriterGroup){
  379. return UA_STATUSCODE_BADNOTFOUND;
  380. }
  381. UA_WriterGroupConfig tmpWriterGroupConfig;
  382. //deep copy of the actual config
  383. retVal |= UA_WriterGroupConfig_copy(&currentWriterGroup->config, &tmpWriterGroupConfig);
  384. *config = tmpWriterGroupConfig;
  385. return retVal;
  386. }
  387. UA_WriterGroup *
  388. UA_WriterGroup_findWGbyId(UA_Server *server, UA_NodeId identifier){
  389. for(size_t i = 0; i < server->pubSubManager.connectionsSize; i++){
  390. UA_WriterGroup *tmpWriterGroup;
  391. LIST_FOREACH(tmpWriterGroup, &server->pubSubManager.connections[i].writerGroups, listEntry) {
  392. if(UA_NodeId_equal(&identifier, &tmpWriterGroup->identifier)){
  393. return tmpWriterGroup;
  394. }
  395. }
  396. }
  397. return NULL;
  398. }
  399. void
  400. UA_WriterGroupConfig_deleteMembers(UA_WriterGroupConfig *writerGroupConfig){
  401. //delete writerGroup config
  402. UA_String_deleteMembers(&writerGroupConfig->name);
  403. UA_ExtensionObject_deleteMembers(&writerGroupConfig->transportSettings);
  404. UA_ExtensionObject_deleteMembers(&writerGroupConfig->messageSettings);
  405. for(size_t i = 0; i < writerGroupConfig->groupPropertiesSize; i++){
  406. UA_KeyValuePair_deleteMembers(&writerGroupConfig->groupProperties[i]);
  407. }
  408. UA_free(writerGroupConfig->groupProperties);
  409. }
  410. void
  411. UA_WriterGroup_deleteMembers(UA_Server *server, UA_WriterGroup *writerGroup) {
  412. UA_WriterGroupConfig_deleteMembers(&writerGroup->config);
  413. //delete WriterGroup
  414. //delete all writers. Therefore removeDataSetWriter is called from PublishedDataSet
  415. UA_DataSetWriter *dataSetWriter, *tmpDataSetWriter;
  416. LIST_FOREACH_SAFE(dataSetWriter, &writerGroup->writers, listEntry, tmpDataSetWriter){
  417. UA_Server_removeDataSetWriter(server, dataSetWriter->identifier);
  418. }
  419. LIST_REMOVE(writerGroup, listEntry);
  420. UA_NodeId_deleteMembers(&writerGroup->linkedConnection);
  421. UA_NodeId_deleteMembers(&writerGroup->identifier);
  422. }
  423. UA_StatusCode
  424. UA_Server_addDataSetWriter(UA_Server *server,
  425. const UA_NodeId writerGroup, const UA_NodeId dataSet,
  426. const UA_DataSetWriterConfig *dataSetWriterConfig,
  427. UA_NodeId *writerIdentifier) {
  428. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  429. if(!dataSetWriterConfig)
  430. return UA_STATUSCODE_BADINVALIDARGUMENT;
  431. UA_PublishedDataSet *currentDataSetContext = UA_PublishedDataSet_findPDSbyId(server, dataSet);
  432. if(!currentDataSetContext)
  433. return UA_STATUSCODE_BADNOTFOUND;
  434. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup);
  435. if(!wg)
  436. return UA_STATUSCODE_BADNOTFOUND;
  437. UA_DataSetWriter *newDataSetWriter = (UA_DataSetWriter *) UA_calloc(1, sizeof(UA_DataSetWriter));
  438. if(!newDataSetWriter)
  439. return UA_STATUSCODE_BADOUTOFMEMORY;
  440. //copy the config into the new dataSetWriter
  441. UA_DataSetWriterConfig tmpDataSetWriterConfig;
  442. retVal |= UA_DataSetWriterConfig_copy(dataSetWriterConfig, &tmpDataSetWriterConfig);
  443. newDataSetWriter->config = tmpDataSetWriterConfig;
  444. //save the current version of the connected PublishedDataSet
  445. newDataSetWriter->connectedDataSetVersion = currentDataSetContext->dataSetMetaData.configurationVersion;
  446. //initialize the queue for the last values
  447. newDataSetWriter->lastSamplesCount = currentDataSetContext->fieldSize;
  448. newDataSetWriter->lastSamples = (UA_DataSetWriterSample * )
  449. UA_calloc(newDataSetWriter->lastSamplesCount, sizeof(UA_DataSetWriterSample));
  450. if(!newDataSetWriter->lastSamples) {
  451. UA_DataSetWriterConfig_deleteMembers(&newDataSetWriter->config);
  452. UA_free(newDataSetWriter);
  453. return UA_STATUSCODE_BADOUTOFMEMORY;
  454. }
  455. for(size_t i = 0; i < newDataSetWriter->lastSamplesCount; i++) {
  456. newDataSetWriter->lastSamples[i].value = (UA_DataValue *) UA_calloc(1, sizeof(UA_DataValue));
  457. if(!newDataSetWriter->lastSamples[i].value) {
  458. for(size_t j = 0; j < i; j++)
  459. UA_free(newDataSetWriter->lastSamples[j].value);
  460. UA_DataSetWriterConfig_deleteMembers(&newDataSetWriter->config);
  461. UA_free(newDataSetWriter);
  462. return UA_STATUSCODE_BADOUTOFMEMORY;
  463. }
  464. }
  465. //connect PublishedDataSet with DataSetWriter
  466. newDataSetWriter->connectedDataSet = currentDataSetContext->identifier;
  467. newDataSetWriter->linkedWriterGroup = wg->identifier;
  468. UA_PubSubManager_generateUniqueNodeId(server, &newDataSetWriter->identifier);
  469. if(writerIdentifier != NULL)
  470. UA_NodeId_copy(&newDataSetWriter->identifier, writerIdentifier);
  471. //add the new writer to the group
  472. LIST_INSERT_HEAD(&wg->writers, newDataSetWriter, listEntry);
  473. wg->writersCount++;
  474. return retVal;
  475. }
  476. UA_StatusCode
  477. UA_Server_removeDataSetWriter(UA_Server *server, const UA_NodeId dsw){
  478. UA_DataSetWriter *dataSetWriter = UA_DataSetWriter_findDSWbyId(server, dsw);
  479. if(!dataSetWriter)
  480. return UA_STATUSCODE_BADNOTFOUND;
  481. UA_WriterGroup *linkedWriterGroup = UA_WriterGroup_findWGbyId(server, dataSetWriter->linkedWriterGroup);
  482. if(!linkedWriterGroup)
  483. return UA_STATUSCODE_BADNOTFOUND;
  484. linkedWriterGroup->writersCount--;
  485. //remove DataSetWriter from group
  486. UA_DataSetWriter_deleteMembers(server, dataSetWriter);
  487. UA_free(dataSetWriter);
  488. return UA_STATUSCODE_GOOD;
  489. }
  490. /**********************************************/
  491. /* DataSetField */
  492. /**********************************************/
  493. UA_StatusCode
  494. UA_DataSetFieldConfig_copy(const UA_DataSetFieldConfig *src, UA_DataSetFieldConfig *dst){
  495. memcpy(dst, src, sizeof(UA_DataSetFieldConfig));
  496. if(src->dataSetFieldType == UA_PUBSUB_DATASETFIELD_VARIABLE) {
  497. UA_String_copy(&src->field.variable.fieldNameAlias, &dst->field.variable.fieldNameAlias);
  498. UA_PublishedVariableDataType_copy(&src->field.variable.publishParameters,
  499. &dst->field.variable.publishParameters);
  500. } else {
  501. return UA_STATUSCODE_BADNOTSUPPORTED;
  502. }
  503. return UA_STATUSCODE_GOOD;
  504. }
  505. UA_StatusCode
  506. UA_Server_getDataSetFieldConfig(UA_Server *server, const UA_NodeId dsf,
  507. UA_DataSetFieldConfig *config) {
  508. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  509. if(!config)
  510. return UA_STATUSCODE_BADINVALIDARGUMENT;
  511. UA_DataSetField *currentDataSetField = UA_DataSetField_findDSFbyId(server, dsf);
  512. if(!currentDataSetField)
  513. return UA_STATUSCODE_BADNOTFOUND;
  514. UA_DataSetFieldConfig tmpFieldConfig;
  515. //deep copy of the actual config
  516. retVal |= UA_DataSetFieldConfig_copy(&currentDataSetField->config, &tmpFieldConfig);
  517. *config = tmpFieldConfig;
  518. return retVal;
  519. }
  520. UA_DataSetField *
  521. UA_DataSetField_findDSFbyId(UA_Server *server, UA_NodeId identifier) {
  522. for(size_t i = 0; i < server->pubSubManager.publishedDataSetsSize; i++){
  523. UA_DataSetField *tmpField;
  524. LIST_FOREACH(tmpField, &server->pubSubManager.publishedDataSets[i].fields, listEntry){
  525. if(UA_NodeId_equal(&tmpField->identifier, &identifier)){
  526. return tmpField;
  527. }
  528. }
  529. }
  530. return NULL;
  531. }
  532. void
  533. UA_DataSetFieldConfig_deleteMembers(UA_DataSetFieldConfig *dataSetFieldConfig){
  534. if(dataSetFieldConfig->dataSetFieldType == UA_PUBSUB_DATASETFIELD_VARIABLE){
  535. UA_String_deleteMembers(&dataSetFieldConfig->field.variable.fieldNameAlias);
  536. UA_PublishedVariableDataType_deleteMembers(&dataSetFieldConfig->field.variable.publishParameters);
  537. }
  538. }
  539. void UA_DataSetField_deleteMembers(UA_DataSetField *field) {
  540. UA_DataSetFieldConfig_deleteMembers(&field->config);
  541. //delete DataSetField
  542. UA_NodeId_deleteMembers(&field->identifier);
  543. UA_NodeId_deleteMembers(&field->publishedDataSet);
  544. UA_FieldMetaData_deleteMembers(&field->fieldMetaData);
  545. UA_DataValue_deleteMembers(&field->lastValue);
  546. LIST_REMOVE(field, listEntry);
  547. }
  548. /*********************************************************/
  549. /* PublishValues handling */
  550. /*********************************************************/
  551. /**
  552. * Compare two variants. Internally used for value change detection.
  553. *
  554. * @return UA_TRUE if the value has changed
  555. */
  556. static UA_Boolean
  557. valueChangedVariant(UA_Variant *oldValue, UA_Variant *newValue){
  558. if(! (oldValue && newValue))
  559. return UA_FALSE;
  560. UA_ByteString *oldValueEncoding = UA_ByteString_new(), *newValueEncoding = UA_ByteString_new();
  561. size_t oldValueEncodingSize, newValueEncodingSize;
  562. oldValueEncodingSize = UA_calcSizeBinary(oldValue, &UA_TYPES[UA_TYPES_VARIANT]);
  563. newValueEncodingSize = UA_calcSizeBinary(newValue, &UA_TYPES[UA_TYPES_VARIANT]);
  564. if((oldValueEncodingSize == 0) || (newValueEncodingSize == 0))
  565. return UA_FALSE;
  566. if(oldValueEncodingSize != newValueEncodingSize)
  567. return UA_TRUE;
  568. if(UA_ByteString_allocBuffer(oldValueEncoding, oldValueEncodingSize) != UA_STATUSCODE_GOOD)
  569. return UA_FALSE;
  570. if(UA_ByteString_allocBuffer(newValueEncoding, newValueEncodingSize) != UA_STATUSCODE_GOOD)
  571. return UA_FALSE;
  572. UA_Byte *bufPosOldValue = oldValueEncoding->data;
  573. const UA_Byte *bufEndOldValue = &oldValueEncoding->data[oldValueEncoding->length];
  574. UA_Byte *bufPosNewValue = newValueEncoding->data;
  575. const UA_Byte *bufEndNewValue = &newValueEncoding->data[newValueEncoding->length];
  576. if(UA_encodeBinary(oldValue, &UA_TYPES[UA_TYPES_VARIANT],
  577. &bufPosOldValue, &bufEndOldValue, NULL, NULL) != UA_STATUSCODE_GOOD){
  578. return UA_FALSE;
  579. }
  580. if(UA_encodeBinary(newValue, &UA_TYPES[UA_TYPES_VARIANT],
  581. &bufPosNewValue, &bufEndNewValue, NULL, NULL) != UA_STATUSCODE_GOOD){
  582. return UA_FALSE;
  583. }
  584. oldValueEncoding->length = (uintptr_t)bufPosOldValue - (uintptr_t)oldValueEncoding->data;
  585. newValueEncoding->length = (uintptr_t)bufPosNewValue - (uintptr_t)newValueEncoding->data;
  586. UA_Boolean compareResult = !UA_ByteString_equal(oldValueEncoding, newValueEncoding);
  587. UA_ByteString_delete(oldValueEncoding);
  588. UA_ByteString_delete(newValueEncoding);
  589. return compareResult;
  590. }
  591. /**
  592. * Obtain the latest value for a specific DataSetField. This method is currently
  593. * called inside the DataSetMessage generation process.
  594. */
  595. static UA_StatusCode
  596. UA_PubSubDataSetField_sampleValue(UA_Server *server, UA_DataSetField *field) {
  597. /* Read the value */
  598. UA_ReadValueId rvid;
  599. UA_ReadValueId_init(&rvid);
  600. rvid.nodeId = field->config.field.variable.publishParameters.publishedVariable;
  601. rvid.attributeId = field->config.field.variable.publishParameters.attributeId;
  602. rvid.indexRange = field->config.field.variable.publishParameters.indexRange;
  603. UA_DataValue value = UA_Server_read(server, &rvid, UA_TIMESTAMPSTORETURN_BOTH);
  604. UA_DataValue_deleteMembers(&field->lastValue);
  605. field->lastValue = value;
  606. return UA_STATUSCODE_GOOD;
  607. }
  608. static UA_StatusCode
  609. UA_PubSubDataSetWriter_generateKeyFrameMessage(UA_Server *server, UA_DataSetMessage *dataSetMessage,
  610. UA_DataSetWriter *dataSetWriter) {
  611. UA_PublishedDataSet *currentDataSet = UA_PublishedDataSet_findPDSbyId(server, dataSetWriter->connectedDataSet);
  612. if(!currentDataSet)
  613. return UA_STATUSCODE_BADNOTFOUND;
  614. //prepare DataSetMessageContent
  615. dataSetMessage->header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  616. dataSetMessage->data.keyFrameData.fieldCount = currentDataSet->fieldSize;
  617. dataSetMessage->data.keyFrameData.dataSetFields = (UA_DataValue *)
  618. UA_Array_new(currentDataSet->fieldSize, &UA_TYPES[UA_TYPES_DATAVALUE]);
  619. if(!dataSetMessage->data.keyFrameData.dataSetFields)
  620. return UA_STATUSCODE_BADOUTOFMEMORY;
  621. UA_DataSetField *tmpDataSetField;
  622. size_t counter = 0;
  623. LIST_FOREACH(tmpDataSetField, &currentDataSet->fields, listEntry){
  624. if(UA_PubSubDataSetField_sampleValue(server, tmpDataSetField) == UA_STATUSCODE_GOOD){
  625. //include field into DSM
  626. UA_DataValue_init(&dataSetMessage->data.keyFrameData.dataSetFields[counter]);
  627. UA_DataValue_copy(&tmpDataSetField->lastValue, &dataSetMessage->data.keyFrameData.dataSetFields[counter]);
  628. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_STATUSCODE) == 0){
  629. dataSetMessage->data.keyFrameData.dataSetFields[counter].hasStatus = UA_FALSE;
  630. }
  631. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCETIMESTAMP) == 0){
  632. dataSetMessage->data.keyFrameData.dataSetFields[counter].hasSourceTimestamp = UA_FALSE;
  633. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCEPICOSECONDS) == 0){
  634. dataSetMessage->data.keyFrameData.dataSetFields[counter].hasServerPicoseconds = UA_FALSE;
  635. }
  636. }
  637. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SERVERTIMESTAMP) == 0){
  638. dataSetMessage->data.keyFrameData.dataSetFields[counter].hasServerTimestamp = UA_FALSE;
  639. }
  640. //Update lastValue store
  641. UA_DataValue_deleteMembers(dataSetWriter->lastSamples[counter].value);
  642. UA_DataValue_copy(&tmpDataSetField->lastValue, dataSetWriter->lastSamples[counter++].value);
  643. }
  644. }
  645. return UA_STATUSCODE_GOOD;
  646. }
  647. static UA_StatusCode
  648. UA_PubSubDataSetWriter_generateDeltaFrameMessage(UA_Server *server, UA_DataSetMessage *dataSetMessage,
  649. UA_DataSetWriter *dataSetWriter) {
  650. UA_PublishedDataSet *currentDataSet = UA_PublishedDataSet_findPDSbyId(server, dataSetWriter->connectedDataSet);
  651. if(!currentDataSet)
  652. return UA_STATUSCODE_BADNOTFOUND;
  653. //prepare DataSetMessageContent
  654. memset(dataSetMessage, 0, sizeof(UA_DataSetMessage));
  655. dataSetMessage->header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  656. UA_DataSetField *tmpDataSetField;
  657. size_t counter = 0;
  658. LIST_FOREACH(tmpDataSetField, &currentDataSet->fields, listEntry) {
  659. if(UA_PubSubDataSetField_sampleValue(server, tmpDataSetField) == UA_STATUSCODE_GOOD) {
  660. //check if the value has changed
  661. if(valueChangedVariant(&dataSetWriter->lastSamples[counter].value->value, &tmpDataSetField->lastValue.value)){
  662. //increase fieldCount for current delta message
  663. dataSetMessage->data.deltaFrameData.fieldCount++;
  664. dataSetWriter->lastSamples[counter].valeChanged = UA_TRUE;
  665. }
  666. //update last stored sample
  667. UA_DataValue_init(dataSetWriter->lastSamples[counter].value);
  668. UA_DataValue_copy(&tmpDataSetField->lastValue, dataSetWriter->lastSamples[counter++].value);
  669. }
  670. }
  671. //allocate DeltaFrameFields
  672. UA_DataSetMessage_DeltaFrameField * deltaFields = (UA_DataSetMessage_DeltaFrameField *)
  673. UA_calloc(dataSetMessage->data.deltaFrameData.fieldCount, sizeof(UA_DataSetMessage_DeltaFrameField));
  674. if(!deltaFields)
  675. return UA_STATUSCODE_BADOUTOFMEMORY;
  676. dataSetMessage->data.deltaFrameData.deltaFrameFields = deltaFields;
  677. size_t currentDeltaField = 0;
  678. for(size_t i = 0; i < currentDataSet->fieldSize; i++){
  679. if(dataSetWriter->lastSamples[i].valeChanged){
  680. deltaFields[currentDeltaField].fieldIndex = (UA_UInt16) i;
  681. UA_DataValue_copy(dataSetWriter->lastSamples[i].value, &deltaFields[currentDeltaField].fieldValue);
  682. dataSetWriter->lastSamples[i].valeChanged = false;
  683. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_STATUSCODE) == 0){
  684. dataSetMessage->data.deltaFrameData.deltaFrameFields[currentDeltaField].fieldValue.hasStatus = UA_FALSE;
  685. }
  686. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCETIMESTAMP) == 0){
  687. dataSetMessage->data.deltaFrameData.deltaFrameFields[currentDeltaField].fieldValue.hasSourceTimestamp = UA_FALSE;
  688. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCEPICOSECONDS) == 0){
  689. dataSetMessage->data.deltaFrameData.deltaFrameFields[currentDeltaField].fieldValue.hasServerPicoseconds = UA_FALSE;
  690. }
  691. }
  692. if((dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_SERVERTIMESTAMP) == 0){
  693. dataSetMessage->data.deltaFrameData.deltaFrameFields[currentDeltaField].fieldValue.hasServerTimestamp = UA_FALSE;
  694. }
  695. currentDeltaField++;
  696. }
  697. }
  698. return UA_STATUSCODE_GOOD;
  699. }
  700. /**
  701. * Generate a DataSetMessage for the given writer.
  702. *
  703. * @param dataSetWriter ptr to corresponding writer
  704. * @return ptr to generated DataSetMessage
  705. */
  706. static UA_StatusCode
  707. UA_DataSetWriter_generateDataSetMessage(UA_Server *server, UA_DataSetMessage *dataSetMessage,
  708. UA_DataSetWriter *dataSetWriter) {
  709. UA_PublishedDataSet *currentDataSet = UA_PublishedDataSet_findPDSbyId(server, dataSetWriter->connectedDataSet);
  710. if(!currentDataSet)
  711. return UA_STATUSCODE_BADNOTFOUND;
  712. memset(dataSetMessage, 0, sizeof(UA_DataSetMessage));
  713. //currently is only UADP supported. The configuration Flags are included inside the std. defined UA_UadpDataSetWriterMessageDataType
  714. UA_UadpDataSetWriterMessageDataType *dataSetWriterMessageDataType = NULL;
  715. if((dataSetWriter->config.messageSettings.encoding == UA_EXTENSIONOBJECT_DECODED ||
  716. dataSetWriter->config.messageSettings.encoding == UA_EXTENSIONOBJECT_DECODED_NODELETE) &&
  717. (dataSetWriter->config.messageSettings.content.decoded.type == &UA_TYPES[UA_TYPES_UADPDATASETWRITERMESSAGEDATATYPE])) {
  718. dataSetWriterMessageDataType = (UA_UadpDataSetWriterMessageDataType *) dataSetWriter->config.messageSettings.content.decoded.data;
  719. } else {
  720. //create default flag configuration if no UadpDataSetWriterMessageDataType was passed in
  721. UA_UadpDataSetWriterMessageDataType defaultUadpConfiguration;
  722. memset(&defaultUadpConfiguration, 0, sizeof(UA_UadpDataSetWriterMessageDataType));
  723. defaultUadpConfiguration.dataSetMessageContentMask = (UA_UadpDataSetMessageContentMask) ((unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_TIMESTAMP |
  724. (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_MAJORVERSION |
  725. (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_MINORVERSION);
  726. dataSetWriterMessageDataType = &defaultUadpConfiguration;
  727. }
  728. if(dataSetWriterMessageDataType->networkMessageNumber != 0 || dataSetWriterMessageDataType->dataSetOffset != 0 ||
  729. dataSetWriterMessageDataType->configuredSize !=0 ){
  730. UA_LOG_WARNING(server->config.logger, UA_LOGCATEGORY_SERVER, "Static DSM configuration not supported. Using defaults");
  731. dataSetWriterMessageDataType->networkMessageNumber = 0;
  732. dataSetWriterMessageDataType->dataSetOffset = 0;
  733. dataSetWriterMessageDataType->configuredSize = 0;
  734. }
  735. //The encoding depends on the flags inside the writer config.
  736. if(dataSetWriter->config.dataSetFieldContentMask & (unsigned int) UA_DATASETFIELDCONTENTMASK_RAWDATAENCODING) {
  737. dataSetMessage->header.fieldEncoding = UA_FIELDENCODING_RAWDATA;
  738. } else if (dataSetWriter->config.dataSetFieldContentMask &
  739. ((unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCETIMESTAMP |
  740. (unsigned int) UA_DATASETFIELDCONTENTMASK_SERVERPICOSECONDS |
  741. (unsigned int) UA_DATASETFIELDCONTENTMASK_SOURCEPICOSECONDS |
  742. (unsigned int) UA_DATASETFIELDCONTENTMASK_STATUSCODE)) {
  743. dataSetMessage->header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  744. } else {
  745. dataSetMessage->header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  746. }
  747. //Std: 'The DataSetMessageContentMask defines the flags for the content of the DataSetMessage header.'
  748. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_MAJORVERSION){
  749. dataSetMessage->header.configVersionMajorVersionEnabled = UA_TRUE;
  750. dataSetMessage->header.configVersionMajorVersion = currentDataSet->dataSetMetaData.configurationVersion.majorVersion;
  751. }
  752. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_MINORVERSION){
  753. dataSetMessage->header.configVersionMinorVersionEnabled = UA_TRUE;
  754. dataSetMessage->header.configVersionMinorVersion = currentDataSet->dataSetMetaData.configurationVersion.minorVersion;
  755. }
  756. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_SEQUENCENUMBER) {
  757. dataSetMessage->header.dataSetMessageSequenceNrEnabled = UA_TRUE;
  758. dataSetMessage->header.dataSetMessageSequenceNr = dataSetWriter->actualDataSetMessageSequenceCount;
  759. }
  760. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_TIMESTAMP) {
  761. dataSetMessage->header.timestampEnabled = UA_TRUE;
  762. dataSetMessage->header.timestamp = UA_DateTime_now();
  763. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_PICOSECONDS) {
  764. dataSetMessage->header.picoSecondsIncluded = UA_FALSE;
  765. UA_LOG_WARNING(server->config.logger, UA_LOGCATEGORY_SERVER, "DSM picosecond field is currently not supported. Using defaults");
  766. }
  767. }
  768. if(dataSetWriterMessageDataType->dataSetMessageContentMask & (unsigned int) UA_UADPDATASETMESSAGECONTENTMASK_STATUS){
  769. dataSetMessage->header.statusEnabled = UA_FALSE;
  770. UA_LOG_WARNING(server->config.logger, UA_LOGCATEGORY_SERVER, "DSM status field is currently not supported. Using defaults");
  771. }
  772. if(dataSetWriter->actualDataSetMessageSequenceCount < UA_UINT16_MAX){
  773. dataSetWriter->actualDataSetMessageSequenceCount++;
  774. } else {
  775. dataSetWriter->actualDataSetMessageSequenceCount = 0;
  776. }
  777. //check if the PublishedDataSet version has changed -> if yes flush the lastValue store and send a KeyFrame.
  778. if(dataSetWriter->connectedDataSetVersion.majorVersion != currentDataSet->dataSetMetaData.configurationVersion.majorVersion ||
  779. dataSetWriter->connectedDataSetVersion.minorVersion != currentDataSet->dataSetMetaData.configurationVersion.minorVersion) {
  780. //realloc pds dependent memory
  781. dataSetWriter->lastSamplesCount = currentDataSet->fieldSize;
  782. dataSetWriter->lastSamples = (UA_DataSetWriterSample * ) UA_realloc(dataSetWriter->lastSamples,
  783. sizeof(UA_DataSetWriterSample) * dataSetWriter->lastSamplesCount);
  784. if(!dataSetWriter->lastSamples)
  785. return UA_STATUSCODE_BADOUTOFMEMORY;
  786. for (size_t i = 0; i < dataSetWriter->lastSamplesCount; i++) {
  787. dataSetWriter->lastSamples[i].value = (UA_DataValue *) UA_calloc(1, sizeof(UA_DataValue));
  788. if(!dataSetWriter->lastSamples[i].value)
  789. return UA_STATUSCODE_BADOUTOFMEMORY;
  790. }
  791. dataSetWriter->connectedDataSetVersion = currentDataSet->dataSetMetaData.configurationVersion;
  792. UA_PubSubDataSetWriter_generateKeyFrameMessage(server, dataSetMessage, dataSetWriter);
  793. dataSetWriter->deltaFrameCounter = 0;
  794. } else if (currentDataSet->fieldSize == 1 || dataSetWriter->deltaFrameCounter == 0 || dataSetWriter->deltaFrameCounter > dataSetWriter->config.keyFrameCount){
  795. //@info the standard defines: if a PDS contains only one fields no delta messages should be generated
  796. //because they need more memory than a keyframe with 1 field.
  797. UA_PubSubDataSetWriter_generateKeyFrameMessage(server, dataSetMessage, dataSetWriter);
  798. dataSetWriter->deltaFrameCounter = 1;
  799. } else {
  800. UA_PubSubDataSetWriter_generateDeltaFrameMessage(server, dataSetMessage, dataSetWriter);
  801. dataSetWriter->deltaFrameCounter++;
  802. }
  803. return UA_STATUSCODE_GOOD;
  804. }
  805. /*
  806. * This callback triggers the collection and publish of NetworkMessages and the contained DataSetMessages.
  807. */
  808. void
  809. UA_WriterGroup_publishCallback(UA_Server *server, UA_WriterGroup *writerGroup) {
  810. if(!writerGroup){
  811. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Publish failed. WriterGroup not found");
  812. return;
  813. }
  814. if(writerGroup->writersCount <= 0)
  815. return;
  816. if(writerGroup->config.encodingMimeType != UA_PUBSUB_ENCODING_UADP) {
  817. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Unknown encoding type.");
  818. return;
  819. }
  820. //prevent error if the maxEncapsulatedDataSetMessageCount is set to 0->1
  821. writerGroup->config.maxEncapsulatedDataSetMessageCount = (UA_UInt16) (writerGroup->config.maxEncapsulatedDataSetMessageCount == 0 ||
  822. writerGroup->config.maxEncapsulatedDataSetMessageCount > UA_BYTE_MAX
  823. ? 1 : writerGroup->config.maxEncapsulatedDataSetMessageCount);
  824. UA_DataSetMessage *dsmStore = (UA_DataSetMessage *) UA_calloc(writerGroup->writersCount, sizeof(UA_DataSetMessage));
  825. if(!dsmStore) {
  826. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "DataSetMessage allocation failed");
  827. return;
  828. }
  829. //The binary DataSetMessage sizes are part of the payload. Memory is allocated on the stack.
  830. UA_STACKARRAY(UA_UInt16, dsmSizes, writerGroup->writersCount);
  831. memset(dsmSizes, 0, writerGroup->writersCount * sizeof(UA_UInt16));
  832. /*
  833. * Calculate the number of needed NetworkMessages. The previous allocated DataSetMessage array is
  834. * filled from left for combined DSM messages and from the right for single DSM.
  835. * Allocated DSM Array
  836. * +----------------------------+
  837. * |DSM1||DSM2||DSM3||DSM4||DSM5|
  838. * +--+----+-----+-----+-----+--+
  839. * | | | | |
  840. * | | | | |
  841. * +--v----v-----v-----v--+--v--+
  842. * | NM1 || NM2 | NM3 |
  843. * +----------------------+-----+
  844. * NetworkMessages
  845. */
  846. UA_UInt16 combinedNetworkMessageCount = 0, singleNetworkMessagesCount = 0;
  847. UA_DataSetWriter *tmpDataSetWriter;
  848. LIST_FOREACH(tmpDataSetWriter, &writerGroup->writers, listEntry){
  849. //if promoted fields are contained in the PublishedDataSet, then this DSM must encapsulated in one NM
  850. UA_PublishedDataSet *tmpPublishedDataSet = UA_PublishedDataSet_findPDSbyId(server, tmpDataSetWriter->connectedDataSet);
  851. if(!tmpPublishedDataSet) {
  852. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Publish failed. PublishedDataSet not found");
  853. return;
  854. }
  855. if(tmpPublishedDataSet->promotedFieldsCount > 0) {
  856. if(UA_DataSetWriter_generateDataSetMessage(server, &dsmStore[(writerGroup->writersCount - 1) - singleNetworkMessagesCount],
  857. tmpDataSetWriter) != UA_STATUSCODE_GOOD){
  858. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Publish failed. DataSetMessage creation failed");
  859. return;
  860. };
  861. dsmSizes[(writerGroup->writersCount-1) - singleNetworkMessagesCount] = (UA_UInt16) UA_DataSetMessage_calcSizeBinary(&dsmStore[(writerGroup->writersCount-1)
  862. - singleNetworkMessagesCount]);
  863. singleNetworkMessagesCount++;
  864. } else {
  865. if(UA_DataSetWriter_generateDataSetMessage(server, &dsmStore[combinedNetworkMessageCount], tmpDataSetWriter) != UA_STATUSCODE_GOOD){
  866. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Publish failed. DataSetMessage creation failed");
  867. return;
  868. };
  869. dsmSizes[combinedNetworkMessageCount] = (UA_UInt16) UA_DataSetMessage_calcSizeBinary(&dsmStore[combinedNetworkMessageCount]);
  870. combinedNetworkMessageCount++;
  871. }
  872. }
  873. UA_UInt32 networkMessageCount = singleNetworkMessagesCount;
  874. if(combinedNetworkMessageCount != 0){
  875. networkMessageCount += combinedNetworkMessageCount / writerGroup->config.maxEncapsulatedDataSetMessageCount +
  876. (combinedNetworkMessageCount % writerGroup->config.maxEncapsulatedDataSetMessageCount) == 0 ? 0 : 1;
  877. }
  878. //Alloc memory for the NetworkMessages on the stack
  879. UA_STACKARRAY(UA_NetworkMessage, nmStore, networkMessageCount);
  880. memset(nmStore, 0, networkMessageCount * sizeof(UA_NetworkMessage));
  881. UA_UInt32 currentDSMPosition = 0;
  882. for(UA_UInt32 i = 0; i < networkMessageCount; i++) {
  883. nmStore[i].version = 1;
  884. nmStore[i].networkMessageType = UA_NETWORKMESSAGE_DATASET;
  885. //create combined NetworkMessages
  886. if(i < (networkMessageCount-singleNetworkMessagesCount)){
  887. if(combinedNetworkMessageCount - (i * writerGroup->config.maxEncapsulatedDataSetMessageCount) > 0){
  888. currentDSMPosition = i * writerGroup->config.maxEncapsulatedDataSetMessageCount;
  889. nmStore[i].payloadHeader.dataSetPayloadHeader.count = (UA_Byte) writerGroup->config.maxEncapsulatedDataSetMessageCount;
  890. nmStore[i].payload.dataSetPayload.dataSetMessages = &dsmStore[currentDSMPosition];
  891. nmStore->payload.dataSetPayload.sizes = &dsmSizes[currentDSMPosition];
  892. } else {
  893. currentDSMPosition = i * writerGroup->config.maxEncapsulatedDataSetMessageCount;
  894. nmStore[i].payloadHeader.dataSetPayloadHeader.count = (UA_Byte) (currentDSMPosition - ((i - 1) * writerGroup->config.maxEncapsulatedDataSetMessageCount)); //attention cast from uint32 to byte
  895. nmStore[i].payload.dataSetPayload.dataSetMessages = &dsmStore[currentDSMPosition];
  896. nmStore->payload.dataSetPayload.sizes = &dsmSizes[currentDSMPosition];
  897. }
  898. } else {///create single NetworkMessages (1 DSM per NM)
  899. nmStore[i].payloadHeader.dataSetPayloadHeader.count = 1;
  900. currentDSMPosition = (UA_UInt32) combinedNetworkMessageCount + (i - combinedNetworkMessageCount/writerGroup->config.maxEncapsulatedDataSetMessageCount
  901. + (combinedNetworkMessageCount % writerGroup->config.maxEncapsulatedDataSetMessageCount) == 0 ? 0 : 1);
  902. nmStore[i].payload.dataSetPayload.dataSetMessages = &dsmStore[currentDSMPosition];
  903. nmStore->payload.dataSetPayload.sizes = &dsmSizes[currentDSMPosition];
  904. }
  905. UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, writerGroup->linkedConnection);
  906. if(!connection){
  907. UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER, "Publish failed. PubSubConnection invalid.");
  908. return;
  909. }
  910. //send the prepared messages
  911. UA_ByteString buf;
  912. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&nmStore[i]);
  913. if(UA_ByteString_allocBuffer(&buf, msgSize) == UA_STATUSCODE_GOOD) {
  914. UA_Byte *bufPos = buf.data;
  915. memset(bufPos, 0, msgSize);
  916. const UA_Byte *bufEnd = &(buf.data[buf.length]);
  917. if(UA_NetworkMessage_encodeBinary(&nmStore[i], &bufPos, bufEnd) != UA_STATUSCODE_GOOD){
  918. UA_ByteString_deleteMembers(&buf);
  919. return;
  920. };
  921. connection->channel->send(connection->channel, NULL, &buf);
  922. }
  923. UA_ByteString_deleteMembers(&buf);
  924. UA_NetworkMessage_deleteMembers(&nmStore[i]);
  925. }
  926. }
  927. /*
  928. * Add new publishCallback. The first execution is triggered directly after creation.
  929. * @Warning - The duration (double) is currently casted to int. -> intervals smaller 1ms are not possible.
  930. */
  931. UA_StatusCode
  932. UA_WriterGroup_addPublishCallback(UA_Server *server, UA_WriterGroup *writerGroup) {
  933. UA_StatusCode retval =
  934. UA_PubSubManager_addRepeatedCallback(server, (UA_ServerCallback) UA_WriterGroup_publishCallback,
  935. writerGroup, (UA_UInt32) writerGroup->config.publishingInterval,
  936. &writerGroup->publishCallbackId);
  937. if(retval == UA_STATUSCODE_GOOD)
  938. writerGroup->publishCallbackIsRegistered = true;
  939. //run once after creation
  940. UA_WriterGroup_publishCallback(server, writerGroup);
  941. return retval;
  942. }