Rollback changes to submit TIZEN:COMMON project
[platform/core/connectivity/bluetooth-frwk.git] / test / telephony / bluetooth-telephony-test.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /**
21  * @file       bluetooth-telephony-test.c
22  * @brief      This is the source file for bluetooth telephony test suite.
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <dbus/dbus-glib.h>
29 #include <pthread.h>
30
31 #include "bluetooth-telephony-api.h"
32
33
34 #define PRT(format, args...) printf("%s:%d() "format, \
35                         __FUNCTION__, __LINE__, ##args)
36 #define TC_PRT(format, args...) PRT(format"\n", ##args)
37
38 GMainLoop *main_loop = NULL;
39 static int timeout_status = 0;
40 #define DEFAULT_CALL_ID 1
41 /*Change this number with Testing SIM*/
42 #define TEST_NUMBER "9986008917"
43
44 typedef struct {
45         const char *tc_name;
46         int tc_code;
47 } tc_table_t;
48
49 tc_table_t tc_table[] = {
50         /*Telephony Application*/
51         {"bluetooth_telephony_init", 70},
52         {"bluetooth_telephony_deinit", 71},
53         {"Indicate Outgoing call", 72},
54         {"Indicate Incoming call", 73},
55         {"Speaker to Headphone", 74},
56         {"Headphone to Speaker ", 75},
57         {"Call End/Release", 76},
58         {"Call Hold", 77},
59         {"bluetooth_telephony_call_remote_ringing", 78},
60         {"Call Swap", 79},
61         {"Call Reject", 80},
62         {"Call Answer", 81},
63         {"Is SCO channel connected", 82},
64         {"Voice Recognition Start", 83},
65         {"Voice Recognition Stop", 84},
66         {"NREC Status", 85},
67         /* -----------------------------------------*/
68         {"Finish", 0x00ff},
69         {NULL, 0x0000},
70
71 };
72
73 #define tc_result(success, tc_index) \
74         TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, \
75                         tc_table[tc_index].tc_name, \
76                         ((success == TC_PASS) ? "Success" : "Failed"));
77
78 void tc_usage_print(void)
79 {
80         int i = 0;
81
82         while (tc_table[i].tc_name) {
83                 if (tc_table[i].tc_code != 0x00ff) {
84                         TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
85                                                         tc_table[i].tc_name);
86                 } else {
87                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
88                                                         tc_table[i].tc_name);
89                 }
90
91                 i++;
92         }
93 }
94
95 void telephony_event_handler(int event, void *data, void *user_data)
96 {
97         telephony_event_param_t *bt_event;
98
99         if (data == NULL)
100                 return;
101         bt_event = data;
102
103         TC_PRT("AG event : [0x%04x]", event);
104
105         switch (event) {
106         case BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL:
107                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL");
108                 bluetooth_telephony_call_answered(DEFAULT_CALL_ID, TRUE);
109                 break;
110
111         case BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL:
112                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL");
113                 bluetooth_telephony_call_end(DEFAULT_CALL_ID);
114                 break;
115
116         case BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL:
117                 bluetooth_telephony_call_end(DEFAULT_CALL_ID);
118                 break;
119
120         case BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL:
121                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL");
122                 break;
123
124         case BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL:
125                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL");
126                 break;
127
128         case BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL:
129                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL");
130                 break;
131
132         case BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL:
133                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL");
134                 break;
135
136         case BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF:
137                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF");
138                 break;
139
140         case BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER:
141                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_CHLD_4_EXPLICIT_CALL_TRANSFER");
142                 break;
143
144         case BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED: {
145                 gboolean *nrec;
146                 TC_PRT("BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED");
147                 nrec = bt_event->param_data;
148                 TC_PRT("NREC status = [%d]", *nrec);
149                 break;
150         }
151
152         default:
153                 break;
154         }
155 }
156
157 int test_input_callback(void *data)
158 {
159         int test_id = (int)data;
160
161         switch (test_id) {
162                 case 0x00ff:
163                         TC_PRT("Finished");
164                         g_main_loop_quit(main_loop);
165                         break;
166
167                 case 70:
168                         bluetooth_telephony_init(telephony_event_handler, NULL);
169                         break;
170                 case 71:
171                         bluetooth_telephony_deinit();
172                         break;
173
174                 case 72:
175                         bluetooth_telephony_indicate_outgoing_call(
176                                         TEST_NUMBER, DEFAULT_CALL_ID, TRUE);
177                         break;
178                 case 73:
179                         bluetooth_telephony_indicate_incoming_call(
180                                         TEST_NUMBER, TRUE);
181                         break;
182                 case 74:
183                         bluetooth_telephony_audio_open();
184                         break;
185                 case 75:
186                         bluetooth_telephony_audio_close();
187                         break;
188                 case 76:
189                         bluetooth_telephony_call_end(DEFAULT_CALL_ID);
190                         break;
191                 case 77:
192                         bluetooth_telephony_call_held(DEFAULT_CALL_ID);
193                         break;
194                 case 78:
195                         bluetooth_telephony_call_remote_ringing(
196                                                         DEFAULT_CALL_ID);
197                         break;
198                 case 79:
199                         TC_PRT("bluetooth_telephony_call_swapped  \n");
200                         break;
201                 case 80:
202                         bluetooth_telephony_call_answered(
203                                                         DEFAULT_CALL_ID, FALSE);
204                         break;
205                 case 81:
206                         bluetooth_telephony_call_answered(
207                                                         DEFAULT_CALL_ID, TRUE);
208                         break;
209
210                 case 82: {
211                         int state;
212
213                         state = bluetooth_telephony_is_sco_connected();
214
215                         TC_PRT("State = %d \n", state);
216                         break;
217                 }
218
219                 case 83: {
220                         int ret = 0;
221
222                         TC_PRT("**********************\n");
223                         TC_PRT("           PLEASE SPEAK          \n");
224                         TC_PRT("**********************\n");
225
226                         ret = bluetooth_telephony_start_voice_recognition();
227
228                         if (ret == BLUETOOTH_TELEPHONY_ERROR_NONE) {
229                                 TC_PRT("No error\n");
230                                 bluetooth_telephony_audio_open();
231                         }
232                         break;
233                 }
234
235                 case 84: {
236                         TC_PRT("Rcognition finished \n");
237                         bluetooth_telephony_audio_close();
238                         bluetooth_telephony_stop_voice_recognition();
239                         break;
240                 }
241
242                 case 85: {
243                         int ret;
244                         gboolean status = FALSE;
245
246                         ret = bluetooth_telephony_is_nrec_enabled(&status);
247
248                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
249                                 TC_PRT("Error getting NREC Status\n");
250
251                         TC_PRT("NREC status = %d\n", status);
252                         break;
253                 }
254
255                 default:
256                         break;
257         }
258
259         return 0;
260 }
261
262 void startup()
263 {
264         TC_PRT("bluetooth framework TC startup");
265
266         if (!g_thread_supported()) {
267                 g_thread_init(NULL);
268         }
269
270         dbus_g_thread_init();
271
272         g_type_init();
273         main_loop = g_main_loop_new(NULL, FALSE);
274 }
275
276 void cleanup()
277 {
278         TC_PRT("bluetooth framework TC cleanup");
279         if (main_loop != NULL) {
280                 g_main_loop_unref(main_loop);
281         }
282 }
283
284 int timeout_callback(void *data)
285 {
286         TC_PRT("timeout callback");
287         timeout_status = -1;
288
289         g_main_loop_quit(main_loop);
290
291         return FALSE;
292 }
293
294 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond ,
295                                                         gpointer data)
296 {
297         char buf[10] = {0};
298
299         unsigned int len = 0;
300         int test_id;
301         memset(buf, 0, sizeof(buf));
302
303         if (g_io_channel_read(chan, buf, sizeof(buf), &len) !=
304                                         G_IO_ERROR_NONE) {
305
306                 printf("IO Channel read error");
307                 return FALSE;
308
309         }
310         printf("%s\n", buf);
311         tc_usage_print();
312
313         test_id = atoi(buf);
314
315         if (test_id)
316                 g_idle_add(test_input_callback, (void *)test_id);
317
318         return TRUE;
319 }
320
321 int main()
322 {
323         startup();
324
325         GIOChannel *key_io;
326         key_io = g_io_channel_unix_new(fileno(stdin));
327
328         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
329                         key_event_cb, NULL);
330         g_io_channel_unref(key_io);
331
332
333         g_main_loop_run(main_loop);
334
335         cleanup();
336         return 0;
337 }