smpclient 1.3.2__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {smpclient-1.3.2 → smpclient-2.0.1}/PKG-INFO +10 -5
- {smpclient-1.3.2 → smpclient-2.0.1}/README.md +5 -1
- {smpclient-1.3.2 → smpclient-2.0.1}/pyproject.toml +47 -7
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/__init__.py +40 -9
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/extensions/intercreate.py +1 -1
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/generics.py +13 -10
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/mcuboot.py +2 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/requests/image_management.py +10 -8
- smpclient-2.0.1/smpclient/requests/os_management.py +50 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/requests/shell_management.py +4 -6
- smpclient-2.0.1/smpclient/requests/user/intercreate.py +18 -0
- smpclient-2.0.1/smpclient/transport/__init__.py +50 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/transport/ble.py +37 -11
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/transport/serial.py +7 -1
- smpclient-1.3.2/smpclient/requests/os_management.py +0 -24
- smpclient-1.3.2/smpclient/requests/user/intercreate.py +0 -20
- smpclient-1.3.2/smpclient/transport/__init__.py +0 -28
- {smpclient-1.3.2 → smpclient-2.0.1}/LICENSE +0 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/exceptions.py +0 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/extensions/__init__.py +0 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/py.typed +0 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/requests/__init__.py +0 -0
- {smpclient-1.3.2 → smpclient-2.0.1}/smpclient/requests/user/__init__.py +0 -0
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: smpclient
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Simple Management Protocol (SMP) Client for remotely managing MCU firmware
|
|
5
5
|
License: Apache-2.0
|
|
6
6
|
Author: J.P. Hutchins
|
|
7
7
|
Author-email: jphutchins@gmail.com
|
|
8
|
-
Requires-Python: >=3.
|
|
8
|
+
Requires-Python: >=3.8.1,<3.13
|
|
9
9
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
10
|
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
12
|
Classifier: Programming Language :: Python :: 3.10
|
|
12
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
13
14
|
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
-
Requires-Dist: bleak (>=0.
|
|
15
|
+
Requires-Dist: bleak (>=0.22.1,<0.23.0)
|
|
15
16
|
Requires-Dist: intelhex (>=2.3.0,<3.0.0)
|
|
16
17
|
Requires-Dist: pyserial (>=3.5,<4.0)
|
|
17
|
-
Requires-Dist: smp (>=0.
|
|
18
|
+
Requires-Dist: smp (>=1.0.1,<2.0.0)
|
|
18
19
|
Description-Content-Type: text/markdown
|
|
19
20
|
|
|
20
21
|
# Simple Management Protocol (SMP) Client
|
|
@@ -54,12 +55,16 @@ If you'd like an SMP CLI application instead of a library, then you should try
|
|
|
54
55
|
```
|
|
55
56
|
poetry add -G dev <my_dev_dependency>
|
|
56
57
|
```
|
|
58
|
+
7. run tests for all supported python versions:
|
|
59
|
+
```
|
|
60
|
+
tox
|
|
61
|
+
```
|
|
57
62
|
|
|
58
63
|
## Development Environment Setup
|
|
59
64
|
|
|
60
65
|
### Install Dependencies
|
|
61
66
|
|
|
62
|
-
- python >=3.
|
|
67
|
+
- python >=3.8, <3.13
|
|
63
68
|
- poetry: https://python-poetry.org/docs/#installation
|
|
64
69
|
|
|
65
70
|
### Create the venv
|
|
@@ -35,12 +35,16 @@ If you'd like an SMP CLI application instead of a library, then you should try
|
|
|
35
35
|
```
|
|
36
36
|
poetry add -G dev <my_dev_dependency>
|
|
37
37
|
```
|
|
38
|
+
7. run tests for all supported python versions:
|
|
39
|
+
```
|
|
40
|
+
tox
|
|
41
|
+
```
|
|
38
42
|
|
|
39
43
|
## Development Environment Setup
|
|
40
44
|
|
|
41
45
|
### Install Dependencies
|
|
42
46
|
|
|
43
|
-
- python >=3.
|
|
47
|
+
- python >=3.8, <3.13
|
|
44
48
|
- poetry: https://python-poetry.org/docs/#installation
|
|
45
49
|
|
|
46
50
|
### Create the venv
|
|
@@ -19,12 +19,11 @@ mcuimg = "smpclient.mcuboot:mcuimg"
|
|
|
19
19
|
source = "git-tag"
|
|
20
20
|
|
|
21
21
|
[tool.poetry.dependencies]
|
|
22
|
-
python = ">=3.
|
|
22
|
+
python = ">=3.8.1, <3.13"
|
|
23
23
|
pyserial = "^3.5"
|
|
24
|
-
smp = "^0.
|
|
24
|
+
smp = "^1.0.1"
|
|
25
25
|
intelhex = "^2.3.0"
|
|
26
|
-
bleak = "^0.
|
|
27
|
-
|
|
26
|
+
bleak = "^0.22.1"
|
|
28
27
|
|
|
29
28
|
[tool.poetry.group.dev.dependencies]
|
|
30
29
|
pytest = "^7.4.3"
|
|
@@ -36,21 +35,62 @@ mypy = "^1.7.0"
|
|
|
36
35
|
mypy-extensions = "^1.0.0"
|
|
37
36
|
pytest-asyncio = "^0.23.2"
|
|
38
37
|
types-pyserial = "^3.5.0.11"
|
|
38
|
+
tox = "^4.15.0"
|
|
39
39
|
|
|
40
40
|
[tool.black]
|
|
41
41
|
line-length = 100
|
|
42
|
-
skip-string-normalization =
|
|
42
|
+
skip-string-normalization = true
|
|
43
|
+
extend-exclude = "dutfirmware|.venv|tests/fixtures|.tox"
|
|
43
44
|
|
|
44
45
|
[tool.isort]
|
|
45
46
|
profile = "black"
|
|
46
47
|
line_length = 100
|
|
47
48
|
multi_line_output = 3
|
|
49
|
+
skip = [
|
|
50
|
+
".venv",
|
|
51
|
+
"dutfirmware",
|
|
52
|
+
".tox",
|
|
53
|
+
]
|
|
48
54
|
|
|
49
55
|
[tool.mypy]
|
|
50
56
|
disallow_untyped_defs = true
|
|
51
|
-
exclude = ['.venv']
|
|
57
|
+
exclude = ['.venv', 'dutfirmware', '.tox']
|
|
52
58
|
|
|
53
59
|
[tool.pytest.ini_options]
|
|
60
|
+
norecursedirs = "dutfirmware/*"
|
|
54
61
|
filterwarnings = [
|
|
55
62
|
"ignore:The --rsyncdir:DeprecationWarning",
|
|
56
|
-
]
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.tox]
|
|
66
|
+
legacy_tox_ini = """
|
|
67
|
+
[tox]
|
|
68
|
+
min_version = 4.15
|
|
69
|
+
env_list =
|
|
70
|
+
py38
|
|
71
|
+
py39
|
|
72
|
+
py310
|
|
73
|
+
py311
|
|
74
|
+
py312
|
|
75
|
+
|
|
76
|
+
[testenv]
|
|
77
|
+
allowlist_externals =
|
|
78
|
+
poetry
|
|
79
|
+
black
|
|
80
|
+
isort
|
|
81
|
+
flake8
|
|
82
|
+
mypy
|
|
83
|
+
coverage
|
|
84
|
+
commands =
|
|
85
|
+
poetry install
|
|
86
|
+
black --check .
|
|
87
|
+
isort --check-only .
|
|
88
|
+
flake8 .
|
|
89
|
+
mypy .
|
|
90
|
+
coverage erase
|
|
91
|
+
pytest --cov --maxfail=1
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
[build-system]
|
|
95
|
+
requires = ["poetry-core>=1.0.0"]
|
|
96
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"""Simple Management Protocol (SMP) Client."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
3
6
|
from hashlib import sha256
|
|
4
|
-
from
|
|
7
|
+
from types import TracebackType
|
|
8
|
+
from typing import AsyncIterator, Final, Tuple, Type, cast
|
|
5
9
|
|
|
6
10
|
from pydantic import ValidationError
|
|
7
11
|
from smp import header as smpheader
|
|
@@ -10,8 +14,11 @@ from smp import message as smpmsg
|
|
|
10
14
|
from smpclient.exceptions import SMPBadSequence, SMPUploadError
|
|
11
15
|
from smpclient.generics import SMPRequest, TEr0, TEr1, TErr, TRep, error, flatten_error, success
|
|
12
16
|
from smpclient.requests.image_management import ImageUploadWrite
|
|
17
|
+
from smpclient.requests.os_management import MCUMgrParametersRead
|
|
13
18
|
from smpclient.transport import SMPTransport
|
|
14
19
|
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
15
22
|
|
|
16
23
|
class SMPClient:
|
|
17
24
|
def __init__(self, transport: SMPTransport, address: str):
|
|
@@ -22,6 +29,7 @@ class SMPClient:
|
|
|
22
29
|
async def connect(self) -> None:
|
|
23
30
|
"""Connect to the SMP server."""
|
|
24
31
|
await self._transport.connect(self._address)
|
|
32
|
+
await self._initialize()
|
|
25
33
|
|
|
26
34
|
async def disconnect(self) -> None:
|
|
27
35
|
"""Disconnect from the SMP server."""
|
|
@@ -38,12 +46,12 @@ class SMPClient:
|
|
|
38
46
|
raise SMPBadSequence("Bad sequence")
|
|
39
47
|
|
|
40
48
|
try:
|
|
41
|
-
return request.
|
|
49
|
+
return request._Response.loads(frame) # type: ignore
|
|
42
50
|
except ValidationError:
|
|
43
51
|
return flatten_error( # type: ignore
|
|
44
|
-
request.
|
|
52
|
+
request._ErrorV0.loads(frame)
|
|
45
53
|
if header.version == smpheader.Version.V0
|
|
46
|
-
else request.
|
|
54
|
+
else request._ErrorV1.loads(frame)
|
|
47
55
|
)
|
|
48
56
|
|
|
49
57
|
async def upload(
|
|
@@ -56,9 +64,9 @@ class SMPClient:
|
|
|
56
64
|
|
|
57
65
|
response = await self.request(
|
|
58
66
|
self._maximize_packet(
|
|
59
|
-
ImageUploadWrite(
|
|
67
|
+
ImageUploadWrite(
|
|
60
68
|
off=0,
|
|
61
|
-
data=b
|
|
69
|
+
data=b"",
|
|
62
70
|
image=slot,
|
|
63
71
|
len=len(image),
|
|
64
72
|
sha=sha256(image).digest(),
|
|
@@ -71,6 +79,8 @@ class SMPClient:
|
|
|
71
79
|
if error(response):
|
|
72
80
|
raise SMPUploadError(response)
|
|
73
81
|
elif success(response):
|
|
82
|
+
if response.off is None:
|
|
83
|
+
raise SMPUploadError(f"No offset received: {response=}")
|
|
74
84
|
yield response.off
|
|
75
85
|
else: # pragma: no cover
|
|
76
86
|
raise Exception("Unreachable")
|
|
@@ -78,11 +88,13 @@ class SMPClient:
|
|
|
78
88
|
# send chunks until the SMP server reports that the offset is at the end of the image
|
|
79
89
|
while response.off != len(image):
|
|
80
90
|
response = await self.request(
|
|
81
|
-
self._maximize_packet(ImageUploadWrite(off=response.off, data=b
|
|
91
|
+
self._maximize_packet(ImageUploadWrite(off=response.off, data=b""), image)
|
|
82
92
|
)
|
|
83
93
|
if error(response):
|
|
84
94
|
raise SMPUploadError(response)
|
|
85
95
|
elif success(response):
|
|
96
|
+
if response.off is None:
|
|
97
|
+
raise SMPUploadError(f"No offset received: {response=}")
|
|
86
98
|
yield response.off
|
|
87
99
|
else: # pragma: no cover
|
|
88
100
|
raise Exception("Unreachable")
|
|
@@ -91,11 +103,18 @@ class SMPClient:
|
|
|
91
103
|
def address(self) -> str:
|
|
92
104
|
return self._address
|
|
93
105
|
|
|
94
|
-
async def __aenter__(self) ->
|
|
106
|
+
async def __aenter__(self) -> "SMPClient":
|
|
95
107
|
await self.connect()
|
|
96
108
|
return self
|
|
97
109
|
|
|
98
|
-
async def __aexit__(
|
|
110
|
+
async def __aexit__(
|
|
111
|
+
self,
|
|
112
|
+
exc_type: Type[BaseException] | None,
|
|
113
|
+
exc_value: BaseException | None,
|
|
114
|
+
traceback: TracebackType | None,
|
|
115
|
+
) -> None:
|
|
116
|
+
if exc_value is not None:
|
|
117
|
+
logger.error(f"Exception in SMPClient: {exc_type=}, {exc_value=}, {traceback=}")
|
|
99
118
|
await self.disconnect()
|
|
100
119
|
|
|
101
120
|
@staticmethod
|
|
@@ -157,3 +176,15 @@ class SMPClient:
|
|
|
157
176
|
sha=request.sha,
|
|
158
177
|
upgrade=request.upgrade,
|
|
159
178
|
)
|
|
179
|
+
|
|
180
|
+
async def _initialize(self) -> None:
|
|
181
|
+
"""Gather initialization information from the SMP server."""
|
|
182
|
+
|
|
183
|
+
mcumgr_parameters = await self.request(MCUMgrParametersRead())
|
|
184
|
+
if success(mcumgr_parameters):
|
|
185
|
+
logger.debug(f"MCUMgr parameters: {mcumgr_parameters}")
|
|
186
|
+
self._transport.initialize(mcumgr_parameters.buf_size)
|
|
187
|
+
elif error(mcumgr_parameters):
|
|
188
|
+
logger.error(f"MCUMgr parameters error: {mcumgr_parameters}")
|
|
189
|
+
else:
|
|
190
|
+
raise Exception("Unreachable")
|
|
@@ -17,7 +17,7 @@ class ICUploadClient(SMPClient):
|
|
|
17
17
|
"""Iteratively upload `data` to the SMP server, yielding the offset."""
|
|
18
18
|
|
|
19
19
|
response = await self.request(
|
|
20
|
-
ic.ImageUploadWrite(off=0, data=b'', image=image, len=len(data))
|
|
20
|
+
ic.ImageUploadWrite(off=0, data=b'', image=image, len=len(data))
|
|
21
21
|
)
|
|
22
22
|
|
|
23
23
|
if error(response):
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
"""Some Generic helpers for the SMP module."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
3
5
|
from enum import IntEnum
|
|
4
|
-
from typing import Protocol, Type,
|
|
6
|
+
from typing import Protocol, Type, TypeVar, Union
|
|
5
7
|
|
|
6
8
|
from smp import error as smperror
|
|
7
9
|
from smp import header as smpheader
|
|
8
10
|
from smp import message as smpmessage
|
|
11
|
+
from typing_extensions import TypeGuard
|
|
9
12
|
|
|
10
13
|
TErrEnum = TypeVar("TErrEnum", bound=IntEnum)
|
|
11
14
|
TEr0 = TypeVar("TEr0", bound=smperror.ErrorV0)
|
|
12
15
|
TEr1 = TypeVar("TEr1", bound=smperror.ErrorV1)
|
|
13
16
|
TErr = TypeVar("TErr", bound='SMPError')
|
|
14
|
-
TRep = TypeVar("TRep", bound=smpmessage.ReadResponse
|
|
17
|
+
TRep = TypeVar("TRep", bound=Union[smpmessage.ReadResponse, smpmessage.WriteResponse])
|
|
15
18
|
|
|
16
19
|
|
|
17
20
|
class SMPError(smperror.ErrorV0[TErrEnum]):
|
|
@@ -53,17 +56,17 @@ class SMPRequest(Protocol[TRep, TEr0, TEr1, TErr]):
|
|
|
53
56
|
|
|
54
57
|
|
|
55
58
|
class ImageStatesRead(smpimg.ImageStatesReadRequest):
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
_Response = smpimg.ImageStatesReadResponse
|
|
60
|
+
_ErrorV0 = smpimg.ImageManagementError1
|
|
61
|
+
_ErrorV1 = smpimg.ImageManagementError2
|
|
62
|
+
_Error = ImageManagementError
|
|
60
63
|
```
|
|
61
64
|
"""
|
|
62
65
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
_Response: Type[TRep]
|
|
67
|
+
_ErrorV0: Type[TEr0]
|
|
68
|
+
_ErrorV1: Type[TEr1]
|
|
69
|
+
_Error: Type[TErr]
|
|
67
70
|
|
|
68
71
|
@property
|
|
69
72
|
def BYTES(self) -> bytes: # pragma: no cover
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import ClassVar
|
|
2
|
-
|
|
3
1
|
from smp import header as smpheader
|
|
4
2
|
from smp import image_management as smpimg
|
|
5
3
|
|
|
@@ -11,18 +9,22 @@ class ImageManagementError(SMPError[smpimg.IMG_MGMT_ERR]):
|
|
|
11
9
|
|
|
12
10
|
|
|
13
11
|
class _ImageGroupBase:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
_ErrorV0 = smpimg.ImageManagementErrorV0
|
|
13
|
+
_ErrorV1 = smpimg.ImageManagementErrorV1
|
|
14
|
+
_Error = ImageManagementError
|
|
17
15
|
|
|
18
16
|
|
|
19
17
|
class ImageStatesRead(smpimg.ImageStatesReadRequest, _ImageGroupBase):
|
|
20
|
-
|
|
18
|
+
_Response = smpimg.ImageStatesReadResponse
|
|
21
19
|
|
|
22
20
|
|
|
23
21
|
class ImageStatesWrite(smpimg.ImageStatesWriteRequest, _ImageGroupBase):
|
|
24
|
-
|
|
22
|
+
_Response = smpimg.ImageStatesWriteResponse
|
|
25
23
|
|
|
26
24
|
|
|
27
25
|
class ImageUploadWrite(smpimg.ImageUploadWriteRequest, _ImageGroupBase):
|
|
28
|
-
|
|
26
|
+
_Response = smpimg.ImageUploadProgressWriteResponse
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ImageErase(smpimg.ImageEraseRequest, _ImageGroupBase):
|
|
30
|
+
_Response = smpimg.ImageEraseResponse
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from smp import header as smpheader
|
|
2
|
+
from smp import os_management as smpos
|
|
3
|
+
|
|
4
|
+
from smpclient.generics import SMPError
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class OSManagementError(SMPError[smpos.OS_MGMT_RET_RC]):
|
|
8
|
+
_GROUP_ID = smpheader.GroupId.OS_MANAGEMENT
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _OSGroupBase:
|
|
12
|
+
_ErrorV0 = smpos.OSManagementErrorV0
|
|
13
|
+
_ErrorV1 = smpos.OSManagementErrorV1
|
|
14
|
+
_Error = OSManagementError
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class EchoWrite(smpos.EchoWriteRequest, _OSGroupBase):
|
|
18
|
+
_Response = smpos.EchoWriteResponse
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ResetWrite(smpos.ResetWriteRequest, _OSGroupBase):
|
|
22
|
+
_Response = smpos.ResetWriteResponse
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TaskStatisticsRead(smpos.TaskStatisticsReadRequest, _OSGroupBase):
|
|
26
|
+
_Response = smpos.TaskStatisticsReadResponse
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class MemoryPoolStatisticsRead(smpos.MemoryPoolStatisticsReadRequest, _OSGroupBase):
|
|
30
|
+
_Response = smpos.MemoryPoolStatisticsReadResponse
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class DateTimeRead(smpos.DateTimeReadRequest, _OSGroupBase):
|
|
34
|
+
_Response = smpos.DateTimeReadResponse
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DateTimeWrite(smpos.DateTimeWriteRequest, _OSGroupBase):
|
|
38
|
+
_Response = smpos.DateTimeWriteResponse
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class MCUMgrParametersRead(smpos.MCUMgrParametersReadRequest, _OSGroupBase):
|
|
42
|
+
_Response = smpos.MCUMgrParametersReadResponse
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class OSApplicationInfoRead(smpos.OSApplicationInfoReadRequest, _OSGroupBase):
|
|
46
|
+
_Response = smpos.OSApplicationInfoReadResponse
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class BootloaderInformationRead(smpos.BootloaderInformationReadRequest, _OSGroupBase):
|
|
50
|
+
_Response = smpos.BootloaderInformationReadResponse
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
from typing import ClassVar
|
|
2
|
-
|
|
3
1
|
from smp import header as smpheader
|
|
4
2
|
from smp import shell_management as smpshell
|
|
5
3
|
|
|
@@ -11,10 +9,10 @@ class ShellManagementError(SMPError[smpshell.SHELL_MGMT_RET_RC]):
|
|
|
11
9
|
|
|
12
10
|
|
|
13
11
|
class _ShellGroupBase:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
_ErrorV0 = smpshell.ShellManagementErrorV0
|
|
13
|
+
_ErrorV1 = smpshell.ShellManagementErrorV1
|
|
14
|
+
_Error = ShellManagementError
|
|
17
15
|
|
|
18
16
|
|
|
19
17
|
class Execute(smpshell.ExecuteRequest, _ShellGroupBase):
|
|
20
|
-
|
|
18
|
+
_Response = smpshell.ExecuteResponse
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from smp import header as smphdr
|
|
2
|
+
from smp.user import intercreate as smpic
|
|
3
|
+
|
|
4
|
+
from smpclient.generics import SMPError
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Error(SMPError[smpic.IC_MGMT_ERR]):
|
|
8
|
+
_GROUP_ID = smphdr.UserGroupId.INTERCREATE
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _GroupBase:
|
|
12
|
+
_ErrorV0 = smpic.ErrorV0
|
|
13
|
+
_ErrorV1 = smpic.ErrorV1
|
|
14
|
+
_Error = Error
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ImageUploadWrite(smpic.ImageUploadWriteRequest, _GroupBase):
|
|
18
|
+
_Response = smpic.ImageUploadWriteResponse
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Simple Management Protocol (SMP) Client Transport Protocol."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Protocol
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SMPTransport(Protocol):
|
|
9
|
+
_smp_server_transport_buffer_size: int | None = None
|
|
10
|
+
"""The SMP server transport buffer size, in 8-bit bytes."""
|
|
11
|
+
|
|
12
|
+
async def connect(self, address: str) -> None: # pragma: no cover
|
|
13
|
+
"""Connect the `SMPTransport`."""
|
|
14
|
+
|
|
15
|
+
async def disconnect(self) -> None: # pragma: no cover
|
|
16
|
+
"""Disconnect the `SMPTransport`."""
|
|
17
|
+
|
|
18
|
+
async def send(self, data: bytes) -> None: # pragma: no cover
|
|
19
|
+
"""Send the encoded `SMPRequest` `data`."""
|
|
20
|
+
|
|
21
|
+
async def receive(self) -> bytes: # pragma: no cover
|
|
22
|
+
"""Receive the decoded `SMPResponse`."""
|
|
23
|
+
|
|
24
|
+
async def send_and_receive(self, data: bytes) -> bytes: # pragma: no cover
|
|
25
|
+
"""Send the encoded `SMPRequest` `data` and receive the decoded `SMPResponse`."""
|
|
26
|
+
|
|
27
|
+
def initialize(self, smp_server_transport_buffer_size: int) -> None: # pragma: no cover
|
|
28
|
+
"""Initialize the `SMPTransport` with the server transport buffer size."""
|
|
29
|
+
self._smp_server_transport_buffer_size = smp_server_transport_buffer_size
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def mtu(self) -> int: # pragma: no cover
|
|
33
|
+
"""The Maximum Transmission Unit (MTU) in 8-bit bytes."""
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def max_unencoded_size(self) -> int: # pragma: no cover
|
|
37
|
+
"""The maximum size of an unencoded message that can be sent, in 8-bit bytes."""
|
|
38
|
+
|
|
39
|
+
# There is a potential speedup in the future by taking advantage of the
|
|
40
|
+
# multiple buffers that are provided by the SMP server implementation.
|
|
41
|
+
# Generally, the idea is to send as many as buf_count messages BEFORE
|
|
42
|
+
# awaiting the response. This will allow the SMP server to buffer the
|
|
43
|
+
# new IO while waiting for flash writes to complete. It creates some
|
|
44
|
+
# complexity in both the client and server and it's debatable whether
|
|
45
|
+
# or not the speedup is worth the complexity. Specifically, if there is
|
|
46
|
+
# an error in some write, then some of the writes that have already been
|
|
47
|
+
# sent out are no longer valid. That is, the response to each
|
|
48
|
+
# concurrent write needs to be tracked very carefully!
|
|
49
|
+
|
|
50
|
+
return self._smp_server_transport_buffer_size or self.mtu
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"""A Bluetooth Low Energy (BLE) SMPTransport."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
3
5
|
import asyncio
|
|
4
6
|
import logging
|
|
7
|
+
import platform
|
|
5
8
|
import re
|
|
6
|
-
from typing import Final
|
|
9
|
+
from typing import Final, List
|
|
7
10
|
from uuid import UUID
|
|
8
11
|
|
|
9
12
|
from bleak import BleakClient, BleakGATTCharacteristic, BleakScanner
|
|
@@ -11,13 +14,14 @@ from bleak.backends.device import BLEDevice
|
|
|
11
14
|
from smp import header as smphdr
|
|
12
15
|
|
|
13
16
|
from smpclient.exceptions import SMPClientException
|
|
17
|
+
from smpclient.transport import SMPTransport
|
|
14
18
|
|
|
15
19
|
SMP_SERVICE_UUID: Final = UUID("8D53DC1D-1DB7-4CD3-868B-8A527460AA84")
|
|
16
20
|
SMP_CHARACTERISTIC_UUID: Final = UUID("DA2E7828-FBCE-4E01-AE9E-261174997C48")
|
|
17
21
|
|
|
18
|
-
MAC_ADDRESS_PATTERN: Final = re.compile(r
|
|
22
|
+
MAC_ADDRESS_PATTERN: Final = re.compile(r"([0-9A-F]{2}[:]){5}[0-9A-F]{2}$", flags=re.IGNORECASE)
|
|
19
23
|
UUID_PATTERN: Final = re.compile(
|
|
20
|
-
r
|
|
24
|
+
r"^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z",
|
|
21
25
|
flags=re.IGNORECASE,
|
|
22
26
|
)
|
|
23
27
|
|
|
@@ -37,7 +41,7 @@ class SMPBLETransportNotSMPServer(SMPBLETransportException):
|
|
|
37
41
|
logger = logging.getLogger(__name__)
|
|
38
42
|
|
|
39
43
|
|
|
40
|
-
class SMPBLETransport:
|
|
44
|
+
class SMPBLETransport(SMPTransport):
|
|
41
45
|
def __init__(self) -> None:
|
|
42
46
|
self._buffer = bytearray()
|
|
43
47
|
self._notify_condition = asyncio.Condition()
|
|
@@ -45,7 +49,7 @@ class SMPBLETransport:
|
|
|
45
49
|
|
|
46
50
|
async def connect(self, address: str) -> None:
|
|
47
51
|
logger.debug(f"Scanning for {address=}")
|
|
48
|
-
device = (
|
|
52
|
+
device: BLEDevice | None = (
|
|
49
53
|
await BleakScanner.find_device_by_address(address) # type: ignore # upstream fix
|
|
50
54
|
if MAC_ADDRESS_PATTERN.match(address) or UUID_PATTERN.match(address)
|
|
51
55
|
else await BleakScanner.find_device_by_name(address) # type: ignore # upstream fix
|
|
@@ -64,6 +68,22 @@ class SMPBLETransport:
|
|
|
64
68
|
if smp_characteristic is None:
|
|
65
69
|
raise SMPBLETransportNotSMPServer("Missing the SMP characteristic UUID.")
|
|
66
70
|
else:
|
|
71
|
+
logger.debug(f"Found SMP characteristic: {smp_characteristic=}")
|
|
72
|
+
logger.info(f"{smp_characteristic.max_write_without_response_size=}")
|
|
73
|
+
if (
|
|
74
|
+
platform.system() == "Windows"
|
|
75
|
+
and smp_characteristic.max_write_without_response_size == 20
|
|
76
|
+
):
|
|
77
|
+
# https://github.com/hbldh/bleak/pull/1552#issuecomment-2105573291
|
|
78
|
+
logger.warning(
|
|
79
|
+
"The SMP characteristic MTU is 20 bytes, possibly a Windows bug, checking again"
|
|
80
|
+
)
|
|
81
|
+
await asyncio.sleep(2)
|
|
82
|
+
smp_characteristic._max_write_without_response_size = (
|
|
83
|
+
self._client._backend._session.max_pdu_size - 3 # type: ignore
|
|
84
|
+
)
|
|
85
|
+
logger.warning(f"{smp_characteristic.max_write_without_response_size=}")
|
|
86
|
+
|
|
67
87
|
self._smp_characteristic = smp_characteristic
|
|
68
88
|
|
|
69
89
|
logger.debug(f"Starting notify on {SMP_CHARACTERISTIC_UUID=}")
|
|
@@ -76,9 +96,6 @@ class SMPBLETransport:
|
|
|
76
96
|
logger.debug(f"Disconnected from {self._client.address}")
|
|
77
97
|
|
|
78
98
|
async def send(self, data: bytes) -> None:
|
|
79
|
-
# TODO: Unclear whether Bleak + SMP spec support transport-level fragmentation. We can
|
|
80
|
-
# continue this manual fragmentation for now, but it is not ideal.
|
|
81
|
-
|
|
82
99
|
logger.debug(f"Sending {len(data)} bytes, {self.mtu=}")
|
|
83
100
|
for offset in range(0, len(data), self.mtu):
|
|
84
101
|
await self._client.write_gatt_char(
|
|
@@ -132,6 +149,15 @@ class SMPBLETransport:
|
|
|
132
149
|
def mtu(self) -> int:
|
|
133
150
|
return self._smp_characteristic.max_write_without_response_size
|
|
134
151
|
|
|
135
|
-
@
|
|
136
|
-
def
|
|
137
|
-
|
|
152
|
+
@staticmethod
|
|
153
|
+
async def scan(timeout: int = 5) -> List[BLEDevice]:
|
|
154
|
+
"""Scan for BLE devices."""
|
|
155
|
+
logger.debug(f"Scanning for BLE devices for {timeout} seconds")
|
|
156
|
+
devices: Final = await BleakScanner(service_uuids=[str(SMP_SERVICE_UUID)]).discover(
|
|
157
|
+
timeout=timeout, return_adv=True
|
|
158
|
+
)
|
|
159
|
+
smp_servers: Final = [
|
|
160
|
+
d for d, a in devices.values() if SMP_SERVICE_UUID in {UUID(u) for u in a.service_uuids}
|
|
161
|
+
]
|
|
162
|
+
logger.debug(f"Found {len(smp_servers)} SMP devices: {smp_servers=}")
|
|
163
|
+
return smp_servers
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""A serial SMPTransport."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
3
5
|
import asyncio
|
|
4
6
|
import logging
|
|
5
7
|
import math
|
|
@@ -9,6 +11,9 @@ from typing import Final
|
|
|
9
11
|
|
|
10
12
|
from serial import Serial
|
|
11
13
|
from smp import packet as smppacket
|
|
14
|
+
from typing_extensions import override
|
|
15
|
+
|
|
16
|
+
from smpclient.transport import SMPTransport
|
|
12
17
|
|
|
13
18
|
logger = logging.getLogger(__name__)
|
|
14
19
|
|
|
@@ -31,7 +36,7 @@ def _base64_max(size: int) -> int:
|
|
|
31
36
|
return math.floor(3 / 4 * size) - 2
|
|
32
37
|
|
|
33
38
|
|
|
34
|
-
class SMPSerialTransport:
|
|
39
|
+
class SMPSerialTransport(SMPTransport):
|
|
35
40
|
_POLLING_INTERVAL_S = 0.005
|
|
36
41
|
|
|
37
42
|
class _ReadBuffer:
|
|
@@ -235,6 +240,7 @@ class SMPSerialTransport:
|
|
|
235
240
|
return self._mtu
|
|
236
241
|
|
|
237
242
|
@cached_property
|
|
243
|
+
@override
|
|
238
244
|
def max_unencoded_size(self) -> int:
|
|
239
245
|
"""The serial transport encodes each packet instead of sending SMP messages as raw bytes.
|
|
240
246
|
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
from typing import ClassVar
|
|
2
|
-
|
|
3
|
-
from smp import header as smpheader
|
|
4
|
-
from smp import os_management as smpos
|
|
5
|
-
|
|
6
|
-
from smpclient.generics import SMPError
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class OSManagementError(SMPError[smpos.OS_MGMT_RET_RC]):
|
|
10
|
-
_GROUP_ID = smpheader.GroupId.OS_MANAGEMENT
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class _OSGroupBase:
|
|
14
|
-
ErrorV0 = smpos.OSManagementErrorV0
|
|
15
|
-
ErrorV1 = smpos.OSManagementErrorV1
|
|
16
|
-
Error = OSManagementError
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class EchoWrite(smpos.EchoWriteRequest, _OSGroupBase):
|
|
20
|
-
Response: ClassVar = smpos.EchoWriteResponse
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class ResetWrite(smpos.ResetWriteRequest, _OSGroupBase):
|
|
24
|
-
Response: ClassVar = smpos.ResetWriteResponse
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
from typing import ClassVar
|
|
2
|
-
|
|
3
|
-
from smp import header as smpheader
|
|
4
|
-
from smp.user import intercreate as smpic
|
|
5
|
-
|
|
6
|
-
from smpclient.generics import SMPError
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Error(SMPError[smpic.IC_MGMT_ERR]):
|
|
10
|
-
_GROUP_ID = smpheader.GroupId.INTERCREATE
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class _GroupBase:
|
|
14
|
-
ErrorV0 = smpic.ErrorV0
|
|
15
|
-
ErrorV1 = smpic.ErrorV1
|
|
16
|
-
Error = Error
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class ImageUploadWrite(smpic.ImageUploadWriteRequest, _GroupBase):
|
|
20
|
-
Response: ClassVar = smpic.ImageUploadWriteResponse
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"""Simple Management Protocol (SMP) Client Transport Protocol."""
|
|
2
|
-
|
|
3
|
-
from typing import Protocol
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class SMPTransport(Protocol):
|
|
7
|
-
async def connect(self, address: str) -> None: # pragma: no cover
|
|
8
|
-
"""Connect the `SMPTransport`."""
|
|
9
|
-
|
|
10
|
-
async def disconnect(self) -> None: # pragma: no cover
|
|
11
|
-
"""Disconnect the `SMPTransport`."""
|
|
12
|
-
|
|
13
|
-
async def send(self, data: bytes) -> None: # pragma: no cover
|
|
14
|
-
"""Send the encoded `SMPRequest` `data`."""
|
|
15
|
-
|
|
16
|
-
async def receive(self) -> bytes: # pragma: no cover
|
|
17
|
-
"""Receive the decoded `SMPResponse`."""
|
|
18
|
-
|
|
19
|
-
async def send_and_receive(self, data: bytes) -> bytes: # pragma: no cover
|
|
20
|
-
"""Send the encoded `SMPRequest` `data` and receive the decoded `SMPResponse`."""
|
|
21
|
-
|
|
22
|
-
@property
|
|
23
|
-
def mtu(self) -> int: # pragma: no cover
|
|
24
|
-
"""The Maximum Transmission Unit (MTU) in 8-bit bytes."""
|
|
25
|
-
|
|
26
|
-
@property
|
|
27
|
-
def max_unencoded_size(self) -> int: # pragma: no cover
|
|
28
|
-
"""The maximum size of an unencoded message that can be sent in one MTU, in 8-bit bytes."""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|