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 <ua_server.h>
  4. #include <ua_config_default.h>
  5. #include <ua_log_stdout.h>
  6. #include <signal.h>
  7. UA_Boolean running = true;
  8. static void stopHandler(int sig) {
  9. running = false;
  10. }
  11. int main(void) {
  12. signal(SIGINT, stopHandler);
  13. signal(SIGTERM, stopHandler);
  14. UA_ServerConfig *config = UA_ServerConfig_new_default();
  15. UA_Server *server = UA_Server_new(config);
  16. /* Create a rudimentary objectType
  17. *
  18. * BaseObjectType
  19. * |
  20. * +- (OT) MamalType
  21. * + (V) Age
  22. * + (OT) DogType
  23. * |
  24. * + (V) Name
  25. */
  26. UA_StatusCode retval;
  27. UA_ObjectTypeAttributes otAttr = UA_ObjectTypeAttributes_default;
  28. otAttr.description = UA_LOCALIZEDTEXT("en-US", "A mamal");
  29. otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "MamalType");
  30. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10000),
  31. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  32. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  33. UA_QUALIFIEDNAME(1, "MamalType"), otAttr, NULL, NULL);
  34. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  35. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mamals Age in months");
  36. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Age");
  37. UA_UInt32 ageVar = 0;
  38. UA_Variant_setScalar(&vAttr.value, &ageVar, &UA_TYPES[UA_TYPES_UINT32]);
  39. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10001),
  40. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  41. UA_QUALIFIEDNAME(1, "Age"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), vAttr, NULL, NULL);
  42. otAttr = UA_ObjectTypeAttributes_default;
  43. otAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog, subtype of mamal");
  44. otAttr.displayName = UA_LOCALIZEDTEXT("en-US", "DogType");
  45. UA_Server_addObjectTypeNode(server, UA_NODEID_NUMERIC(1, 10002),
  46. UA_NODEID_NUMERIC(1, 10000), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  47. UA_QUALIFIEDNAME(1, "DogType"), otAttr, NULL, NULL);
  48. vAttr = UA_VariableAttributes_default;
  49. vAttr.description = UA_LOCALIZEDTEXT("en-US", "This mamals Age in months");
  50. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Name");
  51. UA_String defaultName = UA_STRING("unnamed dog");
  52. UA_Variant_setScalar(&vAttr.value, &defaultName, &UA_TYPES[UA_TYPES_STRING]);
  53. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 10003),
  54. UA_NODEID_NUMERIC(1, 10002), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  55. UA_QUALIFIEDNAME(1, "Name"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), vAttr, NULL, NULL);
  56. /* Instatiate a dog named bello:
  57. * (O) Objects
  58. * + O Bello <DogType>
  59. * + Age
  60. * + Name
  61. */
  62. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  63. oAttr.description = UA_LOCALIZEDTEXT("en-US", "A dog named Bello");
  64. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Bello");
  65. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, 0),
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  67. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  68. UA_QUALIFIEDNAME(1, "Bello"), UA_NODEID_NUMERIC(1, 10002),
  69. oAttr, NULL, NULL);
  70. retval = UA_Server_run(server, &running);
  71. UA_Server_delete(server);
  72. UA_ServerConfig_delete(config);
  73. return (int)retval;
  74. }