internal static void SetValue(GXByteBuffer buff, object data)
 {
     GXByteBuffer tmp = new GXByteBuffer();
     tmp.Add(data);
     buff.Add((byte)tmp.Size);
     buff.Set(tmp.Array());
 }
示例#2
0
        ///<summary>
        /// Chipher text.
        ///</summary>
        ///<param name="settings">
        ///DLMS settings.
        ///</param>
        ///<param name="cipher">
        ///Cipher.
        ///</param>
        ///<param name="ic">
        ///Invocation counter.
        ///</param>
        ///<param name="data">
        ///Text to chipher.
        ///</param>
        ///<param name="secret">
        ///Secret.
        ///</param>
        ///<returns>
        ///Chiphered text.
        ///</returns>
        public static byte[] Secure(GXDLMSSettings settings, GXICipher cipher, uint ic, byte[] data, byte[] secret)
        {
            byte[] tmp;
            if (settings.Authentication == Authentication.High)
            {
                int len = secret.Length;
                if (len % 16 != 0)
                {
                    len += 16 - (secret.Length % 16);
                }
                byte[] p = new byte[len];
                byte[] s = new byte[16];
                byte[] x = new byte[16];
                int    i;
                data.CopyTo(p, 0);
                secret.CopyTo(s, 0);
                for (i = 0; i < p.Length; i += 16)
                {
                    Buffer.BlockCopy(p, i, x, 0, 16);
                    GXAes128.Encrypt(x, s);
                    Buffer.BlockCopy(x, 0, p, i, 16);
                }
                Buffer.BlockCopy(p, 0, x, 0, 16);
                return(x);
            }
            // Get server Challenge.
            GXByteBuffer challenge = new GXByteBuffer();

            // Get shared secret
            if (settings.Authentication == Authentication.HighGMAC)
            {
                challenge.Set(data);
            }
            else
            {
                challenge.Set(data);
                challenge.Set(secret);
            }
            tmp = challenge.Array();
            if (settings.Authentication == Authentication.HighMD5)
            {
#if !WINDOWS_UWP
                using (MD5 md5Hash = MD5.Create())
                {
                    tmp = md5Hash.ComputeHash(tmp);
                    return(tmp);
                }
#endif
            }
            else if (settings.Authentication == Authentication.HighSHA1)
            {
#if !WINDOWS_UWP
                using (SHA1 sha = new SHA1CryptoServiceProvider())
                {
                    tmp = sha.ComputeHash(tmp);
                    return(tmp);
                }
#endif
            }
            else if (settings.Authentication == Authentication.HighGMAC)
            {
                //SC is always Security.Authentication.
                AesGcmParameter p = new AesGcmParameter(0, Security.Authentication, ic,
                                                        secret, cipher.BlockCipherKey, cipher.AuthenticationKey);
                p.Type = CountType.Tag;
                challenge.Clear();
                challenge.SetUInt8((byte)Security.Authentication);
                challenge.SetUInt32(p.InvocationCounter);
                challenge.Set(GXDLMSChippering.EncryptAesGcm(p, tmp));
                tmp = challenge.Array();
                return(tmp);
            }
            return(data);
        }
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            switch (e.Index)
            {
            case 1:
                return(GXCommon.LogicalNameToBytes(LogicalName));

            case 2:
                if (CalendarName == null)
                {
                    return(null);
                }
                return(ASCIIEncoding.ASCII.GetBytes(CalendarName));

            case 3:
                return(Enabled);

            case 4:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(4);
                data.SetUInt8((byte)DataType.UInt8);
                data.SetUInt8(Plan.DefaultTariffBand);

                data.SetUInt8((byte)DataType.Array);
                data.SetUInt8(2);
                GetSeason(Plan.WinterSeason, data);
                GetSeason(Plan.SummerSeason, data);

                GXCommon.SetData(null, data, DataType.BitString, Plan.WeeklyActivation);
                data.SetUInt8((byte)DataType.Array);
                if (Plan.SpecialDays == null)
                {
                    data.SetUInt8(0);
                }
                else
                {
                    data.SetUInt8((byte)Plan.SpecialDays.Length);
                    foreach (UInt16 it in Plan.SpecialDays)
                    {
                        data.SetUInt8((byte)DataType.UInt16);
                        data.SetUInt16(it);
                    }
                }
                return(data.Array());
            }

            case 5:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                //Count
                data.SetUInt8(2);
                if (ActivationTime == null)
                {
                    //Time
                    GXCommon.SetData(settings, data, DataType.OctetString, new GXTime());
                    //Date
                    GXCommon.SetData(settings, data, DataType.OctetString, new GXDate(DateTime.MinValue));
                }
                else
                {
                    //Time
                    GXCommon.SetData(settings, data, DataType.OctetString, new GXTime(ActivationTime));
                    //Date
                    GXCommon.SetData(settings, data, DataType.OctetString, new GXDate(ActivationTime));
                }
                return(data.Array());
            }
            }
            e.Error = ErrorCode.ReadWriteDenied;
            return(null);
        }
