private static void GetInterval(GXDLMSInterval interval, GXByteBuffer data)
        {
            data.SetUInt8((byte)DataType.Array);
            data.SetUInt8(5);
            byte b = interval.StartHour;

            b |= (byte)((byte)interval.IntervalTariff << 5);
            b |= (byte)((interval.UseInterval ? 1 : 0) << 7);
            data.SetUInt8(b);
            data.SetUInt16((UInt16)interval.WeeklyActivation);
            UInt16 v = interval.SpecialDayMonth;

            v |= (UInt16)(interval.SpecialDay << 8);
            v |= (UInt16)((interval.SpecialDayEnabled ? 1 : 0) << 15);
            data.SetUInt16(v);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate User information initiate request.
        /// </summary>
        /// <param name="settings">DLMS settings.</param>
        /// <param name="cipher"></param>
        /// <param name="data"></param>
        private static void GetInitiateRequest(GXDLMSSettings settings, GXICipher cipher, GXByteBuffer data)
        {
            // Tag for xDLMS-Initiate request
            data.SetUInt8(GXCommon.InitialRequest);
            // Usage field for dedicated-key component. Not used
            data.SetUInt8(0x00);
            //encoding of the response-allowed component (BOOLEAN DEFAULT TRUE)
            // usage flag (FALSE, default value TRUE conveyed)
            data.SetUInt8(0);

            // Usage field of the proposed-quality-of-service component. Not used
            data.SetUInt8(0x00);
            data.SetUInt8(settings.DLMSVersion);
            // Tag for conformance block
            data.SetUInt8(0x5F);
            data.SetUInt8(0x1F);
            // length of the conformance block
            data.SetUInt8(0x04);
            // encoding the number of unused bits in the bit string
            data.SetUInt8(0x00);
            if (settings.UseLogicalNameReferencing)
            {
                data.Set(settings.LnSettings.ConformanceBlock);
            }
            else
            {
                data.Set(settings.SnSettings.ConformanceBlock);
            }
            data.SetUInt16(settings.MaxReceivePDUSize);
        }
Ejemplo n.º 3
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(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.UInt16);
             data.SetUInt16(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);
             data.Set(GXCommon.LogicalNameToBytes(it.LogicalName));
             //Add script selector.
             data.SetUInt8((byte)DataType.UInt16);
             data.SetUInt16(it.ScriptSelector);
             //Add switch time.
             GXCommon.SetData(settings, data, DataType.OctetString, it.SwitchTime);
             //Add validity window.
             data.SetUInt8((byte)DataType.UInt16);
             data.SetUInt16(it.ValidityWindow);
             //Add exec week days.
             GXCommon.SetData(settings, data, DataType.BitString, it.ExecWeekdays);
             //Add exec spec days.
             GXCommon.SetData(settings, data, DataType.BitString, it.ExecSpecDays);
             //Add begin date.
             GXCommon.SetData(settings, data, DataType.OctetString, it.BeginDate);
             //Add end date.
             GXCommon.SetData(settings, data, DataType.OctetString, it.EndDate);
         }
         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)
            {
                return(CalendarName);
            }
            if (e.Index == 3)
            {
                return(Enabled);
            }
            if (e.Index == 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);
                data.SetUInt8((byte)Plan.SpecialDays.Length);
                foreach (UInt16 it in Plan.SpecialDays)
                {
                    data.SetUInt8((byte)DataType.UInt16);
                    data.SetUInt16(it);
                }
            }
            if (e.Index == 5)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Structure);
                if (ActivationTime == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    data.SetUInt8((byte)DataType.Structure);
                    //Count
                    data.SetUInt8(2);
                    //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);
        }
