server_datasource.c 7.6 KB

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