The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netbt/rfcomm_dlc.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $OpenBSD: rfcomm_dlc.c,v 1.4 2008/11/22 04:42:58 uwe Exp $      */
    2 /*      $NetBSD: rfcomm_dlc.c,v 1.6 2008/08/06 15:01:24 plunky Exp $    */
    3 
    4 /*-
    5  * Copyright (c) 2006 Itronix Inc.
    6  * All rights reserved.
    7  *
    8  * Written by Iain Hibbert for Itronix Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. The name of Itronix Inc. may not be used to endorse
   19  *    or promote products derived from this software without specific
   20  *    prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
   26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   29  * ON ANY THEORY OF LIABILITY, WHETHER IN
   30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32  * POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 
   35 #include <sys/param.h>
   36 #include <sys/kernel.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/proc.h>
   39 #include <sys/systm.h>
   40 
   41 #include <netbt/bluetooth.h>
   42 #include <netbt/hci.h>
   43 #include <netbt/l2cap.h>
   44 #include <netbt/rfcomm.h>
   45 
   46 /*
   47  * rfcomm_dlc_lookup(rfcomm_session, dlci)
   48  *
   49  * Find DLC on session with matching dlci
   50  */
   51 struct rfcomm_dlc *
   52 rfcomm_dlc_lookup(struct rfcomm_session *rs, int dlci)
   53 {
   54         struct rfcomm_dlc *dlc;
   55 
   56         LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) {
   57                 if (dlc->rd_dlci == dlci)
   58                         break;
   59         }
   60 
   61         return dlc;
   62 }
   63 
   64 /*
   65  * rfcomm_dlc_newconn(rfcomm_session, dlci)
   66  *
   67  * handle a new dlc request (since its called from a couple of places)
   68  */
   69 struct rfcomm_dlc *
   70 rfcomm_dlc_newconn(struct rfcomm_session *rs, int dlci)
   71 {
   72         struct rfcomm_session *ls;
   73         struct rfcomm_dlc *new, *dlc, *any, *best;
   74         struct sockaddr_bt laddr, raddr, addr;
   75         int chan;
   76 
   77         /*
   78          * Search amongst the listening DLC community for the best match for
   79          * address & channel. We keep listening DLC's hanging on listening
   80          * sessions in a last first order, so scan the entire bunch and keep
   81          * a note of the best address and BDADDR_ANY matches in order to find
   82          * the oldest and most specific match.
   83          */
   84         l2cap_sockaddr(rs->rs_l2cap, &laddr);
   85         l2cap_peeraddr(rs->rs_l2cap, &raddr);
   86         chan = RFCOMM_CHANNEL(dlci);
   87         new = NULL;
   88 
   89         any = best = NULL;
   90         LIST_FOREACH(ls, &rfcomm_session_listen, rs_next) {
   91                 l2cap_sockaddr(ls->rs_l2cap, &addr);
   92 
   93                 if (addr.bt_psm != laddr.bt_psm)
   94                         continue;
   95 
   96                 if (bdaddr_same(&laddr.bt_bdaddr, &addr.bt_bdaddr)) {
   97                         LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
   98                                 if (dlc->rd_laddr.bt_channel == chan)
   99                                         best = dlc;
  100                         }
  101                 }
  102 
  103                 if (bdaddr_any(&addr.bt_bdaddr)) {
  104                         LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
  105                                 if (dlc->rd_laddr.bt_channel == chan)
  106                                         any = dlc;
  107                         }
  108                 }
  109         }
  110 
  111         dlc = best ? best : any;
  112 
  113         /* XXX
  114          * Note that if this fails, we could have missed a chance to open
  115          * a connection - really need to rewrite the strategy for storing
  116          * listening DLC's so all can be checked in turn..
  117          */
  118         if (dlc != NULL)
  119                 new = (*dlc->rd_proto->newconn)(dlc->rd_upper, &laddr, &raddr);
  120 
  121         if (new == NULL) {
  122                 rfcomm_session_send_frame(rs, RFCOMM_FRAME_DM, dlci);
  123                 return NULL;
  124         }
  125 
  126         new->rd_dlci = dlci;
  127         new->rd_mtu = rfcomm_mtu_default;
  128         new->rd_mode = dlc->rd_mode;
  129 
  130         memcpy(&new->rd_laddr, &laddr, sizeof(struct sockaddr_bt));
  131         new->rd_laddr.bt_channel = chan;
  132 
  133         memcpy(&new->rd_raddr, &raddr, sizeof(struct sockaddr_bt));
  134         new->rd_raddr.bt_channel = chan;
  135 
  136         new->rd_session = rs;
  137         new->rd_state = RFCOMM_DLC_WAIT_CONNECT;
  138         LIST_INSERT_HEAD(&rs->rs_dlcs, new, rd_next);
  139 
  140         return new;
  141 }
  142 
  143 /*
  144  * rfcomm_dlc_close(dlc, error)
  145  *
  146  * detach DLC from session and clean up
  147  */
  148 void
  149 rfcomm_dlc_close(struct rfcomm_dlc *dlc, int err)
  150 {
  151         struct rfcomm_session *rs;
  152         struct rfcomm_credit *credit;
  153 
  154         KASSERT(dlc->rd_state != RFCOMM_DLC_CLOSED);
  155 
  156         /* Clear credit history */
  157         rs = dlc->rd_session;
  158         SIMPLEQ_FOREACH(credit, &rs->rs_credits, rc_next)
  159                 if (credit->rc_dlc == dlc)
  160                         credit->rc_dlc = NULL;
  161 
  162         timeout_del(&dlc->rd_timeout);
  163 
  164         LIST_REMOVE(dlc, rd_next);
  165         dlc->rd_session = NULL;
  166         dlc->rd_state = RFCOMM_DLC_CLOSED;
  167 
  168         (*dlc->rd_proto->disconnected)(dlc->rd_upper, err);
  169 
  170         /*
  171          * It is the responsibility of the party who sends the last
  172          * DISC(dlci) to disconnect the session, but we will schedule
  173          * an expiry just in case that doesnt happen..
  174          */
  175         if (LIST_EMPTY(&rs->rs_dlcs)) {
  176                 if (rs->rs_state == RFCOMM_SESSION_LISTEN)
  177                         rfcomm_session_free(rs);
  178                 else
  179                         timeout_add_sec(&rs->rs_timeout, rfcomm_ack_timeout);
  180         }
  181 }
  182 
  183 /*
  184  * rfcomm_dlc_timeout(dlc)
  185  *
  186  * DLC timeout function is schedUled when we sent any of SABM,
  187  * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get
  188  * the relevant response. There is nothing to do but shut this
  189  * DLC down.
  190  */
  191 void
  192 rfcomm_dlc_timeout(void *arg)
  193 {
  194         struct rfcomm_dlc *dlc = arg;
  195 
  196         mutex_enter(&bt_lock);
  197 
  198         if (dlc->rd_state != RFCOMM_DLC_CLOSED)
  199                 rfcomm_dlc_close(dlc, ETIMEDOUT);
  200         else if (dlc->rd_flags & RFCOMM_DLC_DETACH)
  201                 free(dlc, M_BLUETOOTH);
  202 
  203         mutex_exit(&bt_lock);
  204 }
  205 
  206 /*
  207  * rfcomm_dlc_setmode(rfcomm_dlc)
  208  *
  209  * Set link mode for DLC.  This is only called when the session is
  210  * already open, so we don't need to worry about any previous mode
  211  * settings.
  212  */
  213 int
  214 rfcomm_dlc_setmode(struct rfcomm_dlc *dlc)
  215 {
  216         int mode = 0;
  217 
  218         KASSERT(dlc->rd_session != NULL);
  219         KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
  220 
  221         DPRINTF("dlci %d, auth %s, encrypt %s, secure %s\n", dlc->rd_dlci,
  222                 (dlc->rd_mode & RFCOMM_LM_AUTH ? "yes" : "no"),
  223                 (dlc->rd_mode & RFCOMM_LM_ENCRYPT ? "yes" : "no"),
  224                 (dlc->rd_mode & RFCOMM_LM_SECURE ? "yes" : "no"));
  225 
  226         if (dlc->rd_mode & RFCOMM_LM_AUTH)
  227                 mode |= L2CAP_LM_AUTH;
  228 
  229         if (dlc->rd_mode & RFCOMM_LM_ENCRYPT)
  230                 mode |= L2CAP_LM_ENCRYPT;
  231 
  232         if (dlc->rd_mode & RFCOMM_LM_SECURE)
  233                 mode |= L2CAP_LM_SECURE;
  234 
  235         return l2cap_setopt(dlc->rd_session->rs_l2cap, SO_L2CAP_LM, &mode);
  236 }
  237 
  238 /*
  239  * rfcomm_dlc_connect(rfcomm_dlc)
  240  *
  241  * initiate DLC connection (session is already connected)
  242  */
  243 int
  244 rfcomm_dlc_connect(struct rfcomm_dlc *dlc)
  245 {
  246         struct rfcomm_mcc_pn pn;
  247         int err = 0;
  248 
  249         KASSERT(dlc->rd_session != NULL);
  250         KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
  251         KASSERT(dlc->rd_state == RFCOMM_DLC_WAIT_SESSION);
  252 
  253         /*
  254          * If we have not already sent a PN on the session, we must send
  255          * a PN to negotiate Credit Flow Control, and this setting will
  256          * apply to all future connections for this session. We ask for
  257          * this every time, in order to establish initial credits.
  258          */
  259         memset(&pn, 0, sizeof(pn));
  260         pn.dlci = dlc->rd_dlci;
  261         pn.priority = dlc->rd_dlci | 0x07;
  262         pn.mtu = htole16(dlc->rd_mtu);
  263 
  264         pn.flow_control = 0xf0;
  265         dlc->rd_rxcred = (dlc->rd_rxsize / dlc->rd_mtu);
  266         dlc->rd_rxcred = min(dlc->rd_rxcred, RFCOMM_CREDITS_DEFAULT);
  267         pn.credits = dlc->rd_rxcred;
  268 
  269         err = rfcomm_session_send_mcc(dlc->rd_session, 1,
  270                                         RFCOMM_MCC_PN, &pn, sizeof(pn));
  271         if (err)
  272                 return err;
  273 
  274         dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT;
  275         timeout_add_sec(&dlc->rd_timeout, rfcomm_mcc_timeout);
  276 
  277         return 0;
  278 }
  279 
  280 /*
  281  * rfcomm_dlc_open(rfcomm_dlc)
  282  *
  283  * send "Modem Status Command" and mark DLC as open.
  284  */
  285 int
  286 rfcomm_dlc_open(struct rfcomm_dlc *dlc)
  287 {
  288         struct rfcomm_mcc_msc msc;
  289         int err;
  290 
  291         KASSERT(dlc->rd_session != NULL);
  292         KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
  293 
  294         memset(&msc, 0, sizeof(msc));
  295         msc.address = RFCOMM_MKADDRESS(1, dlc->rd_dlci);
  296         msc.modem = dlc->rd_lmodem & 0xfe;      /* EA = 0 */
  297         msc.brk =       0x00       | 0x01;      /* EA = 1 */
  298 
  299         err = rfcomm_session_send_mcc(dlc->rd_session, 1,
  300                                 RFCOMM_MCC_MSC, &msc, sizeof(msc));
  301         if (err)
  302                 return err;
  303 
  304         timeout_add_sec(&dlc->rd_timeout, rfcomm_mcc_timeout);
  305 
  306         dlc->rd_state = RFCOMM_DLC_OPEN;
  307         (*dlc->rd_proto->connected)(dlc->rd_upper);
  308 
  309         return 0;
  310 }
  311 
  312 /*
  313  * rfcomm_dlc_start(rfcomm_dlc)
  314  *
  315  * Start sending data (and/or credits) for DLC. Our strategy is to
  316  * send anything we can down to the l2cap layer. When credits run
  317  * out, data will naturally bunch up. When not using credit flow
  318  * control, we limit the number of packets we have pending to reduce
  319  * flow control lag.
  320  * We should deal with channel priority somehow.
  321  */
  322 void
  323 rfcomm_dlc_start(struct rfcomm_dlc *dlc)
  324 {
  325         struct rfcomm_session *rs = dlc->rd_session;
  326         struct mbuf *m;
  327         int len, credits;
  328 
  329         KASSERT(rs != NULL);
  330         KASSERT(rs->rs_state == RFCOMM_SESSION_OPEN);
  331         KASSERT(dlc->rd_state == RFCOMM_DLC_OPEN);
  332 
  333         for (;;) {
  334                 credits = 0;
  335                 len = dlc->rd_mtu;
  336                 if (rs->rs_flags & RFCOMM_SESSION_CFC) {
  337                         credits = (dlc->rd_rxsize / dlc->rd_mtu);
  338                         credits -= dlc->rd_rxcred;
  339                         credits = min(credits, RFCOMM_CREDITS_MAX);
  340 
  341                         if (credits > 0)
  342                                 len--;
  343 
  344                         if (dlc->rd_txcred == 0)
  345                                 len = 0;
  346                 } else {
  347                         if (rs->rs_flags & RFCOMM_SESSION_RFC)
  348                                 break;
  349 
  350                         if (dlc->rd_rmodem & RFCOMM_MSC_FC)
  351                                 break;
  352 
  353                         if (dlc->rd_pending > RFCOMM_CREDITS_DEFAULT)
  354                                 break;
  355                 }
  356 
  357                 if (dlc->rd_txbuf == NULL)
  358                         len = 0;
  359 
  360                 if (len == 0) {
  361                         if (credits == 0)
  362                                 break;
  363 
  364                         /*
  365                          * No need to send small numbers of credits on their
  366                          * own unless the other end hasn't many left.
  367                          */
  368                         if (credits < RFCOMM_CREDITS_DEFAULT
  369                             && dlc->rd_rxcred > RFCOMM_CREDITS_DEFAULT)
  370                                 break;
  371 
  372                         m = NULL;
  373                 } else {
  374                         /*
  375                          * take what data we can from (front of) txbuf
  376                          */
  377                         m = dlc->rd_txbuf;
  378                         if (len < m->m_pkthdr.len) {
  379                                 dlc->rd_txbuf = m_split(m, len, M_DONTWAIT);
  380                                 if (dlc->rd_txbuf == NULL) {
  381                                         dlc->rd_txbuf = m;
  382                                         break;
  383                                 }
  384                         } else {
  385                                 dlc->rd_txbuf = NULL;
  386                                 len = m->m_pkthdr.len;
  387                         }
  388                 }
  389 
  390                 DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n",
  391                         dlc->rd_dlci, len, credits, dlc->rd_rxcred);
  392 
  393                 if (rfcomm_session_send_uih(rs, dlc, credits, m)) {
  394                         printf("%s: lost %d bytes on DLCI %d\n",
  395                                 __func__, len, dlc->rd_dlci);
  396 
  397                         break;
  398                 }
  399 
  400                 dlc->rd_pending++;
  401 
  402                 if (rs->rs_flags & RFCOMM_SESSION_CFC) {
  403                         if (len > 0)
  404                                 dlc->rd_txcred--;
  405 
  406                         if (credits > 0)
  407                                 dlc->rd_rxcred += credits;
  408                 }
  409         }
  410 }

Cache object: a0cb5a044d0af9c26efd66a39c39e05c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.