def test_crc_single_bit_changed(): data_a = [0x3f,0x1a,0xff,0x56,0x3a] crc_a = crc16(data_a) data_b = [0x3f,0x1a,0xef,0x56,0x3a] crc_b = crc16(data_b) assert crc_a != crc_b
def test_returned_value_float(): distantio = distantio_protocol() # inputs var_id = 124 fmt = 0 value = 1.362e-12 extraid_1 = 1.25 extraid_2 = 12715 # formatting v = (var_id).to_bytes(2,byteorder='big') n = pack(distantio.format_lookup[fmt],value) frame = bytearray() frame.append(0x01) frame += v frame.append(fmt) frame += pack('>f',extraid_1) frame += pack('>H',extraid_2) frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc response = distantio.process(frame) assert response['type'] == 'returned-value' assert response['var-id'] == var_id assert round(response['var-value'],5) == round(value,5) assert response['var-type'] == fmt assert round(response['var-time'],5) == round(extraid_1,5) assert response['var-array-index'] == extraid_2
def test_returned_value_writeable_int(): distantio = distantio_protocol() # inputs var_id = 124 fmt = 0xF3 value = 126594521 extraid_1 = 2.725 extraid_2 = 9513 # formatting v = (var_id).to_bytes(2,byteorder='big') n = pack(distantio.format_lookup[fmt & 0x0F],value) frame = bytearray() frame.append(0x01) frame += v frame.append(fmt) frame += pack('>f',extraid_1) frame += pack('>H',extraid_2) frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc response = distantio.process(frame) assert response['type'] == 'returned-value' assert response['var-id'] == var_id assert response['var-value'] == value assert response['var-type'] == fmt & 0x0F assert round(response['var-time'],5) == round(extraid_1,5) assert response['var-array-index'] == extraid_2
def test_returned_descriptor_variable_writeable(): distantio = distantio_protocol() # inputs var_id = 999 name = "testvariable 2" fmt = 0xF3 # formatting formatted_name = "{:14s}".format(name) v = (var_id).to_bytes(2,byteorder='big') n = bytes(formatted_name,'ascii') frame = bytearray() frame.append(0x00) frame += v frame.append(fmt) frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc response = distantio.process(frame) assert response['type'] == 'returned-descriptor' assert response['var-id'] == var_id assert response['var-name'] == formatted_name assert response['var-type'] == fmt & 0x0F assert response['var-writeable'] == True
def test_decode_group_descriptor(): distantio = distantio_protocol() # inputs group_id = 39 fmt = 0x07 name = "testvar" # formatting formatted_name = "{:14s}".format(name) n = bytes(formatted_name,'ascii') # formatting v = (group_id << 10).to_bytes(2,byteorder='big') frame = bytearray() frame.append(0x00) frame += v frame.append(fmt) frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc response = distantio.process(frame) assert response['type'] == 'returned-group-descriptor' assert response['group-id'] == group_id assert response['group-name'] == formatted_name
def test_returned_descriptor_variable_non_writeable(): distantio = distantio_protocol() # inputs var_id = 999 group_id = 18 name = "testvariable 1" fmt = 3 # formatting formatted_name = "{:14s}".format(name) data_ID = (var_id + (group_id << 10)).to_bytes(2,byteorder='big') data = bytes(formatted_name,'ascii') frame = bytearray() frame.append(0x00) frame += data_ID frame.append(fmt) frame += data crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc # Testing frame response = distantio.process(frame) assert response['type'] == 'returned-descriptor' assert response['var-id'] == var_id assert response['var-name'] == formatted_name assert response['var-type'] == fmt assert response['var-writeable'] == False assert response['var-group'] == group_id
def get_descriptors_frame(self): packet = bytearray(self.frame_size) # Build data frame packet[self.cmd_start] = 0x02 # Compute and pack CRC packet[self.crc_start:self.crc_end] = crc16(packet[:-2]).to_bytes(2,byteorder='big') return packet
def get_stop_reading_frame(self,variable_id,variable_type): packet = bytearray(self.frame_size) packet[self.cmd_start] = 0x06 packet[self.data_id_start:self.data_id_end] = variable_id.to_bytes(2,byteorder='big') packet[self.data_type_start] = variable_type packet[self.crc_start:self.crc_end] = crc16(packet[:-2]).to_bytes(2,byteorder='big') return packet
def test_alive_signal(): distantio = distantio_protocol() frame = bytearray(18) frame[0] = 0x03 crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc response = distantio.process(frame) assert response['type'] == 'alive-signal'
def get_write_value_frame(self,variable_id,variable_type,value): packet = bytearray(self.frame_size) # Cast value to chosen type if variable_type == 0: value = float(value) else: value = int(value) packet[self.cmd_start] = 0x04 packet[self.data_id_start:self.data_id_end] = variable_id.to_bytes(2,byteorder='big') packet[self.data_type_start] = variable_type packet[self.data_start:self.data_end] = pack(self.format_lookup[variable_type],value) packet[self.crc_start:self.crc_end] = crc16(packet[:-2]).to_bytes(2,byteorder='big') return packet
def test_decode_wrong_cmd(): distantio = distantio_protocol() var_id = 38153 name = "testvariable 1" formatted_name = "{:14s}".format(name) v = (var_id).to_bytes(2, byteorder='big') n = bytes(formatted_name, 'ascii') frame = bytearray() frame.append(0xFF) # Wrong command frame += v frame.append(0x00) frame += n crc = crc16(frame).to_bytes(2, byteorder='big') frame += crc assert pytest.raises(ValueError, distantio.process, frame)
def test_returned_descriptor_wrong_format(): distantio = distantio_protocol() var_id = 38153 name = "testvar" formatted_name = "{:14s}".format(name) v = (var_id).to_bytes(2, byteorder='big') n = bytes(formatted_name, 'ascii') frame = bytearray() frame.append(0x00) frame += v frame.append(0xFF) # Wrong format frame += n crc = crc16(frame).to_bytes(2, byteorder='big') frame += crc assert pytest.raises(ValueError, distantio.process, frame)
def test_decode_wrong_cmd(): distantio = distantio_protocol() var_id = 38153 name = "testvariable 1" formatted_name = "{:14s}".format(name) v = (var_id).to_bytes(2,byteorder='big') n = bytes(formatted_name,'ascii') frame = bytearray() frame.append(0xFF)# Wrong command frame += v frame.append(0x00) frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc assert pytest.raises(ValueError,distantio.process,frame)
def test_returned_descriptor_wrong_format(): distantio = distantio_protocol() var_id = 38153 name = "testvar" formatted_name = "{:14s}".format(name) v = (var_id).to_bytes(2,byteorder='big') n = bytes(formatted_name,'ascii') frame = bytearray() frame.append(0x00) frame += v frame.append(0xFF)# Wrong format frame += n crc = crc16(frame).to_bytes(2,byteorder='big') frame += crc assert pytest.raises(ValueError,distantio.process,frame)
def test_crc_know_value(): # test the alive-signal from DistantIO data = [0x03,0x00,0x00,0x00,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17] crc = crc16(data) assert crc == 13216
def process(self,frame): returned_instruction = dict() returned_instruction['type'] = None # First, check data size if len(frame) != self.frame_size: raise IndexError("Frame size "+str(len(frame))+", expecting "+str(self.frame_size)) # Second, check crc crc_frame = unpack('>H',frame[-2:])[0] crc_ref = crc16(frame[:-2]) if crc_frame != crc_ref: raise ValueError("CRCs do not match, crc frame : "+str(crc_frame)+" versus reference :"+str(crc_ref)) # Identify command cmd = frame[self.cmd_start] # returned-descriptor command if cmd == 0x00: # Check format is valid raw_format = frame[self.data_type_start] & 0x0F if raw_format < 0 or raw_format > 7: raise ValueError("Received format identifier "+str(raw_format)+" unknown.") # Received a group descriptor if raw_format == 7: returned_instruction['type'] = 'returned-group-descriptor' value = unpack('>H',(frame[self.data_id_start:self.data_id_end]))[0] value = (value >> 10) & 0x3F returned_instruction['group-id'] = value returned_instruction['group-name'] = frame[self.var_name_start:self.var_name_end].decode(encoding='UTF-8') else: returned_instruction['type'] = 'returned-descriptor' var_id = unpack('>H',(frame[self.data_id_start:self.data_id_end]))[0] var_id = var_id & 0x3FF var_group = unpack('>H',(frame[self.data_id_start:self.data_id_end]))[0] var_group = (var_group >> 10) & 0x3F returned_instruction['var-id'] = var_id returned_instruction['var-type'] = raw_format returned_instruction['var-writeable'] = ((frame[self.data_type_start])>>4 & 0x0F == 0x0F) returned_instruction['var-name'] = frame[self.var_name_start:self.var_name_end].decode(encoding='UTF-8') returned_instruction['var-group'] = var_group return returned_instruction # returned-value command if cmd == 0x01: returned_instruction['type'] = 'returned-value' # Check format is valid raw_format = frame[self.data_type_start] & 0x0F if raw_format < 0 or raw_format > 6:# TODO : Change with format lookup raise ValueError("Received format identifier "+str(frame)+" unknown.") value = unpack('>H',(frame[self.data_id_start:self.data_id_end]))[0] value = value & 0x3FF returned_instruction['var-id'] = value returned_instruction['var-type'] = raw_format returned_instruction['var-value'] = unpack(self.format_lookup[raw_format],frame[self.data_start:self.data_end])[0] returned_instruction['var-time'] = unpack('>f',frame[self.extraid_1_start:self.extraid_1_end])[0] returned_instruction['var-array-index'] = unpack('>H',frame[self.extraid_2_start:self.extraid_2_end])[0] return returned_instruction # alive-signal command if cmd == 0x03: returned_instruction['type'] = 'alive-signal' return returned_instruction # Emergency send if cmd == 0x09: returned_instruction['type'] = 'emergency-send' returned_instruction['data-id'] = frame[self.data_id_start:self.data_id_end].decode(encoding='UTF-8') # Check format is valid raw_format = frame[self.data_type_start] & 0x0F if raw_format < 0 or raw_format > 6:# TODO : Change with format lookup raise ValueError("Received format identifier "+str(frame)+" unknown.") returned_instruction['data-type'] = raw_format returned_instruction['data-time'] = unpack('>f',frame[self.extraid_1_start:self.extraid_1_end])[0] returned_instruction['data-index'] = unpack('>H',frame[self.extraid_2_start:self.extraid_2_end])[0] returned_instruction['data-value'] = unpack(self.format_lookup[raw_format],frame[self.data_start:self.data_end])[0] return returned_instruction raise ValueError("Received command "+str(cmd)+" unknown.")