xref: /openbmc/qemu/qapi/qmp-event.c (revision 10358b6a)
1 /*
2  * QMP Event related
3  *
4  * Copyright (c) 2014 Wenchao Xia
5  *
6  * Authors:
7  *  Wenchao Xia   <wenchaoqemu@gmail.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  *
12  */
13 
14 #include <inttypes.h>
15 
16 #include "qemu-common.h"
17 #include "qapi/qmp-event.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qmp/qjson.h"
20 
21 #ifdef _WIN32
22 #include "sysemu/os-win32.h"
23 #endif
24 
25 #ifdef CONFIG_POSIX
26 #include "sysemu/os-posix.h"
27 #endif
28 
29 static QMPEventFuncEmit qmp_emit;
30 
31 void qmp_event_set_func_emit(QMPEventFuncEmit emit)
32 {
33     qmp_emit = emit;
34 }
35 
36 QMPEventFuncEmit qmp_event_get_func_emit(void)
37 {
38     return qmp_emit;
39 }
40 
41 static void timestamp_put(QDict *qdict)
42 {
43     int err;
44     QObject *obj;
45     qemu_timeval tv;
46     int64_t sec, usec;
47 
48     err = qemu_gettimeofday(&tv);
49     if (err < 0) {
50         /* Put -1 to indicate failure of getting host time */
51         sec = -1;
52         usec = -1;
53     } else {
54         sec = tv.tv_sec;
55         usec = tv.tv_usec;
56     }
57 
58     obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
59                              "'microseconds': %" PRId64 " }",
60                              sec, usec);
61     qdict_put_obj(qdict, "timestamp", obj);
62 }
63 
64 /*
65  * Build a QDict, then fill event name and time stamp, caller should free the
66  * QDict after usage.
67  */
68 QDict *qmp_event_build_dict(const char *event_name)
69 {
70     QDict *dict = qdict_new();
71     qdict_put(dict, "event", qstring_from_str(event_name));
72     timestamp_put(dict);
73     return dict;
74 }
75