示例#1
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.");
 }
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            object ret;

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

            case 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(DataType.Structure);
                        bb.SetUInt8(2);
                        GXCommon.SetData(settings, bb, DataType.UInt16, it.Key);
                        GXCommon.SetData(settings, bb, DataType.UInt32, it.Value);
                    }
                }
                ret = bb.Array();
            }
            break;

            case 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);
                ret = bb.Array();
            }
            break;

            case 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(DataType.Structure);
                        bb.SetUInt8(2);
                        GXCommon.SetData(settings, bb, DataType.UInt16, it.Key);
                        GXCommon.SetData(settings, bb, DataType.UInt32, it.Value);
                    }
                }
                ret = bb.Array();
            }
            break;

            case 5:
                ret = RepetitionsCounter;
                break;

            case 6:
                ret = TransmissionsCounter;
                break;

            case 7:
                ret = CrcOkFramesCounter;
                break;

            case 8:
                ret = CrcNOkFramesCounter;
                break;

            default:
                e.Error = ErrorCode.ReadWriteDenied;
                ret     = null;
                break;
            }
            return(ret);
        }
        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);
        }
 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);
 }
 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);
 }
示例#6
0
        ///
        ///<summary>Handle read request.
        /// </summary>
        ///<returns> Reply to the client. </returns>
        ///
        private byte[][] HandleReadRequest()
        {
            GXByteBuffer data = Reply.Data;
            short        type;
            object       value = null;
            GXByteBuffer bb    = new GXByteBuffer();
            int          cnt   = GXCommon.GetObjectCount(data);

            GXCommon.SetObjectCount(cnt, bb);
            GXSNInfo info;

            for (int pos = 0; pos != cnt; ++pos)
            {
                type = data.GetUInt8();
                // GetRequest normal
                if (type == 2)
                {
                    int sn = data.GetUInt16();
                    info = FindSNObject(sn);
                    if (!info.IsAction)
                    {
                        ValueEventArgs e = new ValueEventArgs(info.Item, info.Index, 0, null);
                        Read(e);
                        if (e.Handled)
                        {
                            value = e.Value;
                        }
                        else
                        {
                            value = (info.Item as IGXDLMSBase).GetValue(Settings, info.Index, 0, null);
                        }
                        // Set status.
                        bb.SetUInt8(0);
                        GXDLMS.AppedData(info.Item, info.Index, bb, value);
                    }
                    else
                    {
                        ValueEventArgs e = new ValueEventArgs(info.Item, info.Index, 0, null);
                        Action(e);
                        if (e.Handled)
                        {
                            value = e.Value;
                        }
                        else
                        {
                            value = ((IGXDLMSBase)info.Item).Invoke(Settings, info.Index, null);
                        }
                        // Set status.
                        bb.SetUInt8(0);
                        // Add value
                        bb.SetUInt8(GXCommon.GetValueType(value));
                        bb.Add(value);
                    }
                }
                else if (type == 2)
                {
                    // Get request for next data block
                    throw new System.ArgumentException("TODO: Invalid Command.");
                }
                else if (type == 4)
                {
                    // Parameterised access.
                    int        sn         = data.GetUInt16();
                    int        selector   = data.GetUInt8();
                    GXDataInfo di         = new GXDataInfo();
                    object     parameters = GXCommon.GetData(data, di);
                    info = FindSNObject(sn);
                    if (!info.IsAction)
                    {
                        ValueEventArgs e = new ValueEventArgs(info.Item, info.Index, 0, parameters);
                        Read(e);
                        if (e.Handled)
                        {
                            value = e.Value;
                        }
                        else
                        {
                            value = (info.Item as IGXDLMSBase).GetValue(Settings, info.Index, selector, parameters);
                        }
                        // Set status.
                        bb.SetUInt8(0);
                        GXDLMS.AppedData(info.Item, info.Index, bb, value);
                    }
                    else
                    {
                        ValueEventArgs e = new ValueEventArgs(info.Item, info.Index, 0, parameters);
                        e.Value = parameters;
                        Action(e);
                        if (e.Handled)
                        {
                            value = e.Value;
                        }
                        else
                        {
                            value = ((IGXDLMSBase)info.Item).Invoke(Settings, info.Index, parameters);
                        }
                        // Add value
                        bb.Add(value);
                    }
                }
                else
                {
                    throw new System.ArgumentException("Invalid Command.");
                }
            }
            return(GXDLMS.SplitPdu(Settings, Command.ReadResponse, 1, bb,
                                   ErrorCode.Ok, DateTime.MinValue)[0]);
        }
示例#7
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);
 }
