hfp_ag_demo.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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_ag_demo.c"
  38. /*
  39. * hfp_ag_demo.c
  40. */
  41. // *****************************************************************************
  42. /* EXAMPLE_START(hfp_ag_demo): HFP Audio Gateway (AG) Demo
  43. *
  44. * @text This HFP Audio Gateway example demonstrates how to receive
  45. * an output from a remote HFP Hands-Free (HF) unit, and,
  46. * if HAVE_BTSTACK_STDIN is defined, how to control the HFP HF.
  47. */
  48. // *****************************************************************************
  49. #include <stdint.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include "btstack.h"
  54. #include "sco_demo_util.h"
  55. #ifdef HAVE_BTSTACK_STDIN
  56. #include "btstack_stdin.h"
  57. #endif
  58. // uncomment to temp disable mSBC codec
  59. // #undef ENABLE_HFP_WIDE_BAND_SPEECH
  60. uint8_t hfp_service_buffer[150];
  61. const uint8_t rfcomm_channel_nr = 1;
  62. const char hfp_ag_service_name[] = "HFP AG Demo";
  63. static bd_addr_t device_addr;
  64. static const char * device_addr_string = "00:21:3C:AC:F7:38";
  65. #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
  66. static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
  67. #else
  68. static uint8_t codecs[] = {HFP_CODEC_CVSD};
  69. #endif
  70. static uint8_t negotiated_codec = HFP_CODEC_CVSD;
  71. static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
  72. static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
  73. static int memory_1_enabled = 1;
  74. static btstack_packet_callback_registration_t hci_event_callback_registration;
  75. static int ag_indicators_nr = 7;
  76. static hfp_ag_indicator_t ag_indicators[] = {
  77. // index, name, min range, max range, status, mandatory, enabled, status changed
  78. {1, "service", 0, 1, 1, 0, 0, 0},
  79. {2, "call", 0, 1, 0, 1, 1, 0},
  80. {3, "callsetup", 0, 3, 0, 1, 1, 0},
  81. {4, "battchg", 0, 5, 3, 0, 0, 0},
  82. {5, "signal", 0, 5, 5, 0, 1, 0},
  83. {6, "roam", 0, 1, 0, 0, 1, 0},
  84. {7, "callheld", 0, 2, 0, 1, 1, 0}
  85. };
  86. static int call_hold_services_nr = 5;
  87. static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"};
  88. static int hf_indicators_nr = 2;
  89. static hfp_generic_status_indicator_t hf_indicators[] = {
  90. {1, 1},
  91. {2, 1},
  92. };
  93. #define INQUIRY_INTERVAL 5
  94. enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
  95. enum STATE state = INIT;
  96. static void dump_supported_codecs(void){
  97. unsigned int i;
  98. int mSBC_skipped = 0;
  99. printf("Supported codecs: ");
  100. for (i = 0; i < sizeof(codecs); i++){
  101. switch(codecs[i]){
  102. case HFP_CODEC_CVSD:
  103. printf("CVSD");
  104. break;
  105. case HFP_CODEC_MSBC:
  106. if (hci_extended_sco_link_supported()){
  107. printf(", mSBC");
  108. } else {
  109. mSBC_skipped = 1;
  110. }
  111. break;
  112. }
  113. }
  114. printf("\n");
  115. if (mSBC_skipped){
  116. printf("mSBC codec disabled because eSCO not supported by local controller.\n");
  117. }
  118. }
  119. #ifdef HAVE_BTSTACK_STDIN
  120. // Testig User Interface
  121. static void show_usage(void){
  122. bd_addr_t iut_address;
  123. gap_local_bd_addr(iut_address);
  124. printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
  125. printf("\n");
  126. printf("a - establish HFP connection to %s\n", bd_addr_to_str(device_addr));
  127. // printf("A - release HFP connection\n");
  128. printf("b - establish AUDIO connection | B - release AUDIO connection\n");
  129. printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n");
  130. printf("d - report AG failure\n");
  131. printf("e - answer call on AG | E - reject call on AG\n");
  132. printf("r - disable in-band ring tone | R - enable in-band ring tone\n");
  133. printf("f - Disable cellular network | F - Enable cellular network\n");
  134. printf("g - Set signal strength to 0 | G - Set signal strength to 5\n");
  135. printf("h - Disable roaming | H - Enable roaming\n");
  136. printf("i - Set battery level to 3 | I - Set battery level to 5\n");
  137. printf("j - Answering call on remote side\n");
  138. printf("k - Clear memory #1 | K - Set memory #1\n");
  139. printf("l - Clear last number | L - Set last number\n");
  140. printf("m - simulate incoming call from 7654321\n");
  141. // printf("M - simulate call from 7654321 dropped\n");
  142. printf("n - Disable Voice Regocnition | N - Enable Voice Recognition\n");
  143. printf("o - Set speaker volume to 0 (minimum) | O - Set speaker volume to 9 (default)\n");
  144. printf("p - Set speaker volume to 12 (higher) | P - Set speaker volume to 15 (maximum)\n");
  145. printf("q - Set microphone gain to 0 (minimum) | Q - Set microphone gain to 9 (default)\n");
  146. printf("s - Set microphone gain to 12 (higher) | S - Set microphone gain to 15 (maximum)\n");
  147. printf("t - terminate connection\n");
  148. printf("u - join held call\n");
  149. printf("v - discover nearby HF units\n");
  150. printf("w - put incoming call on hold (Response and Hold)\n");
  151. printf("x - accept held incoming call (Response and Hold)\n");
  152. printf("X - reject held incoming call (Response and Hold)\n");
  153. printf("---\n");
  154. }
  155. static void stdin_process(char cmd){
  156. switch (cmd){
  157. case 'a':
  158. log_info("USER:\'%c\'", cmd);
  159. printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
  160. hfp_ag_establish_service_level_connection(device_addr);
  161. break;
  162. case 'A':
  163. log_info("USER:\'%c\'", cmd);
  164. printf("Release HFP service level connection.\n");
  165. hfp_ag_release_service_level_connection(acl_handle);
  166. break;
  167. case 'Z':
  168. log_info("USER:\'%c\'", cmd);
  169. printf("Release HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
  170. hfp_ag_release_service_level_connection(acl_handle);
  171. break;
  172. case 'b':
  173. log_info("USER:\'%c\'", cmd);
  174. printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
  175. hfp_ag_establish_audio_connection(acl_handle);
  176. break;
  177. case 'B':
  178. log_info("USER:\'%c\'", cmd);
  179. printf("Release Audio connection.\n");
  180. hfp_ag_release_audio_connection(acl_handle);
  181. break;
  182. case 'c':
  183. log_info("USER:\'%c\'", cmd);
  184. printf("Simulate incoming call from 1234567\n");
  185. hfp_ag_set_clip(129, "1234567");
  186. hfp_ag_incoming_call();
  187. break;
  188. case 'm':
  189. log_info("USER:\'%c\'", cmd);
  190. printf("Simulate incoming call from 7654321\n");
  191. hfp_ag_set_clip(129, "7654321");
  192. hfp_ag_incoming_call();
  193. break;
  194. case 'C':
  195. log_info("USER:\'%c\'", cmd);
  196. printf("Simulate terminate call\n");
  197. hfp_ag_call_dropped();
  198. break;
  199. case 'd':
  200. log_info("USER:\'%c\'", cmd);
  201. printf("Report AG failure\n");
  202. hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
  203. break;
  204. case 'e':
  205. log_info("USER:\'%c\'", cmd);
  206. printf("Answer call on AG\n");
  207. hfp_ag_answer_incoming_call();
  208. break;
  209. case 'E':
  210. log_info("USER:\'%c\'", cmd);
  211. printf("Reject call on AG\n");
  212. hfp_ag_terminate_call();
  213. break;
  214. case 'f':
  215. log_info("USER:\'%c\'", cmd);
  216. printf("Disable cellular network\n");
  217. hfp_ag_set_registration_status(0);
  218. break;
  219. case 'F':
  220. log_info("USER:\'%c\'", cmd);
  221. printf("Enable cellular network\n");
  222. hfp_ag_set_registration_status(1);
  223. break;
  224. case 'g':
  225. log_info("USER:\'%c\'", cmd);
  226. printf("Set signal strength to 0\n");
  227. hfp_ag_set_signal_strength(0);
  228. break;
  229. case 'G':
  230. log_info("USER:\'%c\'", cmd);
  231. printf("Set signal strength to 5\n");
  232. hfp_ag_set_signal_strength(5);
  233. break;
  234. case 'h':
  235. log_info("USER:\'%c\'", cmd);
  236. printf("Disable roaming\n");
  237. hfp_ag_set_roaming_status(0);
  238. break;
  239. case 'H':
  240. log_info("USER:\'%c\'", cmd);
  241. printf("Enable roaming\n");
  242. hfp_ag_set_roaming_status(1);
  243. break;
  244. case 'i':
  245. log_info("USER:\'%c\'", cmd);
  246. printf("Set battery level to 3\n");
  247. hfp_ag_set_battery_level(3);
  248. break;
  249. case 'I':
  250. log_info("USER:\'%c\'", cmd);
  251. printf("Set battery level to 5\n");
  252. hfp_ag_set_battery_level(5);
  253. break;
  254. case 'j':
  255. log_info("USER:\'%c\'", cmd);
  256. printf("Answering call on remote side\n");
  257. hfp_ag_outgoing_call_established();
  258. break;
  259. case 'r':
  260. log_info("USER:\'%c\'", cmd);
  261. printf("Disable in-band ring tone\n");
  262. hfp_ag_set_use_in_band_ring_tone(0);
  263. break;
  264. case 'k':
  265. log_info("USER:\'%c\'", cmd);
  266. printf("Memory 1 cleared\n");
  267. memory_1_enabled = 0;
  268. break;
  269. case 'K':
  270. log_info("USER:\'%c\'", cmd);
  271. printf("Memory 1 set\n");
  272. memory_1_enabled = 1;
  273. break;
  274. case 'l':
  275. log_info("USER:\'%c\'", cmd);
  276. printf("Last dialed number cleared\n");
  277. hfp_ag_clear_last_dialed_number();
  278. break;
  279. case 'L':
  280. log_info("USER:\'%c\'", cmd);
  281. printf("Outgoing call connected, ringing\n");
  282. hfp_ag_outgoing_call_ringing();
  283. break;
  284. case 'n':
  285. log_info("USER:\'%c\'", cmd);
  286. printf("Disable Voice Recognition\n");
  287. hfp_ag_activate_voice_recognition(acl_handle, 0);
  288. break;
  289. case 'N':
  290. log_info("USER:\'%c\'", cmd);
  291. printf("Enable Voice Recognition\n");
  292. hfp_ag_activate_voice_recognition(acl_handle, 1);
  293. break;
  294. case 'o':
  295. log_info("USER:\'%c\'", cmd);
  296. printf("Set speaker gain to 0 (minimum)\n");
  297. hfp_ag_set_speaker_gain(acl_handle, 0);
  298. break;
  299. case 'O':
  300. log_info("USER:\'%c\'", cmd);
  301. printf("Set speaker gain to 9 (default)\n");
  302. hfp_ag_set_speaker_gain(acl_handle, 9);
  303. break;
  304. case 'p':
  305. log_info("USER:\'%c\'", cmd);
  306. printf("Set speaker gain to 12 (higher)\n");
  307. hfp_ag_set_speaker_gain(acl_handle, 12);
  308. break;
  309. case 'P':
  310. log_info("USER:\'%c\'", cmd);
  311. printf("Set speaker gain to 15 (maximum)\n");
  312. hfp_ag_set_speaker_gain(acl_handle, 15);
  313. break;
  314. case 'q':
  315. log_info("USER:\'%c\'", cmd);
  316. printf("Set microphone gain to 0\n");
  317. hfp_ag_set_microphone_gain(acl_handle, 0);
  318. break;
  319. case 'Q':
  320. log_info("USER:\'%c\'", cmd);
  321. printf("Set microphone gain to 9\n");
  322. hfp_ag_set_microphone_gain(acl_handle, 9);
  323. break;
  324. case 's':
  325. log_info("USER:\'%c\'", cmd);
  326. printf("Set microphone gain to 12\n");
  327. hfp_ag_set_microphone_gain(acl_handle, 12);
  328. break;
  329. case 'S':
  330. log_info("USER:\'%c\'", cmd);
  331. printf("Set microphone gain to 15\n");
  332. hfp_ag_set_microphone_gain(acl_handle, 15);
  333. break;
  334. case 'R':
  335. log_info("USER:\'%c\'", cmd);
  336. printf("Enable in-band ring tone\n");
  337. hfp_ag_set_use_in_band_ring_tone(1);
  338. break;
  339. case 't':
  340. log_info("USER:\'%c\'", cmd);
  341. printf("Terminate HCI connection. 0x%2x\n", acl_handle);
  342. gap_disconnect(acl_handle);
  343. break;
  344. case 'u':
  345. log_info("USER:\'%c\'", cmd);
  346. printf("Join held call\n");
  347. hfp_ag_join_held_call();
  348. break;
  349. case 'v':
  350. printf("Start scanning...\n");
  351. gap_inquiry_start(INQUIRY_INTERVAL);
  352. break;
  353. case 'w':
  354. log_info("USER:\'%c\'", cmd);
  355. printf("AG: Put incoming call on hold (Response and Hold)\n");
  356. hfp_ag_hold_incoming_call();
  357. break;
  358. case 'x':
  359. log_info("USER:\'%c\'", cmd);
  360. printf("AG: Accept held incoming call (Response and Hold)\n");
  361. hfp_ag_accept_held_incoming_call();
  362. break;
  363. case 'X':
  364. log_info("USER:\'%c\'", cmd);
  365. printf("AG: Reject held incoming call (Response and Hold)\n");
  366. hfp_ag_reject_held_incoming_call();
  367. break;
  368. default:
  369. show_usage();
  370. break;
  371. }
  372. }
  373. #endif
  374. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
  375. UNUSED(channel);
  376. bd_addr_t addr;
  377. uint8_t status;
  378. switch (packet_type){
  379. case HCI_EVENT_PACKET:
  380. switch(hci_event_packet_get_type(event)){
  381. #ifndef HAVE_BTSTACK_STDIN
  382. case BTSTACK_EVENT_STATE:
  383. if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
  384. printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
  385. hfp_ag_establish_service_level_connection(device_addr);
  386. break;
  387. #endif
  388. case GAP_EVENT_INQUIRY_RESULT:
  389. gap_event_inquiry_result_get_bd_addr(event, addr);
  390. // print info
  391. printf("Device found: %s ", bd_addr_to_str(addr));
  392. printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
  393. if (gap_event_inquiry_result_get_rssi_available(event)){
  394. printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
  395. }
  396. if (gap_event_inquiry_result_get_name_available(event)){
  397. char name_buffer[240];
  398. int name_len = gap_event_inquiry_result_get_name_len(event);
  399. memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
  400. name_buffer[name_len] = 0;
  401. printf(", name '%s'", name_buffer);
  402. }
  403. printf("\n");
  404. break;
  405. case GAP_EVENT_INQUIRY_COMPLETE:
  406. printf("Inquiry scan complete.\n");
  407. break;
  408. case HCI_EVENT_SCO_CAN_SEND_NOW:
  409. sco_demo_send(sco_handle);
  410. break;
  411. case HCI_EVENT_COMMAND_COMPLETE:
  412. if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
  413. dump_supported_codecs();
  414. }
  415. break;
  416. default:
  417. break;
  418. }
  419. if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
  420. if ((event_size > 3)
  421. && (event[3] != 0)
  422. && (hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER)
  423. && (hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG)
  424. && (hci_event_hfp_meta_get_subevent_code(event) != HFP_SUBEVENT_TRANSMIT_DTMF_CODES)){
  425. printf("ERROR, status: %u\n", event[3]);
  426. return;
  427. }
  428. switch (hci_event_hfp_meta_get_subevent_code(event)) {
  429. case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
  430. status = hfp_subevent_service_level_connection_established_get_status(event);
  431. if (status){
  432. printf("Connection failed, staus 0x%02x\n", status);
  433. break;
  434. }
  435. acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
  436. hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
  437. printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
  438. dump_supported_codecs();
  439. #ifndef HAVE_BTSTACK_STDIN
  440. log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
  441. printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
  442. hfp_ag_establish_audio_connection(acl_handle);
  443. #endif
  444. break;
  445. case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
  446. printf("Service level connection released.\n");
  447. acl_handle = HCI_CON_HANDLE_INVALID;
  448. break;
  449. case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
  450. if (hfp_subevent_audio_connection_established_get_status(event)){
  451. printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
  452. } else {
  453. sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
  454. printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
  455. negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
  456. switch (negotiated_codec){
  457. case 0x01:
  458. printf("Using CVSD codec.\n");
  459. break;
  460. case 0x02:
  461. printf("Using mSBC codec.\n");
  462. break;
  463. default:
  464. printf("Using unknown codec 0x%02x.\n", negotiated_codec);
  465. break;
  466. }
  467. sco_demo_set_codec(negotiated_codec);
  468. hci_request_sco_can_send_now_event();
  469. }
  470. break;
  471. case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
  472. printf("Audio connection released\n");
  473. sco_handle = HCI_CON_HANDLE_INVALID;
  474. sco_demo_close();
  475. break;
  476. case HFP_SUBEVENT_START_RINGINIG:
  477. printf("Start Ringing\n");
  478. break;
  479. case HFP_SUBEVENT_STOP_RINGINIG:
  480. printf("Stop Ringing\n");
  481. break;
  482. case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
  483. printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
  484. // validate number
  485. if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
  486. || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
  487. || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
  488. printf("Dialstring valid: accept call\n");
  489. hfp_ag_outgoing_call_accepted();
  490. } else {
  491. printf("Dialstring invalid: reject call\n");
  492. hfp_ag_outgoing_call_rejected();
  493. }
  494. break;
  495. case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
  496. printf("Attach number to voice tag. Sending '1234567\n");
  497. hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
  498. break;
  499. case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
  500. printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
  501. hfp_ag_send_dtmf_code_done(acl_handle);
  502. break;
  503. case HFP_SUBEVENT_CALL_ANSWERED:
  504. printf("Call answered by HF\n");
  505. break;
  506. default:
  507. printf("Event not handled %u\n", hci_event_hfp_meta_get_subevent_code(event));
  508. break;
  509. }
  510. break;
  511. case HCI_SCO_DATA_PACKET:
  512. if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
  513. sco_demo_receive(event, event_size);
  514. break;
  515. default:
  516. break;
  517. }
  518. }
  519. static hfp_phone_number_t subscriber_number = {
  520. 129, "225577"
  521. };
  522. /* @section Main Application Setup
  523. *
  524. * @text Listing MainConfiguration shows main application code.
  525. * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
  526. * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
  527. * The stdin_process callback allows for sending commands to the HFP HF.
  528. * At the end the Bluetooth stack is started.
  529. */
  530. /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
  531. int btstack_main(int argc, const char * argv[]);
  532. int btstack_main(int argc, const char * argv[]){
  533. (void)argc;
  534. (void)argv;
  535. sco_demo_init();
  536. gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
  537. gap_discoverable_control(1);
  538. // L2CAP
  539. l2cap_init();
  540. uint16_t supported_features =
  541. (1<<HFP_AGSF_ESCO_S4) |
  542. (1<<HFP_AGSF_HF_INDICATORS) |
  543. (1<<HFP_AGSF_CODEC_NEGOTIATION) |
  544. (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
  545. (1<<HFP_AGSF_ENHANCED_CALL_CONTROL) |
  546. (1<<HFP_AGSF_ENHANCED_CALL_STATUS) |
  547. (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) |
  548. (1<<HFP_AGSF_IN_BAND_RING_TONE) |
  549. (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) |
  550. (1<<HFP_AGSF_THREE_WAY_CALLING);
  551. int wide_band_speech = 1;
  552. // HFP
  553. rfcomm_init();
  554. hfp_ag_init(rfcomm_channel_nr);
  555. hfp_ag_init_supported_features(supported_features);
  556. hfp_ag_init_codecs(sizeof(codecs), codecs);
  557. hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
  558. hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
  559. hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
  560. hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
  561. // SDP Server
  562. sdp_init();
  563. memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
  564. hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
  565. printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
  566. sdp_register_service(hfp_service_buffer);
  567. // register for HCI events and SCO packets
  568. hci_event_callback_registration.callback = &packet_handler;
  569. hci_add_event_handler(&hci_event_callback_registration);
  570. hci_register_sco_packet_handler(&packet_handler);
  571. // register for HFP events
  572. hfp_ag_register_packet_handler(&packet_handler);
  573. // parse humand readable Bluetooth address
  574. sscanf_bd_addr(device_addr_string, device_addr);
  575. #ifdef HAVE_BTSTACK_STDIN
  576. btstack_stdin_setup(stdin_process);
  577. #endif
  578. // turn on!
  579. hci_power_control(HCI_POWER_ON);
  580. return 0;
  581. }
  582. /* LISTING_END */
  583. /* EXAMPLE_END */