Exemplo n.º 1
0
        public override EnIPNetworkStatus ReadDataFromNetwork()
        {
            byte[]            DataPath = EnIPPath.GetPath(myInstance.myClass.Id, myInstance.Id, Id);
            EnIPNetworkStatus ret      = ReadDataFromNetwork(DataPath, CIPServiceCodes.GetAttributeSingle);

            if (ret == EnIPNetworkStatus.OnLine)
            {
                CIPObjectLibrary classid = (CIPObjectLibrary)myInstance.myClass.Id;
                try
                {
                    if (DecodedMembers == null) // No decoder
                    {
                        if (myInstance.DecodedMembers == null)
                        {
                            myInstance.AttachDecoderClass();
                        }

                        DecodedMembers = myInstance.DecodedMembers; // get the same object as the associated Instance
                    }
                    int Idx = 0;
                    DecodedMembers.DecodeAttr(Id, ref Idx, RawData);
                }
                catch { }
            }
            return(ret);
        }
Exemplo n.º 2
0
        public EnIPNetworkStatus ForwardOpen(EnIPAttribut Config, EnIPAttribut O2T, EnIPAttribut T2O, out ForwardClose_Packet ClosePacket, ForwardOpen_Config conf, bool WriteConfig = false)
        {
            ClosePacket = null;

            byte[] DataPath = GetForwardOpenPath(Config, O2T, T2O);

            if ((WriteConfig == true) && (Config != null)) // Add data segment
            {
                DataPath = EnIPPath.AddDataSegment(DataPath, Config.RawData);

                /*
                 * byte[] FinaleFrame = new byte[DataPath.Length + 2 + Config.RawData.Length];
                 * Array.Copy(DataPath, FinaleFrame, DataPath.Length);
                 * FinaleFrame[DataPath.Length] = 0x80;
                 * FinaleFrame[DataPath.Length + 1] = (byte)(Config.RawData.Length / 2); // Certainly the lenght is always even !!!
                 * Array.Copy(Config.RawData, 0, FinaleFrame, DataPath.Length + 2, Config.RawData.Length);
                 * DataPath = FinaleFrame;
                 */
            }

            ForwardOpen_Packet FwPkt = new ForwardOpen_Packet(DataPath, conf);

            int Offset = 0;
            int Lenght = 0;

            byte[] packet;

            EnIPNetworkStatus Status;

            if (FwPkt.IsLargeForwardOpen)
            {
                Status = SendUCMM_RR_Packet(EnIPPath.GetPath(6, 1), CIPServiceCodes.LargeForwardOpen, FwPkt.toByteArray(), ref Offset, ref Lenght, out packet);
            }
            else
            {
                Status = SendUCMM_RR_Packet(EnIPPath.GetPath(6, 1), CIPServiceCodes.ForwardOpen, FwPkt.toByteArray(), ref Offset, ref Lenght, out packet);
            }

            if (Status == EnIPNetworkStatus.OnLine)
            {
                if (O2T != null)
                {
                    O2T.O2T_ConnectionId = BitConverter.ToUInt32(packet, Offset);                          // badly made
                    O2T.SequenceItem     = new SequencedAddressItem(O2T.O2T_ConnectionId, 0, O2T.RawData); // ready to send
                }

                if (T2O != null)
                {
                    T2O.Class1Enrolment();
                    T2O.T2O_ConnectionId = BitConverter.ToUInt32(packet, Offset + 4);
                }
                ClosePacket = new ForwardClose_Packet(FwPkt, T2O);
            }

            return(Status);
        }
Exemplo n.º 3
0
        public EnIPNetworkStatus GetClassInstanceAttributList()
        {
            byte[] DataPath = EnIPPath.GetPath(myClass.Id, Id, null);

            int Offset = 0;
            int Lenght = 0;

            byte[] packet;

            Status = RemoteDevice.GetClassInstanceAttribut_Data(DataPath, CIPServiceCodes.GetAttributeList, ref Offset, ref Lenght, out packet);

            return(Status);
        }
Exemplo n.º 4
0
        public EnIPNetworkStatus ForwardClose(ForwardClose_Packet ClosePacket)
        {
            int Offset = 0;
            int Lenght = 0;

            byte[] packet;

            if (ClosePacket.T2O != null)
            {
                ClosePacket.T2O.Class1UnEnrolment();
            }

            return(SendUCMM_RR_Packet(EnIPPath.GetPath(6, 1), CIPServiceCodes.ForwardClose, ClosePacket.toByteArray(), ref Offset, ref Lenght, out packet));
        }
