MCPcopy Create free account
hub / github.com/bitcraze/crazyflie-lib-python / _IncomingPacketHandler

Class _IncomingPacketHandler

cflib/crazyflie/__init__.py:357–419  ·  view source on GitHub ↗

Handles incoming packets and sends the data to the correct receivers

Source from the content-addressed store, hash-verified

355
356
357class _IncomingPacketHandler(Thread):
358 """Handles incoming packets and sends the data to the correct receivers"""
359
360 def __init__(self, cf):
361 Thread.__init__(self)
362 self.cf = cf
363 self.cb = []
364
365 def add_port_callback(self, port, cb):
366 """Add a callback for data that comes on a specific port"""
367 logger.debug('Adding callback on port [%d] to [%s]', port, cb)
368 self.add_header_callback(cb, port, 0, 0xff, 0x0)
369
370 def remove_port_callback(self, port, cb):
371 """Remove a callback for data that comes on a specific port"""
372 logger.debug('Removing callback on port [%d] to [%s]', port, cb)
373 for port_callback in self.cb:
374 if port_callback.port == port and port_callback.callback == cb:
375 self.cb.remove(port_callback)
376
377 def add_header_callback(self, cb, port, channel, port_mask=0xFF,
378 channel_mask=0xFF):
379 """
380 Add a callback for a specific port/header callback with the
381 possibility to add a mask for channel and port for multiple
382 hits for same callback.
383 """
384 self.cb.append(_CallbackContainer(port, port_mask,
385 channel, channel_mask, cb))
386
387 def run(self):
388 while True:
389 if self.cf.link is None:
390 time.sleep(1)
391 continue
392 pk = self.cf.link.receive_packet(1)
393
394 if pk is None:
395 continue
396
397 # All-packet callbacks
398 self.cf.packet_received.call(pk)
399
400 found = False
401 for cb in (cb for cb in self.cb
402 if cb.port == (pk.port & cb.port_mask) and
403 cb.channel == (pk.channel & cb.channel_mask)):
404 try:
405 cb.callback(pk)
406 except Exception: # pylint: disable=W0703
407 # Disregard pylint warning since we want to catch all
408 # exceptions and we can't know what will happen in
409 # the callbacks.
410 import traceback
411
412 logger.error('Exception while doing callback on port'
413 ' [%d]\n\n%s', pk.port,
414 traceback.format_exc())

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected