• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-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 main functions to support PAN profile
22  *  commands and events.
23  *
24  *****************************************************************************/
25 
26 #include "pan_api.h"
27 #include <base/logging.h>
28 #include <string.h>
29 #include "bnep_api.h"
30 #include "bt_common.h"
31 #include "bt_types.h"
32 #include "bta_sys.h"
33 #include "btm_api.h"
34 #include "hcidefs.h"
35 #include "l2c_api.h"
36 #include "pan_int.h"
37 #include "sdp_api.h"
38 #include "sdpdefs.h"
39 
40 /*******************************************************************************
41  *
42  * Function         PAN_Register
43  *
44  * Description      This function is called by the application to register
45  *                  its callbacks with PAN profile. The application then
46  *                  should set the PAN role explicitly.
47  *
48  * Parameters:      p_register - contains all callback function pointers
49  *
50  *
51  * Returns          none
52  *
53  ******************************************************************************/
PAN_Register(tPAN_REGISTER * p_register)54 void PAN_Register(tPAN_REGISTER* p_register) {
55   BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, 0, 0);
56   BTM_SetConnectability(BTM_CONNECTABLE, 0, 0);
57 
58   pan_register_with_bnep();
59 
60   if (!p_register) return;
61 
62   pan_cb.pan_conn_state_cb = p_register->pan_conn_state_cb;
63   pan_cb.pan_bridge_req_cb = p_register->pan_bridge_req_cb;
64   pan_cb.pan_data_buf_ind_cb = p_register->pan_data_buf_ind_cb;
65   pan_cb.pan_data_ind_cb = p_register->pan_data_ind_cb;
66   pan_cb.pan_pfilt_ind_cb = p_register->pan_pfilt_ind_cb;
67   pan_cb.pan_mfilt_ind_cb = p_register->pan_mfilt_ind_cb;
68   pan_cb.pan_tx_data_flow_cb = p_register->pan_tx_data_flow_cb;
69 
70   return;
71 }
72 
73 /*******************************************************************************
74  *
75  * Function         PAN_Deregister
76  *
77  * Description      This function is called by the application to de-register
78  *                  its callbacks with PAN profile. This will make the PAN to
79  *                  become inactive. This will deregister PAN services from SDP
80  *                  and close all active connections
81  *
82  * Parameters:      none
83  *
84  *
85  * Returns          none
86  *
87  ******************************************************************************/
PAN_Deregister(void)88 void PAN_Deregister(void) {
89   pan_cb.pan_bridge_req_cb = NULL;
90   pan_cb.pan_data_buf_ind_cb = NULL;
91   pan_cb.pan_data_ind_cb = NULL;
92   pan_cb.pan_conn_state_cb = NULL;
93   pan_cb.pan_pfilt_ind_cb = NULL;
94   pan_cb.pan_mfilt_ind_cb = NULL;
95 
96   PAN_SetRole(PAN_ROLE_INACTIVE, NULL, NULL, NULL, NULL);
97   BNEP_Deregister();
98 
99   return;
100 }
101 
102 /*******************************************************************************
103  *
104  * Function         PAN_SetRole
105  *
106  * Description      This function is called by the application to set the PAN
107  *                  profile role. This should be called after PAN_Register.
108  *                  This can be called any time to change the PAN role
109  *
110  * Parameters:      role        - is bit map of roles to be active
111  *                                      PAN_ROLE_CLIENT is for PANU role
112  *                                      PAN_ROLE_GN_SERVER is for GN role
113  *                                      PAN_ROLE_NAP_SERVER is for NAP role
114  *                  sec_mask    - Security mask for different roles
115  *                                      It is array of uint8_t. The bytes
116  *                                      represent the security for roles PANU,
117  *                                      GN and NAP in order
118  *                  p_user_name - Service name for PANU role
119  *                  p_gn_name   - Service name for GN role
120  *                  p_nap_name  - Service name for NAP role
121  *                                      Can be NULL if user wants the default
122  *
123  * Returns          PAN_SUCCESS     - if the role is set successfully
124  *                  PAN_FAILURE     - if the role is not valid
125  *
126  ******************************************************************************/
PAN_SetRole(uint8_t role,uint8_t * sec_mask,const char * p_user_name,const char * p_gn_name,const char * p_nap_name)127 tPAN_RESULT PAN_SetRole(uint8_t role, uint8_t* sec_mask,
128                         const char* p_user_name, const char* p_gn_name,
129                         const char* p_nap_name) {
130   const char* p_desc;
131   uint8_t security[3] = {PAN_PANU_SECURITY_LEVEL, PAN_GN_SECURITY_LEVEL,
132                          PAN_NAP_SECURITY_LEVEL};
133   uint8_t* p_sec;
134 
135   /* If the role is not a valid combination reject it */
136   if ((!(role &
137          (PAN_ROLE_CLIENT | PAN_ROLE_GN_SERVER | PAN_ROLE_NAP_SERVER))) &&
138       role != PAN_ROLE_INACTIVE) {
139     PAN_TRACE_ERROR("PAN role %d is invalid", role);
140     return PAN_FAILURE;
141   }
142 
143   /* If the current active role is same as the role being set do nothing */
144   if (pan_cb.role == role) {
145     PAN_TRACE_EVENT("PAN role already was set to: %d", role);
146     return PAN_SUCCESS;
147   }
148 
149   if (!sec_mask)
150     p_sec = security;
151   else
152     p_sec = sec_mask;
153 
154   /* Register all the roles with SDP */
155   PAN_TRACE_API("PAN_SetRole() called with role 0x%x", role);
156 #if (PAN_SUPPORTS_ROLE_NAP == TRUE)
157   if (role & PAN_ROLE_NAP_SERVER) {
158     /* Check the service name */
159     if ((p_nap_name == NULL) || (*p_nap_name == 0))
160       p_nap_name = PAN_NAP_DEFAULT_SERVICE_NAME;
161 
162     /* Registering for NAP service with SDP */
163     p_desc = PAN_NAP_DEFAULT_DESCRIPTION;
164 
165     if (pan_cb.pan_nap_sdp_handle != 0)
166       SDP_DeleteRecord(pan_cb.pan_nap_sdp_handle);
167 
168     pan_cb.pan_nap_sdp_handle =
169         pan_register_with_sdp(UUID_SERVCLASS_NAP, p_sec[2], p_nap_name, p_desc);
170     bta_sys_add_uuid(UUID_SERVCLASS_NAP);
171   }
172   /* If the NAP role is already active and now being cleared delete the record
173    */
174   else if (pan_cb.role & PAN_ROLE_NAP_SERVER) {
175     if (pan_cb.pan_nap_sdp_handle != 0) {
176       SDP_DeleteRecord(pan_cb.pan_nap_sdp_handle);
177       pan_cb.pan_nap_sdp_handle = 0;
178       bta_sys_remove_uuid(UUID_SERVCLASS_NAP);
179     }
180   }
181 #endif
182 
183 #if (PAN_SUPPORTS_ROLE_GN == TRUE)
184   if (role & PAN_ROLE_GN_SERVER) {
185     /* Check the service name */
186     if ((p_gn_name == NULL) || (*p_gn_name == 0))
187       p_gn_name = PAN_GN_DEFAULT_SERVICE_NAME;
188 
189     /* Registering for GN service with SDP */
190     p_desc = PAN_GN_DEFAULT_DESCRIPTION;
191 
192     if (pan_cb.pan_gn_sdp_handle != 0)
193       SDP_DeleteRecord(pan_cb.pan_gn_sdp_handle);
194 
195     pan_cb.pan_gn_sdp_handle =
196         pan_register_with_sdp(UUID_SERVCLASS_GN, p_sec[1], p_gn_name, p_desc);
197     bta_sys_add_uuid(UUID_SERVCLASS_GN);
198   }
199   /* If the GN role is already active and now being cleared delete the record */
200   else if (pan_cb.role & PAN_ROLE_GN_SERVER) {
201     if (pan_cb.pan_gn_sdp_handle != 0) {
202       SDP_DeleteRecord(pan_cb.pan_gn_sdp_handle);
203       pan_cb.pan_gn_sdp_handle = 0;
204       bta_sys_remove_uuid(UUID_SERVCLASS_GN);
205     }
206   }
207 #endif
208 
209 #if (PAN_SUPPORTS_ROLE_PANU == TRUE)
210   if (role & PAN_ROLE_CLIENT) {
211     /* Check the service name */
212     if ((p_user_name == NULL) || (*p_user_name == 0))
213       p_user_name = PAN_PANU_DEFAULT_SERVICE_NAME;
214 
215     /* Registering for PANU service with SDP */
216     p_desc = PAN_PANU_DEFAULT_DESCRIPTION;
217     if (pan_cb.pan_user_sdp_handle != 0)
218       SDP_DeleteRecord(pan_cb.pan_user_sdp_handle);
219 
220     pan_cb.pan_user_sdp_handle = pan_register_with_sdp(
221         UUID_SERVCLASS_PANU, p_sec[0], p_user_name, p_desc);
222     bta_sys_add_uuid(UUID_SERVCLASS_PANU);
223   }
224   /* If the PANU role is already active and now being cleared delete the record
225    */
226   else if (pan_cb.role & PAN_ROLE_CLIENT) {
227     if (pan_cb.pan_user_sdp_handle != 0) {
228       SDP_DeleteRecord(pan_cb.pan_user_sdp_handle);
229       pan_cb.pan_user_sdp_handle = 0;
230       bta_sys_remove_uuid(UUID_SERVCLASS_PANU);
231     }
232   }
233 #endif
234 
235   /* Check if it is a shutdown request */
236   if (role == PAN_ROLE_INACTIVE) pan_close_all_connections();
237 
238   pan_cb.role = role;
239   PAN_TRACE_EVENT("PAN role set to: %d", role);
240   return PAN_SUCCESS;
241 }
242 
243 /*******************************************************************************
244  *
245  * Function         PAN_Connect
246  *
247  * Description      This function is called by the application to initiate a
248  *                  connection to the remote device
249  *
250  * Parameters:      rem_bda     - BD Addr of the remote device
251  *                  src_role    - Role of the local device for the connection
252  *                  dst_role    - Role of the remote device for the connection
253  *                                      PAN_ROLE_CLIENT is for PANU role
254  *                                      PAN_ROLE_GN_SERVER is for GN role
255  *                                      PAN_ROLE_NAP_SERVER is for NAP role
256  *                  *handle     - Pointer for returning Handle to the connection
257  *
258  * Returns          PAN_SUCCESS      - if the connection is initiated
259  *                                     successfully
260  *                  PAN_NO_RESOURCES - resources are not sufficent
261  *                  PAN_FAILURE      - if the connection cannot be initiated
262  *                                     this can be because of the combination of
263  *                                     src and dst roles may not be valid or
264  *                                     allowed at that point of time
265  *
266  ******************************************************************************/
PAN_Connect(const RawAddress & rem_bda,uint8_t src_role,uint8_t dst_role,uint16_t * handle)267 tPAN_RESULT PAN_Connect(const RawAddress& rem_bda, uint8_t src_role,
268                         uint8_t dst_role, uint16_t* handle) {
269   tPAN_CONN* pcb;
270   tBNEP_RESULT result;
271   tBT_UUID src_uuid, dst_uuid;
272   uint32_t mx_chan_id;
273 
274   /*
275   ** Initialize the handle so that in case of failure return values
276   ** the profile will not get confused
277   */
278   *handle = BNEP_INVALID_HANDLE;
279 
280   /* Check if PAN is active or not */
281   if (!(pan_cb.role & src_role)) {
282     PAN_TRACE_ERROR("PAN is not active for the role %d", src_role);
283     return PAN_FAILURE;
284   }
285 
286   /* Validate the parameters before proceeding */
287   if ((src_role != PAN_ROLE_CLIENT && src_role != PAN_ROLE_GN_SERVER &&
288        src_role != PAN_ROLE_NAP_SERVER) ||
289       (dst_role != PAN_ROLE_CLIENT && dst_role != PAN_ROLE_GN_SERVER &&
290        dst_role != PAN_ROLE_NAP_SERVER)) {
291     PAN_TRACE_ERROR("Either source %d or destination role %d is invalid",
292                     src_role, dst_role);
293     return PAN_FAILURE;
294   }
295 
296   /* Check if connection exists for this remote device */
297   pcb = pan_get_pcb_by_addr(rem_bda);
298 
299   /* If we are PANU for this role validate destination role */
300   if (src_role == PAN_ROLE_CLIENT) {
301     if ((pan_cb.num_conns > 1) || (pan_cb.num_conns && (!pcb))) {
302       /*
303       ** If the request is not for existing connection reject it
304       ** because if there is already a connection we cannot accept
305       ** another connection in PANU role
306       */
307       PAN_TRACE_ERROR(
308           "Cannot make PANU connections when there are more than one "
309           "connection");
310       return PAN_INVALID_SRC_ROLE;
311     }
312 
313     src_uuid.uu.uuid16 = UUID_SERVCLASS_PANU;
314     if (dst_role == PAN_ROLE_CLIENT) {
315       dst_uuid.uu.uuid16 = UUID_SERVCLASS_PANU;
316     } else if (dst_role == PAN_ROLE_GN_SERVER) {
317       dst_uuid.uu.uuid16 = UUID_SERVCLASS_GN;
318     } else {
319       dst_uuid.uu.uuid16 = UUID_SERVCLASS_NAP;
320     }
321     mx_chan_id = dst_uuid.uu.uuid16;
322   }
323   /* If destination is PANU role validate source role */
324   else if (dst_role == PAN_ROLE_CLIENT) {
325     if (pan_cb.num_conns && pan_cb.active_role == PAN_ROLE_CLIENT && !pcb) {
326       PAN_TRACE_ERROR("Device already have a connection in PANU role");
327       return PAN_INVALID_SRC_ROLE;
328     }
329 
330     dst_uuid.uu.uuid16 = UUID_SERVCLASS_PANU;
331     if (src_role == PAN_ROLE_GN_SERVER) {
332       src_uuid.uu.uuid16 = UUID_SERVCLASS_GN;
333     } else {
334       src_uuid.uu.uuid16 = UUID_SERVCLASS_NAP;
335     }
336     mx_chan_id = src_uuid.uu.uuid16;
337   }
338   /* The role combination is not valid */
339   else {
340     PAN_TRACE_ERROR(
341         "Source %d and Destination roles %d are not valid combination",
342         src_role, dst_role);
343     return PAN_FAILURE;
344   }
345 
346   /* Allocate control block and initiate connection */
347   if (!pcb) pcb = pan_allocate_pcb(rem_bda, BNEP_INVALID_HANDLE);
348   if (!pcb) {
349     PAN_TRACE_ERROR("PAN Connection failed because of no resources");
350     return PAN_NO_RESOURCES;
351   }
352   BTM_SetOutService(rem_bda, BTM_SEC_SERVICE_BNEP_PANU, mx_chan_id);
353 
354   VLOG(0) << __func__ << " for BD Addr: " << rem_bda;
355   if (pcb->con_state == PAN_STATE_IDLE) {
356     pan_cb.num_conns++;
357   } else if (pcb->con_state == PAN_STATE_CONNECTED) {
358     pcb->con_flags |= PAN_FLAGS_CONN_COMPLETED;
359   } else
360     /* PAN connection is still in progress */
361     return PAN_WRONG_STATE;
362 
363   pcb->con_state = PAN_STATE_CONN_START;
364   pcb->prv_src_uuid = pcb->src_uuid;
365   pcb->prv_dst_uuid = pcb->dst_uuid;
366 
367   pcb->src_uuid = src_uuid.uu.uuid16;
368   pcb->dst_uuid = dst_uuid.uu.uuid16;
369 
370   src_uuid.len = 2;
371   dst_uuid.len = 2;
372 
373   result = BNEP_Connect(rem_bda, &src_uuid, &dst_uuid, &(pcb->handle));
374   if (result != BNEP_SUCCESS) {
375     pan_release_pcb(pcb);
376     return result;
377   }
378 
379   PAN_TRACE_DEBUG("PAN_Connect() current active role set to %d", src_role);
380   pan_cb.prv_active_role = pan_cb.active_role;
381   pan_cb.active_role = src_role;
382   *handle = pcb->handle;
383   return PAN_SUCCESS;
384 }
385 
386 /*******************************************************************************
387  *
388  * Function         PAN_Disconnect
389  *
390  * Description      This is used to disconnect the connection
391  *
392  * Parameters:      handle           - handle for the connection
393  *
394  * Returns          PAN_SUCCESS      - if the connection is closed successfully
395  *                  PAN_FAILURE      - if the connection is not found or
396  *                                           there is an error in disconnecting
397  *
398  ******************************************************************************/
PAN_Disconnect(uint16_t handle)399 tPAN_RESULT PAN_Disconnect(uint16_t handle) {
400   tPAN_CONN* pcb;
401   tBNEP_RESULT result;
402 
403   /* Check if the connection exists */
404   pcb = pan_get_pcb_by_handle(handle);
405   if (!pcb) {
406     PAN_TRACE_ERROR("PAN connection not found for the handle %d", handle);
407     return PAN_FAILURE;
408   }
409 
410   result = BNEP_Disconnect(pcb->handle);
411   if (pcb->con_state != PAN_STATE_IDLE) pan_cb.num_conns--;
412 
413   if (pan_cb.pan_bridge_req_cb && pcb->src_uuid == UUID_SERVCLASS_NAP)
414     (*pan_cb.pan_bridge_req_cb)(pcb->rem_bda, false);
415 
416   pan_release_pcb(pcb);
417 
418   if (result != BNEP_SUCCESS) {
419     PAN_TRACE_EVENT("Error in closing PAN connection");
420     return PAN_FAILURE;
421   }
422 
423   PAN_TRACE_EVENT("PAN connection closed");
424   return PAN_SUCCESS;
425 }
426 
427 /*******************************************************************************
428  *
429  * Function         PAN_Write
430  *
431  * Description      This sends data over the PAN connections. If this is called
432  *                  on GN or NAP side and the packet is multicast or broadcast
433  *                  it will be sent on all the links. Otherwise the correct link
434  *                  is found based on the destination address and forwarded on
435  *                  it.
436  *
437  * Parameters:      handle   - handle for the connection
438  *                  dst      - MAC or BD Addr of the destination device
439  *                  src      - MAC or BD Addr of the source who sent this packet
440  *                  protocol - protocol of the ethernet packet like IP or ARP
441  *                  p_data   - pointer to the data
442  *                  len      - length of the data
443  *                  ext      - to indicate that extension headers present
444  *
445  * Returns          PAN_SUCCESS       - if the data is sent successfully
446  *                  PAN_FAILURE       - if the connection is not found or
447  *                                           there is an error in sending data
448  *
449  ******************************************************************************/
PAN_Write(uint16_t handle,const RawAddress & dst,const RawAddress & src,uint16_t protocol,uint8_t * p_data,uint16_t len,bool ext)450 tPAN_RESULT PAN_Write(uint16_t handle, const RawAddress& dst,
451                       const RawAddress& src, uint16_t protocol, uint8_t* p_data,
452                       uint16_t len, bool ext) {
453   if (pan_cb.role == PAN_ROLE_INACTIVE || !pan_cb.num_conns) {
454     PAN_TRACE_ERROR("%s PAN is not active, data write failed.", __func__);
455     return PAN_FAILURE;
456   }
457 
458   // If the packet is broadcast or multicast, we're going to have to create
459   // a copy of the packet for each connection. We can save one extra copy
460   // by fast-pathing here and calling BNEP_Write instead of placing the packet
461   // in a BT_HDR buffer, calling BNEP_Write, and then freeing the buffer.
462   if (dst.address[0] & 0x01) {
463     int i;
464     for (i = 0; i < MAX_PAN_CONNS; ++i) {
465       if (pan_cb.pcb[i].con_state == PAN_STATE_CONNECTED)
466         BNEP_Write(pan_cb.pcb[i].handle, dst, p_data, len, protocol, &src, ext);
467     }
468     return PAN_SUCCESS;
469   }
470 
471   BT_HDR* buffer = (BT_HDR*)osi_malloc(PAN_BUF_SIZE);
472   buffer->len = len;
473   buffer->offset = PAN_MINIMUM_OFFSET;
474   memcpy((uint8_t*)buffer + sizeof(BT_HDR) + buffer->offset, p_data,
475          buffer->len);
476 
477   return PAN_WriteBuf(handle, dst, src, protocol, buffer, ext);
478 }
479 
480 /*******************************************************************************
481  *
482  * Function         PAN_WriteBuf
483  *
484  * Description      This sends data over the PAN connections. If this is called
485  *                  on GN or NAP side and the packet is multicast or broadcast
486  *                  it will be sent on all the links. Otherwise the correct link
487  *                  is found based on the destination address and forwarded on
488  *                  it. If the return value is not PAN_SUCCESS, the application
489  *                  should take care of releasing the message buffer.
490  *
491  * Parameters:      handle   - handle for the connection
492  *                  dst      - MAC or BD Addr of the destination device
493  *                  src      - MAC or BD Addr of the source who sent this packet
494  *                  protocol - protocol of the ethernet packet like IP or ARP
495  *                  p_buf    - pointer to the data buffer
496  *                  ext      - to indicate that extension headers present
497  *
498  * Returns          PAN_SUCCESS       - if the data is sent successfully
499  *                  PAN_FAILURE       - if the connection is not found or
500  *                                           there is an error in sending data
501  *
502  ******************************************************************************/
PAN_WriteBuf(uint16_t handle,const RawAddress & dst,const RawAddress & src,uint16_t protocol,BT_HDR * p_buf,bool ext)503 tPAN_RESULT PAN_WriteBuf(uint16_t handle, const RawAddress& dst,
504                          const RawAddress& src, uint16_t protocol,
505                          BT_HDR* p_buf, bool ext) {
506   tPAN_CONN* pcb;
507   uint16_t i;
508   tBNEP_RESULT result;
509 
510   if (pan_cb.role == PAN_ROLE_INACTIVE || (!(pan_cb.num_conns))) {
511     PAN_TRACE_ERROR("PAN is not active Data write failed");
512     osi_free(p_buf);
513     return PAN_FAILURE;
514   }
515 
516   /* Check if it is broadcast or multicast packet */
517   if (dst.address[0] & 0x01) {
518     uint8_t* data = (uint8_t*)p_buf + sizeof(BT_HDR) + p_buf->offset;
519     for (i = 0; i < MAX_PAN_CONNS; ++i) {
520       if (pan_cb.pcb[i].con_state == PAN_STATE_CONNECTED)
521         BNEP_Write(pan_cb.pcb[i].handle, dst, data, p_buf->len, protocol, &src,
522                    ext);
523     }
524     osi_free(p_buf);
525     return PAN_SUCCESS;
526   }
527 
528   /* Check if the data write is on PANU side */
529   if (pan_cb.active_role == PAN_ROLE_CLIENT) {
530     /* Data write is on PANU connection */
531     for (i = 0; i < MAX_PAN_CONNS; i++) {
532       if (pan_cb.pcb[i].con_state == PAN_STATE_CONNECTED &&
533           pan_cb.pcb[i].src_uuid == UUID_SERVCLASS_PANU)
534         break;
535     }
536 
537     if (i == MAX_PAN_CONNS) {
538       PAN_TRACE_ERROR("PAN Don't have any user connections");
539       osi_free(p_buf);
540       return PAN_FAILURE;
541     }
542 
543     result =
544         BNEP_WriteBuf(pan_cb.pcb[i].handle, dst, p_buf, protocol, &src, ext);
545     if (result == BNEP_IGNORE_CMD) {
546       PAN_TRACE_DEBUG("PAN ignored data write for PANU connection");
547       return result;
548     } else if (result != BNEP_SUCCESS) {
549       PAN_TRACE_ERROR("PAN failed to write data for the PANU connection");
550       return result;
551     }
552 
553     PAN_TRACE_DEBUG("PAN successfully wrote data for the PANU connection");
554     return PAN_SUCCESS;
555   }
556 
557   /* findout to which connection the data is meant for */
558   pcb = pan_get_pcb_by_handle(handle);
559   if (!pcb) {
560     PAN_TRACE_ERROR("PAN Buf write for wrong handle");
561     osi_free(p_buf);
562     return PAN_FAILURE;
563   }
564 
565   if (pcb->con_state != PAN_STATE_CONNECTED) {
566     PAN_TRACE_ERROR("PAN Buf write when conn is not active");
567     osi_free(p_buf);
568     return PAN_FAILURE;
569   }
570 
571   result = BNEP_WriteBuf(pcb->handle, dst, p_buf, protocol, &src, ext);
572   if (result == BNEP_IGNORE_CMD) {
573     PAN_TRACE_DEBUG("PAN ignored data buf write to PANU");
574     return result;
575   } else if (result != BNEP_SUCCESS) {
576     PAN_TRACE_ERROR("PAN failed to send data buf to the PANU");
577     return result;
578   }
579 
580   PAN_TRACE_DEBUG("PAN successfully sent data buf to the PANU");
581   return PAN_SUCCESS;
582 }
583 
584 /*******************************************************************************
585  *
586  * Function         PAN_SetProtocolFilters
587  *
588  * Description      This function is used to set protocol filters on the peer
589  *
590  * Parameters:      handle      - handle for the connection
591  *                  num_filters - number of protocol filter ranges
592  *                  start       - array of starting protocol numbers
593  *                  end         - array of ending protocol numbers
594  *
595  *
596  * Returns          PAN_SUCCESS    if protocol filters are set successfully
597  *                  PAN_FAILURE    if connection not found or error in setting
598  *
599  ******************************************************************************/
PAN_SetProtocolFilters(uint16_t handle,uint16_t num_filters,uint16_t * p_start_array,uint16_t * p_end_array)600 tPAN_RESULT PAN_SetProtocolFilters(uint16_t handle, uint16_t num_filters,
601                                    uint16_t* p_start_array,
602                                    uint16_t* p_end_array) {
603   tPAN_CONN* pcb;
604   tPAN_RESULT result;
605 
606   /* Check if the connection exists */
607   pcb = pan_get_pcb_by_handle(handle);
608   if (!pcb) {
609     PAN_TRACE_ERROR("PAN connection not found for the handle %d", handle);
610     return PAN_FAILURE;
611   }
612 
613   result = BNEP_SetProtocolFilters(pcb->handle, num_filters, p_start_array,
614                                    p_end_array);
615   if (result != BNEP_SUCCESS) {
616     PAN_TRACE_ERROR("PAN failed to set protocol filters for handle %d", handle);
617     return result;
618   }
619 
620   PAN_TRACE_API("PAN successfully sent protocol filters for handle %d", handle);
621   return PAN_SUCCESS;
622 }
623 
624 /*******************************************************************************
625  *
626  * Function         PAN_SetMulticastFilters
627  *
628  * Description      This function is used to set multicast filters on the peer
629  *
630  * Parameters:      handle      - handle for the connection
631  *                  num_filters - number of multicast filter ranges
632  *                  start       - array of starting multicast filter addresses
633  *                  end         - array of ending multicast filter addresses
634  *
635  *
636  * Returns          PAN_SUCCESS    if multicast filters are set successfully
637  *                  PAN_FAILURE    if connection not found or error in setting
638  *
639  ******************************************************************************/
PAN_SetMulticastFilters(uint16_t handle,uint16_t num_mcast_filters,uint8_t * p_start_array,uint8_t * p_end_array)640 tBNEP_RESULT PAN_SetMulticastFilters(uint16_t handle,
641                                      uint16_t num_mcast_filters,
642                                      uint8_t* p_start_array,
643                                      uint8_t* p_end_array) {
644   tPAN_CONN* pcb;
645   tPAN_RESULT result;
646 
647   /* Check if the connection exists */
648   pcb = pan_get_pcb_by_handle(handle);
649   if (!pcb) {
650     PAN_TRACE_ERROR("PAN connection not found for the handle %d", handle);
651     return PAN_FAILURE;
652   }
653 
654   result = BNEP_SetMulticastFilters(pcb->handle, num_mcast_filters,
655                                     p_start_array, p_end_array);
656   if (result != BNEP_SUCCESS) {
657     PAN_TRACE_ERROR("PAN failed to set multicast filters for handle %d",
658                     handle);
659     return result;
660   }
661 
662   PAN_TRACE_API("PAN successfully sent multicast filters for handle %d",
663                 handle);
664   return PAN_SUCCESS;
665 }
666 
667 /*******************************************************************************
668  *
669  * Function         PAN_SetTraceLevel
670  *
671  * Description      This function sets the trace level for PAN. If called with
672  *                  a value of 0xFF, it simply reads the current trace level.
673  *
674  * Returns          the new (current) trace level
675  *
676  ******************************************************************************/
PAN_SetTraceLevel(uint8_t new_level)677 uint8_t PAN_SetTraceLevel(uint8_t new_level) {
678   if (new_level != 0xFF)
679     pan_cb.trace_level = new_level;
680   else
681     pan_dump_status();
682 
683   return (pan_cb.trace_level);
684 }
685 
686 /*******************************************************************************
687  *
688  * Function         PAN_Init
689  *
690  * Description      This function initializes the PAN module variables
691  *
692  * Parameters:      none
693  *
694  * Returns          none
695  *
696  ******************************************************************************/
PAN_Init(void)697 void PAN_Init(void) {
698   memset(&pan_cb, 0, sizeof(tPAN_CB));
699 
700 #if defined(PAN_INITIAL_TRACE_LEVEL)
701   pan_cb.trace_level = PAN_INITIAL_TRACE_LEVEL;
702 #else
703   pan_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
704 #endif
705 }
706