Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / test / handsfree / bluetooth-hf-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-audio-api.h"
36 #include "bluetooth-api.h"
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 "9980785507"
47 #define BLUETOOTH_HF_SPEAKER_GAIN 2
48
49 typedef struct {
50         const char *tc_name;
51         int tc_code;
52 } tc_table_t;
53
54 tc_table_t tc_table[] = {
55         /*HF Application*/
56         {"bluetooth_hf_init", 1},
57         {"bluetooth_hf_deinit", 2},
58         {"Answer Call", 3},
59         {"Terminate Call", 4},
60         {"Initiate Call", 5},
61         {"Last number Redial ", 6},
62         {"(PTS) Connect last bonded device", 7},
63         {"Disconnect", 8},
64         {"(PTS) Voice Recognition Enable", 9},
65         {"Voice RecognitionDisable", 10},
66         {"SCO disconnect", 11},
67         {"Speaker gain", 12},
68         {"Dual Tone mulitple frequency", 13},
69         {"Send AT+XSAT=appid command", 14},
70         {"Release All Call(CHLD=0)", 15},
71         {"Release and Accept(CHLD=1)", 16},
72         {"Swap call (CHLD=2)", 17},
73         {"Join Call (CHLD=3)", 18},
74         {"(PTS) Initiate Codec based SCO", 19},
75         {"(PTS) Unbond all devices", 20},
76         {"Get Current Codec", 21},
77         {"Get Call List", 22},
78         {"Get Audio Connected Status", 23},
79         {"Is Handsfree Connected?", 24},
80
81         /* -----------------------------------------*/
82         {"Finish", 0x00ff},
83         {NULL, 0x0000},
84
85 };
86
87 #define tc_result(success, tc_index) \
88         TC_PRT("Test case [%d - %s] %s", tc_table[tc_index].tc_code, \
89                         tc_table[tc_index].tc_name, \
90                         ((success == TC_PASS) ? "Success" : "Failed"));
91
92 void tc_usage_print(void)
93 {
94         int i = 0;
95
96         while (tc_table[i].tc_name) {
97                 if (tc_table[i].tc_code != 0x00ff) {
98                         TC_PRT("Key %d : usage %s", tc_table[i].tc_code,
99                                                         tc_table[i].tc_name);
100                 } else {
101                         TC_PRT("Key %d : usage %s\n\n", 0x00ff,
102                                                         tc_table[i].tc_name);
103                 }
104
105                 i++;
106         }
107 }
108 void bt_event_callback(int event, bluetooth_event_param_t *param, void *user_data)
109 {
110         GMainLoop *main_loop = (GMainLoop*) user_data;
111
112         switch(event)
113         {
114                 // Code for each event
115                 default:
116                         break;
117         }
118 }
119
120 void hf_event_handler(int event, void *data, void *user_data)
121 {
122         bt_hf_event_param_t *hf_event;
123
124         if (data == NULL)
125                 return;
126         hf_event = data;
127
128         TC_PRT("HF event : [0x%04x]", event);
129
130         switch (event) {
131         case BLUETOOTH_EVENT_HF_CONNECTED:
132                 TC_PRT("BLUETOOTH_EVENT_HF_CONNECTED");
133                 break;
134
135         case BLUETOOTH_EVENT_HF_DISCONNECTED:
136                 TC_PRT("BLUETOOTH_EVENT_HF_DISCONNECTED");
137                 break;
138
139         case BLUETOOTH_EVENT_HF_AUDIO_CONNECTED:
140                 TC_PRT("BLUETOOTH_EVENT_HF_AUDIO_CONNECTED");
141                 break;
142
143         case BLUETOOTH_EVENT_HF_AUDIO_DISCONNECTED:
144                 TC_PRT("BLUETOOTH_EVENT_HF_AUDIO_DISCONNECTED");
145                 break;
146
147         case BLUETOOTH_EVENT_HF_RING_INDICATOR:
148                 TC_PRT("BLUETOOTH_EVENT_HF_RING_INDICATOR");
149                 if (hf_event->param_data)
150                         TC_PRT("Phone number %s", hf_event->param_data);
151                 break;
152
153         case BLUETOOTH_EVENT_HF_CALL_WAITING:
154                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_WAITING");
155                 if (hf_event->param_data)
156                         TC_PRT("Waiting Phone number %s", hf_event->param_data);
157                 break;
158
159         case BLUETOOTH_EVENT_HF_CALL_TERMINATED:
160                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_TERMINATED");
161                 break;
162
163         case BLUETOOTH_EVENT_HF_CALL_STARTED:
164                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_STARTED");
165                 break;
166
167         case BLUETOOTH_EVENT_HF_CALL_ENDED:
168                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_ENDED");
169                 break;
170
171         case BLUETOOTH_EVENT_HF_CALL_UNHOLD:
172                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_UNHOLD");
173                 break;
174
175         case BLUETOOTH_EVENT_HF_CALL_SWAPPED:
176                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_SWAPPED");
177                 break;
178
179         case BLUETOOTH_EVENT_HF_CALL_ON_HOLD:
180                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_ON_HOLD");
181                 break;
182
183         case BLUETOOTH_EVENT_HF_CALL_STATUS:
184         {
185                 TC_PRT("BLUETOOTH_EVENT_HF_CALL_STATUS");
186                 int i;
187                 bt_hf_call_list_s * call_list = hf_event->param_data;
188                 bt_hf_call_status_info_t **call_info;
189                 TC_PRT("call_list length : %d ", call_list->count);
190                 call_info = g_malloc0(sizeof(bt_hf_call_status_info_t *) *
191                                                 call_list->count);
192                 bluetooth_hf_get_call_list(call_list->list, call_info);
193
194                 for (i= 0; i < call_list->count; i++) {
195                         TC_PRT("Phone Number : %s ", call_info[i]->number);
196                         TC_PRT("Direction (in -1, out 0 : %d ", call_info[i]->direction);
197                         TC_PRT("Call status : %d ", call_info[i]->status);
198                         TC_PRT("MultyParty : %d ", call_info[i]->mpart);
199                         TC_PRT("Call ID : %d ", call_info[i]->idx);
200                 }
201                 g_free(call_info);
202                 break;
203         }
204         case BLUETOOTH_EVENT_HF_VOICE_RECOGNITION_ENABLED:
205                 TC_PRT("BLUETOOTH_EVENT_HF_VOICE_RECOGNITION_ENABLED");
206                 break;
207
208         case BLUETOOTH_EVENT_HF_VOICE_RECOGNITION_DISABLED:
209                 TC_PRT("BLUETOOTH_EVENT_HF_VOICE_RECOGNITION_DISABLED");
210                 break;
211
212         case BLUETOOTH_EVENT_HF_VOLUME_SPEAKER:
213         {
214                 unsigned int *value;
215                 value = hf_event->param_data;
216                 TC_PRT("BLUETOOTH_EVENT_HF_VOLUME_SPEAKER - value = %d", *value);
217                 break;
218         }
219         case BLUETOOTH_EVENT_HF_VENDOR_DEP_CMD:
220         {
221                 bluetooth_vendor_dep_at_cmd_t *cmd = hf_event->param_data;
222                 TC_PRT("BLUETOOTH_EVENT_HF_VENDOR_DEP_CMD - appid = %d, msg = %s",
223                         cmd->app_id, cmd->message);
224                 break;
225         }
226
227         default:
228                 break;
229         }
230 }
231
232 static int  __bt_unbond_all_bonded_devices(void)
233 {
234         int ret;
235         int i;
236         bluetooth_device_info_t *ptr;
237
238         GPtrArray *dev_list = NULL;
239         dev_list = g_ptr_array_new();
240         TC_PRT("g pointer arrary count : [%d]", dev_list->len);
241
242         ret = bluetooth_get_bonded_device_list(&dev_list);
243         if (ret < 0) {
244                 TC_PRT("failed bluetooth_get_bonded_device_list");
245                 g_ptr_array_free(dev_list, TRUE);
246                 return 1;
247         }
248         TC_PRT("g pointer arrary count : [%d]", dev_list->len);
249
250         if (dev_list->len == 0) {
251                 TC_PRT("No paired device found");
252                 g_ptr_array_free(dev_list, TRUE);
253                 return 1;
254         }
255
256         for (i = 0; i < dev_list->len; i++) {
257                 ptr = g_ptr_array_index(dev_list, i);
258                 if (ptr == NULL)
259                         continue;
260                 TC_PRT("[%d] Unbond %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", i + 1,
261                         ptr->device_address.addr[0], ptr->device_address.addr[1],
262                         ptr->device_address.addr[2], ptr->device_address.addr[3],
263                         ptr->device_address.addr[4], ptr->device_address.addr[5]);
264                         bluetooth_unbond_device(&ptr->device_address);
265
266         }
267         g_ptr_array_foreach(dev_list, (GFunc)g_free, NULL);
268         g_ptr_array_free(dev_list, TRUE);
269         return 0;
270
271 }
272 static int  __bt_get_last_bonded_device(bluetooth_device_address_t *device_address)
273 {
274         int ret;
275         int i;
276         bluetooth_device_info_t *ptr;
277
278         GPtrArray *dev_list = NULL;
279         dev_list = g_ptr_array_new();
280         TC_PRT("g pointer arrary count : [%d]", dev_list->len);
281
282         ret = bluetooth_get_bonded_device_list(&dev_list);
283         if (ret < 0) {
284                 TC_PRT("failed bluetooth_get_bonded_device_list");
285                 g_ptr_array_free(dev_list, TRUE);
286                 return 1;
287         }
288         TC_PRT("g pointer arrary count : [%d]", dev_list->len);
289
290         if (dev_list->len == 0) {
291                 TC_PRT("No paired device found");
292                 g_ptr_array_free(dev_list, TRUE);
293                 return 1;
294         }
295
296         for (i = 0; i < dev_list->len; i++) {
297                 ptr = g_ptr_array_index(dev_list, i);
298                 if (ptr == NULL)
299                         continue;
300                 TC_PRT("[%d] %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X", i + 1,
301                         ptr->device_address.addr[0], ptr->device_address.addr[1],
302                         ptr->device_address.addr[2], ptr->device_address.addr[3],
303                         ptr->device_address.addr[4], ptr->device_address.addr[5]);
304                 memcpy(device_address->addr, ptr->device_address.addr,
305                                 sizeof(bluetooth_device_address_t));
306         }
307         g_ptr_array_foreach(dev_list, (GFunc)g_free, NULL);
308         g_ptr_array_free(dev_list, TRUE);
309         return 0;
310
311 }
312
313 int test_input_callback(void *data)
314 {
315         int ret;
316         int test_id = (int)data;
317
318         switch (test_id) {
319                 case 0x00ff:
320                         TC_PRT("Finished");
321                         g_main_loop_quit(main_loop);
322                         break;
323
324                 case 1:
325                         bluetooth_hf_init(hf_event_handler, NULL);
326                         break;
327                 case 2:
328                         bluetooth_hf_deinit();
329                         break;
330                 case 3:
331                         bluetooth_hf_answer_call();
332                         break;
333                 case 4:
334                         bluetooth_hf_terminate_call();
335                         break;
336                 case 5:
337                         ret = bluetooth_hf_initiate_call(TEST_NUMBER);
338                         TC_PRT("ret = %d", ret);
339                         break;
340                 case 6:
341                         bluetooth_hf_initiate_call(NULL);
342                         break;
343                 case 7:
344                 {       bluetooth_device_address_t device_address = { {0} };
345                         ret = __bt_get_last_bonded_device(&device_address);
346                         if (ret != 0) {
347                                 TC_PRT("Error in getting last bonded device.....");
348                                 return FALSE;
349                         }
350
351                         bluetooth_hf_connect(&device_address);
352                         break;
353                 }
354                 case 8:
355                 {
356                         bluetooth_device_address_t device_address = { {0} };
357                         ret = __bt_get_last_bonded_device(&device_address);
358                         if (ret != 0) {
359                                 TC_PRT("Error in getting last bonded device.....");
360                                 return FALSE;
361                         }
362
363                         bluetooth_hf_disconnect(&device_address);
364                         break;
365                 }
366                 case 9:
367                         bluetooth_hf_voice_recognition(1);
368                         break;
369
370                 case 10:
371                         bluetooth_hf_voice_recognition(0);
372                         break;
373                 case 11:
374                         bluetooth_hf_audio_disconnect();
375                         break;
376                 case 12:
377                         bluetooth_hf_set_speaker_gain(BLUETOOTH_HF_SPEAKER_GAIN);
378                         break;
379                 case 13:
380                         bluetooth_hf_send_dtmf("1");
381                         break;
382                 case 14:
383                         /* get the Call Time from AG for DC lauch */
384                         bluetooth_hf_send_xsat_cmd(11, "Q_CT,1,01025561613");
385                         break;
386                 case 15:
387                         bluetooth_hf_release_all_call();
388                         break;
389                 case 16:
390                         bluetooth_hf_release_and_accept();
391                         break;
392                 case 17:
393                         bluetooth_hf_swap_call();
394                         break;
395                 case 18:
396                         bluetooth_hf_join_call();
397                         break;
398                 case 19:
399                         system("dbus-send --system --print-reply --dest=org.bluez.hf_agent  /org/bluez/handsfree_agent org.tizen.HfApp.SendAtCmd string:AT+BCC");
400                         break;
401                 case 20:
402                 {
403                         ret = bluetooth_register_callback(bt_event_callback, NULL);
404                         ret = __bt_unbond_all_bonded_devices();
405                         if (ret != 0) {
406                                 TC_PRT("Error in getting last bonded device.....");
407                                 return FALSE;
408                         }
409
410                         break;
411                 }
412                 case 21:
413                 {
414                         unsigned int current_codec;
415                         bluetooth_hf_get_codec(&current_codec);
416                         if (current_codec == BLUETOOTH_CODEC_ID_CVSD)
417                                 TC_PRT("current_codec is CVSD");
418                         else
419                                 TC_PRT("current_codec is. MSBC");
420                         break;
421                 }
422                 case 22:
423                 {
424                         int i;
425                         bt_hf_call_list_s * call_list = NULL;
426                         bt_hf_call_status_info_t **call_info = NULL;
427                         bluetooth_hf_request_call_list(&call_list);
428                         if(call_list == NULL) {
429                                 TC_PRT("call_list is NULL");
430                                 break;
431                         }
432                         TC_PRT("call_list length : %d ", call_list->count);
433                         call_info = g_malloc0(sizeof(bt_hf_call_status_info_t *) *
434                                                 call_list->count);
435                         bluetooth_hf_get_call_list(call_list->list, call_info);
436
437                         for (i= 0; i < call_list->count; i++) {
438                                 TC_PRT("Phone Number : %s ", call_info[i]->number);
439                                 TC_PRT("Direction (in -1, out 0 : %d ", call_info[i]->direction);
440                                 TC_PRT("Call status : %d ", call_info[i]->status);
441                                 TC_PRT("MultyParty : %d ", call_info[i]->mpart);
442                                 TC_PRT("Call ID : %d ", call_info[i]->idx);
443                         }
444                         g_free(call_info);
445                         bluetooth_hf_free_call_list(call_list);
446                         break;
447                 }
448                 case 23:
449                 {
450                         unsigned int sco_audio_connected;
451                         bluetooth_hf_get_audio_connected(&sco_audio_connected);
452                         if (sco_audio_connected == BLUETOOTH_HF_AUDIO_CONNECTED)
453                                 TC_PRT("SCO Audio is connected");
454                         else
455                                 TC_PRT("SCO Audio is disconnected");
456                         break;
457                 }
458                 case 24:
459                 {
460                         gboolean hf_connected;
461                         bluetooth_hf_is_hf_connected(&hf_connected);
462                         if (hf_connected == BLUETOOTH_HF_AUDIO_CONNECTED)
463                                 TC_PRT("HF is connected");
464                         else
465                                 TC_PRT("HF is disconnected");
466                         break;
467                 }
468                 default:
469                         break;
470         }
471
472         return 0;
473 }
474
475 void startup()
476 {
477         TC_PRT("bluetooth framework TC startup");
478
479         if (!g_thread_supported()) {
480                 g_thread_init(NULL);
481         }
482
483         dbus_g_thread_init();
484
485         g_type_init();
486         main_loop = g_main_loop_new(NULL, FALSE);
487 }
488
489 void cleanup()
490 {
491         TC_PRT("bluetooth framework TC cleanup");
492         if (main_loop != NULL) {
493                 g_main_loop_unref(main_loop);
494         }
495 }
496
497 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond ,
498                                                         gpointer data)
499 {
500         char buf[10] = {0};
501
502         unsigned int len = 0;
503         int test_id;
504         memset(buf, 0, sizeof(buf));
505
506         if (g_io_channel_read(chan, buf, sizeof(buf), &len) !=
507                                         G_IO_ERROR_NONE) {
508
509                 printf("IO Channel read error");
510                 return FALSE;
511
512         }
513         printf("%s\n", buf);
514         tc_usage_print();
515
516         test_id = atoi(buf);
517
518         if (test_id)
519                 g_idle_add(test_input_callback, (void *)test_id);
520
521         return TRUE;
522 }
523
524 int main()
525 {
526         startup();
527
528         GIOChannel *key_io;
529         key_io = g_io_channel_unix_new(fileno(stdin));
530
531         g_io_add_watch(key_io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
532                         key_event_cb, NULL);
533         g_io_channel_unref(key_io);
534
535
536         g_main_loop_run(main_loop);
537
538         cleanup();
539         return 0;
540 }