13ce5372Robert Mustacchi/*
23ce5372Robert Mustacchi * This file and its contents are supplied under the terms of the
33ce5372Robert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
43ce5372Robert Mustacchi * You may only use this file in accordance with the terms of version
53ce5372Robert Mustacchi * 1.0 of the CDDL.
63ce5372Robert Mustacchi *
73ce5372Robert Mustacchi * A full copy of the text of the CDDL should have accompanied this
83ce5372Robert Mustacchi * source.  A copy of the CDDL is also available via the Internet at
93ce5372Robert Mustacchi * http://www.illumos.org/license/CDDL.
103ce5372Robert Mustacchi */
113ce5372Robert Mustacchi
123ce5372Robert Mustacchi/*
133ce5372Robert Mustacchi * Copyright 2020 Oxide Computer Company
143ce5372Robert Mustacchi */
153ce5372Robert Mustacchi
163ce5372Robert Mustacchi/*
173ce5372Robert Mustacchi * Basic ksensor functionality test
183ce5372Robert Mustacchi */
193ce5372Robert Mustacchi
203ce5372Robert Mustacchi#include <sys/types.h>
213ce5372Robert Mustacchi#include <sys/stat.h>
223ce5372Robert Mustacchi#include <fcntl.h>
233ce5372Robert Mustacchi#include <stdlib.h>
243ce5372Robert Mustacchi#include <err.h>
253ce5372Robert Mustacchi#include <sys/sensors.h>
263ce5372Robert Mustacchi#include <unistd.h>
273ce5372Robert Mustacchi#include <errno.h>
283ce5372Robert Mustacchi#include <string.h>
291045e13Robert Mustacchi#include <sys/sysmacros.h>
303ce5372Robert Mustacchi
311045e13Robert Mustacchitypedef struct sensor_test {
321045e13Robert Mustacchi	const char *st_path;
331045e13Robert Mustacchi	uint64_t st_kind;
341045e13Robert Mustacchi	uint32_t st_unit;
351045e13Robert Mustacchi	int32_t st_gran;
361045e13Robert Mustacchi	uint32_t st_prec;
371045e13Robert Mustacchi	int64_t st_val;
381045e13Robert Mustacchi} sensor_test_t;
393ce5372Robert Mustacchi
401045e13Robert Mustacchi/*
411045e13Robert Mustacchi * These values come from the dummy sensors in the ksensor_test driver.
421045e13Robert Mustacchi */
431045e13Robert Mustacchistatic sensor_test_t ksensor_basic_tests[] = {
441045e13Robert Mustacchi	{ "/dev/sensors/test/test.temp.0.1", SENSOR_KIND_TEMPERATURE,
451045e13Robert Mustacchi	    SENSOR_UNIT_CELSIUS, 4, -2, 23 },
461045e13Robert Mustacchi	{ "/dev/sensors/test/test.volt.0.1", SENSOR_KIND_VOLTAGE,
471045e13Robert Mustacchi	    SENSOR_UNIT_VOLTS, 1000, 0, 3300 },
481045e13Robert Mustacchi	{ "/dev/sensors/test/test.current.0.1", SENSOR_KIND_CURRENT,
491045e13Robert Mustacchi	    SENSOR_UNIT_AMPS, 10, 0, 5 },
501045e13Robert Mustacchi};
511045e13Robert Mustacchi
521045e13Robert Mustacchistatic boolean_t
531045e13Robert Mustacchiksensor_basic(sensor_test_t *st)
543ce5372Robert Mustacchi{
553ce5372Robert Mustacchi	sensor_ioctl_kind_t kind;
561045e13Robert Mustacchi	sensor_ioctl_scalar_t scalar;
571045e13Robert Mustacchi	int fd;
583ce5372Robert Mustacchi
591045e13Robert Mustacchi	fd = open(st->st_path, O_RDONLY);
603ce5372Robert Mustacchi	if (fd < 0) {
611045e13Robert Mustacchi		warn("TEST FAILED: failed to open %s", st->st_path);
621045e13Robert Mustacchi		return (B_FALSE);
633ce5372Robert Mustacchi	}
643ce5372Robert Mustacchi
653ce5372Robert Mustacchi	arc4random_buf(&kind, sizeof (kind));
661045e13Robert Mustacchi	arc4random_buf(&scalar, sizeof (scalar));
671045e13Robert Mustacchi
681045e13Robert Mustacchi	if (ioctl(fd, SENSOR_IOCTL_KIND, &kind) != 0) {
691045e13Robert Mustacchi		warn("TEST FAILED: %s: failed to get sensor kind", st->st_path);
701045e13Robert Mustacchi		goto err;
711045e13Robert Mustacchi	}
723ce5372Robert Mustacchi
731045e13Robert Mustacchi	if (kind.sik_kind != st->st_kind) {
741045e13Robert Mustacchi		warnx("TEST FAILED: %s: expected kind %" PRIu64 ", found kind %"
751045e13Robert Mustacchi		    PRIu64, st->st_path, st->st_kind, kind);
761045e13Robert Mustacchi		goto err;
773ce5372Robert Mustacchi	}
783ce5372Robert Mustacchi
791045e13Robert Mustacchi	if (ioctl(fd, SENSOR_IOCTL_SCALAR, &scalar) != 0) {
801045e13Robert Mustacchi		warn("TEST FAILED: %s: failed to read sensor", st->st_path);
811045e13Robert Mustacchi		goto err;
823ce5372Robert Mustacchi	}
833ce5372Robert Mustacchi
841045e13Robert Mustacchi	if (scalar.sis_unit != st->st_unit) {
851045e13Robert Mustacchi		warnx("TEST FAILED: %s: expected unit %" PRIu32 ", but found "
861045e13Robert Mustacchi		    "%" PRIu32, st->st_path, st->st_unit, scalar.sis_unit);
871045e13Robert Mustacchi		goto err;
883ce5372Robert Mustacchi	}
893ce5372Robert Mustacchi
901045e13Robert Mustacchi	if (scalar.sis_gran != st->st_gran) {
911045e13Robert Mustacchi		warnx("TEST FAILED: %s: expected gran %" PRId32 ", but found "
921045e13Robert Mustacchi		    "%" PRId32, st->st_path, st->st_gran, scalar.sis_gran);
931045e13Robert Mustacchi		goto err;
943ce5372Robert Mustacchi	}
953ce5372Robert Mustacchi
961045e13Robert Mustacchi	if (scalar.sis_prec != st->st_prec) {
971045e13Robert Mustacchi		warnx("TEST FAILED: %s: expected prec %" PRIu32 ", but found "
981045e13Robert Mustacchi		    "%" PRIu32, st->st_path, st->st_prec, scalar.sis_prec);
991045e13Robert Mustacchi		goto err;
1003ce5372Robert Mustacchi	}
1013ce5372Robert Mustacchi
1021045e13Robert Mustacchi	if (scalar.sis_value != st->st_val) {
1031045e13Robert Mustacchi		warnx("TEST FAILED: %s: expected value %" PRId64 ", but found "
1041045e13Robert Mustacchi		    "%" PRId64, st->st_path, st->st_val, scalar.sis_value);
1051045e13Robert Mustacchi		goto err;
1063ce5372Robert Mustacchi	}
1073ce5372Robert Mustacchi
1081045e13Robert Mustacchi	return (B_TRUE);
1091045e13Robert Mustacchierr:
1101045e13Robert Mustacchi	(void) close(fd);
1111045e13Robert Mustacchi	return (B_FALSE);
1121045e13Robert Mustacchi}
1131045e13Robert Mustacchi
1141045e13Robert Mustacchiint
1151045e13Robert Mustacchimain(void)
1161045e13Robert Mustacchi{
1171045e13Robert Mustacchi	size_t i;
1181045e13Robert Mustacchi	int ret = EXIT_SUCCESS;
1191045e13Robert Mustacchi
1201045e13Robert Mustacchi	for (i = 0; i < ARRAY_SIZE(ksensor_basic_tests); i++) {
1211045e13Robert Mustacchi		if (!ksensor_basic(&ksensor_basic_tests[i])) {
1221045e13Robert Mustacchi			ret = EXIT_FAILURE;
1231045e13Robert Mustacchi		}
1243ce5372Robert Mustacchi	}
1253ce5372Robert Mustacchi
1263ce5372Robert Mustacchi	return (ret);
1273ce5372Robert Mustacchi}
128