server_inheritance.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. int main(void) {
  10. signal(SIGINT, stopHandler);
  11. signal(SIGTERM, stopHandler);
  12. UA_ServerConfig *config = UA_ServerConfig_new_default();
  13. UA_Server *server = UA_Server_new(config);
  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. UA_StatusCode retval;
  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_NULL,
  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_NULL,
  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_NULL,
  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_NULL,
  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. /* Run the server */
  114. retval = UA_Server_run(server, &running);
  115. UA_Server_delete(server);
  116. UA_ServerConfig_delete(config);
  117. return (int)retval;
  118. }