server_inheritance.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <signal.h>
  4. #include "open62541.h"
  5. UA_Boolean running = true;
  6. static void stopHandler(int sig) {
  7. running = false;
  8. }
  9. /**
  10. * This will create a type structure and some instances of the types:
  11. *
  12. * Create a rudimentary objectType
  13. *
  14. * Type:
  15. * + MamalType
  16. * v- Class = "mamalia"
  17. * v- Species
  18. * o- Abilities
  19. * v- MakeSound
  20. * v- Breathe = True
  21. * + DogType
  22. * v- Species = "Canis"
  23. * v- Name
  24. * o- Abilities
  25. * v- MakeSound = "Wuff"
  26. * v- FetchNewPaper
  27. */
  28. static void createMamals(UA_Server *server) {
  29. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  30. otAttr.description = UA_LOCALIZEDTEXT("en-US", "A mamal");
  31. otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MamalType");
  32. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
  33. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  34. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  35. UA_QUALIFIEDNAME(1, "MamalType"), otAttr, NULL, NULL);
  36. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  37. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mamals class");
  38. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Class");
  39. UA_String classVar = UA_STRING("mamalia");
  40. UA_Variant_setScalar(&vAttr.value, &classVar, &UA_TYPES[UA_TYPES_STRING]);
  41. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
  42. UA_NODEID_NUMERIC(1, 10000),
  43. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  44. UA_QUALIFIEDNAME(1, "Class"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  45. vAttr, NULL, NULL);
  46. vAttr = UA_VariableAttributes_default;
  47. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mamals species");
  48. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Species");
  49. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10002),
  50. UA_NODEID_NUMERIC(1, 10000),
  51. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  52. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  53. vAttr, NULL, NULL);
  54. otAttr = UA_ObjectTypeAttributes_default;
  55. otAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog, subtype of mamal");
  56. otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DogType");
  57. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 20000),
  58. UA_NODEID_NUMERIC(1, 10000),
  59. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  60. UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
  61. vAttr = UA_VariableAttributes_default;
  62. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This dogs species");
  63. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Species");
  64. UA_String defaultSpecies = UA_STRING("Canis");
  65. UA_Variant_setScalar(&vAttr.value, &defaultSpecies, &UA_TYPES[UA_TYPES_STRING]);
  66. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20001),
  67. UA_NODEID_NUMERIC(1, 20000),
  68. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  69. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  70. vAttr, NULL, NULL);
  71. vAttr = UA_VariableAttributes_default;
  72. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This dogs name");
  73. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Name");
  74. UA_String defaultName = UA_STRING("unnamed dog");
  75. UA_Variant_setScalar(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
  76. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20002),
  77. UA_NODEID_NUMERIC(1, 20000),
  78. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  79. UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  80. vAttr, NULL, NULL);
  81. /* Instatiate a dog named bello:
  82. * (O) Objects
  83. * + O Bello <DogType>
  84. * + Age
  85. * + Name
  86. */
  87. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  88. oAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog named Bello");
  89. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Bello");
  90. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  91. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  92. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  93. UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 20000),
  94. oAttr, NULL, NULL);
  95. oAttr = UA_ObjectAttributes_default;
  96. oAttr.description = UA_LOCALIZEDTEXT("en-US", "Another dog");
  97. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Dog2");
  98. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  100. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  101. UA_QUALIFIEDNAME(1, "Dog2"), UA_NODEID_NUMERIC(1, 20000),
  102. oAttr, NULL, NULL);
  103. oAttr = UA_ObjectAttributes_default;
  104. oAttr.description = UA_LOCALIZEDTEXT("en-US", "A mamal");
  105. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Mamal1");
  106. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  107. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  108. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  109. UA_QUALIFIEDNAME(1, "Mamal1"), UA_NODEID_NUMERIC(1, 10000),
  110. oAttr, NULL, NULL);
  111. }
  112. /**
  113. * This method shows the usage of _begin and _finish methods.
  114. * Normally, if you create an instance of an object type, all its
  115. * mandatory children are inherited and created.
  116. * It could be the case that you first need to create a node,
  117. * add some children with specific IDs and then all the remaining
  118. * inherited children should be created.
  119. * For this use-case you can use first the _begin method,
  120. * which creates the node, including its parent references.
  121. * Then you can add any children, and then you should
  122. * call _finish on that node, which then adds all the inherited children.
  123. *
  124. * For further details check the example below or the corresponding
  125. * method documentation.
  126. *
  127. * To demonstrate this, we use the following example:
  128. *
  129. * + ObjectType
  130. * + LampType (Object)
  131. * + IsOn (Variable, Boolean, Mandatory)
  132. * + Brightness (Variable, UInt16, Mandatory)
  133. * + Objects
  134. * + LampGreen
  135. * Should inherit the mandatory IsOn and Brightness with a generated node ID
  136. * + LampRed
  137. * IsOn should have the node ID 30101, Brightness will be inherited with a generated node ID
  138. *
  139. */
  140. static void createCustomInheritance(UA_Server *server) {
  141. /* Add LampType object type node */
  142. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  143. otAttr.description = UA_LOCALIZEDTEXT("en-US", "A Lamp");
  144. otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampType");
  145. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 30000),
  146. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  147. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  148. UA_QUALIFIEDNAME(1, "LampType"), otAttr, NULL, NULL);
  149. /* Add the two mandatory children, IsOn and Brightness */
  150. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  151. vAttr.description = UA_LOCALIZEDTEXT("en-US", "Switch lamp on/off");
  152. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IsOn");
  153. UA_Boolean isOn = UA_FALSE;
  154. UA_Variant_setScalar(&vAttr.value, &isOn, &UA_TYPES[UA_TYPES_BOOLEAN]);
  155. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30001),
  156. UA_NODEID_NUMERIC(1, 30000),
  157. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  158. UA_QUALIFIEDNAME(1, "IsOn"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  159. vAttr, NULL, NULL);
  160. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 30001),
  161. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  162. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  163. vAttr = UA_VariableAttributes_default;
  164. vAttr.description = UA_LOCALIZEDTEXT("en-US", "Lamp brightness");
  165. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Brightness");
  166. UA_UInt16 brightness = 142;
  167. UA_Variant_setScalar(&vAttr.value, &brightness, &UA_TYPES[UA_TYPES_UINT16]);
  168. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30002),
  169. UA_NODEID_NUMERIC(1, 30000),
  170. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  171. UA_QUALIFIEDNAME(1, "Brightness"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  172. vAttr, NULL, NULL);
  173. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, 30002),
  174. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  175. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  176. /* Now we want to inherit all the mandatory children for LampGreen and don't care about the node ids.
  177. * These will be automatically generated. This will internally call the _begin and _finish methods */
  178. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  179. oAttr.description = UA_LOCALIZEDTEXT("en-US", "A green lamp");
  180. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampGreen");
  181. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  182. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  183. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  184. UA_QUALIFIEDNAME(1, "LampGreen"), UA_NODEID_NUMERIC(1, 30000),
  185. oAttr, NULL, NULL);
  186. /* For the red lamp we want to set the node ID of the IsOn child manually, thus we need to use
  187. * the _begin method, add the child and then _finish: */
  188. /* The call to UA_Server_addNode_begin will create the node and its parent references,
  189. * but it will not instantiate the mandatory children */
  190. oAttr = UA_ObjectAttributes_default;
  191. oAttr.description = UA_LOCALIZEDTEXT("en-US", "A red lamp");
  192. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "LampRed");
  193. UA_Server_addNode_begin(server, UA_NODECLASS_OBJECT,
  194. UA_NODEID_NUMERIC(1, 30100),
  195. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  196. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  197. UA_QUALIFIEDNAME(1, "LampRed"),
  198. UA_NODEID_NUMERIC(1, 30000),
  199. (const UA_NodeAttributes*)&oAttr, &UA_TYPES[UA_TYPES_OBJECTATTRIBUTES],
  200. NULL, NULL);
  201. /* Now we can add the IsOn with our own node ID */
  202. vAttr = UA_VariableAttributes_default;
  203. vAttr.description = UA_LOCALIZEDTEXT("en-US", "Switch lamp on/off");
  204. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IsOn");
  205. isOn = UA_FALSE;
  206. UA_Variant_setScalar(&vAttr.value, &isOn, &UA_TYPES[UA_TYPES_BOOLEAN]);
  207. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 30101),
  208. UA_NODEID_NUMERIC(1, 30100),
  209. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  210. UA_QUALIFIEDNAME(1, "IsOn"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  211. vAttr, NULL, NULL);
  212. /* And then we need to call the UA_Server_addNode_finish which adds all the remaining
  213. * children and does some further initialization. It will not add the IsNode child,
  214. * since a child with the same browse name already exists */
  215. UA_Server_addNode_finish(server, UA_NODEID_NUMERIC(1, 30100));
  216. }
  217. int main(void) {
  218. signal(SIGINT, stopHandler);
  219. signal(SIGTERM, stopHandler);
  220. UA_ServerConfig *config = UA_ServerConfig_new_default();
  221. UA_Server *server = UA_Server_new(config);
  222. createMamals(server);
  223. createCustomInheritance(server);
  224. /* Run the server */
  225. UA_StatusCode retval = UA_Server_run(server, &running);
  226. UA_Server_delete(server);
  227. UA_ServerConfig_delete(config);
  228. return (int)retval;
  229. }