Beispiel #1
0
        /// <summary>
        /// Presets the time to a new value (preset_time) and defines a validity_interval within which the new time can be activated.
        /// </summary>
        /// <param name="presetTime"></param>
        /// <param name="validityIntervalStart"></param>
        /// <param name="validityIntervalEnd"></param>
        /// <returns></returns>
        public byte[][] PresetAdjustingTime(GXDLMSClient client, DateTime presetTime, DateTime validityIntervalStart, DateTime validityIntervalEnd)
        {
            GXByteBuffer buff = new GXByteBuffer();

            buff.Add((byte)DataType.Structure);
            buff.Add((byte)3);
            GXCommon.SetData(client.Settings, buff, DataType.OctetString, presetTime);
            GXCommon.SetData(client.Settings, buff, DataType.OctetString, validityIntervalStart);
            GXCommon.SetData(client.Settings, buff, DataType.OctetString, validityIntervalEnd);
            return(client.Method(this, 5, buff.Array(), DataType.Array));
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(this.LogicalName);
     }
     if (e.Index == 2)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(ListeningWindow.Count, buff);
         foreach (KeyValuePair <GXDateTime, GXDateTime> it in ListeningWindow)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(2);
             GXCommon.SetData(settings, buff, DataType.OctetString, it.Key);
             GXCommon.SetData(settings, buff, DataType.OctetString, it.Value);
         }
         return(buff.Array());
     }
     if (e.Index == 3)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(AllowedSenders.Length, buff);
         foreach (string it in AllowedSenders)
         {
             GXCommon.SetData(settings, buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it));
         }
         return(buff.Array());
     }
     if (e.Index == 4)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(SendersAndActions.Count, buff);
         foreach (KeyValuePair <string, KeyValuePair <int, GXDLMSScriptAction> > it in SendersAndActions)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(2);
             GXCommon.SetData(settings, buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Key));
             //TODO: GXCommon.SetData(buff, DataType.OctetString, (it.Value.));
         }
         return(buff.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
 internal static void SetValue(GXByteBuffer buff, object data)
 {
     GXByteBuffer tmp = new GXByteBuffer();
     tmp.Add(data);
     buff.Add((byte)tmp.Size);
     buff.Set(tmp.Array());
 }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, int index, int selector, object parameters)
 {
     if (index == 1)
     {
         return(this.LogicalName);
     }
     if (index == 2)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(ListeningWindow.Count, buff);
         foreach (KeyValuePair <GXDateTime, GXDateTime> it in ListeningWindow)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(2);
             GXCommon.SetData(buff, DataType.DateTime, it.Key);
             GXCommon.SetData(buff, DataType.DateTime, it.Value);
         }
         return(buff.Array());
     }
     if (index == 3)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(AllowedSenders.Length, buff);
         foreach (string it in AllowedSenders)
         {
             GXCommon.SetData(buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it));
         }
         return(buff.Array());
     }
     if (index == 4)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(SendersAndActions.Count, buff);
         foreach (KeyValuePair <string, KeyValuePair <int, GXDLMSScriptAction> > it in SendersAndActions)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(2);
             GXCommon.SetData(buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Key));
             //TODO: GXCommon.SetData(buff, DataType.OctetString, (it.Value.));
         }
         return(buff.Array());
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
        internal static void SetValue(GXByteBuffer buff, object data)
        {
            GXByteBuffer tmp = new GXByteBuffer();

            tmp.Add(data);
            buff.Add((byte)tmp.Size);
            buff.Set(tmp.Array());
        }
Beispiel #6
0
        void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            switch (e.Index)
            {
            case 1:
                LogicalName = GXCommon.ToLogicalName(e.Value);
                break;

            case 2:
                Mode = (IecTwistedPairSetupMode)Convert.ToByte(e.Value);
                break;

            case 3:
                Speed = (BaudRate)Convert.ToByte(e.Value);
                break;

            case 4:
            {
                GXByteBuffer list = new GXByteBuffer();
                if (e.Value != null)
                {
                    foreach (object it in (List <object>)e.Value)
                    {
                        list.Add((byte)it);
                    }
                }
                PrimaryAddresses = list.Array();
                break;
            }

            case 5:
            {
                List <sbyte> list = new List <sbyte>();
                if (e.Value != null)
                {
                    foreach (object it in (List <object>)e.Value)
                    {
                        list.Add((sbyte)it);
                    }
                }
                Tabis = list.ToArray();
                break;
            }

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
        }
        private static string ToAddressString(object value)
        {
            if (value == null)
            {
                return("");
            }
            if (value is string)
            {
                return(System.Net.IPAddress.Parse((string)value).ToString());
            }
            GXByteBuffer bb = new GXByteBuffer();

            bb.Add(value);
            return(new System.Net.IPAddress(bb.Array()).ToString());
        }
