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