server_datasource.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <time.h>
  6. #include "ua_types.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #define __USE_XOPEN2K
  11. #include <pthread.h>
  12. // provided by the open62541 lib
  13. #include "ua_server.h"
  14. // provided by the user, implementations available in the /examples folder
  15. #include "logger_stdout.h"
  16. #include "networklayer_tcp.h"
  17. /*************************/
  18. /* Read-only data source */
  19. /*************************/
  20. static UA_StatusCode readTimeData(const void *handle, UA_DataValue *value) {
  21. UA_DateTime *currentTime = UA_DateTime_new();
  22. if(!currentTime)
  23. return UA_STATUSCODE_BADOUTOFMEMORY;
  24. *currentTime = UA_DateTime_now();
  25. value->value.arrayLength = 1;
  26. value->value.dataPtr = currentTime;
  27. value->value.arrayDimensionsSize = -1;
  28. value->value.arrayDimensions = NULL;
  29. value->hasVariant = UA_TRUE;
  30. return UA_STATUSCODE_GOOD;
  31. }
  32. static void releaseTimeData(const void *handle, UA_DataValue *value) {
  33. UA_DateTime_delete((UA_DateTime*)value->value.dataPtr);
  34. }
  35. static UA_StatusCode writeTimeData(const void *handle, const UA_Variant *data) {
  36. return UA_STATUSCODE_BADINTERNALERROR;
  37. }
  38. /**************************/
  39. /* Read/write data source */
  40. /**************************/
  41. UA_Int32 deviceStatus = 0;
  42. pthread_rwlock_t deviceStatusLock;
  43. static void printDeviceStatus(UA_Server *server, void *data) {
  44. printf("Device Status: %i\n", deviceStatus);
  45. }
  46. static UA_StatusCode readDeviceStatus(const void *handle, UA_DataValue *value) {
  47. /* In order to reduce blocking time, we could alloc memory for every read
  48. and return a copy of the data. */
  49. pthread_rwlock_rdlock(&deviceStatusLock);
  50. value->value.arrayLength = 1;
  51. value->value.dataPtr = &deviceStatus;
  52. value->value.arrayDimensionsSize = -1;
  53. value->value.arrayDimensions = NULL;
  54. value->hasVariant = UA_TRUE;
  55. value->sourceTimestamp = UA_DateTime_now();
  56. value->hasSourceTimestamp = UA_TRUE;
  57. return UA_STATUSCODE_GOOD;
  58. }
  59. static void releaseDeviceStatus(const void *handle, UA_DataValue *value) {
  60. /* If we allocated memory for a specific read, free the content of the
  61. variantdata. */
  62. value->value.arrayLength = -1;
  63. value->value.dataPtr = NULL;
  64. pthread_rwlock_unlock(&deviceStatusLock);
  65. }
  66. static UA_StatusCode writeDeviceStatus(const void *handle, const UA_Variant *data) {
  67. pthread_rwlock_wrlock(&deviceStatusLock);
  68. if(data->dataPtr)
  69. deviceStatus = *(UA_Int32*)data->dataPtr;
  70. pthread_rwlock_unlock(&deviceStatusLock);
  71. return UA_STATUSCODE_GOOD;
  72. }
  73. UA_Boolean running = 1;
  74. static void stopHandler(int sign) {
  75. printf("Received Ctrl-C\n");
  76. running = 0;
  77. }
  78. int main(int argc, char** argv) {
  79. signal(SIGINT, stopHandler); /* catches ctrl-c */
  80. pthread_rwlock_init(&deviceStatusLock, 0);
  81. UA_Server *server = UA_Server_new();
  82. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  83. // add node with the datetime data source
  84. UA_Variant *dateVariant = UA_Variant_new();
  85. dateVariant->storage.datasource = (UA_DataSource)
  86. {.handle = UA_NULL,
  87. .read = readTimeData,
  88. .release = releaseTimeData,
  89. .write = writeTimeData};
  90. dateVariant->type = &UA_TYPES[UA_TYPES_DATETIME];
  91. UA_QualifiedName dateName;
  92. UA_QUALIFIEDNAME_ASSIGN(dateName, "the time");
  93. UA_Server_addVariableNode(server, dateVariant, &UA_NODEID_NULL, &dateName,
  94. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  95. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  96. // print the status every 2 sec
  97. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL, .work.methodCall = {.method = printDeviceStatus, .data = UA_NULL} };
  98. UA_Server_addRepeatedWorkItem(server, &work, 20000000, UA_NULL);
  99. // add node with the device status data source
  100. UA_Variant *statusVariant = UA_Variant_new();
  101. statusVariant->storageType = UA_VARIANT_DATASOURCE;
  102. statusVariant->storage.datasource = (UA_VariantDataSource)
  103. {.handle = UA_NULL,
  104. .read = readDeviceStatus,
  105. .release = releaseDeviceStatus,
  106. .write = writeDeviceStatus};
  107. statusVariant->type = &UA_TYPES[UA_TYPES_INT32];
  108. UA_QualifiedName statusName;
  109. UA_QUALIFIEDNAME_ASSIGN(statusName, "device status");
  110. UA_Server_addVariableNode(server, statusVariant, &UA_NODEID_NULL, &statusName,
  111. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  112. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  113. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  114. UA_Server_delete(server);
  115. pthread_rwlock_destroy(&deviceStatusLock);
  116. return retval;
  117. }