Beispiel #8
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         if (e.Value is string)
         {
             LogicalName = e.Value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])e.Value, DataType.OctetString).ToString();
         }
     }
     else if (e.Index == 2)
     {
         Mode = (IecTwistedPairSetupMode)e.Value;
     }
     else if (e.Index == 3)
     {
         Speed = (BaudRate)e.Value;
     }
     else if (e.Index == 4)
     {
         GXByteBuffer list = new GXByteBuffer();
         foreach (object it in (object[])e.Value)
         {
             list.Add((byte)it);
         }
         PrimaryAddresses = list.Array();
     }
     else if (e.Index == 5)
     {
         List <sbyte> list = new List <sbyte>();
         foreach (object it in (object[])e.Value)
         {
             list.Add((sbyte)it);
         }
         Tabis = list.ToArray();
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, int index, object value)
 {
     if (index == 1)
     {
         if (value is string)
         {
             LogicalName = value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])value, DataType.OctetString).ToString();
         }
     }
     else if (index == 2)
     {
         Mode = (IecTwistedPairSetupMode)value;
     }
     else if (index == 3)
     {
         Speed = (BaudRate)value;
     }
     else if (index == 4)
     {
         GXByteBuffer list = new GXByteBuffer();
         foreach (object it in (object[])value)
         {
             list.Add((byte)it);
         }
         PrimaryAddresses = list.Array();
     }
     else if (index == 5)
     {
         List <sbyte> list = new List <sbyte>();
         foreach (object it in (object[])value)
         {
             list.Add((sbyte)it);
         }
         Tabis = list.ToArray();
     }
     else
     {
         throw new ArgumentException("SetValue failed. Invalid attribute index.");
     }
 }
Beispiel #10
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         LogicalName = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 2)
     {
         Mode = (IecTwistedPairSetupMode)Convert.ToByte(e.Value);
     }
     else if (e.Index == 3)
     {
         Speed = (BaudRate)Convert.ToByte(e.Value);
     }
     else if (e.Index == 4)
     {
         GXByteBuffer list = new GXByteBuffer();
         if (e.Value != null)
         {
             foreach (object it in (List <object>)e.Value)
             {
                 list.Add((byte)it);
             }
         }
         PrimaryAddresses = list.Array();
     }
     else if (e.Index == 5)
     {
         List <sbyte> list = new List <sbyte>();
         if (e.Value != null)
         {
             foreach (object it in (List <object>)e.Value)
             {
                 list.Add((sbyte)it);
             }
         }
         Tabis = list.ToArray();
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="sn">Short name settings. </param>
        ///<param name="type">
        /// Interface type.
        ///</param>
        ///<param name="flagID">
        /// Three letters FLAG ID.
        ///</param>
        ///<param name="serialNumber">
        /// Meter serial number. Size of serial number is 5 bytes.
        ///</param>
        public GXDLMSSecureServer(GXDLMSAssociationShortName sn, InterfaceType type, string flagID, UInt64 serialNumber) :
            base(sn, type)
        {
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            GXByteBuffer bb = new GXByteBuffer();

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

            serial.SetUInt64(serialNumber);
            bb.Set(serial.Data, 3, 5);
            Ciphering       = new GXCiphering(bb.Array());
            Settings.Cipher = Ciphering;
        }
Beispiel #12
0
        ///<summary>
        /// Constructor.
        ///</summary>
        ///<param name="ln">Logical name settings. </param>
        ///<param name="hdlc">
        /// Interface type.
        ///</param>
        ///<param name="flagID">
        /// Three letters FLAG ID.
        ///</param>
        ///<param name="serialNumber">
        /// Meter serial number. Size of serial number is 5 bytes.
        ///</param>
        public GXDLMSSecureServer(GXDLMSAssociationLogicalName ln, GXDLMSHdlcSetup hdlc, string flagID, UInt64 serialNumber) :
            base(ln, hdlc)
        {
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            if (flagID == null || flagID.Length != 3)
            {
                throw new ArgumentOutOfRangeException("Invalid FLAG ID.");
            }
            ln.XDLMSContextInfo.settings = Settings;
            GXByteBuffer bb = new GXByteBuffer();

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

            serial.SetUInt64(serialNumber);
            bb.Set(serial.Data, 3, 5);
            Ciphering       = new GXCiphering(bb.Array());
            Settings.Cipher = Ciphering;
        }
Beispiel #13
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(this.LogicalName);
     }
     if (e.Index == 2)
     {
         return(MBusPortReference);
     }
     if (e.Index == 3)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(CaptureDefinition.Count, buff);
         foreach (KeyValuePair <string, string> it in CaptureDefinition)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(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);
 }
