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 "btm_api.h"
25 #include "l2c_api.h"
26 #include "l2cdefs.h"
27 #include "profiles_api.h"
28 #include "stack/include/bt_hdr.h"
29 #include "types/bt_transport.h"
30 #include "types/raw_address.h"
31 
32 /*****************************************************************************
33  *  Constants
34  ****************************************************************************/
35 /*** GAP Error and Status Codes ***/
36 /* An illegal parameter was detected */
37 #define GAP_ERR_ILL_PARM (GAP_ERR_GRP + 0x09)
38 
39 /* Bad GAP handle */
40 #define GAP_ERR_BAD_HANDLE (GAP_ERR_GRP + 0x0e)
41 /* Connection is in invalid state */
42 #define GAP_ERR_BAD_STATE (GAP_ERR_GRP + 0x10)
43 /* No data available */
44 #define GAP_NO_DATA_AVAIL (GAP_ERR_GRP + 0x11)
45 #define GAP_EVT_CONN_OPENED 0x0100
46 #define GAP_EVT_CONN_CLOSED 0x0101
47 #define GAP_EVT_CONN_DATA_AVAIL 0x0102
48 #define GAP_EVT_CONN_CONGESTED 0x0103
49 #define GAP_EVT_CONN_UNCONGESTED 0x0104
50 #define GAP_EVT_TX_EMPTY 0x0105
51 
52 /*** used in connection variables and functions ***/
53 #define GAP_INVALID_HANDLE 0xFFFF
54 
55 #ifndef GAP_PREFER_CONN_INT_MAX
56 #define GAP_PREFER_CONN_INT_MAX BTM_BLE_CONN_INT_MIN
57 #endif
58 
59 #ifndef GAP_PREFER_CONN_INT_MIN
60 #define GAP_PREFER_CONN_INT_MIN BTM_BLE_CONN_INT_MIN
61 #endif
62 
63 #ifndef GAP_PREFER_CONN_LATENCY
64 #define GAP_PREFER_CONN_LATENCY 0
65 #endif
66 
67 #ifndef GAP_PREFER_CONN_SP_TOUT
68 #define GAP_PREFER_CONN_SP_TOUT 2000
69 #endif
70 
71 struct tGAP_COC_CREDITS {
72   uint16_t credits_received;
73   uint16_t credit_count;
74 };
75 
76 struct tGAP_L2CAP_CIDS {
77   uint16_t local_cid;
78   uint16_t remote_cid;
79 };
80 
81 union tGAP_CB_DATA {
82   tGAP_COC_CREDITS coc_credits;
83   uint16_t l2cap_result;
84   tGAP_L2CAP_CIDS l2cap_cids;
85 };
86 
87 /*****************************************************************************
88  *  Type Definitions
89  ****************************************************************************/
90 /*
91  * Callback function for connection services
92 */
93 typedef void(tGAP_CONN_CALLBACK)(uint16_t gap_handle, uint16_t event,
94                                  tGAP_CB_DATA* data);
95 
96 typedef struct {
97   uint16_t int_min;
98   uint16_t int_max;
99   uint16_t latency;
100   uint16_t sp_tout;
101 } tGAP_BLE_PREF_PARAM;
102 
103 typedef union {
104   tGAP_BLE_PREF_PARAM conn_param;
105   RawAddress reconn_bda;
106   uint16_t icon;
107   uint8_t* p_dev_name;
108   uint8_t addr_resolution;
109 
110 } tGAP_BLE_ATTR_VALUE;
111 
112 typedef void(tGAP_BLE_CMPL_CBACK)(bool status, const RawAddress& addr,
113                                   uint16_t length, char* p_name);
114 
115 /*****************************************************************************
116  *  External Function Declarations
117  ****************************************************************************/
118 
119 /*** Functions for L2CAP connection interface ***/
120 
121 /*******************************************************************************
122  *
123  * Function         GAP_ConnOpen
124  *
125  * Description      This function is called to open a generic L2CAP connection.
126  *
127  * Returns          handle of the connection if successful, else
128  *                  GAP_INVALID_HANDLE
129  *
130  ******************************************************************************/
131 uint16_t GAP_ConnOpen(const char* p_serv_name, uint8_t service_id,
132                       bool is_server, const RawAddress* p_rem_bda, uint16_t psm,
133                       uint16_t le_mps, tL2CAP_CFG_INFO* p_cfg,
134                       tL2CAP_ERTM_INFO* ertm_info, uint16_t security,
135                       tGAP_CONN_CALLBACK* p_cb, tBT_TRANSPORT transport);
136 
137 /*******************************************************************************
138  *
139  * Function         GAP_ConnClose
140  *
141  * Description      This function is called to close a connection.
142  *
143  * Returns          BT_PASS             - closed OK
144  *                  GAP_ERR_BAD_HANDLE  - invalid handle
145  *
146  ******************************************************************************/
147 uint16_t GAP_ConnClose(uint16_t gap_handle);
148 
149 /*******************************************************************************
150  *
151  * Function         GAP_ConnReadData
152  *
153  * Description      GKI buffer unaware application will call this function
154  *                  after receiving GAP_EVT_RXDATA event. A data copy is made
155  *                  into the receive buffer parameter.
156  *
157  * Returns          BT_PASS             - data read
158  *                  GAP_ERR_BAD_HANDLE  - invalid handle
159  *                  GAP_NO_DATA_AVAIL   - no data available
160  *
161  ******************************************************************************/
162 uint16_t GAP_ConnReadData(uint16_t gap_handle, uint8_t* p_data,
163                           uint16_t max_len, uint16_t* p_len);
164 
165 /*******************************************************************************
166  *
167  * Function         GAP_GetRxQueueCnt
168  *
169  * Description      This function return number of bytes on the rx queue.
170  *
171  * Parameters:      handle     - Handle returned in the GAP_ConnOpen
172  *                  p_rx_queue_count - Pointer to return queue count in.
173  *
174  *
175  ******************************************************************************/
176 int GAP_GetRxQueueCnt(uint16_t handle, uint32_t* p_rx_queue_count);
177 
178 /*******************************************************************************
179  *
180  * Function         GAP_ConnWriteData
181  *
182  * Description      GKI buffer unaware application will call this function
183  *                  to send data to the connection. A data copy is made into a
184  *                  GKI buffer.
185  *
186  * Returns          BT_PASS                 - data read
187  *                  GAP_ERR_BAD_HANDLE      - invalid handle
188  *                  GAP_ERR_BAD_STATE       - connection not established
189  *                  GAP_CONGESTION          - system is congested
190  *
191  ******************************************************************************/
192 uint16_t GAP_ConnWriteData(uint16_t gap_handle, BT_HDR* msg);
193 
194 /*******************************************************************************
195  *
196  * Function         GAP_ConnGetRemoteAddr
197  *
198  * Description      This function is called to get the remote BD address
199  *                  of a connection.
200  *
201  * Returns          BT_PASS             - closed OK
202  *                  GAP_ERR_BAD_HANDLE  - invalid handle
203  *
204  ******************************************************************************/
205 const RawAddress* GAP_ConnGetRemoteAddr(uint16_t gap_handle);
206 
207 /*******************************************************************************
208  *
209  * Function         GAP_ConnGetRemMtuSize
210  *
211  * Description      Returns the remote device's MTU size.
212  *
213  * Returns          uint16_t - maximum size buffer that can be transmitted to
214  *                             the peer
215  *
216  ******************************************************************************/
217 uint16_t GAP_ConnGetRemMtuSize(uint16_t gap_handle);
218 
219 /*******************************************************************************
220  *
221  * Function         GAP_ConnGetL2CAPCid
222  *
223  * Description      Returns the L2CAP channel id
224  *
225  * Parameters:      handle      - Handle of the connection
226  *
227  * Returns          uint16_t    - The L2CAP channel id
228  *                  0, if error
229  *
230  ******************************************************************************/
231 uint16_t GAP_ConnGetL2CAPCid(uint16_t gap_handle);
232 
233 /*******************************************************************************
234  *
235  * Function         GAP_Init
236  *
237  * Description      Initializes the control blocks used by GAP.
238  *                  This routine should not be called except once per
239  *                      stack invocation.
240  *
241  * Returns          Nothing
242  *
243  ******************************************************************************/
244 void GAP_Init(void);
245 
246 /*******************************************************************************
247  *
248  * Function         GAP_BleAttrDBUpdate
249  *
250  * Description      update GAP local BLE attribute database.
251  *
252  * Returns          Nothing
253  *
254  ******************************************************************************/
255 void GAP_BleAttrDBUpdate(uint16_t attr_uuid, tGAP_BLE_ATTR_VALUE* p_value);
256 
257 /*******************************************************************************
258  *
259  * Function         GAP_BleReadPeerPrefConnParams
260  *
261  * Description      Start a process to read a connected peripheral's preferred
262  *                  connection parameters
263  *
264  * Returns          true if read started, else false if GAP is busy
265  *
266  ******************************************************************************/
267 bool GAP_BleReadPeerPrefConnParams(const RawAddress& peer_bda);
268 
269 /*******************************************************************************
270  *
271  * Function         GAP_BleReadPeerDevName
272  *
273  * Description      Start a process to read a connected peripheral's device
274  *                  name.
275  *
276  * Returns          true if request accepted
277  *
278  ******************************************************************************/
279 bool GAP_BleReadPeerDevName(const RawAddress& peer_bda,
280                             tGAP_BLE_CMPL_CBACK* p_cback);
281 
282 /*******************************************************************************
283  *
284  * Function         GAP_BleCancelReadPeerDevName
285  *
286  * Description      Cancel reading a peripheral's device name.
287  *
288  * Returns          true if request accepted
289  *
290  ******************************************************************************/
291 bool GAP_BleCancelReadPeerDevName(const RawAddress& peer_bda);
292 
293 #endif /* GAP_API_H */
294