opcuaServer.c 3.4 KB

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