qmp-event.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <inttypes.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. #ifdef _WIN32
  19. #include "sysemu/os-win32.h"
  20. #endif
  21. #ifdef CONFIG_POSIX
  22. #include "sysemu/os-posix.h"
  23. #endif
  24. static QMPEventFuncEmit qmp_emit;
  25. void qmp_event_set_func_emit(QMPEventFuncEmit emit)
  26. {
  27. qmp_emit = emit;
  28. }
  29. QMPEventFuncEmit qmp_event_get_func_emit(void)
  30. {
  31. return qmp_emit;
  32. }
  33. static void timestamp_put(QDict *qdict)
  34. {
  35. int err;
  36. QObject *obj;
  37. qemu_timeval tv;
  38. int64_t sec, usec;
  39. err = qemu_gettimeofday(&tv);
  40. if (err < 0) {
  41. /* Put -1 to indicate failure of getting host time */
  42. sec = -1;
  43. usec = -1;
  44. } else {
  45. sec = tv.tv_sec;
  46. usec = tv.tv_usec;
  47. }
  48. obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
  49. "'microseconds': %" PRId64 " }",
  50. sec, usec);
  51. qdict_put_obj(qdict, "timestamp", obj);
  52. }
  53. /*
  54. * Build a QDict, then fill event name and time stamp, caller should free the
  55. * QDict after usage.
  56. */
  57. QDict *qmp_event_build_dict(const char *event_name)
  58. {
  59. QDict *dict = qdict_new();
  60. qdict_put(dict, "event", qstring_from_str(event_name));
  61. timestamp_put(dict);
  62. return dict;
  63. }