Пример #1
0
        // Copy master SN to buffer.
        // Modifies end of SN to be netIfIndex.
        // Returns len.
        public byte CopyMasterSerialNumber(byte[] buffer, byte bufferIndex, byte netIfIndex)
        {
            int        idx; byte len = 0; byte[] s;
            LinkDriver p = Manager.GetLinkDriver(ProtocolConstants.NETIF_USER_BASE);

            if (p != null)
            {
                idx = p.SerialIndex; // Get the master's SN.
                if ((idx == -1) || idx >= SerialNumbersSize)
                {
                    s = MasterSN;
                }
                else
                {
                    s = IoSerialNumbers[idx].Text;
                }
                len = Primitives.Strlen(s);
                if (len == 0)   // Should never happen.
                {
                    s   = Primitives.StringToByteArray("XXX-0000-0\0");
                    len = Primitives.Strlen(s);
                }
                Array.Copy(s, 0, buffer, bufferIndex, len);
                // Modify end of SN to be netIfIndex.
                buffer[len - 1] = Primitives.NibbleToHexChar(netIfIndex);
            }
            return(len);
        }
Пример #2
0
        public LinkDriver GetLinkDriver(byte netIfIndex)
        {
            LinkDriver p = null;

            IoNetIfs.TryGetValue(netIfIndex, out p);
            return(p);
        }
Пример #3
0
 public LinkSchedulerThread(LinkIOThreads master, LinkDriver lp)
 {
     Master        = master;
     LnkPrt        = lp;
     Task          = new Thread(new ThreadStart(Run));
     Task.Name     = "LinkSchedulerThread:" + LnkPrt.NetIfIndex;
     Task.Priority = ThreadPriority.Highest;
 }
Пример #4
0
 public LinkIOThreads(LinkDriver lp)
 {
     LnkPrt          = lp;
     Started         = false;
     SchedulerThread = null;
     ReadThread      = null;
     WriteThread     = null;
 }
Пример #5
0
 public LinkWriteThread(LinkIOThreads master, LinkDriver lp)
 {
     Master        = master;
     LnkPrt        = lp;
     Task          = new Thread(new ThreadStart(Run));
     Task.Name     = "LinkWriteThread:" + LnkPrt.NetIfIndex;
     Task.Priority = ThreadPriority.AboveNormal;
 }
Пример #6
0
        public void ResetDriver(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                p.Monitor.ResetLink(LinkMonitor.Destination); //TODO: set destination.
            }
        }
Пример #7
0
        public void PingNetIf(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                p.Monitor.PingLink();
            }
        }
Пример #8
0
        // Push message on to RNetIfIndex's Write Q.
        // Messages that cannot be posted are freed.
        public static void PostMessage(NetIfManager manager, int ioIndex, byte rNetIfIndex)
        {
            String dbgmsg = null;

            if (ioIndex == -1)
            {
                return;
            }
            LinkDriver p = manager.GetLinkDriver(rNetIfIndex);

            if (p == null)
            {
                rNetIfIndex = ProtocolConstants.NETIF_UNSET;
            }

            if ((p != null) || !isMaster)   // Check destination netIf was not detached.
            {
                if (p == null)
                {
                    p = manager.GetLinkDriver(ProtocolConstants.NETIF_BRIDGE_LINK);
                }
                if (p != null)
                {
                    PacketBuffer b = manager.IoBuffers[ioIndex];
                    // Check for circular routing: examine prior destination netIf.
                    if ((b.dNetIf != ProtocolConstants.NETIF_UNSET) && (b.dNetIf == rNetIfIndex))
                    {
                        // Ignore circular messages.
                        dbgmsg = String.Format("Circular message on netIf [{0}]: ditching message.", rNetIfIndex);
                    }
                    else
                    {
                        b.dNetIf = rNetIfIndex;
                        if (p.NetIfIndex == ProtocolConstants.NETIF_BRIDGE_LINK)
                        {
                            byte netIfs = (byte)((rNetIfIndex << 4) | b.iNetIf); // Use destination to pass netIfs across bridge.
                            b.destination((ushort)(((256 - netIfs) << 8) | netIfs));
                        }
                        if (p.LinkWriteQueue.Push(ioIndex) != -1)   // Check post queue not full.
                        {
                            if (Settings.DebugLevel > 6)
                            {
                                Log.d(TAG, "Posting:" + ioIndex + " to " + p.NetIfIndex);
                            }
                            return;
                        }
                        dbgmsg = String.Format("Write Q full on netIf [{0}]: ditching message.", rNetIfIndex);
                    }
                }
            }
            // Messages that were not posted are freed.
            if (Settings.DebugLevel > 1)
            {
                Log.w(TAG, dbgmsg);
            }
            manager.IoBuffersFreeHeap.Release(ioIndex);
        }
