2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file bluetooth-gatt-test.c
20 * @brief This is the source file for bluetooth framework test suite.
31 #include "bluetooth-api.h"
34 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
35 #define TC_PRT(format, args...) PRT(format"\n", ##args)
40 GMainLoop *main_loop = NULL;
47 tc_table_t tc_table[] = {
48 {"Send alert to remote le device" , 1},
49 {"Set Link loss alert" , 2},
51 /* -----------------------------------------*/
57 #define tc_result(success, tc_index) \
58 TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, tc_table[tc_index].tc_name, ((success == TC_PASS) ? "Success" : "Failed"));
60 char *g_alert_char_handle = NULL;
61 guint8 g_alert_level = 0;
63 #define IMMEDIATE_ALERT_UUID "00001802-0000-1000-8000-00805f9b34fb"
64 #define LINK_LOSS_UUID "00001803-0000-1000-8000-00805f9b34fb"
65 #define ALERT_LEVEL_CHR_UUID "2a06"
67 #define BD_ADDR_FILE "/opt/remote-bd"
69 void tc_usage_print(void)
73 while (tc_table[i].tc_name) {
74 if (tc_table[i].tc_code != 0x00ff) {
75 TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
78 TC_PRT("Key %d : usage %s\n\n", 0x00ff,
86 static void convert_addr_string_to_addr_type(bluetooth_device_address_t *addr,
89 char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
91 if (!address || !addr)
94 addr->addr[0] = strtol(address, &ptr5, 16);
95 addr->addr[1] = strtol(ptr5 + 1, &ptr4, 16);
96 addr->addr[2] = strtol(ptr4 + 1, &ptr3, 16);
97 addr->addr[3] = strtol(ptr3 + 1, &ptr2, 16);
98 addr->addr[4] = strtol(ptr2 + 1, &ptr1, 16);
99 addr->addr[5] = strtol(ptr1 + 1, NULL, 16);
102 char * get_bd_from_file(char *filename)
107 if ((fd = open(filename, O_RDONLY)) < 0) {
108 perror("Can't open file");
114 if (read(fd, buf, 17) < 17) {
115 perror("Can't load firmware");
126 static void __accept_bdaddress(bluetooth_device_address_t *device_address)
128 char str_address[20] = {0};
131 addr = get_bd_from_file(BD_ADDR_FILE);
133 TC_PRT("Remote bd address from file: %s", addr);
134 convert_addr_string_to_addr_type(device_address, addr);
139 TC_PRT("Enter bd address: ");
141 ret = scanf("%s", str_address);
143 TC_PRT("Some read error");
144 TC_PRT("You have entered bd address %s\n", str_address);
145 convert_addr_string_to_addr_type(device_address, str_address);
148 static void __accept_alert_level()
150 TC_PRT("Enter alert level \n 0 - no alert 1 - mild alert 2 - High alert : ");
152 ret = scanf("%c", &g_alert_level);
154 TC_PRT("Some read error");
155 TC_PRT("You have selected alert level %hu ", g_alert_level);
158 int test_input_callback(void *data)
161 int test_id = (int)data;
162 bluetooth_device_address_t device_address;
163 bt_gatt_service_property_t service;
168 g_main_loop_quit(main_loop);
171 TC_PRT("Immediate Alert");
172 __accept_bdaddress(&device_address);
174 __accept_alert_level();
176 if (g_alert_char_handle) {
177 if (bluetooth_gatt_set_characteristics_value(g_alert_char_handle,
178 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
179 TC_PRT("Set char val failed");
184 ret = bluetooth_gatt_get_service_from_uuid(&device_address,
185 IMMEDIATE_ALERT_UUID,
187 if (ret != BLUETOOTH_ERROR_NONE) {
188 TC_PRT(" bluetooth_gatt_get_service_from_uuid FAILED");
192 ret = bluetooth_gatt_get_char_from_uuid(service.handle,
193 ALERT_LEVEL_CHR_UUID);
194 if (ret != BLUETOOTH_ERROR_NONE) {
195 TC_PRT(" bluetooth_gatt_get_char_from_uuid FAILED");
201 TC_PRT("Proximity Link loss alert");
202 __accept_bdaddress(&device_address);
204 __accept_alert_level();
217 TC_PRT("bluetooth framework TC startup");
219 dbus_threads_init_default();
221 main_loop = g_main_loop_new(NULL, FALSE);
226 TC_PRT("bluetooth framework TC cleanup");
227 if (main_loop != NULL)
228 g_main_loop_unref(main_loop);
231 static void __handle_alert_char(char *char_handle,
232 bt_gatt_char_property_t *char_pty)
234 if (char_pty->val == NULL)
235 TC_PRT("Read only char");
237 TC_PRT("Current Alert level [%d]", char_pty->val[0]);
239 g_alert_char_handle = g_strdup(char_handle);
241 if (bluetooth_gatt_set_characteristics_value(char_handle,
242 &g_alert_level, 1) != BLUETOOTH_ERROR_NONE)
243 TC_PRT("Set char val failed");
247 static gboolean __handle_char(char *char_handle,
248 bt_gatt_char_property_t *char_pty)
250 TC_PRT("char uuid %s", char_pty->uuid);
252 if (g_strstr_len(char_pty->uuid, -1, ALERT_LEVEL_CHR_UUID) != NULL) {
253 TC_PRT("Alert char recieved");
254 __handle_alert_char(char_handle, char_pty);
256 } /* Add else if for other chars*/
261 void bt_event_callback(int event, bluetooth_event_param_t* param,
264 TC_PRT(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
265 TC_PRT("bt event callback 0x%04x", event);
267 case BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID:
269 TC_PRT("BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID");
270 if (param->result != 0) {
274 bt_gatt_char_property_t *char_pty = param->param_data;
276 __handle_char(char_pty->handle, char_pty);
281 TC_PRT("received event [0x%04x]", event);
284 TC_PRT("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
287 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
290 unsigned int len = 0;
293 if (g_io_channel_read_chars(chan, buf, sizeof(buf),
294 &len, NULL) == G_IO_STATUS_ERROR) {
295 printf("IO Channel read error");
304 g_idle_add(test_input_callback, (void*)test_id);
316 /* Register callback function */
317 TC_PRT("TC : %s", tc_table[0].tc_name);
318 ret_val = bluetooth_register_callback(bt_event_callback, NULL);
319 if (ret_val >= BLUETOOTH_ERROR_NONE) {
320 TC_PRT("bluetooth_register_callback returned Success");
321 tc_result(TC_PASS, 0);
323 TC_PRT("bluetooth_register_callback returned failiure [0x%04x]", ret_val);
324 tc_result(TC_FAIL, 0);
328 ret_val = bluetooth_check_adapter();
329 if (ret_val < BLUETOOTH_ERROR_NONE) {
330 TC_PRT("bluetooth_check_adapter returned failiure [0x%04x]", ret_val);
331 tc_result(TC_FAIL, 3);
333 TC_PRT("BT state [0x%04x]", ret_val);
334 tc_result(TC_PASS, 3);
337 key_io = g_io_channel_unix_new(fileno(stdin));
339 g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
341 g_io_channel_unref(key_io);
343 g_main_loop_run(main_loop);