opcuaServer.c 3.4 KB

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