-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv2socand
More file actions
executable file
·70 lines (57 loc) · 2.02 KB
/
Copy pathkv2socand
File metadata and controls
executable file
·70 lines (57 loc) · 2.02 KB
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
#!/usr/bin/env -S python3
import argparse
import textwrap
import asyncio
import logging
import pycangw
import socketcand_utils
import sys
log = logging.getLogger(__name__)
def start_gateway(configs):
try:
log.info("Starting Gateway")
asyncio.run(pycangw.run(configs))
except KeyboardInterrupt:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Autodetect socketcand, and create bridge with kvaser"
)
parser.add_argument("-ch", "--channel", help="Kvaser channel", default="0")
parser.add_argument("-br", "--bitrate", help="Kvaser bitrate", default=1000000)
parser.add_argument("-addr", "--address", help="socketcand host address", default=None)
parser.add_argument("-port", "--port", help="socketcand port", default=None)
parser.add_argument("-dev", "--device", help="socketcand device (e.g. vcan0)", default="vcan0")
parser.add_argument("-log", "--loglevel", help="logging level", default="info")
args = parser.parse_args()
logging.basicConfig(level=args.loglevel.upper())
socketcand_cfg = {
"interface": "socketcand",
"channel": args.device,
"host": args.address,
"port": args.port,
"tcp_tune": True,
}
kvaser_cfg = {
"interface": "kvaser",
"channel": args.channel,
"single_handle": True,
"accept_virtual": True,
"bitrate": args.bitrate,
}
if (args.address is not None) and (args.port is not None):
start_gateway([socketcand_cfg, kvaser_cfg])
else:
log.info("Starting autodetect socketcand")
devs, host, port = socketcand_utils.detect()
if not(devs and host and port):
log.warning("socketcand not detected")
sys.exit(1)
if args.device not in devs:
log.info(
f"Requested device ({args.device}) not found in advertised devices ({devs})"
)
sys.exit(1)
socketcand_cfg['host'] = host
socketcand_cfg['port'] = port
start_gateway([socketcand_cfg, kvaser_cfg])