server_rpi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. UA_Boolean running = 1;
  18. UA_Logger logger;
  19. /*************************/
  20. /* Read-only temperature */
  21. /*************************/
  22. FILE* temperatureFile = NULL;
  23. static UA_StatusCode readTemperature(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  24. UA_Double* currentTemperature = UA_Double_new();
  25. if(!currentTemperature)
  26. return UA_STATUSCODE_BADOUTOFMEMORY;
  27. if (fseek(temperatureFile, 0, SEEK_SET))
  28. {
  29. puts("Error seeking to start of file");
  30. exit(1);
  31. }
  32. if(fscanf(temperatureFile, "%lf", currentTemperature) != 1){
  33. printf("Can not parse temperature!\n");
  34. exit(1);
  35. }
  36. *currentTemperature /= 1000.0;
  37. value->value.type = &UA_TYPES[UA_TYPES_DOUBLE];
  38. value->value.arrayLength = 1;
  39. value->value.dataPtr = currentTemperature;
  40. value->value.arrayDimensionsSize = -1;
  41. value->value.arrayDimensions = NULL;
  42. value->hasVariant = UA_TRUE;
  43. return UA_STATUSCODE_GOOD;
  44. }
  45. static void releaseTemperature(const void *handle, UA_DataValue *value) {
  46. UA_Double_delete((UA_Double*)value->value.dataPtr);
  47. }
  48. /*************************/
  49. /* Read-write status led */
  50. /*************************/
  51. pthread_rwlock_t ledStatusLock;
  52. FILE* triggerFile = NULL;
  53. FILE* ledFile = NULL;
  54. UA_Boolean ledStatus = 0;
  55. static UA_StatusCode readLedStatus(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  56. /* In order to reduce blocking time, we could alloc memory for every read
  57. and return a copy of the data. */
  58. pthread_rwlock_rdlock(&ledStatusLock);
  59. value->value.type = &UA_TYPES[UA_TYPES_BOOLEAN];
  60. value->value.arrayLength = 1;
  61. value->value.dataPtr = &ledStatus;
  62. value->value.arrayDimensionsSize = -1;
  63. value->value.arrayDimensions = NULL;
  64. value->hasVariant = UA_TRUE;
  65. if(sourceTimeStamp) {
  66. value->sourceTimestamp = UA_DateTime_now();
  67. value->hasSourceTimestamp = UA_TRUE;
  68. }
  69. return UA_STATUSCODE_GOOD;
  70. }
  71. static void releaseLedStatus(const void *handle, UA_DataValue *value) {
  72. /* If we allocated memory for a specific read, free the content of the
  73. variantdata. */
  74. value->value.arrayLength = -1;
  75. value->value.dataPtr = NULL;
  76. pthread_rwlock_unlock(&ledStatusLock);
  77. }
  78. static UA_StatusCode writeLedStatus(const void *handle, const UA_Variant *data) {
  79. pthread_rwlock_wrlock(&ledStatusLock);
  80. if(data->dataPtr)
  81. ledStatus = *(UA_Boolean*)data->dataPtr;
  82. if (fseek(triggerFile, 0, SEEK_SET))
  83. {
  84. puts("Error seeking to start of led file");
  85. }
  86. if(ledStatus == 1){
  87. fprintf(ledFile, "%s", "1");
  88. } else {
  89. fprintf(ledFile, "%s", "0");
  90. }
  91. pthread_rwlock_unlock(&ledStatusLock);
  92. return UA_STATUSCODE_GOOD;
  93. }
  94. static void stopHandler(int sign) {
  95. printf("Received Ctrl-C\n");
  96. running = 0;
  97. }
  98. int main(int argc, char** argv) {
  99. signal(SIGINT, stopHandler); /* catches ctrl-c */
  100. pthread_rwlock_init(&ledStatusLock, 0);
  101. UA_Server *server = UA_Server_new();
  102. logger = Logger_Stdout_new();
  103. UA_Server_setLogger(server, logger);
  104. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  105. if(!(temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
  106. printf("Can not open temperature file, no temperature node will be added\n");
  107. } else {
  108. // add node with the datetime data source
  109. UA_DataSource temperatureDataSource = (UA_DataSource)
  110. {.handle = NULL,
  111. .read = readTemperature,
  112. .release = releaseTemperature,
  113. .write = NULL};
  114. UA_QualifiedName dateName;
  115. UA_QUALIFIEDNAME_ASSIGN(dateName, "cpu temperature");
  116. UA_Server_addDataSourceVariableNode(server, temperatureDataSource, &UA_NODEID_NULL, &dateName,
  117. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  118. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  119. }
  120. if ( !(triggerFile = fopen("/sys/class/leds/led0/trigger", "w"))
  121. || !(ledFile = fopen("/sys/class/leds/led0/brightness", "w"))) {
  122. printf("Can not open trigger or led file, no led node will be added\n");
  123. } else {
  124. //setting led mode to manual
  125. fprintf(triggerFile, "%s", "none");
  126. //turning off led initially
  127. fprintf(ledFile, "%s", "1");
  128. // add node with the device status data source
  129. UA_DataSource ledStatusDataSource = (UA_DataSource)
  130. {.handle = NULL,
  131. .read = readLedStatus,
  132. .release = releaseLedStatus,
  133. .write = writeLedStatus};
  134. UA_QualifiedName statusName;
  135. UA_QUALIFIEDNAME_ASSIGN(statusName, "status led");
  136. UA_Server_addDataSourceVariableNode(server, ledStatusDataSource, &UA_NODEID_NULL, &statusName,
  137. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  138. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  139. }
  140. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  141. UA_Server_delete(server);
  142. if(temperatureFile)
  143. fclose(temperatureFile);
  144. if(triggerFile){
  145. if (fseek(triggerFile, 0, SEEK_SET))
  146. {
  147. puts("Error seeking to start of led file");
  148. }
  149. //setting led mode to default
  150. fprintf(triggerFile, "%s", "mmc0");
  151. fclose(triggerFile);
  152. }
  153. if(ledFile){
  154. fclose(ledFile);
  155. }
  156. pthread_rwlock_destroy(&ledStatusLock);
  157. return retval;
  158. }