Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cobs #126

Merged
merged 13 commits into from Oct 10, 2020
Merged

Cobs #126

merged 13 commits into from Oct 10, 2020

Conversation

flytrex-vadim
Copy link

Switched from brute-force escaping to COBS for serial protocol serialization

Copy link
Member

@pavel-kirienko pavel-kirienko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the forum:

At the whole pyuavcan level there are some dsdl test failures that are probably setup-related (PYTHONASYNCIODEBUG)

You should be able to fix that by exporting PYTHONASYNCIODEBUG=1, or just by running test.sh.

Some of the docs make references to escaping which may need reviewing. For example, the docs for the method SerialFrame.compile_into talk about escaping, is it still valid?

The docs would benefit if you added a link to https://forum.uavcan.org/t/uavcan-serial-issues-with-dma-friendliness-and-bandwidth-overhead/846 because that thread provides an exhaustive explanation of why COBS has been chosen.

@@ -64,13 +64,6 @@ def _process_byte(self, b: int, timestamp: pyuavcan.transport.Timestamp) -> None
return

# Unescaping is done only if we're inside a frame currently.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangling comment


out_buffer[next_byte_index] = self.FRAME_DELIMITER_BYTE
next_byte_index += 1

assert (next_byte_index - 2) >= (len(header) + len(self.payload) + len(payload_crc_bytes))
return memoryview(out_buffer)[:next_byte_index]

@staticmethod
def calc_cobs_size(payload_size_bytes: int) -> int:
return int(math.ceil(payload_size_bytes * 255.0 / 254.0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to avoid floating point when dealing with discrete entities like bytes for simplicity and to provide cleaner implementation examples for people who will be re-implementing this logic for embedded systems.

Suggested change
return int(math.ceil(payload_size_bytes * 255.0 / 254.0))
return (payload_size_bytes * 255 + 253) // 254

This change is equivalent which can be verified:

>>> set(int(math.ceil(payload_size_bytes * 255.0 / 254.0)) - ((payload_size_bytes * 255 + 253) // 254) for payload_size_bytes in range(10000000))
{0}

The math import is no longer needed.

pyuavcan/transport/serial/__init__.py Outdated Show resolved Hide resolved
@flytrex-vadim
Copy link
Author

The docs for the method SerialFrame.compile_into reads valid to me, considering COBS is also an escaping of sorts.
Other remarks fixed.

Copy link
Member

@pavel-kirienko pavel-kirienko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one issue found by MyPy. Please accept my suggestion, then we should be good to go. The other issues from MyPy are caused by the fact that its version number is not yet frozen (because the library is still being actively developed); a new one was released which found extra issues in unrelated code. I should freeze it soon.

import dataclasses
import pyuavcan
from cobs import cobs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to suppress the MyPy warning about the type hints being unavailable.

Suggested change
from cobs import cobs
from cobs import cobs # type: ignore

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, just committed this fix

@coveralls
Copy link

Pull Request Test Coverage Report for Build 35677795

  • 40 of 40 (100.0%) changed or added relevant lines in 2 files are covered.
  • 28 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.73%

Files with Coverage Reduction New Missed Lines %
pyuavcan/transport/can/media/socketcan/_socketcan.py 1 80.56%
pyuavcan/transport/serial/_frame.py 1 99.09%
.test_dsdl_generated/uavcan/file/Write_1_0.py 1 86.63%
.test_dsdl_generated/uavcan/register/Value_1_0.py 25 86.34%
Totals Coverage Status
Change from base Build 35571550: -0.02%
Covered Lines: 20121
Relevant Lines: 21985

💛 - Coveralls

@pavel-kirienko pavel-kirienko merged commit 8e95d1c into OpenCyphal:master Oct 10, 2020
@flytrex-vadim flytrex-vadim deleted the cobs branch October 11, 2020 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UAVCAN/serial prototype: switch from byte stuffing to COBS
4 participants