2
0

qmp-event.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include "qemu/osdep.h"
  14. #include "qemu-common.h"
  15. #include "qapi/qmp-event.h"
  16. #include "qapi/qmp/qstring.h"
  17. #include "qapi/qmp/qjson.h"
  18. static QMPEventFuncEmit qmp_emit;
  19. void qmp_event_set_func_emit(QMPEventFuncEmit emit)
  20. {
  21. qmp_emit = emit;
  22. }
  23. QMPEventFuncEmit qmp_event_get_func_emit(void)
  24. {
  25. return qmp_emit;
  26. }
  27. static void timestamp_put(QDict *qdict)
  28. {
  29. int err;
  30. QObject *obj;
  31. qemu_timeval tv;
  32. err = qemu_gettimeofday(&tv);
  33. /* Put -1 to indicate failure of getting host time */
  34. obj = qobject_from_jsonf("{ 'seconds': %lld, 'microseconds': %lld }",
  35. err < 0 ? -1LL : (long long)tv.tv_sec,
  36. err < 0 ? -1LL : (long long)tv.tv_usec);
  37. qdict_put_obj(qdict, "timestamp", obj);
  38. }
  39. /*
  40. * Build a QDict, then fill event name and time stamp, caller should free the
  41. * QDict after usage.
  42. */
  43. QDict *qmp_event_build_dict(const char *event_name)
  44. {
  45. QDict *dict = qdict_new();
  46. qdict_put_str(dict, "event", event_name);
  47. timestamp_put(dict);
  48. return dict;
  49. }