opcuaServer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "../src/server/ua_nodestore_interface.h"
  16. #include "../src/server/ua_namespace_manager.h"
  17. #include "../src/server/nodestore/open62541_nodestore.h"
  18. UA_Boolean running = 1;
  19. void stopHandler(int sign) {
  20. running = 0;
  21. }
  22. void serverCallback(UA_Server *server) {
  23. // printf("does whatever servers do\n");
  24. }
  25. UA_ByteString loadCertificate() {
  26. UA_ByteString certificate = UA_STRING_NULL;
  27. FILE *fp = NULL;
  28. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  29. fp=fopen("localhost.der", "rb");
  30. if(!fp) {
  31. errno = 0; // otherwise we think sth went wrong on the tcp socket level
  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. fseek(fp, 0, SEEK_SET);
  38. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  39. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  40. fclose(fp);
  41. return certificate;
  42. }
  43. int main(int argc, char** argv) {
  44. signal(SIGINT, stopHandler); /* catches ctrl-c */
  45. UA_Server *server;
  46. UA_String endpointUrl;
  47. UA_String_copycstring("no endpoint url",&endpointUrl);
  48. UA_NodeStore nodeStore;
  49. open62541NodeStore *myNodeStore;
  50. open62541NodeStore_new(&myNodeStore);
  51. open62541NodeStore_setNodeStore(myNodeStore);
  52. UA_NodeStore_registerReadNodesOperation(&nodeStore,open62541NodeStore_ReadNodes);
  53. UA_NodeStore_registerBrowseNodesOperation(&nodeStore,open62541NodeStore_BrowseNodes);
  54. UA_NodeStore_registerAddNodesOperation(&nodeStore,open62541NodeStore_AddNodes);
  55. UA_NodeStore_registerWriteNodesOperation(&nodeStore,open62541NodeStore_WriteNodes);
  56. //register more operations/ services here
  57. UA_ByteString certificate = loadCertificate();
  58. server = UA_Server_new(&endpointUrl, &certificate, &nodeStore);
  59. UA_Int32 myInteger = 42;
  60. UA_QualifiedName *myIntegerName;
  61. myIntegerName = UA_QualifiedName_new();
  62. UA_QualifiedName_copycstring("the answer is",myIntegerName);
  63. UA_ExpandedNodeId parentNodeId;
  64. parentNodeId.namespaceUri.length = 0;
  65. parentNodeId.nodeId = UA_NODEIDS[UA_OBJECTSFOLDER];
  66. UA_Server_addScalarVariableNode(server, myIntegerName, (void*)&myInteger, &UA_TYPES[UA_INT32],
  67. &parentNodeId, (UA_NodeId*)&UA_NODEIDS[UA_HASCOMPONENT]);
  68. #ifdef BENCHMARK
  69. UA_UInt32 nodeCount = 500;
  70. UA_Int32 data = 42;
  71. char str[15];
  72. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  73. UA_VariableNode *tmpNode = UA_VariableNode_new();
  74. sprintf(str,"%d",i);
  75. UA_QualifiedName_copycstring(str,&tmpNode->browseName);
  76. UA_LocalizedText_copycstring(str,&tmpNode->displayName);
  77. UA_LocalizedText_copycstring("integer value", &tmpNode->description);
  78. tmpNode->nodeId.identifier.numeric = 19000+i;
  79. tmpNode->nodeClass = UA_NODECLASS_VARIABLE;
  80. //tmpNode->valueRank = -1;
  81. tmpNode->value.vt = &UA_TYPES[UA_INT32];
  82. tmpNode->value.storage.data.dataPtr = &data;
  83. tmpNode->value.storageType = UA_VARIANT_DATA_NODELETE;
  84. tmpNode->value.storage.data.arrayLength = 1;
  85. UA_Server_addNode(server, (UA_Node**)&tmpNode, &UA_NODEIDS[UA_OBJECTSFOLDER], &UA_NODEIDS[UA_HASCOMPONENT]);
  86. }
  87. #endif
  88. #define PORT 16664
  89. NetworklayerTCP* nl = NetworklayerTCP_new(UA_ConnectionConfig_standard, PORT);
  90. printf("Server started, connect to to opc.tcp://127.0.0.1:%i\n", PORT);
  91. struct timeval callback_interval = {1, 0}; // 1 second
  92. UA_Int32 retval = NetworkLayerTCP_run(nl, server, callback_interval, serverCallback, &running);
  93. UA_Server_delete(server);
  94. NetworklayerTCP_delete(nl);
  95. UA_String_deleteMembers(&endpointUrl);
  96. return retval == UA_STATUSCODE_GOOD ? 0 : retval;
  97. }