opcuaServer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "ua_types.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #include <errno.h> // errno, EINTR
  11. // provided by the open62541 lib
  12. #include "ua_server.h"
  13. // provided by the user, implementations available in the /examples folder
  14. #include "logger_stdout.h"
  15. #include "networklayer_tcp.h"
  16. #ifdef EXTENSION_UDP
  17. #include "networklayer_udp.h"
  18. #endif
  19. #include "ua_types.h"
  20. #include "ua_nodeids.h"
  21. UA_Boolean running = 1;
  22. static void stopHandler(int sign) {
  23. printf("Received Ctrl-C\n");
  24. running = 0;
  25. }
  26. static UA_ByteString loadCertificate(void) {
  27. UA_ByteString certificate = UA_STRING_NULL;
  28. FILE *fp = NULL;
  29. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  30. fp=fopen("localhost.der", "rb");
  31. if(!fp) {
  32. errno = 0; // we read errno also from the tcp layer...
  33. return certificate;
  34. }
  35. fseek(fp, 0, SEEK_END);
  36. certificate.length = ftell(fp);
  37. certificate.data = malloc(certificate.length*sizeof(UA_Byte));
  38. if(!certificate.data)
  39. return certificate;
  40. fseek(fp, 0, SEEK_SET);
  41. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  42. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  43. fclose(fp);
  44. return certificate;
  45. }
  46. static void testCallback(UA_Server *server, void *data) {
  47. printf("testcallback\n");
  48. }
  49. int main(int argc, char** argv) {
  50. signal(SIGINT, stopHandler); /* catches ctrl-c */
  51. UA_Server *server = UA_Server_new();
  52. UA_Server_setServerCertificate(server, loadCertificate());
  53. #ifdef EXTENSION_UDP
  54. UA_Server_addNetworkLayer(server, ServerNetworkLayerUDP_new(UA_ConnectionConfig_standard, 16664));
  55. #else
  56. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  57. #endif
  58. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL, .work.methodCall = {.method = testCallback, .data = UA_NULL} };
  59. UA_Server_addRepeatedWorkItem(server, &work, 20000000, UA_NULL); // call every 2 sec
  60. //add a node to the adresspace
  61. UA_Int32 *myInteger = UA_Int32_new();
  62. *myInteger = 42;
  63. UA_QualifiedName myIntegerName;
  64. UA_QUALIFIEDNAME_ASSIGN(myIntegerName, "the answer");
  65. UA_Server_addScalarVariableNode(server, &myIntegerName,
  66. myInteger, UA_NODEID_STATIC(UA_TYPES_IDS[UA_TYPES_INT32],0),
  67. &UA_EXPANDEDNODEID_STATIC(UA_NS0ID_OBJECTSFOLDER,0),
  68. &UA_NODEID_STATIC(UA_NS0ID_ORGANIZES,0));
  69. #ifdef BENCHMARK
  70. UA_UInt32 nodeCount = 500;
  71. char str[15];
  72. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  73. UA_Int32 *data = UA_Int32_new();
  74. UA_QualifiedName *nodeName = UA_QualifiedName_new();
  75. *data = 42;
  76. sprintf(str,"%d",i);
  77. UA_QualifiedName_copycstring(str, nodeName);
  78. UA_Server_addScalarVariableNode(server, nodeName,
  79. data, UA_NODEID_STATIC(UA_TYPES_IDS[UA_TYPES_INT32],0),
  80. &UA_EXPANDEDNODEID_STATIC(UA_NS0ID_OBJECTSFOLDER,0),
  81. &UA_NODEID_STATIC(UA_NS0ID_ORGANIZES,0));
  82. }
  83. #endif
  84. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  85. UA_Server_delete(server);
  86. return retval;
  87. }