3b62622656000312512eb386a59334ab2590ff50
[platform/core/connectivity/bluetooth-frwk.git] / test / le-adv / bluetooth-advertising-test.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 /**
19  * @file       bluetooth-gatt-test.c
20  * @brief      This is the source file for bluetooth framework test suite.
21  */
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <glib.h>
31 #include <dbus/dbus-glib.h>
32 #include <dbus/dbus.h>
33
34 #include "bluetooth-api.h"
35
36
37 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
38 #define TC_PRT(format, args...) PRT(format"\n", ##args)
39
40 #define TC_PASS 1
41 #define TC_FAIL 0
42
43 GMainLoop *main_loop = NULL;
44
45 typedef struct {
46         const char *tc_name;
47         int tc_code;
48 } tc_table_t;
49
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},
59         {"Stop LE Scan"         , 9},
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},
66
67         /* -----------------------------------------*/
68         {"Finish"                                       , 0x00ff},
69         {NULL                                   , 0x0000},
70
71 };
72
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"));
75
76
77 void tc_usage_print(void)
78 {
79         int i = 0;
80
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,
84                                                         tc_table[i].tc_name);
85                 } else {
86                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
87                                                         tc_table[i].tc_name);
88                 }
89
90                 i++;
91         }
92 }
93
94 int test_input_callback(void *data)
95 {
96         int ret = 0;
97         int test_id = (int)data;
98         bluetooth_advertising_params_t params;
99
100         switch (test_id) {
101         case 0x00ff:
102                 TC_PRT("Finished");
103                 g_main_loop_quit(main_loop);
104                 break;
105         case 1:
106                 TC_PRT("SetAdvertising ON");
107                 ret = bluetooth_set_advertising(0, TRUE);
108                 break;
109         case 2:
110                 TC_PRT("SetAdvertising OFF");
111                 ret = bluetooth_set_advertising(0, FALSE);
112                 break;
113         case 3:
114                 TC_PRT("SetCustomAdvertising ON, Filter 0x03");
115                 params.interval_min = 1280;
116                 params.interval_max = 1280;
117                 params.filter_policy = 0x03;
118                 params.type = 0x00;
119                 ret = bluetooth_set_custom_advertising(0, TRUE, &params);
120                 break;
121         case 4:
122                 TC_PRT("SetCustomAdvertising ON, Filter 0x00");
123                 params.interval_min = 1280;
124                 params.interval_max = 1280;
125                 params.filter_policy = 0x00;
126                 params.type = 0x00;
127                 ret = bluetooth_set_custom_advertising(0, TRUE, &params);
128                 break;
129         case 5: {
130                 TC_PRT("SetAdvertisingData");
131                 bluetooth_advertising_data_t adv;
132                 guint8 data[6]  = {0x05, 0xFF, 0x02, 0x03, 0x04, 0x05};
133
134                 TC_PRT("%x %x %x %x %x %x", data[0], data[1], data[2], data[3],
135                                 data[4], data[5]);
136                 memcpy(adv.data, data, sizeof(data));
137                 ret = bluetooth_set_advertising_data(0, &adv, sizeof(data));
138                 break;
139         }
140         case 6:
141                 TC_PRT("SetScanParameters");
142         //      ret = bluetooth_set_scan_parameters(1280, 160 /* 80 */);
143                 break;
144         case 7: {
145                 TC_PRT("SetScanResponseData");
146                 bluetooth_scan_resp_data_t rsp;
147                 guint8 data[7]  = {0x06, 0xFF, 0x02, 0x03, 0x04, 0x05, 0x06};
148
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));
153                 break;
154         }
155         case 8:
156                 TC_PRT("Start LE Scan");
157                 ret = bluetooth_start_le_discovery();
158                 break;
159         case 9:
160                 TC_PRT("Stop LE Scan");
161                 ret = bluetooth_stop_le_discovery();
162                 break;
163         case 10: {
164                 TC_PRT("IsAdvertising");
165                 gboolean advertising = FALSE;
166                 int ret;
167
168                 ret = bluetooth_is_advertising(&advertising);
169                 if (ret == BLUETOOTH_ERROR_NONE)
170                         TC_PRT("Advertising is %s", advertising ? "started" : "stopped");
171                 else
172                         TC_PRT("bluetooth_is_advertising failed with [%d]", ret);
173                 break;
174         }
175         case 11: {
176                 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
177
178                 TC_PRT("Add White List");
179
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);
183
184                 break;
185         }
186         case 12: {
187                 bluetooth_device_address_t device_address = { {0x00, 0x19, 0x0E, 0x11, 0x56, 0x06} };
188
189                 TC_PRT("Remove White List");
190
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);
194
195                 break;
196         }
197         case 13: {
198                 TC_PRT("Clear White List");
199
200                 ret = bluetooth_clear_white_list();
201                 if (ret != BLUETOOTH_ERROR_NONE)
202                         TC_PRT("bluetooth_clear_white_list failed with [%d]", ret);
203
204                 break;
205         }
206         case 14: {
207                 TC_PRT("Set Connectable ON");
208
209                 ret = bluetooth_set_connectable(TRUE);
210                 if (ret != BLUETOOTH_ERROR_NONE)
211                         TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
212
213                 break;
214         }
215         case 15: {
216                 TC_PRT("Set Connectable OFF");
217
218                 ret = bluetooth_set_connectable(FALSE);
219                 if (ret != BLUETOOTH_ERROR_NONE)
220                         TC_PRT("bt_adapter_set_connectable failed with [%d]", ret);
221
222                 break;
223         }
224
225         default:
226                 break;
227         }
228
229         TC_PRT("Result : %d", ret);
230         return 0;
231 }
232
233 void startup()
234 {
235         TC_PRT("bluetooth framework TC startup");
236
237         dbus_threads_init_default();
238
239         main_loop = g_main_loop_new(NULL, FALSE);
240 }
241
242 void cleanup()
243 {
244         TC_PRT("bluetooth framework TC cleanup");
245         if (main_loop != NULL)
246                 g_main_loop_unref(main_loop);
247 }
248
249 void bt_event_callback(int event, bluetooth_event_param_t* param,
250                                                         void *user_data)
251 {
252         TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
253         TC_PRT("bt event callback 0x%04x", event);
254         switch (event) {
255         case BLUETOOTH_EVENT_DISCOVERY_STARTED:
256                 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_STARTED, result [0x%04x]", param->result);
257                 break;
258
259         case BLUETOOTH_EVENT_LE_DISCOVERY_STARTED:
260                 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_STARTED, result [0x%04x]", param->result);
261                 break;
262
263         case BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED:
264                 TC_PRT("BLUETOOTH_EVENT_LE_DISCOVERY_FINISHED, result [0x%04x]", param->result);
265                 break;
266
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]);
274
275                 break;
276
277         case BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND:
278         {
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]);
285                 break;
286         }
287
288         case BLUETOOTH_EVENT_REMOTE_DEVICE_NAME_UPDATED:
289         {
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]);
296                 break;
297         }
298
299         case BLUETOOTH_EVENT_DISCOVERY_FINISHED:
300                 TC_PRT("BLUETOOTH_EVENT_DISCOVERY_FINISHED, result [0x%04x]", param->result);
301                 break;
302
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]",
306                                 param->result,
307                                 ((bluetooth_advertising_params_t *)param->param_data)->interval_min,
308                                 ((bluetooth_advertising_params_t *)param->param_data)->interval_max);
309                 break;
310
311         case BLUETOOTH_EVENT_ADVERTISING_STOPPED:
312                 TC_PRT("BLUETOOTH_EVENT_ADVERTISING_STOPPED, result [0x%04x]", param->result);
313                 break;
314
315         default:
316                 TC_PRT("received event [0x%04x]", event);
317                 break;
318         }
319         TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
320 }
321
322 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
323 {
324         char buf[10] = {0};
325         unsigned int len = 0;
326         int test_id;
327
328         if (g_io_channel_read_chars(chan, buf, sizeof(buf), 
329                         &len, NULL) ==  G_IO_STATUS_ERROR) {
330                 printf("IO Channel read error");
331                 return FALSE;
332         }
333         printf("%s\n", buf);
334         tc_usage_print();
335
336         test_id = atoi(buf);
337
338         if (test_id)
339                 g_idle_add(test_input_callback, (void*)test_id);
340
341         return TRUE;
342 }
343
344 int main()
345 {
346         int ret_val;
347         GIOChannel *key_io;
348
349         startup();
350
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);
357         } else {
358                 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
359                 tc_result(TC_FAIL, 0);
360                 return 0;
361         }
362
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);
367         } else {
368                 TC_PRT("BT state [0x%04x]", ret_val);
369                 tc_result(TC_PASS, 3);
370         }
371
372         key_io = g_io_channel_unix_new(fileno(stdin));
373
374         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
375                         key_event_cb, NULL);
376         g_io_channel_unref(key_io);
377
378         g_main_loop_run(main_loop);
379
380         cleanup();
381         return 0;
382 }