|
6 | 6 | #include <tusb.h> |
7 | 7 |
|
8 | 8 | #include <M5.h> |
| 9 | +#include <api.h> |
9 | 10 | #include <buffer.h> |
10 | 11 | #include <log.h> |
11 | 12 | #include <sys.h> |
@@ -83,7 +84,7 @@ void uart_exec(const uart_inst_t *, char *); |
83 | 84 | void uart_write(const uart_inst_t *, const char *); |
84 | 85 |
|
85 | 86 | void uart_swipe(const uart_inst_t *uart, char *msg); |
86 | | -void uart_keypad(const uart_inst_t *uart, char *msg); |
| 87 | +void uart_keycode(const uart_inst_t *uart, char *msg); |
87 | 88 |
|
88 | 89 | void uart0_rx(buffer *); |
89 | 90 | void uart1_rx(buffer *); |
@@ -235,64 +236,23 @@ void uart_exec(const uart_inst_t *uart, char *msg) { |
235 | 236 | if (strncasecmp(msg, "swipe ", 6) == 0) { |
236 | 237 | uart_swipe(uart, &msg[6]); |
237 | 238 | } else if (strncasecmp(msg, "code ", 5) == 0) { |
238 | | - uart_keypad(uart, &msg[5]); |
| 239 | + uart_keycode(uart, &msg[5]); |
239 | 240 | } |
240 | 241 | } |
241 | 242 |
|
242 | 243 | void uart_swipe(const uart_inst_t *uart, char *msg) { |
243 | | - char *token = strtok(msg, " ,"); |
| 244 | + const char *reply = swipe(msg); |
244 | 245 |
|
245 | | - if (token != NULL) { |
246 | | - uint32_t u32; |
247 | | - |
248 | | - if (sscanf(msg, "%u", &u32) < 1) { |
249 | | - uart_write(uart, ERR_BAD_REQUEST); |
250 | | - return; |
251 | | - } |
252 | | - |
253 | | - uint32_t facility_code = u32 / 100000; |
254 | | - uint32_t card = u32 % 100000; |
255 | | - char *code; |
256 | | - |
257 | | - if (facility_code < 1 || facility_code > 255 || card > 65535) { |
258 | | - uart_write(uart, ERR_BAD_REQUEST); |
259 | | - return; |
260 | | - } |
261 | | - |
262 | | - if (!write_card(facility_code, card)) { |
263 | | - uart_write(uart, ERR_WRITE); |
264 | | - debugf(LOGTAG, "card %u%05u error", facility_code, card); |
265 | | - return; |
266 | | - } |
267 | | - |
268 | | - if ((code = strtok(NULL, " ,")) != NULL) { |
269 | | - int N = strlen(code); |
270 | | - for (int i = 0; i < N; i++) { |
271 | | - if (!write_keycode(code[i])) { |
272 | | - uart_write(uart, ERR_WRITE); |
273 | | - debugf(LOGTAG, "keycode %c error", code[i]); |
274 | | - return; |
275 | | - } |
276 | | - } |
277 | | - } |
278 | | - |
279 | | - uart_write(uart, ERR_OK); |
| 246 | + if (strlen(reply) > 0) { |
| 247 | + uart_write(uart, reply); |
280 | 248 | } |
281 | 249 | } |
282 | 250 |
|
283 | | -void uart_keypad(const uart_inst_t *uart, char *msg) { |
284 | | - int N = strlen(msg); |
285 | | - |
286 | | - if (N > 0) { |
287 | | - for (int i = 0; i < N; i++) { |
288 | | - if (!write_keycode(msg[i])) { |
289 | | - uart_write(uart, ERR_WRITE); |
290 | | - debugf(LOGTAG, "keycode %c error", msg[i]); |
291 | | - return; |
292 | | - } |
293 | | - } |
| 251 | +void uart_keycode(const uart_inst_t *uart, char *msg) { |
| 252 | + const char *reply = keycode(msg); |
294 | 253 |
|
295 | | - uart_write(uart, ERR_OK); |
| 254 | + if (strlen(reply) > 0) { |
| 255 | + uart_write(uart, reply); |
296 | 256 | } |
297 | 257 | } |
298 | 258 |
|
|
0 commit comments