sm_sensor.c 3.6 KB

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