Skip to content

Commit

Permalink
Python 3.4 back-patches
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Dec 13, 2018
1 parent 1d78e2f commit 613f8b2
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 54 deletions.
54 changes: 54 additions & 0 deletions pyxcp/master/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__copyright__="""
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2009-2018 by Christoph Schueler <cpu12.gems@googlemail.com>
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 sys

version = sys.version_info
PRE35 = version.major >= 3 and version.minor < 5 # We need some pre-3.5 fixes, e.g. flatten() function.

if PRE35:
from pyxcp.master.pre35 import Master
else:
from pyxcp.master.py35 import Master


def unlock(client, privilege):
length, seed = client.getSeed(0, privilege)
#print("SEED: ", hexDump(seed), flush = True)
_, kee = dllif.getKey(b"SeedNKeyXcp.dll", privilege, seed)
print("KEE:", kee)
# res = client.unlock(len(kee), kee)
#print(res)


def verify(client, addr, length):
client.setMta(addr)
cs = client.buildChecksum(length)
print("CS: {:08X}".format(cs.checksum))
client.setMta(addr)
data = client.upload(length)
cc = checksum.check(data, cs.checksumType)
print("CS: {:08X}".format(cc))

56 changes: 2 additions & 54 deletions pyxcp/master.py → pyxcp/master/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from pyxcp import checksum
from pyxcp import types

class Master(object):

class MasterBaseType(object):

def __init__(self, transport):
self.ctr = 0
Expand Down Expand Up @@ -248,19 +249,6 @@ def downloadMax(self, *data):
response = self.transport.request(types.Command.DOWNLOAD_MAX, *data)
return response

def shortDownload(self, address, addressExt, *data):
length = len(data)
addr = struct.pack("<I", address)
response = self.transport.request(types.Command.SHORT_DOWNLOAD, length, 0, addressExt, *addr, *data)
return response

def modifyBits(self, shiftValue, andMask, xorMask):
# A = ( (A) & ((~((dword)(((word)~MA)<<S))) )^((dword)(MX<<S)) )
am = struct.pack("<H", andMask)
xm = struct.pack("<H", xorMask)
response = self.transport.request(types.Command.MODIFY_BITS, shiftValue, *am, *xm)
return response

##
## Page Switching Commands (PAG)
##
Expand Down Expand Up @@ -309,22 +297,11 @@ def clearDaqList(self, daqListNumber):
response = self.transport.request(types.Command.CLEAR_DAQ_LIST, 0, *daqList)
return response

def setDaqPtr(self, daqListNumber, odtNumber, odtEntryNumber):
daqList = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.SET_DAQ_PTR, 0, *daqList, odtNumber, odtEntryNumber)
return response

def writeDaq(self, bitOffset, entrySize, addressExt, address):
addr = struct.pack("<I", address)
response = self.transport.request(types.Command.WRITE_DAQ, bitOffset, entrySize, addressExt, *addr)
return response

def setDaqListMode(self, mode, daqListNumber, eventChannelNumber, prescaler, priority):
dln = struct.pack("<H", daqListNumber)
ecn = struct.pack("<H", eventChannelNumber)
response = self.transport.request(types.Command.SET_DAQ_LIST_MODE, mode, *dln, *ecn, prescaler, priority)
return response

def getDaqListMode(self, daqListNumber):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.GET_DAQ_LIST_MODE, 0, *dln)
Expand Down Expand Up @@ -377,16 +354,6 @@ def allocDaq(self, daqCount):
response = self.transport.request(types.Command.ALLOC_DAQ, 0, *dq)
return response

def allocOdt(self, daqListNumber, odtCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT, 0, *dln, odtCount)
return response

def allocOdtEntry(self, daqListNumber, odtNumber, odtEntriesCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT_ENTRY, 0, *dln, odtNumber, odtEntriesCount)
return response

##
## PGM
##
Expand All @@ -412,22 +379,3 @@ def program(self):
ELEMENT Data elements
"""


def unlock(client, privilege):
length, seed = client.getSeed(0, privilege)
#print("SEED: ", hexDump(seed), flush = True)
_, kee = dllif.getKey(b"SeedNKeyXcp.dll", privilege, seed)
print("KEE:", kee)
# res = client.unlock(len(kee), kee)
#print(res)


