blob: ca00f74795ba8c00f5e5d415f7d39ad08e0b1fba [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,
19 QmpCommandFunc *fn, QmpCommandOptions options)
Michael Roth43c20a42011-07-19 14:50:36 -050020{
Anthony Liguori7267c092011-08-20 22:09:37 -050021 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050022
23 cmd->name = name;
Michael Roth43c20a42011-07-19 14:50:36 -050024 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060025 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030026 cmd->options = options;
Markus Armbruster1527bad2017-03-03 13:32:25 +010027 QTAILQ_INSERT_TAIL(cmds, cmd, node);
Michael Roth43c20a42011-07-19 14:50:36 -050028}
29
Markus Armbruster1527bad2017-03-03 13:32:25 +010030QmpCommand *qmp_find_command(QmpCommandList *cmds, const char *name)
Michael Roth43c20a42011-07-19 14:50:36 -050031{
Michael Rothabd6cf62011-12-06 22:03:42 -060032 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050033
Markus Armbruster1527bad2017-03-03 13:32:25 +010034 QTAILQ_FOREACH(cmd, cmds, node) {
Michael Rothabd6cf62011-12-06 22:03:42 -060035 if (strcmp(cmd->name, name) == 0) {
36 return cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050037 }
38 }
39 return NULL;
40}
Michael Rothabd6cf62011-12-06 22:03:42 -060041
Markus Armbruster1527bad2017-03-03 13:32:25 +010042static void qmp_toggle_command(QmpCommandList *cmds, const char *name,
43 bool enabled)
Michael Rothabd6cf62011-12-06 22:03:42 -060044{
45 QmpCommand *cmd;
46
Markus Armbruster1527bad2017-03-03 13:32:25 +010047 QTAILQ_FOREACH(cmd, cmds, node) {
Michael Rothabd6cf62011-12-06 22:03:42 -060048 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050049 cmd->enabled = enabled;
Michael Rothabd6cf62011-12-06 22:03:42 -060050 return;
51 }
52 }
53}
54
Markus Armbruster1527bad2017-03-03 13:32:25 +010055void qmp_disable_command(QmpCommandList *cmds, const char *name)
Michael Rothf22d85e2012-04-17 19:01:45 -050056{
Markus Armbruster1527bad2017-03-03 13:32:25 +010057 qmp_toggle_command(cmds, name, false);
Michael Rothf22d85e2012-04-17 19:01:45 -050058}
59
Markus Armbruster1527bad2017-03-03 13:32:25 +010060void qmp_enable_command(QmpCommandList *cmds, const char *name)
Michael Rothf22d85e2012-04-17 19:01:45 -050061{
Markus Armbruster1527bad2017-03-03 13:32:25 +010062 qmp_toggle_command(cmds, name, true);
Michael Rothf22d85e2012-04-17 19:01:45 -050063}
64
Mark Wu8dc4d912013-10-09 11:25:07 +080065bool qmp_command_is_enabled(const QmpCommand *cmd)
Michael Rothbf95c0d2011-12-06 22:03:43 -060066{
Mark Wu8dc4d912013-10-09 11:25:07 +080067 return cmd->enabled;
Michael Rothbf95c0d2011-12-06 22:03:43 -060068}
69
Mark Wu8dc4d912013-10-09 11:25:07 +080070const char *qmp_command_name(const QmpCommand *cmd)
71{
72 return cmd->name;
73}
74
Mark Wu0106dc42013-10-09 10:37:26 +080075bool qmp_has_success_response(const QmpCommand *cmd)
76{
77 return !(cmd->options & QCO_NO_SUCCESS_RESP);
78}
79
Markus Armbruster1527bad2017-03-03 13:32:25 +010080void qmp_for_each_command(QmpCommandList *cmds, qmp_cmd_callback_fn fn,
81 void *opaque)
Michael Rothabd6cf62011-12-06 22:03:42 -060082{
83 QmpCommand *cmd;
Michael Rothabd6cf62011-12-06 22:03:42 -060084
Markus Armbruster1527bad2017-03-03 13:32:25 +010085 QTAILQ_FOREACH(cmd, cmds, node) {
Mark Wu8dc4d912013-10-09 11:25:07 +080086 fn(cmd, opaque);
Michael Rothabd6cf62011-12-06 22:03:42 -060087 }
Michael Rothabd6cf62011-12-06 22:03:42 -060088}