server_inheritance.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifdef UA_NO_AMALGAMATION
  5. #include "ua_types.h"
  6. #include "ua_server.h"
  7. #include "ua_config_standard.h"
  8. #include "ua_network_tcp.h"
  9. #else
  10. #include "open62541.h"
  11. #endif
  12. UA_Boolean running = true;
  13. static void stopHandler(int sig) {
  14. running = false;
  15. }
  16. int main(void) {
  17. signal(SIGINT, stopHandler);
  18. signal(SIGTERM, stopHandler);
  19. UA_ServerConfig config = UA_ServerConfig_standard;
  20. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  21. config.networkLayers = &nl;
  22. config.networkLayersSize = 1;
  23. UA_Server *server = UA_Server_new(config);
  24. /* Create a rudimentary objectType
  25. *
  26. * Type:
  27. * + MamalType
  28. * v- Class = "mamalia"
  29. * v- Species
  30. * o- Abilities
  31. * v- MakeSound
  32. * v- Breathe = True
  33. * + DogType
  34. * v- Species = "Canis"
  35. * v- Name
  36. * o- Abilities
  37. * v- MakeSound = "Wuff"
  38. * v- FetchNewPaper
  39. */
  40. UA_StatusCode retval;
  41. UA_ObjectTypeAttributes otAttr;
  42. UA_ObjectTypeAttributes_init(&otAttr);
  43. otAttr.description = UA_LOCALIZEDTEXT("en_US", "A mamal");
  44. otAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MamalType");
  45. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
  46. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  47. UA_QUALIFIEDNAME(1, "MamalType"), otAttr, NULL, NULL);
  48. UA_VariableAttributes vAttr;
  49. UA_VariableAttributes_init(&vAttr);
  50. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals class");
  51. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Class");
  52. UA_String classVar = UA_STRING("mamalia");
  53. UA_Variant_setScalarCopy(&vAttr.value, &classVar, &UA_TYPES[UA_TYPES_STRING]);
  54. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
  55. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  56. UA_QUALIFIEDNAME(1, "Class"), UA_NODEID_NULL, vAttr, NULL, NULL);
  57. UA_VariableAttributes_init(&vAttr);
  58. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals species");
  59. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Species");
  60. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10002),
  61. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  62. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NULL, vAttr, NULL, NULL);
  63. UA_ObjectTypeAttributes_init(&otAttr);
  64. otAttr.description = UA_LOCALIZEDTEXT("en_US", "A dog, subtype of mamal");
  65. otAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DogType");
  66. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 20000),
  67. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  68. UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
  69. UA_VariableAttributes_init(&vAttr);
  70. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This dogs species");
  71. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Species");
  72. UA_String defaultSpecies = UA_STRING("Canis");
  73. UA_Variant_setScalarCopy(&vAttr.value, &defaultSpecies, &UA_TYPES[UA_TYPES_STRING]);
  74. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20001),
  75. UA_NODEID_NUMERIC(1, 20000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  76. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NULL, vAttr, NULL, NULL);
  77. UA_VariableAttributes_init(&vAttr);
  78. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This dogs name");
  79. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Name");
  80. UA_String defaultName = UA_STRING("unnamed dog");
  81. UA_Variant_setScalarCopy(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
  82. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20002),
  83. UA_NODEID_NUMERIC(1, 20000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  84. UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NULL, vAttr, NULL, NULL);
  85. /* Instatiate a dog named bello:
  86. * (O) Objects
  87. * + O Bello <DogType>
  88. * + Age
  89. * + Name
  90. */
  91. UA_ObjectAttributes oAttr;
  92. UA_ObjectAttributes_init(&oAttr);
  93. oAttr.description = UA_LOCALIZEDTEXT("en_US", "A dog named Bello");
  94. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Bello");
  95. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  97. UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 20000), oAttr, NULL, NULL);
  98. UA_ObjectAttributes_init(&oAttr);
  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), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  103. UA_QUALIFIEDNAME(1, "Dog2"), UA_NODEID_NUMERIC(1, 20000), oAttr, NULL, NULL);
  104. UA_ObjectAttributes_init(&oAttr);
  105. oAttr.description = UA_LOCALIZEDTEXT("en_US", "A mamal");
  106. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Mamal1");
  107. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  108. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  109. UA_QUALIFIEDNAME(1, "Mamal1"), UA_NODEID_NUMERIC(1, 10000), oAttr, NULL, NULL);
  110. retval = UA_Server_run(server, &running);
  111. UA_Server_delete(server);
  112. nl.deleteMembers(&nl);
  113. return (int)retval;
  114. }