Skip to content

Commit

Permalink
drivers/main.{c,h}, drivers/dstate.c: introduce and handle shared mai…
Browse files Browse the repository at this point in the history
…n_instcmd() and main_setvar() handlers [networkupstools#1285, networkupstools#1914]
  • Loading branch information
jimklimov committed Apr 20, 2023
1 parent c6968db commit e2715f0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
27 changes: 25 additions & 2 deletions drivers/dstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,18 @@ static int sock_arg(conn_t *conn, size_t numarg, char **arg)
if (cmdid)
upsdebugx(3, "%s: TRACKING = %s", __func__, cmdid);

/* try the new handler first if present */
/* try the handler shared by all drivers first */
ret = main_instcmd(arg[1], arg[2]);
if (ret == STAT_INSTCMD_HANDLED) {
/* send back execution result */
if (cmdid)
send_tracking(conn, cmdid, ret);

/* The command was handled, status is a separate consideration */
return 1;
} /* else try other handler(s) */

/* try the driver-provided handler if present */
if (upsh.instcmd) {
ret = upsh.instcmd(cmdname, cmdparam);

Expand All @@ -753,6 +764,7 @@ static int sock_arg(conn_t *conn, size_t numarg, char **arg)
/* The command was handled, status is a separate consideration */
return 1;
}

upslogx(LOG_NOTICE, "Got INSTCMD, but driver lacks a handler");
return 1;
}
Expand All @@ -779,7 +791,18 @@ static int sock_arg(conn_t *conn, size_t numarg, char **arg)
upsdebugx(3, "%s: TRACKING = %s", __func__, setid);
}

/* try the new handler first if present */
/* try the handler shared by all drivers first */
ret = main_setvar(arg[1], arg[2]);
if (ret == STAT_SET_HANDLED) {
/* send back execution result */
if (setid)
send_tracking(conn, setid, ret);

/* The command was handled, status is a separate consideration */
return 1;
} /* else try other handler(s) */

/* try the driver-provided handler if present */
if (upsh.setvar) {
ret = upsh.setvar(arg[1], arg[2]);

Expand Down
20 changes: 20 additions & 0 deletions drivers/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ void addvar(int vartype, const char *name, const char *desc)
do_addvar(vartype, name, desc, 0);
}

/* handle instant commands common for all drivers */
int main_instcmd(const char *cmdname, const char *extra) {
NUT_UNUSED_VARIABLE(cmdname);
NUT_UNUSED_VARIABLE(extra);

/* By default, the driver-specific values are
* unknown to shared standard handler */
return STAT_INSTCMD_UNKNOWN;
}

/* handle setting variables common for all drivers */
int main_setvar(const char *varname, const char *val) {
NUT_UNUSED_VARIABLE(varname);
NUT_UNUSED_VARIABLE(val);

/* By default, the driver-specific values are
* unknown to shared standard handler */
return STAT_SET_UNKNOWN;
}

/* handle -x / ups.conf config details that are for this part of the code */
static int main_arg(char *var, char *val)
{
Expand Down
11 changes: 11 additions & 0 deletions drivers/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "common.h"
#include "upsconf.h"
#include "upshandler.h"
#include "dstate.h"
#include "extstate.h"
#ifdef WIN32
Expand All @@ -29,6 +30,16 @@ void set_exit_flag(int sig);

/* --- details for the variable/value sharing --- */

/* handle instant commands common for all drivers
* (returns STAT_INSTCMD_* state values per enum in upshandler.h)
*/
int main_instcmd(const char *cmdname, const char *extra);

/* handle setting variables common for all drivers
* (returns STAT_SET_* state values per enum in upshandler.h)
*/
int main_setvar(const char *varname, const char *val);

/* main calls this driver function - it needs to call addvar */
void upsdrv_makevartable(void);

Expand Down

0 comments on commit e2715f0

Please sign in to comment.