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