2 * Copyright (C) 2014 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "emulator/bthost.h"
21 #include "src/shared/util.h"
22 #include "src/shared/tester.h"
23 #include "src/shared/queue.h"
24 #include "lib/bluetooth.h"
25 #include "android/utils.h"
26 #include "tester-main.h"
28 static struct queue *list;
30 #define req_dsc 0x00, 0x01
31 #define rsp_dsc 0x02, 0x01, 0x04, 0x08
32 #define req_get 0x10, 0x02, 0x04
33 #define rsp_get 0x12, 0x02, 0x01, 0x00, 0x07, 0x06, 0x00, \
34 0x00, 0xff, 0xff, 0x02, 0x40
35 #define req_cfg 0x20, 0x03, 0x04, 0x04, 0x01, 0x00, 0x07, \
36 0x06, 0x00, 0x00, 0x21, 0x15, 0x02, \
38 #define rsp_cfg 0x22, 0x03
39 #define req_open 0x30, 0x06, 0x04
40 #define rsp_open 0x32, 0x06
41 #define req_close 0x40, 0x08, 0x04
42 #define rsp_close 0x42, 0x08
43 #define req_start 0x40, 0x07, 0x04
44 #define rsp_start 0x42, 0x07
45 #define req_suspend 0x50, 0x09, 0x04
46 #define rsp_suspend 0x52, 0x09
48 static const struct pdu_set pdus[] = {
49 { raw_pdu(req_dsc), raw_pdu(rsp_dsc) },
50 { raw_pdu(req_get), raw_pdu(rsp_get) },
51 { raw_pdu(req_cfg), raw_pdu(rsp_cfg) },
52 { raw_pdu(req_open), raw_pdu(rsp_open) },
53 { raw_pdu(req_close), raw_pdu(rsp_close) },
54 { raw_pdu(req_start), raw_pdu(rsp_start) },
55 { raw_pdu(req_suspend), raw_pdu(rsp_suspend) },
59 static struct emu_l2cap_cid_data cid_data = {
63 static void a2dp_connect_request_cb(uint16_t handle, uint16_t cid,
66 struct emu_l2cap_cid_data *cid_data = user_data;
71 cid_data->handle = handle;
74 tester_handle_l2cap_data_exchange(cid_data);
77 static struct emu_set_l2cap_data l2cap_setup_data = {
79 .func = a2dp_connect_request_cb,
80 .user_data = &cid_data,
83 static void a2dp_connect_action(void)
85 struct test_data *data = tester_get_data();
86 const uint8_t *addr = hciemu_get_client_bdaddr(data->hciemu);
87 struct step *step = g_new0(struct step, 1);
93 bdaddr2android((const bdaddr_t *) addr, &bdaddr);
95 step->action_status = data->if_a2dp->connect(&bdaddr);
97 schedule_action_verification(step);
100 static void a2dp_disconnect_action(void)
102 struct test_data *data = tester_get_data();
103 const uint8_t *addr = hciemu_get_client_bdaddr(data->hciemu);
104 struct step *step = g_new0(struct step, 1);
107 bdaddr2android((const bdaddr_t *) addr, &bdaddr);
109 step->action_status = data->if_a2dp->disconnect(&bdaddr);
111 schedule_action_verification(step);
114 static void audio_resume_action(void)
116 struct test_data *data = tester_get_data();
117 struct step *step = g_new0(struct step, 1);
120 err = data->audio->open_output_stream(data->audio,
122 AUDIO_DEVICE_OUT_ALL_A2DP,
123 AUDIO_OUTPUT_FLAG_NONE,
125 &data->if_stream, NULL);
127 step->action_status = BT_STATUS_FAIL;
131 /* Write something to force resume */
132 data->if_stream->write(data->if_stream, &err, sizeof(err));
135 schedule_action_verification(step);
138 static void audio_suspend_action(void)
140 struct test_data *data = tester_get_data();
141 struct step *step = g_new0(struct step, 1);
143 data->if_stream->common.standby(&data->if_stream->common);
145 schedule_action_verification(step);
148 static struct test_case test_cases[] = {
149 TEST_CASE_BREDRLE("A2DP Init",
150 ACTION_SUCCESS(dummy_action, NULL),
152 TEST_CASE_BREDRLE("A2DP Connect - Success",
153 ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
154 ACTION_SUCCESS(bluetooth_enable_action, NULL),
155 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
156 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
157 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
158 ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
159 ACTION_SUCCESS(a2dp_connect_action, NULL),
160 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
161 BTAV_CONNECTION_STATE_CONNECTING),
162 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
163 BTAV_CONNECTION_STATE_CONNECTED),
164 ACTION_SUCCESS(bluetooth_disable_action, NULL),
165 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
166 BTAV_CONNECTION_STATE_DISCONNECTED),
167 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
169 TEST_CASE_BREDRLE("A2DP Disconnect - Success",
170 ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
171 ACTION_SUCCESS(bluetooth_enable_action, NULL),
172 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
173 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
174 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
175 ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
176 ACTION_SUCCESS(a2dp_connect_action, NULL),
177 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
178 BTAV_CONNECTION_STATE_CONNECTING),
179 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
180 BTAV_CONNECTION_STATE_CONNECTED),
181 ACTION_SUCCESS(a2dp_disconnect_action, NULL),
182 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
183 BTAV_CONNECTION_STATE_DISCONNECTING),
184 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
185 BTAV_CONNECTION_STATE_DISCONNECTED),
186 ACTION_SUCCESS(bluetooth_disable_action, NULL),
187 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
189 TEST_CASE_BREDRLE("A2DP Resume - Success",
190 ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
191 ACTION_SUCCESS(bluetooth_enable_action, NULL),
192 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
193 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
194 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
195 ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
196 ACTION_SUCCESS(a2dp_connect_action, NULL),
197 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
198 BTAV_CONNECTION_STATE_CONNECTING),
199 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
200 BTAV_CONNECTION_STATE_CONNECTED),
201 ACTION_SUCCESS(audio_resume_action, NULL),
202 CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
203 BTAV_AUDIO_STATE_STARTED),
204 ACTION_SUCCESS(bluetooth_disable_action, NULL),
205 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
206 BTAV_CONNECTION_STATE_DISCONNECTED),
207 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
209 TEST_CASE_BREDRLE("A2DP Suspend - Success",
210 ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
211 ACTION_SUCCESS(bluetooth_enable_action, NULL),
212 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
213 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
214 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
215 ACTION_SUCCESS(emu_add_l2cap_server_action, &l2cap_setup_data),
216 ACTION_SUCCESS(a2dp_connect_action, NULL),
217 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
218 BTAV_CONNECTION_STATE_CONNECTING),
219 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
220 BTAV_CONNECTION_STATE_CONNECTED),
221 ACTION_SUCCESS(audio_resume_action, NULL),
222 CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
223 BTAV_AUDIO_STATE_STARTED),
224 ACTION_SUCCESS(audio_suspend_action, NULL),
225 CALLBACK_AV_AUDIO_STATE(CB_A2DP_AUDIO_STATE,
226 BTAV_AUDIO_STATE_STOPPED),
227 ACTION_SUCCESS(bluetooth_disable_action, NULL),
228 CALLBACK_AV_CONN_STATE(CB_A2DP_CONN_STATE,
229 BTAV_CONNECTION_STATE_DISCONNECTED),
230 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
234 struct queue *get_a2dp_tests(void)
240 for (; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i)
241 queue_push_tail(list, &test_cases[i]);
246 void remove_a2dp_tests(void)
248 queue_destroy(list, NULL);