From 91fb1f5a92e8784446c4e354fe5a8c465d6b3cb8 Mon Sep 17 00:00:00 2001
From: Scott Murray <scott.murray@konsulko.com>
Date: Tue, 2 May 2023 16:27:04 -0400
Subject: [PATCH 2/2] dbc2val: usability improvements

Changes:
- Tweaked default configuration file search path to better match
  Linux FHS and kuksa-val-server.  First look for a config.ini in
  /etc/kuksa-dbc-feeder, then /etc/dbc_feeder.ini.
- Add catching of exceptions around CAN device opening so that the
  script can exit cleanly with an error message if the device is
  not available.
- Fixed shutdown behavior with some tweaks to actually stop the
  reader and KUKSA.val client library threads.  This makes the
  script actually exit on SIGTERM as opposed to hanging.

Upstream-Status: pending

Signed-off-by: Scott Murray <scott.murray@konsulko.com>
---
 dbc2val/dbcfeeder.py              | 10 ++++++++--
 dbc2val/dbcfeederlib/dbcreader.py | 11 ++++++++---
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/dbc2val/dbcfeeder.py b/dbc2val/dbcfeeder.py
index d32bb04..b73b96e 100755
--- a/dbc2val/dbcfeeder.py
+++ b/dbc2val/dbcfeeder.py
@@ -170,7 +170,11 @@ class Feeder:
 
             # use socketCAN
             log.info("Using socket CAN device '%s'", canport)
-            self._reader.start_listening(bustype="socketcan", channel=canport)
+            try:
+                self._reader.start_listening(bustype="socketcan", channel=canport)
+            except:
+                log.error("Could not open {}, exiting".format(canport))
+                sys.exit(-1)
 
         self._run()
 
@@ -278,8 +282,10 @@ def parse_config(filename):
         configfile = filename
     else:
         config_candidates = [
-            "/config/dbc_feeder.ini",
+            "/etc/kuksa-dbc-feeder/config.ini",
+            "/etc/kuksa-dbc-feeder/dbc_feeder.ini",
             "/etc/dbc_feeder.ini",
+            "/config/dbc_feeder.ini",
             "config/dbc_feeder.ini",
         ]
         for candidate in config_candidates:
diff --git a/dbc2val/dbcfeederlib/dbcreader.py b/dbc2val/dbcfeederlib/dbcreader.py
index 5dad41f..c2f5b55 100644
--- a/dbc2val/dbcfeederlib/dbcreader.py
+++ b/dbc2val/dbcfeederlib/dbcreader.py
@@ -56,8 +56,8 @@ class DBCReader:
             Bitrate in bit/s.
         """
         self.bus = can.interface.Bus(*args, **kwargs) # pylint: disable=abstract-class-instantiated
-        rxThread = threading.Thread(target=self.rxWorker)
-        rxThread.start()
+        self.rxThread = threading.Thread(target=self.rxWorker)
+        self.rxThread.start()
 
     def get_whitelist(self):
         log.info("Collecting signals, generating CAN ID whitelist")
@@ -86,7 +86,10 @@ class DBCReader:
     def rxWorker(self):
         log.info("Starting Rx thread")
         while self.run:
-            msg = self.bus.recv(timeout=1)
+            try:
+                msg = self.bus.recv(timeout=1)
+            except Exception:
+                break
             log.debug("processing message from CAN bus")
             if msg and msg.arbitration_id in self.canidwl:
                 try:
@@ -113,3 +116,5 @@ class DBCReader:
 
     def stop(self):
         self.run = False
+        self.bus.shutdown()
+        self.rxThread.join()
-- 
2.39.2