public MmProtocol(IMmStream mmStream, int sourceId = 0, int transactionLabel = 0) { this.mmStream = mmStream; // Optional parameter. If you are running two or more applications simultaneously, you can give // each application a different ID in this parameter. If an error occurs, you can read the // Source address in the memory map Status area to find out which application caused the error. // Range 0x0000-0xffff (16bit) var si = sourceId & 0xffff; if (si != sourceId) { Thrower.Throw("Source id {0:X} out of range (0x0000 to 0xFFFF)", sourceId); } this.sourceId = sourceId; // A label specified by the requester and identifying this transaction. // Range 0x00-0x3F (8bit). This value is returned in the response packet. var tl = (transactionLabel << 2) & 0xff; if (tl >> 2 != transactionLabel) { Thrower.Throw( "Transaction label {0:X} out of range (0x00 to 0x3F)", transactionLabel); } this.transactionLabel = tl; }
private void checkBlockLength(int length) { if (length != (length & 0xffff)) { Thrower.Throw( "Invalid length {0:X}, range is 0x0000 to 0xffff", length); } }
public void Read(byte[] data) { var count = socket.GetStream().Read(data, 0, data.Length); if (count != data.Length) { Thrower.Throw("Read count mismatch requested:{0} available:{1}", data.Length, count); } }
private void checkBlockLength(byte[] response, int length) { var _length = response[12] << 8 | response[13]; if (_length != length) { Thrower.Throw( "Invalid block length, expected {0} but received {1}", length, _length); } }
private void checkDestinationOffset(long destinationOffset) { var _destinationOffset = (destinationOffset & 0xffffffffffffL); if (destinationOffset != _destinationOffset) { Thrower.Throw( "Invalid destinationOffset {0:X}, range is 0x000000000000 to 0xffffffffffff", destinationOffset); } }
private void checkData(byte[] data, int length) { if (data == null) { Thrower.Throw("data is null"); } if (length >= 0 && data.Length != length) // do not test if length<0 { Thrower.Throw( "Invalid data length, expected {0} but received {1}", length, data.Length); } }
public SocketMmStream(string host, int port, int timeout) { this.socket = new TcpClient(); var result = socket.BeginConnect(host, port, null, null); if (!result.AsyncWaitHandle.WaitOne(timeout, true)) { Thrower.Throw("Timeout connecting to {0}:{1}", host, port); } socket.EndConnect(result); socket.ReceiveTimeout = timeout; socket.SendTimeout = timeout; }
private void checkResponse(byte[] response, TransactionCode transactionCode) { // 40h returned for required powerup clear // 50h returned for invalid address var _resultCode = response.Length >= 7 ? response[6] : (byte)0; if (_resultCode != 0) { throw new NakException(_resultCode); } // check source id matches var _sourceId = 0xffff & response[4] << 8 | response[5]; if (_sourceId != sourceId) { Thrower.Throw( "Invalid source id, expected {0:X} but received {1:X}", sourceId, _sourceId); } // check transaction label matches var _transactionLabel = response[2] & 0xff; if (_transactionLabel != transactionLabel) { Thrower.Throw( "Invalid transaction label, expected {0:X} but received {1:X}", transactionLabel, _transactionLabel); } // check transaction code matches var _transactionCode = response[3] & 0xff; if (!Enum.IsDefined(typeof(TransactionCode), _transactionCode) || (TransactionCode)_transactionCode != transactionCode) { Thrower.Throw( "Invalid transaction code, expected {0:X} but received {1:X}", transactionCode, _transactionCode); } }