testdev.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * QEMU Char Device for testsuite control
  3. *
  4. * Copyright (c) 2014 Red Hat, Inc.
  5. *
  6. * Author: Paolo Bonzini <pbonzini@redhat.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu-common.h"
  27. #include "sysemu/char.h"
  28. #define BUF_SIZE 32
  29. typedef struct {
  30. CharDriverState *chr;
  31. uint8_t in_buf[32];
  32. int in_buf_used;
  33. } TestdevCharState;
  34. /* Try to interpret a whole incoming packet */
  35. static int testdev_eat_packet(TestdevCharState *testdev)
  36. {
  37. const uint8_t *cur = testdev->in_buf;
  38. int len = testdev->in_buf_used;
  39. uint8_t c;
  40. int arg;
  41. #define EAT(c) do { \
  42. if (!len--) { \
  43. return 0; \
  44. } \
  45. c = *cur++; \
  46. } while (0)
  47. EAT(c);
  48. while (isspace(c)) {
  49. EAT(c);
  50. }
  51. arg = 0;
  52. while (isdigit(c)) {
  53. arg = arg * 10 + c - '0';
  54. EAT(c);
  55. }
  56. while (isspace(c)) {
  57. EAT(c);
  58. }
  59. switch (c) {
  60. case 'q':
  61. exit((arg << 1) | 1);
  62. break;
  63. default:
  64. break;
  65. }
  66. return cur - testdev->in_buf;
  67. }
  68. /* The other end is writing some data. Store it and try to interpret */
  69. static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
  70. {
  71. TestdevCharState *testdev = chr->opaque;
  72. int tocopy, eaten, orig_len = len;
  73. while (len) {
  74. /* Complete our buffer as much as possible */
  75. tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
  76. memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
  77. testdev->in_buf_used += tocopy;
  78. buf += tocopy;
  79. len -= tocopy;
  80. /* Interpret it as much as possible */
  81. while (testdev->in_buf_used > 0 &&
  82. (eaten = testdev_eat_packet(testdev)) > 0) {
  83. memmove(testdev->in_buf, testdev->in_buf + eaten,
  84. testdev->in_buf_used - eaten);
  85. testdev->in_buf_used -= eaten;
  86. }
  87. }
  88. return orig_len;
  89. }
  90. static void testdev_close(struct CharDriverState *chr)
  91. {
  92. TestdevCharState *testdev = chr->opaque;
  93. g_free(testdev);
  94. }
  95. CharDriverState *chr_testdev_init(void)
  96. {
  97. TestdevCharState *testdev;
  98. CharDriverState *chr;
  99. testdev = g_malloc0(sizeof(TestdevCharState));
  100. testdev->chr = chr = g_malloc0(sizeof(CharDriverState));
  101. chr->opaque = testdev;
  102. chr->chr_write = testdev_write;
  103. chr->chr_close = testdev_close;
  104. return chr;
  105. }
  106. static void register_types(void)
  107. {
  108. register_char_driver("testdev", CHARDEV_BACKEND_KIND_TESTDEV, NULL);
  109. }
  110. type_init(register_types);