summaryrefslogtreecommitdiff
path: root/tests/unittest/utc_bluetooth_le_l2cap_positive.c
blob: 91f4fca376f5c22149e93df7b0fa2dff7cc9e75d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// Copyright (c) 2022 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "assert_local.h"
#include <bluetooth.h>
#include <stdlib.h>
#include <stdbool.h>
#include <glib.h>
#include <string.h>
#include <system_info.h>

static int startup_flag = BT_ERROR_NONE;
static int socket_fd = 0;
static int ret = BT_ERROR_NONE;
static int l2cap_test_psm = 37;
static bool bt_supported = false;
static bool le_supported = false;
static bool le_coc_supported = false;

static void socket_l2cap_channel_connection_state_changed_cb_for_socket_p(int result,
		bt_socket_connection_state_e connection_state,
		bt_socket_l2cap_le_connection_s *connection, void *user_data)
{
}

/**
 * @function		utc_bluetooth_bt_socket_l2cap_channel_positive_startup
 * @description	Get system information to check for the support of Bluetooth. If BT is supported on the device,
 *				then intialize the bluetooth on the device successfully and retrieve the adapter state.
 * @parameter       NA
 * @return          NA
 */
void utc_bluetooth_bt_socket_l2cap_channel_positive_startup(void)
{
	bt_supported = false;
	system_info_get_platform_bool("http://tizen.org/feature/network.bluetooth", &bt_supported);

	le_supported = false;
	system_info_get_platform_bool("http://tizen.org/feature/network.bluetooth.le", &le_supported);

	le_coc_supported = false;
	system_info_get_platform_bool("http://tizen.org/feature/network.bluetooth.le.coc", &le_coc_supported);

	startup_flag = BT_ERROR_NONE;

	if(bt_supported && le_supported && le_coc_supported) {
		ret = bt_initialize();
		if(BT_ERROR_NONE != ret) {
			fprintf(stderr, "Startup error at %s:%d\n", __FILE__, __LINE__);
			fprintf(stderr, "bt_initialize failed (code: %d)\n", ret);
			startup_flag = ret;
			return;
			}

		bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;

		ret = bt_adapter_get_state(&adapter_state);
		if (adapter_state != BT_ADAPTER_ENABLED) {
			fprintf(stdout, "BT is not enabled!!");
			startup_flag = BT_ERROR_NOT_ENABLED;
		}
	}
}

/**
 * @function		utc_bluetooth_bt_socket_l2cap_channel_positive_cleanup
 * @description	Get system information to check for the support of Bluetooth. If BT is supported on the device,
 *				then deintialize the bluetooth on the device successfully.
 * @parameter       NA
 * @return          NA
 */
void utc_bluetooth_bt_socket_l2cap_channel_positive_cleanup(void)
{
	if(bt_supported && le_supported && le_coc_supported)
		bt_deinitialize();
}

/**
 * @testcase		utc_bluetooth_bt_socket_set_l2cap_channel_connection_state_changed_cb_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device, set the connection
 *				state changed callback and check for error. If bluetooth is initialized, set and unset connection state changed callback successfully.
 * @senario		Check if BT is supported and then set bt_socket_set_l2cap_channel_connection_state_changed_cb on the device and check for expected result.
 */
int utc_bluetooth_bt_socket_set_l2cap_channel_connection_state_changed_cb_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_set_l2cap_channel_connection_state_changed_cb(socket_l2cap_channel_connection_state_changed_cb_for_socket_p, NULL);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_set_l2cap_channel_connection_state_changed_cb(socket_l2cap_channel_connection_state_changed_cb_for_socket_p, NULL);
	assert_eq(ret, BT_ERROR_NONE);

	bt_socket_unset_l2cap_channel_connection_state_changed_cb();

	return 0;
}