示例#8
0
 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);
 }
 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, GXCommon.GetValueType(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, int index, int selector, object parameters)
 {
     if (ObjectList == null)
     {
         ObjectList = new GXDLMSObjectCollection();
     }
     if (index == 1)
     {
         return(this.LogicalName);
     }
     else if (index == 2)
     {
         int          cnt  = ObjectList.Count;
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         //Add count
         GXCommon.SetObjectCount(cnt, data);
         if (cnt != 0)
         {
             foreach (GXDLMSObject it in ObjectList)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)4);                                       //Count
                 GXCommon.SetData(data, DataType.Int16, it.ShortName);         //base address.
                 GXCommon.SetData(data, DataType.UInt16, it.ObjectType);       //ClassID
                 GXCommon.SetData(data, DataType.UInt8, 0);                    //Version
                 GXCommon.SetData(data, DataType.OctetString, it.LogicalName); //LN
             }
             if (ObjectList.FindBySN(this.ShortName) == null)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8((byte)4);                                         //Count
                 GXCommon.SetData(data, DataType.Int16, this.ShortName);         //base address.
                 GXCommon.SetData(data, DataType.UInt16, this.ObjectType);       //ClassID
                 GXCommon.SetData(data, DataType.UInt8, 0);                      //Version
                 GXCommon.SetData(data, DataType.OctetString, this.LogicalName); //LN
             }
         }
         return(data.Array());
     }
     else if (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(it, data);
         }
         if (!lnExists)
         {
             GetAccessRights(this, data);
         }
         return(data.Array());
     }
     else if (index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         GXCommon.SetData(data, DataType.OctetString, SecuritySetupReference);
         return(data.Array());
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
        private object[] GetData()
        {
            GXAsn1ObjectIdentifier a = new GXAsn1ObjectIdentifier(HashAlgorithmConverter.GetString(SignatureAlgorithm));
            GXAsn1Context          p = new GXAsn1Context();

            p.Add((sbyte)Version);
            object         subjectPKInfo = GXAsn1Converter.FromByteArray(PublicKey.RawValue);
            GXAsn1Sequence s             = new GXAsn1Sequence();
            GXAsn1Sequence s1;

            if (SubjectKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.SubjectKeyIdentifier)));
                GXByteBuffer bb = new GXByteBuffer();
                bb.SetUInt8(BerType.OctetString);
                GXCommon.SetObjectCount(SubjectKeyIdentifier.Length, bb);
                bb.Set(SubjectKeyIdentifier);
                s1.Add(bb.Array());
                s.Add(s1);
            }
            if (AuthorityKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.AuthorityKeyIdentifier)));
                GXAsn1Sequence seq = new GXAsn1Sequence();
                seq.Add(AuthorityKeyIdentifier);
                s1.Add(GXAsn1Converter.ToByteArray(seq));
                s.Add(s1);
            }
            if (BasicConstraints)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.BasicConstraints)));
                GXAsn1Sequence seq = new GXAsn1Sequence();
                seq.Add(BasicConstraints);
                s1.Add(GXAsn1Converter.ToByteArray(seq));
                s.Add(s1);
            }
            if (KeyUsage == KeyUsage.None)
            {
                throw new Exception("Key usage not present.");
            }
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.KeyUsage)));
            byte value = 0;
            int  min   = 255;

            foreach (KeyUsage it in Enum.GetValues(typeof(KeyUsage)))
            {
                if ((it & KeyUsage) != 0)
                {
                    byte val = (byte)it;
                    value |= val;
                    if (val < min)
                    {
                        min = val;
                    }
                }
            }
            int offset = 7;

            while ((min >>= 2) != 0)
            {
                ++offset;
            }
            byte[] tmp = GXAsn1Converter.ToByteArray(new GXAsn1BitString(new byte[] { 0, value }));
            s1.Add(tmp);
            s.Add(s1);
            GXAsn1Sequence valid = new GXAsn1Sequence();

            valid.Add(ValidFrom);
            valid.Add(ValidTo);
            object[] list;
            if (s.Count == 0)
            {
                list = new object[] { p, SerialNumber, new object[] { a, SignatureParameters }, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo };
            }
            else
            {
                GXAsn1Context tmp2 = new GXAsn1Context();
                tmp2.Index = 3;
                tmp2.Add(s);
                list = new object[] { p, SerialNumber, new object[] { a, SignatureParameters }, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), subjectPKInfo, tmp2 };
            }
            return(list);
        }
