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