示例#4
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            if (e.Index == 1)
            {
                return(this.LogicalName);
            }
            GXByteBuffer buff = new GXByteBuffer();

            if (e.Index == 2)
            {
                buff.SetUInt8(DataType.Array);
                GXCommon.SetObjectCount(PushObjectList.Count, buff);
                foreach (KeyValuePair <GXDLMSObject, GXDLMSCaptureObject> it in PushObjectList)
                {
                    buff.SetUInt8(DataType.Structure);
                    buff.SetUInt8(4);
                    GXCommon.SetData(settings, buff, DataType.UInt16, it.Key.ObjectType);
                    GXCommon.SetData(settings, buff, DataType.OctetString, it.Key.LogicalName);
                    GXCommon.SetData(settings, buff, DataType.Int8, it.Value.AttributeIndex);
                    GXCommon.SetData(settings, buff, DataType.UInt16, it.Value.DataIndex);
                }
                return(buff.Array());
            }
            if (e.Index == 3)
            {
                buff.SetUInt8(DataType.Structure);
                buff.SetUInt8(3);
                GXCommon.SetData(settings, buff, DataType.UInt8, Service);
                if (Destination != null)
                {
                    GXCommon.SetData(settings, buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(Destination));
                }
                else
                {
                    GXCommon.SetData(settings, buff, DataType.OctetString, null);
                }
                GXCommon.SetData(settings, buff, DataType.UInt8, Message);
                return(buff.Array());
            }
            if (e.Index == 4)
            {
                buff.SetUInt8(DataType.Array);
                GXCommon.SetObjectCount(CommunicationWindow.Count, buff);
                foreach (KeyValuePair <GXDateTime, GXDateTime> it in CommunicationWindow)
                {
                    buff.SetUInt8(DataType.Structure);
                    buff.SetUInt8(2);
                    GXCommon.SetData(settings, buff, DataType.OctetString, it.Key);
                    GXCommon.SetData(settings, buff, DataType.OctetString, it.Value);
                }
                return(buff.Array());
            }
            if (e.Index == 5)
            {
                return(RandomisationStartInterval);
            }
            if (e.Index == 6)
            {
                return(NumberOfRetries);
            }
            if (e.Index == 7)
            {
                return(RepetitionDelay);
            }
            e.Error = ErrorCode.ReadWriteDenied;
            return(null);
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         //If client set new value.
         if (!settings.IsServer && Scaler != 1 && CurrentAverageValue != null)
         {
             Type type = null;
             if (CurrentAverageValue != null)
             {
                 type = CurrentAverageValue.GetType();
             }
             object tmp;
             tmp = Convert.ToDouble(CurrentAverageValue) / Scaler;
             if (type != null)
             {
                 tmp = Convert.ChangeType(tmp, type);
             }
             return(tmp);
         }
         return(CurrentAverageValue);
     }
     if (e.Index == 3)
     {
         //If client set new value.
         if (!settings.IsServer && Scaler != 1 && LastAverageValue != null)
         {
             DataType dt = base.GetDataType(3);
             if (dt == DataType.None && LastAverageValue != null)
             {
                 dt = GXCommon.GetDLMSDataType(LastAverageValue.GetType());
             }
             object tmp;
             tmp = Convert.ToDouble(LastAverageValue) / Scaler;
             if (dt != DataType.None)
             {
                 tmp = Convert.ChangeType(tmp, GXCommon.GetDataType(dt));
             }
             return(tmp);
         }
         return(LastAverageValue);
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(2);
         GXCommon.SetData(settings, data, DataType.Int8, scaler);
         GXCommon.SetData(settings, data, DataType.Enum, Unit);
         return(data.Array());
     }
     if (e.Index == 5)
     {
         return(Status);
     }
     if (e.Index == 6)
     {
         return(CaptureTime);
     }
     if (e.Index == 7)
     {
         return(StartTimeCurrent);
     }
     if (e.Index == 8)
     {
         return(Period);
     }
     if (e.Index == 9)
     {
         return(NumberOfPeriods);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (ObjectList == null)
     {
         ObjectList = new GXDLMSObjectCollection();
     }
     if (e.Index == 1)
     {
         return this.LogicalName;
     }
     else if (e.Index == 2)
     {
         return GetObjects(settings, e).Array();
     }
     else if (e.Index == 3)
     {
         bool lnExists = ObjectList.FindBySN(this.ShortName) != null;
         //Add count
         int cnt = ObjectList.Count;
         if (!lnExists)
         {
             ++cnt;
         }
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         GXCommon.SetObjectCount(cnt, data);
         foreach (GXDLMSObject it in ObjectList)
         {
             GetAccessRights(settings, it, data);
         }
         if (!lnExists)
         {
             GetAccessRights(settings, this, data);
         }
         return data.Array();
     }
     else if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         GXCommon.SetData(settings, data, DataType.OctetString, SecuritySetupReference);
         return data.Array();
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return null;
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8(DataType.Array);
         if (SynchronizationRegister == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(SynchronizationRegister.Count, bb);
             foreach (var it in SynchronizationRegister)
             {
                 bb.SetUInt8(2);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.Key);
                 GXCommon.SetData(settings, bb, DataType.UInt32, it.Value);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 3)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8(DataType.Structure);
         bb.SetUInt8(5);
         GXCommon.SetData(settings, bb, DataType.UInt32, PhysicalLayerDesynchronization);
         GXCommon.SetData(settings, bb, DataType.UInt32, TimeOutNotAddressedDesynchronization);
         GXCommon.SetData(settings, bb, DataType.UInt32, TimeOutFrameNotOkDesynchronization);
         GXCommon.SetData(settings, bb, DataType.UInt32, WriteRequestDesynchronization);
         GXCommon.SetData(settings, bb, DataType.UInt32, WrongInitiatorDesynchronization);
         return(bb.Array());
     }
     if (e.Index == 4)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8(DataType.Array);
         if (BroadcastFramesCounter == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(BroadcastFramesCounter.Count, bb);
             foreach (var it in BroadcastFramesCounter)
             {
                 bb.SetUInt8(2);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.Key);
                 GXCommon.SetData(settings, bb, DataType.UInt32, it.Value);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 5)
     {
         return(RepetitionsCounter);
     }
     if (e.Index == 6)
     {
         return(TransmissionsCounter);
     }
     if (e.Index == 7)
     {
         return(CrcOkFramesCounter);
     }
     if (e.Index == 8)
     {
         return(CrcNOkFramesCounter);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#8
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, int index, int selector, object parameters)
 {
     if (index == 1)
     {
         return(this.LogicalName);
     }
     if (index == 2)
     {
         return(PHYReference);
     }
     if (index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (LCPOptions == null)
         {
             data.SetUInt8(0);
         }
         else
         {
             data.SetUInt8((byte)IPCPOptions.Length);
             foreach (GXDLMSPppSetupLcpOption it in LCPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)3);
                 GXCommon.SetData(data, DataType.UInt8, it.Type);
                 GXCommon.SetData(data, DataType.UInt8, it.Length);
                 GXCommon.SetData(data, GXCommon.GetValueType(it.Data), it.Data);
             }
         }
         return(data.Array());
     }
     if (index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (IPCPOptions == null)
         {
             data.SetUInt8(0);
         }
         else
         {
             data.SetUInt8((byte)IPCPOptions.Length);
             foreach (GXDLMSPppSetupIPCPOption it in IPCPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)3);
                 GXCommon.SetData(data, DataType.UInt8, it.Type);
                 GXCommon.SetData(data, DataType.UInt8, it.Length);
                 GXCommon.SetData(data, GXCommon.GetValueType(it.Data), it.Data);
             }
         }
         return(data.Array());
     }
     else if (index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(2);
         GXCommon.SetData(data, DataType.OctetString, UserName);
         GXCommon.SetData(data, DataType.OctetString, Password);
         return(data.Array());
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
示例#9
0
        /// <summary>
        /// Decrypt data.
        /// </summary>
        /// <param name="p">Decryption parameters</param>
        /// <returns>Decrypted data.</returns>
        public static byte[] DecryptAesGcm(AesGcmParameter p, GXByteBuffer data)
        {
            if (data == null || data.Size < 2)
            {
                throw new ArgumentOutOfRangeException("cryptedData");
            }
            int len;
            Command cmd = (Command)data.GetUInt8();
            switch (cmd)
            {
                case Command.GloGeneralCiphering:
                    len = GXCommon.GetObjectCount(data);
                    p.SystemTitle = new byte[len];
                    data.Get(p.SystemTitle);
                    break;
                case Command.GloInitiateRequest:
                case Command.GloInitiateResponse:
                case Command.GloReadRequest:
                case Command.GloReadResponse:
                case Command.GloWriteRequest:
                case Command.GloWriteResponse:
                case Command.GloGetRequest:
                case Command.GloGetResponse:
                case Command.GloSetRequest:
                case Command.GloSetResponse:
                case Command.GloMethodRequest:
                case Command.GloMethodResponse:
                case Command.GloEventNotificationRequest:
                    break;
                default:
                    throw new ArgumentOutOfRangeException("cryptedData");
            }
            len = Gurux.DLMS.Internal.GXCommon.GetObjectCount(data);
            p.Security = (Gurux.DLMS.Enums.Security)data.GetUInt8();
            p.FrameCounter = data.GetUInt32();
            System.Diagnostics.Debug.WriteLine("Decrypt settings: " + p.ToString());
            System.Diagnostics.Debug.WriteLine("Encrypted: " + GXCommon.ToHex(data.Array(), true));

            byte[] tag = new byte[12];
            byte[] encryptedData;
            int length;
            if (p.Security == Gurux.DLMS.Enums.Security.Authentication)
            {
                length = data.Size - data.Position - 12;
                encryptedData = new byte[length];
                data.Get(encryptedData);
                data.Get(tag);
                // Check tag.
                EncryptAesGcm(p, encryptedData);
                if (!GXDLMSChipperingStream.TagsEquals(tag, p.CountTag))
                {
                    throw new GXDLMSException("Decrypt failed. Invalid tag.");
                }
                return encryptedData;
            }
            byte[] ciphertext = null;
            if (p.Security == Gurux.DLMS.Enums.Security.Encryption)
            {
                length = data.Size - data.Position;
                ciphertext = new byte[length];
                data.Get(ciphertext);
            }
            else if (p.Security == Gurux.DLMS.Enums.Security.AuthenticationEncryption)
            {
                length = data.Size - data.Position - 12;
                ciphertext = new byte[length];
                data.Get(ciphertext);
                data.Get(tag);
            }
            byte[] aad = GetAuthenticatedData(p.Security, p.AuthenticationKey, data.Array());
            byte[] iv = GetNonse(p.FrameCounter, p.SystemTitle);
            GXDLMSChipperingStream gcm = new GXDLMSChipperingStream(p.Security, true, p.BlockCipherKey, aad, iv, tag);
            gcm.Write(ciphertext);
            return gcm.FlushFinalBlock();
        }
示例#10
0
 private static byte[] GetAuthenticatedData(Gurux.DLMS.Enums.Security security, byte[] AuthenticationKey, byte[] plainText)
 {
     if (security == Gurux.DLMS.Enums.Security.Authentication)
     {
         GXByteBuffer tmp2 = new GXByteBuffer();
         tmp2.SetUInt8((byte)security);
         tmp2.Set(AuthenticationKey);
         tmp2.Set(plainText);
         return tmp2.Array();
     }
     else if (security == Gurux.DLMS.Enums.Security.Encryption)
     {
         return AuthenticationKey;
     }
     else if (security == Gurux.DLMS.Enums.Security.AuthenticationEncryption)
     {
         GXByteBuffer tmp2 = new GXByteBuffer();
         tmp2.SetUInt8((byte)security);
         tmp2.Set(AuthenticationKey);
         return tmp2.Array();
     }
     return null;
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, int index, int selector, object parameters)
 {
     if (index == 1)
     {
         return(this.LogicalName);
     }
     if (index == 2)
     {
         return(GetObjects());
     }
     if (index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         data.SetUInt8(2);
         data.SetUInt8((byte)DataType.UInt8);
         data.SetUInt16(ClientSAP);
         data.SetUInt8((byte)DataType.UInt16);
         data.SetUInt16(ServerSAP);
         return(data.Array());
     }
     if (index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.JointIsoCtt);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.Country);
         GXCommon.SetData(data, DataType.UInt16, ApplicationContextName.CountryName);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.IdentifiedOrganization);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.DlmsUA);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.ApplicationContext);
         GXCommon.SetData(data, DataType.UInt8, ApplicationContextName.ContextId);
         return(data.Array());
     }
     if (index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(6);
         GXCommon.SetData(data, DataType.BitString, XDLMSContextInfo.Conformance);
         GXCommon.SetData(data, DataType.UInt16, XDLMSContextInfo.MaxReceivePduSize);
         GXCommon.SetData(data, DataType.UInt16, XDLMSContextInfo.MaxSendPpuSize);
         GXCommon.SetData(data, DataType.UInt8, XDLMSContextInfo.DlmsVersionNumber);
         GXCommon.SetData(data, DataType.Int8, XDLMSContextInfo.QualityOfService);
         GXCommon.SetData(data, DataType.OctetString, XDLMSContextInfo.CypheringInfo);
         return(data.Array());
     }
     if (index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.JointIsoCtt);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.Country);
         GXCommon.SetData(data, DataType.UInt16, AuthenticationMechanismMame.CountryName);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.IdentifiedOrganization);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.DlmsUA);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.AuthenticationMechanismName);
         GXCommon.SetData(data, DataType.UInt8, AuthenticationMechanismMame.MechanismId);
         return(data.Array());
     }
     if (index == 7)
     {
         return(Secret);
     }
     if (index == 8)
     {
         return(AssociationStatus);
     }
     if (index == 9)
     {
         if (SecuritySetupReference == null)
         {
             return(null);
         }
         return(ASCIIEncoding.ASCII.GetBytes(SecuritySetupReference));
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
示例#12
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return this.LogicalName;
     }
     if (e.Index == 2)
     {
         return (byte)Mode;
     }
     if (e.Index == 3)
     {
         int cnt = ListeningWindow.Count;
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         GXCommon.SetObjectCount(cnt, data);
         if (cnt != 0)
         {
             foreach (var it in ListeningWindow)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)2); //Count
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Key); //start_time
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Value); //end_time
             }
         }
         return data.Array();
     }
     if (e.Index == 4)
     {
         return Status;
     }
     if (e.Index == 5)
     {
         return NumberOfCalls;
     }
     if (e.Index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         GXCommon.SetObjectCount(2, data);
         GXCommon.SetData(settings, data, DataType.UInt8, NumberOfRingsInListeningWindow);
         GXCommon.SetData(settings, data, DataType.UInt8, NumberOfRingsOutListeningWindow);
         return data.Array();
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return null;
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return this.LogicalName;
     }
     if (e.Index == 2)
     {
         return GetObjects(settings, e).Array();
     }
     if (e.Index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         data.SetUInt8(2);
         data.SetUInt8((byte)DataType.UInt8);
         data.SetUInt8(ClientSAP);
         data.SetUInt8((byte)DataType.UInt16);
         data.SetUInt16(ServerSAP);
         return data.Array();
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.JointIsoCtt);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.Country);
         GXCommon.SetData(settings, data, DataType.UInt16, ApplicationContextName.CountryName);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.IdentifiedOrganization);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.DlmsUA);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.ApplicationContext);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.ContextId);
         return data.Array();
     }
     if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(6);
         GXCommon.SetData(settings, data, DataType.BitString, XDLMSContextInfo.Conformance);
         GXCommon.SetData(settings, data, DataType.UInt16, XDLMSContextInfo.MaxReceivePduSize);
         GXCommon.SetData(settings, data, DataType.UInt16, XDLMSContextInfo.MaxSendPpuSize);
         GXCommon.SetData(settings, data, DataType.UInt8, XDLMSContextInfo.DlmsVersionNumber);
         GXCommon.SetData(settings, data, DataType.Int8, XDLMSContextInfo.QualityOfService);
         GXCommon.SetData(settings, data, DataType.OctetString, XDLMSContextInfo.CypheringInfo);
         return data.Array();
     }
     if (e.Index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.JointIsoCtt);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.Country);
         GXCommon.SetData(settings, data, DataType.UInt16, AuthenticationMechanismMame.CountryName);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.IdentifiedOrganization);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.DlmsUA);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.AuthenticationMechanismName);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismMame.MechanismId);
         return data.Array();
     }
     if (e.Index == 7)
     {
         return Secret;
     }
     if (e.Index == 8)
     {
         return AssociationStatus;
     }
     if (e.Index == 9)
     {
         if (SecuritySetupReference == null)
         {
             return null;
         }
         return ASCIIEncoding.ASCII.GetBytes(SecuritySetupReference);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return null;
 }
