hfp_hf_demo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright (C) 2014 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "hfp_hf_demo.c"
  38. /*
  39. * hfp_hs_demo.c
  40. */
  41. // *****************************************************************************
  42. /* EXAMPLE_START(hfp_hs_demo): HFP Hands-Free (HF) Demo
  43. *
  44. * @text This HFP Hands-Free example demonstrates how to receive
  45. * an output from a remote HFP audio gateway (AG), and,
  46. * if HAVE_BTSTACK_STDIN is defined, how to control the HFP AG.
  47. */
  48. // *****************************************************************************
  49. #include "btstack_config.h"
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "btstack.h"
  55. #include "sco_demo_util.h"
  56. uint8_t hfp_service_buffer[150];
  57. const uint8_t rfcomm_channel_nr = 1;
  58. const char hfp_hf_service_name[] = "HFP HF Demo";
  59. #ifdef HAVE_BTSTACK_STDIN
  60. // static const char * device_addr_string = "6C:72:E7:10:22:EE";
  61. static const char * device_addr_string = "54:E4:3A:26:A2:39";
  62. #endif
  63. static bd_addr_t device_addr;
  64. #ifdef HAVE_BTSTACK_STDIN
  65. // 80:BE:05:D5:28:48
  66. // prototypes
  67. static void show_usage(void);
  68. #endif
  69. static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
  70. static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
  71. #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
  72. static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
  73. #else
  74. static uint8_t codecs[] = {HFP_CODEC_CVSD};
  75. #endif
  76. static uint16_t indicators[1] = {0x01};
  77. static uint8_t negotiated_codec = HFP_CODEC_CVSD;
  78. static btstack_packet_callback_registration_t hci_event_callback_registration;
  79. char cmd;
  80. static void dump_supported_codecs(void){
  81. unsigned int i;
  82. int mSBC_skipped = 0;
  83. printf("Supported codecs: ");
  84. for (i = 0; i < sizeof(codecs); i++){
  85. switch(codecs[i]){
  86. case HFP_CODEC_CVSD:
  87. printf("CVSD");
  88. break;
  89. case HFP_CODEC_MSBC:
  90. if (hci_extended_sco_link_supported()){
  91. printf(", mSBC");
  92. } else {
  93. mSBC_skipped = 1;
  94. }
  95. break;
  96. }
  97. }
  98. printf("\n");
  99. if (mSBC_skipped){
  100. printf("mSBC codec disabled because eSCO not supported by local controller.\n");
  101. }
  102. }
  103. #ifdef HAVE_BTSTACK_STDIN
  104. // Testig User Interface
  105. static void show_usage(void){
  106. bd_addr_t iut_address;
  107. gap_local_bd_addr(iut_address);
  108. printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
  109. printf("\n");
  110. printf("a - establish SLC to %s | ", bd_addr_to_str(device_addr));
  111. printf("A - release SLC connection to device\n");
  112. printf("b - establish Audio connection | B - release Audio connection\n");
  113. printf("d - query network operator | D - Enable HFP AG registration status update via bitmap(IIA)\n");
  114. printf("f - answer incoming call | F - Hangup call\n");
  115. printf("g - query network operator name | G - reject incoming call\n");
  116. printf("i - dial 1234567 | I - dial 7654321\n");
  117. printf("j - dial #1 | J - dial #99\n");
  118. printf("o - Set speaker volume to 0 (minimum) | O - Set speaker volume to 9 (default)\n");
  119. printf("p - Set speaker volume to 12 (higher) | P - Set speaker volume to 15 (maximum)\n");
  120. printf("q - Set microphone gain to 0 (minimum) | Q - Set microphone gain to 9 (default)\n");
  121. printf("s - Set microphone gain to 12 (higher) | S - Set microphone gain to 15 (maximum)\n");
  122. printf("t - terminate connection\n");
  123. printf("u - send 'user busy' (TWC 0)\n");
  124. printf("U - end active call and accept other call' (TWC 1)\n");
  125. printf("v - Swap active call call (TWC 2) | V - Join held call (TWC 3)\n");
  126. printf("w - Connect calls (TWC 4) | W - redial\n");
  127. printf("c/C - disable/enable registration status update for all AG indicators\n");
  128. printf("e/E - disable/enable reporting of the extended AG error result code\n");
  129. printf("k/K - deactivate/activate call waiting notification\n");
  130. printf("l/L - deactivate/activate calling line notification\n");
  131. printf("m/M - deactivate/activate echo canceling and noise reduction\n");
  132. printf("n/N - deactivate/activate voice recognition\n");
  133. printf("0123456789#*-+ - send DTMF dial tones\n");
  134. printf("x - request phone number for voice tag | X - current call status (ECS)\n");
  135. printf("y - release call with index 2 (ECC) | Y - private consulation with call 2(ECC)\n");
  136. printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n");
  137. printf("{ - Accept held call(RHH 1) | } - Reject held call(RHH 2)\n");
  138. printf("? - Query Subscriber Number (NUM)\n");
  139. printf("! - Update HF indicator with assigned number 1 (HFI)\n");
  140. printf("\n");
  141. }
  142. static void stdin_process(char c){
  143. cmd = c; // used in packet handler
  144. if (cmd >= '0' && cmd <= '9'){
  145. printf("DTMF Code: %c\n", cmd);
  146. hfp_hf_send_dtmf_code(acl_handle, cmd);
  147. return;
  148. }
  149. switch (cmd){
  150. case '#':
  151. case '-':
  152. case '+':
  153. case '*':
  154. log_info("USER:\'%c\'", cmd);
  155. printf("DTMF Code: %c\n", cmd);
  156. hfp_hf_send_dtmf_code(acl_handle, cmd);
  157. break;
  158. case 'a':
  159. log_info("USER:\'%c\'", cmd);
  160. printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
  161. hfp_hf_establish_service_level_connection(device_addr);
  162. break;
  163. case 'A':
  164. log_info("USER:\'%c\'", cmd);
  165. printf("Release Service level connection.\n");
  166. hfp_hf_release_service_level_connection(acl_handle);
  167. break;
  168. case 'b':
  169. log_info("USER:\'%c\'", cmd);
  170. printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
  171. hfp_hf_establish_audio_connection(acl_handle);
  172. break;
  173. case 'B':
  174. log_info("USER:\'%c\'", cmd);
  175. printf("Release Audio service level connection.\n");
  176. hfp_hf_release_audio_connection(acl_handle);
  177. break;
  178. case 'C':
  179. log_info("USER:\'%c\'", cmd);
  180. printf("Enable registration status update for all AG indicators.\n");
  181. hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle);
  182. break;
  183. case 'c':
  184. log_info("USER:\'%c\'", cmd);
  185. printf("Disable registration status update for all AG indicators.\n");
  186. hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle);
  187. break;
  188. case 'D':
  189. log_info("USER:\'%c\'", cmd);
  190. printf("Set HFP AG registration status update for individual indicators (0111111).\n");
  191. hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63);
  192. break;
  193. case 'd':
  194. log_info("USER:\'%c\'", cmd);
  195. printf("Query network operator.\n");
  196. hfp_hf_query_operator_selection(acl_handle);
  197. break;
  198. case 'E':
  199. log_info("USER:\'%c\'", cmd);
  200. printf("Enable reporting of the extended AG error result code.\n");
  201. hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle);
  202. break;
  203. case 'e':
  204. log_info("USER:\'%c\'", cmd);
  205. printf("Disable reporting of the extended AG error result code.\n");
  206. hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle);
  207. break;
  208. case 'f':
  209. log_info("USER:\'%c\'", cmd);
  210. printf("Answer incoming call.\n");
  211. hfp_hf_answer_incoming_call(acl_handle);
  212. break;
  213. case 'F':
  214. log_info("USER:\'%c\'", cmd);
  215. printf("Hangup call.\n");
  216. hfp_hf_terminate_call(acl_handle);
  217. break;
  218. case 'G':
  219. log_info("USER:\'%c\'", cmd);
  220. printf("Reject incoming call.\n");
  221. hfp_hf_reject_incoming_call(acl_handle);
  222. break;
  223. case 'g':
  224. log_info("USER:\'%c\'", cmd);
  225. printf("Query operator.\n");
  226. hfp_hf_query_operator_selection(acl_handle);
  227. break;
  228. case 't':
  229. log_info("USER:\'%c\'", cmd);
  230. printf("Terminate HCI connection.\n");
  231. gap_disconnect(acl_handle);
  232. break;
  233. case 'i':
  234. log_info("USER:\'%c\'", cmd);
  235. printf("Dial 1234567\n");
  236. hfp_hf_dial_number(acl_handle, "1234567");
  237. break;
  238. case 'I':
  239. log_info("USER:\'%c\'", cmd);
  240. printf("Dial 7654321\n");
  241. hfp_hf_dial_number(acl_handle, "7654321");
  242. break;
  243. case 'j':
  244. log_info("USER:\'%c\'", cmd);
  245. printf("Dial #1\n");
  246. hfp_hf_dial_memory(acl_handle,1);
  247. break;
  248. case 'J':
  249. log_info("USER:\'%c\'", cmd);
  250. printf("Dial #99\n");
  251. hfp_hf_dial_memory(acl_handle,99);
  252. break;
  253. case 'k':
  254. log_info("USER:\'%c\'", cmd);
  255. printf("Deactivate call waiting notification\n");
  256. hfp_hf_deactivate_call_waiting_notification(acl_handle);
  257. break;
  258. case 'K':
  259. log_info("USER:\'%c\'", cmd);
  260. printf("Activate call waiting notification\n");
  261. hfp_hf_activate_call_waiting_notification(acl_handle);
  262. break;
  263. case 'l':
  264. log_info("USER:\'%c\'", cmd);
  265. printf("Deactivate calling line notification\n");
  266. hfp_hf_deactivate_calling_line_notification(acl_handle);
  267. break;
  268. case 'L':
  269. log_info("USER:\'%c\'", cmd);
  270. printf("Activate calling line notification\n");
  271. hfp_hf_activate_calling_line_notification(acl_handle);
  272. break;
  273. case 'm':
  274. log_info("USER:\'%c\'", cmd);
  275. printf("Deactivate echo canceling and noise reduction\n");
  276. hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle);
  277. break;
  278. case 'M':
  279. log_info("USER:\'%c\'", cmd);
  280. printf("Activate echo canceling and noise reduction\n");
  281. hfp_hf_activate_echo_canceling_and_noise_reduction(acl_handle);
  282. break;
  283. case 'n':
  284. log_info("USER:\'%c\'", cmd);
  285. printf("Deactivate voice recognition\n");
  286. hfp_hf_deactivate_voice_recognition_notification(acl_handle);
  287. break;
  288. case 'N':
  289. log_info("USER:\'%c\'", cmd);
  290. printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr));
  291. hfp_hf_activate_voice_recognition_notification(acl_handle);
  292. break;
  293. case 'o':
  294. log_info("USER:\'%c\'", cmd);
  295. printf("Set speaker gain to 0 (minimum)\n");
  296. hfp_hf_set_speaker_gain(acl_handle, 0);
  297. break;
  298. case 'O':
  299. log_info("USER:\'%c\'", cmd);
  300. printf("Set speaker gain to 9 (default)\n");
  301. hfp_hf_set_speaker_gain(acl_handle, 9);
  302. break;
  303. case 'p':
  304. log_info("USER:\'%c\'", cmd);
  305. printf("Set speaker gain to 12 (higher)\n");
  306. hfp_hf_set_speaker_gain(acl_handle, 12);
  307. break;
  308. case 'P':
  309. log_info("USER:\'%c\'", cmd);
  310. printf("Set speaker gain to 15 (maximum)\n");
  311. hfp_hf_set_speaker_gain(acl_handle, 15);
  312. break;
  313. case 'q':
  314. log_info("USER:\'%c\'", cmd);
  315. printf("Set microphone gain to 0\n");
  316. hfp_hf_set_microphone_gain(acl_handle, 0);
  317. break;
  318. case 'Q':
  319. log_info("USER:\'%c\'", cmd);
  320. printf("Set microphone gain to 9\n");
  321. hfp_hf_set_microphone_gain(acl_handle, 9);
  322. break;
  323. case 's':
  324. log_info("USER:\'%c\'", cmd);
  325. printf("Set microphone gain to 12\n");
  326. hfp_hf_set_microphone_gain(acl_handle, 12);
  327. break;
  328. case 'S':
  329. log_info("USER:\'%c\'", cmd);
  330. printf("Set microphone gain to 15\n");
  331. hfp_hf_set_microphone_gain(acl_handle, 15);
  332. break;
  333. case 'u':
  334. log_info("USER:\'%c\'", cmd);
  335. printf("Send 'user busy' (Three-Way Call 0)\n");
  336. hfp_hf_user_busy(acl_handle);
  337. break;
  338. case 'U':
  339. log_info("USER:\'%c\'", cmd);
  340. printf("End active call and accept waiting/held call (Three-Way Call 1)\n");
  341. hfp_hf_end_active_and_accept_other(acl_handle);
  342. break;
  343. case 'v':
  344. log_info("USER:\'%c\'", cmd);
  345. printf("Swap active call and hold/waiting call (Three-Way Call 2)\n");
  346. hfp_hf_swap_calls(acl_handle);
  347. break;
  348. case 'V':
  349. log_info("USER:\'%c\'", cmd);
  350. printf("Join hold call (Three-Way Call 3)\n");
  351. hfp_hf_join_held_call(acl_handle);
  352. break;
  353. case 'w':
  354. log_info("USER:\'%c\'", cmd);
  355. printf("Connect calls (Three-Way Call 4)\n");
  356. hfp_hf_connect_calls(acl_handle);
  357. break;
  358. case 'W':
  359. log_info("USER:\'%c\'", cmd);
  360. printf("Redial\n");
  361. hfp_hf_redial_last_number(acl_handle);
  362. break;
  363. case 'x':
  364. log_info("USER:\'%c\'", cmd);
  365. printf("Request phone number for voice tag\n");
  366. hfp_hf_request_phone_number_for_voice_tag(acl_handle);
  367. break;
  368. case 'X':
  369. log_info("USER:\'%c\'", cmd);
  370. printf("Query current call status\n");
  371. hfp_hf_query_current_call_status(acl_handle);
  372. break;
  373. case 'y':
  374. log_info("USER:\'%c\'", cmd);
  375. printf("Release call with index 2\n");
  376. hfp_hf_release_call_with_index(acl_handle, 2);
  377. break;
  378. case 'Y':
  379. log_info("USER:\'%c\'", cmd);
  380. printf("Private consulation with call 2\n");
  381. hfp_hf_private_consultation_with_call(acl_handle, 2);
  382. break;
  383. case '[':
  384. log_info("USER:\'%c\'", cmd);
  385. printf("Query Response and Hold status (RHH ?)\n");
  386. hfp_hf_rrh_query_status(acl_handle);
  387. break;
  388. case ']':
  389. log_info("USER:\'%c\'", cmd);
  390. printf("Place call in a response and held state (RHH 0)\n");
  391. hfp_hf_rrh_hold_call(acl_handle);
  392. break;
  393. case '{':
  394. log_info("USER:\'%c\'", cmd);
  395. printf("Accept held call (RHH 1)\n");
  396. hfp_hf_rrh_accept_held_call(acl_handle);
  397. break;
  398. case '}':
  399. log_info("USER:\'%c\'", cmd);
  400. printf("Reject held call (RHH 2)\n");
  401. hfp_hf_rrh_reject_held_call(acl_handle);
  402. break;
  403. case '?':
  404. log_info("USER:\'%c\'", cmd);
  405. printf("Query Subscriber Number\n");
  406. hfp_hf_query_subscriber_number(acl_handle);
  407. break;
  408. case '!':
  409. log_info("USER:\'%c\'", cmd);
  410. printf("Update HF indicator with assigned number 1 (HFI)\n");
  411. hfp_hf_set_hf_indicator(acl_handle, 1, 1);
  412. break;
  413. default:
  414. show_usage();
  415. break;
  416. }
  417. }
  418. #endif
  419. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
  420. UNUSED(channel);
  421. switch (packet_type){
  422. case HCI_SCO_DATA_PACKET:
  423. if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
  424. sco_demo_receive(event, event_size);
  425. break;
  426. case HCI_EVENT_PACKET:
  427. switch (event[0]){
  428. case HCI_EVENT_SCO_CAN_SEND_NOW:
  429. sco_demo_send(sco_handle);
  430. break;
  431. case HCI_EVENT_COMMAND_COMPLETE:
  432. if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
  433. dump_supported_codecs();
  434. }
  435. break;
  436. case HCI_EVENT_HFP_META:
  437. switch (event[2]) {
  438. case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
  439. acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
  440. hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
  441. printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr));
  442. break;
  443. case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
  444. acl_handle = HCI_CON_HANDLE_INVALID;
  445. printf("Service level connection released.\n\n");
  446. break;
  447. case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
  448. if (hfp_subevent_audio_connection_established_get_status(event)){
  449. printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
  450. } else {
  451. sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
  452. printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
  453. negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
  454. switch (negotiated_codec){
  455. case 0x01:
  456. printf("Using CVSD codec.\n");
  457. break;
  458. case 0x02:
  459. printf("Using mSBC codec.\n");
  460. break;
  461. default:
  462. printf("Using unknown codec 0x%02x.\n", negotiated_codec);
  463. break;
  464. }
  465. sco_demo_set_codec(negotiated_codec);
  466. hci_request_sco_can_send_now_event();
  467. }
  468. break;
  469. case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
  470. sco_handle = HCI_CON_HANDLE_INVALID;
  471. printf("Audio connection released\n");
  472. sco_demo_close();
  473. break;
  474. case HFP_SUBEVENT_COMPLETE:
  475. switch (cmd){
  476. case 'd':
  477. printf("HFP AG registration status update enabled.\n");
  478. break;
  479. case 'e':
  480. printf("HFP AG registration status update for individual indicators set.\n");
  481. break;
  482. default:
  483. break;
  484. }
  485. break;
  486. case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
  487. printf("AG_INDICATOR_STATUS_CHANGED, AG indicator (index: %d) to: %d of range [%d, %d], name '%s'\n",
  488. hfp_subevent_ag_indicator_status_changed_get_indicator_index(event),
  489. hfp_subevent_ag_indicator_status_changed_get_indicator_status(event),
  490. hfp_subevent_ag_indicator_status_changed_get_indicator_min_range(event),
  491. hfp_subevent_ag_indicator_status_changed_get_indicator_max_range(event),
  492. (const char*) hfp_subevent_ag_indicator_status_changed_get_indicator_name(event));
  493. break;
  494. case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
  495. printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n",
  496. hfp_subevent_network_operator_changed_get_network_operator_mode(event),
  497. hfp_subevent_network_operator_changed_get_network_operator_format(event),
  498. (char *) hfp_subevent_network_operator_changed_get_network_operator_name(event));
  499. break;
  500. case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
  501. printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n",
  502. hfp_subevent_extended_audio_gateway_error_get_error(event));
  503. break;
  504. case HFP_SUBEVENT_RING:
  505. printf("** Ring **\n");
  506. break;
  507. case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG:
  508. printf("Phone number for voice tag: %s\n",
  509. (const char *) hfp_subevent_number_for_voice_tag_get_number(event));
  510. break;
  511. case HFP_SUBEVENT_SPEAKER_VOLUME:
  512. printf("Speaker volume: gain %u\n",
  513. hfp_subevent_speaker_volume_get_gain(event));
  514. break;
  515. case HFP_SUBEVENT_MICROPHONE_VOLUME:
  516. printf("Microphone volume: gain %u\n",
  517. hfp_subevent_microphone_volume_get_gain(event));
  518. break;
  519. case HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION:
  520. printf("Caller ID, number %s\n", hfp_subevent_calling_line_identification_notification_get_number(event));
  521. break;
  522. case HFP_SUBEVENT_ENHANCED_CALL_STATUS:
  523. printf("Enhanced call status:\n");
  524. printf(" - call index: %d \n", hfp_subevent_enhanced_call_status_get_clcc_idx(event));
  525. printf(" - direction : %s \n", hfp_enhanced_call_dir2str(hfp_subevent_enhanced_call_status_get_clcc_dir(event)));
  526. printf(" - status : %s \n", hfp_enhanced_call_status2str(hfp_subevent_enhanced_call_status_get_clcc_status(event)));
  527. printf(" - mode : %s \n", hfp_enhanced_call_mode2str(hfp_subevent_enhanced_call_status_get_clcc_mode(event)));
  528. printf(" - multipart : %s \n", hfp_enhanced_call_mpty2str(hfp_subevent_enhanced_call_status_get_clcc_mpty(event)));
  529. printf(" - type : %d \n", hfp_subevent_enhanced_call_status_get_bnip_type(event));
  530. printf(" - number : %s \n", hfp_subevent_enhanced_call_status_get_bnip_number(event));
  531. break;
  532. default:
  533. printf("event not handled %u\n", event[2]);
  534. break;
  535. }
  536. break;
  537. default:
  538. break;
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. /* @section Main Application Setup
  546. *
  547. * @text Listing MainConfiguration shows main application code.
  548. * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it.
  549. * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers.
  550. * The stdin_process callback allows for sending commands to the HFP AG.
  551. * At the end the Bluetooth stack is started.
  552. */
  553. /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */
  554. int btstack_main(int argc, const char * argv[]);
  555. int btstack_main(int argc, const char * argv[]){
  556. (void)argc;
  557. (void)argv;
  558. sco_demo_init();
  559. gap_discoverable_control(1);
  560. gap_set_class_of_device(0x200408);
  561. gap_set_local_name("HFP HF Demo 00:00:00:00:00:00");
  562. // init L2CAP
  563. l2cap_init();
  564. uint16_t hf_supported_features =
  565. (1<<HFP_HFSF_ESCO_S4) |
  566. (1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) |
  567. (1<<HFP_HFSF_HF_INDICATORS) |
  568. (1<<HFP_HFSF_CODEC_NEGOTIATION) |
  569. (1<<HFP_HFSF_ENHANCED_CALL_STATUS) |
  570. (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
  571. int wide_band_speech = 1;
  572. rfcomm_init();
  573. hfp_hf_init(rfcomm_channel_nr);
  574. hfp_hf_init_supported_features(hf_supported_features);
  575. hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
  576. hfp_hf_init_codecs(sizeof(codecs), codecs);
  577. sdp_init();
  578. memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
  579. hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
  580. printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
  581. sdp_register_service(hfp_service_buffer);
  582. // register for HCI events and SCO packets
  583. hci_event_callback_registration.callback = &packet_handler;
  584. hci_add_event_handler(&hci_event_callback_registration);
  585. hci_register_sco_packet_handler(&packet_handler);
  586. hci_register_sco_packet_handler(&packet_handler);
  587. // register for HFP events
  588. hfp_hf_register_packet_handler(packet_handler);
  589. #ifdef HAVE_BTSTACK_STDIN
  590. // parse human readable Bluetooth address
  591. sscanf_bd_addr(device_addr_string, device_addr);
  592. btstack_stdin_setup(stdin_process);
  593. #endif
  594. // turn on!
  595. hci_power_control(HCI_POWER_ON);
  596. return 0;
  597. }
  598. /* LISTING_END */
  599. /* EXAMPLE_END */