예제 #1
0
def test_handler_callbacks():
    handler = Handler(None, None)
    global_counter1 = TestCallbackCounter()
    global_counter2 = TestCallbackCounter()
    msg_type_counter1 = TestCallbackCounter()
    msg_type_counter2 = TestCallbackCounter()
    handler.add_callback(global_counter1)
    handler.add_callback(global_counter2)
    handler.add_callback(global_counter2)
    handler.add_callback(msg_type_counter1, 0x55)
    handler.add_callback(msg_type_counter1, 0x55)
    handler.add_callback(msg_type_counter2, 0x66)
    handler.call(SBP(0x11, None, None, None, None))
    handler.call(SBP(0x55, None, None, None, None))
    assert global_counter1.value == 2
    assert global_counter2.value == 2
    assert msg_type_counter1.value == 1
    assert msg_type_counter2.value == 0
    handler.remove_callback(global_counter1)
    handler.remove_callback(global_counter2)
    handler.remove_callback(msg_type_counter1, 0x55)
    handler.remove_callback(msg_type_counter2, 0x66)
    handler.call(SBP(0x11, None, None, None, None))
    handler.call(SBP(0x55, None, None, None, None))
    assert global_counter1.value == 2
    assert global_counter2.value == 2
    assert msg_type_counter1.value == 1
    assert msg_type_counter2.value == 0
예제 #2
0
def test_available_messages():
    """
  Simple example with a limited dispatch table.

  """
    table = {
        acq.SBP_MSG_ACQ_RESULT_DEP_A: acq.MsgAcqResultDepA,
        log.SBP_MSG_PRINT_DEP: log.MsgPrintDep
    }
    msg = SBP(msg_type=0x15,
              sender=1219,
              length=13,
              payload='\x92$yA\x00\x00\xbcC\x81\xc1\xf9\xc5\x1d')
    # TODO (Buro): Replace this message constructor once generated SBP
    # can support kwargs for constructor, instead of requiring SBP
    # object.
    assert dispatch(msg, table) == acq.MsgAcqResultDepA(msg)
    msg = SBP(msg_type=0xB0, sender=1219, length=4, payload='v1.2', crc=0xCE01)
    with warnings.catch_warnings(record=True) as w:
        dispatch(msg, table)
        warnings.simplefilter("always")
        assert len(w) == 1
        assert issubclass(w[0].category, RuntimeWarning)
        assert str(
            w[0].message).find("No message found for msg_type id 176 for msg*")
예제 #3
0
def test_filter():
  msgs = ((SBP(0x11, None, None, None, None), {}),
          (SBP(0x33, None, None, None, None), {}),
          (SBP(0x55, None, None, None, None), {}))
  handler = Handler(msgs)
  def latestart():
    time.sleep(0.1)
    handler.start()
  threading.Thread(target=latestart).start()
  middle = tuple(handler.filter(0x33))
  assert len(middle) == 1
  assert middle[0] == msgs[1]
예제 #4
0
def test_rolling_json_log():
  """
  Rolling JSON log iterator sanity tests.
  """
  count = 0
  import os, tempfile, time
  # Duration of test
  test_interval = 6
  # Rotating interval
  r_interval = 2
  try:
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as tf:
      #print tf.name
      with RotatingFileLogger(tf.name, when='S', interval=r_interval) as log:
        t0 = time.time()
        t = time.time()
        msg = SBP(0x10, 2, 3, 'abc\n', 4)
        msgs = []
        while t - t0 < test_interval:
          log(msg, delta=t-t0, timestamp=t)
          if t - t0 <= r_interval:
            msgs.append(msg)
          t = time.time()
      i = 0
      with JSONLogIterator(tf.name) as log:
        for msg, metadata in log.next():
          assert isinstance(msg, MsgPrintDep)
          assert msg.text == "abc\n"
          i += 1
      assert i > 0
      assert i <= len(msgs)
  except Exception:
    raise
  finally:
    os.unlink(tf.name)
 def _receive(self):
     """
     Read and build SBP message.
     """
     # preamble - not readall(1) to allow breaking before messages,
     # empty input
     preamble = self._read(1)
     if not preamble:
         return None
     elif ord(preamble) != SBP_PREAMBLE:
         if self._verbose:
             print("Host Side Unhandled byte: 0x%02x" % ord(preamble))
         return None
     # hdr
     hdr = self._readall(5)
     msg_crc = crc16(hdr)
     msg_type, sender, msg_len = struct.unpack("<HHB", hdr)
     # data
     data = self._readall(msg_len)
     msg_crc = crc16(data, msg_crc)
     # crc
     crc = self._readall(2)
     crc, = struct.unpack("<H", crc)
     if crc != msg_crc:
         if self._verbose:
             print("crc mismatch: 0x%04X 0x%04X" % (msg_crc, crc))
         return None
     msg = SBP(msg_type, sender, msg_len, data, crc)
     try:
         msg = self._dispatch(msg)
     except Exception as exc:
         warnings.warn("SBP dispatch error: %s" % (exc, ))
     return msg
예제 #6
0
def test_multiple_handler_callbacks():
    handler = Handler(None, None)
    msg_type_counter1 = TestCallbackCounter()
    msg_type_counter2 = TestCallbackCounter()
    handler.add_callback(msg_type_counter1, [0x55, 0x66])
    handler.add_callback(msg_type_counter2, [0x11, 0x55])
    handler.call(SBP(0x11, None, None, None, None))
    handler.call(SBP(0x55, None, None, None, None))
    assert msg_type_counter1.value == 1
    assert msg_type_counter2.value == 2
    handler.remove_callback(msg_type_counter1, [0x55, 0x66])
    handler.remove_callback(msg_type_counter2, [0x11, 0x55])
    handler.call(SBP(0x11, None, None, None, None))
    handler.call(SBP(0x55, None, None, None, None))
    assert msg_type_counter1.value == 1
    assert msg_type_counter2.value == 2