示例#12
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)
     {
         //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)
     {
         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 (ushort it in MulticastIPAddress)
             {
                 GXCommon.SetData(settings, data, DataType.UInt16, 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)
     {
         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);
 }
示例#13
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            object ret;

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

            case 2:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (Actions == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    GXCommon.SetObjectCount(Actions.Length, data);
                    foreach (GXDLMSActionItem it in Actions)
                    {
                        data.SetUInt8((byte)DataType.Structure);
                        //Count
                        data.SetUInt8((byte)2);
                        GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(it.LogicalName));
                        GXCommon.SetData(settings, data, DataType.UInt16, it.ScriptSelector);
                    }
                }
                ret = data.Array();
            }
            break;

            case 3:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (PermissionsTable == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    GXCommon.SetObjectCount(PermissionsTable.Length, data);
                    foreach (string it in PermissionsTable)
                    {
                        GXCommon.SetData(settings, data, DataType.BitString, it);
                    }
                }
                ret = data.Array();
            }
            break;

            case 4:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (WeightingsTable == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    GXCommon.SetObjectCount(WeightingsTable.Length, data);
                    foreach (UInt16[] it in WeightingsTable)
                    {
                        data.SetUInt8((byte)DataType.Array);
                        GXCommon.SetObjectCount(it.Length, data);
                        foreach (UInt16 it2 in it)
                        {
                            GXCommon.SetData(settings, data, DataType.UInt16, it2);
                        }
                    }
                }
                ret = data.Array();
            }
            break;

            case 5:
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (MostRecentRequestsTable == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    GXCommon.SetObjectCount(MostRecentRequestsTable.Length, data);
                    foreach (string it in MostRecentRequestsTable)
                    {
                        GXCommon.SetData(settings, data, DataType.BitString, it);
                    }
                }
                ret = data.Array();
            }
            break;

            case 6:
                ret = LastOutcome;
                break;

            default:
                ret     = null;
                e.Error = ErrorCode.ReadWriteDenied;
                break;
            }
            return(ret);
        }
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, int index, int selector, object parameters)
 {
     if (index == 1)
     {
         return(this.LogicalName);
     }
     if (index == 2)
     {
         return(GXDLMSClient.ChangeType(ASCIIEncoding.ASCII.GetBytes(CalendarNameActive), DataType.OctetString));
     }
     if (index == 3)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (SeasonProfileActive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = SeasonProfileActive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSSeasonProfile it in SeasonProfileActive)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Name));
                 GXCommon.SetData(data, DataType.OctetString, it.Start);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.WeekName));
             }
         }
         return(data.Array());
     }
     if (index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (WeekProfileTableActive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = WeekProfileTableActive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSWeekProfile it in WeekProfileTableActive)
             {
                 data.SetUInt8((byte)DataType.Array);
                 data.SetUInt8(8);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Name));
                 GXCommon.SetData(data, DataType.UInt8, it.Monday);
                 GXCommon.SetData(data, DataType.UInt8, it.Tuesday);
                 GXCommon.SetData(data, DataType.UInt8, it.Wednesday);
                 GXCommon.SetData(data, DataType.UInt8, it.Thursday);
                 GXCommon.SetData(data, DataType.UInt8, it.Friday);
                 GXCommon.SetData(data, DataType.UInt8, it.Saturday);
                 GXCommon.SetData(data, DataType.UInt8, it.Sunday);
             }
         }
         return(data.Array());
     }
     if (index == 5)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (DayProfileTableActive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = DayProfileTableActive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSDayProfile it in DayProfileTableActive)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(2);
                 GXCommon.SetData(data, DataType.UInt8, it.DayId);
                 data.SetUInt8((byte)DataType.Array);
                 //Add count
                 GXCommon.SetObjectCount(it.DaySchedules.Length, data);
                 foreach (GXDLMSDayProfileAction action in it.DaySchedules)
                 {
                     data.SetUInt8((byte)DataType.Structure);
                     data.SetUInt8(3);
                     GXCommon.SetData(data, DataType.Time, action.StartTime);
                     GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(action.ScriptLogicalName));
                     GXCommon.SetData(data, DataType.UInt16, action.ScriptSelector);
                 }
             }
         }
         return(data.Array());
     }
     if (index == 6)
     {
         return(GXDLMSClient.ChangeType(ASCIIEncoding.ASCII.GetBytes(CalendarNamePassive), DataType.OctetString));
     }
     //
     if (index == 7)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (SeasonProfileActive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = SeasonProfileActive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSSeasonProfile it in SeasonProfileActive)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(3);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Name));
                 GXCommon.SetData(data, DataType.OctetString, it.Start);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.WeekName));
             }
         }
         return(data.Array());
     }
     if (index == 8)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (WeekProfileTableActive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = WeekProfileTableActive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSWeekProfile it in WeekProfileTableActive)
             {
                 data.SetUInt8((byte)DataType.Array);
                 data.SetUInt8(8);
                 GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Name));
                 GXCommon.SetData(data, DataType.UInt8, it.Monday);
                 GXCommon.SetData(data, DataType.UInt8, it.Tuesday);
                 GXCommon.SetData(data, DataType.UInt8, it.Wednesday);
                 GXCommon.SetData(data, DataType.UInt8, it.Thursday);
                 GXCommon.SetData(data, DataType.UInt8, it.Friday);
                 GXCommon.SetData(data, DataType.UInt8, it.Saturday);
                 GXCommon.SetData(data, DataType.UInt8, it.Sunday);
             }
         }
         return(data.Array());
     }
     if (index == 9)
     {
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         if (DayProfileTablePassive == null)
         {
             //Add count
             GXCommon.SetObjectCount(0, data);
         }
         else
         {
             int cnt = DayProfileTablePassive.Length;
             //Add count
             GXCommon.SetObjectCount(cnt, data);
             foreach (GXDLMSDayProfile it in DayProfileTablePassive)
             {
                 data.SetUInt8((byte)DataType.Structure);
                 data.SetUInt8(2);
                 GXCommon.SetData(data, DataType.UInt8, it.DayId);
                 data.SetUInt8((byte)DataType.Array);
                 //Add count
                 GXCommon.SetObjectCount(it.DaySchedules.Length, data);
                 foreach (GXDLMSDayProfileAction action in it.DaySchedules)
                 {
                     data.SetUInt8((byte)DataType.Structure);
                     data.SetUInt8(3);
                     GXCommon.SetData(data, DataType.Time, action.StartTime);
                     GXCommon.SetData(data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(action.ScriptLogicalName));
                     GXCommon.SetData(data, DataType.UInt16, action.ScriptSelector);
                 }
             }
         }
         return(data.Array());
     }
     if (index == 10)
     {
         return(Time);
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
示例#15
0
        ///<summary>
        /// Handle write request.
        ///</summary>
        ///<param name="Reply">
        /// Received data from the client.
        /// </param>
        ///<returns>
        /// Reply.
        ///</returns>
        private byte[][] HandleWriteRequest()
        {
            GXByteBuffer data = Reply.Data;
            short        type;
            object       value;
            // Get object count.
            IList <GXSNInfo> targets = new List <GXSNInfo>();
            int          cnt         = GXCommon.GetObjectCount(data);
            GXByteBuffer results     = new GXByteBuffer((ushort)cnt);

            for (int pos = 0; pos != cnt; ++pos)
            {
                type = data.GetUInt8();
                if (type == 2)
                {
                    int      sn   = data.GetUInt16();
                    GXSNInfo info = FindSNObject(sn);
                    targets.Add(info);
                    // If target is unknown.
                    if (info == null)
                    {
                        // Device reports a undefined object.
                        results.SetUInt8(ErrorCode.UndefinedObject);
                    }
                    else
                    {
                        results.SetUInt8(ErrorCode.Ok);
                    }
                }
                else
                {
                    // Device reports a HW error.
                    results.SetUInt8(ErrorCode.HardwareFault);
                }
            }
            // Get data count.
            cnt = GXCommon.GetObjectCount(data);
            GXDataInfo di = new GXDataInfo();

            for (int pos = 0; pos != cnt; ++pos)
            {
                if (results.GetUInt8(pos) == 0)
                {
                    // If object has found.
                    GXSNInfo target = targets[pos];
                    value = GXCommon.GetData(data, di);
                    if (value is byte[])
                    {
                        DataType dt = target.Item.GetDataType(target.Index);
                        if (dt != DataType.None && dt != DataType.OctetString)
                        {
                            value = GXDLMSClient.ChangeType((byte[])value, dt);
                        }
                    }
                    di.Clear();
                    AccessMode am = target.Item.GetAccess(target.Index);
                    // If write is denied.
                    if (am != AccessMode.Write && am != AccessMode.ReadWrite)
                    {
                        results.SetUInt8((byte)pos, (byte)ErrorCode.ReadWriteDenied);
                    }
                    else
                    {
                        ValueEventArgs e = new ValueEventArgs(target.Item, target.Index, 0, null);
                        e.Value = value;
                        Write(e);
                        if (!e.Handled)
                        {
                            (target.Item as IGXDLMSBase).SetValue(Settings, target.Index, value);
                        }
                    }
                }
            }
            GXByteBuffer bb = new GXByteBuffer((UInt16)(2 * cnt + 2));

            GXCommon.SetObjectCount(cnt, bb);
            byte ret;

            for (int pos = 0; pos != cnt; ++pos)
            {
                ret = results.GetUInt8(pos);
                // If meter returns error.
                if (ret != 0)
                {
                    bb.SetUInt8(1);
                }
                bb.SetUInt8(ret);
            }
            return(GXDLMS.SplitPdu(Settings, Command.WriteResponse, 1, bb, ErrorCode.Ok, DateTime.MinValue)[0]);
        }
示例#16
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         return(GXCommon.LogicalNameToBytes(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);
 }
示例#17
0
        private byte[][] HandleGetRequest()
        {
            ErrorCode    error = ErrorCode.Ok;
            GXByteBuffer data  = Reply.Data;
            GXByteBuffer bb    = new GXByteBuffer();
            short        type;
            int          index      = 0;
            object       parameters = null;

            // Get type.
            type = data.GetUInt8();
            // Get invoke ID and priority.
            data.GetUInt8();
            // GetRequest normal
            if (type == 1)
            {
                Settings.ResetBlockIndex();
                ServerReply.Index = 0;
                parameters        = null;
                // CI
                ObjectType ci = (ObjectType)data.GetUInt16();
                byte[]     ln = new byte[6];
                data.Get(ln);
                // Attribute Id
                int          attributeIndex = data.GetUInt8();
                GXDLMSObject obj            = Settings.Objects.FindByLN(ci, GXDLMSObject.ToLogicalName(ln));
                if (obj == null)
                {
                    obj = FindObject(ci, 0, GXDLMSObject.ToLogicalName(ln));
                }
                if (obj == null)
                {
                    // "Access Error : Device reports a undefined object."
                    error = ErrorCode.UndefinedObject;
                }
                else
                {
                    // AccessSelection
                    int selection = data.GetUInt8();
                    int selector  = 0;
                    if (selection != 0)
                    {
                        selector = data.GetUInt8();
                        GXDataInfo info = new GXDataInfo();
                        parameters = GXCommon.GetData(data, info);
                    }

                    ValueEventArgs e = new ValueEventArgs(obj, attributeIndex, 0, parameters);
                    Read(e);
                    object value;
                    if (e.Handled)
                    {
                        value = e.Value;
                    }
                    else
                    {
                        value = (obj as IGXDLMSBase).GetValue(Settings, attributeIndex, selector, parameters);
                    }
                    GXDLMS.AppedData(obj, attributeIndex, bb, value);
                }
                ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb, error, DateTime.MinValue);
            }
            else if (type == 2)
            {
                // Get request for next data block
                // Get block index.
                index = (int)data.GetUInt32();
                if (index != Settings.BlockIndex + 1)
                {
                    Debug.WriteLine("handleGetRequest failed. Invalid block number. " + Settings.BlockIndex + "/" + index);
                    ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb,
                                                                ErrorCode.DataBlockNumberInvalid, DateTime.MinValue);
                    index             = 0;
                    ServerReply.Index = index;
                }
                else
                {
                    Settings.IncreaseBlockIndex();
                    index             = ServerReply.Index + 1;
                    ServerReply.Index = index;
                }
            }
            else if (type == 3)
            {
                // Get request with a list.
                int cnt = GXCommon.GetObjectCount(data);
                GXCommon.SetObjectCount(cnt, bb);
                for (int pos = 0; pos != cnt; ++pos)
                {
                    ObjectType ci = (ObjectType)data.GetUInt16();
                    byte[]     ln = new byte[6];
                    data.Get(ln);
                    short attributeIndex = data.GetUInt8();

                    GXDLMSObject obj = Settings.Objects.FindByLN(ci, GXDLMSObject.ToLogicalName(ln));
                    if (obj == null)
                    {
                        obj = FindObject(ci, 0, GXDLMSObject.ToLogicalName(ln));
                    }
                    if (obj == null)
                    {
                        // "Access Error : Device reports a undefined object."
                        error = ErrorCode.UndefinedObject;
                    }
                    else
                    {
                        // AccessSelection
                        int selection = data.GetUInt8();
                        int selector  = 0;
                        if (selection != 0)
                        {
                            selector = data.GetUInt8();
                            GXDataInfo info = new GXDataInfo();
                            parameters = GXCommon.GetData(data, info);
                        }
                        try
                        {
                            ValueEventArgs e = new ValueEventArgs(obj, attributeIndex, 0, parameters);
                            Read(e);
                            object value;
                            if (e.Handled)
                            {
                                value = e.Value;
                            }
                            else
                            {
                                value = (obj as IGXDLMSBase).GetValue(Settings, attributeIndex, selector, parameters);
                            }
                            bb.SetUInt8(ErrorCode.Ok);
                            GXDLMS.AppedData(obj, attributeIndex, bb, value);
                        }
                        catch (Exception)
                        {
                            bb.SetUInt8(1);
                            bb.SetUInt8(ErrorCode.HardwareFault);
                        }
                    }
                }
                ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 3, bb, error, DateTime.MinValue);
            }
            else
            {
                Debug.WriteLine("handleGetRequest failed. Invalid command type.");
                Settings.ResetBlockIndex();
                // Access Error : Device reports a hardware fault.
                ServerReply.ReplyMessages = GXDLMS.SplitPdu(Settings, Command.GetResponse, 1, bb, ErrorCode.HardwareFault, DateTime.MinValue);
            }
            ServerReply.Index = index;
            return(ServerReply.ReplyMessages[index]);
        }
        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.Structure);
                data.SetUInt8(2);
                //LN
                if (Target != null)
                {
                    GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(Target.LogicalName));
                }
                else
                {
#pragma warning disable CS0618
                    GXCommon.SetData(settings, data, DataType.OctetString, GXCommon.LogicalNameToBytes(ExecutedScriptLogicalName));
#pragma warning restore CS0618
                }
                GXCommon.SetData(settings, data, DataType.UInt16, ExecutedScriptSelector);
                return(data.Array());
            }
            if (e.Index == 3)
            {
                return(this.Type);
            }
            if (e.Index == 4)
            {
                GXByteBuffer data = new GXByteBuffer();
                data.SetUInt8((byte)DataType.Array);
                if (ExecutionTime == null)
                {
                    GXCommon.SetObjectCount(0, data);
                }
                else
                {
                    GXCommon.SetObjectCount(ExecutionTime.Length, data);
                    foreach (GXDateTime it in ExecutionTime)
                    {
                        data.SetUInt8((byte)DataType.Structure);
                        //Count
                        data.SetUInt8((byte)2);
                        if (settings != null && settings.Standard == Standard.SaudiArabia)
                        {
                            //Time
                            GXCommon.SetData(settings, data, DataType.Time, new GXTime(it));
                            //Date
                            GXCommon.SetData(settings, data, DataType.Date, new GXDate(it));
                        }
                        else
                        {
                            //Time
                            GXCommon.SetData(settings, data, DataType.OctetString, new GXTime(it));
                            //Date
                            GXCommon.SetData(settings, data, DataType.OctetString, new GXDate(it));
                        }
                    }
                }
                return(data.Array());
            }
            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         = 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());
        }
 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);
 }