Ejemplo n.º 5
0
 private void AddEntry(GXDLMSSettings settings, GXScheduleEntry it, GXByteBuffer data)
 {
     data.SetUInt8((byte)DataType.Structure);
     data.SetUInt8(10);
     //Add index.
     data.SetUInt8((byte)DataType.UInt16);
     data.SetUInt16(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(6);
     if (it.Script == null)
     {
         data.Set(new byte[] { 0, 0, 0, 0, 0, 0 });
     }
     else
     {
         data.Set(GXCommon.LogicalNameToBytes(it.Script.LogicalName));
     }
     //Add script selector.
     data.SetUInt8((byte)DataType.UInt16);
     data.SetUInt16(it.ScriptSelector);
     //Add switch time.
     GXCommon.SetData(settings, data, DataType.OctetString, it.SwitchTime);
     //Add validity window.
     data.SetUInt8((byte)DataType.UInt16);
     data.SetUInt16(it.ValidityWindow);
     //Add exec week days.
     GXCommon.SetData(settings, data, DataType.BitString, GXBitString.ToBitString((byte)it.ExecWeekdays, 7));
     //Add exec spec days.
     GXCommon.SetData(settings, data, DataType.BitString, it.ExecSpecDays);
     //Add begin date.
     GXCommon.SetData(settings, data, DataType.OctetString, it.BeginDate);
     //Add end date.
     GXCommon.SetData(settings, data, DataType.OctetString, it.EndDate);
 }
Ejemplo n.º 6
0
        private static byte[] GetUserInformation(GXDLMSSettings settings, GXICipher cipher)
        {
            GXByteBuffer data = new GXByteBuffer();

            data.SetUInt8(GXCommon.InitialResponce); // 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.MaxReceivePDUSize);
            //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(0x28, cipher.SystemTitle, data.Array()));
            }
            return(data.Array());
        }
Ejemplo n.º 7
0
        private static void UpdateTemplateDescription(GXByteBuffer columns, GXByteBuffer data, int index)
        {
            DataType ch    = (DataType)data.GetUInt8();
            int      count = GXCommon.GetObjectCount(data);

            if (index == -1)
            {
                columns.SetUInt8(ch);
                if (ch == DataType.Array)
                {
                    columns.SetUInt16((UInt16)count);
                }
                else
                {
                    columns.SetUInt8((byte)count);
                }
            }
            GXDataInfo info = new GXDataInfo();

            for (int pos = 0; pos < count; ++pos)
            {
                //If all data is captured.
                if (index == -1 || pos == index)
                {
                    DataType dt = (DataType)data.GetUInt8(data.Position);
                    if (dt == DataType.Array || dt == DataType.Structure)
                    {
                        UpdateTemplateDescription(columns, data, -1);
                        if (ch == DataType.Array)
                        {
                            break;
                        }
                    }
                    else
                    {
                        info.Clear();
                        columns.SetUInt8((byte)dt);
                        //Get data.
                        GXCommon.GetData(null, data, info);
                    }
                    if (index == pos)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 8
0
 private void ValuesLb_ItemCheck(object sender, ItemCheckEventArgs e)
 {
     try
     {
         if (DurationTb.Text != "")
         {
             UInt32        dur = UInt32.Parse(DurationTb.Text);
             StringBuilder sb  = new StringBuilder();
             for (int pos = 0; pos != 16; ++pos)
             {
                 if (e != null && e.Index == pos)
                 {
                     sb.Append(e.NewValue == CheckState.Checked ? "1" : "0");
                 }
                 else
                 {
                     if (ValuesLb.GetItemChecked(pos))
                     {
                         sb.Append("1");
                     }
                     else
                     {
                         sb.Append("0");
                     }
                 }
             }
             GXBitString  bs = new GXBitString(sb.ToString());
             GXByteBuffer bb = new GXByteBuffer();
             bb.SetUInt16(Convert.ToUInt16(bs));
             bb.SetUInt32(dur);
             (Target as GXDLMSData).Value = bb.Array();
             errorProvider1.SetError((Control)sender, Properties.Resources.ValueChangedTxt);
             Target.UpdateDirty(2, bb.Array());
             HexTb.Text = bb.ToString();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
 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.Structure);
         //Add count
         data.SetUInt8(2);
         data.SetUInt8((byte)DataType.Int8);
         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));
     }
     if (e.Index == 10)
     {
         return(GetUserList(settings, e).Array());
     }
     if (e.Index == 11)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Structure);
         //Add structure size.
         data.SetUInt8(2);
         GXCommon.SetData(settings, data, DataType.UInt8, CurrentUser.Key);
         GXCommon.SetData(settings, data, DataType.String, CurrentUser.Value);
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
Ejemplo n.º 11
0
        ///<summary>
        ///Server generates AARE message.
        ///</summary>
        internal static void GenerateAARE(GXDLMSSettings settings, GXByteBuffer data,
                                          AssociationResult result, SourceDiagnostic diagnostic, GXICipher cipher)
        {
            int offset = data.Position;

            // 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);
            }
            //Add User Information
            //Tag 0xBE
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
            byte[] 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));
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Generate User information initiate request.
        /// </summary>
        /// <param name="settings">DLMS settings.</param>
        /// <param name="cipher"></param>
        /// <param name="data"></param>
        private static void GetInitiateRequest(GXDLMSSettings settings, GXICipher cipher, GXByteBuffer data)
        {
            // Tag for xDLMS-Initiate request
            data.SetUInt8((byte)Command.InitiateRequest);
            // Usage field for dedicated-key component. Not used
            data.SetUInt8(0x00);
            //encoding of the response-allowed component (BOOLEAN DEFAULT TRUE)
            // usage flag (FALSE, default value TRUE conveyed)
            data.SetUInt8(0);

            // Usage field of the proposed-quality-of-service component. Not used
            data.SetUInt8(0x00);
            data.SetUInt8(settings.DLMSVersion);
            // Tag for conformance block
            data.SetUInt8(0x5F);
            data.SetUInt8(0x1F);
            // length of the conformance block
            data.SetUInt8(0x04);
            // encoding the number of unused bits in the bit string
            data.SetUInt8(0x00);
            if (settings.UseLogicalNameReferencing)
            {
                data.Set(settings.LnSettings.ConformanceBlock);
            }
            else
            {
                data.Set(settings.SnSettings.ConformanceBlock);
            }
            data.SetUInt16(settings.MaxPduSize);
        }
 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.");
 }
 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;
 }
