server_inheritance.c 13 KB

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