summaryrefslogtreecommitdiff
path: root/hid/src/main.cpp
blob: 08ec3f6501b2a0fe679cb614af3412ba1dfa300c (plain)
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
/*****************************************************************************
#                                                                            #
#    KVMD - The main Pi-KVM daemon.                                          #
#                                                                            #
#    Copyright (C) 2018  Maxim Devaev <[email protected]>                    #
#                                                                            #
#    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 3 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, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
*****************************************************************************/


#include <Arduino.h>
#include <TimerOne.h>

#include "inline.h"

#if defined(HID_USB_KBD) || defined(HID_USB_MOUSE)
#	include "usb/hid.h"
#endif
#ifdef HID_PS2_KBD
#	include "ps2/hid.h"
#endif


// #define CMD_SERIAL		Serial1
#define CMD_SERIAL_SPEED	115200
#define CMD_RECV_TIMEOUT	100000

#define PROTO_MAGIC			0x33
#define PROTO_CRC_POLINOM	0xA001

#define PROTO_RESP_OK				0x20
#define PROTO_RESP_NONE				0x24
#define PROTO_RESP_CRC_ERROR		0x40
#define PROTO_RESP_INVALID_ERROR	0x45
#define PROTO_RESP_TIMEOUT_ERROR	0x48

#define PROTO_RESP_PONG_PREFIX	0x80
#define PROTO_RESP_PONG_CAPS	0b00000001
#define PROTO_RESP_PONG_SCROLL	0b00000010
#define PROTO_RESP_PONG_NUM		0b00000100

#define PROTO_CMD_PING					0x01
#define PROTO_CMD_REPEAT				0x02
#define PROTO_CMD_RESET_HID				0x10
#define PROTO_CMD_KEY_EVENT				0x11
#define PROTO_CMD_MOUSE_BUTTON_EVENT	0x13 // Legacy sequence
#define PROTO_CMD_MOUSE_MOVE_EVENT		0x12
#define PROTO_CMD_MOUSE_WHEEL_EVENT		0x14

#define PROTO_CMD_MOUSE_BUTTON_LEFT_SELECT		0b10000000
#define PROTO_CMD_MOUSE_BUTTON_LEFT_STATE		0b00001000
#define PROTO_CMD_MOUSE_BUTTON_RIGHT_SELECT		0b01000000
#define PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE		0b00000100
#define PROTO_CMD_MOUSE_BUTTON_MIDDLE_SELECT	0b00100000
#define PROTO_CMD_MOUSE_BUTTON_MIDDLE_STATE		0b00000010


// -----------------------------------------------------------------------------
#ifdef HID_USB_KBD
	UsbHidKeyboard hid_kbd;
#elif defined(HID_PS2_KBD)
	Ps2HidKeyboard hid_kbd;
#endif
#ifdef HID_USB_MOUSE
	UsbHidMouse hid_mouse;
#endif


// -----------------------------------------------------------------------------
INLINE uint8_t cmdResetHid(const uint8_t *buffer) { // 0 bytes
#	ifdef HID_USB_KBD
	hid_kbd.reset();
#	endif
#	ifdef HID_USB_MOUSE
	hid_mouse.reset();
#	endif
	return PROTO_RESP_OK;
}

INLINE uint8_t cmdKeyEvent(const uint8_t *buffer) { // 2 bytes
	hid_kbd.sendKey(buffer[0], buffer[1]);
	return PROTO_RESP_OK;
}

INLINE uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 1 byte
#	ifdef HID_USB_MOUSE
	uint8_t state = buffer[0];

	hid_mouse.sendMouseButtons(
		state & PROTO_CMD_MOUSE_BUTTON_LEFT_SELECT, state & PROTO_CMD_MOUSE_BUTTON_LEFT_STATE,
		state & PROTO_CMD_MOUSE_BUTTON_RIGHT_SELECT, state & PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE,
		state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_SELECT, state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_STATE
	);
#	endif
	return PROTO_RESP_OK;
}

INLINE uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes
#	ifdef HID_USB_MOUSE
	int x = (int)buffer[0] << 8;
	x |= (int)buffer[1];
	x = (x + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details

	int y = (int)buffer[2] << 8;
	y |= (int)buffer[3];
	y = (y + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details

	hid_mouse.sendMouseMove(x, y);
#	endif
	return PROTO_RESP_OK;
}

INLINE uint8_t cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes
#	ifdef HID_USB_MOUSE
	hid_mouse.sendMouseWheel(buffer[1]); // Y only, X is not supported
#	endif
	return PROTO_RESP_OK;
}

