libosmoctrl  1.11.0.2-4150.202502242026
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) */
27  CTRL_NODE_LCHAN, /* LCHAN specific (net.btsN.trxM.tsI.lchanL) */
29 };
30 
32 enum ctrl_type {
40 };
41 
43 extern const struct value_string ctrl_type_vals[];
44 
47  struct llist_head list_entry;
48 
50  struct osmo_wqueue write_queue;
51 
53  struct msgb *pending_msg;
54 
56  void (*closed_cb)(struct ctrl_connection *conn);
57 
59  struct llist_head cmds;
60 
62  struct llist_head def_cmds;
63 };
64 
65 struct ctrl_cmd_def;
66 
68 struct ctrl_cmd {
72  enum ctrl_type type;
73  char *id;
75  void *node;
77  char *variable;
79  char *value;
81  char *reply;
84 };
85 
86 #define ctrl_cmd_reply_printf(cmd, fmt, args ...) \
87  osmo_talloc_asprintf(cmd, cmd->reply, fmt, ## args)
88 
91  char **command;
92 };
93 
98  const char *name;
99  struct ctrl_cmd_struct strcmd;
101  int (*set)(struct ctrl_cmd *cmd, void *data);
103  int (*get)(struct ctrl_cmd *cmd, void *data);
105  int (*verify)(struct ctrl_cmd *cmd, const char *value, void *data);
106 };
107 
108 struct ctrl_cmd_map {
109  char *cmd;
110  enum ctrl_type type;
111 };
112 
113 /* deferred control command, i.e. responded asynchronously */
114 struct ctrl_cmd_def {
115  struct llist_head list; /* ctrl_connection.def_cmds */
116  struct ctrl_cmd *cmd;
117  void *data; /* opaque user data */
118 };
119 
120 struct ctrl_cmd_def *
121 ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs);
122 int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd);
123 int ctrl_cmd_def_send(struct ctrl_cmd_def *cd);
124 
125 int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data);
127 /* ctrl_cmd_send is also declared in control_if.h, but legacy openbsc.git expects it here */
128 int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd);
129 int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd);
130 struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed);
131 struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg);
132 struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg);
133 struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd);
134 struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd);
135 struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type);
136 struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd);
137 
142 #define CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_name) \
143 static struct ctrl_cmd_element cmd_##cmdname = { \
144  .name = cmdstr, \
145  .get = &get_##cmdname, \
146  .set = &set_##cmdname, \
147  .verify = verify_name, \
148 }
149 
154 #define CTRL_HELPER_GET_INT(cmdname, dtype, element) \
155 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
156 { \
157  dtype *node = cmd->node; \
158  cmd->reply = talloc_asprintf(cmd, "%i", node->element); \
159  if (!cmd->reply) { \
160  cmd->reply = "OOM"; \
161  return CTRL_CMD_ERROR; \
162  } \
163  return CTRL_CMD_REPLY; \
164 }
165 
170 #define CTRL_HELPER_SET_INT(cmdname, dtype, element) \
171 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
172 { \
173  dtype *node = cmd->node; \
174  int tmp = atoi(cmd->value); \
175  node->element = tmp; \
176  return get_##cmdname(cmd, _data); \
177 }
178 
183 #define CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
184 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *_data) \
185 { \
186  int tmp = atoi(value); \
187  if ((tmp >= min)&&(tmp <= max)) { \
188  return 0; \
189  } \
190  cmd->reply = "Input not within the range"; \
191  return -1; \
192 }
193 
201 #define CTRL_CMD_DEFINE_RANGE(cmdname, cmdstr, dtype, element, min, max) \
202  CTRL_HELPER_GET_INT(cmdname, dtype, element) \
203  CTRL_HELPER_SET_INT(cmdname, dtype, element) \
204  CTRL_HELPER_VERIFY_RANGE(cmdname, min, max) \
205 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
206 
211 #define CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
212 static int get_##cmdname(struct ctrl_cmd *cmd, void *_data) \
213 { \
214  dtype *data = cmd->node; \
215  cmd->reply = talloc_asprintf(cmd, "%s", data->element); \
216  if (!cmd->reply) { \
217  cmd->reply = "OOM"; \
218  return CTRL_CMD_ERROR; \
219  } \
220  return CTRL_CMD_REPLY; \
221 }
222 
227 #define CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
228 static int set_##cmdname(struct ctrl_cmd *cmd, void *_data) \
229 { \
230  dtype *data = cmd->node; \
231  osmo_talloc_replace_string(cmd->node, &data->element, cmd->value); \
232  return get_##cmdname(cmd, _data); \
233 }
234 
240 #define CTRL_CMD_DEFINE_STRING(cmdname, cmdstr, dtype, element) \
241  CTRL_HELPER_GET_STRING(cmdname, dtype, element) \
242  CTRL_HELPER_SET_STRING(cmdname, dtype, element) \
243 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, NULL)
244 
248 #define CTRL_CMD_DEFINE(cmdname, cmdstr) \
249 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
250 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
251 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data); \
252 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
253 
257 #define CTRL_CMD_DEFINE_RO(cmdname, cmdstr) \
258 static int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
259 static int set_##cmdname(struct ctrl_cmd *cmd, void *data) \
260 { \
261  cmd->reply = "Read Only attribute"; \
262  return CTRL_CMD_ERROR; \
263 } \
264 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data) \
265 { \
266  cmd->reply = "Read Only attribute"; \
267  return 1; \
268 } \
269 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
270 
274 #define CTRL_CMD_DEFINE_WO(cmdname, cmdstr) \
275 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
276 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
277 { \
278  cmd->reply = "Write Only attribute"; \
279  return CTRL_CMD_ERROR; \
280 } \
281 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data); \
282 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
283 
287 #define CTRL_CMD_DEFINE_WO_NOVRF(cmdname, cmdstr) \
288 static int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
289 static int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
290 { \
291  cmd->reply = "Write Only attribute"; \
292  return CTRL_CMD_ERROR; \
293 } \
294 static int verify_##cmdname(struct ctrl_cmd *cmd, const char *val, void *data) \
295 { \
296  return 0; \
297 } \
298 CTRL_CMD_DEFINE_STRUCT(cmdname, cmdstr, verify_##cmdname)
299 
300 struct gsm_network;
ctrl_type
Ctrl command types (GET, SET, ...)
Definition: control_cmd.h:32
@ CTRL_TYPE_GET_REPLY
Definition: control_cmd.h:36
@ CTRL_TYPE_ERROR
Definition: control_cmd.h:39
@ CTRL_TYPE_SET_REPLY
Definition: control_cmd.h:37
@ CTRL_TYPE_SET
Definition: control_cmd.h:35
@ CTRL_TYPE_GET
Definition: control_cmd.h:34
@ CTRL_TYPE_UNKNOWN
Definition: control_cmd.h:33
@ CTRL_TYPE_TRAP
Definition: control_cmd.h:38
struct ctrl_cmd * ctrl_cmd_parse2(void *ctx, struct msgb *msg)
Parse/Decode CTRL from Message buffers into command struct.
Definition: control_cmd.c:329
struct ctrl_cmd * ctrl_cmd_create(void *ctx, enum ctrl_type)
Allocate a control command of given type.
Definition: control_cmd.c:245
struct ctrl_cmd * ctrl_cmd_trap(struct ctrl_cmd *cmd)
Copy given cmd and convert copy to CTRL_TYPE_TRAP.
Definition: control_if.c:175
const struct value_string ctrl_type_vals[]
human-readable string names for ctrl_type
Definition: control_cmd.c:44
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:343
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:615
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:261
int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
Send the response to a deferred ctrl command.
Definition: control_cmd.c:632
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
@ CTRL_NODE_LCHAN
Definition: control_cmd.h:27
@ _LAST_CTRL_NODE
Definition: control_cmd.h:28
@ 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:593
int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
Encode a CTRL command and append it to the given ctrl_connection.
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:302
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:210
int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
Execute a given received command.
Definition: control_cmd.c:95
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:524
uint16_t node
uint8_t data[0]
Definition: control_cmd.h:114
void * data
Definition: control_cmd.h:117
struct llist_head list
Definition: control_cmd.h:115
struct ctrl_cmd * cmd
Definition: control_cmd.h:116
Implementation of a given CTRL command.
Definition: control_cmd.h:96
int(* set)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the SET operation
Definition: control_cmd.h:101
struct ctrl_cmd_struct strcmd
Definition: control_cmd.h:99
const char * name
textual name/id of the CTRL command
Definition: control_cmd.h:98
int(* get)(struct ctrl_cmd *cmd, void *data)
call-back function implementing the GET operation
Definition: control_cmd.h:103
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:105
Definition: control_cmd.h:108
char * cmd
Definition: control_cmd.h:109
enum ctrl_type type
Definition: control_cmd.h:110
Definition: control_cmd.h:89
char ** command
Definition: control_cmd.h:91
int nr_commands
Definition: control_cmd.h:90
Represents a single ctrl command after parsing.
Definition: control_cmd.h:68
struct ctrl_cmd_def * defer
state representing deferred (async) response, if any
Definition: control_cmd.h:83
struct ctrl_connection * ccon
connection through which the command was received
Definition: control_cmd.h:70
char * variable
name of the variable
Definition: control_cmd.h:77
char * value
value of the specified CTRL variable
Definition: control_cmd.h:79
enum ctrl_type type
command type
Definition: control_cmd.h:72
char * id
Definition: control_cmd.h:73
void * node
node of the specified variable
Definition: control_cmd.h:75
char * reply
respnse message string
Definition: control_cmd.h:81
Represents a single ctrl connection.
Definition: control_cmd.h:46
void(* closed_cb)(struct ctrl_connection *conn)
Callback if the connection was closed.
Definition: control_cmd.h:56
struct llist_head list_entry
Definition: control_cmd.h:47
struct osmo_wqueue write_queue
The queue for sending data back.
Definition: control_cmd.h:50
struct msgb * pending_msg
Buffer for partial input data.
Definition: control_cmd.h:53
struct llist_head def_cmds
Pending deferred command responses for this connection.
Definition: control_cmd.h:62
struct llist_head cmds
Pending commands for this connection.
Definition: control_cmd.h:59
Definition: control_if.h:14