示例#14
0
        private static byte[] GetUserInformation(GXDLMSSettings settings, GXICipher cipher)
        {
            GXByteBuffer data = new GXByteBuffer();
            data.SetUInt8(Command.InitiateResponse); // Tag for xDLMS-Initiate response
                                                     // NegotiatedQualityOfService (not used)
            data.SetUInt8(0x1);
            data.SetUInt8(0x00);
            // DLMS Version Number
            data.SetUInt8(06);
            data.SetUInt8(0x5F);
            data.SetUInt8(0x1F);
            data.SetUInt8(0x04);// length of the conformance block
            data.SetUInt8(0x00);// encoding the number of unused bits in the bit string
            if (settings.UseLogicalNameReferencing)
            {
                data.Set(settings.LnSettings.ConformanceBlock);
            }
            else
            {
                data.Set(settings.SnSettings.ConformanceBlock);

            }
            data.SetUInt16(settings.MaxPduSize);
            //VAA Name VAA name (0x0007 for LN referencing and 0xFA00 for SN)
            if (settings.UseLogicalNameReferencing)
            {
                data.SetUInt16(0x0007);
            }
            else
            {
                data.SetUInt16(0xFA00);
            }
            if (cipher != null && cipher.IsCiphered())
            {
                return cipher.Encrypt((byte)Command.GloInitiateResponse, cipher.SystemTitle, data.Array());
            }
            return data.Array();
        }
示例#15
0
 /// <summary>
 /// Generate user information.
 /// </summary>
 /// <param name="settings">DLMS settings.</param>
 /// <param name="cipher"></param>
 /// <param name="data">Generated user information.</param>
 static internal void GenerateUserInformation(GXDLMSSettings settings, GXICipher cipher, GXByteBuffer encryptedData, GXByteBuffer data)
 {
     data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
     if (cipher == null || !cipher.IsCiphered())
     {
         //Length for AARQ user field
         data.SetUInt8(0x10);
         //Coding the choice for user-information (Octet STRING, universal)
         data.SetUInt8(BerType.OctetString);
         //Length
         data.SetUInt8(0x0E);
         GetInitiateRequest(settings, cipher, data);
     }
     else
     {
         if (encryptedData != null && encryptedData.Size != 0)
         {
             //Length for AARQ user field
             data.SetUInt8((byte)(4 + encryptedData.Size));
             //Tag
             data.SetUInt8(BerType.OctetString);
             data.SetUInt8((byte)(2 + encryptedData.Size));
             //Coding the choice for user-information (Octet STRING, universal)
             data.SetUInt8((byte)Command.GloInitiateRequest);
             data.SetUInt8((byte)encryptedData.Size);
             data.Set(encryptedData);
         }
         else
         {
             GXByteBuffer tmp = new GXByteBuffer();
             GetInitiateRequest(settings, cipher, tmp);
             byte[] crypted = cipher.Encrypt((byte)Command.GloInitiateRequest, cipher.SystemTitle, tmp.Array());
             //Length for AARQ user field
             data.SetUInt8((byte)(2 + crypted.Length));
             //Coding the choice for user-information (Octet STRING, universal)
             data.SetUInt8(BerType.OctetString);
             data.SetUInt8((byte)crypted.Length);
             data.Set(crypted);
         }
     }
 }
示例#16
0
        static internal byte[] EncryptAesGcm(AesGcmParameter param, byte[] plainText)
        {
            System.Diagnostics.Debug.WriteLine("Encrypt settings: " + param.ToString());
            param.CountTag = null;
            GXByteBuffer data = new GXByteBuffer();

            if (param.Type == CountType.Packet)
            {
                data.SetUInt8((byte)param.Security);
            }
            byte[] tmp = BitConverter.GetBytes((UInt32)param.InvocationCounter).Reverse().ToArray();
            byte[] aad = GetAuthenticatedData(param, plainText);
            GXDLMSChipperingStream gcm = new GXDLMSChipperingStream(param.Security, true, param.BlockCipherKey,
                                                                    aad, GetNonse((UInt32)param.InvocationCounter, param.SystemTitle), null);

            // Encrypt the secret message
            if (param.Security != Gurux.DLMS.Enums.Security.Authentication)
            {
                gcm.Write(plainText);
            }
            byte[] ciphertext = gcm.FlushFinalBlock();
            if (param.Security == Gurux.DLMS.Enums.Security.Authentication)
            {
                if (param.Type == CountType.Packet)
                {
                    data.Set(tmp);
                }
                if ((param.Type & CountType.Data) != 0)
                {
                    data.Set(plainText);
                }
                if ((param.Type & CountType.Tag) != 0)
                {
                    param.CountTag = gcm.GetTag();
                    data.Set(param.CountTag);
                }
            }
            else if (param.Security == Gurux.DLMS.Enums.Security.Encryption)
            {
                if (param.Type == CountType.Packet)
                {
                    data.Set(tmp);
                }
                data.Set(ciphertext);
            }
            else if (param.Security == Gurux.DLMS.Enums.Security.AuthenticationEncryption)
            {
                if (param.Type == CountType.Packet)
                {
                    data.Set(tmp);
                }
                if ((param.Type & CountType.Data) != 0)
                {
                    data.Set(ciphertext);
                }
                if ((param.Type & CountType.Tag) != 0)
                {
                    param.CountTag = gcm.GetTag();
                    data.Set(param.CountTag);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("security");
            }
            if (param.Type == CountType.Packet)
            {
                GXByteBuffer tmp2 = new GXByteBuffer((ushort)(10 + data.Size));
                tmp2.SetUInt8(param.Tag);
                GXCommon.SetObjectCount(data.Size, tmp2);
                tmp2.Set(data.Array());
                return(tmp2.Array());
            }
            byte[] crypted = data.Array();
            System.Diagnostics.Debug.WriteLine("Crypted: " + GXCommon.ToHex(crypted, true));
            return(crypted);
        }
        /// <summary>
        /// Returns buffer data.
        /// </summary>
        /// <param name="settings">DLMS settings.</param>
        /// <param name="e"></param>
        /// <param name="table"></param>
        /// <param name="columns">Columns to get. NULL if not used.</param>
        /// <returns></returns>
        byte[] GetData(GXDLMSSettings settings, ValueEventArgs e, List <object[]> table,
                       List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > columns)
        {
            int pos = 0;
            List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > cols = columns;

            if (columns == null)
            {
                cols = CaptureObjects;
            }
            DataType[] types = new DataType[cols.Count];
            foreach (GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> it in cols)
            {
                types[pos] = it.Key.GetDataType(it.Value.AttributeIndex);
                ++pos;
            }
            UInt16 columnStart = 1, columnEnd = 0;

            if (e.Selector == 2)
            {
                columnStart = (UInt16)((object[])e.Parameters)[2];
                columnEnd   = (UInt16)((object[])e.Parameters)[3];
            }

            if (columnStart > 1 || columnEnd != 0)
            {
                pos  = 1;
                cols = new List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> >();
                foreach (GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> it in CaptureObjects)
                {
                    if (!(pos < columnStart || pos > columnEnd))
                    {
                        cols.Add(it);
                    }
                    ++pos;
                }
                pos = 0;
            }
            GXByteBuffer data = new GXByteBuffer();

            if (settings.Index == 0)
            {
                data.SetUInt8((byte)DataType.Array);
                if (e.RowEndIndex != 0)
                {
                    GXCommon.SetObjectCount((int)(e.RowEndIndex - e.RowBeginIndex), data);
                }
                else
                {
                    GXCommon.SetObjectCount(table.Count, data);
                }
            }

            foreach (object[] items in table)
            {
                data.SetUInt8((byte)DataType.Structure);
                GXCommon.SetObjectCount(cols.Count, data);
                pos = 0;
                DataType tp;
                foreach (object value in items)
                {
                    if (cols == null || cols.Contains(CaptureObjects[pos]))
                    {
                        tp = types[pos];
                        if (tp == DataType.None)
                        {
                            tp         = GXDLMSConverter.GetDLMSDataType(value);
                            types[pos] = tp;
                        }
                        if (value == null)
                        {
                            tp = DataType.None;
                        }
                        GXCommon.SetData(settings, data, tp, value);
                    }
                    ++pos;
                }
                ++settings.Index;
            }
            if (e.RowEndIndex != 0)
            {
                e.RowBeginIndex += (UInt32)table.Count;
            }
            else
            {
                settings.Index = 0;
            }
            return(data.Array());
        }
示例#18
0
 static internal byte[] EncryptAesGcm(AesGcmParameter param, byte[] plainText)
 {
     System.Diagnostics.Debug.WriteLine("Encrypt settings: " + param.ToString());
     param.CountTag = null;
     GXByteBuffer data = new GXByteBuffer();
     if (param.Type == CountType.Packet)
     {
         data.SetUInt8((byte)param.Security);
     }
     byte[] tmp = BitConverter.GetBytes(param.FrameCounter).Reverse().ToArray();
     byte[] aad = GetAuthenticatedData(param.Security, param.AuthenticationKey, plainText);
     GXDLMSChipperingStream gcm = new GXDLMSChipperingStream(param.Security, true, param.BlockCipherKey,
             aad, GetNonse(param.FrameCounter, param.SystemTitle), null);
     // Encrypt the secret message
     if (param.Security != Gurux.DLMS.Enums.Security.Authentication)
     {
         gcm.Write(plainText);
     }
     byte[] ciphertext = gcm.FlushFinalBlock();
     if (param.Security == Gurux.DLMS.Enums.Security.Authentication)
     {
         if (param.Type == CountType.Packet)
         {
             data.Set(tmp);
         }
         if ((param.Type & CountType.Data) != 0)
         {
             data.Set(plainText);
         }
         if ((param.Type & CountType.Tag) != 0)
         {
             param.CountTag = gcm.GetTag();
             data.Set(param.CountTag);
         }
     }
     else if (param.Security == Gurux.DLMS.Enums.Security.Encryption)
     {
         if (param.Type == CountType.Packet)
         {
             data.Set(tmp);
         }
         data.Set(ciphertext);
     }
     else if (param.Security == Gurux.DLMS.Enums.Security.AuthenticationEncryption)
     {
         if (param.Type == CountType.Packet)
         {
             data.Set(tmp);
         }
         if ((param.Type & CountType.Data) != 0)
         {
             data.Set(ciphertext);
         }
         if ((param.Type & CountType.Tag) != 0)
         {
             param.CountTag = gcm.GetTag();
             data.Set(param.CountTag);
         }
     }
     else
     {
         throw new ArgumentOutOfRangeException("security");
     }
     if (param.Type == CountType.Packet)
     {
         GXByteBuffer tmp2 = new GXByteBuffer((ushort)(10 + data.Size));
         tmp2.SetUInt8(param.Tag);
         GXCommon.SetObjectCount(data.Size, tmp2);
         tmp2.Set(data.Array());
         return tmp2.Array();
     }
     byte[] crypted = data.Array();
     System.Diagnostics.Debug.WriteLine("Crypted: " + GXCommon.ToHex(crypted, true));
     return crypted;
 }
示例#19
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(GXCommon.LogicalNameToBytes(MBusPortReference));
     }
     if (e.Index == 3)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.SetUInt8((byte)DataType.Array);
         GXCommon.SetObjectCount(CaptureDefinition.Count, buff);
         foreach (KeyValuePair <string, string> it in CaptureDefinition)
         {
             buff.SetUInt8((byte)DataType.Structure);
             buff.SetUInt8(2);
             GXCommon.SetData(settings, buff, DataType.UInt8, it.Key);
             GXCommon.SetData(settings, buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Value));
         }
         return(buff.Array());
     }
     if (e.Index == 4)
     {
         return(CapturePeriod);
     }
     if (e.Index == 5)
     {
         return(PrimaryAddress);
     }
     if (e.Index == 6)
     {
         return(IdentificationNumber);
     }
     if (e.Index == 7)
     {
         return(ManufacturerID);
     }
     if (e.Index == 8)
     {
         return(DataHeaderVersion);
     }
     if (e.Index == 9)
     {
         return(DeviceType);
     }
     if (e.Index == 10)
     {
         return(AccessNumber);
     }
     if (e.Index == 11)
     {
         return(Status);
     }
     if (e.Index == 12)
     {
         return(Alarm);
     }
     if (Version > 0)
     {
         if (e.Index == 13)
         {
             return(Configuration);
         }
         if (e.Index == 14)
         {
             return(EncryptionKeyStatus);
         }
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#20
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            if (e.Index == 1)
            {
                return(this.LogicalName);
            }
            if (e.Index == 2)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                data.SetUInt8((byte)Entries.Count);

                /*
                 * foreach (GXScheduleEntry it in Entries)
                 * {
                 *  data.SetUInt8((byte)DataType.Structure);
                 *  data.SetUInt8(10);
                 *  //Add index.
                 *  data.SetUInt8((byte)DataType.UInt8);
                 *  data.SetUInt8(it.Index);
                 *  //Add enable.
                 *  data.SetUInt8((byte)DataType.Boolean);
                 *  data.SetUInt8((byte) (it.Enable ? 1 : 0));
                 *
                 *  //Add logical Name.
                 *  data.SetUInt8((byte)DataType.OctetString);
                 *  data.SetUInt8((byte) it.LogicalName.Length);
                 *  //TODO: data.SetUInt8((byte)it.LogicalName.Length);
                 *
                 *  //Add script selector.
                 *  data.SetUInt8((byte)DataType.UInt8);
                 *  data.SetUInt8(it.ScriptSelector);
                 *
                 *  //Add switch time.
                 *  ret = var_setDateTime(&tmp, &se->switchTime);
                 *  if (ret != 0)
                 *  {
                 *      var_clear(&tmp);
                 *      break;
                 *  }
                 *  ret = var_getBytes(&tmp, &value->byteArr);
                 *  var_clear(&tmp);
                 *  if (ret != 0)
                 *  {
                 *      break;
                 *  }
                 *  //Add validity window.
                 *  data.SetUInt8((byte)DataType.UInt8);
                 *  data.SetUInt8(it.ValidityWindow);
                 *
                 *  //Add exec week days.
                 *  ba_setUInt8(&value->byteArr, DLMS_DATA_TYPE_BIT_STRING);
                 *  setObjectCount(se->execWeekdays.size, &value->byteArr);
                 *  ba_addRange(&value->byteArr, se->execWeekdays.data, bit_getByteCount(se->execWeekdays.size));
                 *
                 *  //Add exec spec days.
                 *  ba_setUInt8(&value->byteArr, DLMS_DATA_TYPE_BIT_STRING);
                 *  setObjectCount(se->execSpecDays.size, &value->byteArr);
                 *  ba_addRange(&value->byteArr, se->execSpecDays.data, bit_getByteCount(se->execSpecDays.size));
                 *
                 *  //Add begin date.
                 *  ret = var_setDateTime(&tmp, &se->beginDate);
                 *  if (ret != 0)
                 *  {
                 *      var_clear(&tmp);
                 *      break;
                 *  }
                 *  ret = var_getBytes(&tmp, &value->byteArr);
                 *  var_clear(&tmp);
                 *  if (ret != 0)
                 *  {
                 *      break;
                 *  }
                 *  //Add end date.
                 *  ret = var_setDateTime(&tmp, &se->endDate);
                 *  if (ret != 0)
                 *  {
                 *      var_clear(&tmp);
                 *      break;
                 *  }
                 *  ret = var_getBytes(&tmp, &value->byteArr);
                 *  var_clear(&tmp);
                 *  if (ret != 0)
                 *  {
                 *      break;
                 *  }
                 * }
                 * */
                return(data.Array());
            }
            e.Error = ErrorCode.ReadWriteDenied;
            return(null);
        }