Пример #9
0
        public ComponentState GetComponentState(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                return(p.GetComponentState());
            }
            return(ComponentState.Problem);
        }
Пример #10
0
 public PacketParser(LinkDriver ltx)
 {
     IoNetIf         = ltx;
     Manager         = ltx.Manager;
     HandshakePacket = new PacketBuffer(ProtocolConstants.HANDSHAKE_PACKET_SIZE);
     InputBuffer     = null;
     CurrentPacket   = null;
     IoIndex         = -1;
     ResetParser();
 }
Пример #11
0
        public string GetComponentId(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                return(p.GetComponentId());
            }
            return(null);
        }
Пример #12
0
        public int GetSessionId(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                return(p.GetSessionId());
            }
            return(0);
        }
Пример #13
0
        public int GetWriteQueueSize(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                return(p.GetWriteQueueSize());
            }
            return(-1);
        }
Пример #14
0
        public bool AddDirectNetIf(LinkDevice dev, byte netIfIndex)
        {
            // Important: check device not already in use.
            if ((dev == null) || (Manager.GetLinkDriver(dev) != null))
            {
                return(false);
            }
            LinkDriver lp = new LinkDriver(Manager, dev, netIfIndex, true, true, true, false);

            Manager.AddDriver(lp);
            lp.StartLinkDriver();
            return(true);
        }
Пример #15
0
        public bool AddListenerNetIf(LinkDevice dev, byte netIfIndex)
        {
            // Important: check device not already in use.
            if ((dev == null) || (Manager.GetLinkDriver(dev) != null))
            {
                return(false);
            }
            NetIfDevice pd = new NetIfDevice(this, dev); // TODO: who should owner be?
            LinkDriver  lp = new LinkDriver(Manager, pd, netIfIndex, true, true, true, false);

            Manager.AddDriver(lp);
            lp.StartLinkDriver();
            return(true);
        }
Пример #16
0
        public int GetWriteQueueCapacity(byte netIfIndex)
        {
            LinkDriver p = GetLinkDriver(netIfIndex);

            if (p != null)
            {
                int c = p.LinkWriteQueue.Capacity();
                if (c > 0)
                {
                    return(c - 1);
                }
            }
            return(0);
        }
Пример #17
0
 public void AddDriver(LinkDriver p)
 {
     if (p != null)
     {
         LinkDriver q = null;
         if (IoNetIfs.TryGetValue(p.NetIfIndex, out q))
         {
             if (q != null)
             {
                 q.Suspend();
             }
         }
         IoNetIfs[p.NetIfIndex] = p;
     }
 }
Пример #18
0
        // Submit a mediator command to link WriteQ.
        // For Mediator messages:
        // Inbound=0, HasSender=1,
        // Receiver=ADRS_LOCAL, RNetIf=NETIF_MEDIATOR_PORT,
        // sender=NodeAdrs, ident=0, SNetIf=link netIf
        public int SendLinkMediatorCommand(LinkDriver ltx, bool verified, byte cmd, byte dataLen, byte[] pData, byte bufferIndex)
        {
            int idx = Manager.Factory.CreateGeneralMessage(verified, cmd,
                                                           ProtocolConstants.ADRS_LOCAL, Manager.NodeAdrs, 0, (byte)0,
                                                           dataLen, pData, bufferIndex);

            if (idx != -1)
            {
                if (ltx.LinkWriteQueue.Push(idx) == -1)
                {
                    Manager.IoBuffersFreeHeap.Release(idx);
                    return(-1);
                }
            }
            return(idx);
        }
