In Peripheral Device mode, the application acts as an OSDP endpoint.
Key workflow:
- Define
PDInfo with a Channel and an optional scbk (Secure Channel Base Key). If scbk is None, secure channel is disabled. - Define
PDCapabilities to describe what the device can do. - Implement a
command_handler(cmd: Command) -> Command | None function. This function should:- Return a
Command to answer a request inline. - Return
None to accept a command without an immediate response. - Raise
NakError to decline a command.
- Initialize
PeripheralDevice with pd_info, pd_cap, and your command_handler. - Call
.start() and .sc_wait() to begin operation. - Use
.submit_event(event) to send events (like CardRead) to the CP.
from osdp import (
Capability, CardFormat, Command, NakCode, NakError, PDCapabilities, PDInfo,
PeripheralDevice, commands, events
)
channel = SerialChannel("/dev/ttyUSB0")
pd_info = PDInfo(101, channel, scbk=None)
pd_cap = PDCapabilities([
(Capability.OutputControl, 1, 1),
(Capability.LEDControl, 2, 1),
])
def command_handler(cmd: Command) -> Command | None:
match cmd:
case commands.Status(type=report_type):
return commands.Status(type=report_type, report=read_inputs())
case commands.BioRead():
raise NakError(NakCode.BioType)
return None
pd = PeripheralDevice(pd_info, pd_cap, command_handler=command_handler)
pd.start()
pd.sc_wait()
# Sending an event
card_event = events.CardRead(
reader_no=1,
direction=1,
format=CardFormat.ASCII,
data=bytes([9, 1, 9, 2, 6, 3, 1, 7, 7, 0]),
)
pd.submit_event(card_event)