def verify(client, addr, length):
client.setMta(addr)
cs = client.buildChecksum(length)
print("CS: {:08X}".format(cs.checksum))
client.setMta(addr)
data = client.upload(length)
cc = checksum.check(data, cs.checksumType)
print("CS: {:08X}".format(cc))

66 changes: 66 additions & 0 deletions pyxcp/master/pre35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__copyright__="""
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2009-2018 by Christoph Schueler <cpu12.gems@googlemail.com>
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 pyxcp.master.base import MasterBaseType
from pyxcp.utils import flatten

class Master(MasterBaseType):
##
## Python <= 3.4 requires nasty hack (flatten) to unpack tuples.
##

def shortDownload(self, address, addressExt, *data):
length = len(data)
addr = struct.pack("<I", address)
response = self.transport.request(types.Command.SHORT_DOWNLOAD, length, 0, addressExt, flatten(addr, data))
return response

def modifyBits(self, shiftValue, andMask, xorMask):
# A = ( (A) & ((~((dword)(((word)~MA)<<S))) )^((dword)(MX<<S)) )
am = struct.pack("<H", andMask)
xm = struct.pack("<H", xorMask)
response = self.transport.request(types.Command.MODIFY_BITS, shiftValue, flatten(am, xm))
return response

def setDaqPtr(self, daqListNumber, odtNumber, odtEntryNumber):
daqList = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.SET_DAQ_PTR, 0, flatten(daqList), odtNumber, odtEntryNumber)
return response

def setDaqListMode(self, mode, daqListNumber, eventChannelNumber, prescaler, priority):
dln = struct.pack("<H", daqListNumber)
ecn = struct.pack("<H", eventChannelNumber)
response = self.transport.request(types.Command.SET_DAQ_LIST_MODE, mode, flatten(dln, ecn), prescaler, priority)
return response

def allocOdt(self, daqListNumber, odtCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT, 0, flatten(dln), odtCount)
return response

def allocOdtEntry(self, daqListNumber, odtNumber, odtEntriesCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT_ENTRY, 0, flatten(dln), odtNumber, odtEntriesCount)
return response
67 changes: 67 additions & 0 deletions pyxcp/master/py35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__copyright__="""
pySART - Simplified AUTOSAR-Toolkit for Python.
(C) 2009-2018 by Christoph Schueler <cpu12.gems@googlemail.com>
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 pyxcp.master.base import MasterBaseType


class Master(MasterBaseType):
##
## Python >= 3.5 permits nice unpacking syntax (PEP448).
##

def shortDownload(self, address, addressExt, *data):
length = len(data)
addr = struct.pack("<I", address)
response = self.transport.request(types.Command.SHORT_DOWNLOAD, length, 0, addressExt, *addr, *data)
return response

def modifyBits(self, shiftValue, andMask, xorMask):
# A = ( (A) & ((~((dword)(((word)~MA)<<S))) )^((dword)(MX<<S)) )
am = struct.pack("<H", andMask)
xm = struct.pack("<H", xorMask)
response = self.transport.request(types.Command.MODIFY_BITS, shiftValue, *am, *xm)
return response

def setDaqPtr(self, daqListNumber, odtNumber, odtEntryNumber):
daqList = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.SET_DAQ_PTR, 0, *daqList, odtNumber, odtEntryNumber)
return response

def setDaqListMode(self, mode, daqListNumber, eventChannelNumber, prescaler, priority):
dln = struct.pack("<H", daqListNumber)
ecn = struct.pack("<H", eventChannelNumber)
response = self.transport.request(types.Command.SET_DAQ_LIST_MODE, mode, *dln, *ecn, prescaler, priority)
return response

def allocOdt(self, daqListNumber, odtCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT, 0, *dln, odtCount)
return response

def allocOdtEntry(self, daqListNumber, odtNumber, odtEntriesCount):
dln = struct.pack("<H", daqListNumber)
response = self.transport.request(types.Command.ALLOC_ODT_ENTRY, 0, *dln, odtNumber, odtEntriesCount)
return response

0 comments on commit 613f8b2

Please sign in to comment.