INLINE uint8_t cmdPongLeds(const uint8_t *buffer) { // 0 bytes
	return ((uint8_t) PROTO_RESP_PONG_PREFIX) | hid_kbd.getLedsAs(
		PROTO_RESP_PONG_CAPS,
		PROTO_RESP_PONG_SCROLL,
		PROTO_RESP_PONG_NUM
	);
}


// -----------------------------------------------------------------------------
INLINE uint16_t makeCrc16(const uint8_t *buffer, unsigned length) {
	uint16_t crc = 0xFFFF;

	for (unsigned byte_count = 0; byte_count < length; ++byte_count) {
		crc = crc ^ buffer[byte_count];
		for (unsigned bit_count = 0; bit_count < 8; ++bit_count) {
			if ((crc & 0x0001) == 0) {
				crc = crc >> 1;
			} else {
				crc = crc >> 1;
				crc = crc ^ PROTO_CRC_POLINOM;
			}
		}
	}
	return crc;
}


// -----------------------------------------------------------------------------
volatile bool cmd_recv_timed_out = false;

INLINE void recvTimerStop(bool flag) {
	Timer1.stop();
	cmd_recv_timed_out = flag;
}

INLINE void resetCmdRecvTimeout() {
	recvTimerStop(false);
	Timer1.initialize(CMD_RECV_TIMEOUT);
}

INLINE void sendCmdResponse(uint8_t code=0) {
	static uint8_t prev_code = PROTO_RESP_NONE;
	if (code == 0) {
		code = prev_code; // Repeat the last code
	} else {
		prev_code = code;
	}

	uint8_t buffer[4];
	buffer[0] = PROTO_MAGIC;
	buffer[1] = code;
	uint16_t crc = makeCrc16(buffer, 2);
	buffer[2] = (uint8_t)(crc >> 8);
	buffer[3] = (uint8_t)(crc & 0xFF);

	recvTimerStop(false);
	CMD_SERIAL.write(buffer, 4);
}

void intRecvTimedOut() {
	recvTimerStop(true);
}

void setup() {
	hid_kbd.begin();
#	ifdef HID_USB_MOUSE
	hid_mouse.begin();
#	endif

	Timer1.attachInterrupt(intRecvTimedOut);
	CMD_SERIAL.begin(CMD_SERIAL_SPEED);
}

void loop() {
	uint8_t buffer[8];
	unsigned index = 0;

	while (true) {
#		ifdef HID_PS2_KBD
		hid_kbd.periodic();
#		endif

		if (CMD_SERIAL.available() > 0) {
			buffer[index] = (uint8_t)CMD_SERIAL.read();
			if (index == 7) {
				uint16_t crc = (uint16_t)buffer[6] << 8;
				crc |= (uint16_t)buffer[7];

				if (makeCrc16(buffer, 6) == crc) {
#					define HANDLE(_handler) { sendCmdResponse(_handler(buffer + 2)); break; }
					switch (buffer[1]) {
						case PROTO_CMD_RESET_HID:			HANDLE(cmdResetHid);
						case PROTO_CMD_KEY_EVENT:			HANDLE(cmdKeyEvent);
						case PROTO_CMD_MOUSE_BUTTON_EVENT:	HANDLE(cmdMouseButtonEvent);
						case PROTO_CMD_MOUSE_MOVE_EVENT:	HANDLE(cmdMouseMoveEvent);
						case PROTO_CMD_MOUSE_WHEEL_EVENT:	HANDLE(cmdMouseWheelEvent);
						case PROTO_CMD_PING:				HANDLE(cmdPongLeds);
						case PROTO_CMD_REPEAT:	sendCmdResponse(); break;
						default:				sendCmdResponse(PROTO_RESP_INVALID_ERROR); break;
					}
#					undef HANDLE
				} else {
					sendCmdResponse(PROTO_RESP_CRC_ERROR);
				}
				index = 0;
			} else {
				resetCmdRecvTimeout();
				index += 1;
			}
		} else if (index > 0 && cmd_recv_timed_out) {
			sendCmdResponse(PROTO_RESP_TIMEOUT_ERROR);
			index = 0;
		}
	}
}