1 /******************************************************************************
2 *
3 * Copyright 2001-2012 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 /******************************************************************************
20 *
21 * this file contains the PAN API definitions
22 *
23 ******************************************************************************/
24 #ifndef PAN_API_H
25 #define PAN_API_H
26
27 #include <base/strings/stringprintf.h>
28 #include <bluetooth/log.h>
29
30 #include <cstdint>
31
32 #include "bnep_api.h"
33 #include "macros.h"
34 #include "stack/include/bt_hdr.h"
35 #include "types/raw_address.h"
36
37 /*****************************************************************************
38 * Constants
39 ****************************************************************************/
40
41 /* Define the minimum offset needed in a GKI buffer for
42 * sending PAN packets. Note, we are currently not sending
43 * extension headers, but may in the future, so allow
44 * space for them
45 */
46 #define PAN_MINIMUM_OFFSET BNEP_MINIMUM_OFFSET
47
48 /*
49 * The handle is passed from BNEP to PAN. The same handle is used
50 * between PAN and application as well
51 */
52 #define PAN_INVALID_HANDLE BNEP_INVALID_HANDLE
53
54 /* Bit map for PAN roles */
55 #define PAN_ROLE_CLIENT 0x01 /* PANU role */
56 #define PAN_ROLE_GROUP 0x02 /* Adhoc network group role */
57 #define PAN_ROLE_NAP_SERVER 0x04 /* NAP role */
58 typedef uint8_t tPAN_ROLE;
59
pan_role_to_text(const tPAN_ROLE & role)60 inline const std::string pan_role_to_text(const tPAN_ROLE& role) {
61 return base::StringPrintf("%c%c%c[0x%x]", (role & PAN_ROLE_CLIENT) ? 'C' : '.',
62 (role & PAN_ROLE_GROUP) ? 'G' : '.',
63 (role & PAN_ROLE_NAP_SERVER) ? 'N' : '.', role);
64 }
65
66 /*****************************************************************************
67 * Type Definitions
68 ****************************************************************************/
69
70 /* Define the result codes from PAN */
71 typedef enum : uint8_t {
72 PAN_SUCCESS, /* Success */
73 PAN_DISCONNECTED = BNEP_CONN_DISCONNECTED, /* Connection terminated */
74 PAN_CONN_FAILED = BNEP_CONN_FAILED, /* Connection failed */
75 PAN_NO_RESOURCES = BNEP_NO_RESOURCES, /* No resources */
76 PAN_MTU_EXCEEDED = BNEP_MTU_EXCEEDED, /* Attempt to write long data */
77 PAN_INVALID_OFFSET = BNEP_INVALID_OFFSET, /* Insufficient offset in GKI buffer */
78 PAN_CONN_FAILED_CFG = BNEP_CONN_FAILED_CFG, /* Connection failed cos of config */
79 PAN_INVALID_SRC_ROLE = BNEP_CONN_FAILED_SRC_UUID, /* Connection failed wrong source UUID */
80 PAN_INVALID_DST_ROLE = BNEP_CONN_FAILED_DST_UUID, /* Connection failed wrong destination UUID */
81 PAN_CONN_FAILED_UUID_SIZE = BNEP_CONN_FAILED_UUID_SIZE, /* Connection failed wrong size UUID */
82 PAN_Q_SIZE_EXCEEDED = BNEP_Q_SIZE_EXCEEDED, /* Too many buffers to dest */
83 PAN_TOO_MANY_FILTERS = BNEP_TOO_MANY_FILTERS, /* Too many local filters specified */
84 PAN_SET_FILTER_FAIL = BNEP_SET_FILTER_FAIL, /* Set Filter failed */
85 PAN_WRONG_HANDLE = BNEP_WRONG_HANDLE, /* Wrong handle for the connection */
86 PAN_WRONG_STATE = BNEP_WRONG_STATE, /* Connection is in wrong state */
87 PAN_SECURITY_FAIL = BNEP_SECURITY_FAIL, /* Failed because of security */
88 PAN_IGNORE_CMD = BNEP_IGNORE_CMD, /* To ignore the rcvd command */
89 PAN_TX_FLOW_ON = BNEP_TX_FLOW_ON, /* tx data flow enabled */
90 PAN_TX_FLOW_OFF = BNEP_TX_FLOW_OFF, /* tx data flow disabled */
91 PAN_FAILURE = 19, /* Failure */
92 PAN_HOTSPOT_DISABLED = 20, /* Hotspot disabled */
93 } tPAN_RESULT;
94
pan_result_text(const tPAN_RESULT & result)95 inline const std::string pan_result_text(const tPAN_RESULT& result) {
96 switch (result) {
97 CASE_RETURN_TEXT(PAN_SUCCESS);
98 CASE_RETURN_TEXT(PAN_DISCONNECTED);
99 CASE_RETURN_TEXT(PAN_CONN_FAILED);
100 CASE_RETURN_TEXT(PAN_NO_RESOURCES);
101 CASE_RETURN_TEXT(PAN_MTU_EXCEEDED);
102 CASE_RETURN_TEXT(PAN_INVALID_OFFSET);
103 CASE_RETURN_TEXT(PAN_CONN_FAILED_CFG);
104 CASE_RETURN_TEXT(PAN_INVALID_SRC_ROLE);
105 CASE_RETURN_TEXT(PAN_INVALID_DST_ROLE);
106 CASE_RETURN_TEXT(PAN_CONN_FAILED_UUID_SIZE);
107 CASE_RETURN_TEXT(PAN_Q_SIZE_EXCEEDED);
108 CASE_RETURN_TEXT(PAN_TOO_MANY_FILTERS);
109 CASE_RETURN_TEXT(PAN_SET_FILTER_FAIL);
110 CASE_RETURN_TEXT(PAN_WRONG_HANDLE);
111 CASE_RETURN_TEXT(PAN_WRONG_STATE);
112 CASE_RETURN_TEXT(PAN_SECURITY_FAIL);
113 CASE_RETURN_TEXT(PAN_IGNORE_CMD);
114 CASE_RETURN_TEXT(PAN_TX_FLOW_ON);
115 CASE_RETURN_TEXT(PAN_TX_FLOW_OFF);
116 CASE_RETURN_TEXT(PAN_FAILURE);
117 CASE_RETURN_TEXT(PAN_HOTSPOT_DISABLED);
118 default:
119 return base::StringPrintf("UNKNOWN[%hhu]", result);
120 }
121 }
122
123 /*****************************************************************
124 * Callback Function Prototypes
125 ****************************************************************/
126
127 /* This is call back function used to report connection status
128 * to the application. The second parameter true means
129 * to create the bridge and false means to remove it.
130 */
131 typedef void(tPAN_CONN_STATE_CB)(uint16_t handle, const RawAddress& bd_addr, tPAN_RESULT state,
132 bool is_role_change, uint8_t src_role, uint8_t dst_role);
133
134 /* This is call back function used to create bridge for the
135 * Connected device. The parameter "state" indicates
136 * whether to create the bridge or remove it. true means
137 * to create the bridge and false means to remove it.
138 */
139 typedef void(tPAN_BRIDGE_REQ_CB)(const RawAddress& bd_addr, bool state);
140
141 /* Data received indication callback prototype. Parameters are
142 * Source BD/Ethernet Address
143 * Dest BD/Ethernet address
144 * Protocol
145 * Address of buffer (or data if non-GKI)
146 * Length of data (non-GKI)
147 * ext is flag to indicate whether it has aby extension headers
148 * Flag used to indicate to forward on LAN
149 * false - Use it for internal stack
150 * true - Send it across the ethernet as well
151 */
152 typedef void(tPAN_DATA_IND_CB)(uint16_t handle, const RawAddress& src, const RawAddress& dst,
153 uint16_t protocol, uint8_t* p_data, uint16_t len, bool ext,
154 bool forward);
155
156 /* Data buffer received indication callback prototype. Parameters are
157 * Source BD/Ethernet Address
158 * Dest BD/Ethernet address
159 * Protocol
160 * pointer to the data buffer
161 * ext is flag to indicate whether it has aby extension headers
162 * Flag used to indicate to forward on LAN
163 * false - Use it for internal stack
164 * true - Send it across the ethernet as well
165 */
166 typedef void(tPAN_DATA_BUF_IND_CB)(uint16_t handle, const RawAddress& src, const RawAddress& dst,
167 uint16_t protocol, BT_HDR* p_buf, bool ext, bool forward);
168
169 /* Flow control callback for TX data. Parameters are
170 * Handle to the connection
171 * Event flow status
172 */
173 typedef void(tPAN_TX_DATA_FLOW_CB)(uint16_t handle, tPAN_RESULT event);
174
175 /* Filters received indication callback prototype. Parameters are
176 * Handle to the connection
177 * true if the cb is called for indication
178 * Ignore this if it is indication, otherwise it is the result
179 * for the filter set operation performed by the local
180 * device
181 * Number of protocol filters present
182 * Pointer to the filters start. Filters are present in pairs
183 * of start of the range and end of the range.
184 * They will be present in big endian order. First
185 * two bytes will be starting of the first range and
186 * next two bytes will be ending of the range.
187 */
188 typedef void(tPAN_FILTER_IND_CB)(uint16_t handle, bool indication, tPAN_RESULT result,
189 uint16_t num_filters, uint8_t* p_filters);
190
191 /* Multicast Filters received indication callback prototype. Parameters are
192 * Handle to the connection
193 * true if the cb is called for indication
194 * Ignore this if it is indication, otherwise it is the result
195 * for the filter set operation performed by the local
196 * device
197 * Number of multicast filters present
198 * Pointer to the filters start. Filters are present in pairs
199 * of start of the range and end of the range.
200 * First six bytes will be starting of the first range and
201 * next six bytes will be ending of the range.
202 */
203 typedef void(tPAN_MFILTER_IND_CB)(uint16_t handle, bool indication, tPAN_RESULT result,
204 uint16_t num_mfilters, uint8_t* p_mfilters);
205
206 /* This structure is used to register with PAN profile
207 * It is passed as a parameter to PAN_Register call.
208 */
209 typedef struct {
210 tPAN_CONN_STATE_CB* pan_conn_state_cb; /* Connection state callback */
211 tPAN_BRIDGE_REQ_CB* pan_bridge_req_cb; /* Bridge request callback */
212 tPAN_DATA_IND_CB* pan_data_ind_cb; /* Data indication callback */
213 tPAN_DATA_BUF_IND_CB* pan_data_buf_ind_cb; /* Data buffer indication callback */
214 tPAN_FILTER_IND_CB* pan_pfilt_ind_cb; /* protocol filter indication callback */
215 tPAN_MFILTER_IND_CB* pan_mfilt_ind_cb; /* multicast filter indication callback */
216 tPAN_TX_DATA_FLOW_CB* pan_tx_data_flow_cb; /* data flow callback */
217 } tPAN_REGISTER;
218
219 /*****************************************************************************
220 * External Function Declarations
221 ****************************************************************************/
222
223 /*******************************************************************************
224 *
225 * Function PAN_Register
226 *
227 * Description This function is called by the application to register
228 * its callbacks with PAN profile. The application then
229 * should set the PAN role explicitly.
230 *
231 * Parameters: p_register - contains all callback function pointers
232 *
233 *
234 * Returns none
235 *
236 ******************************************************************************/
237 void PAN_Register(tPAN_REGISTER* p_register);
238
239 /*******************************************************************************
240 *
241 * Function PAN_Deregister
242 *
243 * Description This function is called by the application to de-register
244 * its callbacks with PAN profile. This will make the PAN to
245 * become inactive. This will deregister PAN services from SDP
246 * and close all active connections
247 *
248 * Returns none
249 *
250 ******************************************************************************/
251 void PAN_Deregister(void);
252
253 /*******************************************************************************
254 *
255 * Function PAN_SetRole
256 *
257 * Description This function is called by the application to set the PAN
258 * profile role. This should be called after PAN_Register.
259 * This can be called any time to change the PAN role
260 *
261 * Parameters: role - is bit map of roles to be active
262 * PAN_ROLE_CLIENT is for PANU role
263 * PAN_ROLE_NAP_SERVER is for NAP role
264 * sec_mask - Security mask for different roles
265 * It is array of uint8_t. The bytes
266 * represent the security for roles PANU,
267 * GN and NAP in order
268 *
269 * p_user_name - Service name for PANU role
270 * p_nap_name - Service name for NAP role
271 * Can be NULL if user wants it to be default
272 *
273 * Returns PAN_SUCCESS - if the role is set successfully
274 * PAN_FAILURE - if the role is not valid
275 *
276 ******************************************************************************/
277 tPAN_RESULT PAN_SetRole(uint8_t role, std::string user_name, std::string nap_name);
278
279 /*******************************************************************************
280 *
281 * Function PAN_Connect
282 *
283 * Description This function is called by the application to initiate a
284 * connection to the remote device
285 *
286 * Parameters: rem_bda - BD Addr of the remote device
287 * src_role - Role of the local device for the connection
288 * dst_role - Role of the remote device for the connection
289 * PAN_ROLE_CLIENT is for PANU role
290 * PAN_ROLE_NAP_SERVER is for NAP role
291 * *handle - Pointer for returning Handle to the connection
292 *
293 * Returns PAN_SUCCESS - if the connection is initiated successfully
294 * PAN_NO_RESOURCES - resources are not sufficient
295 * PAN_FAILURE - if the connection cannot be initiated
296 * this can be because of the combination of
297 * src and dst roles may not be valid or
298 * allowed at that point of time
299 *
300 ******************************************************************************/
301 tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, tPAN_ROLE src_role, tPAN_ROLE dst_role,
302 uint16_t* handle);
303
304 /*******************************************************************************
305 *
306 * Function PAN_Disconnect
307 *
308 * Description This is used to disconnect the connection
309 *
310 * Parameters: handle - handle for the connection
311 *
312 * Returns PAN_SUCCESS - if the connection is closed successfully
313 * PAN_FAILURE - if the connection is not found or
314 * there is an error in disconnecting
315 *
316 ******************************************************************************/
317 tPAN_RESULT PAN_Disconnect(uint16_t handle);
318
319 /*******************************************************************************
320 *
321 * Function PAN_Write
322 *
323 * Description This sends data over the PAN connections. If this is called
324 * on GN or NAP side and the packet is multicast or broadcast
325 * it will be sent on all the links. Otherwise the correct link
326 * is found based on the destination address and forwarded on
327 * it. If the return value is not PAN_SUCCESS the application
328 * should take care of releasing the message buffer
329 *
330 * Parameters: dst - MAC or BD Addr of the destination device
331 * src - MAC or BD Addr of the source who sent this packet
332 * protocol - protocol of the ethernet packet like IP or ARP
333 * p_data - pointer to the data
334 * len - length of the data
335 * ext - to indicate that extension headers present
336 *
337 * Returns PAN_SUCCESS - if the data is sent successfully
338 * PAN_FAILURE - if the connection is not found or
339 * there is an error in sending data
340 *
341 ******************************************************************************/
342 tPAN_RESULT PAN_Write(uint16_t handle, const RawAddress& dst, const RawAddress& src,
343 uint16_t protocol, uint8_t* p_data, uint16_t len, bool ext);
344
345 /*******************************************************************************
346 *
347 * Function PAN_WriteBuf
348 *
349 * Description This sends data over the PAN connections. If this is called
350 * on GN or NAP side and the packet is multicast or broadcast
351 * it will be sent on all the links. Otherwise the correct link
352 * is found based on the destination address and forwarded on
353 * it. If the return value is not PAN_SUCCESS the application
354 * should take care of releasing the message buffer
355 *
356 * Parameters: dst - MAC or BD Addr of the destination device
357 * src - MAC or BD Addr of the source who sent this packet
358 * protocol - protocol of the ethernet packet like IP or ARP
359 * p_buf - pointer to the data buffer
360 * ext - to indicate that extension headers present
361 *
362 * Returns PAN_SUCCESS - if the data is sent successfully
363 * PAN_FAILURE - if the connection is not found or
364 * there is an error in sending data
365 *
366 ******************************************************************************/
367 tPAN_RESULT PAN_WriteBuf(uint16_t handle, const RawAddress& dst, const RawAddress& src,
368 uint16_t protocol, BT_HDR* p_buf, bool ext);
369
370 /*******************************************************************************
371 *
372 * Function PAN_SetProtocolFilters
373 *
374 * Description This function is used to set protocol filters on the peer
375 *
376 * Parameters: handle - handle for the connection
377 * num_filters - number of protocol filter ranges
378 * start - array of starting protocol numbers
379 * end - array of ending protocol numbers
380 *
381 *
382 * Returns PAN_SUCCESS if protocol filters are set successfully
383 * PAN_FAILURE if connection not found or error in setting
384 *
385 ******************************************************************************/
386 tPAN_RESULT PAN_SetProtocolFilters(uint16_t handle, uint16_t num_filters, uint16_t* p_start_array,
387 uint16_t* p_end_array);
388
389 /*******************************************************************************
390 *
391 * Function PAN_SetMulticastFilters
392 *
393 * Description This function is used to set multicast filters on the peer
394 *
395 * Parameters: handle - handle for the connection
396 * num_filters - number of multicast filter ranges
397 * p_start_array - Pointer to sequence of beginings of all
398 * multicast address ranges
399 * p_end_array - Pointer to sequence of ends of all
400 * multicast address ranges
401 *
402 *
403 * Returns PAN_SUCCESS if multicast filters are set successfully
404 * PAN_FAILURE if connection not found or error in setting
405 *
406 ******************************************************************************/
407 tPAN_RESULT PAN_SetMulticastFilters(uint16_t handle, uint16_t num_mcast_filters,
408 uint8_t* p_start_array, uint8_t* p_end_array);
409
410 /*******************************************************************************
411 *
412 * Function PAN_Init
413 *
414 * Description This function initializes the PAN unit. It should be called
415 * before accessing any other APIs to initialize the control
416 * block.
417 *
418 * Returns void
419 *
420 ******************************************************************************/
421 void PAN_Init(void);
422
423 void PAN_Dumpsys(int fd);
424
425 namespace std {
426 template <>
427 struct formatter<tPAN_RESULT> : enum_formatter<tPAN_RESULT> {};
428 } // namespace std
429
430 #endif /* PAN_API_H */
431