Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some bugs that lead to invalid XML being created #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,12 +811,11 @@ def _pduToXml2(self, xml, value, omitDeclaration, omitNameSpace, allowUnknownCom
p = AesGcmParameter(0, st, settings.cipher.blockCipherKey, settings.cipher.authenticationKey)
if p.blockCipherKey:
data2 = GXByteBuffer(GXCiphering.decrypt(settings.cipher, p, value))
xml.startComment("Decrypt data: " + str(data2))
xml.appendComment("Decrypt data: " + str(data2))
self._pduToXml2(xml, data2, omitDeclaration, omitNameSpace, False)
xml.endComment()
except Exception:
# It's OK if this fails. Ciphering settings are not correct.
xml.xml.setXmlLength(len_)
xml.setXmlLength(len_)
value.position = originalPosition
cnt = _GXCommon.getObjectCount(value)
if cnt != len(value) - value.position:
Expand All @@ -833,9 +832,8 @@ def _pduToXml2(self, xml, value, omitDeclaration, omitNameSpace, allowUnknownCom
p.xml = xml
tmp = GXByteBuffer(GXCiphering.decrypt(settings.cipher, p, tmp))
len_ = xml.getXmlLength()
xml.startComment("Decrypt data: " + str(tmp))
xml.appendComment("Decrypt data: " + str(tmp))
self._pduToXml2(xml, tmp, omitDeclaration, omitNameSpace, False)
xml.endComment()
except Exception:
# It's OK if this fails. Ciphering settings are not
# correct.
Expand Down
9 changes: 7 additions & 2 deletions Gurux.DLMS.python/gurux_dlms/GXDLMSTranslatorStructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, type_, numericsAshex, numericshex, isHex, addComments, list_)
# Amount of spaces.
self.offset = 0
self.outputType = type_
self.comment_started = False
numericsAshex = numericshex
self.showNumericsAsHex = numericsAshex
self.showStringAsHex = isHex
Expand Down Expand Up @@ -131,9 +132,11 @@ def appendLine(self, tag, name=None, value=None):
def appendComment(self, comment):
if self.comments:
self.appendSpaces(2 * self.offset)
self.sb += "<!--"
if not self.comment_started:
self.sb += "<!--"
self.sb += comment
self.sb += "-->"
if not self.comment_started:
self.sb += "-->"
self.sb += '\r'
self.sb += '\n'

Expand All @@ -151,6 +154,7 @@ def startComment(self, comment):
self.sb += '\r'
self.sb += '\n'
self.offset += 1
self.comment_started = True

#
# End comment section.
Expand All @@ -162,6 +166,7 @@ def endComment(self):
self.sb += "-->"
self.sb += '\r'
self.sb += '\n'
self.comment_started = False

def append(self, tag, start=None):
if isinstance(tag, str):
Expand Down
4 changes: 2 additions & 2 deletions Gurux.DLMS.python/gurux_dlms/_GXAPDU.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def parse(cls, initiateRequest, settings, cipher, data, xml, tag2):
raise GXDLMSConfirmedServiceError(ConfirmedServiceError(data.getUInt8()), ServiceError(data.getUInt8()), data.getUInt8())
else:
if xml:
xml.appendComment("Error: Failed to descypt data.")
xml.appendComment("Error: Failed to decrypt data.")
data.position = len(data)
return
raise ValueError("Invalid tag.")
Expand Down Expand Up @@ -433,7 +433,7 @@ def parse(cls, initiateRequest, settings, cipher, data, xml, tag2):
# The number of unused bits in the bit string.
tag = data.getUInt8()
v = cls.getConformanceToArray(data)
if settings.isServer:
if not response:
settings.negotiatedConformance = v & settings.proposedConformance
if xml:
xml.appendStartTag(TranslatorGeneralTags.PROPOSED_CONFORMANCE)
Expand Down