public void BuildsWithSimplePayload()
        {
            var responseBuilder = new SpheroResponseBuilder();

            AssertOKButNotComplete(responseBuilder);

            responseBuilder.Add(0xff); // SOP1
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xff); // SOP2
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xab); // Response
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0x21); // Sequence
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0x05); // Length of payload + checksum
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xa1); // payload
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xa2); // payload
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xa3); // payload
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xa4); // payload
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xa4); // inverted checksum
            AssertOKAndComplete(responseBuilder);
        }
        public void ThrowsExceptionWithInvalidHeader2()
        {
            var responseBuilder = new SpheroResponseBuilder();

            responseBuilder.Add(0xff); // SOP1
            responseBuilder.Add(0xfd); // SOP2
            Assert.Fail("We should throw an exception before here");
        }
        private SpheroResponse ReceiveResponse()
        {
            var builder = new SpheroResponseBuilder();

            while (true)
            {
                byte x;

                try
                {
                    var task = _streamSpheroWrapper.ReceiveByte();
                    task.Start();
                    x = task.Result;
                }
                catch (AggregateException exception)
                {
                    throw exception.InnerException;
                }

                builder.Add(x);

                if (builder.IsErrored)
                {
                    throw new SpheroPluginException("Error reading packet {0}", builder.CurrentState);
                }

                if (builder.IsComplete)
                {
                    return(builder.Response);
                }
            }
        }
        public void ThrowsExceptionWithInvalidCheckSum()
        {
            var responseBuilder = new SpheroResponseBuilder();

            responseBuilder.Add(0xff); // SOP1
            responseBuilder.Add(0xff); // SOP2
            responseBuilder.Add(0xab); // Response
            responseBuilder.Add(0x21); // Sequence
            responseBuilder.Add(0x05); // Length of payload + checksum
            responseBuilder.Add(0xa1); // payload
            responseBuilder.Add(0xa2); // payload
            responseBuilder.Add(0xa3); // payload
            responseBuilder.Add(0xa4); // payload

            // deliberately corrupt checksum
            responseBuilder.Add(0xd8); // inverted checksum
            Assert.Fail("We should throw an exception before here");
        }
        private async Task <SpheroResponse> ReceiveResponse()
        {
            var builder = new SpheroResponseBuilder();

            while (true)
            {
                var x = await _streamSpheroWrapper.ReceiveByte();

                builder.Add(x);

                if (builder.IsErrored)
                {
                    throw new SpheroPluginException("Error reading packet {0}", builder.CurrentState);
                }

                if (builder.IsComplete)
                {
                    return(builder.Response);
                }
            }
        }
        public void BuildsWithEmptyPayload()
        {
            var responseBuilder = new SpheroResponseBuilder();

            AssertOKButNotComplete(responseBuilder);

            responseBuilder.Add(0xff); // SOP1
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xff); // SOP2
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0x01); // Response
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0x31); // Sequence
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0x01); // Length of payload + checksum
            AssertOKButNotComplete(responseBuilder);
            responseBuilder.Add(0xcc); // inverted checksum
            AssertOKAndComplete(responseBuilder);
        }