]> git.sesse.net Git - ultimatescore/commitdiff
Accept and parse BT6000 data over UDP.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 16 Oct 2018 16:39:41 +0000 (18:39 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 16 Oct 2018 16:39:41 +0000 (18:39 +0200)
client/bodet.cpp
client/mainwindow.cpp
client/mainwindow.h
client/mainwindow.ui
score.css

index 840094a840f048cc7d7dc79c616d449edf10eb2c..54ec609e1f9ea108b156c8881ed3e85d887a4e31 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,36 @@ 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);
+       }
+       saddr6.sin6_port = htons(6000);
+
        // TODO: open serial port
 
        string buf;
index 6b8f7136e06c41087bb1fc393145897a036fc44a..b186e6a6309d5b680b8e5c9d306b071b7f5aeada 100644 (file)
@@ -2,7 +2,12 @@
 #include "post_to_main_thread.h"
 #include "ui_mainwindow.h"
 
+#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;
 
@@ -92,6 +97,8 @@ MainWindow::MainWindow(QWidget *parent) :
                });
        });
 
+       udp_thread = std::thread(&MainWindow::udp_thread_func, this);
+
        connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
        connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
        connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
@@ -358,3 +365,100 @@ void MainWindow::show_nothing_clicked()
        ws->send_command("eval stopcarousel()");
        ws->send_command("eval hidetable()");
 }
+
+void MainWindow::udp_thread_func()
+{
+       int 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);
+       }
+
+       sockaddr_in6 saddr6;
+       memset(&saddr6, 0, sizeof(saddr6));
+       saddr6.sin6_family = AF_INET6;
+       inet_pton(AF_INET6, "::", &saddr6.sin6_addr);
+       saddr6.sin6_port = htons(6000);
+       if (bind(sock, (sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
+               perror("bind");
+               exit(1);
+       }
+
+       for ( ;; ) {
+               char buf[4096];
+               int err = recv(sock, buf, sizeof(buf), 0);
+               if (err == -1) {
+                       perror("recv");
+                       exit(1);
+               }
+
+               post_to_main_thread([buf, err, this] {
+                       bt6000_message_received(string(buf, err));
+               });
+       }
+}
+
+int parse_digit(char ch)
+{
+       if (ch >= '0' && ch <= '9') {
+               return ch - '0';
+       }
+       return 0;
+}
+
+int parse_clock(char ch1, char ch2)
+{
+       int s1 = parse_digit(ch1);
+       int s2 = parse_digit(ch2);
+       return s1 * 10 + s2;
+}
+
+int parse_score(char ch1, char ch2, char ch3)
+{
+       int s1 = parse_digit(ch1);
+       int s2 = parse_digit(ch2);
+       int s3 = parse_digit(ch3);
+       return s1 * 100 + s2 * 10 + s3;
+}
+
+void MainWindow::bt6000_message_received(const string &msg)
+{
+       fprintf(stderr, "BT6000 message: '%s'\n", msg.c_str());
+       if (!ui->bt6000_enable->isChecked()) {
+               return;
+       }
+
+       if (msg.size() >= 9 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '1') {
+               // G01: Game clock, period number, and number of time-outs.
+               bool clock_running = (msg[4] & 0x02);
+//             bool klaxon = (msg[4] & 0x04);
+               int minutes = parse_clock(msg[5], msg[6]);
+               int seconds = parse_clock(msg[7], msg[8]);
+
+               if (clock_running) {
+                       ws->send_command("eval startclock()");
+                       map<string, string> param;
+                       param["clock_min"] = to_string(minutes);
+                       param["clock_sec"] = to_string(seconds);
+                       ws->send_command("update " + serialize_as_json(param));
+                       ws->send_command("eval setclockfromstate()");
+               } else {
+                       ws->send_command("eval stopclock()");
+               }
+       }
+       if (msg.size() >= 10 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '2') {
+               int score1 = parse_score(msg[4], msg[5], msg[6]);
+               int score2 = parse_score(msg[7], msg[8], msg[9]);
+               ui->score_1_box->setValue(score1);
+               ui->score_2_box->setValue(score2);
+               set_score_clicked();
+       }
+
+       // Ignore type 3 (penalties) and type 4 (timeouts).
+}
index 1d7fe88d665b0ec725400af8ae5299e01fabf369..5402654fdce11930ffe935ad34e2ac641680af1b 100644 (file)
@@ -3,6 +3,9 @@
 
 #include <QMainWindow>
 
+#include <string>
+#include <thread>
+
 #include "ws_server.h"
 #include "event_device.h"
 
@@ -48,10 +51,14 @@ private:
        void show_carousel_clicked();
        void show_roster_carousel_clicked();
        void show_nothing_clicked();
+       void udp_thread_func();
+       void bt6000_message_received(const std::string &msg);
 
        Ui::MainWindow *ui;
        WSServer *ws;
        EventDevice *event_device;
+
+       std::thread udp_thread;
 };
 
 #endif // MAINWINDOW_H
index c3804a277de9722eb67e771eb436b27b3e964cd5..333e4b89c8deb9d7baf3f84c8ee0fdd9f820d39d 100644 (file)
           </property>
          </widget>
         </item>
+        <item>
+         <widget class="QCheckBox" name="bt6000_enable">
+          <property name="text">
+           <string>Get BT6000 data on UDP</string>
+          </property>
+          <property name="checked">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
        </layout>
       </item>
       <item>
index db72488207cbae47882270d2ee0667a2ca6cffae..ff84068a86ae13137f866ea3db33a4435a6d0c9a 100644 (file)
--- a/score.css
+++ b/score.css
@@ -10,8 +10,8 @@ body {
   overflow: hidden;
 
   /* 720p -> 1080p */
-  /* transform: scale(1.5);
-  transform-origin: top left; */
+  transform: scale(1.5);
+  transform-origin: top left;
 }
 body {
   font-family: 'Lato', sans-serif;