server_instantiation.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * BaseObjectType
  27. * |
  28. * +- (OT) MamalType
  29. * + (V) Age
  30. * + (OT) DogType
  31. * |
  32. * + (V) Name
  33. */
  34. UA_StatusCode retval;
  35. UA_ObjectTypeAttributes otAttr;
  36. UA_ObjectTypeAttributes_init(&otAttr);
  37. otAttr.description = UA_LOCALIZEDTEXT("en_US", "A mamal");
  38. otAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MamalType");
  39. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
  40. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  41. UA_QUALIFIEDNAME(1, "MamalType"), otAttr, NULL, NULL);
  42. UA_VariableAttributes vAttr;
  43. UA_VariableAttributes_init(&vAttr);
  44. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals Age in months");
  45. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Age");
  46. UA_UInt32 ageVar = 0;
  47. UA_Variant_setScalarCopy(&vAttr.value, &ageVar, &UA_TYPES[UA_TYPES_UINT32]);
  48. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
  49. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  50. UA_QUALIFIEDNAME(1, "Age"), UA_NODEID_NULL, vAttr, NULL, NULL);
  51. UA_ObjectTypeAttributes_init(&otAttr);
  52. otAttr.description = UA_LOCALIZEDTEXT("en_US", "A dog, subtype of mamal");
  53. otAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DogType");
  54. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10002),
  55. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  56. UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
  57. UA_VariableAttributes_init(&vAttr);
  58. vAttr.description = UA_LOCALIZEDTEXT("en_US", "This mamals Age in months");
  59. vAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Name");
  60. UA_String defaultName = UA_STRING("unnamed dog");
  61. UA_Variant_setScalarCopy(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
  62. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10003),
  63. UA_NODEID_NUMERIC(1, 10002), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  64. UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NULL, vAttr, NULL, NULL);
  65. /* Instatiate a dog named bello:
  66. * (O) Objects
  67. * + O Bello <DogType>
  68. * + Age
  69. * + Name
  70. */
  71. UA_ObjectAttributes oAttr;
  72. UA_ObjectAttributes_init(&oAttr);
  73. oAttr.description = UA_LOCALIZEDTEXT("en_US", "A dog named Bello");
  74. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Bello");
  75. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  77. UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 10002), oAttr, NULL, NULL);
  78. retval = UA_Server_run(server, &running);
  79. UA_Server_delete(server);
  80. nl.deleteMembers(&nl);
  81. return (int)retval;
  82. }