tester-a2dp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdbool.h>
  8. #include "emulator/bthost.h"
  9. #include "src/shared/util.h"
  10. #include "src/shared/tester.h"
  11. #include "src/shared/queue.h"
  12. #include "lib/bluetooth.h"
  13. #include "android/utils.h"
  14. #include "tester-main.h"
  15. static struct queue *list;
  16. #define req_dsc 0x00, 0x01
  17. #define rsp_dsc 0x02, 0x01, 0x04, 0x08
  18. #define req_get 0x10, 0x02, 0x04
  19. #define rsp_get 0x12, 0x02, 0x01, 0x00, 0x07, 0x06, 0x00, \
  20. 0x00, 0xff, 0xff, 0x02, 0x40
  21. #define req_cfg 0x20, 0x03, 0x04, 0x04, 0x01, 0x00, 0x07, \
  22. 0x06, 0x00, 0x00, 0x21, 0x15, 0x02, \
  23. 0x40
  24. #define rsp_cfg 0x22, 0x03
  25. #define req_open 0x30, 0x06, 0x04
  26. #define rsp_open 0x32, 0x06
  27. #define req_close 0x40, 0x08, 0x04
  28. #define rsp_close 0x42, 0x08
  29. #define req_start 0x40, 0x07, 0x04
  30. #define rsp_start 0x42, 0x07
  31. #define req_suspend 0x50, 0x09, 0x04
  32. #define rsp_suspend 0x52, 0x09
  33. static const struct pdu_set pdus[] = {
  34. { raw_pdu(req_dsc), raw_pdu(rsp_dsc) },
  35. { raw_pdu(req_get), raw_pdu(rsp_get) },
  36. { raw_pdu(req_cfg), raw_pdu(rsp_cfg) },
  37. { raw_pdu(req_open), raw_pdu(rsp_open) },
  38. { raw_pdu(req_close), raw_pdu(rsp_close) },
  39. { raw_pdu(req_start), raw_pdu(rsp_start) },
  40. { raw_pdu(req_suspend), raw_pdu(rsp_suspend) },
  41. { end_pdu, end_pdu },
  42. };
  43. static struct emu_l2cap_cid_data cid_data = {
  44. .pdu = pdus,
  45. };
  46. static void a2dp_connect_request_cb(uint16_t handle, uint16_t cid,
  47. void *user_data)
  48. {
  49. struct emu_l2cap_cid_data *cid_data = user_data;
  50. if (cid_data->handle)
  51. return;
  52. cid_data->handle = handle;
  53. cid_data->cid = cid;
  54. tester_handle_l2cap_data_exchange(cid_data);
  55. }
  56. static struct emu_set_l2cap_data l2cap_setup_data = {
  57. .psm = 25,
  58. .func = a2dp_connect_request_cb,
  59. .user_data = &cid_data,
  60. };
  61. static void a2dp_connect_action(void)
  62. {
  63. struct test_data *data = tester_get_data();
  64. const uint8_t *addr = hciemu_get_client_bdaddr(data->hciemu);
  65. struct step *step = g_new0(struct step, 1);
  66. bt_bdaddr_t bdaddr;
  67. cid_data.handle = 0;
  68. cid_data.cid = 0;
  69. bdaddr2android((const bdaddr_t *) addr, &bdaddr);
  70. step->action_status = data->if_a2dp->connect(&bdaddr);
  71. schedule_action_verification(step);
  72. }
  73. static void a2dp_disconnect_action(void)
  74. {
  75. struct test_data *data = tester_get_data();
  76. const uint8_t *addr = hciemu_get_client_bdaddr(data->hciemu);
  77. struct step *step = g_new0(struct step, 1);
  78. bt_bdaddr_t bdaddr;
  79. bdaddr2android((const bdaddr_t *) addr, &bdaddr);
  80. step->action_status = data->if_a2dp->disconnect(&bdaddr);
  81. schedule_action_verification(step);
  82. }
  83. static void audio_resume_action(void)
  84. {
  85. struct test_data *data = tester_get_data();
  86. struct step *step = g_new0(struct step, 1);
  87. int err;
  88. err = data->audio->open_output_stream(data->audio,
  89. 0,
  90. AUDIO_DEVICE_OUT_ALL_A2DP,
  91. AUDIO_OUTPUT_FLAG_NONE,
  92. NULL,
  93. &data->if_stream, NULL);
  94. if (err < 0) {
  95. step->action_status = BT_STATUS_FAIL;
  96. goto done;
  97. }
  98. /* Write something to force resume */
  99. data->if_stream->write(data->if_stream, &err, sizeof(err));
  100. done:
  101. schedule_action_verification(step);
  102. }
  103. static void audio_suspend_action(void)
  104. {
  105. struct test_data *data = tester_get_data();
  106. struct step *step = g_new0(struct step, 1);
  107. data->if_stream->common.standby(&data->if_stream->common);
  108. schedule_action_verification(step);
  109. }
  110. static struct test_case test_cases[] = {
  111. TEST_CASE_BREDRLE("A2DP Init",
  112. ACTION_SUCCESS(dummy_action, NULL),
  113. ),
  114. TEST_CASE_BREDRLE("A2DP Connect - Success",
  115. ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
  116. ACTION_SUCCESS(bluetooth_enable_action, NULL),
  117. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
  118. ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
  119. ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
  120. ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
  121. ACTION_SUCCESS(a2dp_connect_action, NULL),
  122. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  123. BTAV_CONNECTION_STATE_CONNECTING),
  124. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  125. BTAV_CONNECTION_STATE_CONNECTED),
  126. ACTION_SUCCESS(bluetooth_disable_action, NULL),
  127. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  128. BTAV_CONNECTION_STATE_DISCONNECTED),
  129. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
  130. ),
  131. TEST_CASE_BREDRLE("A2DP Disconnect - Success",
  132. ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
  133. ACTION_SUCCESS(bluetooth_enable_action, NULL),
  134. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
  135. ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
  136. ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
  137. ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
  138. ACTION_SUCCESS(a2dp_connect_action, NULL),
  139. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  140. BTAV_CONNECTION_STATE_CONNECTING),
  141. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  142. BTAV_CONNECTION_STATE_CONNECTED),
  143. ACTION_SUCCESS(a2dp_disconnect_action, NULL),
  144. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  145. BTAV_CONNECTION_STATE_DISCONNECTING),
  146. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  147. BTAV_CONNECTION_STATE_DISCONNECTED),
  148. ACTION_SUCCESS(bluetooth_disable_action, NULL),
  149. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
  150. ),
  151. TEST_CASE_BREDRLE("A2DP Resume - Success",
  152. ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
  153. ACTION_SUCCESS(bluetooth_enable_action, NULL),
  154. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
  155. ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
  156. ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
  157. ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
  158. ACTION_SUCCESS(a2dp_connect_action, NULL),
  159. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  160. BTAV_CONNECTION_STATE_CONNECTING),
  161. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  162. BTAV_CONNECTION_STATE_CONNECTED),
  163. ACTION_SUCCESS(audio_resume_action, NULL),
  164. CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
  165. BTAV_AUDIO_STATE_STARTED),
  166. ACTION_SUCCESS(bluetooth_disable_action, NULL),
  167. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  168. BTAV_CONNECTION_STATE_DISCONNECTED),
  169. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
  170. ),
  171. TEST_CASE_BREDRLE("A2DP Suspend - Success",
  172. ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
  173. ACTION_SUCCESS(bluetooth_enable_action, NULL),
  174. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
  175. ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
  176. ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
  177. ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
  178. ACTION_SUCCESS(a2dp_connect_action, NULL),
  179. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  180. BTAV_CONNECTION_STATE_CONNECTING),
  181. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  182. BTAV_CONNECTION_STATE_CONNECTED),
  183. ACTION_SUCCESS(audio_resume_action, NULL),
  184. CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
  185. BTAV_AUDIO_STATE_STARTED),
  186. ACTION_SUCCESS(audio_suspend_action, NULL),
  187. CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
  188. BTAV_AUDIO_STATE_STOPPED),
  189. ACTION_SUCCESS(bluetooth_disable_action, NULL),
  190. CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
  191. BTAV_CONNECTION_STATE_DISCONNECTED),
  192. CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
  193. ),
  194. };
  195. struct queue *get_a2dp_tests(void)
  196. {
  197. uint16_t i = 0;
  198. list = queue_new();
  199. for (; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i)
  200. queue_push_tail(list, &test_cases[i]);
  201. return list;
  202. }
  203. void remove_a2dp_tests(void)
  204. {
  205. queue_destroy(list, NULL);
  206. }