Beispiel #14
0
        /// <summary>
        /// Add ASN1 object to byte buffer.
        /// </summary>
        /// <param name="bb">Byte buffer where ANS1 object is serialized. </param>
        /// <param name="target">ANS1 object </param>
        /// <returns>Size of object. </returns>
        private static int GetBytes(GXByteBuffer bb, object target)
        {
            GXByteBuffer tmp;
            string       str;
            int          start = bb.Size;
            int          cnt   = 0;

            if (target is GXAsn1Context a)
            {
                tmp = new GXByteBuffer();
                foreach (object it in a)
                {
                    cnt += GetBytes(tmp, it);
                }
                start = bb.Size;
                if (a.Constructed)
                {
                    bb.SetUInt8((byte)((int)BerType.Constructed | (int)BerType.Context | a.Index));
                    GXCommon.SetObjectCount(cnt, bb);
                }
                else
                {
                    tmp.SetUInt8(0, (byte)((int)BerType.Context | a.Index));
                }
                cnt += bb.Size - start;
                bb.Set(tmp);
                return(cnt);
            }
            else if (target is object[])
            {
                tmp = new GXByteBuffer();
                foreach (object it in (object[])target)
                {
                    cnt += GetBytes(tmp, it);
                }
                start = bb.Size;
                bb.SetUInt8(BerType.Constructed | BerType.Sequence);
                GXCommon.SetObjectCount(cnt, bb);
                cnt += bb.Size - start;
                bb.Set(tmp);
                return(cnt);
            }
            else if (target is string)
            {
                bb.SetUInt8(BerType.PrintableString);
                GXCommon.SetObjectCount(((string)target).Length, bb);
                bb.Add(target);
            }
            else if (target is sbyte)
            {
                bb.SetUInt8((byte)BerType.Integer);
                GXCommon.SetObjectCount(1, bb);
                bb.Add(target);
            }
            else if (target is short)
            {
                bb.SetUInt8((byte)BerType.Integer);
                GXCommon.SetObjectCount(2, bb);
                bb.Add(target);
            }
            else if (target is int)
            {
                bb.SetUInt8((byte)BerType.Integer);
                GXCommon.SetObjectCount(4, bb);
                bb.Add(target);
            }
            else if (target is GXAsn1Integer)
            {
                bb.SetUInt8((byte)BerType.Integer);
                byte[] b = ((GXAsn1Integer)target).Value;
                GXCommon.SetObjectCount(b.Length, bb);
                bb.Add(b);
            }
            else if (target is long)
            {
                bb.SetUInt8((byte)BerType.Integer);
                GXCommon.SetObjectCount(8, bb);
                bb.Add(target);
            }
            else if (target is byte[])
            {
                bb.SetUInt8(BerType.OctetString);
                GXCommon.SetObjectCount(((byte[])target).Length, bb);
                bb.Add(target);
            }
            else if (target == null)
            {
                bb.SetUInt8(BerType.Null);
                GXCommon.SetObjectCount(0, bb);
            }
            else if (target is bool)
            {
                bb.SetUInt8(BerType.Boolean);
                bb.SetUInt8(1);
                if ((bool)target)
                {
                    bb.SetUInt8(255);
                }
                else
                {
                    bb.SetUInt8(0);
                }
            }
            else if (target is GXAsn1ObjectIdentifier)
            {
                bb.SetUInt8(BerType.ObjectIdentifier);
                byte[] t = ((GXAsn1ObjectIdentifier)target).Encoded;
                GXCommon.SetObjectCount(t.Length, bb);
                bb.Add(t);
            }
            else if (target is KeyValuePair <object, object> )
            {
                KeyValuePair <object, object> e = (KeyValuePair <object, object>)target;
                GXByteBuffer tmp2 = new GXByteBuffer();
                if (e.Value != null)
                {
                    tmp  = new GXByteBuffer();
                    cnt += GetBytes(tmp2, e.Key);
                    cnt += GetBytes(tmp2, e.Value);
                    tmp.SetUInt8(BerType.Constructed | BerType.Sequence);
                    GXCommon.SetObjectCount(cnt, tmp);
                    tmp.Set(tmp2);
                }
                else
                {
                    GetBytes(tmp2, (e.Key as List <object>)[0]);
                    tmp = tmp2;
                }
                // Update len.
                cnt = bb.Size;
                bb.SetUInt8(BerType.Constructed | BerType.Set);
                GXCommon.SetObjectCount(tmp.Size, bb);
                bb.Set(tmp);
                return(bb.Size - cnt);
            }
            else if (target is GXAsn1Utf8String)
            {
                bb.SetUInt8(BerType.Utf8StringTag);
                str = target.ToString();
                GXCommon.SetObjectCount(str.Length, bb);
                bb.Add(str);
            }
            else if (target is GXAsn1Ia5String)
            {
                bb.SetUInt8(BerType.Ia5String);
                str = target.ToString();
                GXCommon.SetObjectCount(str.Length, bb);
                bb.Add(str);
            }
            else if (target is GXAsn1BitString)
            {
                GXAsn1BitString bs = (GXAsn1BitString)target;
                bb.SetUInt8(BerType.BitString);
                GXCommon.SetObjectCount(1 + bs.Value.Length, bb);
                bb.SetUInt8((byte)bs.PadBits);
                bb.Add(bs.Value);
            }
            else if (target is GXAsn1PublicKey)
            {
                GXAsn1PublicKey bs = (GXAsn1PublicKey)target;
                bb.SetUInt8(BerType.BitString);
                // Size is 64 bytes + padding and uncompressed point indicator.
                GXCommon.SetObjectCount(66, bb);
                // Add padding.
                bb.SetUInt8(0);
                // prefixed with the uncompressed point indicator 04
                bb.SetUInt8(4);
                bb.Add(bs.Value);
                // Count is type + size + 64 bytes + padding + uncompressed point
                // indicator.
                return(68);
            }
            else if (target is DateTime)
            {
                // Save date time in UTC.
                bb.SetUInt8(BerType.UtcTime);
                str = DateToString((DateTime)target);
                bb.SetUInt8((byte)str.Length);
                bb.Add(str);
            }
            else if (target is GXAsn1Sequence || target is IList)
            {
                tmp = new GXByteBuffer();
                foreach (object it in (IList)target)
                {
                    cnt += GetBytes(tmp, it);
                }
                start = bb.Size;
                if (target is GXAsn1Context c)
                {
                    if (c.Constructed)
                    {
                        bb.SetUInt8((byte)((byte)BerType.Constructed | (byte)BerType.Sequence | c.Index));
                    }
                    else
                    {
                        bb.SetUInt8((byte)((byte)BerType.Sequence | c.Index));
                    }
                }
                else
                {
                    bb.SetUInt8(BerType.Constructed | BerType.Sequence);
                }
                GXCommon.SetObjectCount(cnt, bb);
                cnt += bb.Size - start;
                bb.Set(tmp);
                return(cnt);
            }
            else
            {
                throw new ArgumentException("Invalid type: " + target.GetType().ToString());
            }
            return(bb.Size - start);
        }
