Пример #1
0
 /// <summary>
 /// User has change server SAP.
 /// </summary>
 private void ServerSAPTb_Leave(object sender, EventArgs e)
 {
     try
     {
         if (ServerSAPTb.Text != "")
         {
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             UInt16 v;
             if (UInt16.TryParse(ServerSAPTb.Text, out v) && target.ServerSAP != v)
             {
                 if (v > 0x3FFF)
                 {
                     throw new ArgumentOutOfRangeException("Server SAP must be between 0 to 16383");
                 }
                 target.ServerSAP = v;
                 Target.UpdateDirty(3, target.ServerSAP);
                 errorProvider1.SetError(ServerSAPTb, Properties.Resources.ValueChangedTxt);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Пример #2
0
 /// <summary>
 /// Check authentication.
 /// </summary>
 protected override SourceDiagnostic ValidateAuthentication(Authentication authentication, byte[] password)
 {
     if (this.UseLogicalNameReferencing)
     {
         GXDLMSAssociationLogicalName target = null;
         foreach (GXDLMSAssociationLogicalName it in Items.GetObjects(ObjectType.AssociationLogicalName))
         {
             if (it.AuthenticationMechanismName.MechanismId == authentication)
             {
                 target = it;
                 break;
             }
         }
         if (target != null)
         {
             if (authentication != Authentication.Low)
             {
                 //Other authentication levels are check later.
                 return(SourceDiagnostic.None);
             }
             if (GXCommon.EqualBytes(target.Secret, password))
             {
                 return(SourceDiagnostic.None);
             }
         }
     }
     return(SourceDiagnostic.AuthenticationFailure);
 }
Пример #3
0
 protected override void PostWrite(ValueEventArgs[] args)
 {
     foreach (ValueEventArgs e in args)
     {
         //If association is updated.
         if (e.Target is GXDLMSAssociationLogicalName && e.Index == 2)
         {
             //Update new items to object list.
             GXDLMSAssociationLogicalName a = (GXDLMSAssociationLogicalName)e.Target;
             UpdateAssociationView(a.ObjectList);
         }
         //If association is updated.
         else if (e.Target is GXDLMSAssociationShortName && e.Index == 2)
         {
             //Update new items to object list.
             GXDLMSAssociationShortName a = (GXDLMSAssociationShortName)e.Target;
             UpdateAssociationView(a.ObjectList);
         }
         else
         {
             SaveSettings();
         }
         System.Diagnostics.Debug.WriteLine("Writing " + e.Target.LogicalName);
     }
 }
Пример #4
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical name settings.</param>
 /// <param name="type">Interface type.</param>
 public GXDLMSBase(GXDLMSAssociationLogicalName ln, InterfaceType type)
     : base(ln, type, "GRX", 12345678)
 {
     MaxReceivePDUSize = 1024;
     //Default secret.
     ln.Secret = ASCIIEncoding.ASCII.GetBytes("Gurux");
 }
Пример #5
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical name settings.</param>
 /// <param name="hdlc">HDLC settings.</param>
 public GXDLMSBase(GXDLMSAssociationLogicalName ln, GXDLMSHdlcSetup hdlc)
     : base(ln, hdlc)
 {
     MaxReceivePDUSize = 1024;
     //Default secret.
     ln.Secret = ASCIIEncoding.ASCII.GetBytes("Gurux");
 }
Пример #6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical name settings.</param>
 /// <param name="wrapper">Wrapper settings.</param>
 public GXDLMSBase(GXDLMSAssociationLogicalName ln, GXDLMSTcpUdpSetup wrapper)
     : base(ln, wrapper, "GRX", 12345678)
 {
     MaxReceivePDUSize = 1024;
     //Default secret.
     ln.Secret = ASCIIEncoding.ASCII.GetBytes("Gurux");
 }
Пример #7
0
 /// <summary>
 /// Our example server accept all authentications.
 /// </summary>
 protected override SourceDiagnostic ValidateAuthentication(Authentication authentication, byte[] password)
 {
     //If low authentication fails.
     if (authentication == Authentication.Low)
     {
         byte[] expected;
         if (UseLogicalNameReferencing)
         {
             GXDLMSAssociationLogicalName ln = (GXDLMSAssociationLogicalName)Items.FindByLN(ObjectType.AssociationLogicalName, "0.0.40.0.0.255");
             expected = ln.Secret;
         }
         else
         {
             GXDLMSAssociationShortName sn = (GXDLMSAssociationShortName)Items.FindByLN(ObjectType.AssociationShortName, "0.0.40.0.0.255");
             expected = sn.Secret;
         }
         if (Gurux.Common.GXCommon.EqualBytes(expected, password))
         {
             return(SourceDiagnostic.None);
         }
         return(SourceDiagnostic.AuthenticationFailure);
     }
     //Other authentication levels are check later.
     return(SourceDiagnostic.None);
 }
Пример #8
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical name settings.</param>
 /// <param name="type">Interface type.</param>
 public GXDLMSServer(GXDLMSAssociationLogicalName ln, InterfaceType type)
 {
     Settings = new GXDLMSSettings(true);
     Settings.UseLogicalNameReferencing = true;
     Reset();
     Settings.Objects.Add(ln);
     this.InterfaceType = type;
 }
Пример #9
0
        /// <summary>
        /// Read all data from the meter.
        /// </summary>
        public void ReadAll(bool useCache)
        {
            try
            {
                InitializeConnection();
                GetAssociationView(useCache);
                //Mikko
                GXDLMSAssociationLogicalName ln = Client.Objects.GetObjects(ObjectType.AssociationLogicalName)[0] as GXDLMSAssociationLogicalName;
                Read(ln, 10);
                Read(ln, 11);
                if (ln.UserList.Count != 0)
                {
                    throw new Exception("Mikko");
                }
                GXReplyData reply = new GXReplyData();
                if (!ReadDataBlock(ln.AddUser(Client, 1, "Mikko"), reply))
                {
                    if (reply.Error != (short)ErrorCode.Rejected)
                    {
                        throw new GXDLMSException(reply.Error);
                    }
                }
                //Update data type.
                Read(ln, 10);
                if (ln.UserList.Count != 1)
                {
                    throw new Exception("Mikko");
                }
                if (ln.UserList[0].Key != 1 || ln.UserList[0].Value != "Mikko")
                {
                    throw new Exception("Mikko");
                }
                reply.Clear();
                if (!ReadDataBlock(ln.RemoveUser(Client, 1, "Mikko"), reply))
                {
                    if (reply.Error != (short)ErrorCode.Rejected)
                    {
                        throw new GXDLMSException(reply.Error);
                    }
                }
                Read(ln, 10);
                if (ln.UserList.Count != 0)
                {
                    throw new Exception("Mikko");
                }

                /*
                 * GetScalersAndUnits();
                 * GetProfileGenericColumns();
                 * GetReadOut();
                 * GetProfileGenerics();
                 */
            }
            finally
            {
                Close();
            }
        }
 public void OnAccessRightsChange(int index, AccessMode access, bool connected)
 {
     if (index == 2)
     {
     }
     else if (index == 3)
     {
         ClientSAPTb.ReadOnly = ServerSAPTb.ReadOnly = (access & AccessMode.Write) == 0;
     }
     else if (index == 4)
     {
         // Application context name.
         ApplicationJointISOCTTTb.ReadOnly           = ApplicationCountryTb.ReadOnly =
             ApplicationCountryNameTb.ReadOnly       = ApplicationIdentifiedOrganizationTb.ReadOnly =
                 ApplicationDLMSUATb.ReadOnly        = ApplicationContextTb.ReadOnly =
                     ApplicationContextIDTb.ReadOnly = (access & AccessMode.Write) == 0;
     }
     else if (index == 5)
     {
         // xDLMS_context_info
         ConformanceTB.ReadOnly           = MaxReceivePDUSizeTb.ReadOnly = MaxSendPDUSizeTb.ReadOnly =
             DLMSVersionNumberTB.ReadOnly = CypheringInfoTb.ReadOnly = (access & AccessMode.Write) == 0;
     }
     else if (index == 6)
     {
         // authentication_mechanism_name
         AuthenticationJointISOCTTTb.ReadOnly = AuthenticationCountryTb.ReadOnly = AuthenticationCountryNameTb.ReadOnly =
             AuthenticationIdentifiedorganizationTb.ReadOnly = AuthenticationDLMSUATb.ReadOnly =
                 AuthenticationMechanismNameTb.ReadOnly      = AuthenticationMechanismIdTb.ReadOnly = (access & AccessMode.Write) == 0;
     }
     else if (index == 7)
     {
         GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
         bool enabled = false;
         if (target.AuthenticationMechanismName.MechanismId == Authentication.Low)
         {
             enabled        = (access & AccessMode.Write) != 0;
             SecretLbl.Text = "Low Secret:";
         }
         else if (target.AuthenticationMechanismName.MechanismId == Authentication.High)
         {
             enabled        = Target.GetMethodAccess(2) == MethodAccessMode.Access;
             SecretLbl.Text = "High Secret:";
         }
         else
         {
             SecretLbl.Text = "Secret:";
         }
         UpdatePwBtn.Enabled = enabled;
         SecretTB.ReadOnly   = !enabled;
     }
     else if (index == 9)
     {
         SecuritySetupCb.Enabled = (access & AccessMode.Write) != 0;
     }
 }
 void OnShowDialog(GXDLMSObject it, GXActionArgs arg)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new ShowDialogEventHandler(OnShowDialog), it, arg).AsyncWaitHandle.WaitOne();
     }
     else
     {
         ListViewItem li     = null;
         bool         remove = it == null;
         if (remove)
         {
             if (ObjectsView.SelectedItems.Count == 1)
             {
                 li = ObjectsView.SelectedItems[0];
                 it = (GXDLMSObject)li.Tag;
             }
             else
             {
                 arg.Handled = true;
                 return;
             }
         }
         GXDLMSAssociationViewDlg dlg = new GXDLMSAssociationViewDlg(it, true, remove);
         if (dlg.ShowDialog(this) == DialogResult.OK)
         {
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             if (remove)
             {
                 arg.Value = target.RemoveObject(arg.Client, it);
                 li.Remove();
             }
             else
             {
                 it = dlg.GetTarget();
                 li = ObjectsView.Items.Add(it.ObjectType.ToString());
                 li.SubItems.Add(it.Version.ToString());
                 li.SubItems.Add(it.LogicalName);
                 li.SubItems.Add("");
                 li.SubItems.Add("");
                 li.Tag = it;
                 target.ObjectList.Add(it);
                 arg.Value = target.AddObject(arg.Client, it);
             }
         }
         else
         {
             arg.Handled = true;
         }
     }
 }
Пример #12
0
 private void ApplicationContextIDCb_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
         target.ApplicationContextName.ContextId = (ApplicationContextName)ApplicationContextIDCb.SelectedItem;
         errorProvider1.SetError(ApplicationContextIDCb, Properties.Resources.ValueChangedTxt);
         Target.UpdateDirty(4, ApplicationContextIDCb.SelectedItem);
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message);
     }
 }
 public void OnValueChanged(int index, object value, bool user)
 {
     if (index == 2)
     {
         GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
         GXDLMSObjectCollection       items  = target.ObjectList;
         CallingWindowLV.Items.Clear();
         if (items != null)
         {
             foreach (GXDLMSObject it in items)
             {
                 ListViewItem li = CallingWindowLV.Items.Add(it.ObjectType.ToString());
                 li.SubItems.Add(it.Version.ToString()); //Version
                 li.SubItems.Add(it.LogicalName);
                 //access_rights: access_right
                 if (it is IGXDLMSBase)
                 {
                     string str = null;
                     //Show attribute access.
                     int cnt = (it as IGXDLMSBase).GetAttributeCount();
                     for (int pos = 1; pos != cnt + 1; ++pos)
                     {
                         if (str != null)
                         {
                             str += ", ";
                         }
                         AccessMode mode = it.GetAccess(pos);
                         str += pos.ToString() + " = " + mode;
                     }
                     li.SubItems.Add(str);
                     //Show method access.
                     str = null;
                     cnt = (it as IGXDLMSBase).GetMethodCount();
                     for (int pos = 1; pos != cnt + 1; ++pos)
                     {
                         if (str != null)
                         {
                             str += ", ";
                         }
                         MethodAccessMode mode = it.GetMethodAccess(pos);
                         str += pos.ToString() + " = " + mode;
                     }
                     li.SubItems.Add(str);
                 }
             }
         }
     }
 }
