File: hid-set.c

package info (click to toggle)
apcupsd 3.14.14-7
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,904 kB
  • sloc: ansic: 24,826; cpp: 9,230; sh: 4,484; makefile: 1,200; tcl: 368; objc: 317; php: 107
file content (101 lines) | stat: -rw-r--r-- 2,780 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 *  Copyright (c) 2002 Paul Stewart
 *
 *  UPS (HID) set values in UPS
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
 * 
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <hiddev@wetlogic.net>.
 */


#define HID_MAX_USAGES 1024

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <asm/types.h>
#include <linux/hiddev.h>

#define UPS_USAGE		0x840004

static inline int find_application(int fd, unsigned usage) {
	int i = 0;
	unsigned ret;

	while ((ret = ioctl(fd, HIDIOCAPPLICATION, i)) != -1 &&
               ret != usage) { printf("App: %08x\n", ret); i++; }
	if (ret < 0) {
                fprintf(stderr, "GUSAGE returns %x", ret);
                perror("");
	}
	return (ret == usage);
}

int main (int argc, char **argv) {
    int fd = -1, i, is_ups = 0;
    struct hiddev_report_info rinfo;
    struct hiddev_usage_ref uref;

    char evdev[20];
    for (i = 0; i < 4; i++) {
        sprintf(evdev, "/dev/usb/hid/hiddev%d", i);
	if ((fd = open(evdev, O_RDONLY)) >= 0) {
	    if (find_application(fd, UPS_USAGE)) {
                printf("It's a UPS!\n");
		is_ups = 1;
	    }
	    break;
	}
    }
    if (i >= 4) {
        fprintf(stderr, "Couldn't find hiddev device.\n");
	exit(1);
    }

   /* 
   Usage: 00840057 val -1 (Feature/53/0/0) [DelayBeforeShutdown]
   Usage: 00840056 val -1 (Feature/54/0/0) [DelayBeforeStartup]
    */

    memset(&uref, 0, sizeof(uref));
    uref.report_type = HID_REPORT_TYPE_FEATURE;
    if (argc > 1 && strcmp(argv[1], "off")) {
	uref.report_id = 54;
    } else {
	uref.report_id = 53;
    }

    uref.field_index = 0;
    uref.usage_index = 0;
    uref.value = 5;
    if (ioctl(fd, HIDIOCSUSAGE, &uref) != 0) perror("SUSAGE/S");
    rinfo.report_type = uref.report_type;
    rinfo.report_id = uref.report_id;
    if (ioctl(fd, HIDIOCSREPORT, &rinfo) != 0) perror("SREPORT/S");

    printf("Set 5 in report id %d\n", uref.report_id);

    exit(0);
}