server_instantiation.c 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <open62541/plugin/log_stdout.h>
  4. #include <open62541/server.h>
  5. #include <open62541/server_config_default.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. UA_Boolean running = true;
  9. static void stopHandler(int sig) {
  10. running = false;
  11. }
  12. int main(void) {
  13. signal(SIGINT, stopHandler);
  14. signal(SIGTERM, stopHandler);
  15. UA_ServerConfig *config = UA_ServerConfig_new_default();
  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_setScalar(&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_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), 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_setScalar(&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_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), 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. UA_ServerConfig_delete(config);
  74. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  75. }