Пример #14
0
 public void PreAction(GXActionArgs arg)
 {
     //Edit object in association view.
     if (arg.Action == ActionType.Action && arg.Index == -1)
     {
         if (arg.Tag == null)
         {
             OnShowDialog(null, arg);
         }
         else
         {
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             arg.Value = target.AddObject(arg.Client, arg.Tag as GXDLMSObject);
             arg.Tag   = null;
         }
     }
     //Add object to association view.
     else if (arg.Index == 3)
     {
         GXDLMSObject it = new GXDLMSData();
         OnShowDialog(it, arg);
     }
     else if (arg.Index == 4)
     {
         // Remove object from association view.
         OnShowDialog(null, arg);
     }
     //Add user to user list.
     else if (arg.Index == 5)
     {
         OnShowDialog(true, arg);
     }
     else if (arg.Index == 6)
     {
         // Remove user from user list.
         OnShowDialog(false, arg);
     }
     if ((arg.Action == ActionType.Write && arg.Index == 7) ||
         arg.Action == ActionType.Action && arg.Index == 2)
     {
         OnUpdatePassword(arg);
     }
     else if (arg.Action == ActionType.Write && arg.Index == 2)
     {
         //Skip write invoke.
         arg.Handled = true;
     }
 }
Пример #15
0
 private void AuthenticationMechanismIdCb_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (!Freeze)
     {
         try
         {
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             target.AuthenticationMechanismName.MechanismId = (Authentication)AuthenticationMechanismIdCb.SelectedItem;
             errorProvider1.SetError(AuthenticationMechanismIdCb, Properties.Resources.ValueChangedTxt);
             Target.UpdateDirty(6, AuthenticationMechanismIdCb.SelectedItem);
         }
         catch (Exception ex)
         {
             MessageBox.Show(this, ex.Message);
         }
     }
 }
