temperature.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* See LICENSE file for copyright and license details. */
  2. #include <err.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <dirent.h>
  7. #include <limits.h>
  8. #include <unistd.h>
  9. #include "../lib/util.h"
  10. #include "../aslstatus.h"
  11. #define MAX_NAME 16
  12. #define TEMP_FILE "temp1_input"
  13. #define SYS_CLASS "/sys/class/hwmon"
  14. static const char *GOOD_NAMES[] = {
  15. "coretemp",
  16. "acpitz",
  17. "k10temp",
  18. "fam15h_power",
  19. };
  20. void
  21. temp(char *out,
  22. const char *device,
  23. uint32_t __unused _i,
  24. static_data_t *static_data)
  25. {
  26. DIR *d;
  27. struct dirent *dp;
  28. uint8_t i, found = 0;
  29. size_t readed;
  30. int name_fd;
  31. char name[MAX_NAME];
  32. char buf[JU_STR_SIZE + 3 /* zeros at the end */];
  33. int *fd = static_data->data;
  34. if (!static_data->cleanup) static_data->cleanup = fd_cleanup;
  35. if (*fd > 0) {
  36. if (!fd_rewind(*fd)) ERRRET(out);
  37. goto get_temp;
  38. }
  39. if (!!device) {
  40. if (!sysfs_fd_or_rewind(fd, SYS_CLASS, device, TEMP_FILE))
  41. ERRRET(out);
  42. goto get_temp;
  43. }
  44. if (!(d = opendir(SYS_CLASS))) {
  45. warn("opendir(%s)", SYS_CLASS);
  46. ERRRET(out);
  47. }
  48. while ((dp = readdir(d))) {
  49. if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
  50. continue;
  51. if ((name_fd = sysfs_fd(SYS_CLASS, dp->d_name, "name")) == -1)
  52. continue;
  53. if (!eread_ret(readed, name_fd, WITH_LEN(name))) {
  54. eclose(name_fd);
  55. continue;
  56. }
  57. /* remove last new line char */
  58. name[--readed] = '\0';
  59. for (i = 0; i < LEN(GOOD_NAMES); i++) {
  60. if (!strncmp(name, GOOD_NAMES[i], readed)) {
  61. if ((*fd = sysfs_fd(SYS_CLASS,
  62. dp->d_name,
  63. TEMP_FILE))
  64. == -1)
  65. ERRRET(out);
  66. found = !0;
  67. goto end_loop;
  68. }
  69. }
  70. }
  71. end_loop:
  72. closedir(d);
  73. if (!!found) {
  74. get_temp:
  75. if (!eread_ret(readed, *fd, WITH_LEN(buf))) ERRRET(out);
  76. if (readed > 4)
  77. readed -= 4; /* 3 zeros and '\n' at the end */
  78. else
  79. buf[(readed = 1) - 1] = '0';
  80. buf[readed] = '\0';
  81. bprintf(out, "%s", buf);
  82. }
  83. }