tizen 2.3.1 release
[framework/connectivity/bluez.git] / android / tester-a2dp.c
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdbool.h>
19
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"
27
28 static struct queue *list;
29
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, \
37                                         0x40
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
47
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) },
56         { end_pdu, end_pdu },
57 };
58
59 static struct emu_l2cap_cid_data cid_data = {
60         .pdu = pdus,
61 };
62
63 static void a2dp_connect_request_cb(uint16_t handle, uint16_t cid,
64                                                         void *user_data)
65 {
66         struct emu_l2cap_cid_data *cid_data = user_data;
67
68         if (cid_data->handle)
69                 return;
70
71         cid_data->handle = handle;
72         cid_data->cid = cid;
73
74         tester_handle_l2cap_data_exchange(cid_data);
75 }
76
77 static struct emu_set_l2cap_data l2cap_setup_data = {
78         .psm = 25,
79         .func = a2dp_connect_request_cb,
80         .user_data = &cid_data,
81 };
82
83 static void a2dp_connect_action(void)
84 {
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);
88         bt_bdaddr_t bdaddr;
89
90         cid_data.handle = 0;
91         cid_data.cid = 0;
92
93         bdaddr2android((const bdaddr_t *) addr, &bdaddr);
94
95         step->action_status = data->if_a2dp->connect(&bdaddr);
96
97         schedule_action_verification(step);
98 }
99
100 static void a2dp_disconnect_action(void)
101 {
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);
105         bt_bdaddr_t bdaddr;
106
107         bdaddr2android((const bdaddr_t *) addr, &bdaddr);
108
109         step->action_status = data->if_a2dp->disconnect(&bdaddr);
110
111         schedule_action_verification(step);
112 }
113
114 static void audio_resume_action(void)
115 {
116         struct test_data *data = tester_get_data();
117         struct step *step = g_new0(struct step, 1);
118         int err;
119
120         err = data->audio->open_output_stream(data->audio,
121                                                 0,
122                                                 AUDIO_DEVICE_OUT_ALL_A2DP,
123                                                 AUDIO_OUTPUT_FLAG_NONE,
124                                                 NULL,
125                                                 &data->if_stream, NULL);
126         if (err < 0) {
127                 step->action_status = BT_STATUS_FAIL;
128                 goto done;
129         }
130
131         /* Write something to force resume */
132         data->if_stream->write(data->if_stream, &err, sizeof(err));
133
134 done:
135         schedule_action_verification(step);
136 }
137
138 static void audio_suspend_action(void)
139 {
140         struct test_data *data = tester_get_data();
141         struct step *step = g_new0(struct step, 1);
142
143         data->if_stream->common.standby(&data->if_stream->common);
144
145         schedule_action_verification(step);
146 }
147
148 static struct test_case test_cases[] = {
149         TEST_CASE_BREDRLE("A2DP Init",
150                 ACTION_SUCCESS(dummy_action, NULL),
151         ),
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),
168         ),
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),
188         ),
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),
208         ),
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),
231         ),
232 };
233
234 struct queue *get_a2dp_tests(void)
235 {
236         uint16_t i = 0;
237
238         list = queue_new();
239         if (!list)
240                 return NULL;
241
242         for (; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i)
243                 if (!queue_push_tail(list, &test_cases[i])) {
244                         queue_destroy(list, NULL);
245                         return NULL;
246                 }
247
248         return list;
249 }
250
251 void remove_a2dp_tests(void)
252 {
253         queue_destroy(list, NULL);
254 }