-
Notifications
You must be signed in to change notification settings - Fork 3
/
jvsio_common_impl.h
264 lines (247 loc) · 6.27 KB
/
jvsio_common_impl.h
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
// Copyright 2023 Takashi Toyoshima <toyoshim@gmail.com>.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "jvsio_common.h"
#include <stdint.h>
#include <stdlib.h>
#include "jvsio_client.h"
static uint8_t tx_data[256];
static uint8_t tx_report_size;
static uint8_t rx_data[256];
static uint8_t rx_size;
static uint8_t rx_read_ptr;
static bool rx_receiving;
static bool rx_escaping;
static bool rx_available;
static bool rx_error;
static uint8_t nodes;
static uint8_t address[2];
static bool downstream_ready;
static bool matchAddress(void) {
uint8_t target = rx_data[0];
for (uint8_t i = 0; i < nodes; ++i) {
if (target == address[i]) {
return true;
}
}
return false;
}
static bool getCommandSize(uint8_t* command, uint8_t len, uint8_t* size) {
switch (*command) {
case kCmdReset:
case kCmdAddressSet:
*size = 2;
return true;
case kCmdIoId:
case kCmdCommandRev:
case kCmdJvRev:
case kCmdProtocolVer:
case kCmdFunctionCheck:
*size = 1;
return true;
case kCmdMainId:
break; // handled later
case kCmdSwInput:
*size = 3;
return true;
case kCmdCoinInput:
case kCmdAnalogInput:
case kCmdRotaryInput:
*size = 2;
return true;
case kCmdKeyCodeInput:
*size = 1;
return true;
case kCmdScreenPositionInput:
*size = 2;
return true;
case kCmdRetry:
*size = 1;
return true;
case kCmdCoinSub:
case kCmdCoinAdd:
*size = 4;
return true;
case kCmdDriverOutput:
*size = (len < 2) ? 0 : (command[1] + 2);
return true;
case kCmdAnalogOutput:
*size = (len < 2) ? 0 : (command[1] * 2 + 2);
return true;
case kCmdCharacterOutput:
*size = (len < 2) ? 0 : (command[1] + 2);
return true;
case kCmdNamco:
if (len < 2) {
*size = 0;
} else {
switch (command[1]) {
case 0x04:
*size = 4;
break;
case 0x18:
if (len < 5) {
*size = 0;
} else {
switch (command[4]) {
case 0x02:
*size = 7;
break;
case 0x14:
*size = 12;
break;
case 0x80:
*size = 6;
break;
default:
return false;
}
}
break;
default:
return false;
}
}
return true;
case kCmdCommSup:
*size = 1;
return true;
case kCmdCommChg:
*size = 2;
return true;
default:
return false;
}
*size = 2;
for (uint8_t i = 1; i < len && command[i]; ++i) {
*size = *size + 1;
}
if (command[*size - 1]) {
*size = 0; // data is incomplete.
}
return true;
}
static void writeEscapedByte(uint8_t data) {
if (data == kMarker || data == kSync) {
JVSIO_Client_send(kMarker);
JVSIO_Client_send(data - 1);
} else {
JVSIO_Client_send(data);
}
}
static void sendPacket(void) {
JVSIO_Client_send(kSync);
uint8_t sum = 0;
for (uint8_t i = 0; i <= tx_data[1]; ++i) {
sum += tx_data[i];
writeEscapedByte(tx_data[i]);
}
writeEscapedByte(sum);
JVSIO_Client_willReceive();
}
static void pushOverflowStatus(void) {
tx_data[0] = kHostAddress;
tx_data[1] = 2;
tx_data[2] = 0x04;
}
static void pushUnknownCommandStatus(void) {
if (tx_report_size > 253) {
return pushOverflowStatus();
}
tx_data[0] = kHostAddress;
tx_data[1] = 2 + tx_report_size;
tx_data[2] = 0x02;
}
// If `speculative` is true, `rx_available` is set to true when a command is
// ready to process spculatively. If `rx_receiving` is still true, the packet
// isn't verified yet. `rx_receiving` is set to false for the last command.
// Caller should set `rx_available` to false after processing the command.
static void receive(bool speculative) {
while (JVSIO_Client_isDataAvailable()) {
uint8_t data = JVSIO_Client_receive();
if (data == kSync) {
rx_size = 0;
rx_read_ptr = 2;
rx_receiving = true;
rx_available = false;
rx_escaping = false;
rx_error = false;
tx_report_size = 0;
downstream_ready = JVSIO_Client_isSenseReady();
continue;
}
if (!rx_receiving) {
continue;
}
if (data == kMarker) {
rx_escaping = true;
continue;
}
if (rx_escaping) {
rx_data[rx_size++] = data + 1;
rx_escaping = false;
} else {
rx_data[rx_size++] = data;
}
}
if (!rx_receiving) {
return;
}
if (rx_size < 2) {
return;
}
if (rx_data[0] != kBroadcastAddress && !matchAddress()) {
// Ignore packets for other nodes.
rx_receiving = false;
return;
}
if (rx_size == rx_read_ptr) {
// No data.
return;
}
if (speculative) {
// Speculatively handle receiving commands.
uint8_t command_size;
if (!getCommandSize(&rx_data[rx_read_ptr], rx_size - rx_read_ptr,
&command_size)) {
// Contain an unknown comamnd. Reply with the error status and ignore the
// whole packet.
rx_receiving = false;
pushUnknownCommandStatus();
return;
}
if (command_size == 0 || (rx_read_ptr + command_size) > rx_size) {
// No command is ready to process.
return;
}
// The last command needs a checksum verification. Do nothing until the
// last byte is received.
if ((rx_data[1] + 1) != (rx_read_ptr + command_size)) {
// At least, one command is ready to process.
rx_available = true;
return;
}
}
// Wait for the last byte, checksum.
if ((rx_data[1] + 2) != rx_size) {
return;
}
// Let's calculate the checksum.
rx_receiving = false;
rx_available = true;
uint8_t sum = 0;
for (size_t i = 0; i < (rx_size - 1u); ++i) {
sum += rx_data[i];
}
if (rx_data[rx_size - 1] != sum) {
// Handles check sum error cases.
if (address[0] == kHostAddress) {
// Host mode does not need to send an error response back.
} else if (rx_data[2] == kCmdReset || rx_data[2] == kCmdCommChg) {
// These commands don't need a response.
} else {
// Reply with the error and ignore commands in the packet.
rx_error = true;
}
}
}