Skip to content

Commit

Permalink
Add service data (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Oct 19, 2023
1 parent 9c77b1f commit 093d4bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 3 additions & 1 deletion improv_ble_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

from . import errors
from .client import ImprovBLEClient, device_filter
from .protocol import SERVICE_UUID, Error, State
from .protocol import SERVICE_DATA_UUID, SERVICE_UUID, Error, ImprovServiceData, State

__all__ = [
"SERVICE_DATA_UUID",
"SERVICE_UUID",
"Error",
"State",
"ImprovBLEClient",
"ImprovServiceData",
"device_filter",
"errors",
]
17 changes: 15 additions & 2 deletions improv_ble_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CHARACTERISTIC_UUID_RPC_RESULT,
CHARACTERISTIC_UUID_STATE,
IMPROV_CHARACTERISTICS,
SERVICE_DATA_UUID,
SERVICE_UUID,
STATE_MAP,
parse_result,
Expand All @@ -57,9 +58,21 @@


def device_filter(advertisement_data: AdvertisementData) -> bool:
"""Return True if the device is supported."""
"""Return True if the device is supported and ready to be provisioned."""
uuids = advertisement_data.service_uuids
return SERVICE_UUID in uuids
service_data = advertisement_data.service_data
if SERVICE_UUID not in uuids or SERVICE_DATA_UUID not in service_data:
return False
try:
improv_service_data = prot.ImprovServiceData.from_bytes(
service_data[SERVICE_DATA_UUID]
)
except InvalidCommand:
return False
return improv_service_data.state not in (
prot.State.PROVISIONING,
prot.State.PROVISIONED,
)


class NotificationHandler:
Expand Down
24 changes: 23 additions & 1 deletion improv_ble_client/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .errors import InvalidCommand

SERVICE_UUID: Final = "00467768-6228-2272-4663-277478268000"
SERVICE_DATA_UUID: Final = "00004677-0000-1000-8000-00805f9b34fb"
CHARACTERISTIC_UUID_CAPABILITIES: Final = "00467768-6228-2272-4663-277478268005"
CHARACTERISTIC_UUID_STATE: Final = "00467768-6228-2272-4663-277478268001"
CHARACTERISTIC_UUID_ERROR: Final = "00467768-6228-2272-4663-277478268002"
Expand All @@ -25,7 +26,7 @@


class Capabilities(IntFlag):
"""State."""
"""Capabilities."""

IDENTIFY = 1

Expand Down Expand Up @@ -259,6 +260,27 @@ def _validate(cls, data: bytes) -> None:
raise InvalidCommand("Invalid strings", data.hex())


class ImprovServiceData:
"""Service data."""

def __init__(self, state: State, capabilities: Capabilities) -> None:
"""Initialize."""
self.capabilities = capabilities
self.state = state

@classmethod
def from_bytes(cls, data: bytes) -> ImprovServiceData:
"""Initialize from serialized representation of the command."""
if len(data) != 6:
raise InvalidCommand("Invalid service data", data.hex())
try:
state = State(data[0])
capabilities = Capabilities(data[1])
except ValueError as exc:
raise InvalidCommand("Invalid service data", data.hex()) from exc
return cls(state, capabilities)


RESULT_TYPES: dict[int, type[Command]] = {
0x01: WiFiSettingsRes,
}
Expand Down

0 comments on commit 093d4bf

Please sign in to comment.