Exemplo n.º 1
0
 private static byte GetCommandSuccess(INTV.Shared.Utility.ASCIIBinaryReader reader)
 {
     return(reader.ReadByte());
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the response to a command - ACK, NAK, or other. See remarks.
        /// </summary>
        /// <param name="reader">The reader to use to get the response.</param>
        /// <param name="command">The command that is executing.</param>
        /// <param name="errorDetail">Receives detailed error information if the command is not acknowledged.</param>
        /// <param name="timedOut">Receives a value indicating whether or not command execution timed out.</param>
        /// <returns>The response, which could be Ack, Nak, or possibly another invalid value.</returns>
        /// <remarks>From Joe's implementation of lc_wait_acknak:
        /// This function is weird. It must consume some number of partial
        /// beacons w/out advancing the CRC, and then an ACK/NAK. Only the ACK
        /// advances the CRC. If we get an unexpected byte, for now just flag
        /// an error.
        /// To simplify things, this function does not update the CRC. The
        /// caller should update the CRC.
        /// </remarks>
        private static byte GetCommandAcknowledgement(INTV.Shared.Utility.ASCIIBinaryReader reader, ProtocolCommandId command, ref string errorDetail, out bool timedOut)
        {
            byte result = 0xFF; // unknown state

            timedOut = false;
#if false
            try
            {
                do
                {
                    result = reader.ReadByte();
                } while (Device.BeaconCharacters.Contains((char)result));
            }
            catch (TimeoutException)
            {
                result = Nak;
            }
#else
            int state       = -1;
            var prevTimeout = reader.BaseStream.ReadTimeout;

            if (reader.BaseStream.CanTimeout)
            {
                reader.BaseStream.ReadTimeout = 200;
            }
            try
            {
                const int RetryCount = 6;
                for (int i = 0; i < RetryCount; ++i)
                {
                    try
                    {
                        do
                        {
                            timedOut = false;
                            result   = reader.ReadByte();
#if DEBUG
                            var forceNak = false;
                            switch (command)
                            {
                            case ProtocolCommandId.Ping:
                            case ProtocolCommandId.GarbageCollect:
                                // never inject a fake NAK for these
                                break;

                            default:
                                forceNak = ReturnNakForNextCommand;
                                break;
                            }
                            if (forceNak)
                            {
                                result = Nak;
                            }
#endif // DEBUG
                            if ((result == Ack) || (result == Nak))
                            {
                                i = RetryCount;
                                var errorMessage = "Command returned NAK directly on port getting command acknowledgement for: " + command + ", not 'virtual' Nak due to garbage.";
                                if (!string.IsNullOrEmpty(errorDetail))
                                {
                                    errorDetail += Environment.NewLine;
                                }
                                errorDetail += errorMessage;
                                DebugOutputIf(result == Nak, errorMessage);
                                break;
                            }
                            if ((state == -1 || state == '\n') && result == 'L')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'L') && result == 'O')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'O') && result == 'C')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'C') && result == 'U')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'U') && result == 'T')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'T') && result == 'U')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'U') && result == 'S')
                            {
                                state = result;
                            }
                            else if ((state == -1 || state == 'S') && result == '\n')
                            {
                                state = result;
                            }
                            else
                            {
                                var errorMessage = "Unexpected response when executing command: " + command + ", returned: " + result;
                                if (!string.IsNullOrEmpty(errorDetail))
                                {
                                    errorDetail += Environment.NewLine;
                                }
                                errorDetail += errorMessage;
                                DebugOutput(errorMessage);
                                state = -1;
                                i     = RetryCount;
                            }
                        }while (Device.BeaconCharacters.Contains((char)state));
                    }
                    catch (TimeoutException)
                    {
                        var errorMessage = "Command response for: " + command + " timed out! Returning 'virtual' Nak";
                        errorDetail += errorMessage;
                        DebugOutput(errorMessage);
                        timedOut = true;
                        result   = Nak;
                    }
                }
            }
            finally
            {
                // reader.BaseStream may go bad if cord was pulled during communication.
                if ((reader.BaseStream != null) && reader.BaseStream.CanTimeout)
                {
                    reader.BaseStream.ReadTimeout = prevTimeout;
                }
            }
#endif // false
            return(result);
        }