示例#21
0
        ///<summary>
        ///Server generates AARE message.
        ///</summary>
        internal static void GenerateAARE(GXDLMSSettings settings, GXByteBuffer data,
                                          AssociationResult result, SourceDiagnostic diagnostic, GXICipher cipher,
                                          GXByteBuffer encryptedData)
        {
            int offset = data.Size;
            // Set AARE tag and length
            data.SetUInt8(((byte)BerType.Application | (byte)BerType.Constructed | (byte)PduType.ApplicationContextName)); //0x61
                                                                                                                           // Length is updated later.
            data.SetUInt8(0);
            GenerateApplicationContextName(settings, data, cipher);
            //Result
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)BerType.Integer);//0xA2
            data.SetUInt8(3); //len
            data.SetUInt8(BerType.Integer); //Tag
                                            //Choice for result (INTEGER, universal)
            data.SetUInt8(1); //Len
            data.SetUInt8((byte)result); //ResultValue
                                         //SourceDiagnostic
            data.SetUInt8(0xA3);
            data.SetUInt8(5); //len
            data.SetUInt8(0xA1); //Tag
            data.SetUInt8(3); //len
            data.SetUInt8(2); //Tag
                              //Choice for result (INTEGER, universal)
            data.SetUInt8(1); //Len
            data.SetUInt8((byte)diagnostic); //diagnostic

            //SystemTitle
            if (cipher != null && (cipher.IsCiphered() || settings.Authentication == Authentication.HighGMAC))
            {
                data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.CalledApInvocationId);
                data.SetUInt8((byte)(2 + cipher.SystemTitle.Length));
                data.SetUInt8((byte)BerType.OctetString);
                data.SetUInt8((byte)cipher.SystemTitle.Length);
                data.Set(cipher.SystemTitle);
            }

            if (result != AssociationResult.PermanentRejected && diagnostic == SourceDiagnostic.AuthenticationRequired)
            {
                //Add server ACSE-requirenents field component.
                data.SetUInt8(0x88);
                data.SetUInt8(0x02);  //Len.
                data.SetUInt16(0x0780);
                //Add tag.
                data.SetUInt8(0x89);
                data.SetUInt8(0x07);//Len
                data.SetUInt8(0x60);
                data.SetUInt8(0x85);
                data.SetUInt8(0x74);
                data.SetUInt8(0x05);
                data.SetUInt8(0x08);
                data.SetUInt8(0x02);
                data.SetUInt8((byte)settings.Authentication);
                //Add tag.
                data.SetUInt8(0xAA);
                data.SetUInt8((byte)(2 + settings.StoCChallenge.Length));//Len
                data.SetUInt8((byte)BerType.Context);
                data.SetUInt8((byte)settings.StoCChallenge.Length);
                data.Set(settings.StoCChallenge);
            }
            byte[] tmp;
            //Add User Information
            //Tag 0xBE
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
            if (encryptedData != null && encryptedData.Size != 0)
            {
                GXByteBuffer tmp2 = new GXByteBuffer((UInt16)(2 + encryptedData.Size));
                tmp2.SetUInt8((byte)Command.GloInitiateResponse);
                GXCommon.SetObjectCount(encryptedData.Size, tmp2);
                tmp2.Set(encryptedData);
                tmp = tmp2.Array();
            }
            else
            {
                tmp = GetUserInformation(settings, cipher);
            }
            data.SetUInt8((byte)(2 + tmp.Length));
            //Coding the choice for user-information (Octet STRING, universal)
            data.SetUInt8(BerType.OctetString);
            //Length
            data.SetUInt8((byte)tmp.Length);
            data.Set(tmp);
            data.SetUInt8((UInt16)(offset + 1), (byte)(data.Size - offset - 2));
        }
