server_datasource.c 7.8 KB

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