• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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 module contains functions for port emulation entity and RFCOMM
22  *  communications
23  *
24  ******************************************************************************/
25 #include <base/logging.h>
26 #include <string.h>
27 
28 #include "osi/include/mutex.h"
29 #include "osi/include/osi.h"
30 
31 #include "bt_common.h"
32 #include "bt_target.h"
33 #include "bt_utils.h"
34 #include "btm_api.h"
35 #include "port_api.h"
36 #include "port_int.h"
37 #include "rfc_int.h"
38 #include "rfcdefs.h"
39 
40 /*
41  * Local function definitions
42 */
43 uint32_t port_rfc_send_tx_data(tPORT* p_port);
44 void port_rfc_closed(tPORT* p_port, uint8_t res);
45 void port_get_credits(tPORT* p_port, uint8_t k);
46 
47 /*******************************************************************************
48  *
49  * Function         port_open_continue
50  *
51  * Description      This function is called after security manager completes
52  *                  required security checks.
53  *
54  * Returns          void
55  *
56  ******************************************************************************/
port_open_continue(tPORT * p_port)57 int port_open_continue(tPORT* p_port) {
58   RFCOMM_TRACE_EVENT("port_open_continue, p_port:%p", p_port);
59 
60   /* Check if multiplexer channel has already been established */
61   tRFC_MCB* p_mcb = rfc_alloc_multiplexer_channel(p_port->bd_addr, true);
62   if (p_mcb == nullptr) {
63     RFCOMM_TRACE_WARNING("port_open_continue no mx channel");
64     port_release_port(p_port);
65     return (PORT_NO_RESOURCES);
66   }
67 
68   p_port->rfc.p_mcb = p_mcb;
69 
70   p_mcb->port_handles[p_port->dlci] = p_port->handle;
71 
72   /* Connection is up and we know local and remote features, select MTU */
73   port_select_mtu(p_port);
74 
75   if (p_mcb->state == RFC_MX_STATE_CONNECTED) {
76     RFCOMM_ParameterNegotiationRequest(p_mcb, p_port->dlci, p_port->mtu);
77   } else if ((p_mcb->state == RFC_MX_STATE_IDLE) ||
78              (p_mcb->state == RFC_MX_STATE_DISC_WAIT_UA)) {
79     // In RFC_MX_STATE_IDLE state, MX state machine will create connection
80     // In RFC_MX_STATE_DISC_WAIT_UA state, MX state machine will recreate
81     // connection after disconnecting is completed
82     RFCOMM_StartReq(p_mcb);
83   } else {
84     // MX state machine ignores RFC_MX_EVENT_START_REQ in these states
85     // When it enters RFC_MX_STATE_CONNECTED, it will check any openning ports
86     RFCOMM_TRACE_DEBUG(
87         "port_open_continue: mx state(%d) mx channel is openning",
88         p_mcb->state);
89   }
90   return (PORT_SUCCESS);
91 }
92 
93 /*******************************************************************************
94  *
95  * Function         port_start_control
96  *
97  * Description      This function is called in the BTU_TASK context to
98  *                  send control information
99  *
100  * Returns          void
101  *
102  ******************************************************************************/
port_start_control(tPORT * p_port)103 void port_start_control(tPORT* p_port) {
104   tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
105 
106   if (p_mcb == NULL) return;
107 
108   RFCOMM_ControlReq(p_mcb, p_port->dlci, &p_port->local_ctrl);
109 }
110 
111 /*******************************************************************************
112  *
113  * Function         port_start_par_neg
114  *
115  * Description      This function is called in the BTU_TASK context to
116  *                  send configuration information
117  *
118  * Returns          void
119  *
120  ******************************************************************************/
port_start_par_neg(tPORT * p_port)121 void port_start_par_neg(tPORT* p_port) {
122   tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
123 
124   if (p_mcb == NULL) return;
125 
126   RFCOMM_PortParameterNegotiationRequest(p_mcb, p_port->dlci,
127                                          &p_port->user_port_pars);
128 }
129 
130 /*******************************************************************************
131  *
132  * Function         port_start_close
133  *
134  * Description      This function is called in the BTU_TASK context to
135  *                  release DLC
136  *
137  * Returns          void
138  *
139  ******************************************************************************/
port_start_close(tPORT * p_port)140 void port_start_close(tPORT* p_port) {
141   tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
142   uint8_t old_signals;
143   uint32_t events = 0;
144 
145   /* At first indicate to the user that signals on the connection were dropped
146    */
147   p_port->line_status |= LINE_STATUS_FAILED;
148   old_signals = p_port->peer_ctrl.modem_signal;
149 
150   p_port->peer_ctrl.modem_signal &=
151       ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
152 
153   events |= port_get_signal_changes(p_port, old_signals,
154                                     p_port->peer_ctrl.modem_signal);
155 
156   if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
157 
158   if (p_port->ev_mask & PORT_EV_ERR) events |= PORT_EV_ERR;
159 
160   if ((p_port->p_callback != NULL) && events)
161     p_port->p_callback(events, p_port->handle);
162 
163   /* Check if RFCOMM side has been closed while the message was queued */
164   if ((p_mcb == NULL) || (p_port->rfc.state == RFC_STATE_CLOSED)) {
165     /* Call management callback function before calling port_release_port() to
166      * clear tPort */
167     if (p_port->p_mgmt_callback)
168       p_port->p_mgmt_callback(PORT_CLOSED, p_port->handle);
169 
170     port_release_port(p_port);
171   } else {
172     RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
173   }
174 }
175 
176 /*******************************************************************************
177  *
178  * Function         PORT_StartCnf
179  *
180  * Description      This function is called from the RFCOMM layer when
181  *                  establishing of the multiplexer channel is completed.
182  *                  Continue establishing of the connection for all ports that
183  *                  are in the OPENING state
184  *
185  ******************************************************************************/
PORT_StartCnf(tRFC_MCB * p_mcb,uint16_t result)186 void PORT_StartCnf(tRFC_MCB* p_mcb, uint16_t result) {
187   bool no_ports_up = true;
188 
189   RFCOMM_TRACE_EVENT("%s: result %d", __func__, result);
190 
191   tPORT* p_port = &rfc_cb.port.port[0];
192   for (int i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
193     if (p_port->rfc.p_mcb == p_mcb) {
194       no_ports_up = false;
195 
196       if (result == RFCOMM_SUCCESS) {
197         RFCOMM_TRACE_EVENT("%s: dlci %d", __func__, p_port->dlci);
198         RFCOMM_ParameterNegotiationRequest(p_mcb, p_port->dlci, p_port->mtu);
199       } else {
200         RFCOMM_TRACE_WARNING("%s: failed result:%d", __func__, result);
201 
202         /* Warning: result is also set to 4 when l2cap connection
203            fails due to l2cap connect cnf (no_resources) */
204         if (result == HCI_ERR_PAGE_TIMEOUT) {
205           p_port->error = PORT_PAGE_TIMEOUT;
206         } else {
207           p_port->error = PORT_START_FAILED;
208         }
209 
210         rfc_release_multiplexer_channel(p_mcb);
211 
212         /* Send event to the application */
213         if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECT_ERR)) {
214           (p_port->p_callback)(PORT_EV_CONNECT_ERR, p_port->handle);
215         }
216 
217         if (p_port->p_mgmt_callback) {
218           p_port->p_mgmt_callback(PORT_START_FAILED, p_port->handle);
219         }
220 
221         port_release_port(p_port);
222       }
223     }
224   }
225 
226   /* There can be a situation when after starting connection, user closes the */
227   /* port, we can catch it here to close multiplexor channel */
228   if (no_ports_up) {
229     rfc_check_mcb_active(p_mcb);
230   }
231 }
232 
233 /*******************************************************************************
234  *
235  * Function         PORT_StartInd
236  *
237  * Description      This function is called from the RFCOMM layer when
238  *                  some peer device wants to establish a multiplexer
239  *                  connection.  Check if there are any ports open with this
240  *                  or not assigned multiplexer.
241  *
242  ******************************************************************************/
PORT_StartInd(tRFC_MCB * p_mcb)243 void PORT_StartInd(tRFC_MCB* p_mcb) {
244   tPORT* p_port;
245   int i;
246 
247   RFCOMM_TRACE_EVENT("PORT_StartInd");
248 
249   p_port = &rfc_cb.port.port[0];
250   for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
251     if ((p_port->rfc.p_mcb == NULL) || (p_port->rfc.p_mcb == p_mcb)) {
252       RFCOMM_TRACE_DEBUG(
253           "PORT_StartInd, RFCOMM_StartRsp RFCOMM_SUCCESS: p_mcb:%p", p_mcb);
254       RFCOMM_StartRsp(p_mcb, RFCOMM_SUCCESS);
255       return;
256     }
257   }
258   RFCOMM_StartRsp(p_mcb, RFCOMM_ERROR);
259 }
260 
261 /*******************************************************************************
262  *
263  * Function         PORT_ParNegInd
264  *
265  * Description      This function is called from the RFCOMM layer to change
266  *                  DLCI parameters (currently only MTU is negotiated).
267  *                  If can not find the port do not accept the request.
268  *                  Otherwise save the MTU size supported by the peer.
269  *
270  ******************************************************************************/
PORT_ParNegInd(tRFC_MCB * p_mcb,uint8_t dlci,uint16_t mtu,uint8_t cl,uint8_t k)271 void PORT_ParNegInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
272                     uint8_t k) {
273   RFCOMM_TRACE_EVENT("%s: bd_addr=%s, dlci=%d, mtu=%d", __func__,
274                      p_mcb->bd_addr.ToString().c_str(), dlci, mtu);
275   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
276   if (!p_port) {
277     /* This can be a first request for this port */
278     p_port = port_find_dlci_port(dlci);
279     if (!p_port) {
280       LOG(ERROR) << __func__ << ": Disconnect RFCOMM, port not found, dlci="
281                  << std::to_string(dlci) << ", p_mcb=" << p_mcb
282                  << ", bd_addr=" << p_mcb->bd_addr;
283       /* If the port cannot be opened, send a DM.  Per Errata 1205 */
284       rfc_send_dm(p_mcb, dlci, false);
285       /* check if this is the last port open, some headsets have
286       problem, they don't disconnect if we send DM */
287       rfc_check_mcb_active(p_mcb);
288       return;
289     }
290     RFCOMM_TRACE_EVENT("%s: port_handles[dlci:%d]:%d->%d", __func__, dlci,
291                        p_mcb->port_handles[dlci], p_port->handle);
292     p_mcb->port_handles[dlci] = p_port->handle;
293   }
294 
295   p_port->bd_addr = p_mcb->bd_addr;
296 
297   /* Connection is up and we know local and remote features, select MTU */
298   port_select_mtu(p_port);
299 
300   p_port->rfc.p_mcb = p_mcb;
301   p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
302   p_port->peer_mtu = p_port->mtu;
303 
304   /* Negotiate the flow control mechanism.  If flow control mechanism for */
305   /* mux has not been set yet, set it now.  If either we or peer wants TS 07.10,
306    */
307   /* use that.  Otherwise both must want credit based, so use that. If flow is
308    */
309   /* already defined for this mux, we respond with that value. */
310   if (p_mcb->flow == PORT_FC_UNDEFINED) {
311     if ((PORT_FC_DEFAULT == PORT_FC_TS710) ||
312         (cl == RFCOMM_PN_CONV_LAYER_TYPE_1)) {
313       p_mcb->flow = PORT_FC_TS710;
314     } else {
315       p_mcb->flow = PORT_FC_CREDIT;
316     }
317   }
318 
319   /* Regardless of our flow control mechanism, if the PN cl is zero, we must */
320   /* respond with zero.  "A responding implementation must set this field to 14
321    */
322   /* if (and only if) the PN request was 15."  This could happen if a PN is sent
323    */
324   /* after the DLCI is already established-- the PN in that case must have cl =
325    * 0. */
326   /* See RFCOMM spec 5.5.3 */
327   uint8_t our_cl;
328   uint8_t our_k;
329   if (cl == RFCOMM_PN_CONV_LAYER_TYPE_1) {
330     our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
331     our_k = 0;
332   } else if (p_mcb->flow == PORT_FC_CREDIT) {
333     /* get credits */
334     port_get_credits(p_port, k);
335 
336     /* Set convergence layer and number of credits (k) */
337     our_cl = RFCOMM_PN_CONV_LAYER_CBFC_R;
338     our_k = (p_port->credit_rx_max < RFCOMM_K_MAX) ? p_port->credit_rx_max
339                                                    : RFCOMM_K_MAX;
340     p_port->credit_rx = our_k;
341   } else {
342     /* must not be using credit based flow control; use TS 7.10 */
343     our_cl = RFCOMM_PN_CONV_LAYER_TYPE_1;
344     our_k = 0;
345   }
346   RFCOMM_ParameterNegotiationResponse(p_mcb, dlci, p_port->mtu, our_cl, our_k);
347 }
348 
349 /*******************************************************************************
350  *
351  * Function         PORT_ParNegCnf
352  *
353  * Description      This function is called from the RFCOMM layer to change
354  *                  DLCI parameters (currently only MTU is negotiated).
355  *                  Save the MTU size supported by the peer.
356  *                  If the confirmation is received during the port opening
357  *                  procedure send EstablishRequest to continue.
358  *
359  ******************************************************************************/
PORT_ParNegCnf(tRFC_MCB * p_mcb,uint8_t dlci,uint16_t mtu,uint8_t cl,uint8_t k)360 void PORT_ParNegCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu, uint8_t cl,
361                     uint8_t k) {
362   RFCOMM_TRACE_EVENT("PORT_ParNegCnf dlci:%d mtu:%d cl: %d k: %d", dlci, mtu,
363                      cl, k);
364   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
365   if (!p_port) {
366     LOG(WARNING) << __func__ << ": port is null for " << p_mcb->bd_addr;
367     return;
368   }
369 
370   /* Flow control mechanism not set yet.  Negotiate flow control mechanism. */
371   if (p_mcb->flow == PORT_FC_UNDEFINED) {
372     /* Our stack is configured for TS07.10 and they responded with credit-based.
373      */
374     /* This is illegal-- negotiation fails. */
375     if ((PORT_FC_DEFAULT == PORT_FC_TS710) &&
376         (cl == RFCOMM_PN_CONV_LAYER_CBFC_R)) {
377       RFCOMM_TRACE_WARNING("%s, negotiation fails, index=%d", __func__,
378                            p_port->handle);
379       rfc_send_disc(p_mcb, p_port->dlci);
380       rfc_port_closed(p_port);
381       return;
382     } else if (cl == RFCOMM_PN_CONV_LAYER_CBFC_R) {
383       // Our stack is configured for credit-based and they responded with
384       // credit-based.
385       p_mcb->flow = PORT_FC_CREDIT;
386     } else {
387       // They responded with any other value.  Treat this as negotiation to
388       // TS07.10.
389       p_mcb->flow = PORT_FC_TS710;
390     }
391   }
392   /* If mux flow control mechanism set, we honor that setting regardless of */
393   /* the CL value in their response.  This allows us to gracefully accept any */
394   /* illegal PN negotiation scenarios. */
395 
396   p_port->mtu = (p_port->mtu < mtu) ? p_port->mtu : mtu;
397   p_port->peer_mtu = p_port->mtu;
398 
399   if (p_mcb->flow == PORT_FC_CREDIT) {
400     port_get_credits(p_port, k);
401   }
402 
403   if (p_port->state == PORT_STATE_OPENING)
404     RFCOMM_DlcEstablishReq(p_mcb, p_port->dlci, p_port->mtu);
405 }
406 
407 /*******************************************************************************
408  *
409  * Function         PORT_DlcEstablishInd
410  *
411  * Description      This function is called from the RFCOMM layer when peer
412  *                  device wants to establish a new DLC.  If this is not the
413  *                  first message in the establishment procedure port_handle
414  *                  has a handle to the port control block otherwise the control
415  *                  block should be found based on the muliplexer channel and
416  *                  dlci.  The block should be allocated allocated before
417  *                  meaning that application already made open.
418  *
419  ******************************************************************************/
PORT_DlcEstablishInd(tRFC_MCB * p_mcb,uint8_t dlci,uint16_t mtu)420 void PORT_DlcEstablishInd(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu) {
421   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
422 
423   RFCOMM_TRACE_DEBUG(
424       "PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci,
425       mtu, p_port);
426   VLOG(1) << __func__ << " p_mcb addr:" << p_mcb->bd_addr;
427 
428   if (!p_port) {
429     /* This can be a first request for this port */
430     p_port = port_find_dlci_port(dlci);
431     if (!p_port) {
432       RFCOMM_DlcEstablishRsp(p_mcb, dlci, 0, RFCOMM_ERROR);
433       return;
434     }
435     p_mcb->port_handles[dlci] = p_port->handle;
436   }
437 
438   /* If L2CAP's mtu less then RFCOMM's take it */
439   if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
440 
441   /* If there was an inactivity timer running for MCB stop it */
442   rfc_timer_stop(p_mcb);
443 
444   RFCOMM_DlcEstablishRsp(p_mcb, dlci, p_port->mtu, RFCOMM_SUCCESS);
445 
446   /* This is the server side.  If application wants to know when connection */
447   /* is established, thats the place */
448   if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
449     (p_port->p_callback)(PORT_EV_CONNECTED, p_port->handle);
450 
451   if (p_port->p_mgmt_callback)
452     p_port->p_mgmt_callback(PORT_SUCCESS, p_port->handle);
453 
454   p_port->state = PORT_STATE_OPENED;
455 }
456 
457 /*******************************************************************************
458  *
459  * Function         PORT_DlcEstablishCnf
460  *
461  * Description      This function is called from the RFCOMM layer when peer
462  *                  acknowledges establish procedure (SABME/UA).  Send reply
463  *                  to the user and set state to OPENED if result was
464  *                  successfull.
465  *
466  ******************************************************************************/
PORT_DlcEstablishCnf(tRFC_MCB * p_mcb,uint8_t dlci,uint16_t mtu,uint16_t result)467 void PORT_DlcEstablishCnf(tRFC_MCB* p_mcb, uint8_t dlci, uint16_t mtu,
468                           uint16_t result) {
469   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
470 
471   RFCOMM_TRACE_EVENT("PORT_DlcEstablishCnf dlci:%d mtu:%d result:%d", dlci, mtu,
472                      result);
473 
474   if (!p_port) return;
475 
476   if (result != RFCOMM_SUCCESS) {
477     p_port->error = PORT_START_FAILED;
478     port_rfc_closed(p_port, PORT_START_FAILED);
479     return;
480   }
481 
482   /* If L2CAP's mtu less then RFCOMM's take it */
483   if (mtu && (mtu < p_port->peer_mtu)) p_port->peer_mtu = mtu;
484 
485   /* If there was an inactivity timer running for MCB stop it */
486   rfc_timer_stop(p_mcb);
487 
488   if (p_port->p_callback && (p_port->ev_mask & PORT_EV_CONNECTED))
489     (p_port->p_callback)(PORT_EV_CONNECTED, p_port->handle);
490 
491   if (p_port->p_mgmt_callback)
492     p_port->p_mgmt_callback(PORT_SUCCESS, p_port->handle);
493 
494   p_port->state = PORT_STATE_OPENED;
495 
496   /* RPN is required only if we want to tell DTE how the port should be opened
497    */
498   if ((p_port->uuid == UUID_SERVCLASS_DIALUP_NETWORKING) ||
499       (p_port->uuid == UUID_SERVCLASS_FAX))
500     RFCOMM_PortParameterNegotiationRequest(p_port->rfc.p_mcb, p_port->dlci,
501                                            NULL);
502   else
503     RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
504 }
505 
506 /*******************************************************************************
507  *
508  * Function         PORT_PortNegInd
509  *
510  * Description      This function is called from the RFCOMM layer when peer
511  *                  device wants to set parameters of the port.  As per the spec
512  *                  this message has to be sent before the first data packet
513  *                  and can be sent before establish.  The block should be
514  *                  allocated before meaning that application already made open.
515  *
516  ******************************************************************************/
PORT_PortNegInd(tRFC_MCB * p_mcb,uint8_t dlci,tPORT_STATE * p_pars,uint16_t param_mask)517 void PORT_PortNegInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_STATE* p_pars,
518                      uint16_t param_mask) {
519   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
520 
521   RFCOMM_TRACE_EVENT("PORT_PortNegInd");
522 
523   if (!p_port) {
524     /* This can be a first request for this port */
525     p_port = port_find_dlci_port(dlci);
526     if (!p_port) {
527       RFCOMM_PortParameterNegotiationResponse(p_mcb, dlci, p_pars, 0);
528       return;
529     }
530     p_mcb->port_handles[dlci] = p_port->handle;
531   }
532 
533   /* Check if the flow control is acceptable on local side */
534   p_port->peer_port_pars = *p_pars;
535   RFCOMM_PortParameterNegotiationResponse(p_mcb, dlci, p_pars, param_mask);
536 }
537 
538 /*******************************************************************************
539  *
540  * Function         PORT_PortNegCnf
541  *
542  * Description      This function is called from the RFCOMM layer to change
543  *                  state for the port.  Propagate change to the user.
544  *
545  ******************************************************************************/
PORT_PortNegCnf(tRFC_MCB * p_mcb,uint8_t dlci,UNUSED_ATTR tPORT_STATE * p_pars,uint16_t result)546 void PORT_PortNegCnf(tRFC_MCB* p_mcb, uint8_t dlci,
547                      UNUSED_ATTR tPORT_STATE* p_pars, uint16_t result) {
548   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
549 
550   RFCOMM_TRACE_EVENT("PORT_PortNegCnf");
551 
552   if (!p_port) {
553     RFCOMM_TRACE_WARNING("PORT_PortNegCnf no port");
554     return;
555   }
556   /* Port negotiation failed. Drop the connection */
557   if (result != RFCOMM_SUCCESS) {
558     p_port->error = PORT_PORT_NEG_FAILED;
559 
560     RFCOMM_DlcReleaseReq(p_mcb, p_port->dlci);
561 
562     port_rfc_closed(p_port, PORT_PORT_NEG_FAILED);
563     return;
564   }
565 
566   if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
567     RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
568   } else {
569     RFCOMM_TRACE_WARNING("PORT_PortNegCnf Control Already sent");
570   }
571 }
572 
573 /*******************************************************************************
574  *
575  * Function         PORT_ControlInd
576  *
577  * Description      This function is called from the RFCOMM layer on the modem
578  *                  signal change.  Propagate change to the user.
579  *
580  ******************************************************************************/
PORT_ControlInd(tRFC_MCB * p_mcb,uint8_t dlci,tPORT_CTRL * p_pars)581 void PORT_ControlInd(tRFC_MCB* p_mcb, uint8_t dlci, tPORT_CTRL* p_pars) {
582   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
583   uint32_t event;
584   uint8_t old_signals;
585 
586   RFCOMM_TRACE_EVENT("PORT_ControlInd");
587 
588   if (!p_port) return;
589 
590   old_signals = p_port->peer_ctrl.modem_signal;
591 
592   event = port_get_signal_changes(p_port, old_signals, p_pars->modem_signal);
593 
594   p_port->peer_ctrl = *p_pars;
595 
596   if (!(p_port->port_ctrl & PORT_CTRL_REQ_SENT)) {
597     RFCOMM_ControlReq(p_port->rfc.p_mcb, p_port->dlci, &p_port->local_ctrl);
598   } else {
599     /* If this is the first time we received control RFCOMM is connected */
600     if (!(p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)) {
601       event |= (PORT_EV_CONNECTED & p_port->ev_mask);
602     }
603 
604     if (p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED) {
605       event |= port_rfc_send_tx_data(p_port);
606     }
607   }
608 
609   p_port->port_ctrl |= (PORT_CTRL_IND_RECEIVED | PORT_CTRL_IND_RESPONDED);
610 
611   if (p_pars->break_signal) event |= (PORT_EV_BREAK & p_port->ev_mask);
612 
613   /* execute call back function only if the application is registered for events
614    */
615   if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->handle);
616 
617   RFCOMM_TRACE_EVENT(
618       "PORT_ControlInd DTR_DSR : %d, RTS_CTS : %d, RI : %d, DCD : %d",
619       ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DTRDSR) ? 1 : 0),
620       ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RTSCTS) ? 1 : 0),
621       ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_RI) ? 1 : 0),
622       ((p_port->peer_ctrl.modem_signal & MODEM_SIGNAL_DCD) ? 1 : 0));
623 }
624 
625 /*******************************************************************************
626  *
627  * Function         PORT_ControlCnf
628  *
629  * Description      This function is called from the RFCOMM layer when
630  *                  peer acknowleges change of the modem signals.
631  *
632  ******************************************************************************/
PORT_ControlCnf(tRFC_MCB * p_mcb,uint8_t dlci,UNUSED_ATTR tPORT_CTRL * p_pars)633 void PORT_ControlCnf(tRFC_MCB* p_mcb, uint8_t dlci,
634                      UNUSED_ATTR tPORT_CTRL* p_pars) {
635   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
636   uint32_t event = 0;
637 
638   RFCOMM_TRACE_EVENT("PORT_ControlCnf");
639 
640   if (!p_port) return;
641 
642   if (!(p_port->port_ctrl & PORT_CTRL_REQ_CONFIRMED)) {
643     p_port->port_ctrl |= PORT_CTRL_REQ_CONFIRMED;
644 
645     if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED)
646       event = (p_port->ev_mask & PORT_EV_CONNECTED);
647   }
648 
649   if (p_port->port_ctrl & PORT_CTRL_IND_RECEIVED) {
650     event |= port_rfc_send_tx_data(p_port);
651   }
652 
653   /* execute call back function only if the application is registered for events
654    */
655   if (event && p_port->p_callback) (p_port->p_callback)(event, p_port->handle);
656 }
657 
658 /*******************************************************************************
659  *
660  * Function         PORT_LineStatusInd
661  *
662  * Description      This function is called from the RFCOMM layer when
663  *                  peer indicates change in the line status
664  *
665  ******************************************************************************/
PORT_LineStatusInd(tRFC_MCB * p_mcb,uint8_t dlci,uint8_t line_status)666 void PORT_LineStatusInd(tRFC_MCB* p_mcb, uint8_t dlci, uint8_t line_status) {
667   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
668   uint32_t event = 0;
669 
670   RFCOMM_TRACE_EVENT("PORT_LineStatusInd");
671 
672   if (!p_port) return;
673 
674   p_port->line_status |= line_status;
675 
676   if (line_status & PORT_ERR_OVERRUN) event |= PORT_EV_OVERRUN;
677 
678   if (line_status & PORT_ERR_BREAK) event |= PORT_EV_BREAK;
679 
680   if (line_status & ~(PORT_ERR_OVERRUN | PORT_ERR_BREAK)) event |= PORT_EV_ERR;
681 
682   if ((p_port->p_callback != NULL) && (p_port->ev_mask & event))
683     p_port->p_callback((p_port->ev_mask & event), p_port->handle);
684 }
685 
686 /*******************************************************************************
687  *
688  * Function         PORT_DlcReleaseInd
689  *
690  * Description      This function is called from the RFCOMM layer when
691  *                  DLC connection is released.
692  *
693  ******************************************************************************/
PORT_DlcReleaseInd(tRFC_MCB * p_mcb,uint8_t dlci)694 void PORT_DlcReleaseInd(tRFC_MCB* p_mcb, uint8_t dlci) {
695   VLOG(1) << __func__ << ": dlci=" << std::to_string(dlci)
696           << ", bd_addr=" << p_mcb->bd_addr;
697   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
698   if (!p_port) return;
699   port_rfc_closed(p_port, PORT_CLOSED);
700 }
701 
702 /*******************************************************************************
703  *
704  * Function         PORT_CloseInd
705  *
706  * Description      This function is called from the RFCOMM layer when
707  *                  multiplexer connection is released.
708  *
709  ******************************************************************************/
PORT_CloseInd(tRFC_MCB * p_mcb)710 void PORT_CloseInd(tRFC_MCB* p_mcb) {
711   tPORT* p_port;
712   int i;
713 
714   RFCOMM_TRACE_EVENT("PORT_CloseInd");
715 
716   p_port = &rfc_cb.port.port[0];
717   for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
718     if (p_port->rfc.p_mcb == p_mcb) {
719       port_rfc_closed(p_port, PORT_PEER_CONNECTION_FAILED);
720     }
721   }
722   rfc_release_multiplexer_channel(p_mcb);
723 }
724 
725 /*******************************************************************************
726  *
727  * Function         Port_TimeOutCloseMux
728  *
729  * Description      This function is called when RFCOMM timesout on a command
730  *                  as a result multiplexer connection is closed.
731  *
732  ******************************************************************************/
Port_TimeOutCloseMux(tRFC_MCB * p_mcb)733 void Port_TimeOutCloseMux(tRFC_MCB* p_mcb) {
734   tPORT* p_port;
735   int i;
736 
737   RFCOMM_TRACE_EVENT("Port_TimeOutCloseMux");
738 
739   p_port = &rfc_cb.port.port[0];
740   for (i = 0; i < MAX_RFC_PORTS; i++, p_port++) {
741     if (p_port->rfc.p_mcb == p_mcb) {
742       port_rfc_closed(p_port, PORT_PEER_TIMEOUT);
743     }
744   }
745 }
746 
747 /*******************************************************************************
748  *
749  * Function         PORT_DataInd
750  *
751  * Description      This function is called from the RFCOMM layer when data
752  *                  buffer is received from the peer.
753  *
754  ******************************************************************************/
PORT_DataInd(tRFC_MCB * p_mcb,uint8_t dlci,BT_HDR * p_buf)755 void PORT_DataInd(tRFC_MCB* p_mcb, uint8_t dlci, BT_HDR* p_buf) {
756   tPORT* p_port = port_find_mcb_dlci_port(p_mcb, dlci);
757   uint8_t rx_char1;
758   uint32_t events = 0;
759   uint8_t* p;
760   int i;
761 
762   RFCOMM_TRACE_EVENT(
763       "PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
764       p_buf->len, p_mcb, p_port, dlci);
765   if (!p_port) {
766     osi_free(p_buf);
767     return;
768   }
769   /* If client registered callout callback with flow control we can just deliver
770    * receive data */
771   if (p_port->p_data_co_callback) {
772     /* Another packet is delivered to user.  Send credits to peer if required */
773     if (p_port->p_data_co_callback(p_port->handle, (uint8_t*)p_buf, -1,
774                                    DATA_CO_CALLBACK_TYPE_INCOMING)) {
775       port_flow_control_peer(p_port, true, 1);
776     } else {
777       port_flow_control_peer(p_port, false, 0);
778     }
779     // osi_free(p_buf);
780     return;
781   }
782   /* If client registered callback we can just deliver receive data */
783   if (p_port->p_data_callback) {
784     /* Another packet is delivered to user.  Send credits to peer if required */
785     port_flow_control_peer(p_port, true, 1);
786     p_port->p_data_callback(p_port->handle,
787                             (uint8_t*)(p_buf + 1) + p_buf->offset, p_buf->len);
788     osi_free(p_buf);
789     return;
790   }
791   /* Check if rx queue exceeds the limit */
792   if ((p_port->rx.queue_size + p_buf->len > PORT_RX_CRITICAL_WM) ||
793       (fixed_queue_length(p_port->rx.queue) + 1 > p_port->rx_buf_critical)) {
794     RFCOMM_TRACE_EVENT("PORT_DataInd. Buffer over run. Dropping the buffer");
795     osi_free(p_buf);
796     RFCOMM_LineStatusReq(p_mcb, dlci, LINE_STATUS_OVERRUN);
797     return;
798   }
799   /* If user registered to receive notification when a particular byte is */
800   /* received we mast check all received bytes */
801   if (((rx_char1 = p_port->user_port_pars.rx_char1) != 0) &&
802       (p_port->ev_mask & PORT_EV_RXFLAG)) {
803     for (i = 0, p = (uint8_t*)(p_buf + 1) + p_buf->offset; i < p_buf->len;
804          i++) {
805       if (*p++ == rx_char1) {
806         events |= PORT_EV_RXFLAG;
807         break;
808       }
809     }
810   }
811 
812   mutex_global_lock();
813 
814   fixed_queue_enqueue(p_port->rx.queue, p_buf);
815   p_port->rx.queue_size += p_buf->len;
816 
817   mutex_global_unlock();
818 
819   /* perform flow control procedures if necessary */
820   port_flow_control_peer(p_port, false, 0);
821 
822   /* If user indicated flow control can not deliver any notifications to them */
823   if (p_port->rx.user_fc) {
824     if (events & PORT_EV_RXFLAG) {
825       p_port->rx_flag_ev_pending = true;
826     }
827     return;
828   }
829 
830   events |= PORT_EV_RXCHAR;
831 
832   /* Mask out all events that are not of interest to user */
833   events &= p_port->ev_mask;
834 
835   if (p_port->p_callback && events) p_port->p_callback(events, p_port->handle);
836 }
837 
838 /*******************************************************************************
839  *
840  * Function         PORT_FlowInd
841  *
842  * Description      This function is called from the RFCOMM layer on the flow
843  *                  control signal change.  Propagate change to the user.
844  *
845  ******************************************************************************/
PORT_FlowInd(tRFC_MCB * p_mcb,uint8_t dlci,bool enable_data)846 void PORT_FlowInd(tRFC_MCB* p_mcb, uint8_t dlci, bool enable_data) {
847   tPORT* p_port = (tPORT*)NULL;
848   uint32_t events = 0;
849   int i;
850 
851   RFCOMM_TRACE_EVENT("PORT_FlowInd fc:%d", enable_data);
852 
853   if (dlci == 0) {
854     p_mcb->peer_ready = enable_data;
855   } else {
856     p_port = port_find_mcb_dlci_port(p_mcb, dlci);
857     if (p_port == NULL) return;
858 
859     p_port->tx.peer_fc = !enable_data;
860   }
861 
862   for (i = 0; i < MAX_RFC_PORTS; i++) {
863     /* If DLCI is 0 event applies to all ports */
864     if (dlci == 0) {
865       p_port = &rfc_cb.port.port[i];
866       if (!p_port->in_use || (p_port->rfc.p_mcb != p_mcb) ||
867           (p_port->rfc.state != RFC_STATE_OPENED))
868         continue;
869     }
870     events = 0;
871 
872     /* Check if flow of data is still enabled */
873     events |= port_flow_control_user(p_port);
874 
875     /* Check if data can be sent and send it */
876     events |= port_rfc_send_tx_data(p_port);
877 
878     /* Mask out all events that are not of interest to user */
879     events &= p_port->ev_mask;
880 
881     /* Send event to the application */
882     if (p_port->p_callback && events)
883       (p_port->p_callback)(events, p_port->handle);
884 
885     /* If DLCI is not 0 event applies to one port only */
886     if (dlci != 0) break;
887   }
888 }
889 
890 /*******************************************************************************
891  *
892  * Function         port_rfc_send_tx_data
893  *
894  * Description      This function is when forward data can be sent to the peer
895  *
896  ******************************************************************************/
port_rfc_send_tx_data(tPORT * p_port)897 uint32_t port_rfc_send_tx_data(tPORT* p_port) {
898   uint32_t events = 0;
899   BT_HDR* p_buf;
900 
901   /* if there is data to be sent */
902   if (p_port->tx.queue_size > 0) {
903     /* while the rfcomm peer is not flow controlling us, and peer is ready */
904     while (!p_port->tx.peer_fc && p_port->rfc.p_mcb &&
905            p_port->rfc.p_mcb->peer_ready) {
906       /* get data from tx queue and send it */
907       mutex_global_lock();
908 
909       p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_port->tx.queue);
910       if (p_buf != NULL) {
911         p_port->tx.queue_size -= p_buf->len;
912 
913         mutex_global_unlock();
914 
915         RFCOMM_TRACE_DEBUG("Sending RFCOMM_DataReq tx.queue_size=%d",
916                            p_port->tx.queue_size);
917 
918         RFCOMM_DataReq(p_port->rfc.p_mcb, p_port->dlci, p_buf);
919 
920         events |= PORT_EV_TXCHAR;
921 
922         if (p_port->tx.queue_size == 0) {
923           events |= PORT_EV_TXEMPTY;
924           break;
925         }
926       }
927       /* queue is empty-- all data sent */
928       else {
929         mutex_global_unlock();
930 
931         events |= PORT_EV_TXEMPTY;
932         break;
933       }
934     }
935     /* If we flow controlled user based on the queue size enable data again */
936     events |= port_flow_control_user(p_port);
937   }
938   return (events & p_port->ev_mask);
939 }
940 
941 /*******************************************************************************
942  *
943  * Function         port_rfc_closed
944  *
945  * Description      Called when RFCOMM port is closed
946  *
947  ******************************************************************************/
port_rfc_closed(tPORT * p_port,uint8_t res)948 void port_rfc_closed(tPORT* p_port, uint8_t res) {
949   uint8_t old_signals;
950   uint32_t events = 0;
951   tRFC_MCB* p_mcb = p_port->rfc.p_mcb;
952 
953   if ((p_port->state == PORT_STATE_OPENING) && (p_port->is_server)) {
954     /* The server side was not informed that connection is up, ignore */
955     RFCOMM_TRACE_WARNING("port_rfc_closed in OPENING state ignored");
956 
957     rfc_port_timer_stop(p_port);
958     p_port->rfc.state = RFC_STATE_CLOSED;
959 
960     if (p_mcb) {
961       p_mcb->port_handles[p_port->dlci] = 0;
962 
963       /* If there are no more ports opened on this MCB release it */
964       rfc_check_mcb_active(p_mcb);
965       p_port->rfc.p_mcb = NULL;
966     }
967 
968     /* Need to restore DLCI to listening state
969      * if the server was on the initiating RFC
970      */
971     p_port->dlci &= 0xfe;
972 
973     return;
974   }
975 
976   if ((p_port->state != PORT_STATE_CLOSING) &&
977       (p_port->state != PORT_STATE_CLOSED)) {
978     p_port->line_status |= LINE_STATUS_FAILED;
979 
980     old_signals = p_port->peer_ctrl.modem_signal;
981 
982     p_port->peer_ctrl.modem_signal &=
983         ~(PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
984 
985     events |= port_get_signal_changes(p_port, old_signals,
986                                       p_port->peer_ctrl.modem_signal);
987 
988     if (p_port->ev_mask & PORT_EV_CONNECT_ERR) events |= PORT_EV_CONNECT_ERR;
989   }
990 
991   if ((p_port->p_callback != NULL) && events)
992     p_port->p_callback(events, p_port->handle);
993 
994   if (p_port->p_mgmt_callback) p_port->p_mgmt_callback(res, p_port->handle);
995 
996   p_port->rfc.state = RFC_STATE_CLOSED;
997 
998   LOG(INFO) << __func__ << ": RFCOMM connection closed, index="
999             << std::to_string(p_port->handle)
1000             << ", state=" << std::to_string(p_port->state)
1001             << ", reason=" << PORT_GetResultString(res) << "["
1002             << std::to_string(res) << "], UUID=" << loghex(p_port->uuid)
1003             << ", bd_addr=" << p_port->bd_addr
1004             << ", is_server=" << p_port->is_server;
1005 
1006   port_release_port(p_port);
1007 }
1008 
1009 /*******************************************************************************
1010  *
1011  * Function         port_get_credits
1012  *
1013  * Description      Set initial values for credits.
1014  *                  Adjust max number of rx credits based on negotiated MTU.
1015  *                  Check max allowed num of bytes, max allowed num buffers,
1016  *                  should be less then 255
1017  *
1018  ******************************************************************************/
port_get_credits(tPORT * p_port,uint8_t k)1019 void port_get_credits(tPORT* p_port, uint8_t k) {
1020   p_port->credit_tx = k;
1021   if (p_port->credit_tx == 0) p_port->tx.peer_fc = true;
1022 }
1023