示例#22
0
        private void Init(byte[] data)
        {
            Attributes = new List <KeyValuePair <PkcsObjectIdentifier, object[]> >();
            GXAsn1Sequence seq = (GXAsn1Sequence)GXAsn1Converter.FromByteArray(data);

            if (seq.Count < 3)
            {
                throw new System.ArgumentException("Wrong number of elements in sequence.");
            }
            /////////////////////////////
            // CertificationRequestInfo ::= SEQUENCE {
            // version INTEGER { v1(0) } (v1,...),
            // subject Name,
            // subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
            // attributes [0] Attributes{{ CRIAttributes }}
            // }

            GXAsn1Sequence reqInfo = (GXAsn1Sequence)seq[0];

            Version = (CertificateVersion)(sbyte)reqInfo[0];
            Subject = GXAsn1Converter.GetSubject((GXAsn1Sequence)reqInfo[1]);
            // subject Public key info.
            GXAsn1Sequence subjectPKInfo = (GXAsn1Sequence)reqInfo[2];

            if (reqInfo.Count > 3)
            {
                //PkcsObjectIdentifier
                foreach (GXAsn1Sequence it in (GXAsn1Context)reqInfo[3])
                {
                    List <object> values = new List <object>();
                    foreach (object v in (List <object>)((KeyValuePair <object, object>)it[1]).Key)
                    {
                        values.Add(v);
                    }
                    Attributes.Add(new KeyValuePair <PkcsObjectIdentifier, object[]>(PkcsObjectIdentifierConverter.FromString(it[0].ToString()), values.ToArray()));
                }
            }
            GXAsn1Sequence tmp = (GXAsn1Sequence)subjectPKInfo[0];

            Algorithm = X9ObjectIdentifierConverter.FromString(tmp[0].ToString());
            if (Algorithm != X9ObjectIdentifier.IdECPublicKey)
            {
                object algorithm = Algorithm;
                if (Algorithm == X9ObjectIdentifier.None)
                {
                    algorithm = PkcsObjectIdentifierConverter.FromString(tmp[0].ToString());
                    if ((PkcsObjectIdentifier)algorithm == PkcsObjectIdentifier.None)
                    {
                        algorithm = tmp[0].ToString();
                    }
                }
                throw new Exception("Invalid PKCS #10 certificate algorithm. " + algorithm);
            }
            PublicKey = GXPublicKey.FromRawBytes(((GXAsn1BitString)subjectPKInfo[1]).Value);
            GXEcdsa.Validate(PublicKey);
            /////////////////////////////
            // signatureAlgorithm
            GXAsn1Sequence sign = (GXAsn1Sequence)seq[1];

            SignatureAlgorithm = HashAlgorithmConverter.FromString(sign[0].ToString());
            if (SignatureAlgorithm != HashAlgorithm.Sha256WithEcdsa && SignatureAlgorithm != HashAlgorithm.Sha384WithEcdsa)
            {
                throw new GXDLMSCertificateException("Invalid signature algorithm. " + sign[0].ToString());
            }
            if (sign.Count != 1)
            {
                SignatureParameters = sign[1];
            }
            /////////////////////////////
            // signature
            Signature = ((GXAsn1BitString)seq[2]).Value;
            GXEcdsa        e    = new GXEcdsa(PublicKey);
            GXAsn1Sequence tmp2 = (GXAsn1Sequence)GXAsn1Converter.FromByteArray(Signature);
            GXByteBuffer   bb   = new GXByteBuffer();
            int            size = SignatureAlgorithm == HashAlgorithm.Sha256WithEcdsa ? 32 : 48;

            //Some implementations might add extra byte. It must removed.
            bb.Set(((GXAsn1Integer)tmp2[0]).Value, ((GXAsn1Integer)tmp2[0]).Value.Length == size ? 0 : 1, size);
            bb.Set(((GXAsn1Integer)tmp2[1]).Value, ((GXAsn1Integer)tmp2[1]).Value.Length == size ? 0 : 1, size);
            if (!e.Verify(bb.Array(), GXAsn1Converter.ToByteArray(reqInfo)))
            {
                throw new ArgumentException("Invalid Signature.");
            }
        }
示例#23
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         if (APN != null)
         {
             return(ASCIIEncoding.ASCII.GetBytes(APN));
         }
         return(null);
     }
     if (e.Index == 3)
     {
         return(PINCode);
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8((byte)2);
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8((byte)5);
         if (DefaultQualityOfService != null)
         {
             GXCommon.SetData(settings, data, DataType.UInt8, DefaultQualityOfService.Precedence);
             GXCommon.SetData(settings, data, DataType.UInt8, DefaultQualityOfService.Delay);
             GXCommon.SetData(settings, data, DataType.UInt8, DefaultQualityOfService.Reliability);
             GXCommon.SetData(settings, data, DataType.UInt8, DefaultQualityOfService.PeakThroughput);
             GXCommon.SetData(settings, data, DataType.UInt8, DefaultQualityOfService.MeanThroughput);
         }
         else
         {
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
         }
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8((byte)5);
         if (RequestedQualityOfService != null)
         {
             GXCommon.SetData(settings, data, DataType.UInt8, RequestedQualityOfService.Precedence);
             GXCommon.SetData(settings, data, DataType.UInt8, RequestedQualityOfService.Delay);
             GXCommon.SetData(settings, data, DataType.UInt8, RequestedQualityOfService.Reliability);
             GXCommon.SetData(settings, data, DataType.UInt8, RequestedQualityOfService.PeakThroughput);
             GXCommon.SetData(settings, data, DataType.UInt8, RequestedQualityOfService.MeanThroughput);
         }
         else
         {
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
             GXCommon.SetData(settings, data, DataType.UInt8, 0);
         }
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#24
0
 ///<summary>
 /// Chipher text.
 ///</summary>
 ///<param name="settings">
 ///DLMS settings. 
 ///</param>
 ///<param name="cipher">
 ///Cipher. 
 ///</param>
 ///<param name="ic">
 ///Invocation counter.
 ///</param>
 ///<param name="data">
 ///Text to chipher. 
 ///</param>
 ///<param name="secret">
 ///Secret. 
 ///</param>
 ///<returns>
 ///Chiphered text.
 ///</returns>
 public static byte[] Secure(GXDLMSSettings settings, GXICipher cipher, UInt32 ic, byte[] data, byte[] secret)
 {
     byte[] tmp;
     if (settings.Authentication == Authentication.High)
     {
         int len = secret.Length;
         if (len % 16 != 0)
         {
             len += 16 - (secret.Length % 16);
         }
         byte[] p = new byte[len];
         byte[] s = new byte[16];
         byte[] x = new byte[16];
         int i;
         data.CopyTo(p, 0);
         secret.CopyTo(s, 0);
         for (i = 0; i < p.Length; i += 16)
         {
             Buffer.BlockCopy(p, i, x, 0, 16);
             GXAes128.Encrypt(x, s);
             Buffer.BlockCopy(x, 0, p, i, 16);
         }
         Buffer.BlockCopy(p, 0, x, 0, 16);
         return x;
     }
     // Get server Challenge.
     GXByteBuffer challenge = new GXByteBuffer();
     // Get shared secret
     if (settings.Authentication == Authentication.HighGMAC)
     {
         challenge.Set(data);              
     }
     else
     {
         challenge.Set(data);
         challenge.Set(secret);
     }
     tmp = challenge.Array();
     if (settings.Authentication == Authentication.HighMD5)
     {
         using (MD5 md5Hash = MD5.Create())
         {
             tmp = md5Hash.ComputeHash(tmp);
             return tmp;
         }
     }
     else if (settings.Authentication == Authentication.HighSHA1)
     {
         using (SHA1 sha = new SHA1CryptoServiceProvider())
         {
             tmp = sha.ComputeHash(tmp);
             return tmp;
         }
     }
     else if (settings.Authentication == Authentication.HighGMAC)
     {
         //SC is always Security.Authentication.
         AesGcmParameter p = new AesGcmParameter(0, Security.Authentication, ic,
             secret, cipher.BlockCipherKey, cipher.AuthenticationKey);
         p.Type = CountType.Tag;
         challenge.Clear();
         challenge.SetUInt8((byte)Security.Authentication);
         challenge.SetUInt32(p.FrameCounter);
         challenge.Set(GXDLMSChippering.EncryptAesGcm(p, tmp));
         tmp = challenge.Array();
         return tmp;
     }
     return data;
 }
示例#25
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(ImageBlockSize);
     }
     if (e.Index == 3)
     {
         return(ImageTransferredBlocksStatus);
     }
     if (e.Index == 4)
     {
         return(ImageFirstNotTransferredBlockNumber);
     }
     if (e.Index == 5)
     {
         return(ImageTransferEnabled);
     }
     if (e.Index == 6)
     {
         return(ImageTransferStatus);
     }
     if (e.Index == 7)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (ImageActivateInfo == null)
         {
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             GXCommon.SetObjectCount(ImageActivateInfo.Length, data);
             foreach (GXDLMSImageActivateInfo it in ImageActivateInfo)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(settings, data, DataType.UInt32, it.Size);
                 if (it.Identification == null)
                 {
                     GXCommon.SetData(settings, data, DataType.OctetString, null);
                 }
                 else
                 {
                     GXCommon.SetData(settings, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Identification));
                 }
                 if (it.Signature == null || it.Signature.Length == 0)
                 {
                     GXCommon.SetData(settings, data, DataType.OctetString, null);
                 }
                 else
                 {
                     GXCommon.SetData(settings, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Signature));
                 }
             }
         }
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#26
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            GXByteBuffer bb;

            switch (e.Index)
            {
            case 1:
                return(GXCommon.LogicalNameToBytes(LogicalName));

            case 2:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Structure);
                bb.SetUInt8(2);
                bb.SetUInt8(DataType.Enum);
                bb.SetUInt8(AccountStatus);
                bb.SetUInt8(DataType.Enum);
                bb.SetUInt8(PaymentMode);
                return(bb.Array());

            case 3:
                return(CurrentCreditInUse);

            case 4:
                return((byte)CurrentCreditStatus);

            case 5:
                return(AvailableCredit);

            case 6:
                return(AmountToClear);

            case 7:
                return(ClearanceThreshold);

            case 8:
                return(AggregatedDebt);

            case 9:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Array);
                if (CreditReferences == null)
                {
                    bb.SetUInt8(0);
                }
                else
                {
                    GXCommon.SetObjectCount(CreditReferences.Count, bb);
                    foreach (string it in CreditReferences)
                    {
                        bb.SetUInt8(DataType.OctetString);
                        bb.SetUInt8(6);
                        bb.Set(GXCommon.LogicalNameToBytes(it));
                    }
                }
                return(bb.Array());

            case 10:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Array);
                if (ChargeReferences == null)
                {
                    bb.SetUInt8(0);
                }
                else
                {
                    GXCommon.SetObjectCount(ChargeReferences.Count, bb);
                    foreach (string it in ChargeReferences)
                    {
                        bb.SetUInt8(DataType.OctetString);
                        bb.SetUInt8(6);
                        bb.Set(GXCommon.LogicalNameToBytes(it));
                    }
                }
                return(bb.Array());

            case 11:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Array);
                if (CreditChargeConfigurations == null)
                {
                    bb.SetUInt8(0);
                }
                else
                {
                    GXCommon.SetObjectCount(CreditChargeConfigurations.Count, bb);
                    foreach (GXCreditChargeConfiguration it in CreditChargeConfigurations)
                    {
                        bb.SetUInt8(DataType.Structure);
                        bb.SetUInt8(3);
                        bb.SetUInt8(DataType.OctetString);
                        bb.SetUInt8(6);
                        bb.Set(GXCommon.LogicalNameToBytes(it.CreditReference));
                        bb.SetUInt8(DataType.OctetString);
                        bb.SetUInt8(6);
                        bb.Set(GXCommon.LogicalNameToBytes(it.ChargeReference));
                        bb.SetUInt8(DataType.BitString);
                        bb.SetUInt8(3);
                        bb.SetUInt8(it.CollectionConfiguration);
                    }
                }
                return(bb.Array());

            case 12:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Array);
                if (TokenGatewayConfigurations == null)
                {
                    bb.SetUInt8(0);
                }
                else
                {
                    GXCommon.SetObjectCount(TokenGatewayConfigurations.Count, bb);
                    foreach (GXTokenGatewayConfiguration it in TokenGatewayConfigurations)
                    {
                        bb.SetUInt8(DataType.Structure);
                        bb.SetUInt8(2);
                        bb.SetUInt8(DataType.OctetString);
                        bb.SetUInt8(6);
                        bb.Set(GXCommon.LogicalNameToBytes(it.CreditReference));
                        bb.SetUInt8(DataType.UInt8);
                        bb.SetUInt8(it.TokenProportion);
                    }
                }
                return(bb.Array());

            case 13:
                return(AccountActivationTime);

            case 14:
                return(AccountClosureTime);

            case 15:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Structure);
                bb.SetUInt8(3);
                GXCommon.SetData(settings, bb, DataType.StringUTF8, Currency.Name);
                GXCommon.SetData(settings, bb, DataType.Int8, Currency.Scale);
                GXCommon.SetData(settings, bb, DataType.Enum, Currency.Unit);
                return(bb.Array());

            case 16:
                return(LowCreditThreshold);

            case 17:
                return(NextCreditAvailableThreshold);

            case 18:
                return(MaxProvision);

            case 19:
                return(MaxProvisionPeriod);

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
            return(null);
        }