Пример #16
0
 void OnUpdatePassword(GXActionArgs arg)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new UpdatePasswordDialogEventHandler(OnUpdatePassword), arg).AsyncWaitHandle.WaitOne();
     }
     else
     {
         //Update pw.
         GXTextDlg dlg;
         if (arg.Action == ActionType.Write)
         {
             dlg = new GXTextDlg("Update Low level password.", "Password", "");
         }
         else
         {
             dlg = new GXTextDlg("Update High level password.", "Password", "");
         }
         arg.Handled = dlg.ShowDialog(Parent) != DialogResult.OK;
         if (!arg.Handled)
         {
             byte[] value;
             if (!dlg.GetValue().StartsWith("0x"))
             {
                 value = ASCIIEncoding.ASCII.GetBytes(dlg.GetValue());
             }
             else
             {
                 value = GXDLMSTranslator.HexToBytes(dlg.GetValue());
             }
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             if (arg.Action == ActionType.Write)
             {
                 target.Secret = value;
             }
             else
             {
                 GXByteBuffer bb = new GXByteBuffer();
                 bb.SetUInt8((byte)DataType.OctetString);
                 bb.SetUInt8((byte)value.Length);
                 bb.Set(value);
                 arg.Value = bb.Array();
             }
         }
     }
 }
