Repository navigation
Add HKSV 17.99 camera capabilities, motion zones and global operating mode #532
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e12ebc
Add HKSV 17.99 camera services and characteristics to the catalog
RaHehl 207041c
Fix the camera operating mode characteristic permissions
RaHehl f83c74c
Add TLV8 codecs for the HKSV 17.99 camera values
RaHehl 576f6e7
Add HKSV 17.99 camera capabilities and motion zones
RaHehl 3dea2d5
Add the HKSV 17.99 camera global operating mode
RaHehl f3b5ce5
Merge remote-tracking branch 'upstream/dev' into hksv3-operating-mode
RaHehl a41aa48
Say why hksv3 has its own TLV list codec
RaHehl 80a22f9
Honor both HomeKit Camera Active values and end streams when turned off
RaHehl 0af78d7
Address the review of the global operating mode option
RaHehl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| """TLV8 values of the HomeKit Secure Video 17.99 camera services. | ||
|
|
||
| Follows the HAP TLV8 list encoding: each item of a list is a TLV of its own, | ||
| and items of the same type are separated by an empty TLV of type 0. Values | ||
| longer than 255 bytes are sent as 255-byte fragments of the same type; a TLV | ||
| continues the value before it only after such a full fragment. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterable | ||
| from dataclasses import dataclass, field | ||
| from enum import IntEnum | ||
| from typing import TypeVar | ||
| from uuid import UUID | ||
|
|
||
| SEPARATOR = b"\x00\x00" | ||
|
|
||
| # Version characteristic of the Camera Capabilities and Motion Zones services. | ||
| SERVICE_VERSION = "17.99" | ||
| CAPABILITIES_VERSION = 1 | ||
| ZONES_VERSION = 2 | ||
|
|
||
| _E = TypeVar("_E", bound=IntEnum) | ||
|
|
||
|
|
||
| class SensorType(IntEnum): | ||
| UNKNOWN = 0 | ||
| PRIMARY = 1 | ||
| GENERIC = 255 | ||
|
|
||
|
|
||
| class SensorIntent(IntEnum): | ||
| UNKNOWN = 0 | ||
| MAIN = 1 | ||
| PACKAGE = 2 | ||
| GENERIC = 255 | ||
|
|
||
|
|
||
| class VideoQuality(IntEnum): | ||
| HIGHEST = 1 | ||
| HIGH = 2 | ||
| MEDIUM = 3 | ||
| LOW = 4 | ||
|
|
||
|
|
||
| class ZoneMethod(IntEnum): | ||
| NORMAL = 1 | ||
| INVERTED = 2 | ||
|
|
||
|
|
||
| def encode_item(tag: int, value: bytes) -> bytes: | ||
| """Encode one value, fragmented into TLVs of at most 255 bytes.""" | ||
| if not value: | ||
| return bytes((tag, 0)) | ||
| return b"".join( | ||
| bytes((tag, len(chunk))) + chunk | ||
| for chunk in (value[i : i + 255] for i in range(0, len(value), 255)) | ||
| ) | ||
|
|
||
|
|
||
| def encode_list(tag: int, values: Iterable[bytes]) -> bytes: | ||
| """Encode the values as list items of one type.""" | ||
| return SEPARATOR.join(encode_item(tag, value) for value in values) | ||
|
|
||
|
|
||
| def decode_items(data: bytes) -> list[tuple[int, bytes]]: | ||
| """Split TLV8 data into its items, joining fragments and dropping separators. | ||
|
|
||
| Raises ``ValueError`` for truncated data. | ||
| """ | ||
| items: list[tuple[int, bytearray]] = [] | ||
| # Type of the full 255-byte fragment right before, which a TLV continues. | ||
| current: int | None = None | ||
| offset = 0 | ||
| while offset < len(data): | ||
| if offset + 2 > len(data): | ||
| raise ValueError(f"Truncated TLV header at offset {offset}") | ||
| tag, length = data[offset], data[offset + 1] | ||
| end = offset + 2 + length | ||
| if end > len(data): | ||
| raise ValueError(f"Truncated TLV value at offset {offset}") | ||
| value = data[offset + 2 : end] | ||
| offset = end | ||
| if tag == current: | ||
| items[-1][1].extend(value) | ||
| elif tag or length: | ||
| items.append((tag, bytearray(value))) | ||
| current = tag if length == 255 else None | ||
| return [(tag, bytes(value)) for tag, value in items] | ||
|
|
||
|
|
||
| def _fields(data: bytes) -> dict[int, list[bytes]]: | ||
| fields: dict[int, list[bytes]] = {} | ||
| for tag, value in decode_items(data): | ||
| fields.setdefault(tag, []).append(value) | ||
| return fields | ||
|
|
||
|
|
||
| def _required(fields: dict[int, list[bytes]], tag: int) -> bytes: | ||
| if tag not in fields: | ||
| raise ValueError(f"Missing TLV type {tag}") | ||
| return fields[tag][0] | ||
|
|
||
|
|
||
| def _uint(value: int, size: int) -> bytes: | ||
| return value.to_bytes(size, "little") | ||
|
|
||
|
|
||
| def _to_int(data: bytes) -> int: | ||
| if not 0 < len(data) <= 8: | ||
| raise ValueError(f"Invalid integer length {len(data)}") | ||
| return int.from_bytes(data, "little") | ||
|
|
||
|
|
||
| def _to_enum(enum_type: type[_E], data: bytes) -> _E | int: | ||
| # Values a newer controller may send are kept as plain ints. | ||
| value = _to_int(data) | ||
| try: | ||
| return enum_type(value) | ||
| except ValueError: | ||
| return value | ||
|
|
||
|
|
||
| def _to_uuid(data: bytes) -> UUID: | ||
| if len(data) != 16: | ||
| raise ValueError(f"Invalid UUID length {len(data)}") | ||
| return UUID(bytes=data) | ||
|
|
||
|
|
||
| @dataclass | ||
| class VideoStreamCapability: | ||
| """A video encoding the camera can produce from a sensor, bit rates in kbps.""" | ||
|
|
||
| identifier: UUID | ||
| quality: VideoQuality | int | ||
| width: int | ||
| height: int | ||
| frame_rate: int | ||
| average_bitrate: int | ||
| peak_bitrate: int | ||
|
|
||
| def encode(self) -> bytes: | ||
| return b"".join( | ||
| ( | ||
| encode_item(1, self.identifier.bytes), | ||
| encode_item(2, _uint(self.quality, 1)), | ||
| encode_item(3, _uint(self.width, 2)), | ||
| encode_item(4, _uint(self.height, 2)), | ||
| encode_item(5, _uint(self.frame_rate, 1)), | ||
| encode_item(6, _uint(self.average_bitrate, 4)), | ||
| encode_item(7, _uint(self.peak_bitrate, 4)), | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class SensorConfiguration: | ||
| """An image sensor of the camera and its video encodings.""" | ||
|
|
||
| sensor_uuid: UUID | ||
| width: int | ||
| height: int | ||
| sensor_type: SensorType | int = SensorType.PRIMARY | ||
| intent: SensorIntent | int = SensorIntent.MAIN | ||
| video_streams: list[VideoStreamCapability] = field(default_factory=list) | ||
|
|
||
| def encode(self) -> bytes: | ||
| dimensions = encode_item(1, _uint(self.width, 2)) + encode_item( | ||
| 2, _uint(self.height, 2) | ||
| ) | ||
| return b"".join( | ||
| ( | ||
| encode_item(1, dimensions), | ||
| encode_item(2, self.sensor_uuid.bytes), | ||
| encode_item(3, _uint(self.sensor_type, 1)), | ||
| encode_item(4, _uint(self.intent, 1)), | ||
| encode_list(5, (stream.encode() for stream in self.video_streams)), | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class CameraCapabilities: | ||
| """Value of the Camera Capabilities characteristic.""" | ||
|
|
||
| sensors: list[SensorConfiguration] | ||
| version: int = CAPABILITIES_VERSION | ||
|
|
||
| def encode(self) -> bytes: | ||
| sensors = encode_list(1, (sensor.encode() for sensor in self.sensors)) | ||
| return encode_item(1, _uint(self.version, 1)) + encode_item(2, sensors) | ||
|
|
||
|
|
||
| def encode_contributing_sensors(sensor_uuids: Iterable[UUID]) -> bytes: | ||
| """Encode the value of the Contributing Sensors characteristic.""" | ||
| return encode_list( | ||
| 1, (encode_item(1, sensor_uuid.bytes) for sensor_uuid in sensor_uuids) | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZonePolygon: | ||
| """A polygon in sensor pixels, the origin is the top left corner. | ||
|
|
||
| A controller may leave out the identifier, it is then None. | ||
| """ | ||
|
|
||
| identifier: UUID | None | ||
| vertices: list[tuple[int, int]] | ||
|
|
||
| def encode(self) -> bytes: | ||
| vertices = b"".join(_uint(x, 2) + _uint(y, 2) for x, y in self.vertices) | ||
| identifier = b"" | ||
| if self.identifier is not None: | ||
| identifier = encode_item(1, self.identifier.bytes) | ||
| return identifier + encode_item(3, vertices) | ||
|
|
||
| @classmethod | ||
| def decode(cls, data: bytes) -> ZonePolygon: | ||
| fields = _fields(data) | ||
| raw = fields.get(3, [b""])[0] | ||
| if len(raw) % 4: | ||
| raise ValueError(f"Invalid vertices length {len(raw)}") | ||
| vertices = [ | ||
| (_to_int(raw[i : i + 2]), _to_int(raw[i + 2 : i + 4])) | ||
| for i in range(0, len(raw), 4) | ||
| ] | ||
| identifier = _to_uuid(fields[1][0]) if 1 in fields else None | ||
| return cls(identifier, vertices) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZoneData: | ||
| """Polygons and how they form the region of interest. | ||
|
|
||
| A missing method is :attr:`ZoneMethod.NORMAL`. | ||
| """ | ||
|
|
||
| method: ZoneMethod | int | ||
| polygons: list[ZonePolygon] = field(default_factory=list) | ||
|
|
||
| def encode(self) -> bytes: | ||
| return encode_item(1, _uint(self.method, 1)) + encode_list( | ||
| 3, (polygon.encode() for polygon in self.polygons) | ||
| ) | ||
|
|
||
| @classmethod | ||
| def decode(cls, data: bytes) -> ZoneData: | ||
| fields = _fields(data) | ||
| method: ZoneMethod | int = ZoneMethod.NORMAL | ||
| if 1 in fields: | ||
| method = _to_enum(ZoneMethod, fields[1][0]) | ||
| return cls( | ||
| method, [ZonePolygon.decode(polygon) for polygon in fields.get(3, [])] | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class CameraZones: | ||
| """Value of the Camera Zones characteristic. | ||
|
|
||
| Only version 2 zone data is decoded, ``zones`` is empty for others. | ||
| """ | ||
|
|
||
| zones: list[ZoneData] = field(default_factory=list) | ||
| version: int = ZONES_VERSION | ||
|
|
||
| def encode(self) -> bytes: | ||
| return encode_item(1, _uint(self.version, 1)) + encode_list( | ||
| 2, (zone.encode() for zone in self.zones) | ||
| ) | ||
|
|
||
| @classmethod | ||
| def decode(cls, data: bytes) -> CameraZones: | ||
| fields = _fields(data) | ||
| version = _to_int(_required(fields, 1)) | ||
| if version != ZONES_VERSION: | ||
| return cls([], version) | ||
| return cls([ZoneData.decode(zone) for zone in fields.get(2, [])], version) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is
utils.hap_type_to_uuid, can you reuse it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hap_type_to_uuidtakes a HAP type string, here the TLV carries the 16 raw bytes. I dropped_to_uuidand useUUID(bytes=...)directly, it raisesValueErrorfor a wrong length by itself.