示例#21
0
 /// <summary>
 /// Set item count.
 /// </summary>
 /// <param name="count"></param>
 /// <param name="data"></param>
 internal static void SetObjectCount(int count, GXByteBuffer data)
 {
     GXCommon.SetObjectCount(count, data);
 }
示例#22
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 (IPAddress 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);
 }
示例#23
0
        object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            if (e.Index == 1)
            {
                return(GXCommon.LogicalNameToBytes(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, GXCommon.LogicalNameToBytes(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);
        }
示例#24
0
        private object[] GetDataList()
        {
            if (string.IsNullOrEmpty(Issuer))
            {
                throw new ArgumentNullException("Issuer is empty.");
            }
            if (string.IsNullOrEmpty(Subject))
            {
                throw new ArgumentNullException("Subject is empty.");
            }
            GXAsn1ObjectIdentifier a = new GXAsn1ObjectIdentifier(HashAlgorithmConverter.GetString(SignatureAlgorithm));
            GXAsn1Sequence         seq;
            GXAsn1Context          p = new GXAsn1Context();

            p.Add((sbyte)Version);
            GXAsn1Sequence s = new GXAsn1Sequence();
            GXAsn1Sequence s1;

            if (SubjectKeyIdentifier != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.SubjectKeyIdentifier)));
                GXByteBuffer bb = new GXByteBuffer();
                bb.SetUInt8(BerType.OctetString);
                GXCommon.SetObjectCount(SubjectKeyIdentifier.Length, bb);
                bb.Set(SubjectKeyIdentifier);
                s1.Add(bb.Array());
                s.Add(s1);
            }
            if (AuthorityKeyIdentifier != null || AuthorityCertIssuer != null || AuthorityCertificationSerialNumber != null)
            {
                s1 = new GXAsn1Sequence();
                s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.AuthorityKeyIdentifier)));
                s.Add(s1);
                GXAsn1Context s2 = new GXAsn1Context()
                {
                    Index = 3
                };
                GXAsn1Sequence c1 = new GXAsn1Sequence();
                if (AuthorityKeyIdentifier != null)
                {
                    GXAsn1Context c4 = new GXAsn1Context()
                    {
                        Constructed = false, Index = 0
                    };
                    c4.Add(AuthorityKeyIdentifier);
                    c1.Add(c4);
                    s1.Add(GXAsn1Converter.ToByteArray(c1));
                }
                if (AuthorityCertIssuer != null)
                {
                    GXAsn1Context c2 = new GXAsn1Context();
                    c2.Index = 1;
                    c1.Add(c2);
                    GXAsn1Context c3 = new GXAsn1Context()
                    {
                        Index = 4
                    };
                    c2.Add(c3);
                    c3.Add(GXAsn1Converter.EncodeSubject(AuthorityCertIssuer));
                    s2.Add(c1);
                }
                if (AuthorityCertificationSerialNumber != null)
                {
                    GXAsn1Context c4 = new GXAsn1Context()
                    {
                        Constructed = false, Index = 2
                    };
                    c4.Add(AuthorityCertificationSerialNumber);
                    c1.Add(c4);
                    s1.Add(GXAsn1Converter.ToByteArray(c1));
                }
            }
            // BasicConstraints
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.BasicConstraints)));
            seq = new GXAsn1Sequence();
            if (BasicConstraints)
            {
                //BasicConstraints is critical if it exists.
                s1.Add(BasicConstraints);
            }
            else if (KeyUsage == KeyUsage.None)
            {
                throw new Exception("Key usage not present.");
            }
            s1.Add(GXAsn1Converter.ToByteArray(seq));
            s.Add(s1);
            s1 = new GXAsn1Sequence();
            s1.Add(new GXAsn1ObjectIdentifier(X509CertificateTypeConverter.GetString(Enums.X509CertificateType.KeyUsage)));
            byte value    = 0;
            int  min      = 255;
            byte keyUsage = GXCommon.SwapBits((byte)KeyUsage);

            foreach (KeyUsage it in Enum.GetValues(typeof(KeyUsage)))
            {
                if ((((byte)it) & keyUsage) != 0)
                {
                    byte val = (byte)it;
                    value |= val;
                    if (val < min)
                    {
                        min = val;
                    }
                }
            }
            int ignore = 0;

            while ((min >>= 1) != 0)
            {
                ++ignore;
            }
            byte[] tmp = GXAsn1Converter.ToByteArray(new GXAsn1BitString(new byte[] { (byte)(ignore % 8), value }));
            s1.Add(tmp);
            s.Add(s1);
            GXAsn1Sequence valid = new GXAsn1Sequence();

            valid.Add(ValidFrom);
            valid.Add(ValidTo);
            GXAsn1ObjectIdentifier alg;

            if (PublicKey.Scheme == Ecdsa.Enums.Ecc.P256)
            {
                alg = new GXAsn1ObjectIdentifier("1.2.840.10045.3.1.7");
            }
            else
            {
                alg = new GXAsn1ObjectIdentifier("1.3.132.0.34");
            }
            object[] list;
            object[] tmp3 = new object[] { new GXAsn1ObjectIdentifier("1.2.840.10045.2.1"),
                                           alg };
            GXAsn1Context tmp4 = new GXAsn1Context();

            tmp4.Index = 3;
            tmp4.Add(s);
            object[] tmp2 = new object[] { tmp3, new GXAsn1BitString(PublicKey.RawValue, 0) };
            object[] p2;
            if (SignatureParameters == null)
            {
                p2 = new object[] { a };
            }
            else
            {
                p2 = new object[] { a, SignatureParameters };
            }
            list = new object[] { p, new GXAsn1Integer(SerialNumber.ToByteArray()), p2, GXAsn1Converter.EncodeSubject(Issuer), valid, GXAsn1Converter.EncodeSubject(Subject), tmp2, tmp4 };
            return(list);
        }
        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);
        }
 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);
 }
        /// <summary>
        /// Handle get request with list command.
        /// </summary>
        /// <param name="data">Received data.</param>
        private static void GetRequestWithList(GXDLMSSettings settings, byte invokeID, GXDLMSServer server, GXByteBuffer data, GXByteBuffer replyData, GXDLMSTranslatorStructure xml)
        {
            ValueEventArgs e;
            GXByteBuffer   bb = new GXByteBuffer();
            int            pos;
            int            cnt = GXCommon.GetObjectCount(data);

            GXCommon.SetObjectCount(cnt, bb);
            List <ValueEventArgs> list = new List <ValueEventArgs>();

            if (xml != null)
            {
                xml.AppendStartTag(TranslatorTags.AttributeDescriptorList, "Qty", xml.IntegerToHex(cnt, 2));
            }
            try
            {
                for (pos = 0; pos != cnt; ++pos)
                {
                    ObjectType ci = (ObjectType)data.GetUInt16();
                    byte[]     ln = new byte[6];
                    data.Get(ln);
                    short attributeIndex = data.GetUInt8();
                    // AccessSelection
                    int    selection  = data.GetUInt8();
                    int    selector   = 0;
                    object parameters = null;
                    if (selection != 0)
                    {
                        selector = data.GetUInt8();
                        GXDataInfo info = new GXDataInfo();
                        parameters = GXCommon.GetData(settings, data, info);
                    }
                    if (xml != null)
                    {
                        xml.AppendStartTag(TranslatorTags.AttributeDescriptorWithSelection);
                        xml.AppendStartTag(TranslatorTags.AttributeDescriptor);
                        xml.AppendComment(ci.ToString());
                        xml.AppendLine(TranslatorTags.ClassId, "Value", xml.IntegerToHex((int)ci, 4));
                        xml.AppendComment(GXCommon.ToLogicalName(ln));
                        xml.AppendLine(TranslatorTags.InstanceId, "Value", GXCommon.ToHex(ln, false));
                        xml.AppendLine(TranslatorTags.AttributeId, "Value", xml.IntegerToHex(attributeIndex, 2));
                        xml.AppendEndTag(TranslatorTags.AttributeDescriptor);
                        xml.AppendEndTag(TranslatorTags.AttributeDescriptorWithSelection);
                    }
                    else
                    {
                        GXDLMSObject obj = settings.Objects.FindByLN(ci, GXCommon.ToLogicalName(ln));
                        if (obj == null)
                        {
                            obj = server.NotifyFindObject(ci, 0, GXCommon.ToLogicalName(ln));
                        }
                        if (obj == null)
                        {
                            // "Access Error : Device reports a undefined object."
                            e       = new ValueEventArgs(server, obj, attributeIndex, 0, 0);
                            e.Error = ErrorCode.UndefinedObject;
                            list.Add(e);
                        }
                        else
                        {
                            ValueEventArgs arg = new ValueEventArgs(server, obj, attributeIndex, selector, parameters);
                            arg.InvokeId = invokeID;
                            if (server.NotifyGetAttributeAccess(arg) == AccessMode.NoAccess)
                            {
                                //Read Write denied.
                                arg.Error = ErrorCode.ReadWriteDenied;
                                list.Add(arg);
                            }
                            else
                            {
                                list.Add(arg);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (xml == null)
                {
                    throw ex;
                }
            }
            if (xml != null)
            {
                xml.AppendEndTag(TranslatorTags.AttributeDescriptorList);
                return;
            }

            server.NotifyRead(list.ToArray());
            object value;

            pos = 0;
            foreach (ValueEventArgs it in list)
            {
                try
                {
                    if (it.Handled)
                    {
                        value = it.Value;
                    }
                    else
                    {
                        value = (it.Target as IGXDLMSBase).GetValue(settings, it);
                    }
                    bb.SetUInt8(it.Error);
                    if (it.ByteArray)
                    {
                        bb.Set((byte[])value);
                    }
                    else
                    {
                        GXDLMS.AppendData(settings, it.Target, it.Index, bb, value);
                    }
                    invokeID = (byte)it.InvokeId;
                }
                catch (Exception)
                {
                    bb.SetUInt8((byte)ErrorCode.HardwareFault);
                }
                if (settings.Index != settings.Count)
                {
                    server.transaction = new GXDLMSLongTransaction(list.ToArray(), Command.GetRequest, null);
                }
                ++pos;
            }
            server.NotifyPostRead(list.ToArray());
            GXDLMSLNParameters p = new GXDLMSLNParameters(settings, invokeID, Command.GetResponse, 3, null, bb, 0xFF);

            GXDLMS.GetLNPdu(p, replyData);
        }
        /// <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)
            {
                if (it.Value.AttributeIndex == 0)
                {
                    types[pos] = DataType.Structure;
                }
                else
                {
                    types[pos] = it.Key.GetDataType(it.Value.AttributeIndex);
                }
                ++pos;
            }
            UInt16 columnStart = 1, columnEnd = 0;

            if (e.Selector == 2)
            {
                List <object> arr = null;
                if (e.Parameters is List <object> )
                {
                    arr = (List <object>)e.Parameters;
                }
                else if (e.Parameters != null)
                {
                    arr = new List <object>((object[])e.Parameters);
                }
                columnStart = (UInt16)arr[2];
                columnEnd   = (UInt16)arr[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;
                        }
                        if (value is GXDLMSObject)
                        {
                            GXCommon.SetData(settings, data, tp, (value as GXDLMSObject).GetValues());
                        }
                        else
                        {
                            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());
        }
示例#29
0
        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);
        }
        /// <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);
        }