Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 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 | |
Peter Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 15 | |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 16 | #include "qapi/qmp-event.h" |
| 17 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qdict.h" |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 19 | #include "qapi/qmp/qjson.h" |
| 20 | |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 21 | static void timestamp_put(QDict *qdict) |
| 22 | { |
| 23 | int err; |
Markus Armbruster | cd499d2 | 2018-07-03 10:53:54 +0200 | [diff] [blame] | 24 | QDict *ts; |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 25 | qemu_timeval tv; |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 26 | |
| 27 | err = qemu_gettimeofday(&tv); |
Eric Blake | 043b5a4 | 2016-11-23 11:36:54 -0600 | [diff] [blame] | 28 | /* Put -1 to indicate failure of getting host time */ |
Markus Armbruster | cd499d2 | 2018-07-03 10:53:54 +0200 | [diff] [blame] | 29 | ts = qdict_from_jsonf_nofail("{ 'seconds': %lld, 'microseconds': %lld }", |
| 30 | err < 0 ? -1LL : (long long)tv.tv_sec, |
| 31 | err < 0 ? -1LL : (long long)tv.tv_usec); |
| 32 | qdict_put(qdict, "timestamp", ts); |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Build a QDict, then fill event name and time stamp, caller should free the |
| 37 | * QDict after usage. |
| 38 | */ |
| 39 | QDict *qmp_event_build_dict(const char *event_name) |
| 40 | { |
| 41 | QDict *dict = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 42 | qdict_put_str(dict, "event", event_name); |
Wenchao Xia | f882126 | 2014-06-18 08:43:27 +0200 | [diff] [blame] | 43 | timestamp_put(dict); |
| 44 | return dict; |
| 45 | } |