Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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");
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        public void ThrowsExceptionWithInvalidHeader2()
        {
            var responseBuilder = new SpheroResponseBuilder();

            responseBuilder.Add(0xff); // SOP1
            responseBuilder.Add(0xfd); // SOP2
            Assert.Fail("We should throw an exception before here");
        }
Ejemplo n.º 5
0
 private static void AssertOKAndComplete(SpheroResponseBuilder responseBuilder)
 {
     Assert.IsTrue(responseBuilder.IsComplete);
     Assert.IsFalse(responseBuilder.IsErrored);
 }
        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;
                }
            }
        }
        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;
                }
            }
        }