]> git.sesse.net Git - ultimatescore/blobdiff - client/bodet.cpp
Check in some scripts based on OR-tools to try to generate good group schedules.
[ultimatescore] / client / bodet.cpp
index 840094a840f048cc7d7dc79c616d449edf10eb2c..b9eecc9c6ac83991daaebf5ae05ceabe5d8226e4 100644 (file)
@@ -1,13 +1,24 @@
 // Bodet BT-6000 decoder.
 
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <string>
 #include <vector>
 
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
 using namespace std;
 
+int sock;
+sockaddr_in6 saddr6;
+
 void process(const string &buf)
 {
        unsigned char checksum = 0;
@@ -22,12 +33,41 @@ void process(const string &buf)
        //      fprintf(stderr, "discarding message with broken checksum: [%s] [%x vs. %x]\n", buf.c_str(), checksum, buf.back());
        } else {
                string realmsg = buf.substr(3, buf.size() - 5);
-               fprintf(stderr, "msg: [%s]\n", realmsg.c_str());
+               sendto(sock, realmsg.data(), realmsg.size(), 0, (sockaddr *)&saddr6, sizeof(saddr6));
        }
 }
 
 int main(int argc, char **argv)
 {
+       sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+       if (sock == -1) {
+               perror("socket");
+               exit(1);
+       }
+
+       int one = 1;
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
+               perror("setsockopt");
+               exit(1);
+       }
+
+       memset(&saddr6, 0, sizeof(saddr6));
+       saddr6.sin6_family = AF_INET6;
+       if (argc >= 2) {
+               if (inet_pton(AF_INET6, argv[1], &saddr6.sin6_addr) != 1) {
+                       fprintf(stderr, "Invalid address '%s'\n", argv[1]);
+                       exit(1);
+               }
+       } else {
+               inet_pton(AF_INET6, "::1", &saddr6.sin6_addr);
+       }
+
+       int port = 6000;
+       if (argc >= 3) {
+               port = atoi(argv[2]);
+       }
+       saddr6.sin6_port = htons(port);
+
        // TODO: open serial port
 
        string buf;