2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
19 * @file bluetooth-gatt-test.c
20 * @brief This is the source file for bluetooth framework test suite.
31 #include <dbus/dbus-glib.h>
32 #include <dbus/dbus.h>
34 #include "bluetooth-api.h"
37 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
38 #define TC_PRT(format, args...) PRT(format"\n", ##args)
43 GMainLoop *main_loop = NULL;
50 tc_table_t tc_table[] = {
51 {"SetAdvertising ON" , 1},
52 {"SetAdvertising OFF" , 2},
53 {"SetCustomAdvertising ON, filter 0x03" , 3},
54 {"SetCustomAdvertising ON, filter 0x00" , 4},
55 {"SetAdvertisingData" , 5},
56 {"SetScanParameters" , 6},
57 {"SetScanResponseData" , 7},
58 {"Start LE Scan" , 8},
60 {"IsAdvertising" , 10},
61 {"Add White List" , 11},
62 {"Remove White List" , 12},
63 {"Clear White List" , 13},
64 {"Set Connectable ON" , 14},
65 {"Set Connectable OFF" , 15},
67 /* -----------------------------------------*/
73 #define tc_result(success, tc_index) \
74 TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, tc_table[tc_index].tc_name, ((success == TC_PASS) ? "Success" : "Failed"));
77 void tc_usage_print(void)
81 while (tc_table[i].tc_name) {
82 if (tc_table[i].tc_code != 0x00ff) {
83 TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
86 TC_PRT("Key %d : usage %s\n\n", 0x00ff,
94 int test_input_callback(void *data)
97 int test_id = (int)data;
98 bluetooth_advertising_params_t params;
103 g_main_loop_quit(main_loop);
106 TC_PRT("SetAdvertising ON");
107 ret = bluetooth_set_advertising(0, TRUE);
110 TC_PRT("SetAdvertising OFF");
111 ret = bluetooth_set_advertising(0, FALSE);
114 TC_PRT("SetCustomAdvertising ON, Filter 0x03");
115 params.interval_min = 1280;
116 params.interval_max = 1280;
117 params.filter_policy = 0x03;
119 ret = bluetooth_set_custom_advertising(0, TRUE, ¶ms);
122 TC_PRT("SetCustomAdvertising ON, Filter 0x00");
123 params.interval_min = 1280;
124 params.interval_max = 1280;
125 params.filter_policy = 0x00;
127 ret = bluetooth_set_custom_advertising(0, TRUE, ¶ms);
130 TC_PRT("SetAdvertisingData");
131 bluetooth_advertising_data_t adv;
132 guint8 data[6] = {0x05, 0xFF, 0x02, 0x03, 0x04, 0x05};
134 TC_PRT("%x %x %x %x %x %x", data[0], data[1], data[2], data[3],
136 memcpy(adv.data, data, sizeof(data));
137 ret = bluetooth_set_advertising_data(0, &adv, sizeof(data));
141 TC_PRT("SetScanParameters");
142 // ret = bluetooth_set_scan_parameters(1280, 160 /* 80 */);
145 TC_PRT("SetScanResponseData");
146 bluetooth_scan_resp_data_t rsp;
147 guint8 data[7] = {0x06, 0xFF, 0x02, 0x03, 0x04, 0x05, 0x06};
149 TC_PRT("%x %x %x %x %x %x %x", data[0], data[1], data[2],
150 data[3], data[4], data[5], data[6]);
151 memcpy(rsp.data, data, sizeof(data));
152 ret = bluetooth_set_scan_response_data(0, &rsp, sizeof(data));
156 TC_PRT("Start LE Scan");
157 ret = bluetooth_start_le_discovery();
160 TC_PRT("Stop LE Scan");
161 ret = bluetooth_stop_le_discovery();
164 TC_PRT("IsAdvertising");
165 gboolean advertising = FALSE;
168 ret = bluetooth_is_advertising(&advertising);
169 if (ret == BLUETOOTH_ERROR_NONE)
170 TC_PRT("Advertising is %s", advertising ? "started" : "stopped");
172 TC_PRT("bluetooth_is_advertising failed with [%d]", ret);
176 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
178 TC_PRT("Add White List");
180 ret = bluetooth_add_white_list(&device_address, BLUETOOTH_DEVICE_PUBLIC_ADDRESS);
181 if (ret != BLUETOOTH_ERROR_NONE)
182 TC_PRT("bluetooth_add_white_list failed with [%d]", ret);
187 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
189 TC_PRT("Remove White List");
191 ret = bluetooth_remove_white_list(&device_address, BLUETOOTH_DEVICE_PUBLIC_ADDRESS);
192 if (ret != BLUETOOTH_ERROR_NONE)
193 TC_PRT("bluetooth_remove_white_list failed with [%d]", ret);
198 TC_PRT("Clear White List");
200 ret = bluetooth_clear_white_list();
201 if (ret != BLUETOOTH_ERROR_NONE)
202 TC_PRT("bluetooth_clear_white_list failed with [%d]", ret);
207 TC_PRT("Set Connectable ON");
209 ret = bluetooth_set_connectable(TRUE);
210 if (ret != BLUETOOTH_ERROR_NONE)
211 TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
216 TC_PRT("Set Connectable OFF");
218 ret = bluetooth_set_connectable(FALSE);
219 if (ret != BLUETOOTH_ERROR_NONE)
220 TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
229 TC_PRT("Result : %d", ret);
235 TC_PRT("bluetooth framework TC startup");
237 dbus_threads_init_default();
239 main_loop = g_main_loop_new(NULL, FALSE);
244 TC_PRT("bluetooth framework TC cleanup");
245 if (main_loop != NULL)
246 g_main_loop_unref(main_loop);
249 void bt_event_callback(int event, bluetooth_event_param_t* param,
252 TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
253 TC_PRT("bt event callback 0x%04x", event);
255 case BLUETOOTH_EVENT_DISCOVERY_STARTED:
256 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_STARTED, result [0x%04x]", param->result);
259 case BLUETOOTH_EVENT_LE_DISCOVERY_STARTED:
260 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_STARTED, result [0x%04x]", param->result);
263 case BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED:
264 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED, result [0x%04x]", param->result);
267 case BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND:
268 TC_PRT("LE device founded");
269 bluetooth_le_device_info_t *le_device_info = NULL;
270 le_device_info = (bluetooth_le_device_info_t *)param->param_data;
271 TC_PRT("dev [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", \
272 le_device_info->device_address.addr[0], le_device_info->device_address.addr[1], le_device_info->device_address.addr[2], \
273 le_device_info->device_address.addr[3], le_device_info->device_address.addr[4], le_device_info->device_address.addr[5]);
277 case BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND:
279 bluetooth_device_info_t *device_info = NULL;
280 TC_PRT("BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND, result [0x%04x]", param->result);
281 device_info = (bluetooth_device_info_t *)param->param_data;
282 TC_PRT("dev [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", \
283 device_info->device_address.addr[0], device_info->device_address.addr[1], device_info->device_address.addr[2], \
284 device_info->device_address.addr[3], device_info->device_address.addr[4], device_info->device_address.addr[5]);
288 case BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED:
290 bluetooth_device_info_t *device_info = NULL;
291 TC_PRT("BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED, result [0x%04x]", param->result);
292 device_info = (bluetooth_device_info_t *)param->param_data;
293 TC_PRT("dev [%s] [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", device_info->device_name.name, \
294 device_info->device_address.addr[0], device_info->device_address.addr[1], device_info->device_address.addr[2], \
295 device_info->device_address.addr[3], device_info->device_address.addr[4], device_info->device_address.addr[5]);
299 case BLUETOOTH_EVENT_DISCOVERY_FINISHED:
300 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_FINISHED, result [0x%04x]", param->result);
303 case BLUETOOTH_EVENT_ADVERTISING_STARTED:
304 TC_PRT("BLUETOOTH_EVENT_ADVERTISING_STARTED, result [0x%04x], "
305 "interval_min [%f ms], interval_max [%f ms]",
307 ((bluetooth_advertising_params_t *)param->param_data)->interval_min,
308 ((bluetooth_advertising_params_t *)param->param_data)->interval_max);
311 case BLUETOOTH_EVENT_ADVERTISING_STOPPED:
312 TC_PRT("BLUETOOTH_EVENT_ADVERTISING_STOPPED, result [0x%04x]", param->result);
316 TC_PRT("received event [0x%04x]", event);
319 TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
322 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
325 unsigned int len = 0;
328 if (g_io_channel_read_chars(chan, buf, sizeof(buf),
329 &len, NULL) == G_IO_STATUS_ERROR) {
330 printf("IO Channel read error");
339 g_idle_add(test_input_callback, (void*)test_id);
351 /* Register callback function */
352 TC_PRT("TC : %s", tc_table[0].tc_name);
353 ret_val = bluetooth_register_callback(bt_event_callback, NULL);
354 if (ret_val >= BLUETOOTH_ERROR_NONE) {
355 TC_PRT("bluetooth_register_callback returned Success");
356 tc_result(TC_PASS, 0);
358 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
359 tc_result(TC_FAIL, 0);
363 ret_val = bluetooth_check_adapter();
364 if (ret_val < BLUETOOTH_ERROR_NONE) {
365 TC_PRT("bluetooth_check_adapter returned failiure [0x%04x]", ret_val);
366 tc_result(TC_FAIL, 3);
368 TC_PRT("BT state [0x%04x]", ret_val);
369 tc_result(TC_PASS, 3);
372 key_io = g_io_channel_unix_new(fileno(stdin));
374 g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
376 g_io_channel_unref(key_io);
378 g_main_loop_run(main_loop);