1 /******************************************************************************
2  *
3  *  Copyright 2009-2013 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef GAP_API_H
20 #define GAP_API_H
21 
22 #include <cstdint>
23 
24 #include "profiles_api.h"
25 #include "stack/include/bt_hdr.h"
26 #include "stack/include/l2cap_types.h"
27 #include "types/bt_transport.h"
28 #include "types/raw_address.h"
29 
30 /*****************************************************************************
31  *  Constants
32  ****************************************************************************/
33 /*** GAP Error and Status Codes ***/
34 /* An illegal parameter was detected */
35 #define GAP_ERR_ILL_PARM (GAP_ERR_GRP + 0x09)
36 
37 /* Bad GAP handle */
38 #define GAP_ERR_BAD_HANDLE (GAP_ERR_GRP + 0x0e)
39 /* Connection is in invalid state */
40 #define GAP_ERR_BAD_STATE (GAP_ERR_GRP + 0x10)
41 /* No data available */
42 #define GAP_NO_DATA_AVAIL (GAP_ERR_GRP + 0x11)
43 #define GAP_EVT_CONN_OPENED 0x0100
44 #define GAP_EVT_CONN_CLOSED 0x0101
45 #define GAP_EVT_CONN_DATA_AVAIL 0x0102
46 #define GAP_EVT_CONN_CONGESTED 0x0103
47 #define GAP_EVT_CONN_UNCONGESTED 0x0104
48 #define GAP_EVT_TX_EMPTY 0x0105
49 
50 /*** used in connection variables and functions ***/
51 #define GAP_INVALID_HANDLE 0xFFFF
52 
53 #ifndef GAP_PREFER_CONN_INT_MAX
54 #define GAP_PREFER_CONN_INT_MAX BTM_BLE_CONN_INT_MIN
55 #endif
56 
57 #ifndef GAP_PREFER_CONN_INT_MIN
58 #define GAP_PREFER_CONN_INT_MIN BTM_BLE_CONN_INT_MIN
59 #endif
60 
61 #ifndef GAP_PREFER_CONN_LATENCY
62 #define GAP_PREFER_CONN_LATENCY 0
63 #endif
64 
65 #ifndef GAP_PREFER_CONN_SP_TOUT
66 #define GAP_PREFER_CONN_SP_TOUT 2000
67 #endif
68 
69 struct tGAP_COC_CREDITS {
70   uint16_t credits_received;
71   uint16_t credit_count;
72 };
73 
74 struct tGAP_L2CAP_CIDS {
75   uint16_t local_cid;
76   uint16_t remote_cid;
77 };
78 
79 union tGAP_CB_DATA {
80   tGAP_COC_CREDITS coc_credits;
81   tL2CAP_CONN l2cap_result;
82   tGAP_L2CAP_CIDS l2cap_cids;
83 };
84 
85 /*****************************************************************************
86  *  Type Definitions
87  ****************************************************************************/
88 /*
89  * Callback function for connection services
90  */
91 typedef void(tGAP_CONN_CALLBACK)(uint16_t gap_handle, uint16_t event, tGAP_CB_DATA* data);
92 
93 typedef struct {
94   uint16_t int_min;
95   uint16_t int_max;
96   uint16_t latency;
97   uint16_t sp_tout;
98 } tGAP_BLE_PREF_PARAM;
99 
100 typedef union {
101   tGAP_BLE_PREF_PARAM conn_param;
102   RawAddress reconn_bda;
103   uint16_t icon;
104   uint8_t* p_dev_name;
105   uint8_t addr_resolution;
106 } tGAP_BLE_ATTR_VALUE;
107 
108 typedef void(tGAP_BLE_CMPL_CBACK)(bool status, const RawAddress& addr, uint16_t length,
109                                   char* p_name);
110 
111 /*****************************************************************************
112  *  External Function Declarations
113  ****************************************************************************/
114 
115 /*** Functions for L2CAP connection interface ***/
116 
117 /*******************************************************************************
118  *
119  * Function         GAP_ConnOpen
120  *
121  * Description      This function is called to open a generic L2CAP connection.
122  *
123  * Returns          handle of the connection if successful, else
124  *                  GAP_INVALID_HANDLE
125  *
126  ******************************************************************************/
127 uint16_t GAP_ConnOpen(const char* p_serv_name, uint8_t service_id, bool is_server,
128                       const RawAddress* p_rem_bda, uint16_t psm, uint16_t le_mps,
129                       tL2CAP_CFG_INFO* p_cfg, tL2CAP_ERTM_INFO* ertm_info, uint16_t security,
130                       tGAP_CONN_CALLBACK* p_cb, tBT_TRANSPORT transport);
131 
132 /*******************************************************************************
133  *
134  * Function         GAP_ConnClose
135  *
136  * Description      This function is called to close a connection.
137  *
138  * Returns          BT_PASS             - closed OK
139  *                  GAP_ERR_BAD_HANDLE  - invalid handle
140  *
141  ******************************************************************************/
142 uint16_t GAP_ConnClose(uint16_t gap_handle);
143 
144 /*******************************************************************************
145  *
146  * Function         GAP_ConnReadData
147  *
148  * Description      GKI buffer unaware application will call this function
149  *                  after receiving GAP_EVT_RXDATA event. A data copy is made
150  *                  into the receive buffer parameter.
151  *
152  * Returns          BT_PASS             - data read
153  *                  GAP_ERR_BAD_HANDLE  - invalid handle
154  *                  GAP_NO_DATA_AVAIL   - no data available
155  *
156  ******************************************************************************/
157 uint16_t GAP_ConnReadData(uint16_t gap_handle, uint8_t* p_data, uint16_t max_len, uint16_t* p_len);
158 
159 /*******************************************************************************
160  *
161  * Function         GAP_GetRxQueueCnt
162  *
163  * Description      This function return number of bytes on the rx queue.
164  *
165  * Parameters:      handle     - Handle returned in the GAP_ConnOpen
166  *                  p_rx_queue_count - Pointer to return queue count in.
167  *
168  *
169  ******************************************************************************/
170 int GAP_GetRxQueueCnt(uint16_t handle, uint32_t* p_rx_queue_count);
171 
172 /*******************************************************************************
173  *
174  * Function         GAP_ConnWriteData
175  *
176  * Description      GKI buffer unaware application will call this function
177  *                  to send data to the connection. A data copy is made into a
178  *                  GKI buffer.
179  *
180  * Returns          BT_PASS                 - data read
181  *                  GAP_ERR_BAD_HANDLE      - invalid handle
182  *                  GAP_ERR_BAD_STATE       - connection not established
183  *                  GAP_CONGESTION          - system is congested
184  *
185  ******************************************************************************/
186 uint16_t GAP_ConnWriteData(uint16_t gap_handle, BT_HDR* msg);
187 
188 /*******************************************************************************
189  *
190  * Function         GAP_ConnGetRemoteAddr
191  *
192  * Description      This function is called to get the remote BD address
193  *                  of a connection.
194  *
195  * Returns          BT_PASS             - closed OK
196  *                  GAP_ERR_BAD_HANDLE  - invalid handle
197  *
198  ******************************************************************************/
199 const RawAddress* GAP_ConnGetRemoteAddr(uint16_t gap_handle);
200 
201 /*******************************************************************************
202  *
203  * Function         GAP_ConnGetRemMtuSize
204  *
205  * Description      Returns the remote device's MTU size.
206  *
207  * Returns          uint16_t - maximum size buffer that can be transmitted to
208  *                             the peer
209  *
210  ******************************************************************************/
211 uint16_t GAP_ConnGetRemMtuSize(uint16_t gap_handle);
212 
213 /*******************************************************************************
214  *
215  * Function         GAP_ConnGetL2CAPCid
216  *
217  * Description      Returns the L2CAP channel id
218  *
219  * Parameters:      handle      - Handle of the connection
220  *
221  * Returns          uint16_t    - The L2CAP channel id
222  *                  0, if error
223  *
224  ******************************************************************************/
225 uint16_t GAP_ConnGetL2CAPCid(uint16_t gap_handle);
226 
227 /*******************************************************************************
228  *
229  * Function         GAP_GetLeChannelInfo
230  *
231  * Description      This function is called to get LE L2CAP channel information
232  *                  by the gap handle. All OUT parameters must NOT be nullptr.
233  *
234  * Parameters:      handle        - Handle of the port returned in the Open
235  *                  remote_mtu    - OUT remote L2CAP MTU
236  *                  local_mps     - OUT local L2CAP COC MPS
237  *                  remote_mps    - OUT remote L2CAP COC MPS
238  *                  local_credit  - OUT local L2CAP COC credit
239  *                  remote_credit - OUT remote L2CAP COC credit
240  *                  local_cid     - OUT local L2CAP CID
241  *                  remote_cid    - OUT remote L2CAP CID
242  *                  acl_handle    - OUT ACL handle
243  *
244  * Returns          true if request accepted
245  *
246  ******************************************************************************/
247 bool GAP_GetLeChannelInfo(uint16_t gap_handle, uint16_t* remote_mtu, uint16_t* local_mps,
248                           uint16_t* remote_mps, uint16_t* local_credit, uint16_t* remote_credit,
249                           uint16_t* local_cid, uint16_t* remote_cid, uint16_t* acl_handle);
250 
251 /*******************************************************************************
252  *
253  * Function         GAP_IsTransportLe
254  *
255  * Description      This function returns if the transport is LE by the gap handle.
256  *
257  * Parameters:      handle        - Handle of the port returned in the Open
258  *
259  * Returns          true if transport is LE, else false
260  *
261  ******************************************************************************/
262 bool GAP_IsTransportLe(uint16_t gap_handle);
263 
264 /*******************************************************************************
265  *
266  * Function         GAP_Init
267  *
268  * Description      Initializes the control blocks used by GAP.
269  *                  This routine should not be called except once per
270  *                      stack invocation.
271  *
272  * Returns          Nothing
273  *
274  ******************************************************************************/
275 void GAP_Init(void);
276 
277 /*******************************************************************************
278  *
279  * Function         GAP_BleAttrDBUpdate
280  *
281  * Description      update GAP local BLE attribute database.
282  *
283  * Returns          Nothing
284  *
285  ******************************************************************************/
286 void GAP_BleAttrDBUpdate(uint16_t attr_uuid, tGAP_BLE_ATTR_VALUE* p_value);
287 
288 /*******************************************************************************
289  *
290  * Function         GAP_BleReadPeerPrefConnParams
291  *
292  * Description      Start a process to read a connected peripheral's preferred
293  *                  connection parameters
294  *
295  * Returns          true if read started, else false if GAP is busy
296  *
297  ******************************************************************************/
298 bool GAP_BleReadPeerPrefConnParams(const RawAddress& peer_bda);
299 
300 /*******************************************************************************
301  *
302  * Function         GAP_BleReadPeerDevName
303  *
304  * Description      Start a process to read a connected peripheral's device
305  *                  name.
306  *
307  * Returns          true if request accepted
308  *
309  ******************************************************************************/
310 bool GAP_BleReadPeerDevName(const RawAddress& peer_bda, tGAP_BLE_CMPL_CBACK* p_cback);
311 
312 /*******************************************************************************
313  *
314  * Function         GAP_BleReadPeerAppearance
315  *
316  * Description      Start a process to read a connected peripheral's appearance.
317  *
318  * Returns          true if request accepted
319  *
320  ******************************************************************************/
321 bool GAP_BleReadPeerAppearance(const RawAddress& peer_bda, tGAP_BLE_CMPL_CBACK* p_cback);
322 
323 /*******************************************************************************
324  *
325  * Function         GAP_BleCancelReadPeerDevName
326  *
327  * Description      Cancel reading a peripheral's device name.
328  *
329  * Returns          true if request accepted
330  *
331  ******************************************************************************/
332 bool GAP_BleCancelReadPeerDevName(const RawAddress& peer_bda);
333 
334 #endif /* GAP_API_H */
335