Beispiel #15
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, int index, int selector, object parameters)
 {
     if (index == 1)
     {
         return(this.LogicalName);
     }
     if (index == 2)
     {
         return(MBusPortReference);
     }
     if (index == 3)
     {
         GXByteBuffer buff = new GXByteBuffer();
         buff.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(CaptureDefinition.Count, buff);
         foreach (KeyValuePair <string, string> it in CaptureDefinition)
         {
             buff.Add((byte)DataType.Structure);
             buff.Add(2);
             GXCommon.SetData(buff, DataType.UInt8, it.Key);
             GXCommon.SetData(buff, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Value));
         }
         return(buff.Array());
     }
     if (index == 4)
     {
         return(CapturePeriod);
     }
     if (index == 5)
     {
         return(PrimaryAddress);
     }
     if (index == 6)
     {
         return(IdentificationNumber);
     }
     if (index == 7)
     {
         return(ManufacturerID);
     }
     if (index == 8)
     {
         return(DataHeaderVersion);
     }
     if (index == 9)
     {
         return(DeviceType);
     }
     if (index == 10)
     {
         return(AccessNumber);
     }
     if (index == 11)
     {
         return(Status);
     }
     if (index == 12)
     {
         return(Alarm);
     }
     if (Version > 0)
     {
         if (index == 13)
         {
             return(Configuration);
         }
         if (index == 14)
         {
             return(EncryptionKeyStatus);
         }
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }