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.
32 #include "bluetooth-api.h"
35 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
36 #define TC_PRT(format, args...) PRT(format"\n", ##args)
41 GMainLoop *main_loop = NULL;
48 tc_table_t tc_table[] = {
49 {"SetAdvertising ON" , 1},
50 {"SetAdvertising OFF" , 2},
51 {"SetCustomAdvertising ON, filter 0x03" , 3},
52 {"SetCustomAdvertising ON, filter 0x00" , 4},
53 {"SetAdvertisingData" , 5},
54 {"SetScanParameters" , 6},
55 {"SetScanResponseData" , 7},
56 {"Start LE Scan" , 8},
58 {"IsAdvertising" , 10},
59 {"Add White List" , 11},
60 {"Remove White List" , 12},
61 {"Clear White List" , 13},
62 {"Set Connectable ON" , 14},
63 {"Set Connectable OFF" , 15},
65 /* -----------------------------------------*/
71 #define tc_result(success, tc_index) \
72 TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, tc_table[tc_index].tc_name, ((success == TC_PASS) ? "Success" : "Failed"));
75 void tc_usage_print(void)
79 while (tc_table[i].tc_name) {
80 if (tc_table[i].tc_code != 0x00ff) {
81 TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
84 TC_PRT("Key %d : usage %s\n\n", 0x00ff,
92 int test_input_callback(void *data)
95 int test_id = (int)data;
96 bluetooth_advertising_params_t params;
101 g_main_loop_quit(main_loop);
104 TC_PRT("SetAdvertising ON");
105 ret = bluetooth_set_advertising(0, TRUE);
108 TC_PRT("SetAdvertising OFF");
109 ret = bluetooth_set_advertising(0, FALSE);
112 TC_PRT("SetCustomAdvertising ON, Filter 0x03");
113 params.interval_min = 1280;
114 params.interval_max = 1280;
115 params.filter_policy = 0x03;
117 ret = bluetooth_set_custom_advertising(0, TRUE, ¶ms);
120 TC_PRT("SetCustomAdvertising ON, Filter 0x00");
121 params.interval_min = 1280;
122 params.interval_max = 1280;
123 params.filter_policy = 0x00;
125 ret = bluetooth_set_custom_advertising(0, TRUE, ¶ms);
128 TC_PRT("SetAdvertisingData");
129 bluetooth_advertising_data_t adv;
130 guint8 data[6] = {0x05, 0xFF, 0x02, 0x03, 0x04, 0x05};
132 TC_PRT("%x %x %x %x %x %x", data[0], data[1], data[2], data[3],
134 memcpy(adv.data, data, sizeof(data));
135 ret = bluetooth_set_advertising_data(0, &adv, sizeof(data));
139 TC_PRT("SetScanParameters");
140 // ret = bluetooth_set_scan_parameters(1280, 160 /* 80 */);
143 TC_PRT("SetScanResponseData");
144 bluetooth_scan_resp_data_t rsp;
145 guint8 data[7] = {0x06, 0xFF, 0x02, 0x03, 0x04, 0x05, 0x06};
147 TC_PRT("%x %x %x %x %x %x %x", data[0], data[1], data[2],
148 data[3], data[4], data[5], data[6]);
149 memcpy(rsp.data, data, sizeof(data));
150 ret = bluetooth_set_scan_response_data(0, &rsp, sizeof(data));
154 TC_PRT("Start LE Scan");
155 ret = bluetooth_start_le_discovery();
158 TC_PRT("Stop LE Scan");
159 ret = bluetooth_stop_le_discovery();
162 TC_PRT("IsAdvertising");
163 gboolean advertising = FALSE;
166 ret = bluetooth_is_advertising(&advertising);
167 if (ret == BLUETOOTH_ERROR_NONE)
168 TC_PRT("Advertising is %s", advertising ? "started" : "stopped");
170 TC_PRT("bluetooth_is_advertising failed with [%d]", ret);
174 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
176 TC_PRT("Add White List");
178 ret = bluetooth_add_white_list(&device_address, BLUETOOTH_DEVICE_PUBLIC_ADDRESS);
179 if (ret != BLUETOOTH_ERROR_NONE)
180 TC_PRT("bluetooth_add_white_list failed with [%d]", ret);
185 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
187 TC_PRT("Remove White List");
189 ret = bluetooth_remove_white_list(&device_address, BLUETOOTH_DEVICE_PUBLIC_ADDRESS);
190 if (ret != BLUETOOTH_ERROR_NONE)
191 TC_PRT("bluetooth_remove_white_list failed with [%d]", ret);
196 TC_PRT("Clear White List");
198 ret = bluetooth_clear_white_list();
199 if (ret != BLUETOOTH_ERROR_NONE)
200 TC_PRT("bluetooth_clear_white_list failed with [%d]", ret);
205 TC_PRT("Set Connectable ON");
207 ret = bluetooth_set_connectable(TRUE);
208 if (ret != BLUETOOTH_ERROR_NONE)
209 TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
214 TC_PRT("Set Connectable OFF");
216 ret = bluetooth_set_connectable(FALSE);
217 if (ret != BLUETOOTH_ERROR_NONE)
218 TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
227 TC_PRT("Result : %d", ret);
233 TC_PRT("bluetooth framework TC startup");
235 dbus_threads_init_default();
237 main_loop = g_main_loop_new(NULL, FALSE);
242 TC_PRT("bluetooth framework TC cleanup");
243 if (main_loop != NULL)
244 g_main_loop_unref(main_loop);
247 void bt_event_callback(int event, bluetooth_event_param_t* param,
250 TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
251 TC_PRT("bt event callback 0x%04x", event);
253 case BLUETOOTH_EVENT_DISCOVERY_STARTED:
254 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_STARTED, result [0x%04x]", param->result);
257 case BLUETOOTH_EVENT_LE_DISCOVERY_STARTED:
258 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_STARTED, result [0x%04x]", param->result);
261 case BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED:
262 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED, result [0x%04x]", param->result);
265 case BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND:
266 TC_PRT("LE device founded");
267 bluetooth_le_device_info_t *le_device_info = NULL;
268 le_device_info = (bluetooth_le_device_info_t *)param->param_data;
269 TC_PRT("dev [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", \
270 le_device_info->device_address.addr[0], le_device_info->device_address.addr[1], le_device_info->device_address.addr[2], \
271 le_device_info->device_address.addr[3], le_device_info->device_address.addr[4], le_device_info->device_address.addr[5]);
275 case BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND:
277 bluetooth_device_info_t *device_info = NULL;
278 TC_PRT("BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND, result [0x%04x]", param->result);
279 device_info = (bluetooth_device_info_t *)param->param_data;
280 TC_PRT("dev [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", \
281 device_info->device_address.addr[0], device_info->device_address.addr[1], device_info->device_address.addr[2], \
282 device_info->device_address.addr[3], device_info->device_address.addr[4], device_info->device_address.addr[5]);
286 case BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED:
288 bluetooth_device_info_t *device_info = NULL;
289 TC_PRT("BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED, result [0x%04x]", param->result);
290 device_info = (bluetooth_device_info_t *)param->param_data;
291 TC_PRT("dev [%s] [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X]", device_info->device_name.name, \
292 device_info->device_address.addr[0], device_info->device_address.addr[1], device_info->device_address.addr[2], \
293 device_info->device_address.addr[3], device_info->device_address.addr[4], device_info->device_address.addr[5]);
297 case BLUETOOTH_EVENT_DISCOVERY_FINISHED:
298 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_FINISHED, result [0x%04x]", param->result);
301 case BLUETOOTH_EVENT_ADVERTISING_STARTED:
302 TC_PRT("BLUETOOTH_EVENT_ADVERTISING_STARTED, result [0x%04x], "
303 "interval_min [%f ms], interval_max [%f ms]",
305 ((bluetooth_advertising_params_t *)param->param_data)->interval_min,
306 ((bluetooth_advertising_params_t *)param->param_data)->interval_max);
309 case BLUETOOTH_EVENT_ADVERTISING_STOPPED:
310 TC_PRT("BLUETOOTH_EVENT_ADVERTISING_STOPPED, result [0x%04x]", param->result);
314 TC_PRT("received event [0x%04x]", event);
317 TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
320 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
323 unsigned int len = 0;
326 if (g_io_channel_read_chars(chan, buf, sizeof(buf),
327 &len, NULL) == G_IO_STATUS_ERROR) {
328 printf("IO Channel read error");
337 g_idle_add(test_input_callback, (void*)test_id);
349 /* Register callback function */
350 TC_PRT("TC : %s", tc_table[0].tc_name);
351 ret_val = bluetooth_register_callback(bt_event_callback, NULL);
352 if (ret_val >= BLUETOOTH_ERROR_NONE) {
353 TC_PRT("bluetooth_register_callback returned Success");
354 tc_result(TC_PASS, 0);
356 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
357 tc_result(TC_FAIL, 0);
361 ret_val = bluetooth_check_adapter();
362 if (ret_val < BLUETOOTH_ERROR_NONE) {
363 TC_PRT("bluetooth_check_adapter returned failiure [0x%04x]", ret_val);
364 tc_result(TC_FAIL, 3);
366 TC_PRT("BT state [0x%04x]", ret_val);
367 tc_result(TC_PASS, 3);
370 key_io = g_io_channel_unix_new(fileno(stdin));
372 g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
374 g_io_channel_unref(key_io);
376 g_main_loop_run(main_loop);