blob: 0dfc39622d0a0b5e6a865488a01283bf1a6f31a0 [file] [log] [blame]
Harald Welte31c0fef2017-04-16 17:26:30 +02001#include <string.h>
2#include <errno.h>
3
4#include <osmocom/core/fsm.h>
5
6#include <osmocom/ctrl/control_cmd.h>
7#include <osmocom/ctrl/control_if.h>
8
9/*! \brief control interface lookup function for FSM's
10 * \param[in] data Private data passed to controlif_setup()
11 * \param[in] vline Vector of the line holding the command string
12 * \param[out] node_type type (CTRL_NODE_) that was determined
13 * \param[out] node_data private data of node that was determined
14 * \param i Current index into vline, up to which it is parsed
15 */
16static int fsm_ctrl_node_lookup(void *data, vector vline, int *node_type,
17 void **node_data, int *i)
18{
19 struct osmo_fsm *fsm = NULL;
20 struct osmo_fsm_inst *fi = NULL;;
21 const char *token = vector_slot(vline, *i);
22
23 switch (*node_type) {
24 case CTRL_NODE_ROOT:
25 if (!strcmp(token, "fsm")) {
26 const char *fsm_name;
27 (*i)++;
28 fsm_name = vector_lookup(vline, *i);
29 if (!fsm_name)
30 goto err_index;
31 fsm = osmo_fsm_find_by_name(fsm_name);
32 if (!fsm)
33 goto err_missing;
34 *node_data = fsm;
35 *node_type = CTRL_NODE_FSM;
36 }
37 break;
38 case CTRL_NODE_FSM:
39 fsm = *node_data;
40 if (!strcmp(token, "name")) {
41 const char *inst_name;
42 (*i)++;
43 inst_name = vector_lookup(vline, *i);
44 if (!inst_name)
45 goto err_index;
46 fi = osmo_fsm_inst_find_by_name(fsm, inst_name);
47 if (!fi)
48 goto err_missing;
49 *node_data = fi;
50 *node_type = CTRL_NODE_FSM_INST;
51 } else if (!strcmp(token, "id")) {
52 const char *inst_id;
53 (*i)++;
54 inst_id = vector_lookup(vline, *i);
55 if (!inst_id)
56 goto err_index;
57 fi = osmo_fsm_inst_find_by_id(fsm, inst_id);
58 if (!fi)
59 goto err_missing;
60 *node_data = fi;
61 *node_type = CTRL_NODE_FSM_INST;
62 }
63 break;
64 default:
65 return 0;
66 }
67
68 return 1;
69
70err_index:
71 return -ERANGE;
72err_missing:
73 return -ENODEV;
74}
75
76static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data)
77{
78 struct osmo_fsm_inst *fi = cmd->node;
79
80 if (!fi) {
81 cmd->reply = "No such FSM found";
82 return CTRL_CMD_ERROR;
83 }
84
85 cmd->reply = talloc_strdup(cmd, osmo_fsm_state_name(fi->fsm, fi->state));
86 return CTRL_CMD_REPLY;
87}
88CTRL_CMD_DEFINE_RO(fsm_inst_state, "state");
89
90static int get_fsm_inst_parent_name(struct ctrl_cmd *cmd, void *data)
91{
92 struct osmo_fsm_inst *fi = cmd->node;
93
94 if (!fi) {
95 cmd->reply = "No such FSM found";
96 return CTRL_CMD_ERROR;
97 }
98 if (!fi->proc.parent) {
99 cmd->reply = "No parent";
100 return CTRL_CMD_ERROR;
101 }
102 cmd->reply = talloc_strdup(cmd, fi->proc.parent->name);
103 return CTRL_CMD_REPLY;
104}
105CTRL_CMD_DEFINE_RO(fsm_inst_parent_name, "parent-name");
106
107static int get_fsm_inst_timer(struct ctrl_cmd *cmd, void *data)
108{
109 struct osmo_fsm_inst *fi = cmd->node;
110 struct timeval remaining;
111
112 if (!fi) {
113 cmd->reply = "No such FSM found";
114 return CTRL_CMD_ERROR;
115 }
116 if (osmo_timer_remaining(&fi->timer, NULL, &remaining) < 0)
117 cmd->reply = "0,0,0";
118 else
119 cmd->reply = talloc_asprintf(cmd, "%u,%ld,%ld", fi->T, remaining.tv_sec, remaining.tv_usec);
120
121 return CTRL_CMD_REPLY;
122}
123CTRL_CMD_DEFINE_RO(fsm_inst_timer, "timer");
124
125
126static int get_fsm_inst_dump(struct ctrl_cmd *cmd, void *data)
127{
128 struct osmo_fsm_inst *fi = cmd->node;
129 struct osmo_fsm_inst *child;
130
131 if (!fi) {
132 cmd->reply = "No such FSM found";
133 return CTRL_CMD_ERROR;
134 }
135
136 /* Fixed Part: Name, ID, log_level, state, timer number */
137 cmd->reply = talloc_asprintf(cmd, "'%s','%s','%s','%s',%u", fi->name, fi->id,
138 log_level_str(fi->log_level),
139 osmo_fsm_state_name(fi->fsm, fi->state), fi->T);
140
141 /* Variable Parts below */
142 if (fi->T) {
143 struct timeval remaining;
144 int rc;
145 rc = osmo_timer_remaining(&fi->timer, NULL, &remaining);
146 if (rc == 0) {
147 cmd->reply = talloc_asprintf_append(cmd->reply, ",timeout_sec=%ld,timeout_usec=%ld",
148 remaining.tv_sec, remaining.tv_usec);
149 }
150 }
151
152 if (fi->proc.parent)
153 cmd->reply = talloc_asprintf_append(cmd->reply, ",parent='%s'", fi->proc.parent->name);
154
155 llist_for_each_entry(child, &fi->proc.children, list) {
156 cmd->reply = talloc_asprintf_append(cmd->reply, ",child='%s'", child->name);
157 }
158
159 return CTRL_CMD_REPLY;
160}
161
162CTRL_CMD_DEFINE_RO(fsm_inst_dump, "dump");
163
164int osmo_fsm_ctrl_cmds_install(void)
165{
166 int rc = 0;
167
168 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_dump);
169 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_state);
170 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_parent_name);
171 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_timer);
172 rc |= ctrl_lookup_register(fsm_ctrl_node_lookup);
173
174 return rc;
175}