-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from christoph2/master
pull changes from base repo
- Loading branch information
Showing
23 changed files
with
386 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,4 +119,7 @@ result.xml | |
/docs/_templates | ||
/docs/_build | ||
/docs/_static | ||
|
||
**/*gz | ||
**/*tar | ||
**/*bz2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
version: 2 | ||
|
||
sphinx: | ||
configuration: docs/conf.py | ||
|
||
formats: all | ||
|
||
python: | ||
version: 3.7 | ||
install: | ||
- requirements: docs/requirements.txt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sphinxcontrib-napoleon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Parse (transport-layer specific) command line parameters | ||
and create a XCP master instance. | ||
.. note:: | ||
There is currently no interface for further customization. | ||
""" | ||
|
||
__copyright__ = """ | ||
pySART - Simplified AUTOSAR-Toolkit for Python. | ||
(C) 2009-2019 by Christoph Schueler <[email protected]> | ||
All Rights Reserved | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
""" | ||
|
||
|
||
import argparse | ||
import json | ||
import pathlib | ||
|
||
from pprint import pprint | ||
|
||
try: | ||
import toml | ||
except ImportError: | ||
HAS_TOML = False | ||
else: | ||
HAS_TOML = True | ||
|
||
from pyxcp.master import Master | ||
from pyxcp.transport import Eth | ||
from pyxcp.transport import SxI | ||
|
||
|
||
ARGUMENTS = { | ||
"can": ("driver", "loglevel"), | ||
"eth": ("host", "port", "protocol", "ipv6", "loglevel"), | ||
"sxi": ("port", "baudrate", "bytesize", "parity", "stopbits", "loglevel"), | ||
} | ||
|
||
|
||
def makeNonNullValuesDict(**params): | ||
"""Only add items with non-None values. | ||
""" | ||
return {k: v for k, v in params.items() if not v is None} | ||
|
||
|
||
def readConfiguration(conf): | ||
""" | ||
""" | ||
if conf: | ||
pth = pathlib.Path(conf.name) | ||
suffix = pth.suffix.lower() | ||
if suffix == '.json': | ||
reader = json | ||
elif suffix == '.toml' and HAS_TOML: | ||
reader = toml | ||
else: | ||
reader = None | ||
if reader: | ||
return reader.loads(conf.read()) | ||
else: | ||
return {} | ||
else: | ||
return {} | ||
|
||
|
||
def mergeParameters(transport, config, params): | ||
"""Merge parameters from config-file and command-line. | ||
The latter have precedence. | ||
""" | ||
args = ARGUMENTS.get(transport) | ||
result = {} | ||
for arg in args: | ||
cvalue = config.get(arg.upper()) | ||
if cvalue: | ||
result[arg] = cvalue | ||
pvalue = params.get(arg) | ||
if pvalue: | ||
result[arg] = pvalue | ||
return result | ||
|
||
|
||
def removeParameters(transport, config): | ||
"""Remove constructor parameters from configuration. | ||
""" | ||
stoplist = [arg.upper() for arg in ARGUMENTS.get(transport)] | ||
return {k: v for k, v in config.items() if not k in stoplist} | ||
|
||
|
||
class ArgumentParser: | ||
""" | ||
""" | ||
|
||
def __init__(self, *args, **kws): | ||
kws.update(formatter_class = argparse.RawDescriptionHelpFormatter) | ||
self.parser = argparse.ArgumentParser(*args, **kws) | ||
subparsers = self.parser.add_subparsers(dest = "transport") | ||
subparsers.help = "Transport layer" | ||
self.parser.add_argument('-c', '--config-file', type=argparse.FileType('r'), dest = "conf", | ||
help = 'File to read (extended) parameters from.') | ||
self.parser.add_argument('-l', '--loglevel', choices = ["ERROR", "WARN", "INFO", "DEBUG"]) | ||
self.parser.epilog = "To get specific help on transport layers\nuse <layer> -h, e.g. {} eth -h".format(self.parser.prog) | ||
|
||
eth = subparsers.add_parser("eth", description = "XCPonEth specific options:") | ||
eth.set_defaults(eth = True) | ||
sxi = subparsers.add_parser("sxi", description = "XCPonSxI specific options:") | ||
sxi.set_defaults(sxi = True) | ||
can = subparsers.add_parser("can", description = "XCPonCAN specific options:") | ||
can.set_defaults(can = True) | ||
|
||
eth.add_argument('-p', '--port', type = int, metavar = "port") | ||
proto = eth.add_mutually_exclusive_group() | ||
proto.add_argument('-t', '--tcp', default = True, const = True, metavar = "tcp", action = "store_const") | ||
proto.add_argument('-u', '--udp', default = False, const = True, metavar = "udp", action = "store_const") | ||
|
||
eth.add_argument('-6', '--ipv6', const = True, metavar = "ipv6", action = "store_const") | ||
eth.add_argument('-H', '--host', help = "Host name or IP.") | ||
|
||
sxi.add_argument('-p', '--port', help = "Name or number of your serial interface.") | ||
sxi.add_argument('-b', '--baudrate', type = int) | ||
sxi.add_argument('--bytesize', type = int) | ||
sxi.add_argument('--parity', choices = ['N', 'E', 'O']) | ||
sxi.add_argument('--stopbits', type = int, choices = [1, 2]) | ||
self._args = [] | ||
|
||
|
||
def run(self): | ||
""" | ||
""" | ||
self._args = self.parser.parse_args() | ||
args = self.args | ||
config = readConfiguration(args.conf) | ||
transport = args.transport | ||
if not transport: | ||
print("missing argument transport: choose from {}".format(['can', 'eth', 'sxi'])) | ||
exit(1) | ||
if transport == "eth": | ||
params = makeNonNullValuesDict( | ||
host = args.host, | ||
port = args.port, | ||
protocol = "UDP" if args.udp else "TCP", | ||
ipv6 = args.ipv6, | ||
loglevel = args.loglevel) | ||
Klass = Eth | ||
elif transport == "sxi": | ||
params = makeNonNullValuesDict( | ||
port = args.port, | ||
baudrate = args.baudrate, | ||
bytesize = args.bytesize, | ||
parity = args.parity, | ||
stopbits = args.stopbits, | ||
loglevel = args.loglevel) | ||
klass = SxI | ||
elif transport == "can": | ||
raise NotImplementedError("No CAN support for now.") | ||
params = mergeParameters(transport, config, params) | ||
config = removeParameters(transport, config) | ||
params.update(config = config) | ||
tr = Klass(**params) | ||
return Master(tr) | ||
|
||
@property | ||
def args(self): | ||
return self._args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"DRIVER": "kvaser", | ||
"KV_CHANNEL": 0, | ||
"CAN_ID_MASTER": 257, | ||
"CAN_ID_SLAVE": 258, | ||
"CAN_ID_BROADCAST": 256, | ||
"MAX_DLC_REQUIRED": false, | ||
"FREQ": 250000, | ||
"BTQ": 16, | ||
"TSEG1": 14, | ||
"TSEG2": 2, | ||
"NOSAMP": 1, | ||
"SJW": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
DRIVER = "kvaser" | ||
KV_CHANNEL = 0 | ||
CAN_ID_MASTER = 257 | ||
CAN_ID_SLAVE = 258 | ||
CAN_ID_BROADCAST = 256 | ||
MAX_DLC_REQUIRED = false | ||
FREQ = 250000 | ||
BTQ = 16 | ||
TSEG1 = 14 | ||
TSEG2 = 2 | ||
NOSAMP = 1 | ||
SJW = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"HOST": "localhost", | ||
"PORT": 5555, | ||
"PROTOCOL": "TCP", | ||
"IPV6": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HOST = "localhost" | ||
PORT = 5555 | ||
PROTOCOL = "TCP" | ||
IPV6 = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"PORT": "COM10", | ||
"BAUDRATE": 38400, | ||
"BYTESIZE": 8, | ||
"PARITY": "N", | ||
"STOPBITS": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PORT = "COM10" | ||
BAUDRATE = 38400 | ||
PARITY = "N" | ||
BYTESIZE = 8 | ||
STOPBITS = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
""" | ||
|
||
__copyright__ = """ | ||
pySART - Simplified AUTOSAR-Toolkit for Python. | ||
(C) 2009-2019 by Christoph Schueler <[email protected]> | ||
All Rights Reserved | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
""" | ||
|
||
|
||
|
||
from pprint import pprint | ||
|
||
from pyxcp.cmdline import ArgumentParser | ||
|
||
ap = ArgumentParser(description = "pyXCP hello world.") | ||
with ap.run() as x: | ||
x.connect() | ||
if x.slaveProperties.optionalCommMode: | ||
x.getCommModeInfo() | ||
gid = x.getId(0x1) | ||
result = x.fetch(gid.length) | ||
print("ID: '{}'".format(result.decode("utf8"))) | ||
|
||
x.disconnect() | ||
#print(ap.args) | ||
pprint(x.slaveProperties) |
Oops, something went wrong.