/// <summary> /// Convert message to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TranslateBtn_Click(object sender, EventArgs e) { MessageXmlTB.Text = ""; StringBuilder sb = new StringBuilder(); GXByteBuffer bb = new GXByteBuffer(); //TODO: This can remove later. Security s = translator.Security; try { translator.Clear(); UpdateSecurity(); translator.Security = Security.Authentication; translator.PduOnly = PduOnlyCB.Checked; GXByteBuffer pdu = new GXByteBuffer(); bb.Set(GXDLMSTranslator.HexToBytes(RemoveComments(MessagePduTB.Text))); InterfaceType type = GXDLMSTranslator.GetDlmsFraming(bb); int cnt = 1; while (translator.FindNextFrame(bb, pdu, type)) { int start = bb.Position; string tmp = translator.MessageToXml(bb); sb.AppendLine(cnt + ": " + bb.ToHex(true, start, bb.Position - start)); ++cnt; sb.Append(tmp); pdu.Clear(); } MessageXmlTB.Text = sb.ToString(); translator.Security = s; } catch (Exception ex) { translator.Security = s; MessageXmlTB.AppendText(sb.ToString()); MessageXmlTB.AppendText(Environment.NewLine); MessageXmlTB.AppendText(bb.RemainingHexString(true)); MessageBox.Show(this, ex.Message); } }
/// <summary> /// Convert message to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TranslateBtn_Click(object sender, EventArgs e) { MessageXmlTB.Text = ""; StringBuilder sb = new StringBuilder(); GXByteBuffer bb = new GXByteBuffer(); //TODO: This can remove later. byte s = translator.Security; try { translator.Clear(); UpdateSecurity(); translator.Security = (byte)Security.Authentication; translator.PduOnly = PduOnlyCB.Checked; GXByteBuffer pdu = new GXByteBuffer(); bb.Set(GXDLMSTranslator.HexToBytes(RemoveComments(MessagePduTB.Text))); InterfaceType type = GXDLMSTranslator.GetDlmsFraming(bb); int cnt = 1; string last = ""; while (translator.FindNextFrame(bb, pdu, type)) { int start = bb.Position; GXDLMSTranslatorMessage msg = new GXDLMSTranslatorMessage(); msg.Message = bb; translator.MessageToXml(msg); //Remove duplicate messages. if (RemoveDuplicatesCb.Checked) { if (last == msg.Xml) { continue; } } last = msg.Xml; if (msg.Command == Command.Aarq) { if (msg.SystemTitle != null) { string st = UpdateSystemTitle(this, "Current System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, SystemTitleTB.Text, SystemTitleAsciiCb.Checked); if (st != null) { SystemTitleTB.Text = ""; SystemTitleAsciiCb.Checked = false; SystemTitleTB.Text = st; } } if (msg.DedicatedKey != null) { string key = UpdateSystemTitle(this, "Current dedicated key \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.DedicatedKey, DedicatedKeyTb.Text, DedicatedKeyAsciiCb.Checked); if (key != null) { DedicatedKeyTb.Text = ""; DedicatedKeyAsciiCb.Checked = false; DedicatedKeyTb.Text = key; } } } if (msg.Command == Command.Aare && msg.SystemTitle != null) { string st = UpdateSystemTitle(this, "Current Server System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, ServerSystemTitleTB.Text, ServerSystemTitleAsciiCb.Checked); if (st != null) { ServerSystemTitleTB.Text = ""; SystemTitleAsciiCb.Checked = false; ServerSystemTitleTB.Text = st; } } if (!AllRb.Checked) { switch (msg.Command) { case Command.None: break; case Command.InitiateRequest: case Command.ReadRequest: case Command.WriteRequest: case Command.GetRequest: case Command.SetRequest: case Command.MethodRequest: case Command.Snrm: case Command.Aarq: case Command.ReleaseRequest: case Command.DisconnectRequest: case Command.AccessRequest: case Command.GloGetRequest: case Command.GloSetRequest: case Command.GloMethodRequest: case Command.GloInitiateRequest: case Command.GloReadRequest: case Command.GloWriteRequest: case Command.DedInitiateRequest: case Command.DedReadRequest: case Command.DedWriteRequest: case Command.DedGetRequest: case Command.DedSetRequest: case Command.DedMethodRequest: case Command.GatewayRequest: if (ReceivedRb.Checked) { continue; } break; case Command.InitiateResponse: case Command.ReadResponse: case Command.WriteResponse: case Command.GetResponse: case Command.SetResponse: case Command.MethodResponse: case Command.Ua: case Command.Aare: case Command.ReleaseResponse: case Command.AccessResponse: case Command.GloGetResponse: case Command.GloSetResponse: case Command.GloMethodResponse: case Command.GloInitiateResponse: case Command.GloReadResponse: case Command.GloWriteResponse: case Command.DedInitiateResponse: case Command.DedReadResponse: case Command.DedWriteResponse: case Command.DedGetResponse: case Command.DedSetResponse: case Command.DedMethodResponse: case Command.GatewayResponse: if (SentRb.Checked) { continue; } break; case Command.DisconnectMode: case Command.UnacceptableFrame: case Command.ConfirmedServiceError: case Command.ExceptionResponse: case Command.GeneralBlockTransfer: case Command.DataNotification: case Command.GloEventNotification: case Command.GloConfirmedServiceError: case Command.GeneralGloCiphering: case Command.GeneralDedCiphering: case Command.GeneralCiphering: case Command.InformationReport: case Command.EventNotification: case Command.DedConfirmedServiceError: case Command.DedUnconfirmedWriteRequest: case Command.DedInformationReport: case Command.DedEventNotification: break; } } sb.AppendLine(cnt + ": " + bb.ToHex(true, start, bb.Position - start)); ++cnt; sb.Append(msg.Xml); pdu.Clear(); } MessageXmlTB.Text = sb.ToString(); translator.Security = s; } catch (Exception ex) { translator.Security = s; MessageXmlTB.AppendText(sb.ToString()); MessageXmlTB.AppendText(Environment.NewLine); MessageXmlTB.AppendText(bb.RemainingHexString(true)); MessageBox.Show(this, ex.Message); } }
/// <summary> /// Convert message to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TranslateBtn_Click(object sender, EventArgs e) { if (MessagePduTB.ReadOnly) { StatusLbl.Text = "Cancelling translate."; CancelTranslate.Set(); return; } TranslateBtn.Checked = true; MessagePduTB.ReadOnly = true; Follow follow = (Follow)FollowmessagesCb.SelectedItem; ShowMessages show = (ShowMessages)ShowCb.SelectedItem; MessageXmlTB.Text = null; ProgressBar.Visible = true; object selectedInterface = InterfaceCb.SelectedItem; UpdateSecuritySettings(); StatusLbl.Text = "Finding frames"; GXByteBuffer bb = new GXByteBuffer(); bb.Set(GXDLMSTranslator.HexToBytes(RemoveComments(string.Join(Environment.NewLine, MessagePduTB.Lines)))); System.Threading.Tasks.Task.Run(async() => { if (translator.BlockCipherKey != null) { OnAppendMessage("BlockCipher key: " + GXDLMSTranslator.ToHex(translator.BlockCipherKey) + Environment.NewLine, Color.Green); } if (translator.AuthenticationKey != null) { OnAppendMessage("Authentication Key:" + GXDLMSTranslator.ToHex(translator.AuthenticationKey) + Environment.NewLine, Color.Green); } StringBuilder sb = new StringBuilder(); Security s = translator.Security; int count = 1; try { translator.Clear(); translator.PduOnly = PduOnlyMnu.Checked; GXByteBuffer pdu = new GXByteBuffer(); UpdateProgress(0, 0); UpdateMaxProgress(bb.Size, 0); GXDLMSTranslatorMessage frame = new GXDLMSTranslatorMessage(); frame.Message = bb; if (selectedInterface is string) { frame.InterfaceType = GXDLMSTranslator.GetDlmsFraming(bb); BeginInvoke((Action)(() => { InterfaceCb.SelectedItem = frame.InterfaceType; })); } else { frame.InterfaceType = (InterfaceType)selectedInterface; } string last = ""; int clientAddress = 0, serverAddress = 0; while (translator.FindNextFrame(frame, pdu, clientAddress, serverAddress)) { //Translate is cancelled. if (CancelTranslate.WaitOne(1)) { UpdateMaxProgress(0, 0); BeginInvoke((Action)(() => { MessagePduTB.ReadOnly = false; TranslateBtn.Checked = false; })); CancelTranslate.Reset(); return; } int start = bb.Position; UpdateProgress(start, count); GXDLMSTranslatorMessage msg = new GXDLMSTranslatorMessage(); msg.Message = bb; translator.MessageToXml(msg); if (follow != Follow.None) { if ((follow & Follow.Client) != 0 && clientAddress == 0) { clientAddress = msg.SourceAddress; } if ((follow & Follow.Meter) != 0 && serverAddress == 0) { serverAddress = msg.TargetAddress; } } //Remove duplicate messages. if (RemoveDuplicatesMnu.Checked) { if (last == msg.Xml) { continue; } } last = msg.Xml; if (msg.Command == Command.Aarq) { if (msg.SystemTitle != null && msg.SystemTitle.Length == 8) { if (UpdateSystemTitle(this, "Current System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, translator.SystemTitle)) { translator.SystemTitle = msg.SystemTitle; BeginInvoke((Action)(() => { Ciphering.SystemTitle = msg.SystemTitle; })); } } if (msg.DedicatedKey != null && msg.DedicatedKey.Length == 16) { if (UpdateSystemTitle(this, "Current dedicated key \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.DedicatedKey, translator.DedicatedKey)) { translator.DedicatedKey = msg.DedicatedKey; BeginInvoke((Action)(() => { Ciphering.DedicatedKey = msg.DedicatedKey; })); } } } if (msg.Command == Command.Aare && msg.SystemTitle != null && msg.SystemTitle.Length == 8) { if (UpdateSystemTitle(this, "Current Server System title \"{0}\" is different in the parsed \"{1}\". Do you want to start using parsed one?", msg.SystemTitle, translator.ServerSystemTitle)) { translator.ServerSystemTitle = msg.SystemTitle; BeginInvoke((Action)(() => { Ciphering.ServerSystemTitle = msg.SystemTitle; })); } } if (show != ShowMessages.All) { switch (msg.Command) { case Command.None: break; case Command.InitiateRequest: case Command.ReadRequest: case Command.WriteRequest: case Command.GetRequest: case Command.SetRequest: case Command.MethodRequest: case Command.Snrm: case Command.Aarq: case Command.ReleaseRequest: case Command.DisconnectRequest: case Command.AccessRequest: case Command.GloGetRequest: case Command.GloSetRequest: case Command.GloMethodRequest: case Command.GloInitiateRequest: case Command.GloReadRequest: case Command.GloWriteRequest: case Command.DedInitiateRequest: case Command.DedReadRequest: case Command.DedWriteRequest: case Command.DedGetRequest: case Command.DedSetRequest: case Command.DedMethodRequest: case Command.GatewayRequest: if (show == ShowMessages.Received) { continue; } break; case Command.InitiateResponse: case Command.ReadResponse: case Command.WriteResponse: case Command.GetResponse: case Command.SetResponse: case Command.MethodResponse: case Command.Ua: case Command.Aare: case Command.ReleaseResponse: case Command.AccessResponse: case Command.GloGetResponse: case Command.GloSetResponse: case Command.GloMethodResponse: case Command.GloInitiateResponse: case Command.GloReadResponse: case Command.GloWriteResponse: case Command.DedInitiateResponse: case Command.DedReadResponse: case Command.DedWriteResponse: case Command.DedGetResponse: case Command.DedSetResponse: case Command.DedMethodResponse: case Command.GatewayResponse: if (show == ShowMessages.Sent) { continue; } break; case Command.DisconnectMode: case Command.UnacceptableFrame: case Command.ConfirmedServiceError: case Command.ExceptionResponse: case Command.GeneralBlockTransfer: case Command.DataNotification: case Command.GloEventNotification: case Command.GloConfirmedServiceError: case Command.GeneralGloCiphering: case Command.GeneralDedCiphering: case Command.GeneralCiphering: case Command.GeneralSigning: case Command.InformationReport: case Command.EventNotification: case Command.DedConfirmedServiceError: case Command.DedUnconfirmedWriteRequest: case Command.DedInformationReport: case Command.DedEventNotification: break; } } if (Properties.Settings.Default.TranslatorFrame) { if (Properties.Settings.Default.FrameNumber) { sb.Append(count + ": "); } sb.AppendLine(bb.ToHex(true, start, bb.Position - start)); } if (Properties.Settings.Default.TranslatorXml) { sb.Append(msg.Xml); } if (msg.Exception != null) { ++count; OnAppendMessage(sb.ToString(), Color.Red); } else if (show != ShowMessages.Failed) { ++count; OnAppendMessage(sb.ToString()); } sb.Clear(); pdu.Clear(); } OnAppendMessage(sb.ToString()); translator.Security = s; //Update UI. await System.Threading.Tasks.Task.Delay(1); } catch (Exception ex) { translator.Security = s; OnAppendMessage(sb.ToString()); OnAppendMessage(Environment.NewLine); OnAppendMessage(bb.RemainingHexString(true)); MessageBox.Show(ex.Message); } //Count starts from 1. UpdateMaxProgress(0, count - 1); BeginInvoke((Action)(() => { MessagePduTB.ReadOnly = false; TranslateBtn.Checked = false; })); }); }