示例#27
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(this.LogicalName);
     }
     if (e.Index == 2)
     {
         return((byte)Mode);
     }
     if (e.Index == 3)
     {
         return(Repetitions);
     }
     if (e.Index == 4)
     {
         return(RepetitionDelay);
     }
     if (e.Index == 5)
     {
         int          cnt  = CallingWindow.Count;
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         GXCommon.SetObjectCount(cnt, data);
         if (cnt != 0)
         {
             foreach (var it in CallingWindow)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)2);                                           //Count
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Key);   //start_time
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Value); //end_time
             }
         }
         return(data.Array());
     }
     if (e.Index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (Destinations == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = Destinations.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (string it in Destinations)
             {
                 GXCommon.SetData(settings, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it)); //destination
             }
         }
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#28
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(this.LogicalName);
     }
     if (e.Index == 2)
     {
         return(this.DataLinkLayerReference);
     }
     if (e.Index == 3)
     {
         //If IP address is not given.
         if (IPAddress == null || IPAddress.Trim().Length == 0)
         {
             return(0);
         }
         return(BitConverter.ToUInt32(System.Net.IPAddress.Parse(IPAddress).GetAddressBytes(), 0));
     }
     if (e.Index == 4)
     {
         return(this.MulticastIPAddress);
     }
     if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (IPOptions == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(IPOptions.Length, data);
             foreach (GXDLMSIp4SetupIpOption it in IPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Type);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Length);
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Data);
             }
         }
         return(data.Array());
     }
     if (e.Index == 6)
     {
         return(this.SubnetMask);
     }
     if (e.Index == 7)
     {
         return(this.GatewayIPAddress);
     }
     if (e.Index == 8)
     {
         return(this.UseDHCP);
     }
     if (e.Index == 9)
     {
         return(this.PrimaryDNSAddress);
     }
     if (e.Index == 10)
     {
         return(this.SecondaryDNSAddress);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#29
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            if (e.Index == 1)
            {
                return(GXCommon.LogicalNameToBytes(LogicalName));
            }
            else if (e.Index == 2)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(3);
                if (MonitoredValue == null)
                {
                    GXCommon.SetData(settings, data, DataType.UInt16, 0);
                    GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(null));
                    GXCommon.SetData(settings, data, DataType.Int8, 0);
                }
                else
                {
                    GXCommon.SetData(settings, data, DataType.UInt16, MonitoredValue.ObjectType);
                    GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(MonitoredValue.LogicalName));
                    GXCommon.SetData(settings, data, DataType.Int8, MonitoredAttributeIndex);
                }
                return(data.Array());
            }
            else if (e.Index == 3)
            {
                return(ThresholdActive);
            }
            else if (e.Index == 4)
            {
                return(ThresholdNormal);
            }
            else if (e.Index == 5)
            {
                return(ThresholdEmergency);
            }
            else if (e.Index == 6)
            {
                return(MinOverThresholdDuration);
            }
            else if (e.Index == 7)
            {
                return(MinUnderThresholdDuration);
            }
            else if (e.Index == 8)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(3);
                GXCommon.SetData(settings, data, DataType.UInt16, EmergencyProfile.ID);
                GXCommon.SetData(settings, data, DataType.OctetString, EmergencyProfile.ActivationTime);
                GXCommon.SetData(settings, data, DataType.UInt32, EmergencyProfile.Duration);
                return(data.Array());
            }
            else if (e.Index == 9)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (EmergencyProfileGroupIDs == null)
                {
                    data.SetUInt8(0);
                }
                else
                {
                    data.SetUInt8((byte)EmergencyProfileGroupIDs.Length);
                    foreach (object it in EmergencyProfileGroupIDs)
                    {
                        GXCommon.SetData(settings, data, DataType.UInt16, it);
                    }
                }

                return(data.Array());
            }
            else if (e.Index == 10)
            {
                return(EmergencyProfileActive);
            }
            else if (e.Index == 11)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(2);
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(2);
                GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(ActionOverThreshold.LogicalName));
                GXCommon.SetData(settings, data, DataType.UInt16, ActionOverThreshold.ScriptSelector);
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(2);
                GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(ActionUnderThreshold.LogicalName));
                GXCommon.SetData(settings, data, DataType.UInt16, ActionUnderThreshold.ScriptSelector);
                return(data.Array());
            }
            e.Error = ErrorCode.ReadWriteDenied;
            return(null);
        }
