<SourceAddress Value="1" />
<FrameType Value="52" />
<PDU>
<!-- Invocation Counter: 1252 -->
<!-- Decrypt data: C4 02 81 01 00 00 00 01 00 82 00 38 01 03 02 04 12 00 08 09 06 00 00 01 00 00 FF 0F 02 12 00 00 02 04 12 00 01 09 06 00 00 60 0B 05 FF 0F 02 12 00 00 02 04 12 00 01 09 06 00 00 60 0F 05 FF 0F 02 12 00 00
<GetResponse>
<GetResponsewithDataBlock>
# Priority: High, ServiceClass: UnConfirmed, Invoke ID: 1
<InvokeIdAndPriority Value="81" />
<Result>
<LastBlock Value="01" />
<BlockNumber Value="00000001" />
<Result>
<RawData Value="0103020412000809060000010000FF0F02120000020412000109060000600B05FF0F02120000020412000109060000600F05FF0F02120000" />
</Result>
</Result>
</GetResponsewithDataBlock>
</GetResponse>
-->
<glo_GetResponse Value="30000004E4528E5D05921427F59267F07DBD90A843870ACF2D959C83B4CFA7EFC01575F1AC1C2F9EFE3ACACF00BEABC2A4A9D6B5989395D1059AD0CBE2CF6BD634C2879F8804B87E3504BA6B4E3095330CC666C179" />
</PDU>
</HDLC>
Forums
Hi, here i am facing the…
Hi,
here i am facing the problem with raw data parsing
Hi, You don't need to parse…
Hi,
You don't need to parse it from the XML. It's updated to the attribute and you can get it there. Like reading the invocation counter shows.
https://github.com/Gurux/Gurux.DLMS.Net/blob/c3b4b26bbcc2f566e238288aa6…
BR,
Mikko
then how can i get raw data
then how can i get raw data
Hi, If you want to get the…
Hi,
If you want to get the raw bytes, you can get the decrypted data from the data property before updateValue is called.
https://github.com/Gurux/Gurux.DLMS.Net/blob/c3b4b26bbcc2f566e238288aa6…
BR,
Mikko
Sir, Thank you for you…
Sir,
Thank you for you valuable time, but the problem still exists.
I want data2 value from the PduToXml method, so what should i needto do to get the value
internal string PduToXml(GXDLMSTranslatorStructure xml,
GXByteBuffer value,
bool omitDeclaration,
bool omitNameSpace,
bool allowUnknownCommand,
GXDLMSTranslatorMessage msg)
{
if (msg != null && msg.InterfaceType == InterfaceType.PrimeDcWrapper)
{
if (GXPrimeDcHandlers.HandleNotification(value, null, xml))
{
return xml.ToString();
}
}
GXDLMSSettings settings = new GXDLMSSettings(true, InterfaceType.HDLC);
try
{
GXReplyData data = new GXReplyData();
byte cmd = value.GetUInt8();
if (msg != null)
{
msg.Command = (Command)cmd;
}
GetCiphering(settings, GXDLMS.IsCiphered(cmd));
if (settings != null && settings.Cipher != null)
{
((GXCiphering)settings.Cipher).TestMode = true;
}
settings.Standard = Standard;
string str;
int len;
byte[] tmp;
switch (cmd)
{
case (byte)Command.Aarq:
value.Position = 0;
settings.SourceSystemTitle = null;
GXAPDU.ParsePDU(settings, settings.Cipher, value, xml);
//Update new dedicated key.
if (settings.Cipher != null && settings.Cipher.DedicatedKey != null)
{
DedicatedKey = settings.Cipher.DedicatedKey;
}
if (msg != null)
{
msg.SystemTitle = settings.SourceSystemTitle;
msg.DedicatedKey = settings.Cipher.DedicatedKey;
}
break;
case (byte)Command.InitiateRequest:
value.Position = 0;
settings = new GXDLMSSettings(true, InterfaceType.HDLC);
GXAPDU.ParseInitiate(true, settings, settings.Cipher, value,
xml);
break;
case (byte)Command.InitiateResponse:
value.Position = 0;
settings = new GXDLMSSettings(false, InterfaceType.HDLC);
GetCiphering(settings, true);
GXAPDU.ParseInitiate(true, settings, settings.Cipher, value,
xml);
break;
case 0x81://Ua
if (msg != null)
{
msg.Command = Command.Ua;
}
value.Position = 0;
GetUa(value, xml);
break;
case (byte)Command.Aare:
value.Position = 0;
settings = new GXDLMSSettings(false, InterfaceType.HDLC);
GetCiphering(settings, true);
settings.SourceSystemTitle = null;
GXAPDU.ParsePDU(settings, settings.Cipher, value, xml);
if (msg != null)
{
msg.SystemTitle = settings.SourceSystemTitle;
}
break;
case (byte)Command.GetRequest:
GXDLMSLNCommandHandler.HandleGetRequest(settings, null, value, null, xml, Command.None);
break;
case (byte)Command.SetRequest:
GXDLMSLNCommandHandler.HandleSetRequest(settings, null, value, null, xml, Command.None);
break;
case (byte)Command.ReadRequest:
GXDLMSSNCommandHandler.HandleReadRequest(settings, null, value, null, xml, Command.None);
break;
case (byte)Command.MethodRequest:
GXDLMSLNCommandHandler.HandleMethodRequest(settings, null, value, null, null, xml, Command.None);
break;
case (byte)Command.WriteRequest:
GXDLMSSNCommandHandler.HandleWriteRequest(settings, null, value, null, xml, Command.None);
break;
case (byte)Command.AccessRequest:
GXDLMSLNCommandHandler.HandleAccessRequest(settings, null, value, null, xml, Command.None);
break;
case (byte)Command.DataNotification:
data.Xml = xml;
data.Data = value;
value.Position = 0;
GXDLMS.GetPdu(settings, data);
break;
case (byte)Command.InformationReport:
data.Xml = xml;
data.Data = value;
GXDLMSSNCommandHandler.HandleInformationReport(settings, data, null);
break;
case (byte)Command.EventNotification:
data.Xml = xml;
data.Data = value;
GXDLMSLNCommandHandler.HandleEventNotification(settings, data, null);
break;
case (byte)Command.ReadResponse:
case (byte)Command.WriteResponse:
case (byte)Command.GetResponse:
case (byte)Command.SetResponse:
case (byte)Command.MethodResponse:
case (byte)Command.AccessResponse:
case (byte)Command.GeneralBlockTransfer:
data.Xml = xml;
data.Data = value;
value.Position = 0;
GXDLMS.GetPdu(settings, data);
break;
case (byte)Command.GeneralCiphering:
data.Xml = xml;
data.Data = value;
value.Position = 0;
GXDLMS.GetPdu(settings, data);
break;
case (byte)Command.ReleaseRequest:
xml.AppendStartTag((Command)cmd);
value.GetUInt8();
// Len.
if (value.Available != 0)
{
// BerType
value.GetUInt8();
// Len.
value.GetUInt8();
if (xml.OutputType == TranslatorOutputType.SimpleXml)
{
str = TranslatorSimpleTags.ReleaseRequestReasonToString((ReleaseRequestReason)value.GetUInt8());
}
else
{
str = TranslatorStandardTags.ReleaseRequestReasonToString((ReleaseRequestReason)value.GetUInt8());
}
xml.AppendLine(TranslatorTags.Reason, null, str);
if (value.Available != 0)
{
int len2 = xml.GetXmlLength();
try
{
xml.AppendStartTag(TranslatorGeneralTags.UserInformation);
GXAPDU.ParsePDU2(settings, settings.Cipher, value,
xml);
xml.AppendEndTag(TranslatorGeneralTags.UserInformation);
}
catch (Exception)
{
//This usually fails if keys are wrong.
xml.SetXmlLength(len2);
}
}
}
xml.AppendEndTag(cmd);
break;
case (byte)Command.ReleaseResponse:
xml.AppendStartTag((Command)cmd);
if (value.GetUInt8() != 0)
{
//BerType
value.GetUInt8();
//Len.
value.GetUInt8();
if (xml.OutputType == TranslatorOutputType.SimpleXml)
{
str = TranslatorSimpleTags.ReleaseResponseReasonToString((ReleaseResponseReason)value.GetUInt8());
}
else
{
str = TranslatorStandardTags.ReleaseResponseReasonToString((ReleaseResponseReason)value.GetUInt8());
}
xml.AppendLine(TranslatorTags.Reason, "Value", str);
if (value.Available != 0)
{
GXAPDU.ParsePDU2(settings, settings.Cipher, value, xml);
}
}
xml.AppendEndTag((Command)cmd);
break;
case (byte)Command.GloReadRequest:
case (byte)Command.GloWriteRequest:
case (byte)Command.GloGetRequest:
case (byte)Command.GloSetRequest:
case (byte)Command.GloReadResponse:
case (byte)Command.GloWriteResponse:
case (byte)Command.GloGetResponse:
case (byte)Command.GloSetResponse:
case (byte)Command.GloMethodRequest:
case (byte)Command.GloMethodResponse:
case (byte)Command.DedGetRequest:
case (byte)Command.DedSetRequest:
case (byte)Command.DedReadResponse:
case (byte)Command.DedWriteResponse:
case (byte)Command.DedGetResponse:
case (byte)Command.DedSetResponse:
case (byte)Command.DedMethodRequest:
case (byte)Command.DedMethodResponse:
case (byte)Command.GloConfirmedServiceError:
case (byte)Command.DedConfirmedServiceError:
if (settings.Cipher != null && Comments)
{
int originalPosition = value.Position;
int len2 = xml.GetXmlLength();
try
{
--value.Position;
Command c = (Command)cmd;
byte[] st;
if (c == Command.GloReadRequest || c == Command.GloWriteRequest || c == Command.GloGetRequest || c == Command.GloSetRequest || c == Command.GloMethodRequest ||
c == Command.DedGetRequest || c == Command.DedSetRequest || c == Command.DedMethodRequest)
{
st = settings.Cipher.SystemTitle;
}
else
{
st = settings.SourceSystemTitle;
}
if (st != null)
{
AesGcmParameter p;
if ((cmd == (byte)Command.DedGetRequest || cmd == (byte)Command.DedSetRequest ||
cmd == (byte)Command.DedReadResponse || cmd == (byte)Command.DedWriteResponse ||
cmd == (byte)Command.DedGetResponse || cmd == (byte)Command.DedSetResponse ||
cmd == (byte)Command.DedMethodRequest || cmd == (byte)Command.DedMethodResponse) &&
settings.Cipher.DedicatedKey != null && settings.Cipher.DedicatedKey.Length != 0)
{
p = new AesGcmParameter(settings, st, settings.Cipher.DedicatedKey, settings.Cipher.AuthenticationKey);
}
else
{
p = new AesGcmParameter(settings, st, settings.Cipher.BlockCipherKey, settings.Cipher.AuthenticationKey);
}
p.Xml = xml;
if (p.BlockCipherKey != null)
{
GXByteBuffer data2 = new GXByteBuffer(GXSecure.DecryptAesGcm(p, value));
xml.AppendComment("Invocation Counter: " + p.InvocationCounter.ToString());
xml.StartComment("Decrypt data: " + data2.ToString());
PduToXml(xml, data2, omitDeclaration, omitNameSpace, false, msg);
xml.EndComment();
}
}
}
catch (Exception)
{
// It's OK if this fails. Ciphering settings are not correct.
if (msg != null)
{
msg.Command = (Command)cmd;
}
xml.SetXmlLength(len2);
xml.AppendComment("Decrypt failed. Different block cipher or authentiocation key is used to generate this.");
value.Position = originalPosition;
break;
}
value.Position = originalPosition;
}
int cnt = GXCommon.GetObjectCount(value);
if (cnt != value.Size - value.Position)
{
xml.AppendComment("Invalid length: " + cnt + ". It should be: " + (value.Size - value.Position));
}
xml.AppendLine(cmd, "Value", GXCommon.ToHex(value.Data, false, value.Position, value.Size - value.Position));
break;
case (byte)Command.GeneralGloCiphering:
case (byte)Command.GeneralDedCiphering:
{
AesGcmParameter p = null;
if (settings.Cipher != null && Comments)
{
int len2 = xml.GetXmlLength();
int originalPosition = value.Position;
--value.Position;
//Check is this client msg.
try
{
byte[] st;
st = settings.Cipher.SystemTitle;
if (cmd == (byte)Command.GeneralDedCiphering && settings.Cipher.DedicatedKey != null)
{
p = new AesGcmParameter(settings, st, settings.Cipher.DedicatedKey, settings.Cipher.AuthenticationKey);
}
else
{
p = new AesGcmParameter(settings, st, settings.Cipher.BlockCipherKey, settings.Cipher.AuthenticationKey);
}
p.Xml = xml;
GXByteBuffer data2 = new GXByteBuffer(GXSecure.DecryptAesGcm(p, value));
len2 = xml.GetXmlLength();
xml.AppendComment("Invocation Counter: " + p.InvocationCounter.ToString());
xml.StartComment("Decrypt data: " + data2.ToString());
PduToXml(xml, data2, omitDeclaration, omitNameSpace, false, msg);
xml.EndComment();
}
catch (Exception)
{
xml.SetXmlLength(len2);
value.Position = originalPosition;
--value.Position;
//Check is this server msg.
try
{
byte[] st;
st = settings.SourceSystemTitle;
if (st != null)
{
p = new AesGcmParameter(settings, st, settings.Cipher.BlockCipherKey, settings.Cipher.AuthenticationKey);
p.Xml = xml;
GXByteBuffer data2 = new GXByteBuffer(GXSecure.DecryptAesGcm(p, value));
xml.AppendComment("Invocation Counter: " + p.InvocationCounter.ToString());
xml.StartComment("Decrypt data: " + data2.ToString());
PduToXml(xml, data2, omitDeclaration, omitNameSpace, false, msg);
xml.EndComment();
}
}
catch (Exception)
{
// It's OK if this fails. Ciphering settings are not correct.
if (msg != null)
{
msg.Command = (Command)cmd;
}
xml.SetXmlLength(len2);
}
}
value.Position = originalPosition;
}
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendStartTag((Command)cmd);
xml.AppendLine(TranslatorTags.SystemTitle, null,
GXCommon.ToHex(tmp, false, 0, len));
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.CipheredService, null,
GXCommon.ToHex(tmp, false, 0, len));
xml.AppendEndTag(cmd);
}
break;
case (byte)Command.GeneralSigning:
{
xml.AppendStartTag((Command)cmd);
AesGcmParameter p = null;
int originalPosition = value.Position;
if (settings.Cipher != null && Comments)
{
int len2 = xml.GetXmlLength();
--value.Position;
try
{
byte[] st;
st = settings.Cipher.SystemTitle;
p = new AesGcmParameter(settings, st, settings.Cipher.BlockCipherKey, settings.Cipher.AuthenticationKey);
p.Xml = xml;
GXByteBuffer data2 = new GXByteBuffer(GXSecure.DecryptAesGcm(p, value));
xml.AppendComment("Security : " + p.Security);
xml.AppendComment("Security Suite: " + p.SecuritySuite);
xml.AppendComment("Invocation Counter: " + p.InvocationCounter.ToString());
xml.StartComment("Decrypt data: " + data2.ToString());
PduToXml(xml, data2, omitDeclaration, omitNameSpace, false, msg);
xml.EndComment();
p.Xml.AppendLine(TranslatorTags.Content, null, GXCommon.ToHex(p.CipheredContent, false));
p.Xml.AppendLine(TranslatorTags.Signature, null, GXCommon.ToHex(p.Signature, false));
}
catch (Exception)
{
// It's OK if this fails. Ciphering settings are not correct.
if (msg != null)
{
msg.Command = (Command)cmd;
}
xml.SetXmlLength(len2);
value.Position = originalPosition;
}
}
if (value.Position == originalPosition)
{
GXByteBuffer transactionId = new GXByteBuffer();
len = GXCommon.GetObjectCount(value);
GXCommon.SetObjectCount(len, transactionId);
transactionId.Set(value, len);
xml.AppendLine(TranslatorTags.TransactionId, null, xml.IntegerToHex(transactionId.GetUInt64(1), 16, true));
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.OriginatorSystemTitle, null, GXCommon.ToHex(tmp, false));
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.RecipientSystemTitle, null, GXCommon.ToHex(tmp, false));
// Get date time.
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.DateTime, null, GXCommon.ToHex(tmp, false));
// other-information
len = value.GetUInt8();
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.OtherInformation, null, GXCommon.ToHex(tmp, false));
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.Content, null, GXCommon.ToHex(tmp, false));
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendLine(TranslatorTags.Signature, null, GXCommon.ToHex(tmp, false));
}
value.Position = originalPosition;
xml.AppendEndTag(cmd);
}
break;
case (byte)Command.ConfirmedServiceError:
data.Xml = xml;
data.Data = value;
GXDLMS.HandleConfirmedServiceError(data);
break;
case (byte)Command.GatewayRequest:
case (byte)Command.GatewayResponse:
data.Xml = xml;
data.Data = value;
//Get Network ID.
byte id = value.GetUInt8();
//Get Physical device address.
len = GXCommon.GetObjectCount(value);
tmp = new byte[len];
value.Get(tmp);
xml.AppendStartTag((Command)cmd);
xml.AppendLine(TranslatorTags.NetworkId, null, id.ToString());
xml.AppendLine(TranslatorTags.PhysicalDeviceAddress, null, GXCommon.ToHex(tmp, false, 0, len));
PduToXml(xml, new GXByteBuffer(value.Remaining()), omitDeclaration, omitNameSpace, allowUnknownCommand, msg);
xml.AppendEndTag(cmd);
break;
case (byte)Command.ExceptionResponse:
data.Xml = xml;
data.Data = value;
GXDLMS.HandleExceptionResponse(data);
break;
case (byte)Command.DiscoverRequest:
data.Xml = xml;
data.Data = value;
xml.AppendStartTag((Command)cmd);
HandleDiscoverRequest(data);
xml.AppendEndTag((Command)cmd);
break;
case (byte)Command.DiscoverReport:
data.Xml = xml;
data.Data = value;
xml.AppendStartTag((Command)cmd);
HandleDiscoverResponse(settings, data);
xml.AppendEndTag((Command)cmd);
break;
case (byte)Command.RegisterRequest:
data.Xml = xml;
data.Data = value;
xml.AppendStartTag((Command)cmd);
HandleRegisterRequest(msg.InterfaceType, data);
xml.AppendEndTag((Command)cmd);
break;
case (byte)Command.PingResponse:
data.Xml = xml;
data.Data = value;
xml.AppendStartTag((Command)cmd);
GXDLMS.HandleExceptionResponse(data);
xml.AppendEndTag((Command)cmd);
break;
case (byte)Command.RepeatCallRequest:
data.Xml = xml;
data.Data = value;
xml.AppendStartTag((Command)cmd);
GXDLMS.HandleExceptionResponse(data);
xml.AppendEndTag((Command)cmd);
break;
default:
if (!allowUnknownCommand)
{
throw new Exception("Invalid command.");
}
--value.Position;
xml.AppendLine("<Data=\"" + GXCommon.ToHex(value.Data, false, value.Position, value.Size - value.Position) + "\" />");
break;
}
if (OutputType == TranslatorOutputType.StandardXml)
{
StringBuilder sb = new StringBuilder();
if (!omitDeclaration)
{
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
}
if (!omitNameSpace)
{
if (cmd != (byte)Command.Aare && cmd != (byte)Command.Aarq)
{
sb.AppendLine(
"<x:xDLMS-APDU xmlns:x=\"http://www.dlms.com/COSEMpdu\">");
}
else
{
sb.AppendLine(
"<x:aCSE-APDU xmlns:x=\"http://www.dlms.com/COSEMpdu\">");
}
}
sb.Append(xml.ToString());
if (!omitNameSpace)
{
if (cmd != (byte)Command.Aare && cmd != (byte)Command.Aarq)
{
sb.AppendLine("</x:xDLMS-APDU>");
}
else
{
sb.AppendLine("</x:aCSE-APDU>");
}
}
return sb.ToString();
}
return xml.ToString();
}
finally
{
if (settings != null && settings.Cipher != null)
{
((GXCiphering)settings.Cipher).TestMode = false;
}
}
}