feat: add Nix devShell and LG webOS discovery/control PoC scripts
- Add flake.nix, flake.lock, and pyproject.toml provisioning Python 3.14, websockets, pyright, and ruff - Add .gitignore to exclude Python caches and local TV pairing keys - Add discover.py for SSDP multicast discovery of LG webOS TVs - Add tv_test.py for SSAP wss:// pairing, client-key persistence, and basic remote commands
This commit is contained in:
1 parent
e936af244c
commit
9cd722ed49
6 files changed
+289
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.ruff_cache/
|
||||||
|
.client_key.json
|
||||||
|
.tv_state.json
|
||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
"""SSDP multicast discovery probe for LG webOS TVs on the local network."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import socket
|
||||||
|
|
||||||
|
LG_WEBOS_ST = "urn:lge-com:service:webos-second-screen:1"
|
||||||
|
SSDP_ADDR = ("239.255.255.250", 1900)
|
||||||
|
|
||||||
|
|
||||||
|
def discover(search_target: str, timeout: float = 3.0) -> list[tuple[str, str]]:
|
||||||
|
"""Send an SSDP M-SEARCH multicast packet and collect responses."""
|
||||||
|
payload = (
|
||||||
|
"M-SEARCH * HTTP/1.1\r\n"
|
||||||
|
f"HOST: {SSDP_ADDR[0]}:{SSDP_ADDR[1]}\r\n"
|
||||||
|
'MAN: "ssdp:discover"\r\n'
|
||||||
|
"MX: 2\r\n"
|
||||||
|
f"ST: {search_target}\r\n"
|
||||||
|
"\r\n"
|
||||||
|
).encode()
|
||||||
|
|
||||||
|
results: list[tuple[str, str]] = []
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
||||||
|
sock.settimeout(timeout)
|
||||||
|
sock.sendto(payload, SSDP_ADDR)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data, addr = sock.recvfrom(4096)
|
||||||
|
except TimeoutError:
|
||||||
|
break
|
||||||
|
results.append((addr[0], data.decode(errors="replace")))
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""CLI entry point for SSDP discovery."""
|
||||||
|
parser = argparse.ArgumentParser(description="Discover LG webOS TVs via SSDP.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--all",
|
||||||
|
action="store_true",
|
||||||
|
help="Search for ssdp:all instead of only LG webOS devices.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--timeout",
|
||||||
|
type=float,
|
||||||
|
default=3.0,
|
||||||
|
help="Seconds to wait for responses (default: 3.0).",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
target = "ssdp:all" if args.all else LG_WEBOS_ST
|
||||||
|
print(f"Sending SSDP M-SEARCH for {target} (timeout={args.timeout}s)...")
|
||||||
|
responses = discover(target, timeout=args.timeout)
|
||||||
|
if not responses:
|
||||||
|
print("No SSDP responses received.")
|
||||||
|
return
|
||||||
|
|
||||||
|
for ip, raw in responses:
|
||||||
|
print(f"\n=== {ip} ===")
|
||||||
|
print(raw.strip())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Generated
+27
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1794386569,
|
||||||
|
"narHash": "sha256-x9sADJvoVG0L6RejZB+/BB4YiGZFBZrfTlhF4uBq1tQ=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "d4f25a3e562008648d45bcf2d70bcc7422cad84e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
description = "LG webOS local control testing environment";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs }:
|
||||||
|
let
|
||||||
|
system = "x86_64-linux";
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
pythonEnv = pkgs.python3.withPackages (ps: [
|
||||||
|
ps.websockets
|
||||||
|
]);
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells.${system}.default = pkgs.mkShell {
|
||||||
|
packages = [
|
||||||
|
pythonEnv
|
||||||
|
pkgs.pyright
|
||||||
|
pkgs.ruff
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[tool.ruff]
|
||||||
|
line-length = 100
|
||||||
|
target-version = "py314"
|
||||||
|
|
||||||
|
[tool.ruff.lint]
|
||||||
|
select = ["E", "F", "I", "UP", "B"]
|
||||||
|
|
||||||
|
[tool.pyright]
|
||||||
|
typeCheckingMode = "standard"
|
||||||
|
pythonVersion = "3.14"
|
||||||
+157
@@ -0,0 +1,157 @@
|
|||||||
|
"""Minimal LG webOS SSAP pairing and command test script."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import ssl
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
DEFAULT_KEY_FILE = Path(".client_key.json")
|
||||||
|
|
||||||
|
PERMISSIONS = [
|
||||||
|
"LAUNCH",
|
||||||
|
"CONTROL_AUDIO",
|
||||||
|
"CONTROL_DISPLAY",
|
||||||
|
"CONTROL_POWER",
|
||||||
|
"CONTROL_INPUT_TEXT",
|
||||||
|
"CONTROL_INPUT_MEDIA_PLAYBACK",
|
||||||
|
"READ_INSTALLED_APPS",
|
||||||
|
"WRITE_NOTIFICATION_TOAST",
|
||||||
|
]
|
||||||
|
|
||||||
|
COMMANDS: dict[str, tuple[str, dict[str, Any]]] = {
|
||||||
|
"info": ("ssap://system/getSystemInfo", {}),
|
||||||
|
"apps": ("ssap://com.webos.applicationManager/listLaunchPoints", {}),
|
||||||
|
"vol-up": ("ssap://audio/volumeUp", {}),
|
||||||
|
"vol-down": ("ssap://audio/volumeDown", {}),
|
||||||
|
"mute": ("ssap://audio/setMute", {"mute": True}),
|
||||||
|
"unmute": ("ssap://audio/setMute", {"mute": False}),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def build_ssl_context() -> ssl.SSLContext:
|
||||||
|
"""Create an SSL context that accepts the TV's self-signed certificate."""
|
||||||
|
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||||
|
ctx.check_hostname = False
|
||||||
|
ctx.verify_mode = ssl.CERT_NONE
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
def load_client_keys(key_file: Path) -> dict[str, str]:
|
||||||
|
"""Load saved IP -> client-key mappings from disk."""
|
||||||
|
if not key_file.exists():
|
||||||
|
return {}
|
||||||
|
data = json.loads(key_file.read_text(encoding="utf-8"))
|
||||||
|
return {str(k): str(v) for k, v in data.items()}
|
||||||
|
|
||||||
|
|
||||||
|
def save_client_key(key_file: Path, ip: str, client_key: str) -> None:
|
||||||
|
"""Persist a paired TV's client-key to disk."""
|
||||||
|
keys = load_client_keys(key_file)
|
||||||
|
keys[ip] = client_key
|
||||||
|
key_file.write_text(json.dumps(keys, indent=2) + "\n", encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def build_register_payload(client_key: str | None) -> dict[str, Any]:
|
||||||
|
"""Build the SSAP register handshake payload."""
|
||||||
|
payload: dict[str, Any] = {
|
||||||
|
"forcePairing": False,
|
||||||
|
"pairingType": "PROMPT",
|
||||||
|
"manifest": {
|
||||||
|
"manifestVersion": 1,
|
||||||
|
"appVersion": "0.1.0",
|
||||||
|
"permissions": PERMISSIONS,
|
||||||
|
"localizedAppNames": {"": "webOS Linux Test"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if client_key:
|
||||||
|
payload["client-key"] = client_key
|
||||||
|
return {"type": "register", "id": "register_0", "payload": payload}
|
||||||
|
|
||||||
|
|
||||||
|
async def pair_and_send(
|
||||||
|
ip: str,
|
||||||
|
port: int,
|
||||||
|
uri: str,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
key_file: Path,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Connect to the TV, complete registration/pairing, and send one SSAP request."""
|
||||||
|
keys = load_client_keys(key_file)
|
||||||
|
saved_key = keys.get(ip)
|
||||||
|
scheme = "wss" if port == 3001 else "ws"
|
||||||
|
ssl_ctx = build_ssl_context() if scheme == "wss" else None
|
||||||
|
|
||||||
|
async with websockets.connect(f"{scheme}://{ip}:{port}", ssl=ssl_ctx) as ws:
|
||||||
|
await ws.send(json.dumps(build_register_payload(saved_key)))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
msg = json.loads(await ws.recv())
|
||||||
|
msg_type = msg.get("type")
|
||||||
|
if msg_type == "response" and msg.get("payload", {}).get("pairingType") == "PROMPT":
|
||||||
|
print("Pairing prompt sent — accept it on the TV screen...")
|
||||||
|
continue
|
||||||
|
if msg_type == "registered":
|
||||||
|
new_key = msg["payload"]["client-key"]
|
||||||
|
if new_key != saved_key:
|
||||||
|
save_client_key(key_file, ip, new_key)
|
||||||
|
print(f"Paired! Saved client-key to {key_file}")
|
||||||
|
break
|
||||||
|
if msg_type == "error":
|
||||||
|
raise RuntimeError(f"Registration failed: {msg}")
|
||||||
|
|
||||||
|
req = {"id": "cmd_1", "type": "request", "uri": uri, "payload": payload}
|
||||||
|
await ws.send(json.dumps(req))
|
||||||
|
return json.loads(await ws.recv())
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
|
"""Parse CLI arguments."""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Pair with an LG webOS TV and send a test command."
|
||||||
|
)
|
||||||
|
parser.add_argument("--ip", required=True, help="TV IP address (e.g. 192.168.1.11)")
|
||||||
|
parser.add_argument("--port", type=int, default=3001, help="WebSocket port (default: 3001)")
|
||||||
|
parser.add_argument(
|
||||||
|
"--key-file",
|
||||||
|
type=Path,
|
||||||
|
default=DEFAULT_KEY_FILE,
|
||||||
|
help="Path to store client-key (default: .client_key.json)",
|
||||||
|
)
|
||||||
|
|
||||||
|
sub = parser.add_subparsers(dest="command", required=True)
|
||||||
|
toast_p = sub.add_parser("toast", help="Show a toast notification on the TV.")
|
||||||
|
toast_p.add_argument("message", nargs="?", default="Hello from Linux!")
|
||||||
|
|
||||||
|
for name in COMMANDS:
|
||||||
|
sub.add_parser(name, help=f"Send {COMMANDS[name][0]}")
|
||||||
|
|
||||||
|
raw_p = sub.add_parser("raw", help="Send an arbitrary SSAP URI + JSON payload.")
|
||||||
|
raw_p.add_argument("uri", help="SSAP URI (e.g. ssap://system/getSystemInfo)")
|
||||||
|
raw_p.add_argument("--payload", default="{}", help="JSON payload string")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""CLI entry point."""
|
||||||
|
args = parse_args()
|
||||||
|
if args.command == "toast":
|
||||||
|
uri = "ssap://system.notifications/createToast"
|
||||||
|
payload: dict[str, Any] = {"message": args.message}
|
||||||
|
elif args.command == "raw":
|
||||||
|
uri = args.uri
|
||||||
|
payload = json.loads(args.payload)
|
||||||
|
else:
|
||||||
|
uri, payload = COMMANDS[args.command]
|
||||||
|
|
||||||
|
response = asyncio.run(pair_and_send(args.ip, args.port, uri, payload, args.key_file))
|
||||||
|
print(json.dumps(response, indent=2))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in new issue
Block a user