示例#30
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            object ret;

            switch (e.Index)
            {
            case 1:
                ret = GXCommon.LogicalNameToBytes(LogicalName);
                break;

            case 2:
                ret = (byte)Mode;
                break;

            case 3:
                ret = (byte)Speed;
                break;

            case 4:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (PrimaryAddresses == null)
                {
                    data.SetUInt8(0);
                }
                else
                {
                    data.SetUInt8((byte)PrimaryAddresses.Length);
                    foreach (byte it in PrimaryAddresses)
                    {
                        data.SetUInt8((byte)DataType.UInt8);
                        data.SetUInt8(it);
                    }
                }
                ret = data.Array();
                break;
            }

            case 5:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (Tabis == null)
                {
                    data.SetUInt8(0);
                }
                else
                {
                    data.SetUInt8((byte)Tabis.Length);
                    foreach (sbyte it in Tabis)
                    {
                        data.SetUInt8((byte)DataType.Int8);
                        data.SetUInt8((byte)it);
                    }
                }
                ret = data.Array();
                break;
            }

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                ret     = null;
                break;
            }
            return(ret);
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(GetObjects(settings, e).Array());
     }
     if (e.Index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         data.SetUInt8(2);
         data.SetUInt8((byte)DataType.UInt8);
         data.SetUInt8(ClientSAP);
         data.SetUInt8((byte)DataType.UInt16);
         data.SetUInt16(ServerSAP);
         return(data.Array());
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.JointIsoCtt);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.Country);
         GXCommon.SetData(settings, data, DataType.UInt16, ApplicationContextName.CountryName);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.IdentifiedOrganization);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.DlmsUA);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.ApplicationContext);
         GXCommon.SetData(settings, data, DataType.UInt8, ApplicationContextName.ContextId);
         return(data.Array());
     }
     if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(6);
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt32((UInt32)XDLMSContextInfo.Conformance);
         GXCommon.SetData(settings, data, DataType.BitString, bb.SubArray(1, 3));
         GXCommon.SetData(settings, data, DataType.UInt16, XDLMSContextInfo.MaxReceivePduSize);
         GXCommon.SetData(settings, data, DataType.UInt16, XDLMSContextInfo.MaxSendPduSize);
         GXCommon.SetData(settings, data, DataType.UInt8, XDLMSContextInfo.DlmsVersionNumber);
         GXCommon.SetData(settings, data, DataType.Int8, XDLMSContextInfo.QualityOfService);
         GXCommon.SetData(settings, data, DataType.OctetString, XDLMSContextInfo.CypheringInfo);
         return(data.Array());
     }
     if (e.Index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add count
         data.SetUInt8(0x7);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.JointIsoCtt);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.Country);
         GXCommon.SetData(settings, data, DataType.UInt16, AuthenticationMechanismName.CountryName);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.IdentifiedOrganization);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.DlmsUA);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.AuthenticationMechanismName);
         GXCommon.SetData(settings, data, DataType.UInt8, AuthenticationMechanismName.MechanismId);
         return(data.Array());
     }
     if (e.Index == 7)
     {
         return(Secret);
     }
     if (e.Index == 8)
     {
         return(AssociationStatus);
     }
     if (e.Index == 9)
     {
         return(GXCommon.LogicalNameToBytes(SecuritySetupReference));
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            GXByteBuffer buff = new GXByteBuffer();
            object       ret;

            switch (e.Index)
            {
            case 1:
                ret = GXCommon.LogicalNameToBytes(LogicalName);
                break;

            case 2:
                ret = GetPushObjectList(settings);
                break;

            case 3:
                buff.SetUInt8(DataType.Structure);
                buff.SetUInt8(3);
                GXCommon.SetData(settings, buff, DataType.Enum, Service);
                if (Destination != null)
                {
                    GXCommon.SetData(settings, buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(Destination));
                }
                else
                {
                    GXCommon.SetData(settings, buff, DataType.OctetString, null);
                }
                GXCommon.SetData(settings, buff, DataType.Enum, Message);
                ret = buff.Array();
                break;

            case 4:
                buff.SetUInt8(DataType.Array);
                GXCommon.SetObjectCount(CommunicationWindow.Count, buff);
                foreach (KeyValuePair <GXDateTime, GXDateTime> it in CommunicationWindow)
                {
                    buff.SetUInt8(DataType.Structure);
                    buff.SetUInt8(2);
                    GXCommon.SetData(settings, buff, DataType.OctetString, it.Key);
                    GXCommon.SetData(settings, buff, DataType.OctetString, it.Value);
                }
                ret = buff.Array();
                break;

            case 5:
                ret = RandomisationStartInterval;
                break;

            case 6:
                ret = NumberOfRetries;
                break;

            case 7:
                ret = RepetitionDelay;
                break;

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                ret     = null;
                break;
            }
            return(ret);
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     else if (e.Index == 2)
     {
         return(InitiatorElectricalPhase);
     }
     else if (e.Index == 3)
     {
         return(DeltaElectricalPhase);
     }
     else if (e.Index == 4)
     {
         return(MaxReceivingGain);
     }
     else if (e.Index == 5)
     {
         return(MaxTransmittingGain);
     }
     else if (e.Index == 6)
     {
         return(SearchInitiatorThreshold);
     }
     else if (e.Index == 7)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8(DataType.Structure);
         bb.SetUInt8(2);
         GXCommon.SetData(settings, bb, DataType.UInt32, MarkFrequency);
         GXCommon.SetData(settings, bb, DataType.UInt32, SpaceFrequency);
         return(bb.Array());
     }
     else if (e.Index == 8)
     {
         return(MacAddress);
     }
     else if (e.Index == 9)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8(DataType.Array);
         if (MacGroupAddresses == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(MacGroupAddresses.Length, bb);
             foreach (UInt16 it in MacGroupAddresses)
             {
                 GXCommon.SetData(settings, bb, DataType.UInt16, it);
             }
         }
         return(bb.Array());
     }
     else if (e.Index == 10)
     {
         return(Repeater);
     }
     else if (e.Index == 11)
     {
         return(RepeaterStatus);
     }
     else if (e.Index == 12)
     {
         return(MinDeltaCredit);
     }
     else if (e.Index == 13)
     {
         return(InitiatorMacAddress);
     }
     else if (e.Index == 14)
     {
         return(SynchronizationLocked);
     }
     else if (e.Index == 15)
     {
         return(TransmissionSpeed);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#34
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(ShortAddress);
     }
     if (e.Index == 3)
     {
         return(RcCoord);
     }
     if (e.Index == 4)
     {
         return(PANId);
     }
     if (e.Index == 5)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (KeyTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(KeyTable.Count, bb);
             foreach (GXKeyValuePair <byte, byte[]> it in KeyTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(2);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Key);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Value);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 6)
     {
         return(FrameCounter);
     }
     if (e.Index == 7)
     {
         return(ToneMask);
     }
     if (e.Index == 8)
     {
         return(TmrTtl);
     }
     if (e.Index == 9)
     {
         return(MaxFrameRetries);
     }
     if (e.Index == 10)
     {
         return(NeighbourTableEntryTtl);
     }
     if (e.Index == 11)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (NeighbourTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(NeighbourTable.Length, bb);
             foreach (GXDLMSNeighbourTable it in NeighbourTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(11);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.ShortAddress);
                 GXCommon.SetData(settings, bb, DataType.Boolean, it.Enabled);
                 GXCommon.SetData(settings, bb, DataType.BitString, it.ToneMap);
                 GXCommon.SetData(settings, bb, DataType.Enum, it.Modulation);
                 GXCommon.SetData(settings, bb, DataType.Int8, it.TxGain);
                 GXCommon.SetData(settings, bb, DataType.Enum, it.TxRes);
                 GXCommon.SetData(settings, bb, DataType.BitString, it.TxCoeff);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Lqi);
                 GXCommon.SetData(settings, bb, DataType.Int8, it.PhaseDifferential);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.TMRValidTime);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.NeighbourValidTime);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 12)
     {
         return(HighPriorityWindowSize);
     }
     if (e.Index == 13)
     {
         return(CscmFairnessLimit);
     }
     if (e.Index == 14)
     {
         return(BeaconRandomizationWindowLength);
     }
     if (e.Index == 15)
     {
         return(A);
     }
     if (e.Index == 16)
     {
         return(K);
     }
     if (e.Index == 17)
     {
         return(MinCwAttempts);
     }
     if (e.Index == 18)
     {
         return(CenelecLegacyMode);
     }
     if (e.Index == 19)
     {
         return(FccLegacyMode);
     }
     if (e.Index == 20)
     {
         return(MaxBe);
     }
     if (e.Index == 21)
     {
         return(MaxCsmaBackoffs);
     }
     if (e.Index == 22)
     {
         return(MinBe);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#35
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(GXCommon.LogicalNameToBytes(PHYReference));
     }
     if (e.Index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (LCPOptions == null)
         {
             data.SetUInt8(0);
         }
         else
         {
             data.SetUInt8((byte)LCPOptions.Length);
             foreach (GXDLMSPppSetupLcpOption it in LCPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)3);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Type);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Length);
                 GXCommon.SetData(settings, data, GXDLMSConverter.GetDLMSDataType(it.Data), it.Data);
             }
         }
         return(data.Array());
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (IPCPOptions == null)
         {
             data.SetUInt8(0);
         }
         else
         {
             data.SetUInt8((byte)IPCPOptions.Length);
             foreach (GXDLMSPppSetupIPCPOption it in IPCPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)3);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Type);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Length);
                 GXCommon.SetData(settings, data, GXDLMSConverter.GetDLMSDataType(it.Data), it.Data);
             }
         }
         return(data.Array());
     }
     else if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         data.SetUInt8(2);
         GXCommon.SetData(settings, data, DataType.OctetString, UserName);
         GXCommon.SetData(settings, data, DataType.OctetString, Password);
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#36
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            GXByteBuffer bb;

            switch (e.Index)
            {
            case 1:
                return(GXCommon.LogicalNameToBytes(LogicalName));

            case 2:
                if (string.IsNullOrEmpty(Operator))
                {
                    return(null);
                }
                return(ASCIIEncoding.ASCII.GetBytes(Operator));

            case 3:
                return(Status);

            case 4:
                return(CircuitSwitchStatus);

            case 5:
                return(PacketSwitchStatus);

            case 6:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Structure);
                if (Version == 0)
                {
                    bb.SetUInt8(4);
                    GXCommon.SetData(settings, bb, DataType.UInt16, CellInfo.CellId);
                }
                else
                {
                    bb.SetUInt8(7);
                    GXCommon.SetData(settings, bb, DataType.UInt32, CellInfo.CellId);
                }
                GXCommon.SetData(settings, bb, DataType.UInt16, CellInfo.LocationId);
                GXCommon.SetData(settings, bb, DataType.UInt8, CellInfo.SignalQuality);
                GXCommon.SetData(settings, bb, DataType.UInt8, CellInfo.Ber);
                if (Version > 0)
                {
                    GXCommon.SetData(settings, bb, DataType.UInt16, CellInfo.MobileCountryCode);
                    GXCommon.SetData(settings, bb, DataType.UInt16, CellInfo.MobileNetworkCode);
                    GXCommon.SetData(settings, bb, DataType.UInt32, CellInfo.ChannelNumber);
                }
                return(bb.Array());

            case 7:
                bb = new GXByteBuffer();
                bb.SetUInt8(DataType.Array);
                if (AdjacentCells == null)
                {
                    bb.SetUInt8(0);
                }
                else
                {
                    bb.SetUInt8((byte)AdjacentCells.Count);
                }
                foreach (AdjacentCell it in AdjacentCells)
                {
                    bb.SetUInt8(DataType.Structure);
                    bb.SetUInt8(2);
                    GXCommon.SetData(settings, bb, Version == 0 ? DataType.UInt16 : DataType.UInt32, it.CellId);
                    GXCommon.SetData(settings, bb, DataType.UInt8, it.SignalQuality);
                }
                return(bb.Array());

            case 8:
                return(CaptureTime);
            }
            e.Error = ErrorCode.ReadWriteDenied;
            return(null);
        }
        /// <summary>
        /// Returns buffer data.
        /// </summary>
        /// <param name="settings">DLMS settings.</param>
        /// <param name="e"></param>
        /// <param name="table"></param>
        /// <param name="columns">Columns to get. NULL if not used.</param>
        /// <returns></returns>
        byte[] GetData(GXDLMSSettings settings, ValueEventArgs e, List <object[]> table,
                       List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > columns)
        {
            int pos = 0;
            List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > cols = columns;

            if (columns == null)
            {
            }
            GXByteBuffer data = new GXByteBuffer();

            if (settings.Index == 0)
            {
                data.SetUInt8((byte)DataType.Array);
                if (e.RowEndIndex != 0)
                {
                    GXCommon.SetObjectCount((int)(e.RowEndIndex - e.RowBeginIndex), data);
                }
                else
                {
                    GXCommon.SetObjectCount(table.Count, data);
                }
            }
            DataType[] types = new DataType[CaptureObjects.Count];
            foreach (GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> it in CaptureObjects)
            {
                types[pos] = it.Key.GetDataType(it.Value.AttributeIndex);
                ++pos;
            }

            foreach (object[] items in table)
            {
                data.SetUInt8((byte)DataType.Structure);
                if (columns == null || columns.Count == 0)
                {
                    GXCommon.SetObjectCount(items.Length, data);
                }
                else
                {
                    GXCommon.SetObjectCount(columns.Count, data);
                }
                pos = 0;
                DataType tp;
                foreach (object value in items)
                {
                    if (columns == null || columns.Contains(CaptureObjects[pos]))
                    {
                        tp = types[pos];
                        if (tp == DataType.None)
                        {
                            tp         = Gurux.DLMS.Internal.GXCommon.GetValueType(value);
                            types[pos] = tp;
                        }
                        if (value == null)
                        {
                            tp = DataType.None;
                        }
                        GXCommon.SetData(settings, data, tp, value);
                    }
                    ++pos;
                }
                ++settings.Index;
            }
            if (e.RowEndIndex != 0)
            {
                e.RowBeginIndex += (UInt32)table.Count;
            }
            else
            {
                settings.Index = 0;
            }
            return(data.Array());
        }
