blob: 485bc5e6fc713ebd0d44888c2be4fc31a00d988c [file] [log] [blame]
Michael Roth43c20a42011-07-19 14:50:36 -05001/*
2 * Core Definitions for QAPI/QMP Dispatch
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Roth <mdroth@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
Peter Maydellcbf21152016-01-29 17:49:57 +000015#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/dispatch.h"
Michael Roth43c20a42011-07-19 14:50:36 -050017
Markus Armbruster1527bad2017-03-03 13:32:25 +010018void qmp_register_command(QmpCommandList *cmds, const char *name,
Markus Armbruster6604e472021-10-28 12:25:17 +020019 QmpCommandFunc *fn, QmpCommandOptions options,
20 unsigned special_features)
Michael Roth43c20a42011-07-19 14:50:36 -050021{
Anthony Liguori7267c092011-08-20 22:09:37 -050022 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050023
Kevin Wolf9ce44e22020-10-05 17:58:50 +020024 /* QCO_COROUTINE and QCO_ALLOW_OOB are incompatible for now */
25 assert(!((options & QCO_COROUTINE) && (options & QCO_ALLOW_OOB)));
26
Michael Roth43c20a42011-07-19 14:50:36 -050027 cmd->name = name;
Michael Roth43c20a42011-07-19 14:50:36 -050028 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060029 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030030 cmd->options = options;
Markus Armbruster6604e472021-10-28 12:25:17 +020031 cmd->special_features = special_features;
Markus Armbruster1527bad2017-03-03 13:32:25 +010032 QTAILQ_INSERT_TAIL(cmds, cmd, node);
Michael Roth43c20a42011-07-19 14:50:36 -050033}
34
Marc-André Lureauf0ccc002020-03-16 18:18:24 +010035const QmpCommand *qmp_find_command(const QmpCommandList *cmds, const char *name)
Michael Roth43c20a42011-07-19 14:50:36 -050036{
Michael Rothabd6cf62011-12-06 22:03:42 -060037 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050038
Markus Armbruster1527bad2017-03-03 13:32:25 +010039 QTAILQ_FOREACH(cmd, cmds, node) {
Michael Rothabd6cf62011-12-06 22:03:42 -060040 if (strcmp(cmd->name, name) == 0) {
41 return cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050042 }
43 }
44 return NULL;
45}
Michael Rothabd6cf62011-12-06 22:03:42 -060046
Markus Armbruster1527bad2017-03-03 13:32:25 +010047static void qmp_toggle_command(QmpCommandList *cmds, const char *name,
Marc-André Lureauc98939d2021-02-19 12:28:14 +040048 bool enabled, const char *disable_reason)
Michael Rothabd6cf62011-12-06 22:03:42 -060049{
50 QmpCommand *cmd;
51
Markus Armbruster1527bad2017-03-03 13:32:25 +010052 QTAILQ_FOREACH(cmd, cmds, node) {
Michael Rothabd6cf62011-12-06 22:03:42 -060053 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050054 cmd->enabled = enabled;
Marc-André Lureauc98939d2021-02-19 12:28:14 +040055 cmd->disable_reason = disable_reason;
Michael Rothabd6cf62011-12-06 22:03:42 -060056 return;
57 }
58 }
59}
60
Marc-André Lureauc98939d2021-02-19 12:28:14 +040061void qmp_disable_command(QmpCommandList *cmds, const char *name,
62 const char *disable_reason)
Michael Rothf22d85e2012-04-17 19:01:45 -050063{
Marc-André Lureauc98939d2021-02-19 12:28:14 +040064 qmp_toggle_command(cmds, name, false, disable_reason);
Michael Rothf22d85e2012-04-17 19:01:45 -050065}
66
Markus Armbruster1527bad2017-03-03 13:32:25 +010067void qmp_enable_command(QmpCommandList *cmds, const char *name)
Michael Rothf22d85e2012-04-17 19:01:45 -050068{
Marc-André Lureauc98939d2021-02-19 12:28:14 +040069 qmp_toggle_command(cmds, name, true, NULL);
Michael Rothf22d85e2012-04-17 19:01:45 -050070}
71
Mark Wu8dc4d912013-10-09 11:25:07 +080072bool qmp_command_is_enabled(const QmpCommand *cmd)
Michael Rothbf95c0d2011-12-06 22:03:43 -060073{
Mark Wu8dc4d912013-10-09 11:25:07 +080074 return cmd->enabled;
Michael Rothbf95c0d2011-12-06 22:03:43 -060075}
76
Mark Wu8dc4d912013-10-09 11:25:07 +080077const char *qmp_command_name(const QmpCommand *cmd)
78{
79 return cmd->name;
80}
81
Mark Wu0106dc42013-10-09 10:37:26 +080082bool qmp_has_success_response(const QmpCommand *cmd)
83{
84 return !(cmd->options & QCO_NO_SUCCESS_RESP);
85}
86
Marc-André Lureauf0ccc002020-03-16 18:18:24 +010087void qmp_for_each_command(const QmpCommandList *cmds, qmp_cmd_callback_fn fn,
Markus Armbruster1527bad2017-03-03 13:32:25 +010088 void *opaque)
Michael Rothabd6cf62011-12-06 22:03:42 -060089{
Marc-André Lureauf0ccc002020-03-16 18:18:24 +010090 const QmpCommand *cmd;
Michael Rothabd6cf62011-12-06 22:03:42 -060091
Markus Armbruster1527bad2017-03-03 13:32:25 +010092 QTAILQ_FOREACH(cmd, cmds, node) {
Mark Wu8dc4d912013-10-09 11:25:07 +080093 fn(cmd, opaque);
Michael Rothabd6cf62011-12-06 22:03:42 -060094 }
Michael Rothabd6cf62011-12-06 22:03:42 -060095}