Skip to content

Commit

Permalink
pyosdp: Hoist cmd->id set in pyosdp_make_struct_cmd
Browse files Browse the repository at this point in the history
This is needed to avoid code duplication for commands that do not have a
body (the ones that just have a command ID).

Closes: #116
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Jun 27, 2023
1 parent 6dc2e4a commit 4feb2f8
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions python/pyosdp_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ static int pyosdp_make_struct_cmd_output(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_output *cmd = &p->output;
int output_no, control_code, timer_count;

p->id = OSDP_CMD_OUTPUT;

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

Expand Down Expand Up @@ -81,8 +79,6 @@ static int pyosdp_make_struct_cmd_led(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_led *cmd = &p->led;
struct osdp_cmd_led_params *params = &cmd->permanent;

p->id = OSDP_CMD_LED;

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

Expand Down Expand Up @@ -144,8 +140,6 @@ static int pyosdp_make_struct_cmd_buzzer(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_buzzer *cmd = &p->buzzer;
int reader, on_count, off_count, rep_count, control_code;

p->id = OSDP_CMD_BUZZER;

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

Expand Down Expand Up @@ -201,8 +195,6 @@ static int pyosdp_make_struct_cmd_text(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_text *cmd = &p->text;
int length, reader, control_code, offset_row, offset_col, temp_time;

p->id = OSDP_CMD_TEXT;

if (pyosdp_dict_get_str(dict, "data", &data))
goto exit;

Expand Down Expand Up @@ -255,8 +247,6 @@ static int pyosdp_make_struct_cmd_keyset(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_keyset *cmd = &p->keyset;
uint8_t *buf;

p->id = OSDP_CMD_KEYSET;

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

Expand Down Expand Up @@ -285,8 +275,6 @@ static int pyosdp_make_struct_cmd_comset(struct osdp_cmd *p, PyObject *dict)
struct osdp_cmd_comset *cmd = &p->comset;
int address, baud_rate;

p->id = OSDP_CMD_COMSET;

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

Expand Down Expand Up @@ -316,8 +304,6 @@ static int pyosdp_make_struct_cmd_mfg(struct osdp_cmd *p, PyObject *dict)
uint8_t *data_bytes;
int vendor_code, mfg_command;

p->id = OSDP_CMD_MFG;

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

Expand Down Expand Up @@ -349,15 +335,12 @@ static int pyosdp_make_struct_cmd_file_tx(struct osdp_cmd *p, PyObject *dict)
int id, flags;
struct osdp_cmd_file_tx *cmd = &p->file_tx;

p->id = OSDP_CMD_FILE_TX;

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

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

cmd->id = id;
cmd->flags = flags;
return 0;
}
Expand Down Expand Up @@ -641,8 +624,11 @@ int pyosdp_make_struct_cmd(struct osdp_cmd *cmd, PyObject *dict)
return -1;
if (cmd_id < OSDP_CMD_OUTPUT || cmd_id >= OSDP_CMD_SENTINEL)
return -1;
if (command_translator[cmd_id].dict_to_struct(cmd, dict))
return -1;

return command_translator[cmd_id].dict_to_struct(cmd, dict);
cmd->id = cmd_id;
return 0;
}

int pyosdp_make_dict_cmd(PyObject **dict, struct osdp_cmd *cmd)
Expand Down Expand Up @@ -695,4 +681,5 @@ int pyosdp_make_dict_event(PyObject **dict, struct osdp_event *event)

*dict = obj;
return 0;
}
}

0 comments on commit 4feb2f8

Please sign in to comment.