Exemplo n.º 5
0
        public List <EnIPClass> GetObjectList()
        {
            SupportedClassLists.Clear();

            if (autoRegisterSession)
            {
                RegisterSession();
            }
            if (SessionHandle == 0)
            {
                return(null);
            }

            // Class 2, Instance 1, Attribut 1
            byte[] MessageRouterObjectList = EnIPPath.GetPath("2.1.1");

            int Lenght = 0;
            int Offset = 0;

            if (GetClassInstanceAttribut_Data(MessageRouterObjectList, CIPServiceCodes.GetAttributeSingle, ref Offset, ref Lenght, out packet) == EnIPNetworkStatus.OnLine)
            {
                ushort NbClasses = BitConverter.ToUInt16(packet, Offset);
                Offset += 2;
                for (int i = 0; i < NbClasses; i++)
                {
                    SupportedClassLists.Add(new EnIPClass(this, BitConverter.ToUInt16(packet, Offset)));
                    Offset += 2;
                }
            }

            if (SupportedClassLists.Count == 0) // service not supported : add basic class, but some could be not present
            {
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.Identity));
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.MessageRouter));
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.Assembly));
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.TCPIPInterface));
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.EtherNetLink));
                SupportedClassLists.Add(new EnIPClass(this, (ushort)CIPObjectLibrary.ConnectionManager));
            }

            return(SupportedClassLists);
        }
Exemplo n.º 6
0
        // Never tested, certainly not like this
        public bool CreateRemoteInstance()
        {
            byte[] ClassDataPath = EnIPPath.GetPath(myClass.Id, Id, null);

            int Offset = 0;
            int Lenght = 0;

            byte[] packet;

            Status = RemoteDevice.SendUCMM_RR_Packet(ClassDataPath, CIPServiceCodes.Create, RawData, ref Offset, ref Lenght, out packet);

            if (Status == EnIPNetworkStatus.OnLine)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        public override EnIPNetworkStatus ReadDataFromNetwork()
        {
            byte[]            DataPath = EnIPPath.GetPath(myClass.Id, Id, null);
            EnIPNetworkStatus ret      = ReadDataFromNetwork(DataPath, CIPServiceCodes.GetAttributesAll);

            if (ret == EnIPNetworkStatus.OnLine)
            {
                if (DecodedMembers == null)
                {
                    AttachDecoderClass();
                }

                try
                {
                    DecodedMembers.SetRawBytes(RawData);
                }
                catch { }
            }
            return(ret);
        }
Exemplo n.º 8
0
        // Gives a compressed Path with a list of Attributs
        private byte[] GetForwardOpenPath(EnIPAttribut Config, EnIPAttribut O2T, EnIPAttribut T2O)
        {
            byte[] ConstructDataPath = new byte[20];
            int    offset            = 0;

            ushort?Cid, Aid;

            EnIPAttribut previousAtt = null;

            EnIPAttribut[] Atts = new EnIPAttribut[] { Config, O2T, T2O };

            foreach (EnIPAttribut att in Atts)
            {
                if (att != null)
                {
                    byte[] DataPath;

                    Cid = att.myInstance.myClass.Id;
                    if ((previousAtt != null) && (Cid == previousAtt.myInstance.myClass.Id))
                    {
                        Cid = null;
                    }

                    Aid = ((att.Id == 3) && (att.myInstance.myClass.Id == 4)) ? null : (ushort?)att.Id;

                    DataPath = EnIPPath.GetPath(Cid, att.myInstance.Id, Aid, att != Config);
                    Array.Copy(DataPath, 0, ConstructDataPath, offset, DataPath.Length);
                    offset += DataPath.Length;

                    previousAtt = att;
                }
            }

            byte[] FinalPath = new byte[offset];
            Array.Copy(ConstructDataPath, 0, FinalPath, 0, offset);
            return(FinalPath);

            // return something like  0x20, 0x04, 0x24, 0x80, 0x2C, 0x66, 0x2C, 0x65
        }
Exemplo n.º 9
0
 public override EnIPNetworkStatus WriteDataToNetwork()
 {
     byte[] DataPath = EnIPPath.GetPath(myInstance.myClass.Id, myInstance.Id, Id);
     return(WriteDataToNetwork(DataPath, CIPServiceCodes.SetAttributeSingle));
 }
Exemplo n.º 10
0
        public override EnIPNetworkStatus ReadDataFromNetwork()
        {
            // Read all class static attributes
            byte[]            ClassDataPath = EnIPPath.GetPath(Id, 0, null);
            EnIPNetworkStatus ret           = ReadDataFromNetwork(ClassDataPath, CIPServiceCodes.GetAttributesAll);

            // If rejected try to read all attributes one by one
            if (ret == EnIPNetworkStatus.OnLineReadRejected)
            {
                MemoryStream rawbuffer = new MemoryStream();

                ushort AttId = 1; // first static attribut number

                do
                {
                    ClassDataPath = EnIPPath.GetPath(Id, 0, AttId);
                    ret           = ReadDataFromNetwork(ClassDataPath, CIPServiceCodes.GetAttributeSingle);

                    // push the buffer into the data stream
                    if (ret == EnIPNetworkStatus.OnLine)
                    {
                        rawbuffer.Write(RawData, 0, RawData.Length);
                    }
                    AttId++;
                }while (ret == EnIPNetworkStatus.OnLine);

                // yes OK like this, pull the data out of the stream into the RawData
                if (rawbuffer.Length != 0)
                {
                    Status  = ret = EnIPNetworkStatus.OnLine; // all is OK even if the last request is (always) rejected
                    RawData = rawbuffer.ToArray();
                }
            }

            if (ret == EnIPNetworkStatus.OnLine)
            {
                CIPObjectLibrary classid = (CIPObjectLibrary)Id;
                try
                {
                    if (DecodedMembers == null)
                    {
                        try
                        {
                            if (DecoderClass == null)
                            {
                                // try to create the associated class object
                                var o = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, "System.Net.EnIPStack.ObjectsLibrary.CIP_" + classid.ToString() + "_class");
                                DecodedMembers = (CIPObject)o.Unwrap();
                            }
                            else
                            {
                                var o = Activator.CreateInstance(DecoderClass);
                                DecodedMembers = (CIPObject)o;
                            }
                        }
                        catch
                        {
                            // echec, get the base class as described in Volume 1, §4-4.1 Class Attributes
                            DecodedMembers = new CIPObjectBaseClass(classid.ToString());
                        }
                    }
                    DecodedMembers.SetRawBytes(RawData);
                }
                catch { }
            }
            return(ret);
        }
