Skip to content

Commit

Permalink
python: Adapt to changes to status event/command handling
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Jan 14, 2024
1 parent caadc3e commit afe66c6
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 80 deletions.
2 changes: 1 addition & 1 deletion python/osdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .key_store import KeyStore
from .constants import (
LibFlag, Command, CommandLEDColor, CommandFileTxFlags,
Event, CardFormat, Capability, LogLevel
Event, CardFormat, Capability, LogLevel, CommandStatusType, EventStatusType
)
from .helpers import PDInfo, PDCapabilities

Expand Down
14 changes: 13 additions & 1 deletion python/osdp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class LogLevel:
Info = osdp_sys.LOG_INFO
Debug = osdp_sys.LOG_DEBUG

class CommandStatusType:
Local = osdp_sys.CMD_STATUS_QUERY_LOCAL
Input = osdp_sys.CMD_STATUS_QUERY_INPUT
Output = osdp_sys.CMD_STATUS_QUERY_OUTPUT
Remote = osdp_sys.CMD_STATUS_QUERY_REMOTE

class Command:
Output = osdp_sys.CMD_OUTPUT
Buzzer = osdp_sys.CMD_BUZZER
Expand All @@ -30,6 +36,7 @@ class Command:
Manufacturer = osdp_sys.CMD_MFG
Keyset = osdp_sys.CMD_KEYSET
FileTransfer = osdp_sys.CMD_FILE_TX
Status = osdp_sys.CMD_STATUS

class CommandLEDColor:
Black = osdp_sys.LED_COLOR_NONE
Expand All @@ -41,11 +48,16 @@ class CommandLEDColor:
class CommandFileTxFlags:
Cancel = osdp_sys.CMD_FILE_TX_FLAG_CANCEL

class EventStatusType:
Input = osdp_sys.EVENT_STATUS_TYPE_INPUT
Output = osdp_sys.EVENT_STATUS_TYPE_OUTPUT
Remote = osdp_sys.EVENT_STATUS_TYPE_REMOTE
Local = osdp_sys.EVENT_STATUS_TYPE_LOCAL

class Event:
CardRead = osdp_sys.EVENT_CARDREAD
KeyPress = osdp_sys.EVENT_KEYPRESS
ManufacturerReply = osdp_sys.EVENT_MFGREP
InputOutput = osdp_sys.EVENT_IO
Status = osdp_sys.EVENT_STATUS

class CardFormat:
Expand Down
16 changes: 12 additions & 4 deletions python/osdp/control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import time
import queue
import threading
from typing import Callable

from .helpers import PDInfo
from .constants import LibFlag, LogLevel

class ControlPanel():
def __init__(self, pd_info_list, log_level: LogLevel=LogLevel.Info,
master_key: bytes=None):
master_key: bytes=None, event_handler: Callable[[int, dict], int]=None):
self.pd_addr = []
info_list = []
self.num_pds = len(pd_info_list)
Expand All @@ -29,7 +30,7 @@ def __init__(self, pd_info_list, log_level: LogLevel=LogLevel.Info,
self.ctx = osdp_sys.ControlPanel(info_list, master_key=master_key)
else:
self.ctx = osdp_sys.ControlPanel(info_list)
self.ctx.set_event_callback(self.event_handler)
self.set_event_handler(event_handler)
self.event = None
self.lock = None
self.thread = None
Expand All @@ -42,8 +43,15 @@ def refresh(event, lock, ctx):
lock.release()
time.sleep(0.020) #sleep for 20ms

def event_handler(self, pd, event):
def set_event_handler(self, handler: Callable[[int, dict], int]):
if handler:
self.ctx.set_event_callback(handler)
else:
self.ctx.set_event_callback(self.event_handler)

def event_handler(self, pd, event) -> int:
self.event_queue[pd].put(event)
return 0

def get_event(self, address, timeout: int=5):
pd = self.pd_addr.index(address)
Expand Down Expand Up @@ -191,4 +199,4 @@ def sc_wait_all(self, timeout=5):

def teardown(self):
self.stop()
self.ctx = None
self.ctx = None
22 changes: 17 additions & 5 deletions python/osdp/peripheral_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
import time
import queue
import threading
from typing import Callable, Tuple

from .helpers import PDInfo, PDCapabilities
from .constants import LogLevel

class PeripheralDevice():
def __init__(self, pd_info: PDInfo, pd_cap: PDCapabilities,
log_level: LogLevel=LogLevel.Info):
log_level: LogLevel=LogLevel.Info,
command_handler: Callable[[dict], Tuple[int, dict]]=None):
self.command_queue = queue.Queue()
self.address = pd_info.address
osdp_sys.set_loglevel(log_level)
self.ctx = osdp_sys.PeripheralDevice(pd_info.get(), capabilities=pd_cap.get())
self.ctx.set_command_callback(self.command_handler)
if command_handler:
self.ctx.set_command_callback(command_handler)
else:
self.ctx.set_command_callback(self.command_handler)
self.set_command_handler(command_handler)
self.event = None
self.lock = None
self.thread = None
Expand All @@ -32,9 +38,15 @@ def refresh(event, lock, ctx):
lock.release()
time.sleep(0.020) #sleep for 20ms

