summaryrefslogtreecommitdiff
path: root/hid/src/main.cpp
blob: 8f84fd1dfac3632196020cbacb4e6b79a550b204 (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
/*****************************************************************************
#                                                                            #
#    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 <HID-Project.h>
#include <TimerOne.h>

#include "inline.h"
#include "keymap.h"


#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_CMD_PING					0x01
#define PROTO_CMD_REPEAT				0x02
#define PROTO_CMD_RESET_HID				0x10
#define PROTO_CMD_KEY_EVENT				0x11
#define PROTO_CMD_MOUSE_MOVE_EVENT		0x12
#define PROTO_CMD_MOUSE_BUTTON_EVENT	0x13
#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


// -----------------------------------------------------------------------------
INLINE void cmdResetHid(const uint8_t *buffer) { // 0 bytes
	BootKeyboard.releaseAll();
	SingleAbsoluteMouse.releaseAll();
}

INLINE void cmdKeyEvent(const uint8_t *buffer) { // 2 bytes
	KeyboardKeycode code = keymap(buffer[0]);

	if (code != KEY_ERROR_UNDEFINED) {
		if (buffer[1]) {
			BootKeyboard.press(code);
		} else {
			BootKeyboard.release(code);
		}
	}
}

INLINE void cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes
	int x = (int)buffer[0] << 8;
	x |= (int)buffer[1];

	int y = (int)buffer[2] << 8;
	y |= (int)buffer[3];

	SingleAbsoluteMouse.moveTo(x, y);
}

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

	if (state & PROTO_CMD_MOUSE_BUTTON_LEFT_SELECT) {
		if (state & PROTO_CMD_MOUSE_BUTTON_LEFT_STATE) {
			SingleAbsoluteMouse.press(MOUSE_LEFT);
		} else {
			SingleAbsoluteMouse.release(MOUSE_LEFT);
		}
	}

	if (state & PROTO_CMD_MOUSE_BUTTON_RIGHT_SELECT) {
		if (state & PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE) {
			SingleAbsoluteMouse.press(MOUSE_RIGHT);
		} else {
			SingleAbsoluteMouse.release(MOUSE_RIGHT);
		}
	}
}

INLINE void cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes
	// delta_x is not supported by hid-project now
	signed char delta_y = buffer[1];

	SingleAbsoluteMouse.move(0, 0, delta_y);
}


// -----------------------------------------------------------------------------
INLINE uint16_t makeCrc16(const uint8_t *buffer, const 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() {
	BootKeyboard.begin();
	SingleAbsoluteMouse.begin();

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

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

	while (true) {
		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) { _handler(buffer + 2); sendCmdResponse(PROTO_RESP_OK); break; }
					switch (buffer[1]) {
						case PROTO_CMD_RESET_HID:			HANDLE(cmdResetHid);
						case PROTO_CMD_KEY_EVENT:			HANDLE(cmdKeyEvent);
						case PROTO_CMD_MOUSE_MOVE_EVENT:	HANDLE(cmdMouseMoveEvent);
						case PROTO_CMD_MOUSE_BUTTON_EVENT:	HANDLE(cmdMouseButtonEvent);
						case PROTO_CMD_MOUSE_WHEEL_EVENT:	HANDLE(cmdMouseWheelEvent);

						case PROTO_CMD_PING:	sendCmdResponse(PROTO_RESP_OK); break;
						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;
		}
	}
}