summaryrefslogtreecommitdiff
path: root/hid/src/main.cpp
blob: 4eeb9838fbd4d7ae6918d065a6bf7b3a893f9638 (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
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
/*****************************************************************************
#                                                                            #
#    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/>.  #
#                                                                            #
*****************************************************************************/


#if !(defined(CMD_SERIAL) || defined(CMD_SPI))
#	error CMD phy is not defined
#endif


#include <Arduino.h>
#ifdef CMD_SPI
#	include <SPI.h>
#endif

#include "proto.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_SERIAL_TIMEOUT	100000
// -- OR --
// #define CMD_SPI


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


// -----------------------------------------------------------------------------
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;
}

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

uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 2 bytes
#	ifdef HID_USB_MOUSE
	uint8_t main_state = buffer[0];
	uint8_t extra_state = buffer[1];

#	define MOUSE_PAIR(_state, _button) \
		_state & PROTO_CMD_MOUSE_BUTTON_##_button##_SELECT, \
		_state & PROTO_CMD_MOUSE_BUTTON_##_button##_STATE
	hid_mouse.sendMouseButtons(
		MOUSE_PAIR(main_state, LEFT),
		MOUSE_PAIR(main_state, RIGHT),
		MOUSE_PAIR(main_state, MIDDLE),
		MOUSE_PAIR(extra_state, EXTRA_UP),
		MOUSE_PAIR(extra_state, EXTRA_DOWN)
	);
#	undef MOUSE_PAIR
#	endif
	return PROTO_RESP_OK;
}

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;
}

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;
}

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
	);
}

uint8_t handleCmdBuffer(const uint8_t *buffer) { // 8 bytes
	uint16_t crc = (uint16_t)buffer[6] << 8;
	crc |= (uint16_t)buffer[7];

	if (protoCrc16(buffer, 6) == crc) {
#		define HANDLE(_handler) { return _handler(buffer + 2); }
		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:	return 0;
			default:				return PROTO_RESP_INVALID_ERROR;
		}
#		undef HANDLE
	}
	return PROTO_RESP_CRC_ERROR;
}


// -----------------------------------------------------------------------------
#ifdef CMD_SPI
volatile uint8_t spi_in[8] = {0};
volatile uint8_t spi_in_index = 0;

volatile uint8_t spi_out[4] = {0};
volatile uint8_t spi_out_index = 0;

bool spiReady() {
	return (!spi_out[0] && spi_in_index == 8);
}

void spiWrite(const uint8_t *buffer) {
	spi_out[3] = buffer[3];
	spi_out[2] = buffer[2];
	spi_out[1] = buffer[1];
	spi_out[0] = buffer[0]; // Меджик разрешает начать ответ
}

ISR(SPI_STC_vect) {
	uint8_t in = SPDR;
	if (spi_out[0] && spi_out_index < 4) {
		SPDR = spi_out[spi_out_index];
		if (!(SPSR & (1 << WCOL))) {
			++spi_out_index;
			if (spi_out_index == 4) {
				spi_out_index = 0;
				spi_in_index = 0;
				spi_out[0] = 0;
			}
		}
	} else {
		static bool receiving = false;
		if (!receiving && in == PROTO_MAGIC) {
			receiving = true;
		}
		if (receiving && spi_in_index < 8) {
			spi_in[spi_in_index] = in;
			++spi_in_index;
		}
		if (spi_in_index == 8) {
			receiving = false;
		}
		SPDR = 0;
	}
}
#endif


// -----------------------------------------------------------------------------
void sendCmdResponse(uint8_t code) {
	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 = protoCrc16(buffer, 2);
	buffer[2] = (uint8_t)(crc >> 8);
	buffer[3] = (uint8_t)(crc & 0xFF);

#	ifdef CMD_SERIAL
	CMD_SERIAL.write(buffer, 4);
#	elif defined(CMD_SPI)
	spiWrite(buffer);
#	endif
}

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

#	ifdef CMD_SERIAL
	CMD_SERIAL.begin(CMD_SERIAL_SPEED);
#	elif defined(CMD_SPI)
	pinMode(MISO, OUTPUT);
	SPCR = (1 << SPE) | (1 << SPIE); // Slave, SPI En, IRQ En
#	endif
}

void loop() {
#	ifdef CMD_SERIAL
	unsigned long last = micros();
	uint8_t buffer[8];
	uint8_t index = 0;
#	endif

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

#		ifdef CMD_SERIAL
		if (CMD_SERIAL.available() > 0) {
			buffer[index] = (uint8_t)CMD_SERIAL.read();
			if (index == 7) {
				sendCmdResponse(handleCmdBuffer(buffer));
				index = 0;
			} else {
				last = micros();
				++index;
			}
		} else if (index > 0) {
			unsigned long now = micros();
			if (
				(now >= last && now - last > CMD_SERIAL_TIMEOUT)
				|| (now < last && ((unsigned long)-1) - last + now > CMD_SERIAL_TIMEOUT)
			) {
				sendCmdResponse(PROTO_RESP_TIMEOUT_ERROR);
				index = 0;
			}
		}
#		elif defined(CMD_SPI)
		if (spiReady()) {
			sendCmdResponse(handleCmdBuffer(spi_in));
		}
#		endif
	}
}