opcuaServer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifdef MULTITHREADING
  16. #include <urcu.h>
  17. #endif
  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. #ifdef MULTITHREADING
  45. rcu_register_thread();
  46. #endif
  47. signal(SIGINT, stopHandler); /* catches ctrl-c */
  48. UA_String endpointUrl;
  49. UA_String_copycstring("opc.tcp://localhost:16664",&endpointUrl);
  50. UA_ByteString certificate = loadCertificate();
  51. UA_Server *server = UA_Server_new(&endpointUrl, &certificate);
  52. //add a node to the adresspace
  53. UA_Int32 *myInteger = malloc(sizeof(UA_Int32));
  54. *myInteger = 42;
  55. UA_QualifiedName myIntegerName;
  56. UA_QualifiedName_copycstring("the answer is",&myIntegerName);
  57. UA_Server_addScalarVariableNode(server, &myIntegerName, myInteger, &UA_TYPES[UA_INT32],
  58. &UA_EXPANDEDNODEIDS[UA_OBJECTSFOLDER], &UA_NODEIDS[UA_ORGANIZES]);
  59. UA_QualifiedName_deleteMembers(&myIntegerName);
  60. #ifdef BENCHMARK
  61. UA_UInt32 nodeCount = 500;
  62. UA_Int32 data = 42;
  63. char str[15];
  64. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  65. UA_VariableNode *tmpNode = UA_VariableNode_new();
  66. sprintf(str,"%d",i);
  67. UA_QualifiedName_copycstring(str,&tmpNode->browseName);
  68. UA_LocalizedText_copycstring(str,&tmpNode->displayName);
  69. UA_LocalizedText_copycstring("integer value", &tmpNode->description);
  70. tmpNode->nodeId.identifier.numeric = 19000+i;
  71. tmpNode->nodeClass = UA_NODECLASS_VARIABLE;
  72. //tmpNode->valueRank = -1;
  73. tmpNode->value.vt = &UA_TYPES[UA_INT32];
  74. tmpNode->value.storage.data.dataPtr = &data;
  75. tmpNode->value.storageType = UA_VARIANT_DATA_NODELETE;
  76. tmpNode->value.storage.data.arrayLength = 1;
  77. UA_Server_addNode(server, (const UA_Node**)&tmpNode, &UA_EXPANDEDNODEIDS[UA_OBJECTSFOLDER],
  78. &UA_NODEIDS[UA_HASCOMPONENT]);
  79. }
  80. #endif
  81. #define PORT 16664
  82. NetworklayerTCP* nl = NetworklayerTCP_new(UA_ConnectionConfig_standard, PORT);
  83. printf("Server started, connect to to opc.tcp://127.0.0.1:%i\n", PORT);
  84. struct timeval callback_interval = {1, 0}; // 1 second
  85. UA_Int32 retval = NetworkLayerTCP_run(nl, server, callback_interval, serverCallback, &running);
  86. UA_Server_delete(server);
  87. NetworklayerTCP_delete(nl);
  88. UA_String_deleteMembers(&endpointUrl);
  89. #ifdef MULTITHREADING
  90. rcu_unregister_thread();
  91. #endif
  92. return retval;
  93. }