-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUHIDDevice.ts
186 lines (159 loc) · 4.64 KB
/
UHIDDevice.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import addon from 'bindings';
const uhid: any = addon( 'linux-uhid' );
import { EventEmitter } from 'events';
/**
* Definitions of Bus Types
*/
export enum UHIDBusType {
PCI = uhid.UHIDBusType.PCI,
ISAPNP = uhid.UHIDBusType.ISAPNP,
USB = uhid.UHIDBusType.USB,
HIL = uhid.UHIDBusType.HIL,
BLUETOOTH = uhid.UHIDBusType.BLUETOOTH,
VIRTUAL = uhid.UHIDBusType.VIRTUAL,
ISA = uhid.UHIDBusType.ISA,
I8042 = uhid.UHIDBusType.I8042,
XTKBD = uhid.UHIDBusType.XTKBD,
RS232 = uhid.UHIDBusType.RS232,
GAMEPORT = uhid.UHIDBusType.GAMEPORT,
PARPORT = uhid.UHIDBusType.PARPORT,
AMIGA = uhid.UHIDBusType.AMIGA,
ADB = uhid.UHIDBusType.ADB,
I2C = uhid.UHIDBusType.I2C,
HOST = uhid.UHIDBusType.HOST,
GSC = uhid.UHIDBusType.GSC,
ATARI = uhid.UHIDBusType.ATARI,
SPI = uhid.UHIDBusType.SPI,
RMI = uhid.UHIDBusType.RMI,
CEC = uhid.UHIDBusType.CEC,
INTEL_ISHTP = uhid.UHIDBusType.INTEL_ISHTP
}
export enum UHIDDevFlags {
FEATURE = uhid.UHIDDevFlags.NUMBERED_FEATURE_REPORTS,
OUTPUT = uhid.UHIDDevFlags.NUMBERED_OUTPUT_REPORTS,
INPUT = uhid.UHIDDevFlags.NUMBERED_INPUT_REPORTS,
}
export enum UHIDReportType {
FEATURE = uhid.UHIDReportType.FEATURE_REPORT,
OUTPUT = uhid.UHIDReportType.OUTPUT_REPORT,
INPUT = uhid.UHIDReportType.INPUT_REPORT,
}
export type UHIDStartEvent = {
devFlags: UHIDDevFlags
};
export type UHIDOutputEvent = {
data: Buffer,
rtype: UHIDReportType
};
export type UHIDGetReportEvent = {
id: number,
rtype: UHIDReportType
};
export type UHIDSetReportEvent = {
data: Buffer,
id: number,
rnum: number,
rtype: UHIDReportType
};
/**
* UHID Device Class
*
* @class UHIDDevice
*/
export class UHIDDevice extends uhid.UHIDDevice {
/** @hidden */
private eventEmitter: any;
/** @hidden */
private eE = new EventEmitter();
constructor () {
super();
this.eventEmitter = this.eE.emit.bind( this.eE );
}
on ( event: "start", listener: ( e: UHIDStartEvent ) => void ): void;
on ( event: "stop", listener: () => void ): void;
on ( event: "open", listener: () => void ): void;
on ( event: "close", listener: () => void ): void;
on ( event: "output", listener: ( e: UHIDOutputEvent ) => void ): void;
on ( event: "getReport", listener: ( e: UHIDGetReportEvent ) => void ): void;
on ( event: "setReport", listener: ( e: UHIDSetReportEvent ) => void ): void;
on ( event: any, listener: any ): void {
this.eE.on( event, listener );
}
once ( event: "start", listener: ( e: UHIDStartEvent ) => void ): void;
once ( event: "stop", listener: () => void ): void;
once ( event: "open", listener: () => void ): void;
once ( event: "close", listener: () => void ): void;
once ( event: "output", listener: ( e: UHIDOutputEvent ) => void ): void;
once ( event: "getReport", listener: ( e: UHIDGetReportEvent ) => void ): void;
once ( event: "setReport", listener: ( e: UHIDSetReportEvent ) => void ): void;
once ( event: any, listener: any ): void {
this.eE.once( event, listener );
}
off ( event: "start", listener: ( e: UHIDStartEvent ) => void ): void;
off ( event: "stop", listener: () => void ): void;
off ( event: "open", listener: () => void ): void;
off ( event: "close", listener: () => void ): void;
off ( event: "output", listener: ( e: UHIDOutputEvent ) => void ): void;
off ( event: "getReport", listener: ( e: UHIDGetReportEvent ) => void ): void;
off ( event: "setReport", listener: ( e: UHIDSetReportEvent ) => void ): void;
off ( event: any, listener: any ): void {
this.eE.off( event, listener );
}
/**
* Create a new UHID Device, with given parameters.
*/
create ( options: {
name: string,
data: Buffer,
bus: UHIDBusType,
vendor: number,
product: number,
version: number,
country: number
} ): void {
super.create( options );
}
/**
* Destroy the UHID Device. Closes device if open.
*/
destroy (): void {
super.destroy();
}
/**
* Send an input report to the device
*/
input ( data: Buffer ): void {
super.input( data );
}
/**
* Send a feature request report to the device.
*/
getReportReply ( options: { id: number, err: number, data: Buffer } ): void {
super.getReportReply( options );
}
/**
* Send a feature report to the device.
*/
setReportReply ( options: { id: number, err: number } ): void {
super.setReportReply( options );
}
/**
* Poll device for any new events.
*/
poll (): void {
super.poll();
}
/**
* Opens communication with the device.
* This has to be run before any other commands.
*/
open (): void {
super.open();
}
/**
* Closes communication with the device.
*/
close (): void {
super.close();
}
}