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

Allow user-defined filters #108

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/commands
.idea
filters/*.py
*.so
__pycache__/
build/
install/on_rpi/bluez/
devices_config.json
*~
2 changes: 1 addition & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
files = adapter.py, agent.py, bluetooth_devices.py, compatibility_device.py, hid_devices.py
files = adapter.py, agent.py, bluetooth_devices.py, compatibility_device.py, hid_devices.py, filters/
check_untyped_defs = True
follow_imports_for_stubs = True
disallow_any_decorated = True
Expand Down
Empty file added filters/README.md
Empty file.
Empty file added filters/__init__.py
Empty file.
25 changes: 19 additions & 6 deletions hid_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import array
import asyncio
import fcntl
import importlib
import os
import json
import re
Expand Down Expand Up @@ -47,7 +48,7 @@ class _InputDevice(TypedDict):

class _HIDDevices(TypedDict):
devices: list[_Device]
filters: tuple[dict[str, str]]
filters: tuple[dict[str, str], ...]
input_devices: list[_InputDevice]


Expand All @@ -58,16 +59,27 @@ class _DeviceConfig(TypedDict, total=False):
mapped_ids: dict[Union[int, Literal["_"]], int]


class FilterDict(TypedDict):
name: str
func: HIDMessageFilter


DEVICES_CONFIG_FILE_NAME = 'devices_config.json'
DEVICES_CONFIG_COMPATIBILITY_DEVICE_KEY = 'compatibility_devices'
CAPTURE_ELEMENT: Literal['capture'] = 'capture'
FILTER_ELEMENT: Literal['filter'] = 'filter'
FILTERS_PATH = Path(__file__).parent / "filters"
REPORT_ID_PATTERN = re.compile(r"(a10185)(..)")
SDP_TEMPLATE_PATH = Path(__file__).with_name("sdp_record_template.xml")
SDP_OUTPUT_PATH = Path("/etc/bluetooth/sdp_record.xml")

FILTERS = ({"id": "_", "name": "No filter"},)
FILTER_INSTANCES: dict[str, HIDMessageFilter] = {"_": lambda m: m}
FILTERS: dict[str, FilterDict] = {"_": {"name": "No filter", "func": lambda m: m}}
for mod_path in FILTERS_PATH.glob("*.py"):
if mod_path.stem == "__init__":
continue
mod = importlib.import_module("filters." + mod_path.stem)
name = mod.__doc__ or mod_path.stem.replace("_", " ").capitalize()
FILTERS[mod_path.stem] = {"name": name, "func": mod.message_filter}


# https://github.com/bentiss/hid-tools/blob/59a0c4b153dbf7d443e63bf68ff830b8353f5f7a/hidtools/hidraw.py#L33-L104
Expand Down Expand Up @@ -404,13 +416,14 @@ def __get_configured_device_filter(self, device_id: str) -> HIDMessageFilter:
if device_id in self.devices_config:
if FILTER_ELEMENT in self.devices_config[device_id]:
filter_id = self.devices_config[device_id][FILTER_ELEMENT]
return FILTER_INSTANCES[filter_id]
return FILTER_INSTANCES["_"]
return FILTER_INSTANCES[filter_id]["func"]
return FILTER_INSTANCES["_"]["func"]
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved

def get_hid_devices_with_config(self) -> _HIDDevices:
for device in self.devices:
if device["id"] in self.devices_config:
device[CAPTURE_ELEMENT] = self.devices_config[device["id"]].get(CAPTURE_ELEMENT, False)
if FILTER_ELEMENT in self.devices_config[device["id"]]:
device[FILTER_ELEMENT] = self.devices_config[device["id"]][FILTER_ELEMENT]
return {"devices": self.devices, "filters": FILTERS, "input_devices": self.input_devices}
f = tuple({"id": k, "name": v["name"]} for k,v in FILTERS.items())
return {"devices": self.devices, "filters": f, "input_devices": self.input_devices}
Loading