server_simple.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <time.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <errno.h> // errno, EINTR
  10. #include <string.h>
  11. #ifdef UA_AMALGAMATE
  12. # include "open62541.h"
  13. #else
  14. # include "ua_types.h"
  15. # include "ua_server.h"
  16. # include "logger_stdout.h"
  17. # include "networklayer_tcp.h"
  18. #endif
  19. UA_Boolean running = 1;
  20. UA_Logger logger;
  21. static void stopHandler(int sign) {
  22. printf("Received Ctrl-C\n");
  23. running = 0;
  24. }
  25. static UA_ByteString loadCertificate(void) {
  26. UA_ByteString certificate = UA_STRING_NULL;
  27. FILE *fp = NULL;
  28. //FIXME: a potential bug of locating the certificate, we need to get the path from the server's config
  29. fp=fopen("server_cert.der", "rb");
  30. if(!fp) {
  31. errno = 0; // we read errno also from the tcp layer...
  32. return certificate;
  33. }
  34. fseek(fp, 0, SEEK_END);
  35. certificate.length = ftell(fp);
  36. certificate.data = malloc(certificate.length*sizeof(UA_Byte));
  37. if(!certificate.data)
  38. return certificate;
  39. fseek(fp, 0, SEEK_SET);
  40. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  41. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  42. fclose(fp);
  43. return certificate;
  44. }
  45. static void testCallback(UA_Server *server, void *data) {
  46. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "testcallback");
  47. }
  48. int main(int argc, char** argv) {
  49. signal(SIGINT, stopHandler); /* catches ctrl-c */
  50. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  51. logger = Logger_Stdout_new();
  52. UA_Server_setLogger(server, logger);
  53. UA_ByteString certificate = loadCertificate();
  54. UA_Server_setServerCertificate(server, certificate);
  55. UA_ByteString_deleteMembers(&certificate);
  56. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  57. UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
  58. .job.methodCall = {.method = testCallback, .data = NULL} };
  59. UA_Server_addRepeatedJob(server, job, 2000, NULL); // call every 2 sec
  60. // add a variable node to the adresspace
  61. UA_Variant *myIntegerVariant = UA_Variant_new();
  62. UA_Int32 myInteger = 42;
  63. UA_Variant_setScalarCopy(myIntegerVariant, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  64. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  65. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer"); /* UA_NODEID_NULL would assign a random free nodeid */
  66. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  67. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  68. UA_Server_addVariableNode(server, myIntegerVariant, myIntegerName,
  69. myIntegerNodeId, parentNodeId, parentReferenceNodeId);
  70. /*
  71. #ifdef BENCHMARK
  72. UA_UInt32 nodeCount = 50;
  73. char str[32];
  74. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  75. // scalar
  76. void *data = UA_new(&UA_TYPES[i]);
  77. UA_Variant *variant = UA_Variant_new();
  78. UA_Variant_setScalar(variant, data, &UA_TYPES[i]);
  79. sprintf(str,"%d",i);
  80. UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, str);
  81. UA_NodeId id = UA_NODEID_NUMERIC(1, 100 + i);
  82. UA_Server_addVariableNode(server, variant, nodeName, id,
  83. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  84. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
  85. // array
  86. data = UA_Array_new(&UA_TYPES[i], 10);
  87. variant = UA_Variant_new();
  88. UA_Variant_setArray(variant, data, 10, &UA_TYPES[i]);
  89. sprintf(str,"array of %d",i);
  90. nodeName = UA_QUALIFIEDNAME(1, str);
  91. id = UA_NODEID_NUMERIC(1, 200 + i);
  92. UA_Server_addVariableNode(server, variant, nodeName, id,
  93. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  94. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
  95. }
  96. #endif
  97. */
  98. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  99. UA_Server_delete(server);
  100. return retval;
  101. }