// SPDX-License-Identifier: GPL-2.0 /* * Hidraw Userspace Example * * Copyright (c) 2010 Alan Ott * Copyright (c) 2010 Signal 11 Software * * The code may be used by anyone for any purpose, * and can serve as a starting point for developing * applications using hidraw. */ /* Linux */ #include #include #include #ifndef HIDIOCSFEATURE #warning Please have your distro update the userspace kernel headers #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len) #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len) #endif /* Unix */ #include #include #include #include #include /* C */ #include #include #include #include int main(int argc, char **argv) { int fd; int i, res, desc_size = 0; char buf[256]; struct hidraw_report_descriptor rpt_desc; struct hidraw_devinfo info; char *device = "/dev/hidraw1"; if (argc > 1) device = argv[1]; /* Open the Device with non-blocking reads. In real life, don't use a hard coded path; use libudev instead. */ fd = open(device, O_RDWR|O_NONBLOCK); if (fd < 0) { perror("Unable to open device"); return 1; } memset(&rpt_desc, 0x0, sizeof(rpt_desc)); memset(&info, 0x0, sizeof(info)); memset(buf, 0x0, sizeof(buf)); /* Send a Report to the Device */ buf[0] = 0x7B; /* Report Number */ buf[1] = 0x03; buf[2] = 0x40; buf[3] = 0x7D; for (i=4; i<66; i++) buf[i]=0x0; res = write(fd, buf, 64); if (res < 0) { printf("Error: %d\n", errno); perror("write"); } //else { //printf("write() wrote %d bytes\n", res); //} sleep(1); /* Get a report from the device */ res = read(fd, buf, 64); if (res < 0) { perror("read"); } //else { // printf("read() read %d bytes:\n\t", res); // for (i = 0; i < res; i++) // printf("%hhx ", buf[i]); // puts("\n"); //} printf("\n"); { int chan=0,t,t1,t2,hu; printf("--------------------------------\n"); printf("Channel | Temperature | Humidity\n"); printf("================================\n"); for (i=1; i<=24; i+=3) { chan+=1; t=(buf[i+1] + (buf[i] << 8)) ; if (t < 0) { t+=256; } t1=buf[i]; t2=buf[i+1]; hu=buf[i+2]; if (t1 != 0x7F && t2 != 0xFF && hu != 0xFF) printf("% 5d %04.1f %2.2d\%\n",chan,t/10.0,hu); } } close(fd); return 0; }