1 // Bodet BT-6000 decoder.
10 #include <arpa/inet.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
22 void process(const string &buf)
24 unsigned char checksum = 0;
25 for (size_t i = 1; i <= buf.size() - 2; ++i) {
29 if (checksum < 0x20) {
32 if (checksum != buf.back()) {
33 // fprintf(stderr, "discarding message with broken checksum: [%s] [%x vs. %x]\n", buf.c_str(), checksum, buf.back());
35 string realmsg = buf.substr(3, buf.size() - 5);
36 sendto(sock, realmsg.data(), realmsg.size(), 0, (sockaddr *)&saddr6, sizeof(saddr6));
40 int main(int argc, char **argv)
42 sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
49 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
54 memset(&saddr6, 0, sizeof(saddr6));
55 saddr6.sin6_family = AF_INET6;
57 if (inet_pton(AF_INET6, argv[1], &saddr6.sin6_addr) != 1) {
58 fprintf(stderr, "Invalid address '%s'\n", argv[1]);
62 inet_pton(AF_INET6, "::1", &saddr6.sin6_addr);
64 saddr6.sin6_port = htons(6000);
66 // TODO: open serial port
72 int ret = read(0, &ch, 1);
78 fprintf(stderr, "short read\n");
86 if (buf.size() == 1) {
92 if (buf.size() == 2) {
95 buf.clear(); // STX out-of-order
100 if (!buf.empty() && buf.back() == 3) { // Last was ETX, so this is LTC.
107 if (ch == 3) { // ETX
108 if (buf.size() >= 4) {
111 buf.clear(); // ETX out-of-order