server_rpi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. FILE* temperatureFile;
  18. UA_Boolean running = 1;
  19. UA_Logger logger;
  20. /*************************/
  21. /* Read-only temperature */
  22. /*************************/
  23. static UA_StatusCode readTemperature(const void *handle, UA_DataValue *value) {
  24. UA_Double* currentTemperature = UA_Double_new();
  25. UA_Int32 i = 0;
  26. if(!currentTemperature)
  27. return UA_STATUSCODE_BADOUTOFMEMORY;
  28. if (fseek(temperatureFile, 0, SEEK_SET))
  29. {
  30. puts("Error seeking to start of file");
  31. exit(1);
  32. }
  33. if(fscanf(temperatureFile, "%d", &i) != 1){
  34. printf("Can not open parse temperature!\n");
  35. exit(1);
  36. }
  37. *currentTemperature = i/1000.0;
  38. value->value.type = &UA_TYPES[UA_TYPES_DOUBLE];
  39. value->value.arrayLength = 1;
  40. value->value.dataPtr = currentTemperature;
  41. value->value.arrayDimensionsSize = -1;
  42. value->value.arrayDimensions = NULL;
  43. value->hasVariant = UA_TRUE;
  44. return UA_STATUSCODE_GOOD;
  45. }
  46. static void releaseTemperature(const void *handle, UA_DataValue *value) {
  47. UA_Double_delete((UA_Double*)value->value.dataPtr);
  48. }
  49. static void stopHandler(int sign) {
  50. printf("Received Ctrl-C\n");
  51. running = 0;
  52. }
  53. int main(int argc, char** argv) {
  54. signal(SIGINT, stopHandler); /* catches ctrl-c */
  55. UA_Server *server = UA_Server_new();
  56. logger = Logger_Stdout_new();
  57. UA_Server_setLogger(server, logger);
  58. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  59. if(!(temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
  60. printf("Can not open temperature file!\n");
  61. exit(1);
  62. }
  63. // add node with the datetime data source
  64. UA_DataSource temperatureDataSource = (UA_DataSource)
  65. {.handle = NULL,
  66. .read = readTemperature,
  67. .release = releaseTemperature,
  68. .write = NULL};
  69. UA_QualifiedName dateName;
  70. UA_QUALIFIEDNAME_ASSIGN(dateName, "cpu temperature");
  71. UA_Server_addDataSourceVariableNode(server, temperatureDataSource, &UA_NODEID_NULL, &dateName,
  72. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  73. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  74. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  75. UA_Server_delete(server);
  76. fclose(temperatureFile);
  77. return retval;
  78. }