summaryrefslogtreecommitdiffstats
path: root/recipes-connectivity/kuksa-val/kuksa-dbc-feeder/0002-dbc2val-usability-improvements.patch
blob: be88fa0ee0c2c36d466d601199e7eefc4ecff40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
From d0730c0d643738ac683d1b5c19b117893e3b2049 Mon Sep 17 00:00:00 2001
From: Scott Murray <scott.murray@konsulko.com>
Date: Tue, 11 Oct 2022 15:28:07 -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 | 21 +++++++++++++++------
 dbc2val/dbcreader.py | 12 ++++++++----
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/dbc2val/dbcfeeder.py b/dbc2val/dbcfeeder.py
index 7daaa2b..33c2212 100755
--- a/dbc2val/dbcfeeder.py
+++ b/dbc2val/dbcfeeder.py
@@ -93,6 +93,7 @@ class Feeder:
         self._connected = False
         self._registered = False
         self._can_queue = queue.Queue()
+        self.kuksa = None
 
     def start(
         self,
@@ -134,7 +135,11 @@ class Feeder:
         else:
             # 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)
        
         # databroker related
         if USE_CASE=="databroker":
@@ -151,6 +156,8 @@ class Feeder:
         log.info("Shutting down...")
         self._shutdown = True
         # Tell others to stop
+        if USE_CASE=="kuksa" and self.kuksa is not None:
+            self.kuksa.stop()
         if self._reader is not None:
             self._reader.stop()
         if self._player is not None:
@@ -204,9 +211,10 @@ class Feeder:
         # kuksa related
         if USE_CASE=="kuksa":
             global kuksaconfig
-            kuksa = KuksaClientThread(kuksaconfig)
-            kuksa.start()
-            kuksa.authorize()
+            self.kuksa = KuksaClientThread(kuksaconfig)
+            self.kuksa.start()
+            if "token" in kuksaconfig:
+                self.kuksa.authorize(kuksaconfig["token"])
         
         while self._shutdown is False:
             # databroker related
@@ -255,7 +263,7 @@ class Feeder:
                             self._provider.update_datapoint(target, value)
                         # kuksa related
                         elif USE_CASE=="kuksa":
-                            resp=json.loads(kuksa.setValue(target, str(value)))
+                            resp=json.loads(self.kuksa.setValue(target, str(value)))
                             if "error" in resp:
                                 if "message" in resp["error"]: 
                                    log.error("Error setting {}: {}".format(target, resp["error"]["message"]))
@@ -282,8 +290,9 @@ def parse_config(filename):
         configfile = filename
     else:
         config_candidates = [
-            "/config/dbc_feeder.ini",
+            "/etc/kuksa-dbc-feeder/config.ini",
             "/etc/dbc_feeder.ini",
+            "/config/dbc_feeder.ini",
             "config/dbc_feeder.ini",
         ]
         configfile = None
diff --git a/dbc2val/dbcreader.py b/dbc2val/dbcreader.py
index 2500832..b537a4d 100644
--- a/dbc2val/dbcreader.py
+++ b/dbc2val/dbcreader.py
@@ -54,8 +54,8 @@ class DBCReader:
             Bitrate in bit/s.
         """
         self.bus = can.interface.Bus(*args, **kwargs)
-        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")
@@ -83,7 +83,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
             if msg and msg.arbitration_id in self.canidwl:
                 try:
                     decode = self.db.decode_message(msg.arbitration_id, msg.data)
@@ -105,4 +108,5 @@ class DBCReader:
 
     def stop(self):
         self.run = False
-
+        self.bus.shutdown()
+        self.rxThread.join()
-- 
2.37.3