def command_handler(self, command):
def command_handler(self, command) -> Tuple[int, dict]:
self.command_queue.put(command)
return { "return_code": 0 }
return 0, None

def set_command_handler(self, handler: Callable[[dict], Tuple[int, dict]]):
if handler:
self.ctx.set_command_callback(handler)
else:
self.ctx.set_command_callback(self.command_handler)

def get_command(self, timeout: int=5):
block = timeout >= 0
Expand Down Expand Up @@ -100,4 +112,4 @@ def stop(self):

def teardown(self):
self.stop()
self.ctx = None
self.ctx = None
103 changes: 49 additions & 54 deletions python/osdp_sys/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,36 @@ static int pyosdp_make_struct_cmd_file_tx(struct osdp_cmd *p, PyObject *dict)
return 0;
}

/* Dummies for commands that don't have any body */
static int pyosdp_make_struct_cmd_dummy(struct osdp_cmd *cmd, PyObject *obj) { return 0; }
static int pyosdp_make_dict_cmd_dummy(PyObject *obj, struct osdp_cmd *cmd) { return 0; }
static int pyosdp_make_dict_cmd_status(PyObject *obj, struct osdp_cmd *cmd)
{
if (pyosdp_dict_add_int(obj, "type", cmd->status.type))
return -1;
if (pyosdp_dict_add_int(obj, "nr_entries", cmd->status.nr_entries))
return -1;
if (pyosdp_dict_add_int(obj, "mask", cmd->status.mask))
return -1;
return 0;
}

static int pyosdp_make_struct_cmd_status(struct osdp_cmd *p, PyObject *dict)
{
int type, nr_entries, mask;
struct osdp_cmd_status *cmd = &p->status;

if (pyosdp_dict_get_int(dict, "type", &type))
return -1;

if (pyosdp_dict_get_int(dict, "nr_entries", &nr_entries))
return -1;

if (pyosdp_dict_get_int(dict, "mask", &mask))
return -1;

cmd->type = type;
cmd->nr_entries = nr_entries;
cmd->mask = mask;
return 0;
}