Пример #17
0
 void OnShowDialog(bool addUser, GXActionArgs arg)
 {
     if (InvokeRequired)
     {
         BeginInvoke(new ShowUserDialogEventHandler(OnShowDialog), addUser, arg).AsyncWaitHandle.WaitOne();
     }
     else
     {
         GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
         if (addUser)
         {
             GXUserDlg dlg = new GXUserDlg((byte)target.UserList.Count, "", false);
             if (dlg.ShowDialog(this) == DialogResult.OK)
             {
                 ListViewItem li = UsersList.Items.Add(dlg.UserId.ToString());
                 li.SubItems.Add(dlg.UserName);
                 arg.Value = target.AddUser(arg.Client, dlg.UserId, dlg.UserName);
             }
             else
             {
                 arg.Handled = true;
             }
         }
         else
         {
             if (UsersList.SelectedItems.Count == 1)
             {
                 ListViewItem li  = UsersList.SelectedItems[0];
                 GXUserDlg    dlg = new GXUserDlg(byte.Parse(li.SubItems[0].Text), li.SubItems[1].Text, true);
                 if (dlg.ShowDialog(this) == DialogResult.OK)
                 {
                     arg.Value = target.RemoveUser(arg.Client, dlg.UserId, dlg.UserName);
                     li.Remove();
                 }
                 else
                 {
                     arg.Handled = true;
                 }
             }
             else
             {
                 arg.Handled = true;
             }
         }
     }
 }
