server_datasource.c 4.5 KB

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