def test_buffer(): ''' Test setting allocating buffer without setting data contents. ''' p = cPacket(iuid=1234, type_=PACKET_TYPES.DATA, buffer_size=1024) eq_(p.buffer_size, 1024) eq_(p.data(), b'')
def test_buffer_auto(): ''' Test auto-buffer based on data content. ''' p = cPacket(iuid=1234, type_=PACKET_TYPES.DATA, data=b'hello, world!') eq_(p.buffer_size, len(b'hello, world!')) eq_(p.data(), b'hello, world!')
def test_parse_data(): a = cPacket(iuid=1234, type_=PACKET_TYPES.DATA, data=b'hello') b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid) nt.eq_(a.crc, b.crc) nt.eq_(a.data(), b.data())
def test_default_packet(): ''' Test default packet allocation, i.e., no iuid, type, or data/buffer. ''' p = cPacket() eq_(p.iuid, 0) eq_(p.type_, PACKET_TYPES.NONE) eq_(p.buffer_size, 0)
def test_buffer_larger_than_data(): ''' Test setting data while allocating larger buffer. ''' p = cPacket(iuid=1234, type_=PACKET_TYPES.DATA, data=b'hello, world!', buffer_size=1024) eq_(p.data(), b'hello, world!') eq_(p.buffer_size, 1024)
def test_parse_data(): a = cPacket(iuid=1234, type_=PACKET_TYPES.DATA, data=b'hello') b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid) nt.eq_(a.crc, b.crc) nt.eq_(a.data(), b.data())
def test_parse_id_request(): ''' .. versionadded:: 0.13 ''' a = cPacket(iuid=1234, type_=PACKET_TYPES.ID_REQUEST) b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid)
def test_no_buffer(): ''' Test no buffer allocation. ''' p = cPacket(iuid=1234, type_=PACKET_TYPES.DATA) try: p.data() except RuntimeError as e: ok_('No buffer has been set/allocated.' in str(e))
def test_parse_id_request(): ''' .. versionadded:: 0.13 ''' a = cPacket(iuid=1234, type_=PACKET_TYPES.ID_REQUEST) b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid)
def test_parse_id_response(): ''' .. versionadded:: 0.13 ''' a = cPacket(iuid=1234, type_=PACKET_TYPES.ID_RESPONSE, data=b'{"id": "my device name"}') b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid) nt.eq_(a.crc, b.crc)
def test_parse_id_response(): ''' .. versionadded:: 0.13 ''' a = cPacket(iuid=1234, type_=PACKET_TYPES.ID_RESPONSE, data=b'{"id": "my device name"}') b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid) nt.eq_(a.crc, b.crc)
def _do_request_from_command_name(self, command_name, iuid=0, **kwargs): request = self._command_request_manager.request(command_name, **kwargs) packet = cPacket(iuid=iuid, type_=PACKET_TYPES.DATA, data=request) # Flush any remaining bytes from stream. self._stream.read() # Write request packet to stream. self._stream.write(packet.tostring()) parser = cPacketParser() data = np.array([ord(v) for v in self._stream.read()], dtype='uint8') start = datetime.now() wait_counts = 0 try: result = parser.parse(data) while not result: data = np.array([ord(v) for v in self._stream.read()], dtype='uint8') result = parser.parse(data) if (datetime.now() - start).total_seconds() > self._timeout: raise ValueError( 'Timeout while waiting for packet.\n"%s"' % (pformat(data.tostring()))) if not result: time.sleep(0.0001) wait_counts += 1 else: response_packet = result break except RuntimeError: raise ValueError('Error parsing response packet.\n"%s"' % (pformat(data.tostring()))) if response_packet.type_ == PACKET_TYPES.DATA: if command_name == 'ForwardI2cRequest': # This was a forwarded request, so we must return the undecoded # response data, since decoding is the responsibility of the # calling code. return response_packet.data() else: return (self._command_request_manager.response( response_packet.data())) else: raise ValueError('Invalid response. (%s).\n"%s"' % (response_packet.type_, pformat(data)))
def _do_request_from_command_name(self, command_name, iuid=0, **kwargs): request = self._command_request_manager.request(command_name, **kwargs) packet = cPacket(iuid=iuid, type_=PACKET_TYPES.DATA, data=request) # Flush any remaining bytes from stream. self._stream.read() # Write request packet to stream. self._stream.write(packet.tostring()) parser = cPacketParser() data = np.array([ord(v) for v in self._stream.read()], dtype='uint8') start = datetime.now() wait_counts = 0 try: result = parser.parse(data) while not result: data = np.array([ord(v) for v in self._stream.read()], dtype='uint8') result = parser.parse(data) if (datetime.now() - start).total_seconds() > self._timeout: raise ValueError('Timeout while waiting for packet.\n"%s"' % (pformat(data.tostring()))) if not result: time.sleep(0.0001) wait_counts += 1 else: response_packet = result break except RuntimeError: raise ValueError('Error parsing response packet.\n"%s"' % (pformat(data.tostring()))) if response_packet.type_ == PACKET_TYPES.DATA: if command_name == 'ForwardI2cRequest': # This was a forwarded request, so we must return the undecoded # response data, since decoding is the responsibility of the # calling code. return response_packet.data() else: return (self._command_request_manager .response(response_packet.data())) else: raise ValueError('Invalid response. (%s).\n"%s"' % (response_packet.type_, pformat(data)))
def test_parse_ack(): a = cPacket(iuid=1010, type_=PACKET_TYPES.ACK) b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid)
def test_cPacket(): ''' Test serialization of ``cPacket`` containing ``"hello, world!"``. ''' packet = cPacket(data=b'hello, world!', type_=PACKET_TYPES.DATA) eq_(packet.tobytes(), b'|||\x00\x00d\x00\rhello, world!\xfa5')
def test_parse_nack(): a = cPacket(iuid=4321, type_=PACKET_TYPES.NACK) b = parse_from_string(a.tobytes()) # TODO Should parse fail with `NACK` type? It currently does. nt.assert_false(b)
def test_parse_nack(): a = cPacket(iuid=4321, type_=PACKET_TYPES.NACK) b = parse_from_string(a.tobytes()) # TODO Should parse fail with `NACK` type? It currently does. nt.assert_false(b)
def test_parse_ack(): a = cPacket(iuid=1010, type_=PACKET_TYPES.ACK) b = parse_from_string(a.tobytes()) nt.eq_(a.type_, b.type_) nt.eq_(a.iuid, b.iuid)
def _send_command(self, packet): response = self.proxy.i2c_request(self.address, list(map(ord, packet.data()))) return cPacket(data=response.tostring(), type_=PACKET_TYPES.DATA)
from __future__ import absolute_import from nadamq.NadaMq import cPacket, PACKET_TYPES ID_REQUEST = cPacket(type_=PACKET_TYPES.ID_REQUEST).tostring() class ParseError(Exception): pass