blob: 64324f2db05db0328ce2114dfd00a7adb55087ac [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;
Max409e8972017-05-02 16:12:56 +020036 } else
37 return 0;
Harald Welte31c0fef2017-04-16 17:26:30 +020038 break;
39 case CTRL_NODE_FSM:
40 fsm = *node_data;
41 if (!strcmp(token, "name")) {
42 const char *inst_name;
43 (*i)++;
44 inst_name = vector_lookup(vline, *i);
45 if (!inst_name)
46 goto err_index;
47 fi = osmo_fsm_inst_find_by_name(fsm, inst_name);
48 if (!fi)
49 goto err_missing;
50 *node_data = fi;
51 *node_type = CTRL_NODE_FSM_INST;
52 } else if (!strcmp(token, "id")) {
53 const char *inst_id;
54 (*i)++;
55 inst_id = vector_lookup(vline, *i);
56 if (!inst_id)
57 goto err_index;
58 fi = osmo_fsm_inst_find_by_id(fsm, inst_id);
59 if (!fi)
60 goto err_missing;
61 *node_data = fi;
62 *node_type = CTRL_NODE_FSM_INST;
63 }
64 break;
65 default:
66 return 0;
67 }
68
69 return 1;
70
71err_index:
72 return -ERANGE;
73err_missing:
74 return -ENODEV;
75}
76
77static int get_fsm_inst_state(struct ctrl_cmd *cmd, void *data)
78{
79 struct osmo_fsm_inst *fi = cmd->node;
80
81 if (!fi) {
82 cmd->reply = "No such FSM found";
83 return CTRL_CMD_ERROR;
84 }
85
86 cmd->reply = talloc_strdup(cmd, osmo_fsm_state_name(fi->fsm, fi->state));
87 return CTRL_CMD_REPLY;
88}
89CTRL_CMD_DEFINE_RO(fsm_inst_state, "state");
90
91static int get_fsm_inst_parent_name(struct ctrl_cmd *cmd, void *data)
92{
93 struct osmo_fsm_inst *fi = cmd->node;
94
95 if (!fi) {
96 cmd->reply = "No such FSM found";
97 return CTRL_CMD_ERROR;
98 }
99 if (!fi->proc.parent) {
100 cmd->reply = "No parent";
101 return CTRL_CMD_ERROR;
102 }
103 cmd->reply = talloc_strdup(cmd, fi->proc.parent->name);
104 return CTRL_CMD_REPLY;
105}
106CTRL_CMD_DEFINE_RO(fsm_inst_parent_name, "parent-name");
107
108static int get_fsm_inst_timer(struct ctrl_cmd *cmd, void *data)
109{
110 struct osmo_fsm_inst *fi = cmd->node;
111 struct timeval remaining;
112
113 if (!fi) {
114 cmd->reply = "No such FSM found";
115 return CTRL_CMD_ERROR;
116 }
117 if (osmo_timer_remaining(&fi->timer, NULL, &remaining) < 0)
118 cmd->reply = "0,0,0";
119 else
120 cmd->reply = talloc_asprintf(cmd, "%u,%ld,%ld", fi->T, remaining.tv_sec, remaining.tv_usec);
121
122 return CTRL_CMD_REPLY;
123}
124CTRL_CMD_DEFINE_RO(fsm_inst_timer, "timer");
125
126
127static int get_fsm_inst_dump(struct ctrl_cmd *cmd, void *data)
128{
129 struct osmo_fsm_inst *fi = cmd->node;
130 struct osmo_fsm_inst *child;
131
132 if (!fi) {
133 cmd->reply = "No such FSM found";
134 return CTRL_CMD_ERROR;
135 }
136
137 /* Fixed Part: Name, ID, log_level, state, timer number */
138 cmd->reply = talloc_asprintf(cmd, "'%s','%s','%s','%s',%u", fi->name, fi->id,
139 log_level_str(fi->log_level),
140 osmo_fsm_state_name(fi->fsm, fi->state), fi->T);
141
142 /* Variable Parts below */
143 if (fi->T) {
144 struct timeval remaining;
145 int rc;
146 rc = osmo_timer_remaining(&fi->timer, NULL, &remaining);
147 if (rc == 0) {
148 cmd->reply = talloc_asprintf_append(cmd->reply, ",timeout_sec=%ld,timeout_usec=%ld",
149 remaining.tv_sec, remaining.tv_usec);
150 }
151 }
152
153 if (fi->proc.parent)
154 cmd->reply = talloc_asprintf_append(cmd->reply, ",parent='%s'", fi->proc.parent->name);
155
156 llist_for_each_entry(child, &fi->proc.children, list) {
157 cmd->reply = talloc_asprintf_append(cmd->reply, ",child='%s'", child->name);
158 }
159
160 return CTRL_CMD_REPLY;
161}
162
163CTRL_CMD_DEFINE_RO(fsm_inst_dump, "dump");
164
165int osmo_fsm_ctrl_cmds_install(void)
166{
167 int rc = 0;
168
169 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_dump);
170 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_state);
171 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_parent_name);
172 rc |= ctrl_cmd_install(CTRL_NODE_FSM_INST, &cmd_fsm_inst_timer);
173 rc |= ctrl_lookup_register(fsm_ctrl_node_lookup);
174
175 return rc;
176}