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