示例#38
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(GXCommon.LogicalNameToBytes(DataLinkLayerReference));
     }
     if (e.Index == 3)
     {
         return(FromAddressString(IPAddress));
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (MulticastIPAddress == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(MulticastIPAddress.Length, data);
             foreach (string it in MulticastIPAddress)
             {
                 GXCommon.SetData(settings, data, DataType.UInt32, FromAddressString(it));
             }
         }
         return(data.Array());
     }
     if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (IPOptions == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(IPOptions.Length, data);
             foreach (GXDLMSIp4SetupIpOption it in IPOptions)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Type);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.Length);
                 GXCommon.SetData(settings, data, DataType.OctetString, it.Data);
             }
         }
         return(data.Array());
     }
     if (e.Index == 6)
     {
         //If subnet mask is not given.
         return(FromAddressString(SubnetMask));
     }
     if (e.Index == 7)
     {
         return(FromAddressString(GatewayIPAddress));
     }
     if (e.Index == 8)
     {
         return(this.UseDHCP);
     }
     if (e.Index == 9)
     {
         return(FromAddressString(PrimaryDNSAddress));
     }
     if (e.Index == 10)
     {
         return(FromAddressString(SecondaryDNSAddress));
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
示例#39
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(this.DataLinkLayerReference);
     }
     if (e.Index == 3)
     {
         return(AddressConfigMode);
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (UnicastIPAddress == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(UnicastIPAddress.Length, data);
             foreach (IPAddress it in UnicastIPAddress)
             {
                 GXCommon.SetData(settings, data, DataType.OctetString, it.GetAddressBytes());
             }
         }
         return(data.Array());
     }
     if (e.Index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (MulticastIPAddress == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(MulticastIPAddress.Length, data);
             foreach (IPAddress it in MulticastIPAddress)
             {
                 GXCommon.SetData(settings, data, DataType.OctetString, it.GetAddressBytes());
             }
         }
         return(data.Array());
     }
     if (e.Index == 6)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (GatewayIPAddress == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(GatewayIPAddress.Length, data);
             foreach (IPAddress it in GatewayIPAddress)
             {
                 GXCommon.SetData(settings, data, DataType.OctetString, it.GetAddressBytes());
             }
         }
         return(data.Array());
     }
     if (e.Index == 7)
     {
         if (PrimaryDNSAddress == null)
         {
             return(null);
         }
         return(PrimaryDNSAddress.GetAddressBytes());
     }
     if (e.Index == 8)
     {
         if (SecondaryDNSAddress == null)
         {
             return(null);
         }
         return(SecondaryDNSAddress.GetAddressBytes());
     }
     if (e.Index == 9)
     {
         return(TrafficClass);
     }
     if (e.Index == 10)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (NeighborDiscoverySetup == null)
         {
             //Object count is zero.
             data.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(NeighborDiscoverySetup.Length, data);
             foreach (GXNeighborDiscoverySetup it in NeighborDiscoverySetup)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(settings, data, DataType.UInt8, it.MaxRetry);
                 GXCommon.SetData(settings, data, DataType.UInt16, it.RetryWaitTime);
                 GXCommon.SetData(settings, data, DataType.UInt32, it.SendPeriod);
             }
         }
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            switch (e.Index)
            {
            case 1:
                return(GXCommon.LogicalNameToBytes(LogicalName));

            case 2:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                data.SetUInt8(4);
                if (ChangedParameter == null)
                {
                    GXCommon.SetData(settings, data, DataType.UInt16, 0);
                    GXCommon.SetData(settings, data, DataType.OctetString, new byte[] { 0, 0, 0, 0, 0, 0 });
                    GXCommon.SetData(settings, data, DataType.Int8, 1);
                    GXCommon.SetData(settings, data, DataType.None, null);
                }
                else
                {
                    GXCommon.SetData(settings, data, DataType.UInt16, ChangedParameter.Target.ObjectType);
                    GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(ChangedParameter.Target.LogicalName));
                    GXCommon.SetData(settings, data, DataType.Int8, ChangedParameter.AttributeIndex);
                    GXCommon.SetData(settings, data, GXDLMSConverter.GetDLMSDataType(ChangedParameter.Value), ChangedParameter.Value);
                }
                return(data.Array());
            }

            case 3:
                return(CaptureTime);

            case 4:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (Parameters == null)
                {
                    data.SetUInt8(0);
                }
                else
                {
                    data.SetUInt8((byte)Parameters.Count);
                    foreach (GXDLMSTarget it in Parameters)
                    {
                        data.SetUInt8((byte)DataType.Structure);
                        data.SetUInt8((byte)3);
                        GXCommon.SetData(settings, data, DataType.UInt16, it.Target.ObjectType);
                        GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(it.Target.LogicalName));
                        GXCommon.SetData(settings, data, DataType.Int8, it.AttributeIndex);
                    }
                }
                return(data.Array());
            }

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
            return(null);
        }
示例#41
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(LogicalName));
     }
     if (e.Index == 2)
     {
         return(MaxHops);
     }
     if (e.Index == 3)
     {
         return(WeakLqiValue);
     }
     if (e.Index == 4)
     {
         return(SecurityLevel);
     }
     if (e.Index == 5)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (PrefixTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(PrefixTable.Length, bb);
             foreach (var it in PrefixTable)
             {
                 GXCommon.SetData(settings, bb, GXDLMSConverter.GetDLMSDataType(it), it);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 6)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (RoutingConfiguration == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(RoutingConfiguration.Count, bb);
             foreach (GXDLMSRoutingConfiguration it in RoutingConfiguration)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(14);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.NetTraversalTime);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.RoutingTableEntryTtl);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Kr);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Km);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Kc);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Kq);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Kh);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.Krt);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.RreqRetries);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.RreqRerrWait);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.BlacklistTableEntryTtl);
                 GXCommon.SetData(settings, bb, DataType.Boolean, it.UnicastRreqGenEnable);
                 GXCommon.SetData(settings, bb, DataType.Boolean, it.RlcEnabled);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.AddRevLinkCost);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 7)
     {
         return(BroadcastLogTableEntryTtl);
     }
     if (e.Index == 8)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (RoutingTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(RoutingTable.Count, bb);
             foreach (GXDLMSRoutingTable it in RoutingTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(6);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.DestinationAddress);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.NextHopAddress);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.RouteCost);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.HopCount);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.WeakLinkCount);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.ValidTime);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 9)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (ContextInformationTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(ContextInformationTable.Count, bb);
             foreach (GXDLMSContextInformationTable it in ContextInformationTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(5);
                 GXCommon.SetData(settings, bb, DataType.BitString, it.CID);
                 if (it.Context == null)
                 {
                     GXCommon.SetData(settings, bb, DataType.UInt8, 0);
                 }
                 else
                 {
                     GXCommon.SetData(settings, bb, DataType.UInt8, it.Context.Length);
                 }
                 GXCommon.SetData(settings, bb, DataType.OctetString, it.Context);
                 GXCommon.SetData(settings, bb, DataType.Boolean, it.Compression);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.ValidLifetime);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 10)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (BlacklistTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(BlacklistTable.Count, bb);
             foreach (var it in BlacklistTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(2);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.Key);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.Value);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 11)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (BroadcastLogTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(BroadcastLogTable.Count, bb);
             foreach (GXDLMSBroadcastLogTable it in BroadcastLogTable)
             {
                 bb.SetUInt8((byte)DataType.Structure);
                 bb.SetUInt8(3);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.SourceAddress);
                 GXCommon.SetData(settings, bb, DataType.UInt8, it.SequenceNumber);
                 GXCommon.SetData(settings, bb, DataType.UInt16, it.ValidTime);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 12)
     {
         GXByteBuffer bb = new GXByteBuffer();
         bb.SetUInt8((byte)DataType.Array);
         if (GroupTable == null)
         {
             bb.SetUInt8(0);
         }
         else
         {
             GXCommon.SetObjectCount(GroupTable.Length, bb);
             foreach (UInt16 it in GroupTable)
             {
                 GXCommon.SetData(settings, bb, DataType.UInt16, it);
             }
         }
         return(bb.Array());
     }
     if (e.Index == 13)
     {
         return(MaxJoinWaitTime);
     }
     if (e.Index == 14)
     {
         return(PathDiscoveryTime);
     }
     if (e.Index == 15)
     {
         return(ActiveKeyIndex);
     }
     if (e.Index == 16)
     {
         return(MetricType);
     }
     if (e.Index == 17)
     {
         return(CoordShortAddress);
     }
     if (e.Index == 18)
     {
         return(DisableDefaultRouting);
     }
     if (e.Index == 19)
     {
         return(DeviceType);
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return this.LogicalName;
     }
     if (e.Index == 2)
     {
         return this.Thresholds;
     }
     if (e.Index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((int)DataType.Structure);
         data.SetUInt8(3);
         GXCommon.SetData(settings, data, DataType.UInt16, MonitoredValue.ObjectType); //ClassID
         GXCommon.SetData(settings, data, DataType.OctetString, MonitoredValue.LogicalName); //Logical name.
         GXCommon.SetData(settings, data, DataType.Int8, MonitoredValue.AttributeIndex); //Attribute Index
         return data.Array();
     }
     if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((int)DataType.Structure);
         if (Actions == null)
         {
             data.SetUInt8(0);
         }
         else
         {
             data.SetUInt8((byte)Actions.Length);
             foreach (GXDLMSActionSet it in Actions)
             {
                 data.SetUInt8((int)DataType.Structure);
                 data.SetUInt8(2);
                 data.SetUInt8((int)DataType.Structure);
                 data.SetUInt8(2);
                 GXCommon.SetData(settings, data, DataType.OctetString, it.ActionUp.LogicalName); //Logical name.
                 GXCommon.SetData(settings, data, DataType.UInt16, it.ActionUp.ScriptSelector); //ScriptSelector
                 data.SetUInt8((int)DataType.Structure);
                 data.SetUInt8(2);
                 GXCommon.SetData(settings, data, DataType.OctetString, it.ActionDown.LogicalName); //Logical name.
                 GXCommon.SetData(settings, data, DataType.UInt16, it.ActionDown.ScriptSelector); //ScriptSelector
             }
         }
         return data.Array();
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return null;
 }