libosmoctrl  1.9.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>
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 #define CTRL_CMD_TRAP_ID "0"
16 
17 struct ctrl_handle;
18 
21  CTRL_NODE_ROOT, /* Root elements */
22  CTRL_NODE_BTS, /* BTS specific (net.btsN.) */
23  CTRL_NODE_TRX, /* TRX specific (net.btsN.trxM.) */
24  CTRL_NODE_TS, /* TS specific (net.btsN.trxM.tsI.) */
25  CTRL_NODE_FSM, /* Finite State Machine (description) */
26  CTRL_NODE_FSM_INST, /* Finite State Machine (instance) */
28 };
29 
31 enum ctrl_type {
39 };
40 
42 extern const struct value_string ctrl_type_vals[];
43 
46  struct llist_head list_entry;
47 
49  struct osmo_wqueue write_queue;
50 
52  struct msgb *pending_msg;
53 
55  void (*closed_cb)(struct ctrl_connection *conn);
56 
58  struct llist_head cmds;
59 
61  struct llist_head def_cmds;
62 };
63 
64 struct ctrl_cmd_def;
65 
67 struct ctrl_cmd {
71  enum ctrl_type type;
72  char *id;
74  void *node;
76  char *variable;
78  char *value;
80  char *reply;
83 };
84 
85 #define ctrl_cmd_reply_printf(cmd, fmt, args ...) \
86  osmo_talloc_asprintf(cmd, cmd->reply, fmt, ## args)
87 
90  char **command;
91 };
92 
97  const char *name;
98  struct ctrl_cmd_struct strcmd;
100  int (*set)(struct ctrl_cmd *cmd, void *data);
102  int (*get)(struct ctrl_cmd *cmd, void *data);
104  int (*verify)(struct ctrl_cmd *cmd, const char *value, void *data);
105 };
106 
107 struct ctrl_cmd_map {
108  char *cmd;
109  enum ctrl_type type;
110 };
111 
112 /* deferred control command, i.e. responded asynchronously */
113 struct ctrl_cmd_def {
114  struct llist_head list; /* ctrl_connection.def_cmds */
115  struct ctrl_cmd *cmd;
116  void *data; /* opaque user data */
117 };
118 
119 struct ctrl_cmd_def *
120 ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs);
121 int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd);
122 int ctrl_cmd_def_send(struct ctrl_cmd_def *cd);
123 
124 int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data);
126 int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd);
127 int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd);
128 struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed);
129 struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg);
130 struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg);
131 struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd);
132 struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd);
133 struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type);
134 struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd);
135 
140 #define CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_name) \
141 static struct ctrl_cmd_element cmd_##cmdname = { \
142  .name = cmdstr, \
143  .get = &get_##cmdname, \
144  .set = &set_##cmdname, \
145  .verify = verify_name, \
146 }
147 
152 #define CTRL_HELPER_GET_INT(cmdname, dtype, element) \
153 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
154 { \
155  dtype *node = cmd->node; \
156  cmd->reply = talloc_asprintf(cmd, "%i", node->element); \
157  if (!cmd->reply) { \
158  cmd->reply = "OOM"; \
159  return CTRL_CMD_ERROR; \
160  } \
161  return CTRL_CMD_REPLY; \
162 }
163 
168 #define CTRL_HELPER_SET_INT(cmdname, dtype, element) \
169 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
170 { \
171  dtype *node = cmd->node; \
172  int tmp = atoi(cmd->value); \
173  node->element = tmp; \
174  return get_##cmdname(cmd, _data); \
175 }
176 
181 #define CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
182 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *_data) \
183 { \
184  int tmp = atoi(value); \
185  if ((tmp >= min)&&(tmp <= max)) { \
186  return 0; \
187  } \
188  cmd->reply = "Input not within the range"; \
189  return -1; \
190 }
191 
199 #define CTRL_CMD_DEFINE_RANGE(cmdname, cmdstr, dtype, element, min, max) \
200  CTRL_HELPER_GET_INT(cmdname, dtype, element) \
201  CTRL_HELPER_SET_INT(cmdname, dtype, element) \
202  CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
203 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
204 
209 #define CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
210 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
211 { \
212  dtype *data = cmd->node; \
213  cmd->reply = talloc_asprintf(cmd, "%s", data->element); \
214  if (!cmd->reply) { \
215  cmd->reply = "OOM"; \
216  return CTRL_CMD_ERROR; \
217  } \
218  return CTRL_CMD_REPLY; \
219 }
220 
225 #define CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
226 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
227 { \
228  dtype *data = cmd->node; \
229  osmo_talloc_replace_string(cmd->node, &data->element, cmd->value); \
230  return get_##cmdname(cmd, _data); \
231 }
232 
238 #define CTRL_CMD_DEFINE_STRING(cmdname, cmdstr, dtype, element) \
239  CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
240  CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
241 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, NULL)
242 
246 #define CTRL_CMD_DEFINE(cmdname, cmdstr) \
247 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
248 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
249 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data); \
250 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
251 
255 #define CTRL_CMD_DEFINE_RO(cmdname, cmdstr) \
256 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
257 static int set_##cmdname(struct ctrl_cmd *cmd, void *data) \
258 { \
259  cmd->reply = "Read Only attribute"; \
260  return CTRL_CMD_ERROR; \
261 } \
262 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data) \
263 { \
264  cmd->reply = "Read Only attribute"; \
265  return 1; \
266 } \
267 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
268 
272 #define CTRL_CMD_DEFINE_WO(cmdname, cmdstr) \
273 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
274 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
275 { \
276  cmd->reply = "Write Only attribute"; \
277  return CTRL_CMD_ERROR; \
278 } \
279 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data); \
280 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
281 
285 #define CTRL_CMD_DEFINE_WO_NOVRF(cmdname, cmdstr) \
286 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
287 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
288 { \
289  cmd->reply = "Write Only attribute"; \
290  return CTRL_CMD_ERROR; \
291 } \
292 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data) \
293 { \
294  return 0; \
295 } \
296 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
297 
298 struct gsm_network;
ctrl_type
Ctrl command types (GET, SET, ...)
Definition: control_cmd.h:31
@ CTRL_TYPE_GET_REPLY
Definition: control_cmd.h:35
@ CTRL_TYPE_ERROR
Definition: control_cmd.h:38
@ CTRL_TYPE_SET_REPLY
Definition: control_cmd.h:36
@ CTRL_TYPE_SET
Definition: control_cmd.h:34
@ CTRL_TYPE_GET
Definition: control_cmd.h:33
@ CTRL_TYPE_UNKNOWN
Definition: control_cmd.h:32
@ CTRL_TYPE_TRAP
Definition: control_cmd.h:37
struct ctrl_cmd * ctrl_cmd_parse2(void *ctx, struct msgb *msg)
Parse/Decode CTRL from Message buffers into command struct.
Definition: control_cmd.c:328
struct ctrl_cmd * ctrl_cmd_create(void *ctx, enum ctrl_type)
Allocate a control command of given type.
Definition: control_cmd.c:244
struct ctrl_cmd * ctrl_cmd_trap(struct ctrl_cmd *cmd)
Copy given cmd and convert copy to CTRL_TYPE_TRAP.
Definition: control_if.c:165
const struct value_string ctrl_type_vals[]
human-readable string names for ctrl_type
Definition: control_cmd.c:43
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:101
struct ctrl_cmd * ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
Parse/Decode CTRL from Message buffers into command struct.
Definition: control_cmd.c:342
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:614
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:260
int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
Send the response to a deferred ctrl command.
Definition: control_cmd.c:631
ctrl_node_type
The class of node at which a ctrl command is registered to.
Definition: control_cmd.h:20
@ CTRL_NODE_ROOT
Definition: control_cmd.h:21
@ _LAST_CTRL_NODE
Definition: control_cmd.h:27
@ CTRL_NODE_FSM
Definition: control_cmd.h:25
@ CTRL_NODE_TS
Definition: control_cmd.h:24
@ CTRL_NODE_TRX
Definition: control_cmd.h:23
@ CTRL_NODE_BTS
Definition: control_cmd.h:22
@ CTRL_NODE_FSM_INST
Definition: control_cmd.h:26
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:592
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:119
struct ctrl_cmd * ctrl_cmd_parse(void *ctx, struct msgb *msg)
Parse/Decode CTRL from Message buffers into command struct.
Definition: control_cmd.c:301
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:209
int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
Execute a given received command.
Definition: control_cmd.c:94
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:523
uint16_t node
uint8_t data[0]
Definition: control_cmd.h:113
void * data
Definition: control_cmd.h:116
struct llist_head list
Definition: control_cmd.h:114
struct ctrl_cmd * cmd
Definition: control_cmd.h:115
Implementation of a given CTRL command.
Definition: control_cmd.h:95
int(* set)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the SET operation
Definition: control_cmd.h:100
struct ctrl_cmd_struct strcmd
Definition: control_cmd.h:98
const char * name
textual name/id of the CTRL command
Definition: control_cmd.h:97
int(* get)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the GET operation
Definition: control_cmd.h:102
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:104
Definition: control_cmd.h:107
char * cmd
Definition: control_cmd.h:108
enum ctrl_type type
Definition: control_cmd.h:109
Definition: control_cmd.h:88
char ** command
Definition: control_cmd.h:90
int nr_commands
Definition: control_cmd.h:89
Represents a single ctrl command after parsing.
Definition: control_cmd.h:67
struct ctrl_cmd_def * defer
state representing deferred (async) response, if any
Definition: control_cmd.h:82
struct ctrl_connection * ccon
connection through which the command was received
Definition: control_cmd.h:69
char * variable
name of the variable
Definition: control_cmd.h:76
char * value
value of the specified CTRL variable
Definition: control_cmd.h:78
enum ctrl_type type
command type
Definition: control_cmd.h:71
char * id
Definition: control_cmd.h:72
void * node
node of the specified variable
Definition: control_cmd.h:74
char * reply
respnse message string
Definition: control_cmd.h:80
Represents a single ctrl connection.
Definition: control_cmd.h:45
void(* closed_cb)(struct ctrl_connection *conn)
Callback if the connection was closed.
Definition: control_cmd.h:55
struct llist_head list_entry
Definition: control_cmd.h:46
struct osmo_wqueue write_queue
The queue for sending data back.
Definition: control_cmd.h:49
struct msgb * pending_msg
Buffer for partial input data.
Definition: control_cmd.h:52
struct llist_head def_cmds
Pending deferred command responses for this connection.
Definition: control_cmd.h:61
struct llist_head cmds
Pending commands for this connection.
Definition: control_cmd.h:58
Definition: control_if.h:14