server_inheritance.c 13 KB

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