forked from denismr/iOSGyroForCemuhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
294 lines (239 loc) · 9.19 KB
/
app.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const WebSocket = require('ws');
const dgram = require('dgram');
const crc = require('crc');
const long = require('long');
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = dgram.createSocket('udp4');
function char(a) {
return a.charCodeAt(0);
}
const maxProtocolVer = 1001;
const MessageType = {
DSUC_VersionReq: 0x100000,
DSUS_VersionRsp: 0x100000,
DSUC_ListPorts: 0x100001,
DSUS_PortInfo: 0x100001,
DSUC_PadDataReq: 0x100002,
DSUS_PadDataRsp: 0x100002
}
const serverID = 0 + Math.floor(Math.random() * 9007199254740992);
console.log(`serverID: ${serverID}`);
var connectedClient = null;
var lastRequestAt = 0;
var phoneIsConnected = false;
var packetCounter = 0;
const clientTimeoutLimit = 5000;
///////////////////////////////////////////////////
function BeginPacket(data) {
let index = 0;
data[index++] = char('D');
data[index++] = char('S');
data[index++] = char('U');
data[index++] = char('S');
data.writeUInt16LE(maxProtocolVer, index, true);
index += 2;
data.writeUInt16LE(data.length - 16, index, true);
index += 2;
data.writeUInt32LE(0, index, true);
index += 4;
data.writeUInt32LE(serverID, index, true);
index += 4;
return index;
}
function FinishPacket(data) {
data.writeUInt32LE(crc.crc32(data), 8, true);
}
function SendPacket(client, data) {
let buffer = Buffer.alloc(data.length + 16);
let index = BeginPacket(buffer);
buffer.fill(data, index);
FinishPacket(buffer);
server.send(buffer, client.port, client.address, (error, bytes) => {
if (error) {
console.log("Send packet error");
console.log(error.message);
}
else if (bytes !== buffer.length) {
console.log(`failed to completely send all of buffer. Sent: ${bytes}. Buffer length: ${buffer.length}`);
}
});
}
///////////////////////////////////////////////////
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on('listening', () => {
const address = server.address();
console.log(`UDP Pad motion data provider listening ${address.address}:${address.port}`);
});
server.on('message', (data, rinfo) => {
if (!(data[0] === char('D') && data[1] === char('S') && data[2] === char('U') && data[3] === char('C'))) return;
let index = 4;
let protocolVer = data.readUInt16LE(index); index += 2;
let packetSize = data.readUInt16LE(index); index += 2;
let receivedCrc = data.readUInt32LE(index);
data[index++] = 0; data[index++] = 0;
data[index++] = 0; data[index++] = 0;
let computedCrc = crc.crc32(data);
// if (receivedCrc !== computedCrc)
let clientId = data.readUInt32LE(index); index += 4;
let msgType = data.readUInt32LE(index); index += 4;
if (msgType == MessageType.DSUC_VersionReq) {
console.log("Version request ignored.");
} else if (msgType == MessageType.DSUC_ListPorts) {
// console.log("List ports request.");
let numOfPadRequests = data.readInt32LE(index); index += 4;
for (let i = 0; i < numOfPadRequests; i++) {
let requestIndex = data[index + i];
if (requestIndex !== 0) continue;
let outBuffer = Buffer.alloc(16);
outBuffer.writeUInt32LE(MessageType.DSUS_PortInfo, 0, true);
let outIndex = 4;
outBuffer[outIndex++] = 0x00; // pad id
// outBuffer[outIndex++] = phoneIsActive ? 0x02 : 00; // state (connected or disconnected)
outBuffer[outIndex++] = 0x02; // state (connected)
outBuffer[outIndex++] = 0x03; // model (generic)
outBuffer[outIndex++] = 0x01; // connection type (usb)
// mac address
for (let j = 0; j < 5; j++) {
outBuffer[outIndex++] = 0;
} outBuffer[outIndex++] = 0xFF; // 00:00:00:00:00:FF
// outBuffer[outIndex++] = 0x00; // battery (none)
outBuffer[outIndex++] = 0xEF; // battery (charged)
outBuffer[outIndex++] = 0; // dunno (probably "is active")
SendPacket(rinfo, outBuffer);
}
} else if (msgType == MessageType.DSUC_PadDataReq) {
let flags = data[index++];
let idToRRegister = data[index++];
let macToRegister = ['', '', '', '', '', ''];
for (let i = 0; i < macToRegister.length; i++ , index++) {
macToRegister[i] = `${data[index] < 15 ? '0' : ''}${data[index].toString(16)}`;
}
macToRegister = macToRegister.join(':');
// console.log(`Pad data request (${flags}, ${idToRRegister}, ${macToRegister})`);
// There is only one controller, so
if (flags == 0 || (idToRRegister == 0 && flags & 0x01 !== 0) || (macToRegister == '00:00:00:00:00:ff' && flags & 0x02 !== 0)) {
connectedClient = rinfo;
lastRequestAt = Date.now();
}
}
});
function Report(motionTimestamp, accelerometer, gyro) {
let client = connectedClient;
if (client === null || Date.now() - lastRequestAt > clientTimeoutLimit) return;
let outBuffer = Buffer.alloc(100);
let outIndex = BeginPacket(outBuffer);
outBuffer.writeUInt32LE(MessageType.DSUS_PadDataRsp, outIndex, true);
outIndex += 4;
outBuffer[outIndex++] = 0x00; // pad id
outBuffer[outIndex++] = 0x02; // state (connected)
outBuffer[outIndex++] = 0x02; // model (generic)
outBuffer[outIndex++] = 0x01; // connection type (usb)
// mac address
for (let i = 0; i < 5; i++) {
outBuffer[outIndex++] = 0x00;
} outBuffer[outIndex++] = 0xFF; // 00:00:00:00:00:FF
outBuffer[outIndex++] = 0xEF; // battery (charged)
outBuffer[outIndex++] = 0x01; // is active (true)
outBuffer.writeUInt32LE(packetCounter++, outIndex, true);
outIndex += 4;
outBuffer[outIndex] = 0x00; // left, down, right, up, options, R3, L3, share
outBuffer[++outIndex] = 0x00; // square, cross, circle, triangle, r1, l1, r2, l2
outBuffer[++outIndex] = 0x00; // PS
outBuffer[++outIndex] = 0x00; // Touch
outBuffer[++outIndex] = 0x80; // position left x
outBuffer[++outIndex] = 0x80; // position left y
outBuffer[++outIndex] = 0x80; // position right x
outBuffer[++outIndex] = 0x80; // position right y
outBuffer[++outIndex] = 0x00; // dpad left
outBuffer[++outIndex] = 0x00; // dpad down
outBuffer[++outIndex] = 0x00; // dpad right
outBuffer[++outIndex] = 0x00; // dpad up
outBuffer[++outIndex] = 0x00; // square
outBuffer[++outIndex] = 0x00; // cross
outBuffer[++outIndex] = 0x00; // circle
outBuffer[++outIndex] = 0x00; // triange
outBuffer[++outIndex] = 0x00; // r1
outBuffer[++outIndex] = 0x00; // l1
outBuffer[++outIndex] = 0x00; // r2
outBuffer[++outIndex] = 0x00; // l2
outIndex++;
outBuffer[outIndex++] = 0x00; // track pad first is active (false)
outBuffer[outIndex++] = 0x00; // track pad first id
outBuffer.writeUInt16LE(0x0000, outIndex, true); // trackpad first x
outIndex += 2;
outBuffer.writeUInt16LE(0x0000, outIndex, true); // trackpad first y
outIndex += 2;
outBuffer[outIndex++] = 0x00; // track pad second is active (false)
outBuffer[outIndex++] = 0x00; // track pad second id
outBuffer.writeUInt16LE(0x0000, outIndex, true); // trackpad second x
outIndex += 2;
outBuffer.writeUInt16LE(0x0000, outIndex, true); // trackpad second y
outIndex += 2;
outBuffer.writeUInt32LE(motionTimestamp.low, outIndex, true);
outIndex += 4;
outBuffer.writeUInt32LE(motionTimestamp.high, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(accelerometer.x, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(accelerometer.y, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(accelerometer.z, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(gyro.x, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(gyro.y, outIndex, true);
outIndex += 4;
outBuffer.writeFloatLE(gyro.z, outIndex, true);
outIndex += 4;
FinishPacket(outBuffer);
server.send(outBuffer, client.port, client.address, (error, bytes) => {
if (error) {
console.log("Send packet error");
console.log(error.message);
}
else if (bytes !== outBuffer.length) {
console.log(`failed to completely send all of buffer. Sent: ${bytes}. Buffer length: ${outBuffer.length}`);
}
});
}
server.bind(26760);
/////////////////////////////////////////////////
const wss = new WebSocket.Server({ port: 1337 });
zeros = {
x: 0,
y: 0,
z: 0,
}
wss.on('connection', function connection(ws) {
console.log("WS Connected");
phoneIsConnected = true;
ws.on('message', function incoming(message) {
// console.log(message);
data = JSON.parse(message);
Report(long.fromNumber(data.ts, true), zeros, data.gyro);
});
ws.on( "error", () => {
phoneIsConnected = false;
console.log("WS ERROR");
});
ws.on( "close", () => {
phoneIsConnected = false;
console.log("WS Disconnected");
});
});
/////////////////////////////////////////////////
http.createServer(function(request, response) {
var filePath = path.join(__dirname, 'static.html');
var stat = fs.statSync(filePath);
response.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(response);
}).listen(8080);