server_instantiation.c 3.7 KB

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