public void Add(byte x)
{
if (IsErrored)
throw new SpheroPluginException("Response already errored. You cannot add more bytes to it.");
switch (_currentState)
{
case State.WaitingForSop1:
if (x != 0xff)
{
_currentState = State.ErrorHeader;
throw new SpheroPluginException("Unexpected packet SOP1 header byte {0}", x);
}
_response.StartOfPacket1 = x;
_currentState++;
break;
case State.WaitingForSop2:
if (x != 0xff && x != 0xfe)
{
_currentState = State.ErrorHeader;
throw new SpheroPluginException("Unexpected packet SOP2 header byte {0}", x);
}
_response.StartOfPacket2 = x;
_currentState++;
break;
case State.WaitingForMrsp:
_response.MessageResponse = x;
_currentState++;
break;
case State.WaitingForSeq:
_response.SequenceNumber = x;
_currentState++;
break;
case State.WaitingForDlen:
_response.DataLength = x;
_currentState++;
break;
case State.WaitingForPayloadAndCheckSum:
if (_response.DataLength - _response.Payload.Count > 1)
{
_response.Payload.Add(x);
}
else
{
_response.CheckSum = x;
if (!_response.Validate())
{
_currentState = State.ErrorCheckSum;
throw new SpheroPluginException("CheckSum error in received packet");
}
_currentState++;
}
break;
case State.Complete:
_currentState = State.ErrorOverflow;
throw new SpheroPluginException("You cannot add any more bytes to this complete response");
case State.Error:
case State.ErrorHeader:
case State.ErrorCheckSum:
case State.ErrorOverflow:
// do nothing
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}