server_pubsub_publisher_iop.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <open62541/plugin/log_stdout.h>
  4. #include <open62541/plugin/pubsub_udp.h>
  5. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  6. #include <open62541/plugin/pubsub_ethernet.h>
  7. #endif
  8. #include <open62541/server.h>
  9. #include <open62541/server_config_default.h>
  10. #include <signal.h>
  11. #include <stdlib.h>
  12. #define Publisher_ID 60
  13. static void addPublisher1(UA_Server *server, UA_NodeId publishedDataSetId);
  14. static void addPublisher2(UA_Server *server, UA_NodeId publishedDataSetId);
  15. static UA_NodeId ds1BoolToggleId;
  16. static int ds1BoolToggleCount = 0;
  17. static UA_Boolean ds1BoolToggleVal = false;
  18. static UA_NodeId ds1Int32Id;
  19. static UA_Int32 ds1Int32Val = 0;
  20. static UA_NodeId ds1Int32FastId;
  21. static UA_Int32 ds1Int32FastVal = 0;
  22. static UA_NodeId ds1DateTimeId;
  23. static UA_NodeId ds2BoolToggleId;
  24. static UA_Boolean ds2BoolToggleVal = false;
  25. static UA_NodeId ds2ByteId;
  26. static UA_Byte ds2ByteVal = 0;
  27. static UA_NodeId ds2Int16Id;
  28. static UA_Int16 ds2Int16Val = 0;
  29. static UA_NodeId ds2Int32Id;
  30. static UA_Int32 ds2Int32Val = 0;
  31. static UA_NodeId ds2Int64Id;
  32. static UA_Int64 ds2Int64Val = 0;
  33. static UA_NodeId ds2SByteId;
  34. static UA_SByte ds2SByteVal = 0;
  35. static UA_NodeId ds2UInt16Id;
  36. static UA_UInt16 ds2UInt16Val = 0;
  37. static UA_NodeId ds2UInt32Id;
  38. static UA_UInt32 ds2UInt32Val = 0;
  39. static UA_NodeId ds2UInt64Id;
  40. static UA_UInt64 ds2UInt64Val = 0;
  41. static UA_NodeId ds2FloatId;
  42. static UA_Float ds2FloatVal = 0;
  43. static UA_NodeId ds2DoubleId;
  44. static UA_Double ds2DoubleVal = 0;
  45. static UA_String *ds2StringArray = NULL;
  46. static size_t ds2StringArrayLen = 0;
  47. static size_t ds2StringIndex = 0;
  48. static UA_NodeId ds2StringId;
  49. static UA_NodeId ds2ByteStringId;
  50. static UA_NodeId ds2GuidId;
  51. static UA_NodeId ds2DateTimeId;
  52. static UA_NodeId ds2UInt32ArrId;
  53. static UA_UInt32 ds2UInt32ArrValue[10] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };
  54. UA_NodeId connectionIdent;
  55. void
  56. timerCallback(UA_Server *server, void *data);
  57. static void
  58. addPubSubConnection(UA_Server *server, UA_String *transportProfile,
  59. UA_NetworkAddressUrlDataType *networkAddressUrl) {
  60. /* Details about the connection configuration and handling are located
  61. * in the pubsub connection tutorial */
  62. UA_PubSubConnectionConfig connectionConfig;
  63. memset(&connectionConfig, 0, sizeof(connectionConfig));
  64. connectionConfig.name = UA_STRING("UADP Connection 1");
  65. connectionConfig.transportProfileUri = *transportProfile;
  66. connectionConfig.enabled = UA_TRUE;
  67. UA_Variant_setScalar(&connectionConfig.address, networkAddressUrl,
  68. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  69. connectionConfig.publisherId.numeric = Publisher_ID;
  70. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  71. }
  72. static void addPublisher1(UA_Server *server, UA_NodeId publishedDataSetId) {
  73. UA_NodeId folderId;
  74. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  75. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Publisher 1");
  76. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  77. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  78. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  79. UA_QUALIFIEDNAME(1, "Publisher 1"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  80. oAttr, NULL, &folderId);
  81. UA_NodeId_init(&ds1BoolToggleId);
  82. UA_VariableAttributes boolToggleAttr = UA_VariableAttributes_default;
  83. boolToggleAttr.valueRank = -1;
  84. UA_NodeId_copy(&UA_TYPES[UA_TYPES_BOOLEAN].typeId, &boolToggleAttr.dataType);
  85. UA_Variant_setScalar(&boolToggleAttr.value, &ds1BoolToggleVal, &UA_TYPES[UA_TYPES_BOOLEAN]);
  86. boolToggleAttr.displayName = UA_LOCALIZEDTEXT("en-US", "BoolToggle");
  87. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher1.BoolToggle"), folderId,
  88. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  89. UA_QUALIFIEDNAME(1, "BoolToggle"),
  90. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), boolToggleAttr, NULL, &ds1BoolToggleId);
  91. UA_NodeId_init(&ds1Int32Id);
  92. UA_VariableAttributes int32Attr = UA_VariableAttributes_default;
  93. int32Attr.valueRank = -1;
  94. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT32].typeId, &int32Attr.dataType);
  95. int32Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  96. UA_Variant_setScalar(&int32Attr.value, &ds1Int32Val, &UA_TYPES[UA_TYPES_INT32]);
  97. int32Attr.displayName = UA_LOCALIZEDTEXT("en-US", "Int32");
  98. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher1.Int32"), folderId,
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  100. UA_QUALIFIEDNAME(1, "Int32"),
  101. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), int32Attr, NULL, &ds1Int32Id);
  102. UA_NodeId_init(&ds1Int32FastId);
  103. UA_VariableAttributes int32FastAttr = UA_VariableAttributes_default;
  104. int32FastAttr.valueRank = -1;
  105. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT32].typeId, &int32FastAttr.dataType);
  106. int32FastAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  107. UA_Variant_setScalar(&int32FastAttr.value, &ds1Int32FastVal, &UA_TYPES[UA_TYPES_INT32]);
  108. int32FastAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Int32Fast");
  109. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher1.Int32Fast"), folderId,
  110. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  111. UA_QUALIFIEDNAME(1, "Int32Fast"),
  112. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), int32FastAttr, NULL, &ds1Int32FastId);
  113. UA_NodeId_init(&ds1DateTimeId);
  114. UA_VariableAttributes dateTimeAttr = UA_VariableAttributes_default;
  115. dateTimeAttr.valueRank = -1;
  116. UA_NodeId_copy(&UA_TYPES[UA_TYPES_DATETIME].typeId, &dateTimeAttr.dataType);
  117. dateTimeAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  118. UA_DateTime dateTimeVal = UA_DateTime_now();
  119. UA_Variant_setScalar(&dateTimeAttr.value, &dateTimeVal, &UA_TYPES[UA_TYPES_DATETIME]);
  120. dateTimeAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DateTime");
  121. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher1.DateTime"), folderId,
  122. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  123. UA_QUALIFIEDNAME(1, "DateTime"),
  124. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), dateTimeAttr, NULL, &ds1DateTimeId);
  125. if (!UA_NodeId_equal(&publishedDataSetId, &UA_NODEID_NULL))
  126. {
  127. // Create and add fields to the PublishedDataSet
  128. UA_DataSetFieldConfig boolToggleConfig;
  129. memset(&boolToggleConfig, 0, sizeof(UA_DataSetFieldConfig));
  130. boolToggleConfig.field.variable.fieldNameAlias = UA_STRING("BoolToggle");
  131. boolToggleConfig.field.variable.promotedField = false;
  132. boolToggleConfig.field.variable.publishParameters.publishedVariable = ds1BoolToggleId;
  133. boolToggleConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  134. UA_DataSetFieldConfig int32Config;
  135. memset(&int32Config, 0, sizeof(UA_DataSetFieldConfig));
  136. int32Config.field.variable.fieldNameAlias = UA_STRING("Int32");
  137. int32Config.field.variable.promotedField = false;
  138. int32Config.field.variable.publishParameters.publishedVariable = ds1Int32Id;
  139. int32Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  140. UA_DataSetFieldConfig int32FastConfig;
  141. memset(&int32FastConfig, 0, sizeof(UA_DataSetFieldConfig));
  142. int32FastConfig.field.variable.fieldNameAlias = UA_STRING("Int32Fast");
  143. int32FastConfig.field.variable.promotedField = false;
  144. int32FastConfig.field.variable.publishParameters.publishedVariable = ds1Int32FastId;
  145. int32FastConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  146. UA_DataSetFieldConfig dateTimeConfig;
  147. memset(&dateTimeConfig, 0, sizeof(UA_DataSetFieldConfig));
  148. dateTimeConfig.field.variable.fieldNameAlias = UA_STRING("DateTime");
  149. dateTimeConfig.field.variable.promotedField = false;
  150. dateTimeConfig.field.variable.publishParameters.publishedVariable = ds1DateTimeId;
  151. dateTimeConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  152. UA_NodeId f1, f2, f3, f4;
  153. // add fields in reverse order, because all fields are added to the beginning of the list
  154. UA_Server_addDataSetField(server, publishedDataSetId, &dateTimeConfig, &f4);
  155. UA_Server_addDataSetField(server, publishedDataSetId, &int32FastConfig, &f3);
  156. UA_Server_addDataSetField(server, publishedDataSetId, &int32Config, &f2);
  157. UA_Server_addDataSetField(server, publishedDataSetId, &boolToggleConfig, &f1);
  158. }
  159. }
  160. static void addPublisher2(UA_Server *server, UA_NodeId publishedDataSetId) {
  161. UA_NodeId folderId;
  162. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  163. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Publisher 2");
  164. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  165. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  166. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  167. UA_QUALIFIEDNAME(1, "Publisher 2"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  168. oAttr, NULL, &folderId);
  169. UA_NodeId_init(&ds2BoolToggleId);
  170. UA_VariableAttributes boolToggleAttr = UA_VariableAttributes_default;
  171. boolToggleAttr.valueRank = -1;
  172. UA_NodeId_copy(&UA_TYPES[UA_TYPES_BOOLEAN].typeId, &boolToggleAttr.dataType);
  173. UA_Variant_setScalar(&boolToggleAttr.value, &ds2BoolToggleVal, &UA_TYPES[UA_TYPES_BOOLEAN]);
  174. boolToggleAttr.displayName = UA_LOCALIZEDTEXT("en-US", "BoolToggle");
  175. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.BoolToggle"), folderId,
  176. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  177. UA_QUALIFIEDNAME(1, "BoolToggle"),
  178. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), boolToggleAttr, NULL, &ds2BoolToggleId);
  179. UA_NodeId_init(&ds2ByteId);
  180. UA_VariableAttributes byteAttr = UA_VariableAttributes_default;
  181. byteAttr.valueRank = -1;
  182. UA_NodeId_copy(&UA_TYPES[UA_TYPES_BYTE].typeId, &byteAttr.dataType);
  183. byteAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  184. UA_Variant_setScalar(&byteAttr.value, &ds2ByteVal, &UA_TYPES[UA_TYPES_BYTE]);
  185. byteAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Byte");
  186. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Byte"), folderId,
  187. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  188. UA_QUALIFIEDNAME(1, "Byte"),
  189. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), byteAttr, NULL, &ds2ByteId);
  190. UA_NodeId_init(&ds2Int16Id);
  191. UA_VariableAttributes int16Attr = UA_VariableAttributes_default;
  192. int16Attr.valueRank = -1;
  193. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT16].typeId, &int16Attr.dataType);
  194. int16Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  195. UA_Variant_setScalar(&int16Attr.value, &ds2Int16Val, &UA_TYPES[UA_TYPES_INT16]);
  196. int16Attr.displayName = UA_LOCALIZEDTEXT("en-US", "Int16");
  197. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Int16"), folderId,
  198. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  199. UA_QUALIFIEDNAME(1, "Int16"),
  200. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), int16Attr, NULL, &ds2Int16Id);
  201. UA_NodeId_init(&ds2Int32Id);
  202. UA_VariableAttributes int32Attr = UA_VariableAttributes_default;
  203. int32Attr.valueRank = -1;
  204. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT32].typeId, &int32Attr.dataType);
  205. int32Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  206. UA_Variant_setScalar(&int32Attr.value, &ds2Int32Val, &UA_TYPES[UA_TYPES_INT32]);
  207. int32Attr.displayName = UA_LOCALIZEDTEXT("en-US", "Int32");
  208. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Int32"), folderId,
  209. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  210. UA_QUALIFIEDNAME(1, "Int32"),
  211. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), int32Attr, NULL, &ds2Int32Id);
  212. UA_NodeId_init(&ds2Int64Id);
  213. UA_VariableAttributes int64Attr = UA_VariableAttributes_default;
  214. int64Attr.valueRank = -1;
  215. UA_NodeId_copy(&UA_TYPES[UA_TYPES_INT64].typeId, &int64Attr.dataType);
  216. int64Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  217. UA_Variant_setScalar(&int64Attr.value, &ds2Int64Val, &UA_TYPES[UA_TYPES_INT64]);
  218. int64Attr.displayName = UA_LOCALIZEDTEXT("en-US", "Int64");
  219. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Int64"), folderId,
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  221. UA_QUALIFIEDNAME(1, "Int64"),
  222. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), int64Attr, NULL, &ds2Int64Id);
  223. UA_NodeId_init(&ds2SByteId);
  224. UA_VariableAttributes sbyteAttr = UA_VariableAttributes_default;
  225. sbyteAttr.valueRank = -1;
  226. UA_NodeId_copy(&UA_TYPES[UA_TYPES_SBYTE].typeId, &sbyteAttr.dataType);
  227. sbyteAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  228. UA_Variant_setScalar(&sbyteAttr.value, &ds2SByteVal, &UA_TYPES[UA_TYPES_SBYTE]);
  229. sbyteAttr.displayName = UA_LOCALIZEDTEXT("en-US", "SByte");
  230. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.SByte"), folderId,
  231. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  232. UA_QUALIFIEDNAME(1, "SByte"),
  233. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), sbyteAttr, NULL, &ds2SByteId);
  234. UA_NodeId_init(&ds2UInt16Id);
  235. UA_VariableAttributes uint16Attr = UA_VariableAttributes_default;
  236. uint16Attr.valueRank = -1;
  237. UA_NodeId_copy(&UA_TYPES[UA_TYPES_UINT16].typeId, &uint16Attr.dataType);
  238. uint16Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  239. UA_Variant_setScalar(&uint16Attr.value, &ds2UInt16Val, &UA_TYPES[UA_TYPES_UINT16]);
  240. uint16Attr.displayName = UA_LOCALIZEDTEXT("en-US", "UInt16");
  241. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.UInt16"), folderId,
  242. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  243. UA_QUALIFIEDNAME(1, "UInt16"),
  244. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), uint16Attr, NULL, &ds2UInt16Id);
  245. UA_NodeId_init(&ds2UInt32Id);
  246. UA_VariableAttributes uint32Attr = UA_VariableAttributes_default;
  247. uint32Attr.valueRank = -1;
  248. UA_NodeId_copy(&UA_TYPES[UA_TYPES_UINT32].typeId, &uint32Attr.dataType);
  249. uint32Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  250. UA_Variant_setScalar(&uint32Attr.value, &ds2UInt32Val, &UA_TYPES[UA_TYPES_UINT32]);
  251. uint32Attr.displayName = UA_LOCALIZEDTEXT("en-US", "UInt32");
  252. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.UInt32"), folderId,
  253. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  254. UA_QUALIFIEDNAME(1, "UInt32"),
  255. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), uint32Attr, NULL, &ds2UInt32Id);
  256. UA_NodeId_init(&ds2UInt64Id);
  257. UA_VariableAttributes uint64Attr = UA_VariableAttributes_default;
  258. uint64Attr.valueRank = -1;
  259. UA_NodeId_copy(&UA_TYPES[UA_TYPES_UINT64].typeId, &uint64Attr.dataType);
  260. uint64Attr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  261. UA_Variant_setScalar(&uint64Attr.value, &ds2UInt64Val, &UA_TYPES[UA_TYPES_UINT64]);
  262. uint64Attr.displayName = UA_LOCALIZEDTEXT("en-US", "UInt64");
  263. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.UInt64"), folderId,
  264. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  265. UA_QUALIFIEDNAME(1, "UInt64"),
  266. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), uint64Attr, NULL, &ds2UInt64Id);
  267. UA_NodeId_init(&ds2FloatId);
  268. UA_VariableAttributes floatAttr = UA_VariableAttributes_default;
  269. floatAttr.valueRank = -1;
  270. UA_NodeId_copy(&UA_TYPES[UA_TYPES_FLOAT].typeId, &floatAttr.dataType);
  271. floatAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  272. UA_Variant_setScalar(&floatAttr.value, &ds2FloatVal, &UA_TYPES[UA_TYPES_FLOAT]);
  273. floatAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Float");
  274. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Float"), folderId,
  275. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  276. UA_QUALIFIEDNAME(1, "Float"),
  277. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), floatAttr, NULL, &ds2FloatId);
  278. UA_NodeId_init(&ds2DoubleId);
  279. UA_VariableAttributes doubleAttr = UA_VariableAttributes_default;
  280. doubleAttr.valueRank = -1;
  281. UA_NodeId_copy(&UA_TYPES[UA_TYPES_DOUBLE].typeId, &doubleAttr.dataType);
  282. doubleAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  283. UA_Variant_setScalar(&doubleAttr.value, &ds2DoubleVal, &UA_TYPES[UA_TYPES_DOUBLE]);
  284. doubleAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Double");
  285. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Double"), folderId,
  286. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  287. UA_QUALIFIEDNAME(1, "Double"),
  288. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), doubleAttr, NULL, &ds2DoubleId);
  289. ds2StringArrayLen = 26;
  290. ds2StringArray = (UA_String*)UA_Array_new(ds2StringArrayLen, &UA_TYPES[UA_TYPES_STRING]);
  291. ds2StringArray[0] = UA_STRING_ALLOC("Alpha");
  292. ds2StringArray[1] = UA_STRING_ALLOC("Bravo");
  293. ds2StringArray[2] = UA_STRING_ALLOC("Charlie");
  294. ds2StringArray[3] = UA_STRING_ALLOC("Delta");
  295. ds2StringArray[4] = UA_STRING_ALLOC("Echo");
  296. ds2StringArray[5] = UA_STRING_ALLOC("Foxtrot");
  297. ds2StringArray[6] = UA_STRING_ALLOC("Golf");
  298. ds2StringArray[7] = UA_STRING_ALLOC("Hotel");
  299. ds2StringArray[8] = UA_STRING_ALLOC("India");
  300. ds2StringArray[9] = UA_STRING_ALLOC("Juliet");
  301. ds2StringArray[10] = UA_STRING_ALLOC("Kilo");
  302. ds2StringArray[11] = UA_STRING_ALLOC("Lima");
  303. ds2StringArray[12] = UA_STRING_ALLOC("Mike");
  304. ds2StringArray[13] = UA_STRING_ALLOC("November");
  305. ds2StringArray[14] = UA_STRING_ALLOC("Oscar");
  306. ds2StringArray[15] = UA_STRING_ALLOC("Papa");
  307. ds2StringArray[16] = UA_STRING_ALLOC("Quebec");
  308. ds2StringArray[17] = UA_STRING_ALLOC("Romeo");
  309. ds2StringArray[18] = UA_STRING_ALLOC("Sierra");
  310. ds2StringArray[19] = UA_STRING_ALLOC("Tango");
  311. ds2StringArray[20] = UA_STRING_ALLOC("Uniform");
  312. ds2StringArray[21] = UA_STRING_ALLOC("Victor");
  313. ds2StringArray[22] = UA_STRING_ALLOC("Whiskey");
  314. ds2StringArray[23] = UA_STRING_ALLOC("X-ray");
  315. ds2StringArray[24] = UA_STRING_ALLOC("Yankee");
  316. ds2StringArray[25] = UA_STRING_ALLOC("Zulu");
  317. UA_String stringVal;
  318. UA_String_init(&stringVal);
  319. UA_NodeId_init(&ds2StringId);
  320. UA_VariableAttributes stringAttr = UA_VariableAttributes_default;
  321. stringAttr.valueRank = -1;
  322. UA_NodeId_copy(&UA_TYPES[UA_TYPES_STRING].typeId, &stringAttr.dataType);
  323. stringAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  324. UA_Variant_setScalar(&stringAttr.value, &stringVal, &UA_TYPES[UA_TYPES_STRING]);
  325. stringAttr.displayName = UA_LOCALIZEDTEXT("en-US", "String");
  326. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.String"), folderId,
  327. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  328. UA_QUALIFIEDNAME(1, "String"),
  329. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), stringAttr, NULL, &ds2StringId);
  330. UA_Byte data[] = { 0x00 };
  331. UA_ByteString byteStringVal = { 1, data };
  332. UA_NodeId_init(&ds2ByteStringId);
  333. UA_VariableAttributes byteStringAttr = UA_VariableAttributes_default;
  334. byteStringAttr.valueRank = -1;
  335. UA_NodeId_copy(&UA_TYPES[UA_TYPES_BYTESTRING].typeId, &byteStringAttr.dataType);
  336. byteStringAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  337. UA_Variant_setScalar(&byteStringAttr.value, &byteStringVal, &UA_TYPES[UA_TYPES_BYTESTRING]);
  338. byteStringAttr.displayName = UA_LOCALIZEDTEXT("en-US", "ByteString");
  339. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.ByteString"), folderId,
  340. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  341. UA_QUALIFIEDNAME(1, "ByteString"),
  342. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), byteStringAttr, NULL, &ds2ByteStringId);
  343. UA_Guid guidVal = UA_Guid_random();
  344. UA_NodeId_init(&ds2GuidId);
  345. UA_VariableAttributes guidAttr = UA_VariableAttributes_default;
  346. guidAttr.valueRank = -1;
  347. UA_NodeId_copy(&UA_TYPES[UA_TYPES_GUID].typeId, &guidAttr.dataType);
  348. guidAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  349. UA_Variant_setScalar(&guidAttr.value, &guidVal, &UA_TYPES[UA_TYPES_GUID]);
  350. guidAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Guid");
  351. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.Guid"), folderId,
  352. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  353. UA_QUALIFIEDNAME(1, "Guid"),
  354. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), guidAttr, NULL, &ds2GuidId);
  355. UA_NodeId_init(&ds2DateTimeId);
  356. UA_VariableAttributes dateTimeAttr = UA_VariableAttributes_default;
  357. dateTimeAttr.valueRank = -1;
  358. UA_NodeId_copy(&UA_TYPES[UA_TYPES_DATETIME].typeId, &dateTimeAttr.dataType);
  359. dateTimeAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  360. UA_DateTime dateTimeVal = UA_DateTime_now();
  361. UA_Variant_setScalar(&dateTimeAttr.value, &dateTimeVal, &UA_TYPES[UA_TYPES_DATETIME]);
  362. dateTimeAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DateTime");
  363. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.DateTime"), folderId,
  364. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  365. UA_QUALIFIEDNAME(1, "DateTime"),
  366. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), dateTimeAttr, NULL, &ds2DateTimeId);
  367. // UInt32Array
  368. UA_NodeId_init(&ds2UInt32ArrId);
  369. UA_VariableAttributes uint32ArrAttr = UA_VariableAttributes_default;
  370. uint32ArrAttr.valueRank = 1; // 1-dimensional array
  371. uint32ArrAttr.arrayDimensionsSize = 1;
  372. UA_UInt32 arrayDims[1] = { 10 };
  373. uint32ArrAttr.arrayDimensions = arrayDims;
  374. UA_NodeId_copy(&UA_TYPES[UA_TYPES_UINT32].typeId, &uint32ArrAttr.dataType);
  375. uint32ArrAttr.accessLevel = UA_ACCESSLEVELMASK_READ ^ UA_ACCESSLEVELMASK_WRITE;
  376. UA_Variant_setArray(&uint32ArrAttr.value, ds2UInt32ArrValue, 10, &UA_TYPES[UA_TYPES_UINT32]);
  377. uint32ArrAttr.displayName = UA_LOCALIZEDTEXT("en-US", "UInt32Array");
  378. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Publisher2.UInt32Array"), folderId,
  379. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  380. UA_QUALIFIEDNAME(1, "UInt32Array"),
  381. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), uint32ArrAttr, NULL, &ds2UInt32ArrId);
  382. if (!UA_NodeId_equal(&publishedDataSetId, &UA_NODEID_NULL))
  383. {
  384. // Create and add fields to the PublishedDataSet
  385. UA_DataSetFieldConfig boolToggleConfig;
  386. memset(&boolToggleConfig, 0, sizeof(UA_DataSetFieldConfig));
  387. boolToggleConfig.field.variable.fieldNameAlias = UA_STRING("BoolToggle");
  388. boolToggleConfig.field.variable.promotedField = false;
  389. boolToggleConfig.field.variable.publishParameters.publishedVariable = ds2BoolToggleId;
  390. boolToggleConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  391. UA_DataSetFieldConfig byteConfig;
  392. memset(&byteConfig, 0, sizeof(UA_DataSetFieldConfig));
  393. byteConfig.field.variable.fieldNameAlias = UA_STRING("Byte");
  394. byteConfig.field.variable.promotedField = false;
  395. byteConfig.field.variable.publishParameters.publishedVariable = ds2ByteId;
  396. byteConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  397. UA_DataSetFieldConfig int16Config;
  398. memset(&int16Config, 0, sizeof(UA_DataSetFieldConfig));
  399. int16Config.field.variable.fieldNameAlias = UA_STRING("Int16");
  400. int16Config.field.variable.promotedField = false;
  401. int16Config.field.variable.publishParameters.publishedVariable = ds2Int16Id;
  402. int16Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  403. UA_DataSetFieldConfig int32Config;
  404. memset(&int32Config, 0, sizeof(UA_DataSetFieldConfig));
  405. int32Config.field.variable.fieldNameAlias = UA_STRING("Int32");
  406. int32Config.field.variable.promotedField = false;
  407. int32Config.field.variable.publishParameters.publishedVariable = ds2Int32Id;
  408. int32Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  409. UA_DataSetFieldConfig int64Config;
  410. memset(&int64Config, 0, sizeof(UA_DataSetFieldConfig));
  411. int64Config.field.variable.fieldNameAlias = UA_STRING("Int64");
  412. int64Config.field.variable.promotedField = false;
  413. int64Config.field.variable.publishParameters.publishedVariable = ds2Int64Id;
  414. int64Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  415. UA_DataSetFieldConfig sbyteConfig;
  416. memset(&sbyteConfig, 0, sizeof(UA_DataSetFieldConfig));
  417. sbyteConfig.field.variable.fieldNameAlias = UA_STRING("SByte");
  418. sbyteConfig.field.variable.promotedField = false;
  419. sbyteConfig.field.variable.publishParameters.publishedVariable = ds2SByteId;
  420. sbyteConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  421. UA_DataSetFieldConfig uint16Config;
  422. memset(&uint16Config, 0, sizeof(UA_DataSetFieldConfig));
  423. uint16Config.field.variable.fieldNameAlias = UA_STRING("UInt16");
  424. uint16Config.field.variable.promotedField = false;
  425. uint16Config.field.variable.publishParameters.publishedVariable = ds2UInt16Id;
  426. uint16Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  427. UA_DataSetFieldConfig uint32Config;
  428. memset(&uint32Config, 0, sizeof(UA_DataSetFieldConfig));
  429. uint32Config.field.variable.fieldNameAlias = UA_STRING("UInt32");
  430. uint32Config.field.variable.promotedField = false;
  431. uint32Config.field.variable.publishParameters.publishedVariable = ds2UInt32Id;
  432. uint32Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  433. UA_DataSetFieldConfig uint64Config;
  434. memset(&uint64Config, 0, sizeof(UA_DataSetFieldConfig));
  435. uint64Config.field.variable.fieldNameAlias = UA_STRING("UInt64");
  436. uint64Config.field.variable.promotedField = false;
  437. uint64Config.field.variable.publishParameters.publishedVariable = ds2UInt64Id;
  438. uint64Config.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  439. UA_DataSetFieldConfig floatConfig;
  440. memset(&floatConfig, 0, sizeof(UA_DataSetFieldConfig));
  441. floatConfig.field.variable.fieldNameAlias = UA_STRING("Float");
  442. floatConfig.field.variable.promotedField = false;
  443. floatConfig.field.variable.publishParameters.publishedVariable = ds2FloatId;
  444. floatConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  445. UA_DataSetFieldConfig doubleConfig;
  446. memset(&doubleConfig, 0, sizeof(UA_DataSetFieldConfig));
  447. doubleConfig.field.variable.fieldNameAlias = UA_STRING("Double");
  448. doubleConfig.field.variable.promotedField = false;
  449. doubleConfig.field.variable.publishParameters.publishedVariable = ds2DoubleId;
  450. doubleConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  451. UA_DataSetFieldConfig stringConfig;
  452. memset(&stringConfig, 0, sizeof(UA_DataSetFieldConfig));
  453. stringConfig.field.variable.fieldNameAlias = UA_STRING("String");
  454. stringConfig.field.variable.promotedField = false;
  455. stringConfig.field.variable.publishParameters.publishedVariable = ds2StringId;
  456. stringConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  457. UA_DataSetFieldConfig byteStringConfig;
  458. memset(&byteStringConfig, 0, sizeof(UA_DataSetFieldConfig));
  459. byteStringConfig.field.variable.fieldNameAlias = UA_STRING("ByteString");
  460. byteStringConfig.field.variable.promotedField = false;
  461. byteStringConfig.field.variable.publishParameters.publishedVariable = ds2ByteStringId;
  462. byteStringConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  463. UA_DataSetFieldConfig guidConfig;
  464. memset(&guidConfig, 0, sizeof(UA_DataSetFieldConfig));
  465. guidConfig.field.variable.fieldNameAlias = UA_STRING("Guid");
  466. guidConfig.field.variable.promotedField = false;
  467. guidConfig.field.variable.publishParameters.publishedVariable = ds2GuidId;
  468. guidConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  469. UA_DataSetFieldConfig dateTimeConfig;
  470. memset(&dateTimeConfig, 0, sizeof(UA_DataSetFieldConfig));
  471. dateTimeConfig.field.variable.fieldNameAlias = UA_STRING("DateTime");
  472. dateTimeConfig.field.variable.promotedField = false;
  473. dateTimeConfig.field.variable.publishParameters.publishedVariable = ds2DateTimeId;
  474. dateTimeConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  475. UA_DataSetFieldConfig uint32ArrConfig;
  476. memset(&uint32ArrConfig, 0, sizeof(UA_DataSetFieldConfig));
  477. uint32ArrConfig.field.variable.fieldNameAlias = UA_STRING("UInt32Array");
  478. uint32ArrConfig.field.variable.promotedField = false;
  479. uint32ArrConfig.field.variable.publishParameters.publishedVariable = ds2UInt32ArrId;
  480. uint32ArrConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  481. UA_NodeId f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16;
  482. // add fields in reverse order, because all fields are added to the beginning of the list
  483. UA_Server_addDataSetField(server, publishedDataSetId, &uint32ArrConfig, &f16);
  484. UA_Server_addDataSetField(server, publishedDataSetId, &dateTimeConfig, &f15);
  485. UA_Server_addDataSetField(server, publishedDataSetId, &guidConfig, &f14);
  486. UA_Server_addDataSetField(server, publishedDataSetId, &byteStringConfig, &f13);
  487. UA_Server_addDataSetField(server, publishedDataSetId, &stringConfig, &f12);
  488. UA_Server_addDataSetField(server, publishedDataSetId, &doubleConfig, &f11);
  489. UA_Server_addDataSetField(server, publishedDataSetId, &floatConfig, &f10);
  490. UA_Server_addDataSetField(server, publishedDataSetId, &uint64Config, &f9);
  491. UA_Server_addDataSetField(server, publishedDataSetId, &uint32Config, &f8);
  492. UA_Server_addDataSetField(server, publishedDataSetId, &uint16Config, &f7);
  493. UA_Server_addDataSetField(server, publishedDataSetId, &sbyteConfig, &f6);
  494. UA_Server_addDataSetField(server, publishedDataSetId, &int64Config, &f5);
  495. UA_Server_addDataSetField(server, publishedDataSetId, &int32Config, &f4);
  496. UA_Server_addDataSetField(server, publishedDataSetId, &int16Config, &f3);
  497. UA_Server_addDataSetField(server, publishedDataSetId, &byteConfig, &f2);
  498. UA_Server_addDataSetField(server, publishedDataSetId, &boolToggleConfig, &f1);
  499. }
  500. }
  501. void
  502. timerCallback(UA_Server *server, void *data) {
  503. // DataSet 1
  504. // BoolToggle
  505. ds1BoolToggleCount++;
  506. UA_Variant tmpVari;
  507. if (ds1BoolToggleCount >= 3) {
  508. if (ds1BoolToggleVal) {
  509. ds1BoolToggleVal = false;
  510. } else {
  511. ds1BoolToggleVal = true;
  512. }
  513. UA_Variant_init(&tmpVari);
  514. UA_Variant_setScalar(&tmpVari, &ds1BoolToggleVal, &UA_TYPES[UA_TYPES_BOOLEAN]);
  515. UA_Server_writeValue(server, ds1BoolToggleId, tmpVari);
  516. ds1BoolToggleCount = 0;
  517. }
  518. // Int32
  519. UA_Variant_init(&tmpVari);
  520. ds1Int32Val++;
  521. if(ds1Int32Val > 10000)
  522. ds1Int32Val = 0;
  523. UA_Variant_setScalar(&tmpVari, &ds1Int32Val, &UA_TYPES[UA_TYPES_INT32]);
  524. UA_Server_writeValue(server, ds1Int32Id, tmpVari);
  525. // Int32Fast
  526. UA_Variant_init(&tmpVari);
  527. ds1Int32FastVal += 100;
  528. if(ds1Int32FastVal > 10000)
  529. ds1Int32FastVal = 0;
  530. UA_Variant_setScalar(&tmpVari, &ds1Int32FastVal, &UA_TYPES[UA_TYPES_INT32]);
  531. UA_Server_writeValue(server, ds1Int32FastId, tmpVari);
  532. // DateTime
  533. UA_Variant_init(&tmpVari);
  534. UA_DateTime tmpTime = UA_DateTime_now();
  535. UA_Variant_setScalar(&tmpVari, &tmpTime, &UA_TYPES[UA_TYPES_DATETIME]);
  536. UA_Server_writeValue(server, ds1DateTimeId, tmpVari);
  537. // DataSet 2
  538. UA_Server_writeValue(server, ds2DateTimeId, tmpVari);
  539. // BoolToggle
  540. if (ds2BoolToggleVal)
  541. ds2BoolToggleVal = false;
  542. else
  543. ds2BoolToggleVal = true;
  544. // Write new value
  545. UA_Variant_init(&tmpVari);
  546. UA_Variant_setScalar(&tmpVari, &ds2BoolToggleVal, &UA_TYPES[UA_TYPES_BOOLEAN]);
  547. UA_Server_writeValue(server, ds2BoolToggleId, tmpVari);
  548. // Byte
  549. UA_Variant_init(&tmpVari);
  550. ds2ByteVal++;
  551. UA_Variant_setScalar(&tmpVari, &ds2ByteVal, &UA_TYPES[UA_TYPES_BYTE]);
  552. UA_Server_writeValue(server, ds2ByteId, tmpVari);
  553. // Int16
  554. UA_Variant_init(&tmpVari);
  555. ds2Int16Val++;
  556. UA_Variant_setScalar(&tmpVari, &ds2Int16Val, &UA_TYPES[UA_TYPES_INT16]);
  557. UA_Server_writeValue(server, ds2Int16Id, tmpVari);
  558. // Int32
  559. UA_Variant_init(&tmpVari);
  560. ds2Int32Val++;
  561. UA_Variant_setScalar(&tmpVari, &ds2Int32Val, &UA_TYPES[UA_TYPES_INT32]);
  562. UA_Server_writeValue(server, ds2Int32Id, tmpVari);
  563. // Int64
  564. UA_Variant_init(&tmpVari);
  565. ds2Int64Val++;
  566. UA_Variant_setScalar(&tmpVari, &ds2Int64Val, &UA_TYPES[UA_TYPES_INT64]);
  567. UA_Server_writeValue(server, ds2Int64Id, tmpVari);
  568. // SByte
  569. UA_Variant_init(&tmpVari);
  570. ds2SByteVal++;
  571. UA_Variant_setScalar(&tmpVari, &ds2SByteVal, &UA_TYPES[UA_TYPES_SBYTE]);
  572. UA_Server_writeValue(server, ds2SByteId, tmpVari);
  573. // UInt16
  574. UA_Variant_init(&tmpVari);
  575. ds2UInt16Val++;
  576. UA_Variant_setScalar(&tmpVari, &ds2UInt16Val, &UA_TYPES[UA_TYPES_UINT16]);
  577. UA_Server_writeValue(server, ds2UInt16Id, tmpVari);
  578. // UInt32
  579. UA_Variant_init(&tmpVari);
  580. ds2UInt32Val++;
  581. UA_Variant_setScalar(&tmpVari, &ds2UInt32Val, &UA_TYPES[UA_TYPES_UINT32]);
  582. UA_Server_writeValue(server, ds2UInt32Id, tmpVari);
  583. // UInt64
  584. UA_Variant_init(&tmpVari);
  585. ds2UInt64Val++;
  586. UA_Variant_setScalar(&tmpVari, &ds2UInt64Val, &UA_TYPES[UA_TYPES_UINT64]);
  587. UA_Server_writeValue(server, ds2UInt64Id, tmpVari);
  588. // Float
  589. UA_Variant_init(&tmpVari);
  590. ds2FloatVal++;
  591. UA_Variant_setScalar(&tmpVari, &ds2FloatVal, &UA_TYPES[UA_TYPES_FLOAT]);
  592. UA_Server_writeValue(server, ds2FloatId, tmpVari);
  593. // Double
  594. UA_Variant_init(&tmpVari);
  595. ds2DoubleVal++;
  596. UA_Variant_setScalar(&tmpVari, &ds2DoubleVal, &UA_TYPES[UA_TYPES_DOUBLE]);
  597. UA_Server_writeValue(server, ds2DoubleId, tmpVari);
  598. // String
  599. UA_Variant_init(&tmpVari);
  600. ds2StringIndex++;
  601. if(ds2StringIndex >= ds2StringArrayLen)
  602. ds2StringIndex = 0;
  603. UA_Variant_setScalar(&tmpVari, &ds2StringArray[ds2StringIndex], &UA_TYPES[UA_TYPES_STRING]);
  604. UA_Server_writeValue(server, ds2StringId, tmpVari);
  605. // ByteString
  606. UA_Variant_init(&tmpVari);
  607. UA_ByteString bs;
  608. UA_ByteString_init(&bs);
  609. UA_ByteString_allocBuffer(&bs, 4);
  610. UA_UInt32 ui2 = UA_UInt32_random();
  611. memcpy(bs.data, &ui2, 4);
  612. UA_Variant_setScalar(&tmpVari, &bs, &UA_TYPES[UA_TYPES_BYTESTRING]);
  613. UA_Server_writeValue(server, ds2ByteStringId, tmpVari);
  614. UA_ByteString_clear(&bs);
  615. // Guid
  616. UA_Variant_init(&tmpVari);
  617. UA_Guid g = UA_Guid_random();
  618. UA_Variant_setScalar(&tmpVari, &g, &UA_TYPES[UA_TYPES_GUID]);
  619. UA_Server_writeValue(server, ds2GuidId, tmpVari);
  620. // UInt32Array
  621. for(size_t i = 0; i < 10; i++)
  622. ds2UInt32ArrValue[i]++;
  623. UA_Variant_init(&tmpVari);
  624. UA_Variant_setArray(&tmpVari, ds2UInt32ArrValue, 10, &UA_TYPES[UA_TYPES_UINT32]);
  625. UA_Server_writeValue(server, ds2UInt32ArrId, tmpVari);
  626. }
  627. UA_Boolean running = true;
  628. static void stopHandler(int sig) {
  629. running = false;
  630. }
  631. static int run(UA_String *transportProfile,
  632. UA_NetworkAddressUrlDataType *networkAddressUrl) {
  633. signal(SIGINT, stopHandler);
  634. signal(SIGTERM, stopHandler);
  635. UA_Server *server = UA_Server_new();
  636. UA_ServerConfig *config = UA_Server_getConfig(server);
  637. UA_ServerConfig_setMinimal(config, 4802, NULL);
  638. /* Details about the connection configuration and handling are located in
  639. * the pubsub connection tutorial */
  640. config->pubsubTransportLayers =
  641. (UA_PubSubTransportLayer *)UA_calloc(2, sizeof(UA_PubSubTransportLayer));
  642. if (!config->pubsubTransportLayers) {
  643. UA_Server_delete(server);
  644. return EXIT_FAILURE;
  645. }
  646. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  647. config->pubsubTransportLayersSize++;
  648. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  649. config->pubsubTransportLayers[1] = UA_PubSubTransportLayerEthernet();
  650. config->pubsubTransportLayersSize++;
  651. #endif
  652. addPubSubConnection(server, transportProfile, networkAddressUrl);
  653. /* Create a PublishedDataSet based on a PublishedDataSetConfig. */
  654. UA_PublishedDataSetConfig publishedDataSetConfig;
  655. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  656. publishedDataSetConfig.name = UA_STRING("DataSet 1 (Simple)");
  657. UA_NodeId publishedDataSetIdent;
  658. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  659. addPublisher1(server, publishedDataSetIdent);
  660. /* Create a new WriterGroup and configure parameters like the publish interval. */
  661. UA_WriterGroupConfig writerGroupConfig;
  662. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  663. writerGroupConfig.name = UA_STRING("DataSet WriterGroup");
  664. writerGroupConfig.publishingInterval = 500;
  665. writerGroupConfig.enabled = UA_FALSE;
  666. writerGroupConfig.writerGroupId = 100;
  667. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  668. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 3;
  669. /* Add the new WriterGroup to an existing Connection. */
  670. UA_NodeId writerGroupIdent;
  671. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  672. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  673. /* Create a new Writer and connect it with an existing PublishedDataSet */
  674. // DataSetWriter ID 1 with Variant Encoding
  675. UA_DataSetWriterConfig dataSetWriterConfig;
  676. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  677. dataSetWriterConfig.name = UA_STRING("DataSet 1 DataSetWriter");
  678. dataSetWriterConfig.dataSetWriterId = 1;
  679. //The creation of delta messages is configured in the following line. Value
  680. // 0 -> no delta messages are created.
  681. dataSetWriterConfig.keyFrameCount = 10;
  682. UA_NodeId writerIdentifier;
  683. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  684. &dataSetWriterConfig, &writerIdentifier);
  685. // Published DataSet 2
  686. UA_PublishedDataSetConfig publishedDataSetConfig2;
  687. publishedDataSetConfig2.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  688. publishedDataSetConfig2.name = UA_STRING("DataSet 2 (AllTypes)");
  689. UA_NodeId publishedDataSetIdent2;
  690. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig2, &publishedDataSetIdent2);
  691. addPublisher2(server, publishedDataSetIdent2);
  692. // DataSet Writer 2
  693. // Create a new Writer and connect it with an existing PublishedDataSet
  694. UA_DataSetWriterConfig dataSetWriterConfig2;
  695. memset(&dataSetWriterConfig2, 0, sizeof(UA_DataSetWriterConfig));
  696. dataSetWriterConfig2.name = UA_STRING("DataSet 2 DataSetWriter");
  697. dataSetWriterConfig2.dataSetWriterId = 2;
  698. //The creation of delta messages is configured in the following line. Value
  699. // 0 -> no delta messages are created.
  700. dataSetWriterConfig2.keyFrameCount = 10;
  701. UA_NodeId writerIdentifier2;
  702. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent2,
  703. &dataSetWriterConfig2, &writerIdentifier2);
  704. UA_UInt64 timerCallbackId = 0;
  705. UA_Server_addRepeatedCallback(server, (UA_ServerCallback)timerCallback, NULL, 1000, &timerCallbackId);
  706. UA_StatusCode retval = UA_Server_run(server, &running);
  707. UA_Server_delete(server);
  708. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  709. }
  710. static void
  711. usage(char *progname) {
  712. printf("usage: %s <uri> [device]\n", progname);
  713. }
  714. int main(int argc, char **argv) {
  715. UA_String transportProfile =
  716. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  717. UA_NetworkAddressUrlDataType networkAddressUrl =
  718. { UA_STRING_NULL, UA_STRING("opc.udp://239.0.0.1:4840/") };
  719. if (argc > 1) {
  720. if (strcmp(argv[1], "-h") == 0) {
  721. usage(argv[0]);
  722. return EXIT_SUCCESS;
  723. }
  724. else if (strncmp(argv[1], "opc.udp://", 10) == 0) {
  725. networkAddressUrl.url = UA_STRING(argv[1]);
  726. }
  727. else if (strncmp(argv[1], "opc.eth://", 10) == 0) {
  728. transportProfile =
  729. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-eth-uadp");
  730. if (argc < 3) {
  731. printf("Error: UADP/ETH needs an interface name\n");
  732. return EXIT_FAILURE;
  733. }
  734. networkAddressUrl.networkInterface = UA_STRING(argv[2]);
  735. networkAddressUrl.url = UA_STRING(argv[1]);
  736. }
  737. else {
  738. printf("Error: unknown URI\n");
  739. return EXIT_FAILURE;
  740. }
  741. }
  742. return run(&transportProfile, &networkAddressUrl);
  743. }