Exemplo n.º 1
0
        /// <summary>
        /// Initialize connection to the meter.
        /// </summary>
        public void InitializeConnection()
        {
            UpdateFrameCounter();
            InitializeOpticalHead();
            GXReplyData reply = new GXReplyData();

            byte[] data;
            data = Client.SNRMRequest();
            if (data != null)
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true));
                }
                ReadDataBlock(data, reply);
                if (Trace == TraceLevel.Verbose)
                {
                    Console.WriteLine("Parsing UA reply." + reply.ToString());
                }
                //Has server accepted client.
                Client.ParseUAResponse(reply.Data);
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Parsing UA reply succeeded.");
                }
            }
            //Generate AARQ request.
            //Split requests to multiple packets if needed.
            //If password is used all data might not fit to one packet.
            foreach (byte[] it in Client.AARQRequest())
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send AARQ request", GXCommon.ToHex(it, true));
                }
                reply.Clear();
                ReadDataBlock(it, reply);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply" + reply.ToString());
            }
            //Parse reply.
            Client.ParseAAREResponse(reply.Data);
            reply.Clear();
            //Get challenge Is HLS authentication is used.
            if (Client.IsAuthenticationRequired)
            {
                foreach (byte[] it in Client.GetApplicationAssociationRequest())
                {
                    reply.Clear();
                    ReadDataBlock(it, reply);
                }
                Client.ParseApplicationAssociationResponse(reply.Data);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply succeeded.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send SNRM Request to the meter.
        /// </summary>
        public void SNRMRequest()
        {
            if (UseIec)
            {
                StartWithIec();
            }
            GXReplyData reply = new GXReplyData();

            byte[] data;
            data = Client.SNRMRequest();
            if (data != null)
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true));
                }
                ReadDataBlock(data, reply);
                if (Trace == TraceLevel.Verbose)
                {
                    Console.WriteLine("Parsing UA reply." + reply.ToString());
                }
                //Has server accepted client.
                Client.ParseUAResponse(reply.Data);
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Parsing UA reply succeeded.");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send SNRM Request to the meter.
        /// </summary>
        public void SNRMRequest()
        {
            GXReplyData reply = new GXReplyData();

            byte[] data;
            data = Client.SNRMRequest();
            if (data != null)
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true));
                    GXDLMSTranslator t = new GXDLMSTranslator(TranslatorOutputType.SimpleXml);
                    Console.WriteLine(t.MessageToXml(data));
                }
                ReadDataBlock(data, reply);
                if (Trace == TraceLevel.Verbose)
                {
                    Console.WriteLine("Parsing UA reply." + reply.ToString());
                }
                //Has server accepted client.
                Client.ParseUAResponse(reply.Data);
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Parsing UA reply succeeded.");
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initialize connection to the meter.
        /// </summary>
        public void InitializeConnection()
        {
            Console.WriteLine("Standard: " + Client.Standard);
            if (Client.Ciphering.Security != (byte)Security.None)
            {
                Console.WriteLine("Security: " + Client.Ciphering.Security);
                Console.WriteLine("System title: " + GXCommon.ToHex(Client.Ciphering.SystemTitle, true));
                Console.WriteLine("Authentication key: " + GXCommon.ToHex(Client.Ciphering.AuthenticationKey, true));
                Console.WriteLine("Block cipher key " + GXCommon.ToHex(Client.Ciphering.BlockCipherKey, true));
                if (Client.Ciphering.DedicatedKey != null)
                {
                    Console.WriteLine("Dedicated key: " + GXCommon.ToHex(Client.Ciphering.DedicatedKey, true));
                }
            }
            UpdateFrameCounter();
            InitializeOpticalHead();
            GXReplyData reply = new GXReplyData();

            SNRMRequest();
            //Generate AARQ request.
            //Split requests to multiple packets if needed.
            //If password is used all data might not fit to one packet.
            foreach (byte[] it in Client.AARQRequest())
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send AARQ request", GXCommon.ToHex(it, true));
                }
                reply.Clear();
                ReadDataBlock(it, reply);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply" + reply.ToString());
            }
            //Parse reply.
            Client.ParseAAREResponse(reply.Data);
            reply.Clear();
            //Get challenge Is HLS authentication is used.
            if (Client.IsAuthenticationRequired)
            {
                foreach (byte[] it in Client.GetApplicationAssociationRequest())
                {
                    reply.Clear();
                    ReadDataBlock(it, reply);
                }
                Client.ParseApplicationAssociationResponse(reply.Data);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply succeeded.");
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Handle meter reply.
 /// </summary>
 /// <param name="item">Command to sent.</param>
 /// <param name="reply">Received reply.</param>
 static void HandleReply(GXDLMSXmlPdu item, GXReplyData reply)
 {
     if (reply.Value is byte[])
     {
         Console.WriteLine(GXCommon.ToHex((byte[])reply.Value, true));
         Console.WriteLine(reply.ToString());
     }
     else
     {
         Console.WriteLine(Convert.ToString(reply));
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Send AARQ Request to the meter.
        /// </summary>
        public void AarqRequest()
        {
            GXReplyData reply = new GXReplyData();

            //Generate AARQ request.
            //Split requests to multiple packets if needed.
            //If password is used all data might not fit to one packet.
            foreach (byte[] it in Client.AARQRequest())
            {
                if (Trace > TraceLevel.Info)
                {
                    Console.WriteLine("Send AARQ request" + GXCommon.ToHex(it, true));
                    GXDLMSTranslator t = new GXDLMSTranslator(TranslatorOutputType.SimpleXml);
                    Console.WriteLine(t.MessageToXml(it));
                }
                reply.Clear();
                ReadDataBlock(it, reply);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply\r\n" + reply.ToString());
            }
            //Parse reply.
            Client.ParseAAREResponse(reply.Data);
            reply.Clear();
            //Get challenge Is HLS authentication is used.
            if (Client.IsAuthenticationRequired)
            {
                foreach (byte[] it in Client.GetApplicationAssociationRequest())
                {
                    reply.Clear();
                    ReadDataBlock(it, reply);
                }
                Client.ParseApplicationAssociationResponse(reply.Data);
            }
            if (Trace > TraceLevel.Info)
            {
                Console.WriteLine("Parsing AARE reply succeeded.");
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Read Invocation counter (frame counter) from the meter and update it.
 /// </summary>
 private void UpdateFrameCounter()
 {
     //Read frame counter if GeneralProtection is used.
     if (!string.IsNullOrEmpty(InvocationCounter) && Client.Ciphering != null && Client.Ciphering.Security != (byte)Security.None)
     {
         InitializeOpticalHead();
         byte[]      data;
         GXReplyData reply = new GXReplyData();
         Client.ProposedConformance |= Conformance.GeneralProtection;
         int            add       = Client.ClientAddress;
         Authentication auth      = Client.Authentication;
         Security       security  = Client.Ciphering.Security;
         byte[]         challenge = Client.CtoSChallenge;
         try
         {
             Client.ClientAddress      = 16;
             Client.Authentication     = Authentication.None;
             Client.Ciphering.Security = (byte)Security.None;
             data = Client.SNRMRequest();
             if (data != null)
             {
                 if (Trace > TraceLevel.Info)
                 {
                     Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true));
                 }
                 ReadDataBlock(data, reply);
                 if (Trace == TraceLevel.Verbose)
                 {
                     Console.WriteLine("Parsing UA reply." + reply.ToString());
                 }
                 //Has server accepted client.
                 Client.ParseUAResponse(reply.Data);
                 if (Trace > TraceLevel.Info)
                 {
                     Console.WriteLine("Parsing UA reply succeeded.");
                 }
             }
             //Generate AARQ request.
             //Split requests to multiple packets if needed.
             //If password is used all data might not fit to one packet.
             foreach (byte[] it in Client.AARQRequest())
             {
                 if (Trace > TraceLevel.Info)
                 {
                     Console.WriteLine("Send AARQ request", GXCommon.ToHex(it, true));
                 }
                 reply.Clear();
                 ReadDataBlock(it, reply);
             }
             if (Trace > TraceLevel.Info)
             {
                 Console.WriteLine("Parsing AARE reply" + reply.ToString());
             }
             try
             {
                 //Parse reply.
                 Client.ParseAAREResponse(reply.Data);
                 reply.Clear();
                 GXDLMSData d = new GXDLMSData(InvocationCounter);
                 Read(d, 2);
                 Client.Ciphering.InvocationCounter = 1 + Convert.ToUInt32(d.Value);
                 Console.WriteLine("Invocation counter: " + Convert.ToString(Client.Ciphering.InvocationCounter));
                 reply.Clear();
                 Disconnect();
             }
             catch (Exception Ex)
             {
                 Disconnect();
                 throw Ex;
             }
         }
         finally
         {
             Client.ClientAddress      = add;
             Client.Authentication     = auth;
             Client.Ciphering.Security = security;
             Client.CtoSChallenge      = challenge;
         }
     }
 }
Exemplo n.º 8
0
        public void InitializeConnection(GXManufacturer man)
        {
            Manufacturer = man;
            UpdateManufactureSettings(man.Identification);
            if (Media is GXSerial)
            {
                Console.WriteLine("Initializing serial connection.");
                InitSerial();
            }
            else if (Media is GXNet)
            {
                Console.WriteLine("Initializing Network connection.");
                InitNet();
                //Some Electricity meters need some time before first message can be send.
                System.Threading.Thread.Sleep(500);
            }
            else
            {
                throw new Exception("Unknown media type.");
            }
            GXReplyData reply = new GXReplyData();

            byte[] data;
            data = Client.SNRMRequest();
            if (data != null)
            {
                if (Trace)
                {
                    Console.WriteLine("Send SNRM request." + GXCommon.ToHex(data, true));
                }
                ReadDLMSPacket(data, reply);
                if (Trace)
                {
                    Console.WriteLine("Parsing UA reply." + reply.ToString());
                }
                //Has server accepted client.
                Client.ParseUAResponse(reply.Data);
                Console.WriteLine("Parsing UA reply succeeded.");
            }
            //Generate AARQ request.
            //Split requests to multiple packets if needed.
            //If password is used all data might not fit to one packet.
            reply.Clear();
            ReadDataBlock(Client.AARQRequest(), reply);
            try
            {
                //Parse reply.
                Client.ParseAAREResponse(reply.Data);
                reply.Clear();
            }
            catch (Exception Ex)
            {
                reply.Clear();
                ReadDLMSPacket(Client.DisconnectRequest(), reply);
                throw Ex;
            }

            //Get challenge Is HLS authentication is used.
            if (Client.IsAuthenticationRequired)
            {
                foreach (byte[] it in Client.GetApplicationAssociationRequest())
                {
                    reply.Clear();
                    ReadDLMSPacket(it, reply);
                }
                Client.ParseApplicationAssociationResponse(reply.Data);
            }
            Console.WriteLine("Parsing AARE reply succeeded.");
        }
Exemplo n.º 9
0
        private static void Execute(GXSettings settings, GXDLMSReader reader, string name, List <GXDLMSXmlPdu> actions, GXOutput output)
        {
            GXReplyData reply = new GXReplyData();
            string      ln    = null;
            int         index = 0;
            ObjectType  ot    = ObjectType.None;
            List <KeyValuePair <ObjectType, int> > succeeded = new List <KeyValuePair <ObjectType, int> >();

            foreach (GXDLMSXmlPdu it in actions)
            {
                if (it.Command == Command.Snrm && settings.client.InterfaceType == InterfaceType.WRAPPER)
                {
                    continue;
                }
                if (it.Command == Command.DisconnectRequest && settings.client.InterfaceType == InterfaceType.WRAPPER)
                {
                    break;
                }
                //Send
                if (it.IsRequest())
                {
                    XmlNode i = it.XmlNode.SelectNodes("GetRequestNormal")[0];
                    if (i == null)
                    {
                        ot    = ObjectType.None;
                        index = 0;
                        ln    = null;
                    }
                    else
                    {
                        ot    = (ObjectType)int.Parse(i.SelectNodes("AttributeDescriptor/ClassId")[0].Attributes["Value"].Value);
                        index = int.Parse(i.SelectNodes("AttributeDescriptor/AttributeId")[0].Attributes["Value"].Value);
                        ln    = (i.SelectNodes("AttributeDescriptor/InstanceId")[0].Attributes["Value"].Value);
                        ln    = GetLogicalName(ln);
                    }
                    reply.Clear();
                    if (settings.trace > TraceLevel.Warning)
                    {
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine(it.ToString());
                    }
                    byte[][] tmp = settings.client.PduToMessages(it);
                    reader.ReadDataBlock(tmp, reply);
                }
                else if (reply.Data.Size != 0)
                {
                    List <string> list = it.Compare(reply.ToString());
                    if (list.Count != 0)
                    {
                        if (ot == ObjectType.None)
                        {
                            foreach (string err in list)
                            {
                                output.Errors.Add(err);
                            }
                        }
                        else
                        {
                            //   output.Errors.Add("<div class=\"description\">\r\n");
                            output.Errors.Add("<a target=\"_blank\" href=http://www.gurux.fi/Gurux.DLMS.Objects.GXDLMS" + ot + ">" + ot + "</a> " + ln + " attribute " + index + " is <div class=\"tooltip\">invalid.");
                            output.Errors.Add("<span class=\"tooltiptext\">");
                            output.Errors.Add("Expected:</b><br/>");
                            output.Errors.Add(it.PduAsXml.Replace("<", "&lt;").Replace(">", "&gt;").Replace("\r\n", "<br/>"));
                            output.Errors.Add("<br/><b>Actual:</b><br/>");
                            output.Errors.Add(reply.ToString().Replace("<", "&lt;").Replace(">", "&gt;").Replace("\r\n", "<br/>"));
                            output.Errors.Add("</span></div>");
                            //   output.Errors.Add("</div>");
                        }
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine("Test" + name + "failed. Invalid reply: " + string.Join("\n", list.ToArray()));
                    }
                    else
                    {
                        if (ot == ObjectType.None)
                        {
                            output.Info.Add(name + " succeeded.");
                        }
                        else
                        {
                            succeeded.Add(new KeyValuePair <ObjectType, int>(ot, index));
                        }
                    }
                    if (settings.trace > TraceLevel.Warning)
                    {
                        Console.WriteLine("------------------------------------------------------------");
                        Console.WriteLine(reply.ToString());
                    }
                }
            }
            if (succeeded.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class=\"tooltip\">" + ln);
                sb.Append("<span class=\"tooltiptext\">");
                foreach (var it in succeeded)
                {
                    sb.Append("Index: " + it.Value + "<br/>");
                }
                sb.Append("</span></div>");
                sb.Append("&nbsp;" + settings.converter.GetDescription(ln, succeeded[0].Key)[0] + "&nbsp;" + "<a target=\"_blank\" href=http://www.gurux.fi/Gurux.DLMS.Objects.GXDLMS" + ot + ">" + ot + "</a>.");
                output.Info.Add(sb.ToString());
            }
        }