Пример #19
0
 public void RemoveDriver(LinkDriver p)
 {
     if (p != null)
     {
         LinkDriver q = null;
         if (IoNetIfs.TryGetValue(p.NetIfIndex, out q))
         {
             if (q != null)
             {
                 q.Suspend();
                 q.StopLinkDriver();
                 q.Close();
                 IoNetIfs.Remove(q.NetIfIndex);
             }
         }
     }
 }
Пример #20
0
        // Update the serial number for device at other end of netIf.
        public void UpdateSerialNumbers(byte[] buffer, byte bufferIndex, byte len, byte netIfIndex, ushort senderAdrs)
        {
            int idx;

            if ((netIfIndex == ProtocolConstants.NETIF_USER_BASE) && (senderAdrs == ProtocolConstants.ADRS_LOCAL))
            {
                UpdateProductCode(buffer);
                // This is the master's SN, so save it.
                Manager.AppBabelSaveSerialNumber(buffer, bufferIndex);
            }
            LinkDriver p = Manager.GetLinkDriver(netIfIndex);

            if (p != null)
            {
                idx = p.SerialIndex;
                if (idx == -1)
                {
                    idx = SerialNumbersFreeHeap.Allocate();
                }
                CopyToSerialNumber(idx, len, buffer, bufferIndex);
            }
        }
Пример #21
0
        // Returns true if message sent successfully.
        // Returns false if low level write Q count above water mark.
        // If connection dies during a long xfer, abort & return false.
        public bool DispatchMessage(MessageBinder binder)
        {
            if (PostNetIfIndex == ProtocolConstants.NETIF_UNSET)
            {
                OutgoingNetIfIndex = ProtocolConstants.NETIF_USER_BASE;
            }
            else
            {
                OutgoingNetIfIndex = PostNetIfIndex;
            }
            LinkDriver p = Exchange.Manager.GetLinkDriver(OutgoingNetIfIndex);

            if (p == null)
            {
                LastError = MessageError.BadNetIf;
                return(false);
            }
            if (p.GetWriteQueueSize() > p.LinkWriteQWaterMark)
            {
                return(false);
            }
            if (p.GetComponentState() != ComponentState.Working)
            {
                return(false);
            }

            if (binder.Message.SenderId != 0)
            {
                binder.Exchange.SubmitWaiter(binder);
            }
            while (HasRemainingTransfers())
            {
                ;
            }
            return(true);
        }
Пример #22
0
        // IOPort set up serial number for device at other end of link.
        // For the master port, set up temp SN and append netIfIndex.
        // For all other ports, set SN idx to null. It will be set when device connects.
        public void NetIfSerialNumberSetup(byte netIfIndex)
        {
            LinkDriver p = Manager.GetLinkDriver(netIfIndex);

            if (p != null)
            {
                if (netIfIndex == ProtocolConstants.NETIF_USER_BASE)
                {
                    byte len;
                    int  idx = SerialNumbersFreeHeap.Allocate();
                    if (idx != -1)
                    {
                        len = Primitives.Strlen(MasterSN);
                        CopyToSerialNumber(idx, len, MasterSN, 0);
                        IoSerialNumbers[idx].Text[len - 1] = Primitives.NibbleToHexChar(netIfIndex);
                    }
                    p.SerialIndex = idx;
                }
                else
                {
                    p.SerialIndex = -1;
                }
            }
        }
Пример #23
0
 public LinkMonitor(LinkDriver ltx)
 {
     IoNetIf         = ltx;
     Manager         = ltx.Manager;
     NumMessagesRead = 0;
 }