Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file fsm_ctrl_commands.c */ |
| 2 | |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 3 | #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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 11 | /*! control interface lookup function for FSM's |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 12 | * \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 | */ |
| 18 | static 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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 32 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 33 | fsm = osmo_fsm_find_by_name(fsm_name); |
| 34 | if (!fsm) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 35 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 36 | *node_data = fsm; |
| 37 | *node_type = CTRL_NODE_FSM; |
Max | 409e897 | 2017-05-02 16:12:56 +0200 | [diff] [blame] | 38 | } else |
| 39 | return 0; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 40 | 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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 48 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 49 | fi = osmo_fsm_inst_find_by_name(fsm, inst_name); |
| 50 | if (!fi) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 51 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 52 | *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) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 59 | return -ERANGE; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 60 | fi = osmo_fsm_inst_find_by_id(fsm, inst_id); |
| 61 | if (!fi) |
Max | 33c7ba6 | 2017-05-02 16:24:12 +0200 | [diff] [blame] | 62 | return -ENODEV; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 63 | *node_data = fi; |
| 64 | *node_type = CTRL_NODE_FSM_INST; |
| 65 | } |
| 66 | break; |
| 67 | default: |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | return 1; |
Harald Welte | 31c0fef | 2017-04-16 17:26:30 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static 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 | } |
| 86 | CTRL_CMD_DEFINE_RO(fsm_inst_state, "state"); |
| 87 | |
| 88 | static 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 | } |
| 103 | CTRL_CMD_DEFINE_RO(fsm_inst_parent_name, "parent-name"); |
| 104 | |
| 105 | static 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 | } |
| 121 | CTRL_CMD_DEFINE_RO(fsm_inst_timer, "timer"); |
| 122 | |
| 123 | |
| 124 | static 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 | |
| 160 | CTRL_CMD_DEFINE_RO(fsm_inst_dump, "dump"); |
| 161 | |
| 162 | int 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 | } |