12e366aa800d8e6bd91ec7016ba8ce25932e421b
[AGL/meta-agl-demo.git] / recipes-connectivity / kuksa-val / kuksa-dbc-feeder / 0002-dbc2val-usability-improvements.patch
1 From fe10a3645e77cd8122d3d312d317bedcb88bc683 Mon Sep 17 00:00:00 2001
2 From: Scott Murray <scott.murray@konsulko.com>
3 Date: Thu, 12 May 2022 17:39:56 +0200
4 Subject: [PATCH] dbc2val: usability improvements
5
6 Changes:
7 - Tweaked default configuration file search path to better match
8   Linux FHS and kuksa-val-server.  First look for a config.ini in
9   /etc/kuksa-dbc-feeder, then /etc/dbc_feeder.ini.
10 - Added a command-line option to specify configuration file, this
11   should allow running two instances against different interfaces.
12 - Added verbosity command-line option and made several messages
13   verbose mode only to avoid log spamming.
14 - Added '-u' option to python invocation to disable output buffering.
15   The intent is to make logging immediate, otherwise errors may not
16   get logged for some time (or at all).
17 - Add catching of exceptions around CAN device opening so that the
18   script can exit cleanly with an error message if the device is
19   not available.
20
21 Upstream-Status: pending
22 Signed-off-by: Scott Murray <scott.murray@konsulko.com>
23 ---
24  kuksa_feeders/dbc2val/dbcfeeder.py | 40 ++++++++++++++++++++++--------
25  1 file changed, 29 insertions(+), 11 deletions(-)
26
27 diff --git a/kuksa_feeders/dbc2val/dbcfeeder.py b/kuksa_feeders/dbc2val/dbcfeeder.py
28 index 56c316a..d2d70b9 100755
29 --- a/kuksa_feeders/dbc2val/dbcfeeder.py
30 +++ b/kuksa_feeders/dbc2val/dbcfeeder.py
31 @@ -1,4 +1,4 @@
32 -#!/usr/bin/env python
33 +#!/usr/bin/env -S python -u
34  
35  ########################################################################
36  # Copyright (c) 2020 Robert Bosch GmbH
37 @@ -15,15 +15,21 @@ import os, sys, signal
38  import configparser
39  import queue
40  import json
41 +import argparse
42  
43  from dbc2val import dbc2vssmapper, dbcreader, j1939reader, elm2canbridge
44 -
45 -scriptDir= os.path.dirname(os.path.realpath(__file__))
46 -sys.path.append(os.path.join(scriptDir, "../../"))
47  from kuksa_viss_client import KuksaClientThread
48  
49 -print("kuksa.val DBC example feeder")
50 -config_candidates=['/config/dbc_feeder.ini', '/etc/dbc_feeder.ini', os.path.join(scriptDir, 'config/dbc_feeder.ini')]
51 +parser = argparse.ArgumentParser("kuksa.val DBC example feeder")
52 +parser.add_argument("-c", "--config", dest="userconfig")
53 +parser.add_argument("-v", "--verbose", action="store_true")
54 +args = parser.parse_args()
55 +
56 +if args.verbose:
57 +    print("kuksa.val DBC example feeder")
58 +config_candidates=['/etc/kuksa-dbc-feeder/config.ini', '/etc/dbc_feeder.ini']
59 +if args.userconfig is not None:
60 +    config_candidates.insert(0, args.userconfig)
61  configfile = None
62  for candidate in config_candidates:
63      if os.path.isfile(candidate):
64 @@ -54,10 +60,12 @@ cancfg = config['can']
65  canport = cancfg['port']
66  
67  if config["can"].getboolean("j1939", False):
68 -    print("Use j1939 reader")
69 +    if args.verbose:
70 +        print("Use j1939 reader")
71      reader = j1939reader.J1939Reader(cancfg,canQueue,mapping)
72  else:
73 -    print("Use dbc reader")
74 +    if args.verbose:
75 +        print("Use dbc reader")
76      reader = dbcreader.DBCReader(cancfg, canQueue,mapping)
77  
78  if canport == 'elmcan':
79 @@ -65,10 +73,18 @@ if canport == 'elmcan':
80          print("section {} missing from configuration, exiting".format(canport))
81          sys.exit(-1)
82  
83 -    print("Using elmcan. Trying to set up elm2can bridge")
84 +    if args.verbose:
85 +        print("Using elmcan. Trying to set up elm2can bridge")
86      elmbr=elm2canbridge.elm2canbridge(canport, config[canport], reader.canidwl)
87  
88 -reader.start_listening()
89 +try:
90 +    reader.start_listening()
91 +except:
92 +    print("Could not open {}, exiting".format(canport))
93 +    kuksa.stop()
94 +    reader.stop()
95 +    sys.exit(-1)
96 +
97  running = True
98  
99  def terminationSignalreceived(signalNumber, frame):
100 @@ -77,6 +93,7 @@ def terminationSignalreceived(signalNumber, frame):
101      kuksa.stop()
102      reader.stop()
103      print("Received termination signal. Shutting down")
104 +    sys.exit(0)
105  
106  signal.signal(signal.SIGINT, terminationSignalreceived)
107  signal.signal(signal.SIGQUIT, terminationSignalreceived)
108 @@ -88,7 +105,8 @@ while running:
109          for target in mapping[signal]['targets']:
110              tv=mapping.transform(signal,target,value)
111              if tv is not None: #none indicates the transform decided to not set the value
112 -                print("Update VSS path {} to {} based on signal {}".format(target, tv, signal))
113 +                if args.verbose:
114 +                    print("Update VSS path {} to {} based on signal {}".format(target, tv, signal))
115                  resp=json.loads(kuksa.setValue(target, str(tv)))
116                  if "error" in resp:
117                      if "message" in resp["error"]: 
118 -- 
119 2.35.1
120