1# 2# QAPI event generator 3# 4# Copyright (c) 2014 Wenchao Xia 5# Copyright (c) 2015-2016 Red Hat Inc. 6# 7# Authors: 8# Wenchao Xia <wenchaoqemu@gmail.com> 9# Markus Armbruster <armbru@redhat.com> 10# 11# This work is licensed under the terms of the GNU GPL, version 2. 12# See the COPYING file in the top-level directory. 13 14from qapi import * 15 16 17def gen_event_send_proto(name, arg_type): 18 return 'void qapi_event_send_%(c_name)s(%(param)s)' % { 19 'c_name': c_name(name.lower()), 20 'param': gen_params(arg_type, 'Error **errp')} 21 22 23def gen_event_send_decl(name, arg_type): 24 return mcgen(''' 25 26%(proto)s; 27''', 28 proto=gen_event_send_proto(name, arg_type)) 29 30 31def gen_event_send(name, arg_type): 32 ret = mcgen(''' 33 34%(proto)s 35{ 36 QDict *qmp; 37 Error *err = NULL; 38 QMPEventFuncEmit emit; 39''', 40 proto=gen_event_send_proto(name, arg_type)) 41 42 if arg_type and arg_type.members: 43 ret += mcgen(''' 44 QmpOutputVisitor *qov; 45 Visitor *v; 46 QObject *obj; 47 48''') 49 50 ret += mcgen(''' 51 emit = qmp_event_get_func_emit(); 52 if (!emit) { 53 return; 54 } 55 56 qmp = qmp_event_build_dict("%(name)s"); 57 58''', 59 name=name) 60 61 if arg_type and arg_type.members: 62 ret += mcgen(''' 63 qov = qmp_output_visitor_new(); 64 v = qmp_output_get_visitor(qov); 65 66 visit_start_struct(v, NULL, NULL, "%(name)s", 0, &err); 67''', 68 name=name) 69 ret += gen_err_check() 70 ret += gen_visit_fields(arg_type.members, need_cast=True, 71 label='out_obj') 72 ret += mcgen(''' 73out_obj: 74 visit_end_struct(v, err ? NULL : &err); 75 if (err) { 76 goto out; 77 } 78 79 obj = qmp_output_get_qobject(qov); 80 g_assert(obj); 81 82 qdict_put_obj(qmp, "data", obj); 83''') 84 85 ret += mcgen(''' 86 emit(%(c_enum)s, qmp, &err); 87 88''', 89 c_enum=c_enum_const(event_enum_name, name)) 90 91 if arg_type and arg_type.members: 92 ret += mcgen(''' 93out: 94 qmp_output_visitor_cleanup(qov); 95''') 96 ret += mcgen(''' 97 error_propagate(errp, err); 98 QDECREF(qmp); 99} 100''') 101 return ret 102 103 104class QAPISchemaGenEventVisitor(QAPISchemaVisitor): 105 def __init__(self): 106 self.decl = None 107 self.defn = None 108 self._event_names = None 109 110 def visit_begin(self, schema): 111 self.decl = '' 112 self.defn = '' 113 self._event_names = [] 114 115 def visit_end(self): 116 self.decl += gen_enum(event_enum_name, self._event_names) 117 self.defn += gen_enum_lookup(event_enum_name, self._event_names) 118 self._event_names = None 119 120 def visit_event(self, name, info, arg_type): 121 self.decl += gen_event_send_decl(name, arg_type) 122 self.defn += gen_event_send(name, arg_type) 123 self._event_names.append(name) 124 125 126(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() 127 128c_comment = ''' 129/* 130 * schema-defined QAPI event functions 131 * 132 * Copyright (c) 2014 Wenchao Xia 133 * 134 * Authors: 135 * Wenchao Xia <wenchaoqemu@gmail.com> 136 * 137 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 138 * See the COPYING.LIB file in the top-level directory. 139 * 140 */ 141''' 142h_comment = ''' 143/* 144 * schema-defined QAPI event functions 145 * 146 * Copyright (c) 2014 Wenchao Xia 147 * 148 * Authors: 149 * Wenchao Xia <wenchaoqemu@gmail.com> 150 * 151 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 152 * See the COPYING.LIB file in the top-level directory. 153 * 154 */ 155''' 156 157(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, 158 'qapi-event.c', 'qapi-event.h', 159 c_comment, h_comment) 160 161fdef.write(mcgen(''' 162#include "qemu-common.h" 163#include "%(prefix)sqapi-event.h" 164#include "%(prefix)sqapi-visit.h" 165#include "qapi/qmp-output-visitor.h" 166#include "qapi/qmp-event.h" 167 168''', 169 prefix=prefix)) 170 171fdecl.write(mcgen(''' 172#include "qapi/error.h" 173#include "qapi/qmp/qdict.h" 174#include "%(prefix)sqapi-types.h" 175 176''', 177 prefix=prefix)) 178 179event_enum_name = c_name(prefix + "QAPIEvent", protect=False) 180 181schema = QAPISchema(input_file) 182gen = QAPISchemaGenEventVisitor() 183schema.visit(gen) 184fdef.write(gen.defn) 185fdecl.write(gen.decl) 186 187close_output(fdef, fdecl) 188