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