예제 #7
0
def test_multiple_handler_callbacks():
  handler = Handler(())
  msg_type_counter1 = CallbackCounter()
  msg_type_counter2 = CallbackCounter()
  handler.add_callback(msg_type_counter1, [0x55, 0x66])
  handler.add_callback(msg_type_counter2, [0x11, 0x55])
  handler._call(SBP(0x11, None, None, None, None), x=1, y=6)
  handler._call(SBP(0x55, None, None, None, None))
  assert msg_type_counter1.value == 1
  assert msg_type_counter2.value == 2
  handler.remove_callback(msg_type_counter1, [0x55, 0x66])
  handler.remove_callback(msg_type_counter2, [0x11, 0x55])
  handler._call(SBP(0x11, None, None, None, None), delta=0)
  handler._call(SBP(0x55, None, None, None, None), something=False)
  assert msg_type_counter1.value == 1
  assert msg_type_counter2.value == 2
예제 #8
0
def rewrite(records, outfile):
    """
    Returns array of (timestamp in sec, SBP object, parsed).
    skips unparseable objects.

    """
    new_datafile = open(outfile, 'w')
    if not records:
        print("No SBP log records passed to rewrite function. Exiting.")
        return

    items = []
    i = 0
    for (timestamp, msg_type, sender_id, msg_len, bin_data) in records:
        sbp = SBP(msg_type, sender_id, msg_len, bin_data, 0x1337)
        try:
            _SBP_TABLE[msg_type](sbp)
            item = (timestamp, sbp, dispatch(sbp))
            items.append(item)
            m = {
                "time": timestamp,
                "data": dispatch(sbp).to_json_dict(),
                "metadata": {}
            }
            new_datafile.write(json.dumps(m) + "\n")
        except Exception:
            print("Exception received for message type {0}.".format(
                _SBP_TABLE[msg_type]))
            import traceback
            print(traceback.format_exc())
            i += 1
            continue
    print("Of %d records, skipped %i." % (len(records), i))
    return items
예제 #9
0
def reset(port='/dev/ttyUSB0', baud=115200):
    '''
    Resets the Multi.

    Args:
        port: serial port [[default='/dev/ttyUSB0']
        baud: baud rate [default=115200]

    Returns:
        None
    '''

    print('Reading from {} at {}'.format(port, baud))

    verify = raw_input('Are you sure want to reset the Multi? Y/[n]: ') or 'n'
    if verify == 'Y':
        print('Retting the Multi ...')
        with PySerialDriver(port, baud) as driver:
            reset_sbp = SBP(SBP_MSG_RESET)
            reset_sbp.payload = ''
            reset_sbp = reset_sbp.pack()
            driver.write(reset_sbp)
        print('Resetted the Multi.')
    else:
        print('Skipped resetting.')
예제 #10
0
def test_listener_thread_ok():
    sema = TestCallbackSemaphore()
    listener_thread = ReceiveThread(lambda: SBP(True, None, None, None, None),
                                    sema)
    listener_thread.start()
    assert listener_thread.is_alive()
    until(lambda: sema.sema.acquire(False))
    listener_thread.stop()
    until(lambda: listener_thread.is_alive())
    until(lambda: sema.sema.acquire(False))
예제 #11
0
def test_dead_gc():
  handler = Handler(((SBP(0x11, None, None, None, None), {}),))
  def latestart():
    time.sleep(0.1)
    handler.start()
  thread = threading.Thread(target=latestart)
  thread.start()
  iter(handler) # Dead iterator, should be gc'd
  xx = iter(handler)
  thread.join()
  handler._receive_thread.join()
  gc.collect()
  assert [sink() for sink in handler._sinks] == [xx]
예제 #12
0
def rewrite(records, outfile):
    """
  Returns array of (time delta offset from beginning of log in msec,
  timestamp in sec, SBP object, parsed). skips unparseable objects.

  """
    new_datafile = open(outfile, 'w')
    if not records:
        print "No SBP log records passed to rewrite function. Exiting."
        return
    start_t, msg_type, sender_id, msg_len, bin_data = records[0]
    items = []
    i = 0
    for (timestamp, msg_type, sender_id, msg_len, bin_data) in records:
        sbp = SBP(msg_type, sender_id, msg_len, bin_data, 0x1337)
        try:
            _SBP_TABLE[msg_type](sbp)
            deltat = (timestamp - start_t) * 1000.
            item = (deltat, timestamp, sbp, dispatch(sbp))
            items.append(item)
            m = {
                "delta": deltat,
                "timestamp": timestamp,
                "data": dispatch(sbp).to_json_dict(),
                "metadata": {}
            }
            new_datafile.write(json.dumps(m) + "\n")
        except Exception as exc_info:
            print "Exception received for message type {0}.".format(
                _SBP_TABLE[msg_type])
            import traceback
            print traceback.format_exc()
            i += 1
            continue
    print "Of %d records, skipped %i." % (len(records), i)
    return items
예제 #13
0
def test_udp_logger():
  msg = SBP(1, 2, 3, 'abc', 4)
  handler = udp_handler(msg.pack())
  ip, port = udp_server(handler)
  with UdpLogger(ip, port) as udp:
    udp(msg)