server_instantiation.c 3.6 KB

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