server_inheritance.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_standard;
  13. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  14. config.networkLayers = &nl;
  15. config.networkLayersSize = 1;
  16. UA_Server *server = UA_Server_new(config);
  17. /* Create a rudimentary objectType
  18. *
  19. * Type:
  20. * + MamalType
  21. * v- Class = "mamalia"
  22. * v- Species
  23. * o- Abilities
  24. * v- MakeSound
  25. * v- Breathe = True
  26. * + DogType
  27. * v- Species = "Canis"
  28. * v- Name
  29. * o- Abilities
  30. * v- MakeSound = "Wuff"
  31. * v- FetchNewPaper
  32. */
  33. UA_StatusCode retval;
  34. UA_ObjectTypeAttributes otAttr;
  35. UA_ObjectTypeAttributes_init(&otAttr);
  36. otAttr.description = UA_LOCALIZEDTEXT("en_US", "A mamal");
  37. otAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MamalType");
  38. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
  39. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  40. UA_QUALIFIEDNAME(1, "MamalType"), otAttr, NULL, NULL);
  41. UA_VariableAttributes vAttr;
  42. UA_VariableAttributes_init(&vAttr);
  43. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals class");
  44. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Class");
  45. UA_String classVar = UA_STRING("mamalia");
  46. UA_Variant_setScalarCopy(&vAttr.value, &classVar, &UA_TYPES[UA_TYPES_STRING]);
  47. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
  48. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  49. UA_QUALIFIEDNAME(1, "Class"), UA_NODEID_NULL, vAttr, NULL, NULL);
  50. UA_VariableAttributes_init(&vAttr);
  51. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals species");
  52. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Species");
  53. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10002),
  54. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  55. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NULL, vAttr, NULL, NULL);
  56. UA_ObjectTypeAttributes_init(&otAttr);
  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), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  61. UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
  62. UA_VariableAttributes_init(&vAttr);
  63. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This dogs species");
  64. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Species");
  65. UA_String defaultSpecies = UA_STRING("Canis");
  66. UA_Variant_setScalarCopy(&vAttr.value, &defaultSpecies, &UA_TYPES[UA_TYPES_STRING]);
  67. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20001),
  68. UA_NODEID_NUMERIC(1, 20000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  69. UA_QUALIFIEDNAME(1, "Species"), UA_NODEID_NULL, vAttr, NULL, NULL);
  70. UA_VariableAttributes_init(&vAttr);
  71. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This dogs name");
  72. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Name");
  73. UA_String defaultName = UA_STRING("unnamed dog");
  74. UA_Variant_setScalarCopy(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
  75. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 20002),
  76. UA_NODEID_NUMERIC(1, 20000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  77. UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NULL, vAttr, NULL, NULL);
  78. /* Instatiate a dog named bello:
  79. * (O) Objects
  80. * + O Bello <DogType>
  81. * + Age
  82. * + Name
  83. */
  84. UA_ObjectAttributes oAttr;
  85. UA_ObjectAttributes_init(&oAttr);
  86. oAttr.description = UA_LOCALIZEDTEXT("en_US", "A dog named Bello");
  87. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Bello");
  88. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  89. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  90. UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 20000), oAttr, NULL, NULL);
  91. UA_ObjectAttributes_init(&oAttr);
  92. oAttr.description = UA_LOCALIZEDTEXT("en_US", "Another dog");
  93. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Dog2");
  94. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  95. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  96. UA_QUALIFIEDNAME(1, "Dog2"), UA_NODEID_NUMERIC(1, 20000), oAttr, NULL, NULL);
  97. UA_ObjectAttributes_init(&oAttr);
  98. oAttr.description = UA_LOCALIZEDTEXT("en_US", "A mamal");
  99. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Mamal1");
  100. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  101. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  102. UA_QUALIFIEDNAME(1, "Mamal1"), UA_NODEID_NUMERIC(1, 10000), oAttr, NULL, NULL);
  103. retval = UA_Server_run(server, &running);
  104. UA_Server_delete(server);
  105. nl.deleteMembers(&nl);
  106. return (int)retval;
  107. }