blob: 3e4498a3f6b88980f3c6194a1f7db501270fa111 [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
Eduardo Habkostccff63c2012-10-23 21:35:44 -020015#include <glib.h>
16#include <string.h>
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/dispatch.h"
Michael Roth43c20a42011-07-19 14:50:36 -050018
Michael Rothabd6cf62011-12-06 22:03:42 -060019static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands =
Michael Roth43c20a42011-07-19 14:50:36 -050020 QTAILQ_HEAD_INITIALIZER(qmp_commands);
21
Luiz Capitulinod34b8672012-05-08 14:24:44 -030022void qmp_register_command(const char *name, QmpCommandFunc *fn,
23 QmpCommandOptions options)
Michael Roth43c20a42011-07-19 14:50:36 -050024{
Anthony Liguori7267c092011-08-20 22:09:37 -050025 QmpCommand *cmd = g_malloc0(sizeof(*cmd));
Michael Roth43c20a42011-07-19 14:50:36 -050026
27 cmd->name = name;
28 cmd->type = QCT_NORMAL;
29 cmd->fn = fn;
Michael Rothabd6cf62011-12-06 22:03:42 -060030 cmd->enabled = true;
Luiz Capitulinod34b8672012-05-08 14:24:44 -030031 cmd->options = options;
Michael Roth43c20a42011-07-19 14:50:36 -050032 QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
33}
34
35QmpCommand *qmp_find_command(const char *name)
36{
Michael Rothabd6cf62011-12-06 22:03:42 -060037 QmpCommand *cmd;
Michael Roth43c20a42011-07-19 14:50:36 -050038
Michael Rothabd6cf62011-12-06 22:03:42 -060039 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
40 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
Michael Rothf22d85e2012-04-17 19:01:45 -050047static void qmp_toggle_command(const char *name, bool enabled)
Michael Rothabd6cf62011-12-06 22:03:42 -060048{
49 QmpCommand *cmd;
50
51 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
52 if (strcmp(cmd->name, name) == 0) {
Michael Rothf22d85e2012-04-17 19:01:45 -050053 cmd->enabled = enabled;
Michael Rothabd6cf62011-12-06 22:03:42 -060054 return;
55 }
56 }
57}
58
Michael Rothf22d85e2012-04-17 19:01:45 -050059void qmp_disable_command(const char *name)
60{
61 qmp_toggle_command(name, false);
62}
63
64void qmp_enable_command(const char *name)
65{
66 qmp_toggle_command(name, true);
67}
68
Mark Wu8dc4d912013-10-09 11:25:07 +080069bool qmp_command_is_enabled(const QmpCommand *cmd)
Michael Rothbf95c0d2011-12-06 22:03:43 -060070{
Mark Wu8dc4d912013-10-09 11:25:07 +080071 return cmd->enabled;
Michael Rothbf95c0d2011-12-06 22:03:43 -060072}
73
Mark Wu8dc4d912013-10-09 11:25:07 +080074const char *qmp_command_name(const QmpCommand *cmd)
75{
76 return cmd->name;
77}
78
Mark Wu0106dc42013-10-09 10:37:26 +080079bool qmp_has_success_response(const QmpCommand *cmd)
80{
81 return !(cmd->options & QCO_NO_SUCCESS_RESP);
82}
83
Mark Wu8dc4d912013-10-09 11:25:07 +080084void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque)
Michael Rothabd6cf62011-12-06 22:03:42 -060085{
86 QmpCommand *cmd;
Michael Rothabd6cf62011-12-06 22:03:42 -060087
88 QTAILQ_FOREACH(cmd, &qmp_commands, node) {
Mark Wu8dc4d912013-10-09 11:25:07 +080089 fn(cmd, opaque);
Michael Rothabd6cf62011-12-06 22:03:42 -060090 }
Michael Rothabd6cf62011-12-06 22:03:42 -060091}