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)
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);
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);
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 = NG_HCI_LINK_ACL;
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 ((error = ng_l2cap_lp_untimeout(con)) != 0)
122 ng_l2cap_free_con(con);
126 } /* ng_l2cap_lp_con_req */
129 * Process LP_ConnectCfm event from the lower layer protocol. It could be
130 * positive or negative. Verify remote unit address then stop the timer and
135 ng_l2cap_lp_con_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
137 ng_hci_lp_con_cfm_ep *ep = NULL;
138 ng_l2cap_con_p con = NULL;
142 if (msg->header.arglen != sizeof(*ep)) {
144 "%s: %s - invalid LP_ConnectCfm[Neg] message size\n",
145 __func__, NG_NODE_NAME(l2cap->node));
150 ep = (ng_hci_lp_con_cfm_ep *) (msg->data);
152 /* Check if we have requested/accepted this connection */
153 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr);
156 "%s: %s - unexpected LP_ConnectCfm event. Connection does not exist\n",
157 __func__, NG_NODE_NAME(l2cap->node));
162 /* Check connection state */
163 if (con->state != NG_L2CAP_W4_LP_CON_CFM) {
165 "%s: %s - unexpected LP_ConnectCfm event. " \
166 "Invalid connection state, state=%d, con_handle=%d\n",
167 __func__, NG_NODE_NAME(l2cap->node), con->state,
174 * Looks like it is our confirmation. It is safe now to cancel
175 * connection timer and notify upper layer. If timeout already
176 * happened then ignore connection confirmation and let timeout
180 if ((error = ng_l2cap_lp_untimeout(con)) != 0)
183 if (ep->status == 0) {
184 con->state = NG_L2CAP_CON_OPEN;
185 con->con_handle = ep->con_handle;
186 ng_l2cap_lp_deliver(con);
187 } else /* Negative confirmation - remove connection descriptor */
188 ng_l2cap_con_fail(con, ep->status);
191 } /* ng_l2cap_lp_con_cfm */
194 * Process LP_ConnectInd event from the lower layer protocol. This is a good
195 * place to put some extra check on remote unit address and/or class. We could
196 * even forward this information to control hook (or check against internal
197 * black list) and thus implement some kind of firewall. But for now be simple
198 * and create new connection descriptor, start timer and send LP_ConnectRsp
199 * event (i.e. accept connection).
203 ng_l2cap_lp_con_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
205 ng_hci_lp_con_ind_ep *ep = NULL;
206 ng_hci_lp_con_rsp_ep *rp = NULL;
207 struct ng_mesg *rsp = NULL;
208 ng_l2cap_con_p con = NULL;
212 if (msg->header.arglen != sizeof(*ep)) {
214 "%s: %s - invalid LP_ConnectInd message size\n",
215 __func__, NG_NODE_NAME(l2cap->node));
220 ep = (ng_hci_lp_con_ind_ep *) (msg->data);
222 /* Make sure we have only one connection to the remote unit */
223 con = ng_l2cap_con_by_addr(l2cap, &ep->bdaddr);
226 "%s: %s - unexpected LP_ConnectInd event. " \
227 "Connection already exists, state=%d, con_handle=%d\n",
228 __func__, NG_NODE_NAME(l2cap->node), con->state,
234 /* Check if lower layer protocol is still connected */
235 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
237 "%s: %s - hook \"%s\" is not connected or valid",
238 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
243 /* Create and intialize new connection descriptor */
244 con = ng_l2cap_new_con(l2cap, &ep->bdaddr);
250 /* Create and send LP_ConnectRsp event */
251 NG_MKMESSAGE(rsp, NGM_HCI_COOKIE, NGM_HCI_LP_CON_RSP,
252 sizeof(*rp), M_NOWAIT);
254 ng_l2cap_free_con(con);
259 rp = (ng_hci_lp_con_rsp_ep *)(rsp->data);
260 rp->status = 0x00; /* accept connection */
261 rp->link_type = NG_HCI_LINK_ACL;
262 bcopy(&ep->bdaddr, &rp->bdaddr, sizeof(rp->bdaddr));
264 con->state = NG_L2CAP_W4_LP_CON_CFM;
265 ng_l2cap_lp_timeout(con);
267 NG_SEND_MSG_HOOK(error, l2cap->node, rsp, l2cap->hci, 0);
269 if ((error = ng_l2cap_lp_untimeout(con)) != 0)
272 ng_l2cap_free_con(con);
276 } /* ng_hci_lp_con_ind */
279 * Process LP_DisconnectInd event from the lower layer protocol. We have been
280 * disconnected from the remote unit. So notify the upper layer protocol.
284 ng_l2cap_lp_discon_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
286 ng_hci_lp_discon_ind_ep *ep = NULL;
287 ng_l2cap_con_p con = NULL;
291 if (msg->header.arglen != sizeof(*ep)) {
293 "%s: %s - invalid LP_DisconnectInd message size\n",
294 __func__, NG_NODE_NAME(l2cap->node));
299 ep = (ng_hci_lp_discon_ind_ep *) (msg->data);
301 /* Check if we have this connection */
302 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
305 "%s: %s - unexpected LP_DisconnectInd event. " \
306 "Connection does not exist, con_handle=%d\n",
307 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
312 /* XXX Verify connection state -- do we need to check this? */
313 if (con->state != NG_L2CAP_CON_OPEN) {
315 "%s: %s - unexpected LP_DisconnectInd event. " \
316 "Invalid connection state, state=%d, con_handle=%d\n",
317 __func__, NG_NODE_NAME(l2cap->node), con->state,
324 * Notify upper layer and remove connection
325 * Note: The connection could have auto disconnect timeout set. Try
326 * to remove it. If auto disconnect timeout happened then ignore
327 * disconnect indication and let timeout handle that.
330 if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)
331 if ((error = ng_l2cap_discon_untimeout(con)) != 0)
334 ng_l2cap_con_fail(con, ep->reason);
337 } /* ng_l2cap_lp_discon_ind */
340 * Send LP_QoSSetupReq event to the lower layer protocol
344 ng_l2cap_lp_qos_req(ng_l2cap_p l2cap, u_int16_t con_handle,
345 ng_l2cap_flow_p flow)
347 struct ng_mesg *msg = NULL;
348 ng_hci_lp_qos_req_ep *ep = NULL;
349 ng_l2cap_con_p con = NULL;
352 /* Verify that we have this connection */
353 con = ng_l2cap_con_by_handle(l2cap, con_handle);
356 "%s: %s - unexpected LP_QoSSetupReq event. " \
357 "Connection does not exist, con_handle=%d\n",
358 __func__, NG_NODE_NAME(l2cap->node), con_handle);
363 /* Verify connection state */
364 if (con->state != NG_L2CAP_CON_OPEN) {
366 "%s: %s - unexpected LP_QoSSetupReq event. " \
367 "Invalid connection state, state=%d, con_handle=%d\n",
368 __func__, NG_NODE_NAME(l2cap->node), con->state,
374 /* Check if lower layer protocol is still connected */
375 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
377 "%s: %s - hook \"%s\" is not connected or valid",
378 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
383 /* Create and send LP_QoSSetupReq event */
384 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_QOS_REQ,
385 sizeof(*ep), M_NOWAIT);
389 ep = (ng_hci_lp_qos_req_ep *) (msg->data);
390 ep->con_handle = con_handle;
391 ep->flags = flow->flags;
392 ep->service_type = flow->service_type;
393 ep->token_rate = flow->token_rate;
394 ep->peak_bandwidth = flow->peak_bandwidth;
395 ep->latency = flow->latency;
396 ep->delay_variation = flow->delay_variation;
398 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
401 } /* ng_l2cap_lp_con_req */
404 * Process LP_QoSSetupCfm from the lower layer protocol
408 ng_l2cap_lp_qos_cfm(ng_l2cap_p l2cap, struct ng_mesg *msg)
410 ng_hci_lp_qos_cfm_ep *ep = NULL;
414 if (msg->header.arglen != sizeof(*ep)) {
416 "%s: %s - invalid LP_QoSSetupCfm[Neg] message size\n",
417 __func__, NG_NODE_NAME(l2cap->node));
422 ep = (ng_hci_lp_qos_cfm_ep *) (msg->data);
423 /* XXX FIXME do something */
426 } /* ng_l2cap_lp_qos_cfm */
429 * Process LP_QoSViolationInd event from the lower layer protocol. Lower
430 * layer protocol has detected QoS Violation, so we MUST notify the
435 ng_l2cap_lp_qos_ind(ng_l2cap_p l2cap, struct ng_mesg *msg)
437 ng_hci_lp_qos_ind_ep *ep = NULL;
438 ng_l2cap_con_p con = NULL;
442 if (msg->header.arglen != sizeof(*ep)) {
444 "%s: %s - invalid LP_QoSViolation message size\n",
445 __func__, NG_NODE_NAME(l2cap->node));
450 ep = (ng_hci_lp_qos_ind_ep *) (msg->data);
452 /* Check if we have this connection */
453 con = ng_l2cap_con_by_handle(l2cap, ep->con_handle);
456 "%s: %s - unexpected LP_QoSViolationInd event. " \
457 "Connection does not exist, con_handle=%d\n",
458 __func__, NG_NODE_NAME(l2cap->node), ep->con_handle);
463 /* Verify connection state */
464 if (con->state != NG_L2CAP_CON_OPEN) {
466 "%s: %s - unexpected LP_QoSViolationInd event. " \
467 "Invalid connection state, state=%d, con_handle=%d\n",
468 __func__, NG_NODE_NAME(l2cap->node), con->state,
474 /* XXX FIXME Notify upper layer and terminate channels if required */
477 } /* ng_l2cap_qos_ind */
480 * Prepare L2CAP packet. Prepend packet with L2CAP packet header and then
481 * segment it according to HCI MTU.
485 ng_l2cap_lp_send(ng_l2cap_con_p con, u_int16_t dcid, struct mbuf *m0)
487 ng_l2cap_p l2cap = con->l2cap;
488 ng_l2cap_hdr_t *l2cap_hdr = NULL;
489 ng_hci_acldata_pkt_t *acl_hdr = NULL;
490 struct mbuf *m_last = NULL, *m = NULL;
491 int len, flag = NG_HCI_PACKET_START;
493 KASSERT((con->tx_pkt == NULL),
494 ("%s: %s - another packet pending?!\n", __func__, NG_NODE_NAME(l2cap->node)));
495 KASSERT((l2cap->pkt_size > 0),
496 ("%s: %s - invalid l2cap->pkt_size?!\n", __func__, NG_NODE_NAME(l2cap->node)));
498 /* Prepend mbuf with L2CAP header */
499 m0 = ng_l2cap_prepend(m0, sizeof(*l2cap_hdr));
502 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
503 __func__, NG_NODE_NAME(l2cap->node),
509 l2cap_hdr = mtod(m0, ng_l2cap_hdr_t *);
510 l2cap_hdr->length = htole16(m0->m_pkthdr.len - sizeof(*l2cap_hdr));
511 l2cap_hdr->dcid = htole16(dcid);
514 * Segment single L2CAP packet according to the HCI layer MTU. Convert
515 * each segment into ACL data packet and prepend it with ACL data packet
516 * header. Link all segments together via m_nextpkt link.
518 * XXX BC (Broadcast flag) will always be 0 (zero).
522 /* Check length of the packet against HCI MTU */
523 len = m0->m_pkthdr.len;
524 if (len > l2cap->pkt_size) {
525 m = m_split(m0, l2cap->pkt_size, M_DONTWAIT);
528 "%s: %s - m_split(%d) failed\n", __func__, NG_NODE_NAME(l2cap->node),
533 len = l2cap->pkt_size;
536 /* Convert packet fragment into ACL data packet */
537 m0 = ng_l2cap_prepend(m0, sizeof(*acl_hdr));
540 "%s: %s - ng_l2cap_prepend(%zd) failed\n",
541 __func__, NG_NODE_NAME(l2cap->node),
546 acl_hdr = mtod(m0, ng_hci_acldata_pkt_t *);
547 acl_hdr->type = NG_HCI_ACL_DATA_PKT;
548 acl_hdr->length = htole16(len);
549 acl_hdr->con_handle = htole16(NG_HCI_MK_CON_HANDLE(
550 con->con_handle, flag, 0));
552 /* Add fragment to the chain */
553 m0->m_nextpkt = NULL;
555 if (con->tx_pkt == NULL)
556 con->tx_pkt = m_last = m0;
558 m_last->m_nextpkt = m0;
563 "%s: %s - attaching ACL packet, con_handle=%d, PB=%#x, length=%d\n",
564 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
569 flag = NG_HCI_PACKET_FRAGMENT;
577 while (con->tx_pkt != NULL) {
578 m = con->tx_pkt->m_nextpkt;
579 m_freem(con->tx_pkt);
584 } /* ng_l2cap_lp_send */
587 * Receive ACL data packet from the HCI layer. First strip ACL packet header
588 * and get connection handle, PB (Packet Boundary) flag and payload length.
589 * Then find connection descriptor and verify its state. Then process ACL
592 * 1) If we got first segment (pb == NG_HCI_PACKET_START) then extract L2CAP
593 * header and get total length of the L2CAP packet. Then start new L2CAP
596 * 2) If we got other (then first :) segment (pb == NG_HCI_PACKET_FRAGMENT)
597 * then add segment to the packet.
601 ng_l2cap_lp_receive(ng_l2cap_p l2cap, struct mbuf *m)
603 ng_hci_acldata_pkt_t *acl_hdr = NULL;
604 ng_l2cap_hdr_t *l2cap_hdr = NULL;
605 ng_l2cap_con_p con = NULL;
606 u_int16_t con_handle, length, pb;
609 /* Check ACL data packet */
610 if (m->m_pkthdr.len < sizeof(*acl_hdr)) {
612 "%s: %s - invalid ACL data packet. Packet too small, length=%d\n",
613 __func__, NG_NODE_NAME(l2cap->node), m->m_pkthdr.len);
618 /* Strip ACL data packet header */
619 NG_L2CAP_M_PULLUP(m, sizeof(*acl_hdr));
623 acl_hdr = mtod(m, ng_hci_acldata_pkt_t *);
624 m_adj(m, sizeof(*acl_hdr));
626 /* Get ACL connection handle, PB flag and payload length */
627 acl_hdr->con_handle = le16toh(acl_hdr->con_handle);
628 con_handle = NG_HCI_CON_HANDLE(acl_hdr->con_handle);
629 pb = NG_HCI_PB_FLAG(acl_hdr->con_handle);
630 length = le16toh(acl_hdr->length);
633 "%s: %s - got ACL data packet, con_handle=%d, PB=%#x, length=%d\n",
634 __func__, NG_NODE_NAME(l2cap->node), con_handle, pb, length);
636 /* Get connection descriptor */
637 con = ng_l2cap_con_by_handle(l2cap, con_handle);
640 "%s: %s - unexpected ACL data packet. " \
641 "Connection does not exist, con_handle=%d\n",
642 __func__, NG_NODE_NAME(l2cap->node), con_handle);
647 /* Verify connection state */
648 if (con->state != NG_L2CAP_CON_OPEN) {
650 "%s: %s - unexpected ACL data packet. Invalid connection state=%d\n",
651 __func__, NG_NODE_NAME(l2cap->node), con->state);
657 if (pb == NG_HCI_PACKET_START) {
658 if (con->rx_pkt != NULL) {
660 "%s: %s - dropping incomplete L2CAP packet, got %d bytes, want %d bytes\n",
661 __func__, NG_NODE_NAME(l2cap->node),
662 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
663 NG_FREE_M(con->rx_pkt);
667 /* Get L2CAP header */
668 if (m->m_pkthdr.len < sizeof(*l2cap_hdr)) {
670 "%s: %s - invalid L2CAP packet start fragment. Packet too small, length=%d\n",
671 __func__, NG_NODE_NAME(l2cap->node),
677 NG_L2CAP_M_PULLUP(m, sizeof(*l2cap_hdr));
681 l2cap_hdr = mtod(m, ng_l2cap_hdr_t *);
684 "%s: %s - staring new L2CAP packet, con_handle=%d, length=%d\n",
685 __func__, NG_NODE_NAME(l2cap->node), con_handle,
686 le16toh(l2cap_hdr->length));
688 /* Start new L2CAP packet */
690 con->rx_pkt_len = le16toh(l2cap_hdr->length)+sizeof(*l2cap_hdr);
691 } else if (pb == NG_HCI_PACKET_FRAGMENT) {
692 if (con->rx_pkt == NULL) {
694 "%s: %s - unexpected ACL data packet fragment, con_handle=%d\n",
695 __func__, NG_NODE_NAME(l2cap->node),
700 /* Add fragment to the L2CAP packet */
701 m_cat(con->rx_pkt, m);
702 con->rx_pkt->m_pkthdr.len += length;
705 "%s: %s - invalid ACL data packet. Invalid PB flag=%#x\n",
706 __func__, NG_NODE_NAME(l2cap->node), pb);
711 con->rx_pkt_len -= length;
712 if (con->rx_pkt_len < 0) {
714 "%s: %s - packet length mismatch. Got %d bytes, offset %d bytes\n",
715 __func__, NG_NODE_NAME(l2cap->node),
716 con->rx_pkt->m_pkthdr.len, con->rx_pkt_len);
717 NG_FREE_M(con->rx_pkt);
719 } else if (con->rx_pkt_len == 0) {
720 /* OK, we have got complete L2CAP packet, so process it */
721 error = ng_l2cap_receive(con);
732 } /* ng_l2cap_lp_receive */
735 * Send queued ACL packets to the HCI layer
739 ng_l2cap_lp_deliver(ng_l2cap_con_p con)
741 ng_l2cap_p l2cap = con->l2cap;
742 struct mbuf *m = NULL;
745 /* Check connection */
746 if (con->state != NG_L2CAP_CON_OPEN)
749 if (con->tx_pkt == NULL)
750 ng_l2cap_con_wakeup(con);
752 if (con->tx_pkt == NULL)
755 /* Check if lower layer protocol is still connected */
756 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
758 "%s: %s - hook \"%s\" is not connected or valid",
759 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
761 goto drop; /* XXX what to do with "pending"? */
764 /* Send ACL data packets */
765 while (con->pending < con->l2cap->num_pkts && con->tx_pkt != NULL) {
767 con->tx_pkt = con->tx_pkt->m_nextpkt;
771 "%s: %s - sending ACL packet, con_handle=%d, len=%d\n",
772 __func__, NG_NODE_NAME(l2cap->node), con->con_handle,
775 NG_SEND_DATA_ONLY(error, l2cap->hci, m);
778 "%s: %s - could not send ACL data packet, con_handle=%d, error=%d\n",
779 __func__, NG_NODE_NAME(l2cap->node),
780 con->con_handle, error);
782 goto drop; /* XXX what to do with "pending"? */
789 "%s: %s - %d ACL packets have been sent, con_handle=%d\n",
790 __func__, NG_NODE_NAME(l2cap->node), con->pending,
796 while (con->tx_pkt != NULL) {
797 m = con->tx_pkt->m_nextpkt;
798 m_freem(con->tx_pkt);
801 } /* ng_l2cap_lp_deliver */
804 * Process connection timeout. Remove connection from the list. If there
805 * are any channels that wait for the connection then notify them. Free
806 * connection descriptor.
810 ng_l2cap_process_lp_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
812 ng_l2cap_p l2cap = NULL;
813 ng_l2cap_con_p con = NULL;
815 if (NG_NODE_NOT_VALID(node)) {
816 printf("%s: Netgraph node is not valid\n", __func__);
820 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
821 con = ng_l2cap_con_by_handle(l2cap, con_handle);
825 "%s: %s - could not find connection, con_handle=%d\n",
826 __func__, NG_NODE_NAME(node), con_handle);
830 if (!(con->flags & NG_L2CAP_CON_LP_TIMO)) {
832 "%s: %s - no pending LP timeout, con_handle=%d, state=%d, flags=%#x\n",
833 __func__, NG_NODE_NAME(node), con_handle, con->state,
839 * Notify channels that connection has timed out. This will remove
840 * connection, channels and pending commands.
843 con->flags &= ~NG_L2CAP_CON_LP_TIMO;
844 ng_l2cap_con_fail(con, NG_L2CAP_TIMEOUT);
845 } /* ng_l2cap_process_lp_timeout */
848 * Process auto disconnect timeout and send LP_DisconReq event to the
849 * lower layer protocol
853 ng_l2cap_process_discon_timeout(node_p node, hook_p hook, void *arg1, int con_handle)
855 ng_l2cap_p l2cap = NULL;
856 ng_l2cap_con_p con = NULL;
857 struct ng_mesg *msg = NULL;
858 ng_hci_lp_discon_req_ep *ep = NULL;
861 if (NG_NODE_NOT_VALID(node)) {
862 printf("%s: Netgraph node is not valid\n", __func__);
866 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node);
867 con = ng_l2cap_con_by_handle(l2cap, con_handle);
871 "%s: %s - could not find connection, con_handle=%d\n",
872 __func__, NG_NODE_NAME(node), con_handle);
876 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)) {
878 "%s: %s - no pending disconnect timeout, con_handle=%d, state=%d, flags=%#x\n",
879 __func__, NG_NODE_NAME(node), con_handle, con->state,
884 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO;
886 /* Check if lower layer protocol is still connected */
887 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci)) {
889 "%s: %s - hook \"%s\" is not connected or valid\n",
890 __func__, NG_NODE_NAME(l2cap->node), NG_L2CAP_HOOK_HCI);
894 /* Create and send LP_DisconReq event */
895 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_LP_DISCON_REQ,
896 sizeof(*ep), M_NOWAIT);
900 ep = (ng_hci_lp_discon_req_ep *) (msg->data);
901 ep->con_handle = con->con_handle;
902 ep->reason = 0x13; /* User Ended Connection */
904 NG_SEND_MSG_HOOK(error, l2cap->node, msg, l2cap->hci, 0);
905 } /* ng_l2cap_process_discon_timeout */