ctrl: Allow installation of additional node lookup helpers

The existing code assumes that the main application knows about all
control command nodes and can thus present one lookup function.

As libraries are getting their own control interface handling, this
is too restrictive, and we need a way how library code can dynamically
register more node lookup helpers.   We can now do this by means of a
ctrl_lookup_register() function.

Change-Id: Ib69908d1c57f5bb721d5496e3b4a5258fca450e3
This commit is contained in:
Harald Welte 2017-04-16 16:57:40 +02:00
parent a81055db1d
commit d6b1f85fd8
2 changed files with 43 additions and 0 deletions

View File

@ -29,3 +29,5 @@ struct ctrl_handle *ctrl_interface_setup_dynip(void *data,
ctrl_cmd_lookup lookup);
struct ctrl_connection *osmo_ctrl_conn_alloc(void *ctx, void *data);
int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data);
int ctrl_lookup_register(ctrl_cmd_lookup lookup);

View File

@ -61,6 +61,13 @@
vector ctrl_node_vec;
/* global list of control interface lookup helpers */
struct lookup_helper {
struct llist_head list;
ctrl_cmd_lookup lookup;
};
static LLIST_HEAD(ctrl_lookup_helpers);
int ctrl_parse_get_num(vector vline, int i, long *num)
{
char *token, *tmp;
@ -225,6 +232,7 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
}
for (i=0;i<vector_active(vline);i++) {
struct lookup_helper *lh;
int rc;
if (ctrl->lookup)
@ -232,6 +240,14 @@ int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
else
rc = 0;
if (!rc) {
llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
rc = lh->lookup(data, vline, &node, &cmd->node, &i);
if (rc)
break;
}
}
if (rc == 1) {
/* do nothing */
} else if (rc == -ENODEV)
@ -733,3 +749,28 @@ err:
talloc_free(ctrl);
return NULL;
}
/*! \brief Install a lookup helper function for control nodes
* This function is used by e.g. library code to install lookup helpers
* for additional nodes in the control interface.
* \param[in] lookup The lookup helper function
* \retuns - on success; negative on error.
*/
int ctrl_lookup_register(ctrl_cmd_lookup lookup)
{
struct lookup_helper *lh;
/* avoid double registration */
llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
if (lh->lookup == lookup)
return -EEXIST;
}
lh = talloc_zero(NULL, struct lookup_helper);
if (!lh)
return -ENOMEM;
lh->lookup = lookup;
llist_add_tail(&lh->list, &ctrl_lookup_helpers);
return 0;
}