6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_l2cap_llpi.c,v 1.5 2003/09/08 19:11:45 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include <netgraph/ng_message.h>
42 #include <netgraph/netgraph.h>
43 #include <netgraph/bluetooth/include/ng_bluetooth.h>
44 #include <netgraph/bluetooth/include/ng_hci.h>
45 #include <netgraph/bluetooth/include/ng_l2cap.h>
46 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h>
47 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h>
48 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h>
49 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h>
50 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h>
51 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h>
53 /******************************************************************************
54 ******************************************************************************
55 ** Lower Layer Protocol (HCI) Interface module
56 ******************************************************************************
57 ******************************************************************************/
60 * Send LP_ConnectReq event to the lower layer protocol. Create new connection
61 * descriptor and initialize it. Create LP_ConnectReq event and send it to the
62 * lower layer, then adjust connection state and start timer. The function WILL
63 * FAIL if connection to the remote unit already exists.
67 ng_l2cap_lp_con_req(ng_l2cap_p l2cap, bdaddr_p bdaddr, int type)
69 struct ng_mesg *msg = NULL;
70 ng_hci_lp_con_req_ep *ep = NULL;
71 ng_l2cap_con_p con = NULL;
74 /* Verify that we DO NOT have connection to the remote unit */
75 con = ng_l2cap_con_by_addr(l2cap, bdaddr, type);
78 "%s: %s - unexpected LP_ConnectReq event. " \
79 "Connection already exists, state=%d, con_handle=%d\n",
80 __func__, NG_NODE_NAME(l2cap->node), con->state,
86 /* Check if lower layer protocol is still connected */
87 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
89 "%s: %s - hook \"%s\" is not connected or valid\n",
90 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
95 /* Create and intialize new connection descriptor */
96 con = ng_l2cap_new_con(l2cap, bdaddr, type);
100 /* Create and send LP_ConnectReq event */
101 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_CON_REQ,
102 sizeof(*ep), M_NOWAIT);
104 ng_l2cap_free_con(con);
109 ep = (ng_hci_lp_con_req_ep *) (msg->data);
110 bcopy(bdaddr, &ep->bdaddr, sizeof(ep->bdaddr));
111 ep->link_type = type;
113 con->flags |= NG_L2CAP_CON_OUTGOING;
114 con->state = NG_L2CAP_W4_LP_CON_CFM;
115 ng_l2cap_lp_timeout(con);
117 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
119 if (ng_l2cap_lp_untimeout(con) == 0)
120 ng_l2cap_free_con(con);
123 * Do not free connection if ng_l2cap_lp_untimeout() failed
124 * let timeout handler deal with it. Always return error to
130 } /* ng_l2cap_lp_con_req */
133 * Process LP_ConnectCfm event from the lower layer protocol. It could be
134 * positive or negative. Verify remote unit address then stop the timer and
139 ng_l2cap_lp_con_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
141 ng_hci_lp_con_cfm_ep *ep = NULL;
142 ng_l2cap_con_p con = NULL;
146 if (msg->header.arglen != sizeof(*ep)) {
148 "%s: %s - invalid LP_ConnectCfm[Neg] message size\n",
149 __func__, NG_NODE_NAME(l2cap->node));
154 ep = (ng_hci_lp_con_cfm_ep *) (msg->data);
155 /* Check if we have requested/accepted this connection */
156 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr, ep->link_type);
159 "%s: %s - unexpected LP_ConnectCfm event. Connection does not exist\n",
160 __func__, NG_NODE_NAME(l2cap->node));
165 /* Check connection state */
166 if (con->state != NG_L2CAP_W4_LP_CON_CFM) {
168 "%s: %s - unexpected LP_ConnectCfm event. " \
169 "Invalid connection state, state=%d, con_handle=%d\n",
170 __func__, NG_NODE_NAME(l2cap->node), con->state,
177 * Looks like it is our confirmation. It is safe now to cancel
178 * connection timer and notify upper layer. If timeout already
179 * happened then ignore connection confirmation and let timeout
183 if ((error = ng_l2cap_lp_untimeout(con)) != 0)
186 if (ep->status == 0) {
187 con->state = NG_L2CAP_CON_OPEN;
188 con->con_handle = ep->con_handle;
189 ng_l2cap_lp_deliver(con);
190 } else /* Negative confirmation - remove connection descriptor */
191 ng_l2cap_con_fail(con, ep->status);
194 } /* ng_l2cap_lp_con_cfm */
197 * Process LP_ConnectInd event from the lower layer protocol. This is a good
198 * place to put some extra check on remote unit address and/or class. We could
199 * even forward this information to control hook (or check against internal
200 * black list) and thus implement some kind of firewall. But for now be simple
201 * and create new connection descriptor, start timer and send LP_ConnectRsp
202 * event (i.e. accept connection).
206 ng_l2cap_lp_con_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
208 ng_hci_lp_con_ind_ep *ep = NULL;
209 ng_hci_lp_con_rsp_ep *rp = NULL;
210 struct ng_mesg *rsp = NULL;
211 ng_l2cap_con_p con = NULL;
215 if (msg->header.arglen != sizeof(*ep)) {
217 "%s: %s - invalid LP_ConnectInd message size\n",
218 __func__, NG_NODE_NAME(l2cap->node));
223 ep = (ng_hci_lp_con_ind_ep *) (msg->data);
225 /* Make sure we have only one connection to the remote unit */
226 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr, ep->link_type);
229 "%s: %s - unexpected LP_ConnectInd event. " \
230 "Connection already exists, state=%d, con_handle=%d\n",
231 __func__, NG_NODE_NAME(l2cap->node), con->state,
237 /* Check if lower layer protocol is still connected */
238 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
240 "%s: %s - hook \"%s\" is not connected or valid",
241 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
246 /* Create and intialize new connection descriptor */
247 con = ng_l2cap_new_con(l2cap, &ep->bdaddr, ep->link_type);
251 /* Create and send LP_ConnectRsp event */
252 NG_MKMESSAGE(rsp, NGM_HCI_COOKIE, NGM_HCI_LP_CON_RSP,
253 sizeof(*rp), M_NOWAIT);
255 ng_l2cap_free_con(con);
260 rp = (ng_hci_lp_con_rsp_ep *)(rsp->data);
261 rp->status = 0x00; /* accept connection */
262 rp->link_type = NG_HCI_LINK_ACL;
263 bcopy(&ep->bdaddr, &rp->bdaddr, sizeof(rp->bdaddr));
265 con->state = NG_L2CAP_W4_LP_CON_CFM;
266 ng_l2cap_lp_timeout(con);
268 NG_SEND_MSG_HOOK(error, l2cap->node, rsp, l2cap->hci, 0);
270 if (ng_l2cap_lp_untimeout(con) == 0)
271 ng_l2cap_free_con(con);
274 * Do not free connection if ng_l2cap_lp_untimeout() failed
275 * let timeout handler deal with it. Always return error to
281 } /* ng_l2cap_lp_con_ind */
284 * Process LP_DisconnectInd event from the lower layer protocol. We have been
285 * disconnected from the remote unit. So notify the upper layer protocol.
289 ng_l2cap_lp_discon_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
291 ng_hci_lp_discon_ind_ep *ep = NULL;
292 ng_l2cap_con_p con = NULL;
296 if (msg->header.arglen != sizeof(*ep)) {
298 "%s: %s - invalid LP_DisconnectInd message size\n",
299 __func__, NG_NODE_NAME(l2cap->node));
304 ep = (ng_hci_lp_discon_ind_ep *) (msg->data);
306 /* Check if we have this connection */
307 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
310 "%s: %s - unexpected LP_DisconnectInd event. " \
311 "Connection does not exist, con_handle=%d\n",
312 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
317 /* XXX Verify connection state -- do we need to check this? */
318 if (con->state != NG_L2CAP_CON_OPEN) {
320 "%s: %s - unexpected LP_DisconnectInd event. " \
321 "Invalid connection state, state=%d, con_handle=%d\n",
322 __func__, NG_NODE_NAME(l2cap->node), con->state,
329 * Notify upper layer and remove connection
330 * Note: The connection could have auto disconnect timeout set. Try
331 * to remove it. If auto disconnect timeout happened then ignore
332 * disconnect indication and let timeout handle that.
335 if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
336 if ((error = ng_l2cap_discon_untimeout(con)) != 0)
339 ng_l2cap_con_fail(con, ep->reason);
342 } /* ng_l2cap_lp_discon_ind */
345 * Send LP_QoSSetupReq event to the lower layer protocol
349 ng_l2cap_lp_qos_req(ng_l2cap_p l2cap, u_int16_t con_handle,
350 ng_l2cap_flow_p flow)
352 struct ng_mesg *msg = NULL;
353 ng_hci_lp_qos_req_ep *ep = NULL;
354 ng_l2cap_con_p con = NULL;
357 /* Verify that we have this connection */
358 con = ng_l2cap_con_by_handle(l2cap, con_handle);
361 "%s: %s - unexpected LP_QoSSetupReq event. " \
362 "Connection does not exist, con_handle=%d\n",
363 __func__, NG_NODE_NAME(l2cap->node), con_handle);
368 /* Verify connection state */
369 if (con->state != NG_L2CAP_CON_OPEN) {
371 "%s: %s - unexpected LP_QoSSetupReq event. " \
372 "Invalid connection state, state=%d, con_handle=%d\n",
373 __func__, NG_NODE_NAME(l2cap->node), con->state,
379 /* Check if lower layer protocol is still connected */
380 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
382 "%s: %s - hook \"%s\" is not connected or valid",
383 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
388 /* Create and send LP_QoSSetupReq event */
389 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_QOS_REQ,
390 sizeof(*ep), M_NOWAIT);
394 ep = (ng_hci_lp_qos_req_ep *) (msg->data);
395 ep->con_handle = con_handle;
396 ep->flags = flow->flags;
397 ep->service_type = flow->service_type;
398 ep->token_rate = flow->token_rate;
399 ep->peak_bandwidth = flow->peak_bandwidth;
400 ep->latency = flow->latency;
401 ep->delay_variation = flow->delay_variation;
403 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
406 } /* ng_l2cap_lp_con_req */
409 * Process LP_QoSSetupCfm from the lower layer protocol
413 ng_l2cap_lp_qos_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
415 ng_hci_lp_qos_cfm_ep *ep = NULL;
419 if (msg->header.arglen != sizeof(*ep)) {
421 "%s: %s - invalid LP_QoSSetupCfm[Neg] message size\n",
422 __func__, NG_NODE_NAME(l2cap->node));
427 ep = (ng_hci_lp_qos_cfm_ep *) (msg->data);
428 /* XXX FIXME do something */
431 } /* ng_l2cap_lp_qos_cfm */
434 * Process LP_QoSViolationInd event from the lower layer protocol. Lower
435 * layer protocol has detected QoS Violation, so we MUST notify the
440 ng_l2cap_lp_qos_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
442 ng_hci_lp_qos_ind_ep *ep = NULL;
443 ng_l2cap_con_p con = NULL;
447 if (msg->header.arglen != sizeof(*ep)) {
449 "%s: %s - invalid LP_QoSViolation message size\n",
450 __func__, NG_NODE_NAME(l2cap->node));
455 ep = (ng_hci_lp_qos_ind_ep *) (msg->data);
457 /* Check if we have this connection */
458 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
461 "%s: %s - unexpected LP_QoSViolationInd event. " \
462 "Connection does not exist, con_handle=%d\n",
463 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
468 /* Verify connection state */
469 if (con->state != NG_L2CAP_CON_OPEN) {
471 "%s: %s - unexpected LP_QoSViolationInd event. " \
472 "Invalid connection state, state=%d, con_handle=%d\n",
473 __func__, NG_NODE_NAME(l2cap->node), con->state,
479 /* XXX FIXME Notify upper layer and terminate channels if required */
482 } /* ng_l2cap_qos_ind */
485 * Prepare L2CAP packet. Prepend packet with L2CAP packet header and then
486 * segment it according to HCI MTU.
490 ng_l2cap_lp_send(ng_l2cap_con_p con, u_int16_t dcid, struct mbuf *m0)
492 ng_l2cap_p l2cap = con->l2cap;
493 ng_l2cap_hdr_t *l2cap_hdr = NULL;
494 ng_hci_acldata_pkt_t *acl_hdr = NULL;
495 struct mbuf *m_last = NULL, *m = NULL;
496 int len, flag = NG_HCI_PACKET_START;
498 KASSERT((con->tx_pkt == NULL),
499 ("%s: %s - another packet pending?!\n", __func__, NG_NODE_NAME(l2cap->node)));
500 KASSERT((l2cap->pkt_size > 0),
501 ("%s: %s - invalid l2cap->pkt_size?!\n", __func__, NG_NODE_NAME(l2cap->node)));
503 /* Prepend mbuf with L2CAP header */
504 m0 = ng_l2cap_prepend(m0, sizeof(*l2cap_hdr));
507 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
508 __func__, NG_NODE_NAME(l2cap->node),
514 l2cap_hdr = mtod(m0, ng_l2cap_hdr_t *);
515 l2cap_hdr->length = htole16(m0->m_pkthdr.len - sizeof(*l2cap_hdr));
516 l2cap_hdr->dcid = htole16(dcid);
519 * Segment single L2CAP packet according to the HCI layer MTU. Convert
520 * each segment into ACL data packet and prepend it with ACL data packet
521 * header. Link all segments together via m_nextpkt link.
523 * XXX BC (Broadcast flag) will always be 0 (zero).
527 /* Check length of the packet against HCI MTU */
528 len = m0->m_pkthdr.len;
529 if (len > l2cap->pkt_size) {
530 m = m_split(m0, l2cap->pkt_size, M_NOWAIT);
533 "%s: %s - m_split(%d) failed\n", __func__, NG_NODE_NAME(l2cap->node),
538 len = l2cap->pkt_size;
541 /* Convert packet fragment into ACL data packet */
542 m0 = ng_l2cap_prepend(m0, sizeof(*acl_hdr));
545 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
546 __func__, NG_NODE_NAME(l2cap->node),
551 acl_hdr = mtod(m0, ng_hci_acldata_pkt_t *);
552 acl_hdr->type = NG_HCI_ACL_DATA_PKT;
553 acl_hdr->length = htole16(len);
554 acl_hdr->con_handle = htole16(NG_HCI_MK_CON_HANDLE(
555 con->con_handle, flag, 0));
557 /* Add fragment to the chain */
558 m0->m_nextpkt = NULL;
560 if (con->tx_pkt == NULL)
561 con->tx_pkt = m_last = m0;
563 m_last->m_nextpkt = m0;
568 "%s: %s - attaching ACL packet, con_handle=%d, PB=%#x, length=%d\n",
569 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
574 flag = NG_HCI_PACKET_FRAGMENT;
582 while (con->tx_pkt != NULL) {
583 m = con->tx_pkt->m_nextpkt;
584 m_freem(con->tx_pkt);
589 } /* ng_l2cap_lp_send */
592 * Receive ACL data packet from the HCI layer. First strip ACL packet header
593 * and get connection handle, PB (Packet Boundary) flag and payload length.
594 * Then find connection descriptor and verify its state. Then process ACL
597 * 1) If we got first segment (pb == NG_HCI_PACKET_START) then extract L2CAP
598 * header and get total length of the L2CAP packet. Then start new L2CAP
601 * 2) If we got other (then first :) segment (pb == NG_HCI_PACKET_FRAGMENT)
602 * then add segment to the packet.
606 ng_l2cap_lp_receive(ng_l2cap_p l2cap, struct mbuf *m)
608 ng_hci_acldata_pkt_t *acl_hdr = NULL;
609 ng_l2cap_hdr_t *l2cap_hdr = NULL;
610 ng_l2cap_con_p con = NULL;
611 u_int16_t con_handle, length, pb;
614 /* Check ACL data packet */
615 if (m->m_pkthdr.len < sizeof(*acl_hdr)) {
617 "%s: %s - invalid ACL data packet. Packet too small, length=%d\n",
618 __func__, NG_NODE_NAME(l2cap->node), m->m_pkthdr.len);
623 /* Strip ACL data packet header */
624 NG_L2CAP_M_PULLUP(m, sizeof(*acl_hdr));
628 acl_hdr = mtod(m, ng_hci_acldata_pkt_t *);
629 m_adj(m, sizeof(*acl_hdr));
631 /* Get ACL connection handle, PB flag and payload length */
632 acl_hdr->con_handle = le16toh(acl_hdr->con_handle);
633 con_handle = NG_HCI_CON_HANDLE(acl_hdr->con_handle);
634 pb = NG_HCI_PB_FLAG(acl_hdr->con_handle);
635 length = le16toh(acl_hdr->length);
638 "%s: %s - got ACL data packet, con_handle=%d, PB=%#x, length=%d\n",
639 __func__, NG_NODE_NAME(l2cap->node), con_handle, pb, length);
641 /* Get connection descriptor */
642 con = ng_l2cap_con_by_handle(l2cap, con_handle);
645 "%s: %s - unexpected ACL data packet. " \
646 "Connection does not exist, con_handle=%d\n",
647 __func__, NG_NODE_NAME(l2cap->node), con_handle);
652 /* Verify connection state */
653 if (con->state != NG_L2CAP_CON_OPEN) {
655 "%s: %s - unexpected ACL data packet. Invalid connection state=%d\n",
656 __func__, NG_NODE_NAME(l2cap->node), con->state);
662 if (pb == NG_HCI_PACKET_START) {
663 if (con->rx_pkt != NULL) {
665 "%s: %s - dropping incomplete L2CAP packet, got %d bytes, want %d bytes\n",
666 __func__, NG_NODE_NAME(l2cap->node),
667 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
668 NG_FREE_M(con->rx_pkt);
672 /* Get L2CAP header */
673 if (m->m_pkthdr.len < sizeof(*l2cap_hdr)) {
675 "%s: %s - invalid L2CAP packet start fragment. Packet too small, length=%d\n",
676 __func__, NG_NODE_NAME(l2cap->node),
682 NG_L2CAP_M_PULLUP(m, sizeof(*l2cap_hdr));
686 l2cap_hdr = mtod(m, ng_l2cap_hdr_t *);
689 "%s: %s - staring new L2CAP packet, con_handle=%d, length=%d\n",
690 __func__, NG_NODE_NAME(l2cap->node), con_handle,
691 le16toh(l2cap_hdr->length));
693 /* Start new L2CAP packet */
695 con->rx_pkt_len = le16toh(l2cap_hdr->length)+sizeof(*l2cap_hdr);
696 } else if (pb == NG_HCI_PACKET_FRAGMENT) {
697 if (con->rx_pkt == NULL) {
699 "%s: %s - unexpected ACL data packet fragment, con_handle=%d\n",
700 __func__, NG_NODE_NAME(l2cap->node),
705 /* Add fragment to the L2CAP packet */
706 m_cat(con->rx_pkt, m);
707 con->rx_pkt->m_pkthdr.len += length;
710 "%s: %s - invalid ACL data packet. Invalid PB flag=%#x\n",
711 __func__, NG_NODE_NAME(l2cap->node), pb);
716 con->rx_pkt_len -= length;
717 if (con->rx_pkt_len < 0) {
719 "%s: %s - packet length mismatch. Got %d bytes, offset %d bytes\n",
720 __func__, NG_NODE_NAME(l2cap->node),
721 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
722 NG_FREE_M(con->rx_pkt);
724 } else if (con->rx_pkt_len == 0) {
725 /* OK, we have got complete L2CAP packet, so process it */
726 error = ng_l2cap_receive(con);
737 } /* ng_l2cap_lp_receive */
740 * Send queued ACL packets to the HCI layer
744 ng_l2cap_lp_deliver(ng_l2cap_con_p con)
746 ng_l2cap_p l2cap = con->l2cap;
747 struct mbuf *m = NULL;
750 /* Check connection */
751 if (con->state != NG_L2CAP_CON_OPEN)
754 if (con->tx_pkt == NULL)
755 ng_l2cap_con_wakeup(con);
757 if (con->tx_pkt == NULL)
760 /* Check if lower layer protocol is still connected */
761 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
763 "%s: %s - hook \"%s\" is not connected or valid",
764 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
766 goto drop; /* XXX what to do with "pending"? */
769 /* Send ACL data packets */
770 while (con->pending < con->l2cap->num_pkts && con->tx_pkt != NULL) {
772 con->tx_pkt = con->tx_pkt->m_nextpkt;
775 if(m->m_flags &M_PROTO2){
776 ng_l2cap_lp_receive(con->l2cap, m);
780 "%s: %s - sending ACL packet, con_handle=%d, len=%d\n",
781 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
784 NG_SEND_DATA_ONLY(error, l2cap->hci, m);
787 "%s: %s - could not send ACL data packet, con_handle=%d, error=%d\n",
788 __func__, NG_NODE_NAME(l2cap->node),
789 con->con_handle, error);
791 goto drop; /* XXX what to do with "pending"? */
798 "%s: %s - %d ACL packets have been sent, con_handle=%d\n",
799 __func__, NG_NODE_NAME(l2cap->node), con->pending,
805 while (con->tx_pkt != NULL) {
806 m = con->tx_pkt->m_nextpkt;
807 m_freem(con->tx_pkt);
810 } /* ng_l2cap_lp_deliver */
813 * Process connection timeout. Remove connection from the list. If there
814 * are any channels that wait for the connection then notify them. Free
815 * connection descriptor.
819 ng_l2cap_process_lp_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
821 ng_l2cap_p l2cap = NULL;
822 ng_l2cap_con_p con = NULL;
824 if (NG_NODE_NOT_VALID(node)) {
825 printf("%s: Netgraph node is not valid\n", __func__);
829 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
830 con = ng_l2cap_con_by_handle(l2cap, con_handle);
834 "%s: %s - could not find connection, con_handle=%d\n",
835 __func__, NG_NODE_NAME(node), con_handle);
839 if (!(con->flags & NG_L2CAP_CON_LP_TIMO)) {
841 "%s: %s - no pending LP timeout, con_handle=%d, state=%d, flags=%#x\n",
842 __func__, NG_NODE_NAME(node), con_handle, con->state,
848 * Notify channels that connection has timed out. This will remove
849 * connection, channels and pending commands.
852 con->flags &= ~NG_L2CAP_CON_LP_TIMO;
853 ng_l2cap_con_fail(con, NG_L2CAP_TIMEOUT);
854 } /* ng_l2cap_process_lp_timeout */
857 * Process auto disconnect timeout and send LP_DisconReq event to the
858 * lower layer protocol
862 ng_l2cap_process_discon_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
864 ng_l2cap_p l2cap = NULL;
865 ng_l2cap_con_p con = NULL;
866 struct ng_mesg *msg = NULL;
867 ng_hci_lp_discon_req_ep *ep = NULL;
870 if (NG_NODE_NOT_VALID(node)) {
871 printf("%s: Netgraph node is not valid\n", __func__);
875 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
876 con = ng_l2cap_con_by_handle(l2cap, con_handle);
880 "%s: %s - could not find connection, con_handle=%d\n",
881 __func__, NG_NODE_NAME(node), con_handle);
885 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)) {
887 "%s: %s - no pending disconnect timeout, con_handle=%d, state=%d, flags=%#x\n",
888 __func__, NG_NODE_NAME(node), con_handle, con->state,
893 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO;
895 /* Check if lower layer protocol is still connected */
896 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
898 "%s: %s - hook \"%s\" is not connected or valid\n",
899 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
903 /* Create and send LP_DisconReq event */
904 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_DISCON_REQ,
905 sizeof(*ep), M_NOWAIT);
909 ep = (ng_hci_lp_discon_req_ep *) (msg->data);
910 ep->con_handle = con->con_handle;
911 ep->reason = 0x13; /* User Ended Connection */
913 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
914 } /* ng_l2cap_process_discon_timeout */