server_datasource.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "open62541.h"
  6. #include <time.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #define __USE_XOPEN2K
  11. #ifdef UA_MULTITHREADING
  12. #include <pthread.h>
  13. #endif
  14. // provided by the user, implementations available in the /examples folder
  15. #include "logger_stdout.h"
  16. #include "networklayer_tcp.h"
  17. /****************************/
  18. /* Server-related variables */
  19. /****************************/
  20. UA_Boolean running = 1;
  21. UA_Logger logger;
  22. /*************************/
  23. /* Read-only data source */
  24. /*************************/
  25. static UA_StatusCode readTimeData(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  26. UA_DateTime *currentTime = UA_DateTime_new();
  27. if(!currentTime)
  28. return UA_STATUSCODE_BADOUTOFMEMORY;
  29. *currentTime = UA_DateTime_now();
  30. value->value.type = &UA_TYPES[UA_TYPES_DATETIME];
  31. value->value.arrayLength = -1;
  32. value->value.dataPtr = currentTime;
  33. value->value.arrayDimensionsSize = -1;
  34. value->value.arrayDimensions = NULL;
  35. value->hasVariant = UA_TRUE;
  36. if(sourceTimeStamp) {
  37. value->hasSourceTimestamp = UA_TRUE;
  38. value->sourceTimestamp = *currentTime;
  39. }
  40. return UA_STATUSCODE_GOOD;
  41. }
  42. static void releaseTimeData(const void *handle, UA_DataValue *value) {
  43. UA_DateTime_delete((UA_DateTime*)value->value.dataPtr);
  44. }
  45. /*****************************/
  46. /* Read-only CPU temperature */
  47. /* Only on Linux */
  48. /*****************************/
  49. FILE* temperatureFile = NULL;
  50. static UA_StatusCode readTemperature(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  51. UA_Double* currentTemperature = UA_Double_new();
  52. if(!currentTemperature)
  53. return UA_STATUSCODE_BADOUTOFMEMORY;
  54. fseek(temperatureFile, 0, SEEK_SET);
  55. if(fscanf(temperatureFile, "%lf", currentTemperature) != 1){
  56. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "Can not parse temperature");
  57. exit(1);
  58. }
  59. *currentTemperature /= 1000.0;
  60. value->value.type = &UA_TYPES[UA_TYPES_DOUBLE];
  61. value->value.arrayLength = -1;
  62. value->value.dataPtr = currentTemperature;
  63. value->value.arrayDimensionsSize = -1;
  64. value->value.arrayDimensions = NULL;
  65. value->hasVariant = UA_TRUE;
  66. return UA_STATUSCODE_GOOD;
  67. }
  68. static void releaseTemperature(const void *handle, UA_DataValue *value) {
  69. UA_Double_delete((UA_Double*)value->value.dataPtr);
  70. }
  71. /*************************/
  72. /* Read-write status led */
  73. /*************************/
  74. #ifdef UA_MULTITHREADING
  75. pthread_rwlock_t writeLock;
  76. #endif
  77. FILE* triggerFile = NULL;
  78. FILE* ledFile = NULL;
  79. UA_Boolean ledStatus = 0;
  80. static UA_StatusCode readLedStatus(const void *handle, UA_Boolean sourceTimeStamp, UA_DataValue *value) {
  81. /* In order to reduce blocking time, we could alloc memory for every read
  82. and return a copy of the data. */
  83. #ifdef UA_MULTITHREADING
  84. pthread_rwlock_rdlock(&writeLock);
  85. #endif
  86. value->value.type = &UA_TYPES[UA_TYPES_BOOLEAN];
  87. value->value.arrayLength = -1;
  88. value->value.dataPtr = &ledStatus;
  89. value->value.arrayDimensionsSize = -1;
  90. value->value.arrayDimensions = NULL;
  91. value->hasVariant = UA_TRUE;
  92. if(sourceTimeStamp) {
  93. value->sourceTimestamp = UA_DateTime_now();
  94. value->hasSourceTimestamp = UA_TRUE;
  95. }
  96. return UA_STATUSCODE_GOOD;
  97. }
  98. static void releaseLedStatus(const void *handle, UA_DataValue *value) {
  99. /* If we allocated memory for a specific read, free the content of the
  100. variantdata. */
  101. value->value.arrayLength = -1;
  102. value->value.dataPtr = NULL;
  103. #ifdef UA_MULTITHREADING
  104. pthread_rwlock_unlock(&writeLock);
  105. #endif
  106. }
  107. static UA_StatusCode writeLedStatus(const void *handle, const UA_Variant *data) {
  108. #ifdef UA_MULTITHREADING
  109. pthread_rwlock_wrlock(&writeLock);
  110. #endif
  111. if(data->dataPtr)
  112. ledStatus = *(UA_Boolean*)data->dataPtr;
  113. if(triggerFile)
  114. fseek(triggerFile, 0, SEEK_SET);
  115. if(ledFile){
  116. if(ledStatus == 1){
  117. fprintf(ledFile, "%s", "1");
  118. } else {
  119. fprintf(ledFile, "%s", "0");
  120. }
  121. fflush(ledFile);
  122. }
  123. #ifdef UA_MULTITHREADING
  124. pthread_rwlock_unlock(&writeLock);
  125. #endif
  126. return UA_STATUSCODE_GOOD;
  127. }
  128. static void printLedStatus(UA_Server *server, void *data) {
  129. UA_LOG_INFO(logger, UA_LOGGERCATEGORY_SERVER, ledStatus ? "LED is on" : "LED is off");
  130. }
  131. static void stopHandler(int sign) {
  132. printf("Received Ctrl-C\n");
  133. running = 0;
  134. }
  135. int main(int argc, char** argv) {
  136. signal(SIGINT, stopHandler); /* catches ctrl-c */
  137. #ifdef UA_MULTITHREADING
  138. pthread_rwlock_init(&writeLock, 0);
  139. #endif
  140. UA_Server *server = UA_Server_new();
  141. logger = Logger_Stdout_new();
  142. UA_Server_setLogger(server, logger);
  143. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  144. // print the status every 2 sec
  145. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL,
  146. .work.methodCall = {.method = printLedStatus, .data = NULL} };
  147. UA_Server_addRepeatedWorkItem(server, &work, 20000000, NULL);
  148. // add node with the datetime data source
  149. UA_DataSource dateDataSource = (UA_DataSource)
  150. {.handle = NULL,
  151. .read = readTimeData,
  152. .release = releaseTimeData,
  153. .write = NULL};
  154. UA_QualifiedName dateName;
  155. UA_QUALIFIEDNAME_ASSIGN(dateName, "current time");
  156. UA_Server_addDataSourceVariableNode(server, dateDataSource, &UA_NODEID_NULL, &dateName,
  157. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  158. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  159. if(!(temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
  160. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "[Linux specific] Can not open temperature file, no temperature node will be added");
  161. } else {
  162. // add node with the datetime data source
  163. UA_DataSource temperatureDataSource = (UA_DataSource)
  164. {.handle = NULL,
  165. .read = readTemperature,
  166. .release = releaseTemperature,
  167. .write = NULL};
  168. UA_QualifiedName ledName;
  169. UA_QUALIFIEDNAME_ASSIGN(ledName, "cpu temperature");
  170. UA_Server_addDataSourceVariableNode(server, temperatureDataSource, &UA_NODEID_NULL, &ledName,
  171. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  172. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  173. }
  174. if ( !(triggerFile = fopen("/sys/class/leds/led0/trigger", "w"))
  175. || !(ledFile = fopen("/sys/class/leds/led0/brightness", "w"))) {
  176. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "[Raspberry Pi specific] Can not open trigger or LED file (try to run server with sudo if on a Raspberry PI)");
  177. UA_LOG_WARNING(logger, UA_LOGGERCATEGORY_USERLAND, "An LED node will be added but no physical LED will be operated");
  178. } else {
  179. //setting led mode to manual
  180. fprintf(triggerFile, "%s", "none");
  181. fflush(triggerFile);
  182. //turning off led initially
  183. fprintf(ledFile, "%s", "1");
  184. fflush(ledFile);
  185. }
  186. // add node with the LED status data source
  187. UA_DataSource ledStatusDataSource = (UA_DataSource)
  188. {.handle = NULL,
  189. .read = readLedStatus,
  190. .release = releaseLedStatus,
  191. .write = writeLedStatus};
  192. UA_QualifiedName statusName;
  193. UA_QUALIFIEDNAME_ASSIGN(statusName, "status LED");
  194. UA_Server_addDataSourceVariableNode(server, ledStatusDataSource, &UA_NODEID_NULL, &statusName,
  195. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  196. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  197. //start server
  198. UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false
  199. //ctrl-c received -> clean up
  200. UA_Server_delete(server);
  201. if(temperatureFile)
  202. fclose(temperatureFile);
  203. if(triggerFile){
  204. fseek(triggerFile, 0, SEEK_SET);
  205. //setting led mode to default
  206. fprintf(triggerFile, "%s", "mmc0");
  207. fclose(triggerFile);
  208. }
  209. if(ledFile){
  210. fclose(ledFile);
  211. }
  212. #ifdef UA_MULTITHREADING
  213. pthread_rwlock_destroy(&writeLock);
  214. #endif
  215. return retval;
  216. }