7 #include <bluetooth_extension.h>
9 #define LOG_RED "\033[0;31m"
10 #define LOG_GREEN "\033[0;32m"
11 #define LOG_BROWN "\033[0;33m"
12 #define LOG_BLUE "\033[0;34m"
13 #define LOG_END "\033[0;m"
15 #define BT_PROFILE_SERVICE_UUID_HFP_HF "111e"
16 #define BT_PROFILE_SERVICE_UUID_HFP_AG "111f"
17 #define BT_PROFILE_SERVICE_UUID_A2DP_SOURCE "110a"
18 #define BT_PROFILE_SERVICE_UUID_A2DP_SINK "110b"
19 #define PHONE_NUMBER_LENGTH 15
22 static const char *__bt_get_error(bt_error_e err)
24 const char *err_str = NULL;
28 err_str = "BT_ERROR_NONE";
30 case BT_ERROR_CANCELLED:
31 err_str = "BT_ERROR_CANCELLED";
33 case BT_ERROR_INVALID_PARAMETER:
34 err_str = "BT_ERROR_INVALID_PARAMETER";
36 case BT_ERROR_OUT_OF_MEMORY:
37 err_str = "BT_ERROR_OUT_OF_MEMORY";
39 case BT_ERROR_RESOURCE_BUSY:
40 err_str = "BT_ERROR_RESOURCE_BUSY";
42 case BT_ERROR_TIMED_OUT:
43 err_str = "BT_ERROR_TIMED_OUT";
45 case BT_ERROR_NOW_IN_PROGRESS:
46 err_str = "BT_ERROR_NOW_IN_PROGRESS";
48 case BT_ERROR_NOT_INITIALIZED:
49 err_str = "BT_ERROR_NOT_INITIALIZED";
51 case BT_ERROR_NOT_ENABLED:
52 err_str = "BT_ERROR_NOT_ENABLED";
54 case BT_ERROR_ALREADY_DONE:
55 err_str = "BT_ERROR_ALREADY_DONE";
57 case BT_ERROR_OPERATION_FAILED:
58 err_str = "BT_ERROR_OPERATION_FAILED";
60 case BT_ERROR_NOT_IN_PROGRESS:
61 err_str = "BT_ERROR_NOT_IN_PROGRESS";
63 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
64 err_str = "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
66 case BT_ERROR_AUTH_REJECTED:
67 err_str = "BT_ERROR_AUTH_REJECTED";
69 case BT_ERROR_AUTH_FAILED:
70 err_str = "BT_ERROR_AUTH_FAILED";
72 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
73 err_str = "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
75 case BT_ERROR_SERVICE_SEARCH_FAILED:
76 err_str = "BT_ERROR_SERVICE_SEARCH_FAILED";
78 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
79 err_str = "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
81 case BT_ERROR_PERMISSION_DENIED:
82 err_str = "BT_ERROR_PERMISSION_DENIED";
84 case BT_ERROR_SERVICE_NOT_FOUND:
85 err_str = "BT_ERROR_SERVICE_NOT_FOUND";
87 case BT_ERROR_NO_DATA:
88 err_str = "BT_ERROR_NO_DATA";
90 case BT_ERROR_NOT_SUPPORTED:
91 err_str = "BT_ERROR_NOT_SUPPORTED";
93 case BT_ERROR_DEVICE_POLICY_RESTRICTION:
94 err_str = "DEVICE_POLICY_RESTRICTION";
97 err_str = "NOT Defined";
105 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
107 static char remote_phone_number[PHONE_NUMBER_LENGTH] = "+8200000000000";
108 static char remote_phone_bt_mac[18] = "00:00:00:00:00:00";
109 static int remote_phone_pb_size = 0;
110 static bool ringing_status = false;
112 void __bt_hf_set_remote_call_event_cb(bt_hf_remote_call_event_e event,
113 char *phone_number, void *user_data)
116 case BT_HF_REMOTE_CALL_EVENT_IDLE: {
117 printf("[remote_call_event_cb] event [IDLE]\n");
118 if (ringing_status) {
119 printf("[APP] STOP ALARM\n");
120 ringing_status = false;
124 case BT_HF_REMOTE_CALL_EVENT_INCOMING:
125 printf("[remote_call_event_cb] event [INCOMING]\n");
127 case BT_HF_REMOTE_CALL_EVENT_DIALING: {
131 printf("[remote_call_event_cb] event [DIALING]\n");
132 ret = bt_hf_get_call_status_info_list(&list);
134 if (ret < BT_ERROR_NONE)
135 printf("failed with [0x%04x]\n", ret);
137 for (; list; list = g_slist_next(list)) {
138 bt_hf_call_status_info_s *call_info = list->data;
139 printf("[CALL INFO] number : %s\n", call_info->number);
140 printf("[CALL INFO] direction (0: outgoing, 1: incoming) : %d\n", call_info->direction);
141 printf("[CALL INFO] status (0: active, 1: held, 2: dialing, 3: alerting) : %d\n", call_info->status);
142 strncpy(remote_phone_number, call_info->number, PHONE_NUMBER_LENGTH - 1);
144 bt_hf_free_call_status_info_list(list);
148 case BT_HF_REMOTE_CALL_EVENT_ALERTING:
149 printf("[remote_call_event_cb] event [ALERTING]\n");
150 printf("[remote_call_event_cb] number : %s\n", remote_phone_number);
152 case BT_HF_REMOTE_CALL_EVENT_CALL_TERMINATED:
153 printf("[remote_call_event_cb] event [TERMINATED]\n");
155 case BT_HF_REMOTE_CALL_EVENT_CALL_STARTED: {
156 printf("[remote_call_event_cb] event [STARTED]\n");
157 if (ringing_status) {
158 printf("[APP] STOP ALARM\n");
159 ringing_status = false;
163 case BT_HF_REMOTE_CALL_EVENT_CALL_ENDED:
164 printf("[remote_call_event_cb] event [ENDED]\n");
166 case BT_HF_REMOTE_CALL_EVENT_UNHELD:
167 printf("[remote_call_event_cb] event [UNHELD]\n");
169 case BT_HF_REMOTE_CALL_EVENT_HELD:
170 printf("[remote_call_event_cb] event [HELD]\n");
172 case BT_HF_REMOTE_CALL_EVENT_RINGING: {
173 printf("[remote_call_event_cb] event [RINGING]\n");
174 printf("[remote_call_event_cb] phone_number [%s]\n", phone_number);
175 if (!ringing_status) {
176 printf("[APP] START ALARM\n");
177 ringing_status = true;
181 case BT_HF_REMOTE_CALL_EVENT_WAITING:
182 printf("[remote_call_event_cb] event [WAITING]\n");
184 case BT_HF_REMOTE_CALL_EVENT_FAILED_TO_DIALING:
185 printf("[remote_call_event_cb] event [FAILED_TO_DIALING]\n");
188 printf("[remote_call_event_cb] event [UNKNOWN]\n");
194 void __bt_pbap_phonebook_pull_cb(int result, const char *remote_address,
195 const char *vcf_file, void *user_data)
197 printf("[__bt_pbap_phonebook_pull_cb] Result: %d\n", result);
198 printf("[__bt_pbap_phonebook_pull_cb] Remote Device: %s\n", remote_address);
199 printf("[__bt_pbap_phonebook_pull_cb] Phonebook Download File: %s\n", vcf_file);
200 printf("[__bt_pbap_phonebook_pull_cb] Phonebook Download Status: %s\n",
201 (result == BT_ERROR_NONE) ? "Successful" : "Unsuccessful");
203 if (result == BT_ERROR_NONE) {
204 printf("[__bt_pbap_phonebook_pull_cb] PHONEBOOK DOWNLOAD DONE.. DISCONNECT PBAP CLIENT\n");
206 printf("[__bt_pbap_phonebook_pull_cb] PHONEBOOK DOWNLOAD FAILED.. (ERROR: %s)\n", __bt_get_error(result));
209 void __bt_pbap_phonebook_size_cb(int result, const char *remote_address, int size, void *user_data)
213 printf("[__bt_pbap_phonebook_size_cb] Result: %d\n", result);
214 printf("[__bt_pbap_phonebook_size_cb] Remote Device: %s\n", remote_address);
215 printf("[__bt_pbap_phonebook_size_cb] Phonebook Size: %d\n", size);
217 remote_phone_pb_size = size;
219 if (result == BT_ERROR_NONE) {
220 ret = bt_pbap_client_get_phone_book(remote_address, BT_PBAP_SOURCE_DEVICE, BT_PBAP_FOLDER_PHONE_BOOK,
221 BT_PBAP_VCARD_FORMAT_VCARD30, BT_PBAP_ORDER_INDEXED, 0, size, BT_PBAP_FIELD_N | BT_PBAP_FIELD_TEL,
222 __bt_pbap_phonebook_pull_cb, NULL);
223 if (ret != BT_ERROR_NONE) {
224 printf("bt_pbap_client_get_phone_book failed (%d)\n", ret);
228 void __bt_pbap_connection_state_changed_cb(int result, bool connected, const char *remote_address, void *user_data)
232 printf("[__bt_pbap_connection_state_changed_cb] Result: %d\n", result);
233 printf("[__bt_pbap_connection_state_changed_cb] Remote Device: %s\n", remote_address);
234 printf("[__bt_pbap_connection_state_changed_cb] Connected Status: %d\n", connected);
236 // if phone is PBAP connected, get connected mobile's phonebook size
237 if ((result == 0) && connected) {
238 ret = bt_pbap_client_get_phone_book_size(remote_address, BT_PBAP_SOURCE_DEVICE,
239 BT_PBAP_FOLDER_PHONE_BOOK, __bt_pbap_phonebook_size_cb, NULL);
240 if (ret != BT_ERROR_NONE) {
241 printf("bt_pbap_client_get_phone_book_size failed (%s)\n", __bt_get_error(result));
246 void __bt_audio_connection_state_changed_cb(int result, bool connected,
247 const char *remote_address, bt_audio_profile_type_e type, void *user_data)
250 if (type == BT_AUDIO_PROFILE_TYPE_AG) {
251 printf("[__bt_audio_connection_state_changed_cb] result : %d\n", result);
252 printf("[__bt_audio_connection_state_changed_cb] connected : %d\n", connected);
253 printf("[__bt_audio_connection_state_changed_cb] address : %s\n", remote_address);
254 printf("[__bt_audio_connection_state_changed_cb] type : %d\n", type);
257 printf("[__bt_audio_connection_state_changed_cb] mobile connected!!!!\n");
259 strncpy(remote_phone_bt_mac, remote_address, strlen(remote_phone_bt_mac));
262 printf("[__bt_audio_connection_state_changed_cb] mobile disconnected!!!!\n");
263 if (ringing_status) {
264 //mobile is disconnected during RINING event.. Stop all alarm
265 printf("[APP] STOP ALARM\n");
266 ringing_status = false;
272 static int initialize_bluetooth(void)
276 ret = bt_initialize();
277 if (ret != BT_ERROR_NONE) {
278 printf("bt_initialize failed (%s)\n", __bt_get_error(ret));
282 ret = bt_audio_initialize();
283 if (ret != BT_ERROR_NONE) {
284 printf("bt_audio_initialize failed (%s)\n", __bt_get_error(ret));
288 ret = bt_hf_initialize();
289 if (ret != BT_ERROR_NONE) {
290 printf("bt_hf_initialize failed (%s)\n", __bt_get_error(ret));
293 ret = bt_hf_set_remote_call_event_cb(__bt_hf_set_remote_call_event_cb, NULL);
294 if (ret != BT_ERROR_NONE) {
295 printf("bt_hf_set_remote_call_event_cb failed (%s)\n", __bt_get_error(ret));
299 ret = bt_audio_set_connection_state_changed_cb(__bt_audio_connection_state_changed_cb, NULL);
300 if (ret != BT_ERROR_NONE) {
301 printf("bt_audio_set_connection_state_changed_cb failed (%s)\n", __bt_get_error(ret));
305 ret = bt_pbap_client_initialize();
306 if (ret != BT_ERROR_NONE && ret != BT_ERROR_ALREADY_DONE) {
307 printf("bt_pbap_client_initialize failed (%s)\n", __bt_get_error(ret));
310 ret = bt_pbap_client_set_connection_state_changed_cb(__bt_pbap_connection_state_changed_cb, NULL);
311 if (ret != BT_ERROR_NONE) {
312 printf("bt_pbap_client_set_connection_state_changed_cb failed (%s)\n", __bt_get_error(ret));
318 void test_deinitialize(void)
322 ret = bt_hf_unset_remote_call_event_cb();
323 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
324 printf("bt_hf_unset_remote_call_event_cb failed (%s)\n", __bt_get_error(ret));
327 ret = bt_pbap_client_unset_connection_state_changed_cb();
328 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
329 printf("bt_pbap_client_unset_connection_state_changed_cb failed (%s)\n", __bt_get_error(ret));
332 ret = bt_audio_unset_connection_state_changed_cb();
333 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
334 printf("bt_audio_set_connection_state_changed_cb failed (%s)\n", __bt_get_error(ret));
337 ret = bt_pbap_client_deinitialize();
338 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
339 printf("bt_pbap_client_deinitialize failed (%s)\n", __bt_get_error(ret));
342 ret = bt_hf_deinitialize();
343 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
344 printf("bt_hf_deinitialize failed (%s)\n", __bt_get_error(ret));
347 ret = bt_audio_deinitialize();
348 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
349 printf("bt_audio_deinitialize failed (%s)\n", __bt_get_error(ret));
352 ret = bt_deinitialize();
353 if (ret != BT_ERROR_NONE && ret != BT_ERROR_NOT_INITIALIZED) {
354 printf("bt_deinitialize failed (%s)\n", __bt_get_error(ret));
358 int test_accept_incoming_call(void)
361 ret = bt_hf_notify_call_event(BT_HF_CALL_EVENT_ANSWER, NULL);
363 if (ret != BT_ERROR_NONE) {
364 printf("bt_hf_notify_call_event(BT_HF_CALL_EVENT_ANSWER) failed (%s)\n", __bt_get_error(ret));
367 printf("[SUCCESS] accept incoming call\n");
372 int test_initiate_call(void)
376 printf("Input full phone number to dial : ");
377 ret = scanf("%14s", remote_phone_number);
379 ret = bt_hf_notify_call_event(BT_HF_CALL_EVENT_DIAL, remote_phone_number);
380 if (ret != BT_ERROR_NONE) {
381 printf("bt_hf_notify_call_event(BT_HF_CALL_EVENT_DIAL) failed (%s)\n", __bt_get_error(ret));
382 printf("phone number : %s", remote_phone_number);
385 printf("[SUCCESS] initiate call.. [%s]\n", remote_phone_number);
390 int test_terminate_call(void)
393 ret = bt_hf_notify_call_event(BT_HF_CALL_EVENT_IDLE, NULL);
395 if (ret != BT_ERROR_NONE) {
396 printf("bt_hf_notify_call_event(BT_HF_CALL_EVENT_IDLE) failed (%s)\n", __bt_get_error(ret));
399 printf("[SUCCESS] terminate call\n");
404 int test_audio_mute_on(void)
407 ret = bt_hf_notify_call_event(BT_HF_CALL_EVENT_AUDIO_MUTE_ON, NULL);
409 if (ret != BT_ERROR_NONE) {
410 printf("bt_hf_notify_call_event(BT_HF_CALL_EVENT_AUDIO_MUTE_ON) failed (%s)\n", __bt_get_error(ret));
413 printf("[SUCCESS] audio mute\n");
418 int test_audio_mute_off(void)
421 ret = bt_hf_notify_call_event(BT_HF_CALL_EVENT_AUDIO_MUTE_OFF, NULL);
423 if (ret != BT_ERROR_NONE) {
424 printf("bt_hf_notify_call_event(BT_HF_CALL_EVENT_AUDIO_MUTE_OFF) failed (%s)\n", __bt_get_error(ret));
427 printf("[SUCCESS] audio unmute\n");
432 int test_pbap_connect(void)
436 // try to PBAP connect to connected mobile phone device
437 ret = bt_pbap_client_connect(remote_phone_bt_mac);
438 if (ret != BT_ERROR_NONE) {
439 if (ret == BT_ERROR_ALREADY_DONE) {
440 printf("PBAP already connected [%s]\n", remote_phone_bt_mac);
442 /// if phone is PBAP connected, get connected mobile's phonebook size
443 ret = bt_pbap_client_get_phone_book_size(remote_phone_bt_mac, BT_PBAP_SOURCE_DEVICE,
444 BT_PBAP_FOLDER_PHONE_BOOK, __bt_pbap_phonebook_size_cb, NULL);
445 if (ret != BT_ERROR_NONE) {
446 printf("bt_pbap_client_get_phone_book_size failed (%s)\n", __bt_get_error(ret));
449 printf("bt_pbap_client_connect failed (%s)\n", __bt_get_error(ret));
453 printf("[SUCCESS] pbap connect.. [%s]\n", remote_phone_bt_mac);
458 int test_pbap_disconnect(void)
462 // try to PBAP connect to connected mobile phone device
463 ret = bt_pbap_client_disconnect(remote_phone_bt_mac);
464 if (ret != BT_ERROR_NONE) {
465 printf("bt_pbap_client_disconnect failed (%s)\n", __bt_get_error(ret));
468 printf("[SUCCESS] pbap disconnect.. [%s]\n", remote_phone_bt_mac);
475 GMainLoop *gmain_loop;
477 gmain_loop = g_main_loop_new(NULL, FALSE);
479 if (gmain_loop == NULL) {
480 printf("GMainLoop create failed\n");
484 GIOChannel *channel = g_io_channel_unix_new(0);
485 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
487 printf("Test Thread created...\n");
489 g_main_loop_run(gmain_loop);
492 g_main_loop_unref(gmain_loop);
497 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
502 printf("Event received from stdin\n");
506 if (rv <= 0 || a[0] == '0') {
511 if (a[0] == '\n' || a[0] == '\r') {
513 printf("\n\n Bluetooth HFP Test App\n\n");
514 printf("Options..\n");
515 printf(LOG_GREEN "1 - Bluetooth init and set callbacks\n" LOG_END);
516 printf("2 - accept incoming call\n");
517 printf("3 - initiate call\n");
518 printf("4 - terminatate call\n");
519 printf("5 - audio mute\n");
520 printf("6 - audio unmute\n");
521 printf("7 - connect PBAP\n");
522 printf("8 - disconnect PBAP\n");
523 printf(LOG_RED "0 - Exit \n" LOG_END);
525 printf("ENTER - Show options menu.......\n");
531 rv = initialize_bluetooth();
534 rv = test_accept_incoming_call();
537 rv = test_initiate_call();
540 rv = test_terminate_call();
543 rv = test_audio_mute_on();
546 rv = test_audio_mute_off();
549 rv = test_pbap_connect();
552 rv = test_pbap_disconnect();
560 printf("Operation succeeded!\n");
562 printf("Operation failed!\n");