opcuaServer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdlib.h>
  7. #ifdef WIN32
  8. #include "winsock2.h"
  9. #else
  10. #include <sys/mman.h>
  11. #include <sys/wait.h>
  12. #include <unistd.h>
  13. #include <sys/time.h>
  14. #endif
  15. #include <sys/types.h>
  16. #include <fcntl.h>
  17. #include <signal.h>
  18. #include <errno.h> // errno, EINTR
  19. #include "ua_server.h"
  20. #include "logger_stdout.h"
  21. #include "networklayer_tcp.h"
  22. #include "ua_namespace_0.h"
  23. UA_Boolean running = UA_TRUE;
  24. void stopHandler(int sign) {
  25. running = UA_FALSE;
  26. }
  27. void serverCallback(UA_Server *server) {
  28. // printf("does whatever servers do\n");
  29. }
  30. UA_ByteString loadCertificate() {
  31. UA_ByteString certificate = UA_STRING_NULL;
  32. FILE *fp = UA_NULL;
  33. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  34. fp=fopen("localhost.der", "rb");
  35. if(!fp) {
  36. errno = 0; // otherwise we think sth went wrong on the tcp socket level
  37. return certificate;
  38. }
  39. fseek(fp, 0, SEEK_END);
  40. certificate.length = ftell(fp);
  41. certificate.data = malloc(certificate.length*sizeof(UA_Byte));
  42. fseek(fp, 0, SEEK_SET);
  43. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  44. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  45. fclose(fp);
  46. return certificate;
  47. }
  48. int main(int argc, char** argv) {
  49. signal(SIGINT, stopHandler); /* catches ctrl-c */
  50. UA_Server server;
  51. UA_String endpointUrl;
  52. UA_String_copycstring("no endpoint url",&endpointUrl);
  53. UA_Server_init(&server, &endpointUrl);
  54. Logger_Stdout_init(&server.logger);
  55. server.serverCertificate = loadCertificate();
  56. #ifdef BENCHMARK
  57. UA_UInt32 nodeCount = 500;
  58. UA_VariableNode *tmpNode;
  59. UA_ExpandedNodeId objectNodeId;
  60. UA_ExpandedNodeId_init(&objectNodeId);
  61. objectNodeId.nodeId.identifier.numeric = 85;
  62. UA_NodeId hasComponentReferenceId;
  63. UA_NodeId_init(&hasComponentReferenceId);
  64. hasComponentReferenceId.identifier.numeric = 47;
  65. UA_Int32 data = 42;
  66. char str[15];
  67. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  68. UA_VariableNode_new(&tmpNode);
  69. sprintf(str,"%d",i);
  70. UA_QualifiedName_copycstring(str,&tmpNode->browseName);
  71. UA_LocalizedText_copycstring(str,&tmpNode->displayName);
  72. UA_LocalizedText_copycstring("integer value", &tmpNode->description);
  73. tmpNode->nodeId.identifier.numeric = 19000+i;
  74. tmpNode->nodeClass = UA_NODECLASS_VARIABLE;
  75. //tmpNode->valueRank = -1;
  76. tmpNode->value.vt = &UA_[UA_INT32];
  77. tmpNode->value.storage.data.dataPtr = &data;
  78. tmpNode->value.storageType = UA_VARIANT_DATA_NODELETE;
  79. tmpNode->value.storage.data.arrayLength = 1;
  80. UA_Server_addNode(&server, (UA_Node**)&tmpNode, &objectNodeId, &hasComponentReferenceId);
  81. }
  82. #endif
  83. #define PORT 16664
  84. NetworklayerTCP* nl;
  85. NetworklayerTCP_new(&nl, UA_ConnectionConfig_standard, PORT);
  86. printf("Server started, connect to to opc.tcp://127.0.0.1:%i\n", PORT);
  87. struct timeval callback_interval = {1, 0}; // 1 second
  88. UA_Int32 retval = NetworkLayerTCP_run(nl, &server, callback_interval,
  89. serverCallback, &running);
  90. UA_Server_deleteMembers(&server);
  91. NetworklayerTCP_delete(nl);
  92. UA_String_deleteMembers(&endpointUrl);
  93. return retval == UA_SUCCESS ? 0 : retval;
  94. }