mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-08-08 14:42:19 -04:00
fw: Simplify logic
Switch on state, then read commands specifically in the states that allow reading of commands, then switch on specific command.
This commit is contained in:
parent
7a97f1ee5f
commit
4e3f5469ef
4 changed files with 206 additions and 196 deletions
|
@ -5,7 +5,9 @@
|
|||
|
||||
#include "proto.h"
|
||||
#include "../tk1_mem.h"
|
||||
#include "led.h"
|
||||
#include "lib.h"
|
||||
#include "state.h"
|
||||
#include "types.h"
|
||||
|
||||
// clang-format off
|
||||
|
@ -20,6 +22,34 @@ uint8_t genhdr(uint8_t id, uint8_t endpoint, uint8_t status, enum cmdlen len)
|
|||
return (id << 5) | (endpoint << 3) | (status << 2) | len;
|
||||
}
|
||||
|
||||
int readcommand(struct frame_header *hdr, uint8_t *cmd, int state)
|
||||
{
|
||||
uint8_t in;
|
||||
|
||||
*led = (state == FW_STATE_LOADING) ? LED_BLACK : LED_WHITE;
|
||||
in = readbyte();
|
||||
|
||||
if (parseframe(in, hdr) == -1) {
|
||||
htif_puts("Couldn't parse header\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cmd, 0, CMDLEN_MAXBYTES);
|
||||
// Now we know the size of the cmd frame, read it all
|
||||
if (read(cmd, CMDLEN_MAXBYTES, hdr->len) != 0) {
|
||||
htif_puts("read: buffer overrun\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Is it for us?
|
||||
if (hdr->endpoint != DST_FW) {
|
||||
htif_puts("Message not meant for us\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parseframe(uint8_t b, struct frame_header *hdr)
|
||||
{
|
||||
if ((b & 0x80) != 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue