def process(self, request_adu): """ Process request ADU and return response. :param request_adu: A bytearray containing the ADU request. :return: A bytearray containing the response of the ADU request. """ validate_crc(request_adu) return super(RTUServer, self).process(request_adu)
def get_frame(adu): frame = adu[:2] i = 2 while 1: try: validate_crc(frame) break except (CRCError, struct.error) as e: frame += adu[i:i + 1] i += 1 return frame
def test_request_returning_server_device_failure_error(rtu_server, function): """ Validate response PDU of request returning excepetion response with error code 4. """ adu = function() function_code = struct.unpack('>B', adu[1:2])[0] rtu_server.serial_port.write(adu) rtu_server.serve_once() resp = rtu_server.serial_port.read(rtu_server.serial_port.in_waiting) validate_crc(resp) assert struct.unpack('>BB', resp[1:-2]) == (0x80 + function_code, 4)
def test_request_returning_invalid_data_value_error(rtu_server, function_code, quantity): """ Validate response PDU of request returning excepetion response with error code 3. """ starting_address = 0 slave_id = 1 adu = add_crc(struct.pack('>BBHH', slave_id, function_code, starting_address, quantity)) rtu_server.serial_port.write(adu) rtu_server.serve_once() resp = rtu_server.serial_port.read(rtu_server.serial_port.in_waiting) validate_crc(resp) assert struct.unpack('>BB', resp[1:-2]) == (0x80 + function_code, 3)
def parse_response_adu(resp_adu, req_adu=None): """ Parse response ADU and return response data. Some functions require request ADU to fully understand request ADU. :param resp_adu: Resonse ADU. :param req_adu: Request ADU, default None. :return: Response data. """ resp_pdu = resp_adu[1:-2] validate_crc(resp_adu) req_pdu = None if req_adu is not None: req_pdu = req_adu[1:-2] function = create_function_from_response_pdu(resp_pdu, req_pdu) return function.data
def parse_response_adu(resp_adu, req_adu=None): """ Parse response ADU and return response data. Some functions require request ADU to fully understand request ADU. :param resp_adu: Resonse ADU. :param req_adu: Request ADU, default None. :return: Response data. """ aes = AES.new(key, AES.MODE_CBC, IV) resp_pdu = aes.decrypt(resp_adu[1:-2]) validate_crc(resp_adu) req_pdu = None if req_adu is not None: req_pdu = req_adu[1:-2] aes = AES.new(key, AES.MODE_CBC, IV) req_pdu = aes.decrypt(req_pdu) function = create_function_from_response_pdu(resp_pdu, req_pdu) return function.data
def test_validate_invalid_crc(): """" Method should raise assertion error. """ with pytest.raises(CRCError): validate_crc(b'\x01\x02\x07')
def test_validate_valid_crc(): """" Method should not raise assertion error. """ validate_crc(b'\x00\x01\x02\xf1\x91')