libosmoctrl  0.12.0
Osmocom CTRL library
control_cmd.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <osmocom/core/msgb.h>
6 #include <osmocom/core/talloc.h>
7 #include <osmocom/core/write_queue.h>
8 #include <osmocom/core/logging.h>
9 #include <osmocom/core/utils.h>
10 #include <osmocom/vty/vector.h>
11 
12 #define CTRL_CMD_ERROR -1
13 #define CTRL_CMD_HANDLED 0
14 #define CTRL_CMD_REPLY 1
15 
16 struct ctrl_handle;
17 
20  CTRL_NODE_ROOT, /* Root elements */
21  CTRL_NODE_BTS, /* BTS specific (net.btsN.) */
22  CTRL_NODE_TRX, /* TRX specific (net.btsN.trxM.) */
23  CTRL_NODE_TS, /* TS specific (net.btsN.trxM.tsI.) */
24  CTRL_NODE_FSM, /* Finite State Machine (description) */
25  CTRL_NODE_FSM_INST, /* Finite State Machine (instance) */
27 };
28 
30 enum ctrl_type {
38 };
39 
41 extern const struct value_string ctrl_type_vals[];
42 
45  struct llist_head list_entry;
46 
48  struct osmo_wqueue write_queue;
49 
51  struct msgb *pending_msg;
52 
54  void (*closed_cb)(struct ctrl_connection *conn);
55 
57  struct llist_head cmds;
58 
60  struct llist_head def_cmds;
61 };
62 
63 struct ctrl_cmd_def;
64 
66 struct ctrl_cmd {
71  char *id;
73  void *node;
75  char *variable;
77  char *value;
79  char *reply;
82 };
83 
84 #define ctrl_cmd_reply_printf(cmd, fmt, args ...) \
85  osmo_talloc_asprintf(cmd, cmd->reply, fmt, ## args)
86 
89  char **command;
90 };
91 
96  const char *name;
99  int (*set)(struct ctrl_cmd *cmd, void *data);
101  int (*get)(struct ctrl_cmd *cmd, void *data);
103  int (*verify)(struct ctrl_cmd *cmd, const char *value, void *data);
104 };
105 
106 struct ctrl_cmd_map {
107  char *cmd;
109 };
110 
111 /* deferred control command, i.e. responded asynchronously */
112 struct ctrl_cmd_def {
113  struct llist_head list; /* ctrl_connection.def_cmds */
114  struct ctrl_cmd *cmd;
115  void *data; /* opaque user data */
116 };
117 
118 struct ctrl_cmd_def *
119 ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs);
120 int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd);
121 int ctrl_cmd_def_send(struct ctrl_cmd_def *cd);
122 
123 int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data);
124 int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd);
125 int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd);
126 int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd);
127 struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed);
128 struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg);
129 struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg);
130 struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd);
131 struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd);
132 struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type);
133 struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd);
134 
139 #define CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_name) \
140 static struct ctrl_cmd_element cmd_##cmdname = { \
141  .name = cmdstr, \
142  .get = &get_##cmdname, \
143  .set = &set_##cmdname, \
144  .verify = verify_name, \
145 }
146 
151 #define CTRL_HELPER_GET_INT(cmdname, dtype, element) \
152 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
153 { \
154  dtype *node = cmd->node; \
155  cmd->reply = talloc_asprintf(cmd, "%i", node->element); \
156  if (!cmd->reply) { \
157  cmd->reply = "OOM"; \
158  return CTRL_CMD_ERROR; \
159  } \
160  return CTRL_CMD_REPLY; \
161 }
162 
167 #define CTRL_HELPER_SET_INT(cmdname, dtype, element) \
168 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
169 { \
170  dtype *node = cmd->node; \
171  int tmp = atoi(cmd->value); \
172  node->element = tmp; \
173  return get_##cmdname(cmd, _data); \
174 }
175 
180 #define CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
181 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *_data) \
182 { \
183  int tmp = atoi(value); \
184  if ((tmp >= min)&&(tmp <= max)) { \
185  return 0; \
186  } \
187  cmd->reply = "Input not within the range"; \
188  return -1; \
189 }
190 
198 #define CTRL_CMD_DEFINE_RANGE(cmdname, cmdstr, dtype, element, min, max) \
199  CTRL_HELPER_GET_INT(cmdname, dtype, element) \
200  CTRL_HELPER_SET_INT(cmdname, dtype, element) \
201  CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
202 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
203 
208 #define CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
209 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
210 { \
211  dtype *data = cmd->node; \
212  cmd->reply = talloc_asprintf(cmd, "%s", data->element); \
213  if (!cmd->reply) { \
214  cmd->reply = "OOM"; \
215  return CTRL_CMD_ERROR; \
216  } \
217  return CTRL_CMD_REPLY; \
218 }
219 
224 #define CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
225 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
226 { \
227  dtype *data = cmd->node; \
228  osmo_talloc_replace_string(cmd->node, &data->element, cmd->value); \
229  return get_##cmdname(cmd, _data); \
230 }
231 
239 #define CTRL_CMD_DEFINE_STRING(cmdname, cmdstr, dtype, element) \
240  CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
241  CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
242 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, NULL)
243 
247 #define CTRL_CMD_DEFINE(cmdname, cmdstr) \
248 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
249 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
250 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data); \
251 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
252 
256 #define CTRL_CMD_DEFINE_RO(cmdname, cmdstr) \
257 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
258 static int set_##cmdname(struct ctrl_cmd *cmd, void *data) \
259 { \
260  cmd->reply = "Read Only attribute"; \
261  return CTRL_CMD_ERROR; \
262 } \
263 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data) \
264 { \
265  cmd->reply = "Read Only attribute"; \
266  return 1; \
267 } \
268 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
269 
273 #define CTRL_CMD_DEFINE_WO(cmdname, cmdstr) \
274 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
275 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
276 { \
277  cmd->reply = "Write Only attribute"; \
278  return CTRL_CMD_ERROR; \
279 } \
280 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data); \
281 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
282 
286 #define CTRL_CMD_DEFINE_WO_NOVRF(cmdname, cmdstr) \
287 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
288 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
289 { \
290  cmd->reply = "Write Only attribute"; \
291  return CTRL_CMD_ERROR; \
292 } \
293 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data) \
294 { \
295  return 0; \
296 } \
297 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
298 
299 struct gsm_network;
ctrl_cmd_exec
int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
Execute a given received command.
Definition: control_cmd.c:98
ctrl_cmd_parse2
struct ctrl_cmd * ctrl_cmd_parse2(void *ctx, struct msgb *msg)
Parse/Decode CTRL from msgb into command struct.
Definition: control_cmd.c:322
ctrl_cmd::reply
char * reply
respnse message string
Definition: control_cmd.h:79
ctrl_cmd_def_is_zombie
int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
Determine if the given deferred control command is still alive or a zombie.
Definition: control_cmd.c:639
ctrl_connection::write_queue
struct osmo_wqueue write_queue
The queue for sending data back.
Definition: control_cmd.h:48
ctrl_cmd_map::type
enum ctrl_type type
Definition: control_cmd.h:108
ctrl_cmd_make
struct msgb * ctrl_cmd_make(struct ctrl_cmd *cmd)
Encode a given CTRL command from its parsed form into a message buffer.
Definition: control_cmd.c:517
ctrl_cmd_send
int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
Encode a CTRL command and append it to the given write queue.
Definition: control_if.c:124
CTRL_NODE_BTS
@ CTRL_NODE_BTS
Definition: control_cmd.h:21
ctrl_cmd::variable
char * variable
name of the variable
Definition: control_cmd.h:75
ctrl_cmd_element::strcmd
struct ctrl_cmd_struct strcmd
Definition: control_cmd.h:97
ctrl_cmd_struct
Definition: control_cmd.h:87
ctrl_cmd::id
char * id
Definition: control_cmd.h:71
ctrl_connection::pending_msg
struct msgb * pending_msg
Buffer for partial input data.
Definition: control_cmd.h:51
CTRL_NODE_ROOT
@ CTRL_NODE_ROOT
Definition: control_cmd.h:20
CTRL_NODE_TRX
@ CTRL_NODE_TRX
Definition: control_cmd.h:22
ctrl_connection::closed_cb
void(* closed_cb)(struct ctrl_connection *conn)
Callback if the connection was closed.
Definition: control_cmd.h:54
ctrl_handle
Definition: control_if.h:13
ctrl_type_vals
const struct value_string ctrl_type_vals[]
human-readable string names for ctrl_type
Definition: control_cmd.c:47
ctrl_cmd::value
char * value
value of the specified CTRL variable
Definition: control_cmd.h:77
CTRL_TYPE_UNKNOWN
@ CTRL_TYPE_UNKNOWN
Definition: control_cmd.h:31
ctrl_cmd_map
Definition: control_cmd.h:106
ctrl_cmd_def
Definition: control_cmd.h:112
CTRL_NODE_FSM
@ CTRL_NODE_FSM
Definition: control_cmd.h:24
CTRL_TYPE_SET
@ CTRL_TYPE_SET
Definition: control_cmd.h:33
ctrl_cmd_element::get
int(* get)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the GET operation
Definition: control_cmd.h:101
ctrl_cmd_def::data
void * data
Definition: control_cmd.h:115
ctrl_cmd_element::set
int(* set)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the SET operation
Definition: control_cmd.h:99
ctrl_type
ctrl_type
Ctrl command types (GET, SET, ...)
Definition: control_cmd.h:30
ctrl_cmd_send_to_all
int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd)
Send a CTRL command to all connections.
Definition: control_if.c:106
ctrl_cmd_trap
struct ctrl_cmd * ctrl_cmd_trap(struct ctrl_cmd *cmd)
Copy given cmd and convert copy to CTRL_TYPE_TRAP.
Definition: control_if.c:170
ctrl_cmd_def::cmd
struct ctrl_cmd * cmd
Definition: control_cmd.h:114
ctrl_cmd_struct::nr_commands
int nr_commands
Definition: control_cmd.h:88
CTRL_NODE_TS
@ CTRL_NODE_TS
Definition: control_cmd.h:23
ctrl_cmd
Represents a single ctrl command after parsing.
Definition: control_cmd.h:66
ctrl_cmd::type
enum ctrl_type type
command type
Definition: control_cmd.h:70
ctrl_cmd_element
Implementation of a given CTRL command.
Definition: control_cmd.h:94
ctrl_cmd::node
void * node
node of the specified variable
Definition: control_cmd.h:73
CTRL_TYPE_GET
@ CTRL_TYPE_GET
Definition: control_cmd.h:32
CTRL_TYPE_GET_REPLY
@ CTRL_TYPE_GET_REPLY
Definition: control_cmd.h:34
ctrl_cmd_element::verify
int(* verify)(struct ctrl_cmd *cmd, const char *value, void *data)
call-back function to validate a value; called before SET
Definition: control_cmd.h:103
ctrl_connection::def_cmds
struct llist_head def_cmds
Pending deferred command responses for this connection.
Definition: control_cmd.h:60
ctrl_node_type
ctrl_node_type
The class of node at which a ctrl command is registered to.
Definition: control_cmd.h:19
ctrl_cmd_struct::command
char ** command
Definition: control_cmd.h:89
_LAST_CTRL_NODE
@ _LAST_CTRL_NODE
Definition: control_cmd.h:26
ctrl_cmd::ccon
struct ctrl_connection * ccon
connection through which the command was received
Definition: control_cmd.h:68
ctrl_cmd_install
int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
Install a given command definition at a given CTRL node.
Definition: control_cmd.c:213
ctrl_connection::cmds
struct llist_head cmds
Pending commands for this connection.
Definition: control_cmd.h:57
ctrl_connection
Represents a single ctrl connection.
Definition: control_cmd.h:44
CTRL_NODE_FSM_INST
@ CTRL_NODE_FSM_INST
Definition: control_cmd.h:25
ctrl_cmd_create
struct ctrl_cmd * ctrl_cmd_create(void *ctx, enum ctrl_type)
Allocate a control command of given type.
Definition: control_cmd.c:238
ctrl_cmd_parse
struct ctrl_cmd * ctrl_cmd_parse(void *ctx, struct msgb *msg)
Parse/Decode CTRL from msgb into command struct.
Definition: control_cmd.c:295
ctrl_cmd_def_make
struct ctrl_cmd_def * ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
Build a deferred control command state and keep it the per-connection list of deferred commands.
Definition: control_cmd.c:617
ctrl_cmd_cpy
struct ctrl_cmd * ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
Perform a deepl copy of the given cmd, allocating memory from ctx.
Definition: control_cmd.c:254
ctrl_cmd::defer
struct ctrl_cmd_def * defer
state representing deferred (async) response, if any
Definition: control_cmd.h:81
ctrl_cmd_def_send
int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
Send the response to a deferred ctrl command.
Definition: control_cmd.c:656
ctrl_cmd_map::cmd
char * cmd
Definition: control_cmd.h:107
CTRL_TYPE_ERROR
@ CTRL_TYPE_ERROR
Definition: control_cmd.h:37
CTRL_TYPE_TRAP
@ CTRL_TYPE_TRAP
Definition: control_cmd.h:36
ctrl_cmd_def::list
struct llist_head list
Definition: control_cmd.h:113
ctrl_cmd_element::name
const char * name
textual name/id of the CTRL command
Definition: control_cmd.h:96
ctrl_connection::list_entry
struct llist_head list_entry
Definition: control_cmd.h:45
CTRL_TYPE_SET_REPLY
@ CTRL_TYPE_SET_REPLY
Definition: control_cmd.h:35
ctrl_cmd_parse3
struct ctrl_cmd * ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
Parse/Decode CTRL from msgb into command struct.
Definition: control_cmd.c:336