1 /******************************************************************************
3 * Copyright (C) 2001-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the BNEP API code
23 ******************************************************************************/
29 /*******************************************************************************
33 ** Description This function initializes the BNEP unit. It should be called
34 ** before accessing any other APIs to initialize the control block
38 *******************************************************************************/
41 memset (&bnep_cb, 0, sizeof (tBNEP_CB));
43 #if defined(BNEP_INITIAL_TRACE_LEVEL)
44 bnep_cb.trace_level = BNEP_INITIAL_TRACE_LEVEL;
46 bnep_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
49 /* Start a timer to read our BD address */
50 btu_start_timer (&bnep_cb.bnep_tle, BTU_TTYPE_BNEP, 2);
54 /*******************************************************************************
56 ** Function BNEP_Register
58 ** Description This function is called by the upper layer to register
59 ** its callbacks with BNEP
61 ** Parameters: p_reg_info - contains all callback function pointers
64 ** Returns BNEP_SUCCESS if registered successfully
65 ** BNEP_FAILURE if connection state callback is missing
67 *******************************************************************************/
68 tBNEP_RESULT BNEP_Register (tBNEP_REGISTER *p_reg_info)
70 /* There should be connection state call back registered */
71 if ((!p_reg_info) || (!(p_reg_info->p_conn_state_cb)))
72 return BNEP_SECURITY_FAIL;
74 bnep_cb.p_conn_ind_cb = p_reg_info->p_conn_ind_cb;
75 bnep_cb.p_conn_state_cb = p_reg_info->p_conn_state_cb;
76 bnep_cb.p_data_ind_cb = p_reg_info->p_data_ind_cb;
77 bnep_cb.p_data_buf_cb = p_reg_info->p_data_buf_cb;
78 bnep_cb.p_filter_ind_cb = p_reg_info->p_filter_ind_cb;
79 bnep_cb.p_mfilter_ind_cb = p_reg_info->p_mfilter_ind_cb;
80 bnep_cb.p_tx_data_flow_cb = p_reg_info->p_tx_data_flow_cb;
82 if (bnep_register_with_l2cap ())
83 return BNEP_SECURITY_FAIL;
85 bnep_cb.profile_registered = TRUE;
90 /*******************************************************************************
92 ** Function BNEP_Deregister
94 ** Description This function is called by the upper layer to de-register
102 *******************************************************************************/
103 void BNEP_Deregister (void)
105 /* Clear all the call backs registered */
106 bnep_cb.p_conn_ind_cb = NULL;
107 bnep_cb.p_conn_state_cb = NULL;
108 bnep_cb.p_data_ind_cb = NULL;
109 bnep_cb.p_data_buf_cb = NULL;
110 bnep_cb.p_filter_ind_cb = NULL;
111 bnep_cb.p_mfilter_ind_cb = NULL;
113 bnep_cb.profile_registered = FALSE;
114 L2CA_Deregister (BT_PSM_BNEP);
118 /*******************************************************************************
120 ** Function BNEP_Connect
122 ** Description This function creates a BNEP connection to a remote
125 ** Parameters: p_rem_addr - BD_ADDR of the peer
126 ** src_uuid - source uuid for the connection
127 ** dst_uuid - destination uuid for the connection
128 ** p_handle - pointer to return the handle for the connection
130 ** Returns BNEP_SUCCESS if connection started
131 ** BNEP_NO_RESOURCES if no resources
133 *******************************************************************************/
134 tBNEP_RESULT BNEP_Connect (BD_ADDR p_rem_bda,
140 tBNEP_CONN *p_bcb = bnepu_find_bcb_by_bd_addr (p_rem_bda);
142 BNEP_TRACE_API ("BNEP_Connect() BDA: %02x-%02x-%02x-%02x-%02x-%02x",
143 p_rem_bda[0], p_rem_bda[1], p_rem_bda[2],
144 p_rem_bda[3], p_rem_bda[4], p_rem_bda[5]);
146 if (!bnep_cb.profile_registered)
147 return BNEP_WRONG_STATE;
149 /* Both source and destination UUID lengths should be same */
150 if (src_uuid->len != dst_uuid->len)
151 return BNEP_CONN_FAILED_UUID_SIZE;
155 if ((p_bcb = bnepu_allocate_bcb (p_rem_bda)) == NULL)
156 return (BNEP_NO_RESOURCES);
158 else if (p_bcb->con_state != BNEP_STATE_CONNECTED)
159 return BNEP_WRONG_STATE;
162 /* Backup current UUID values to restore if role change fails */
163 memcpy ((UINT8 *)&(p_bcb->prv_src_uuid), (UINT8 *)&(p_bcb->src_uuid), sizeof (tBT_UUID));
164 memcpy ((UINT8 *)&(p_bcb->prv_dst_uuid), (UINT8 *)&(p_bcb->dst_uuid), sizeof (tBT_UUID));
167 /* We are the originator of this connection */
168 p_bcb->con_flags |= BNEP_FLAGS_IS_ORIG;
170 memcpy ((UINT8 *)&(p_bcb->src_uuid), (UINT8 *)src_uuid, sizeof (tBT_UUID));
171 memcpy ((UINT8 *)&(p_bcb->dst_uuid), (UINT8 *)dst_uuid, sizeof (tBT_UUID));
173 if (p_bcb->con_state == BNEP_STATE_CONNECTED)
175 /* Transition to the next appropriate state, waiting for connection confirm. */
176 p_bcb->con_state = BNEP_STATE_SEC_CHECKING;
178 BNEP_TRACE_API ("BNEP initiating security procedures for src uuid 0x%x",
179 p_bcb->src_uuid.uu.uuid16);
181 #if (defined (BNEP_DO_AUTH_FOR_ROLE_SWITCH) && BNEP_DO_AUTH_FOR_ROLE_SWITCH == TRUE)
182 btm_sec_mx_access_request (p_bcb->rem_bda, BT_PSM_BNEP, TRUE,
184 bnep_get_uuid32(src_uuid),
185 &bnep_sec_check_complete, p_bcb);
187 bnep_sec_check_complete (p_bcb->rem_bda, p_bcb, BTM_SUCCESS);
193 /* Transition to the next appropriate state, waiting for connection confirm. */
194 p_bcb->con_state = BNEP_STATE_CONN_START;
196 if ((cid = L2CA_ConnectReq (BT_PSM_BNEP, p_bcb->rem_bda)) != 0)
198 p_bcb->l2cap_cid = cid;
203 BNEP_TRACE_ERROR ("BNEP - Originate failed");
204 if (bnep_cb.p_conn_state_cb)
205 (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
206 bnepu_release_bcb (p_bcb);
207 return BNEP_CONN_FAILED;
210 /* Start timer waiting for connect */
211 btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);
214 *p_handle = p_bcb->handle;
215 return (BNEP_SUCCESS);
219 /*******************************************************************************
221 ** Function BNEP_ConnectResp
223 ** Description This function is called in responce to connection indication
226 ** Parameters: handle - handle given in the connection indication
227 ** resp - responce for the connection indication
229 ** Returns BNEP_SUCCESS if connection started
230 ** BNEP_WRONG_HANDLE if the connection is not found
231 ** BNEP_WRONG_STATE if the responce is not expected
233 *******************************************************************************/
234 tBNEP_RESULT BNEP_ConnectResp (UINT16 handle, tBNEP_RESULT resp)
237 UINT16 resp_code = BNEP_SETUP_CONN_OK;
239 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
240 return (BNEP_WRONG_HANDLE);
242 p_bcb = &(bnep_cb.bcb[handle - 1]);
244 if (p_bcb->con_state != BNEP_STATE_CONN_SETUP ||
245 (!(p_bcb->con_flags & BNEP_FLAGS_SETUP_RCVD)))
246 return (BNEP_WRONG_STATE);
248 BNEP_TRACE_API ("BNEP_ConnectResp() for handle %d, responce %d", handle, resp);
250 /* Form appropriate responce based on profile responce */
251 if (resp == BNEP_CONN_FAILED_SRC_UUID) resp_code = BNEP_SETUP_INVALID_SRC_UUID;
252 else if (resp == BNEP_CONN_FAILED_DST_UUID) resp_code = BNEP_SETUP_INVALID_DEST_UUID;
253 else if (resp == BNEP_CONN_FAILED_UUID_SIZE) resp_code = BNEP_SETUP_INVALID_UUID_SIZE;
254 else if (resp == BNEP_SUCCESS) resp_code = BNEP_SETUP_CONN_OK;
255 else resp_code = BNEP_SETUP_CONN_NOT_ALLOWED;
257 bnep_send_conn_responce (p_bcb, resp_code);
258 p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD);
260 if (resp == BNEP_SUCCESS)
261 bnep_connected (p_bcb);
262 else if (p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)
264 /* Restore the original parameters */
265 p_bcb->con_state = BNEP_STATE_CONNECTED;
266 p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD);
268 memcpy ((UINT8 *)&(p_bcb->src_uuid), (UINT8 *)&(p_bcb->prv_src_uuid), sizeof (tBT_UUID));
269 memcpy ((UINT8 *)&(p_bcb->dst_uuid), (UINT8 *)&(p_bcb->prv_dst_uuid), sizeof (tBT_UUID));
272 /* Process remaining part of the setup message (extension headers) */
273 if (p_bcb->p_pending_data)
275 UINT8 extension_present = TRUE, *p, ext_type;
278 rem_len = p_bcb->p_pending_data->len;
279 p = (UINT8 *)(p_bcb->p_pending_data + 1) + p_bcb->p_pending_data->offset;
280 while (extension_present && p && rem_len)
283 extension_present = ext_type >> 7;
286 /* if unknown extension present stop processing */
290 p = bnep_process_control_packet (p_bcb, p, &rem_len, TRUE);
293 GKI_freebuf (p_bcb->p_pending_data);
294 p_bcb->p_pending_data = NULL;
296 return (BNEP_SUCCESS);
300 /*******************************************************************************
302 ** Function BNEP_Disconnect
304 ** Description This function is called to close the specified connection.
306 ** Parameters: handle - handle of the connection
308 ** Returns BNEP_SUCCESS if connection is disconnected
309 ** BNEP_WRONG_HANDLE if no connection is not found
311 *******************************************************************************/
312 tBNEP_RESULT BNEP_Disconnect (UINT16 handle)
316 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
317 return (BNEP_WRONG_HANDLE);
319 p_bcb = &(bnep_cb.bcb[handle - 1]);
321 if (p_bcb->con_state == BNEP_STATE_IDLE)
322 return (BNEP_WRONG_HANDLE);
324 BNEP_TRACE_API ("BNEP_Disconnect() for handle %d", handle);
326 L2CA_DisconnectReq (p_bcb->l2cap_cid);
328 bnepu_release_bcb (p_bcb);
330 return (BNEP_SUCCESS);
334 /*******************************************************************************
336 ** Function BNEP_WriteBuf
338 ** Description This function sends data in a GKI buffer on BNEP connection
340 ** Parameters: handle - handle of the connection to write
341 ** p_dest_addr - BD_ADDR/Ethernet addr of the destination
342 ** p_buf - pointer to address of buffer with data
343 ** protocol - protocol type of the packet
344 ** p_src_addr - (optional) BD_ADDR/ethernet address of the source
345 ** (should be NULL if it is local BD Addr)
346 ** fw_ext_present - forwarded extensions present
348 ** Returns: BNEP_WRONG_HANDLE - if passed handle is not valid
349 ** BNEP_MTU_EXCEDED - If the data length is greater than MTU
350 ** BNEP_IGNORE_CMD - If the packet is filtered out
351 ** BNEP_Q_SIZE_EXCEEDED - If the Tx Q is full
352 ** BNEP_SUCCESS - If written successfully
354 *******************************************************************************/
355 tBNEP_RESULT BNEP_WriteBuf (UINT16 handle,
360 BOOLEAN fw_ext_present)
365 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
368 return (BNEP_WRONG_HANDLE);
371 p_bcb = &(bnep_cb.bcb[handle - 1]);
373 if (p_buf->len > BNEP_MTU_SIZE)
375 BNEP_TRACE_ERROR ("BNEP_Write() length %d exceeded MTU %d", p_buf->len, BNEP_MTU_SIZE);
377 return (BNEP_MTU_EXCEDED);
380 /* Check if the packet should be filtered out */
381 p_data = (UINT8 *)(p_buf + 1) + p_buf->offset;
382 if (bnep_is_packet_allowed (p_bcb, p_dest_addr, protocol, fw_ext_present, p_data) != BNEP_SUCCESS)
385 ** If packet is filtered and ext headers are present
386 ** drop the data and forward the ext headers
391 UINT16 org_len, new_len;
392 /* parse the extension headers and findout the new packet len */
393 org_len = p_buf->len;
401 new_len += (length + 2);
403 if (new_len > org_len)
406 return BNEP_IGNORE_CMD;
409 } while (ext & 0x80);
411 if (protocol != BNEP_802_1_P_PROTOCOL)
419 p_buf->len = new_len;
424 return BNEP_IGNORE_CMD;
428 /* Check transmit queue */
429 if (fixed_queue_length(p_bcb->xmit_q) >= BNEP_MAX_XMITQ_DEPTH)
432 return (BNEP_Q_SIZE_EXCEEDED);
435 /* Build the BNEP header */
436 bnepu_build_bnep_hdr (p_bcb, p_buf, protocol, p_src_addr, p_dest_addr, fw_ext_present);
438 /* Send the data or queue it up */
439 bnepu_check_send_packet (p_bcb, p_buf);
441 return (BNEP_SUCCESS);
445 /*******************************************************************************
447 ** Function BNEP_Write
449 ** Description This function sends data over a BNEP connection
451 ** Parameters: handle - handle of the connection to write
452 ** p_dest_addr - BD_ADDR/Ethernet addr of the destination
453 ** p_data - pointer to data start
454 ** protocol - protocol type of the packet
455 ** p_src_addr - (optional) BD_ADDR/ethernet address of the source
456 ** (should be NULL if it is local BD Addr)
457 ** fw_ext_present - forwarded extensions present
459 ** Returns: BNEP_WRONG_HANDLE - if passed handle is not valid
460 ** BNEP_MTU_EXCEDED - If the data length is greater than MTU
461 ** BNEP_IGNORE_CMD - If the packet is filtered out
462 ** BNEP_Q_SIZE_EXCEEDED - If the Tx Q is full
463 ** BNEP_NO_RESOURCES - If not able to allocate a buffer
464 ** BNEP_SUCCESS - If written successfully
466 *******************************************************************************/
467 tBNEP_RESULT BNEP_Write (UINT16 handle,
473 BOOLEAN fw_ext_present)
479 /* Check MTU size. Consider the possibility of having extension headers */
480 if (len > BNEP_MTU_SIZE)
482 BNEP_TRACE_ERROR ("BNEP_Write() length %d exceeded MTU %d", len, BNEP_MTU_SIZE);
483 return (BNEP_MTU_EXCEDED);
486 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
487 return (BNEP_WRONG_HANDLE);
489 p_bcb = &(bnep_cb.bcb[handle - 1]);
491 /* Check if the packet should be filtered out */
492 if (bnep_is_packet_allowed (p_bcb, p_dest_addr, protocol, fw_ext_present, p_data) != BNEP_SUCCESS)
495 ** If packet is filtered and ext headers are present
496 ** drop the data and forward the ext headers
501 UINT16 org_len, new_len;
502 /* parse the extension headers and findout the new packet len */
512 new_len += (length + 2);
514 if (new_len > org_len)
515 return BNEP_IGNORE_CMD;
517 } while (ext & 0x80);
519 if (protocol != BNEP_802_1_P_PROTOCOL)
531 return BNEP_IGNORE_CMD;
534 /* Check transmit queue */
535 if (fixed_queue_length(p_bcb->xmit_q) >= BNEP_MAX_XMITQ_DEPTH)
536 return (BNEP_Q_SIZE_EXCEEDED);
538 /* Get a buffer to copy the data into */
539 p_buf = (BT_HDR *)GKI_getbuf(BNEP_BUF_SIZE);
542 BNEP_TRACE_ERROR ("BNEP_Write() not able to get buffer");
543 return (BNEP_NO_RESOURCES);
547 p_buf->offset = BNEP_MINIMUM_OFFSET;
548 p = (UINT8 *)(p_buf + 1) + BNEP_MINIMUM_OFFSET;
550 memcpy (p, p_data, len);
552 /* Build the BNEP header */
553 bnepu_build_bnep_hdr (p_bcb, p_buf, protocol, p_src_addr, p_dest_addr, fw_ext_present);
555 /* Send the data or queue it up */
556 bnepu_check_send_packet (p_bcb, p_buf);
558 return (BNEP_SUCCESS);
562 /*******************************************************************************
564 ** Function BNEP_SetProtocolFilters
566 ** Description This function sets the protocol filters on peer device
568 ** Parameters: handle - Handle for the connection
569 ** num_filters - total number of filter ranges
570 ** p_start_array - Array of beginings of all protocol ranges
571 ** p_end_array - Array of ends of all protocol ranges
573 ** Returns BNEP_WRONG_HANDLE - if the connection handle is not valid
574 ** BNEP_SET_FILTER_FAIL - if the connection is in wrong state
575 ** BNEP_TOO_MANY_FILTERS - if too many filters
576 ** BNEP_SUCCESS - if request sent successfully
578 *******************************************************************************/
579 tBNEP_RESULT BNEP_SetProtocolFilters (UINT16 handle,
581 UINT16 *p_start_array,
587 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
588 return (BNEP_WRONG_HANDLE);
590 p_bcb = &(bnep_cb.bcb[handle - 1]);
592 /* Check the connection state */
593 if ((p_bcb->con_state != BNEP_STATE_CONNECTED) &&
594 (!(p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)))
595 return (BNEP_WRONG_STATE);
597 /* Validate the parameters */
598 if (num_filters && (!p_start_array || !p_end_array))
599 return (BNEP_SET_FILTER_FAIL);
601 if (num_filters > BNEP_MAX_PROT_FILTERS)
602 return (BNEP_TOO_MANY_FILTERS);
604 /* Fill the filter values in connnection block */
605 for (xx = 0; xx < num_filters; xx++)
607 p_bcb->sent_prot_filter_start[xx] = *p_start_array++;
608 p_bcb->sent_prot_filter_end[xx] = *p_end_array++;
611 p_bcb->sent_num_filters = num_filters;
613 bnepu_send_peer_our_filters (p_bcb);
615 return (BNEP_SUCCESS);
619 /*******************************************************************************
621 ** Function BNEP_SetMulticastFilters
623 ** Description This function sets the filters for multicast addresses for BNEP.
625 ** Parameters: handle - Handle for the connection
626 ** num_filters - total number of filter ranges
627 ** p_start_array - Pointer to sequence of beginings of all
628 ** multicast address ranges
629 ** p_end_array - Pointer to sequence of ends of all
630 ** multicast address ranges
632 ** Returns BNEP_WRONG_HANDLE - if the connection handle is not valid
633 ** BNEP_SET_FILTER_FAIL - if the connection is in wrong state
634 ** BNEP_TOO_MANY_FILTERS - if too many filters
635 ** BNEP_SUCCESS - if request sent successfully
637 *******************************************************************************/
638 tBNEP_RESULT BNEP_SetMulticastFilters (UINT16 handle,
640 UINT8 *p_start_array,
646 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
647 return (BNEP_WRONG_HANDLE);
649 p_bcb = &(bnep_cb.bcb[handle - 1]);
651 /* Check the connection state */
652 if ((p_bcb->con_state != BNEP_STATE_CONNECTED) &&
653 (!(p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)))
654 return (BNEP_WRONG_STATE);
656 /* Validate the parameters */
657 if (num_filters && (!p_start_array || !p_end_array))
658 return (BNEP_SET_FILTER_FAIL);
660 if (num_filters > BNEP_MAX_MULTI_FILTERS)
661 return (BNEP_TOO_MANY_FILTERS);
663 /* Fill the multicast filter values in connnection block */
664 for (xx = 0; xx < num_filters; xx++)
666 memcpy (p_bcb->sent_mcast_filter_start[xx], p_start_array, BD_ADDR_LEN);
667 memcpy (p_bcb->sent_mcast_filter_end[xx], p_end_array, BD_ADDR_LEN);
669 p_start_array += BD_ADDR_LEN;
670 p_end_array += BD_ADDR_LEN;
673 p_bcb->sent_mcast_filters = num_filters;
675 bnepu_send_peer_our_multi_filters (p_bcb);
677 return (BNEP_SUCCESS);
680 /*******************************************************************************
682 ** Function BNEP_SetTraceLevel
684 ** Description This function sets the trace level for BNEP. If called with
685 ** a value of 0xFF, it simply reads the current trace level.
687 ** Returns the new (current) trace level
689 *******************************************************************************/
690 UINT8 BNEP_SetTraceLevel (UINT8 new_level)
692 if (new_level != 0xFF)
693 bnep_cb.trace_level = new_level;
695 return (bnep_cb.trace_level);
699 /*******************************************************************************
701 ** Function BNEP_GetStatus
703 ** Description This function gets the status information for BNEP connection
705 ** Returns BNEP_SUCCESS - if the status is available
706 ** BNEP_NO_RESOURCES - if no structure is passed for output
707 ** BNEP_WRONG_HANDLE - if the handle is invalid
708 ** BNEP_WRONG_STATE - if not in connected state
710 *******************************************************************************/
711 tBNEP_RESULT BNEP_GetStatus (UINT16 handle, tBNEP_STATUS *p_status)
713 #if (defined (BNEP_SUPPORTS_STATUS_API) && BNEP_SUPPORTS_STATUS_API == TRUE)
717 return BNEP_NO_RESOURCES;
719 if ((!handle) || (handle > BNEP_MAX_CONNECTIONS))
720 return (BNEP_WRONG_HANDLE);
722 p_bcb = &(bnep_cb.bcb[handle - 1]);
724 memset (p_status, 0, sizeof (tBNEP_STATUS));
725 if ((p_bcb->con_state != BNEP_STATE_CONNECTED) &&
726 (!(p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)))
727 return BNEP_WRONG_STATE;
729 /* Read the status parameters from the connection control block */
730 p_status->con_status = BNEP_STATUS_CONNECTED;
731 p_status->l2cap_cid = p_bcb->l2cap_cid;
732 p_status->rem_mtu_size = p_bcb->rem_mtu_size;
733 p_status->xmit_q_depth = fixed_queue_length(p_bcb->xmit_q);
734 p_status->sent_num_filters = p_bcb->sent_num_filters;
735 p_status->sent_mcast_filters = p_bcb->sent_mcast_filters;
736 p_status->rcvd_num_filters = p_bcb->rcvd_num_filters;
737 p_status->rcvd_mcast_filters = p_bcb->rcvd_mcast_filters;
739 memcpy (p_status->rem_bda, p_bcb->rem_bda, BD_ADDR_LEN);
740 memcpy (&(p_status->src_uuid), &(p_bcb->src_uuid), sizeof (tBT_UUID));
741 memcpy (&(p_status->dst_uuid), &(p_bcb->dst_uuid), sizeof (tBT_UUID));
745 return (BNEP_IGNORE_CMD);