Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / test / telephony / bluetooth-telephony-test.c
1 /*
2  * Bluetooth-telephony
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       bluetooth-telephony-test.c
26  * @brief      This is the source file for bluetooth telephony test suite.
27  */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <dbus/dbus-glib.h>
33 #include <pthread.h>
34
35 #include "bluetooth-telephony-api.h"
36
37
38 #define PRT(format, args...) printf("%s:%d() "format, \
39                         __FUNCTION__, __LINE__, ##args)
40 #define TC_PRT(format, args...) PRT(format"\n", ##args)
41
42 GMainLoop *main_loop = NULL;
43 static int timeout_status = 0;
44 #define DEFAULT_CALL_ID 1
45 /*Change this number with Testing SIM*/
46 #define TEST_NUMBER "9986008917"
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         /*Telephony Application*/
55         {"bluetooth_telephony_init", 70},
56         {"bluetooth_telephony_deinit", 71},
57         {"Indicate Outgoing call", 72},
58         {"Indicate Incoming call", 73},
59         {"Speaker to Headphone", 74},
60         {"Headphone to Speaker ", 75},
61         {"Call End/Release", 76},
62         {"Call Hold", 77},
63         {"bluetooth_telephony_call_remote_ringing", 78},
64         {"Call Swap", 79},
65         {"Call Reject", 80},
66         {"Call Answer", 81},
67         {"Is SCO channel connected", 82},
68         {"Voice Recognition Start", 83},
69         {"Voice Recognition Stop", 84},
70         {"NREC Status", 85},
71         {"WBS Status", 86},
72         {"Vendor dep AT CMD [+XSAT: 11,DUAL,DUAL]", 87},
73
74         /* -----------------------------------------*/
75         {"Finish", 0x00ff},
76         {NULL, 0x0000},
77
78 };
79
80 #define tc_result(success, tc_index) \
81         TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, \
82                         tc_table[tc_index].tc_name, \
83                         ((success == TC_PASS) ? "Success" : "Failed"));
84
85 void tc_usage_print(void)
86 {
87         int i = 0;
88
89         while (tc_table[i].tc_name) {
90                 if (tc_table[i].tc_code != 0x00ff) {
91                         TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
92                                                         tc_table[i].tc_name);
93                 } else {
94                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
95                                                         tc_table[i].tc_name);
96                 }
97
98                 i++;
99         }
100 }
101
102 void telephony_event_handler(int event, void *data, void *user_data)
103 {
104         telephony_event_param_t *bt_event;
105
106         if (data == NULL)
107                 return;
108         bt_event = data;
109
110         TC_PRT("AG event : [0x%04x]", event);
111
112         switch (event) {
113         case BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL:
114                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL");
115                 bluetooth_telephony_call_answered(DEFAULT_CALL_ID, TRUE);
116                 break;
117
118         case BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL:
119                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL");
120                 bluetooth_telephony_call_end(DEFAULT_CALL_ID);
121                 break;
122
123         case BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL:
124                 bluetooth_telephony_call_end(DEFAULT_CALL_ID);
125                 break;
126
127         case BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL:
128                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL");
129                 break;
130
131         case BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL:
132                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL");
133                 break;
134
135         case BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL:
136                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL");
137                 break;
138
139         case BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL:
140                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL");
141                 break;
142
143         case BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF:
144                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF");
145                 break;
146
147         case BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER:
148                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER");
149                 break;
150
151         case BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED: {
152                 gboolean *nrec;
153                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED");
154                 nrec = bt_event->param_data;
155                 TC_PRT("NREC status = [%d]", *nrec);
156                 break;
157         case BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD:
158                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD %s", bt_event->param_data);
159                 break;
160         }
161
162         default:
163                 break;
164         }
165 }
166
167 int test_input_callback(void *data)
168 {
169         int test_id = (int)data;
170
171         switch (test_id) {
172                 case 0x00ff:
173                         TC_PRT("Finished");
174                         g_main_loop_quit(main_loop);
175                         break;
176
177                 case 70:
178                         bluetooth_telephony_init(telephony_event_handler, NULL);
179                         break;
180                 case 71:
181                         bluetooth_telephony_deinit();
182                         break;
183
184                 case 72:
185                         bluetooth_telephony_indicate_outgoing_call(
186                                         TEST_NUMBER, DEFAULT_CALL_ID, TRUE);
187                         break;
188                 case 73:
189                         bluetooth_telephony_indicate_incoming_call(
190                                         TEST_NUMBER, TRUE);
191                         break;
192                 case 74:
193                         bluetooth_telephony_audio_open();
194                         break;
195                 case 75:
196                         bluetooth_telephony_audio_close();
197                         break;
198                 case 76:
199                         bluetooth_telephony_call_end(DEFAULT_CALL_ID);
200                         break;
201                 case 77:
202                         bluetooth_telephony_call_held(DEFAULT_CALL_ID);
203                         break;
204                 case 78:
205                         bluetooth_telephony_call_remote_ringing(
206                                                         DEFAULT_CALL_ID);
207                         break;
208                 case 79:
209                         TC_PRT("bluetooth_telephony_call_swapped  \n");
210                         break;
211                 case 80:
212                         bluetooth_telephony_call_answered(
213                                                         DEFAULT_CALL_ID, FALSE);
214                         break;
215                 case 81:
216                         bluetooth_telephony_call_answered(
217                                                         DEFAULT_CALL_ID, TRUE);
218                         break;
219
220                 case 82: {
221                         int state;
222
223                         state = bluetooth_telephony_is_sco_connected();
224
225                         TC_PRT("State = %d \n", state);
226                         break;
227                 }
228
229                 case 83: {
230                         int ret = 0;
231
232                         TC_PRT("**********************\n");
233                         TC_PRT("           PLEASE SPEAK          \n");
234                         TC_PRT("**********************\n");
235
236                         ret = bluetooth_telephony_start_voice_recognition();
237
238                         if (ret == BLUETOOTH_TELEPHONY_ERROR_NONE) {
239                                 TC_PRT("No error\n");
240                                 bluetooth_telephony_audio_open();
241                         }
242                         break;
243                 }
244
245                 case 84: {
246                         TC_PRT("Rcognition finished \n");
247                         bluetooth_telephony_audio_close();
248                         bluetooth_telephony_stop_voice_recognition();
249                         break;
250                 }
251
252                 case 85: {
253                         int ret;
254                         gboolean status = FALSE;
255
256                         ret = bluetooth_telephony_is_nrec_enabled(&status);
257
258                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
259                                 TC_PRT("Error getting NREC Status\n");
260
261                         TC_PRT("NREC status = %d\n", status);
262                         break;
263                 }
264
265                 case 86: {
266                         int ret;
267                         gboolean status = FALSE;
268
269                         ret = bluetooth_telephony_is_wbs_mode(&status);
270
271                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
272                                 TC_PRT("Error getting WBS Status\n");
273
274                         TC_PRT("WBS status = %d\n", status);
275                         break;
276                 }
277                 case 87: {
278                         int ret = 0;
279
280                         TC_PRT("Vendor dependent AT Command\n");
281
282                         ret = bluetooth_telephony_send_vendor_cmd("+XSAT: 11,DUAL,DUAL");
283
284                         if (ret == BLUETOOTH_TELEPHONY_ERROR_NONE) {
285                                 TC_PRT("No error\n");
286                         }
287                         break;
288                 }
289                 default:
290                         break;
291         }
292
293         return 0;
294 }
295
296 void startup()
297 {
298         TC_PRT("bluetooth framework TC startup");
299
300         if (!g_thread_supported()) {
301                 g_thread_init(NULL);
302         }
303
304         dbus_g_thread_init();
305
306         g_type_init();
307         main_loop = g_main_loop_new(NULL, FALSE);
308 }
309
310 void cleanup()
311 {
312         TC_PRT("bluetooth framework TC cleanup");
313         if (main_loop != NULL) {
314                 g_main_loop_unref(main_loop);
315         }
316 }
317
318 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond ,
319                                                         gpointer data)
320 {
321         char buf[10] = {0};
322
323         unsigned int len = 0;
324         int test_id;
325         memset(buf, 0, sizeof(buf));
326
327         if (g_io_channel_read(chan, buf, sizeof(buf), &len) !=
328                                         G_IO_ERROR_NONE) {
329
330                 printf("IO Channel read error");
331                 return FALSE;
332
333         }
334         printf("%s\n", buf);
335         tc_usage_print();
336
337         test_id = atoi(buf);
338
339         if (test_id)
340                 g_idle_add(test_input_callback, (void *)test_id);
341
342         return TRUE;
343 }
344
345 int main()
346 {
347         startup();
348
349         GIOChannel *key_io;
350         key_io = g_io_channel_unix_new(fileno(stdin));
351
352         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
353                         key_event_cb, NULL);
354         g_io_channel_unref(key_io);
355
356
357         g_main_loop_run(main_loop);
358
359         cleanup();
360         return 0;
361 }