// SPDX-License-Identifier: GPL-2.0
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "drhyme_sensor_uapi.h"

static void die(const char *what)
{
	fprintf(stderr, "%s: %s\n", what, strerror(errno));
	exit(1);
}

int main(int argc, char **argv)
{
	const char *path = argc > 1 ? argv[1] : "/dev/drhyme_sensor0";
	struct drhyme_sensor_config config = { .config = 42 };
	struct drhyme_sensor_status status;
	int fd;

	fd = open(path, O_RDONLY);
	if (fd < 0)
		die("open");

	if (ioctl(fd, DRHYME_IOCTL_SET_CONFIG, &config) < 0)
		die("DRHYME_IOCTL_SET_CONFIG");

	memset(&status, 0, sizeof(status));
	if (ioctl(fd, DRHYME_IOCTL_GET_STATUS, &status) < 0)
		die("DRHYME_IOCTL_GET_STATUS");

	printf("abi=%u enabled=%u config=%u read_count=%llu\n",
	       status.abi_version, status.enabled, status.config,
	       (unsigned long long)status.read_count);

	close(fd);
	return 0;
}
