server_udp.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <stdio.h>
  6. #include <signal.h>
  7. #ifdef UA_NO_AMALGAMATION
  8. #include "ua_types.h"
  9. #include "ua_server.h"
  10. #include "ua_config_standard.h"
  11. #include "ua_log_stdout.h"
  12. #include "ua_network_udp.h"
  13. #else
  14. #include "open62541.h"
  15. #endif
  16. UA_Logger logger = UA_Log_Stdout;
  17. UA_Boolean running = 1;
  18. static void stopHandler(int sign) {
  19. printf("Received Ctrl-C\n");
  20. running = 0;
  21. }
  22. int main(int argc, char** argv) {
  23. signal(SIGINT, stopHandler);
  24. signal(SIGTERM, stopHandler);
  25. UA_ServerConfig config = UA_ServerConfig_standard;
  26. UA_ServerNetworkLayer nl;
  27. nl = UA_ServerNetworkLayerUDP(UA_ConnectionConfig_standard, 16664);
  28. config.networkLayers = &nl;
  29. config.networkLayersSize = 1;
  30. UA_Server *server = UA_Server_new(config);
  31. // add a variable node to the adresspace
  32. UA_VariableAttributes attr;
  33. UA_VariableAttributes_init(&attr);
  34. UA_Int32 myInteger = 42;
  35. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  36. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  37. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  38. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  39. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  40. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  41. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  42. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  43. parentReferenceNodeId, myIntegerName,
  44. UA_NODEID_NULL, attr, NULL, NULL);
  45. UA_StatusCode retval = UA_Server_run(server, &running);
  46. UA_Server_delete(server);
  47. return (int) retval;
  48. }