Exemplo n.º 11
0
        public EnIPNetworkStatus SendUCMM_RR_Packet(byte[] DataPath, CIPServiceCodes Service, byte[] data, ref int Offset, ref int Lenght, out byte[] packet)
        {
            packet = this.packet;

            if (autoRegisterSession)
            {
                RegisterSession();
            }
            if (SessionHandle == 0)
            {
                return(EnIPNetworkStatus.OffLine);
            }

            try
            {
                UCMM_RR_Packet       m = new UCMM_RR_Packet(Service, true, DataPath, data);
                Encapsulation_Packet p = new Encapsulation_Packet(EncapsulationCommands.SendRRData, SessionHandle, m.toByteArray());

                Encapsulation_Packet rep;
                Offset = 0;

                lock (LockTransaction)
                    Lenght = Tcpclient.SendReceive(p, out rep, out Offset, ref packet);

                String ErrorMsg = "TCP mistake";

                if (Lenght > 24)
                {
                    if ((rep.IsOK) && (rep.Command == EncapsulationCommands.SendRRData))
                    {
                        m = new UCMM_RR_Packet(packet, ref Offset, Lenght);
                        if ((m.IsOK) && (m.IsService(Service)))
                        {
                            // all is OK, and Offset is ready set at the beginning of data[]
                            return(EnIPNetworkStatus.OnLine);
                        }
                        else
                        {
                            ErrorMsg = m.GeneralStatus.ToString();
                        }
                    }
                    else
                    {
                        ErrorMsg = rep.Status.ToString();
                    }
                }

                Trace.WriteLine(Service.ToString() + " : " + ErrorMsg + " - Node " + EnIPPath.GetPath(DataPath) + " - Endpoint " + ep.ToString());

                if (ErrorMsg == "TCP mistake")
                {
                    return(EnIPNetworkStatus.OffLine);
                }

                if (Service == CIPServiceCodes.SetAttributeSingle)
                {
                    return(EnIPNetworkStatus.OnLineWriteRejected);
                }
                else
                {
                    return(EnIPNetworkStatus.OnLineReadRejected);
                }
            }
            catch
            {
                Trace.TraceWarning("Error while sending reques to endpoint " + ep.ToString());
                return(EnIPNetworkStatus.OffLine);
            }
        }