Пример #18
0
 private void MaxSendPDUSizeTb_Leave(object sender, EventArgs e)
 {
     try
     {
         if (MaxSendPDUSizeTb.Text != "")
         {
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             UInt16 v;
             if (UInt16.TryParse(MaxSendPDUSizeTb.Text, out v) && target.XDLMSContextInfo.MaxSendPduSize != v)
             {
                 target.XDLMSContextInfo.MaxSendPduSize = v;
                 Target.UpdateDirty(5, target.XDLMSContextInfo);
                 errorProvider1.SetError(MaxSendPDUSizeTb, Properties.Resources.ValueChangedTxt);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Пример #19
0
 private void SecuritySetupCb_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
         if (SecuritySetupCb.SelectedItem is GXDLMSSecuritySetup)
         {
             target.SecuritySetupReference = ((GXDLMSSecuritySetup)SecuritySetupCb.SelectedItem).LogicalName;
         }
         else
         {
             target.SecuritySetupReference = null;
         }
         errorProvider1.SetError(SecuritySetupCb, Properties.Resources.ValueChangedTxt);
         Target.UpdateDirty(9, SecuritySetupCb.SelectedItem);
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message);
     }
 }
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="ln">Logical name settings. </param>
        ///<param name="type">
        /// Interface type.
        ///</param>
        ///<param name="flagID">
        /// Three letters FLAG ID.
        ///</param>
        ///<param name="serialNumber">
        /// Meter serial number. Size of serial number is 5 bytes.
        ///</param>
        public GXDLMSSecureServer(GXDLMSAssociationLogicalName ln, InterfaceType type, string flagID, UInt64 serialNumber) :
            base(ln, type)
        {
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            ln.XDLMSContextInfo.settings = Settings;
            GXByteBuffer bb = new GXByteBuffer();

            bb.Add(flagID);
            GXByteBuffer serial = new GXByteBuffer();

            serial.SetUInt64(serialNumber);
            bb.Set(serial.Data, 3, 5);
            Ciphering       = new GXCiphering(bb.Array());
            Settings.Cipher = Ciphering;
        }
Пример #21
0
        /// <summary>
        ///  Initialize server.
        /// </summary>
        /// <remarks>
        /// This must call after server objects are set.
        /// </remarks>
        public void Initialize()
        {
            bool association = false;

            Initialized = true;
            if (SortedItems.Count != Items.Count)
            {
                for (int pos = 0; pos != Items.Count; ++pos)
                {
                    GXDLMSObject it = Items[pos];
                    if (this.UseLogicalNameReferencing &&
                        (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6))
                    {
                        throw new Exception("Invalid Logical Name.");
                    }
                    if (it is GXDLMSProfileGeneric)
                    {
                        GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric;
                        if (pg.ProfileEntries < 1)
                        {
                            throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table.");
                        }
                        foreach (var obj in pg.CaptureObjects)
                        {
                            if (obj.Value.AttributeIndex < 1)
                            {
                                throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name);
                            }
                        }
                        if (pg.ProfileEntries < 1)
                        {
                            throw new Exception("Invalid Profile Entries. Profile entries tells amount of rows in the table.");
                        }
                        if (pg.CapturePeriod != 0)
                        {
                            GXProfileGenericUpdater p = new GXProfileGenericUpdater(this, pg);
                            Thread thread             = new Thread(new ThreadStart(p.UpdateProfileGenericData));
                            thread.IsBackground = true;
                            thread.Start();
                        }
                    }
                    else if ((it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing) ||
                             (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing))
                    {
                        association = true;
                    }
                    else if (!(it is IGXDLMSBase))//Remove unsupported items.
                    {
                        Debug.WriteLine(it.ObjectType.ToString() + " not supported.");
                        Items.RemoveAt(pos);
                        --pos;
                    }
                }

                if (!association)
                {
                    if (UseLogicalNameReferencing)
                    {
                        GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName();
                        it.ObjectList = this.Items;
                        Items.Add(it);
                    }
                    else
                    {
                        GXDLMSAssociationShortName it = new GXDLMSAssociationShortName();
                        it.ObjectList = this.Items;
                        Items.Add(it);
                    }
                }
                //Arrange items by Short Name.
                ushort sn = 0xA0;
                if (!this.UseLogicalNameReferencing)
                {
                    SortedItems.Clear();
                    foreach (GXDLMSObject it in Items)
                    {
                        if (it is GXDLMSAssociationShortName)
                        {
                            it.ShortName = 0xFA00;
                        }
                        //Generate Short Name if not given.
                        if (it.ShortName == 0)
                        {
                            do
                            {
                                it.ShortName = sn;
                                sn          += 0xA0;
                            }while (SortedItems.ContainsKey(it.ShortName));
                        }
                        SortedItems.Add(it.ShortName, it);
                    }
                }
            }
        }
 public void PreAction(GXActionArgs arg)
 {
     //Add object to association view.
     if (arg.Index == 3)
     {
         GXDLMSObject it = new GXDLMSData();
         OnShowDialog(it, arg);
     }
     else if (arg.Index == 4)
     {
         // Remove object from association view.
         OnShowDialog(null, arg);
     }
     //Add user to user list.
     else if (arg.Index == 5)
     {
         OnShowDialog(true, arg);
     }
     else if (arg.Index == 6)
     {
         // Remove user from user list.
         OnShowDialog(false, arg);
     }
     if (arg.Action == ActionType.Write && arg.Index == 7)
     {
         DialogResult ret;
         //Update pw.
         ret = GXHelpers.ShowMessageBox(this, Properties.Resources.SetPasswordWarning, "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
         if (ret == DialogResult.Yes)
         {
             byte[] value;
             if (SecretAsciiCb.Checked)
             {
                 value = ASCIIEncoding.ASCII.GetBytes(SecretTB.Text);
             }
             else
             {
                 value = GXDLMSTranslator.HexToBytes(SecretTB.Text);
             }
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             if (target.AuthenticationMechanismName.MechanismId == Authentication.Low)
             {
                 arg.Index  = 7;
                 arg.Action = ActionType.Write;
             }
             else
             {
                 arg.Action = ActionType.Action;
                 arg.Index  = 2;
             }
             if (arg.Action == ActionType.Write)
             {
                 target.Secret = value;
             }
             else
             {
                 GXByteBuffer bb = new GXByteBuffer();
                 bb.SetUInt8((byte)DataType.OctetString);
                 bb.SetUInt8((byte)value.Length);
                 bb.Set(value);
                 arg.Value = bb.Array();
             }
         }
         arg.Handled = ret != DialogResult.Yes;
     }
     else if (arg.Action == ActionType.Write && arg.Index == 2)
     {
         //Skip write invoke.
         arg.Handled = true;
     }
 }
Пример #23
0
        /// <summary>
        ///  Initialize server.
        /// </summary>
        /// <remarks>
        /// This must call after server objects are set.
        /// <param name="manually">If true, server handle objects and all data are updated manually.</param>
        public void Initialize(bool manually)
        {
            Initialized = true;
            if (manually)
            {
                return;
            }
            bool association = false;

            for (int pos = 0; pos != Items.Count; ++pos)
            {
                GXDLMSObject it = Items[pos];
                if (this.UseLogicalNameReferencing &&
                    (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6))
                {
                    throw new Exception("Invalid Logical Name.");
                }
                it.Start(this);
                if (it is GXDLMSProfileGeneric)
                {
                    GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric;
                    foreach (var obj in pg.CaptureObjects)
                    {
                        if (obj.Value.AttributeIndex < 1)
                        {
                            throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name);
                        }
                    }
                }
                else if (it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing)
                {
                    if ((it as GXDLMSAssociationShortName).ObjectList.Count == 0)
                    {
                        (it as GXDLMSAssociationShortName).ObjectList.AddRange(this.Items);
                    }
                    association = true;
                }
                else if (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing)
                {
                    if ((it as GXDLMSAssociationLogicalName).ObjectList.Count == 0)
                    {
                        (it as GXDLMSAssociationLogicalName).ObjectList.AddRange(this.Items);
                    }
                    association = true;
                }
                else if (it is GXDLMSHdlcSetup)
                {
                    hdlcSetup = it as GXDLMSHdlcSetup;
                }
                else if (!(it is IGXDLMSBase))//Remove unsupported items.
                {
                    Debug.WriteLine(it.ObjectType.ToString() + " not supported.");
                    Items.RemoveAt(pos);
                    --pos;
                }
            }
            if (!association)
            {
                if (UseLogicalNameReferencing)
                {
                    GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName();
                    it.ObjectList = this.Items;
                    Items.Add(it);
                }
                else
                {
                    GXDLMSAssociationShortName it = new GXDLMSAssociationShortName();
                    it.ObjectList = this.Items;
                    Items.Add(it);
                }
            }
            //Arrange items by Short Name.
            UpdateShortNames(false);
        }
