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:
Michael Cardell Widerkrantz 2023-03-09 14:11:43 +01:00 committed by Daniel Lublin
parent 7a97f1ee5f
commit 4e3f5469ef
No known key found for this signature in database
GPG key ID: 75BD0FEB8D3E7830
4 changed files with 206 additions and 196 deletions

View file

@ -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) {