test-uhid.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <inttypes.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <sys/socket.h>
  20. #include <glib.h>
  21. #include "src/shared/uhid.h"
  22. #include "src/shared/util.h"
  23. #include "src/shared/tester.h"
  24. struct test_pdu {
  25. bool valid;
  26. const uint8_t *data;
  27. size_t size;
  28. };
  29. struct test_data {
  30. char *test_name;
  31. struct test_pdu *pdu_list;
  32. };
  33. struct context {
  34. struct bt_uhid *uhid;
  35. guint source;
  36. guint process;
  37. int fd;
  38. unsigned int pdu_offset;
  39. const struct test_data *data;
  40. };
  41. #define event(args...) \
  42. { \
  43. .valid = true, \
  44. .data = (void *) args, \
  45. .size = sizeof(*args), \
  46. }
  47. #define define_test(name, function, args...) \
  48. do { \
  49. const struct test_pdu pdus[] = { \
  50. args, { } \
  51. }; \
  52. static struct test_data data; \
  53. data.test_name = g_strdup(name); \
  54. data.pdu_list = g_memdup(pdus, sizeof(pdus)); \
  55. tester_add(name, &data, NULL, function, NULL); \
  56. } while (0)
  57. static void test_debug(const char *str, void *user_data)
  58. {
  59. const char *prefix = user_data;
  60. tester_debug("%s%s\n", prefix, str);
  61. }
  62. static void test_free(gconstpointer user_data)
  63. {
  64. const struct test_data *data = user_data;
  65. g_free(data->test_name);
  66. g_free(data->pdu_list);
  67. }
  68. static void destroy_context(struct context *context)
  69. {
  70. if (context->source > 0)
  71. g_source_remove(context->source);
  72. bt_uhid_unref(context->uhid);
  73. test_free(context->data);
  74. g_free(context);
  75. }
  76. static gboolean context_quit(gpointer user_data)
  77. {
  78. struct context *context = user_data;
  79. if (context == NULL)
  80. return FALSE;
  81. if (context->process > 0)
  82. g_source_remove(context->process);
  83. destroy_context(context);
  84. tester_test_passed();
  85. return FALSE;
  86. }
  87. static gboolean send_pdu(gpointer user_data)
  88. {
  89. struct context *context = user_data;
  90. const struct test_pdu *pdu;
  91. ssize_t len;
  92. pdu = &context->data->pdu_list[context->pdu_offset++];
  93. len = write(context->fd, pdu->data, pdu->size);
  94. util_hexdump('<', pdu->data, len, test_debug, "uHID: ");
  95. g_assert_cmpint(len, ==, pdu->size);
  96. context->process = 0;
  97. return FALSE;
  98. }
  99. static void context_process(struct context *context)
  100. {
  101. if (!context->data->pdu_list[context->pdu_offset].valid) {
  102. context_quit(context);
  103. return;
  104. }
  105. context->process = g_idle_add(send_pdu, context);
  106. }
  107. static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
  108. gpointer user_data)
  109. {
  110. struct context *context = user_data;
  111. const struct test_pdu *pdu;
  112. unsigned char buf[sizeof(struct uhid_event)];
  113. ssize_t len;
  114. int fd;
  115. pdu = &context->data->pdu_list[context->pdu_offset++];
  116. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  117. context->source = 0;
  118. g_print("%s: cond %x\n", __func__, cond);
  119. return FALSE;
  120. }
  121. fd = g_io_channel_unix_get_fd(channel);
  122. len = read(fd, buf, sizeof(buf));
  123. g_assert(len > 0);
  124. util_hexdump('>', buf, len, test_debug, "uHID: ");
  125. g_assert_cmpint(len, ==, pdu->size);
  126. g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
  127. context_process(context);
  128. return TRUE;
  129. }
  130. static struct context *create_context(gconstpointer data)
  131. {
  132. struct context *context = g_new0(struct context, 1);
  133. GIOChannel *channel;
  134. int err, sv[2];
  135. err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
  136. g_assert(err == 0);
  137. context->uhid = bt_uhid_new(sv[0]);
  138. g_assert(context->uhid != NULL);
  139. channel = g_io_channel_unix_new(sv[1]);
  140. g_io_channel_set_close_on_unref(channel, TRUE);
  141. g_io_channel_set_encoding(channel, NULL, NULL);
  142. g_io_channel_set_buffered(channel, FALSE);
  143. context->source = g_io_add_watch(channel,
  144. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  145. test_handler, context);
  146. g_assert(context->source > 0);
  147. g_io_channel_unref(channel);
  148. context->fd = sv[1];
  149. context->data = data;
  150. return context;
  151. }
  152. static const struct uhid_event ev_create = {
  153. .type = UHID_CREATE,
  154. };
  155. static const struct uhid_event ev_destroy = {
  156. .type = UHID_DESTROY,
  157. };
  158. static const struct uhid_event ev_feature_answer = {
  159. .type = UHID_FEATURE_ANSWER,
  160. };
  161. static const struct uhid_event ev_input = {
  162. .type = UHID_INPUT,
  163. };
  164. static const struct uhid_event ev_output = {
  165. .type = UHID_OUTPUT,
  166. };
  167. static const struct uhid_event ev_feature = {
  168. .type = UHID_FEATURE,
  169. };
  170. static void test_client(gconstpointer data)
  171. {
  172. struct context *context = create_context(data);
  173. if (g_str_equal(context->data->test_name, "/uhid/command/create"))
  174. bt_uhid_send(context->uhid, &ev_create);
  175. if (g_str_equal(context->data->test_name, "/uhid/command/destroy"))
  176. bt_uhid_send(context->uhid, &ev_destroy);
  177. if (g_str_equal(context->data->test_name,
  178. "/uhid/command/feature_answer"))
  179. bt_uhid_send(context->uhid, &ev_feature_answer);
  180. if (g_str_equal(context->data->test_name, "/uhid/command/input"))
  181. bt_uhid_send(context->uhid, &ev_input);
  182. context_quit(context);
  183. }
  184. static void handle_output(struct uhid_event *ev, void *user_data)
  185. {
  186. g_assert_cmpint(ev->type, ==, UHID_OUTPUT);
  187. context_quit(user_data);
  188. }
  189. static void handle_feature(struct uhid_event *ev, void *user_data)
  190. {
  191. g_assert_cmpint(ev->type, ==, UHID_FEATURE);
  192. context_quit(user_data);
  193. }
  194. static void test_server(gconstpointer data)
  195. {
  196. struct context *context = create_context(data);
  197. bt_uhid_register(context->uhid, UHID_OUTPUT, handle_output, context);
  198. bt_uhid_register(context->uhid, UHID_FEATURE, handle_feature, context);
  199. g_idle_add(send_pdu, context);
  200. }
  201. int main(int argc, char *argv[])
  202. {
  203. tester_init(&argc, &argv);
  204. define_test("/uhid/command/create", test_client, event(&ev_create));
  205. define_test("/uhid/command/destroy", test_client, event(&ev_destroy));
  206. define_test("/uhid/command/feature_answer", test_client,
  207. event(&ev_feature_answer));
  208. define_test("/uhid/command/input", test_client, event(&ev_input));
  209. define_test("/uhid/event/output", test_server, event(&ev_output));
  210. define_test("/uhid/event/feature", test_server, event(&ev_feature));
  211. return tester_run();
  212. }