sm_sensor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2004 Matthew Gream
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get sensor data from the kernel and return in symon_buf as
  32. *
  33. * num : value
  34. */
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <fcntl.h>
  40. #include <sys/envsys.h>
  41. #include <sys/ioctl.h>
  42. #include "error.h"
  43. #include "symon.h"
  44. /* Globals for this module start with sn_ */
  45. static int sn_dev = -1;
  46. void
  47. privinit_sensor()
  48. {
  49. if (sn_dev == -1 && (sn_dev = open("/dev/sysmon", O_RDONLY)) == -1) {
  50. warning("could not open \"/dev/sysmon\", %.200s", strerror(errno));
  51. }
  52. }
  53. void
  54. init_sensor(struct stream *st)
  55. {
  56. if (sn_dev == -1) {
  57. privinit_sensor();
  58. }
  59. st->parg.sn = (strtol(st->arg, NULL, 10) & SYMON_SENSORMASK);
  60. info("started module sensors(%.200s)", st->arg);
  61. }
  62. int
  63. get_sensor(char *symon_buf, int maxlen, struct stream *st)
  64. {
  65. double t;
  66. envsys_basic_info_t e_info;
  67. envsys_tre_data_t e_data;
  68. e_info.sensor = st->parg.sn;
  69. if (ioctl(sn_dev, ENVSYS_GTREINFO, &e_info) == -1) {
  70. warning("%s:%d: sensor can't get sensor info %.200s -- %.200s",
  71. __FILE__, __LINE__, st->arg, strerror(errno));
  72. return 0;
  73. }
  74. if (!(e_info.validflags & ENVSYS_FVALID)) {
  75. warning("%s:%d: sensor info is invalid %.200s -- %.200s",
  76. __FILE__, __LINE__, st->arg, strerror(errno));
  77. return 0;
  78. }
  79. e_data.sensor = st->parg.sn;
  80. if (ioctl(sn_dev, ENVSYS_GTREDATA, &e_data) == -1) {
  81. warning("%s:%d: sensor can't get sensor data %.200s -- %.200s",
  82. __FILE__, __LINE__, st->arg, strerror(errno));
  83. return 0;
  84. }
  85. if (!(e_data.validflags & ENVSYS_FCURVALID)) {
  86. warning("%s:%d: sensor data is invalid %.200s -- %.200s",
  87. __FILE__, __LINE__, st->arg, strerror(errno));
  88. return 0;
  89. }
  90. switch (e_data.units) {
  91. case ENVSYS_INDICATOR:
  92. t = (double) (e_data.cur.data_us ? 1.0 : 0.0);
  93. break;
  94. case ENVSYS_INTEGER:
  95. t = (double) (e_data.cur.data_s);
  96. break;
  97. case ENVSYS_STEMP:
  98. t = (double) (e_data.cur.data_s / 1000.0 / 1000.0) - 273.16;
  99. break;
  100. case ENVSYS_SFANRPM:
  101. t = (double) (e_data.cur.data_us);
  102. break;
  103. default: /* generic - includes volts/etc */
  104. t = (double) (e_data.cur.data_s / 1000.0 / 1000.0);
  105. break;
  106. }
  107. return snpack(symon_buf, maxlen, st->arg, MT_SENSOR, t);
  108. }