testdev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/osdep.h"
  27. #include "qemu/module.h"
  28. #include "chardev/char.h"
  29. #include "qom/object.h"
  30. #define BUF_SIZE 32
  31. struct TestdevChardev {
  32. Chardev parent;
  33. uint8_t in_buf[32];
  34. int in_buf_used;
  35. };
  36. typedef struct TestdevChardev TestdevChardev;
  37. #define TYPE_CHARDEV_TESTDEV "chardev-testdev"
  38. DECLARE_INSTANCE_CHECKER(TestdevChardev, TESTDEV_CHARDEV,
  39. TYPE_CHARDEV_TESTDEV)
  40. /* Try to interpret a whole incoming packet */
  41. static int testdev_eat_packet(TestdevChardev *testdev)
  42. {
  43. const uint8_t *cur = testdev->in_buf;
  44. int len = testdev->in_buf_used;
  45. uint8_t c;
  46. int arg;
  47. #define EAT(c) do { \
  48. if (!len--) { \
  49. return 0; \
  50. } \
  51. c = *cur++; \
  52. } while (0)
  53. EAT(c);
  54. while (isspace(c)) {
  55. EAT(c);
  56. }
  57. arg = 0;
  58. while (isdigit(c)) {
  59. arg = arg * 10 + c - '0';
  60. EAT(c);
  61. }
  62. while (isspace(c)) {
  63. EAT(c);
  64. }
  65. switch (c) {
  66. case 'q':
  67. exit((arg << 1) | 1);
  68. break;
  69. default:
  70. break;
  71. }
  72. return cur - testdev->in_buf;
  73. }
  74. /* The other end is writing some data. Store it and try to interpret */
  75. static int testdev_chr_write(Chardev *chr, const uint8_t *buf, int len)
  76. {
  77. TestdevChardev *testdev = TESTDEV_CHARDEV(chr);
  78. int tocopy, eaten, orig_len = len;
  79. while (len) {
  80. /* Complete our buffer as much as possible */
  81. tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
  82. memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
  83. testdev->in_buf_used += tocopy;
  84. buf += tocopy;
  85. len -= tocopy;
  86. /* Interpret it as much as possible */
  87. while (testdev->in_buf_used > 0 &&
  88. (eaten = testdev_eat_packet(testdev)) > 0) {
  89. memmove(testdev->in_buf, testdev->in_buf + eaten,
  90. testdev->in_buf_used - eaten);
  91. testdev->in_buf_used -= eaten;
  92. }
  93. }
  94. return orig_len;
  95. }
  96. static void char_testdev_class_init(ObjectClass *oc, void *data)
  97. {
  98. ChardevClass *cc = CHARDEV_CLASS(oc);
  99. cc->chr_write = testdev_chr_write;
  100. }
  101. static const TypeInfo char_testdev_type_info = {
  102. .name = TYPE_CHARDEV_TESTDEV,
  103. .parent = TYPE_CHARDEV,
  104. .instance_size = sizeof(TestdevChardev),
  105. .class_init = char_testdev_class_init,
  106. };
  107. static void register_types(void)
  108. {
  109. type_register_static(&char_testdev_type_info);
  110. }
  111. type_init(register_types);