#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/sensors.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <limits.h>
#include <strings.h>
static uint32_t timeout = 1500;
static uint32_t skew = 10;
int
main(int argc, const char *argv[])
{
int nsensors = 0, ninst = 0;
uint32_t ms;
if (argc != 3) {
errx(EXIT_FAILURE, "missing required args: ninstance, "
"nsensors");
}
nsensors = atoi(argv[1]);
ninst = atoi(argv[2]);
if (nsensors <= 0 || ninst <= 0) {
errx(EXIT_FAILURE, "got bad values for some of nesnsors (%u), "
"ninst (%u)", nsensors, ninst);
}
for (;;) {
int fd;
char buf[PATH_MAX];
uint32_t sens, inst;
struct timespec ts;
sensor_ioctl_scalar_t scalar;
sens = arc4random_uniform(nsensors);
inst = arc4random_uniform(ninst) + 1;
(void) snprintf(buf, sizeof (buf),
"/dev/sensors/test/test.temp.%u.%u", sens, inst);
fd = open(buf, O_RDONLY);
if (fd < 0) {
warn("failed to open %s", buf);
goto wait;
}
bzero(&scalar, sizeof (scalar));
if (ioctl(fd, SENSOR_IOCTL_SCALAR, &scalar) != 0) {
warn("failed to get sensor temperature on %s", buf);
}
if (scalar.sis_unit != SENSOR_UNIT_CELSIUS) {
warnx("data from sensor %s looks off, expected sensor "
"to indicate Celsius, but instead %u", buf,
scalar.sis_unit);
}
(void) close(fd);
wait:
ms = arc4random_uniform(timeout) + skew;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * (NANOSEC / MILLISEC);
(void) nanosleep(&ts, NULL);
}
return (0);
}