/**
 * @testcase		utc_bluetooth_bt_socket_create_l2cap_channel_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device, create l2cap channel
 *				and check for error. If bluetooth is initialized, then create l2cap channel successfully.
 * @senario		Check if BT is supported and then call bt_socket_create_l2cap_channel on the device and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_create_l2cap_channel_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_destroy_l2cap_channel(socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

/**
 * @testcase            utc_bluetooth_bt_socket_listen_l2cap_channel_p
 * @since_tizen 7.0
 * @description Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *                              call bt_socket_listen_l2cap_channel and check for error. If bluetooth is initialized, then create l2cap channel
 *                              and listen for l2cap connection successfully.
 * @senario             Check if BT is supported and then call bt_socket_create_l2cap_channel and bt_socket_listen_l2cap_channel on the device
 *                              and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_listen_l2cap_channel_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_listen_l2cap_channel(socket_fd, 1);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_listen_l2cap_channel(socket_fd, 1);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_destroy_l2cap_channel(socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

/**
 * @testcase		utc_bluetooth_bt_socket_listen_and_accept_l2cap_channel_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *				call bt_socket_listen_and_accept_l2cap_channel and check for error. If bluetooth is initialized, then create l2cap channel
 *				and listen and accept for l2cap connection successfully.
 * @senario		Check if BT is supported and then call bt_socket_create_l2cap_channel and bt_socket_listen_and_accept_l2cap_channel on the device
 *				and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_listen_and_accept_l2cap_channel_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_listen_and_accept_l2cap_channel(socket_fd, 1);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_listen_and_accept_l2cap_channel(socket_fd, 1);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_destroy_l2cap_channel(socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

/**
 * @testcase            utc_bluetooth_bt_socket_get_l2cap_psm_p
 * @since_tizen 7.0
 * @description Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *                              call bt_socket_get_l2cap_psm and check for error. If bluetooth is initialized, then create l2cap channel
 *                              and get psm for l2cap channel successfully.
 * @senario             Check if BT is supported and then call bt_socket_create_l2cap_channel, bt_socket_listen_and_accept_l2cap_channel
 *                              and bt_socket_get_l2cap_psm on the device and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_get_l2cap_psm_p(void)
{
	int psm = -1;
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_get_l2cap_psm(socket_fd, &psm);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_listen_and_accept_l2cap_channel(socket_fd, 1);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_get_l2cap_psm(socket_fd, &psm);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_destroy_l2cap_channel(socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

/**
 * @testcase		utc_bluetooth_bt_socket_destroy_l2cap_channel_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *				destroy the l2cap channel and check for error. If bluetooth is initialized, then create l2cap channel
 *				and destroy l2cap channel successfully.
 * @senario		Check if BT is supported and then call bt_socket_create_l2cap_channel and bt_socket_destroy_l2cap_channel on the device
 *				and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_destroy_l2cap_channel_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_destroy_l2cap_channel(socket_fd);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_create_l2cap_channel(l2cap_test_psm, &socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_destroy_l2cap_channel(socket_fd);
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

/**
 * @testcase		utc_bluetooth_bt_socket_unset_l2cap_channel_connection_state_changed_cb_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *				unset the connection state changed callback and check for error. If bluetooth is initialized, then set
 *				connection state changed callback and unset connection state changed successfully.
 * @senario		Check if BT is supported and then set and unset connection state changed callback on the device
 *				and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_unset_l2cap_channel_connection_state_changed_cb_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_unset_l2cap_channel_connection_state_changed_cb();
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_set_l2cap_channel_connection_state_changed_cb(socket_l2cap_channel_connection_state_changed_cb_for_socket_p, NULL);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_unset_l2cap_channel_connection_state_changed_cb();
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}

static void bt_socket_connection_requested_cb_func(int socket_fd, const char *remote_address, void *user_data) {
}

/**
 * @testcase		utc_bluetooth_bt_socket_set_l2cap_channel_connection_requested_cb_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *				set the connection requested callback and check for error. If bluetooth is initialized, then set
 *				connection requested callback and unset connection requested callback successfully.
 * @senario		Check if BT is supported and then set and unset connection requested callback on the device
 *				and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_set_l2cap_channel_connection_requested_cb_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_set_l2cap_channel_connection_requested_cb(bt_socket_connection_requested_cb_func, NULL);
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_set_l2cap_channel_connection_requested_cb(bt_socket_connection_requested_cb_func, NULL);
	assert_eq(ret, BT_ERROR_NONE);

	bt_socket_unset_l2cap_channel_connection_requested_cb();

	return 0;
}

/**
 * @testcase		utc_bluetooth_bt_socket_unset_l2cap_channel_connection_requested_cb_p
 * @since_tizen	7.0
 * @description	Get system information to check for the support of Bluetooth. If BT is not supported on the device,
 *				unset the connection requested callback and check for error. If bluetooth is initialized, then set
 *				connection requested callback and unset connection requested callback successfully and check for error.
 * @senario		Check if BT is supported and then set and unset connection requested callback on the device
 *				and check for BT_ERROR_NONE.
 */
int utc_bluetooth_bt_socket_unset_l2cap_channel_connection_requested_cb_p(void)
{
	if(!bt_supported || !le_supported || !le_coc_supported) {
		ret = bt_socket_unset_l2cap_channel_connection_requested_cb();
		assert_eq(ret, BT_ERROR_NOT_SUPPORTED);
		return 0;
	}

	assert_eq(startup_flag, BT_ERROR_NONE);

	ret = bt_socket_set_l2cap_channel_connection_requested_cb(bt_socket_connection_requested_cb_func, NULL);
	assert_eq(ret, BT_ERROR_NONE);

	ret = bt_socket_unset_l2cap_channel_connection_requested_cb();
	assert_eq(ret, BT_ERROR_NONE);

	return 0;
}