Пример #24
0
        /// <summary>
        ///  Initialize server.
        /// </summary>
        /// <remarks>
        /// This must call after server objects are set.
        /// </remarks>
        public void Initialize()
        {
            bool association = false;

            Initialized = true;
            for (int pos = 0; pos != Items.Count; ++pos)
            {
                GXDLMSObject it = Items[pos];
                if (this.UseLogicalNameReferencing &&
                    (string.IsNullOrEmpty(it.LogicalName) || it.LogicalName.Split('.').Length != 6))
                {
                    throw new Exception("Invalid Logical Name.");
                }
                it.Start(this);
                if (it is GXDLMSProfileGeneric)
                {
                    GXDLMSProfileGeneric pg = it as GXDLMSProfileGeneric;
                    foreach (var obj in pg.CaptureObjects)
                    {
                        if (obj.Value.AttributeIndex < 1)
                        {
                            throw new Exception("Invalid attribute index. SelectedAttributeIndex is not set for " + obj.Key.Name);
                        }
                    }
                }
                else if (it is GXDLMSAssociationShortName && !this.UseLogicalNameReferencing)
                {
                    if ((it as GXDLMSAssociationShortName).ObjectList.Count == 0)
                    {
                        (it as GXDLMSAssociationShortName).ObjectList.AddRange(this.Items);
                    }
                    association = true;
                }
                else if (it is GXDLMSAssociationLogicalName && this.UseLogicalNameReferencing)
                {
                    if ((it as GXDLMSAssociationLogicalName).ObjectList.Count == 0)
                    {
                        (it as GXDLMSAssociationLogicalName).ObjectList.AddRange(this.Items);
                    }
                    association = true;
                }
                else if (it is GXDLMSHdlcSetup)
                {
                    hdlcSetup = it as GXDLMSHdlcSetup;
                }

                else if (!(it is IGXDLMSBase))//Remove unsupported items.
                {
                    Debug.WriteLine(it.ObjectType.ToString() + " not supported.");
                    Items.RemoveAt(pos);
                    --pos;
                }
            }

            if (!association)
            {
                if (UseLogicalNameReferencing)
                {
                    GXDLMSAssociationLogicalName it = new GXDLMSAssociationLogicalName();
                    it.ObjectList = this.Items;
                    Items.Add(it);
                }
                else
                {
                    GXDLMSAssociationShortName it = new GXDLMSAssociationShortName();
                    it.ObjectList = this.Items;
                    Items.Add(it);
                }
            }
            //Arrange items by Short Name.
            int sn = 0xA0;
            int offset, count;

            if (!this.UseLogicalNameReferencing)
            {
                foreach (GXDLMSObject it in Items)
                {
                    //Generate Short Name if not given.
                    if (it.ShortName == 0)
                    {
                        it.ShortName = (ushort)sn;
                        //Add method index addresses.
                        GXDLMS.GetActionInfo(it.ObjectType, out offset, out count);
                        if (count != 0)
                        {
                            sn += offset + (8 * count);
                        }
                        else //If there are no methods.
                        {
                            //Add attribute index addresses.
                            sn += 8 * (it as IGXDLMSBase).GetAttributeCount();
                        }
                    }
                }
            }
        }
