1 /*
2  * Copyright 2016-2023, Cypress Semiconductor Corporation or
3  * an affiliate of Cypress Semiconductor Corporation.  All rights reserved.
4  *
5  * This software, including source code, documentation and related
6  * materials ("Software") is owned by Cypress Semiconductor Corporation
7  * or one of its affiliates ("Cypress") and is protected by and subject to
8  * worldwide patent protection (United States and foreign),
9  * United States copyright laws and international treaty provisions.
10  * Therefore, you may use this Software only as provided in the license
11  * agreement accompanying the software package from which you
12  * obtained this Software ("EULA").
13  * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
14  * non-transferable license to copy, modify, and compile the Software
15  * source code solely for use in connection with Cypress's
16  * integrated circuit products.  Any reproduction, modification, translation,
17  * compilation, or representation of this Software except as specified
18  * above is prohibited without the express written permission of Cypress.
19  *
20  * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
23  * reserves the right to make changes to the Software without notice. Cypress
24  * does not assume any liability arising out of the application or use of the
25  * Software or any product or circuit described in the Software. Cypress does
26  * not authorize its products for use in any products where a malfunction or
27  * failure of the Cypress product may reasonably be expected to result in
28  * significant property damage, injury or death ("High Risk Product"). By
29  * including Cypress's product in a High Risk Product, the manufacturer
30  * of such system or application assumes all risk of such use and in doing
31  * so agrees to indemnify Cypress against all liability.
32  */
33 
34 /** @file
35  *
36  * Bluetooth Synchronous Connection Oriented Channel Application Programming Interface
37  *
38  */
39 #pragma once
40 
41 
42 /**
43  * @cond DUAL_MODE
44  * @addtogroup  sco      Synchronous Connection Oriented (SCO) Channel
45  * @ingroup     wicedbt_sco
46  *
47  * @{
48  */
49 
50 
51 /******************************************************
52  *              Constants
53  ******************************************************/
54 #define WICED_BT_SCO_CONNECTION_ACCEPT              0x00 /**< Status accept connection */
55 #define WICED_BT_SCO_CONNECTION_REJECT_RESOURCES    0x0D /**< Status reject connection due to no resources */
56 #define WICED_BT_SCO_CONNECTION_REJECT_SECURITY     0x0E /**< Status reject connection due to security */
57 #define WICED_BT_SCO_CONNECTION_REJECT_DEVICE       0x0F /**< Status reject connection from peer */
58 
59 #ifndef WICED_SCO_PKT_TYPES_MASK
60 #define WICED_INVALID_SCO_INDEX           0xFFFF    /**< Default SCO index */
61 #define WICED_SCO_LINK_ALL_PKT_MASK       0x003F    /**< SCO packet type all */
62 #define WICED_SCO_PKT_TYPES_MASK_HV3      0x0004    /**< SCO packet type HV3 */
63 #define WICED_SCO_PKT_TYPES_MASK_EV3      0x0008    /**< SCO packet type EV3 */
64 #define WICED_SCO_PKT_TYPES_MASK_EV4      0x0010    /**< SCO packet type EV4 */
65 #define WICED_SCO_PKT_TYPES_MASK_EV5      0x0020    /**< SCO packet type EV5 */
66 #define WICED_SCO_PKT_TYPES_MASK_NO_2_EV3 0x0040    /**< SCO packet type 2-EV3 */
67 #define WICED_SCO_PKT_TYPES_MASK_NO_3_EV3 0x0080    /**< SCO packet type 3-EV3 */
68 #define WICED_SCO_PKT_TYPES_MASK_NO_2_EV5 0x0100    /**< SCO packet type 2-EV5 */
69 #define WICED_SCO_PKT_TYPES_MASK_NO_3_EV5 0x0200    /**< SCO packet type 3-EV5 */
70 #endif
71 
72 /** SCO route path */
73 typedef enum
74 {
75      WICED_BT_SCO_OVER_HCI,         /**< SCO over HCI to Wiced stack in the host */
76      WICED_BT_SCO_OVER_PCM    /**< Not supported yet. PCM data config for routing over I2S/PCM interface */
77 }wiced_bt_sco_route_path_t;
78 
79 /******************************************************
80  *              Type Definitions
81  ******************************************************/
82 #define  WICED_BT_SCO_DATA_CB_GET_LENGTH(ltch_len)   ((ltch_len>>8)&0xff)	/**< SCO data callback length */
83 
84 /** Call back function for pcm data transfer, ltch_len = (length)<<8|(sco_channel) */
85 typedef void (wiced_bt_sco_data_cb_t) (uint16_t sco_channel, uint16_t length, uint8_t *p_data);
86 
87 
88 /** Subset for the enhanced setup/accept synchronous connection paramters
89  * See BT 4.1 or later HCI spec for details */
90 typedef struct
91 {
92     uint16_t max_latency;                   /**< Maximum latency (0x4-0xFFFE in msecs) */
93     uint16_t packet_types;                  /**< Packet Types */
94     uint8_t retrans_effort;                 /**< 0x00-0x02, 0xFF don't care */
95     wiced_bool_t use_wbs;                   /**< True to use wide band, False to use narrow band */
96 } wiced_bt_sco_params_t;
97 
98 /** SCO path config */
99 typedef struct
100 {
101     wiced_bt_sco_route_path_t    path;           /**< sco routing path see #wiced_bt_sco_route_path_t*/
102     wiced_bt_sco_data_cb_t       *p_sco_data_cb; /**< If not NULL and route is APP_CB, callback function called for incoming pcm data */
103 }wiced_bt_voice_path_setup_t;
104 
105 
106 /******************************************************
107  *              Function Declarations
108  ******************************************************/
109 #ifdef __cplusplus
110 extern "C"
111 {
112 #endif
113 
114 /**
115  *The wiced_sco_lib.a was required to included before we link this function.
116  *Creates a synchronous connection oriented connection as initiator.
117  *
118  *  @param[in]  bd_addr                 : Peer bd_addr
119  *  @param[in]  p_params                : Pointer to the SCO parameter structure
120  *  @param[out] p_sco_index             : SCO index returned
121  *
122  *  @return     <b> WICED_BT_UNKNOWN_ADDR </b>      : Create connection failed, ACL connection is not up
123  *              <b> WICED_BT_BUSY </b>              : Create connection failed, another SCO is being
124  *                                                    conncted to the same BD address
125  *              <b> WICED_BT_WRONG_MODE </b>        : Create connection failed, wrong mode
126  *              <b> WICED_BT_NO_RESOURCES </b>      : Create connection failed, max SCO limit has been
127  *                                                    reached
128  *              <b> WICED_BT_PENDING </b>            : Create connection successfully, "p_sco_index" is returned
129  */
130 wiced_bt_dev_status_t wiced_bt_sco_create_as_initiator (wiced_bt_device_address_t bd_addr,
131                                                         uint16_t *p_sco_index,
132                                                         wiced_bt_sco_params_t *p_params);
133 
134 /**
135  * Creates a synchronous connection oriented connection as acceptor.
136  *
137  *  @param[in]  remote_bda              :  remote device bd_addr
138  *  @param[out] p_sco_index             : SCO index returned
139  *
140  *  @return     <b> WICED_BT_UNKNOWN_ADDR </b>      : Create connection failed, ACL connection is not up or
141  *                                                    address is invalid
142  *              <b> WICED_BT_BUSY </b>              : Create connection failed, a SCO connection is already
143  *                                                    conncted to the same BD address
144  *              <b> WICED_BT_WRONG_MODE </b>        : Create connection failed, link in park mode or
145  *                                                    automatic un-park is not supported
146  *              <b> WICED_BT_NO_RESOURCES </b>      : Create connection failed, max SCO limit has been
147  *                                                    reached
148  *              <b> WICED_BT_PENDING </b>            : Create connection successfully, "p_sco_index" is returned
149  */
150 wiced_bt_dev_status_t wiced_bt_sco_create_as_acceptor_ex (wiced_bt_device_address_t remote_bda, uint16_t *p_sco_index);
151 
152 /**   defined this macro for backward compatiblity */
153 #define wiced_bt_sco_create_as_acceptor(p_sco_index) wiced_bt_sco_create_as_acceptor_ex(NULL, p_sco_index)
154 
155 
156 /**
157  * Removes a specific synchronous connection oriented connection.
158  *
159  *  @param[in]  sco_index                           : SCO index to remove
160  *
161  *  @return     <b> WICED_BT_UNKNOWN_ADDR </b>      : Remove connection failed, invalid SCO index
162  *              <b> WICED_BT_NO_RESOURCES </b>      : Remove connection failed, no resource
163  *              <b> WICED_BT_SUCCESS </b>           : Remove connection successfully, device is still
164  *                                                    listening for incoming connection
165  *              <b> WICED_BT_PENDING </b>           : Remove connection successfully
166  */
167 wiced_bt_dev_status_t wiced_bt_sco_remove (uint16_t sco_index);
168 
169 /**
170  *
171  *The wiced_sco_lib.a was required to be included before we link this function.
172  *Called to handle (e)SCO connection request event (wiced_bt_sco_connect_request_event).
173  *
174  *
175  *  @param[in]  sco_index           : SCO index
176  *
177  *  @param[in]  hci_status          : WICED_BT_SCO_CONNECTION_ACCEPT              0x00
178  *                                    WICED_BT_SCO_CONNECTION_REJECT_RESOURCES    0x0D
179  *                                    WICED_BT_SCO_CONNECTION_REJECT_SECURITY     0x0E
180  *                                    WICED_BT_SCO_CONNECTION_REJECT_DEVICE       0x0F
181  *  @param[in]  p_params            : Pointer to the SCO parameter structure
182  *
183  */
184 void wiced_bt_sco_accept_connection (uint16_t sco_index, uint8_t hci_status,
185                                      wiced_bt_sco_params_t *p_params);
186 
187 
188 /**
189  *
190  *The wiced_sco_lib.a was required to include before we link this function.
191  *Configure the SCO routing path.
192  *
193  *  @param[in]  pData                 : To setup SCO routing path
194  *
195  * @return
196  *              WICED_BT_SUCCESS    : Config success.
197  *              WICED_BT_PENDING    : Command sent. Waiting for command complete event.
198  *              WICED_BT_BUSY       : Command not sent. Waiting for command complete event for prior command.
199  */
200 wiced_bt_dev_status_t wiced_bt_sco_setup_voice_path(wiced_bt_voice_path_setup_t *pData);
201 
202 
203 /**
204  *
205  *The wiced_sco_lib.a was required to include before we link this function.
206  *This function write SCO data to a specified instance
207  *
208  *  @param[in]  sco_inx             : sco connection instance
209  *  @param[in]  p_data              : Pointer to  data
210  *  @param[in]  len                 : Length of data at p_data
211  *
212  * @return
213  *              WICED_BT_SUCCESS            : data write is successful.
214  *              WICED_BT_ERROR              : generic error
215  *              WICED_BT_UNKNOWN_ADDR       : unknown SCO connection instance.
216  */
217 
218 wiced_bt_dev_status_t wiced_bt_sco_write_buffer (uint16_t sco_inx, uint8_t *p_data, uint8_t len);
219 
220 
221 
222 /**
223  *
224  *The wiced_sco_lib.a was required to include before we link this function.
225  *Send pcm data to internal audio pipe.
226  *
227  *
228  *  @param[in]  sco_index     : SCO index to send the stream
229  *  @param[in]  p_pcmsrc        : Audio stream source.
230  *  @param[in]  len             : Length of stream.
231  *
232  *  @return                     : Return the legth of non transmited stream.
233  */
234 uint16_t wiced_bt_sco_output_stream( uint16_t sco_index, uint8_t* p_pcmsrc,uint16_t len );
235 
236 /**
237  *
238  *The wiced_voice_path.a was required to include before we link this function.
239  *To turn off the PCM/I2S hardware clock
240  *This function needs to be called after the application turns off (or mutes audio data in/out of) the codec
241  *
242  *  @return                     : None
243  */
244 
245 void  wiced_bt_sco_turn_off_pcm_clock( void );
246 
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 /** @} sco */
253 /* @endcond*/
254