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