2 * capi-network-bluetooth
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 bt_unit_test.c
26 * @brief This is the source file for capi unit test.
32 #include <dbus/dbus-glib.h>
34 #include "bluetooth.h"
37 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
38 #define TC_PRT(format, args...) PRT(format"\n", ##args)
40 const char *spp_uuid = "00001101-0000-1000-8000-00805F9B34FB";
41 const char *opp_uuid = "00001105-0000-1000-8000-00805f9b34fb";
46 GMainLoop *main_loop = NULL;
53 tc_table_t tc_table[] = {
54 /* Adapter functions */
55 {"bt_initialize" , 1},
56 {"bt_deinitialize" , 2},
57 {"bt_adapter_get_state" , 3},
58 {"bt_adapter_enable" , 4},
59 {"bt_adapter_disable" , 5},
60 {"bt_adapter_start_device_discovery" , 6},
61 {"bt_adapter_stop_device_discovery" , 7},
62 {"bt_adapter_is_discovering" , 8},
63 {"bt_adapter_get_bonded_device_info" , 9},
64 {"bt_adapter_is_service_used" , 10},
65 {"bt_adapter_set_device_discovery_state_changed_cb" , 11},
66 {"bt_adapter_unset_device_discovery_state_changed_cb" , 12},
68 /* Socket functions */
69 {"bt_socket_create_rfcomm" , 50},
70 {"bt_socket_destroy_rfcomm" , 51},
71 {"bt_socket_listen_and_accept_rfcomm" , 52},
72 {"bt_socket_listen" , 53},
73 {"bt_socket_accept" , 54},
74 {"bt_socket_reject" , 55},
75 {"bt_socket_connect_rfcomm" , 56},
76 {"bt_socket_disconnect_rfcomm" , 57},
77 {"bt_socket_send_data" , 58},
78 {"bt_socket_set_data_received_cb" , 59},
79 {"bt_socket_unset_data_received_cb" , 60},
80 {"bt_socket_set_connection_requested_cb" , 61},
81 {"bt_socket_unset_connection_requested_cb" , 62},
82 {"bt_socket_set_connection_state_changed_cb" , 63},
83 {"bt_socket_unset_connection_state_changed_cb" , 64},
86 {"bt_opp_client_initialize" , 70},
87 {"bt_opp_client_deinitialize" , 71},
88 {"bt_opp_client_add_file" , 72},
89 {"bt_opp_client_clear_files" , 73},
90 {"bt_opp_client_push_files" , 74},
91 {"bt_opp_client_cancel_push" , 75},
93 /* -----------------------------------------*/
98 void tc_usage_print(void)
102 while (tc_table[i].tc_name) {
103 if (tc_table[i].tc_code != 0x00ff) {
104 TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
105 tc_table[i].tc_name);
107 TC_PRT("Key %d : usage %s\n\n", 0x00ff,
108 tc_table[i].tc_name);
115 static void __bt_adapter_device_discovery_state_changed_cb(int result,
116 bt_adapter_device_discovery_state_e discovery_state,
117 bt_adapter_device_discovery_info_s *discovery_info,
122 TC_PRT("discovery_state: %d", discovery_state);
124 if (discovery_info == NULL) {
125 TC_PRT("No discovery_info!");
129 TC_PRT("remote_address: %s", discovery_info->remote_address);
130 TC_PRT("remote_name: %s", discovery_info->remote_name);
131 TC_PRT("rssi: %d", discovery_info->rssi);
132 TC_PRT("is_bonded: %d", discovery_info->is_bonded);
133 TC_PRT("service_count: %d", discovery_info->service_count);
135 if (discovery_info->service_uuid == NULL) {
140 for (i = 0; i < discovery_info->service_count; i++) {
141 TC_PRT("uuid: %s", discovery_info->service_uuid[i]);
145 static void __bt_socket_data_received_cb(bt_socket_received_data_s *data, void *user_data)
150 TC_PRT("No recieved data!");
154 TC_PRT("Socket fd: %d", data->socket_fd);
155 TC_PRT("Data: %s", data->data);
156 TC_PRT("Size: %d", data->data_size);
159 static void __bt_socket_connection_requested_cb(int socket_fd, const char *remote_address, void *user_data)
161 TC_PRT("Socket fd: %d", socket_fd);
162 TC_PRT("remote_address: %s", remote_address);
165 static void __bt_socket_connection_state_changed_cb(int result,
166 bt_socket_connection_state_e connection_state,
167 bt_socket_connection_s *connection,
170 TC_PRT("result: %d", result);
171 TC_PRT("connection_state: %d", connection_state);
173 if (connection == NULL) {
174 TC_PRT("No connection data!");
178 TC_PRT("socket fd: %d", connection->socket_fd);
179 TC_PRT("role: %d", connection->local_role);
180 TC_PRT("remote address: %s", connection->remote_address);
181 TC_PRT("service_uuid: %s", connection->service_uuid);
184 void __bt_opp_client_push_responded_cb(int result,
185 const char *remote_address,
188 TC_PRT("result: %d", result);
189 TC_PRT("remote_address: %s", remote_address);
192 void __bt_opp_client_push_progress_cb(const char *file,
197 TC_PRT("size: %ld", (long)size);
198 TC_PRT("percent: %d", percent);
199 TC_PRT("file: %s", file);
202 void __bt_opp_client_push_finished_cb(int result,
203 const char *remote_address,
206 TC_PRT("result: %d", result);
207 TC_PRT("remote_address: %s", remote_address);
211 int test_input_callback(void *data)
214 int test_id = (int)data;
219 g_main_loop_quit(main_loop);
223 ret = bt_initialize();
224 if (ret < BT_ERROR_NONE)
225 TC_PRT("failed with [0x%04x]", ret);
228 ret = bt_deinitialize();
229 if (ret < BT_ERROR_NONE)
230 TC_PRT("failed with [0x%04x]", ret);
233 bt_adapter_state_e state = BT_ADAPTER_DISABLED;
235 ret = bt_adapter_get_state(&state);
236 if (ret < BT_ERROR_NONE)
237 TC_PRT("failed with [0x%04x]", ret);
239 TC_PRT("state: %d", state);
243 ret = bt_adapter_enable();
244 if (ret < BT_ERROR_NONE)
245 TC_PRT("failed with [0x%04x]", ret);
248 ret = bt_adapter_disable();
249 if (ret < BT_ERROR_NONE)
250 TC_PRT("failed with [0x%04x]", ret);
253 ret = bt_adapter_start_device_discovery();
254 if (ret < BT_ERROR_NONE)
255 TC_PRT("failed with [0x%04x]", ret);
258 ret = bt_adapter_stop_device_discovery();
259 if (ret < BT_ERROR_NONE)
260 TC_PRT("failed with [0x%04x]", ret);
263 bool is_discovering = FALSE;
264 ret = bt_adapter_is_discovering(&is_discovering);
265 if (ret < BT_ERROR_NONE)
266 TC_PRT("failed with [0x%04x]", ret);
268 TC_PRT("is_discovering: %d", is_discovering);
274 bt_device_info_s *device_info = NULL;
276 address = g_strdup("00:19:0E:01:61:17");
278 ret = bt_adapter_get_bonded_device_info((const char *)address,
280 if (ret < BT_ERROR_NONE)
281 TC_PRT("failed with [0x%04x]", ret);
286 TC_PRT("address: %s", device_info->remote_address);
287 TC_PRT("name: %s", device_info->remote_name);
290 bt_adapter_free_device_info(device_info);
296 ret = bt_adapter_is_service_used(opp_uuid, &used);
297 if (ret < BT_ERROR_NONE)
298 TC_PRT("failed with [0x%04x]", ret);
300 TC_PRT("used: %d", used);
304 ret = bt_adapter_set_device_discovery_state_changed_cb(__bt_adapter_device_discovery_state_changed_cb, NULL);
305 if (ret < BT_ERROR_NONE)
306 TC_PRT("failed with [0x%04x]", ret);
309 ret = bt_adapter_unset_device_discovery_state_changed_cb();
310 if (ret < BT_ERROR_NONE)
311 TC_PRT("failed with [0x%04x]", ret);
314 /* Socket functions */
318 ret = bt_socket_create_rfcomm(spp_uuid, &socket_fd);
319 if (ret < BT_ERROR_NONE) {
320 TC_PRT("failed with [0x%04x]", ret);
322 TC_PRT("socket_fd: %d", socket_fd);
323 server_fd = socket_fd;
328 ret = bt_socket_destroy_rfcomm(server_fd);
329 if (ret < BT_ERROR_NONE)
330 TC_PRT("failed with [0x%04x]", ret);
333 ret = bt_socket_listen_and_accept_rfcomm(server_fd, 1);
334 if (ret < BT_ERROR_NONE)
335 TC_PRT("failed with [0x%04x]", ret);
338 ret = bt_socket_listen(server_fd, 1);
339 if (ret < BT_ERROR_NONE)
340 TC_PRT("failed with [0x%04x]", ret);
345 ret = bt_socket_accept(server_fd, &socket_fd);
346 if (ret < BT_ERROR_NONE) {
347 TC_PRT("failed with [0x%04x]", ret);
349 TC_PRT("socket_fd: %d", socket_fd);
350 client_fd = socket_fd;
355 ret = bt_socket_reject(server_fd);
356 if (ret < BT_ERROR_NONE) {
357 TC_PRT("failed with [0x%04x]", ret);
363 address = g_strdup("00:02:48:F4:3E:D2");
365 ret = bt_socket_connect_rfcomm(address, spp_uuid);
366 if (ret < BT_ERROR_NONE) {
367 TC_PRT("failed with [0x%04x]", ret);
374 ret = bt_socket_disconnect_rfcomm(client_fd);
375 if (ret < BT_ERROR_NONE) {
376 TC_PRT("failed with [0x%04x]", ret);
380 ret = bt_socket_send_data(client_fd, "Sending test\0", 20);
381 if (ret < BT_ERROR_NONE) {
382 TC_PRT("failed with [0x%04x]", ret);
386 ret = bt_socket_set_data_received_cb(__bt_socket_data_received_cb, NULL);
387 if (ret < BT_ERROR_NONE) {
388 TC_PRT("failed with [0x%04x]", ret);
392 ret = bt_socket_unset_data_received_cb();
393 if (ret < BT_ERROR_NONE) {
394 TC_PRT("failed with [0x%04x]", ret);
398 ret = bt_socket_set_connection_requested_cb(__bt_socket_connection_requested_cb, NULL);
399 if (ret < BT_ERROR_NONE) {
400 TC_PRT("failed with [0x%04x]", ret);
404 ret = bt_socket_unset_connection_requested_cb();
405 if (ret < BT_ERROR_NONE) {
406 TC_PRT("failed with [0x%04x]", ret);
410 ret = bt_socket_set_connection_state_changed_cb(__bt_socket_connection_state_changed_cb, NULL);
411 if (ret < BT_ERROR_NONE) {
412 TC_PRT("failed with [0x%04x]", ret);
416 ret = bt_socket_unset_connection_state_changed_cb();
417 if (ret < BT_ERROR_NONE) {
418 TC_PRT("failed with [0x%04x]", ret);
422 ret = bt_opp_client_initialize();
423 if (ret < BT_ERROR_NONE) {
424 TC_PRT("failed with [0x%04x]", ret);
428 ret = bt_opp_client_deinitialize();
429 if (ret < BT_ERROR_NONE) {
430 TC_PRT("failed with [0x%04x]", ret);
434 ret = bt_opp_client_add_file("/opt/media/Images/image1.jpg");
435 if (ret < BT_ERROR_NONE) {
436 TC_PRT("failed with [0x%04x]", ret);
440 ret = bt_opp_client_clear_files();
441 if (ret < BT_ERROR_NONE) {
442 TC_PRT("failed with [0x%04x]", ret);
448 address = g_strdup("00:02:37:A9:17:9E");
450 ret = bt_opp_client_push_files(address,__bt_opp_client_push_responded_cb,
451 __bt_opp_client_push_progress_cb,
452 __bt_opp_client_push_finished_cb,
454 if (ret < BT_ERROR_NONE) {
455 TC_PRT("failed with [0x%04x]", ret);
460 ret = bt_opp_client_cancel_push();
461 if (ret < BT_ERROR_NONE) {
462 TC_PRT("failed with [0x%04x]", ret);
473 static gboolean key_event_cb(GIOChannel *chan,
477 char buf[BUFFER_LEN] = { 0 };
479 unsigned int len = 0;
482 memset(buf, 0, sizeof(buf));
484 if(g_io_channel_read_chars(chan, buf, sizeof(buf),
485 &len, NULL) == G_IO_STATUS_ERROR) {
486 TC_PRT("IO Channel read error");
496 g_idle_add(test_input_callback, (void *)test_id);
507 key_io = g_io_channel_unix_new(fileno(stdin));
509 g_io_channel_set_encoding(key_io, NULL, NULL);
510 g_io_channel_set_flags(key_io, G_IO_FLAG_NONBLOCK, NULL);
512 g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
515 g_io_channel_unref(key_io);
517 main_loop = g_main_loop_new(NULL, FALSE);
519 g_main_loop_run(main_loop);