meshtastic/docs/development/python/index.mdx

118 lines
3.4 KiB
Plaintext
Raw Normal View History

---
2022-11-04 10:06:45 -07:00
id: python
2022-07-03 12:12:36 -07:00
title: Meshtastic Python Development
2022-11-04 10:06:45 -07:00
sidebar_position: 5
sidebar_label: Python
---
## A note to developers of this lib
2022-10-31 21:26:19 -07:00
We use the Visual Studio Code (VScode) default python formatting conventions (autopep8). So if you use that IDE you should be able to use "Format Document" and not generate unrelated diffs. If you use some other editor, please do not change formatting on lines you have not changed yourself.
2022-11-02 11:46:54 -07:00
### Building
2023-01-19 05:01:57 -08:00
```shell title="To build a new release"
apt install pandoc
sudo pip3 install markdown pdoc3 webencodings pyparsing twine autopep8 pylint pytest pytest-cov
```
2023-01-19 05:01:57 -08:00
```shell title="For development"
pip3 install -r requirements.txt
```
2022-11-02 11:46:54 -07:00
### Linting
2023-01-19 05:01:57 -08:00
```shell
pylint meshtastic
```
2023-01-19 05:01:57 -08:00
2022-11-02 11:46:54 -07:00
### Testing
2023-01-19 05:01:57 -08:00
#### Install and run pytest
2023-01-19 05:01:57 -08:00
- For more verbosity, add `-v` or even `-vv`
2023-01-19 05:01:57 -08:00
```shell
2022-11-02 11:46:54 -07:00
pip3 install .
pytest -vv
```
2023-01-19 05:01:57 -08:00
```shell title="Run just unit tests"
pytest
# or (more verbosely)
pytest -m unit
# or
make
```
2023-01-19 05:01:57 -08:00
```shell title="Run just integration tests"
pytest -m int
```
2023-01-19 05:01:57 -08:00
```shell title="Run the smoke test with only one device connected serially (aka smoke1)"
pytest -m smoke1
```
2022-11-02 11:46:54 -07:00
:::caution
Running `smoke1` will reset values on the device, including the region to 1 (US).
Be sure to hit the reset button on the device after the test is completed.
2022-11-02 11:46:54 -07:00
:::
2023-01-19 05:01:57 -08:00
```shell title="Run the smoke test with only two device connected serially (aka smoke2)"
pytest -m smoke2
```
2023-01-19 05:01:57 -08:00
```shell title="Run the wifi smoke test"
pytest -m smokewifi
```
2023-01-19 05:01:57 -08:00
```shell title="Run a specific test"
pytest -msmoke1 meshtastic/tests/test_smoke1.py::test_smoke1_info
2022-10-31 21:26:19 -07:00
# or to run a specific smoke2 test
pytest -m smoke2 meshtastic/tests/test_smoke2.py::test_smoke2_info
2022-10-31 21:26:19 -07:00
# or to run a specific smoke_wifi test
pytest -m smokewifi meshtastic/tests/test_smoke_wifi.py::test_smokewifi_info
```
2022-11-02 11:46:54 -07:00
**Add another classification of tests such as `unit` or `smoke1`**
2022-10-31 21:26:19 -07:00
See [pytest.ini](https://github.com/meshtastic/Meshtastic-python/blob/master/pytest.ini).
2023-01-19 05:01:57 -08:00
```shell title="To see the unit test code coverage"
pytest --cov=meshtastic
# or if want html coverage report
pytest --cov-report html --cov=meshtastic
# or
make cov
```
2023-01-19 05:01:57 -08:00
```shell title="To see slowest unit tests, you can run"
pytest --durations=0
# or
make slow
```
2022-11-02 11:46:54 -07:00
### Generate the Python API documentation
2022-10-31 21:26:19 -07:00
Pre-generated: [API documentation](https://python.meshtastic.org)
2023-01-19 05:01:57 -08:00
```shell
bin/regen-docs.sh
```
2022-10-31 21:26:19 -07:00
## Wire encoding
When sending protobuf packets over serial or TCP each packet is preceded by uint32 sent in network byte order (big endian).
The upper 16 bits must be 0x94C3. The lower 16 bits are packet length (this encoding gives room to eventually allow quite large packets).
Implementations validate length against the maximum possible size of a BLE packet (our lowest common denominator) of 512 bytes. If the
length provided is larger than that we assume the packet is corrupted and begin again looking for 0x4403 framing.
The packets flowing towards the device are ToRadio protobufs, the packets flowing from the device are FromRadio protobufs.
The 0x94C3 marker can be used as framing to (eventually) resync if packets are corrupted over the wire.
Note: the 0x94C3 framing was chosen to prevent confusion with the 7 bit ascii character set. It also doesn't collide with any valid utf8 encoding. This makes it a bit easier to start a device outputting regular debug output on its serial port and then only after it has received a valid packet from the PC, turn off unencoded debug printing and switch to this
packet encoding.