16481972f1f6520079f8bff02881219e471c0d6b
[platform/core/connectivity/bluetooth-frwk.git] / test / gatt-test / bluetooth-gatt-test.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 /**
25  * @file       bluetooth-gatt-test.c
26  * @brief      This is the source file for bluetooth framework test suite.
27  */
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <dbus/dbus-glib.h>
38
39 #include "bluetooth-api.h"
40
41
42 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
43 #define TC_PRT(format, args...) PRT(format"\n", ##args)
44
45 #define TC_PASS 1
46 #define TC_FAIL 0
47
48 GMainLoop *main_loop = NULL;
49
50 typedef struct
51 {
52         const char *tc_name;
53         int tc_code;
54 } tc_table_t;
55
56 tc_table_t tc_table[] =
57 {
58         {"Send alert to remote le device"               , 1},
59         {"Set Link loss alert"          , 2},
60
61         /* -----------------------------------------*/
62         {"Finish"                                       , 0x00ff},
63         {NULL                                   , 0x0000},
64
65 };
66
67 #define tc_result(success, tc_index) \
68         TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, tc_table[tc_index].tc_name, ((success == TC_PASS)?"Success":"Failed"));
69
70 char *g_alert_char_handle = NULL;
71 guint8 g_alert_level = 0;
72
73 #define IMMEDIATE_ALERT_UUID    "00001802-0000-1000-8000-00805f9b34fb"
74 #define LINK_LOSS_UUID          "00001803-0000-1000-8000-00805f9b34fb"
75 #define ALERT_LEVEL_CHR_UUID    "2a06"
76
77 #define BD_ADDR_FILE "/opt/remote-bd"
78
79 void tc_usage_print(void)
80 {
81         int i = 0;
82
83         while (tc_table[i].tc_name) {
84                 if (tc_table[i].tc_code != 0x00ff) {
85                         TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
86                                                         tc_table[i].tc_name);
87                 } else {
88                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
89                                                         tc_table[i].tc_name);
90                 }
91
92                 i++;
93         }
94 }
95
96 static void convert_addr_string_to_addr_type(bluetooth_device_address_t *addr,
97                                                         const char *address)
98 {
99         char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
100
101         if (!address || !addr)
102                 return;
103
104         addr->addr[0] = strtol(address, &ptr5, 16);
105         addr->addr[1] = strtol(ptr5 + 1, &ptr4, 16);
106         addr->addr[2] = strtol(ptr4 + 1, &ptr3, 16);
107         addr->addr[3] = strtol(ptr3 + 1, &ptr2, 16);
108         addr->addr[4] = strtol(ptr2 + 1, &ptr1, 16);
109         addr->addr[5] = strtol(ptr1 + 1, NULL, 16);
110 }
111
112 char * get_bd_from_file(char *filename)
113 {
114         int fd;
115         char *buf;
116
117         if ((fd = open(filename, O_RDONLY)) < 0) {
118                 perror("Can't open file");
119                 return NULL;
120         }
121
122         buf = g_malloc0(20);
123
124         if (read(fd, buf, 17) < 17) {
125                 perror("Can't load firmware");
126                 g_free(buf);
127                 close(fd);
128                 return NULL;
129         }
130
131         close(fd);
132
133         return buf;
134 }
135
136 static void __accept_bdaddress(bluetooth_device_address_t *device_address)
137 {
138         char str_address[20] = {0};
139         char *addr;
140
141         addr = get_bd_from_file(BD_ADDR_FILE);
142         if (addr) {
143                 TC_PRT("Remote bd adress from file: %s", addr);
144                 convert_addr_string_to_addr_type(device_address, addr);
145                 return;
146         }
147
148         TC_PRT("Enter bd address: ");
149         scanf("%s", str_address);
150         TC_PRT("You have entered bd address %s\n", str_address);
151         convert_addr_string_to_addr_type(device_address, str_address);
152 }
153
154 static void __accept_alert_level()
155 {
156         TC_PRT("Enter alert level \n 0 - no alert 1 - mild alert 2 - High alert : ");
157         scanf("%d", &g_alert_level);
158         TC_PRT("You have selected alert level %d ", g_alert_level);
159 }
160
161 int test_input_callback(void *data)
162 {
163         int ret = 0;
164         int test_id = (int)data;
165         bluetooth_device_address_t device_address;
166         bt_gatt_service_property_t service;
167
168         switch (test_id) {
169         case 0x00ff:
170                 TC_PRT("Finished");
171                 g_main_loop_quit(main_loop);
172                 break;
173         case 1:
174                 TC_PRT("Immediate Alert");
175                 __accept_bdaddress(&device_address);
176
177                 __accept_alert_level();
178
179                 if (g_alert_char_handle) {
180                         if (bluetooth_gatt_set_characteristics_value(g_alert_char_handle,
181                                                 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
182                                 TC_PRT("Set char val failed");
183
184                         return 0;
185                 }
186
187                 ret = bluetooth_gatt_get_service_from_uuid(&device_address,
188                                                         IMMEDIATE_ALERT_UUID,
189                                                         &service);
190                 if (ret != BLUETOOTH_ERROR_NONE) {
191                         TC_PRT(" bluetooth_gatt_get_service_from_uuid FAILED");
192                         return 0;
193                 }
194
195                 ret = bluetooth_gatt_get_char_from_uuid(service.handle,
196                                                         ALERT_LEVEL_CHR_UUID);
197                 if (ret != BLUETOOTH_ERROR_NONE) {
198                         TC_PRT(" bluetooth_gatt_get_char_from_uuid FAILED");
199                         return 0;
200                 }
201
202                 break;
203         case 2:
204                 TC_PRT("Proximity Link loss alert");
205                 __accept_bdaddress(&device_address);
206
207                 __accept_alert_level();
208
209                 /* TODO */
210                 break;
211         default:
212                 break;
213         }
214
215         return 0;
216 }
217
218 void startup()
219 {
220         TC_PRT("bluetooth framework TC startup");
221
222         if (!g_thread_supported())
223                 g_thread_init(NULL);
224
225         dbus_g_thread_init();
226
227         g_type_init();
228         main_loop = g_main_loop_new(NULL, FALSE);
229 }
230
231 void cleanup()
232 {
233         TC_PRT("bluetooth framework TC cleanup");
234         if ( main_loop != NULL)
235                 g_main_loop_unref(main_loop);
236 }
237
238 static void __handle_alert_char(char *char_handle,
239                                         bt_gatt_char_property_t *char_pty)
240 {
241         if (char_pty->val == NULL)
242                 TC_PRT("Read only char");
243         else
244                 TC_PRT("Current Alert level [%d]", char_pty->val[0]);
245
246         g_alert_char_handle = g_strdup(char_handle);
247
248         if (bluetooth_gatt_set_characteristics_value(char_handle,
249                                 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
250                 TC_PRT("Set char val failed");
251
252 }
253
254 static gboolean __handle_char(char *char_handle,
255                                         bt_gatt_char_property_t *char_pty)
256 {
257         TC_PRT("char uuid %s", char_pty->uuid);
258
259         if (g_strstr_len(char_pty->uuid, -1, ALERT_LEVEL_CHR_UUID) != NULL) {
260                 TC_PRT("Alert char recieved");
261                 __handle_alert_char(char_handle, char_pty);
262                 return TRUE;
263         } /* Add else if for other chars*/
264
265         return FALSE;
266 }
267
268 void bt_event_callback(int event, bluetooth_event_param_t* param,
269                                                         void *user_data)
270 {
271         TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
272         TC_PRT("bt event callback 0x%04x", event);
273         switch(event) {
274         case BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID:
275         {
276                 TC_PRT("BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID");
277                 if (param->result != 0) {
278                         TC_PRT("Failed!!!");
279                         return;
280                 }
281                 bt_gatt_char_property_t *char_pty = param->param_data;
282
283                 __handle_char(char_pty->handle, char_pty);
284
285         }
286         break;
287         default:
288                 TC_PRT("received event [0x%04x]", event);
289                 break;
290         }
291         TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
292 }
293
294 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
295 {
296         char buf[10] = {0};
297         unsigned int len = 0;
298         int test_id;
299
300         if (g_io_channel_read(chan, buf, sizeof(buf), &len) !=
301                                                         G_IO_ERROR_NONE) {
302                 printf("IO Channel read error");
303                 return FALSE;
304         }
305         printf("%s\n",buf);
306         tc_usage_print();
307
308         test_id = atoi(buf);
309
310         if (test_id)
311                 g_idle_add(test_input_callback, (void*)test_id);
312
313         return TRUE;
314 }
315
316 int main()
317 {
318         int ret_val;
319         GIOChannel *key_io;
320
321         startup();
322
323         /* Register callback function */
324         TC_PRT("TC : %s", tc_table[0].tc_name);
325         ret_val = bluetooth_register_callback(bt_event_callback, NULL);
326         if (ret_val >= BLUETOOTH_ERROR_NONE) {
327                 TC_PRT("bluetooth_register_callback returned Success");
328                 tc_result(TC_PASS, 0);
329         } else {
330                 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
331                 tc_result(TC_FAIL, 0);
332                 return 0;
333         }
334
335         ret_val = bluetooth_check_adapter();
336         if (ret_val < BLUETOOTH_ERROR_NONE) {
337                 TC_PRT("bluetooth_check_adapter returned failiure [0x%04x]", ret_val);
338                 tc_result(TC_FAIL, 3);
339         } else {
340                 TC_PRT("BT state [0x%04x]", ret_val);
341                 tc_result(TC_PASS, 3);
342         }
343
344         key_io = g_io_channel_unix_new(fileno(stdin));
345
346         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
347                         key_event_cb, NULL);
348         g_io_channel_unref(key_io);
349
350         g_main_loop_run(main_loop);
351
352         cleanup();
353         return 0;
354 }