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_Boolean sourceTimeStamp, 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.type = &UA_TYPES[UA_TYPES_DATETIME];
  26. value->value.arrayLength = 1;
  27. value->value.dataPtr = currentTime;
  28. value->value.arrayDimensionsSize = -1;
  29. value->value.arrayDimensions = NULL;
  30. value->hasVariant = UA_TRUE;
  31. if(sourceTimeStamp) {
  32. value->hasSourceTimestamp = UA_TRUE;
  33. value->sourceTimestamp = *currentTime;
  34. }
  35. return UA_STATUSCODE_GOOD;
  36. }
  37. static void releaseTimeData(const void *handle, UA_DataValue *value) {
  38. UA_DateTime_delete((UA_DateTime*)value->value.dataPtr);
  39. }
  40. /**************************/
  41. /* Read/write data source */
  42. /**************************/
  43. UA_Int32 deviceStatus = 0;
  44. pthread_rwlock_t deviceStatusLock;
  45. static void printDeviceStatus(UA_Server *server, void *data) {
  46. printf("Device Status: %i\n", deviceStatus);
  47. }
  48. static UA_StatusCode readDeviceStatus(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  49. /* In order to reduce blocking time, we could alloc memory for every read
  50. and return a copy of the data. */
  51. pthread_rwlock_rdlock(&deviceStatusLock);
  52. value->value.type = &UA_TYPES[UA_TYPES_INT32];
  53. value->value.arrayLength = 1;
  54. value->value.dataPtr = &deviceStatus;
  55. value->value.arrayDimensionsSize = -1;
  56. value->value.arrayDimensions = NULL;
  57. value->hasVariant = UA_TRUE;
  58. if(sourceTimeStamp) {
  59. value->sourceTimestamp = UA_DateTime_now();
  60. value->hasSourceTimestamp = UA_TRUE;
  61. }
  62. return UA_STATUSCODE_GOOD;
  63. }
  64. static void releaseDeviceStatus(const void *handle, UA_DataValue *value) {
  65. /* If we allocated memory for a specific read, free the content of the
  66. variantdata. */
  67. value->value.arrayLength = -1;
  68. value->value.dataPtr = NULL;
  69. pthread_rwlock_unlock(&deviceStatusLock);
  70. }
  71. static UA_StatusCode writeDeviceStatus(const void *handle, const UA_Variant *data) {
  72. pthread_rwlock_wrlock(&deviceStatusLock);
  73. if(data->dataPtr)
  74. deviceStatus = *(UA_Int32*)data->dataPtr;
  75. pthread_rwlock_unlock(&deviceStatusLock);
  76. return UA_STATUSCODE_GOOD;
  77. }
  78. UA_Boolean running = 1;
  79. static void stopHandler(int sign) {
  80. printf("Received Ctrl-C\n");
  81. running = 0;
  82. }
  83. int main(int argc, char** argv) {
  84. signal(SIGINT, stopHandler); /* catches ctrl-c */
  85. pthread_rwlock_init(&deviceStatusLock, 0);
  86. UA_Server *server = UA_Server_new();
  87. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  88. // add node with the datetime data source
  89. UA_DataSource dateDataSource = (UA_DataSource)
  90. {.handle = NULL,
  91. .read = readTimeData,
  92. .release = releaseTimeData,
  93. .write = NULL};
  94. UA_QualifiedName dateName;
  95. UA_QUALIFIEDNAME_ASSIGN(dateName, "the time");
  96. UA_Server_addDataSourceVariableNode(server, dateDataSource, &UA_NODEID_NULL, &dateName,
  97. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  98. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  99. // print the status every 2 sec
  100. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL,
  101. .work.methodCall = {.method = printDeviceStatus, .data = NULL} };
  102. UA_Server_addRepeatedWorkItem(server, &work, 20000000, NULL);
  103. // add node with the device status data source
  104. UA_DataSource deviceStatusDataSource = (UA_DataSource)
  105. {.handle = NULL,
  106. .read = readDeviceStatus,
  107. .release = releaseDeviceStatus,
  108. .write = writeDeviceStatus};
  109. UA_QualifiedName statusName;
  110. UA_QUALIFIEDNAME_ASSIGN(statusName, "device status");
  111. UA_Server_addDataSourceVariableNode(server, deviceStatusDataSource, &UA_NODEID_NULL, &statusName,
  112. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  113. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  114. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  115. UA_Server_delete(server);
  116. pthread_rwlock_destroy(&deviceStatusLock);
  117. return retval;
  118. }