Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = [
"pyfirmata2>=2.5.0",
"pyzmq>=26.2.0",
"thorlabs-apt-device>=0.3.8",
"tomli-w>=1.0.0",
"typer>=0.15.1",
]

Expand Down
2 changes: 2 additions & 0 deletions src/pqnstack/app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pqnstack.app.api.routes import chsh
from pqnstack.app.api.routes import coordination
from pqnstack.app.api.routes import debug
from pqnstack.app.api.routes import games
from pqnstack.app.api.routes import qkd
from pqnstack.app.api.routes import rng
from pqnstack.app.api.routes import serial
Expand All @@ -16,3 +17,4 @@
api_router.include_router(serial.router)
api_router.include_router(coordination.router)
api_router.include_router(debug.router)
api_router.include_router(games.router)
11 changes: 11 additions & 0 deletions src/pqnstack/app/api/routes/games.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import APIRouter

from pqnstack.app.core.config import GamesAvailability
from pqnstack.app.core.config import get_settings

router = APIRouter(prefix="/games", tags=["games"])


@router.get("/availability")
def get_availability() -> GamesAvailability:
return get_settings().games_availability
7 changes: 7 additions & 0 deletions src/pqnstack/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class QKDSettings(BaseModel):
measurement_config: MeasurementConfig = Field(default_factory=lambda: MeasurementConfig(integration_time_s=5))


class GamesAvailability(BaseModel):
chsh: bool = True # "Verify Quantum Link"
qf: bool = True # "Quantum Fortune"
ssm: bool = True # "Share a Secret Message"


class Settings(BaseSettings):
node_name: str = "node1"
router_name: str = "router1"
Expand All @@ -54,6 +60,7 @@ class Settings(BaseSettings):
timetagger: tuple[str, str] | None = None # Name of the timetagger to use for the CHSH experiment.
rotary_encoder_address: str = "/dev/ttyACM0"
virtual_rotator: bool = False # If True, use terminal input instead of hardware rotary encoder
games_availability: GamesAvailability = Field(default_factory=GamesAvailability)

model_config = SettingsConfigDict(
toml_file="./config.toml",
Expand Down
33 changes: 33 additions & 0 deletions src/pqnstack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Annotated

import tomli_w
import typer

from pqnstack.base.errors import InvalidNetworkConfigurationError
Expand Down Expand Up @@ -184,5 +185,37 @@ def start_router(
router.start()


@app.command()
def toggle_game(
games: Annotated[list[str], typer.Argument(help="Games to toggle: chsh, qf, ssm")],
enable: Annotated[bool, typer.Option("--enable/--disable", help="Enable or disable the games")] = True, # noqa: FBT002
config: Annotated[str, typer.Option(help="Path to config.toml")] = "./config.toml",
) -> None:
"""
Enable or disable one or more games in config.toml.

Changes take effect on the next server restart. Games: chsh (Verify Quantum Link), qf (Quantum Fortune), ssm (Share a Secret Message).
"""
valid_games = {"chsh", "qf", "ssm"}
invalid = [g for g in games if g not in valid_games]
if invalid:
msg = f"Game(s) must be one of: chsh, qf, ssm. Invalid: {invalid}"
raise InvalidNetworkConfigurationError(msg)

path = Path(config)
with path.open("rb") as f:
cfg = tomllib.load(f)

cfg.setdefault("games_availability", {})
for game in games:
cfg["games_availability"][game] = enable

with path.open("wb") as f:
tomli_w.dump(cfg, f)

status = "enabled" if enable else "disabled"
logger.info("Games %s %s in %s. Restart the server for changes to take effect.", games, status, path)


if __name__ == "__main__":
app()
11 changes: 11 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.