108 lines
3.7 KiB
C
108 lines
3.7 KiB
C
#include "rmic_control.h"
|
|
#include <string.h>
|
|
|
|
static uint16_t read16(const uint8_t *p) {
|
|
return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
|
|
}
|
|
|
|
static uint32_t read32(const uint8_t *p) {
|
|
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
|
|
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
|
}
|
|
|
|
static uint16_t crc16(const uint8_t *bytes, size_t count) {
|
|
uint16_t crc = UINT16_C(0xffff);
|
|
size_t i;
|
|
for (i = 0; i < count; ++i) {
|
|
unsigned int bit;
|
|
crc ^= (uint16_t)((uint16_t)bytes[i] << 8);
|
|
for (bit = 0; bit < 8; ++bit) {
|
|
crc = (uint16_t)((crc & UINT16_C(0x8000)) ?
|
|
((uint32_t)crc << 1) ^ UINT32_C(0x1021) : (uint32_t)crc << 1);
|
|
}
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
static void stop(rmic_receiver *receiver) {
|
|
memset(&receiver->control, 0, sizeof(receiver->control));
|
|
receiver->synchronized = false;
|
|
}
|
|
|
|
void rmic_receiver_init(rmic_receiver *receiver) {
|
|
memset(receiver, 0, sizeof(*receiver));
|
|
}
|
|
|
|
rmic_control rmic_receiver_poll(rmic_receiver *receiver, uint32_t now_ms) {
|
|
if (receiver->synchronized &&
|
|
(uint32_t)(now_ms - receiver->last_valid_ms) >= RMIC_CONTROL_TIMEOUT_MS) {
|
|
stop(receiver);
|
|
}
|
|
return receiver->control;
|
|
}
|
|
|
|
static bool decode(const uint8_t *bytes, rmic_control *result, uint32_t *sequence) {
|
|
unsigned int i;
|
|
uint8_t flags = bytes[8];
|
|
if (bytes[0] != 0xa5 || bytes[1] != 0x5a || bytes[2] != 1 ||
|
|
bytes[3] != RMIC_CONTROL_FRAME_SIZE || bytes[9] != 0 || (flags & 0xf8) != 0 ||
|
|
crc16(bytes, 18) != read16(bytes + 18)) {
|
|
return false;
|
|
}
|
|
memset(result, 0, sizeof(*result));
|
|
result->enabled = (flags & 1) != 0;
|
|
result->debug = (flags & 2) != 0;
|
|
result->fire = (flags & 4) != 0;
|
|
*sequence = read32(bytes + 4);
|
|
for (i = 0; i < 4; ++i) {
|
|
uint16_t raw = read16(bytes + 10 + 2 * i);
|
|
/* Avoid implementation-defined conversion of values above INT16_MAX. */
|
|
int32_t value = raw <= INT16_MAX ? (int32_t)raw : (int32_t)raw - 65536;
|
|
if (value < -1000 || value > 1000 || (!result->enabled && value != 0)) {
|
|
return false;
|
|
}
|
|
result->channel[i] = (int16_t)value;
|
|
}
|
|
return result->enabled || (!result->debug && !result->fire);
|
|
}
|
|
|
|
size_t rmic_receiver_feed(rmic_receiver *receiver, const uint8_t *data, size_t length,
|
|
uint32_t now_ms) {
|
|
size_t accepted = 0;
|
|
size_t i;
|
|
(void)rmic_receiver_poll(receiver, now_ms);
|
|
for (i = 0; i < length; ++i) {
|
|
rmic_control next;
|
|
uint32_t sequence;
|
|
receiver->buffer[receiver->used++] = data[i];
|
|
if (receiver->used < RMIC_CONTROL_FRAME_SIZE) {
|
|
continue;
|
|
}
|
|
if (!decode(receiver->buffer, &next, &sequence)) {
|
|
/* One-byte rescan preserves an embedded prefix after noise or a truncated frame. */
|
|
memmove(receiver->buffer, receiver->buffer + 1, RMIC_CONTROL_FRAME_SIZE - 1);
|
|
receiver->used = RMIC_CONTROL_FRAME_SIZE - 1;
|
|
continue;
|
|
}
|
|
receiver->used = 0;
|
|
if (!receiver->synchronized) {
|
|
/* Cold boot or timeout: first observe a complete neutral frame.
|
|
* This also permits the C3 sequence to restart at zero after reboot. */
|
|
if (next.enabled) {
|
|
continue;
|
|
}
|
|
} else {
|
|
uint32_t delta = sequence - receiver->sequence;
|
|
if (delta == 0 || delta >= UINT32_C(0x80000000)) {
|
|
continue;
|
|
}
|
|
}
|
|
receiver->synchronized = true;
|
|
receiver->sequence = sequence;
|
|
receiver->last_valid_ms = now_ms;
|
|
receiver->control = next;
|
|
++accepted;
|
|
}
|
|
return accepted;
|
|
}
|