Add test application codes
[framework/api/bluetooth.git] / test / bt_unit_test.c
1 /*
2  * capi-network-bluetooth
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
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
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23
24 /**
25  * @file       bt_unit_test.c
26  * @brief      This is the source file for capi unit test.
27  */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <dbus/dbus-glib.h>
33
34 #include "bluetooth.h"
35
36 #define BUFFER_LEN 10
37 #define PRT(format, args...) printf("%s:%d() "format, __FUNCTION__, __LINE__, ##args)
38 #define TC_PRT(format, args...) PRT(format"\n", ##args)
39
40 const char *spp_uuid = "00001101-0000-1000-8000-00805F9B34FB";
41 const char *opp_uuid = "00001105-0000-1000-8000-00805f9b34fb";
42
43 static int server_fd;
44 static int client_fd;
45
46 GMainLoop *main_loop = NULL;
47
48 typedef struct {
49         const char *tc_name;
50         int tc_code;
51 } tc_table_t;
52
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},
67
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},
84
85         /* OPP functions */
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},
92
93         /* -----------------------------------------*/
94         {"Finish"                               , 0x00ff},
95         {NULL                                   , 0x0000},
96 };
97
98 void tc_usage_print(void)
99 {
100         int i = 0;
101
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);
106                 } else {
107                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
108                                                 tc_table[i].tc_name);
109                 }
110
111                 i++;
112         }
113 }
114
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,
118                                 void *user_data)
119 {
120         int i;
121
122         TC_PRT("discovery_state: %d", discovery_state);
123
124         if (discovery_info == NULL) {
125                 TC_PRT("No discovery_info!");
126                 return;
127         }
128
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);
134
135         if (discovery_info->service_uuid == NULL) {
136                 TC_PRT("No uuids");
137                 return;
138         }
139
140         for (i = 0; i < discovery_info->service_count; i++) {
141                 TC_PRT("uuid: %s", discovery_info->service_uuid[i]);
142         }
143 }
144
145 static void __bt_socket_data_received_cb(bt_socket_received_data_s *data, void *user_data)
146 {
147         TC_PRT("+");
148
149         if (data == NULL) {
150                 TC_PRT("No recieved data!");
151                 return;
152         }
153
154         TC_PRT("Socket fd: %d", data->socket_fd);
155         TC_PRT("Data: %s", data->data);
156         TC_PRT("Size: %d", data->data_size);
157 }
158
159 static void __bt_socket_connection_requested_cb(int socket_fd, const char *remote_address, void *user_data)
160 {
161         TC_PRT("Socket fd: %d", socket_fd);
162         TC_PRT("remote_address: %s", remote_address);
163 }
164
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,
168                                 void *user_data)
169 {
170         TC_PRT("result: %d", result);
171         TC_PRT("connection_state: %d", connection_state);
172
173         if (connection == NULL) {
174                 TC_PRT("No connection data!");
175                 return;
176         }
177
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);
182 }
183
184 void __bt_opp_client_push_responded_cb(int result,
185                                         const char *remote_address,
186                                         void *user_data)
187 {
188         TC_PRT("result: %d", result);
189         TC_PRT("remote_address: %s", remote_address);
190 }
191
192 void __bt_opp_client_push_progress_cb(const char *file,
193                                         long long size,
194                                         int percent,
195                                         void *user_data)
196 {
197         TC_PRT("size: %ld", (long)size);
198         TC_PRT("percent: %d", percent);
199         TC_PRT("file: %s", file);
200 }
201
202 void __bt_opp_client_push_finished_cb(int result,
203                                 const char *remote_address,
204                                 void *user_data)
205 {
206         TC_PRT("result: %d", result);
207         TC_PRT("remote_address: %s", remote_address);
208 }
209
210
211 int test_input_callback(void *data)
212 {
213         int ret = 0;
214         int test_id = (int)data;
215
216         switch (test_id) {
217         case 0x00ff:
218                 TC_PRT("Finished");
219                 g_main_loop_quit(main_loop);
220                 break;
221
222         case 1:
223                 ret = bt_initialize();
224                 if (ret < BT_ERROR_NONE)
225                         TC_PRT("failed with [0x%04x]", ret);
226                 break;
227         case 2:
228                 ret = bt_deinitialize();
229                 if (ret < BT_ERROR_NONE)
230                         TC_PRT("failed with [0x%04x]", ret);
231                 break;
232         case 3: {
233                 bt_adapter_state_e state = BT_ADAPTER_DISABLED;
234
235                 ret = bt_adapter_get_state(&state);
236                 if (ret < BT_ERROR_NONE)
237                         TC_PRT("failed with [0x%04x]", ret);
238
239                 TC_PRT("state: %d", state);
240                 break;
241         }
242         case 4:
243                 ret = bt_adapter_enable();
244                 if (ret < BT_ERROR_NONE)
245                         TC_PRT("failed with [0x%04x]", ret);
246                 break;
247         case 5:
248                 ret = bt_adapter_disable();
249                 if (ret < BT_ERROR_NONE)
250                         TC_PRT("failed with [0x%04x]", ret);
251                 break;
252         case 6:
253                 ret = bt_adapter_start_device_discovery();
254                 if (ret < BT_ERROR_NONE)
255                         TC_PRT("failed with [0x%04x]", ret);
256                 break;
257         case 7:
258                 ret = bt_adapter_stop_device_discovery();
259                 if (ret < BT_ERROR_NONE)
260                         TC_PRT("failed with [0x%04x]", ret);
261                 break;
262         case 8: {
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);
267                 else
268                         TC_PRT("is_discovering: %d", is_discovering);
269
270                 break;
271         }
272         case 9: {
273                 char *address;
274                 bt_device_info_s *device_info = NULL;
275
276                 address = g_strdup("00:19:0E:01:61:17");
277
278                 ret = bt_adapter_get_bonded_device_info((const char *)address,
279                                                         &device_info);
280                 if (ret < BT_ERROR_NONE)
281                         TC_PRT("failed with [0x%04x]", ret);
282
283                 g_free(address);
284
285                 if (device_info) {
286                         TC_PRT("address: %s", device_info->remote_address);
287                         TC_PRT("name: %s", device_info->remote_name);
288                 }
289
290                 bt_adapter_free_device_info(device_info);
291                 break;
292         }
293         case 10: {
294                 bool used = FALSE;
295
296                 ret = bt_adapter_is_service_used(opp_uuid, &used);
297                 if (ret < BT_ERROR_NONE)
298                         TC_PRT("failed with [0x%04x]", ret);
299
300                 TC_PRT("used: %d", used);
301                 break;
302         }
303         case 11:
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);
307                 break;
308         case 12:
309                 ret = bt_adapter_unset_device_discovery_state_changed_cb();
310                 if (ret < BT_ERROR_NONE)
311                         TC_PRT("failed with [0x%04x]", ret);
312                 break;
313
314         /* Socket functions */
315         case 50: {
316                 int socket_fd = 0;
317
318                 ret = bt_socket_create_rfcomm(spp_uuid, &socket_fd);
319                 if (ret < BT_ERROR_NONE) {
320                         TC_PRT("failed with [0x%04x]", ret);
321                 } else {
322                         TC_PRT("socket_fd: %d", socket_fd);
323                         server_fd = socket_fd;
324                 }
325                 break;
326         }
327         case 51:
328                 ret = bt_socket_destroy_rfcomm(server_fd);
329                 if (ret < BT_ERROR_NONE)
330                         TC_PRT("failed with [0x%04x]", ret);
331                 break;
332         case 52:
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);
336                 break;
337         case 53:
338                 ret = bt_socket_listen(server_fd, 1);
339                 if (ret < BT_ERROR_NONE)
340                         TC_PRT("failed with [0x%04x]", ret);
341                 break;
342         case 54: {
343                 int socket_fd = 0;
344
345                 ret = bt_socket_accept(server_fd, &socket_fd);
346                 if (ret < BT_ERROR_NONE) {
347                         TC_PRT("failed with [0x%04x]", ret);
348                 } else {
349                         TC_PRT("socket_fd: %d", socket_fd);
350                         client_fd = socket_fd;
351                 }
352                 break;
353         }
354         case 55:
355                 ret = bt_socket_reject(server_fd);
356                 if (ret < BT_ERROR_NONE) {
357                         TC_PRT("failed with [0x%04x]", ret);
358                 }
359                 break;
360         case 56: {
361                 char *address;
362
363                 address = g_strdup("00:02:48:F4:3E:D2");
364
365                 ret = bt_socket_connect_rfcomm(address, spp_uuid);
366                 if (ret < BT_ERROR_NONE) {
367                         TC_PRT("failed with [0x%04x]", ret);
368                 }
369
370                 g_free(address);
371                 break;
372         }
373         case 57:
374                 ret = bt_socket_disconnect_rfcomm(client_fd);
375                 if (ret < BT_ERROR_NONE) {
376                         TC_PRT("failed with [0x%04x]", ret);
377                 }
378                 break;
379         case 58:
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);
383                 }
384                 break;
385         case 59:
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);
389                 }
390                 break;
391         case 60:
392                 ret = bt_socket_unset_data_received_cb();
393                 if (ret < BT_ERROR_NONE) {
394                         TC_PRT("failed with [0x%04x]", ret);
395                 }
396                 break;
397         case 61:
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);
401                 }
402                 break;
403         case 62:
404                 ret = bt_socket_unset_connection_requested_cb();
405                 if (ret < BT_ERROR_NONE) {
406                         TC_PRT("failed with [0x%04x]", ret);
407                 }
408                 break;
409         case 63:
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);
413                 }
414                 break;
415         case 64:
416                 ret = bt_socket_unset_connection_state_changed_cb();
417                 if (ret < BT_ERROR_NONE) {
418                         TC_PRT("failed with [0x%04x]", ret);
419                 }
420                 break;
421         case 70:
422                 ret = bt_opp_client_initialize();
423                 if (ret < BT_ERROR_NONE) {
424                         TC_PRT("failed with [0x%04x]", ret);
425                 }
426                 break;
427         case 71:
428                 ret = bt_opp_client_deinitialize();
429                 if (ret < BT_ERROR_NONE) {
430                         TC_PRT("failed with [0x%04x]", ret);
431                 }
432                 break;
433         case 72:
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);
437                 }
438                 break;
439         case 73:
440                 ret = bt_opp_client_clear_files();
441                 if (ret < BT_ERROR_NONE) {
442                         TC_PRT("failed with [0x%04x]", ret);
443                 }
444                 break;
445         case 74: {
446                 char *address;
447
448                 address = g_strdup("00:02:37:A9:17:9E");
449
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,
453                                         NULL);
454                 if (ret < BT_ERROR_NONE) {
455                         TC_PRT("failed with [0x%04x]", ret);
456                 }
457                 break;
458         }
459         case 75:
460                 ret = bt_opp_client_cancel_push();
461                 if (ret < BT_ERROR_NONE) {
462                         TC_PRT("failed with [0x%04x]", ret);
463                 }
464                 break;
465
466         default:
467                 break;
468         }
469
470         return 0;
471 }
472
473 static gboolean key_event_cb(GIOChannel *chan,
474                                 GIOCondition cond,
475                                 gpointer data)
476 {
477         char buf[BUFFER_LEN] = { 0 };
478
479         unsigned int len = 0;
480         int test_id;
481
482         memset(buf, 0, sizeof(buf));
483
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");
487                 return FALSE;
488         }
489
490         TC_PRT("%s", buf);
491         tc_usage_print();
492
493         test_id = atoi(buf);
494
495         if(test_id)
496                 g_idle_add(test_input_callback, (void *)test_id);
497
498         return TRUE;
499 }
500
501 int main()
502 {
503         GIOChannel *key_io;
504
505         g_type_init();
506
507         key_io = g_io_channel_unix_new(fileno(stdin));
508
509         g_io_channel_set_encoding(key_io, NULL, NULL);
510         g_io_channel_set_flags(key_io, G_IO_FLAG_NONBLOCK, NULL);
511
512         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
513                         key_event_cb, NULL);
514
515         g_io_channel_unref(key_io);
516
517         main_loop = g_main_loop_new(NULL, FALSE);
518
519         g_main_loop_run(main_loop);
520
521         bt_deinitialize();
522
523         return 0;
524 }
525