opcuaServer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 EXTENSION_UDP
  16. #include "networklayer_udp.h"
  17. #endif
  18. UA_Boolean running = 1;
  19. static void stopHandler(int sign) {
  20. printf("Received Ctrl-C\n");
  21. running = 0;
  22. }
  23. static UA_ByteString loadCertificate(void) {
  24. UA_ByteString certificate = UA_STRING_NULL;
  25. FILE *fp = NULL;
  26. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  27. fp=fopen("localhost.der", "rb");
  28. if(!fp) {
  29. errno = 0; // we read errno also from the tcp layer...
  30. return certificate;
  31. }
  32. fseek(fp, 0, SEEK_END);
  33. certificate.length = ftell(fp);
  34. certificate.data = malloc(certificate.length*sizeof(UA_Byte));
  35. if(!certificate.data)
  36. return certificate;
  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. static void testCallback(UA_Server *server, void *data) {
  44. printf("testcallback\n");
  45. }
  46. int main(int argc, char** argv) {
  47. signal(SIGINT, stopHandler); /* catches ctrl-c */
  48. UA_Server *server = UA_Server_new();
  49. UA_Server_setServerCertificate(server, loadCertificate());
  50. #ifdef EXTENSION_UDP
  51. UA_Server_addNetworkLayer(server, NetworkLayerUDP_new(UA_ConnectionConfig_standard, 16664));
  52. #else
  53. UA_Server_addNetworkLayer(server, NetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  54. #endif
  55. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL, .work.methodCall = {.method = testCallback, .data = UA_NULL} };
  56. UA_Server_addRepeatedWorkItem(server, &work, 20000000, UA_NULL); // call every 2 sec
  57. //add a node to the adresspace
  58. UA_Int32 *myInteger = UA_Int32_new();
  59. *myInteger = 42;
  60. UA_QualifiedName myIntegerName;
  61. UA_QUALIFIEDNAME_STATIC(myIntegerName, "the answer");
  62. UA_Server_addScalarVariableNode(server, &myIntegerName,
  63. myInteger, &UA_TYPES[UA_INT32],
  64. &UA_EXPANDEDNODEIDS[UA_OBJECTSFOLDER],
  65. &UA_NODEIDS[UA_ORGANIZES]);
  66. #ifdef BENCHMARK
  67. UA_UInt32 nodeCount = 500;
  68. UA_Int32 data = 42;
  69. char str[15];
  70. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  71. UA_VariableNode *tmpNode = UA_VariableNode_new();
  72. sprintf(str,"%d",i);
  73. UA_QualifiedName_copycstring(str,&tmpNode->browseName);
  74. UA_LocalizedText_copycstring(str,&tmpNode->displayName);
  75. UA_LocalizedText_copycstring("integer value", &tmpNode->description);
  76. tmpNode->nodeId.identifier.numeric = 19000+i;
  77. tmpNode->nodeClass = UA_NODECLASS_VARIABLE;
  78. //tmpNode->valueRank = -1;
  79. tmpNode->value.vt = &UA_TYPES[UA_INT32];
  80. tmpNode->value.storage.data.dataPtr = &data;
  81. tmpNode->value.storageType = UA_VARIANT_DATA_NODELETE;
  82. tmpNode->value.storage.data.arrayLength = 1;
  83. UA_Server_addNode(server, (const UA_Node**)&tmpNode,
  84. &UA_EXPANDEDNODEIDS[UA_OBJECTSFOLDER],
  85. &UA_NODEIDS[UA_HASCOMPONENT]);
  86. }
  87. #endif
  88. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  89. UA_Server_delete(server);
  90. return retval;
  91. }