From 92f8645b0cbb2bf02fdb436e12edcad68aa240fc Mon Sep 17 00:00:00 2001
From: Michel Verbraak <michel@verbraak.org>
Date: Wed, 19 Jan 2022 08:31:42 +0100
Subject: [PATCH] Added decoding of report answer to command "X"

---
 cul.js | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/cul.js b/cul.js
index 3b1c932..b41dc5e 100644
--- a/cul.js
+++ b/cul.js
@@ -73,6 +73,8 @@ const Cul = function (options) {
     options.networkTimeout = options.networkTimeout || true;
     options.logger = options.logger || console.log;
 
+    that.getReport = 0;
+
     if (options.coc) {
         options.baudrate = options.baudrate || 38400;
         options.serialport = options.serialport || '/dev/ttyACM0';
@@ -179,6 +181,9 @@ const Cul = function (options) {
                 options.logger('->', data);
             }
 
+            if (data == "X")
+                that.getReport++;
+        
             serialPort.write(data + '\r\n');
             serialPort.drain(callback);
         };
@@ -320,6 +325,23 @@ const Cul = function (options) {
                     message = protocol[p].parse(data);
                 }
             }
+            else {
+                if (that.getReport > 0) {
+                    let reportData = Number.parseInt(data.substr(0,2), 16);
+                    message.report = {
+                        knownMessages: (reportData & 0x01) == 0x01,
+                        eachPacket: (reportData & 0x02) == 0x02,
+                        detailedData: (reportData & 0x04) == 0x04,
+                        monitorMode: (reportData & 0x08) == 0x08,
+                        timing: (reportData & 0x10) == 0x10,
+                        rssi: (reportData & 0x20) == 0x20,
+                        FHTprotocolMessage: (reportData & 0x40) == 0x40,
+                        rawRssi: (reportData & 0x80) == 0x80
+                    }
+                    message.availableTime = Number.parseInt(data.substr(2).replace(" ",""));
+                    that.report--;
+                }    
+            }
 
             if (options.rssi) {
                 rssi = Number.parseInt(data.slice(-2), 16);