public GXServerAddress GetServer(HDLCAddressType type) { foreach (GXServerAddress it in this.ServerSettings) { if (it.HDLCAddress == type) { return(it); } } return(null); }
/// <summary> /// Count server address from physical and logical addresses. /// </summary> /// <param name="addressing"></param> /// <param name="formula"></param> /// <param name="physicalAddress"></param> /// <param name="LogicalAddress"></param> /// <returns></returns> public static object CountServerAddress(HDLCAddressType addressing, string formula, object physicalAddress, int LogicalAddress) { object value; if (addressing == HDLCAddressType.Custom) { value = Convert.ChangeType(physicalAddress, physicalAddress.GetType()); } else if (addressing == HDLCAddressType.Default || addressing == HDLCAddressType.SerialNumber) { if (addressing == HDLCAddressType.SerialNumber) { Type type = typeof(UInt32); physicalAddress = Convert.ChangeType(EvaluateSN(Convert.ToInt32(physicalAddress), formula), type); } if (physicalAddress is byte) { value = ((LogicalAddress & 0x7) << 5) | (((byte)physicalAddress & 0x7) << 1) | 0x1; value = Convert.ChangeType(value, typeof(byte)); } else if (physicalAddress is UInt16) { int physicalID = Convert.ToInt32(physicalAddress); int logicalID = Convert.ToInt32(LogicalAddress); int total = (physicalID) << 1 | 1; value = Convert.ToUInt32(total | (logicalID << 9)); value = Convert.ChangeType(value, typeof(UInt16)); } else if (physicalAddress is UInt32) { int physicalID = Convert.ToInt32(physicalAddress); int logicalID = Convert.ToInt32(LogicalAddress); int total = (((physicalID >> 7) & 0x7F) << 8) | (physicalID & 0x7F); value = Convert.ToUInt32(((total << 1) | 1 | (logicalID << 17))); } else { throw new Exception("Unknown physical address type."); } } else { throw new Exception("Invalid HDLCAddressing"); } return(value); }
private void ManufacturerCB_SelectedIndexChanged(object sender, EventArgs e) { try { GXManufacturer man = (GXManufacturer)ManufacturerCB.SelectedItem; StartProtocolCB.SelectedItem = man.StartProtocol; this.ClientAddTB.Value = Convert.ToDecimal(Convert.ToUInt32(man.GetActiveAuthentication().ClientID)); AuthenticationCB.Items.Clear(); foreach (GXAuthentication it in man.Settings) { int pos = AuthenticationCB.Items.Add(it); if (it.Type == Device.Authentication) { this.AuthenticationCB.SelectedIndex = pos; } } ServerAddressTypeCB.Items.Clear(); HDLCAddressType type = Device.HDLCAddressing; //If we are creating new device. if (Device.Name == null) { type = man.GetActiveServer().HDLCAddress; } foreach (GXServerAddress it in ((GXManufacturer)ManufacturerCB.SelectedItem).ServerSettings) { ServerAddressTypeCB.Items.Add(it); if (it.HDLCAddress == type) { ServerAddressTypeCB.SelectedItem = it; } } UpdateStartProtocol(); } catch (Exception Ex) { GXDLMS.Common.Error.ShowError(this, Ex); } }
public GXServerAddress GetServer(HDLCAddressType type) { foreach (GXServerAddress it in this.ServerSettings) { if (it.HDLCAddress == type) { return it; } } return null; }
/// <summary> /// Constructor. /// </summary> public GXServerAddress(HDLCAddressType address, int value, bool enabled) { HDLCAddress = address; PhysicalAddress = value; }
private void ManufacturerCB_SelectedIndexChanged(object sender, EventArgs e) { try { GXManufacturer man = (GXManufacturer)ManufacturerCB.SelectedItem; StartProtocolCB.SelectedItem = man.StartProtocol; this.ClientAddTB.Value = man.GetActiveAuthentication().ClientAddress; AuthenticationCB.Items.Clear(); foreach (GXAuthentication it in man.Settings) { int pos = AuthenticationCB.Items.Add(it); if (it.Type == Device.Authentication) { this.AuthenticationCB.SelectedIndex = pos; } } ServerAddressTypeCB.Items.Clear(); HDLCAddressType type = Device.HDLCAddressing; //If we are creating new device. if (Device.Name == null) { type = man.GetActiveServer().HDLCAddress; } foreach (GXServerAddress it in ((GXManufacturer)ManufacturerCB.SelectedItem).ServerSettings) { ServerAddressTypeCB.Items.Add(it); if (it.HDLCAddress == type) { ServerAddressTypeCB.SelectedItem = it; } } UpdateStartProtocol(); SecurityCB.SelectedItem = man.Security; if (man.SystemTitle != null || man.BlockCipherKey != null || man.AuthenticationKey != null) { SystemTitleTB.Text = GXCommon.ToHex(man.SystemTitle, true); BlockCipherKeyTB.Text = GXCommon.ToHex(man.BlockCipherKey, true); AuthenticationKeyTB.Text = GXCommon.ToHex(man.AuthenticationKey, true); if (!DeviceTab.TabPages.Contains(CipheringTab)) { DeviceTab.TabPages.Add(CipheringTab); if (DeviceTab.TabPages.Contains(SupportedServicesTab)) { DeviceTab.TabPages.Remove(SupportedServicesTab); DeviceTab.TabPages.Add(SupportedServicesTab); } } if (!IsPrintable(man.SystemTitle) || !IsPrintable(man.BlockCipherKey) || !IsPrintable(man.AuthenticationKey)) { AsciiRB.Enabled = false; } else { AsciiRB.Enabled = true; } } else if (DeviceTab.TabPages.Contains(CipheringTab)) { DeviceTab.TabPages.Remove(CipheringTab); } } catch (Exception Ex) { GXDLMS.Common.Error.ShowError(this, Ex); } }
/// <summary> /// Constructor. /// </summary> public GXServerAddress(HDLCAddressType address, int value, bool enabled) { HDLCAddress = address; PhysicalAddress = value; }
/// <summary> /// Count server address from physical and logical addresses. /// </summary> /// <param name="addressing"></param> /// <param name="formula"></param> /// <param name="physicalAddress"></param> /// <param name="LogicalAddress"></param> /// <returns></returns> public static object CountServerAddress(HDLCAddressType addressing, string formula, object physicalAddress, int LogicalAddress) { object value; if (addressing == HDLCAddressType.Custom) { value = Convert.ChangeType(physicalAddress, physicalAddress.GetType()); } else if (addressing == HDLCAddressType.Default || addressing == HDLCAddressType.SerialNumber) { if (addressing == HDLCAddressType.SerialNumber) { Type type = typeof(UInt32); physicalAddress = Convert.ChangeType(EvaluateSN(Convert.ToInt32(physicalAddress), formula), type); } if (physicalAddress is byte) { value = ((LogicalAddress & 0x7) << 5) | (((byte)physicalAddress & 0x7) << 1) | 0x1; value = Convert.ChangeType(value, typeof(byte)); } else if (physicalAddress is UInt16) { int physicalID = Convert.ToInt32(physicalAddress); int logicalID = Convert.ToInt32(LogicalAddress); int total = (physicalID) << 1 | 1; value = Convert.ToUInt32(total | (logicalID << 9)); value = Convert.ChangeType(value, typeof(UInt16)); } else if (physicalAddress is UInt32) { int physicalID = Convert.ToInt32(physicalAddress); int logicalID = Convert.ToInt32(LogicalAddress); int total = (((physicalID >> 7) & 0x7F) << 8) | (physicalID & 0x7F); value = Convert.ToUInt32(((total << 1) | 1 | (logicalID << 17))); } else { throw new Exception("Unknown physical address type."); } } else { throw new Exception("Invalid HDLCAddressing"); } return value; }
private void ManufacturerCB_SelectedIndexChanged(object sender, EventArgs e) { try { GXManufacturer man = (GXManufacturer)ManufacturerCB.SelectedItem; StartProtocolCB.SelectedItem = man.StartProtocol; this.ClientAddTB.Value = man.GetActiveAuthentication().ClientAddress; AuthenticationCB.Items.Clear(); foreach (GXAuthentication it in man.Settings) { int pos = AuthenticationCB.Items.Add(it); if ((int)it.Type == Properties.Settings.Default.Authentication) { this.AuthenticationCB.SelectedIndex = pos; } } ServerAddressTypeCB.Items.Clear(); HDLCAddressType type = (HDLCAddressType)Properties.Settings.Default.HDLCAddressing; foreach (GXServerAddress it in ((GXManufacturer)ManufacturerCB.SelectedItem).ServerSettings) { ServerAddressTypeCB.Items.Add(it); if (it.HDLCAddress == type) { ServerAddressTypeCB.SelectedItem = it; } } UpdateStartProtocol(); SecurityCB.SelectedItem = man.Security; SystemTitleAsciiCb.CheckedChanged -= SystemTitleAsciiCb_CheckedChanged; BlockCipherKeyAsciiCb.CheckedChanged -= BlockCipherKeyAsciiCb_CheckedChanged; AuthenticationKeyAsciiCb.CheckedChanged -= AuthenticationKeyAsciiCb_CheckedChanged; SystemTitleAsciiCb.Checked = IsAscii(man.SystemTitle); if (SystemTitleAsciiCb.Checked) { SystemTitleTB.Text = ASCIIEncoding.ASCII.GetString(man.SystemTitle); } else { SystemTitleTB.Text = GXCommon.ToHex(man.SystemTitle, true); } BlockCipherKeyAsciiCb.Checked = IsAscii(man.BlockCipherKey); if (BlockCipherKeyAsciiCb.Checked) { SystemTitleTB.Text = ASCIIEncoding.ASCII.GetString(man.BlockCipherKey); } else { BlockCipherKeyTB.Text = GXCommon.ToHex(man.BlockCipherKey, true); } AuthenticationKeyAsciiCb.Checked = IsAscii(man.AuthenticationKey); if (AuthenticationKeyAsciiCb.Checked) { SystemTitleTB.Text = ASCIIEncoding.ASCII.GetString(man.AuthenticationKey); } else { AuthenticationKeyTB.Text = GXCommon.ToHex(man.AuthenticationKey, true); } InvocationCounterTB.Text = "0"; ChallengeTB.Text = ""; SystemTitleAsciiCb.CheckedChanged += SystemTitleAsciiCb_CheckedChanged; BlockCipherKeyAsciiCb.CheckedChanged += BlockCipherKeyAsciiCb_CheckedChanged; AuthenticationKeyAsciiCb.CheckedChanged += AuthenticationKeyAsciiCb_CheckedChanged; } catch (Exception Ex) { MessageBox.Show(this, Ex.Message, Properties.Resources.CTT, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void OKBtn_Click(object sender, EventArgs e) { try { //Check security settings. if ((Security)SecurityCB.SelectedItem != Security.None || ((GXAuthentication)AuthenticationCB.SelectedItem).Type == Authentication.HighGMAC) { if (SystemTitleTB.Text.Trim().Length == 0) { throw new ArgumentException("Invalid system title."); } if (AuthenticationKeyTB.Text.Trim().Length == 0) { throw new ArgumentException("Invalid authentication key."); } if (BlockCipherKeyTB.Text.Trim().Length == 0) { throw new ArgumentException("Invalid block cipher key."); } } GXServerAddress server = (GXServerAddress)ServerAddressTypeCB.SelectedItem; if (server.HDLCAddress == HDLCAddressType.SerialNumber && PhysicalServerAddressTB.Value == 0) { throw new Exception("Invalid Serial Number."); } GXManufacturer man = (GXManufacturer)ManufacturerCB.SelectedItem; Target.client.Authentication = ((GXAuthentication)this.AuthenticationCB.SelectedItem).Type; if (Target.client.Authentication != Authentication.None) { if (PasswordAsciiCb.Checked) { Target.client.Password = ASCIIEncoding.ASCII.GetBytes(PasswordTB.Text); } else { Target.client.Password = GXDLMSTranslator.HexToBytes(this.PasswordTB.Text); } } else { Target.client.Password = null; } Target.media = SelectedMedia; if (SelectedMedia is GXSerial) { if (this.SerialPortCB.Text.Length == 0) { throw new Exception("Invalid serial port."); } ((GXSerial)SelectedMedia).PortName = this.SerialPortCB.Text; } else if (SelectedMedia is GXNet) { if (this.HostNameTB.Text.Length == 0) { throw new Exception("Invalid host name."); } ((GXNet)SelectedMedia).HostName = this.HostNameTB.Text; int port; if (!Int32.TryParse(this.PortTB.Text, out port)) { throw new Exception("Invalid port number."); } ((GXNet)SelectedMedia).Port = port; ((GXNet)SelectedMedia).Protocol = (NetworkType)NetProtocolCB.SelectedItem; } Properties.Settings.Default.Media = SelectedMedia.MediaType; Properties.Settings.Default.MediaSettings = SelectedMedia.Settings; Properties.Settings.Default.SelectedManufacturer = man.Identification; Properties.Settings.Default.WaitTime = Convert.ToInt32(WaitTimeTB.Value); Target.WaitTime = Properties.Settings.Default.WaitTime * 1000; GXAuthentication authentication = (GXAuthentication)AuthenticationCB.SelectedItem; HDLCAddressType HDLCAddressing = ((GXServerAddress)ServerAddressTypeCB.SelectedItem).HDLCAddress; Properties.Settings.Default.HDLCAddressing = (int)HDLCAddressing; Properties.Settings.Default.ClientAddress = Target.client.ClientAddress = Convert.ToInt32(ClientAddTB.Value); if (HDLCAddressing == HDLCAddressType.SerialNumber) { int address = Convert.ToInt32(PhysicalServerAddressTB.Value); Properties.Settings.Default.PhysicalServerAddress = address; Target.client.ServerAddress = GXDLMSClient.GetServerAddress(address); } else if (HDLCAddressing == HDLCAddressType.Default) { Properties.Settings.Default.PhysicalServerAddress = Convert.ToInt32(PhysicalServerAddressTB.Value); Properties.Settings.Default.LogicalServerAddress = Convert.ToInt32(LogicalServerAddressTB.Value); Target.client.ServerAddress = GXDLMSClient.GetServerAddress(Properties.Settings.Default.LogicalServerAddress, Properties.Settings.Default.PhysicalServerAddress); } Target.client.UseLogicalNameReferencing = this.UseLNCB.Checked; Target.iec = (StartProtocolType)this.StartProtocolCB.SelectedItem == StartProtocolType.IEC; Target.client.Ciphering.Security = (Security)SecurityCB.SelectedItem; Target.client.Ciphering.SystemTitle = GetAsHex(SystemTitleTB.Text, SystemTitleAsciiCb.Checked); Target.client.Ciphering.BlockCipherKey = GetAsHex(BlockCipherKeyTB.Text, BlockCipherKeyAsciiCb.Checked); Target.client.Ciphering.AuthenticationKey = GetAsHex(AuthenticationKeyTB.Text, AuthenticationKeyAsciiCb.Checked); Target.client.Ciphering.InvocationCounter = UInt32.Parse(InvocationCounterTB.Text); Target.client.CtoSChallenge = GXCommon.HexToBytes(ChallengeTB.Text); if (man.UseIEC47) { Target.client.InterfaceType = InterfaceType.WRAPPER; } UpdateConformance(); Properties.Settings.Default.Save(); } catch (Exception Ex) { this.DialogResult = DialogResult.None; MessageBox.Show(this, Ex.Message, Properties.Resources.CTT, MessageBoxButtons.OK, MessageBoxIcon.Error); } }