Exemple #1
0
        /// <summary>
        /// Creates an Encaps Packet from a raw byte stream
        /// </summary>
        /// <param name="RawPacket">Raw bytes</param>
        /// <returns>Encaps Packet</returns>
        public static EncapsPacket CreatePacket(byte[] RawPacket)
        {
            EncapsPacket packet = new EncapsPacket();
            int          temp   = 0;

            packet.Expand(RawPacket, 0, out temp);

            return(packet);
        }
Exemple #2
0
        /// <summary>
        /// Creates a ListInterfaces packet
        /// </summary>
        /// <returns>ListInterfaces EncapsPacket</returns>
        public static EncapsPacket CreateListInterfaces()
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.ListInterfaces;
            packet.Length        = 0;
            packet.SessionHandle = 0;
            packet.Status        = 0;
            packet.SenderContext = new byte[8];
            packet.OptionsFlags  = 0;

            return(packet);
        }
Exemple #3
0
        /// <summary>
        /// Creates an UnRegisterSession packet
        /// </summary>
        /// <param name="SessionHandle">Session handle given during the RegisterSession process</param>
        /// <param name="SenderContext">Sender Context, array of 8 bytes</param>
        /// <returns>UnRegisterSession EncapsPacket</returns>
        public static EncapsPacket CreateUnRegisterSession(uint SessionHandle, byte[] SenderContext)
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.UnRegisterSession;
            packet.Length        = 0;
            packet.SessionHandle = SessionHandle;
            packet.Status        = 0;
            packet.SenderContext = SenderContext;
            packet.OptionsFlags  = 0;

            return(packet);
        }
        public EncapsReply SendRRData(CommonPacketItem AddressItem, CommonPacketItem DataItem, CommonPacketItem[] AdditionalItems = null)
        {
            if (OnPreOperation != null)
            {
                OnPreOperation();
            }

            if ((CommonPacketTypeId)AddressItem.TypeId == CommonPacketTypeId.ConnectionBased)
            {
                //The connection ID may have changed, so we have to reset it
                AddressItem.Data = BitConverter.GetBytes(ConnectionParameters.O2T_CID);
            }

            EncapsRRData rrData = new EncapsRRData();

            rrData.CPF             = new CommonPacket();
            rrData.Timeout         = (ushort)MillisecondTimeout;
            rrData.CPF.AddressItem = AddressItem;
            rrData.CPF.DataItem    = DataItem;

            if (AdditionalItems != null)
            {
                for (int i = 0; i < AdditionalItems.Length; i++)
                {
                    rrData.CPF.AddItem(AdditionalItems[i]);
                }
            }

            EncapsPacket request = EncapsPacketFactory.CreateSendRRData(SessionHandle, 0, SenderContext, rrData.Pack());

            byte[] rawRequest = request.Pack();

            byte[] rawReply = SendData_WaitReply(rawRequest);

            if (OnPostOperation != null)
            {
                OnPostOperation();
            }

            if (rawReply == null)
            {
                return(null);
            }

            EncapsReply reply = new EncapsReply();
            int         temp  = 0;

            reply.Expand(rawReply, 0, out temp);

            return(reply);
        }
Exemple #5
0
        /// <summary>
        /// Creates a ListServices packet
        /// </summary>
        /// <param name="SenderContext">Sender Context, array of 8 bytes</param>
        /// <returns>ListServices EncapsPacket</returns>
        public static EncapsPacket CreateListServices(byte[] SenderContext = null)
#endif
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.ListServices;
            packet.Length        = 0;
            packet.SessionHandle = 0;
            packet.Status        = 0;
            packet.SenderContext = SenderContext;
            packet.OptionsFlags  = 0;

            return(packet);
        }
Exemple #6
0
        /// <summary>
        /// Create a NOP (No Operation) packet
        /// </summary>
        /// <param name="EncapsData">Data to be sent with the NOP packet, which is dropped</param>
        /// <returns>NOP EncapsPacket</returns>
        /// <remarks>The NOP command will not issue a reply on the device, and all the data is dropped.</remarks>
        public static EncapsPacket CreateNOP(byte[] EncapsData = null)
#endif
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.NOP;
            packet.Length        = (ushort)(EncapsData == null ? 0 : EncapsData.Length);
            packet.SessionHandle = 0;
            packet.Status        = 0;
            packet.SenderContext = new byte[8];
            packet.OptionsFlags  = 0;
            packet.EncapsData    = EncapsData;

            return(packet);
        }
Exemple #7
0
        /// <summary>
        /// Creates a SendUnitData packet
        /// </summary>
        /// <param name="SessionHandle">Session handle given during the RegisterSession process</param>
        /// <param name="SenderContext">Sender Context, array of 8 bytes</param>
        /// <param name="EncapsData">Data to be sent in the SendUnitData Packet</param>
        /// <returns>EncapsPacket ready to be sent</returns>
        public static EncapsPacket CreateSendUnitData(uint SessionHandle, byte[] SenderContext = null, byte[] EncapsData = null)
#endif
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.SendUnitData;
            packet.Length        = (ushort)(EncapsData.Length);
            packet.SessionHandle = SessionHandle;
            packet.Status        = 0;
            packet.SenderContext = SenderContext;
            packet.OptionsFlags  = 0;
            packet.EncapsData    = EncapsData;

            return(packet);
        }
