|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <shell/shell.h> |
| 9 | +#include <event_manager.h> |
| 10 | + |
| 11 | +u32_t event_manager_displayed_events; |
| 12 | + |
| 13 | +static int show_events(const struct shell *shell, size_t argc, |
| 14 | + char **argv) |
| 15 | +{ |
| 16 | + shell_fprintf(shell, SHELL_NORMAL, |
| 17 | + "Registered Events:\n"); |
| 18 | + |
| 19 | + for (const struct event_type *et = __start_event_types; |
| 20 | + (et != NULL) && (et != __stop_event_types); et++) { |
| 21 | + |
| 22 | + size_t ev_id = et - __start_event_types; |
| 23 | + |
| 24 | + shell_fprintf(shell, |
| 25 | + SHELL_NORMAL, |
| 26 | + "%c %d:\t%s\n", |
| 27 | + (event_manager_displayed_events & BIT(ev_id)) ? |
| 28 | + 'E' : 'D', |
| 29 | + ev_id, |
| 30 | + et->name); |
| 31 | + } |
| 32 | + |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +static int show_listeners(const struct shell *shell, size_t argc, |
| 37 | + char **argv) |
| 38 | +{ |
| 39 | + shell_fprintf(shell, SHELL_NORMAL, "Registered Listeners:\n"); |
| 40 | + for (const struct event_listener *el = __start_event_listeners; |
| 41 | + el != __stop_event_listeners; |
| 42 | + el++) { |
| 43 | + |
| 44 | + __ASSERT_NO_MSG(el != NULL); |
| 45 | + shell_fprintf(shell, SHELL_NORMAL, "|\t[L:%s]\n", el->name); |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +static int show_subscribers(const struct shell *shell, size_t argc, |
| 52 | + char **argv) |
| 53 | +{ |
| 54 | + shell_fprintf(shell, SHELL_NORMAL, "Registered Subscribers:\n"); |
| 55 | + for (const struct event_type *et = __start_event_types; |
| 56 | + (et != NULL) && (et != __stop_event_types); |
| 57 | + et++) { |
| 58 | + |
| 59 | + bool is_subscribed = false; |
| 60 | + |
| 61 | + for (size_t prio = SUBS_PRIO_MIN; |
| 62 | + prio <= SUBS_PRIO_MAX; |
| 63 | + prio++) { |
| 64 | + for (const struct event_subscriber *es = |
| 65 | + et->subs_start[prio]; |
| 66 | + es != et->subs_stop[prio]; |
| 67 | + es++) { |
| 68 | + |
| 69 | + __ASSERT_NO_MSG(es != NULL); |
| 70 | + const struct event_listener *el = es->listener; |
| 71 | + |
| 72 | + __ASSERT_NO_MSG(el != NULL); |
| 73 | + shell_fprintf(shell, SHELL_NORMAL, |
| 74 | + "|\tprio:%u\t[E:%s] -> [L:%s]\n", |
| 75 | + prio, et->name, el->name); |
| 76 | + |
| 77 | + is_subscribed = true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (!is_subscribed) { |
| 82 | + shell_fprintf(shell, SHELL_NORMAL, |
| 83 | + "|\t[E:%s] has no subscribers\n", |
| 84 | + et->name); |
| 85 | + } |
| 86 | + shell_fprintf(shell, SHELL_NORMAL, "\n"); |
| 87 | + } |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +static void set_event_displaying(const struct shell *shell, size_t argc, |
| 93 | + char **argv, bool enable) |
| 94 | +{ |
| 95 | + u32_t evt_mask = 0; |
| 96 | + |
| 97 | + /* If no IDs specified, all registered events are affected */ |
| 98 | + if (argc == 1) { |
| 99 | + for (const struct event_type *et = __start_event_types; |
| 100 | + (et != NULL) && (et != __stop_event_types); |
| 101 | + et++) { |
| 102 | + |
| 103 | + size_t ev_id = et - __start_event_types; |
| 104 | + |
| 105 | + evt_mask |= BIT(ev_id); |
| 106 | + } |
| 107 | + |
| 108 | + shell_fprintf(shell, |
| 109 | + SHELL_NORMAL, |
| 110 | + "Displaying all events %sabled\n", |
| 111 | + enable ? "en":"dis"); |
| 112 | + } else { |
| 113 | + int event_indexes[argc - 1]; |
| 114 | + |
| 115 | + for (size_t i = 0; i < ARRAY_SIZE(event_indexes); i++) { |
| 116 | + char *end; |
| 117 | + |
| 118 | + event_indexes[i] = strtol(argv[i + 1], &end, 10); |
| 119 | + |
| 120 | + if ((event_indexes[i] < 0) |
| 121 | + || (event_indexes[i] >= __stop_event_types - __start_event_types) |
| 122 | + || (*end != '\0')) { |
| 123 | + |
| 124 | + shell_error(shell, "Invalid event ID: %s", |
| 125 | + argv[i + 1]); |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + for (size_t i = 0; i < ARRAY_SIZE(event_indexes); i++) { |
| 131 | + evt_mask |= BIT(event_indexes[i]); |
| 132 | + const struct event_type *et = |
| 133 | + __start_event_types + event_indexes[i]; |
| 134 | + const char *event_name = et->name; |
| 135 | + |
| 136 | + shell_fprintf(shell, |
| 137 | + SHELL_NORMAL, |
| 138 | + "Displaying event %s %sabled\n", |
| 139 | + event_name, |
| 140 | + enable ? "en":"dis"); |
| 141 | + } |
| 142 | + } |
| 143 | + if (enable) { |
| 144 | + event_manager_displayed_events |= evt_mask; |
| 145 | + } else { |
| 146 | + event_manager_displayed_events &= ~evt_mask; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +static int enable_event_displaying(const struct shell *shell, size_t argc, |
| 151 | + char **argv) |
| 152 | +{ |
| 153 | + set_event_displaying(shell, argc, argv, true); |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +static int disable_event_displaying(const struct shell *shell, size_t argc, |
| 158 | + char **argv) |
| 159 | +{ |
| 160 | + set_event_displaying(shell, argc, argv, false); |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +SHELL_CREATE_STATIC_SUBCMD_SET(sub_event_manager) |
| 166 | +{ |
| 167 | + SHELL_CMD_ARG(show_listeners, NULL, "Show listeners", |
| 168 | + show_listeners, 0, 0), |
| 169 | + SHELL_CMD_ARG(show_subscribers, NULL, "Show subscribers", |
| 170 | + show_subscribers, 0, 0), |
| 171 | + SHELL_CMD_ARG(show_events, NULL, "Show events", show_events, 0, 0), |
| 172 | + SHELL_CMD_ARG(disable, NULL, "Disable displaying event with given ID", |
| 173 | + disable_event_displaying, 0, |
| 174 | + sizeof(event_manager_displayed_events) * 8 - 1), |
| 175 | + SHELL_CMD_ARG(enable, NULL, "Enable displaying event with given ID", |
| 176 | + enable_event_displaying, 0, |
| 177 | + sizeof(event_manager_displayed_events) * 8 - 1), |
| 178 | + SHELL_SUBCMD_SET_END |
| 179 | +}; |
| 180 | + |
| 181 | +SHELL_CMD_REGISTER(event_manager, &sub_event_manager, |
| 182 | + "Event Manager commands", NULL); |
0 commit comments