server.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <signal.h>
  6. #include <iostream>
  7. #include <cstring>
  8. #ifdef UA_NO_AMALGAMATION
  9. # include "ua_server.h"
  10. # include "logger_stdout.h"
  11. # include "networklayer_tcp.h"
  12. #else
  13. # include "open62541.h"
  14. #endif
  15. /**
  16. * Build Instructions (Linux)
  17. * - gcc -std=c99 -c open62541.c
  18. * - g++ server.cpp open62541.o -o server
  19. */
  20. using namespace std;
  21. UA_Boolean running = true;
  22. UA_Logger logger = Logger_Stdout;
  23. static void stopHandler(int sign) {
  24. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  25. running = false;
  26. }
  27. int main() {
  28. signal(SIGINT, stopHandler); /* catches ctrl-c */
  29. UA_ServerConfig config = UA_ServerConfig_standard;
  30. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  31. config.logger = Logger_Stdout;
  32. config.networkLayers = &nl;
  33. config.networkLayersSize = 1;
  34. UA_Server *server = UA_Server_new(config);
  35. // add a variable node to the adresspace
  36. UA_VariableAttributes attr;
  37. UA_VariableAttributes_init(&attr);
  38. UA_Int32 myInteger = 42;
  39. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  40. attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  41. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  42. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  43. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  44. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  45. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  46. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  47. parentReferenceNodeId, myIntegerName,
  48. UA_NODEID_NULL, attr, NULL, NULL);
  49. /* allocations on the heap need to be freed */
  50. UA_VariableAttributes_deleteMembers(&attr);
  51. UA_NodeId_deleteMembers(&myIntegerNodeId);
  52. UA_QualifiedName_deleteMembers(&myIntegerName);
  53. UA_StatusCode retval = UA_Server_run(server, &running);
  54. UA_Server_delete(server);
  55. nl.deleteMembers(&nl);
  56. return retval;
  57. }