Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 1 | /* |
| 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 Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/dispatch.h" |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 17 | |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 18 | static QTAILQ_HEAD(QmpCommandList, QmpCommand) qmp_commands = |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 19 | QTAILQ_HEAD_INITIALIZER(qmp_commands); |
| 20 | |
Luiz Capitulino | d34b867 | 2012-05-08 14:24:44 -0300 | [diff] [blame] | 21 | void qmp_register_command(const char *name, QmpCommandFunc *fn, |
| 22 | QmpCommandOptions options) |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 23 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 24 | QmpCommand *cmd = g_malloc0(sizeof(*cmd)); |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 25 | |
| 26 | cmd->name = name; |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 27 | cmd->fn = fn; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 28 | cmd->enabled = true; |
Luiz Capitulino | d34b867 | 2012-05-08 14:24:44 -0300 | [diff] [blame] | 29 | cmd->options = options; |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 30 | QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node); |
| 31 | } |
| 32 | |
| 33 | QmpCommand *qmp_find_command(const char *name) |
| 34 | { |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 35 | QmpCommand *cmd; |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 36 | |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 37 | QTAILQ_FOREACH(cmd, &qmp_commands, node) { |
| 38 | if (strcmp(cmd->name, name) == 0) { |
| 39 | return cmd; |
Michael Roth | 43c20a4 | 2011-07-19 14:50:36 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | return NULL; |
| 43 | } |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 44 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 45 | static void qmp_toggle_command(const char *name, bool enabled) |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 46 | { |
| 47 | QmpCommand *cmd; |
| 48 | |
| 49 | QTAILQ_FOREACH(cmd, &qmp_commands, node) { |
| 50 | if (strcmp(cmd->name, name) == 0) { |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 51 | cmd->enabled = enabled; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame] | 57 | void qmp_disable_command(const char *name) |
| 58 | { |
| 59 | qmp_toggle_command(name, false); |
| 60 | } |
| 61 | |
| 62 | void qmp_enable_command(const char *name) |
| 63 | { |
| 64 | qmp_toggle_command(name, true); |
| 65 | } |
| 66 | |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 67 | bool qmp_command_is_enabled(const QmpCommand *cmd) |
Michael Roth | bf95c0d | 2011-12-06 22:03:43 -0600 | [diff] [blame] | 68 | { |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 69 | return cmd->enabled; |
Michael Roth | bf95c0d | 2011-12-06 22:03:43 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 72 | const char *qmp_command_name(const QmpCommand *cmd) |
| 73 | { |
| 74 | return cmd->name; |
| 75 | } |
| 76 | |
Mark Wu | 0106dc4 | 2013-10-09 10:37:26 +0800 | [diff] [blame] | 77 | bool qmp_has_success_response(const QmpCommand *cmd) |
| 78 | { |
| 79 | return !(cmd->options & QCO_NO_SUCCESS_RESP); |
| 80 | } |
| 81 | |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 82 | void qmp_for_each_command(qmp_cmd_callback_fn fn, void *opaque) |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 83 | { |
| 84 | QmpCommand *cmd; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 85 | |
| 86 | QTAILQ_FOREACH(cmd, &qmp_commands, node) { |
Mark Wu | 8dc4d91 | 2013-10-09 11:25:07 +0800 | [diff] [blame] | 87 | fn(cmd, opaque); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 88 | } |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 89 | } |