Пример #25
0
 ///<summary>
 /// Constructor.
 ///</summary>
 ///<param name="logicalNameReferencing">
 /// Is logical name referencing used.
 ///</param>
 ///<param name="type">
 /// Interface type.
 ///</param>
 public GXDLMSSecureServer(GXDLMSAssociationLogicalName ln, GXDLMSHdlcSetup hdlc) :
     base(ln, hdlc)
 {
     Ciphering       = new GXCiphering(ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"));
     Settings.Cipher = Ciphering;
 }
Пример #26
0
        public void OnValueChanged(GXDLMSViewArguments arg)
        {
            GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;

            //object list.
            if (arg.Index == 2)
            {
                GXDLMSObjectCollection items = target.ObjectList;
                ObjectsView.Items.Clear();
                if (items != null)
                {
                    foreach (GXDLMSObject it in items)
                    {
                        ListViewItem li = ObjectsView.Items.Add(it.ObjectType.ToString());
                        li.SubItems.Add(it.Version.ToString()); //Version
                        li.SubItems.Add(it.LogicalName);
                        li.Tag = it;
                        //access_rights: access_right
                        if (it is IGXDLMSBase)
                        {
                            string str = null;
                            //Show attribute access.
                            int cnt = (it as IGXDLMSBase).GetAttributeCount();
                            for (int pos = 1; pos != cnt + 1; ++pos)
                            {
                                if (str != null)
                                {
                                    str += ", ";
                                }
                                if (target.Version < 3)
                                {
                                    AccessMode mode = it.GetAccess(pos);
                                    str += pos.ToString() + " = " + mode;
                                }
                                else
                                {
                                    AccessMode3 mode = it.GetAccess3(pos);
                                    str += pos.ToString() + " = " + mode;
                                }
                            }
                            li.SubItems.Add(str);
                            //Show method access.
                            str = null;
                            cnt = (it as IGXDLMSBase).GetMethodCount();
                            for (int pos = 1; pos != cnt + 1; ++pos)
                            {
                                if (str != null)
                                {
                                    str += ", ";
                                }
                                if (target.Version < 3)
                                {
                                    MethodAccessMode mode = it.GetMethodAccess(pos);
                                    str += pos.ToString() + " = " + mode;
                                }
                                else
                                {
                                    MethodAccessMode3 mode = it.GetMethodAccess3(pos);
                                    str += pos.ToString() + " = " + mode;
                                }
                            }
                            li.SubItems.Add(str);
                        }
                    }
                }
            }
            //Associated partners ID.
            else if (arg.Index == 3)
            {
                ClientSAPTb.Text = Convert.ToString(target.ClientSAP);
                ServerSAPTb.Text = Convert.ToString(target.ServerSAP);
            }
            else if (arg.Index == 4)
            {
                try
                {
                    this.ApplicationContextIDCb.SelectedIndexChanged -= new System.EventHandler(this.ApplicationContextIDCb_SelectedIndexChanged);
                    ApplicationContextIDCb.Items.Clear();
                    if (target.ApplicationContextName.ContextId == ApplicationContextName.LogicalName ||
                        target.ApplicationContextName.ContextId == ApplicationContextName.LogicalNameWithCiphering)
                    {
                        ApplicationContextIDCb.Items.AddRange(new object[] {
                            ApplicationContextName.LogicalName, ApplicationContextName.LogicalNameWithCiphering
                        });
                    }
                    else
                    {
                        ApplicationContextIDCb.Items.AddRange(new object[] {
                            ApplicationContextName.ShortName, ApplicationContextName.ShortNameWithCiphering
                        });
                    }
                    // Application context name.
                    ApplicationJointISOCTTTb.Text            = Convert.ToString(target.ApplicationContextName.JointIsoCtt);
                    ApplicationCountryTb.Text                = Convert.ToString(target.ApplicationContextName.Country);
                    ApplicationCountryNameTb.Text            = Convert.ToString(target.ApplicationContextName.CountryName);
                    ApplicationIdentifiedOrganizationTb.Text = Convert.ToString(target.ApplicationContextName.IdentifiedOrganization);
                    ApplicationDLMSUATb.Text                        = Convert.ToString(target.ApplicationContextName.DlmsUA);
                    ApplicationContextTb.Text                       = Convert.ToString(target.ApplicationContextName.ApplicationContext);
                    ApplicationContextIDCb.SelectedItem             = target.ApplicationContextName.ContextId;
                    ApplicationRegistrationAuthorityCb.SelectedItem = FindCountry(target.ApplicationContextName.JointIsoCtt, target.ApplicationContextName.Country, target.ApplicationContextName.CountryName);
                }
                finally
                {
                    ApplicationContextIDCb.SelectedIndexChanged += new System.EventHandler(this.ApplicationContextIDCb_SelectedIndexChanged);
                }
            }
            else if (arg.Index == 5)
            {
                // xDLMS_context_info
                ShowConformance(target.XDLMSContextInfo.Conformance);
                MaxReceivePDUSizeTb.Text = target.XDLMSContextInfo.MaxReceivePduSize.ToString();
                MaxSendPDUSizeTb.Text    = target.XDLMSContextInfo.MaxSendPduSize.ToString();
                DLMSVersionNumberTB.Text = target.XDLMSContextInfo.DlmsVersionNumber.ToString();
                CypheringInfoTb.Text     = GXDLMSTranslator.ToHex(target.XDLMSContextInfo.CypheringInfo);
            }
            else if (arg.Index == 6)
            {
                Freeze = true;
                // authentication_mechanism_name
                AuthenticationJointISOCTTTb.Text            = Convert.ToString(target.AuthenticationMechanismName.JointIsoCtt);
                AuthenticationCountryTb.Text                = Convert.ToString(target.AuthenticationMechanismName.Country);
                AuthenticationCountryNameTb.Text            = Convert.ToString(target.AuthenticationMechanismName.CountryName);
                AuthenticationIdentifiedorganizationTb.Text = Convert.ToString(target.AuthenticationMechanismName.IdentifiedOrganization);
                AuthenticationDLMSUATb.Text        = Convert.ToString(target.AuthenticationMechanismName.DlmsUA);
                AuthenticationMechanismNameTb.Text = Convert.ToString(target.AuthenticationMechanismName.AuthenticationMechanismName);
                AuthenticationMechanismIdCb.Text   = Convert.ToString(target.AuthenticationMechanismName.MechanismId);
                AuthenticationRegistrationAuthorityCb.SelectedItem = FindCountry(target.AuthenticationMechanismName.JointIsoCtt, target.AuthenticationMechanismName.Country, target.AuthenticationMechanismName.CountryName);
                Freeze = false;
            }
            else if (arg.Index == 7)
            {
                //Secret.
                if (GXHelpers.IsAscii(target.Secret))
                {
                    SecretAsciiCb.Checked = true;
                    SecretTB.Text         = ASCIIEncoding.ASCII.GetString(target.Secret);
                }
                else
                {
                    SecretTB.Text = GXDLMSTranslator.ToHex(target.Secret);
                }
            }
            else if (arg.Index == 9)
            {
                SecuritySetupCb.Items.Clear();
                //security_setup_reference
                if (target.Parent != null)
                {
                    SecuritySetupCb.Items.Add("");
                    foreach (GXDLMSSecuritySetup it in target.Parent.GetObjects(ObjectType.SecuritySetup))
                    {
                        SecuritySetupCb.Items.Add(it);
                        if (target.SecuritySetupReference == it.LogicalName)
                        {
                            SecuritySetupCb.SelectedIndexChanged -= new System.EventHandler(this.SecuritySetupCb_SelectedIndexChanged);
                            SecuritySetupCb.SelectedItem          = it;
                            SecuritySetupCb.SelectedIndexChanged += new System.EventHandler(this.SecuritySetupCb_SelectedIndexChanged);
                        }
                    }
                }
                else
                {
                    GXDLMSSecuritySetup it = new GXDLMSSecuritySetup(target.SecuritySetupReference);
                    SecuritySetupCb.Items.Add(it);
                    SecuritySetupCb.SelectedIndexChanged -= new System.EventHandler(this.SecuritySetupCb_SelectedIndexChanged);
                    SecuritySetupCb.SelectedItem          = it;
                    SecuritySetupCb.SelectedIndexChanged += new System.EventHandler(this.SecuritySetupCb_SelectedIndexChanged);
                }
            }
            //User list.
            else if (arg.Index == 10)
            {
                List <KeyValuePair <byte, string> > items = target.UserList;
                UsersList.Items.Clear();
                if (items != null)
                {
                    foreach (KeyValuePair <byte, string> it in items)
                    {
                        ListViewItem li = UsersList.Items.Add(it.Key.ToString());
                        li.SubItems.Add(it.Value);
                        li.Tag = it.Key;
                    }
                }
            }
            else if (arg.Index == 11) //Current user
            {
                foreach (ListViewItem it in UsersList.Items)
                {
                    if ((byte)it.Tag == target.CurrentUser.Key)
                    {
                        it.Selected = true;
                    }
                    else if (it.Selected)
                    {
                        it.Selected = false;
                    }
                }
            }
        }
Пример #27
0
 private void OnConformanceChange(object sender, EventArgs e)
 {
     if (!Freeze)
     {
         try
         {
             Conformance c = Conformance.None;
             if (GeneralProtectionCB.Checked)
             {
                 c |= Conformance.GeneralProtection;
             }
             if (GeneralBlockTransferCB.Checked)
             {
                 c |= Conformance.GeneralBlockTransfer;
             }
             if (Attribute0SetReferencingCB.Checked)
             {
                 c |= Conformance.Attribute0SupportedWithSet;
             }
             if (PriorityManagementCB.Checked)
             {
                 c |= Conformance.PriorityMgmtSupported;
             }
             if (Attribute0GetReferencingCB.Checked)
             {
                 c |= Conformance.Attribute0SupportedWithGet;
             }
             if (GetBlockTransferCB.Checked)
             {
                 c |= Conformance.BlockTransferWithGetOrRead;
             }
             if (SetBlockTransferCB.Checked)
             {
                 c |= Conformance.BlockTransferWithSetOrWrite;
             }
             if (ActionBlockTransferCB.Checked)
             {
                 c |= Conformance.BlockTransferWithAction;
             }
             if (MultipleReferencesCB.Checked)
             {
                 c |= Conformance.MultipleReferences;
             }
             if (DataNotificationCB.Checked)
             {
                 c |= Conformance.DataNotification;
             }
             if (AccessCB.Checked)
             {
                 c |= Conformance.Access;
             }
             if (GetCB.Checked)
             {
                 c |= Conformance.Get;
             }
             if (SetCB.Checked)
             {
                 c |= Conformance.Set;
             }
             if (SelectiveAccessCB.Checked)
             {
                 c |= Conformance.SelectiveAccess;
             }
             if (EventNotificationCB.Checked)
             {
                 c |= Conformance.EventNotification;
             }
             if (ActionCB.Checked)
             {
                 c |= Conformance.Action;
             }
             if (DeltaValueEncodingCb.Checked)
             {
                 c |= Conformance.DeltaValueEncoding;
             }
             GXDLMSAssociationLogicalName target = Target as GXDLMSAssociationLogicalName;
             target.XDLMSContextInfo.Conformance = c;
             errorProvider1.SetError(LNSettings, Properties.Resources.ValueChangedTxt);
             Target.UpdateDirty(5, target.XDLMSContextInfo);
         }
         catch (Exception ex)
         {
             MessageBox.Show(this, ex.Message);
         }
     }
 }