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