qslib-server¶
LLM-written (Fable 5, Claude Fable 5.2, Claude Opus 5.5, and an unknown model)
qslib-server is an optional HTTP and SSE interface for QuantStudio
instruments. How a qslib Machine chooses between it and direct SCPI is
described in
Direct and managed transports.
The server keeps one managed InstrumentServer connection for API operations. It normally holds Observer access, serializes Controller transactions, and restores Observer access after each transaction. Raw SCPI and tunnel requests, when enabled, use separate connections.
The service uses unencrypted HTTP, so tokens and data can be read and altered by anyone who can see the traffic. Bind it only to a trusted private interface.
API¶
Routes are under /api/v1, except for GET /health.
GET /api/v1— the hypermedia entry documentGET /api/v1/capabilitiesGET /api/v1/eventsGET /api/v1/operations/{id}/api/v1/instrument/*for status and bounded controls/api/v1/files/{context}/{path}for file reads and writes/api/v1/directories/{context}/{path}for directory listings/api/v1/archives/{context}/{path}for ZIP downloads/api/v1/experiments/*for staged packages/api/v1/runs/*for run state, control, protocols, and EDS filesPOST /api/v1/scpiand/api/v1/scpi/tunnelwhen explicitly enabled
Hypermedia¶
/api/v1 is the sole bootstrap URL. GET /api/v1 returns an entry document
whose _links name every navigable resource and whose _templates keys
name the mutations the presented credentials may perform under current
server policy. Clients should navigate by relation and detect features by
the presence of a relation or template key, rather than requiring a
particular server version.
JSON representations carry HAL _links, and the entry document carries
_templates. Both fields are strictly additive: bodies, status codes, and
errors are unchanged, and the content type remains application/json.
Error responses and binary responses carry no link envelope.
Links and templates are advisory. They reflect server policy and the caller’s role. Server-side verification remains authoritative: an advertised mutation may still fail with 403 or 409.
Link relations¶
qslib relations carry a qs: prefix. IANA relations (self, index,
collection, item, alternate) are used bare. Templated links use RFC
6570 level 2 only: {var} (simple expansion with percent-encoding),
{+var} (reserved expansion for pre-encoded multi-segment paths), and
{?var,var} query expansion. By convention, a trailing / left by an empty
{+path} is trimmed by clients.
Entry-document links (all unconditional except as noted):
Relation |
Target |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Templates¶
Each _templates key maps to an empty object; only its presence carries
meaning. The keys and their conditions (C = Controller role and
--allow-controls; W = Controller role and --allow-file-writes):
set-power, set-block, set-indicator, indicator-off, set-drawer,
set-cover, restart, start-run, run-action, and preflight require
C; stage-package, delete-package, delete-experiment, and put-file
require W; replace-protocol requires C and W; raw-scpi requires
--enable-raw-scpi, and what may be sent depends on the role (see Raw SCPI
below).
Creation responses (201) and operation responses (202) include a
Location header naming the created resource or the operation poll target,
and operation SSE events include an href field. The SCPI tunnel remains a
bootstrap-by-convention exception: it is a raw socket upgrade at a fixed
path and is not link-driven.
Named file contexts are default, experiments, runs, logs, templates,
calibrations, public_run_complete, and private_run_complete. Absolute
paths and paths escaping a context are rejected. File writes are atomic and
require a Controller token plus --allow-file-writes. At most two file or
package uploads are received at once; later ones wait their turn. File reads take a single
Range, and If-Range with the file’s ETag or Last-Modified date: when the
file has changed, the whole file is sent with status 200.
Archive downloads preserve the physical directory tree, including dotfiles,
.attributes, and .shadows. They do not expand InstrumentServer shadows or
follow directory symlinks. The archive is sent in order as it is built, without
a Content-Length; a failure after the reply has begun ends it early. Each
member is deflated, and its sizes and checksum follow its data in a data
descriptor instead of its local header. Save the archive to a file before
extracting it: readers that extract from a pipe as the bytes arrive may not
support data descriptors.
Files removed while the archive is being built are left out. The repeatable exclude query parameter takes a path relative to the
archived directory, which is left out with everything under it.
Experiment packages must contain tcprotocol.xml for the Android UI and
qsl-tcprotocol.xml with the exact SCPI definition in
QSLibProtocolCommand. The server validates that definition against the
standard grammar; tcprotocol.xml is never an executable source.
Run starts and action requests require an Idempotency-Key and return an
operation resource. After an ambiguous transport failure, inspect that
resource rather than repeating the mutation over SCPI. Every token can read
every operation record and receives every operation event.
Operation records and keys are process-local, retained for 24 hours, and capped at 4,096. At capacity, new keys receive a retryable 503. A restart clears the records, so do not repeat a mutation whose outcome was unknown before the restart.
Raw SCPI¶
POST /api/v1/scpi, enabled by --enable-raw-scpi, sends one command on a
separate InstrumentServer connection at the requested access level
(default Observer), which may not exceed --max-access or the level the
token’s role allows (below). The command is sent either as a JSON body
{"command": ..., "access": ..., "timeout_ms": ...} or as a plain-text
body with access and timeout_ms as query parameters. The reply is the
text the instrument wrote after OK <id>, returned as text/plain; for an
ERRor reply, the text after ERRor <id> is the message of the JSON error.
The instrument puts control characters and bytes 0x7F to 0x9F inside
<binary> tags, but may send other bytes that are not UTF-8; those are
replaced with U+FFFD. A command line that the instrument would continue onto
the next line, because it ends in a backslash or leaves a <<< or <tag>
quotation open, is refused with 400.
Role |
Access |
|---|---|
Observer |
Observer |
Controller, with |
up to |
Controller, without both |
Observer |
The server does not look at the command itself: the instrument decides what the session’s access level may run, and some commands that change the instrument need only Observer access:
An Observer session may write files with
FILe:WRITe.Commands defined as macros when the instrument starts run with the access level they were defined with, whatever the caller’s level. For example,
LEDPhotodiode?moves the excitation filter wheel (MOVe m8).ACCess, which any session may send, changes the session’s own level up to the limit for its connection.
Like the roles and access levels generally, these limits guard against
mistakes by honest users, not against a user who wants more access (see
“Security Considerations” in the qslib documentation). The limit for the
loopback connection the server uses comes from the instrument’s access.ini,
and is Administrator on the instruments checked.
Raw SCPI commands do not pass through the managed connection’s Controller
transactions. The SCPI tunnel, enabled by --enable-scpi-tunnel, needs a
Controller token: it forwards bytes to InstrumentServer and cannot apply
--max-access or these rules.
SSE event IDs are opaque epoch:sequence cursors. Reconnect with the last
processed value in Last-Event-ID. The server replays retained events or emits
a reset event with a current status snapshot when the cursor cannot be
replayed. Process-local history is capped at 4,096 entries and 8 MiB, with
256 KiB per event.
Errors use one JSON envelope and repeat its request ID in X-Request-ID:
{
"error": {
"code": "queue_full",
"message": "server operation queue is full",
"retryable": true,
"outcome": "not_started"
},
"request_id": "..."
}
Authentication¶
Pass a root-readable TOML file with --auth-config or
QSLIB_SERVER_AUTH_CONFIG. Store SHA-256 token digests, not tokens:
unauthenticated_role = "observer"
[[tokens]]
name = "owner"
sha256 = "<64 lowercase hexadecimal characters>"
role = "controller"
Roles are observer, controller, and administrator. Controller is
enough for every endpoint, and an administrator token is treated as a
Controller. The optional unauthenticated_role is limited to Observer. A
malformed or invalid supplied token is always rejected, even when anonymous
Observer access is configured.
GET /health is the exception: it ignores credentials and answers every
client, so that deployment tools can identify a server whose token they no
longer hold. It reports the server version, executable SHA-256, uptime and
readiness.
For a read-only service without a token file, pass --no-auth, which gives
every caller anonymous Observer access:
qslib-server --no-auth
Options:
Option |
Default |
Purpose |
|---|---|---|
|
|
HTTP bind address |
|
|
InstrumentServer SCPI address |
|
|
Root for named file contexts |
|
unset |
Token file (also |
|
off |
Run without a token file, with anonymous Observer access |
|
|
Maximum managed SCPI access |
|
off |
Enable Controller file and package writes |
|
off |
Enable Controller hardware and run controls |
|
off |
Enable one-shot SCPI, limited by role |
|
off |
Enable SCPI tunnels for Controller tokens |
|
unset |
Instrument password for managed connections (also |
|
|
Maximum number of queued server operations |
|
|
Maximum number of SCPI tunnels |
|
stderr |
File for structured logs |
A transparent tunnel is not constrained by --max-access.
Build and deployment¶
Build and test natively with:
cargo build -p qslib-server --release
cargo test -p qslib-server
Instrument releases use a static ARMv7 musl binary. Verify its checksum,
install it with the deployment tooling or service manager, and check /health
after restart. The API has no self-upgrade endpoint.
Python can deploy a supplied binary through an existing Controller SCPI connection:
from qslib import Machine
machine = Machine(
"qpcr-example",
password="instrument-password",
server_token="controller-token",
)
machine.ensure_server(
"/path/to/qslib-server-v0.16.0-alpha.1-armv7-unknown-linux-musleabihf",
listen="0.0.0.0:7500",
)
server_machine = Machine("qpcr-example", backend="qslib", server_token="controller-token")
The token is installed with the Controller role.
ensure_server returns an already-ready service without replacing it. When
deployment is needed, it verifies the binary after transfer, installs token
configuration, starts the process, checks its identity and capabilities, and
rolls back failed activation. It uses the same deployment code and
instrument-side lock as qs-monitor’s server management, and the server’s
output goes to <remote_path>.log.