Exemple #8
0
        /// <summary>
        /// Creates a RegisterSession packet
        /// </summary>
        /// <param name="senderContext">Sender Context, array of 8 bytes</param>
        /// <returns>RegisterSession EncapsPacket</returns>
        public static EncapsPacket CreateRegisterSession(byte[] senderContext)
        {
            byte[] data = new byte[4];
            Buffer.BlockCopy(BitConverter.GetBytes((ushort)1), 0, data, 0, 2);

            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.RegisterSession;
            packet.Length        = 4;
            packet.SessionHandle = 0;
            packet.Status        = 0;
            packet.SenderContext = new byte[8];
            packet.OptionsFlags  = 0;
            packet.EncapsData    = data;

            return(packet);
        }
Exemple #9
0
        /// <summary>
        /// Verifies that the device supports CIP by searching for IdentityInfo item 0x0C
        /// </summary>
        /// <param name="si">Session Info</param>
        /// <returns>True if the device supports CIP messaging, false otherwise</returns>
        public static bool VerifyCIP(SessionInfo si)
        {
            if (!si.Connected)
            {
                return(false);
            }

            EncapsPacket request = EncapsPacketFactory.CreateListIdentity();

            request.SessionHandle = si.SessionHandle;
            request.SenderContext = si.SenderContext;

            EncapsReply reply = new EncapsReply();

            byte[] rawReply = si.SendData_WaitReply(request.Pack());
            int    temp     = 0;

            reply.Expand(rawReply, 0, out temp);

            if (reply.Status != 0)
            {
                return(false);
            }

            ListIdentityReply liReply = reply;

            if (liReply is ListIdentityReply_Failure)
            {
                return(false);
            }

            ListIdentityReply_Success liSucc = (ListIdentityReply_Success)(liReply);

            for (int i = 0; i < liSucc.ItemCount; i++)
            {
                if (liSucc.IdentityItems[i].ItemTypeCode == 0x0C)
                {
                    si.IdentityInfo = liSucc.IdentityItems[i];
                    return(true);
                }
            }

            return(false);
        }
Exemple #10
0
        /// <summary>
        /// Creates a SendRRData packet
        /// </summary>
        /// <param name="SessionHandle">Session handle given during the RegisterSession process</param>
        /// <param name="Timeout">Timeout, if 0 it uses the protocol timeout mechanism</param>
        /// <param name="SenderContext">Sender Context, array of 8 bytes</param>
        /// <param name="EncapsData">Data to be sent in the SendRRData Packet</param>
        /// <returns>EncapsPacket ready to be sent</returns>
        public static EncapsPacket CreateSendRRData(uint SessionHandle, uint Timeout, byte[] SenderContext = null, byte[] EncapsData = null)
#endif
        {
            EncapsPacket packet = new EncapsPacket();

            packet.Command       = (ushort)EncapsCommand.SendRRData;
            packet.Length        = (ushort)(6 + EncapsData.Length);
            packet.SessionHandle = SessionHandle;
            packet.Status        = 0;
            packet.SenderContext = SenderContext;
            packet.OptionsFlags  = 0;
            byte[] temp = new byte[(EncapsData == null ? 0 : EncapsData.Length)];

            if (EncapsData != null)
            {
                Buffer.BlockCopy(EncapsData, 0, temp, 0, EncapsData.Length);
            }

            packet.EncapsData = temp;

            return(packet);
        }
Exemple #11
0
        /// <summary>
        /// Creates a new session and registers it with the device
        /// </summary>
        /// <param name="hostNameOrIp">Host name or IP to connect to</param>
        /// <param name="port">Port to connect to (default: 0xAF12)</param>
        /// <param name="senderContext">Sender Context</param>
        /// <returns>Session Info object, check the SessionInfo.Connected and SessionInfo.Registered values</returns>
        public static SessionInfo CreateAndRegister(string hostNameOrIp, int port = 0xAF12, byte[] senderContext = null)
        {
            SessionInfo si = new SessionInfo();

            si.SessionSocket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);
            si.Connected     = false;
            si.Registered    = false;
            si.Pinged        = false;  //AK
            si.HostNameOrIp  = hostNameOrIp;
            si.Port          = port;
            si.SenderContext = senderContext;

            //First we Ping...
            try
            {
                si.TcpPing(hostNameOrIp, port);
            }catch (Exception e) { si.LastSessionErrorStack = e.StackTrace; si.LastSessionErrorString = e.Message; return(si); }

            //And we connect...
            try
            {
                if (si.Pinged)
                {
                    si.SessionSocket.Connect(hostNameOrIp, port);
                    si.Connected = true;
                }
                else
                {
                    return(si); //Ping NOK!
                }
            }
            catch (Exception e) { si.LastSessionErrorStack = e.StackTrace; si.LastSessionErrorString = e.Message; return(si); }

            //Now we register the session
            try
            {
                EncapsPacket packet    = EncapsPacketFactory.CreateRegisterSession(si.SenderContext);
                byte[]       rawPacket = packet.Pack();

                byte[]      rawReply = si.SendData_WaitReply(rawPacket);
                EncapsReply reply    = new EncapsReply();
                int         tmp      = 0;
                reply.Expand(rawReply, 0, out tmp);

                //Now we need to figure out if we successfully registered...
                RegisterSessionReply registerReply = reply;
                if (registerReply is RegisterSession_Success)
                {
                    si.SessionHandle = registerReply.SessionHandle;
                    si.Registered    = true;
                }
                else
                {
                    si.Registered = false;
                    return(si);
                }
            }
            catch (Exception e) { si.LastSessionErrorStack = e.StackTrace; si.LastSessionErrorString = e.Message; return(si); }

            return(si);
        }