Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file fsm_ctrl_commands.c */ |
| 2 | |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | /* (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 9 | #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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 17 | /*! control interface lookup function for FSM's |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 18 | * \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 | */ |
| 24 | static 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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 38 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 39 | fsm = osmo_fsm_find_by_name(fsm_name); |
| 40 | if (!fsm) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 41 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 42 | *node_data = fsm; |
| 43 | *node_type = CTRL_NODE_FSM; |
Max | 409e897 | 2017-05-02 16:12:56 +0200 | [diff] [blame] | 44 | } else |
| 45 | return 0; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 46 | 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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 54 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 55 | fi = osmo_fsm_inst_find_by_name(fsm, inst_name); |
| 56 | if (!fi) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 57 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 58 | *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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 65 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 66 | fi = osmo_fsm_inst_find_by_id(fsm, inst_id); |
| 67 | if (!fi) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 68 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 69 | *node_data = fi; |
| 70 | *node_type = CTRL_NODE_FSM_INST; |
| 71 | } |
| 72 | break; |
| 73 | default: |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | return 1; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static 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 | } |
| 92 | CTRL_CMD_DEFINE_RO(fsm_inst_state, "state"); |
| 93 | |
| 94 | static 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 | } |
| 109 | CTRL_CMD_DEFINE_RO(fsm_inst_parent_name, "parent-name"); |
| 110 | |
| 111 | static 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 | } |
| 127 | CTRL_CMD_DEFINE_RO(fsm_inst_timer, "timer"); |
| 128 | |
| 129 | |
| 130 | static 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 | |
| 159 | llist_for_each_entry(child, &fi->proc.children, list) { |
| 160 | cmd->reply = talloc_asprintf_append(cmd->reply, ",child='%s'", child->name); |
| 161 | } |
| 162 | |
| 163 | return CTRL_CMD_REPLY; |
| 164 | } |
| 165 | |
| 166 | CTRL_CMD_DEFINE_RO(fsm_inst_dump, "dump"); |
| 167 | |
| 168 | int 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 | } |