Ejemplo n.º 17
0
        ///<summary>
        ///Convert object to DLMS bytes.
        ///</summary>
        ///<param name="settings">DLMS settings.</param>
        ///<param name="buff">Byte buffer where data is write.</param>
        ///<param name="dataType">Data type.</param>
        ///<param name="value">Added Value.</param>
        public static void SetData(GXDLMSSettings settings, GXByteBuffer buff, DataType type, object value)
        {
            if ((type == DataType.Array || type == DataType.Structure) && value is byte[])
            {
                // If byte array is added do not add type.
                buff.Set((byte[])value);
                return;
            }
            buff.SetUInt8((byte)type);
            switch (type)
            {
                case DataType.None:
                    break;
                case DataType.Boolean:
                    if (Convert.ToBoolean(value))
                    {
                        buff.SetUInt8(1);
                    }
                    else
                    {
                        buff.SetUInt8(0);
                    }
                    break;
                case DataType.Int8:
                    buff.SetUInt8((byte)Convert.ToSByte(value));
                    break;
                case DataType.UInt8:
                case DataType.Enum:
                    buff.SetUInt8(Convert.ToByte(value));
                    break;
                case DataType.Int16:
                    if (value is UInt16)
                    {
                        buff.SetUInt16((UInt16)value);
                    }
                    else
                    {
                        buff.SetUInt16((UInt16)(Convert.ToInt16(value) & 0xFFFF));
                    }
                    break;
                case DataType.UInt16:
                    buff.SetUInt16(Convert.ToUInt16(value));
                    break;
                case DataType.Int32:
                    buff.SetUInt32((UInt32)Convert.ToInt32(value));
                    break;
                case DataType.UInt32:

                    buff.SetUInt32(Convert.ToUInt32(value));
                    break;
                case DataType.Int64:
                    buff.SetUInt64((UInt64)Convert.ToInt64(value));
                    break;
                case DataType.UInt64:
                    buff.SetUInt64(Convert.ToUInt64(value));
                    break;
                case DataType.Float32:
                    buff.SetFloat((float)value);
                    break;
                case DataType.Float64:
                    buff.SetDouble((double)value);
                    break;
                case DataType.BitString:
                    SetBitString(buff, value);
                    break;
                case DataType.String:
                    SetString(buff, value);
                    break;
                case DataType.StringUTF8:
                    SetUtcString(buff, value);
                    break;
                case DataType.OctetString:
                    if (value is GXDate)
                    {
                        //Add size
                        buff.SetUInt8(5);
                        SetDate(buff, value);
                    }
                    else if (value is GXTime)
                    {
                        //Add size
                        buff.SetUInt8(4);
                        SetTime(buff, value);
                    }
                    else if (value is GXDateTime || value is DateTime)
                    {
                        //Add size
                        buff.SetUInt8(12);
                        SetDateTime(settings, buff, value);
                    }
                    else
                    {
                        SetOctetString(buff, value);
                    }
                    break;
                case DataType.Array:
                case DataType.Structure:
                    SetArray(settings, buff, value);
                    break;
                case DataType.Bcd:
                    SetBcd(buff, value);
                    break;
                case DataType.CompactArray:
                    throw new Exception("Invalid data type.");
                case DataType.DateTime:
                    SetDateTime(settings, buff, value);
                    break;
                case DataType.Date:
                    SetDate(buff, value);
                    break;
                case DataType.Time:
                    SetTime(buff, value);
                    break;
                default:
                    throw new Exception("Invalid data type.");
            }
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Set item count.
 /// </summary>
 /// <param name="count"></param>
 /// <param name="buff"></param>
 internal static void SetObjectCount(int count, GXByteBuffer buff)
 {
     if (count < 0x80)
     {
         buff.SetUInt8((byte)count);
     }
     else if (count < 0x100)
     {
         buff.SetUInt8(0x81);
         buff.SetUInt8((byte)count);
     }
     else if (count < 0x10000)
     {
         buff.SetUInt8(0x82);
         buff.SetUInt16((UInt16)count);
     }
     else
     {
         buff.SetUInt8(0x84);
         buff.SetUInt32((UInt32)count);
     }
 }
Ejemplo n.º 19
0
        ///<summary>
        ///Convert date time to DLMS bytes.
        ///</summary>
        ///<param name="buff">
        ///Byte buffer where data is write.
        ///</param>
        ///<param name="value">
        ///Added value.
        ///</param>
        private static void SetDateTime(GXDLMSSettings settings, GXByteBuffer buff, object value)
        {
            GXDateTime dt;
            if (value is GXDateTime)
            {
                dt = (GXDateTime)value;
            }
            else if (value is DateTime)
            {
                dt = new GXDateTime((DateTime)value);
                dt.Skip = dt.Skip | DateTimeSkips.Ms;
            }
            else if (value is string)
            {
                dt = new GXDateTime(DateTime.Parse((string)value));
                dt.Skip = dt.Skip | DateTimeSkips.Ms;
            }
            else
            {
                throw new Exception("Invalid date format.");
            }
            if (dt.Value.UtcDateTime == DateTime.MinValue)
            {
                dt.Value = DateTime.SpecifyKind(new DateTime(2000, 1, 1).Date, DateTimeKind.Utc);
            }
            else if (dt.Value.UtcDateTime == DateTime.MaxValue)
            {
                dt.Value = DateTime.SpecifyKind(DateTime.Now.AddYears(1).Date, DateTimeKind.Utc);
            }
            DateTimeOffset tm = dt.Value;
            if ((dt.Skip & DateTimeSkips.Year) == 0)
            {
                buff.SetUInt16((ushort)tm.Year);
            }
            else
            {
                buff.SetUInt16(0xFFFF);
            }
            if ((dt.Skip & DateTimeSkips.Month) == 0)
            {
                if (dt.DaylightSavingsBegin)
                {
                    buff.SetUInt8(0xFE);
                }
                else if (dt.DaylightSavingsEnd)
                {
                    buff.SetUInt8(0xFD);
                }
                else
                {
                    buff.SetUInt8((byte)tm.Month);
                }
            }
            else
            {
                buff.SetUInt8(0xFF);
            }
            if ((dt.Skip & DateTimeSkips.Day) == 0)
            {
                buff.SetUInt8((byte)tm.Day);
            }
            else
            {
                buff.SetUInt8(0xFF);
            }
            // Add week day
            if ((dt.Skip & DateTimeSkips.DayOfWeek) != 0)
            {
                buff.SetUInt8(0xFF);
            }
            else
            {
                if (dt.Value.DayOfWeek == DayOfWeek.Sunday)
                {
                    buff.SetUInt8(7);
                }
                else
                {
                    buff.SetUInt8((byte)(dt.Value.DayOfWeek));
                }
            }
            //Add time.
            if ((dt.Skip & DateTimeSkips.Hour) == 0)
            {
                buff.SetUInt8((byte)tm.Hour);
            }
            else
            {
                buff.SetUInt8(0xFF);
            }

            if ((dt.Skip & DateTimeSkips.Minute) == 0)
            {
                buff.SetUInt8((byte)tm.Minute);
            }
            else
            {
                buff.SetUInt8(0xFF);
            }
            if ((dt.Skip & DateTimeSkips.Second) == 0)
            {
                buff.SetUInt8((byte)tm.Second);
            }
            else
            {
                buff.SetUInt8(0xFF);
            }

            if ((dt.Skip & DateTimeSkips.Ms) == 0)
            {
                buff.SetUInt8((byte)(tm.Millisecond / 10));
            }
            else
            {
                buff.SetUInt8((byte)0xFF); //Hundredths of second is not used.
            }
            //Add deviation.
            if ((dt.Skip & DateTimeSkips.Devitation) == 0)
            {
                if (settings != null && settings.UtcTimeZone)
                {
                    buff.SetInt16((Int16)(dt.Value.Offset.TotalMinutes));
                }
                else
                {
                    buff.SetInt16((Int16)(-dt.Value.Offset.TotalMinutes));
                }
            }
            else //deviation not used.
            {
                buff.SetUInt16(0x8000);
            }
            //Add clock_status
            if (dt.Value.LocalDateTime.IsDaylightSavingTime())
            {
                buff.SetUInt8((byte)(dt.Status | ClockStatus.DaylightSavingActive));
            }
            else
            {
                buff.SetUInt8((byte)dt.Status);
            }
        }
Ejemplo n.º 20
0
 ///<summary>
 ///Convert date to DLMS bytes.
 ///</summary>
 ///<param name="buff">
 ///Byte buffer where data is write.
 ///</param>
 ///<param name="value">
 ///Added value.
 ///</param>
 private static void SetDate(GXByteBuffer buff, object value)
 {
     GXDateTime dt;
     if (value is GXDateTime)
     {
         dt = (GXDateTime)value;
     }
     else if (value is DateTime)
     {
         dt = new GXDateTime((DateTime)value);
     }
     else if (value is DateTimeOffset)
     {
         dt = new GXDateTime((DateTimeOffset)value);
     }
     else if (value is string)
     {
         dt = DateTime.Parse((string)value);
     }
     else
     {
         throw new Exception("Invalid date format.");
     }
     // Add year.
     if ((dt.Skip & DateTimeSkips.Year) != 0)
     {
         buff.SetUInt16(0xFFFF);
     }
     else
     {
         buff.SetUInt16((UInt16)dt.Value.Year);
     }
     // Add month
     if (dt.DaylightSavingsBegin)
     {
         buff.SetUInt8(0xFE);
     }
     else if (dt.DaylightSavingsEnd)
     {
         buff.SetUInt8(0xFD);
     }
     else if ((dt.Skip & DateTimeSkips.Month) != 0)
     {
         buff.SetUInt8(0xFF);
     }
     else
     {
         buff.SetUInt8((byte)dt.Value.Month);
     }
     // Add day
     if ((dt.Skip & DateTimeSkips.Day) != 0)
     {
         buff.SetUInt8(0xFF);
     }
     else
     {
         buff.SetUInt8((byte)dt.Value.Day);
     }
     // Add week day
     if ((dt.Skip & DateTimeSkips.DayOfWeek) != 0)
     {
         buff.SetUInt8(0xFF);
     }
     else
     {
         if (dt.Value.DayOfWeek == DayOfWeek.Sunday)
         {
             buff.SetUInt8(7);
         }
         else
         {
             buff.SetUInt8((byte)(dt.Value.DayOfWeek));
         }
     }
 }