Create log configuration
| 396 | |
| 397 | |
| 398 | class Log(): |
| 399 | """Create log configuration""" |
| 400 | |
| 401 | # These codes can be decoded using os.stderror, but |
| 402 | # some of the text messages will look very strange |
| 403 | # in the UI, so they are redefined here |
| 404 | _err_codes = { |
| 405 | errno.ENOMEM: 'No more memory available', |
| 406 | errno.ENOEXEC: 'Command not found', |
| 407 | errno.ENOENT: 'No such block id', |
| 408 | errno.E2BIG: 'Block too large', |
| 409 | errno.EEXIST: 'Block already exists' |
| 410 | } |
| 411 | |
| 412 | def __init__(self, crazyflie=None): |
| 413 | self.log_blocks = [] |
| 414 | # Called with newly created blocks |
| 415 | self.block_added_cb = Caller() |
| 416 | |
| 417 | self.cf = crazyflie |
| 418 | self.toc = None |
| 419 | self.cf.add_port_callback(CRTPPort.LOGGING, self._new_packet_cb) |
| 420 | |
| 421 | self.toc_updated = Caller() |
| 422 | self.state = IDLE |
| 423 | self.fake_toc_crc = 0xDEADBEEF |
| 424 | |
| 425 | self._refresh_callback = None |
| 426 | self._toc_cache = None |
| 427 | |
| 428 | self._config_id_counter = 1 |
| 429 | |
| 430 | self._useV2 = False |
| 431 | |
| 432 | def add_config(self, logconf): |
| 433 | """Add a log configuration to the logging framework. |
| 434 | |
| 435 | When doing this the contents of the log configuration will be validated |
| 436 | and listeners for new log configurations will be notified. When |
| 437 | validating the configuration the variables are checked against the TOC |
| 438 | to see that they actually exist. If they don't then the configuration |
| 439 | cannot be used. Since a valid TOC is required, a Crazyflie has to be |
| 440 | connected when calling this method, otherwise it will fail.""" |
| 441 | |
| 442 | if not self.cf.link: |
| 443 | logger.error('Cannot add configs without being connected to a ' |
| 444 | 'Crazyflie!') |
| 445 | return |
| 446 | |
| 447 | # If the log configuration contains variables that we added without |
| 448 | # type (i.e we want the stored as type for fetching as well) then |
| 449 | # resolve this now and add them to the block again. |
| 450 | for name in logconf.default_fetch_as: |
| 451 | var = self.toc.get_element_by_complete_name(name) |
| 452 | if not var: |
| 453 | logger.warning( |
| 454 | '%s not in TOC, this block cannot be used!', name) |
| 455 | logconf.valid = False |