--- /dev/null
+#include <stdio.h>
+#include <stdint.h>
+
+typedef unsigned char byte;
+typedef unsigned int uint;
+typedef uint16_t u16;
+
+u16 crc_update(u16 crc, byte data)
+{
+ crc = crc ^ (data << 8);
+ for (uint i=0; i<8; i++) {
+ if (crc & 0x8000)
+ crc = (crc << 1) ^ 0x1021;
+ else
+ crc <<= 1;
+ }
+ return crc;
+}
+
+byte msg[] = {
+ 0xdc, // sync
+ 0x80 | 0x42, // src
+ 0x00, // dest
+ 0x00, // length (will be filled)
+ 0x06, // command
+ 0x3d, 0x2e, 0x11, 0x25,
+};
+
+int main(void)
+{
+ msg[3] = sizeof(msg) + 2;
+
+ u16 crc = 0;
+ for (uint i=0; i < sizeof(msg); i++) {
+ printf("%02x ", msg[i]);
+ crc = crc_update(crc, msg[i]);
+ }
+ printf("%02x %02x\n", crc >> 8, crc & 0xff);
+
+ return 0;
+}
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+typedef unsigned char byte;
+typedef unsigned int uint;
+typedef uint16_t u16;
+
+uint rd(void)
+{
+ uint x;
+ if (scanf("%x", &x) == 1)
+ return x;
+ else {
+ puts("EOF");
+ exit(1);
+ }
+}
+
+u16 crc_update(u16 crc, byte data)
+{
+ crc = crc ^ (data << 8);
+ for (uint i=0; i<8; i++) {
+ if (crc & 0x8000)
+ crc = (crc << 1) ^ 0x1021;
+ else
+ crc <<= 1;
+ }
+ return crc;
+}
+
+int main(void)
+{
+ byte pkt[256];
+
+ for (;;) {
+ pkt[0] = rd();
+ if (pkt[0] != 0xdc) {
+ printf("# Bad sync: %02x\n", pkt[0]);
+ continue;
+ }
+
+ int len = 1;
+ while (len < 4 || len < pkt[3]) {
+ pkt[len++] = rd();
+ }
+
+ for (int i=0; i<len; i++)
+ printf("%02x ", pkt[i]);
+
+ u16 crc = 0;
+ for (int i=0; i<len; i++)
+ crc = crc_update(crc, pkt[i]);
+ if (crc) {
+ printf("[BAD CRC]\n");
+ continue;
+ }
+ len -= 2;
+
+ if (len < 9) {
+ printf("[TOO SHORT]\n");
+ continue;
+ }
+
+ printf("\n\t%02x -> %02x: ", pkt[1], pkt[2]);
+ switch (pkt[4]) {
+ case 2:
+ printf("INFO %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
+ for (int i=9; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+ break;
+ case 6:
+ printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
+ break;
+ case 7:
+ printf("RET %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
+ for (int i=9; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+ break;
+ default:
+ printf("??? type=%02x\n", pkt[4]);
+ }
+ }
+}