4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * @file bluetooth-gatt-test.c
26 * @brief This is the source file for bluetooth framework test suite.
37 #include <dbus/dbus-glib.h>
39 #include "bluetooth-api.h"
42 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
43 #define TC_PRT(format, args...) PRT(format"\n", ##args)
48 GMainLoop *main_loop = NULL;
56 tc_table_t tc_table[] =
58 {"Send alert to remote le device" , 1},
59 {"Set Link loss alert" , 2},
61 /* -----------------------------------------*/
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"));
70 char *g_alert_char_handle = NULL;
71 guint8 g_alert_level = 0;
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"
77 #define BD_ADDR_FILE "/opt/remote-bd"
79 void tc_usage_print(void)
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,
88 TC_PRT("Key %d : usage %s\n\n", 0x00ff,
96 static void convert_addr_string_to_addr_type(bluetooth_device_address_t *addr,
99 char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
101 if (!address || !addr)
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);
112 char * get_bd_from_file(char *filename)
117 if ((fd = open(filename, O_RDONLY)) < 0) {
118 perror("Can't open file");
123 /* Fix : NULL_RETURNS */
129 if (read(fd, buf, 17) < 17) {
130 perror("Can't load firmware");
141 static void __accept_bdaddress(bluetooth_device_address_t *device_address)
143 char str_address[20] = {0};
146 addr = get_bd_from_file(BD_ADDR_FILE);
148 TC_PRT("Remote bd adress from file: %s", addr);
149 convert_addr_string_to_addr_type(device_address, addr);
154 TC_PRT("Enter bd address: ");
156 ret = scanf("%s", str_address);
158 TC_PRT("Some read error");
159 TC_PRT("You have entered bd address %s\n", str_address);
160 convert_addr_string_to_addr_type(device_address, str_address);
163 static void __accept_alert_level()
165 TC_PRT("Enter alert level \n 0 - no alert 1 - mild alert 2 - High alert : ");
167 ret = scanf("%c", &g_alert_level);
169 TC_PRT("Some read error");
170 TC_PRT("You have selected alert level %hu ", g_alert_level);
173 int test_input_callback(void *data)
176 int test_id = (int)data;
177 bluetooth_device_address_t device_address;
178 bt_gatt_service_property_t service;
183 g_main_loop_quit(main_loop);
186 TC_PRT("Immediate Alert");
187 __accept_bdaddress(&device_address);
189 __accept_alert_level();
191 if (g_alert_char_handle) {
192 if (bluetooth_gatt_set_characteristics_value(g_alert_char_handle,
193 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
194 TC_PRT("Set char val failed");
199 ret = bluetooth_gatt_get_service_from_uuid(&device_address,
200 IMMEDIATE_ALERT_UUID,
202 if (ret != BLUETOOTH_ERROR_NONE) {
203 TC_PRT(" bluetooth_gatt_get_service_from_uuid FAILED");
207 ret = bluetooth_gatt_get_char_from_uuid(service.handle,
208 ALERT_LEVEL_CHR_UUID);
209 if (ret != BLUETOOTH_ERROR_NONE) {
210 TC_PRT(" bluetooth_gatt_get_char_from_uuid FAILED");
216 TC_PRT("Proximity Link loss alert");
217 __accept_bdaddress(&device_address);
219 __accept_alert_level();
232 TC_PRT("bluetooth framework TC startup");
234 if (!g_thread_supported())
237 dbus_g_thread_init();
240 main_loop = g_main_loop_new(NULL, FALSE);
245 TC_PRT("bluetooth framework TC cleanup");
246 if ( main_loop != NULL)
247 g_main_loop_unref(main_loop);
250 static void __handle_alert_char(char *char_handle,
251 bt_gatt_char_property_t *char_pty)
253 if (char_pty->val == NULL)
254 TC_PRT("Read only char");
256 TC_PRT("Current Alert level [%d]", char_pty->val[0]);
258 g_alert_char_handle = g_strdup(char_handle);
260 if (bluetooth_gatt_set_characteristics_value(char_handle,
261 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
262 TC_PRT("Set char val failed");
266 static gboolean __handle_char(char *char_handle,
267 bt_gatt_char_property_t *char_pty)
269 TC_PRT("char uuid %s", char_pty->uuid);
271 if (g_strstr_len(char_pty->uuid, -1, ALERT_LEVEL_CHR_UUID) != NULL) {
272 TC_PRT("Alert char recieved");
273 __handle_alert_char(char_handle, char_pty);
275 } /* Add else if for other chars*/
280 void bt_event_callback(int event, bluetooth_event_param_t* param,
283 TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
284 TC_PRT("bt event callback 0x%04x", event);
286 case BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID:
288 TC_PRT("BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID");
289 if (param->result != 0) {
293 bt_gatt_char_property_t *char_pty = param->param_data;
295 __handle_char(char_pty->handle, char_pty);
300 TC_PRT("received event [0x%04x]", event);
303 TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
306 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
309 unsigned int len = 0;
312 if (g_io_channel_read_chars(chan, buf, sizeof(buf),
313 &len, NULL) == G_IO_STATUS_ERROR) {
314 printf("IO Channel read error");
323 g_idle_add(test_input_callback, (void*)test_id);
335 /* Register callback function */
336 TC_PRT("TC : %s", tc_table[0].tc_name);
337 ret_val = bluetooth_register_callback(bt_event_callback, NULL);
338 if (ret_val >= BLUETOOTH_ERROR_NONE) {
339 TC_PRT("bluetooth_register_callback returned Success");
340 tc_result(TC_PASS, 0);
342 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
343 tc_result(TC_FAIL, 0);
347 ret_val = bluetooth_check_adapter();
348 if (ret_val < BLUETOOTH_ERROR_NONE) {
349 TC_PRT("bluetooth_check_adapter returned failiure [0x%04x]", ret_val);
350 tc_result(TC_FAIL, 3);
352 TC_PRT("BT state [0x%04x]", ret_val);
353 tc_result(TC_PASS, 3);
356 key_io = g_io_channel_unix_new(fileno(stdin));
358 g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
360 g_io_channel_unref(key_io);
362 g_main_loop_run(main_loop);