]> mj.ucw.cz Git - home-hw.git/blob - test-bsb/gen.c
MQTT: Rainbow
[home-hw.git] / test-bsb / gen.c
1 #include <stdio.h>
2 #include <stdint.h>
3
4 typedef unsigned char byte;
5 typedef unsigned int uint;
6 typedef uint16_t u16;
7
8 u16 crc_update(u16 crc, byte data)
9 {
10         crc = crc ^ (data << 8);
11         for (uint i=0; i<8; i++) {
12                 if (crc & 0x8000)
13                         crc = (crc << 1) ^ 0x1021;
14                 else
15                         crc <<= 1;
16         }
17         return crc;
18 }
19
20 byte msg[] = {
21         0xdc,                   // sync
22         0x80 | 0x42,            // src
23         0x00,                   // dest
24         0x00,                   // length (will be filled)
25         0x06,                   // command
26         0x3d, 0x2e, 0x11, 0x25,
27 };
28
29 int main(void)
30 {
31         msg[3] = sizeof(msg) + 2;
32
33         u16 crc = 0;
34         for (uint i=0; i < sizeof(msg); i++) {
35                 printf("%02x ", msg[i]);
36                 crc = crc_update(crc, msg[i]);
37         }
38         printf("%02x %02x\n", crc >> 8, crc & 0xff);
39
40         return 0;
41 }