コード例 #1
0
 public bool IsMatch(EzspFrameResponse response)
 {
     if (response.GetType() == _requiredResponse && _request.GetSequenceNumber() == response.GetSequenceNumber())
     {
         this._response = response;
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #2
0
            public bool TransactionEvent(EzspFrameResponse ezspResponse)
            {
                // Check if this response completes our transaction
                if (ezspResponse.GetType() != _eventClass)
                {
                    return(false);
                }

                _receivedEvent = ezspResponse;
                TransactionComplete();

                return(true);
            }
コード例 #3
0
        /**
         * Notify any transaction listeners when we receive a response.
         *
         * @param response the response data received
         * @return true if the response was processed
         */
        private bool NotifyTransactionComplete(EzspFrameResponse response)
        {
            bool processed = false;

            lock (_transactionListeners)
            {
                foreach (AshListener listener in _transactionListeners)
                {
                    if (listener.TransactionEvent(response))
                    {
                        processed = true;
                    }
                }
            }

            return(processed);
        }
コード例 #4
0
            public bool TransactionEvent(EzspFrameResponse ezspResponse)
            {
                if (ezspResponse.GetSequenceNumber() == _ezspTransaction.GetRequest().GetSequenceNumber() &&
                    ezspResponse is EzspInvalidCommandResponse)
                {
                    // NCP doesn't support this command!
                    TransactionComplete();
                    return(true);
                }

                // Check if this response completes our transaction
                if (!_ezspTransaction.IsMatch(ezspResponse))
                {
                    return(false);
                }

                TransactionComplete();

                return(true);
            }
コード例 #5
0
        public bool IsMatch(EzspFrameResponse response)
        {
            // Check if this response is related to this transaction
            if (_relatedResponses.Contains(response.GetType()))
            {
                // TODO: Check for a failure

                // Add the response to our responses received list
                _responses.Add(response);
                return(false);
            }

            // Check if this response completes the transaction
            if (response.GetType() == _requiredResponse)
            {
                _responses.Add(response);
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
        private void ParserTaskLoop()
        {
            Log.Debug("AshFrameHandler parser task started");

            int exceptionCnt = 0;

            while (!_parserCancellationToken.IsCancellationRequested)
            {
                try
                {
                    int[] packetData = GetPacket();
                    if (packetData == null)
                    {
                        continue;
                    }

                    AshFrame packet        = AshFrame.CreateFromInput(packetData);
                    AshFrame responseFrame = null;
                    if (packet == null)
                    {
                        Log.Debug("<-- RX ASH error: BAD PACKET {Frame}", FrameToString(packetData));

                        // Send a NAK
                        responseFrame = new AshFrameNak(_ackNum);
                    }
                    else
                    {
                        Log.Debug("<-- RX ASH frame: {Frame}", packet.ToString());

                        // Reset the exception counter
                        exceptionCnt = 0;

                        // Extract the flags for DATA/ACK/NAK frames
                        switch (packet.GetFrameType())
                        {
                        case AshFrame.FrameType.DATA:
                            _statsRxData++;

                            // Always use the ackNum - even if this frame is discarded
                            AckSentQueue(packet.GetAckNum());

                            AshFrameData dataPacket = (AshFrameData)packet;

                            // Check for out of sequence frame number
                            if (packet.GetFrmNum() == _ackNum)
                            {
                                // Frame was in sequence - prepare the response
                                _ackNum       = (_ackNum + 1) & 0x07;
                                responseFrame = new AshFrameAck(_ackNum);

                                // Get the EZSP frame
                                EzspFrameResponse response = EzspFrame.CreateHandler(dataPacket.GetDataBuffer());
                                Log.Verbose("ASH RX EZSP: {Response}", response);
                                if (response == null)
                                {
                                    Log.Debug("ASH: No frame handler created for {Packet}", packet);
                                }
                                else
                                {
                                    NotifyTransactionComplete(response);
                                    HandleIncomingFrame(response);
                                }
                            }
                            else if (!dataPacket.GetReTx())
                            {
                                // Send a NAK - this is out of sequence and not a retransmission
                                Log.Debug("ASH: Frame out of sequence - expected {Expected}, received {Received}", _ackNum, packet.GetFrmNum());
                                responseFrame = new AshFrameNak(_ackNum);
                            }
                            else
                            {
                                // Send an ACK - this was out of sequence but was a retransmission
                                responseFrame = new AshFrameAck(_ackNum);
                            }
                            break;

                        case AshFrame.FrameType.ACK:
                            _statsRxAcks++;
                            AckSentQueue(packet.GetAckNum());
                            break;

                        case AshFrame.FrameType.NAK:
                            _statsRxNaks++;
                            SendRetry();
                            break;

                        case AshFrame.FrameType.RSTACK:
                            // Stack has been reset!
                            HandleReset((AshFrameRstAck)packet);
                            break;

                        case AshFrame.FrameType.ERROR:
                            // Stack has entered FAILED state
                            HandleError((AshFrameError)packet);
                            break;

                        default:
                            break;
                        }
                    }

                    // Due to possible I/O buffering, it is important to note that the Host could receive several
                    // valid or invalid frames after triggering a reset of the NCP. The Host must discard all frames
                    // and errors until a valid RSTACK frame is received.
                    if (!_stateConnected)
                    {
                        continue;
                    }

                    // Send the next frame
                    // Note that ASH protocol requires the host always sends an ack.
                    // Piggybacking on data packets is not allowed
                    if (responseFrame != null)
                    {
                        SendFrame(responseFrame);
                    }

                    SendNextFrame();
                }
                catch (Exception e)
                {
                    Log.Error(e, "AshFrameHandler Exception: ", e);

                    if (exceptionCnt++ > 10)
                    {
                        Log.Error("AshFrameHandler exception count exceeded: {Exception}");
                        _parserCancellationToken.Cancel();
                    }
                }
            }
            Log.Debug("AshFrameHandler exited.");
        }