/* ------------------------------- */
/* EVENTS */
Expand Down Expand Up @@ -386,8 +412,6 @@ static int pyosdp_make_struct_event_cardread(struct osdp_event *p,
struct osdp_event_cardread *ev = &p->cardread;
uint8_t *data_bytes;

p->type = OSDP_EVENT_CARDREAD;

if (pyosdp_dict_get_int(dict, "reader_no", &reader_no))
return -1;

Expand Down Expand Up @@ -440,8 +464,6 @@ static int pyosdp_make_struct_event_keypress(struct osdp_event *p, PyObject *dic
struct osdp_event_keypress *ev = &p->keypress;
uint8_t *data_bytes;

p->type = OSDP_EVENT_KEYPRESS;

if (pyosdp_dict_get_int(dict, "reader_no", &reader_no))
return -1;

Expand Down Expand Up @@ -473,8 +495,6 @@ static int pyosdp_make_struct_event_mfg_reply(struct osdp_event *p,
struct osdp_event_mfgrep *ev = &p->mfgrep;
uint8_t *data_bytes;

p->type = OSDP_EVENT_MFGREP;

if (pyosdp_dict_get_int(dict, "vendor_code", &vendor_code))
return -1;

Expand All @@ -492,58 +512,34 @@ static int pyosdp_make_struct_event_mfg_reply(struct osdp_event *p,
return 0;
}

static int pyosdp_make_dict_event_io(PyObject *obj, struct osdp_event *event)
{
if (pyosdp_dict_add_int(obj, "type", event->io.type))
return -1;
if (pyosdp_dict_add_int(obj, "status", event->io.status))
return -1;
return 0;
}

static int pyosdp_make_struct_event_io(struct osdp_event *p,
PyObject *dict)
{
int type, status;
struct osdp_event_io *ev = &p->io;

p->type = OSDP_EVENT_IO;

if (pyosdp_dict_get_int(dict, "type", &type))
return -1;

if (pyosdp_dict_get_int(dict, "status", &status))
return -1;

ev->type = type;
ev->status = (uint32_t)status;
return 0;
}

static int pyosdp_make_dict_event_status(PyObject *obj, struct osdp_event *event)
{
if (pyosdp_dict_add_int(obj, "tamper", event->status.tamper))
if (pyosdp_dict_add_int(obj, "type", event->status.type))
return -1;
if (pyosdp_dict_add_int(obj, "power", event->status.power))
if (pyosdp_dict_add_int(obj, "nr_entries", event->status.nr_entries))
return -1;
if (pyosdp_dict_add_int(obj, "mask", event->status.mask))
return -1;
return 0;
}

static int pyosdp_make_struct_event_status(struct osdp_event *p, PyObject *dict)
{
int tamper, power;
int type, nr_entries, mask;
struct osdp_event_status *ev = &p->status;

p->type = OSDP_EVENT_STATUS;
if (pyosdp_dict_get_int(dict, "type", &type))
return -1;

if (pyosdp_dict_get_int(dict, "tamper", &tamper))
if (pyosdp_dict_get_int(dict, "nr_entries", &nr_entries))
return -1;

if (pyosdp_dict_get_int(dict, "power", &power))
if (pyosdp_dict_get_int(dict, "mask", &mask))
return -1;

ev->tamper = (uint8_t)tamper;
ev->power = (uint8_t)power;
ev->type = type;
ev->nr_entries = nr_entries;
ev->mask = mask;
return 0;
}

Expand Down Expand Up @@ -584,8 +580,8 @@ static struct {
.struct_to_dict = pyosdp_make_dict_cmd_file_tx,
},
[OSDP_CMD_STATUS] = {
.dict_to_struct = pyosdp_make_struct_cmd_dummy,
.struct_to_dict = pyosdp_make_dict_cmd_dummy,
.dict_to_struct = pyosdp_make_struct_cmd_status,
.struct_to_dict = pyosdp_make_dict_cmd_status,
},
};

Expand All @@ -605,10 +601,6 @@ static struct {
.struct_to_dict = pyosdp_make_dict_event_mfg_reply,
.dict_to_struct = pyosdp_make_struct_event_mfg_reply,
},
[OSDP_EVENT_IO] = {
.struct_to_dict = pyosdp_make_dict_event_io,
.dict_to_struct = pyosdp_make_struct_event_io,
},
[OSDP_EVENT_STATUS] = {
.struct_to_dict = pyosdp_make_dict_event_status,
.dict_to_struct = pyosdp_make_struct_event_status,
Expand All @@ -623,7 +615,7 @@ int pyosdp_make_struct_cmd(struct osdp_cmd *cmd, PyObject *dict)

if (pyosdp_dict_get_int(dict, "command", &cmd_id))
return -1;
if (cmd_id < OSDP_CMD_OUTPUT || cmd_id >= OSDP_CMD_SENTINEL)
if (cmd_id <= 0 || cmd_id >= OSDP_CMD_SENTINEL)
return -1;
if (command_translator[cmd_id].dict_to_struct(cmd, dict))
return -1;
Expand All @@ -636,7 +628,7 @@ int pyosdp_make_dict_cmd(PyObject **dict, struct osdp_cmd *cmd)
{
PyObject *obj;

if (cmd->id < OSDP_CMD_OUTPUT || cmd->id >= OSDP_CMD_SENTINEL)
if (cmd->id <= 0 || cmd->id >= OSDP_CMD_SENTINEL)
return -1;

obj = PyDict_New();
Expand All @@ -660,6 +652,10 @@ int pyosdp_make_struct_event(struct osdp_event *event, PyObject *dict)
if (pyosdp_dict_get_int(dict, "event", &event_type))
return -1;

if (event_type <= 0 || event_type >= OSDP_EVENT_SENTINEL)
return -1;

event->type = event_type;
return event_translator[event_type].dict_to_struct(event, dict);
}

Expand All @@ -683,4 +679,3 @@ int pyosdp_make_dict_event(PyObject **dict, struct osdp_event *event)
*dict = obj;
return 0;
}

12 changes: 11 additions & 1 deletion python/osdp_sys/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ void pyosdp_add_module_constants(PyObject *module)
ADD_CONST("CMD_KEYSET", OSDP_CMD_KEYSET);
ADD_CONST("CMD_MFG", OSDP_CMD_MFG);
ADD_CONST("CMD_FILE_TX", OSDP_CMD_FILE_TX);
ADD_CONST("CMD_STATUS", OSDP_CMD_STATUS);

ADD_CONST("CMD_STATUS_QUERY_LOCAL", OSDP_CMD_STATUS_QUERY_LOCAL)
ADD_CONST("CMD_STATUS_QUERY_INPUT", OSDP_CMD_STATUS_QUERY_INPUT)
ADD_CONST("CMD_STATUS_QUERY_OUTPUT", OSDP_CMD_STATUS_QUERY_OUTPUT)
ADD_CONST("CMD_STATUS_QUERY_REMOTE", OSDP_CMD_STATUS_QUERY_REMOTE)

/* For `struct osdp_cmd_file_tx`::flags */
ADD_CONST("CMD_FILE_TX_FLAG_CANCEL", OSDP_CMD_FILE_TX_FLAG_CANCEL);
Expand All @@ -50,9 +56,13 @@ void pyosdp_add_module_constants(PyObject *module)
ADD_CONST("EVENT_CARDREAD", OSDP_EVENT_CARDREAD);
ADD_CONST("EVENT_KEYPRESS", OSDP_EVENT_KEYPRESS);
ADD_CONST("EVENT_MFGREP", OSDP_EVENT_MFGREP);
ADD_CONST("EVENT_IO", OSDP_EVENT_IO);
ADD_CONST("EVENT_STATUS", OSDP_EVENT_STATUS);

ADD_CONST("EVENT_STATUS_TYPE_INPUT", OSDP_EVENT_STATUS_TYPE_INPUT);
ADD_CONST("EVENT_STATUS_TYPE_OUTPUT", OSDP_EVENT_STATUS_TYPE_OUTPUT);
ADD_CONST("EVENT_STATUS_TYPE_REMOTE", OSDP_EVENT_STATUS_TYPE_REMOTE);
ADD_CONST("EVENT_STATUS_TYPE_LOCAL", OSDP_EVENT_STATUS_TYPE_LOCAL);

/* enum osdp_led_color_e */
ADD_CONST("LED_COLOR_NONE", OSDP_LED_COLOR_NONE);
ADD_CONST("LED_COLOR_RED", OSDP_LED_COLOR_RED);
Expand Down
Loading

0 comments on commit afe66c6

Please sign in to comment.