def make_response(seq, action, error=0, p1=0, p2=0, data=None, length=None): if not length: length = 14 if data: length += 2 + len(data) response = struct.pack('<2B5H', 1, seq, length, action, error, p1, p2) crc = stentura._crc(buffer(response, 1, 11)) response += struct.pack('<H', crc) if data: crc = stentura._crc(data) if not isinstance(data, str) and not isinstance(data, buffer): data = ''.join([chr(b) for b in data]) response += data + struct.pack('<H', crc) return response
def test_make_open(self): buf = array.array('B', [3] * 18) # Start with junk in the buffer. seq = 79 drive = 'A' filename = 'REALTIME.000' p = stentura._make_open(buf, seq, drive, filename) for_crc = [seq, 20 + len(filename), 0, stentura._OPEN, 0, ord(drive), 0, 0, 0, 0, 0, 0, 0, 0, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(filename) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in filename] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_reset(self): buf = array.array('B', [3] * 20) # Start with junk in the buffer. seq = 67 p = stentura._make_reset(buf, seq) for_crc = [seq, 18, 0, stentura._RESET, 0] + ([0] * 10) crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_read(self): buf = array.array('B', [3] * 20) # Start with junk in the buffer. seq = 32 block = 1 byte = 8 length = 20 p = stentura._make_read(buf, seq, block, byte, length) for_crc = [seq, 18, 0, stentura._READC, 0, 1, 0, 0, 0, length, 0, block, 0, byte, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_request(self): buf = array.array('B') seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected]) # Now with data. data = 'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in data] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_crc(self): data = [ord(x) for x in '123456789'] self.assertEqual(stentura._crc(data), 0xBB3D)