hsp_ag_demo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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__ "hsp_ag_demo.c"
  38. /*
  39. * hsp_ag_demo.c
  40. */
  41. // *****************************************************************************
  42. /* EXAMPLE_START(hsp_ag_demo): HSP Audio Gateway Demo
  43. *
  44. * @text This example implements a HSP Audio Gateway device that sends and receives
  45. * audio signal over HCI SCO. It demonstrates how to receive
  46. * an output from a remote headset (HS), and,
  47. * if HAVE_BTSTACK_STDIN is defined, how to control the HS.
  48. */
  49. // *****************************************************************************
  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. #ifdef HAVE_BTSTACK_STDIN
  57. #include "btstack_stdin.h"
  58. #endif
  59. static uint8_t hsp_service_buffer[150];
  60. static const uint8_t rfcomm_channel_nr = 1;
  61. static const char hsp_ag_service_name[] = "Audio Gateway Test";
  62. static uint16_t sco_handle = HCI_CON_HANDLE_INVALID;
  63. static char hs_cmd_buffer[100];
  64. static const char * device_name = "HSP AG Demo 00:00:00:00:00:00";
  65. static const char * device_addr_string = "00:1b:dc:07:32:ef";
  66. static bd_addr_t device_addr;
  67. /* @section Audio Transfer Setup
  68. *
  69. * @text A pre-computed sine wave (160Hz) is used as the input audio signal. 160 Hz.
  70. * To send and receive an audio signal, ENABLE_SCO_OVER_HCI has to be defined.
  71. *
  72. * Tested working setups:
  73. * - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud
  74. * - Ubuntu 14 64-bit, CSR USB dongle
  75. * - OS X 10.11, CSR USB dongle
  76. *
  77. * Broken setups:
  78. * - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud
  79. * - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio
  80. * - looks like bug in select->FTDI driver as it works correct on Linux
  81. *
  82. * SCO not routed over HCI yet:
  83. * - CSR UART dongle
  84. * - Broadcom USB dongle
  85. * - Broadcom UART chipset
  86. * - ..
  87. *
  88. */
  89. #ifdef HAVE_BTSTACK_STDIN
  90. static void show_usage(void){
  91. bd_addr_t iut_address;
  92. gap_local_bd_addr(iut_address);
  93. printf("\n--- Bluetooth HSP Audio Gateway Test Console %s ---\n", bd_addr_to_str(iut_address));
  94. printf("---\n");
  95. printf("c - Connect to %s\n", device_addr_string);
  96. printf("C - Disconnect\n");
  97. printf("a - establish audio connection\n");
  98. printf("A - release audio connection\n");
  99. printf("m - set microphone gain 8\n");
  100. printf("M - set microphone gain 15\n");
  101. printf("o - set speaker gain 0\n");
  102. printf("s - set speaker gain 8\n");
  103. printf("S - set speaker gain 15\n");
  104. printf("r - start ringing\n");
  105. printf("t - stop ringing\n");
  106. printf("\n");
  107. }
  108. static void stdin_process(char c){
  109. switch (c){
  110. case 'c':
  111. printf("Connect to %s\n", device_addr_string);
  112. hsp_ag_connect(device_addr);
  113. break;
  114. case 'C':
  115. printf("Disconnect.\n");
  116. hsp_ag_disconnect();
  117. break;
  118. case 'a':
  119. printf("Establish audio connection\n");
  120. hsp_ag_establish_audio_connection();
  121. break;
  122. case 'A':
  123. printf("Release audio connection\n");
  124. hsp_ag_release_audio_connection();
  125. break;
  126. case 'm':
  127. printf("Setting microphone gain 8\n");
  128. hsp_ag_set_microphone_gain(8);
  129. break;
  130. case 'M':
  131. printf("Setting microphone gain 15\n");
  132. hsp_ag_set_microphone_gain(15);
  133. break;
  134. case 'o':
  135. printf("Setting speaker gain 0\n");
  136. hsp_ag_set_speaker_gain(0);
  137. break;
  138. case 's':
  139. printf("Setting speaker gain 8\n");
  140. hsp_ag_set_speaker_gain(8);
  141. break;
  142. case 'S':
  143. printf("Setting speaker gain 15\n");
  144. hsp_ag_set_speaker_gain(15);
  145. break;
  146. case 'r':
  147. printf("Start ringing\n");
  148. hsp_ag_start_ringing();
  149. break;
  150. case 't':
  151. printf("Stop ringing\n");
  152. hsp_ag_stop_ringing();
  153. break;
  154. default:
  155. show_usage();
  156. break;
  157. }
  158. }
  159. #endif
  160. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
  161. UNUSED(channel);
  162. switch (packet_type){
  163. case HCI_SCO_DATA_PACKET:
  164. if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
  165. sco_demo_receive(event, event_size);
  166. break;
  167. case HCI_EVENT_PACKET:
  168. switch (hci_event_packet_get_type(event)) {
  169. #ifndef HAVE_BTSTACK_STDIN
  170. case BTSTACK_EVENT_STATE:
  171. if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
  172. printf("Establish HSP AG service to %s...\n", device_addr_string);
  173. hsp_ag_connect(device_addr);
  174. break;
  175. #endif
  176. case HCI_EVENT_SCO_CAN_SEND_NOW:
  177. sco_demo_send(sco_handle);
  178. break;
  179. case HCI_EVENT_HSP_META:
  180. switch (event[2]) {
  181. case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE:
  182. if (hsp_subevent_rfcomm_connection_complete_get_status(event)){
  183. printf("RFCOMM connection establishement failed with status %u\n", hsp_subevent_rfcomm_connection_complete_get_status(event));
  184. break;
  185. }
  186. printf("RFCOMM connection established.\n");
  187. #ifndef HAVE_BTSTACK_STDIN
  188. printf("Establish Audio connection to %s...\n", device_addr_string);
  189. hsp_ag_establish_audio_connection();
  190. #endif
  191. break;
  192. case HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE:
  193. if (hsp_subevent_rfcomm_disconnection_complete_get_status(event)){
  194. printf("RFCOMM disconnection failed with status %u.\n", hsp_subevent_rfcomm_disconnection_complete_get_status(event));
  195. } else {
  196. printf("RFCOMM disconnected.\n");
  197. }
  198. break;
  199. case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE:
  200. if (hsp_subevent_audio_connection_complete_get_status(event)){
  201. printf("Audio connection establishment failed with status %u\n", hsp_subevent_audio_connection_complete_get_status(event));
  202. } else {
  203. sco_handle = hsp_subevent_audio_connection_complete_get_handle(event);
  204. printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
  205. hci_request_sco_can_send_now_event();
  206. }
  207. break;
  208. case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE:
  209. printf("Audio connection released.\n\n");
  210. sco_handle = HCI_CON_HANDLE_INVALID;
  211. break;
  212. case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED:
  213. printf("Received microphone gain change %d\n", hsp_subevent_microphone_gain_changed_get_gain(event));
  214. break;
  215. case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED:
  216. printf("Received speaker gain change %d\n", hsp_subevent_speaker_gain_changed_get_gain(event));
  217. break;
  218. case HSP_SUBEVENT_HS_COMMAND:{
  219. memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer));
  220. unsigned int cmd_length = hsp_subevent_hs_command_get_value_length(event);
  221. unsigned int size = cmd_length <= sizeof(hs_cmd_buffer)? cmd_length : sizeof(hs_cmd_buffer);
  222. memcpy(hs_cmd_buffer, hsp_subevent_hs_command_get_value(event), size - 1);
  223. printf("Received custom command: \"%s\". \nExit code or call hsp_ag_send_result.\n", hs_cmd_buffer);
  224. break;
  225. }
  226. default:
  227. printf("event not handled %u\n", event[2]);
  228. break;
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. /* @section Main Application Setup
  240. *
  241. * @text Listing MainConfiguration shows main application code.
  242. * To run a HSP Audio Gateway service you need to initialize the SDP, and to create and register HSP AG record with it.
  243. * In this example, the SCO over HCI is used to receive and send an audio signal.
  244. *
  245. * Two packet handlers are registered:
  246. * - The HCI SCO packet handler receives audio data.
  247. * - The HSP AG packet handler is used to trigger sending of audio data and commands to the HS. It also receives the AG's answers.
  248. *
  249. * The stdin_process callback allows for sending commands to the AG.
  250. * At the end the Bluetooth stack is started.
  251. */
  252. /* LISTING_START(MainConfiguration): Setup HSP Audio Gateway */
  253. int btstack_main(int argc, const char * argv[]);
  254. int btstack_main(int argc, const char * argv[]){
  255. (void)argc;
  256. (void)argv;
  257. sco_demo_init();
  258. sco_demo_set_codec(HFP_CODEC_CVSD);
  259. l2cap_init();
  260. sdp_init();
  261. memset((uint8_t *)hsp_service_buffer, 0, sizeof(hsp_service_buffer));
  262. hsp_ag_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_ag_service_name);
  263. printf("SDP service record size: %u\n", de_get_len(hsp_service_buffer));
  264. sdp_register_service(hsp_service_buffer);
  265. rfcomm_init();
  266. hsp_ag_init(rfcomm_channel_nr);
  267. hsp_ag_register_packet_handler(&packet_handler);
  268. // register for SCO packets
  269. hci_register_sco_packet_handler(&packet_handler);
  270. // parse human readable Bluetooth address
  271. sscanf_bd_addr(device_addr_string, device_addr);
  272. #ifdef HAVE_BTSTACK_STDIN
  273. btstack_stdin_setup(stdin_process);
  274. #endif
  275. gap_set_local_name(device_name);
  276. gap_discoverable_control(1);
  277. gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  278. gap_set_class_of_device(0x400204);
  279. // turn on!
  280. hci_power_control(HCI_POWER_ON);
  281. return 0;
  282. }
  283. /* LISTING_END */
  284. /* EXAMPLE_END */