server_rpi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "Error seeking to start of file");
  30. exit(1);
  31. }
  32. if(fscanf(temperatureFile, "%lf", currentTemperature) != 1){
  33. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "Can not parse temperature");
  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. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "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. fflush(ledFile);
  92. pthread_rwlock_unlock(&ledStatusLock);
  93. return UA_STATUSCODE_GOOD;
  94. }
  95. static void stopHandler(int sign) {
  96. UA_LOG_INFO(logger, UA_LOGGERCATEGORY_SERVER, "Received Ctrl-C");
  97. running = 0;
  98. }
  99. int main(int argc, char** argv) {
  100. signal(SIGINT, stopHandler); /* catches ctrl-c */
  101. pthread_rwlock_init(&ledStatusLock, 0);
  102. UA_Server *server = UA_Server_new();
  103. logger = Logger_Stdout_new();
  104. UA_Server_setLogger(server, logger);
  105. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  106. if(!(temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
  107. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "[Linux specific] Can not open temperature file, no temperature node will be added");
  108. } else {
  109. // add node with the datetime data source
  110. UA_DataSource temperatureDataSource = (UA_DataSource)
  111. {.handle = NULL,
  112. .read = readTemperature,
  113. .release = releaseTemperature,
  114. .write = NULL};
  115. UA_QualifiedName dateName;
  116. UA_QUALIFIEDNAME_ASSIGN(dateName, "cpu temperature");
  117. UA_Server_addDataSourceVariableNode(server, temperatureDataSource, &UA_NODEID_NULL, &dateName,
  118. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  119. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  120. }
  121. if ( !(triggerFile = fopen("/sys/class/leds/led0/trigger", "w"))
  122. || !(ledFile = fopen("/sys/class/leds/led0/brightness", "w"))) {
  123. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "[Raspberry Pi specific] Can not open trigger or led file, no led node will be added (try to run server from sudo)");
  124. } else {
  125. //setting led mode to manual
  126. fprintf(triggerFile, "%s", "none");
  127. fflush(triggerFile);
  128. //turning off led initially
  129. fprintf(ledFile, "%s", "1");
  130. fflush(ledFile);
  131. // add node with the device status data source
  132. UA_DataSource ledStatusDataSource = (UA_DataSource)
  133. {.handle = NULL,
  134. .read = readLedStatus,
  135. .release = releaseLedStatus,
  136. .write = writeLedStatus};
  137. UA_QualifiedName statusName;
  138. UA_QUALIFIEDNAME_ASSIGN(statusName, "status led");
  139. UA_Server_addDataSourceVariableNode(server, ledStatusDataSource, &UA_NODEID_NULL, &statusName,
  140. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  141. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  142. }
  143. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  144. UA_Server_delete(server);
  145. if(temperatureFile)
  146. fclose(temperatureFile);
  147. if(triggerFile){
  148. if (fseek(triggerFile, 0, SEEK_SET))
  149. {
  150. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "Error seeking to start of led file");
  151. }
  152. //setting led mode to default
  153. fprintf(triggerFile, "%s", "mmc0");
  154. fclose(triggerFile);
  155. }
  156. if(ledFile){
  157. fclose(ledFile);
  158. }
  159. pthread_rwlock_destroy(&ledStatusLock);
  160. return retval;
  161. }